{ "metadata": { "name": "sho1d_example" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Example Notebook for sho1d.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the sho1d.py file as well as the test_sho1d.py file" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext sympy.interactive.ipythonprinting \n", "from sympy import *\n", "from IPython.display import display_pretty\n", "from sympy.physics.quantum import *\n", "from sympy.physics.quantum.sho1d import *\n", "from sympy.physics.quantum.tests.test_sho1d import *" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The sympy.interactive.ipythonprinting extension is already loaded. To reload it, use:\n", " %reload_ext sympy.interactive.ipythonprinting\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Printing Of Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a raising and lowering operator and make sure they print correctly" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ad = RaisingOp('a')\n", "a = LoweringOp('a')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "ad" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "RaisingOp(a)" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "a" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "print latex(ad)\n", "print latex(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a^{\\dag}\n", "a\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "display_pretty(ad)\n", "display_pretty(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "display_data", "text": [ "RaisingOp(a)" ] }, { "output_type": "display_data", "text": [ "a" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "print srepr(ad)\n", "print srepr(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "RaisingOp(Symbol('a'))\n", "LoweringOp(Symbol('a'))\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "print repr(ad)\n", "print repr(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "RaisingOp(a)\n", "a\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Printing of States" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a simple harmonic state and check its printing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "k = SHOKet('k')\n", "b = SHOBra('b')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "k" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 10, "text": [ "|k>" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 11, "text": [ "" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Taking the InnerProduct of the bra and ket will return the KroneckerDelta function" ] }, { "cell_type": "code", "collapsed": false, "input": [ "InnerProduct(b,k).doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 21, "text": [ "KroneckerDelta(k, b)" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a look at how the raising and lowering operators act on states. We use qapply to apply an operator to a state" ] }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(ad*k)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 22, "text": [ "sqrt(k + 1)*|k + 1>" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(a*k)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 23, "text": [ "sqrt(k)*|k - 1>" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "But the states may have an explicit energy level. Let's look at the ground and first excited states" ] }, { "cell_type": "code", "collapsed": false, "input": [ "kg = SHOKet(0)\n", "kf = SHOKet(1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(ad*kg)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 25, "text": [ "|1>" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(ad*kf)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 26, "text": [ "sqrt(2)*|2>" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(a*kg)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 27, "text": [ "0" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(a*kf)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 28, "text": [ "|0>" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that a*kg is 0 and a*kf is the |0> the ground state." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "NumberOp & Hamiltonian" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at the Number Operator and Hamiltonian Operator" ] }, { "cell_type": "code", "collapsed": false, "input": [ "k = SHOKet('k')\n", "ad = RaisingOp('a')\n", "a = LoweringOp('a')\n", "N = NumberOp('N')\n", "H = Hamiltonian('H')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The number operator is simply expressed as ad*a" ] }, { "cell_type": "code", "collapsed": false, "input": [ "N().rewrite('a').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 30, "text": [ "RaisingOp(a)*a" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The number operator expressed in terms of the position and momentum operators" ] }, { "cell_type": "code", "collapsed": false, "input": [ "N().rewrite('xp').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 31, "text": [ "-1/2 + (m**2*omega**2*X**2 + Px**2)/(2*hbar*m*omega)" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can also be expressed in terms of the Hamiltonian operator" ] }, { "cell_type": "code", "collapsed": false, "input": [ "N().rewrite('H').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 32, "text": [ "-1/2 + H/(hbar*omega)" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Hamiltonian operator can be expressed in terms of the raising and lowering operators, position and momentum operators, and the number operator" ] }, { "cell_type": "code", "collapsed": false, "input": [ "H().rewrite('a').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 33, "text": [ "hbar*omega*(1/2 + RaisingOp(a)*a)" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "H().rewrite('xp').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 34, "text": [ "(m**2*omega**2*X**2 + Px**2)/(2*m)" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "H().rewrite('N').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 35, "text": [ "hbar*omega*(1/2 + N)" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The raising and lowering operators can also be expressed in terms of the position and momentum operators" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ad().rewrite('xp').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 36, "text": [ "sqrt(2)*(m*omega*X - I*Px)/(2*sqrt(hbar)*sqrt(m*omega))" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "a().rewrite('xp').doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 37, "text": [ "sqrt(2)*(m*omega*X + I*Px)/(2*sqrt(hbar)*sqrt(m*omega))" ] } ], "prompt_number": 37 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Properties" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a look at how the NumberOp and Hamiltonian act on states" ] }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(N*k)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 38, "text": [ "k*|k>" ] } ], "prompt_number": 38 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apply the Number operator to a state returns the state times the ket" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ks = SHOKet(2)\n", "qapply(N*ks)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 39, "text": [ "2*|2>" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "qapply(H*k)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 40, "text": [ "hbar*k*omega*|k> + hbar*omega*|k>/2" ] } ], "prompt_number": 40 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see how the operators commute with each other" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Commutator(N,ad).doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 41, "text": [ "RaisingOp(a)" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "Commutator(N,a).doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 42, "text": [ "-a" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "Commutator(N,H).doit()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 43, "text": [ "0" ] } ], "prompt_number": 43 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Representation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can express the operators in NumberOp basis. There are different ways to create a matrix in Python, we will use 3 different ways." ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Sympy" ] }, { "cell_type": "code", "collapsed": false, "input": [ "represent(ad, basis=N, ndim=4, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 44, "text": [ "[0, 0, 0, 0]\n", "[1, 0, 0, 0]\n", "[0, sqrt(2), 0, 0]\n", "[0, 0, sqrt(3), 0]" ] } ], "prompt_number": 44 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Numpy" ] }, { "cell_type": "code", "collapsed": false, "input": [ "represent(ad, basis=N, ndim=5, format='numpy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 45, "text": [ "array([[ 0. , 0. , 0. , 0. , 0. ],\n", " [ 1. , 0. , 0. , 0. , 0. ],\n", " [ 0. , 1.41421356, 0. , 0. , 0. ],\n", " [ 0. , 0. , 1.73205081, 0. , 0. ],\n", " [ 0. , 0. , 0. , 2. , 0. ]])" ] } ], "prompt_number": 45 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Scipy.Sparse" ] }, { "cell_type": "code", "collapsed": false, "input": [ "represent(ad, basis=N, ndim=4, format='scipy.sparse', spmatrix='lil')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 46, "text": [ "<4x4 sparse matrix of type ''\n", "\twith 3 stored elements in Compressed Sparse Row format>" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "print represent(ad, basis=N, ndim=4, format='scipy.sparse', spmatrix='lil')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " (1, 0)\t1.0\n", " (2, 1)\t1.41421356237\n", " (3, 2)\t1.73205080757\n" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same can be done for the other operators" ] }, { "cell_type": "code", "collapsed": false, "input": [ "represent(a, basis=N, ndim=4, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 48, "text": [ "[0, 1, 0, 0]\n", "[0, 0, sqrt(2), 0]\n", "[0, 0, 0, sqrt(3)]\n", "[0, 0, 0, 0]" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "represent(N, basis=N, ndim=4, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 49, "text": [ "[0, 0, 0, 0]\n", "[0, 1, 0, 0]\n", "[0, 0, 2, 0]\n", "[0, 0, 0, 3]" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "represent(H, basis=N, ndim=4, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 50, "text": [ "[hbar*omega/2, 0, 0, 0]\n", "[ 0, 3*hbar*omega/2, 0, 0]\n", "[ 0, 0, 5*hbar*omega/2, 0]\n", "[ 0, 0, 0, 7*hbar*omega/2]" ] } ], "prompt_number": 50 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Ket and Bra Representation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "k0 = SHOKet(0)\n", "k1 = SHOKet(1)\n", "b0 = SHOBra(0)\n", "b1 = SHOBra(1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "print represent(k0, basis=N, ndim=5, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1]\n", "[0]\n", "[0]\n", "[0]\n", "[0]\n" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "print represent(k1, basis=N, ndim=5, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0]\n", "[1]\n", "[0]\n", "[0]\n", "[0]\n" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "print represent(b0, basis=N, ndim=5, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 0, 0, 0, 0]\n" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "print represent(b1, basis=N, ndim=5, format='sympy')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 0, 0, 0]\n" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }