{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "QuTiP development notebook for testing qutip.ipynbtools.plot_animation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (C) 2011 and later, Paul D. Nation & Robert J. Johansson" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "from matplotlib import rcParams\n", "rcParams[\"font.family\"] = \"STIXGeneral\"\n", "rcParams[\"mathtext.fontset\"] = \"stix\"\n", "rcParams[\"font.size\"] = \"16\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example: Visualize the dynamics of the cavity and atom state in the Jaynes-Cumming model" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from qutip import *\n", "from qutip.ipynbtools import plot_animation" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "wc = 1.0 * 2 * pi # cavity frequency\n", "wa = 1.0 * 2 * pi # atom frequency\n", "g = 0.1 * 2 * pi # coupling strength\n", "kappa = 0.10 # cavity dissipation rate\n", "gamma = 0.05 # atom dissipation rate\n", "N = 15 # number of cavity fock states\n", "tlist = linspace(0, 15, 150)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# intial state\n", "psi0 = tensor(coherent(N,2), basis(2,1)) # start with an excited atom\n", "\n", "# operators\n", "a = tensor(destroy(N), qeye(2))\n", "sm = tensor(qeye(N), destroy(2))\n", "sz = tensor(qeye(N), sigmaz())\n", "\n", "# Hamiltonian\n", "H = wc * a.dag() * a + wa * sm.dag() * sm + g * (a.dag() + a) * (sm + sm.dag())\n", "c_ops = [sqrt(kappa) * a, sqrt(gamma) * sm]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "result = mesolve(H, psi0, tlist, c_ops, [a.dag() * a, sz], options=Odeoptions(store_states=True))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Generate animation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def plot_setup(result): \n", " \n", " fig = plt.figure(figsize=(8,8))\n", " ax1 = plt.subplot2grid((2,2), (0,0), rowspan=1)\n", " ax2 = plt.subplot2grid((2,2), (0,1), rowspan=1)\n", " ax3 = plt.subplot2grid((2,2), (1,0), colspan=2)\n", " axes = array([ax1, ax2, ax3])\n", "\n", " axes[2].plot(result.times, result.expect[1], '-k')\n", " return fig, axes" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "def plot_result(result, n, fig=None, axes=None):\n", " \n", " if fig is None or axes is None:\n", " fig, axes = plot_setup(result)\n", " \n", " axes[0].cla()\n", " axes[1].cla()\n", " wigner_fock_distribution(ptrace(result.states[n], 0), fig=fig, axes=axes[0:2])\n", " \n", " axes[2].plot(result.times[n], result.expect[1][n], 'bo')\n", " axes[2].set_ylim(-1, 1)\n", " axes[2].set_xlim(0, max(tlist))\n", " axes[2].set_xlabel(r'$t$')\n", " axes[2].set_ylabel(r'Atomic inversion $\\langle\\sigma_z\\rangle$')\n", "\n", " return fig, axes" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To embedd video in the notebook, make sure that ffmpeg and libx264 is installed.\n", "\n", " # in ubuntu:\n", " sudo apt-get install ffmpeg\n", " sudo apt-get install libavcodec-extra-53\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot_animation(plot_setup, plot_result, result)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "