Test sympy.physics.quantum.pauli

In [1]:
from sympy import *
init_printing()
In [2]:
from sympy.physics.quantum import *
from sympy.physics.quantum.spin import *
In [9]:
from sympy.physics.quantum.pauli import *
from sympy.physics.quantum.boson import *
from sympy.physics.quantum.density import *
from sympy.physics.quantum.operatorordering import *
In [10]:
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
In [11]:
sx1, sy1, sz1 = SigmaX(1), SigmaY(1), SigmaZ(1)
sx2, sy2, sz2 = SigmaX(2), SigmaY(2), SigmaZ(2)
In [12]:
qsimplify_pauli(sx * sy)
Out[12]:
$$i {\sigma_z}$$
In [13]:
qsimplify_pauli(sx1 * sy1 * sx2 * sy2)
Out[13]:
$$- {\sigma_z^{(1)}} {\sigma_z^{(2)}}$$
In [15]:
 

HTML table

In [12]:
from IPython.display import HTML
In [13]:
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)

Matrix representations

In [14]:
data = [s_ops, [represent(s) for s in s_ops]]

print_table(data)
Out[14]:
${\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]$

Commutators

In [15]:
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)
Out[15]:
${\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}$

Anticommutators

In [16]:
def elim_zero_matrix(e):
    return 0 if e == Matrix([[0, 0], [0, 0]]) else e
In [17]:
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)
Out[17]:
${\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}$

Adjoint

In [18]:
Dagger(sx), Dagger(sy), Dagger(sz)
Out[18]:
$$\begin{pmatrix}{\sigma_x}, & {\sigma_y}, & {\sigma_z}\end{pmatrix}$$
In [19]:
Dagger(sm), Dagger(sp)
Out[19]:
$$\begin{pmatrix}{\sigma_+}, & {\sigma_-}\end{pmatrix}$$
In [20]:
sm, sp
Out[20]:
$$\begin{pmatrix}{\sigma_-}, & {\sigma_+}\end{pmatrix}$$

Operator products

In [26]:
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)
Out[26]:
${\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}$
In [27]:
represent(sm) * represent(sm)
Out[27]:
$$\left[\begin{matrix}0 & 0\\0 & 0\end{matrix}\right]$$

Powers

In [28]:
[sx ** n for n in range(10)]
Out[28]:
$$\begin{bmatrix}1, & {\sigma_x}, & 1, & {\sigma_x}, & 1, & {\sigma_x}, & 1, & {\sigma_x}, & 1, & {\sigma_x}\end{bmatrix}$$
In [29]:
[sy ** n for n in range(10)]
Out[29]:
$$\begin{bmatrix}1, & {\sigma_y}, & 1, & {\sigma_y}, & 1, & {\sigma_y}, & 1, & {\sigma_y}, & 1, & {\sigma_y}\end{bmatrix}$$
In [30]:
[sz ** n for n in range(10)]
Out[30]:
$$\begin{bmatrix}1, & {\sigma_z}, & 1, & {\sigma_z}, & 1, & {\sigma_z}, & 1, & {\sigma_z}, & 1, & {\sigma_z}\end{bmatrix}$$
In [31]:
[sm ** n for n in range(10)]
Out[31]:
$$\begin{bmatrix}1, & {\sigma_-}, & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0\end{bmatrix}$$
In [32]:
[sp ** n for n in range(10)]
Out[32]:
$$\begin{bmatrix}1, & {\sigma_+}, & 0, & 0, & 0, & 0, & 0, & 0, & 0, & 0\end{bmatrix}$$

States

In [33]:
up = SigmaZKet(0)
down = SigmaZKet(1)

