Uriel Muñoz
Uriel Muñoz

Reputation: 41

Confusion in Qiskit to generate Bell states

I want to generate the bell state $(|01\rangle+|10\rangle)/\sqrt{2}$ from the state $|00\rangle$ in qiskit applying the Hadamard gate followed by the CNOT gate. But it generates $(|11\rangle-|00\rangle)/\sqrt{2}$. What is the problem?

This is my code.

sv1 = Statevector.from_label('01')
mycircuit1 = QuantumCircuit(2)
mycircuit1.h(0)
mycircuit1.cx(0,1)
new_sv1 = sv1.evolve(mycircuit1)
print(new_sv1)
plot_state_qsphere(new_sv1.data)

Upvotes: 4

Views: 434

Answers (2)

Shravan Patel
Shravan Patel

Reputation: 294

Your code is correct but you are getting confused by the way qiskit orders the qubits in a state. According to qiskit documentation, qiskit uses an ordering convention called little-endian for both qubits and classical bits. Which means the qubit states will look something like this:

|(state of qubit q1) (state of qubit q0) \rangle

So following this convention, when you specify sv1 = Statevector.from_label('01') in your code what this means is that you want qubit q0 to be in 1 state and qubit q1 to be in 0 state. And so running your code will not give you your expected answer.

To fix this issue, you should replace sv1 = Statevector.from_label('01') with sv1 = Statevector.from_label('10'). I made this change and ran the code which gave me the output state that you are expecting. Below is the link to the qsphere plot for the same.

Qsphere representation of your state

Upvotes: 2

Tara
Tara

Reputation: 165

Your code correctly generates the Bell state (|01⟩ + |10⟩)/√2. The Hadamard gate puts the first qubit into a superposition of |0⟩ and |1⟩, and the CNOT gate entangles the qubits. The trouble is how you'rr interpreting the output of plot_state_qsphere. The Q-sphere visualization in Qiskit usually assumes a standard basis ordering of qubits: qubit 0 is the least significant bit. So in a two-qubit system:

* |00⟩ represents the top of the Q-sphere.
* |01⟩ represents the right point.
* |10⟩ represents the left point.
* |11⟩ represents the bottom.

Since your Bell state has a negative coefficient for |00⟩ and a positive coefficient for |11⟩, it would primarily lie on the bottom half of the Q-sphere, which explains the output you're seeing. I made the same mistakes myself. You can reverse the qubit ordering before plotting it on the Q-sphere.

import numpy as np
from qiskit import QuantumCircuit, Statevector
from qiskit.visualization import plot_state_qsphere

sv1 = Statevector.from_label('01') 
mycircuit1 = QuantumCircuit(2)
mycircuit1.h(0)
mycircuit1.cx(0, 1) 

new_sv1 = sv1.evolve(mycircuit1)

# Reverse the order of the qubit indices for visualization
new_sv1.data = new_sv1.data[::-1] 

print(new_sv1)
plot_state_qsphere(new_sv1.data) 

Upvotes: 0

Related Questions