-
Notifications
You must be signed in to change notification settings - Fork 15
Feature: Ipopt callbacks #157
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7f9c195
feat: add boilerplate and inner function call
jack-champagne 7f35b40
add callbacks tests
jack-champagne f25a057
add tmp callback changes
jack-champagne 1a0e8fb
Merge branch 'main' into feature/ipopt-callbacks
jack-champagne f88cde8
add docstrings
jack-champagne 81cbad7
add generic callback factory
jack-champagne 6b4a6cc
Merge commit '18d52526351f71093d7bca9b111779ac00e77bb4' into feature/…
andgoldschmidt 6428fa6
add get best iterate to callbacks lib
jack-champagne 70d5fa1
fix renamed callback
jack-champagne 972aea9
move callback tests, rename callbacks
andgoldschmidt 587c051
Merge branch 'feature/ipopt-callbacks' of github.com:kestrelquantum/Q…
andgoldschmidt bc406f3
local project toml changes
jack-champagne d267338
remove pulse animation pngs
jack-champagne ddb9f0e
add NamedTrajectories to docs compat
jack-champagne e5e64c6
fix typo NM version
jack-champagne 1701f19
roll back callback fidelity
andgoldschmidt e8dabda
Merge branch 'feature/ipopt-callbacks' of github.com:kestrelquantum/Q…
andgoldschmidt 0ecef44
rm manifest
jack-champagne 1c4d641
test unitary fid callback
andgoldschmidt f712b40
fix doctests
jack-champagne e4f7cbc
fixup docs
jack-champagne a1586aa
callbacks version bump
jack-champagne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # # IpOpt Solver Callbacks | ||
|
|
||
| # This page describes the callback functions that can be used with the IpOpt solver (in the future, may describe more general callback behavior). | ||
|
|
||
| # ## Callbacks | ||
|
|
||
| # By default, IpOpt callbacks are called at each optimization step with the following signature: | ||
| using QuantumCollocation | ||
| using NamedTrajectories | ||
|
|
||
| import ..QuantumStateSmoothPulseProblem | ||
| import ..Callbacks | ||
|
|
||
| function get_history_callback( | ||
| alg_mod::Cint, | ||
| iter_count::Cint, | ||
| obj_value::Float64, | ||
| inf_pr::Float64, | ||
| inf_du::Float64, | ||
| mu::Float64, | ||
| d_norm::Float64, | ||
| regularization_size::Float64, | ||
| alpha_du::Float64, | ||
| alpha_pr::Float64, | ||
| ls_trials::Cint, | ||
| ) | ||
| return true | ||
| end | ||
|
|
||
| # This gives the user access to some of the optimization state internals at each iteration. | ||
| # A callback function with any subset of these arguments can be passed into the `solve!` function via the `callback` keyword argument see below. | ||
| # ```@docs | ||
| # ProblemSolvers.solve!(prob::QuantumControlProblem; callback=nothing) | ||
| # ``` | ||
|
|
||
| # The callback function can be used to monitor the optimization progress, save intermediate results, or modify the optimization process. | ||
| # For example, the following callback function saves the optimization trajectory at each iteration: | ||
|
|
||
| trajectory_history = [] | ||
| function get_history_callback( | ||
| kwargs... | ||
| ) | ||
| push!(trajectory_history, QuantumCollocation.Problems.get_datavec(prob)) | ||
| return true | ||
| end | ||
|
|
||
| # The callback function can also be used to stop the optimization early by returning `false`. The following callback when passed to `solve!` will stop the optimization after the first iteration: | ||
| my_callback = (kwargs...) -> false | ||
|
|
||
| T = 50 | ||
| Δt = 0.2 | ||
| sys = QuantumSystem(0.1 * GATES[:Z], [GATES[:X], GATES[:Y]]) | ||
| ψ_init = Vector{ComplexF64}([1.0, 0.0]) | ||
| ψ_target = Vector{ComplexF64}([0.0, 1.0]) | ||
|
|
||
| # Single initial and target states | ||
| # -------------------------------- | ||
| prob = QuantumStateSmoothPulseProblem( | ||
| sys, ψ_init, ψ_target, T, Δt; | ||
| ipopt_options=IpoptOptions(print_level=1), | ||
| piccolo_options=PiccoloOptions(verbose=false) | ||
| ) | ||
|
|
||
| trajectory_history = [] | ||
| # using the get_history_callback function to save the optimization trajectory at each iteration into the trajectory_history | ||
| solve!(prob, max_iter=20, callback=get_history_callback) | ||
|
|
||
| for (iter, traj) in enumerate(trajectory_history) | ||
| # get the length of the trajectory history depending on length and left pad the index with leading zeros | ||
| str_index = lpad(iter, length(string(length(trajectory_history))), "0") | ||
| # plot the trajectory but on fixed xaxis and yaxis | ||
| plot("./iteration-$str_index-trajectory.png", NamedTrajectory(traj, prob.trajectory), [:ψ̃1, :a], xlims=(-Δt, (T+5)*Δt), plot_ylims=(ψ̃1 = (-2, 2), a = (-1.1, 1.1))) | ||
| end | ||
|
|
||
| # Using a callback to get the best trajectory from all the optimization iterations | ||
| T = 50 | ||
| Δt = 0.2 | ||
| sys = QuantumSystem(0.1 * GATES[:Z], [GATES[:X], GATES[:Y]]) | ||
| ψ_init = Vector{ComplexF64}([0.0, 1.0]) | ||
| ψ_target = Vector{ComplexF64}([1.0, 0.0]) | ||
|
|
||
| # Single initial and target states | ||
| # -------------------------------- | ||
| prob = QuantumStateSmoothPulseProblem( | ||
| sys, ψ_init, ψ_target, T, Δt; | ||
| ipopt_options=IpoptOptions(print_level=1), | ||
| piccolo_options=PiccoloOptions(verbose=false) | ||
| ) | ||
|
|
||
| (best_traj_callback, best_traj) = make_save_best_trajectory_callback(prob, prob.trajectory) | ||
| solve!(prob, max_iter=20, callback=best_traj_callback) | ||
| # fidelity of the last iterate | ||
| @show fidelity(prob) | ||
| # fidelity of the best iterate | ||
| @show fidelity(best_traj) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+322 KB
.../src/assets/min_time_traj_animation_frames/iteration-01-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+216 KB
.../src/assets/min_time_traj_animation_frames/iteration-02-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+189 KB
.../src/assets/min_time_traj_animation_frames/iteration-03-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+154 KB
.../src/assets/min_time_traj_animation_frames/iteration-04-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+143 KB
.../src/assets/min_time_traj_animation_frames/iteration-05-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+148 KB
.../src/assets/min_time_traj_animation_frames/iteration-06-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-07-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-08-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+139 KB
.../src/assets/min_time_traj_animation_frames/iteration-09-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+142 KB
.../src/assets/min_time_traj_animation_frames/iteration-10-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+143 KB
.../src/assets/min_time_traj_animation_frames/iteration-11-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+143 KB
.../src/assets/min_time_traj_animation_frames/iteration-12-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+143 KB
.../src/assets/min_time_traj_animation_frames/iteration-13-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-14-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+142 KB
.../src/assets/min_time_traj_animation_frames/iteration-15-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-16-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-17-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-18-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-19-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-20-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-21-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-22-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
.../src/assets/min_time_traj_animation_frames/iteration-23-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+142 KB
.../src/assets/min_time_traj_animation_frames/iteration-24-min-time-trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+144 KB
.../src/assets/min_time_traj_animation_frames/iteration-25-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+169 KB
.../src/assets/min_time_traj_animation_frames/iteration-26-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+163 KB
.../src/assets/min_time_traj_animation_frames/iteration-27-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+166 KB
.../src/assets/min_time_traj_animation_frames/iteration-28-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+169 KB
.../src/assets/min_time_traj_animation_frames/iteration-29-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+175 KB
.../src/assets/min_time_traj_animation_frames/iteration-30-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-31-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-32-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-33-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-34-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-35-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-36-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-37-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-38-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-39-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-40-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-41-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-42-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-43-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-44-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-45-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-46-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-47-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-48-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-49-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-50-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-51-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-52-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-53-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-54-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-55-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-56-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-57-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-58-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-59-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-60-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-61-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+177 KB
.../src/assets/min_time_traj_animation_frames/iteration-62-min-time-trajectory.png
Oops, something went wrong.
Binary file added
BIN
+4.61 MB
docs/src/assets/min_time_traj_animation_frames/min_time_smooth_pulse_animation.gif
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| module Callbacks | ||
|
|
||
| export make_save_best_trajectory_callback | ||
|
|
||
| using NamedTrajectories | ||
|
|
||
| import ..QuantumControlProblem | ||
| import ..Problems: get_datavec | ||
|
|
||
| function make_save_best_trajectory_callback(prob::QuantumControlProblem, traj::NamedTrajectory) | ||
| best_traj = NamedTrajectory(get_datavec(prob), prob.trajectory) | ||
| best_fidelity = 0.0 | ||
|
|
||
| function save_best_trajectory_callback(prob::QuantumControlProblem) | ||
| fidelity = prob.objective.fidelity(prob) | ||
| if fidelity > best_fidelity | ||
| best_fidelity = fidelity | ||
| copy!(best_traj, prob.trajectory) | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| return save_best_trajectory_callback, best_traj | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| """ | ||
| Testing callback features | ||
|
|
||
| - callback gives early stopping | ||
| - callback gives trajectory data | ||
| - callback example test with full argument list | ||
| """ | ||
|
|
||
| @testitem "Callback returns false early stops" begin | ||
| using MathOptInterface | ||
| const MOI = MathOptInterface | ||
|
|
||
| T = 50 | ||
| Δt = 0.2 | ||
| sys = QuantumSystem(0.1 * GATES[:Z], [GATES[:X], GATES[:Y]]) | ||
| ψ_init = [1.0, 0.0] | ||
| ψ_target = [0.0, 1.0] | ||
|
|
||
| # Single initial and target states | ||
| # -------------------------------- | ||
| prob = QuantumStateSmoothPulseProblem( | ||
| sys, ψ_init, ψ_target, T, Δt; | ||
| ipopt_options=IpoptOptions(print_level=1), | ||
| piccolo_options=PiccoloOptions(verbose=false) | ||
| ) | ||
|
|
||
| my_callback = (kwargs...) -> false | ||
|
|
||
| initial = fidelity(prob) | ||
| solve!(prob, max_iter=20, callback=my_callback) | ||
| final = fidelity(prob) | ||
|
|
||
| # callback forces problem to exit early as per Ipopt documentation | ||
| @test MOI.get(prob.optimizer, MOI.TerminationStatus()) == MOI.INTERRUPTED | ||
| @test initial ≈ final atol=1e-2 | ||
| end | ||
|
|
||
|
|
||
| @testitem "Callback can get internal history" begin | ||
| using MathOptInterface | ||
| using NamedTrajectories | ||
| const MOI = MathOptInterface | ||
|
|
||
| T = 50 | ||
| Δt = 0.2 | ||
| sys = QuantumSystem(0.1 * GATES[:Z], [GATES[:X], GATES[:Y]]) | ||
| ψ_init = [1.0, 0.0] | ||
| ψ_target = [0.0, 1.0] | ||
|
|
||
| # Single initial and target states | ||
| # -------------------------------- | ||
| prob = QuantumStateSmoothPulseProblem( | ||
| sys, ψ_init, ψ_target, T, Δt; | ||
| ipopt_options=IpoptOptions(print_level=1), | ||
| piccolo_options=PiccoloOptions(verbose=false) | ||
| ) | ||
|
|
||
| trajectory_history = [] | ||
| function get_history_callback( | ||
| kwargs... | ||
| ) | ||
| push!(trajectory_history, QuantumCollocation.Problems.get_datavec(prob)) | ||
| return true | ||
| end | ||
|
|
||
| initial = fidelity(prob) | ||
| solve!(prob, max_iter=20, callback=get_history_callback) | ||
|
|
||
| # for (iter, traj) in enumerate(trajectory_history) | ||
| # # change next line to plot the trajectory but on fixed xaxis and yaxis | ||
| # # get the length of the trajectory history depending on length and left pad the index with leading zeros | ||
| # str_index = lpad(iter, length(string(length(trajectory_history))), "0") | ||
| # plot("./test_animation_frames/iteration-$str_index-trajectory.png", NamedTrajectory(traj, prob.trajectory), [:ψ̃1, :a], xlims=(-Δt, (T+5)*Δt), plot_ylims=(ψ̃1 = (-2, 2), a = (-1.1, 1.1))) | ||
| # end | ||
| @test length(trajectory_history) == 21 | ||
| end | ||
|
|
||
| @testitem "Callback with full parameter test" begin | ||
| using MathOptInterface | ||
| using NamedTrajectories | ||
| const MOI = MathOptInterface | ||
|
|
||
| T = 50 | ||
| Δt = 0.2 | ||
| sys = QuantumSystem(0.1 * GATES[:Z], [GATES[:X], GATES[:Y]]) | ||
| ψ_init = [1.0, 0.0] | ||
| ψ_target = [0.0, 1.0] | ||
|
|
||
| # Single initial and target states | ||
| # -------------------------------- | ||
| prob = QuantumStateSmoothPulseProblem( | ||
| sys, ψ_init, ψ_target, T, Δt; | ||
| ipopt_options=IpoptOptions(print_level=1), | ||
| piccolo_options=PiccoloOptions(verbose=false) | ||
| ) | ||
|
|
||
| obj_vals = [] | ||
| function get_history_callback( | ||
| alg_mod::Cint, | ||
| iter_count::Cint, | ||
| obj_value::Float64, | ||
| inf_pr::Float64, | ||
| inf_du::Float64, | ||
| mu::Float64, | ||
| d_norm::Float64, | ||
| regularization_size::Float64, | ||
| alpha_du::Float64, | ||
| alpha_pr::Float64, | ||
| ls_trials::Cint, | ||
| ) | ||
| push!(obj_vals, obj_value) | ||
| return iter_count < 3 | ||
| end | ||
|
|
||
| solve!(prob, max_iter=20, callback=get_history_callback) | ||
|
|
||
| @test MOI.get(prob.optimizer, MOI.TerminationStatus()) == MOI.INTERRUPTED | ||
| @test length(obj_vals) == 4 # problem init, iter 1, iter 2, iter 3 (terminate) | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.