Skip to content
This repository
Browse code

Merge pull request #1396 from gdevanla/trace_permute_equals

Tr printing, Tr partial trace
  • Loading branch information...
commit b33c1ede0f0a1bcb0c0c92f955fedd1d00ad7104 2 parents fb35324 + 4f3405d
Brian E. Granger authored July 16, 2012
303  examples/notebooks/density.ipynb
@@ -28,7 +28,7 @@
28 28
      ], 
29 29
      "language": "python", 
30 30
      "outputs": [], 
31  
-     "prompt_number": 22
  31
+     "prompt_number": 3
32 32
     }, 
33 33
     {
34 34
      "cell_type": "code", 
@@ -38,7 +38,7 @@
38 38
      ], 
39 39
      "language": "python", 
40 40
      "outputs": [], 
41  
-     "prompt_number": 23
  41
+     "prompt_number": 4
42 42
     }, 
43 43
     {
44 44
      "cell_type": "markdown", 
@@ -55,7 +55,7 @@
55 55
      ], 
56 56
      "language": "python", 
57 57
      "outputs": [], 
58  
-     "prompt_number": 24
  58
+     "prompt_number": 5
59 59
     }, 
60 60
     {
61 61
      "cell_type": "code", 
@@ -70,13 +70,13 @@
70 70
         "$$\\rho\\left(\\begin{pmatrix}{\\left|\\psi\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}{\\left|\\phi\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
71 71
        ], 
72 72
        "output_type": "pyout", 
73  
-       "prompt_number": 25, 
  73
+       "prompt_number": 6, 
74 74
        "text": [
75 75
         "\u03c1((\u2758\u03c8\u27e9, 0.5),(\u2758\u03c6\u27e9, 0.5))"
76 76
        ]
77 77
       }
78 78
      ], 
79  
-     "prompt_number": 25
  79
+     "prompt_number": 6
80 80
     }, 
81 81
     {
82 82
      "cell_type": "code", 
@@ -93,7 +93,7 @@
93 93
        ]
94 94
       }
95 95
      ], 
96  
-     "prompt_number": 26
  96
+     "prompt_number": 7
97 97
     }, 
98 98
     {
99 99
      "cell_type": "code", 
@@ -108,13 +108,13 @@
108 108
         "$$\\begin{pmatrix}{\\left|\\psi\\right\\rangle }, & {\\left|\\phi\\right\\rangle }\\end{pmatrix}$$"
109 109
        ], 
110 110
        "output_type": "pyout", 
111  
-       "prompt_number": 27, 
  111
+       "prompt_number": 8, 
112 112
        "text": [
113 113
         "(\u2758\u03c8\u27e9, \u2758\u03c6\u27e9)"
114 114
        ]
115 115
       }
116 116
      ], 
117  
-     "prompt_number": 27
  117
+     "prompt_number": 8
118 118
     }, 
119 119
     {
120 120
      "cell_type": "code", 
@@ -129,13 +129,13 @@
129 129
         "$$\\begin{pmatrix}0.5, & 0.5\\end{pmatrix}$$"
130 130
        ], 
131 131
        "output_type": "pyout", 
132  
-       "prompt_number": 28, 
  132
+       "prompt_number": 9, 
133 133
        "text": [
134 134
         "(0.5, 0.5)"
135 135
        ]
136 136
       }
137 137
      ], 
138  
-     "prompt_number": 28
  138
+     "prompt_number": 9
139 139
     }, 
140 140
     {
141 141
      "cell_type": "code", 
@@ -151,13 +151,13 @@
151 151
        ], 
152 152
        "output_type": "pyout", 
153 153
        "png": "iVBORw0KGgoAAAANSUhEUgAAAKoAAAAXCAYAAACbItQpAAAABHNCSVQICAgIfAhkiAAABZhJREFU\naIHt2m2IHdUZB/DfNoYYk1ijgVRtUFpL0ITUVK00GkwQCrVtaPohEVETo7aQUGxDQRFsaSn0Sw1E\nQdRgu2nVFqHS4JdWgm6rokaE+Ja+qEgt1cZopfXdaNIPz8zu3Lln7s7dO7t3U+4flrtzznPm/8x/\nzstznjMMMMAAbbgZJ/bbiZo4knwdD0fqs4z6/YlSxafxa2zDD/ETnFDjhvfhN1iDhbgKD2BTyW4e\nZtV0cnuibB1W1Wz/Axw/QR6687VXTCfd4WeJssnQvhMfBb+LHXUGRvAgtuJHWfnd2jt06obrsQv/\nwg48gp934WwZRyXK7sOGGm2PES//3xPkmUpMN91hZqJsMrTvxNeCohAXYxHuKpTtxJexsQbZTfit\nEHoFbqjrZQKn4flE+Xv4Z1bfCRvwqx54esUXurCdTrrD5/C3RHnT2o/HV4m9Ytko40XcO07bkZoc\nwzi1ht2VOLOi7iTc2KHtEO6s6U8nnmH1fE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OILbkZXNwddLjQ/g73gyu35bpHuuF7PAITH9X6jzRoBIal+E67KHOFfMIodxqdaYsoirjKVk\nyrhH5BaLJyQT5ZkK9EN3YiP3paz9MyI+vFFkIn6qOe175esLhlXPUmeonkGLOEk62ZxjSOu5+UR5\nhk3NjDoVGNb5WVaLbw7GQ6/ad8s3rHSEOh2wEg/XsNsslu4qHM7us7JHnl7w2vgm0wqr1RtcvWrf\nLd8oprqjHtKeksixVMRXnTBbpDheGMduJy7vgYfOvo6HdRNsN1kY71lOFJ8JdkIT2nfDR8Hvqf4W\n8xqRV0vhI51PYYi4bWcNnnfxD3EkV/4usg4PnX090tDpWWZJH+GW0YT23fDx//UOBhhggAEGGGCA\n+vgfqAPSYqWMIZsAAAAASUVORK5CYII=\n", 
154  
-       "prompt_number": 29, 
  154
+       "prompt_number": 10, 
155 155
        "text": [
156 156
         "0.5\u22c5\u2758\u03c6\u27e9\u27e8\u03c6\u2758 + 0.5\u22c5\u2758\u03c8\u27e9\u27e8\u03c8\u2758"
157 157
        ]
158 158
       }
159 159
      ], 
160  
-     "prompt_number": 29
  160
+     "prompt_number": 10
161 161
     }, 
162 162
     {
163 163
      "cell_type": "code", 
@@ -172,13 +172,13 @@
172 172
         "$$\\rho\\left(\\begin{pmatrix}{\\left|\\psi\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}{\\left|\\phi\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
173 173
        ], 
174 174
        "output_type": "pyout", 
175  
-       "prompt_number": 30, 
  175
+       "prompt_number": 11, 
176 176
        "text": [
177 177
         "\u03c1((\u2758\u03c8\u27e9, 0.5),(\u2758\u03c6\u27e9, 0.5))"
178 178
        ]
179 179
       }
180 180
      ], 
181  
-     "prompt_number": 30
  181
+     "prompt_number": 11
182 182
     }, 
183 183
     {
184 184
      "cell_type": "code", 
@@ -188,7 +188,7 @@
188 188
      ], 
189 189
      "language": "python", 
190 190
      "outputs": [], 
191  
-     "prompt_number": 31
  191
+     "prompt_number": 12
192 192
     }, 
193 193
     {
194 194
      "cell_type": "code", 
@@ -203,13 +203,13 @@
203 203
         "$$\\rho\\left(\\begin{pmatrix}A {\\left|\\psi\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}A {\\left|\\phi\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
204 204
        ], 
205 205
        "output_type": "pyout", 
206  
-       "prompt_number": 32, 
  206
+       "prompt_number": 13, 
207 207
        "text": [
208 208
         "\u03c1((A\u22c5\u2758\u03c8\u27e9, 0.5),(A\u22c5\u2758\u03c6\u27e9, 0.5))"
209 209
        ]
210 210
       }
211 211
      ], 
212  
-     "prompt_number": 32
  212
+     "prompt_number": 13
213 213
     }, 
214 214
     {
215 215
      "cell_type": "markdown", 
@@ -226,7 +226,7 @@
226 226
      ], 
