-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch empty controls in optimization
Trying to run `optimize` when there were no controls in the problem would produce a very non-obvious MethodError. Since this is an easy mistake to make, we can catch this situation with a proper error message.
- Loading branch information
Showing
3 changed files
with
41 additions
and
0 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
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,33 @@ | ||
using Test | ||
using StableRNGs | ||
using QuantumControl: hamiltonian, optimize, ControlProblem, Objective | ||
using QuantumControl.Controls: get_controls | ||
using QuantumControlTestUtils.RandomObjects: random_matrix, random_state_vector | ||
|
||
@testset "empty optimization" begin | ||
|
||
# Test that trying to run an optimization without any controls produces a | ||
# meaningful error message | ||
|
||
rng = StableRNG(2264511904) | ||
|
||
N = 10 | ||
H = random_matrix(N; rng) | ||
objectives = [ | ||
Objective(; | ||
initial_state=random_state_vector(N; rng), | ||
generator=H, | ||
target_state=random_state_vector(N; rng) | ||
) | ||
] | ||
|
||
@test length(get_controls(objectives)) == 0 | ||
|
||
tlist = collect(range(0; length=1001, step=1.0)) | ||
|
||
problem = ControlProblem(; objectives, tlist, pulse_options=Dict()) | ||
|
||
msg = "no controls in objectives: cannot optimize" | ||
@test_throws ErrorException(msg) optimize(problem; method=:krotov) | ||
|
||
end |