Skip to content

Commit

Permalink
automate many of the ECC tests to avoid skipping new codes (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Mar 30, 2024
1 parent e051bd1 commit cae94d8
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 104 deletions.
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ end
@doset "enumerate"
@doset "quantumoptics"
@doset "ecc"
@doset "ecc_codeproperties"
@doset "ecc_decoder_all_setups"
@doset "ecc_encoding"
@doset "ecc_syndromes"
@doset "ecc_gottesman"
@doset "ecc_reedmuller"
@doset "ecc_iscss"
@doset "ecc_syndromes"
@doset "ecc_throws"
@doset "precompile"
@doset "pauliframe"
@doset "allocations"
Expand Down
13 changes: 2 additions & 11 deletions test/test_ecc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@ using QuantumClifford
using QuantumClifford.ECC
using QuantumClifford.ECC: AbstractECC

codes = [
Bitflip3(),
Steane7(),
Shor9(),
Perfect5(),
Cleve8(),
Gottesman(3),
Gottesman(5),
CSS([0 1 1 0; 1 1 0 0], [1 1 1 1]),
]
include("test_ecc_base.jl")

##
codes = all_testablable_code_instances()

function test_naive_syndrome(c::AbstractECC, e::Bool)
# create a random logical state
Expand Down
25 changes: 25 additions & 0 deletions test/test_ecc_base.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Test
using QuantumClifford
using QuantumClifford.ECC
using InteractiveUtils

# generate instances of all implemented codes to make sure nothing skips being checked

const code_instance_args = Dict(
Toric => [(3,3), (4,4), (3,6), (4,3), (5,5)],
Surface => [(3,3), (4,4), (3,6), (4,3), (5,5)],
Gottesman => [3, 4, 5],
CSS => (c -> (parity_checks_x(c), parity_checks_z(c))).([Shor9(), Steane7(), Toric(4,4)])
)

function all_testablable_code_instances(;maxn=nothing)
codeinstances = []
for t in subtypes(QuantumClifford.ECC.AbstractECC)
for c in get(code_instance_args, t, [])
codeinstance = t(c...)
!isnothing(maxn) && nqubits(codeinstance) > maxn && continue
push!(codeinstances, codeinstance)
end
end
return codeinstances
end
40 changes: 40 additions & 0 deletions test/test_ecc_codeproperties.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Test
using QuantumClifford
using QuantumClifford.ECC
using QuantumClifford.ECC: AbstractECC

include("test_ecc_base.jl")

function is_css_matrix(H)
nrows, ncols = size(H)
for i in 1:nrows
has_x = false
has_z = false
for j in 1:ncols
has_x |= H[i,j][1]
has_z |= H[i,j][2]
has_x && has_z && return false
end
end
return true
end

@testset "is CSS" begin
for code in all_testablable_code_instances()
H = parity_checks(code)
@test iscss(code) == is_css_matrix(H)
end
end

@testset "code tableau consistency" begin
for code in all_testablable_code_instances()
H = parity_checks(code)
@test nqubits(code) == size(H, 2) == code_n(code)
@test size(H, 1) == code_s(code)
@test code_s(code) + code_k(code) == code_n(code)
@test size(H, 1) < size(H, 2)
_, _, rank = canonicalize!(copy(H), ranks=true)
@test rank == size(H, 1) # TODO maybe weaken this if we want to permit codes with redundancies
@test QuantumClifford.stab_looks_good(copy(H))
end
end
13 changes: 5 additions & 8 deletions test/test_ecc_decoder_all_setups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ using QuantumClifford.ECC
import PyQDecoders
import LDPCDecoders

include("test_ecc_base.jl")

@testset "table decoder, good for small codes" begin
codes = [
Steane7(),
Shor9(),
Perfect5(),
Cleve8(),
Gottesman(3)
#Gottesman(4), bad threshold
#Gottesman(5), bad threshold
all_testablable_code_instances(;maxn=10)...
]

noise = 0.001
Expand All @@ -39,8 +35,9 @@ end

##

@testset "belief prop decoders, good for small codes" begin
@testset "belief prop decoders, good for sparse codes" begin
codes = [
# TODO
]

noise = 0.001
Expand Down
32 changes: 7 additions & 25 deletions test/test_ecc_encoding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,21 @@ using Test
using QuantumClifford
using QuantumClifford.ECC

##
include("test_ecc_base.jl")

@testset "encoding circuits - compare to algebraic construction of encoded state" begin
# This test verifies that logical measurements on an encoded state match the physical pre-encoded state.
# This test skips verifying the permutations of qubits during canonicalization are properly undone,
# i.e. we modify the code we are testing so that the canonicalization does not need any permutations.
for undoperm in [true, false],
codeexpr in [
:(Cleve8()),
:(Steane7()),
:(Shor9()),
:(Perfect5()),
:(Bitflip3()),
:(Gottesman(3)),
:(Gottesman(5)),
:(S"Y_"),
:(S"Z_"),
:(S"X_"),
:(CSS([0 1 1 0; 1 1 0 0], [1 1 1 1])),
:(Toric(3,3)),
:(Toric(3,6)),
:(Toric(6,4)),
:(Toric(8,8)),
:(Surface(3,3)),
:(Surface(3,6)),
:(Surface(6,4)),
:(Surface(8,8)),
fill(:(random_stabilizer(5,7)), 100)...
code in [
all_testablable_code_instances()...,
S"Y_",
S"Z_",
S"X_",
[random_stabilizer(5,7) for _ in 1:100]...
]

code = eval(codeexpr)
if undoperm==false
# Pre-process the tableau to remove permutations and negative phases.
# Usually that is handled by `naive_encoding_circuit`, but we just want to check both branches for its `undoperm` kwarg.
Expand All @@ -58,7 +42,5 @@ using QuantumClifford.ECC
algebraicₙ = stabilizerview(algebraicₙ) |> canonicalize!

@test (encodedₙ == algebraicₙ)

#println("$codeexpr, $(encodedₙ == algebraicₙ)")
end
end
37 changes: 0 additions & 37 deletions test/test_ecc_iscss.jl

This file was deleted.

23 changes: 3 additions & 20 deletions test/test_ecc_syndromes.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
using Test
using QuantumClifford
using QuantumClifford: mul_left!
using QuantumClifford: mul_left!, embed
using QuantumClifford.ECC
using QuantumClifford.ECC: AbstractECC

codes = [
Bitflip3(),
Steane7(),
Shor9(),
Perfect5(),
Cleve8(),
Gottesman(3),
Gottesman(5),
CSS([0 1 1 0; 1 1 0 0], [1 1 1 1]),
Toric(3,3),
Toric(3,6),
Toric(6,4),
Toric(8,8),
Surface(4,6),
Surface(8,8)
]

##
include("test_ecc_base.jl")

function pframe_naive_vs_shor_syndrome(code)
ecirc = naive_encoding_circuit(code)
Expand Down Expand Up @@ -60,7 +43,7 @@ function pframe_naive_vs_shor_syndrome(code)
end

@testset "naive and shor measurement circuits" begin
for c in codes
for c in all_testablable_code_instances()
pframe_naive_vs_shor_syndrome(c)
end
end
3 changes: 2 additions & 1 deletion test/test_ecc_throws.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Test
using QuantumClifford
using QuantumClifford.ECC
using QuantumClifford.ECC: ReedMuller

@test_throws ArgumentError ReedMuller(-1, 3)
@test_throws ArgumentError ReedMuller(1, 0)
@test_throws ArgumentError ReedMuller(1, 0)

0 comments on commit cae94d8

Please sign in to comment.