Copyright (C) 2011 and later, Paul D. Nation & Robert J. Johansson
%pylab inline
from qutip import *
import qutip.ipynbtools
import time
qutip.ipynbtools.version_table()
Software | Version |
---|---|
Cython | 0.19-dev |
SciPy | 0.13.0.dev-38ad5d2 |
QuTiP | 2.3.0.dev-71d6e0c |
Python | 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] |
IPython | 0.13 |
OS | posix [linux2] |
Numpy | 1.8.0.dev-bd7104c |
matplotlib | 1.3.x |
Tue Apr 23 16:06:27 2013 JST |
delay_times = numpy.random.rand(10)
delay_times
array([ 0.85397305, 0.6850936 , 0.99509335, 0.85106784, 0.65212566, 0.47303261, 0.18808245, 0.13273333, 0.72033813, 0.53014309])
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
result = map(task, delay_times)
result
[0.8548479080200195, 0.6857919692993164, 0.9961011409759521, 0.8519401550292969, 0.6527969837188721, 0.4735140800476074, 0.1882779598236084, 0.1328730583190918, 0.7210910320281982, 0.530709981918335]
result = parfor(task, delay_times)
result
[0.8548691272735596, 0.685809850692749, 0.996117115020752, 0.8517839908599854, 0.6528029441833496, 0.47353196144104004, 0.18828606605529785, 0.13289308547973633, 0.7210750579833984, 0.5306930541992188]
result = qutip.ipynbtools.parfor(task, delay_times, show_scheduling=True, show_progressbar=True)
Elapsed time: 1.77s. Est. remaining time: 0.00s.
result
[0.8548541069030762, 0.6857941150665283, 0.9953198432922363, 0.8512799739837646, 0.6527988910675049, 0.4735231399536133, 0.18829107284545898, 0.13288402557373047, 0.7210788726806641, 0.5306930541992188]
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')
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)
# 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
# serial calculation
t0 = time.time()
n_vec = array([compute_task(g, args) for g in g_vec])
t1 = time.time()
print "elapsed =", (t1-t0)
elapsed = 4.68055295944
visualize_results(g_vec / (2 * pi), n_vec)
# parallel calculation using qutip parfor
t0 = time.time()
n_vec = parfor(compute_task, g_vec, args=args)
t1 = time.time()
print "elapsed =", (t1-t0)
elapsed = 1.57517004013
visualize_results(g_vec / (2 * pi), n_vec)
# 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)
Elapsed time: 1.81s. Est. remaining time: 0.00s.
elapsed = 1.96851205826
visualize_results(g_vec / (2 * pi), n_vec)
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)
# 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
# serial calculation
t0 = time.time()
n_vec = array([compute_task(g, args) for g in g_vec])
t1 = time.time()
print "elapsed =", (t1-t0)
elapsed = 30.7636711597
visualize_results(g_vec / (2 * pi), n_vec)
# parallel calculation using qutip parfor
t0 = time.time()
n_vec = parfor(compute_task, g_vec, args=args)
t1 = time.time()
print "elapsed =", (t1-t0)
elapsed = 13.2212700844
visualize_results(g_vec / (2 * pi), n_vec)
# 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)
Elapsed time: 13.44s. Est. remaining time: 0.00s.
elapsed = 13.6379978657
visualize_results(g_vec / (2 * pi), n_vec)