Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Qibo circuits to Quantum Tensor Networks #214

Merged
merged 7 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
qibo = ""
qiskit = ""

[pip.deps]
Expand Down
40 changes: 40 additions & 0 deletions ext/TenetPythonCallExt/Qibo.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Tenet.Quantum(::Val{:qibo}, pyobj::Py)
qibo = pyimport("qibo")
qibo.set_backend("numpy")
if !pyissubclass(pytype(pyobj), qibo.models.circuit.Circuit)
throw(ArgumentError("Expected a qibo.models.circuit.Circuit object, got $(pyfullyqualname(pyobj))"))
end

n = pyconvert(Int, pyobj.nqubits)
gen = Tenet.IndexCounter()

wire = [[Tenet.nextindex!(gen)] for _ in 1:n]
tn = TensorNetwork()
circgates = pyobj.queue

for gate in circgates
matrix = pyconvert(Array, gate.matrix())

qubits = map(x -> pyconvert(Int, x), gate.qubits)
array = reshape(matrix, fill(2, 2 * length(qubits))...)

inds = (x -> collect(Iterators.flatten(zip(x...))))(
map(qubits) do l
l += 1
from, to = last(wire[l]), Tenet.nextindex!(gen)
push!(wire[l], to)
(from, to)
end,
)

tensor = Tensor(array, Tuple(inds))
push!(tn, tensor)
end

sites = merge(
Dict([Site(site; dual=true) => first(index) for (site, index) in enumerate(wire) if first(index) ∈ tn]),
Dict([Site(site; dual=false) => last(index) for (site, index) in enumerate(wire) if last(index) ∈ tn]),
)

return Quantum(tn, sites)
end
1 change: 1 addition & 0 deletions ext/TenetPythonCallExt/TenetPythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ end

include("Qiskit.jl")
include("Quimb.jl")
include("Qibo.jl")

end
18 changes: 18 additions & 0 deletions test/integration/python/test_qibo.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@testset "qibo" begin
using PythonCall
qibo = pyimport("qibo")

circuit = qibo.Circuit(3)
circuit.add(qibo.gates.H(0))
circuit.add(qibo.gates.H(1))
circuit.add(qibo.gates.CNOT(1, 2))
circuit.add(qibo.gates.CNOT(0, 2))
circuit.add(qibo.gates.H(0))
circuit.add(qibo.gates.H(1))
circuit.add(qibo.gates.H(2))

tn = Tenet.Quantum(circuit)
@test issetequal(sites(tn; set=:inputs), adjoint.(Site.([1, 2, 3])))
@test issetequal(sites(tn; set=:outputs), Site.([1, 2, 3]))
@test Tenet.ntensors(tn) == 7
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if VERSION >= v"1.10"
@testset "Python" begin
include("integration/python/test_quimb.jl")
include("integration/python/test_qiskit.jl")
include("integration/python/test_qibo.jl")
end
end
end
Expand Down
Loading