{ "metadata": { "name": "", "signature": "sha256:b7c2e7b97a0e600ed26f18f7a1ee2f0c4678005d82328e8b3f2062fc318c7b16" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "QuTiP example: Bloch sphere animation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "J.R. Johansson and P.D. Nation\n", "\n", "For more information about QuTiP see [http://qutip.org](http://qutip.org)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Animation with qutip and matplotlib: decaying qubit visualized in a Bloch sphere.\n", "(Animation with matplotlib does not work yet in python3)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.animation as animation\n", "from mpl_toolkits.mplot3d import Axes3D" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "from qutip import *\n", "from qutip.ipynbtools import plot_animation" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "def qubit_integrate(w, theta, gamma1, gamma2, psi0, tlist):\n", " # operators and the hamiltonian\n", " sx = sigmax(); sy = sigmay(); sz = sigmaz(); sm = sigmam()\n", " H = w * (np.cos(theta) * sz + np.sin(theta) * sx)\n", " # collapse operators\n", " c_op_list = []\n", " n_th = 0.5 # temperature\n", " rate = gamma1 * (n_th + 1)\n", " if rate > 0.0: c_op_list.append(np.sqrt(rate) * sm)\n", " rate = gamma1 * n_th\n", " if rate > 0.0: c_op_list.append(np.sqrt(rate) * sm.dag())\n", " rate = gamma2\n", " if rate > 0.0: c_op_list.append(np.sqrt(rate) * sz)\n", "\n", "\n", " # evolve and calculate expectation values\n", " output = mesolve(H, psi0, tlist, c_op_list, [sx, sy, sz]) \n", " return output" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "w = 1.0 * 2 * np.pi # qubit angular frequency\n", "theta = 0.2 * np.pi # qubit angle from sigma_z axis (toward sigma_x axis)\n", "gamma1 = 0.5 # qubit relaxation rate\n", "gamma2 = 0.2 # qubit dephasing rate\n", "# initial state\n", "a = 1.0\n", "psi0 = (a* basis(2,0) + (1-a)*basis(2,1))/(np.sqrt(a**2 + (1-a)**2))\n", "tlist = np.linspace(0, 4, 150)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "result = qubit_integrate(w, theta, gamma1, gamma2, psi0, tlist)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "def plot_setup(result): \n", " \n", " fig = plt.figure(figsize=(8,8))\n", " axes = Axes3D(fig, azim=-40,elev=30)\n", "\n", " return fig, axes" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "sphere = None\n", "\n", "def plot_result(result, n, fig=None, axes=None):\n", "\n", " global sphere\n", " \n", " if fig is None or axes is None:\n", " fig, axes = plot_setup(result)\n", "\n", " if not sphere:\n", " sphere = Bloch(axes=axes)\n", " sphere.vector_color = ['r']\n", " \n", " sphere.clear()\n", " sphere.add_vectors([np.sin(theta), 0, np.cos(theta)])\n", " sphere.add_points([result.expect[0][:n+1], result.expect[1][:n+1], result.expect[2][:n+1]], meth='l')\n", " sphere.make_sphere()\n", "\n", " return fig, axes" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "plot_animation(plot_setup, plot_result, result)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "