Menu Close

Qubit Visualization in Python | Quantum State Representation | Bloch Sphere – Python

Bloch Sphere - Quantum State

The Bloch sphere provides an intuitive way to understand and visualize the state of a quantum system.

The code generates a random quantum state vector and uses a 3D plot with a Bloch sphere representation to visualize it. 


Video Explanation


Steps

    1. First, we import the necessary libraries, NumPy and Matplotlib, to work with numerical operations and plotting.
    2. We set the variable num_qubits to the desired number of qubits. This determines the dimensionality of the quantum state vector.
    3. The rand_ket() function generates a random quantum state vector with a specified dimension, which in this case is 2 ** num_qubits. This means the state vector will have a length of 2 for a single qubit (2 ** 1 = 2), 4 for two qubits (2 ** 2 = 4), and so on.
    4. Next, we create a 3D plot using matplotlib.pyplot and specify that it should have a 3D projection using the projection=’3d’ parameter.
    5. We create a Bloch object, which is a part of the qutip library. This object allows us to visualize quantum states on a Bloch sphere representation.
    6. We add the generated state to the Bloch object using the add_states() method. This visualizes the state on the Bloch sphere.
    7. Finally, we call the show() method on the Bloch object to display the 3D plot representing the quantum state.

Python Program

  • Method #1
import numpy as np
import matplotlib.pyplot as plt
from qutip import *
# Generate a random quantum state vector
num_qubits = 1  # Number of qubits
state = rand_ket(2 ** num_qubits)

# Visualize the state using a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
sphere = Bloch(axes=ax)
sphere.add_states(state)
sphere.show()

 

  • Method #2

import numpy as np
import matplotlib.pyplot as plt
from qutip import *
# Generate a random quantum state vector
num_qubits = 1  # Number of qubits
state = rand_ket(2 ** num_qubits)

# Alternatively, visualize the state using a Bloch sphere representation
bloch = Bloch()
bloch.add_states(state)
bloch.show()

# Show the plots
plt.show()

 

Output

Bloch Sphere - Quantum State
Bloch Sphere – Quantum State

More Related Stuff