How to calculate state fidelity in Qiskit?

I have a circuit with different structures, now I'm trying to calculate the fidelity between those with the original one. How do I calculate the fidelity? I want also to initialize the state vector by myself. Can someone show how to compute fidelity for the below example?

For example if I have a circuit like below,

q = QuantumRegister(2)
qc = QuantumCircuit(q)
qc.cu1(3*pi/4, q[0], q[1]);
print(qc)


and different structure

q = QuantumRegister(2)
qc = QuantumCircuit(q)
qc.h(q[0]);
qc.h(q[1]);
qc.cu1(3*pi/4, q[0], q[1]);
qc.h(q[0]);
qc.h(q[1]);
print(qc)


There is more information towards the end of the tutorial here but essentially how you do this is you run both circuits on the state_vector simulator and then you can use the function state_fidelity to work out the fidelity between the two states.

The code to do this should look something like this

from qiskit.quantum_info import state_fidelity

# set up qc1 and qc2

backend = Aer.get_backend('statevector_simulator')

sv1 = execute(qc1, backend).result().get_statevector(qc1)
sv2 = execute(qc2, backend).result().get_statevector(qc2)

print(state_fidelity(sv1, sv2))

• I looked at the definition of fidelity in the tutorial: $F(|\psi_1\rangle,\psi_2\rangle) = |\langle\psi_1 \,\text{middle}\, \psi_2\rangle|^2$. What does it mean the $\text{middle}$? I would expect that only inner product is used because in case $|\psi_1\rangle = |\psi_2\rangle$ the $F =|\langle\psi_1| \psi_1\rangle|^2 = ||\, |\psi_1\rangle \,||^2 = 1$ and in case of orthogonal states $F$ is zero. Mar 18, 2020 at 9:47
• I think that might be a typo, feel free to raise that as an issue against the repo! Mar 18, 2020 at 10:04