From 71c761d0a53de3dfe3acfc7c1dc965e24d171410 Mon Sep 17 00:00:00 2001 From: Todorbsc <145352308+Todorbsc@users.noreply.github.com> Date: Fri, 16 Feb 2024 12:21:06 +0100 Subject: [PATCH] Integrate Quac with Qrochet (#9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add Quac as weak dep and QrochetQuacExt as extension * Integrate Quac in Qrochet * Add QFT quantum circuit test * Include QrochetQuacExt tests * Move QrochetQuacExt test to integration tests * Add Quac dependency for tests * Rename testsets Co-authored-by: Sergio Sánchez Ramírez <15837247+mofeing@users.noreply.github.com> * Make qft test more complete * Clean qft test * Enhance comment syntax Co-authored-by: Sergio Sánchez Ramírez <15837247+mofeing@users.noreply.github.com> * Refactor expression * Skip "all inner indices are not sites" test * Add comment --------- Co-authored-by: Sergio Sánchez Ramírez <15837247+mofeing@users.noreply.github.com> Co-authored-by: Jofre Vallès Muns <61060572+jofrevalles@users.noreply.github.com> --- Project.toml | 7 +++++ ext/QrochetQuacExt.jl | 42 +++++++++++++++++++++++++ test/Project.toml | 1 + test/integration/QrochetQuacExt_test.jl | 23 ++++++++++++++ test/runtests.jl | 4 ++- 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 ext/QrochetQuacExt.jl create mode 100644 test/integration/QrochetQuacExt_test.jl diff --git a/Project.toml b/Project.toml index 253a3e7..7281e97 100644 --- a/Project.toml +++ b/Project.toml @@ -8,7 +8,14 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Tenet = "85d41934-b9cd-44e1-8730-56d86f15f3ec" ValSplit = "0625e100-946b-11ec-09cd-6328dd093154" +[weakdeps] +Quac = "b9105292-1415-45cf-bff1-d6ccf71e6143" + +[extensions] +QrochetQuacExt = "Quac" + [compat] +Quac = "0.2" Tenet = "0.5" ValSplit = "0.1" julia = "1.9" diff --git a/ext/QrochetQuacExt.jl b/ext/QrochetQuacExt.jl new file mode 100644 index 0000000..bcfb289 --- /dev/null +++ b/ext/QrochetQuacExt.jl @@ -0,0 +1,42 @@ +module QrochetQuacExt + +using Qrochet +using Tenet +using Quac: Circuit, lanes, arraytype, Swap + +function Qrochet.Quantum(circuit::Circuit) + n = lanes(circuit) + wire = [[Tenet.letter(i)] for i in 1:n] + tensors = Tensor[] + + i = n + 1 + for gate in circuit + G = arraytype(gate) + array = G(gate) + + if gate isa Swap + (a, b) = lanes(gate) + wire[a], wire[b] = wire[b], wire[a] + continue + end + + inds = map(lanes(gate)) do l + from, to = last(wire[l]), Tenet.letter(i) + i += 1 + push!(wire[l], to) + (from, to) + end |> x -> zip(x...) |> Iterators.flatten |> collect + + tensor = Tensor(array, tuple(inds...)) + push!(tensors, tensor) + end + + sites = merge( + Dict([Site(site; dual=true) => first(index) for (site, index) in enumerate(wire)]), + Dict([Site(site; dual=false) => last(index) for (site, index) in enumerate(wire)]) + ) + + Quantum(Tenet.TensorNetwork(tensors), sites) +end + +end \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml index f6fa720..dfed1ae 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -2,3 +2,4 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Tenet = "85d41934-b9cd-44e1-8730-56d86f15f3ec" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Quac = "b9105292-1415-45cf-bff1-d6ccf71e6143" diff --git a/test/integration/QrochetQuacExt_test.jl b/test/integration/QrochetQuacExt_test.jl new file mode 100644 index 0000000..eefaeeb --- /dev/null +++ b/test/integration/QrochetQuacExt_test.jl @@ -0,0 +1,23 @@ +@testset "Quac" begin + using Quac + + @testset "QFT" begin + n = 3 + qftcirc = Quac.Algorithms.QFT(n) + qftqtn = Quantum(qftcirc) + + # correct number of inputs and outputs + @test ninputs(qftqtn) == n + @test noutputs(qftqtn) == n + @test socket(qftqtn) == Operator() + + # all open indices are sites + siteinds = getindex.((qftqtn,), sites(qftqtn)) + @test issetequal(inds(TensorNetwork(qftqtn), :open), siteinds) + + # all inner indices are not sites + # TODO too strict condition. remove? + notsiteinds = setdiff(inds(TensorNetwork(qftqtn)), siteinds) + @test_skip issetequal(inds(TensorNetwork(qftqtn), :inner), notsiteinds) + end +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index b4a3d3d..ffceb90 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,7 +8,9 @@ using Qrochet include("Ansatz/Chain_test.jl") end -@testset "Integration tests" verbose = true begin end +@testset "Integration tests" verbose = true begin + include("integration/QrochetQuacExt_test.jl") +end if haskey(ENV, "ENABLE_AQUA_TESTS") @testset "Aqua" verbose = true begin