Skip to content

Commit

Permalink
Convert Qibo circuits to Quantum Tensor Networks (#214)
Browse files Browse the repository at this point in the history
* Add qibo dependency

* Add module for Qibo circuit to Tenet conversion

* Add Qibo.jl module

* Add Qibo-Tenet conversion test

* Format code

* Fix qibo import name

* Set numpy as qibo backend
  • Loading branch information
Todorbsc authored Oct 3, 2024
1 parent 5825ffc commit 4c16d2f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
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

0 comments on commit 4c16d2f

Please sign in to comment.