Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Integrate Quac with Qrochet (#9)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Make qft test more complete

* Clean qft test

* Enhance comment syntax

Co-authored-by: Sergio Sánchez Ramírez <[email protected]>

* Refactor expression

* Skip "all inner indices are not sites" test

* Add comment

---------

Co-authored-by: Sergio Sánchez Ramírez <[email protected]>
Co-authored-by: Jofre Vallès Muns <[email protected]>
  • Loading branch information
3 people authored Feb 16, 2024
1 parent 7b4d4ef commit 71c761d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
42 changes: 42 additions & 0 deletions ext/QrochetQuacExt.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 23 additions & 0 deletions test/integration/QrochetQuacExt_test.jl
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 71c761d

Please sign in to comment.