227 227
      "language": "python", 
228 228
      "outputs": [], 
229  
-     "prompt_number": 33
  229
+     "prompt_number": 14
230 230
     }, 
231 231
     {
232 232
      "cell_type": "code", 
@@ -241,13 +241,13 @@
241 241
         "$$\\rho\\left(\\begin{pmatrix}{\\left|\\frac{1}{2},\\frac{1}{2}\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}{\\left|\\frac{1}{2},- \\frac{1}{2}\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
242 242
        ], 
243 243
        "output_type": "pyout", 
244  
-       "prompt_number": 34, 
  244
+       "prompt_number": 15, 
245 245
        "text": [
246 246
         "\u03c1((\u27581/2,1/2\u27e9, 0.5),(\u27581/2,-1/2\u27e9, 0.5))"
247 247
        ]
248 248
       }
249 249
      ], 
250  
-     "prompt_number": 34
  250
+     "prompt_number": 15
251 251
     }, 
252 252
     {
253 253
      "cell_type": "code", 
@@ -262,7 +262,7 @@
262 262
         "$$\\left[\\begin{smallmatrix}0.5 & 0\\\\0 & 0.5\\end{smallmatrix}\\right]$$"
263 263
        ], 
264 264
        "output_type": "pyout", 
265  
-       "prompt_number": 35, 
  265
+       "prompt_number": 16, 
266 266
        "text": [
267 267
         "", 
268 268
         "\u23a10.5   0 \u23a4", 
@@ -271,7 +271,7 @@
271 271
        ]
272 272
       }
273 273
      ], 
274  
-     "prompt_number": 35
  274
+     "prompt_number": 16
275 275
     }, 
276 276
     {
277 277
      "cell_type": "code", 
@@ -286,7 +286,7 @@
286 286
         "$$\\rho\\left(\\begin{pmatrix}J_z {\\left|\\frac{1}{2},\\frac{1}{2}\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}J_z {\\left|\\frac{1}{2},- \\frac{1}{2}\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
287 287
        ], 
288 288
        "output_type": "pyout", 
289  
-       "prompt_number": 36, 
  289
+       "prompt_number": 17, 
290 290
        "text": [
291 291
         "", 
292 292
         "\u03c1\u239b\u239bJ \u22c5\u27581/2,1/2\u27e9, 0.5\u239e,\u239bJ \u22c5\u27581/2,-1/2\u27e9, 0.5\u239e\u239e", 
@@ -294,7 +294,7 @@
294 294
        ]
295 295
       }
296 296
      ], 
297  
-     "prompt_number": 36
  297
+     "prompt_number": 17
298 298
     }, 
299 299
     {
300 300
      "cell_type": "code", 
@@ -309,7 +309,7 @@
309 309
         "$$\\rho\\left(\\begin{pmatrix}\\frac{1}{2} \\hbar {\\left|\\frac{1}{2},\\frac{1}{2}\\right\\rangle }, & 0.5\\end{pmatrix},\\begin{pmatrix}- \\frac{1}{2} \\hbar {\\left|\\frac{1}{2},- \\frac{1}{2}\\right\\rangle }, & 0.5\\end{pmatrix}\\right)$$"
310 310
        ], 
311 311
        "output_type": "pyout", 
312  
-       "prompt_number": 37, 
  312
+       "prompt_number": 18, 
313 313
        "text": [
314 314
         "", 
315 315
         " \u239b\u239b\u210f\u22c5\u27581/2,1/2\u27e9     \u239e \u239b-\u210f\u22c5\u27581/2,-1/2\u27e9     \u239e\u239e", 
@@ -318,7 +318,7 @@
318 318
        ]
319 319
       }
320 320
      ], 
321  
-     "prompt_number": 37
  321
+     "prompt_number": 18
322 322
     }, 
323 323
     {
324 324
      "cell_type": "code", 
@@ -334,7 +334,7 @@
334 334
        ], 
335 335
        "output_type": "pyout", 
336 336
        "png": "iVBORw0KGgoAAAANSUhEUgAAAZ0AAAAlCAYAAACZMTQOAAAABHNCSVQICAgIfAhkiAAACElJREFU\neJztnWeMFkUYgB/OO0VBxFgQ2w9RVCxYYtTYTkXsJRorNkw0miixxBbrHxSJPdFoNIIGhaAQjV1B\nwRbFQmJJ7CWKXWNDxYL+eL8vt/fdfvvNzO7Olnmf5MLdMLP73nzz3O7MzsyCoiiKoihKRkwC9kpR\nfhNgSkaxKEpdSeOZOqbUijHAzBTlrwIOyCgWRakraTxTx5TasRAY7lCuC1gErJBtOIpSS1w8C86x\nrqIDyIjtkQ/cllWBwzKOJS/SxDoTONah3D7As8C/FmVCqVNwb3chUhVHfXtWBcfUkxjGAR9a5F8X\nOA94EZiRS0TZkUWsw3H70GcBmxnmDa1Owb7dhUzZHS3KszI7losn3SmDqio/A7cCK2P+gRdFFrH+\nBCwBtgDeMSyzOrAO8K5h/tDqVMkX359REZ6V3bFczleX4TVblgJ/FB2EIVnFOg042SL/sdg9GA2x\nTpX88P0ZFeFZ2R3L5XyhXnRCZD6wB+a926ORrr+iKObYeBakY+0uOrYXozTHWRkY2vI12PL8SmeW\nA08C+xvk3QIZJvg514gUV1xuFtVRP5h6FqxjrQ1uY+AhYCpwBXA9MubYiceBR4DjgFHA2cg0wJM6\nlOsB7gHmAF8AvwJvNc6tZM90zLr+Ext5lXLh6ieooz6ZTmfPgnUs2gXsQbqGlwN3N9IuQ7p/e5M8\npW8lYDxwYOPn/4CLkfHNJP4Gjmx8/yCyQGoH4Huz8BVLPgKGAWsB37XJ0w30Ahd4iilUtgI+Rf6I\nm5DGT1BHfdLJs6Adi/Z0JgAjgPsjafci45OnGhzrKkSG84FtgGss49gdeBU/jbkLWBvYCNgAmRpY\n1pl8Wcd6L/JZt2N/ZHhgueVxQ65TFyYgQyw2+dP4Ceqoz/MleVYVx3I536DI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s9n7eTZ0E7Ft3S4FfgIOSh7Z3I3c0y4NCW\nMl/S/yr5B3BWo9zayN3TZ8jK6eiitd+QxVJXIC8vWgM4kfh1Akcg+zs9RZ88i5CKymLK5grIYrfB\nyNjqEmTc2+aVsZORjRD3zSCeJLKItcnJmG0yOA24BLs/NKHWqS9c/QR11KQ9+fasCo5V0RMnxiLD\nDpcCVzbSxpC8ZYRt1/0c+lYKdyPrFK62ilI4AtkeJE+yirULGa833TtpIbLo0JQQ6zTNkNEF2O29\nVibq6GgRnpXdsVw8KeNL3JYg+1GdQN94cy/27x5PYmdk00OQru48zN4zE6UH2BN5wJsnWcQKMm13\nYeMYJswCjrE4foh1moaplHNfOhPq6GgRnpXdsTJ44o2t6P/e8NlIA2+H7V3UevSfCTQTeMCiPMgY\nuctGj7ZkESvI1hs2d9bDsVuXEWKdhjYNOErdHC3Cs7I7FpQnByI78IJM8fuWgdNFo6T5pTZB1jCM\ncizvE9dYh+N2FzoT2avLlhDqFCoiU07U2VGfnlXFsdp7sjrypsPjkWGITzrkd/2lhgGPAls6lPVN\nmlhPB85wKDceu3euQDh1ChWRKSfq6qhvz6rgWFCedAE3INM2k3D5pQYjr/sd2fh5jGV5n6SNdSFy\nF2ZLF/AKMovFhJDqFComU07UydEiPCu7Y5l7UsaJBEORAEcgu+seTOcGbUs3MvPmdmTq6Ujkjq2M\npI11DDKN1uSdK60sR17aZDK9MqQ6DZ06OlqUZ2V2LBhPepA57qcgi9JGG5TZHlkkZ8pNDNwVNW6r\n/zKQNtZJyGwVVzYGrjXIF1KdNrFtd3Whjo4W6VlZHQvOkzL2whRF6UMdVRRFURRFURRFURRFURRF\nURRFUZQa8j/bVKmwXxjmUgAAAABJRU5ErkJggg==\n", 
