7

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?

Glorfindel
  • 628
  • 1
  • 9
  • 24
user12910
  • 471
  • 1
  • 4
  • 9

2 Answers2

3

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) psi.ry(np.pi/4.,0) psi.x(0) psi.x(1)

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

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
Cryoris
  • 2,993
  • 8
  • 15
0

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.

Yitian Wang
  • 1,008
  • 6
  • 16