-
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.
* Prepare TenetPythonCallExt for more Python packages * Convert from Quimb's TensorNetwork to `TensorNetwork` * Refactor type check for Qiskit QuantumCircuit conversion to `Tenet.Quantum` * Fix type check for Quimb's TensorNetwork conversion to `Tenet.TensorNetwork` * Implement Quimb's TensorNetworkGenVector, TensorNetworkGenOperator conversion to `Quantum` * Test Qiskit, Quimb integration * Fix conversion issues * Move CondaPkg.toml to project root Otherwise, `Pkg.test()` won't detect it. * Fix quimb Circuit option * Wrap python tests into a testset
- Loading branch information
Showing
9 changed files
with
286 additions
and
14 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
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,5 @@ | ||
[deps] | ||
qiskit = "" | ||
|
||
[pip.deps] | ||
quimb = "" |
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
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,44 @@ | ||
function Tenet.TensorNetwork(::Val{:quimb}, pyobj::Py) | ||
quimb = pyimport("quimb") | ||
if !pyissubclass(pytype(pyobj), quimb.tensor.tensor_core.TensorNetwork) | ||
throw(ArgumentError("Expected a quimb.tensor.tensor_core.TensorNetwork object, got $(pyfullyqualname(pyobj))")) | ||
end | ||
|
||
ts = map(pyobj.tensors) do tensor | ||
array = pyconvert(Array, tensor.data) | ||
inds = Symbol.(pyconvert(Array, tensor.inds)) | ||
Tensor(array, inds) | ||
end | ||
|
||
return TensorNetwork(ts) | ||
end | ||
|
||
function Tenet.Quantum(::Val{:quimb}, pyobj::Py) | ||
quimb = pyimport("quimb") | ||
if pyissubclass(pytype(pyobj), quimb.tensor.circuit.Circuit) | ||
return Quantum(pyobj.get_uni()) | ||
elseif pyissubclass(pytype(pyobj), quimb.tensor.tensor_arbgeom.TensorNetworkGenVector) | ||
return Quantum(Val(Symbol("quimb.tensor.tensor_arbgeom.TensorNetworkGenVector")), pyobj) | ||
elseif pyissubclass(pytype(pyobj), quimb.tensor.tensor_arbgeom.TensorNetworkGenOperator) | ||
return Quantum(Val(Symbol("quimb.tensor.tensor_arbgeom.TensorNetworkGenOperator")), pyobj) | ||
else | ||
throw(ArgumentError("Unknown treatment for object of class $(pyfullyqualname(pyobj))")) | ||
end | ||
end | ||
|
||
function Tenet.Quantum(::Val{Symbol("quimb.tensor.tensor_arbgeom.TensorNetworkGenVector")}, pyobj::Py) | ||
tn = TensorNetwork(pyobj) | ||
sitedict = Dict(Site(pyconvert(Int, i)) => pyconvert(Symbol, pyobj.site_ind(i)) for i in pyobj.sites) | ||
return Quantum(tn, sitedict) | ||
end | ||
|
||
function Tenet.Quantum(::Val{Symbol("quimb.tensor.tensor_arbgeom.TensorNetworkGenOperator")}, pyobj::Py) | ||
tn = TensorNetwork(pyobj) | ||
|
||
sitedict = merge!( | ||
Dict(Site(pyconvert(Int, i)) => pyconvert(Symbol, pyobj.lower_ind(i)) for i in pyobj.sites), | ||
Dict(Site(pyconvert(Int, i); dual=true) => pyconvert(Symbol, pyobj.upper_ind(i)) for i in pyobj.sites), | ||
) | ||
|
||
return Quantum(tn, sitedict) | ||
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,22 @@ | ||
module TenetPythonCallExt | ||
|
||
using Tenet | ||
using PythonCall | ||
using PythonCall.Core: pyisnone | ||
|
||
pyfullyqualname(pyobj) = join([pytype(pyobj).__module__, pytype(pyobj).__qualname__], '.') | ||
|
||
function Tenet.TensorNetwork(pyobj::Py) | ||
pymodule, _ = split(pyconvert(String, pytype(pyobj).__module__), "."; limit=2) | ||
return TensorNetwork(Val(Symbol(pymodule)), pyobj) | ||
end | ||
|
||
function Tenet.Quantum(pyobj::Py) | ||
pymodule, _ = split(pyconvert(String, pytype(pyobj).__module__), "."; limit=2) | ||
return Quantum(Val(Symbol(pymodule)), pyobj) | ||
end | ||
|
||
include("Qiskit.jl") | ||
include("Quimb.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
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 "qiskit" begin | ||
using PythonCall | ||
qiskit = pyimport("qiskit") | ||
|
||
circuit = qiskit.QuantumCircuit(3) | ||
circuit.h(0) | ||
circuit.h(1) | ||
circuit.cx(1, 2) | ||
circuit.cx(0, 2) | ||
circuit.h(0) | ||
circuit.h(1) | ||
circuit.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@testset "quimb" begin | ||
using PythonCall | ||
qtn = pyimport("quimb.tensor") | ||
|
||
# NOTE quimb.circuit.Circuit splits gates by default | ||
qc = qtn.Circuit(3; gate_opts=Dict(["contract" => false])) | ||
gates = [("H", 0), ("H", 1), ("CNOT", 1, 2), ("CNOT", 0, 2), ("H", 0), ("H", 1), ("H", 2)] | ||
qc.apply_gates(gates) | ||
|
||
tn = Tenet.Quantum(qc) | ||
@test issetequal(sites(tn; set=:inputs), adjoint.(Site.([0, 1, 2]))) | ||
@test issetequal(sites(tn; set=:outputs), Site.([0, 1, 2])) | ||
@test Tenet.ntensors(tn) == 7 | ||
|
||
tn = Tenet.Quantum(qc.psi) | ||
@test isempty(sites(tn; set=:inputs)) | ||
@test issetequal(sites(tn; set=:outputs), Site.([0, 1, 2])) | ||
@test Tenet.ntensors(tn) == 10 | ||
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