Skip to content

Commit

Permalink
Fix Python environment and integration tests for Python extensions (#300
Browse files Browse the repository at this point in the history
)

* Replace type Quantum by Circuit in Qiskit-Tenet conversion

* Fix Lane conversion for LineQubit and GridQubit types

* Fix errors in test

* Copy CondaPkg.toml to root directory for Python integration tests

* Format code

* Add dispatch function for Base.convert function from Py object to Circuit

* Experiment: check runner dir

* Add multiple dispatch method for convert for Lane

* Revert code & replace broadcast by map

* Experiment: check python integration tests

* Uncomment tests

* Experiment: switch int tests order

* Format code

* Experiment: remove CondaPkg.toml copy to the root

* Copy CondaPkg.toml to root dir for python tests
  • Loading branch information
Todorbsc authored Jan 22, 2025
1 parent 7b1be3c commit 5565285
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
4 changes: 2 additions & 2 deletions ext/TenetPythonCallExt/Cirq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function Base.convert(::Type{Circuit}, ::Val{:cirq}, pyobj::Py)

for moment in pyobj
for gate in moment.operations
gatelanes = convert.(Lane, gate.qubits)
gatesites = [Site.(gatelanes; dual=true)..., Site.(gatelanes)...]
gatelanes = map(qubit -> convert(Lane, qubit), gate.qubits)
gatesites = [Site.(gatelanes)..., Site.(gatelanes; dual=true)...]

matrix = pyconvert(Array, cirq.unitary(gate))
array = reshape(matrix, fill(2, length(gatesites))...)
Expand Down
2 changes: 1 addition & 1 deletion ext/TenetPythonCallExt/Qiskit.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Base.convert(::Type{Quantum}, ::Val{:qiskit}, pyobj::Py)
function Base.convert(::Type{Circuit}, ::Val{:qiskit}, pyobj::Py)
qiskit = pyimport("qiskit")
if !pyissubclass(pytype(pyobj), qiskit.circuit.quantumcircuit.QuantumCircuit)
throw(
Expand Down
7 changes: 7 additions & 0 deletions ext/TenetPythonCallExt/TenetPythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pyfullyqualname(pyobj) = join([pytype(pyobj).__module__, pytype(pyobj).__qualnam

Base.convert(::Type{Site}, pyobj::Py) = convert(Site, Val(Symbol(pyfullyqualname(pyobj))), pyobj)

Base.convert(::Type{Lane}, pyobj::Py) = convert(Lane, Val(Symbol(pyfullyqualname(pyobj))), pyobj)

function Base.convert(::Type{TensorNetwork}, pyobj::Py)
pymodule, _ = split(pyconvert(String, pytype(pyobj).__module__), "."; limit=2)
return convert(TensorNetwork, Val(Symbol(pymodule)), pyobj)
Expand All @@ -18,6 +20,11 @@ function Base.convert(::Type{Quantum}, pyobj::Py)
return convert(Quantum, Val(Symbol(pymodule)), pyobj)
end

function Base.convert(::Type{Circuit}, pyobj::Py)
pymodule, _ = split(pyconvert(String, pytype(pyobj).__module__), "."; limit=2)
return convert(Circuit, Val(Symbol(pymodule)), pyobj)
end

include("Qiskit.jl")
include("Quimb.jl")
include("Qibo.jl")
Expand Down
13 changes: 11 additions & 2 deletions test/integration/python/test_cirq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ CondaPkg.update()
@test issetequal(sites(circ; set=:outputs), Site.([0, 1, 2]))
@test Tenet.ntensors(circ) == 7
@test issetequal(
moments(circ), [Moment.(Ref(Lane(0)), 1:4)..., Moment.(Ref(Lane(1)), 1:4)..., Moment.(Ref(Lane(2)), 1:4)...]
moments(circ),
[
Tenet.Moment.(Ref(Lane(0)), 1:4)...,
Tenet.Moment.(Ref(Lane(1)), 1:4)...,
Tenet.Moment.(Ref(Lane(2)), 1:4)...,
],
)
end

Expand All @@ -43,7 +48,11 @@ CondaPkg.update()
@test Tenet.ntensors(circ) == 7
@test issetequal(
moments(circ),
[Moment.(Ref(Lane(0, 0)), 1:4)..., Moment.(Ref(Lane(1, 0)), 1:4)..., Moment.(Ref(Lane(2, 0)), 1:4)...],
[
Tenet.Moment.(Ref(Lane(0, 0)), 1:4)...,
Tenet.Moment.(Ref(Lane(1, 0)), 1:4)...,
Tenet.Moment.(Ref(Lane(2, 0)), 1:4)...,
],
)
end
end
3 changes: 2 additions & 1 deletion test/integration/python/test_qibo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@test issetequal(sites(circ; set=:outputs), Site.([0, 1, 2]))
@test Tenet.ntensors(circ) == 7
@test issetequal(
moments(circ), [Moment.(Ref(Lane(0)), 1:4)..., Moment.(Ref(Lane(1)), 1:4)..., Moment.(Ref(Lane(2)), 1:4)...]
moments(circ),
[Tenet.Moment.(Ref(Lane(0)), 1:4)..., Tenet.Moment.(Ref(Lane(1)), 1:4)..., Tenet.Moment.(Ref(Lane(2)), 1:4)...],
)
end
9 changes: 5 additions & 4 deletions test/integration/python/test_qiskit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

tn = convert(Circuit, circuit)
@test tn isa Circuit
@test issetequal(sites(circ; set=:inputs), Site.([1, 2, 3]; dual=true))
@test issetequal(sites(circ; set=:outputs), Site.([1, 2, 3]))
@test Tenet.ntensors(circ) == 7
@test issetequal(sites(tn; set=:inputs), Site.([0, 1, 2]; dual=true))
@test issetequal(sites(tn; set=:outputs), Site.([0, 1, 2]))
@test Tenet.ntensors(tn) == 7
@test issetequal(
moments(circ), [Moment.(Ref(Lane(1)), 1:4)..., Moment.(Ref(Lane(2)), 1:4)..., Moment.(Ref(Lane(3)), 1:4)...]
moments(tn),
[Tenet.Moment.(Ref(Lane(0)), 1:4)..., Tenet.Moment.(Ref(Lane(1)), 1:4)..., Tenet.Moment.(Ref(Lane(2)), 1:4)...],
)
end
16 changes: 9 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ end

if TENET_TEST_GROUP == "all" || TENET_TEST_GROUP == "integration"
@testset "Integration tests" verbose = true begin
@testset "Python" begin
run(`cp CondaPkg.toml ../CondaPkg.toml`)
include("integration/python/test_cirq.jl")
include("integration/python/test_quimb.jl")
include("integration/python/test_qiskit.jl")
include("integration/python/test_qibo.jl")
run(`rm ../CondaPkg.toml`)
end

include("integration/Reactant_test.jl")
include("integration/ChainRules_test.jl")
# include("integration/BlockArray_test.jl")
Expand All @@ -40,13 +49,6 @@ if TENET_TEST_GROUP == "all" || TENET_TEST_GROUP == "integration"
include("integration/ITensors_test.jl")
include("integration/ITensorNetworks_test.jl")
include("integration/YaoBlocks_test.jl")

@testset "Python" begin
include("integration/python/test_cirq.jl")
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 5565285

Please sign in to comment.