From 0088e8d03fc1c3a57e7fe9e1363d507443a7e8e3 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 14 Mar 2024 15:08:16 +0100 Subject: [PATCH 1/7] Include targettype function for controlled gates and controlled ops --- src/Gate.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Gate.jl b/src/Gate.jl index b089b05..4c05941 100644 --- a/src/Gate.jl +++ b/src/Gate.jl @@ -378,6 +378,8 @@ targettype(::Type{Op}) where {Op<:Operator} = Op targettype(::Type{Control{Op}}) where {Op} = Op targettype(::Type{Control{Op}}) where {Op<:Control} = targettype(Op) targettype(::Type{<:Gate{Op}}) where {Op} = targettype(Op) +targettype(g::Control{Op}) where {Op} = targettype(typeof(g)) +targettype(g::Gate{Op}) where {Op<:Control} = targettype(typeof(g)) control(g::G) where {G<:Gate{<:Control}} = lanes(g)[1:end-length(targettype(G))] target(g::G) where {G<:Gate{<:Control}} = lanes(g)[end-length(targettype(G))+1:end] From cf787b1d3b6a90d250c210be5dd7575b892d71ad Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 14 Mar 2024 15:19:44 +0100 Subject: [PATCH 2/7] Set parameters in new targettype functions as type parameters --- src/Gate.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Gate.jl b/src/Gate.jl index 4c05941..1a3e440 100644 --- a/src/Gate.jl +++ b/src/Gate.jl @@ -378,8 +378,8 @@ targettype(::Type{Op}) where {Op<:Operator} = Op targettype(::Type{Control{Op}}) where {Op} = Op targettype(::Type{Control{Op}}) where {Op<:Control} = targettype(Op) targettype(::Type{<:Gate{Op}}) where {Op} = targettype(Op) -targettype(g::Control{Op}) where {Op} = targettype(typeof(g)) -targettype(g::Gate{Op}) where {Op<:Control} = targettype(typeof(g)) +targettype(::Op) where {Op} = targettype(Op) +targettype(::Gate{Op}) where {Op<:Operator} = targettype(Op) control(g::G) where {G<:Gate{<:Control}} = lanes(g)[1:end-length(targettype(G))] target(g::G) where {G<:Gate{<:Control}} = lanes(g)[end-length(targettype(G))+1:end] From a1a688d0829c98dea70c93124ec39b3decb37de1 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 14 Mar 2024 15:32:57 +0100 Subject: [PATCH 3/7] Add tests for targettype with op and gate parameters --- test/Gate_test.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Gate_test.jl b/test/Gate_test.jl index 8a33f13..a52cb5a 100644 --- a/test/Gate_test.jl +++ b/test/Gate_test.jl @@ -480,6 +480,16 @@ @testset "$Op" for Op in [Control{I}, Control{Control{I}}, Control{Control{Control{I}}}] @test targettype(Gate{Op}) === I end + + @test begin + crzop = operator(CRz(1, 2, θ = π / 4)) + targettype(crzop) === Rz + end + + @test begin + crzgate = CRz(1, 2, θ = π / 4) + targettype(crzgate) === Rz + end end @testset "control" begin From 94e31ef9a00ce3c2407acd72533ea3cbb951abc0 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 14 Mar 2024 15:41:58 +0100 Subject: [PATCH 4/7] Add single-qubit operator in targettype testset --- test/Gate_test.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Gate_test.jl b/test/Gate_test.jl index a52cb5a..47e2fe0 100644 --- a/test/Gate_test.jl +++ b/test/Gate_test.jl @@ -477,7 +477,7 @@ @test targettype(Gate{Op}) === Op end - @testset "$Op" for Op in [Control{I}, Control{Control{I}}, Control{Control{Control{I}}}] + @testset "$Op" for Op in [I, Control{I}, Control{Control{I}}, Control{Control{Control{I}}}] @test targettype(Gate{Op}) === I end From 723a0c8d75afc8f13d6ac0cf653dc0642a58945d Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 14 Mar 2024 16:03:28 +0100 Subject: [PATCH 5/7] Improve gate and operator targettype tests --- test/Gate_test.jl | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/test/Gate_test.jl b/test/Gate_test.jl index 47e2fe0..1bbebd1 100644 --- a/test/Gate_test.jl +++ b/test/Gate_test.jl @@ -481,14 +481,18 @@ @test targettype(Gate{Op}) === I end - @test begin - crzop = operator(CRz(1, 2, θ = π / 4)) - targettype(crzop) === Rz - end - - @test begin - crzgate = CRz(1, 2, θ = π / 4) - targettype(crzgate) === Rz + @testset "gate_operator_targettype" begin + gates = [Rx(1, θ = π / 4), CRy(1, 2, θ = π / 6), Control(1, Control(2, Rz(3, θ = π / 3)))] + targettypes = [Rx, Ry, Rz] + + @testset "gate_targettype_$gate" for (gate, tgttype) in zip(gates, targettypes) + @test targettype(gate) === tgttype + end + + @testset "op_targettype_$gate" for (gate, tgttype) in zip(gates, targettypes) + op = operator(gate) + @test targettype(op) === tgttype + end end end From 2121fe3adf70d2d37bd47cca0e97303a3cc80233 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 14 Mar 2024 16:33:16 +0100 Subject: [PATCH 6/7] Clean testset for targettype --- test/Gate_test.jl | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/test/Gate_test.jl b/test/Gate_test.jl index 1bbebd1..7ca7c8a 100644 --- a/test/Gate_test.jl +++ b/test/Gate_test.jl @@ -481,18 +481,12 @@ @test targettype(Gate{Op}) === I end - @testset "gate_operator_targettype" begin - gates = [Rx(1, θ = π / 4), CRy(1, 2, θ = π / 6), Control(1, Control(2, Rz(3, θ = π / 3)))] - targettypes = [Rx, Ry, Rz] - - @testset "gate_targettype_$gate" for (gate, tgttype) in zip(gates, targettypes) - @test targettype(gate) === tgttype - end - - @testset "op_targettype_$gate" for (gate, tgttype) in zip(gates, targettypes) - op = operator(gate) - @test targettype(op) === tgttype - end + @testset "$(operator(gate))" for (gate, correct) in [ + (Z(1), Z), + (CZ(1, 2), Z), + (Control(1, Control(2, Z(3))), Z), + ] + @test targettype(gate) === correct end end From 7a6c62191ba71dd32b5390e1e3c49b27bafd7096 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 14 Mar 2024 16:38:31 +0100 Subject: [PATCH 7/7] Add coverage for targettype with operator parameter --- test/Gate_test.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Gate_test.jl b/test/Gate_test.jl index 7ca7c8a..349d5fb 100644 --- a/test/Gate_test.jl +++ b/test/Gate_test.jl @@ -487,6 +487,7 @@ (Control(1, Control(2, Z(3))), Z), ] @test targettype(gate) === correct + @test targettype(operator(gate)) === correct end end