Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jrjohansson/5487153 to your computer and use it in GitHub Desktop.
Save jrjohansson/5487153 to your computer and use it in GitHub Desktop.
Test qutip.propagator with Odeoptions.rhs_reuse
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "test-propagator-resue-rhs"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Test qutip.propagator with Odeoptions.rhs_reuse\n",
"\n",
"Code based on problem report by wmwitzel: https://github.com/qutip/qutip/issues/18 "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import qutip as qtp"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"S = qtp.jmat(1/2.)\n",
"I = qtp.qeye(2)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"H0 = qtp.tensor(S[2], I) + 0.1*qtp.tensor(I, S[2])\n",
"\n",
"H0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = True}\\\\[1em]\\begin{pmatrix}0.550 & 0.0 & 0.0 & 0.0\\\\0.0 & 0.450 & 0.0 & 0.0\\\\0.0 & 0.0 & -0.450 & 0.0\\\\0.0 & 0.0 & 0.0 & -0.550\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 3,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = True\n",
"Qobj data =\n",
"[[ 0.55 0. 0. 0. ]\n",
" [ 0. 0.45 0. 0. ]\n",
" [ 0. 0. -0.45 0. ]\n",
" [ 0. 0. 0. -0.55]]"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Hint = 0.1*sum([qtp.tensor(S[i], S[i]) for i in xrange(3)])\n",
"\n",
"Hint"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = True}\\\\[1em]\\begin{pmatrix}0.025 & 0.0 & 0.0 & 0.0\\\\0.0 & -0.025 & 0.050 & 0.0\\\\0.0 & 0.050 & -0.025 & 0.0\\\\0.0 & 0.0 & 0.0 & 0.025\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 4,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = True\n",
"Qobj data =\n",
"[[ 0.025 0. 0. 0. ]\n",
" [ 0. -0.025 0.05 0. ]\n",
" [ 0. 0.05 -0.025 0. ]\n",
" [ 0. 0. 0. 0.025]]"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Turning interaction on linearly:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"qtp.propagator([H0, [Hint, 'w*t']], 5, [], {'w':1/5.})"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = False}\\\\[1em]\\begin{pmatrix}(-0.946-0.323j) & 0.0 & 0.0 & 0.0\\\\0.0 & (-0.581-0.811j) & (0.057-0.040j) & 0.0\\\\0.0 & (-0.051-0.046j) & (-0.677+0.732j) & 0.0\\\\0.0 & 0.0 & 0.0 & (-0.899+0.439j)\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 5,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = False\n",
"Qobj data =\n",
"[[-0.94633811-0.32318029j 0.00000000+0.j 0.00000000+0.j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.58073735-0.81115709j 0.05656517-0.03959453j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.05118736-0.04633785j -0.67733746+0.73242498j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j\n",
" -0.89865955+0.43864333j]]"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Turning interaction off linearly with rhs_reuse (bug?):\n",
"\n",
"#### -> more like unexpected behavior, because RHS function from previous call to propator is reused"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"odeOpts = qtp.Odeoptions(1e-10, 1e-8)\n",
"odeOpts.rhs_reuse = True\n",
"#qtp.rhs_clear() # this fixes the problem\n",
"\n",
"qtp.propagator([H0, [Hint, '1-w*t']], 5, [], {'w':1/5.}, options=odeOpts)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"/usr/local/lib/python2.7/dist-packages/qutip/propagator.py:76: UserWarning: propagator is using previously defined rhs function (options.rhs_reuse = True)\n",
" warnings.warn(\"propagator is using previously defined rhs function (options.rhs_reuse = True)\")\n"
]
},
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = False}\\\\[1em]\\begin{pmatrix}(-0.946-0.323j) & 0.0 & 0.0 & 0.0\\\\0.0 & (-0.581-0.811j) & (0.057-0.040j) & 0.0\\\\0.0 & (-0.051-0.046j) & (-0.677+0.732j) & 0.0\\\\0.0 & 0.0 & 0.0 & (-0.899+0.439j)\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 6,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = False\n",
"Qobj data =\n",
"[[-0.94633580-0.32318436j 0.00000000+0.j 0.00000000+0.j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.58073723-0.81115776j 0.05656514-0.03959461j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.05118735-0.04633793j -0.67733701+0.73242557j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j\n",
" -0.89865924+0.43864704j]]"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Turning interaction off linearly with rhs_reuse:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"qtp.propagator([H0, [Hint, '1-w*t']], 5, [], {'w':1/5.})"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = False}\\\\[1em]\\begin{pmatrix}(-0.946-0.323j) & 0.0 & 0.0 & 0.0\\\\0.0 & (-0.581-0.811j) & (-0.051-0.046j) & 0.0\\\\0.0 & (0.057-0.040j) & (-0.677+0.732j) & 0.0\\\\0.0 & 0.0 & 0.0 & (-0.899+0.439j)\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 7,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = False\n",
"Qobj data =\n",
"[[-0.94633582-0.32318044j 0.00000000+0.j 0.00000000+0.j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.58073805-0.81115714j -0.05118751-0.04633791j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.05656517-0.03959451j -0.67733759+0.73242409j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j\n",
" -0.89866198+0.43864341j]]"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Turning interaction off linearly without rhs_reuse (more like it):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"odeOpts = qtp.Odeoptions(1e-10, 1e-8)\n",
"odeOpts.rhs_reuse = False\n",
"\n",
"qtp.propagator([H0, [Hint, '1-w*t']], 5, [], {'w':1/5.}) #, options=odeOpts)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = False}\\\\[1em]\\begin{pmatrix}(-0.946-0.323j) & 0.0 & 0.0 & 0.0\\\\0.0 & (-0.581-0.811j) & (-0.051-0.046j) & 0.0\\\\0.0 & (0.057-0.040j) & (-0.677+0.732j) & 0.0\\\\0.0 & 0.0 & 0.0 & (-0.899+0.439j)\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 8,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = False\n",
"Qobj data =\n",
"[[-0.94633582-0.32318044j 0.00000000+0.j 0.00000000+0.j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.58073805-0.81115714j -0.05118751-0.04633791j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.05656517-0.03959451j -0.67733759+0.73242409j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j\n",
" -0.89866198+0.43864341j]]"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Turning interaction on linearly 'a+b*t': A solution is to keep the time function the same but change the arguments."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"odeOpts = qtp.Odeoptions(1e-10, 1e-8)\n",
"\n",
"# odeOpts.rhs_reuse = False # this works but give worse performance\n",
"\n",
"odeOpts.rhs_reuse = True # this make sure that both calls to propagator below use the same RHS function\n",
"qtp.rhs_clear()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"qtp.propagator([H0, [Hint, 'a+b*t']], 5, [], {'a':0, 'b':1/5.}, options=odeOpts)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = False}\\\\[1em]\\begin{pmatrix}(-0.946-0.323j) & 0.0 & 0.0 & 0.0\\\\0.0 & (-0.581-0.811j) & (0.057-0.040j) & 0.0\\\\0.0 & (-0.051-0.046j) & (-0.677+0.732j) & 0.0\\\\0.0 & 0.0 & 0.0 & (-0.899+0.439j)\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 10,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = False\n",
"Qobj data =\n",
"[[-0.94633580-0.32318436j 0.00000000+0.j 0.00000000+0.j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.58073723-0.81115776j 0.05656514-0.03959461j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.05118735-0.04633793j -0.67733701+0.73242557j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j\n",
" -0.89865924+0.43864704j]]"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Turning interaction off linearly with rhs_reuse but same 'a+b*t':"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"qtp.propagator([H0, [Hint, 'a+b*t']], 5, [], {'a':1, 'b':-1/5.}, options=odeOpts)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"\\begin{equation}\\text{Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = False}\\\\[1em]\\begin{pmatrix}(-0.946-0.323j) & 0.0 & 0.0 & 0.0\\\\0.0 & (-0.581-0.811j) & (-0.051-0.046j) & 0.0\\\\0.0 & (0.057-0.040j) & (-0.677+0.732j) & 0.0\\\\0.0 & 0.0 & 0.0 & (-0.899+0.439j)\\\\\\end{pmatrix}\\end{equation}"
],
"output_type": "pyout",
"prompt_number": 11,
"text": [
"Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = False\n",
"Qobj data =\n",
"[[-0.94633579-0.32318446j 0.00000000+0.j 0.00000000+0.j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j -0.58073724-0.81115775j -0.05118735-0.04633793j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.05656514-0.03959461j -0.67733700+0.73242558j\n",
" 0.00000000+0.j ]\n",
" [ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j\n",
" -0.89865926+0.43864693j]]"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Software versions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from qutip.ipynbtools import version_table; version_table()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table><tr><th>Software</th><th>Version</th></tr><tr><td>Cython</td><td>0.17.4</td></tr><tr><td>SciPy</td><td>0.11.0</td></tr><tr><td>QuTiP</td><td>2.3.0.dev-374c4b6</td></tr><tr><td>Python</td><td>2.7.4 (default, Apr 19 2013, 18:28:01) \n",
"[GCC 4.7.3]</td></tr><tr><td>IPython</td><td>1.0.dev</td></tr><tr><td>OS</td><td>posix [linux2]</td></tr><tr><td>Numpy</td><td>1.7.1</td></tr><tr><td>matplotlib</td><td>1.3.x</td></tr><tr><td colspan='2'>Tue Apr 30 16:27:55 2013 JST</td></tr></table>"
],
"output_type": "pyout",
"prompt_number": 12,
"text": [
"<IPython.core.display.HTML at 0x573a950>"
]
}
],
"prompt_number": 12
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment