# Get state vector of a single qubit in a circuit in Qiskit

I have two quantum circuits, and I would like to compare state vector of the first qubit and check if equals, what is the best way to do that in qiskit ?

Let's say I have :

psi = QuantumCircuit(5)
psi.ry(np.pi/4.,0)
psi.x(0)
psi.x(1)

psi2 = QuantumCircuit(5)
psi2.ry(np.pi/4.,0)
psi2.x(0)


I'm looking for a function which returns True when qubit 0 has the same state vector in both circuits.

I tried to get the information from Statevector.from_instruction(psi).data, but I don't know how to extract information independently of other qubits.

Edit: I get the right result with this function :

def QuantumCircuits_Statevectors_AreEquals(QuantumCircuit1, QuantumCircuit2, QubitIndex):

statevector1_arr = np.empty([1,2]).astype(complex)
statevector2_arr = np.empty([1,2]).astype(complex)

QuantumCircuit1.snapshot("qbsnap", qubits=[QubitIndex])
QuantumCircuit2.snapshot("qbsnap", qubits=[QubitIndex])

backend = Aer.get_backend('statevector_simulator')

snapshot1 = execute(QuantumCircuit1, backend).result().data()['snapshots']['statevector']['qbsnap']
snapshot2 = execute(QuantumCircuit2, backend).result().data()['snapshots']['statevector']['qbsnap']

statevector1_arr[0][0] = snapshot1[0][0]
statevector1_arr[0][1] = snapshot1[0][1]

statevector2_arr[0][0] = snapshot2[0][0]
statevector2_arr[0][1] = snapshot2[0][1]

return np.array_equal(statevector1_arr, statevector2_arr)


But maybe a cleaner solution is possible?

• Hello, did you try running your circuit with the statevector_simulator in Aer, and work with the statevector retrieved from the result of the job? – Lena Dec 11 '20 at 13:19
• Hello, no in my test I use this code to get the statevector : Statevector.from_instruction(psy) – user12910 Dec 11 '20 at 13:23
• @user12910 are you looking to know whether only the first qubit, $q_0$, from both circuits will have the same probabilities readout? and not caring about the the other qubits? – KAJ226 Dec 11 '20 at 15:43
• Maybe the fidelity $F(\rho,\sigma)$ is helpful, this is a common criterion assessing the similarity of two state vectors or the trace distance. The book of Nielson and Chuang, Quantum Computation and Quantum Information gives an introduction to these two metrics. Although surely the result of these two should be inherently floating-point numbers, not boolearn values(True and False). – Yitian Wang Dec 28 '20 at 13:35

(figure comes from the cited paper.) In this circuit, $$|\psi\rangle$$ and $$|\phi\rangle$$ are the two states that you want to compare, then the probability that measuring the uppermost qubit(the control qubit) returns a "0" is $$\frac{1}{2}+\frac{1}{2}F(|\psi\rangle,|\phi\rangle)$$, where is the fidelity I mentioned in the comment.