4
$\begingroup$

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?

$\endgroup$
  • $\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 '20 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 '20 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 '20 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$ – Yitian Wang Dec 28 '20 at 13:35
0
$\begingroup$

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.

$\endgroup$

Your Answer

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

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