In [28]:
%pylab inline
In [29]:
from qutip import *
import qutip.ipynbtools
In [30]:
import time
In [31]:
qutip.ipynbtools.version_table()
Out[31]:
In [32]:
delay_times = numpy.random.rand(10)
delay_times
Out[32]:
In [33]:
def task(delay):
import os, time
t0 = time.time()
pid = os.getpid()
time.sleep(delay)
t1 = time.time()
return (t1-t0) # return the actual delay
In [34]:
result = map(task, delay_times)
result
Out[34]:
In [35]:
result = parfor(task, delay_times)
result
Out[35]:
In [36]:
result = qutip.ipynbtools.parfor(task, delay_times, show_scheduling=True, show_progressbar=True)
In [37]:
result
Out[37]:
In [38]:
def visualize_results(g_vec, n_vec):
fig, ax = subplots()
ax.plot(g_vec, n_vec, lw=2)
ax.set_xlabel('Coupling strength (g)')
ax.set_ylabel('Photon Number')
ax.set_title('# of photons in the steady state')
In [39]:
def compute_task(g, args):
wc, wa, N, kappa, n_th = args['wc'], args['wa'], args['N'], args['kappa'], args['n_th']
a = tensor(destroy(N), qeye(2))
sm = tensor(qeye(N), destroy(2))
nc = a.dag() * a
na = sm.dag() * sm
c_ops = [sqrt(kappa * (1 + n_th)) * a, sqrt(kappa * n_th) * a.dag()]
H0 = wc * nc + wa * na
H1 = (a.dag() + a) * (sm + sm.dag())
H = H0 + g * H1
rho_ss = steadystate(H, c_ops)
return expect(nc, rho_ss)
In [40]:
# problem parameters
args = {'wc': 1.0 * 2 * pi, # cavity frequency
'wa': 1.0 * 2 * pi, # atom frequency
'N': 25, # number of cavity fock states
'kappa': 0.05,
'n_th': 0.5,
}
g_vec = linspace(0, 2.5, 50) * 2 * pi
In [41]:
# serial calculation
t0 = time.time()
n_vec = array([compute_task(g, args) for g in g_vec])
t1 = time.time()
print "elapsed =", (t1-t0)
In [42]:
visualize_results(g_vec / (2 * pi), n_vec)
In [43]:
# parallel calculation using qutip parfor
t0 = time.time()
n_vec = parfor(compute_task, g_vec, args=args)
t1 = time.time()
print "elapsed =", (t1-t0)
In [44]:
visualize_results(g_vec / (2 * pi), n_vec)
In [45]:
# parallel calculation using qutip IPython.parallel parfor
t0 = time.time()
n_vec = qutip.ipynbtools.parfor(compute_task, g_vec, args=args, show_scheduling=True, show_progressbar=True)
t1 = time.time()
print "elapsed =", (t1-t0)
In [46]:
visualize_results(g_vec / (2 * pi), n_vec)
In [47]:
def compute_task(g, args):
H0, H1, c_ops, nc = args['H0'], args['H1'], args['c_ops'], args['nc']
H = H0 + g * H1
rho_ss = steadystate(H, c_ops)
return expect(nc, rho_ss)
In [48]:
# problem parameters
wc = 1.0 * 2 * pi
wa = 1.0 * 2 * pi
N = 75
kappa = 0.05
n_th = 0.5
a = tensor(destroy(N), qeye(2))
sm = tensor(qeye(N), destroy(2))
nc = a.dag() * a
na = sm.dag() * sm
c_ops = [sqrt(kappa * (1 + n_th)) * a, sqrt(kappa * n_th) * a.dag()]
H0 = wc * nc + wa * na
H1 = (a.dag() + a) * (sm + sm.dag())
args = {'H0': H0,
'H1': H1,
'c_ops': c_ops,
'nc': nc
}
g_vec = linspace(0, 2.5, 50) * 2 * pi
In [49]:
# serial calculation
t0 = time.time()
n_vec = array([compute_task(g, args) for g in g_vec])
t1 = time.time()
print "elapsed =", (t1-t0)
In [50]:
visualize_results(g_vec / (2 * pi), n_vec)
In [51]:
# parallel calculation using qutip parfor
t0 = time.time()
n_vec = parfor(compute_task, g_vec, args=args)
t1 = time.time()
print "elapsed =", (t1-t0)
In [52]:
visualize_results(g_vec / (2 * pi), n_vec)
In [53]:
# parallel calculation
t0 = time.time()
n_vec = qutip.ipynbtools.parfor(compute_task, g_vec, args=args, show_scheduling=True, show_progressbar=True)
t1 = time.time()
print "elapsed =", (t1-t0)
In [54]:
visualize_results(g_vec / (2 * pi), n_vec)