Skip to content

Commit e410b9d

Browse files
committed
Format code and add expv coverage
1 parent dfe3f77 commit e410b9d

7 files changed

Lines changed: 157 additions & 142 deletions

ext/QuantumPropagatorsExponentialUtilitiesExt.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ LinearAlgebra.ishermitian(op::_SizedOp) = LinearAlgebra.ishermitian(op.A)
2929
LinearAlgebra.mul!(y, op::_SizedOp, x) = mul!(y, op.A, x)
3030

3131
_ensure_size_dim(A) =
32-
hasmethod(size, Tuple{typeof(A), Int}) ? A :
33-
(hasmethod(size, Tuple{typeof(A)}) ? _SizedOp(A) : A)
32+
if hasmethod(size, Tuple{typeof(A),Int})
33+
A
34+
else
35+
(hasmethod(size, Tuple{typeof(A)}) ? _SizedOp(A) : A)
36+
end
3437

3538

3639
"""Propagator for Krylov expv propagation via ExponentialUtilities (`method=ExponentialUtilities`).
@@ -85,7 +88,7 @@ expv_propagator = init_prop(
8588
)
8689
```
8790
88-
initializes an [`ExpvPropagator`](@ref).
91+
initializes an `ExpvPropagator`.
8992
9093
# Method-specific keyword arguments
9194
@@ -121,7 +124,7 @@ function init_prop(
121124
t = tlist[1]
122125
if backward
123126
n = length(tlist) - 1
124-
t = float(tlist[n + 1])
127+
t = float(tlist[n+1])
125128
end
126129
GT = typeof(generator)
127130
OT = typeof(G)
@@ -154,16 +157,18 @@ function prop_step!(propagator::ExpvPropagator)
154157
@timeit_debug propagator.timing_data "prop_step!" begin
155158
if nameof(typeof(propagator.state)) == :GradVector &&
156159
nameof(parentmodule(typeof(propagator.state))) == :QuantumGradientGenerators
157-
throw(ArgumentError(
158-
"ExponentialUtilities propagation does not support GRAPE `gradient_method=:gradgen`. " *
159-
"Use `gradient_method=:taylor` instead."
160-
))
160+
throw(
161+
ArgumentError(
162+
"ExponentialUtilities propagation does not support GRAPE `gradient_method=:gradgen`. " *
163+
"Use `gradient_method=:taylor` instead."
164+
)
165+
)
161166
end
162167
H = propagator.genop
163168
n = propagator.n
164169
tlist = getfield(propagator, :tlist)
165170
(0 < n < length(tlist)) || return nothing
166-
dt = tlist[n + 1] - tlist[n]
171+
dt = tlist[n+1] - tlist[n]
167172
if propagator.backward
168173
dt = -dt
169174
end

src/newton.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mutable struct NewtonWrk{T}
3434
restarts::Int64
3535
m_max::Int64
3636
timing_data::TimerOutput
37-
function NewtonWrk(v0::T; m_max::Int64=10, _timing_data=TimerOutput()) where {T}
37+
function NewtonWrk(v0::T; m_max::Int64 = 10, _timing_data = TimerOutput()) where {T}
3838
if m_max <= 2
3939
error("Newton propagation requires m_max > 2")
4040
end
@@ -60,8 +60,8 @@ mutable struct NewtonWrk{T}
6060
end
6161

6262

63-
lbound(array::OffsetArray, dim=1) = first(axes(array)[dim])
64-
ubound(array::OffsetArray, dim=1) = last(axes(array)[dim])
63+
lbound(array::OffsetArray, dim = 1) = first(axes(array)[dim])
64+
ubound(array::OffsetArray, dim = 1) = last(axes(array)[dim])
6565

6666

6767
function leja_radius(z)
@@ -281,9 +281,9 @@ function newton!(Ψ, H, dt, wrk; kwargs...)
281281
wrk.v,
282282
H,
283283
_dt;
284-
extended=true,
285-
norm_min=norm_min,
286-
_timing_data=wrk.timing_data
284+
extended = true,
285+
norm_min = norm_min,
286+
_timing_data = wrk.timing_data
287287
)
288288
end
289289
if m == 1 && s == 0
@@ -294,7 +294,7 @@ function newton!(Ψ, H, dt, wrk; kwargs...)
294294
break
295295
end
296296
@timeit_debug wrk.timing_data "diagonalize_hessenberg_matrix" begin
297-
ritz = diagonalize_hessenberg_matrix(Hess, m, accumulate=true)
297+
ritz = diagonalize_hessenberg_matrix(Hess, m, accumulate = true)
298298
end
299299

300300
# In the first iteration, the radius will be determined

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ using SafeTestsets
9898
include("test_grape_exponentialutilities.jl")
9999
end
100100

101-
println("\n* GRAPE ExponentialUtilities Liouvillian (test_grape_exputils_liouvillian.jl):")
101+
println(
102+
"\n* GRAPE ExponentialUtilities Liouvillian (test_grape_exputils_liouvillian.jl):"
103+
)
102104
@time @safetestset "GRAPE ExponentialUtilities Liouvillian" begin
103105
include("test_grape_exputils_liouvillian.jl")
104106
end

test/test_grape_exponentialutilities.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ using ExponentialUtilities
99
@testset "GRAPE with ExponentialUtilities" begin
1010
rng = StableRNG(20260204)
1111
problem = dummy_control_problem(
12-
N=4,
13-
n_trajectories=1,
14-
n_controls=1,
15-
n_steps=10,
16-
dt=0.1,
17-
density=1.0,
18-
hermitian=true,
19-
complex_operators=true,
20-
prop_method=ExponentialUtilities,
21-
J_T=J_T_sm,
22-
iter_stop=1,
23-
rng=rng
12+
N = 4,
13+
n_trajectories = 1,
14+
n_controls = 1,
15+
n_steps = 10,
16+
dt = 0.1,
17+
density = 1.0,
18+
hermitian = true,
19+
complex_operators = true,
20+
prop_method = ExponentialUtilities,
21+
J_T = J_T_sm,
22+
iter_stop = 1,
23+
rng = rng
2424
)
2525

26-
res = QuantumControl.optimize(problem; method=GRAPE, iter_stop=1, verbose=false)
26+
res = QuantumControl.optimize(problem; method = GRAPE, iter_stop = 1, verbose = false)
2727

2828
@test res.iter >= res.iter_start
2929
@test isfinite(res.J_T)

test/test_grape_exputils_liouvillian.jl

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ function qubit_lvn(eps)
1717

1818
H0 = 1 / 2 * sigma_z
1919

20-
return liouvillian((H0, (sigma_x, eps[1])); convention=:TDSE)
20+
return liouvillian((H0, (sigma_x, eps[1])); convention = :TDSE)
2121
end
2222

2323
@testset "GRAPE ExponentialUtilities Liouvillian" begin
2424
# Pulse Parameters (Same as unitary)
2525
tf = 20ns
2626
Nsteps = 100
27-
tgrid = collect(range(0, tf; length=Nsteps + 1))
27+
tgrid = collect(range(0, tf; length = Nsteps + 1))
2828

2929
# Target gate on the qubit subspace (embed 2×2 into 3×3)
3030
U_tgt = ComplexF64[0 1; 1 0]
@@ -38,33 +38,34 @@ end
3838
basis_tgt = [U_tgt * b * U_tgt' for b in basis]
3939

4040
eps = [
41-
0.2 * (1 + 0.05 * rand()) *
42-
QuantumControl.Shapes.flattop.(tgrid, T=tf, t_rise=0.3, func=:blackman)
43-
for _ in 1:2
41+
0.2 *
42+
(1 + 0.05 * rand()) *
43+
QuantumControl.Shapes.flattop.(tgrid, T = tf, t_rise = 0.3, func = :blackman)
44+
for _ = 1:2
4445
]
4546

4647
lvn = qubit_lvn(eps)
4748

4849
trajectories = [
4950
Trajectory(
50-
initial_state=reshape(basis[i], :),
51-
generator=lvn,
52-
target_state=reshape(basis_tgt[i], :)
51+
initial_state = reshape(basis[i], :),
52+
generator = lvn,
53+
target_state = reshape(basis_tgt[i], :)
5354
) for i in eachindex(basis)
5455
]
5556

5657
problem = ControlProblem(
57-
trajectories=trajectories,
58-
tlist=tgrid,
59-
iter_stop=10,
60-
prop_method=:expv,
61-
gradient_method=:taylor,
62-
prop_expv_kwargs=(; ishermitian=false),
63-
J_T=QuantumControl.Functionals.J_T_re
58+
trajectories = trajectories,
59+
tlist = tgrid,
60+
iter_stop = 10,
61+
prop_method = :expv,
62+
gradient_method = :taylor,
63+
prop_expv_kwargs = (; ishermitian = false),
64+
J_T = QuantumControl.Functionals.J_T_re
6465
)
6566

6667
println("Starting optimization with QuantumPropagators: ExponentialUtilities...")
67-
result = QuantumControl.optimize(problem; method=GRAPE, print_iters=true)
68+
result = QuantumControl.optimize(problem; method = GRAPE, print_iters = true)
6869
display(result)
6970

7071
@test isfinite(result.J_T)
@@ -73,7 +74,7 @@ end
7374
@testset "GRAPE ExponentialUtilities Gradgen Error" begin
7475
tf = 1.0
7576
Nsteps = 10
76-
tgrid = collect(range(0, tf; length=Nsteps + 1))
77+
tgrid = collect(range(0, tf; length = Nsteps + 1))
7778

7879
basis = Matrix{ComplexF64}[]
7980
push!(basis, [1 0; 0 0])
@@ -84,28 +85,28 @@ end
8485
U_tgt = ComplexF64[0 1; 1 0]
8586
basis_tgt = [U_tgt * b * U_tgt' for b in basis]
8687

87-
eps = [0.2 * QuantumControl.Shapes.flattop.(tgrid, T=tf, t_rise=0.3) for _ in 1:2]
88+
eps = [0.2 * QuantumControl.Shapes.flattop.(tgrid, T = tf, t_rise = 0.3) for _ = 1:2]
8889
lvn = qubit_lvn(eps)
8990

9091
trajectories = [
9192
Trajectory(
92-
initial_state=reshape(basis[i], :),
93-
generator=lvn,
94-
target_state=reshape(basis_tgt[i], :),
93+
initial_state = reshape(basis[i], :),
94+
generator = lvn,
95+
target_state = reshape(basis_tgt[i], :),
9596
) for i in eachindex(basis)
9697
]
9798

9899
problem = ControlProblem(
99-
trajectories=trajectories,
100-
tlist=tgrid,
101-
iter_stop=1,
102-
prop_method=:expv,
103-
prop_expv_kwargs=(; ishermitian=false),
104-
grad_prop_method=:expv,
105-
gradient_method=:gradgen,
106-
J_T=QuantumControl.Functionals.J_T_re
100+
trajectories = trajectories,
101+
tlist = tgrid,
102+
iter_stop = 1,
103+
prop_method = :expv,
104+
prop_expv_kwargs = (; ishermitian = false),
105+
grad_prop_method = :expv,
106+
gradient_method = :gradgen,
107+
J_T = QuantumControl.Functionals.J_T_re
107108
)
108109

109-
res = QuantumControl.optimize(problem; method=GRAPE, print_iters=false)
110+
res = QuantumControl.optimize(problem; method = GRAPE, print_iters = false)
110111
@test occursin("gradient_method=:gradgen", res.message)
111112
end

0 commit comments

Comments
 (0)