From d983a48a50fe5edc29f485ca90c4dc3a09b98b77 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sat, 2 Nov 2024 18:19:37 +0500 Subject: [PATCH 1/5] noncliff: add tests for conjugate destabs --- src/nonclifford.jl | 2 +- test/test_nonclifford_quantumoptics.jl | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/nonclifford.jl b/src/nonclifford.jl index 70f99e42f..a628580d5 100644 --- a/src/nonclifford.jl +++ b/src/nonclifford.jl @@ -114,7 +114,7 @@ with ϕᵢⱼ | Pᵢ | Pⱼ: See also: [`GeneralizedStabilizer`](@ref) """ -function apply!(state::GeneralizedStabilizer, gate::AbstractCliffordOperator) # TODO conjugate also the destabs +function apply!(state::GeneralizedStabilizer, gate::AbstractCliffordOperator) apply!(state.stab, gate) state end diff --git a/test/test_nonclifford_quantumoptics.jl b/test/test_nonclifford_quantumoptics.jl index a520578d7..1d91c0d31 100644 --- a/test/test_nonclifford_quantumoptics.jl +++ b/test/test_nonclifford_quantumoptics.jl @@ -50,3 +50,24 @@ qo_tgate.data[2,2] = exp(im*pi/4) end end end + +function test_conj_destabs(num_qubits, gates) + for _ in 1:5 + s = random_stabilizer(num_qubits) + sm = GeneralizedStabilizer(s) + @test dm(Ket(s)) ≈ Operator(sm) + for C in gates + @test Operator(C) * Operator(sm) * Operator(C)' ≈ Operator(apply!(sm, C)) + end + end +end + +@testset "conjugate destabs" begin + @testset "Single-qubit Clifford gate" begin + test_conj_destabs(1, [tHadamard, tPhase, tId1]) + end + + @testset "Two-qubit Clifford gate" begin + test_conj_destabs(2, [tCNOT, tCPHASE, tSWAP]) + end +end From f9342f3bd102c95a943c75c31bec36a5fa6c0991 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sat, 2 Nov 2024 19:08:25 +0500 Subject: [PATCH 2/5] add code review suggestions --- test/test_nonclifford_quantumoptics.jl | 34 ++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/test/test_nonclifford_quantumoptics.jl b/test/test_nonclifford_quantumoptics.jl index 1d91c0d31..8458418e7 100644 --- a/test/test_nonclifford_quantumoptics.jl +++ b/test/test_nonclifford_quantumoptics.jl @@ -51,23 +51,27 @@ qo_tgate.data[2,2] = exp(im*pi/4) end end -function test_conj_destabs(num_qubits, gates) - for _ in 1:5 - s = random_stabilizer(num_qubits) - sm = GeneralizedStabilizer(s) - @test dm(Ket(s)) ≈ Operator(sm) - for C in gates - @test Operator(C) * Operator(sm) * Operator(C)' ≈ Operator(apply!(sm, C)) +@testset "Conjugate destabs" begin + for (num_qubits, gates) in [(1, [tHadamard, tPhase, tId1]), (2, [tCNOT, tCPHASE, tSWAP])] + @testset "$num_qubits-qubit Clifford gate" begin + for _ in 1:10 + s = random_stabilizer(num_qubits) + sm = GeneralizedStabilizer(s) + @test dm(Ket(s)) ≈ Operator(sm) + for C in gates + @test Operator(C) * Operator(sm) * Operator(C)' ≈ Operator(apply!(sm, C)) + end + end end end -end -@testset "conjugate destabs" begin - @testset "Single-qubit Clifford gate" begin - test_conj_destabs(1, [tHadamard, tPhase, tId1]) - end - - @testset "Two-qubit Clifford gate" begin - test_conj_destabs(2, [tCNOT, tCPHASE, tSWAP]) + @testset "non-clifford gate" begin + for n in 5:10 + i = rand(1:(n-1)) + eg = embed(n, i, pcT) + sm = GeneralizedStabilizer(random_stabilizer(n)) + @test dm(Ket(sm.stab)) ≈ Operator(sm) + @test Operator(eg) * Operator(sm) * Operator(eg)' ≈ Operator(apply!(sm, eg)) + end end end From 157fcbbd5223f020a9730852ee73a86364761259 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sat, 2 Nov 2024 19:25:58 +0500 Subject: [PATCH 3/5] use enumerate_cliffords(n, clifford_cardinality(n)) for multi-qubit CliffordOperators --- test/test_nonclifford_quantumoptics.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/test_nonclifford_quantumoptics.jl b/test/test_nonclifford_quantumoptics.jl index 8458418e7..33a033f69 100644 --- a/test/test_nonclifford_quantumoptics.jl +++ b/test/test_nonclifford_quantumoptics.jl @@ -52,8 +52,16 @@ qo_tgate.data[2,2] = exp(im*pi/4) end @testset "Conjugate destabs" begin - for (num_qubits, gates) in [(1, [tHadamard, tPhase, tId1]), (2, [tCNOT, tCPHASE, tSWAP])] - @testset "$num_qubits-qubit Clifford gate" begin + test_cases = [ + (1, [tHadamard, tPhase, tId1]), + (2, [tCNOT, tCPHASE, tSWAP]), + (3, [enumerate_cliffords(3, clifford_cardinality(3))]), + (4, [enumerate_cliffords(4, clifford_cardinality(4))]), + (5, [enumerate_cliffords(5, clifford_cardinality(5))]) + ] + + for (num_qubits, gates) in test_cases + @testset "Conjugate destabs test using $num_qubits-qubit Clifford gate" begin for _ in 1:10 s = random_stabilizer(num_qubits) sm = GeneralizedStabilizer(s) @@ -65,7 +73,7 @@ end end end - @testset "non-clifford gate" begin + @testset "Conjugate destabs test using non-Clifford gate" begin for n in 5:10 i = rand(1:(n-1)) eg = embed(n, i, pcT) From 80ca8830e64380d1f633264abce8c2a7414ad2fe Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sat, 2 Nov 2024 22:45:38 +0500 Subject: [PATCH 4/5] pardon: occurred to me late... improved the quality of testing --- test/test_nonclifford_quantumoptics.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_nonclifford_quantumoptics.jl b/test/test_nonclifford_quantumoptics.jl index 33a033f69..5db65d4b8 100644 --- a/test/test_nonclifford_quantumoptics.jl +++ b/test/test_nonclifford_quantumoptics.jl @@ -55,9 +55,9 @@ end test_cases = [ (1, [tHadamard, tPhase, tId1]), (2, [tCNOT, tCPHASE, tSWAP]), - (3, [enumerate_cliffords(3, clifford_cardinality(3))]), - (4, [enumerate_cliffords(4, clifford_cardinality(4))]), - (5, [enumerate_cliffords(5, clifford_cardinality(5))]) + (3, [enumerate_cliffords(3, clifford_cardinality(3)), CliffordOperator(sHadamard(3), 3), CliffordOperator(sCNOT(1, 2), 3)]), + (4, [enumerate_cliffords(4, clifford_cardinality(4)), CliffordOperator(sHadamard(4), 4), CliffordOperator(sCNOT(2, 1), 4)]), + (5, [enumerate_cliffords(5, clifford_cardinality(5)), CliffordOperator(sHadamard(5), 5), CliffordOperator(sCNOT(2, 3), 5)]) ] for (num_qubits, gates) in test_cases From 3bc6a04b8efd23ded29ad507d0490d05b1a12ad6 Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Sun, 3 Nov 2024 10:12:19 -0500 Subject: [PATCH 5/5] simplify some tests --- test/test_nonclifford_quantumoptics.jl | 46 ++++++++++---------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/test/test_nonclifford_quantumoptics.jl b/test/test_nonclifford_quantumoptics.jl index 5db65d4b8..be14c8813 100644 --- a/test/test_nonclifford_quantumoptics.jl +++ b/test/test_nonclifford_quantumoptics.jl @@ -51,35 +51,25 @@ qo_tgate.data[2,2] = exp(im*pi/4) end end -@testset "Conjugate destabs" begin - test_cases = [ - (1, [tHadamard, tPhase, tId1]), - (2, [tCNOT, tCPHASE, tSWAP]), - (3, [enumerate_cliffords(3, clifford_cardinality(3)), CliffordOperator(sHadamard(3), 3), CliffordOperator(sCNOT(1, 2), 3)]), - (4, [enumerate_cliffords(4, clifford_cardinality(4)), CliffordOperator(sHadamard(4), 4), CliffordOperator(sCNOT(2, 1), 4)]), - (5, [enumerate_cliffords(5, clifford_cardinality(5)), CliffordOperator(sHadamard(5), 5), CliffordOperator(sCNOT(2, 3), 5)]) - ] - - for (num_qubits, gates) in test_cases - @testset "Conjugate destabs test using $num_qubits-qubit Clifford gate" begin - for _ in 1:10 - s = random_stabilizer(num_qubits) - sm = GeneralizedStabilizer(s) - @test dm(Ket(s)) ≈ Operator(sm) - for C in gates - @test Operator(C) * Operator(sm) * Operator(C)' ≈ Operator(apply!(sm, C)) - end - end +@testset "apply!" begin + for n in [1,2,3,4] # exponential cost in this term + s = random_stabilizer(n) + sm = GeneralizedStabilizer(s) + @test dm(Ket(s)) ≈ Operator(sm) + # test clifford gates + for _ in 1:10 + c = random_clifford(n) + uc = Operator(c) + @test uc * Operator(sm) * uc' ≈ Operator(apply!(sm, c)) # sm is modified in place for the next round end - end - - @testset "Conjugate destabs test using non-Clifford gate" begin - for n in 5:10 - i = rand(1:(n-1)) - eg = embed(n, i, pcT) - sm = GeneralizedStabilizer(random_stabilizer(n)) - @test dm(Ket(sm.stab)) ≈ Operator(sm) - @test Operator(eg) * Operator(sm) * Operator(eg)' ≈ Operator(apply!(sm, eg)) + # and now some (repeated) non-clifford ops + for _ in 1:5 # exponential cost in this term + i = rand(1:n) + nc = embed(n, i, pcT) + apply!(sm, nc) # in-place + c = random_clifford(n) + uc = Operator(c) + @test uc * Operator(sm) * uc' ≈ Operator(apply!(sm, c)) # sm is modified in place for the next round end end end