QuTiP development notebook for testing qutip.ipynbtools.plot_animation

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

In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [2]:
from matplotlib import rcParams
rcParams["font.family"] = "STIXGeneral"
rcParams["mathtext.fontset"] = "stix"
rcParams["font.size"] = "16"

Example: Visualize the dynamics of the cavity and atom state in the Jaynes-Cumming model

In [3]:
from qutip import *
from qutip.ipynbtools import plot_animation
In [4]:
wc = 1.0  * 2 * pi  # cavity frequency
wa = 1.0  * 2 * pi  # atom frequency
g  = 0.1 * 2 * pi   # coupling strength
kappa = 0.10        # cavity dissipation rate
gamma = 0.05        # atom dissipation rate
N = 15              # number of cavity fock states
tlist = linspace(0, 15, 150)
In [5]:
# intial state
psi0 = tensor(coherent(N,2), basis(2,1))    # start with an excited atom

# operators
a  = tensor(destroy(N), qeye(2))
sm = tensor(qeye(N), destroy(2))
sz = tensor(qeye(N), sigmaz())

# Hamiltonian
H = wc * a.dag() * a + wa * sm.dag() * sm + g * (a.dag() + a) * (sm + sm.dag())
c_ops = [sqrt(kappa) * a, sqrt(gamma) * sm]
In [6]:
result = mesolve(H, psi0, tlist, c_ops, [a.dag() * a, sz], options=Odeoptions(store_states=True))

Generate animation

In [7]:
def plot_setup(result):    
    
    fig = plt.figure(figsize=(8,8))
    ax1 = plt.subplot2grid((2,2), (0,0), rowspan=1)
    ax2 = plt.subplot2grid((2,2), (0,1), rowspan=1)
    ax3 = plt.subplot2grid((2,2), (1,0), colspan=2)
    axes = array([ax1, ax2, ax3])

    axes[2].plot(result.times, result.expect[1], '-k')
    return fig, axes
In [8]:
def plot_result(result, n, fig=None, axes=None):
    
    if fig is None or axes is None:
        fig, axes = plot_setup(result)
    
    axes[0].cla()
    axes[1].cla()
    wigner_fock_distribution(ptrace(result.states[n], 0), fig=fig, axes=axes[0:2])
    
    axes[2].plot(result.times[n], result.expect[1][n], 'bo')
    axes[2].set_ylim(-1, 1)
    axes[2].set_xlim(0, max(tlist))
    axes[2].set_xlabel(r'$t$')
    axes[2].set_ylabel(r'Atomic inversion $\langle\sigma_z\rangle$')

    return fig, axes

To embedd video in the notebook, make sure that ffmpeg and libx264 is installed.

# in ubuntu:
sudo apt-get install ffmpeg
sudo apt-get install libavcodec-extra-53
In [9]:
plot_animation(plot_setup, plot_result, result)
Out[9]:

Versions

In [10]:
from qutip.ipynbtools import version_table

version_table()
Out[10]:
SoftwareVersion
Cython0.19
SciPy0.14.0.dev-2a4ba40
QuTiP2.3.0.dev-0fd8af4
Python2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3]
IPython2.0.0-dev
OSposix [linux2]
Numpy1.8.0.dev-928289b
matplotlib1.4.x
Sat Sep 28 23:09:43 2013 JST