Skip to content

Commit

Permalink
Merge pull request #1396 from gdevanla/trace_permute_equals
Browse files Browse the repository at this point in the history
Tr printing, Tr partial trace
  • Loading branch information
ellisonbg committed Jul 16, 2012
2 parents fb35324 + 4f3405d commit b33c1ed
Show file tree
Hide file tree
Showing 14 changed files with 792 additions and 58 deletions.
303 changes: 265 additions & 38 deletions examples/notebooks/density.ipynb

Large diffs are not rendered by default.

264 changes: 264 additions & 0 deletions examples/notebooks/trace.ipynb
@@ -0,0 +1,264 @@
{
"metadata": {
"name": "trace"
},
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": true,
"input": [
"from sympy import symbols",
"from sympy.core.trace import Tr",
"from sympy.matrices.matrices import Matrix",
"from IPython.core.display import display_pretty",
"from sympy.printing.latex import *",
"",
"%load_ext sympyprinting"
],
"language": "python",
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"source": [
"###Basic Examples"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"a, b, c, d = symbols('a b c d'); ",
"A, B = symbols('A B', commutative=False)",
"t = Tr(A*B)"
],
"language": "python",
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t"
],
"language": "python",
"outputs": [
{
"latex": [
"$$\\mbox{Tr}\\left(A B\\right)$$"
],
"output_type": "pyout",
"prompt_number": 4,
"text": [
"Tr(A\u22c5B)"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"latex(t)"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 5,
"text": [
"\\mbox{Tr}\\left(A B\\right)"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"display_pretty(t)"
],
"language": "python",
"outputs": [
{
"output_type": "display_data",
"text": [
"Tr(\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5)))"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"source": [
"### Using Matrices"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"t = Tr ( Matrix([ [2,3], [3,4] ]))"
],
"language": "python",
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t"
],
"language": "python",
"outputs": [
{
"latex": [
"$$6$$"
],
"output_type": "pyout",
"png": "iVBORw0KGgoAAAANSUhEUgAAAAwAAAASCAYAAABvqT8MAAAABHNCSVQICAgIfAhkiAAAAO5JREFU\nKJHN0r1KQ0EQhuHnhAgBhaiIFpLOxs5O8CIsFG/A1spCL0CwsUtnaat4C7aWNooiCAEJKBb+oMGg\nSCzOHlyWlWDnV+3M7vvN7O7wRxWZXAs7+MIL3rCHXs5gGh0shXgS19isDtQSYB9tnIa4jgZec+5r\n+MD4sHtUOsTlsEP1aL2AeyxiGbNoYgs3KTiqfJVzbET5FTxhLgVmMEAfY1G+FqoepcBIAC4ybZ8p\n/6KoHOATd6F8ql5oeSIG4ARTGaCBLh7TjVW8V05BBZ5xkDECx9j1M2PruIpN0uFrYhvzofcH5ajc\n/lbhH+gb6f4rZTpaz0QAAAAASUVORK5CYII=\n",
"prompt_number": 16,
"text": [
"6"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"source": [
"### Example using modules in physics.quantum"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"from sympy.physics.quantum.density import Density",
"from sympy.physics.quantum.spin import (",
" Jx, Jy, Jz, Jplus, Jminus, J2,",
" JxBra, JyBra, JzBra,",
" JxKet, JyKet, JzKet,",
")"
],
"language": "python",
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d = Density([JzKet(1,1),0.5],[JzKet(1,-1),0.5]); d"
],
"language": "python",
"outputs": [
{
"latex": [
"$$\\rho\\left(\\begin{pmatrix}{\\left|1,1\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}{\\left|1,-1\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
],
"output_type": "pyout",
"prompt_number": 8,
"text": [
"\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5))"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"t = Tr(d)"
],
"language": "python",
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t"
],
"language": "python",
"outputs": [
{
"latex": [
"$$\\mbox{Tr}\\left(\\rho\\left(\\begin{pmatrix}{\\left|1,1\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}{\\left|1,-1\\right\\rangle }, & 0.5\\end{pmatrix}\\right)\\right)$$"
],
"output_type": "pyout",
"prompt_number": 10,
"text": [
"Tr(\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5)))"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"latex(t)"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 11,
"text": [
"",
"\\mbox{Tr}\\left(\\rho\\left(\\begin{pmatrix}{\\left|1,1\\right\\rangle }, & 0.5\\end{p",
"matrix},\\begin{pmatrix}{\\left|1,-1\\right\\rangle }, & 0.5\\end{pmatrix}\\right)\\r",
"ight)"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t.doit()"
],
"language": "python",
"outputs": [
{
"latex": [
"$$1.0$$"
],
"output_type": "pyout",
"png": "iVBORw0KGgoAAAANSUhEUgAAABsAAAASCAYAAACq26WdAAAABHNCSVQICAgIfAhkiAAAASNJREFU\nOI3t1EErBGEcx/HPimLXgUiU5eKinGxyc8KLkLeDK+WqpChcpJQjBxc33JALsgfFwYrEOsws0za7\nBpOT3+U/z/eZZ77PMzPPwx8mU4P3Yg8DCZ/TjDlkUQzbqziuNyiHSZyinFAEy1iLtMdxja5aAwax\nhVkcfEPWjxdMVPErrCSdaVLZPB4Fry6aJdyioQIa/D4FXOKpip+jA0NpyrrxEMNLYe1JW1aK4RXW\nlqbsGW8xvCmsH31pyE5q8FxYb9KUHaM9hreGtZi2LB/DR3Av+FN/JcujJbw+EqxsNNLfiDEsCr5p\n3WwLNnXccVPAK3bDdgab2PA5+SnBduisJejCPs5CURl3OMR05L4+XGAmwrKCk2QdC9jB8Fcr+s+P\n8g572TfbrLZhHwAAAABJRU5ErkJggg==\n",
"prompt_number": 12,
"text": [
"1.00000000000000"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": true,
"input": [],
"language": "python",
"outputs": [],
"prompt_number": 12
}
]
}
]
}
30 changes: 25 additions & 5 deletions sympy/core/tests/test_trace.py
@@ -1,4 +1,4 @@
from sympy import S, Symbol, symbols, Matrix from sympy import S, Symbol, symbols, Matrix, Tuple
from sympy.core.trace import Tr from sympy.core.trace import Tr


def test_trace_new(): def test_trace_new():
Expand All @@ -19,6 +19,8 @@ def test_trace_new():
assert Tr(a*C*A*D*B*2) == 2*a*Tr(A*D*B*C) assert Tr(a*C*A*D*B*2) == 2*a*Tr(A*D*B*C)
assert Tr(B*A*C*B*A) == Tr(A*C*B*A*B) assert Tr(B*A*C*B*A) == Tr(A*C*B*A*B)
assert Tr(A*C*B*A*B) == Tr(A*B*A*C*B) assert Tr(A*C*B*A*B) == Tr(A*B*A*C*B)
assert Tr(A*A*B*B) == Tr(A**2*B**2)
assert Tr(A*B*B*A) == Tr(A**2*B**2)


# since A is symbol and not commutative # since A is symbol and not commutative
assert isinstance(Tr(A), Tr) assert isinstance(Tr(A), Tr)
Expand All @@ -32,14 +34,32 @@ def test_trace_new():
assert Tr(M) == 3 assert Tr(M) == 3


#trace indices test #trace indices test
t = Tr((A+B), (2)) t = Tr((A+B), [2])
assert t.args[0].args[1] == (2) and t.args[1].args[1] == (2) assert t.args[0].args[1] == Tuple(2) and t.args[1].args[1] == Tuple(2)


t = Tr(a*A, (2,3)) t = Tr(a*A, [2,3])
assert t.args[1].args[1] == (2,3) assert t.args[1].args[1] == Tuple(2,3)


def test_trace_doit(): def test_trace_doit():
a, b, c, d = symbols('a b c d') a, b, c, d = symbols('a b c d')
A, B, C, D = symbols('A B C D', commutative=False) A, B, C, D = symbols('A B C D', commutative=False)


#TODO: needed while testing reduced density operations, etc. #TODO: needed while testing reduced density operations, etc.

#def test_permute():
# A, B, C, D, E, F, G = symbols('A B C D E F G', commutative=False)
# t = Tr(A*B*C*D*E*F*G, cycle=False)

# assert t.permute(0).args[0].args == (A, B, C, D, E, F, G)
# assert t.permute(2).args[0].args == (F, G, A, B, C, D, E)
# assert t.permute(4).args[0].args == (D, E, F, G, A, B, C)
# assert t.permute(6).args[0].args == (B, C, D, E, F, G, A)
# assert t.permute(8).args[0].args == t.permute(1).args[0].args

# assert t.permute(-1).args[0].args == (B, C, D, E, F, G, A)
# assert t.permute(-3).args[0].args == (D, E, F, G, A, B, C)
# assert t.permute(-5).args[0].args == (F, G, A, B, C, D, E)
# assert t.permute(-8).args[0].args == t.permute(-1).args[0].args

# t = Tr((A+B)*(B*B)*C*D,cycle=False)
# assert t.permute(2).args[0].args == (C, D, (A+B), (B**2))

0 comments on commit b33c1ed

Please sign in to comment.