Skip to content

Commit

Permalink
Format .jl files
Browse files Browse the repository at this point in the history
  • Loading branch information
hmyuuu authored Jan 21, 2025
1 parent 44401a3 commit 23432d9
Show file tree
Hide file tree
Showing 57 changed files with 596 additions and 486 deletions.
1 change: 0 additions & 1 deletion examples/Bayesian_CramerRao_bounds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ f_BQCRB2 = BQCRB(scheme; btype = 2)
f_BQCRB3 = BQCRB(scheme; btype = 3)
f_QVTB = QVTB(scheme)
f_QZZB = QZZB(scheme)

4 changes: 2 additions & 2 deletions examples/Bayesian_estimation.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using QuanEstimation, Random

function H0_func(x)
return 0.5 * pi/2 * (σx() * cos(x) + σz() * sin(x))
return 0.5 * pi / 2 * (σx() * cos(x) + σz() * sin(x))
end
function dH_func(x)
return [0.5 * pi/2 * (-σx() * sin(x) + σz() * cos(x))]
return [0.5 * pi / 2 * (-σx() * sin(x) + σz() * cos(x))]
end

# initial state
Expand Down
2 changes: 1 addition & 1 deletion examples/CMopt_qubit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ scheme = GeneralScheme(; probe = rho0, param = dynamics)
opt = CMopt(ctrl_bound = [-2.0, 2.0], seed = 1234)

# run the control optimization problem
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
4 changes: 2 additions & 2 deletions examples/HCRB_NHB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ W = one(zeros(2, 2))
# time length for the evolution
tspan = range(0.0, 5.0, length = 200)
# dynamics
rho, drho = expm(tspan, rho0, H0, dH; decay=decay)
rho, drho = expm(tspan, rho0, H0, dH; decay = decay)
# calculation of the CFIM, QFIM and HCRB
f_HCRB, f_NHB = [], []
for ti = eachindex(tspan)[2:end]
for ti in eachindex(tspan)[2:end]
# HCRB
f_tp1 = HCRB(rho[ti], drho[ti], W)
append!(f_HCRB, f_tp1)
Expand Down
2 changes: 1 addition & 1 deletion examples/SCMopt_NV.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ obj = CFIM_obj()

