def draw_bloch_sphere(ax, R=1, color='yellow', npts=30, elev=15, azim=15, lim=None): from numpy import linspace, sin, cos, pi, outer, ones, size # Set up the basic 3d plot ax.set_aspect("equal") ax.view_init(elev=elev, azim=azim) # Draw the sphere u = linspace(0, 2 * pi, npts) v = linspace(0, pi, npts) x = R * outer(cos(u), sin(v)) y = R * outer(sin(u), sin(v)) z = R * outer(ones(size(u)), cos(v)) ax.plot_surface(x, y, z, rstride=1, cstride=1, color=color, alpha=0.1, linewidth=0) # Draw the axes c = 'black' draw_vec(ax, [1, 0, 0], color=c, label=r'$|+x\rangle$', fudge=1.75) draw_vec(ax, [0, 1, 0], color=c, label=r'$|+y\rangle$') draw_vec(ax, [0, 0, 1], color=c, label=r'$|+z\rangle$') draw_vec(ax, [0, 0, -1], color=c, label=r'$|-z\rangle$', fudge=1.2) # Draw a dashed line around the "equator." N = 100 x = [] y = [] z = [] sin_theta = sin(pi / 2) cos_theta = cos(pi / 2) phi = 0 for n in range(N + 1): x += [ R * sin_theta * cos(phi), ] y += [ R * sin_theta * sin(phi), ] z += [ R * cos_theta, ] phi += 2 * pi / N ax.plot(x, y, z, color='gray', linestyle='--') if lim != None: ax.set_xlim3d(-lim, lim) ax.set_ylim3d(-lim, lim) ax.set_zlim3d(-lim, lim)
def __init__(self, ax, alpha, beta, bs_lim=None): from numpy import sin, cos, arccos, angle from matplotlib import pyplot as pl self.clr = 'green' self.ax = ax draw_bloch_sphere(ax, lim=bs_lim) theta = 2 * arccos(abs(alpha)) phi = angle(beta) - angle(alpha) x = cos(phi) * sin(theta) y = sin(phi) * sin(theta) z = cos(theta) # Label for the Psi vector (if we want one) self.vlabel = r'$|\psi\rangle$' self.vlabel = '' # Don't notate psi for now. # last_vec holds pointers to the vector and text objects. # Delete them with last_vec[0].remove(), etc ... self.last_vec = draw_vec(ax, [x, y, z], color=self.clr, label=self.vlabel, fudge=1.3) pl.draw() pl.show()
def update(self, time, alpha, beta): from numpy import sin, cos, arccos, angle from matplotlib import pyplot as pl ax = self.ax self.last_vec[0].remove() self.last_vec[1].remove() theta = 2*arccos(abs(alpha)) phi = angle(beta)-angle(alpha) x = cos(phi)*sin(theta) y = sin(phi)*sin(theta) z = cos(theta) self.last_vec = draw_vec( ax,[x,y,z],color=self.clr,label=self.vlabel,fudge=1.3) pl.draw() pl.show()
def update(self, time, alpha, beta): from numpy import sin, cos, arccos, angle ax = self.ax self.last_vec[0].remove() self.last_vec[1].remove() theta = 2 * arccos(abs(alpha)) phi = angle(beta) - angle(alpha) x = cos(phi) * sin(theta) y = sin(phi) * sin(theta) z = cos(theta) self.last_vec = draw_vec(ax, [x, y, z], color=self.clr, label=self.vlabel, fudge=1.3) pl.draw() pl.show()
def draw_bloch_sphere(ax, R=1, color='yellow', npts=30, elev=15, azim=15, lim=None): from numpy import linspace, sin, cos, pi, outer, ones, size # Set up the basic 3d plot ax.set_aspect("equal") ax.view_init(elev=elev, azim=azim) # Draw the sphere u = linspace(0, 2 * pi, npts) v = linspace(0, pi, npts) x = R*outer(cos(u), sin(v)) y = R*outer(sin(u), sin(v)) z = R*outer(ones(size(u)), cos(v)) ax.plot_surface( x, y, z, rstride=1, cstride=1, color=color, alpha = 0.1, linewidth = 0) # Draw the axes c = 'black' draw_vec(ax, [1,0,0], color=c, label=r'$|+x\rangle$', fudge=1.75) draw_vec(ax, [0,1,0], color=c, label=r'$|+y\rangle$') draw_vec(ax, [0,0,1], color=c, label=r'$|+z\rangle$') draw_vec(ax, [0,0,-1], color=c, label=r'$|-z\rangle$', fudge=1.2) # Draw a dashed line around the "equator." N=100 x = []; y = []; z = [] sin_theta = sin(pi/2) cos_theta = cos(pi/2) phi = 0 for n in range(N+1): x += [ R*sin_theta*cos(phi), ] y += [ R*sin_theta*sin(phi), ] z += [ R*cos_theta, ] phi += 2*pi/N ax.plot(x, y, z, color='gray', linestyle='--') if lim != None: ax.set_xlim3d(-lim,lim) ax.set_ylim3d(-lim,lim) ax.set_zlim3d(-lim,lim)
def draw_xyz(ax, v, color='green', draw_box=True): from sglib import draw_vec # Draw the axes draw_vec(ax, [1, 0, 0], color='black', label='x') draw_vec(ax, [0, 1, 0], color='black', label='y') draw_vec(ax, [0, 0, 1], color='black', label='z') # Draw the spin vectors draw_vec(ax, v, label=r'$\vec{v}$', color=color) if draw_box: pass else: ax.set_axis_off() # If you use "equal" then the last parm on position sets size? # But this causes it to fail outsize a notebook! #ax.set_position([1,1,1,2]) ax.set_aspect("equal") ax.view_init(elev=5, azim=25) pl.draw() pl.show()
def draw_xyz(ax, v, color='green', draw_box=True): import matplotlib.pyplot as pl from sglib import draw_vec # Draw the axes draw_vec(ax, [1,0,0], color='black', label='x') draw_vec(ax, [0,1,0], color='black', label='y') draw_vec(ax, [0,0,1], color='black', label='z') # Draw the spin vectors draw_vec(ax, v, label=r'$\vec{v}$', color=color) if draw_box: pass else: ax.set_axis_off() # If you use "equal" then the last parm on position sets size? # But this causes it to fail outsize a notebook! #ax.set_position([1,1,1,2]) ax.set_aspect("equal") ax.view_init(elev=5, azim=25) pl.draw() pl.show()
def __init__(self, ax, alpha, beta, bs_lim=None): from numpy import sin, cos, arccos, angle from matplotlib import pyplot as pl self.clr = 'green' self.ax = ax draw_bloch_sphere(ax, lim=bs_lim) theta = 2*arccos(abs(alpha)) phi = angle(beta)-angle(alpha) x = cos(phi)*sin(theta) y = sin(phi)*sin(theta) z = cos(theta) # Label for the Psi vector (if we want one) self.vlabel = r'$|\psi\rangle$' self.vlabel = '' # Don't notate psi for now. # last_vec holds pointers to the vector and text objects. # Delete them with last_vec[0].remove(), etc ... self.last_vec = draw_vec( ax,[x,y,z],color=self.clr,label=self.vlabel,fudge=1.3) pl.draw() pl.show()