337  
-       "prompt_number": 38, 
  337
+       "prompt_number": 19, 
338 338
        "text": [
339 339
         "", 
340 340
         "0.5\u22c5J \u22c5\u27581/2,-1/2\u27e9\u27e81/2,-1/2\u2758 + 0.5\u22c5J \u22c5\u27581/2,1/2\u27e9\u27e81/2,1/2\u2758", 
@@ -342,12 +342,12 @@
342 342
        ]
343 343
       }
344 344
      ], 
345  
-     "prompt_number": 38
  345
+     "prompt_number": 19
346 346
     }, 
347 347
     {
348 348
      "cell_type": "markdown", 
349 349
      "source": [
350  
-      "Evaluate entropy of the density matrices"
  350
+      "## Evaluate entropy of the density matrices"
351 351
      ]
352 352
     }, 
353 353
     {
@@ -364,7 +364,7 @@
364 364
        ], 
365 365
        "output_type": "pyout", 
366 366
        "png": "iVBORw0KGgoAAAANSUhEUgAAAEUAAAAiCAYAAAAJfBTLAAAABHNCSVQICAgIfAhkiAAAA+RJREFU\naIHt2HuIVVUUx/GPw4w5TtmkZTVmNSkRJTlkRVmYGFH0+qOggoygwv8qIaI/sockTERlEVH0/Ksw\nKeglg1lCRlSERJAUNOlYGoWpPaSHPaY/1jncfe/cO9eZ5sytvF+43LP3WfucddZZ+7f2PjT5RxyC\n1kY78W/hACzAZhzbWFfGh5Z9sLkSB6G7YF/+kwxqZsr+SzMoVSi6mszFhZiGt/FawfebiL1J+2Jc\nJKb+BqzBT8PYo/hM2YOfcQvmFXyvI3Bd0l6JhbgPq7BUBObwxOZWEZhRMUFEe7QVaDvuHu3N94EW\nPKaU+YvwcoXNMeIlbUj6ZuOJaherx7V4GhvRKyI+Un4fxZiRcA3W44+sfSk6lFfLrfgQ89GZ9fVj\nG3oK9q8qA4rNlHXKp8HjIrNvrrB7Nus/O+mbgydTo0Yu2+cJEZyIL7AWX1fYtIgsOBNfYjUOxVXY\niXswBW3KBXMZPsCLFdebJYKyKenrF8Vg3BlQnim3iUo0NWvPwrs4I7FpwSvow3Qcj0+zcTOy4zZR\n4Vbtgw8zReBer3JuM9orOwcL+KUMKAXlPPwm3njKAuzCcVl7YXadxYnNA/hFbDtyFgnNq8dTIrtm\nVjm3XrwYlIR2QgG/WizDx/iuov8d8bBXZO08sNsTm+8xSSnD4IcaD5pyCS4TL+SrKuePVL5+GRcG\nRKZMwI94s4bdLiGaORvFVMt5A+9XjDkMW4a590lCs2qtk1qxW1KJx1toB8WDH1zlXKf4ZrMn6XtV\naElv9r9DKZNydohsaRdTK2U6nhElOhfXGaJ0f5u1u0WW/pUPakT1WSfSOV8Q5vQk53M6cL14i9W0\nKud5oUlrk752UYJvUF5tLheZmgdlIZ4b4TOMCQNKQjtbLJgWV9jcJcpomkWfKQnvcEwSAcnXKi14\nCfeK8p3/rhZZkdtNzcYNp4FVaceNWIElRrZXmIv7RWpuEXuNVqH0fXg0c3a5WFNMqRi/En+KKbVb\nBGmNEMxKenBTdrxc7ar4eTJmhaTq5NSLUAsewZ2ZUw/iVJyTOVuPbiF0KX3J2E6ciI8M1YNFYiO5\nWpTSNhyd2S8RwXyhYswMUa1OU77xS9mJ97LjmapXo2GZg0+EYEGXiPZZI73QKOjH6TXOPaT6ImxM\nqLch3CqE79esnW+42opyKGGT8hVuyhRDS/OYMVKBWSrEar7id75H4Q5Mxlv4Rmz/zxfZ22volBt3\nThD7k65xvm8HTsa5mQ+j/ig01nSJjzF5udzvv+1Ow+1KOnIKLmicO8VTT1MOFEK7V2k12SXK5bYC\n/Woo9Zb5k/FwRd+g/3FAmjRp0mSs+Rvvgds8ldAxMgAAAABJRU5ErkJggg==\n", 
367  
-       "prompt_number": 39, 
  367
+       "prompt_number": 20, 
368 368
        "text": [
369 369
         "", 
370 370
         "log(2)", 
@@ -373,7 +373,7 @@
373 373
        ]
374 374
       }
375 375
      ], 
376  
-     "prompt_number": 39
  376
+     "prompt_number": 20
377 377
     }, 
378 378
     {
379 379
      "cell_type": "code", 
@@ -389,7 +389,7 @@
389 389
        ], 
390 390
        "output_type": "pyout", 
391 391
        "png": "iVBORw0KGgoAAAANSUhEUgAAAEUAAAAiCAYAAAAJfBTLAAAABHNCSVQICAgIfAhkiAAAA+RJREFU\naIHt2HuIVVUUx/GPw4w5TtmkZTVmNSkRJTlkRVmYGFH0+qOggoygwv8qIaI/sockTERlEVH0/Ksw\nKeglg1lCRlSERJAUNOlYGoWpPaSHP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3CqE79esnW+42opyKGGT8hVuyhRDS/OYMVKBWSrEar7id75H4Q5Mxlv4Rmz/zxfZ22volBt3\nThD7k65xvm8HTsa5mQ+j/ig01nSJjzF5udzvv+1Ow+1KOnIKLmicO8VTT1MOFEK7V2k12SXK5bYC\n/Woo9Zb5k/FwRd+g/3FAmjRp0mSs+Rvvgds8ldAxMgAAAABJRU5ErkJggg==\n", 
392  
-       "prompt_number": 40, 
  392
+       "prompt_number": 21, 
393 393
        "text": [
394 394
         "", 
395 395
         "log(2)", 
@@ -398,7 +398,7 @@
398 398
        ]
399 399
       }
400 400
      ], 
401  
-     "prompt_number": 40
  401
+     "prompt_number": 21
402 402
     }, 
403 403
     {
404 404
      "cell_type": "code", 
@@ -410,13 +410,13 @@
410 410
      "outputs": [
411 411
       {
412 412
        "output_type": "pyout", 
413  
-       "prompt_number": 41, 
  413
+       "prompt_number": 22, 
414 414
        "text": [
415 415
         "(0.69314718056-0j)"
416 416
        ]
417 417
       }
418 418
      ], 
419  
-     "prompt_number": 41
  419
+     "prompt_number": 22
420 420
     }, 
421 421
     {
422 422
      "cell_type": "code", 
@@ -428,13 +428,240 @@
428 428
      "outputs": [
429 429
       {
430 430
        "output_type": "pyout", 
431  
-       "prompt_number": 42, 
  431
+       "prompt_number": 23, 
432 432
        "text": [
433 433
         "(0.69314718056-0j)"
434 434
        ]
435 435
       }
436 436
      ], 
437  
-     "prompt_number": 42
  437
+     "prompt_number": 23
  438
+    }, 
  439
+    {
  440
+     "cell_type": "markdown", 
  441
+     "source": [
  442
+      "## Density operators with Tensor Products as args"
  443
+     ]
  444
+    }, 
  445
