Skip to content

Commit

Permalink
Merge pull request #84 from JuliaQuantumControl/timings-for-expprop
Browse files Browse the repository at this point in the history
Enable timing-benchmarks for `ExpProp`
  • Loading branch information
goerz authored Nov 13, 2024
2 parents 2873f16 + 30330a6 commit d751869
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
8 changes: 8 additions & 0 deletions docs/src/howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ Parameterized controls are [function-like objects](@extref Julia Function-like-o

It is recommended to define a parameterized control as a subtype of [`QuantumPropagators.Controls.ParameterizedFunction`](@ref). The packages [`ComponentArrays`](https://github.com/jonniedie/ComponentArrays.jl) and [`UnPack`](https://github.com/mauro3/UnPack.jl) might be useful in the implementing of a suitable type . For example,

```@meta
DocTestSetup = quote
# Ensure there's not pre-compilation from package loading during the actual doctest
using ComponentArrays
using UnPack: @unpack
end
```


```jldoctest
using ComponentArrays
Expand Down
3 changes: 3 additions & 0 deletions src/timings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Returns [`QuantumPropagators.timings_enabled()`](@ref timings_enabled), i.e.,
function enable_timings()
enable_debug_timings(@__MODULE__)
enable_debug_timings(Cheby)
enable_debug_timings(ExpProp)
enable_debug_timings(Newton)
enable_debug_timings(Arnoldi)
extmod = Base.get_extension(@__MODULE__, :QuantumPropagatorsODEExt)
Expand All @@ -52,6 +53,7 @@ enable_timings) was called, and `false` otherwise or after
function timings_enabled()
enabled = @eval getfield(@__MODULE__, :timeit_debug_enabled)()
enabled &= @eval getfield(Cheby, :timeit_debug_enabled)()
enabled &= @eval getfield(ExpProp, :timeit_debug_enabled)()
enabled &= @eval getfield(Newton, :timeit_debug_enabled)()
enabled &= @eval getfield(Arnoldi, :timeit_debug_enabled)()
extmod = Base.get_extension(@__MODULE__, :QuantumPropagatorsODEExt)
Expand Down Expand Up @@ -80,6 +82,7 @@ Returns [`QuantumPropagators.timings_enabled()`](@ref timings_enabled), i.e.,
function disable_timings()
disable_debug_timings(@__MODULE__)
disable_debug_timings(Cheby)
disable_debug_timings(ExpProp)
disable_debug_timings(Newton)
disable_debug_timings(Arnoldi)
extmod = Base.get_extension(@__MODULE__, :QuantumPropagatorsODEExt)
Expand Down
84 changes: 35 additions & 49 deletions test/test_hamiltonian.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Test
using Logging
using LinearAlgebra
using IOCapture: IOCapture

using QuantumPropagators
using QuantumPropagators: Generator, Operator
using QuantumPropagators.Interfaces: check_generator, check_state
using QuantumControlTestUtils.RandomObjects: random_matrix, random_state_vector
using QuantumControlTestUtils: QuantumTestLogger
_OT(::Generator{OT,AT}) where {OT,AT} = OT
_AT(::Generator{OT,AT}) where {OT,AT} = AT
_OT(::Operator{OT,CT}) where {OT,CT} = OT
Expand Down Expand Up @@ -165,71 +165,57 @@ end
@test contains(repl_repr, "ops::Vector{Nothing}")
@test contains(repl_repr, "amplitudes::Vector{Function}")

logger = QuantumTestLogger()
with_logger(logger) do
try
H = hamiltonian((H₀, (H₁, ϵ₁), (H₂, ϵ₂)))
catch
end
c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian((H₀, (H₁, ϵ₁), (H₂, ϵ₂)))
end
@test "Warn: Generator terms may not have been properly expanded" logger

logger = QuantumTestLogger()
with_logger(logger) do
try
H = hamiltonian([H₀, (H₁, ϵ₁), (H₂, ϵ₂)])
catch
end
@test contains(c.output, "Generator terms may not have been properly expanded")
@test c.error
@test c.value.msg == "time-dependent term must be 2-tuple"

c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian([H₀, (H₁, ϵ₁), (H₂, ϵ₂)])
end
@test "Warn: Generator terms may not have been properly expanded" logger
@test contains(c.output, "Generator terms may not have been properly expanded")
@test c.error
@test c.value.msg == "time-dependent term must be 2-tuple"

logger = QuantumTestLogger()
with_logger(logger) do
c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian((H₀, (H₁, ϵ₁)))
end
@test "Warn: Generator terms may not have been properly expanded" logger
@test contains(c.output, "Generator terms may not have been properly expanded")
@test !c.error

logger = QuantumTestLogger()
with_logger(logger) do
c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian([H₀, (H₁, ϵ₁)])
end
@test "Warn: Generator terms may not have been properly expanded" logger

logger = QuantumTestLogger()
with_logger(logger) do
try
H = hamiltonian(H₀_r, (H₁, ϵ₁, ϵ₂))
@test "raised Exception" == ""
catch exc
@test exc == ErrorException("time-dependent term must be 2-tuple")
end
@test contains(c.output, "Generator terms may not have been properly expanded")
@test !c.error

c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian(H₀_r, (H₁, ϵ₁, ϵ₂))
end
@test c.output == ""
@test c.error
@test c.value.msg == "time-dependent term must be 2-tuple"

logger = QuantumTestLogger()
with_logger(logger) do
try
H = hamiltonian(H₀_r, ϵ₁, (H₂, ϵ₂))
catch exc
end
c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian(H₀_r, ϵ₁, (H₂, ϵ₂))
end
@test "Error: Collected drift operators are of a disparate type" logger
@test contains(c.output, "Collected drift operators are of a disparate type")
@test c.error
@test c.value isa MethodError

logger = QuantumTestLogger()
with_logger(logger) do
c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian((ϵ₁, H₁), (ϵ₂, H₂))
end
@test "Warn: It looks like (op, ampl) in term are reversed" logger
@test "Warn: Collected operators are not of a concrete type" logger
@test contains(c.output, "It looks like (op, ampl) in term are reversed")
@test contains(c.output, "Collected operators are not of a concrete type")

logger = QuantumTestLogger()
with_logger(logger) do
c = IOCapture.capture(rethrow=Union{}, passthrough=false) do
H = hamiltonian(H₀, (ϵ₁, H₁), (ϵ₂, H₂))
end
@test "Warn: It looks like (op, ampl) in term are reversed" logger
@test "Warn: Collected operators are not of a concrete type: Any" logger
#@test r"Error: evaluate.* for amplitude does not return a number" ∈ logger
#@test "Warn: Collected amplitude #1 is invalid" ∈ logger
#@test "Warn: Collected amplitude #2 is invalid" ∈ logger
@test contains(c.output, "It looks like (op, ampl) in term are reversed")
@test contains(c.output, "Collected operators are not of a concrete type")
@test startswith(repr(H), "Generator(Any[ComplexF64[")
repl_repr = repr("text/plain", H; context=(:limit => true))
@test startswith(repl_repr, "Generator with 3 ops and 2 amplitudes")
Expand Down

0 comments on commit d751869

Please sign in to comment.