opt = SCMopt(ctrl_bound = [-0.2, 0.2], seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/SCMopt_qubit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ obj = CFIM_obj()

opt = SCMopt(ctrl_bound = [-0.2, 0.2], seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/SCopt_NV.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ scheme = GeneralScheme(; probe = rho0, param = dynamics)

opt = SCopt(ctrl_bound = [-0.2, 0.2], seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/SCopt_qubit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ scheme = GeneralScheme(; probe = rho0, param = dynamics)

opt = SCopt(ctrl_bound = [-0.2, 0.2], seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/SMopt_NV.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ obj = CFIM_obj()

opt = SMopt(seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/SMopt_qubit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ obj = CFIM_obj()

opt = SMopt(seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/measurement_optimization_rotation_NV.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ obj = CFIM_obj()
# find the optimal rotated measurement of an input measurement
opt = MeasurementOpt(mtype = :Rotation, POVM_basis = POVM_basis, seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/state_optimization_LMG1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ scheme = GeneralScheme(; probe = psi0, param = dynamics)
# set the optimization type
opt = StateOpt(psi = psi0, seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
2 changes: 1 addition & 1 deletion examples/state_optimization_LMG2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ scheme = GeneralScheme(; probe = psi0, param = dynamics)
# set the optimization type
opt = StateOpt(psi = psi0, seed = 1234)

optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
optimize!(scheme, opt; algorithm = alg, objective = obj, savefile = false)
16 changes: 11 additions & 5 deletions lib/NVMagnetometer/src/NVMagnetometer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ struct NVMagnetometerData
decay_opt::Vector{Matrix{ComplexF64}}##decay_operator
init_state::Vector{ComplexF64}##ρ0
Hc::Vector{Matrix{ComplexF64}} ##control_Hamiltonians
ctrl::Union{Nothing, Vector{Vector{Float64}}} ##control_coefficients
tspan::Union{Vector{Float64}, StepRangeLen} ##time_span
M::Union{Nothing, Vector{Matrix{ComplexF64}}} ##meassurments
ctrl::Union{Nothing,Vector{Vector{Float64}}} ##control_coefficients
tspan::Union{Vector{Float64},StepRangeLen} ##time_span
M::Union{Nothing,Vector{Matrix{ComplexF64}}} ##meassurments
end

# Base.keys(t::NVMagnetometer{names...}) where {names...} = [names...]
Expand Down Expand Up @@ -165,15 +165,21 @@ QuanEstimationBase.CFIM(nv::NVMagnetometerScheme; kwargs...) =
CFIM(getscheme(nv.data); kwargs...)
QuanEstimationBase.HCRB(nv::NVMagnetometerScheme; kwargs...) =
HCRB(getscheme(nv.data); kwargs...)

function QuanEstimationBase.optimize!(
nv::NVMagnetometerScheme,
opt;
algorithm = autoGRAPE(),
objective = QFIM_obj(),
savefile = false,
)
QuanEstimationBase.optimize!(getscheme(nv.data), opt; algorithm = algorithm, objective = objective, savefile = savefile)
QuanEstimationBase.optimize!(
getscheme(nv.data),
opt;
algorithm = algorithm,
objective = objective,
savefile = savefile,
)
end


Expand Down
12 changes: 8 additions & 4 deletions lib/QuanEstimationBase/ext/QuanEstimationBasePyExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ function QuanEstimationBase.ode_py(
)
ctrl_num = length(Hc)
ctrl_interval = ((length(tspan) - 1) / length(ctrl[1])) |> Int
ctrl =
[repeat_copy(ctrl[i], 1, ctrl_interval) |> transpose |> vec |> Array for i = 1:ctrl_num]
ctrl = [
repeat_copy(ctrl[i], 1, ctrl_interval) |> transpose |> vec |> Array for
i = 1:ctrl_num
]
push!.(ctrl, [0.0 for i = 1:ctrl_num])
H(ctrl) = Htot(H0, Hc, ctrl)
dt = tspan[2] - tspan[1]
Expand Down Expand Up @@ -251,8 +253,10 @@ function QuanEstimationBase.ode_py(
param_num = length(dH)
ctrl_num = length(Hc)
ctrl_interval = ((length(tspan) - 1) / length(ctrl[1])) |> Int
ctrl =
[repeat_copy(ctrl[i], 1, ctrl_interval) |> transpose |> vec |> Array for i = 1:ctrl_num]
ctrl = [
repeat_copy(ctrl[i], 1, ctrl_interval) |> transpose |> vec |> Array for
i = 1:ctrl_num
]
push!.(ctrl, [0.0 for i = 1:ctrl_num])
H(ctrl) = Htot(H0, Hc, ctrl)
dt = tspan[2] - tspan[1]
Expand Down
3 changes: 1 addition & 2 deletions lib/QuanEstimationBase/src/Algorithm/DE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ function optimize!(opt::Mopt_LinearComb, alg::DE, obj, scheme, output)
p_fit, p_out = zeros(p_num), zeros(p_num)
for pj = 1:p_num
M = [
sum([populations[pj][i][j] * POVM_basis[j] for j = 1:basis_num]) for
i = 1:M_num
sum([populations[pj][i][j] * POVM_basis[j] for j = 1:basis_num]) for i = 1:M_num
]
obj_copy = set_M(obj, M)
p_out[pj], p_fit[pj] = objective(obj_copy, scheme)
Expand Down
202 changes: 101 additions & 101 deletions lib/QuanEstimationBase/src/Algorithm/GRAPE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end
function scheme_analy(scheme, dim, tnum, para_num, ctrl_num)
pdata = param_data(scheme)
rho0 = state_data(scheme)
sdata = ndims(rho0)[1]==1 ? rho0*rho0' : rho0
sdata = ndims(rho0)[1] == 1 ? rho0 * rho0' : rho0

tspan = pdata.tspan
Δt = tspan[2] - tspan[1]
Expand Down Expand Up @@ -245,7 +245,7 @@ function gradient_QFIM_analy(alg::GRAPE, obj, scheme)
dim = get_dim(scheme)
tnum = length(pdata.tspan)
para_num = length(pdata.hamiltonian.dH)
ctrl_num = get_ctrl_num(scheme)
ctrl_num = get_ctrl_num(scheme)

ρt_T, ∂ρt_T, δρt_δV, ∂xδρt_δV = scheme_analy(scheme, dim, tnum, para_num, ctrl_num)

Expand All @@ -266,43 +266,43 @@ function gradient_QFIM_analy(alg::GRAPE, obj, scheme)
pdata.ctrl[cm][tm] = pdata.ctrl[cm][tm] + alg.epsilon * δF
end
end
# elseif para_num == 2
# coeff1 = real(det(F))
# coeff2 =
# obj.W[1, 1] * F_T[2, 2] + obj.W[2, 2] * F_T[1, 1] - obj.W[1, 2] * F_T[2, 1] -
# obj.W[2, 1] * F_T[1, 2]
# cost_function =
# (abs(det(F_T)) < obj.eps ? (1.0 / obj.eps) : real(tr(obj.W * inv(F_T))))
# for cm = 1:ctrl_num
# for tm = 1:(tnum-1)
# δF_all = [[0.0 for i = 1:para_num] for j = 1:para_num]
# ∂ρt_T_δV = δρt_δV[cm][tm] |> vec2mat
# for pm = 1:para_num
# for pn = 1:para_num
# ∂xδρt_T_δV_a = ∂xδρt_δV[pm][cm][tm] |> vec2mat
# ∂xδρt_T_δV_b = ∂xδρt_δV[pn][cm][tm] |> vec2mat
# term1 = tr(∂xδρt_T_δV_a * Lx[pn])
# term2 = tr(∂xδρt_T_δV_b * Lx[pm])

# anti_commu = Lx[pm] * Lx[pn] + Lx[pn] * Lx[pm]
# term2 = tr(∂ρt_T_δV * anti_commu)
# δF_all[pm][pn] = ((2 * term1 - 0.5 * term2) |> real)
# end
# end
# item1 =
# -coeff2 * (
# F_T[2, 2] * δF_all[1][1] + F_T[1, 1] * δF_all[2][2] -
# F_T[2, 1] * δF_all[1][2] - F_T[1, 2] * δF_all[2][1]
# ) / coeff1^2
# item2 =
# (
# obj.W[1, 1] * δF_all[2][2] + obj.W[2, 2] * δF_all[1][1] -
# obj.W[1, 2] * δF_all[2][1] - obj.W[2, 1] * δF_all[1][2]
# ) / coeff1
# δF = -(item1 + item2) * cost_function^2
# pdata.ctrl[cm][tm] = pdata.ctrl[cm][tm] + alg.epsilon * δF
# end
# end
# elseif para_num == 2
# coeff1 = real(det(F))
# coeff2 =
# obj.W[1, 1] * F_T[2, 2] + obj.W[2, 2] * F_T[1, 1] - obj.W[1, 2] * F_T[2, 1] -
# obj.W[2, 1] * F_T[1, 2]
# cost_function =
# (abs(det(F_T)) < obj.eps ? (1.0 / obj.eps) : real(tr(obj.W * inv(F_T))))
# for cm = 1:ctrl_num
# for tm = 1:(tnum-1)
# δF_all = [[0.0 for i = 1:para_num] for j = 1:para_num]
# ∂ρt_T_δV = δρt_δV[cm][tm] |> vec2mat
# for pm = 1:para_num
# for pn = 1:para_num
# ∂xδρt_T_δV_a = ∂xδρt_δV[pm][cm][tm] |> vec2mat
# ∂xδρt_T_δV_b = ∂xδρt_δV[pn][cm][tm] |> vec2mat
# term1 = tr(∂xδρt_T_δV_a * Lx[pn])
# term2 = tr(∂xδρt_T_δV_b * Lx[pm])

# anti_commu = Lx[pm] * Lx[pn] + Lx[pn] * Lx[pm]
# term2 = tr(∂ρt_T_δV * anti_commu)
# δF_all[pm][pn] = ((2 * term1 - 0.5 * term2) |> real)
# end
# end
# item1 =
# -coeff2 * (
# F_T[2, 2] * δF_all[1][1] + F_T[1, 1] * δF_all[2][2] -
# F_T[2, 1] * δF_all[1][2] - F_T[1, 2] * δF_all[2][1]
# ) / coeff1^2
# item2 =
# (
# obj.W[1, 1] * δF_all[2][2] + obj.W[2, 2] * δF_all[1][1] -
# obj.W[1, 2] * δF_all[2][1] - obj.W[2, 1] * δF_all[1][2]
# ) / coeff1
# δF = -(item1 + item2) * cost_function^2
# pdata.ctrl[cm][tm] = pdata.ctrl[cm][tm] + alg.epsilon * δF
# end
# end
else
cost_function =
(abs(det(F_T)) < obj.eps ? (1.0 / obj.eps) : real(tr(obj.W * inv(F_T))))
Expand Down Expand Up @@ -365,68 +365,68 @@ function gradient_CFIM_analy(alg, obj, scheme)
pdata.ctrl[cm][tm] = pdata.ctrl[cm][tm] + alg.epsilon * δF
end
end
# elseif para_num == 2
# F_T = CFIM(ρt_T, ∂ρt_T, obj.M; eps = obj.eps)
# L1_tidle = [zeros(ComplexF64, dim, dim) for i = 1:para_num]
# L2_tidle = [[zeros(ComplexF64, dim, dim) for i = 1:para_num] for j = 1:para_num]

# for para_i = 1:para_num
# for mi = 1:dim
# p = (tr(ρt_T * obj.M[mi]) |> real)
# dp = (tr(∂ρt_T[para_i] * obj.M[mi]) |> real)
# if p > obj.eps
# L1_tidle[para_i] = L1_tidle[para_i] + dp * obj.M[mi] / p
# end
# end
# end

# for para_i = 1:para_num
# dp_a = (tr(∂ρt_T[para_i] * obj.M[mi]) |> real)
# for para_j = 1:para_num
# dp_b = (tr(∂ρt_T[para_j] * obj.M[mi]) |> real)
# for mi = 1:dim
# p = (tr(ρt_T * obj.M[mi]) |> real)
# if p > obj.eps
# L2_tidle[para_i][para_j] =
# L2_tidle[para_i][para_j] + dp_a * dp_b * obj.M[mi] / p^2
# end
# end
# end
# end
# coeff1 = real(det(F))
# coeff2 =
# obj.W[1, 1] * F_T[2, 2] + obj.W[2, 2] * F_T[1, 1] - obj.W[1, 2] * F_T[2, 1] -
# obj.W[2, 1] * F_T[1, 2]
# cost_function =
# (abs(det(F_T)) < obj.eps ? (1.0 / obj.eps) : real(tr(obj.W * inv(F_T))))
# for cm = 1:ctrl_num
# for tm = 1:(tnum-1)
# δF_all = [[0.0 for i = 1:para_num] for j = 1:para_num]
# ∂ρt_T_δV = δρt_δV[cm][tm] |> vec2mat
# for pm = 1:para_num
# for pn = 1:para_num
# ∂xδρt_T_δV_a = ∂xδρt_δV[pm][cm][tm] |> vec2mat
# ∂xδρt_T_δV_b = ∂xδρt_δV[pn][cm][tm] |> vec2mat
# term1 = tr(∂xδρt_T_δV_a * L1_tidle[pn])
# term2 = tr(∂xδρt_T_δV_b * L1_tidle[pm])
# term3 = tr(∂ρt_T_δV * L2_tidle[pm][pn])
# δF_all[pm][pn] = ((term1 + term2 - term3) |> real)
# end
# end
# item1 =
# -coeff2 * (
# F_T[2, 2] * δF_all[1][1] + F_T[1, 1] * δF_all[2][2] -
# F_T[2, 1] * δF_all[1][2] - F_T[1, 2] * δF_all[2][1]
# ) / coeff1^2
# item2 =
# (
# obj.W[1, 1] * δF_all[2][2] + obj.W[2, 2] * δF_all[1][1] -
# obj.W[1, 2] * δF_all[2][1] - obj.W[2, 1] * δF_all[1][2]
# ) / coeff1
# δF = -(item1 + item2) * cost_function^2
# pdata.ctrl[cm][tm] = pdata.ctrl[cm][tm] + alg.epsilon * δF
# end
# end
# elseif para_num == 2
# F_T = CFIM(ρt_T, ∂ρt_T, obj.M; eps = obj.eps)
# L1_tidle = [zeros(ComplexF64, dim, dim) for i = 1:para_num]
# L2_tidle = [[zeros(ComplexF64, dim, dim) for i = 1:para_num] for j = 1:para_num]

# for para_i = 1:para_num
# for mi = 1:dim
# p = (tr(ρt_T * obj.M[mi]) |> real)
# dp = (tr(∂ρt_T[para_i] * obj.M[mi]) |> real)
# if p > obj.eps
# L1_tidle[para_i] = L1_tidle[para_i] + dp * obj.M[mi] / p
# end
# end
# end

# for para_i = 1:para_num
# dp_a = (tr(∂ρt_T[para_i] * obj.M[mi]) |> real)
# for para_j = 1:para_num
# dp_b = (tr(∂ρt_T[para_j] * obj.M[mi]) |> real)
# for mi = 1:dim
# p = (tr(ρt_T * obj.M[mi]) |> real)
# if p > obj.eps
# L2_tidle[para_i][para_j] =
# L2_tidle[para_i][para_j] + dp_a * dp_b * obj.M[mi] / p^2
# end
# end
# end
# end
# coeff1 = real(det(F))
# coeff2 =
# obj.W[1, 1] * F_T[2, 2] + obj.W[2, 2] * F_T[1, 1] - obj.W[1, 2] * F_T[2, 1] -
# obj.W[2, 1] * F_T[1, 2]
# cost_function =
# (abs(det(F_T)) < obj.eps ? (1.0 / obj.eps) : real(tr(obj.W * inv(F_T))))
# for cm = 1:ctrl_num
# for tm = 1:(tnum-1)
# δF_all = [[0.0 for i = 1:para_num] for j = 1:para_num]
# ∂ρt_T_δV = δρt_δV[cm][tm] |> vec2mat
# for pm = 1:para_num
# for pn = 1:para_num
# ∂xδρt_T_δV_a = ∂xδρt_δV[pm][cm][tm] |> vec2mat
# ∂xδρt_T_δV_b = ∂xδρt_δV[pn][cm][tm] |> vec2mat
# term1 = tr(∂xδρt_T_δV_a * L1_tidle[pn])
# term2 = tr(∂xδρt_T_δV_b * L1_tidle[pm])
# term3 = tr(∂ρt_T_δV * L2_tidle[pm][pn])
# δF_all[pm][pn] = ((term1 + term2 - term3) |> real)
# end
# end
# item1 =
# -coeff2 * (
# F_T[2, 2] * δF_all[1][1] + F_T[1, 1] * δF_all[2][2] -
# F_T[2, 1] * δF_all[1][2] - F_T[1, 2] * δF_all[2][1]
# ) / coeff1^2
# item2 =
# (
# obj.W[1, 1] * δF_all[2][2] + obj.W[2, 2] * δF_all[1][1] -
# obj.W[1, 2] * δF_all[2][1] - obj.W[2, 1] * δF_all[1][2]
# ) / coeff1
# δF = -(item1 + item2) * cost_function^2
# pdata.ctrl[cm][tm] = pdata.ctrl[cm][tm] + alg.epsilon * δF
# end
# end
else
F_T = CFIM(ρt_T, ∂ρt_T, obj.M; eps = obj.eps)
L1_tidle = [zeros(ComplexF64, dim, dim) for i = 1:para_num]
Expand Down
Loading

0 comments on commit 23432d9

Please sign in to comment.