I've been trying to plot the state $\frac{|0>-|1>}{\sqrt{2}}$ using Qiskit. As a result, I need to apply the NOT gate on $|0>$ to get $|1>$ and then apply the Hadamard gate on $|1>$ to get the required state. Here's my code -
from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_bloch_vector
import numpy as np
# Create a quantum circuit with one qubit
qc = QuantumCircuit(1)
# Apply a Hadamard gate to the |1> state
qc.x(0) # Apply an X gate to get |1>
qc.h(0) # Apply a Hadamard gate
# Simulate the circuit on the local qasm simulator
backend = Aer.get_backend('statevector_simulator')
job = execute(qc, backend)
result = job.result()
statevector = result.get_statevector()
# Extract the Bloch vector components from the state vector
bloch_vector = [2 * np.real(statevector[0]), 2 * np.imag(statevector[1]), 0]
# Plot the Bloch vector on the Bloch sphere
plot_bloch_vector(bloch_vector).show()
This image corresponds to the state $\frac{|0>+|1>}{\sqrt{2}}$. Can someone point out the not-so-obvious mistake in my code that I've failed to debug so far?
The issue lies with extracting the Bloch vector from the state vector. Your equation
bloch_vector = [2 * np.real(statevector[0]), 2 * np.imag(statevector[1]), 0]
is not quite correct.
To see why not, if you think about it, the statevectors of the two states are [1\sqrt(2), 1\sqrt(2)]
and [1\sqrt(2), -1\sqrt(2)]
. Clearly in both the cases, there are no imaginary parts.
Given a single qubit state a|0> + b|1>
, the correct equation for extracting the Bloch vectors is
bloch_vector = [2*np.real(a*np.conjugate(b)), 2*np.imag(b*np.conjugate(a)), a*np.conjugate(a)-b*np.conjugate(b)]
.