-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathsymbolic_cliffords.jl
465 lines (409 loc) · 14.9 KB
/
symbolic_cliffords.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
using Random: AbstractRNG, GLOBAL_RNG
abstract type AbstractSymbolicOperator <: AbstractCliffordOperator end
abstract type AbstractSingleQubitOperator <: AbstractSymbolicOperator end
abstract type AbstractTwoQubitOperator <: AbstractSymbolicOperator end
# Stim has a good list of specialized single and two qubit operations at https://github.com/quantumlib/Stim/blob/e51ea66d213b25920e72c08e53266ec56fd14db4/src/stim/stabilizers/tableau_specialized_prepend.cc
# Note that their specialized operations are for prepends (right multiplications), while we implement append (left multiplication) operations.
"""A "symbolic" Hadamard operator which permits faster multiplication than an operator expressed as an explicit tableau.
```jldoctest
julia> sh = sHadamard(1)
Symbolic single-qubit gate on qubit 1
X ⟼ + Z
Z ⟼ + X
julia> typeof(sh)
sHadamard
julia> h = CliffordOperator(sh,1) # Transforming it back into an explicit tableau representation
X ⟼ + Z
Z ⟼ + X
julia> typeof(h)
CliffordOperator{Vector{UInt8}, Matrix{UInt64}}
```
See also: [`SingleQubitOperator`](@ref)
"""
struct sHadamard <: AbstractSingleQubitOperator
q::Int
end
"""A "symbolic" Phase operator which permits faster multiplication than an operator expressed as an explicit tableau.
```jldoctest
julia> sp = sPhase(1)
Symbolic single-qubit gate on qubit 1
X ⟼ + Y
Z ⟼ + Z
julia> typeof(sp)
sPhase
julia> p = CliffordOperator(sp,1) # Transforming it back into an explicit tableau representation
X ⟼ + Y
Z ⟼ + Z
julia> typeof(p)
CliffordOperator{Vector{UInt8}, Matrix{UInt64}}
```
See also: [`SingleQubitOperator`](@ref)
"""
struct sPhase <: AbstractSingleQubitOperator
q::Int
end
"""A "symbolic" inverse of the Phase operator.
See also: [`sPhase`](@ref), [`SingleQubitOperator`](@ref)
"""
struct sInvPhase <: AbstractSingleQubitOperator
q::Int
end
"""A "symbolic" single-qubit Identity operation.
See also: [`SingleQubitOperator`](@ref)
"""
struct sId1 <: AbstractSingleQubitOperator
q::Int
end
"""A "symbolic" single-qubit Pauli X operation.
See also: [`SingleQubitOperator`](@ref)
"""
struct sX <: AbstractSingleQubitOperator
q::Int
end
"""A "symbolic" single-qubit Pauli Y operation.
See also: [`SingleQubitOperator`](@ref)
"""
struct sY <: AbstractSingleQubitOperator
q::Int
end
"""A "symbolic" single-qubit Pauli Z operation.
See also: [`SingleQubitOperator`](@ref)
"""
struct sZ <: AbstractSingleQubitOperator
q::Int
end
"""A "symbolic" general single-qubit operator which permits faster multiplication than an operator expressed as an explicit tableau.
```jldoctest
julia> op = SingleQubitOperator(2, true, true, true, false, true, true) # Tableau components and phases
Symbolic single-qubit gate on qubit 2
X ⟼ - Y
Z ⟼ - X
julia> typeof(op)
SingleQubitOperator
julia> t_op = CliffordOperator(op, 3) # Transforming it back into an explicit tableau representation (specifying the size)
X__ ⟼ + X__
_X_ ⟼ - _Y_
__X ⟼ + __X
Z__ ⟼ + Z__
_Z_ ⟼ - _X_
__Z ⟼ + __Z
julia> typeof(t_op)
CliffordOperator{Vector{UInt8}, Matrix{UInt64}}
julia> CliffordOperator(op, 1, compact=true) # You can also extract just the non-trivial part of the tableau
X ⟼ - Y
Z ⟼ - X
```
See also: [`sHadamard`](@ref), [`sPhase`](@ref), [`sId1`](@ref), [`sX`](@ref), [`sY`](@ref), [`sZ`](@ref), [`CliffordOperator`](@ref)
Or simply consult `subtypes(QuantumClifford.AbstractSingleQubitOperator)` and
`subtypes(QuantumClifford.AbstractTwoQubitOperator)` for a full list.
You can think of the `s` prefix as "symbolic" or "sparse".
"""
struct SingleQubitOperator <: AbstractSingleQubitOperator
q::Int
xx::Bool
xz::Bool
zx::Bool
zz::Bool
px::Bool
pz::Bool
end
SingleQubitOperator(h::sHadamard) = SingleQubitOperator(h.q, false, true , true , false, false, false)
SingleQubitOperator(p::sPhase) = SingleQubitOperator(p.q, true , true , false, true , false, false)
SingleQubitOperator(p::sInvPhase) = SingleQubitOperator(p.q, true , true , false, true , true , false)
SingleQubitOperator(p::sId1) = SingleQubitOperator(p.q, true , false, false, true , false, false)
SingleQubitOperator(p::sX) = SingleQubitOperator(p.q, true , false, false, true , false, true)
SingleQubitOperator(p::sY) = SingleQubitOperator(p.q, true , false, false, true , true , true)
SingleQubitOperator(p::sZ) = SingleQubitOperator(p.q, true , false, false, true , true , false)
SingleQubitOperator(o::SingleQubitOperator) = o
function SingleQubitOperator(op::CliffordOperator, qubit)
nqubits(op)==1 || throw(DimensionMismatch("You are trying to convert a multiqubit `CliffordOperator` into a symbolic `SingleQubitOperator`."))
SingleQubitOperator(qubit,op.tab[1,1]...,op.tab[2,1]...,(~).(iszero.(op.tab.phases))...)
end
SingleQubitOperator(op::CliffordOperator) = SingleQubitOperator(op, 1)
CliffordOperator(op::AbstractSingleQubitOperator, n; kw...) = CliffordOperator(SingleQubitOperator(op), n; kw...)
function CliffordOperator(op::SingleQubitOperator, n; compact=false)
if compact
n==1 || throw(ArgumentError("Set `n=1` as a `SingleQubitOperator` being compacted (`compact=true`) has to result in a 1D `CliffordOperator`."))
return CliffordOperator(Stabilizer([op.px ? 0x2 : 0x0, op.pz ? 0x2 : 0x0],[op.xx op.xz; op.zx op.zz]))
else
n >= op.q || throw(DimensionMismatch("Set a larger `n`, otherwise the `SingleQubitOperator` can not fit in the allocated `CliffordOperator`. Use `compact=true` if you want to discard the target index."))
c = one(CliffordOperator, n)
c[op.q,op.q] = op.xx, op.xz # TODO define an `embed` helper function
c[n+op.q,op.q] = op.zx, op.zz
c.tab.phases[op.q] = op.px ? 0x2 : 0x0 # TODO define a `phasesview` or `phases` helper function
c.tab.phases[n+op.q] = op.pz ? 0x2 : 0x0
return c
end
end
function Base.show(io::IO, op::AbstractSingleQubitOperator)
print(io, "Symbolic single-qubit gate on qubit $(op.q)\n")
show(io, CliffordOperator(op, 1, compact=true))
end
#TODO these are too low level to be used directly in the apply! functions. Make a better abstractions.
@inline getshift(Tme::Type,col::Int) = _mod(Tme,col-1)
@inline getmask(Tme::Type,col::Int) = Tme(0x1)<<getshift(Tme,col)
@inline getbigindex(Tme::Type,col::Int) = _div(Tme,col-1)+1
Base.@propagate_inbounds function getxbit(s, r, c)
Tme = eltype(s.xzs)
s.xzs[getbigindex(Tme,c),r]&getmask(Tme,c)
end
Base.@propagate_inbounds function getzbit(s, r, c)
Tme = eltype(s.xzs)
s.xzs[end÷2+getbigindex(Tme,c),r]&getmask(Tme,c)
end
Base.@propagate_inbounds function setxbit(s, r, c, x)
Tme = eltype(s.xzs)
cbig = getbigindex(Tme,c)
s.xzs[cbig,r] &= ~getmask(Tme,c)
s.xzs[cbig,r] |= x
end
Base.@propagate_inbounds function setzbit(s, r, c, z)
Tme = eltype(s.xzs)
cbig = getbigindex(Tme,c)
s.xzs[end÷2+cbig,r] &= ~getmask(Tme,c)
s.xzs[end÷2+cbig,r] |= z
end
Base.@propagate_inbounds setxbit(s, r, c, x, shift) = setxbit(s, r, c, x<<shift)
Base.@propagate_inbounds setzbit(s, r, c, z, shift) = setzbit(s, r, c, z<<shift)
function apply!(stab::AbstractStabilizer, h::sHadamard; phases::Bool=true)
s = tab(stab)
c = h.q
@batch per=core minbatch=200 for r in eachindex(s)
x = getxbit(s, r, c)
z = getzbit(s, r, c)
setxbit(s, r, c, z)
setzbit(s, r, c, x)
phases && x!=0 && z!=0 && (s.phases[r] = (s.phases[r]+0x2)&3)
end
stab
end
function apply!(stab::AbstractStabilizer, p::sPhase; phases::Bool=true)
s = tab(stab)
c = p.q
@batch per=core minbatch=200 for r in eachindex(s)
x = getxbit(s, r, c)
z = getzbit(s, r, c)
#setxbit no op
setzbit(s, r, c, x⊻z)
phases && x!=0 && z!=0 && (s.phases[r] = (s.phases[r]+0x2)&3)
end
stab
end
function apply!(stab::AbstractStabilizer, p::sInvPhase; phases::Bool=true)
s = tab(stab)
c = p.q
@batch per=core minbatch=200 for r in eachindex(s)
x = getxbit(s, r, c)
z = getzbit(s, r, c)
#setxbit no op
setzbit(s, r, c, x⊻z)
phases && x!=0 && z==0 && (s.phases[r] = (s.phases[r]+0x2)&3)
end
stab
end
function apply!(stab::AbstractStabilizer, p::sX; phases::Bool=true)
phases || return stab
s = tab(stab)
c = p.q
@batch per=core minbatch=200 for r in eachindex(s)
x = getxbit(s, r, c)
z = getzbit(s, r, c)
z!=0 && (s.phases[r] = (s.phases[r]+0x2)&3)
end
stab
end
function apply!(stab::AbstractStabilizer, p::sZ; phases::Bool=true)
phases || return stab
s = tab(stab)
c = p.q
@batch per=core minbatch=200 for r in eachindex(s)
x = getxbit(s, r, c)
z = getzbit(s, r, c)
x!=0 && (s.phases[r] = (s.phases[r]+0x2)&3)
end
stab
end
function apply!(stab::AbstractStabilizer, p::sY; phases::Bool=true)
phases || return stab
s = tab(stab)
c = p.q
@batch per=core minbatch=200 for r in eachindex(s)
x = getxbit(s, r, c)
z = getzbit(s, r, c)
(x⊻z)!=0 && (s.phases[r] = (s.phases[r]+0x2)&3)
end
stab
end
function apply!(stab::AbstractStabilizer, ::sId1; phases::Bool=true)
stab
end
function apply!(stab::AbstractStabilizer, op::SingleQubitOperator; phases::Bool=true) # TODO Generated functions that simplify the whole `if phases` branch might be a good optimization, but a quick benchmakr comparing sHadamard to SingleQubitOperator(sHadamard) did not show a worthwhile difference.
s = tab(stab)
c = op.q
Tme = eltype(s.xzs)
sh = getshift(Tme, c)
xx,zx,xz,zz = Tme.((op.xx,op.zx,op.xz,op.zz)) .<< sh
anticom = ~iszero((~zz & xz & ~xx & zx) | ( zz & ~xz & xx & zx) | (zz & xz & xx & ~zx))
@batch per=core minbatch=200 for r in eachindex(s)
x = getxbit(s, r, c)
z = getzbit(s, r, c)
setxbit(s, r, c, (x&xx)⊻(z&zx))
setzbit(s, r, c, (x&xz)⊻(z&zz))
if phases
if op.px && ~iszero(x)
s.phases[r] += 0x2
s.phases[r] &= 3
end
if op.pz && ~iszero(z)
s.phases[r] += 0x2
s.phases[r] &= 3
end
if ~iszero(x&z) && anticom
s.phases[r] += 0x2
s.phases[r] &= 3
end
end
end
stab
end
const all_single_qubit_patterns = (
(true, false, false, true), # X, Z ↦ X, Z
(false, true, true, true), # X, Z ↦ Z, Y
(true, true, true, false), # X, Z ↦ Y, X
(false, true, true, false), # X, Z ↦ Z, X - Hadamard
(true, false, true, true), # X, Z ↦ X, Y
(true, true, false, true) # X, Z ↦ Y, Z - Phase
)
"""Generate a symbolic single-qubit gate given its index. Optionally, set non-trivial phases."""
function enumerate_single_qubit_gates(index; qubit=1, phases=(false,false))
@assert index<=6 "Only 6 single-qubit gates exit, up to the choice of phases"
if phases==(false,false)
if index==4
return sHadamard(qubit)
elseif index==6
return sPhase(qubit)
else
return SingleQubitOperator(qubit, all_single_qubit_patterns[index]..., false, false)
end
else
if index==1
if (phases[1], phases[2]) == (false, false)
return sId1(qubit)
elseif (phases[1], phases[2]) == (false, true)
return sX(qubit)
elseif (phases[1], phases[2]) == (true, false)
return sZ(qubit)
else
return sY(qubit)
end
else
return SingleQubitOperator(qubit, all_single_qubit_patterns[index]..., phases...)
end
end
end
"""Random symbolic single-qubit Clifford applied to qubit at index `qubit`.
See also: [`SingleQubitOperator`](@ref), [`random_clifford`](@ref)
"""
function random_clifford1(rng::AbstractRNG, qubit)
return enumerate_single_qubit_gates(rand(rng,1:6),qubit=qubit,phases=rand(rng,Bool,2))
end
random_clifford1(qubit) = random_clifford1(GLOBAL_RNG, qubit)
"""A "symbolic" CNOT
See also: [`SingleQubitOperator`](@ref)
"""
struct sCNOT <: AbstractTwoQubitOperator
q1::Int
q2::Int
end
"""A "symbolic" SWAP
See also: [`SingleQubitOperator`](@ref)
"""
struct sSWAP <: AbstractTwoQubitOperator
q1::Int
q2::Int
end
function apply!(stab::AbstractStabilizer, cnot::sCNOT; phases::Bool=true)
s = tab(stab)
q1 = cnot.q1
q2 = cnot.q2
Tme = eltype(s.xzs)
shift = getshift(Tme, q1) - getshift(Tme, q2)
@batch per=core minbatch=200 for r in eachindex(s)
x1 = getxbit(s, r, q1)
z1 = getzbit(s, r, q1)
x2 = getxbit(s, r, q2)
z2 = getzbit(s, r, q2)
setxbit(s, r, q2, x2⊻(x1<<-shift))
setzbit(s, r, q1, z1⊻(z2<<shift))
if phases && ~iszero( x1&z1&((x2&z2)<<shift) | (x1&z2<<shift & ~(z1|x2<<shift)) )
s.phases[r] += 0x2
s.phases[r] &= 3
end
end
stab
end
function apply!(stab::AbstractStabilizer, swap::sSWAP; phases::Bool=true)
s = tab(stab)
q1 = swap.q1
q2 = swap.q2
Tme = eltype(s.xzs)
shift = getshift(Tme, q1) - getshift(Tme, q2)
@batch per=core minbatch=200 for r in eachindex(s)
x1 = getxbit(s, r, q1)
z1 = getzbit(s, r, q1)
x2 = getxbit(s, r, q2)
z2 = getzbit(s, r, q2)
setxbit(s, r, q1, x2, shift)
setzbit(s, r, q1, z2, shift)
setxbit(s, r, q2, x1, -shift)
setzbit(s, r, q2, z1, -shift)
end
stab
end
##############################
# Functions that perform direct application of common operators without needing an operator instance
##############################
# TODO is there a reason to keep these given that sZ/sX/sY exist?
# TODO currently these are faster than sZ/sX/sY
"""Apply a Pauli Z to the `i`-th qubit of state `s`. You might prefer to use `sZ` instead of this."""
function apply_single_z!(stab::AbstractStabilizer, i)
s = tab(stab)
Tme = eltype(s.xzs)
bigi = _div(Tme,i-1)+1
smalli = _mod(Tme,i-1)
mask = Tme(0x1)<<smalli
@inbounds @simd for row in 1:size(s.xzs,2)
if !iszero(s.xzs[bigi,row] & mask)
s.phases[row] = (s.phases[row]+0x2)&0x3
end
end
stab
end
"""Apply a Pauli X to the `i`-th qubit of state `s`. You might prefer to use `sX` instead of this."""
function apply_single_x!(stab::AbstractStabilizer, i)
s = tab(stab)
Tme = eltype(s.xzs)
bigi = _div(Tme,i-1)+1
smalli = _mod(Tme,i-1)
mask = Tme(0x1)<<smalli
@inbounds @simd for row in 1:size(s.xzs,2)
if !iszero(s.xzs[end÷2+bigi,row] & mask)
s.phases[row] = (s.phases[row]+0x2)&0x3
end
end
stab
end
"""Apply a Pauli Y to the `i`-th qubit of state `s`. You might prefer to use `sY` instead of this."""
function apply_single_y!(stab::AbstractStabilizer, i)
s = tab(stab)
Tme = eltype(s.xzs)
bigi = _div(Tme,i-1)+1
smalli = _mod(Tme,i-1)
mask = Tme(0x1)<<smalli
@inbounds @simd for row in 1:size(s.xzs,2)
if !iszero((s.xzs[bigi,row] & mask) ⊻ (s.xzs[end÷2+bigi,row] & mask))
s.phases[row] = (s.phases[row]+0x2)&0x3
end
end
stab
end