ket = (up + down)/sqrt(2)
In [34]:
ket, qapply(sx * ket), qapply(sy * ket), qapply(sz * ket)
Out[34]:
$$\begin{pmatrix}\frac{\sqrt{2}}{2} \left({\left|0\right\rangle } + {\left|1\right\rangle }\right), & \frac{\sqrt{2}}{2} {\left|0\right\rangle } + \frac{\sqrt{2}}{2} {\left|1\right\rangle }, & - \frac{\sqrt{2} i}{2} {\left|0\right\rangle } + \frac{\sqrt{2} i}{2} {\left|1\right\rangle }, & \frac{\sqrt{2}}{2} {\left|0\right\rangle } - \frac{\sqrt{2}}{2} {\left|1\right\rangle }\end{pmatrix}$$
In [35]:
ket, qapply(sm * ket), qapply(sp * ket)
Out[35]:
$$\begin{pmatrix}\frac{\sqrt{2}}{2} \left({\left|0\right\rangle } + {\left|1\right\rangle }\right), & \frac{\sqrt{2}}{2} {\left|1\right\rangle }, & \frac{\sqrt{2}}{2} {\left|0\right\rangle }\end{pmatrix}$$
In [36]:
represent(qapply(sx * ket)) == represent(sx) * represent(ket)
Out[36]:
True
In [37]:
represent(qapply(sy * ket)) == represent(sy) * represent(ket)
Out[37]:
True
In [38]:
represent(qapply(sz * ket)) == represent(sz) * represent(ket)
Out[38]:
True
In [39]:
represent(qapply(sm * ket)) == represent(sm) * represent(ket)
Out[39]:
True
In [40]:
represent(qapply(sp * ket)) == represent(sp) * represent(ket)
Out[40]:
True

Applications

Baker–Campbell–Hausdorff formula

In [41]:
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'
In [42]:
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
In [31]:
A, B = Operator("A"), Operator("B")

Commutator(A, B).doit().doit()
# Add.doit() - > Mul.doit() -> ...
Out[31]:
$$A B - B A$$
In [32]:
Commutator(sx, sy).doit()
# sx * sy - sy * sx
Out[32]:
$$2 i {\sigma_z}$$
In [33]:
A = Matrix([[1, 2], [3, 4]])
B = Matrix([[1, 2], [3, 4]])
A * B, MatMul(A, B)
Out[33]:
$$\left ( \left[\begin{matrix}7 & 10\\15 & 22\end{matrix}\right], \quad \left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right] \left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]\right )$$
In [34]:
1 * 3
Out[34]:
$$3$$
In [35]:
IdentityOperator(2).is_commutative
Out[35]:
False
In [36]:
qsimplify(qsimplify(rc))
Out[36]:
$$32 i {\sigma_x}$$
In [37]:
represent(sx * sx)
Out[37]:
$$1$$
In [38]:
represent(sx) ** 2 == Integer(1) * represent(IdentityOperator(2))
Out[38]:
True
In [38]:
 

Test: $e^{i\theta/2 \sigma_y}\sigma_xe^{-i\theta/2 \sigma_y} = \cos(\theta)\sigma_x + \sin(\theta)\sigma_z$

In [39]:
theta = symbols("theta")
In [40]:
U = exp((I * theta / 2)* sy)
In [41]:
unitary_transformation(U, sx)
Out[41]:
$$\sin{\left (\theta \right )} {\sigma_z} + \cos{\left (\theta \right )} {\sigma_x}$$

Test: $e^{i\theta/2 \sigma_y}\sigma_ze^{-i\theta/2 \sigma_y} = \cos(\theta)\sigma_z - \sin(\theta)\sigma_x$

In [42]:
unitary_transformation(U, sz)
Out[42]:
$$- \sin{\left (\theta \right )} {\sigma_x} + \cos{\left (\theta \right )} {\sigma_z}$$

Calculate $e^{i\theta/2 \sigma_y}\sigma_-e^{-i\theta/2 \sigma_y}$

In [43]:
unitary_transformation(U, sm)
Out[43]:
$$\left(\frac{1}{2} \cos{\left (\theta \right )} - \frac{1}{2}\right) {\sigma_x} + \frac{{\sigma_z}}{2} \sin{\left (\theta \right )} + {\sigma_-}$$

Calculate $e^{i\theta/2 \sigma_y}\sigma_+e^{-i\theta/2 \sigma_y}$

In [44]:
unitary_transformation(U, sp)
Out[44]:
$$\left(\frac{1}{2} \cos{\left (\theta \right )} - \frac{1}{2}\right) {\sigma_x} + \frac{{\sigma_z}}{2} \sin{\left (\theta \right )} + {\sigma_+}$$

Versions

In [7]:
%reload_ext version_information

%version_information sympy
Out[7]:
SoftwareVersion
Python3.4.1 (default, Sep 20 2014, 19:44:17) [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
IPython2.3.0
OSDarwin 13.4.0 x86_64 i386 64bit
sympy0.7.5-git
Thu Oct 09 13:14:39 2014 JST
In [ ]: