-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabi.py
189 lines (156 loc) · 4.84 KB
/
rabi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import numpy as np
import matplotlib.pyplot as plt
import cmath
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
sns.set_style("white")
def ab_to_alphabeta(thet):
"""
Matrix that transforms (a,b)^T (alpha,beta)^T
"""
# dtype = complex?
M = np.array([[np.cos(thet/2),np.sin(thet/2)],[np.sin(thet/2),-np.cos(thet/2)]])
return M
def theta(w0,w1,w):
"""
Calculates theta
"""
weff = np.sqrt((w0-w)**2 + w1**2)
thet = np.arccos((w0-w)/weff)
return thet
def params_to_omegas(B0,B,g,q,m,c):
"""
Calculates omegas from parameters
"""
w0 = -g*q*B0/(2*m*c)
w1 = -g*q*B/(2*m*c)
return [w0,w1]
def psi(t,psi0,w0,w1,w):
"""
psi(t)
"""
# theta
thet = theta(w0,w1,w)
weff = np.sqrt((w0-w)**2 + w1**2)
# alpha and beta
M = ab_to_alphabeta(thet)
alpha_beta = np.dot(M,psi0)
alpha = alpha_beta[0]
beta = alpha_beta[1]
# cos(theta/2) and sin(theta/2)
costo2 = np.cos(thet/2)
sinto2 = np.sin(thet/2)
# top
top = costo2*np.exp(-1j*w/2*t)*np.exp(-1j*weff/2*t)*alpha
top += sinto2*np.exp(-1j*w/2*t)*np.exp(1j*weff/2*t)*beta
# bottom
bot = sinto2*np.exp(1j*w/2*t)*np.exp(-1j*weff/2*t)*alpha
bot += -costo2*np.exp(1j*w/2*t)*np.exp(1j*weff/2*t)*beta
# psi(t)
psi_t = np.array([[top],[bot]],dtype=complex)
return psi_t
def probability(psi_t, proj):
"""
Returns |<proj|psi(t)>|^2
Proj is state to be projected onto
"""
prob_amp = np.conj(proj[0])*psi_t[0] + np.conj(proj[1])*psi_t[1]
prob = np.abs(prob_amp)**2
return prob
def main(t,psi0,proj,B0,B,w,g,q,m,c):
"""
Main
Returns prob to be in key for given t
"""
# omegas
[w0,w1] = params_to_omegas(B0,B,g,q,m,c)
# psi(t)
psi_t = psi(t,psi0,w0,w1,w)
# prob
prob = probability(psi_t,proj)
return prob
def plot_t(psi0,proj,B0,B,w,g,q,m,c):
t = np.linspace(0,25*np.pi,num=1000)
prob_t = main(t,psi0,proj,B0,B,w,g,q,m,c)
fig = plt.figure(figsize=(10,5))
plt.suptitle("$|<+z|\psi(t)>|^2$")
plt.xlabel("$t$")
plt.ylabel("$|<+z|\psi(t)>|^2$")
plt.plot(t,prob_t[0,0])
plt.show()
def main_bloch(t,psi0,B0,B,w,g,q,m,c):
"""
Takes in params and spits out (theta,phi) corresponding to state
"""
# omegas
[w0,w1] = params_to_omegas(B0,B,g,q,m,c)
# psi(t)
psi_t = psi(t,psi0,w0,w1,w)
# top
top = psi_t[0]
polars = cmath.polar(top)
thet = 2*np.arccos(polars[0])
# bot
bot = psi_t[1]
bot *= np.exp(-1j*polars[1])
phi = cmath.polar(bot)[1]
return thet, phi
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
FancyArrowPatch.draw(self, renderer)
def bloch_sphere(t,index,psi0,B0,B,w,g,q,m,c):
"""
Plots bloch sphere at given
"""
fig = plt.figure(figsize=(10,10))
plt.suptitle(r"Bloch sphere visualization of $\uparrow$ state undergoing rabi oscillations ($B_0 = "+str(B0)+"$, $B = "+str(B)+"$, $\omega = "+str(w)+"$)",fontsize=15)
ax = fig.gca(projection='3d')
ax.set_ylabel("$y$",fontsize=15)
ax.set_yticks([])
ax.set_xlabel("$x$",fontsize=15)
ax.set_xticks([])
ax.set_zlabel("$z$",fontsize=15)
ax.set_zticks([])
# sphere
u, v = np.mgrid[0:2*np.pi:100j, 0:np.pi:100j]
x = np.cos(u)*np.sin(v)
y = np.sin(u)*np.sin(v)
z = np.cos(v)
ax.plot_wireframe(x, y, z, color="r",alpha=0.1)
# origin
ax.scatter([0], [0], [0], color="g", s=100)
# state
thet, phi = main_bloch(t,psi0,B0,B,w,g,q,m,c)
a = Arrow3D([0, np.sin(thet)*np.cos(phi)], [0, np.sin(thet)*np.sin(phi)], [0, np.cos(thet)], mutation_scale=20,
lw=1, arrowstyle="-|>", color="k")
ax.add_artist(a)
plt.savefig("images/rabi_bloch_sphere_"+str(index)+".png")
plt.close()
def make_movie(tf,psi0,B0,B,w,g,q,m,c):
"""
Makes bloch sphere animation
NOT OPTIMIZED in the slightest
be sure to have images folder in cwd
"""
ts = np.linspace(0,tf,200)
# make images in folder
for i in range(len(ts)):
bloch_sphere(ts[i],i,psi0,B0,B,w,g,q,m,c)
import imageio
images = []
# make filenames array
filenames = []
for i in range(len(ts)):
filenames.append("images/rabi_bloch_sphere_"+str(i)+".png")
# make movie
for filename in filenames:
images.append(imageio.imread(filename))
imageio.mimsave('bloch_sphere.gif', images, duration = 1/24)