Skip to content

Commit

Permalink
Avoid unnecessary evaluation of chi
Browse files Browse the repository at this point in the history
If an explicit `chi` is given, do not evaluate one with AD. Also, do not
reconstruct a chi! routine in every Krotov iteration. Moving `make_chi`
out of the iteration loop potentially avoids compilation overhead in
each iteration.
  • Loading branch information
goerz committed Jan 19, 2024
1 parent e62314d commit ddc4298
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ function optimize_krotov(problem)
ϵ⁽ⁱ⁾ = wrk.pulses0
ϵ⁽ⁱ⁺¹⁾ = wrk.pulses1

if haskey(wrk.kwargs, :chi)
chi! = wrk.kwargs[:chi]

Check warning on line 120 in src/optimize.jl

View check run for this annotation

Codecov / codecov/patch

src/optimize.jl#L120

Added line #L120 was not covered by tests
else
# we only want to evaluate `make_chi` if `chi` is not a kwarg
J_T_func = wrk.kwargs[:J_T]
chi! = make_chi(J_T_func, wrk.objectives)
end

if skip_initial_forward_propagation
@info "Skipping initial forward propagation"
else
Expand Down Expand Up @@ -145,7 +153,7 @@ function optimize_krotov(problem)
try
while !wrk.result.converged
i = i + 1
krotov_iteration(wrk, ϵ⁽ⁱ⁾, ϵ⁽ⁱ⁺¹⁾)
krotov_iteration(wrk, ϵ⁽ⁱ⁾, ϵ⁽ⁱ⁺¹⁾, chi!)
update_result!(wrk, i)
update_hook!(wrk, i, ϵ⁽ⁱ⁺¹⁾, ϵ⁽ⁱ⁾)
info_tuple = info_hook(wrk, i, ϵ⁽ⁱ⁺¹⁾, ϵ⁽ⁱ⁾)
Expand Down Expand Up @@ -207,11 +215,10 @@ _eval_mu(μ::Operator, _...) = μ
_eval_mu::AbstractMatrix, _...) = μ


function krotov_iteration(wrk, ϵ⁽ⁱ⁾, ϵ⁽ⁱ⁺¹⁾)
function krotov_iteration(wrk, ϵ⁽ⁱ⁾, ϵ⁽ⁱ⁺¹⁾, chi!)

χ = [propagator.state for propagator in wrk.bw_propagators]
J_T_func = wrk.kwargs[:J_T]
chi! = get(wrk.kwargs, :chi, make_chi(J_T_func, wrk.objectives))
tlist = wrk.result.tlist
N_T = length(tlist) - 1
N = length(wrk.objectives)
Expand Down

0 comments on commit ddc4298

Please sign in to comment.