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

Integrate Quac with Qrochet #9

Merged
merged 13 commits into from
Feb 16, 2024
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"
jofrevalles marked this conversation as resolved.
Show resolved Hide resolved

[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"
18 changes: 18 additions & 0 deletions test/integration/QrochetQuacExt_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@testset "QrochetQuacExt" begin
using Quac

@testset "QFT_3qubits" begin
Todorbsc marked this conversation as resolved.
Show resolved Hide resolved
qft3circ = Quac.Algorithms.QFT(3)
qft3qrochet = Quantum(qft3circ)

# test sites (inputs and outputs) of the quantum circuit
for site in values(qft3qrochet.sites)
@test length(qft3qrochet.tn.indexmap[site]) == 1
end

# test inner tensors
for notsite in filter(idx -> idx ∉ values(qrqft3.sites), keys(qrqft3.tn.indexmap))
@test length(qft3qrochet.tn.indexmap[notsite]) > 1
end
mofeing marked this conversation as resolved.
Show resolved Hide resolved
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
Loading