QuTiP development notebook for testing functions qutip.ipynbtools

Copyright (C) 2011 and later, Paul D. Nation & Robert J. Johansson


In [28]:
%pylab inline

In [29]:
from qutip import *
import qutip.ipynbtools

In [30]:
import time

Software versions


In [31]:
qutip.ipynbtools.version_table()


Out[31]:
SoftwareVersion
Cython0.19-dev
SciPy0.13.0.dev-38ad5d2
QuTiP2.3.0.dev-71d6e0c
Python2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2]
IPython0.13
OSposix [linux2]
Numpy1.8.0.dev-bd7104c
matplotlib1.3.x
Tue Apr 23 16:06:27 2013 JST

IPython.parallel based parfor

To run the following examples you first need to start an IPython cluster. Easy to do from the "Clusters" tab on the IPython notebook dashboard page, or from the command-line using

$ ipcluster start -n 4

Simple example


In [32]:
delay_times = numpy.random.rand(10)

delay_times


Out[32]:
array([ 0.85397305,  0.6850936 ,  0.99509335,  0.85106784,  0.65212566,
        0.47303261,  0.18808245,  0.13273333,  0.72033813,  0.53014309])

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]:
[0.8548479080200195,
 0.6857919692993164,
 0.9961011409759521,
 0.8519401550292969,
 0.6527969837188721,
 0.4735140800476074,
 0.1882779598236084,
 0.1328730583190918,
 0.7210910320281982,
 0.530709981918335]

In [35]:
result = parfor(task, delay_times)

result


Out[35]:
[0.8548691272735596,
 0.685809850692749,
 0.996117115020752,
 0.8517839908599854,
 0.6528029441833496,
 0.47353196144104004,
 0.18828606605529785,
 0.13289308547973633,
 0.7210750579833984,
 0.5306930541992188]

In [36]:
result = qutip.ipynbtools.parfor(task, delay_times, show_scheduling=True, show_progressbar=True)


 


In [37]:
result


Out[37]:
[0.8548541069030762,
 0.6857941150665283,
 0.9953198432922363,
 0.8512799739837646,
 0.6527988910675049,
 0.4735231399536133,
 0.18829107284545898,
 0.13288402557373047,
 0.7210788726806641,
 0.5306930541992188]

QuTiP example 1: steady state of a qubit-cavity system


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)


elapsed = 4.68055295944

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)


elapsed = 1.57517004013

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)


 

elapsed = 1.96851205826

In [46]:
visualize_results(g_vec / (2 * pi), n_vec)


QuTiP example 2: steady state of a qubit-cavity system with pre-allocated Qobjs


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)


elapsed = 30.7636711597

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)


elapsed = 13.2212700844

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)


 

elapsed = 13.6379978657

In [54]:
visualize_results(g_vec / (2 * pi), n_vec)