-
Notifications
You must be signed in to change notification settings - Fork 0
/
pstring.py
74 lines (48 loc) · 1.51 KB
/
pstring.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
def commute(b1, b2):
"""check if two pauli matrices b1 and b2 commute
The pauli matrices I, X,Y,Z are represented as integers from 0 to 3 respectively
"""
if b1 == "0" or b2 == "0":
return True
if b1 == b2:
return True
return False
class pstring:
"""The Pauli string Class """
def __init__(self, string, coef):
self.string = string
self.coef = coef
def __le__(self, other):
return self.string <= other.string
def __lt__(self, other):
return self.string <= other.string
def pauli_matrix_form(self):
""" return the Pauli string in Pauli matrix form """
result = ""
for p in self.string:
if p == "0":
result += "I"
elif p == "1":
result += "X"
elif p == "2":
result += "Y"
else:
result += "Z"
return result
def commute(self, other):
"""Return True if the Pauli string self commutes with other
Otherwise return False """
str1 = self.string
str2 = other.string
assert len(str1) == len(str2)
count = 0
for j in range(len(str1)):
if str1[j] == str2[j] or str1[j] == '0' or str2[j] == '0':
pass
else:
count += 1
if count %2 == 0:
return True
return False
def __str__(self):
return "{0} * {1}".format(self.coef, self.pauli_matrix_form())