Skip to content

Commit

Permalink
Refactor tests to use TestItems.jl (#142)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Stefan Krastanov <[email protected]>
  • Loading branch information
BKaperick and Krastanov authored Sep 27, 2024
1 parent 46cb2f3 commit eba99c0
Show file tree
Hide file tree
Showing 33 changed files with 159 additions and 171 deletions.
5 changes: 4 additions & 1 deletion src/ProtocolZoo/switches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ julia> let
function promponas_bruteforce_choice(M,N,backlog,eprobs) # TODO mark as public but unexported
@warn "The switch optimization routine is using a random placeholder optimization method due to issues with installing the BlossomV algorithm. Do not rely on this code to validate research results."
return randperm(N)[1:M]
# TODO-MATCHING due to the dependence on BlossomV.jl this has trouble installing. See https://github.com/JuliaGraphs/GraphsMatching.jl/issues/14
#= TODO-MATCHING due to the dependence on BlossomV.jl this has trouble installing. See https://github.com/JuliaGraphs/GraphsMatching.jl/issues/14
best_weight = 0.0
best_assignment = zeros(Int, M)
graphs = [complete_graph(i) for i in 1:M] # preallocating them to avoid expensive allocations in the inner loop
Expand All @@ -91,8 +91,10 @@ function promponas_bruteforce_choice(M,N,backlog,eprobs) # TODO mark as public b
end
end
return found ? best_assignment : nothing
=#
end

#= TODO-MATCHING due to the dependence on BlossomV.jl this has trouble installing. See https://github.com/JuliaGraphs/GraphsMatching.jl/issues/14
"""
Perform the match of clients in `entangled_nodes` based on matching weights from `backlog`.
`g` and `w` are just preallocated buffers.
Expand All @@ -111,6 +113,7 @@ function match_entangled_pattern(backlog, entangled_nodes, g, w)
mate = [(entangled_nodes[i],entangled_nodes[j]) for (i,j) in enumerate(match.mate) if i<j]
return (;weight, mate)
end
=#

"""Some of the external optimizers we use create a ton of junk console output. This function redirects stdout to hide the junk."""
function capture_stdout(f)
Expand Down
3 changes: 1 addition & 2 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
GraphsMatching = "c3af3a8c-b79e-4b01-bf44-c718d7e0e0d6"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Expand All @@ -28,13 +27,13 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
ResumableFunctions = "c5292f4c-5179-55e1-98c5-05642aab7184"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
SumTypes = "8e1ec7a9-0e02-4297-b0fe-6433085c89f2"
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
ThreadTools = "dbf13d8f-d36e-4350-8970-f3a99faba1a8"
WignerSymbols = "9f57e263-0b3d-5e2e-b1be-24f2bb48858b"
66 changes: 13 additions & 53 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,59 +1,19 @@
using SafeTestsets
using QuantumSavory

function doset(descr)
if length(ARGS) == 0
return true
end
for a in ARGS
if occursin(lowercase(a), lowercase(descr))
return true
end
using TestItemRunner

function testfilter(tags)
exclude = Symbol[]
# Only do the plotting tests if the ENV variable `QUANTUMSAVORY_PLOT_TEST` is set
if get(ENV,"QUANTUMSAVORY_PLOT_TEST","")!="true"
push!(exclude, :plotting_cairo)
push!(exclude, :plotting_gl)
push!(exclude, :doctests)
end
return false
end

macro doset(descr)
quote
if doset($descr)
@safetestset $descr begin include("test_"*$descr*".jl") end
end
if get(ENV,"JET_TEST","")!="true"
push!(exclude, :jet)
end
return all(!in(exclude), tags)
end

println("Starting tests with $(Threads.nthreads()) threads out of `Sys.CPU_THREADS = $(Sys.CPU_THREADS)`...")

@doset "quantumchannel"
@doset "register_interface"
@doset "project_traceout"
@doset "observable"
@doset "noninstant_and_backgrounds_qubit"
@doset "noninstant_and_backgrounds_qumode"
@doset "messagebuffer"
@doset "tags_and_queries"

@doset "protocolzoo_entanglement_tracker"
@doset "protocolzoo_entanglement_consumer"
@doset "protocolzoo_entanglement_tracker_grid"
@doset "protocolzoo_switch"
@doset "protocolzoo_throws"
@doset "protocolzoo_cutoffprot"

@doset "circuitzoo_api"
@doset "circuitzoo_ent_swap"
@doset "circuitzoo_purification"
@doset "circuitzoo_superdense"

@doset "stateszoo_api"

if get(ENV,"QUANTUMSAVORY_PLOT_TEST","")=="true"
using Pkg
Pkg.add("GLMakie")
end
@doset "examples"
get(ENV,"QUANTUMSAVORY_PLOT_TEST","")=="true" && @doset "plotting_cairo"
get(ENV,"QUANTUMSAVORY_PLOT_TEST","")=="true" && @doset "plotting_gl"
get(ENV,"QUANTUMSAVORY_PLOT_TEST","")=="true" && VERSION >= v"1.9" && @doset "doctests"

get(ENV,"JET_TEST","")=="true" && @doset "jet"
@doset "aqua"
@run_package_tests filter=ti->testfilter(ti.tags) verbose=true
22 changes: 22 additions & 0 deletions test/setup_circuitzoo_purification.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using QuantumSavory.CircuitZoo
using QuantumSavory.CircuitZoo: EntanglementSwap, Purify2to1, Purify3to1, Purify3to1Node, Purify2to1Node, PurifyStringent, StringentHead, StringentBody, PurifyExpedient, PurifyStringentNode, PurifyExpedient

const bell = StabilizerState("XX ZZ")
export bell;
# or equivalently `const bell = (Z₁⊗Z₁+Z₂⊗Z₂)/√2`,
# however converting to stabilizer state for Clifford simulations
# is not implemented (and can not be done efficiently).


# QOptics repr
const perfect_pair = (Z1Z1 + Z2Z2) / sqrt(2)
const perfect_pair_dm = SProjector(perfect_pair)
const mixed_dm = MixedState(perfect_pair_dm)
noisy_pair_func(F) = F*perfect_pair_dm + (1-F)*mixed_dm # TODO make a depolarization helper


# Qclifford repr
const stab_perfect_pair = StabilizerState("XX ZZ")
const stab_perfect_pair_dm = SProjector(stab_perfect_pair)
const stab_mixed_dm = MixedState(stab_perfect_pair_dm)
stab_noisy_pair_func(F) = F*stab_perfect_pair_dm + (1-F)*stab_mixed_dm
2 changes: 2 additions & 0 deletions test/setup_plotting.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using Pkg
Pkg.add("GLMakie")
6 changes: 3 additions & 3 deletions test/test_aqua.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testitem "Aqua" tags=[:aqua] begin
using Aqua
using QuantumSavory

if get(ENV,"JET_TEST","")=="true"
# JET generates new methods with ambiguities
Expand All @@ -14,5 +14,5 @@ Aqua.test_all(QuantumSavory,
)

@test length(Aqua.Piracy.hunt(QuantumSavory)) == 6

end
end
end
4 changes: 2 additions & 2 deletions test/test_circuitzoo_api.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using QuantumSavory
@testitem "Circuit Zoo API" tags=[:circuitzoo_api] begin
using QuantumSavory.CircuitZoo
using Test
using QuantumSavory.CircuitZoo: AbstractCircuit, inputqubits
using InteractiveUtils

Expand All @@ -13,3 +12,4 @@ for T in subtypes(AbstractCircuit)
@test m.isva || inputqubits(circ) == m.nargs-1
end
end
end
4 changes: 2 additions & 2 deletions test/test_circuitzoo_ent_swap.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using QuantumSavory
@testitem "Circuit Zoo Entanglement Swap" tags=[:circuitzoo_ent_swap] begin
using QuantumSavory.CircuitZoo
using Test
using QuantumSavory.CircuitZoo: EntanglementSwap, LocalEntanglementSwap

const perfect_pair_stab = StabilizerState("XX ZZ")
Expand Down Expand Up @@ -55,3 +54,4 @@ for pair in (perfect_pair, perfect_pair_stab), n in 3:10, rep in 1:10
@test observable((net[1][1], net[n][2]), ZZ) 1
@test observable((net[1][1], net[n][2]), XX) 1
end
end
81 changes: 44 additions & 37 deletions test/test_circuitzoo_purification.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
using QuantumSavory
@testitem "Circuit Zoo Purification - throws" tags=[:circuitzoo_purification] begin
using QuantumSavory.CircuitZoo
using Test
using QuantumSavory.CircuitZoo: EntanglementSwap, Purify2to1, Purify3to1, Purify3to1Node, Purify2to1Node, PurifyStringent, StringentHead, StringentBody, PurifyExpedient, PurifyStringentNode, PurifyExpedient


const bell = StabilizerState("XX ZZ")
# or equivalently `const bell = (Z₁⊗Z₁+Z₂⊗Z₂)/√2`,
# however converting to stabilizer state for Clifford simulations
# is not implemented (and can not be done efficiently).


# QOptics repr
const perfect_pair = (Z1Z1 + Z2Z2) / sqrt(2)
const perfect_pair_dm = SProjector(perfect_pair)
const mixed_dm = MixedState(perfect_pair_dm)
noisy_pair_func(F) = F*perfect_pair_dm + (1-F)*mixed_dm # TODO make a depolarization helper


# Qclifford repr
const stab_perfect_pair = StabilizerState("XX ZZ")
const stab_perfect_pair_dm = SProjector(stab_perfect_pair)
const stab_mixed_dm = MixedState(stab_perfect_pair_dm)
stab_noisy_pair_func(F) = F*stab_perfect_pair_dm + (1-F)*stab_mixed_dm

@test_throws ArgumentError Purify2to1(:lalala)
@test_throws ArgumentError Purify3to1(:lalala, :X)
@test_throws ArgumentError Purify2to1Node(:lalala)
Expand All @@ -36,9 +15,11 @@ for i in 1:30
end
@test_throws ArgumentError PurifyExpedient()(r[1], r[2], r[3:2:21]...)
@test_throws ArgumentError PurifyStringent()(r[1], r[2], r[3:2:21]...)
end

@testitem "Circuit Zoo Purification - 2to1" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

@testset "2to1" begin
for rep in [QuantumOpticsRepr, CliffordRepr]
for leaveout in [:X, :Y, :Z]
# test that pure state gets mapped to pure state
Expand All @@ -64,7 +45,9 @@ end
end
end

@testset "2to1 - Node" begin
@testitem "Circuit Zoo Purification - 2to1 - Node" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [QuantumOpticsRepr, CliffordRepr]
for leaveout in [:X, :Y, :Z]
# test that pure state gets mapped to pure state
Expand Down Expand Up @@ -95,7 +78,9 @@ end
end
end

@testset "3to1" begin
@testitem "Circuit Zoo Purification - 3to1" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [QuantumOpticsRepr, CliffordRepr]
for leaveout1 in [:X, :Y, :Z]
for leaveout2 in [:X, :Y, :Z]
Expand Down Expand Up @@ -134,7 +119,9 @@ end
end
end

@testset "3to1 - Fidelity - QuantumOpticsRepr" begin
@testitem "Circuit Zoo Purification - 3to1 -- Fidelity - QuantumOpticsRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [QuantumOpticsRepr]
for leaveout1 in [:X, :Y, :Z]
for leaveout2 in [:X, :Y, :Z]
Expand All @@ -154,7 +141,9 @@ end
end
end

@testset "3to1 - Fidelity - CliffordRepr" begin
@testitem "Circuit Zoo Purification - 3to1 -- Fidelity - CliffordRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [CliffordRepr]
for leaveout1 in [:X, :Y, :Z]
for leaveout2 in [:X, :Y, :Z]
Expand All @@ -174,7 +163,9 @@ end
end


@testset "3to1 - Node" begin
@testitem "Circuit Zoo Purification - 3to1 -- Node" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [QuantumOpticsRepr, CliffordRepr]
for leaveout1 in [:X, :Y, :Z]
for leaveout2 in [:X, :Y, :Z]
Expand Down Expand Up @@ -223,7 +214,9 @@ end
end
end

@testset "3to1 - Node - Fidelity - QuantumOpticsRepr" begin
@testitem "Circuit Zoo Purification - 3to1 -- Node - Fidelity - QuantumOpticsRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [QuantumOpticsRepr]
for leaveout1 in [:X, :Y, :Z]
for leaveout2 in [:X, :Y, :Z]
Expand All @@ -245,7 +238,9 @@ end
end
end

@testset "3to1 - Node - Fidelity - CliffordRepr" begin
@testitem "Circuit Zoo Purification - 3to1 -- Node - Fidelity - CliffordRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [CliffordRepr]
for leaveout1 in [:X, :Y, :Z]
for leaveout2 in [:X, :Y, :Z]
Expand All @@ -266,7 +261,9 @@ end
end
end

@testset "Stringent" begin
@testitem "Circuit Zoo Purification - Stringent" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [CliffordRepr, QuantumOpticsRepr]
r = Register(26, rep())
for i in 1:13
Expand All @@ -275,9 +272,11 @@ end
@test PurifyStringent()(r[1], r[2], r[3:2:25]..., r[4:2:26]...) == true
@test observable(r[1:2], projector(bell)) 1.0
end
end
end

@testitem "Circuit Zoo Purification - Stringent - Fidelity - QuantumOpticsRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

@testset "Stringent - Fidelity - QuantumOpticsRepr" begin
for rep in [QuantumOpticsRepr]
r = Register(26, rep())
rnd = rand() / 4 + 0.5
Expand All @@ -291,7 +290,9 @@ end
end
end

@testset "Stringent - Fidelity - CliffordRepr" begin
@testitem "Circuit Zoo Purification - Stringent - Fidelity - CliffordRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [CliffordRepr]
r = Register(26, rep())
noisy_pair = stab_noisy_pair_func(0)
Expand All @@ -304,7 +305,9 @@ end
end
end

@testset "Expedient" begin
@testitem "Circuit Zoo Purification - Expedient" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [CliffordRepr, QuantumOpticsRepr]
r = Register(22, rep())
for i in 1:11
Expand All @@ -315,7 +318,9 @@ end
end
end

@testset "Expedient - Fidelity - QuantumOpticsRepr" begin
@testitem "Circuit Zoo Purification - Expedient - Fidelity - QuantumOpticsRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [QuantumOpticsRepr]
r = Register(22, rep())
rnd = rand() / 4 + 0.5
Expand All @@ -329,7 +334,9 @@ end
end
end

@testset "Expedient - Fidelity - CliffordRepr" begin
@testitem "Circuit Zoo Purification - Expedient - Fidelity - CliffordRepr" tags=[:circuitzoo_purification] begin
include("setup_circuitzoo_purification.jl")

for rep in [CliffordRepr]
r = Register(22, rep())
noisy_pair = stab_noisy_pair_func(0)
Expand Down
4 changes: 2 additions & 2 deletions test/test_circuitzoo_superdense.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using QuantumSavory
@testitem "Circuit Zoo Super Dense" tags=[:circuitzoo_superdense] begin
using QuantumSavory.CircuitZoo: SDEncode, SDDecode
using Test

for i in 1:8
## Set up an entangled bell pair
Expand All @@ -22,3 +21,4 @@ for i in 1:8

@test message == rec
end
end
Loading

0 comments on commit eba99c0

Please sign in to comment.