diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 9d5b81d..e9d2e1f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -11,7 +11,6 @@ jobs: matrix: version: - '1' - - 'nightly' os: - ubuntu-latest - macOS-latest diff --git a/test/to_eincode.jl b/test/Application/to_eincode.jl similarity index 52% rename from test/to_eincode.jl rename to test/Application/to_eincode.jl index 6a6f2ed..25e54a4 100644 --- a/test/to_eincode.jl +++ b/test/Application/to_eincode.jl @@ -1,6 +1,6 @@ using Test, ZXCalculus, LinearAlgebra, ZXCalculus.Utils, ZXCalculus.ZXW using ZXCalculus.Application: - z_tensor, x_tensor, w_tensor, h_tensor, d_tensor + z_tensor, x_tensor, w_tensor, h_tensor, d_tensor using ZXCalculus.Utils: Parameter using ZXCalculus.ZXW: push_gate! @@ -20,22 +20,18 @@ using ZXCalculus.ZXW: push_gate! end @testset "Convert to Einsum" begin - - zxwd = ZXWDiagram(2) push_gate!(zxwd, Val(:H), 1) push_gate!(zxwd, Val(:H), 2) push_gate!(zxwd, Val(:CZ), 1, 2) - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) - push_gate!(zxwd, Val(:X), 2, :b; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) + push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false) zxwd_sub = substitute_variables!(copy(zxwd), Dict(:a => 0.4, :b => 0.3)) zxwd_mtx = Matrix(zxwd_sub) - yao_mtx = ComplexF64[ - 0.6211468747399733+0.23776412907378838im 0.033361622447500294+0.23776412907378833im 0.1727457514062631+0.16674436811368532im -0.1727457514062631+0.642272626261262im - 0.1727457514062631+0.16674436811368532im 0.1727457514062631-0.642272626261262im 0.6211468747399733+0.23776412907378838im -0.033361622447500294-0.23776412907378833im - 0.033361622447500294+0.23776412907378833im 0.6211468747399733+0.23776412907378838im 0.1727457514062631-0.642272626261262im -0.1727457514062631-0.16674436811368532im - 0.1727457514062631-0.642272626261262im 0.1727457514062631+0.16674436811368532im 0.033361622447500294+0.23776412907378833im -0.6211468747399733-0.23776412907378838im - ] + yao_mtx = ComplexF64[0.6211468747399733+0.23776412907378838im 0.033361622447500294+0.23776412907378833im 0.1727457514062631+0.16674436811368532im -0.1727457514062631+0.642272626261262im + 0.1727457514062631+0.16674436811368532im 0.1727457514062631-0.642272626261262im 0.6211468747399733+0.23776412907378838im -0.033361622447500294-0.23776412907378833im + 0.033361622447500294+0.23776412907378833im 0.6211468747399733+0.23776412907378838im 0.1727457514062631-0.642272626261262im -0.1727457514062631-0.16674436811368532im + 0.1727457514062631-0.642272626261262im 0.1727457514062631+0.16674436811368532im 0.033361622447500294+0.23776412907378833im -0.6211468747399733-0.23776412907378838im] @test isapprox(zxwd_mtx' * yao_mtx, Matrix{ComplexF64}((1.0 + 0.0 * im) * I, 4, 4)) end diff --git a/test/planar_multigraph.jl b/test/PMG/planar_multigraph.jl similarity index 91% rename from test/planar_multigraph.jl rename to test/PMG/planar_multigraph.jl index 3ae5fd2..dba10d0 100644 --- a/test/planar_multigraph.jl +++ b/test/PMG/planar_multigraph.jl @@ -1,20 +1,19 @@ using Test, ZXCalculus, ZXCalculus.PMG using ZXCalculus.PMG: - create_vertex!, - create_edge!, - split_vertex!, - split_facet!, - join_facet!, - join_vertex!, - split_edge!, - make_hole!, - add_facet_to_boarder!, - add_vertex_and_facet_to_boarder!, - erase_facet!, - add_multiedge! + create_vertex!, + create_edge!, + split_vertex!, + split_facet!, + join_facet!, + join_vertex!, + split_edge!, + make_hole!, + add_facet_to_boarder!, + add_vertex_and_facet_to_boarder!, + erase_facet!, + add_multiedge! @testset "Half edge constructor" begin - he1 = HalfEdge(1, 2) ophe1 = HalfEdge(2, 1) @@ -34,7 +33,7 @@ end 2, 2, 0, - [0], + [0] ) pmg2 = copy(pmg1) @@ -60,7 +59,7 @@ end 9 => HalfEdge(5, 6), 10 => HalfEdge(6, 5), 11 => HalfEdge(6, 1), - 12 => HalfEdge(1, 6), + 12 => HalfEdge(1, 6) ), # f2he Dict(1 => 1, 0 => 2), @@ -77,7 +76,7 @@ end 9 => 1, 10 => 0, 11 => 1, - 12 => 0, + 12 => 0 ), # next Dict( @@ -92,7 +91,7 @@ end 6 => 8, 8 => 10, 10 => 12, - 12 => 2, + 12 => 2 ), # twin Dict( @@ -107,12 +106,12 @@ end 9 => 10, 10 => 9, 11 => 12, - 12 => 11, + 12 => 11 ), 6, # v_max 12, # he_max 1, # f_max - [0], + [0] ) pmg2 = PlanarMultigraph( @@ -133,7 +132,7 @@ end 11 => HalfEdge(6, 1), 12 => HalfEdge(1, 6), 13 => HalfEdge(1, 4), - 14 => HalfEdge(4, 1), + 14 => HalfEdge(4, 1) ), # f2he Dict(2 => 14, 1 => 13, 0 => 2), @@ -152,7 +151,7 @@ end 11 => 1, 12 => 0, 13 => 1, - 14 => 2, + 14 => 2 ), #next Dict( @@ -169,7 +168,7 @@ end 6 => 8, 8 => 10, 10 => 12, - 12 => 2, + 12 => 2 ), # twin Dict( @@ -186,12 +185,12 @@ end 11 => 12, 12 => 11, 13 => 14, - 14 => 13, + 14 => 13 ), 6, # v_max 14, # he_max 2, # f_max - [0], + [0] ) pmg3 = copy(pmg1) @test split_facet!(pmg3, 11, 5) == 13 @@ -201,7 +200,6 @@ end @test pmg4 == pmg1 end - @testset "Split Vertex fail" begin pmg1 = PlanarMultigraph( Dict(1 => 1, 2 => 2), @@ -209,7 +207,7 @@ end 1 => HalfEdge(1, 2), 2 => HalfEdge(2, 1), 3 => HalfEdge(1, 2), - 4 => HalfEdge(2, 1), + 4 => HalfEdge(2, 1) ), Dict(1 => 1, 0 => 2), Dict(1 => 1, 4 => 1, 3 => 0, 2 => 0), @@ -218,12 +216,12 @@ end 2, 4, 1, - [0], + [0] ) @test_throws "Should use #TODO to add multiedge and split facet!" split_facet!( pmg1, 1, - 4, + 4 ) end @@ -238,7 +236,7 @@ end 5 => HalfEdge(2, 4), 6 => HalfEdge(4, 2), 7 => HalfEdge(2, 5), - 8 => HalfEdge(5, 2), + 8 => HalfEdge(5, 2) ), Dict(0 => 1), Dict(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0), @@ -247,7 +245,7 @@ end 5, 8, 0, - [0], + [0] ) pmg2 = PlanarMultigraph( @@ -262,7 +260,7 @@ end 7 => HalfEdge(6, 5), 8 => HalfEdge(5, 6), 9 => HalfEdge(6, 2), - 10 => HalfEdge(2, 6), + 10 => HalfEdge(2, 6) ), Dict(0 => 1), Dict( @@ -275,7 +273,7 @@ end 7 => 0, 8 => 0, 9 => 0, - 10 => 0, + 10 => 0 ), Dict( 1 => 9, @@ -287,7 +285,7 @@ end 10 => 7, 7 => 8, 8 => 2, - 2 => 1, + 2 => 1 ), Dict( 1 => 2, @@ -299,12 +297,12 @@ end 7 => 8, 8 => 7, 9 => 10, - 10 => 9, + 10 => 9 ), 6, 10, 0, - [0], + [0] ) pmg3 = copy(pmg1) pmg4 = copy(pmg2) @@ -321,7 +319,7 @@ end 1 => HalfEdge(1, 2), 2 => HalfEdge(2, 1), 3 => HalfEdge(2, 3), - 4 => HalfEdge(3, 2), + 4 => HalfEdge(3, 2) ), Dict(0 => 1), Dict(1 => 0, 2 => 0, 3 => 0, 4 => 0), @@ -330,7 +328,7 @@ end 3, 4, 0, - [0], + [0] ) pmg2 = PlanarMultigraph( Dict(1 => 1, 2 => 3, 3 => 4, 4 => 5), @@ -340,7 +338,7 @@ end 3 => HalfEdge(2, 3), 4 => HalfEdge(3, 2), 5 => HalfEdge(4, 2), - 6 => HalfEdge(2, 4), + 6 => HalfEdge(2, 4) ), Dict(0 => 1), Dict(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0), @@ -349,7 +347,7 @@ end 4, 6, 0, - [0], + [0] ) @test split_edge!(pmg1, 3) == 6 @@ -368,7 +366,7 @@ end 5 => HalfEdge(4, 3), 6 => HalfEdge(3, 4), 7 => HalfEdge(1, 3), - 8 => HalfEdge(3, 1), + 8 => HalfEdge(3, 1) ), Dict(0 => 1, 1 => 2), Dict(1 => 0, 2 => 1, 3 => 0, 4 => 1, 5 => 0, 6 => 1, 7 => 1, 8 => 0), @@ -377,7 +375,7 @@ end 4, 8, 1, - [0], + [0] ) pmg2 = PlanarMultigraph( @@ -392,7 +390,7 @@ end 7 => HalfEdge(1, 3), 8 => HalfEdge(3, 1), 9 => HalfEdge(5, 2), - 10 => HalfEdge(2, 5), + 10 => HalfEdge(2, 5) ), Dict(0 => 1, 1 => 2), Dict( @@ -405,7 +403,7 @@ end 7 => 1, 8 => 0, 9 => 0, - 10 => 1, + 10 => 1 ), Dict( 1 => 9, @@ -417,7 +415,7 @@ end 7 => 6, 6 => 4, 4 => 10, - 10 => 2, + 10 => 2 ), Dict( 1 => 2, @@ -429,12 +427,12 @@ end 7 => 8, 8 => 7, 9 => 10, - 10 => 9, + 10 => 9 ), 5, 10, 1, - [0], + [0] ) pmg2f1 = copy(pmg1) @test split_vertex!(pmg2f1, 4, 1) == 9 @@ -450,7 +448,7 @@ end 3 => HalfEdge(2, 3), 4 => HalfEdge(3, 2), 5 => HalfEdge(3, 1), - 6 => HalfEdge(1, 3), + 6 => HalfEdge(1, 3) ), Dict(0 => 2, 1 => 1), Dict(1 => 1, 2 => 0, 3 => 1, 4 => 0, 5 => 1, 6 => 0), @@ -459,10 +457,9 @@ end 3, 6, 1, - [0], + [0] ) - pmg2 = PlanarMultigraph( Dict(1 => 1, 2 => 3, 3 => 5), Dict( @@ -471,7 +468,7 @@ end 3 => HalfEdge(2, 3), 4 => HalfEdge(3, 2), 5 => HalfEdge(3, 1), - 6 => HalfEdge(1, 3), + 6 => HalfEdge(1, 3) ), Dict(0 => 2, 1 => 1), Dict(1 => 1, 2 => 0, 3 => 1, 4 => 0, 5 => 1, 6 => 0), @@ -480,7 +477,7 @@ end 3, 6, 1, - [0, 1], + [0, 1] ) @test_throws "Can't make hole for boundary halfedge" make_hole!(pmg1, 2) @test make_hole!(pmg1, 1) == 1 @@ -496,7 +493,7 @@ end 3 => HalfEdge(2, 3), 4 => HalfEdge(3, 2), 5 => HalfEdge(3, 4), - 6 => HalfEdge(4, 3), + 6 => HalfEdge(4, 3) ), Dict(0 => 1), Dict(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0), @@ -505,7 +502,7 @@ end 4, 6, 0, - [0], + [0] ) pmg2 = PlanarMultigraph( @@ -518,7 +515,7 @@ end 5 => HalfEdge(3, 4), 6 => HalfEdge(4, 3), 7 => HalfEdge(2, 4), - 8 => HalfEdge(4, 2), + 8 => HalfEdge(4, 2) ), Dict(0 => 1, 1 => 8), Dict(1 => 0, 2 => 0, 3 => 1, 4 => 0, 5 => 1, 6 => 0, 7 => 0, 8 => 1), @@ -527,7 +524,7 @@ end 4, 8, 1, - [0], + [0] ) @test add_facet_to_boarder!(pmg1, 1, 5) == 8 @@ -543,7 +540,7 @@ end 3 => HalfEdge(2, 3), 4 => HalfEdge(3, 2), 5 => HalfEdge(3, 4), - 6 => HalfEdge(4, 3), + 6 => HalfEdge(4, 3) ), Dict(0 => 1), Dict(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0), @@ -552,10 +549,9 @@ end 4, 6, 0, - [0], + [0] ) - pmg2 = PlanarMultigraph( Dict(1 => 1, 2 => 3, 3 => 5, 4 => 6, 5 => 10), Dict( @@ -568,7 +564,7 @@ end 7 => HalfEdge(2, 5), 8 => HalfEdge(5, 2), 9 => HalfEdge(3, 5), - 10 => HalfEdge(5, 3), + 10 => HalfEdge(5, 3) ), Dict(0 => 1, 1 => 3), Dict( @@ -581,7 +577,7 @@ end 7 => 0, 8 => 1, 9 => 1, - 10 => 0, + 10 => 0 ), Dict( 1 => 7, @@ -593,7 +589,7 @@ end 2 => 1, 8 => 3, 3 => 9, - 9 => 8, + 9 => 8 ), Dict( 1 => 2, @@ -605,12 +601,12 @@ end 7 => 8, 8 => 7, 9 => 10, - 10 => 9, + 10 => 9 ), 5, 10, 1, - [0], + [0] ) @test add_vertex_and_facet_to_boarder!(pmg1, 1, 3) == 9 @test pmg1 == pmg2 @@ -631,7 +627,7 @@ end 9 => HalfEdge(5, 1), 10 => HalfEdge(1, 5), 11 => HalfEdge(4, 2), - 12 => HalfEdge(2, 4), + 12 => HalfEdge(2, 4) ), Dict(0 => 1, 1 => 2, 2 => 6), Dict( @@ -646,7 +642,7 @@ end 9 => 0, 10 => 1, 11 => 1, - 12 => 2, + 12 => 2 ), Dict( 1 => 3, @@ -660,7 +656,7 @@ end 11 => 2, 4 => 12, 12 => 6, - 6 => 4, + 6 => 4 ), Dict( 1 => 2, @@ -674,12 +670,12 @@ end 9 => 10, 10 => 9, 11 => 12, - 12 => 11, + 12 => 11 ), 5, 12, 2, - [0], + [0] ) pmg2 = PlanarMultigraph( @@ -692,7 +688,7 @@ end 9 => HalfEdge(5, 1), 10 => HalfEdge(1, 5), 11 => HalfEdge(4, 2), - 12 => HalfEdge(2, 4), + 12 => HalfEdge(2, 4) ), Dict(0 => 1, 1 => 2), Dict(1 => 0, 2 => 1, 7 => 0, 8 => 1, 9 => 0, 10 => 1, 11 => 1, 12 => 0), @@ -701,7 +697,7 @@ end 5, 12, 2, - [0], + [0] ) @test erase_facet!(pmg1, 6) == 6 @@ -718,7 +714,7 @@ end 5 => HalfEdge(3, 4), 6 => HalfEdge(4, 3), 7 => HalfEdge(4, 2), - 8 => HalfEdge(2, 4), + 8 => HalfEdge(2, 4) ), Dict(0 => 1, 1 => 4), Dict(1 => 0, 2 => 0, 3 => 0, 4 => 1, 5 => 1, 6 => 0, 7 => 1, 8 => 0), @@ -727,7 +723,7 @@ end 4, 8, 1, - [0], + [0] ) pmg4 = PlanarMultigraph( @@ -740,7 +736,7 @@ end 2, 2, 0, - [0], + [0] ) @test erase_facet!(pmg3, 7) == 7 @test pmg3 == pmg4 @@ -757,7 +753,7 @@ end 2, 2, 0, - [0], + [0] ) pmg2 = PlanarMultigraph( @@ -768,7 +764,7 @@ end 3 => HalfEdge(2, 1), 4 => HalfEdge(1, 2), 5 => HalfEdge(2, 1), - 6 => HalfEdge(1, 2), + 6 => HalfEdge(1, 2) ), Dict(0 => 1, 1 => 2, 2 => 3), Dict(1 => 0, 2 => 1, 4 => 1, 3 => 2, 6 => 2, 5 => 0), @@ -777,7 +773,7 @@ end 2, 2, 0, - [0], + [0] ) @test add_multiedge!(pmg1, 2, 2) == 2 diff --git a/test/parameter.jl b/test/Utils/parameter.jl similarity index 99% rename from test/parameter.jl rename to test/Utils/parameter.jl index 1f87d7b..b8f91d2 100644 --- a/test/parameter.jl +++ b/test/Utils/parameter.jl @@ -52,7 +52,6 @@ using ZXCalculus.Utils: Parameter end @testset "io" begin - io = IOBuffer() p1 = Parameter(Val(:PiUnit), 1.0) show(io, p1) @@ -76,7 +75,6 @@ end f1 = Parameter(Val(:Factor), π) f2 = Parameter(Val(:Factor), 1) - @test p1 == p2 @test p3 == p3 @test p3 != p4 @@ -97,9 +95,6 @@ end @test !contains(p3, :b) end - - - @testset "addition" begin p1 = Parameter(Val(:PiUnit), 1.0) p2 = Parameter(Val(:PiUnit), 2) @@ -121,7 +116,6 @@ end @test p2 + f1 == Parameter(Val(:Factor), exp(im * 2 * π) * 1) @test f1 + p2 == Parameter(Val(:Factor), exp(im * 2 * π) * 1) - end @testset "subtraction" begin @@ -132,7 +126,6 @@ end f1 = Parameter(Val(:Factor), 1) f2 = Parameter(Val(:Factor), 2.5) - @test p1 - p2 == Parameter(Val(:PiUnit), -1.0) @test p1 - 2 == Parameter(Val(:PiUnit), -1.0) @test 2 - p1 == Parameter(Val(:PiUnit), 1.0) @@ -156,5 +149,4 @@ end f1 = Parameter(Val(:Factor), 13) @test Base.rem(f1, 12) == Parameter(Val(:Factor), 1) - end diff --git a/test/phase.jl b/test/Utils/phase.jl similarity index 100% rename from test/phase.jl rename to test/Utils/phase.jl diff --git a/test/scalar.jl b/test/Utils/scalar.jl similarity index 100% rename from test/scalar.jl rename to test/Utils/scalar.jl diff --git a/test/zw_diagram.jl b/test/ZW/zw_diagram.jl similarity index 94% rename from test/zw_diagram.jl rename to test/ZW/zw_diagram.jl index 36b049c..01f25de 100644 --- a/test/zw_diagram.jl +++ b/test/ZW/zw_diagram.jl @@ -12,10 +12,9 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 2, 2, 0, - [0], + [0] ) - @test zx1.st == Dict([1 => ZW.Input(1), 2 => ZW.Output(1)]) @test zx1.inputs == [1] @test zx1.outputs == [2] @@ -33,7 +32,7 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 5 => HalfEdge(3, 1), 6 => HalfEdge(1, 3), 7 => HalfEdge(4, 2), - 8 => HalfEdge(2, 4), + 8 => HalfEdge(2, 4) ]), Dict([0 => 2, 1 => 1]), Dict([1 => 1, 8 => 1, 4 => 1, 5 => 1, 2 => 0, 3 => 0, 6 => 0, 7 => 0]), @@ -42,7 +41,7 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 4, 8, 1, - [0], + [0] ) @test zx2.st == Dict([1 => ZW.Input(1), 2 => ZW.Output(1), 3 => ZW.Input(2), 4 => ZW.Output(2)]) @@ -70,7 +69,7 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 11 => HalfEdge(4, 2), 12 => HalfEdge(2, 4), 13 => HalfEdge(6, 4), - 14 => HalfEdge(4, 6), + 14 => HalfEdge(4, 6) ]), Dict([0 => 2, 1 => 1, 2 => 3]), Dict([ @@ -87,7 +86,7 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 8 => 0, 11 => 0, 10 => 0, - 13 => 0, + 13 => 0 ]), Dict([ 1 => 12, @@ -103,7 +102,7 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 10 => 5, 5 => 13, 13 => 11, - 11 => 2, + 11 => 2 ]), Dict([ 1 => 2, @@ -119,12 +118,12 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 11 => 12, 12 => 11, 13 => 14, - 14 => 13, + 14 => 13 ]), 6, 14, 2, - [0], + [0] ) @test zx3.st == Dict([ @@ -133,7 +132,7 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.PMG, ZXCalculus.Utils 3 => ZW.Input(2), 4 => ZW.Output(2), 5 => ZW.Input(3), - 6 => ZW.Output(3), + 6 => ZW.Output(3) ]) @test zx3.inputs == [1, 3, 5] @test zx3.outputs == [2, 4, 6] diff --git a/test/zw_utils.jl b/test/ZW/zw_utils.jl similarity index 85% rename from test/zw_utils.jl rename to test/ZW/zw_utils.jl index f24537d..17f0e73 100644 --- a/test/zw_utils.jl +++ b/test/ZW/zw_utils.jl @@ -1,33 +1,33 @@ using Test, ZXCalculus, ZXCalculus.ZW, ZXCalculus.Utils, ZXCalculus.PMG using ZXCalculus.ZW: - ZWSpiderType, - set_phase!, - parameter, - nin, - nout, - nqubits, - nv, - ne, - degree, - indegree, - outdegree, - outneighbors, - inneighbors, - neighbors, - spiders, - scalar, - get_inputs, - get_outputs, - get_input_idx, - get_output_idx, - add_power!, - add_global_phase!, - neighbors, - join_spider!, - add_edge!, - add_spider!, - rem_edge!, - rem_spider! + ZWSpiderType, + set_phase!, + parameter, + nin, + nout, + nqubits, + nv, + ne, + degree, + indegree, + outdegree, + outneighbors, + inneighbors, + neighbors, + spiders, + scalar, + get_inputs, + get_outputs, + get_input_idx, + get_output_idx, + add_power!, + add_global_phase!, + neighbors, + join_spider!, + add_edge!, + add_spider!, + rem_edge!, + rem_spider! using ZXCalculus.PMG: trace_vertex using ZXCalculus.Utils: Parameter @@ -62,11 +62,9 @@ using ZXCalculus.Utils: Parameter add_global_phase!(zw, 1 // 2) sc = scalar(zw) @test sc == Scalar{Rational}(2, 1 // 2) - end @testset "Add and Rem Spiders" begin - zw = ZWDiagram(3) pmg2 = PlanarMultigraph( @@ -87,7 +85,7 @@ end 13 => HalfEdge(6, 4), 14 => HalfEdge(4, 6), 15 => HalfEdge(7, 2), - 16 => HalfEdge(2, 7), + 16 => HalfEdge(2, 7) ), Dict(0 => 2, 1 => 1, 2 => 3), Dict( @@ -106,7 +104,7 @@ end 5 => 0, 8 => 0, 13 => 0, - 10 => 0, + 10 => 0 ), Dict( 1 => 15, @@ -124,7 +122,7 @@ end 5 => 13, 13 => 11, 11 => 16, - 16 => 2, + 16 => 2 ), Dict( 1 => 2, @@ -142,12 +140,12 @@ end 13 => 14, 14 => 13, 15 => 16, - 16 => 15, + 16 => 15 ), 7, 16, 2, - [0], + [0] ) ZW.insert_spider!(zw, 12, ZW.binZ(Parameter(Val(:Factor), 2.0))) @@ -177,7 +175,7 @@ end 15 => HalfEdge(7, 2), 16 => HalfEdge(2, 7), 17 => HalfEdge(7, 3), - 18 => HalfEdge(3, 7), + 18 => HalfEdge(3, 7) ), Dict(0 => 2, 1 => 17, 2 => 3, 3 => 18), Dict( @@ -198,7 +196,7 @@ end 13 => 0, 10 => 0, 17 => 1, - 18 => 3, + 18 => 3 ), Dict( 1 => 17, @@ -218,7 +216,7 @@ end 3 => 14, 14 => 6, 6 => 9, - 9 => 3, + 9 => 3 ), Dict( 1 => 2, @@ -238,12 +236,12 @@ end 15 => 16, 16 => 15, 17 => 18, - 18 => 17, + 18 => 17 ), 7, 18, 3, - [0], + [0] ) @test pmg3 == zw.pmg @@ -255,14 +253,14 @@ end add_spider!(zw2, ZW.fSWAP, [1, 7, 4]) st2 = Dict( - 5 => ZWSpiderType.Input(qubit = 3), - 4 => ZWSpiderType.Output(qubit = 2), - 6 => ZWSpiderType.Output(qubit = 3), - 7 => ZWSpiderType.binZ(r = Parameter.Factor(f = 2.0, f_type = Float64)), - 2 => ZWSpiderType.Output(qubit = 1), - 3 => ZWSpiderType.Input(qubit = 2), + 5 => ZWSpiderType.Input(qubit=3), + 4 => ZWSpiderType.Output(qubit=2), + 6 => ZWSpiderType.Output(qubit=3), + 7 => ZWSpiderType.binZ(r=Parameter.Factor(f=2.0, f_type=Float64)), + 2 => ZWSpiderType.Output(qubit=1), + 3 => ZWSpiderType.Input(qubit=2), 8 => ZWSpiderType.fSWAP, - 1 => ZWSpiderType.Input(qubit = 1), + 1 => ZWSpiderType.Input(qubit=1) ) pmg4 = PlanarMultigraph( Dict(1 => 1, 2 => 16, 3 => 7, 4 => 4, 5 => 5, 6 => 6, 7 => 2, 8 => 18), @@ -290,7 +288,7 @@ end 21 => HalfEdge(8, 1), 22 => HalfEdge(1, 8), 23 => HalfEdge(8, 7), - 24 => HalfEdge(7, 8), + 24 => HalfEdge(7, 8) ), Dict(1 => 15, 2 => 3, 3 => 4, 4 => 7, 5 => 1, 0 => 2), Dict( @@ -317,7 +315,7 @@ end 5 => 0, 13 => 0, 11 => 0, - 16 => 0, + 16 => 0 ), Dict( 1 => 24, @@ -343,7 +341,7 @@ end 3 => 14, 14 => 6, 6 => 9, - 9 => 3, + 9 => 3 ), Dict( 1 => 2, @@ -369,12 +367,12 @@ end 21 => 22, 22 => 21, 23 => 24, - 24 => 23, + 24 => 23 ), 8, 24, 5, - [0, 1], + [0, 1] ) @test zw2.pmg == pmg4 @test zw2.st == st2 @@ -399,7 +397,7 @@ end 13 => HalfEdge(6, 4), 14 => HalfEdge(4, 6), 15 => HalfEdge(7, 2), - 16 => HalfEdge(2, 7), + 16 => HalfEdge(2, 7) ), Dict(0 => 2, 4 => 1, 2 => 3), Dict( @@ -418,7 +416,7 @@ end 5 => 0, 8 => 0, 13 => 0, - 10 => 0, + 10 => 0 ), Dict( 1 => 15, @@ -436,7 +434,7 @@ end 5 => 13, 13 => 11, 11 => 16, - 16 => 2, + 16 => 2 ), Dict( 1 => 2, @@ -454,12 +452,12 @@ end 13 => 14, 14 => 13, 15 => 16, - 16 => 15, + 16 => 15 ), 8, 24, 5, - [0, 4], + [0, 4] ) @test zw2.pmg == pmg5 end diff --git a/test/abstract_zx_diagram.jl b/test/ZX/abstract_zx_diagram.jl similarity index 95% rename from test/abstract_zx_diagram.jl rename to test/ZX/abstract_zx_diagram.jl index 232f2a4..394713a 100644 --- a/test/abstract_zx_diagram.jl +++ b/test/ZX/abstract_zx_diagram.jl @@ -2,10 +2,9 @@ using Test, Graphs, ZXCalculus, ZXCalculus.ZX using ZXCalculus.Utils: Phase using ZXCalculus: ZX +struct TestZXDiagram{T, P} <: AbstractZXDiagram{T, P} end -struct TestZXDiagram{T,P} <: AbstractZXDiagram{T,P} end - -test_zxd = TestZXDiagram{Int,Phase}(); +test_zxd = TestZXDiagram{Int, Phase}(); @test_throws MethodError Graphs.nv(test_zxd) @test_throws MethodError Graphs.ne(test_zxd) diff --git a/test/ancilla_extraction.jl b/test/ZX/ancilla_extraction.jl similarity index 99% rename from test/ancilla_extraction.jl rename to test/ZX/ancilla_extraction.jl index 1cc9531..77f31fb 100644 --- a/test/ancilla_extraction.jl +++ b/test/ZX/ancilla_extraction.jl @@ -9,7 +9,7 @@ function gen_phase_gadget() push_gate!(zxd, Val(:Z), 1, 1//4) push_gate!(zxd, Val(:CNOT), 1, 2) push_gate!(zxd, Val(:Z), 1, 1//2) - + return zxd end diff --git a/test/challenge.jl b/test/ZX/challenge.jl similarity index 98% rename from test/challenge.jl rename to test/ZX/challenge.jl index d63e69d..04ab034 100644 --- a/test/challenge.jl +++ b/test/ZX/challenge.jl @@ -56,7 +56,7 @@ st = Dict( 48+1 => SpiderType.Z, 49+1 => SpiderType.Z, 50+1 => SpiderType.Z, - 51+1 => SpiderType.Z, + 51+1 => SpiderType.Z ) ps = Dict( @@ -192,7 +192,7 @@ es = Dict( (40+1, 41+1) => EdgeType.HAD, (42+1, 43+1) => EdgeType.HAD, (44+1, 45+1) => EdgeType.HAD, - (46+1, 47+1) => EdgeType.HAD, + (46+1, 47+1) => EdgeType.HAD ) zxg = ZXGraph(ZXDiagram(0)) @@ -203,7 +203,7 @@ end for (e, _) in es Graphs.add_edge!(zxg, e[1], e[2]) end -for i = 1:5 +for i in 1:5 push!(zxg.inputs, i) push!(zxg.outputs, i+23) end diff --git a/test/circuit_extraction.jl b/test/ZX/circuit_extraction.jl similarity index 100% rename from test/circuit_extraction.jl rename to test/ZX/circuit_extraction.jl diff --git a/test/equality.jl b/test/ZX/equality.jl similarity index 100% rename from test/equality.jl rename to test/ZX/equality.jl diff --git a/test/ir.jl b/test/ZX/ir.jl similarity index 89% rename from test/ir.jl rename to test/ZX/ir.jl index f84bbdc..d468fe4 100644 --- a/test/ir.jl +++ b/test/ZX/ir.jl @@ -39,7 +39,6 @@ push_gate!(chain, Val(:Rx), 3, Phase(1 // 4)) push_gate!(chain, Val(:Rx), 2, Phase(1 // 4)) push_gate!(chain, Val(:S), 3) - @testset "ir.jl" begin # Shadow operation in ir.jl testset, so that they do not overrride SpiderTypes X = YaoHIR.IntrinsicOperation.X @@ -49,35 +48,29 @@ push_gate!(chain, Val(:S), 3) S = YaoHIR.IntrinsicOperation.S SGate = YaoHIR.IntrinsicOperation.SGate - - ir = IRCode() bir = BlockIR(ir, 4, chain) zxd = ZXDiagram(bir) zxwd = ZXWDiagram(bir) - @testset "convert SpiderType to Val" begin - @test ZX.stype_to_val(SpiderType.Z) == Val{:Z}() - @test ZX.stype_to_val(SpiderType.X) == Val{:X}() - @test ZX.stype_to_val(SpiderType.H) == Val{:H}() - @test_throws ArgumentError ZX.stype_to_val("anything else") end + @test ZX.stype_to_val(SpiderType.Z) == Val{:Z}() + @test ZX.stype_to_val(SpiderType.X) == Val{:X}() + @test ZX.stype_to_val(SpiderType.H) == Val{:H}() + @test_throws ArgumentError ZX.stype_to_val("anything else") + end @testset "convert BlockIR into ZXWDiagram" begin - @test !isnothing(zxwd) + @test !isnothing(zxwd) end - @testset "create Matrix from ZXDiagram" begin - matrix_from_zxd = - Matrix(ZXWDiagram(BlockIR(IRCode(), 4, circuit_extraction(full_reduction(zxd))))) - @test !isnothing(matrix_from_zxd) + matrix_from_zxd = Matrix(ZXWDiagram(BlockIR(IRCode(), 4, circuit_extraction(full_reduction(zxd))))) + @test !isnothing(matrix_from_zxd) end - - @testset "BlockIR to Matrix" begin - @test !isnothing(Matrix(zxwd)) + @test !isnothing(Matrix(zxwd)) end @test !isnothing(plot(zxd)) @@ -116,7 +109,6 @@ push_gate!(chain, Val(:S), 3) end @testset "generate_layout!" begin - circ = Chain( Gate(H, Locations(2)), Gate(T, Locations(4)), @@ -217,7 +209,7 @@ push_gate!(chain, Val(:S), 3) Gate(AdjointOperation{SGate}(S), Locations(3)), Gate(H, Locations(5)), Gate(S, Locations(4)), - Gate(Z, Locations(4)), + Gate(Z, Locations(4)) ) ir = IRCode() bir = BlockIR(ir, 5, circ) @@ -229,7 +221,6 @@ push_gate!(chain, Val(:S), 3) @test !isnothing(ZX.generate_layout!(zxg)) end - @testset "generate_layout with chain" begin bir = BlockIR(ir, 5, chain) zxd = ZXDiagram(bir) @@ -238,14 +229,13 @@ push_gate!(chain, Val(:S), 3) ZX.generate_layout!(zxg) @test !isnothing(plot(zxg)) @test !isnothing(ZX.generate_layout!(zxg)) - end - function random_circuit(nbits, ngates; T = 0.1, CZ = 0.0, CNOT = 0.1) + function random_circuit(nbits, ngates; T=0.1, CZ=0.0, CNOT=0.1) ir = IRCode() CLIFF = 1 - T - CZ - CNOT circ = Chain() - for _ = 1:ngates + for _ in 1:ngates x = rand() nbits == 1 && (x = x * (CLIFF + T)) if x <= CLIFF @@ -273,10 +263,10 @@ push_gate!(chain, Val(:S), 3) return BlockIR(ir, nbits, circ) end - function random_identity(nbits, ngates; T = 0.1, CZ = 0.0, CNOT = 0.1) - bir = random_circuit(nbits, ngates; T = T, CZ = CZ, CNOT = CNOT) + function random_identity(nbits, ngates; T=0.1, CZ=0.0, CNOT=0.1) + bir = random_circuit(nbits, ngates; T=T, CZ=CZ, CNOT=CNOT) c = bir.circuit.args - for i = length(c):-1:1 + for i in length(c):-1:1 if c[i] isa Gate g = c[i].operation if (g in (YaoHIR.IntrinsicOperation.S, YaoHIR.IntrinsicOperation.T)) || @@ -301,16 +291,16 @@ push_gate!(chain, Val(:S), 3) @test !isnothing(plot(zxg |> clifford_simplification |> full_reduction)) end - @testset "gates_to_circ with addition Ry" begin - n_qubits = 4 - # TODO add tests for Ry gate - chain_a = Chain() - push_gate!(chain_a, Val(:Ry), n_qubits, Phase(1 // 1)) - push_gate!(chain_a, Val(:Rz), 4, Phase(1 // 1)) - bir = BlockIR(ir, n_qubits, chain_a) + @testset "gates_to_circ with addition Ry" begin + n_qubits = 4 + # TODO add tests for Ry gate + chain_a = Chain() + push_gate!(chain_a, Val(:Ry), n_qubits, Phase(1 // 1)) + push_gate!(chain_a, Val(:Rz), 4, Phase(1 // 1)) + bir = BlockIR(ir, n_qubits, chain_a) - diagram = ZXDiagram(n_qubits) - ZX.gates_to_circ(diagram, chain_a, bir) + diagram = ZXDiagram(n_qubits) + ZX.gates_to_circ(diagram, chain_a, bir) end # TODO add test that checks if error is thrown for unkown gate @@ -321,5 +311,4 @@ push_gate!(chain, Val(:S), 3) # @test chain.args == Chain(ZXDiagram(bir)).args @test !isnothing(Chain(zxd)) end - end diff --git a/test/phase_teleportation.jl b/test/ZX/phase_teleportation.jl similarity index 98% rename from test/phase_teleportation.jl rename to test/ZX/phase_teleportation.jl index 3269521..8123729 100644 --- a/test/phase_teleportation.jl +++ b/test/ZX/phase_teleportation.jl @@ -1,4 +1,4 @@ -using Test, ZXCalculus, ZXCalculus.ZX +using Test, ZXCalculus, ZXCalculus.ZX function gen_cir() cir = ZXDiagram(5) diff --git a/test/plots.jl b/test/ZX/plots.jl similarity index 92% rename from test/plots.jl rename to test/ZX/plots.jl index 069cd23..2f45ead 100644 --- a/test/plots.jl +++ b/test/ZX/plots.jl @@ -1,7 +1,6 @@ using Test, ZXCalculus, ZXCalculus.ZX using ZXCalculus: ZX - # Othertests for ZXGraphs and ZXDigram are embededd into the zx_graph and zx_diagram testsets zxd = ZXDiagram(3) ZX.insert_spider!(zxd, 1, 2, SpiderType.H) @@ -10,4 +9,4 @@ ZX.insert_spider!(zxd, 1, 2, SpiderType.Z) zxg = ZXGraph(zxd) @test !isnothing(plot(zxd)) -@test !isnothing(plot(zxg)) +@test !isnothing(plot(zxg)) diff --git a/test/rules.jl b/test/ZX/rules.jl similarity index 93% rename from test/rules.jl rename to test/ZX/rules.jl index b951ec9..ac9b001 100644 --- a/test/rules.jl +++ b/test/ZX/rules.jl @@ -3,7 +3,7 @@ using ZXCalculus: ZX g = Multigraph([0 2 0; 2 0 1; 0 1 0]) collect(edges(g)) -ps = [i // 4 for i = 1:3] +ps = [i // 4 for i in 1:3] v_t = [SpiderType.Z, SpiderType.Z, SpiderType.X] zxd = ZXDiagram(g, v_t, ps) matches = match(Rule{:f}(), zxd) @@ -19,11 +19,11 @@ v_t = [SpiderType.X, SpiderType.X, SpiderType.Z, SpiderType.Z, SpiderType.Z] zxd = ZXDiagram(g, v_t, ps) matches = match(Rule{:i1}(), zxd) rewrite!(Rule{:i1}(), zxd, matches) -@test nv(zxd) == 3 && ne(zxd, count_mul = true) == 3 && ne(zxd) == 2 +@test nv(zxd) == 3 && ne(zxd, count_mul=true) == 3 && ne(zxd) == 2 @test !isnothing(zxd) g = Multigraph([0 2 0; 2 0 1; 0 1 0]) -ps = [i // 4 for i = 1:3] +ps = [i // 4 for i in 1:3] v_t = [SpiderType.X, SpiderType.X, SpiderType.Z] zxd = ZXDiagram(g, v_t, ps) matches = match(Rule{:h}(), zxd) @@ -33,7 +33,7 @@ rewrite!(Rule{:h}(), zxd, matches) matches = match(Rule{:i2}(), zxd) rewrite!(Rule{:i2}(), zxd, matches) -@test nv(zxd) == 4 && ne(zxd, count_mul = true) == 4 && ne(zxd) == 3 +@test nv(zxd) == 4 && ne(zxd, count_mul=true) == 4 && ne(zxd) == 3 g = Multigraph(6) add_edge!(g, 1, 2) @@ -48,7 +48,7 @@ v_t = [ SpiderType.Z, SpiderType.Out, SpiderType.Out, - SpiderType.Out, + SpiderType.Out ] zxd = ZXDiagram(g, v_t, ps) matches = match(Rule{:pi}(), zxd) @@ -64,7 +64,7 @@ v_t = [SpiderType.X, SpiderType.Z, SpiderType.In] zxd = ZXDiagram(g, v_t, ps) matches = match(Rule{:pi}(), zxd) rewrite!(Rule{:pi}(), zxd, matches) -@test nv(zxd) == 4 && ne(zxd) == 3 && ne(zxd, count_mul = true) == 4 +@test nv(zxd) == 4 && ne(zxd) == 3 && ne(zxd, count_mul=true) == 4 @test zxd.scalar == Scalar(0, 1 // 2) @test !isnothing(zxd) @@ -89,19 +89,19 @@ add_edge!(g, 2, 4) add_edge!(g, 3, 4) add_edge!(g, 3, 5) add_edge!(g, 4, 6) -ps = [0 // 1 for i = 1:6] +ps = [0 // 1 for i in 1:6] v_t = [ SpiderType.In, SpiderType.In, SpiderType.X, SpiderType.Z, SpiderType.Out, - SpiderType.Out, + SpiderType.Out ] layout = ZXCalculus.ZX.ZXLayout( 2, Dict(zip(1:6, [1 // 1, 2, 1, 2, 1, 2])), - Dict(zip(1:6, [1 // 1, 1, 2, 2, 3, 3])), + Dict(zip(1:6, [1 // 1, 1, 2, 2, 3, 3])) ) zxd = ZXDiagram(g, v_t, ps, layout) matches = match(Rule{:b}(), zxd) @@ -124,7 +124,7 @@ st = [ SpiderType.In, SpiderType.In, SpiderType.Out, - SpiderType.Out, + SpiderType.Out ] zxg = ZXGraph(ZXDiagram(g, st, ps)) for e in [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3]] @@ -157,7 +157,7 @@ st = [ SpiderType.In, SpiderType.Out, SpiderType.In, - SpiderType.Out, + SpiderType.Out ] zxg = ZXGraph(ZXDiagram(g, st, ps)) for e in [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 5], [2, 6], [2, 7], [2, 8]] @@ -209,7 +209,7 @@ st = [ SpiderType.In, SpiderType.Out, SpiderType.In, - SpiderType.Out, + SpiderType.Out ] zxg = ZXGraph(ZXDiagram(g, st, ps)) for e in [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 5], [2, 6], [2, 7], [2, 8]] @@ -240,7 +240,7 @@ st = [ SpiderType.Out, SpiderType.In, SpiderType.Out, - SpiderType.Out, + SpiderType.Out ] zxg = ZXGraph(ZXDiagram(g, st, ps)) for e in [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 5], [2, 6], [2, 7], [2, 8]] diff --git a/test/simplify.jl b/test/ZX/simplify.jl similarity index 100% rename from test/simplify.jl rename to test/ZX/simplify.jl diff --git a/test/zx_diagram.jl b/test/ZX/zx_diagram.jl similarity index 97% rename from test/zx_diagram.jl rename to test/ZX/zx_diagram.jl index 4f84884..e0bc6b1 100644 --- a/test/zx_diagram.jl +++ b/test/ZX/zx_diagram.jl @@ -2,7 +2,7 @@ using Test, ZXCalculus, Multigraphs, Graphs, ZXCalculus.ZX using ZXCalculus: ZX g = Multigraph([0 1 0; 1 0 1; 0 1 0]) -ps = [Rational(0) for i ∈ 1:3] +ps = [Rational(0) for i in 1:3] v_t = [SpiderType.X, SpiderType.Z, SpiderType.X] zxd = ZXDiagram(g, v_t, ps) zxd2 = ZXDiagram(g, Dict(zip(1:3, v_t)), Dict(zip(1:3, ps))) @@ -45,7 +45,7 @@ push_gate!(zxd3, Val{:SWAP}(), [2, 3]) push_gate!(zxd, Val(:Z), 3, 0) @test zxd.ps[11] == 0 // 1 @test_warn "" push_gate!(zxd, Val(:Z), 3, sqrt(2)) - @test_throws MethodError push_gate!(zxd, Val(:Z), 3, sqrt(2); autoconvert = false) + @test_throws MethodError push_gate!(zxd, Val(:Z), 3, sqrt(2); autoconvert=false) @test ZX.safe_convert(Rational{Int64}, 1.2) == 6 // 5 && ZX.safe_convert(Rational{Int64}, 1 // 2) == 1 // 2 @test !isnothing(zxd) diff --git a/test/zx_graph.jl b/test/ZX/zx_graph.jl similarity index 98% rename from test/zx_graph.jl rename to test/ZX/zx_graph.jl index e909b30..231d6cd 100644 --- a/test/zx_graph.jl +++ b/test/ZX/zx_graph.jl @@ -7,7 +7,7 @@ add_edge!(g, 2, 4) add_edge!(g, 3, 4) add_edge!(g, 3, 5) add_edge!(g, 4, 6) -ps = [0 // 1 for i = 1:6] +ps = [0 // 1 for i in 1:6] v_t = [SpiderType.In, SpiderType.In, SpiderType.X, SpiderType.Z, SpiderType.Out, SpiderType.Out] zxd = ZXDiagram(g, v_t, ps) zxg1 = ZXGraph(zxd) @@ -22,7 +22,6 @@ replace!(Rule{:b}(), zxd) zxg2 = ZXGraph(zxd) @test !ZX.is_hadamard(zxg2, 5, 8) && !ZX.is_hadamard(zxg2, 1, 7) - @testset "push gates into Diagram then plot ZXGraph" begin zxd = ZXDiagram(2) push_gate!(zxd, Val(:H), 1) diff --git a/test/utils.jl b/test/ZXW/utils.jl similarity index 83% rename from test/utils.jl rename to test/ZXW/utils.jl index 9535779..961510a 100644 --- a/test/utils.jl +++ b/test/ZXW/utils.jl @@ -1,41 +1,39 @@ using Test, ZXCalculus, ZXCalculus.ZXW, ZXCalculus.Utils, Multigraphs, Graphs using ZXCalculus: ZXW using ZXCalculus.ZXW: - Parameter, - _round_phase, - round_phases!, - print_spider, - push_gate!, - pushfirst_gate!, - insert_wtrig!, - expval_circ!, - substitute_variables!, - rem_spiders!, - add_inout!, - Z, - X, - W, - H + Parameter, + _round_phase, + round_phases!, + print_spider, + push_gate!, + pushfirst_gate!, + insert_wtrig!, + expval_circ!, + substitute_variables!, + rem_spiders!, + add_inout!, + Z, + X, + W, + H using MLStyle: @match - @testset "Phase rounding" begin st = Dict( 1 => Z(Parameter(Val(:PiUnit), -1)), 2 => Z(Parameter(Val(:PiUnit), -100 // 3)), 3 => X(Parameter(Val(:PiUnit), 1.5)), - 4 => X(Parameter(Val(:PiUnit), 2 // 3)), + 4 => X(Parameter(Val(:PiUnit), 2 // 3)) ) zxwd = ZXWDiagram(Multigraph(zeros(Int, 4, 4)), st) - st_rounded = Dict( 1 => Z(Parameter(Val(:PiUnit), 1)), 2 => Z(Parameter(Val(:PiUnit), 2 // 3)), 3 => X(Parameter(Val(:PiUnit), 1.5)), - 4 => X(Parameter(Val(:PiUnit), 2 // 3)), + 4 => X(Parameter(Val(:PiUnit), 2 // 3)) ) round_phases!(zxwd) @@ -51,7 +49,6 @@ using MLStyle: @match end @testset "ZXWDiagram Utilities" begin - zxwd = ZXWDiagram(3) @test @match ZXW.spider_type(zxwd, 1) begin @@ -73,20 +70,17 @@ end @test rem_edge!(zxwd, 5, 6) @test outneighbors(zxwd, 5) == inneighbors(zxwd, 5) - @test add_edge!(zxwd, 5, 6) @test neighbors(zxwd, 5) == [6] - @test_throws ErrorException("The vertex to connect does not exist.") ZXW.add_spider!( zxwd, W, - [10, 15], + [10, 15] ) new_v = ZXW.add_spider!(zxwd, W, [2, 3]) - @test @match zxwd.st[new_v] begin W => true _ => false @@ -128,15 +122,11 @@ end @test ZXW.nin(zxwd) == nqubits_prior + 3 @test ZXW.nout(zxwd) == nqubits_prior + 3 nspiders = nv(zxwd) - @test sort!([ZXW.get_inputs(zxwd)[end-2:end]; ZXW.get_outputs(zxwd)[end-2:end]]) == - collect(nspiders-5:nspiders) - - - + @test sort!([ZXW.get_inputs(zxwd)[(end - 2):end]; ZXW.get_outputs(zxwd)[(end - 2):end]]) == + collect((nspiders - 5):nspiders) end @testset "gate insertion" begin - zxwd = ZXWDiagram(2) pushfirst_gate!(zxwd, Val(:X), 1) @@ -157,15 +147,14 @@ end @test insert_wtrig!(zxwd, [1, 2, 3, 4]) == 25 end - @testset "Example 28" begin zxwd = ZXWDiagram(2) push_gate!(zxwd, Val(:H), 1) push_gate!(zxwd, Val(:H), 2) push_gate!(zxwd, Val(:CZ), 1, 2) - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) - push_gate!(zxwd, Val(:X), 2, :b; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) + push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false) exp_zxwd = expval_circ!(copy(zxwd), "ZZ") @@ -175,5 +164,4 @@ end exp_yao = 0.7694208842938131 @test exp_val ≈ exp_yao - end diff --git a/test/zxw_diagram.jl b/test/ZXW/zxw_diagram.jl similarity index 78% rename from test/zxw_diagram.jl rename to test/ZXW/zxw_diagram.jl index c960a78..d8c79e4 100644 --- a/test/zxw_diagram.jl +++ b/test/ZXW/zxw_diagram.jl @@ -4,29 +4,25 @@ using ZXCalculus.Utils: Parameter, PiUnit, Factor using ZXCalculus: ZXW @testset "ZXWSpiderType" begin - - spider_vec = - [Z(Parameter(Val(:PiUnit), 3)) Z(Parameter(Val(:Factor), exp(im * 1.5 * π))) ZXW.X( - Parameter(Val(:PiUnit), 3), - ) ZXW.X(Parameter(Val(:Factor), exp(im * 0.5 * π))) W ZXW.H D Input(10) Output(2)] + spider_vec = [Z(Parameter(Val(:PiUnit), 3)) Z(Parameter(Val(:Factor), exp(im * 1.5 * π))) ZXW.X( + Parameter(Val(:PiUnit), 3), + ) ZXW.X(Parameter(Val(:Factor), exp(im * 0.5 * π))) W ZXW.H D Input(10) Output(2)] @test spider_vec[1].p == Parameter(Val(:PiUnit), 3) @test spider_vec[2].p == Parameter(Val(:Factor), exp(im * 1.5 * π)) @test spider_vec[3].p == Parameter(Val(:PiUnit), 3) @test spider_vec[4].p == Parameter(Val(:Factor), exp(im * 0.5 * π)) - @test spider_vec[end-1].qubit == 10 + @test spider_vec[end - 1].qubit == 10 @test spider_vec[end].qubit == 2 - end @testset "ZXWDiagram Constructors" begin - g = Multigraph([0 1 0; 1 0 1; 0 1 0]) - st = [Z(Parameter(Val(:PiUnit), (-10 * i + 1) // 2)) for i = 1:3] + st = [Z(Parameter(Val(:PiUnit), (-10 * i + 1) // 2)) for i in 1:3] @test_throws ErrorException("There should be a type for each spider!") ZXWDiagram( g, - st[1:2], + st[1:2] ) zxwd_vec = ZXWDiagram(g, st) @@ -46,5 +42,4 @@ end zxwd_copy = copy(zxwd_vec) @test zxwd_copy.mg.adjlist == zxwd_vec.mg.adjlist && zxwd_copy.st == zxwd_vec.st - end diff --git a/test/zxw_rules.jl b/test/ZXW/zxw_rules.jl similarity index 77% rename from test/zxw_rules.jl rename to test/ZXW/zxw_rules.jl index 0b46e6d..86b76dc 100644 --- a/test/zxw_rules.jl +++ b/test/ZXW/zxw_rules.jl @@ -1,17 +1,16 @@ using Test, ZXCalculus, ZXCalculus.ZXW, ZXCalculus.ZX, Graphs using Test: match_logs using ZXCalculus.ZXW: - CalcRule, - rewrite!, - symbol_vertices, - dagger, - concat!, - expval_circ!, - push_gate!, - stack_zxwd!, - substitute_variables!, - insert_spider! - + CalcRule, + rewrite!, + symbol_vertices, + dagger, + concat!, + expval_circ!, + push_gate!, + stack_zxwd!, + substitute_variables!, + insert_spider! @testset "Calculus Rule" begin deri_rule = CalcRule(:diff, :p) @@ -40,27 +39,23 @@ end @test !has_vertex(zxwd.mg, 10) rewrite!(Rule(:s2), zxwd, vs2) @test !has_vertex(zxwd.mg, 11) - end @testset "Parameter Shift" begin - zxwd = ZXWDiagram(2) a = 0.3 b = 0.5 - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) - push_gate!(zxwd, Val(:X), 2, :b; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) + push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false) exp_zxwd = expval_circ!(copy(zxwd), "ZZ") exp_pluspihf = substitute_variables!(copy(exp_zxwd), Dict(:a => a, :b => b + 1 / 2)) exp_mnuspihf = substitute_variables!(copy(exp_zxwd), Dict(:a => a, :b => b - 1 / 2)) # should be around -1.8465 - gradient_parameter_shift = - real(π / 2 * (Matrix(exp_pluspihf)[1, 1] - Matrix(exp_mnuspihf)[1, 1])) - + gradient_parameter_shift = real(π / 2 * (Matrix(exp_pluspihf)[1, 1] - Matrix(exp_mnuspihf)[1, 1])) matches = match(CalcRule(:diff, :b), exp_zxwd) diff_zxwd = rewrite!(CalcRule(:diff, :b), exp_zxwd, matches) @@ -85,7 +80,7 @@ end # where k goes from 0 to 2, hence the factor 2 in test zxwd = ZXWDiagram(1) - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) exp_zxwd = expval_circ!(copy(zxwd), "Z") matches = match(CalcRule(:int, :a), exp_zxwd) @@ -93,12 +88,11 @@ end int_val = real(Matrix(int_zxwd)[1, 1]) - @test isapprox(2 * int_val, 0.0; atol = 1e-10) - + @test isapprox(2 * int_val, 0.0; atol=1e-10) zxwd = ZXWDiagram(2) - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) - push_gate!(zxwd, Val(:X), 2, :b; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) + push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false) exp_zxwd = expval_circ!(copy(zxwd), "IZ") matches = match(CalcRule(:int, :a), exp_zxwd) @@ -106,14 +100,13 @@ end int_zxwd = substitute_variables!(int_zxwd, Dict(:a => 0.3, :b => 0.0)) int_val = real(Matrix(int_zxwd)[1, 1]) # constant, should be 2.0 - @test isapprox(2 * int_val, 2.0; atol = 1e-10) + @test isapprox(2 * int_val, 2.0; atol=1e-10) end @testset "Theorem 23" begin - zxwd = ZXWDiagram(2) - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) - push_gate!(zxwd, Val(:X), 2, :a; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) + push_gate!(zxwd, Val(:X), 2, :a; autoconvert=false) exp_zxwd = expval_circ!(copy(zxwd), "ZZ") matches = match(CalcRule(:int, :a), exp_zxwd) @@ -123,7 +116,7 @@ end # By thm 23, and change of dummy variable, k * pi = alpha # we get 1/2 \int_{-1}^{1} ... dk = ZXWDiagram # hence the factor of two here - @test isapprox(2 * int_val, 1.0; atol = 1e-10) + @test isapprox(2 * int_val, 1.0; atol=1e-10) end @testset "Lemma 30 - a" begin @@ -136,8 +129,8 @@ end push_gate!(zxwd, Val(:H), 1) push_gate!(zxwd, Val(:H), 2) push_gate!(zxwd, Val(:CZ), 1, 2) - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) - push_gate!(zxwd, Val(:X), 2, :b; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) + push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false) exp_zxwd = expval_circ!(zxwd, "ZZ") # essential to take diff here first, not stack then diff @@ -155,7 +148,7 @@ end A = real( Matrix(substitute_variables!(copy(dbdiff_zxwd), Dict(:a => a, :b => 0.0)))[1, 1], ) - @test isapprox(int_valb, A / 2; atol = 1e-10) + @test isapprox(int_valb, A / 2; atol=1e-10) end @testset "Lemma 30 - b" begin @@ -164,8 +157,8 @@ end push_gate!(zxwd, Val(:H), 1) push_gate!(zxwd, Val(:H), 2) push_gate!(zxwd, Val(:CZ), 1, 2) - push_gate!(zxwd, Val(:X), 1, :a; autoconvert = false) - push_gate!(zxwd, Val(:X), 2, :b; autoconvert = false) + push_gate!(zxwd, Val(:X), 1, :a; autoconvert=false) + push_gate!(zxwd, Val(:X), 2, :b; autoconvert=false) exp_zxwd = expval_circ!(zxwd, "ZZ") matches = match(CalcRule(:diff, :b), exp_zxwd) @@ -181,5 +174,5 @@ end int_vala = real(Matrix(int_dadiff)[1, 1]) A = real(Matrix(substitute_variables!(copy(int_dbdiff), Dict(:a => 0.0)))[1, 1]) - @test isapprox(int_vala, A / 2; atol = 1e-10) + @test isapprox(int_vala, A / 2; atol=1e-10) end diff --git a/test/runtests.jl b/test/runtests.jl index 899ccaf..6be9205 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,91 +2,101 @@ using ZXCalculus, Documenter, Test using Vega using DataFrames +@testset "ZX module" begin + @testset "plots.jl" begin + include("ZX/plots.jl") + end -@testset "plots.jl" begin - include("plots.jl") -end + @testset "equality.jl" begin + include("ZX/equality.jl") + end -@testset "equality.jl" begin - include("equality.jl") -end + @testset "abstract_zx_diagram.jl" begin + include("ZX/abstract_zx_diagram.jl") + end -@testset "abstract_zx_diagram.jl" begin - include("abstract_zx_diagram.jl") -end + @testset "zx_diagram.jl" begin + include("ZX/zx_diagram.jl") + end -@testset "zx_diagram.jl" begin - include("zx_diagram.jl") -end + @testset "rules.jl" begin + include("ZX/rules.jl") + end -@testset "rules.jl" begin - include("rules.jl") -end + @testset "zx_graph.jl" begin + include("ZX/zx_graph.jl") + end -@testset "zx_graph.jl" begin - include("zx_graph.jl") -end + @testset "circuit_extraction.jl" begin + include("ZX/circuit_extraction.jl") + end -@testset "circuit_extraction.jl" begin - include("circuit_extraction.jl") -end + @testset "phase_teleportation.jl" begin + include("ZX/phase_teleportation.jl") + end -@testset "phase_teleportation.jl" begin - include("phase_teleportation.jl") -end + @testset "ir.jl" begin + include("ZX/ir.jl") + end -@testset "ir.jl" begin - include("ir.jl") -end + @testset "simplify.jl" begin + include("ZX/simplify.jl") + end -@testset "scalar.jl" begin - include("scalar.jl") + @testset "ancilla_extraction.jl" begin + include("ZX/ancilla_extraction.jl") + include("ZX/challenge.jl") + end end -@testset "phase.jl" begin - include("phase.jl") -end - -@testset "parameter.jl" begin - include("parameter.jl") -end +@testset "Utils module" begin + @testset "scalar.jl" begin + include("Utils/scalar.jl") + end -@testset "simplify.jl" begin - include("simplify.jl") -end + @testset "phase.jl" begin + include("Utils/phase.jl") + end -@testset "ancilla_extraction.jl" begin - include("ancilla_extraction.jl") - include("challenge.jl") + @testset "parameter.jl" begin + include("Utils/parameter.jl") + end end -@testset "zxw_diagram.jl" begin - include("zxw_diagram.jl") -end +@testset "ZXW module" begin + @testset "zxw_diagram.jl" begin + include("ZXW/zxw_diagram.jl") + end -@testset "utils.jl" begin - include("utils.jl") -end + @testset "utils.jl" begin + include("ZXW/utils.jl") + end -@testset "zxw_rules.jl" begin - include("zxw_rules.jl") + @testset "zxw_rules.jl" begin + include("ZXW/zxw_rules.jl") + end end -@testset "planar multigraphs.jl" begin - include("planar_multigraph.jl") +@testset "PMG module" begin + @testset "planar multigraphs.jl" begin + include("PMG/planar_multigraph.jl") + end end -@testset "ZW Diagram with Planar Multigraph" begin - include("zw_diagram.jl") -end +@testset "ZW module" begin + @testset "ZW Diagram with Planar Multigraph" begin + include("ZW/zw_diagram.jl") + end -@testset "ZW Diagram Utilities" begin - include("zw_utils.jl") + @testset "ZW Diagram Utilities" begin + include("ZW/zw_utils.jl") + end end - -@testset "to_eincode.jl" begin - include("to_eincode.jl") +@testset "Application module" begin + @testset "to_eincode.jl" begin + include("Application/to_eincode.jl") + end end doctest(ZXCalculus)