Skip to content

Commit

Permalink
Merge pull request #89 from QuanEstimation/pyext
Browse files Browse the repository at this point in the history
Fix pyext apis used by the python side
  • Loading branch information
hmyuuu authored Feb 12, 2024
2 parents 50f52c4 + df7fb1c commit 5653687
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 16 deletions.
25 changes: 14 additions & 11 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
name: CI
on:
pull_request:
branches:
- main
push:
branches:
- unit-test
tags: '*'
pull_request:
- main

concurrency:
# Skip intermediate builds: always.
# Cancel intermediate builds: only if it is a pull request build.
Expand All @@ -20,14 +22,15 @@ jobs:
version:
# - '1.8'
- '1.9'
- '1.10'
os:
- ubuntu-latest
arch:
- x64
# include:
# - os: windows-latest
# version: '1'
# arch: x64
include:
- os: windows-latest
version: '1.10'
arch: x64
# - os: macOS-latest
# version: '1'
# arch: x64
Expand All @@ -40,7 +43,7 @@ jobs:
- uses: julia-actions/cache@v1
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v2
with:
files: lcov.info
# - uses: julia-actions/julia-processcoverage@v1
# - uses: codecov/codecov-action@v2
# with:
# files: lcov.info
118 changes: 117 additions & 1 deletion ext/QuanEstimationPyExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module QuanEstimationPyExt
using PythonCall
using QuanEstimation
np = pyimport("numpy")
# QuanEstimation.Htot(H0::PyArray, Hc::PyList, ctrl) = QuanEstimation.Htot(Matrix(H0), [Matrix(hc) for hc in Hc], ctrl)

QuanEstimation.ControlOpt(ctrl::PyList, ctrl_bound::PyList, seed::Py) = QuanEstimation.ControlOpt(pyconvert(Vector{Vector{Float64}}, ctrl), pyconvert(Vector{Float64}, ctrl_bound), seed)

