Skip to content

Instantly share code, notes, and snippets.

@amarvutha
Created September 3, 2013 15:20
Show Gist options
  • Save amarvutha/6425331 to your computer and use it in GitHub Desktop.
Save amarvutha/6425331 to your computer and use it in GitHub Desktop.
Bloch sphere. Template for drawing sphere and labels. Just add points.
# Bloch sphere
# Amar Vutha, 2013-08-31
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# Basic Bloch sphere
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
phi = np.linspace(0, 2 * np.pi, 40)
theta = np.linspace(0, np.pi, 60)
x = 1 * np.outer(np.cos(phi), np.sin(theta))
y = 1 * np.outer(np.sin(phi), np.sin(theta))
z = 1 * np.outer(np.ones(np.size(phi)), np.cos(theta))
ax.plot_wireframe(x, y, z, rstride=10, cstride=30, color='gray', alpha=0.5)
ax.scatter([0],[0],[0],color='k',marker=r"$\otimes$",alpha=0.5)
# Annotate points
ax.text(0,0,1.2,r"$|0\rangle$")
ax.text(0,0,-1.2,r"$|1\rangle$")
ax.text(1.2,0,0,r"$x$")
ax.text(0,1.2,0,r"$y$")
# Set camera angle
ax.elev = 30
ax.azim = 35
# Plot points
theta0, phi0 = np.pi/2.3, 0
ax.scatter([np.sin(theta0)*np.cos(phi0)],[np.sin(theta0)*np.sin(phi0)],[np.cos(theta0)],'bo')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment