from sympy import *
init_printing()
from sympy.physics.quantum import *
from sympy.physics.quantum.spin import *
from sympy.physics.quantum.pauli import *
from sympy.physics.quantum.boson import *
from sympy.physics.quantum.density import *
from sympy.physics.quantum.operatorordering import *
s_ops = sx, sy, sz, sp, sm = SigmaX(), SigmaY(), SigmaZ(), SigmaPlus(), SigmaMinus()
#s_ops = sx, sy, sz, sp, sm = Jx, Jy, Jz, Jplus, Jminus # does not work well, many expressions unevaluated
sx1, sy1, sz1 = SigmaX(1), SigmaY(1), SigmaZ(1)
sx2, sy2, sz2 = SigmaX(2), SigmaY(2), SigmaZ(2)
qsimplify_pauli(sx * sy)
qsimplify_pauli(sx1 * sy1 * sx2 * sy2)
from IPython.display import HTML
def print_table(data):
t_table = "<table>\n%s\n</table>"
t_row = "<tr>%s</tr>"
t_col = "<td>%s</td>"
table_code = t_table % "".join([t_row % "".join([t_col % ("$%s$" % latex(col))
for col in row])
for row in data])
return HTML(table_code)
data = [s_ops, [represent(s) for s in s_ops]]
print_table(data)
| ${\sigma_x}$ | ${\sigma_y}$ | ${\sigma_z}$ | ${\sigma_+}$ | ${\sigma_-}$ |
| $\left[\begin{matrix}0 & 1\\1 & 0\end{matrix}\right]$ | $\left[\begin{matrix}0 & - i\\i & 0\end{matrix}\right]$ | $\left[\begin{matrix}1 & 0\\0 & -1\end{matrix}\right]$ | $\left[\begin{matrix}0 & 1\\0 & 0\end{matrix}\right]$ | $\left[\begin{matrix}0 & 0\\1 & 0\end{matrix}\right]$ |
data = [[r"{\rm Expression}", r"{\rm Evaluated\;value}", r"{\rm Numerical\;test}"]] + \
[[Commutator(s1, s2),
Commutator(s1, s2).doit(),
represent(Commutator(s1, s2)) == represent(Commutator(s1, s2).doit())]
for s1 in s_ops for s2 in s_ops if s1 != s2]
print_table(data)
| ${\rm Expression}$ | ${\rm Evaluated\;value}$ | ${\rm Numerical\;test}$ |
| $\left[{\sigma_x},{\sigma_y}\right]$ | $2 i {\sigma_z}$ | $\mathrm{True}$ |
| $\left[{\sigma_x},{\sigma_z}\right]$ | $- 2 i {\sigma_y}$ | $\mathrm{True}$ |
| $- \left[{\sigma_+},{\sigma_x}\right]$ | $- {\sigma_z}$ | $\mathrm{True}$ |
| $- \left[{\sigma_-},{\sigma_x}\right]$ | ${\sigma_z}$ | $\mathrm{True}$ |
| $- \left[{\sigma_x},{\sigma_y}\right]$ | $- 2 i {\sigma_z}$ | $\mathrm{True}$ |
| $\left[{\sigma_y},{\sigma_z}\right]$ | $2 i {\sigma_x}$ | $\mathrm{True}$ |
| $- \left[{\sigma_+},{\sigma_y}\right]$ | $- i {\sigma_z}$ | $\mathrm{True}$ |
| $- \left[{\sigma_-},{\sigma_y}\right]$ | $- i {\sigma_z}$ | $\mathrm{True}$ |
| $- \left[{\sigma_x},{\sigma_z}\right]$ | $2 i {\sigma_y}$ | $\mathrm{True}$ |
| $- \left[{\sigma_y},{\sigma_z}\right]$ | $- 2 i {\sigma_x}$ | $\mathrm{True}$ |
| $- \left[{\sigma_+},{\sigma_z}\right]$ | $2 {\sigma_+}$ | $\mathrm{True}$ |
| $- \left[{\sigma_-},{\sigma_z}\right]$ | $- 2 {\sigma_-}$ | $\mathrm{True}$ |
| $\left[{\sigma_+},{\sigma_x}\right]$ | ${\sigma_z}$ | $\mathrm{True}$ |
| $\left[{\sigma_+},{\sigma_y}\right]$ | $i {\sigma_z}$ | $\mathrm{True}$ |
| $\left[{\sigma_+},{\sigma_z}\right]$ | $- 2 {\sigma_+}$ | $\mathrm{True}$ |
| $- \left[{\sigma_-},{\sigma_+}\right]$ | ${\sigma_z}$ | $\mathrm{True}$ |
| $\left[{\sigma_-},{\sigma_x}\right]$ | $- {\sigma_z}$ | $\mathrm{True}$ |
| $\left[{\sigma_-},{\sigma_y}\right]$ | $i {\sigma_z}$ | $\mathrm{True}$ |
| $\left[{\sigma_-},{\sigma_z}\right]$ | $2 {\sigma_-}$ | $\mathrm{True}$ |
| $\left[{\sigma_-},{\sigma_+}\right]$ | $- {\sigma_z}$ | $\mathrm{True}$ |
def elim_zero_matrix(e):
return 0 if e == Matrix([[0, 0], [0, 0]]) else e
I_op = IdentityOperator(2)
data = [[r"{\rm Expression}", r"{\rm Evaluated\;value}", r"{\rm Numerical\;test}"]] + \
[[AntiCommutator(s1, s2),
AntiCommutator(s1, s2).doit(),
elim_zero_matrix(represent(AntiCommutator(s1, s2))) == represent(I_op * AntiCommutator(s1, s2).doit())]
for s1 in s_ops for s2 in s_ops if s1 != s2]
print_table(data)
| ${\rm Expression}$ | ${\rm Evaluated\;value}$ | ${\rm Numerical\;test}$ |
| $\left\{{\sigma_x},{\sigma_y}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_x},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_+},{\sigma_x}\right\}$ | $1$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_x}\right\}$ | $1$ | $\mathrm{True}$ |
| $\left\{{\sigma_x},{\sigma_y}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_y},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_+},{\sigma_y}\right\}$ | $i$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_y}\right\}$ | $- i$ | $\mathrm{True}$ |
| $\left\{{\sigma_x},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_y},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_+},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_+},{\sigma_x}\right\}$ | $1$ | $\mathrm{True}$ |
| $\left\{{\sigma_+},{\sigma_y}\right\}$ | $i$ | $\mathrm{True}$ |
| $\left\{{\sigma_+},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_+}\right\}$ | $1$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_x}\right\}$ | $1$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_y}\right\}$ | $- i$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_z}\right\}$ | $0$ | $\mathrm{True}$ |
| $\left\{{\sigma_-},{\sigma_+}\right\}$ | $1$ | $\mathrm{True}$ |
Dagger(sx), Dagger(sy), Dagger(sz)
Dagger(sm), Dagger(sp)
sm, sp
s = [sx, sy, sz, sm, sp]
data = [[s2] + [(qsimplify_pauli(s1 * s2), represent((qsimplify_pauli(s1 * s2) * I_op).expand()) == represent(s1) * represent(s2))
for s1 in s] for s2 in s]
print_table(data)
| ${\sigma_x}$ | $\begin{pmatrix}1, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}- i {\sigma_z}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}i {\sigma_y}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}\frac{1}{2} \left(1 - {\sigma_z}\right), & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}\frac{1}{2} \left(1 + {\sigma_z}\right), & \mathrm{True}\end{pmatrix}$ |
| ${\sigma_y}$ | $\begin{pmatrix}i {\sigma_z}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}1, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}- i {\sigma_x}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}- \frac{i}{2} \left(1 - {\sigma_z}\right), & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}\frac{i}{2} \left(1 + {\sigma_z}\right), & \mathrm{True}\end{pmatrix}$ |
| ${\sigma_z}$ | $\begin{pmatrix}- i {\sigma_y}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}i {\sigma_x}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}1, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}{\sigma_-}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}- {\sigma_+}, & \mathrm{True}\end{pmatrix}$ |
| ${\sigma_-}$ | $\begin{pmatrix}\frac{1}{2} + \frac{{\sigma_z}}{2}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}- \frac{i}{2} \left(1 + {\sigma_z}\right), & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}- {\sigma_-}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}0, & \mathrm{False}\end{pmatrix}$ | $\begin{pmatrix}\frac{1}{2} \left(1 + {\sigma_z}\right), & \mathrm{True}\end{pmatrix}$ |
| ${\sigma_+}$ | $\begin{pmatrix}\frac{1}{2} - \frac{{\sigma_z}}{2}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}\frac{i}{2} \left(1 - {\sigma_z}\right), & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}{\sigma_+}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}\frac{1}{2} - \frac{{\sigma_z}}{2}, & \mathrm{True}\end{pmatrix}$ | $\begin{pmatrix}0, & \mathrm{False}\end{pmatrix}$ |
represent(sm) * represent(sm)
[sx ** n for n in range(10)]
[sy ** n for n in range(10)]
[sz ** n for n in range(10)]
[sm ** n for n in range(10)]
[sp ** n for n in range(10)]
up = SigmaZKet(0)
down = SigmaZKet(1)
ket = (up + down)/sqrt(2)
ket, qapply(sx * ket), qapply(sy * ket), qapply(sz * ket)
ket, qapply(sm * ket), qapply(sp * ket)
represent(qapply(sx * ket)) == represent(sx) * represent(ket)
True
represent(qapply(sy * ket)) == represent(sy) * represent(ket)
True
represent(qapply(sz * ket)) == represent(sz) * represent(ket)
True
represent(qapply(sm * ket)) == represent(sm) * represent(ket)
True
represent(qapply(sp * ket)) == represent(sp) * represent(ket)
True
from sympy_quantum_extra import *
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-41-b89e5cd86f97> in <module>() ----> 1 from sympy_quantum_extra import * /Users/rob/Dropbox/projects/sympy-quantum-notebooks/sympy_quantum_extra.py in <module>() 8 from sympy.physics.quantum.fermion import * 9 from sympy.physics.quantum.operatorordering import * ---> 10 from sympy.physics.quantum.expectation import Expectation 11 12 from sympy.physics.quantum.pauli import (SigmaX, SigmaY, SigmaZ, SigmaMinus, ImportError: No module named 'sympy.physics.quantum.expectation'
rc = recursive_commutator(sy, sz, 5).doit().expand()
rc
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-42-3ba084e7083e> in <module>() ----> 1 rc = recursive_commutator(sy, sz, 5).doit().expand() 2 3 rc NameError: name 'recursive_commutator' is not defined
A, B = Operator("A"), Operator("B")
Commutator(A, B).doit().doit()
# Add.doit() - > Mul.doit() -> ...
Commutator(sx, sy).doit()
# sx * sy - sy * sx
A = Matrix([[1, 2], [3, 4]])
B = Matrix([[1, 2], [3, 4]])
A * B, MatMul(A, B)
1 * 3
IdentityOperator(2).is_commutative
False
qsimplify(qsimplify(rc))
represent(sx * sx)
represent(sx) ** 2 == Integer(1) * represent(IdentityOperator(2))
True
theta = symbols("theta")
U = exp((I * theta / 2)* sy)
unitary_transformation(U, sx)
unitary_transformation(U, sz)
unitary_transformation(U, sm)
unitary_transformation(U, sp)
%reload_ext version_information
%version_information sympy
| Software | Version |
|---|---|
| Python | 3.4.1 (default, Sep 20 2014, 19:44:17) [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] |
| IPython | 2.3.0 |
| OS | Darwin 13.4.0 x86_64 i386 64bit |
| sympy | 0.7.5-git |
| Thu Oct 09 13:14:39 2014 JST | |