# Density operator and matrix 


```
from sympy import *
from sympy.physics.quantum import *
from sympy.physics.quantum.density import *
from sympy.physics.quantum.spin import (
    Jx, Jy, Jz, Jplus, Jminus, J2,
    JxBra, JyBra, JzBra,
    JxKet, JyKet, JzKet,
)
from IPython.core.display import display_pretty
```


```
%load_ext sympyprinting
```

Create n density matrix using symbolic states


```
psi = Ket('psi')
phi = Ket('phi')
```


```
d = Density((psi,0.5),(phi,0.5)); d
```




$$\rho(\begin{pmatrix}{\left|\psi\right\rangle }, & 0.5\end{pmatrix},\begin{pmatrix}{\left|\phi\right\rangle }, & 0.5\end{pmatrix})$$




```
display_pretty(d)
```


    ρ((❘ψ⟩, 0.5),(❘φ⟩, 0.5))



```
d.states()
```




$$\begin{pmatrix}{\left|\psi\right\rangle }, & {\left|\phi\right\rangle }\end{pmatrix}$$




```
d.probs()
```




$$\begin{pmatrix}0.5, & 0.5\end{pmatrix}$$




```
d.doit()
```


```
Dagger(d)
```




$$\rho(\begin{pmatrix}{\left|\psi\right\rangle }, & 0.5\end{pmatrix},\begin{pmatrix}{\left|\phi\right\rangle }, & 0.5\end{pmatrix})$$




```
A = Operator('A')
```


```
d.apply_op(A)
```




$$\rho(\begin{pmatrix}A {\left|\psi\right\rangle }, & 0.5\end{pmatrix},\begin{pmatrix}A {\left|\phi\right\rangle }, & 0.5\end{pmatrix})$$



Now create a density matrix using spin states


```
up = JzKet(S(1)/2,S(1)/2)
down = JzKet(S(1)/2,-S(1)/2)
```


```
d2 = Density((up,0.5),(down,0.5)); d2
```




$$\rho(\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})$$




```
represent(d2)
```




$$\left[\begin{smallmatrix}0.5 & 0\\0 & 0.5\end{smallmatrix}\right]$$




```
d2.apply_op(Jz)
```




$$\rho(\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})$$




```
qapply(_)
```




$$\rho(\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})$$




```
qapply((Jy*d2).doit())
```

b04074c97335028df8b58b79f031fe90d0b6bc28
ipynb
Jupyter Notebook
examples/notebooks/density.ipynb
tesseralis/sympy
0c2f7e06b1a43d25ba93bac65e93f8d5f323be7a
BSD-3-Clause
2020-11-17T07:35:20.000Z
2020-11-17T07:35:20.000Z
examples/notebooks/density.ipynb
tesseralis/sympy
0c2f7e06b1a43d25ba93bac65e93f8d5f323be7a
BSD-3-Clause
examples/notebooks/density.ipynb
tesseralis/sympy
0c2f7e06b1a43d25ba93bac65e93f8d5f323be7a
BSD-3-Clause
Qwen/Qwen-72B
1. YES
2. YES