QuanEstimation.Lindblad(
H0::PyArray,
dH::PyList,
Expand All @@ -25,6 +26,29 @@ QuanEstimation.Lindblad(
pyconvert(Vector, γ);
kwargs...,
)

QuanEstimation.Lindblad(
H0::PyArray,
dH::PyList,
Hc::PyList,
ctrl::PyList,
ψ0::PyList,
tspan::PyArray,
decay_opt::PyList,
γ::PyList;
kwargs...,
) = QuanEstimation.Lindblad(
pyconvert(Matrix{ComplexF64}, H0),
pyconvert(Vector{Matrix}, dH),
pyconvert(Vector{Matrix}, Hc),
pyconvert(Vector{Vector{Float64}}, ctrl),
pyconvert(Vector{ComplexF64}, ψ0),
pyconvert(Vector, tspan),
pyconvert(Vector{Matrix}, decay_opt),
pyconvert(Vector, γ);
kwargs...,
)

QuanEstimation.Lindblad(
H0::PyArray,
dH::PyList,
Expand All @@ -42,4 +66,96 @@ QuanEstimation.Lindblad(
pyconvert(Vector, γ);
kwargs...,
)

QuanEstimation.Lindblad(
H0::PyArray,
dH::PyList,
ρ0::PyArray,
tspan::PyArray,
decay_opt::PyList,
γ::PyList;
kwargs...,
) = QuanEstimation.Lindblad(
pyconvert(Matrix{ComplexF64}, H0),
pyconvert(Vector{Matrix}, dH),
pyconvert(Matrix{ComplexF64}, ρ0),
pyconvert(Vector, tspan),
pyconvert(Vector{Matrix}, decay_opt),
pyconvert(Vector, γ);
kwargs...,
)

QuanEstimation.Lindblad(
H0::PyArray,
dH::PyList,
ψ0::PyList,
tspan::PyArray;
kwargs...,
) = QuanEstimation.Lindblad(
pyconvert(Matrix{ComplexF64}, H0),
pyconvert(Vector{Matrix}, dH),
pyconvert(Vector{ComplexF64}, ψ0),
pyconvert(Vector, tspan);
kwargs...,
)

QuanEstimation.Lindblad(
H0::PyArray,
dH::PyList,
ρ0::PyArray,
tspan::PyArray;
kwargs...,
) = QuanEstimation.Lindblad(
pyconvert(Matrix{ComplexF64}, H0),
pyconvert(Vector{Matrix}, dH),
pyconvert(Matrix{ComplexF64}, ρ0),
pyconvert(Vector, tspan);
kwargs...,
)

QuanEstimation.Kraus(
ρ0::PyArray,
K::PyList,
dK::PyList;
kwargs...,
) = QuanEstimation.Kraus(
pyconvert(Matrix{ComplexF64}, ρ0),
pyconvert(Vector{Matrix{ComplexF64}}, K),
pyconvert(Vector{Vector{Matrix{ComplexF64}}}, dK);
kwargs...,
)

QuanEstimation.Kraus(
ψ0::PyList,
K::PyList,
dK::PyList;
kwargs...,
) = QuanEstimation.Kraus(
pyconvert(Vector{ComplexF64}, ψ0),
pyconvert(Vector{Matrix{ComplexF64}}, K),
pyconvert(Vector{Vector{Matrix{ComplexF64}}}, dK);
kwargs...,
)

QuanEstimation.expm_py(
tspan::PyArray,
ρ0::AbstractMatrix,
H0::PyArray,
dH::PyList,
decay_opt::PyList,
γ::PyList,
Hc::PyList,
ctrl::PyList,
) = QuanEstimation.expm_py(
pyconvert(Vector, tspan),
pyconvert(Matrix{ComplexF64}, ρ0),
pyconvert(Matrix{ComplexF64}, H0),
pyconvert(Vector{Matrix{ComplexF64}}, dH),
pyconvert(Vector{Matrix{ComplexF64}}, decay_opt),
pyconvert(Vector, γ),
pyconvert(Vector{Matrix{ComplexF64}}, Hc),
pyconvert(Vector{Vector{Float64}}, ctrl),
)


end
6 changes: 3 additions & 3 deletions src/Common/Common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function initial_ctrl!(opt, ctrl0, dynamics, p_num, rng)
ctrl0 = [ctrl0[i] for i in 1:p_num]
end
for pj in 1:length(ctrl0)
dynamics[pj].data.ctrl = ctrl0[pj] isa AbstractVector ? deepcopy(ctrl0[pj]) : [[ctrl0[pj][i, j] for j in 1:ctrl_length] for i in 1:ctrl_num]
dynamics[pj].data.ctrl = deepcopy(ctrl0[pj])
end
if opt.ctrl_bound[1] == -Inf || opt.ctrl_bound[2] == Inf
for pj in (length(ctrl0)+1):p_num
Expand Down Expand Up @@ -323,7 +323,7 @@ function initial_M!(measurement0, C_all, dim, p_num, M_num, rng)
measurement0 = [measurement0[i] for i in 1:p_num]
end
for pj in 1:length(measurement0)
C_all[pj] = measurement0[pj] isa AbstractVector ? deepcopy(measurement0[pj]) : [[measurement0[pj][i,j] for j in 1:dim] for i in 1:M_num]
C_all[pj] = deepcopy(measurement0[pj])
end
for pj in (length(measurement0)+1):p_num
M_tp = [Vector{ComplexF64}(undef, dim) for i in 1:M_num]
Expand All @@ -344,7 +344,7 @@ function initial_LinearComb!(measurement0, B_all, basis_num, M_num, p_num, rng)
measurement0 = [measurement0[i] for i in 1:p_num]
end
for pj in 1:length(measurement0)
B_all[pj] = measurement0[pj] isa AbstractVector ? deepcopy(measurement0[pj]) : [[measurement0[pj][i,j] for j in 1:basis_num] for i in 1:M_num]
B_all[pj] = deepcopy(measurement0[pj])
end

for pj in (length(measurement0)+1):p_num
Expand Down
2 changes: 1 addition & 1 deletion src/Parameterization/Lindblad/LindbladDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function Htot(
Hc::V,
ctrl::Vector{R},
) where {T<:AbstractArray,V<:AbstractVector, R<:Real}
H0 + ([ctrl[i] .* [Hc[i]] for i = 1:length(ctrl)] |> sum)
H0 + ([ctrl[i] * Hc[i] for i = 1:length(ctrl)] |> sum)
end


Expand Down

0 comments on commit 5653687

Please sign in to comment.