+    {
  446
+     "cell_type": "code", 
  447
+     "collapsed": false, 
  448
+     "input": [
  449
+      "from sympy.core.trace import Tr", 
  450
+      "", 
  451
+      "A, B, C, D = symbols('A B C D',commutative=False)", 
  452
+      "", 
  453
+      "t1 = TensorProduct(A,B,C)", 
  454
+      "", 
  455
+      "d = Density([t1, 1.0])", 
  456
+      "d.doit()", 
  457
+      "", 
  458
+      "t2 = TensorProduct(A,B)", 
  459
+      "t3 = TensorProduct(C,D)", 
  460
+      "", 
  461
+      "d = Density([t2, 0.5], [t3, 0.5])", 
  462
+      "d.doit() "
  463
+     ], 
  464
+     "language": "python", 
  465
+     "outputs": [
  466
+      {
  467
+       "latex": [
  468
+        "$$0.5 \\left({A A^{\\dagger}}\\right)\\otimes \\left({B B^{\\dagger}}\\right) + 0.5 \\left({C C^{\\dagger}}\\right)\\otimes \\left({D D^{\\dagger}}\\right)$$"
  469
+       ], 
  470
+       "output_type": "pyout", 
  471
+       "png": "iVBORw0KGgoAAAANSUhEUgAAAUkAAAAaCAYAAAA6565FAAAABHNCSVQICAgIfAhkiAAACTZJREFU\neJztnHuMHVUdxz93i1sW21Ity2tbsCVaRFo1UF6pbW1BNDwkRFAJ0Rof0CogFDSkFZcVC20o4aFE\nUR4tAeVl1NS3KS0qCCrFEnkoyNqGVwElYK0PyvrHdyZ37uw5M2dmzrCz4XySm/bOOb8z537nN2d+\n53fOLAQCgUCgkfQDE0e6E8AY4KGcOrsBF7wGfTHRT9CpSfQzstej6dehn2b4a5JKmnV5744bK4F1\nwIsjdP4kO4CXcuo8H9U5vf7udBB0ahZNuB5Nvg5N0MdEkzUzMg+4bKQ7kWKFQ50xwH3A7Jr7EjOP\noFOTmEdzrkcTr8M8mqOPiSZqZqQF3A3sM5KdSPAT4EZgDbAW+FpO/VnA9+ruFEGnptGU69HU69AU\nfUzUplkPGlVd6bEc3zn1/TDgZwXaTTOhJtsLC7TzADClQj9ccNGpG5ic8RlXg23TdEpS1GdjGxM+\n/XYCGkRczutq17Tr4KLPROz+tofjeaq0UVqznVKFM1HIfC+wHdgXuAj4W06jP0I33s2R7XHAR4EB\n4KZEvWOAewp0NsnxwBXA1BpsDyjQ1uPAJ9BvqwsXnWYAS4Aj0HW6G3g4KmsB84H/oET6dz3ZNk0n\nKO+zUI/fvgFYCMwB3gY8CfQCG4HzgWnAecDHKtg17Tq46HMK8F7gBDTu3Aq8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Xsc2jiE4mig6SZX0W/Pkt0fn/jlaD43xZH1oYuhx7Dq2s\nXR5N89e1DM/ztlDe70qUh87Lu/poIwurZund9XshR3pz9P1FlBg3bbNIMg7lMqaiH7IJTYVezjJK\ncD16q6CFnq5L6fzDCovRk2RS9P05dCOcX9G2LnYGvoAG51vRBXwF6fM+tM1mAPuL/DYWoUGuLzrH\nS7T3c7XQdbgH+AVaqfRl+1pRJCcZU9ZnobrfJjkK5bOOQBHN48Cd6L3rOux8Upe/3oIG2PjaDNLe\n19qDpud3Ip+z5Rl9tFEJ27uiAT9MRZtfD0CRy19R5FP0nebXC6eiQaLsX4oKVCP4ayAQCAQCgUAg\nEAgEAoFAIBAIBAKBQKC5/B+xC2qm0KevJwAAAABJRU5ErkJggg==\n", 
  472
+       "prompt_number": 24, 
  473
+       "text": [
  474
+        "", 
  475
+        "    \u239b   \u2020\u239e  \u239b   \u2020\u239e       \u239b   \u2020\u239e  \u239b   \u2020\u239e", 
  476
+        "0.5\u22c5\u239dA\u22c5A \u23a0\u2a02 \u239dB\u22c5B \u23a0 + 0.5\u22c5\u239dC\u22c5C \u23a0\u2a02 \u239dD\u22c5D \u23a0"
  477
+       ]
  478
+      }
  479
+     ], 
  480
+     "prompt_number": 24
  481
+    }, 
  482
+    {
  483
+     "cell_type": "code", 
  484
+     "collapsed": false, 
  485
+     "input": [
  486
+      "#mixed states", 
  487
+      "d = Density([t2+t3, 1.0])", 
  488
+      "d.doit() "
  489
+     ], 
  490
+     "language": "python", 
  491
+     "outputs": [
  492
+      {
  493
+       "latex": [
  494
+        "$$1.0 \\left({A A^{\\dagger}}\\right)\\otimes \\left({B B^{\\dagger}}\\right) + 1.0 \\left({A C^{\\dagger}}\\right)\\otimes \\left({B D^{\\dagger}}\\right) + 1.0 \\left({C A^{\\dagger}}\\right)\\otimes \\left({D B^{\\dagger}}\\right) + 1.0 \\left({C C^{\\dagger}}\\right)\\otimes \\left({D D^{\\dagger}}\\right)$$"
  495
+       ], 
  496
+       "output_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td3r2GYuFOS9alp3aXJO+zc25NRRKT6Y4zkM5Ya1g+Cxk7MdmwDuRX\ne9H8YHXK2jCM7MRVXIIMBL4c89HhM4CrkR32SszjflKK4nQEIkzRnbKrk/WmRFinrA22cTLRVJLM\nOwtxeLseuUu3aILr+cj3sn2ucZ7x4uxKpK/WFJRdhtxU8daSdlVRx1vonbuhvK3rXpP51qUvQrlb\n5G0I9zTnCm2726V8G8LbqPLuJGTMwyFkEtg8cxDZ3plZdhXyCK90rMjbkLvOyubZWsfou/AWIAN9\nTePLQE5p70J24HwyrFPWhtmYO9/EZCR+ry/Z5jjkKMHm+en5OIFMzXIImUoiy2xEkseQO/4OZyx1\nylbhEicTvknSx1lo31uA9yNPzVjFyFii+cjZhY9hHl9kw3hydnNS9sTc8mOQG0n2Io8R9KWut9Bb\nd0N4C/7uNZlvXfsipLumGIVwT3PuCG2725V8G8Lb1vJu9okDc5Bnlc5hRJKDyNxSV1D+yKpJyDNl\nlyB3Zc1E7iy7x7KBVyNPWuhDjmjWMfr08QXIwOBZyfsHkTFNF9cs2xRTgfciCXor8HsklguBFyGD\n3IeRqUNcWIMkufnJZxxgZO6vPmSnugm5NHJ9wLK9wnW8Ux1noV1vs7wQOYtwKnKWaTfwIySB94oY\nnb0SOfNyZLLtvYzc9TkNcWU7Eqf9ju0KTS/drettFlf3upJvQ7inOXcsbbpbt1xImsq3IbxtPe+6\nPMtUcWchckSxGBk/80dk0PLNbTYqYs5BkkTRYG6ledRZP9Td9lF33VFv20WdVRRFURRFURRFURRF\nURRFURRFURRFURRFURRFURRFURRFURRFURRF+X/hf2jkcJDynyQoAAAAAElFTkSuQmCC\n", 
  498
+       "prompt_number": 25, 
  499
+       "text": [
  500
+        "", 
  501
+        "    \u239b   \u2020\u239e  \u239b   \u2020\u239e       \u239b   \u2020\u239e  \u239b   \u2020\u239e       \u239b   \u2020\u239e  \u239b   \u2020\u239e       \u239b   \u2020\u239e  \u239b  ", 
  502
+        "1.0\u22c5\u239dA\u22c5A \u23a0\u2a02 \u239dB\u22c5B \u23a0 + 1.0\u22c5\u239dA\u22c5C \u23a0\u2a02 \u239dB\u22c5D \u23a0 + 1.0\u22c5\u239dC\u22c5A \u23a0\u2a02 \u239dD\u22c5B \u23a0 + 1.0\u22c5\u239dC\u22c5C \u23a0\u2a02 \u239dD\u22c5", 
  503
+        "", 
  504
+        " \u2020\u239e", 
  505
+        "D \u23a0"
  506
+       ]
  507
+      }
  508
+     ], 
  509
+     "prompt_number": 25
  510
+    }, 
  511
