-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Qibo circuits to
Quantum
Tensor Networks (#214)
* 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
Showing
5 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[deps] | ||
qibo = "" | ||
qiskit = "" | ||
|
||
[pip.deps] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,6 @@ end | |
|
||
include("Qiskit.jl") | ||
include("Quimb.jl") | ||
include("Qibo.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters