2
$\begingroup$

I'm having trouble to encode classical data into quantum state in Qiskit IBM Quantum Lab. Let me explain the problem clearly: For university purpose I have to encode two 4-dimensional vectors with amplitude encoding (i.e using QRAM) and confront them with the fidelity distance. I wrote something like this:

#qram
def encodeVector(circuit,data,i,controls,rotationQubit,ancillaQubits):
#mcry(angolo,controls,target,ancilla)

# |00>
circuit.x(i)
circuit.mcry(np.arcsin(data[0]),controls,rotationQubit,ancillaQubits)
circuit.x(i)

circuit.barrier()
# |01>
circuit.x(i[1])
circuit.mcry(np.arcsin(data[1]),controls,rotationQubit,ancillaQubits)
circuit.x(i[1])

circuit.barrier()
# |10>
circuit.x(i[0])
circuit.mcry(np.arcsin(data[2]),controls,rotationQubit,ancillaQubits)
circuit.x(i[0])

circuit.barrier()
# |11>
circuit.mcry(np.arcsin(data[3]),controls,rotationQubit,ancillaQubits)

I know I need log(4)=2 qbits to encode 4 components of one vector. So the circuit is something like this: 2 qbit |i> for the first vector, 2 qbit |j> for the second one and finally 2 qbit |r> for the rotation. The teta angle can be found by applying arcsin to the components I think. And first of all I need to create registers 00,01,10,11 with an Hadamard Gate on qbit i and j (superposition). Let's post the code:

psi_norm = [1,0,0,0]
phi_norm = [0,0,0,1]
prova = QuantumRegister(1,"p")
i = QuantumRegister(2,"i") #first vector
j = QuantumRegister(2,"j") #second vector
r = QuantumRegister(2,"r") #rotation qbits
b = ClassicalRegister(1,"b") #for measurement
circuit = QuantumCircuit(prova,i,j,r,b)

circuit.h(i)
circuit.h(j)
circuit.barrier()
encodeVector(circuit, psi_norm, i, i[:], r[0],None) #encode first vector

circuit.barrier()
encodeVector(circuit, phi_norm, j, j[:], r[1],None) #encode second vector

Now I calculate the fidelity distance which is the following circuit:

#fidelity
circuit.h(prova[0])
circuit.cswap(prova[0],i[0],j[0])
circuit.cswap(prova[0],i[1],j[1])
circuit.cswap(prova[0],r[0],r[1])
circuit.h(prova[0])
circuit.measure(prova[0],b[0])

The vector are orthogonal so the fidelity must have probabilities equal to 1/2, but in my case I get them wrong and I don't know where I make mistakes. Here the histogram: enter image description here

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.