+    {
  512
+     "cell_type": "markdown", 
  513
+     "source": [
  514
+      "## Trace operators on Density Operators with Spin States"
  515
+     ]
  516
+    }, 
  517
+    {
  518
+     "cell_type": "code", 
  519
+     "collapsed": false, 
  520
+     "input": [
  521
+      "from sympy.physics.quantum.density import Density", 
  522
+      "from sympy.physics.quantum.spin import (", 
  523
+      "    Jx, Jy, Jz, Jplus, Jminus, J2,", 
  524
+      "    JxBra, JyBra, JzBra,", 
  525
+      "    JxKet, JyKet, JzKet,", 
  526
+      ")", 
  527
+      "from sympy.core.trace import Tr", 
  528
+      "", 
  529
+      "d = Density([JzKet(1,1),0.5],[JzKet(1,-1),0.5]);", 
  530
+      "t = Tr(d); ", 
  531
+      "", 
  532
+      "display_pretty(t)", 
  533
+      "print t.doit()"
  534
+     ], 
  535
+     "language": "python", 
  536
+     "outputs": [
  537
+      {
  538
+       "output_type": "display_data", 
  539
+       "text": [
  540
+        "Tr(\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5)))"
  541
+       ]
  542
+      }, 
  543
+      {
  544
+       "output_type": "stream", 
  545
+       "stream": "stdout", 
  546
+       "text": [
  547
+        "1.00000000000000"
  548
+       ]
  549
+      }
  550
+     ], 
  551
+     "prompt_number": 26
  552
+    }, 
  553
+    {
  554
+     "cell_type": "markdown", 
  555
+     "source": [
  556
+      "## Partial Trace on Density Operators with Mixed State"
  557
+     ]
  558
+    }, 
  559
+    {
  560
+     "cell_type": "code", 
  561
+     "collapsed": false, 
  562
+     "input": [
  563
+      "#Partial trace on mixed state", 
  564
+      "from sympy.core.trace import Tr", 
  565
+      "", 
  566
+      "A, B, C, D = symbols('A B C D',commutative=False)", 
  567
+      "", 
  568
+      "t1 = TensorProduct(A,B,C)", 
  569
+      "", 
  570
+      "d = Density([t1, 1.0])", 
  571
+      "d.doit()", 
  572
+      "", 
  573
+      "t2 = TensorProduct(A,B)", 
  574
+      "t3 = TensorProduct(C,D)", 
  575
+      "", 
  576
+      "d = Density([t2, 0.5], [t3, 0.5])", 
  577
+      "", 
  578
+      "", 
  579
+      "tr = Tr(d,[1])", 
  580
+      "tr.doit()"
  581
+     ], 
  582
+     "language": "python", 
  583
+     "outputs": [
  584
+      {
  585
+       "latex": [
  586
+        "$$0.5 A A^{\\dagger} \\mbox{Tr}\\left(B B^{\\dagger}\\right) + 0.5 C C^{\\dagger} \\mbox{Tr}\\left(D D^{\\dagger}\\right)$$"
  587
+       ], 
  588
+       "output_type": "pyout", 
  589
+       "prompt_number": 27, 
  590
+       "text": [
  591
+        "", 
  592
+        "       \u2020       \u2020           \u2020       \u2020 ", 
  593
+        "0.5\u22c5A\u22c5A \u22c5Tr(B\u22c5B ) + 0.5\u22c5C\u22c5C \u22c5Tr(D\u22c5D )"
  594
+       ]
  595
+      }
  596
+     ], 
  597
+     "prompt_number": 27
  598
+    }, 
  599
+    {
  600
+     "cell_type": "markdown", 
  601
+     "source": [
  602
+      "## Partial Trace on Density Operators with Spin states"
  603
+     ]
  604
+    }, 
  605
+    {
  606
+     "cell_type": "code", 
  607
+     "collapsed": false, 
  608
+     "input": [
  609
+      "from sympy.physics.quantum.density import Density", 
  610
+      "from sympy.physics.quantum.spin import (", 
  611
+      "    Jx, Jy, Jz, Jplus, Jminus, J2,", 
  612
+      "    JxBra, JyBra, JzBra,", 
  613
+      "    JxKet, JyKet, JzKet,", 
  614
+      ")", 
  615
+      "from sympy.core.trace import Tr", 
  616
+      "", 
  617
+      "tp1 = TensorProduct(JzKet(1,1), JzKet(1,-1))", 
  618
+      "", 
  619
+      "#trace out 0 index", 
  620
+      "d = Density([tp1,1]);", 
  621
+      "t = Tr(d,[0]); ", 
  622
+      "", 
  623
+      "display_pretty(t)", 
  624
+      "display_pretty(t.doit())", 
  625
+      "", 
  626
+      "#trace out 1 index", 
  627
+      "t = Tr(d,[1])", 
  628
+      "display_pretty(t)", 
  629
+      "t.doit()", 
  630
+      ""
  631
+     ], 
  632
+     "language": "python", 
  633
+     "outputs": [
  634
+      {
  635
+       "output_type": "display_data", 
  636
+       "text": [
  637
+        "Tr((\u27581,1\u27e9\u2a02 \u27581,-1\u27e9, 1))"
  638
+       ]
  639
+      }, 
  640
+      {
  641
+       "output_type": "display_data", 
  642
+       "text": [
  643
+        "\u27581,-1\u27e9\u27e81,-1\u2758"
  644
+       ]
  645
+      }, 
  646
+      {
  647
+       "output_type": "display_data", 
  648
+       "text": [
  649
+        "Tr((\u27581,1\u27e9\u2a02 \u27581,-1\u27e9, 1))"
  650
+       ]
  651
+      }, 
  652
+      {
  653
+       "latex": [
  654
+        "$${\\left|1,1\\right\\rangle }{\\left\\langle 1,1\\right|}$$"
  655
+       ], 
  656
+       "output_type": "pyout", 
  657
+       "png": "iVBORw0KGgoAAAANSUhEUgAAAEwAAAAXCAYAAACh3qkfAAAABHNCSVQICAgIfAhkiAAAAedJREFU\nWIXt2D+I1EAUx/GPB+ohCqIiKqKNFjanveXaKNhaeI2Kglpb2Z2FnYKVgoqNeCDYXecfUgoWgoJy\nCGJhIQhiYWexFtksa8hO/kzYm2K/TSbz8t77zeMxyYQ5UbzYaAGJMq7LQsmwu+Lh/VjHlo7JQv63\nsKdhnHsdc9TRxLeqLiCbGG/DKXzCEIsthTTxX8LNBrFO4EbHHDH6CrJiUO6wgiNYxQA/W4ho6/8B\nx7C5Jt4VPOpRY+z6xmQVcw9067Cm/mexHPDfhzuROWJ9s2IwrcNmyRrOBOxX5YtKghQKNsRbnKyw\nLeIQvsxUUYAUCgZPcKFifhnPZislTCoF+4NfOFyaH+DV7OVMJ5WCwX1cm7gf4M0GaZlKSgUbYtPE\nfUraxqQk6rq8ywpeyrssKfoo2IEeYmyXH5G+leZfy7/GY+lDI5oVbNfourfCdhzf8bijf8FF+Zuy\nzFOcrxNYk6NOYxN9U8lG152j8bp8bxniN97h0sTzB/EVH/2//zT1N/JbDWhawdGK+RiNbfRRfQIK\nG2pY6ehHs6PR3Yj4BTEas2LQ16YfE+ccngfsP7BV3hUx9LLWcpC/HWKcxvuO+ZfwuUHeh7jcMQdx\nGgno29Ey0AJuRwjp6wdiiFiNtK/LnDlzZsM/36lpQdemeFcAAAAASUVORK5CYII=\n", 
  658
+       "prompt_number": 28, 
  659
+       "text": [
  660
+        "\u27581,1\u27e9\u27e81,1\u2758"
  661
+       ]
  662
+      }
  663
+     ], 
  664
+     "prompt_number": 28
438 665
     }, 
