Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize mutates guess pulses #28

Closed
goerz opened this issue Mar 17, 2023 · 0 comments · Fixed by #29
Closed

optimize mutates guess pulses #28

goerz opened this issue Mar 17, 2023 · 0 comments · Fixed by #29

Comments

@goerz
Copy link
Member

goerz commented Mar 17, 2023

When the guess controls are actually pulses (vectors defined on the midpoint of the time grid), the optimization will mutate the original objective.generator. This happens because currently, disretize_on_midpoints simply returns the input vector instead of a copy of the input vector.

using Test
using QuantumControl
using LinearAlgebra
using StableRNGs
using QuantumControlTestUtils.DummyOptimization: dummy_control_problem
using QuantumControl.Controls: get_controls, discretize_on_midpoints
using QuantumControl.Functionals: J_T_re

rng = StableRNG(1244561944)

# The problem occurs when the controls are actually pulses (on the midpoints of
# the time grid), so that the optimization does not have to call
# `discretize_on_midpoints` internally
problem = dummy_control_problem(; pulses_as_controls=true)
nt = length(problem.tlist)
guess_pulse = QuantumControl.Controls.get_controls(problem.objectives)[1]
@test length(guess_pulse) == nt - 1
guess_pulse_copy = copy(QuantumControl.Controls.get_controls(problem.objectives)[1])

# Optimizing this should not modify the original generator in any way
res = optimize(problem; method=:krotov, J_T=J_T_re, iter_stop=2)
opt_control = res.optimized_controls[1]
@test length(opt_control) == nt  # optimized_controls are always *on* tlist
opt_pulse = discretize_on_midpoints(opt_control, problem.tlist)
post_pulse = QuantumControl.Controls.get_controls(problem.objectives)[1]

# * The generator should still have the exact same objects as controls
@test guess_pulse  post_pulse
# * These objects should not have been modified
@test norm(guess_pulse_copy - guess_pulse)  0.0  # FAILS
# * But the values of the optimized pulse should differ from the pulse in the
#   generator
@test norm(post_pulse - opt_pulse) > 0.1  # FAILS
goerz added a commit to JuliaQuantumControl/QuantumControlTestUtils.jl that referenced this issue Mar 17, 2023
goerz added a commit to JuliaQuantumControl/QuantumControlTestUtils.jl that referenced this issue Mar 17, 2023
goerz added a commit to JuliaQuantumControl/QuantumPropagators.jl that referenced this issue Mar 17, 2023
The `discretize` and `discretize_on_midpoints` routines should not
return the exact input array if the input is already correctly
discretized. At least, they should make a copy. Doing so avoid the
problem described in
JuliaQuantumControl/Krotov.jl#28
goerz added a commit to JuliaQuantumControl/QuantumPropagators.jl that referenced this issue Mar 17, 2023
The `discretize` and `discretize_on_midpoints` routines should not
return the exact input array if the input is already correctly
discretized. At least, they should make a copy. Doing so avoid the
problem described in
JuliaQuantumControl/Krotov.jl#28
@goerz goerz closed this as completed in f6315c0 Mar 17, 2023
goerz added a commit to JuliaQuantumControl/GRAPE.jl that referenced this issue Mar 18, 2023
Specifically, test that the optimization does not mutate guess pulses.

See JuliaQuantumControl/Krotov.jl#28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant