0
$\begingroup$

I'm reading the paper describing the numerical example HHL. The first question related to the Hermitioan-unitary matrix transformation. We can use numpy.linalg.expm to convert Hermitian to unitary matrix - what I did and achieved the result that contradicts to one in paper:

$\begin{pmatrix} 0.51056242-0.79515385j & 0.27532484+0.17678405j \\ 0.27532484+0.17678405j & 0.51056242-0.79515385j \end{pmatrix}$

So, using this matrix, the further result also differs. What did I do wrong?

The second question is related to the result ration of the output vector $\vec{x}$. What the authors write about $\vec{x}$ is that

ratio of $|x_0|^2$ to $|x_1|^2$ is $1:9$.

however the output results show the different ratio: $0.142^2 : 0.361^2 = 1 : 2.54$

The question is resonable: what does the measured ration mean and how to make outcome to be more "true"?

The full code is below:

def qft_dagger(qc, n):
    for qubit in range(n//2): 
        qc.swap(qubit+1, n-qubit)
    for j in range(n):
        for m in range(j):
            qc.cp(np.pi/float(2**(j-m)), m+1, j+1)
        qc.h(j+1)

def qft(qc, n):
    for j in range(n):
        for m in range(j):
            qc.cp(-np.pi/float(2**(j-m)), clock[m], clock[j])
        qc.h(clock[n-j-1])
    for qubit in range(n//2):
        qc.swap(clock[qubit], clock[n-qubit-1])

def simulate(qpe):
    aer_sim = Aer.get_backend('aer_simulator')
    shots = 2048
    t_qpe = transpile(qpe, aer_sim)
    qobj = assemble(t_qpe, shots=shots)
    results = aer_sim.run(qobj).result()
    answer = results.get_counts()
    for k, v in answer.items():
      answer[k] = answer[k] / shots
    return answer

from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister, Parameter
from qiskit.circuit.library import UnitaryGate, CRYGate
from qiskit import Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram
import numpy as np


state = np.array([0, 1])
H = np.array([[1, -1/3], [-1/3, 1]])

U = np.array([[-1+1j, 1+1j], [1+1j, -1+1j]]) / 2

U_gate = UnitaryGate(U, 'U').control(1)

ancilla = QuantumRegister(1, 'ancilla')
clock = QuantumRegister(2, 'clock')
b = QuantumRegister(1, 'b')
classical = ClassicalRegister(2, 'classical')
circuit = QuantumCircuit(ancilla, clock, b, classical)
for q_idx in range(len(clock)):
    circuit.h(clock[q_idx])

circuit.prepare_state(state, b)


for q_idx in range(len(clock)):
    for _ in range(2**q_idx):
        circuit.append(U_gate, [q_idx + 1, b])
circuit.barrier()

qft(circuit, 2)

circuit.cry(np.pi, clock[0], ancilla)
circuit.cry(np.pi/3, clock[1], ancilla)

circuit.measure(ancilla, classical[0])

qft_dagger(circuit, 2)
circuit.barrier()

U = np.linalg.inv(U)
U_gate = UnitaryGate(U, 'U-1').control(1)
for q_idx in range(len(clock)):
    for _ in range(2**(len(clock) - q_idx - 1)):
        circuit.append(U_gate, [len(clock) - q_idx, b])
circuit.barrier()

for q_idx in range(len(clock)):
    circuit.h(clock[q_idx])
circuit.barrier()

circuit.measure(b, classical[1])
measurements = simulate(circuit)

plot_histogram(measurements)

the results are the same as in the paper:

enter image description here

$\endgroup$
2
  • $\begingroup$ When you ask "how can I make the outcome to be more 'true' "? What do you mean by true? Do you mean how can you make the ratio closer to 1:9? if so, then you will have to share your work/code to see where things might be going wrong. $\endgroup$
    – diemilio
    Commented Oct 7 at 11:33
  • $\begingroup$ @diemilio, the question is related not to the code I did, but the results described in paper. In any way I attach the code if it helps :) thanks $\endgroup$ Commented Oct 7 at 11:40

1 Answer 1

1
$\begingroup$

The difference between what you have and what is in the paper is that you are calculating the matrix $U = e^{iA}$, and in the paper they calculate: $ U = e^{iAt},$ with $t=3\pi/4$.

The reason they do this is because they already know what the solution is going to be, so they are basically rescaling the matrix $A$ to given them "nicer" eigenvalues that allow them to cheat in the implementation of the eigenvalue inversion step.

As for why your solution is not giving the 1:9 ratio, it could be that if you are using the same angles as used in the paper for the eigenvalue inversion step but your matrix is $U = e^{iA}$, then your result is obviously going to be incorrect because you haven't accounted for the $3\pi/4$ scaling for the angles.

$\endgroup$
4
  • $\begingroup$ If so, can we say that HHL works if we have to somehow manipulate t without (sic!) knowing $\vec{x}$? Because in reality we do not know this vector. $\endgroup$ Commented Oct 7 at 12:24
  • $\begingroup$ You don't need to know $\vec{x}$ necessarily, but you do need to have some information about the eigenvalues of $A$, and depending on this information you might have to rescale $A$. See this post for more details. $\endgroup$
    – diemilio
    Commented Oct 7 at 12:37
  • 1
    $\begingroup$ In the particular example given in the paper you shared, they didn't really need to rescale the matrix for this reason though. I think this decision has caused a lot of confusion. They did it to simplify the eigenvalue inversion step, but you don't have to do this; you can still run HHL by applying controlled rotations for every possible solution. This approach is obviously very inefficient and it is not how you would do the inversion step in practice, but since this paper is "educational", I feel it would have been better to do this than cheating to pick the right angles needed. $\endgroup$
    – diemilio
    Commented Oct 7 at 12:39
  • $\begingroup$ And one more question: you said "it is not how you would do the inversion step in practice" - could you give a hint on how else can inversion be done? Straightforward measurement and inversion in classical manner? Or any "more quantum" appoach exists? $\endgroup$ Commented Oct 8 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.