-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableau_operations.py
146 lines (84 loc) · 2.44 KB
/
tableau_operations.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
import numpy
from qiskit import *
from tableau import *
from pstring import *
import galois
GF = galois.GF(2)
def hgate(X,Z,S, U, a):
xtmp = numpy.copy(X[:,a])
ztmp = numpy.copy(Z[:,a])
S = (S + X[:,a] * Z[:,a])
X[:,a] = ztmp
Z[:,a] = xtmp
U.h(a)
return X,Z,S, U
def sgate(X,Z,S, U, a):
S = (S + X[:,a] * Z[:,a])
tmp = (Z[:,a] + X[:,a])
Z[:,a] = tmp
U.s(a)
return X, Z, S, U
def cxgate(X,Z,S, U, a, b):
ones = GF(numpy.ones(len(X), dtype=int))
stmp = S + X[:,a] * Z[:,b] * (X[:,b] + Z[:,a] + ones)
S = stmp
ztmp = (Z[:,a] + Z[:,b])
Z[:,a] = ztmp
xtmp = (X[:,b] + X[:,a])
X[:,b] = xtmp
U.cx(a,b)
return X, Z, S, U
def czgate(X,Z,S,U,a,b):
X,Z,S,U = hgate(X,Z,S,U,b)
X,Z,S,U = cxgate(X,Z,S,U,a,b)
X,Z,S,U = hgate(X,Z,S,U,b)
return X, Z, S, U
def swaprows(X,Z,S,a,b):
X[[a,b]] = X[[b,a]]
Z[[a,b]] = Z[[b,a]]
S[[a,b]] = S[[b,a]]
return X,Z,S
def swapcolumns(X,Z,S,U,a,b):
X[:,[a,b]] = X[:,[b,a]]
Z[:,[a,b]] = Z[:,[b,a]]
return X,Z,S,U
# In[144]:
#### other helper functions
def makeTableauMatrix(X,Z):
"""The tableau matrix is defined as [X,Z] """
return numpy.concatenate((X,Z), axis=1)
def rank(M):
""" return the rank of the rank of the Matrix M """
return numpy.linalg.matrix_rank(M)
def tableauMatrixToPauliStrings(T,S,Coefs):
T = numpy.array(T)
S = numpy.array(S)
Coefs = numpy.array(Coefs)
numrows = T.shape[0]
numcols = T.shape[1]//2
pstrs = []
for r in range(numrows):
row = T[r]
coef = Coefs[r] * (-1)**S[r]
strform = ""
for bit in range(numcols):
if row[bit] == 0:
if row[bit + numcols] == 0:
strform += "0"
if row[bit + numcols] == 1:
strform += "3"
if row[bit] == 1:
if row[bit + numcols] == 0:
strform += "1"
if row[bit + numcols] == 1:
strform += "2"
pstrs.append(pstring(strform, coef))
return pstrs
def getIndependentPauliStrings(X,Z):
""" return the independent pauli strings
Their coefficients are 1 """
rowspace = makeTableauMatrix(X,Z).row_space()
numrows = rowspace.shape[0]
S = numpy.ones(numrows, dtype=int)
Coefs = numpy.ones(numrows, dtype=int)
return tableauMatrixToPauliStrings(rowspace,S,Coefs)