439 666
     {
440 667
      "cell_type": "code", 
@@ -442,7 +669,7 @@
442 669
      "input": [], 
443 670
      "language": "python", 
444 671
      "outputs": [], 
445  
-     "prompt_number": 42
  672
+     "prompt_number": 28
446 673
     }
447 674
    ]
448 675
   }
264  examples/notebooks/trace.ipynb
... ...
@@ -0,0 +1,264 @@
  1
+{
  2
+ "metadata": {
  3
+  "name": "trace"
  4
+ }, 
  5
+ "nbformat": 2, 
  6
+ "worksheets": [
  7
+  {
  8
+   "cells": [
  9
+    {
  10
+     "cell_type": "code", 
  11
+     "collapsed": true, 
  12
+     "input": [
  13
+      "from sympy import symbols", 
  14
+      "from sympy.core.trace import Tr", 
  15
+      "from sympy.matrices.matrices import Matrix", 
  16
+      "from IPython.core.display import display_pretty", 
  17
+      "from sympy.printing.latex import *", 
  18
+      "", 
  19
+      "%load_ext sympyprinting"
  20
+     ], 
  21
+     "language": "python", 
  22
+     "outputs": [], 
  23
+     "prompt_number": 2
  24
+    }, 
  25
+    {
  26
+     "cell_type": "markdown", 
  27
+     "source": [
  28
+      "###Basic Examples"
  29
+     ]
  30
+    }, 
  31
+    {
  32
+     "cell_type": "code", 
  33
+     "collapsed": true, 
  34
+     "input": [
  35
+      "a, b, c, d = symbols('a b c d'); ", 
  36
+      "A, B = symbols('A B', commutative=False)", 
  37
+      "t = Tr(A*B)"
  38
+     ], 
  39
+     "language": "python", 
  40
+     "outputs": [], 
  41
+     "prompt_number": 3
  42
+    }, 
  43
+    {
  44
+     "cell_type": "code", 
  45
+     "collapsed": false, 
  46
+     "input": [
  47
+      "t"
  48
+     ], 
  49
+     "language": "python", 
  50
+     "outputs": [
  51
+      {
  52
+       "latex": [
  53
+        "$$\\mbox{Tr}\\left(A B\\right)$$"
  54
+       ], 
  55
+       "output_type": "pyout", 
  56
+       "prompt_number": 4, 
  57
+       "text": [
  58
+        "Tr(A\u22c5B)"
  59
+       ]
  60
+      }
  61
+     ], 
  62
+     "prompt_number": 4
  63
+    }, 
  64
+    {
  65
+     "cell_type": "code", 
  66
+     "collapsed": false, 
  67
+     "input": [
  68
+      "latex(t)"
  69
+     ], 
  70
+     "language": "python", 
  71
+     "outputs": [
  72
+      {
  73
+       "output_type": "pyout", 
  74
+       "prompt_number": 5, 
  75
+       "text": [
  76
+        "\\mbox{Tr}\\left(A B\\right)"
  77
+       ]
  78
+      }
  79
+     ], 
  80
+     "prompt_number": 5
  81
+    }, 
  82
+    {
  83
+     "cell_type": "code", 
  84
+     "collapsed": false, 
  85
+     "input": [
  86
+      "display_pretty(t)"
  87
+     ], 
  88
+     "language": "python", 
  89
+     "outputs": [
  90
+      {
  91
+       "output_type": "display_data", 
  92
+       "text": [
  93
+        "Tr(\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5)))"
  94
+       ]
  95
+      }
  96
+     ], 
  97
+     "prompt_number": 14
  98
+    }, 
  99
+    {
  100
+     "cell_type": "markdown", 
  101
+     "source": [
  102
+      "### Using Matrices"
  103
+     ]
  104
+    }, 
  105
+    {
  106
+     "cell_type": "code", 
  107
+     "collapsed": true, 
  108
+     "input": [
  109
+      "t = Tr ( Matrix([ [2,3], [3,4] ]))"
  110
+     ], 
  111
+     "language": "python", 
  112
+     "outputs": [], 
  113
+     "prompt_number": 15
  114
+    }, 
  115
+    {
  116
+     "cell_type": "code", 
  117
+     "collapsed": false, 
  118
+     "input": [
  119
+      "t"
  120
+     ], 
  121
+     "language": "python", 
  122
+     "outputs": [
  123
+      {
  124
+       "latex": [
  125
+        "$$6$$"
  126
+       ], 
  127
+       "output_type": "pyout", 
  128
+       "png": "iVBORw0KGgoAAAANSUhEUgAAAAwAAAASCAYAAABvqT8MAAAABHNCSVQICAgIfAhkiAAAAO5JREFU\nKJHN0r1KQ0EQhuHnhAgBhaiIFpLOxs5O8CIsFG/A1spCL0CwsUtnaat4C7aWNooiCAEJKBb+oMGg\nSCzOHlyWlWDnV+3M7vvN7O7wRxWZXAs7+MIL3rCHXs5gGh0shXgS19isDtQSYB9tnIa4jgZec+5r\n+MD4sHtUOsTlsEP1aL2AeyxiGbNoYgs3KTiqfJVzbET5FTxhLgVmMEAfY1G+FqoepcBIAC4ybZ8p\n/6KoHOATd6F8ql5oeSIG4ARTGaCBLh7TjVW8V05BBZ5xkDECx9j1M2PruIpN0uFrYhvzofcH5ajc\n/lbhH+gb6f4rZTpaz0QAAAAASUVORK5CYII=\n", 
  129
+       "prompt_number": 16, 
  130
+       "text": [
  131
+        "6"
  132
+       ]
  133
+      }
  134
+     ], 
  135
+     "prompt_number": 16
  136
+    }, 
  137
+    {
  138
+     "cell_type": "markdown", 
  139
+     "source": [
  140
+      "### Example using modules in physics.quantum"
  141
+     ]
  142
+    }, 
  143
+    {
  144
+     "cell_type": "code", 
  145
+     "collapsed": true, 
  146
+     "input": [
  147
+      "from sympy.physics.quantum.density import Density", 
  148
+      "from sympy.physics.quantum.spin import (", 
  149
+      "    Jx, Jy, Jz, Jplus, Jminus, J2,", 
  150
+      "    JxBra, JyBra, JzBra,", 
  151
+      "    JxKet, JyKet, JzKet,", 
  152
+      ")"
  153
+     ], 
  154
+     "language": "python", 
  155
+     "outputs": [], 
  156
+     "prompt_number": 7
  157
+    }, 
  158
+    {
  159
+     "cell_type": "code", 
  160
+     "collapsed": false, 
  161
+     "input": [
  162
+      "d = Density([JzKet(1,1),0.5],[JzKet(1,-1),0.5]); d"
  163
+     ], 
  164
+     "language": "python", 
  165
+     "outputs": [
  166
+      {
  167
+       "latex": [
  168
+        "$$\\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)$$"
  169
+       ], 
  170
+       "output_type": "pyout", 
  171
+       "prompt_number": 8, 
  172
+       "text": [
  173
+        "\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5))"
  174
+       ]
  175
+      }
  176
+     ], 
  177
+     "prompt_number": 8
  178
+    }, 
  179
+    {
  180
+     "cell_type": "code", 
  181
+     "collapsed": true, 
  182
+     "input": [
  183
+      "t = Tr(d)"
  184
+     ], 
  185
+     "language": "python", 
  186
+     "outputs": [], 
  187
+     "prompt_number": 9
  188
+    }, 
  189
+    {
  190
+     "cell_type": "code", 
  191
+     "collapsed": false, 
  192
+     "input": [
  193
+      "t"
  194
+     ], 
  195
+     "language": "python", 
  196
+     "outputs": [
  197
+      {
  198
+       "latex": [
  199
+        "$$\\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)$$"
  200
+       ], 
  201
+       "output_type": "pyout", 
  202
+       "prompt_number": 10, 
  203
+       "text": [
  204
+        "Tr(\u03c1((\u27581,1\u27e9, 0.5),(\u27581,-1\u27e9, 0.5)))"
  205
+       ]
  206
+      }
  207
+     ], 
  208
+     "prompt_number": 10
  209
+    }, 
  210
+    {
  211
+     "cell_type": "code", 
  212
+     "collapsed": false, 
  213
+     "input": [
  214
+      "latex(t)"
  215
+     ], 
  216
+     "language": "python", 
  217
+     "outputs": [
  218
+      {
  219
+       "output_type": "pyout", 
  220
+       "prompt_number": 11, 
  221
+       "text": [
  222
+        "", 
  223
+        "\\mbox{Tr}\\left(\\rho\\left(\\begin{pmatrix}{\\left|1,1\\right\\rangle }, & 0.5\\end{p", 
  224
+        "matrix},\\begin{pmatrix}{\\left|1,-1\\right\\rangle }, & 0.5\\end{pmatrix}\\right)\\r", 
  225
+        "ight)"
  226
+       ]
  227
+      }
  228
+     ], 
  229
+     "prompt_number": 11
  230
+    }, 
  231
+    {
  232
+     "cell_type": "code", 
  233
+     "collapsed": false, 
  234
+     "input": [
  235
+      "t.doit()"
  236
+     ], 
  237
+     "language": "python", 
  238
+     "outputs": [
  239
+      {
  240
+       "latex": [
  241
+        "$$1.0$$"
  242
+       ], 
  243
+       "output_type": "pyout", 
  244
+       "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", 
  245
+       "prompt_number": 12, 
  246
+       "text": [
  247
+        "1.00000000000000"
  248
+       ]
  249
+      }
  250
+     ], 
  251
+     "prompt_number": 12
  252
+    }, 
  253
+    {
  254
+     "cell_type": "code", 
  255
+     "collapsed": true, 
  256
+     "input": [], 
  257
+     "language": "python", 
  258
+     "outputs": [], 
  259
+     "prompt_number": 12
  260
+    }
  261
+   ]
  262
+  }
  263
+ ]
  264
+}
30  sympy/core/tests/test_trace.py
... ...
@@ -1,4 +1,4 @@
1  
-from sympy import S, Symbol, symbols, Matrix
  1
