|
1 | 1 | using Test |
2 | 2 |
|
3 | 3 | using QuantumControlTestUtils.RandomObjects: random_state_vector, random_dynamic_generator |
4 | | -using QuantumPropagators: init_prop |
| 4 | +using QuantumPropagators: QuantumPropagators, init_prop |
5 | 5 | using QuantumPropagators.Interfaces: check_propagator |
6 | 6 | using StableRNGs: StableRNG |
7 | 7 | using OrdinaryDiffEq: OrdinaryDiffEq |
@@ -241,3 +241,123 @@ end |
241 | 241 | @test check_propagator(propagator) |
242 | 242 |
|
243 | 243 | end |
| 244 | + |
| 245 | + |
| 246 | +# Minimal struct for testing the base set_t! (all built-in propagators override it) |
| 247 | +mutable struct _SimpleProp |
| 248 | + tlist::Vector{Float64} |
| 249 | + t::Float64 |
| 250 | +end |
| 251 | + |
| 252 | +# Minimal AbstractPropagator that ignores piecewise/pwc, for testing enforcement errors |
| 253 | +struct _NonPiecewiseProp <: QuantumPropagators.AbstractPropagator end |
| 254 | + |
| 255 | +function QuantumPropagators.init_prop(state, generator, tlist, ::Val{:_non_piecewise}; _...) |
| 256 | + return _NonPiecewiseProp() |
| 257 | +end |
| 258 | + |
| 259 | + |
| 260 | +@testset "Propagator property access" begin |
| 261 | + |
| 262 | + N = 10 |
| 263 | + tlist = collect(range(0, 10, length = 101)) |
| 264 | + rng = StableRNG(677918059) |
| 265 | + Ψ = random_state_vector(N; rng) |
| 266 | + Ĥ = random_dynamic_generator(N, tlist; rng) |
| 267 | + propagator = init_prop(Ψ, Ĥ, tlist; method = :cheby) |
| 268 | + |
| 269 | + @test_throws ErrorException propagator.generator |
| 270 | + @test_throws ErrorException (propagator.state = Ψ) |
| 271 | + @test_throws ErrorException (propagator.tlist = tlist) |
| 272 | + @test_throws ErrorException (propagator.t = 0.0) |
| 273 | + @test_throws ErrorException (propagator.generator = Ĥ) |
| 274 | + |
| 275 | + new_params = copy(propagator.parameters) |
| 276 | + propagator.parameters = new_params |
| 277 | + @test propagator.parameters === new_params |
| 278 | + |
| 279 | + pub = propertynames(propagator) |
| 280 | + @test pub == (:state, :tlist, :t, :parameters, :backward, :inplace) |
| 281 | + |
| 282 | + priv = propertynames(propagator, true) |
| 283 | + @test length(priv) >= length(pub) |
| 284 | + @test all(p ∈ priv for p ∈ pub) |
| 285 | + |
| 286 | +end |
| 287 | + |
| 288 | + |
| 289 | +@testset "init_prop piecewise enforcement" begin |
| 290 | + |
| 291 | + N = 10 |
| 292 | + tlist = collect(range(0, 10, length = 101)) |
| 293 | + rng = StableRNG(677918060) |
| 294 | + Ψ = random_state_vector(N; rng) |
| 295 | + Ĥ = random_dynamic_generator(N, tlist; rng) |
| 296 | + |
| 297 | + # piecewise=true with a PWC method works (ChebyPropagator isa PiecewisePropagator) |
| 298 | + propagator = init_prop(Ψ, Ĥ, tlist; method = :cheby, piecewise = true) |
| 299 | + @test propagator isa QuantumPropagators.PiecewisePropagator |
| 300 | + |
| 301 | + # piecewise=true with a non-piecewise method throws |
| 302 | + @test_throws ErrorException init_prop( |
| 303 | + Ψ, |
| 304 | + Ĥ, |
| 305 | + tlist; |
| 306 | + method = :_non_piecewise, |
| 307 | + piecewise = true |
| 308 | + ) |
| 309 | + |
| 310 | + # pwc=true with a non-piecewise-constant method throws |
| 311 | + @test_throws ErrorException init_prop( |
| 312 | + Ψ, |
| 313 | + Ĥ, |
| 314 | + tlist; |
| 315 | + method = :_non_piecewise, |
| 316 | + pwc = true |
| 317 | + ) |
| 318 | + |
| 319 | +end |
| 320 | + |
| 321 | + |
| 322 | +@testset "init_prop unknown method" begin |
| 323 | + |
| 324 | + tlist = collect(range(0, 10, length = 101)) |
| 325 | + rng = StableRNG(677918061) |
| 326 | + Ψ = random_state_vector(10; rng) |
| 327 | + Ĥ = random_dynamic_generator(10, tlist; rng) |
| 328 | + |
| 329 | + @test_throws ArgumentError init_prop(Ψ, Ĥ, tlist; method = 42) |
| 330 | + |
| 331 | +end |
| 332 | + |
| 333 | + |
| 334 | +@testset "set_t! interior and snapping" begin |
| 335 | + |
| 336 | + tlist = collect(range(0, 10, length = 101)) |
| 337 | + prop = _SimpleProp(tlist, tlist[1]) |
| 338 | + |
| 339 | + # Interior time value (the t <= tlist[1] and t >= tlist[end] branches are |
| 340 | + # covered by reinit_prop! calls, but the searchsortedfirst branch is not) |
| 341 | + QuantumPropagators.set_t!(prop, tlist[10]) |
| 342 | + @test prop.t == tlist[10] |
| 343 | + |
| 344 | + # A value not on the grid triggers the snapping warning |
| 345 | + t_off = (tlist[10] + tlist[11]) / 2 |
| 346 | + @test_logs (:warn, r"Snapping") QuantumPropagators.set_t!(prop, t_off) |
| 347 | + @test prop.t == tlist[11] |
| 348 | + |
| 349 | +end |
| 350 | + |
| 351 | + |
| 352 | +@testset "_get_uniform_dt non-uniform" begin |
| 353 | + |
| 354 | + tlist_nonuniform = [0.0, 1.0, 3.0, 6.0] |
| 355 | + |
| 356 | + @test QuantumPropagators._get_uniform_dt(tlist_nonuniform) === nothing |
| 357 | + |
| 358 | + @test_logs (:warn, r"Non-uniform") QuantumPropagators._get_uniform_dt( |
| 359 | + tlist_nonuniform; |
| 360 | + warn = true |
| 361 | + ) |
| 362 | + |
| 363 | +end |
0 commit comments