-
Notifications
You must be signed in to change notification settings - Fork 2
/
Clifford.jl
308 lines (264 loc) · 10.1 KB
/
Clifford.jl
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
module Clifford
export PauliOperator, CliffordOperator, @p_str, ⊗, commutes, clifford_group, expand_2toN, mul!
t,f = true,false
ops_F2 = [[f,f],[f,t],[t,f],[t,t]]
ops_chars = ['I', 'Z', 'X', 'Y']
d_F2 = Dict(zip(ops_chars, ops_F2))
drev_F2 = Dict(zip(ops_F2, ops_chars))
d_ind = Dict(zip(ops_chars, 1:4))
################################################################################
# PauliOperator and constructors
################################################################################
mutable struct PauliOperator
sign::Int
singlequbitops::Array{Int,1}
end
function PauliOperator(operators::String)
if startswith(operators, "+")
sign = 0
elseif startswith(operators, "i")
sign = 1
elseif startswith(operators, "-i")
sign = 3
elseif startswith(operators, "-")
sign = 2
else
sign = 0
end
singlequbitops = [d_ind[op] for op in strip(operators,['+','-', 'i'])]
PauliOperator(sign, singlequbitops)
end
macro p_str(p)
PauliOperator(p)
end
################################################################################
# methods for PauliOperator
################################################################################
multiplication_table = Array{Tuple{Int,Int},2}(
# I Z X Y <-- second argument
[[(0,1) (0,2) (0,3) (0,4)]; # I <-- first argument
[(0,2) (0,1) (1,4) (3,3)]; # Z
[(0,3) (3,4) (0,1) (1,2)]; # X
[(0,4) (1,3) (3,2) (0,1)]] # Y
)
commutation_table = Array{Int,2}(
# I Z X Y <-- second argument
[[ 0 0 0 0 ]; # I <-- first argument
[ 0 0 1 1 ]; # Z
[ 0 1 0 1 ]; # X
[ 0 1 1 0 ]] # Y
)
function Base.:*(l::PauliOperator, r::PauliOperator)
len = length(l.singlequbitops)
s_ops = Array{Tuple{Int,Int}, 1}(len)
for i in 1:len
s_ops[i] = multiplication_table[l.singlequbitops[i],r.singlequbitops[i]]
end
PauliOperator(
(l.sign+r.sign+sum(s_op[1] for s_op in s_ops))%4,
[s_op[2] for s_op in s_ops])
end
⊗(l::PauliOperator, r::PauliOperator) = PauliOperator(
(l.sign + r.sign)%4,
[l.singlequbitops; r.singlequbitops])
function commutes(l::Array{Int,1}, r::Array{Int,1})::Bool
com = sum(commutation_table[li,ri] for (li,ri) in zip(l, r))
return com%2 == 0
end
commutes(l::PauliOperator, r::PauliOperator)::Bool = commutes(l.singlequbitops, r.singlequbitops)
Base.show(io::IO, p::PauliOperator) = print(io, ["+","i","-","-i"][p.sign+1], mapslices(x->ops_chars[x], p.singlequbitops, 1)...);
Base.length(p::PauliOperator) = length(p.singlequbitops)
Base.ones(p::PauliOperator) = PauliOperator(0, ones(p.singlequbitops))
SparseArrays.permute(p::PauliOperator, perm::Array{Int, 1}) = PauliOperator(p.sign, p.singlequbitops[perm])
Base.hash(p::PauliOperator, h::UInt) = hash(p.sign, hash(p.singlequbitops, h))
Base.:(==)(l::PauliOperator, r::PauliOperator) = l.sign==r.sign && l.singlequbitops==r.singlequbitops
################################################################################
# ClifforOperator and constructors
################################################################################
struct CliffordOperator
zimages::Array{PauliOperator,1}
ximages::Array{PauliOperator,1}
end
################################################################################
# methods for CliffordOperator
################################################################################
function Base.show(io::IO, c::CliffordOperator)
len = length(c.zimages)
for (i, im) in enumerate(c.zimages)
print(io, PauliOperator(0, setindex!(ones(Int, len), 2, i)), " → ", im, '\n')
end
for (i, im) in enumerate(c.ximages)
print(io, PauliOperator(0, setindex!(ones(Int, len), 3, i)), " → ", im, '\n')
end
end
function ⊗(l::CliffordOperator, r::CliffordOperator)
lones = ones(l.zimages[1])
rones = ones(r.zimages[1])
CliffordOperator(
vcat([op⊗rones for op in l.zimages], [lones⊗op for op in r.zimages]),
vcat([op⊗rones for op in l.ximages], [lones⊗op for op in r.ximages]))
end
################################################################################
# mixed methods
################################################################################
function Base.:*(p::PauliOperator, sign::Int)
PauliOperator((p.sign+sign)%4, p.singlequbitops)
end
function Base.:*(c::CliffordOperator, p::PauliOperator)
sign = p.sign
unsigned = prod((i_op for i_op in enumerate(p.singlequbitops) if i_op[2]≠1)) do i_op
i,op = i_op
if op==2
c.zimages[i]
elseif op==3
c.ximages[i]
else
sign += 3
c.zimages[i]*c.ximages[i]
end
end
unsigned*p.sign
end
################################################################################
# fast in-place methods
################################################################################
multiplication_table_s = Array{Int,2}(
# I Z X Y <-- second argument
[[0 0 0 0]; # I <-- first argument
[0 0 1 3]; # Z
[0 3 0 1]; # X
[0 1 3 0]] # Y
)
multiplication_table_op = Array{Int,2}(
# I Z X Y <-- second argument
[[1 2 3 4]; # I <-- first argument
[2 1 4 3]; # Z
[3 4 1 2]; # X
[4 3 2 1]] # Y
)
function mul!(l::PauliOperator, r::PauliOperator, pans::PauliOperator)
pans.sign = l.sign+r.sign
for i in 1:length(l.singlequbitops)
#pans.sign += multiplication_table_s[l.singlequbitops[i],r.singlequbitops[i]]
if l.singlequbitops[i] != 1 && r.singlequbitops[i] != 1 && l.singlequbitops[i] != r.singlequbitops[i]
pans.sign += ((3+l.singlequbitops[i]-r.singlequbitops[i])%3-1)*2+3
end
#pans.singlequbitops[i] = multiplication_table_op[l.singlequbitops[i],r.singlequbitops[i]]
if l.singlequbitops[i] % 2 == 1
pans.singlequbitops[i] = (l.singlequbitops[i]+r.singlequbitops[i]+2)%4+1
else
pans.singlequbitops[i] = (l.singlequbitops[i]-r.singlequbitops[i]+4)%4+1
end
end
pans.sign %= 4
nothing
end
function mul!(c::CliffordOperator, p::PauliOperator, pans::PauliOperator)
# XXX Unsafe if p and pans are the same!
pans.sign = p.sign
pans.singlequbitops = ones(p.singlequbitops)
for i in 1:length(p.singlequbitops)
if p.singlequbitops[i] == 1
continue
elseif p.singlequbitops[i] == 2
mul!(pans, c.zimages[i], pans)
elseif p.singlequbitops[i] == 3
mul!(pans, c.ximages[i], pans)
else
mul!(pans, c.zimages[i], pans)
mul!(pans, c.ximages[i], pans)
pans.sign = (pans.sign+3)%4
end
end
nothing
end
################################################################################
# Enumerating the Clifford group
################################################################################
using IterTools
using Combinatorics
_images(n) = ([ops...]
for ops
in Iterators.drop(product((1:4 for i in 1:n)...),1))
"""
IIII...X -> σσSσ...σ where S≠I
generate arbitrary IIII...Z -> σσ_σ...σ and fill in the blank so that it anticommutes
"""
function _anticomm_constrained_images(n::Int, im::Array{Int,1})
if n==1
return [[[3],[4]],[[4],[2]],[[2],[3]]][im[1]-1]
end
nonIind = findfirst(x->x!=1, im)
nonI = im[nonIind]
im_except = deleteat!(copy(im), nonIind)
function filler(new_im_except::Array{Int,1})::Tuple{Int,Int}
com = commutes(im_except, new_im_except)
if !com
return (1, nonI)
elseif nonI == 2
return (3,4)
elseif nonI == 3
return (4,2)
else
return (2,3)
end
end
iterator = ([ops...] for ops in product((1:4 for i in 1:n-1)...))
return (insert!(copy(ops), nonIind, f)
for ops in iterator
for f in filler(ops))
end
_imagepairs(n::Int) = ((Xim, Zim)
for Xim
in _images(n)
for Zim
in _anticomm_constrained_images(n, Xim))
function _unsigned_unpermuted_clifford_group(n::Int)
if n==1
return _imagepairs(1)
end
pool = collect(_imagepairs(n))
comb = combinations(pool, n)
# In the above combination, no two pairs will be the same,
# but we can still have (a) [(A, _), (A, _), ...] or (b) [(A, _), (_, A), ...]
# which is not a Clifford operator (not injective).
# However, both will fail the commutative test, because
# anticommutation is already enforced inside the pair.
# It would still be nice if we can completely skip generating
# those though.
pred(c) = all(zxpairs->all(zx->commutes(zx[1],zx[2]),
product(zxpairs[1],zxpairs[2])),
combinations(c,2))
return Iterators.filter(pred, comb)
end
function _permute(n::Int, unpermuted)
Iterators.flatten(permutations(unp) for unp in unpermuted)
end
function clifford_group(n::Int)
unsigned_group = collect(_permute(n, _unsigned_unpermuted_clifford_group(n)))
sign_perms = product(([0,2] for i in 1:2n)...)
return (CliffordOperator(
[PauliOperator(s, zx[1]) for (s,zx) in zip(signs[1:n],zximages)],
[PauliOperator(s, zx[2]) for (s,zx) in zip(signs[n+1:end],zximages)])
for signs in sign_perms
for zximages in unsigned_group)
end
signs(n) = (2*n)^2
perms(n) = factorial(n)
signperms(n) = signs(n)*perms(n)
################################################################################
# Helpers
################################################################################
function expand_2toN(n, q1, q2, p::PauliOperator)
Is = PauliOperator(0, ones(Int, n-2))
perm = collect(1:n)
if q1==2
perm[[1,2,q2]] = perm[[q2,1,2]]
elseif q2==1
perm[[1,2,q1]] = perm[[2,1,q1]]
else
perm[[1,2,q1,q2]] = perm[[q1,q2,1,2]]
end
return permute(p⊗Is, perm)
end
end