-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update version to 0.3.0 and add callbacks module; implement suf…
…fix utilities for integrators
- Loading branch information
1 parent
b9d562f
commit 1845e0c
Showing
7 changed files
with
354 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "QuantumCollocationCore" | ||
uuid = "2b384925-53cb-4042-a8d2-6faa627467e1" | ||
authors = ["Aaron Trowbridge <[email protected]> and contributors"] | ||
version = "0.2.1" | ||
version = "0.3.0" | ||
|
||
[deps] | ||
Einsum = "b7d42ee7-0b51-5a75-98ca-779d3107e4c0" | ||
|
@@ -30,7 +30,7 @@ Ipopt = "1.7" | |
JLD2 = "0.5" | ||
MathOptInterface = "1.35" | ||
NamedTrajectories = "0.2" | ||
PiccoloQuantumObjects = "0.2" | ||
PiccoloQuantumObjects = "0.3" | ||
Reexport = "1.2" | ||
TestItemRunner = "1.1" | ||
TestItems = "1.0" | ||
|
This file contains 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 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,173 @@ | ||
module Callbacks | ||
|
||
export best_rollout_fidelity_callback | ||
export best_unitary_rollout_fidelity_callback | ||
export trajectory_history_callback | ||
|
||
using NamedTrajectories | ||
using TestItems | ||
|
||
using PiccoloQuantumObjects | ||
|
||
using ..Problems | ||
|
||
function best_rollout_callback( | ||
prob::QuantumControlProblem, | ||
system::Union{AbstractQuantumSystem, AbstractVector{<:AbstractQuantumSystem}}, rollout_fidelity::Function | ||
) | ||
best_value = 0.0 | ||
best_trajectories = [] | ||
|
||
function callback(args...) | ||
traj = NamedTrajectory(Problems.get_datavec(prob), prob.trajectory) | ||
value = rollout_fidelity(traj, system) | ||
if value > best_value | ||
best_value = value | ||
push!(best_trajectories, traj) | ||
end | ||
return true | ||
end | ||
|
||
return callback, best_trajectories | ||
end | ||
|
||
function trajectory_history_callback(prob::QuantumControlProblem) | ||
trajectory_history = [] | ||
function callback(args...) | ||
push!(trajectory_history, NamedTrajectory(Problems.get_datavec(prob), prob.trajectory)) | ||
return true | ||
end | ||
|
||
return callback, trajectory_history | ||
end | ||
|
||
# *************************************************************************** # | ||
|
||
# TODO: figure out a way to test callback from QCC | ||
# - maybe figure out a way to set up empty ipopt Problems | ||
# - or an abstract callback struct anticipating other interfaces | ||
# - or something with this: https://pkgdocs.julialang.org/v1/creating-packages/index.html#Test-specific-dependencies | ||
|
||
# @testitem "Callback returns false early stops" begin | ||
# using MathOptInterface | ||
# const MOI = MathOptInterface | ||
# using LinearAlgebra | ||
|
||
# include("../test/test_utils.jl") | ||
|
||
# prob = smooth_quantum_state_problem() | ||
|
||
# my_callback = (kwargs...) -> false | ||
|
||
# solve!(prob, max_iter=20, callback=my_callback) | ||
|
||
# # callback forces problem to exit early as per Ipopt documentation | ||
# @test MOI.get(prob.optimizer, MOI.TerminationStatus()) == MOI.INTERRUPTED | ||
# end | ||
|
||
|
||
# @testitem "Callback can get internal history" begin | ||
# using MathOptInterface | ||
# using NamedTrajectories | ||
# const MOI = MathOptInterface | ||
# include("../test/test_utils.jl") | ||
|
||
# prob = smooth_quantum_state_problem() | ||
|
||
# callback, trajectory_history = trajectory_history_callback(prob) | ||
|
||
# solve!(prob, max_iter=20, callback=callback) | ||
# @test length(trajectory_history) == 21 | ||
# end | ||
|
||
# @testitem "Callback can get best state trajectory" begin | ||
# using MathOptInterface | ||
# using NamedTrajectories | ||
# const MOI = MathOptInterface | ||
# include("../test/test_utils.jl") | ||
|
||
# prob, system = smooth_quantum_state_problem(return_system=true) | ||
|
||
# callback, best_trajs = best_rollout_fidelity_callback(prob, system) | ||
# @test length(best_trajs) == 0 | ||
|
||
# # measure fidelity | ||
# before = rollout_fidelity(prob, system) | ||
# solve!(prob, max_iter=20, callback=callback) | ||
|
||
# # length must increase if iterations are made | ||
# @test length(best_trajs) > 0 | ||
# @test best_trajs[end] isa NamedTrajectory | ||
|
||
# # fidelity ranking | ||
# after = rollout_fidelity(prob, system) | ||
# best = rollout_fidelity(best_trajs[end], system) | ||
|
||
# @test before < after | ||
# @test before < best | ||
# @test after ≤ best | ||
# end | ||
|
||
# @testitem "Callback can get best unitary trajectory" begin | ||
# using MathOptInterface | ||
# using NamedTrajectories | ||
# const MOI = MathOptInterface | ||
# include("../test/test_utils.jl") | ||
|
||
# prob, system = smooth_unitary_problem(return_system=true) | ||
|
||
# callback, best_trajs = best_unitary_rollout_fidelity_callback(prob, system) | ||
# @test length(best_trajs) == 0 | ||
|
||
# # measure fidelity | ||
# before = unitary_rollout_fidelity(prob.trajectory, system) | ||
# solve!(prob, max_iter=20, callback=callback) | ||
|
||
# # length must increase if iterations are made | ||
# @test length(best_trajs) > 0 | ||
# @test best_trajs[end] isa NamedTrajectory | ||
|
||
# # fidelity ranking | ||
# after = unitary_rollout_fidelity(prob.trajectory, system) | ||
# best = unitary_rollout_fidelity(best_trajs[end], system) | ||
|
||
# @test before < after | ||
# @test before < best | ||
# @test after ≤ best | ||
# end | ||
|
||
# @testitem "Callback with full parameter test" begin | ||
# using MathOptInterface | ||
# using NamedTrajectories | ||
# const MOI = MathOptInterface | ||
# include("../test/test_utils.jl") | ||
|
||
# prob = smooth_quantum_state_problem() | ||
|
||
# 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 | ||
|
||
|
||
|
||
end |
This file contains 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 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
Oops, something went wrong.
1845e0c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
Release notes:
Breaking changes
1845e0c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/124933
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: