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)
psi2 = QuantumCircuit(5)

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?

  • $\begingroup$ 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? $\endgroup$
    – Lena
    Dec 11, 2020 at 13:19
  • $\begingroup$ Hello, no in my test I use this code to get the statevector : Statevector.from_instruction(psy) $\endgroup$
    – user12910
    Dec 11, 2020 at 13:23
  • $\begingroup$ @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? $\endgroup$
    – KAJ226
    Dec 11, 2020 at 15:43
  • $\begingroup$ 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). $\endgroup$ Dec 28, 2020 at 13:35

2 Answers 2


For a boolean-valued result, your code works.

If you are looking for a "more quantum mechanical" approach, then the swap trick best suits you.

The quantum circuit of the swap trick is as follows:

enter image description here

(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.

Since you know how to get the state vector from qiskit directly, it is more efficient and accurate to calculate the fidelity(or the trace distance) of the two state vectors.


To get the state of a single qubit in a system you can trace out all other qubits. That means you only care about the amplitude of your single qubit and discard the information on the rest.

In Qiskit you can use the qiskit.quantum_info.partial_trace function. Here's the code to compare your two states:

import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import partial_trace, Statevector

# define your two states as circuits
psi = QuantumCircuit(5)

psi2 = QuantumCircuit(5)

def first_qubit_state(circuit):
    """Get the statevector for the first qubit, discarding the rest."""
    # get the full statevector of all qubits
    full_statevector = Statevector(circuit)

    # get the density matrix for the first qubit by taking the partial trace
    partial_density_matrix = partial_trace(full_statevector, [1, 2, 3, 4])

    # extract the statevector out of the density matrix
    partial_statevector = np.diagonal(partial_density_matrix)

    return partial_statevector

def compare_first_qubits(circuit1, circuit2):
    """Return True if the statevector of the first qubits is the same."""
    state1 = first_qubit_state(circuit1)
    state2 = first_qubit_state(circuit2)
    return (state1 == state2).all()  # True if all are the same

print('State of first qubit in psi:\n', first_qubit_state(psi))
print('State of first qubit in psi2:\n', first_qubit_state(psi2))
print('Equal?', compare_first_qubits(psi, psi2)

Running this script gives:

State of first qubit in psi:
 [0.14644661+0.j 0.85355339+0.j]
State of first qubit in psi2:
 [0.14644661+0.j 0.85355339+0.j]
Equal? True

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.