-
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.
Support Qiskit
QuantumCircuit
conversion to Quantum
- Loading branch information
Showing
2 changed files
with
57 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
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,54 @@ | ||
module TenetPythonCallExt | ||
|
||
using Tenet | ||
using PythonCall | ||
using PythonCall.Core: pyisnone | ||
|
||
pyfullyqualname(pyobj) = join([pytype(pyobj).__module__, pytype(pyobj).__qualname__], '.') | ||
|
||
function Tenet.Quantum(pyobj::Py) | ||
pyclassname = pyfullyqualname(pyobj) | ||
if pyclassname != "qiskit.circuit.quantumcircuit.QuantumCircuit" | ||
throw(ArgumentError("Expected a Qiskit's QuantumCircuit object, got $pyclassname")) | ||
end | ||
|
||
n = length(pyobj.qregs[0]) | ||
gen = Tenet.IndexCounter() | ||
|
||
wire = [[Tenet.nextindex!(gen)] for _ in 1:n] | ||
tensors = Tensor[] | ||
|
||
for instr in pyobj | ||
# if unassigned parameters, throw | ||
matrix = instr.matrix | ||
if pyisnone(matrix) | ||
throw(ArgumentError("Expected parameters already assigned, but got $(pyobj.params)")) | ||
end | ||
|
||
matrix = pyconvert(Array, matrix) | ||
|
||
qubits = map(x -> pyconvert(Int, x._index), instr.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!(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)]), | ||
) | ||
|
||
return Quantum(Tenet.TensorNetwork(tensors), sites) | ||
end | ||
|
||
end |