Skip to content

Commit f1f22ab

Browse files
committed
fixed more gates
1 parent fe3ae66 commit f1f22ab

File tree

2 files changed

+94
-64
lines changed

2 files changed

+94
-64
lines changed

src/apply_right.jl

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ function apply_right!(l::CliffordOperator, r::AbstractCliffordOperator; phases=t
66
apply!(CliffordOperator(r, nqubits(l)), l; phases=phases)
77
end
88

9+
# helper
10+
function mul_right_log_i!(l::PauliOperator, r::PauliOperator)
11+
x = mul_right!(l, r; phases=Val(true))
12+
x.phase[] = x.phase[] & 0x02
13+
return x
14+
end
15+
16+
# new symbolics
17+
"""A \"symbolic\" single-qubit SQRTZ. See also : [`SingleQubitOperator`](@ref), [`AbstractSymbolicOperator`](@ref)"""
18+
struct sSQRTZ <: AbstractSingleQubitOperator
19+
q::Int
20+
sSQRTZ(q) = if q<0 throw(NoZeroQubit) else new(q) end
21+
end
22+
"""A \"symbolic\" single-qubit InvSQRTZ. See also : [`SingleQubitOperator`](@ref), [`AbstractSymbolicOperator`](@ref)"""
23+
struct sInvSQRTZ <: AbstractSingleQubitOperator
24+
q::Int
25+
sInvSQRTZ(q) = if q<0 throw(NoZeroQubit) else new(q) end
26+
end
927

1028
##############################
1129
# Single-qubit gates
@@ -17,21 +35,20 @@ function apply_right!(l::CliffordOperator, r::sHadamard)
1735
end
1836

1937
function apply_right!(l::CliffordOperator, r::sHadamardXY)
20-
mul_right!(l[r.q], l[nqubits(l)+r.q]; phases=Val(false))
38+
l[r.q] = mul_right_log_i!(l[r.q], l[nqubits(l)+r.q])
2139
apply_right!(l, sY(r.q))
2240
return l
2341
end
2442

2543
function apply_right!(l::CliffordOperator, r::sHadamardYZ)
26-
mul_right!(l[nqubits(l)+r.q], l[r.q]; phases=Val(false))
44+
l[nqubits(l)+r.q] = mul_right_log_i!(l[nqubits(l)+r.q], l[r.q])
2745
apply_right!(l, sZ(r.q))
2846
return l
2947
end
3048

31-
function apply_right!(l::CliffordOperator, r::sPhase)
32-
mul_right!(l[r.q], l[nqubits(l)+r.q]; phases=Val(true))
33-
return l
34-
end
49+
# function apply_right!(l::CliffordOperator, r::sPhase)
50+
# return l
51+
# end
3552

3653
# function apply_right!(l::CliffordOperator, r::sInvPhase)
3754
# return l
@@ -60,7 +77,7 @@ function apply_right!(l::CliffordOperator, r::sSQRTX)
6077
end
6178

6279
function apply_right!(l::CliffordOperator, r::sInvSQRTX)
63-
mul_right!(l[nqubits(l)+r.q], l[r.q]; phases=Val(false))
80+
l[nqubits(l)+r.q] = mul_right_log_i!(l[nqubits(l)+r.q], l[r.q])
6481
return l
6582
end
6683

@@ -76,6 +93,17 @@ function apply_right!(l::CliffordOperator, r::sInvSQRTY)
7693
return l
7794
end
7895

96+
function apply_right!(l::CliffordOperator, r::sSQRTZ)
97+
apply_right!(l, sInvSQRTZ(r.q))
98+
apply_right!(l, sZ(r.q))
99+
return l
100+
end
101+
102+
function apply_right!(l::CliffordOperator, r::sInvSQRTZ)
103+
l[r.q] = mul_right_log_i!(l[r.q], l[nqubits(l)+r.q])
104+
return l
105+
end
106+
79107
# function apply_right!(l::CliffordOperator, r::sCXYZ)
80108
# return l
81109
# end
@@ -84,7 +112,7 @@ end
84112
# return l
85113
# end
86114

87-
function apply_right!(l::CliffordOperator, r::sId1)
115+
function apply_right!(l::CliffordOperator, ::sId1)
88116
return l
89117
end
90118

@@ -140,8 +168,8 @@ end
140168
# end
141169

142170
function apply_right!(l::CliffordOperator, r::sZCX)
143-
mul_right!(l[nqubits(l)+r.q2], l[nqubits(l)+r.q1]; phases=Val(true))
144-
mul_right!(l[r.q1], l[r.q2]; phases=Val(true))
171+
l[nqubits(l)+r.q2] = mul_right!(l[nqubits(l)+r.q2], l[nqubits(l)+r.q1]; phases=Val(true))
172+
l[r.q1] = mul_right!(l[r.q1], l[r.q2]; phases=Val(true))
145173
return l
146174
end
147175

@@ -153,14 +181,14 @@ function apply_right!(l::CliffordOperator, r::sZCY)
153181
end
154182

155183
function apply_right!(l::CliffordOperator, r::sZCZ)
156-
mul_right!(l[r.q2], l[nqubits(l)+r.q1]; phases=Val(true))
157-
mul_right!(l[r.q1], l[nqubits(l)+r.q2]; phases=Val(true))
184+
l[r.q2] = mul_right!(l[r.q2], l[nqubits(l)+r.q1]; phases=Val(true))
185+
l[r.q1] = mul_right!(l[r.q1], l[nqubits(l)+r.q2]; phases=Val(true))
158186
return l
159187
end
160188

161189
function apply_right!(l::CliffordOperator, r::sXCX)
162-
mul_right!(l[nqubits(l)+r.q2], l[r.q1]; phases=Val(true))
163-
mul_right!(l[nqubits(l)+r.q1], l[r.q2]; phases=Val(true))
190+
l[nqubits(l)+r.q2] = mul_right!(l[nqubits(l)+r.q2], l[r.q1]; phases=Val(true))
191+
l[nqubits(l)+r.q1] = mul_right!(l[nqubits(l)+r.q1], l[r.q2]; phases=Val(true))
164192
return l
165193
end
166194

@@ -208,10 +236,10 @@ function apply_right!(l::CliffordOperator, r::sSQRTZZ)
208236
end
209237

210238
function apply_right!(l::CliffordOperator, r::sInvSQRTZZ)
211-
mul_right!(l[r.q1], l[nqubits(l)+r.q1]; phases=Val(false))
212-
mul_right!(l[r.q1], l[nqubits(l)+r.q2]; phases=Val(false))
213-
mul_right!(l[r.q2], l[nqubits(l)+r.q1]; phases=Val(false))
214-
mul_right!(l[r.q2], l[nqubits(l)+r.q2]; phases=Val(false))
239+
l[r.q1] = mul_right_log_i!(l[r.q1], l[nqubits(l)+r.q1])
240+
l[r.q1] = mul_right_log_i!(l[r.q1], l[nqubits(l)+r.q2])
241+
l[r.q2] = mul_right_log_i!(l[r.q2], l[nqubits(l)+r.q1])
242+
l[r.q2] = mul_right_log_i!(l[r.q2], l[nqubits(l)+r.q2])
215243
return l
216244
end
217245

@@ -223,10 +251,10 @@ function apply_right!(l::CliffordOperator, r::sSQRTXX)
223251
end
224252

225253
function apply_right!(l::CliffordOperator, r::sInvSQRTXX)
226-
mul_right!(l[nqubits(l)+r.q1], l[r.q1]; phases=Val(false))
227-
mul_right!(l[nqubits(l)+r.q1], l[r.q2]; phases=Val(false))
228-
mul_right!(l[nqubits(l)+r.q2], l[r.q1]; phases=Val(false))
229-
mul_right!(l[nqubits(l)+r.q2], l[r.q2]; phases=Val(false))
254+
l[nqubits(l)+r.q1] = mul_right_log_i!(l[nqubits(l)+r.q1], l[r.q1])
255+
l[nqubits(l)+r.q1] = mul_right_log_i!(l[nqubits(l)+r.q1], l[r.q2])
256+
l[nqubits(l)+r.q2] = mul_right_log_i!(l[nqubits(l)+r.q2], l[r.q1])
257+
l[nqubits(l)+r.q2] = mul_right_log_i!(l[nqubits(l)+r.q2], l[r.q2])
230258
return l
231259
end
232260

@@ -238,12 +266,12 @@ function apply_right!(l::CliffordOperator, r::sSQRTYY)
238266
end
239267

240268
function apply_right!(l::CliffordOperator, r::sInvSQRTYY)
241-
mul_right!(l[r.q1], l[nqubits(l)+r.q1]; phases=Val(false))
242-
mul_right!(l[nqubits(l)+r.q1], l[nqubits(l)+r.q2]; phases=Val(false))
243-
mul_right!(l[nqubits(l)+r.q1], l[r.q2]; phases=Val(false))
244-
mul_right!(l[r.q2], l[r.q1]; phases=Val(false))
245-
mul_right!(l[nqubits(l)+r.q2], l[r.q1]; phases=Val(false))
246-
mul_right!(l[r.q1], l[nqubits(l)+r.q1]; phases=Val(false))
269+
l[r.q1] = mul_right_log_i!(l[r.q1], l[nqubits(l)+r.q1])
270+
l[nqubits(l)+r.q1] = mul_right_log_i!(l[nqubits(l)+r.q1], l[nqubits(l)+r.q2])
271+
l[nqubits(l)+r.q1] = mul_right_log_i!(l[nqubits(l)+r.q1], l[r.q2])
272+
l[r.q2] = mul_right_log_i!(l[r.q2], l[r.q1])
273+
l[nqubits(l)+r.q2] = mul_right_log_i!(l[nqubits(l)+r.q2], l[r.q1])
274+
l[r.q1] = mul_right_log_i!(l[r.q1], l[nqubits(l)+r.q1])
247275
rowswap!(tab(l), r.q1, nqubits(l)+r.q1)
248276
rowswap!(tab(l), r.q2, nqubits(l)+r.q2)
249277
apply_right!(l, sZ(r.q2))

test/test_apply_right.jl

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
apply!(CliffordOperator(r, nqubits(l)), l; phases=phases)
88
end
99

10-
q = 2
11-
shots = 1
10+
q = 64
11+
shots = 16
1212

1313
# @testset "Apply Right single-qubit" begin
1414
# for gate in subtypes(AbstractSingleQubitOperator)
15-
# if gate in [sPhase, sInvPhase, SingleQubitOperator, sXYZ, sZYX]
15+
# if gate in [sPhase, sInvPhase, SingleQubitOperator, sCXYZ, sCZYX]
1616
# continue
1717
# end
1818
# r = gate(rand(1:q))
@@ -47,7 +47,7 @@
4747
@test isequal(apply_right!(copy(l), sHadamard(q1)), apply_right_slow!(l, sHadamard(q1)))
4848
@test isequal(apply_right!(copy(l), sHadamardXY(q1)), apply_right_slow!(l, sHadamardXY(q1)))
4949
@test isequal(apply_right!(copy(l), sHadamardYZ(q1)), apply_right_slow!(l, sHadamardYZ(q1)))
50-
@test isequal(apply_right!(copy(l), sPhase(q1)), apply_right_slow!(l, sPhase(q1)))
50+
# @test isequal(apply_right!(copy(l), sPhase(q1)), apply_right_slow!(l, sPhase(q1)))
5151
# @test isequal(apply_right!(copy(l), sInvPhase(q1)), apply_right_slow!(l, sInvPhase(q1)))
5252
@test isequal(apply_right!(copy(l), sX(q1)), apply_right_slow!(l, sX(q1)))
5353
@test isequal(apply_right!(copy(l), sY(q1)), apply_right_slow!(l, sY(q1)))
@@ -56,43 +56,45 @@
5656
@test isequal(apply_right!(copy(l), sInvSQRTX(q1)), apply_right_slow!(l, sInvSQRTX(q1)))
5757
@test isequal(apply_right!(copy(l), sSQRTY(q1)), apply_right_slow!(l, sSQRTY(q1)))
5858
@test isequal(apply_right!(copy(l), sInvSQRTY(q1)), apply_right_slow!(l, sInvSQRTY(q1)))
59+
# @test isequal(apply_right!(copy(l), sSQRTZ(q1)), apply_right_slow!(l, sSQRTZ(q1)))
60+
# @test isequal(apply_right!(copy(l), sInvSQRTZ(q1)), apply_right_slow!(l, sInvSQRTZ(q1)))
5961
# @test isequal(apply_right!(copy(l), sCXYZ(q1)), apply_right_slow!(l, sCXYZ(q1)))
6062
# @test isequal(apply_right!(copy(l), sCZYX(q1)), apply_right_slow!(l, sCZYX(q1)))
6163
@test isequal(apply_right!(copy(l), sId1(q1)), apply_right_slow!(l, sId1(q1)))
6264
end
6365
end
6466

65-
# @testset "Apply Right two-qubit" begin
66-
# for _ in 1:shots
67-
# l = random_clifford(q)
68-
# q1 = rand(1:q); q2 = rand(setdiff(1:q, [q1]))
67+
@testset "Apply Right two-qubit" begin
68+
for _ in 1:shots
69+
l = random_clifford(q)
70+
q1 = rand(1:q); q2 = rand(setdiff(1:q, [q1]))
6971

70-
# @test isequal(apply_right!(copy(l), sSWAP(q1, q2)), apply_right_slow!(l, sSWAP(q1, q2)))
71-
# # @test isequal(apply_right!(copy(l), sSWAPCX(q1, q2)), apply_right_slow!(l, sSWAPCX(q1, q2)))
72-
# # @test isequal(apply_right!(copy(l), sInvSWAPCX(q1, q2)), apply_right_slow!(l, sInvSWAPCX(q1, q2)))
73-
# @test isequal(apply_right!(copy(l), sISWAP(q1, q2)), apply_right_slow!(l, sISWAP(q1, q2)))
74-
# @test isequal(apply_right!(copy(l), sInvISWAP(q1, q2)), apply_right_slow!(l, sInvISWAP(q1, q2)))
75-
# # @test isequal(apply_right!(copy(l), sCZSWAP(q1, q2)), apply_right_slow!(l, sCZSWAP(q1, q2)))
76-
# # @test isequal(apply_right!(copy(l), sCXSWAP(q1, q2)), apply_right_slow!(l, sCXSWAP(q1, q2)))
77-
# @test isequal(apply_right!(copy(l), sCNOT(q1, q2)), apply_right_slow!(l, sCNOT(q1, q2)))
78-
# # @test isequal(apply_right!(copy(l), sCPHASE(q1, q2)), apply_right_slow!(l, sCPHASE(q1, q2)))
79-
# @test isequal(apply_right!(copy(l), sZCX(q1, q2)), apply_right_slow!(l, sZCX(q1, q2)))
80-
# @test isequal(apply_right!(copy(l), sZCY(q1, q2)), apply_right_slow!(l, sZCY(q1, q2)))
81-
# @test isequal(apply_right!(copy(l), sZCZ(q1, q2)), apply_right_slow!(l, sZCZ(q1, q2)))
82-
# @test isequal(apply_right!(copy(l), sXCX(q1, q2)), apply_right_slow!(l, sXCX(q1, q2)))
83-
# @test isequal(apply_right!(copy(l), sXCY(q1, q2)), apply_right_slow!(l, sXCY(q1, q2)))
84-
# @test isequal(apply_right!(copy(l), sXCZ(q1, q2)), apply_right_slow!(l, sXCZ(q1, q2)))
85-
# @test isequal(apply_right!(copy(l), sYCX(q1, q2)), apply_right_slow!(l, sYCX(q1, q2)))
86-
# @test isequal(apply_right!(copy(l), sYCY(q1, q2)), apply_right_slow!(l, sYCY(q1, q2)))
87-
# @test isequal(apply_right!(copy(l), sYCZ(q1, q2)), apply_right_slow!(l, sYCZ(q1, q2)))
88-
# # @test isequal(apply_right!(copy(l), sZCrY(q1, q2)), apply_right_slow!(l, sZCrY(q1, q2)))
89-
# # @test isequal(apply_right!(copy(l), sInvZCrY(q1, q2)), apply_right_slow!(l, sInvZCrY(q1, q2)))
90-
# @test isequal(apply_right!(copy(l), sSQRTZZ(q1, q2)), apply_right_slow!(l, sSQRTZZ(q1, q2)))
91-
# @test isequal(apply_right!(copy(l), sInvSQRTZZ(q1, q2)), apply_right_slow!(l, sInvSQRTZZ(q1, q2)))
92-
# @test isequal(apply_right!(copy(l), sSQRTXX(q1, q2)), apply_right_slow!(l, sSQRTXX(q1, q2)))
93-
# @test isequal(apply_right!(copy(l), sInvSQRTXX(q1, q2)), apply_right_slow!(l, sInvSQRTXX(q1, q2)))
94-
# @test isequal(apply_right!(copy(l), sSQRTYY(q1, q2)), apply_right_slow!(l, sSQRTYY(q1, q2)))
95-
# @test isequal(apply_right!(copy(l), sInvSQRTYY(q1, q2)), apply_right_slow!(l, sInvSQRTYY(q1, q2)))
96-
# end
97-
# end
72+
@test isequal(apply_right!(copy(l), sSWAP(q1, q2)), apply_right_slow!(l, sSWAP(q1, q2)))
73+
# @test isequal(apply_right!(copy(l), sSWAPCX(q1, q2)), apply_right_slow!(l, sSWAPCX(q1, q2)))
74+
# @test isequal(apply_right!(copy(l), sInvSWAPCX(q1, q2)), apply_right_slow!(l, sInvSWAPCX(q1, q2)))
75+
@test isequal(apply_right!(copy(l), sISWAP(q1, q2)), apply_right_slow!(l, sISWAP(q1, q2)))
76+
@test isequal(apply_right!(copy(l), sInvISWAP(q1, q2)), apply_right_slow!(l, sInvISWAP(q1, q2)))
77+
# @test isequal(apply_right!(copy(l), sCZSWAP(q1, q2)), apply_right_slow!(l, sCZSWAP(q1, q2)))
78+
# @test isequal(apply_right!(copy(l), sCXSWAP(q1, q2)), apply_right_slow!(l, sCXSWAP(q1, q2)))
79+
@test isequal(apply_right!(copy(l), sCNOT(q1, q2)), apply_right_slow!(l, sCNOT(q1, q2)))
80+
# @test isequal(apply_right!(copy(l), sCPHASE(q1, q2)), apply_right_slow!(l, sCPHASE(q1, q2)))
81+
@test isequal(apply_right!(copy(l), sZCX(q1, q2)), apply_right_slow!(l, sZCX(q1, q2)))
82+
@test isequal(apply_right!(copy(l), sZCY(q1, q2)), apply_right_slow!(l, sZCY(q1, q2)))
83+
@test isequal(apply_right!(copy(l), sZCZ(q1, q2)), apply_right_slow!(l, sZCZ(q1, q2)))
84+
@test isequal(apply_right!(copy(l), sXCX(q1, q2)), apply_right_slow!(l, sXCX(q1, q2)))
85+
@test isequal(apply_right!(copy(l), sXCY(q1, q2)), apply_right_slow!(l, sXCY(q1, q2)))
86+
@test isequal(apply_right!(copy(l), sXCZ(q1, q2)), apply_right_slow!(l, sXCZ(q1, q2)))
87+
@test isequal(apply_right!(copy(l), sYCX(q1, q2)), apply_right_slow!(l, sYCX(q1, q2)))
88+
@test isequal(apply_right!(copy(l), sYCY(q1, q2)), apply_right_slow!(l, sYCY(q1, q2)))
89+
@test isequal(apply_right!(copy(l), sYCZ(q1, q2)), apply_right_slow!(l, sYCZ(q1, q2)))
90+
# @test isequal(apply_right!(copy(l), sZCrY(q1, q2)), apply_right_slow!(l, sZCrY(q1, q2)))
91+
# @test isequal(apply_right!(copy(l), sInvZCrY(q1, q2)), apply_right_slow!(l, sInvZCrY(q1, q2)))
92+
@test isequal(apply_right!(copy(l), sSQRTZZ(q1, q2)), apply_right_slow!(l, sSQRTZZ(q1, q2)))
93+
@test isequal(apply_right!(copy(l), sInvSQRTZZ(q1, q2)), apply_right_slow!(l, sInvSQRTZZ(q1, q2)))
94+
@test isequal(apply_right!(copy(l), sSQRTXX(q1, q2)), apply_right_slow!(l, sSQRTXX(q1, q2)))
95+
@test isequal(apply_right!(copy(l), sInvSQRTXX(q1, q2)), apply_right_slow!(l, sInvSQRTXX(q1, q2)))
96+
@test isequal(apply_right!(copy(l), sSQRTYY(q1, q2)), apply_right_slow!(l, sSQRTYY(q1, q2)))
97+
@test isequal(apply_right!(copy(l), sInvSQRTYY(q1, q2)), apply_right_slow!(l, sInvSQRTYY(q1, q2)))
98+
end
99+
end
98100
end

0 commit comments

Comments
 (0)