forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paulialgebra.py
163 lines (127 loc) · 3.73 KB
/
paulialgebra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
This module implements Pauli algebra by subclassing Symbol. Only algebraic
properties of Pauli matrices are used (we don't use the Matrix class).
See the documentation to the class Pauli for examples.
References
~~~~~~~~~~
.. [1] http://en.wikipedia.org/wiki/Pauli_matrices
"""
from sympy import Symbol, I
__all__ = ['evaluate_pauli_product']
def delta(i, j):
"""
Returns 1 if i == j, else 0.
This is used in the multiplication of Pauli matrices.
Examples
========
>>> from sympy.physics.paulialgebra import delta
>>> delta(1, 1)
1
>>> delta(2, 3)
0
"""
if i == j:
return 1
else:
return 0
def epsilon(i, j, k):
"""
Return 1 if i,j,k is equal to (1,2,3), (2,3,1), or (3,1,2);
-1 if i,j,k is equal to (1,3,2), (3,2,1), or (2,1,3);
else return 0.
This is used in the multiplication of Pauli matrices.
Examples
========
>>> from sympy.physics.paulialgebra import epsilon
>>> epsilon(1, 2, 3)
1
>>> epsilon(1, 3, 2)
-1
"""
if (i, j, k) in [(1, 2, 3), (2, 3, 1), (3, 1, 2)]:
return 1
elif (i, j, k) in [(1, 3, 2), (3, 2, 1), (2, 1, 3)]:
return -1
else:
return 0
class Pauli(Symbol):
"""The class representing algebraic properties of Pauli matrices
If the left multiplication of symbol or number with Pauli matrix is needed,
please use parentheses to separate Pauli and symbolic multiplication
(for example: 2*I*(Pauli(3)*Pauli(2)))
Another variant is to use evaluate_pauli_product function to evaluate
the product of Pauli matrices and other symbols (with commutative
multiply rules)
See Also
=======
evaluate_pauli_product
Examples
========
>>> from sympy.physics.paulialgebra import Pauli
>>> Pauli(1)
sigma1
>>> Pauli(1)*Pauli(2)
I*sigma3
>>> Pauli(1)*Pauli(1)
1
>>> Pauli(3)**4
1
>>> Pauli(1)*Pauli(2)*Pauli(3)
I
>>> from sympy import I
>>> I*(Pauli(2)*Pauli(3))
-sigma1
>>> from sympy.physics.paulialgebra import evaluate_pauli_product
>>> f = I*Pauli(2)*Pauli(3)
>>> f
I*sigma2*sigma3
>>> evaluate_pauli_product(f)
-sigma1
"""
__slots__ = ["i"]
def __new__(cls, i):
if not i in [1, 2, 3]:
raise IndexError("Invalid Pauli index")
obj = Symbol.__new__(cls, "sigma%d" % i, commutative=False)
obj.i = i
return obj
def __getnewargs__(self):
return (self.i,)
# FIXME don't work for -I*Pauli(2)*Pauli(3)
def __mul__(self, other):
if isinstance(other, Pauli):
j = self.i
k = other.i
return delta(j, k) \
+ I*epsilon(j, k, 1)*Pauli(1) \
+ I*epsilon(j, k, 2)*Pauli(2) \
+ I*epsilon(j, k, 3)*Pauli(3)
return super(Pauli, self).__mul__(other)
def _eval_power(b, e):
if e.is_Integer and e.is_positive:
return super(Pauli, b).__pow__(int(e) % 2)
def evaluate_pauli_product(arg):
'''Help function to evaluate Pauli matrices product
with symbolic objects
Parameters
==========
arg: symbolic expression that contains Paulimatrices
Examples
========
>>> from sympy.physics.paulialgebra import Pauli, evaluate_pauli_product
>>> from sympy import I
>>> evaluate_pauli_product(I*Pauli(1)*Pauli(2))
-sigma3
>>> from sympy.abc import x,y
>>> evaluate_pauli_product(x**2*Pauli(2)*Pauli(1))
-I*x**2*sigma3
'''
tmp = arg.as_coeff_mul()
sigma_product = 1
com_product = 1
for el in tmp[1]:
if isinstance(el, Pauli):
sigma_product *= el
else:
com_product *= el
return (tmp[0]*sigma_product*com_product)