+from sympy import S, Symbol, symbols, Matrix, Tuple
2 2
 from sympy.core.trace import Tr
3 3
 
4 4
 def test_trace_new():
@@ -19,6 +19,8 @@ def test_trace_new():
19 19
     assert Tr(a*C*A*D*B*2) == 2*a*Tr(A*D*B*C)
20 20
     assert Tr(B*A*C*B*A) == Tr(A*C*B*A*B)
21 21
     assert Tr(A*C*B*A*B) == Tr(A*B*A*C*B)
  22
+    assert Tr(A*A*B*B) == Tr(A**2*B**2)
  23
+    assert Tr(A*B*B*A) == Tr(A**2*B**2)
22 24
 
23 25
     # since A is symbol and not commutative
24 26
     assert isinstance(Tr(A), Tr)
@@ -32,14 +34,32 @@ def test_trace_new():
32 34
     assert Tr(M) == 3
33 35
 
34 36
     #trace indices test
35  
-    t = Tr((A+B), (2))
36  
-    assert t.args[0].args[1] == (2) and t.args[1].args[1] == (2)
  37
+    t = Tr((A+B), [2])
  38
+    assert t.args[0].args[1] == Tuple(2) and t.args[1].args[1] == Tuple(2)
37 39
 
38  
-    t = Tr(a*A, (2,3))
39  
-    assert t.args[1].args[1] == (2,3)
  40
+    t = Tr(a*A, [2,3])
  41
+    assert t.args[1].args[1] == Tuple(2,3)
40 42
 
41 43
 def test_trace_doit():
42 44
     a, b, c, d = symbols('a b c d')
43 45
     A, B, C, D = symbols('A B C D', commutative=False)
44 46
 
45 47
     #TODO: needed while testing reduced density operations, etc.
  48
+
  49
+#def test_permute():
  50
+#    A, B, C, D, E, F, G = symbols('A B C D E F G', commutative=False)
  51
+#    t = Tr(A*B*C*D*E*F*G, cycle=False)
  52
+
  53
+#    assert t.permute(0).args[0].args == (A, B, C, D, E, F, G)
  54
+#    assert t.permute(2).args[0].args == (F, G, A, B, C, D, E)
  55
+#    assert t.permute(4).args[0].args == (D, E, F, G, A, B, C)
  56
+#    assert t.permute(6).args[0].args == (B, C, D, E, F, G, A)
  57
+#    assert t.permute(8).args[0].args == t.permute(1).args[0].args
  58
+
  59
+#    assert t.permute(-1).args[0].args == (B, C, D, E, F, G, A)
  60
+#    assert t.permute(-3).args[0].args == (D, E, F, G, A, B, C)
  61
+#    assert t.permute(-5).args[0].args == (F, G, A, B, C, D, E)
  62
+#    assert t.permute(-8).args[0].args == t.permute(-1).args[0].args
  63
+
  64
+#    t = Tr((A+B)*(B*B)*C*D,cycle=False)
  65
+#    assert t.permute(2).args[0].args == (C, D, (A+B), (B**2))
85  sympy/core/trace.py
... ...
@@ -1,4 +1,4 @@
1  
-from sympy import Expr, Add, Mul, Matrix, Pow, sympify, Matrix
  1
+from sympy import Expr, Add, Mul, Matrix, Pow, sympify, Matrix, Tuple
2 2
 
3 3
 def _is_scalar(e):
4 4
     """ Helper method used in Tr"""
@@ -15,7 +15,17 @@ def _is_scalar(e):
15 15
     return False
16 16
 
17 17
 def _cycle_permute(l):
18  
-    """ Cyclic permutations based on canonical ordering"""
  18
+    """ Cyclic permutations based on canonical ordering
  19
+
  20
+    This method does the sort based ascii values while
  21
+    a better approach would be to used lexicographic sort.
  22
+    TODO: Handle condition such as symbols have subscripts/superscripts
  23
+    in case of lexicographic sort
  24
+
  25
+    """
  26
+
  27
+    if len(l) == 1:
  28
+        return l
19 29
 
20 30
     min_item = min(l)
21 31
     indices = [i for i, x in enumerate(l) if x ==  min_item]
@@ -39,6 +49,19 @@ def _cycle_permute(l):
39 49
 
40 50
     return ordered_l
41 51
 
  52
+
  53
+def _rearrange_args(l):
  54
+    """ this just moves the last arg to first position
  55
+     to enable expansion of args
  56
+     A,B,A ==> A**2,B
  57
+    """
  58
+    if len(l) == 1:
  59
+        return l
  60
+
  61
+    x = list(l[-1:])
  62
+    x.extend(l[0:-1])
  63
+    return Mul(*x).args
  64
+
42 65
 class Tr(Expr):
43 66
     """ Generic Trace operation than can trace over:
44 67
 
@@ -63,20 +86,24 @@ class Tr(Expr):
63 86
     >>> from sympy import symbols, Matrix
64 87
     >>> a, b = symbols('a b', commutative=True)
65 88
     >>> A, B = symbols('A B', commutative=False)
66  
-    >>> Tr(a*A,2)
67  
-    a*Tr(A, 2)
  89
+    >>> Tr(a*A,[2])
  90
+    a*Tr(A)
68 91
     >>> m = Matrix([[1,2],[1,1]])
69 92
     >>> Tr(m)
70 93
     2
71 94
 
72 95
     """
73  
-
74 96
     def __new__(cls, *args):
75 97
         """ Construct a Trace object.
76 98
 
  99
+        Parameters
  100
+        ==========
  101
+        args = sympy expression
  102
+
77 103
         """
  104
+
78 105
         expr = args[0]
79  
-        indices = args[1] if len(args) == 2 else -1 #-1 indicates full trace
  106
+        indices = Tuple(*args[1]) if len(args) == 2 else Tuple()
80 107
         if isinstance(expr, Matrix):
81 108
             return expr.trace()
82 109
         elif hasattr(expr, 'trace') and callable(t.x):
@@ -89,10 +116,8 @@ def __new__(cls, *args):
89 116
             if len(nc_part) == 0:
90 117
                 return Mul(*c_part)
91 118
             else:
