```
from sympy import symbols
from sympy.physics.quantum.trace import Tr
from sympy.matrices.matrices import Matrix
from IPython.core.display import display_pretty
from sympy.printing.latex import *

%load_ext sympyprinting
```

###Basic Examples


```
a, b, c, d = symbols('a b c d'); 
A, B = symbols('A B', commutative=False)
t = Tr(A*B)
```


```
t
```




$$\mbox{Tr}\left(A B\right)$$




```
latex(t)
```




    \mbox{Tr}\left(A B\right)




```
display_pretty(t)
```


    Tr(ρ((❘1,1⟩, 0.5),(❘1,-1⟩, 0.5)))


### Using Matrices


```
t = Tr ( Matrix([ [2,3], [3,4] ]))
```


```
t
```

### Example using modules in physics.quantum


```
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,
)
```


```
d = Density([JzKet(1,1),0.5],[JzKet(1,-1),0.5]); d
```




$$\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)$$




```
t = Tr(d)
```


```
t
```




$$\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)$$




```
latex(t)
```




    
    \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)




```
t.doit()
```


```

```

80a62ccac0c5350784e79c0c106a6bff1067d769
ipynb
Jupyter Notebook
examples/notebooks/trace.ipynb
Michal-Gagala/sympy
3cc756c2af73b5506102abaeefd1b654e286e2c8
MIT
examples/notebooks/trace.ipynb
Michal-Gagala/sympy
3cc756c2af73b5506102abaeefd1b654e286e2c8
MIT
examples/notebooks/trace.ipynb
Michal-Gagala/sympy
3cc756c2af73b5506102abaeefd1b654e286e2c8
MIT
Qwen/Qwen-72B
1. YES
2. YES