92  
-                # cyclic permute nc_part for canonical ordering
93  
-                nc_part_ordered = _cycle_permute(nc_part)
94  
-                return Mul(*c_part) * Expr.__new__(cls, Mul(*nc_part_ordered),
95  
-                                                   indices)
  119
+                nc_part_ordered = _cycle_permute(_rearrange_args(nc_part))
  120
+                return Mul(*c_part) * Expr.__new__(cls, Mul(*nc_part_ordered), indices )
96 121
         elif isinstance(expr, Pow):
97 122
             if (_is_scalar(expr.args[0]) and
98 123
                 _is_scalar(expr.args[1])):
@@ -118,7 +143,7 @@ def doit(self,**kwargs):
118 143
 
119 144
         """
120 145
         if hasattr(self.args[0], '_eval_trace'):
121  
-            return self.args[0]._eval_trace()
  146
+            return self.args[0]._eval_trace(indices=self.args[1])
122 147
 
123 148
         return self
124 149
 
@@ -126,5 +151,41 @@ def doit(self,**kwargs):
126 151
     def is_number(self):
127 152
         #TODO : This function to be reviewed
128 153
         # and implementation improved.
129  
-
130 154
         return True
  155
+
  156
+
  157
+    #TODO: Review if the permute method is needed
  158
+    # and if it needs to return a new instance
  159
+    #def permute(self, pos):
  160
+        """ Permute the arguments cyclically.
  161
+
  162
+    #    Parameters
  163
+    #    ==========
  164
+    #    pos : integer, if positive, shift-right, else shift-left
  165
+
  166
+    #    Examples
  167
+        =========
  168
+
  169
+    #   >>> from sympy.core.trace import Tr
  170
+    #    >>> t = Tr(A*B*C*D, 2, cycle=False)
  171
+    #    >>> t.permute(2)
  172
+    #    Tr(C*D*A*B,2)
  173
+
  174
+    #    """
  175
+    #    if pos > 0:
  176
+    #        pos = pos % len(self.args[0].args)
  177
+    #    else:
  178
+    #        pos = - (abs(pos) % len(self.args[0].args))
  179
+    #        #print self.args[0].args[pos:] + self.args[0].args[0:pos]
  180
+
  181
+    #    args = list(self.args[0].args[-pos:] + self.args[0].args[0:-pos])
  182
+    #    print 'args==' , args, Mul(*(args)), self.args[1], type(self.args[1])
  183
+
  184
+    #    x = Tr(Mul(*(args)), 2, cycle=False)
  185
+
  186
+    #    print id(x), x
  187
+    #    return x
  188
+
  189
+
  190
+    def _hashable_content(self):
  191
+        return self.args[0]  + tuple(self.args[1])
21  sympy/physics/quantum/density.py
@@ -8,6 +8,8 @@
8 8
 from sympy.physics.quantum.qubit import Qubit
9 9
 from sympy.physics.quantum.qapply import qapply
10 10
 from matrixutils import numpy_ndarray, scipy_sparse_matrix, to_numpy
  11
+from sympy.physics.quantum.tensorproduct import TensorProduct, tensor_product_simp
  12
+from sympy.core.compatibility import product
11 13
 
12 14
 class Density(HermitianOperator):
13 15
     """Density operator for representing mixed states.
@@ -157,12 +159,26 @@ def doit(self, **hints):
157 159
         0.5*|0><0| + 0.5*|1><1|
158 160
 
159 161
         """
  162
+
160 163
         terms = []
161 164
         for (state, prob) in self.args:
162  
-            terms.append(prob*(state*Dagger(state)))
  165
+            state = state.expand() # needed to break up (a+b)*c
  166
+            if (isinstance(state, Add)):
  167
+                for arg in product(state.args, repeat=2):
  168
+                    terms.append(prob *
  169
+                                 self._generate_outer_prod(arg[0], arg[1]))
  170
+            else:
  171
+                terms.append(prob *
  172
+                             self._generate_outer_prod(state,state))
163 173
 
164 174
         return Add(*terms)
165 175
 
  176
+    def _generate_outer_prod(self,arg1,arg2):
  177
+        if (isinstance(arg1, TensorProduct)):
  178
+            return tensor_product_simp(arg1 * Dagger(arg2))
  179
+        else:
  180
+            return (arg1*Dagger(arg2))
  181
+
166 182
     def _represent(self, **options):
167 183
         return represent(self.doit(), **options)
168 184
 
@@ -173,7 +189,8 @@ def _print_operator_name_pretty(self, printer, *args):
173 189
         return prettyForm(u"\u03C1")
174 190
 
175 191
     def _eval_trace(self, **kwargs):
176  
-        return Tr(self.doit()).doit()
  192
+        indices = kwargs.get('indices',[])
  193
+        return Tr(self.doit(), indices).doit()
177 194
 
178 195
     def entropy(self):
179 196
         """ Compute the entropy of a density matrix.
10  sympy/physics/quantum/tensorproduct.py
@@ -12,6 +12,7 @@
12 12
     scipy_sparse_matrix,
13 13
     matrix_tensor_product
14 14
 )
  15
+from sympy.core.trace import Tr
15 16
 
16 17
 __all__ = [
17 18
     'TensorProduct',
@@ -196,6 +197,15 @@ def expand(self, **hints):
196 197
         tp = TensorProduct(*[sympify(item).expand(**hints) for item in self.args])
197 198
         return Expr.expand(tp, **hints)
198 199
 
  200
+    def _eval_trace(self, **kwargs):
  201
+        indices = kwargs.get('indices', None)
  202
+        exp = tensor_product_simp(self)
  203
+
  204
+        if indices is None or len(indices) == 0:
  205
+            return Mul(*[Tr(arg).doit() for arg in exp.args])
  206
+        else:
  207
+            return Mul(*[Tr(value).doit() if idx in indices else value
  208
+                        for idx, value in enumerate(exp.args)])
199 209
 
200 210
 def tensor_product_simp_Mul(e):
201 211
     """Simplify a Mul with TensorProducts.
49  sympy/physics/quantum/tests/test_density.py
@@ -14,6 +14,7 @@
14 14
 from sympy.functions import sqrt
15 15
 from sympy.utilities.pytest import raises
16 16
 from sympy.physics.quantum.matrixutils import scipy_sparse_matrix
  17
+from sympy.physics.quantum.tensorproduct import TensorProduct
17 18
 
18 19
 
19 20
 def test_eval_args():
@@ -31,7 +32,9 @@ def test_eval_args():
31 32
     raises(ValueError, lambda: Density([Ket(0)], [Ket(1)]))
32 33
 
33 34
 def test_doit():
  35
+
34 36
     x,y = symbols('x y')
  37
+    A, B, C, D, E, F= symbols('A B C D E F', commutative=False)
35 38
     d = Density([XKet(),0.5], [PxKet(),0.5])
36 39
     assert (0.5*(PxKet()*Dagger(PxKet())) +
37 40
             0.5*(XKet()*Dagger(XKet()))) == d.doit()
@@ -41,6 +44,52 @@ def test_doit():
41 44
     assert (0.5*(PxKet(x*y)*Dagger(PxKet(x*y))) +
42 45
             0.5*(XKet(x*y)*Dagger(XKet(x*y)))) == d_with_sym.doit()
43 46
 
  47
+
  48
+    d = Density([(A+B)*C, 1.0])
  49
+    assert d.doit() == (1.0*A*C*Dagger(C)*Dagger(A) +
  50
+                        1.0*A*C*Dagger(C)*Dagger(B) +
  51
+                        1.0*B*C*Dagger(C)*Dagger(A) +
  52
+                        1.0*B*C*Dagger(C)*Dagger(B))
  53
+
  54
+
  55
+    #  With TensorProducts as args
  56
+
  57
+    # Density with simple tensor products as args
  58
+    t = TensorProduct(A, B, C)
  59
+    d = Density([t, 1.0])
  60
+    assert d.doit() == 1.0 * TensorProduct(A*Dagger(A), B*Dagger(B),C*Dagger(C))
  61
+
  62
+    # Density with multiple Tensorproducts as states
  63
+    t2 = TensorProduct(A,B)
  64
+    t3 = TensorProduct(C,D)
  65
+