Skip to content

Commit 856448a

Browse files
committed
Merge #17 from branch operator-interface
2 parents 27cc659 + 88da7d0 commit 856448a

File tree

8 files changed

+93
-70
lines changed

8 files changed

+93
-70
lines changed

docs/make.jl

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,34 @@ links = InterLinks(
2121
"QuantumControl" => "https://juliaquantumcontrol.github.io/QuantumControl.jl/$DEV_OR_STABLE",
2222
)
2323

24+
fallbacks = ExternalFallbacks(
25+
"QuantumPropagators.Interfaces.supports_inplace" => "@extref QuantumPropagators :jl:function:`QuantumPropagators.Interfaces.supports_inplace`",
26+
automatic = false,
27+
)
28+
2429
println("Starting makedocs")
2530

2631
makedocs(;
27-
plugins=[links],
28-
authors=AUTHORS,
29-
sitename="QuantumGradientGenerators.jl",
30-
doctest=false,
31-
format=Documenter.HTML(;
32-
prettyurls=true,
33-
canonical="https://juliaquantumcontrol.github.io/QuantumGradientGenerators.jl",
34-
assets=[
32+
plugins = [links, fallbacks],
33+
authors = AUTHORS,
34+
sitename = "QuantumGradientGenerators.jl",
35+
doctest = false,
36+
format = Documenter.HTML(;
37+
prettyurls = true,
38+
canonical = "https://juliaquantumcontrol.github.io/QuantumGradientGenerators.jl",
39+
assets = [
3540
asset(
3641
"https://juliaquantumcontrol.github.io/QuantumControl.jl/dev/assets/topbar/topbar.css"
3742
),
3843
asset(
3944
"https://juliaquantumcontrol.github.io/QuantumControl.jl/dev/assets/topbar/topbar.js"
4045
),
4146
],
42-
footer="[$NAME.jl]($GITHUB) v$VERSION docs powered by [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl)."
47+
footer = "[$NAME.jl]($GITHUB) v$VERSION docs powered by [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl)."
4348
),
44-
pages=["Home" => "index.md", "API" => "api.md",]
49+
pages = ["Home" => "index.md", "API" => "api.md",]
4550
)
4651

4752
println("Finished makedocs")
4853

49-
deploydocs(; repo="github.com/JuliaQuantumControl/QuantumGradientGenerators.jl",)
54+
deploydocs(; repo = "github.com/JuliaQuantumControl/QuantumGradientGenerators.jl",)

src/evaluate.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ function evaluate(O::GradgenOperator, args...; kwargs...)
66
end
77

88

9-
function evaluate!(G::GradgenOperator, gradgen::GradGenerator, args...; vals_dict=IdDict())
9+
function evaluate!(
10+
G::GradgenOperator,
11+
gradgen::GradGenerator,
12+
args...;
13+
vals_dict = IdDict()
14+
)
1015
evaluate!(G.G, gradgen.G, args...; vals_dict)
1116
for (i, control) in enumerate(gradgen.controls)
1217
μ = gradgen.control_derivs[i]
@@ -18,7 +23,7 @@ function evaluate!(G::GradgenOperator, gradgen::GradGenerator, args...; vals_dic
1823
end
1924

2025

21-
function evaluate(gradgen::GradGenerator, args...; vals_dict=IdDict())
26+
function evaluate(gradgen::GradGenerator, args...; vals_dict = IdDict())
2227
G = evaluate(gradgen.G, args...; vals_dict)
2328
control_deriv_ops = [evaluate(μ, args...; vals_dict) for μ gradgen.control_derivs]
2429
num_controls = length(control_deriv_ops)

src/gradgen_operator.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function get_controls(O1::GradgenOperator)
3030
end
3131

3232

33-
function random_state(H::GradgenOperator; rng=GLOBAL_RNG, _...)
33+
function random_state(H::GradgenOperator; rng = GLOBAL_RNG, _...)
3434
state = random_state(H.G; rng)
3535
num_controls = length(H.control_deriv_ops)
3636
grad_states = [random_state(H.G; rng) for i eachindex(H.control_deriv_ops)]

src/linalg.jl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,16 @@ function Base.length(Ψ::GradVector)
7575
end
7676

7777

78-
function Base.size(G::GradgenOperator)
79-
return Base.size(G.G)
78+
function Base.size(O::GradgenOperator{num_controls,GT,CGT}) where {num_controls,GT,CGT}
79+
return (num_controls + 1) .* size(O.G)
80+
end
81+
82+
83+
function Base.size(
84+
O::GradgenOperator{num_controls,GT,CGT},
85+
dim::Integer
86+
) where {num_controls,GT,CGT}
87+
return (num_controls + 1) * size(O.G, dim)
8088
end
8189

8290

@@ -88,6 +96,9 @@ function Base.similar(G::GradgenOperator{num_controls,GT,CGT}) where {num_contro
8896
return GradgenOperator{num_controls,GT,CGT}(similar(G.G), similar(G.control_deriv_ops))
8997
end
9098

99+
function Base.eltype(O::GradgenOperator{num_controls,GT,CGT}) where {num_controls,GT,CGT}
100+
return promote_type(eltype(GT), eltype(CGT))
101+
end
91102

92103
function Base.copyto!(dest::GradgenOperator, src::GradgenOperator)
93104
copyto!(dest.G, src.G)
@@ -161,23 +172,23 @@ function *(
161172
end
162173

163174

164-
@inline function convert_gradgen_to_dense(G)
175+
@inline function convert_gradgen_to_dense(G::GradGenerator)
165176
N = size(G.G)[1]
166177
L = length(G.control_derivs)
167178
G_full = zeros(eltype(G.G), N * (L + 1), N * (L + 1))
168179
convert_gradgen_to_dense!(G_full, G)
169180
end
170181

171182

172-
@inline function convert_gradgen_to_dense!(G_full, G)
183+
@inline function convert_gradgen_to_dense!(G_full, G::GradGenerator)
173184
N = size(G.G)[1]
174185
L = length(G.control_derivs)
175-
@inbounds for i = 1:L+1
176-
G_full[(i-1)*N+1:i*N, (i-1)*N+1:i*N] .= G.G
186+
@inbounds for i = 1:(L+1)
187+
G_full[((i-1)*N+1):(i*N), ((i-1)*N+1):(i*N)] .= G.G
177188
end
178189
# Set the control-derivatives in the last (block-)column
179190
@inbounds for i = 1:L
180-
G_full[(i-1)*N+1:i*N, L*N+1:(L+1)*N] .= G.control_derivs[i]
191+
G_full[((i-1)*N+1):(i*N), (L*N+1):((L+1)*N)] .= G.control_derivs[i]
181192
end
182193
return G_full
183194
end
@@ -195,9 +206,9 @@ end
195206
N = length.state)
196207
L = length.grad_states)
197208
@inbounds for i = 1:L
198-
Ψ_full[(i-1)*N+1:i*N] .= Ψ.grad_states[i]
209+
Ψ_full[((i-1)*N+1):(i*N)] .= Ψ.grad_states[i]
199210
end
200-
@inbounds Ψ_full[L*N+1:(L+1)*N] .= Ψ.state
211+
@inbounds Ψ_full[(L*N+1):((L+1)*N)] .= Ψ.state
201212
return Ψ_full
202213
end
203214

@@ -206,9 +217,9 @@ end
206217
N = length.state)
207218
L = length.grad_states)
208219
@inbounds for i = 1:L
209-
Ψ.grad_states[i] .= Ψ_full[(i-1)*N+1:i*N]
220+
Ψ.grad_states[i] .= Ψ_full[((i-1)*N+1):(i*N)]
210221
end
211-
@inbounds Ψ.state .= Ψ_full[L*N+1:(L+1)*N]
222+
@inbounds Ψ.state .= Ψ_full[(L*N+1):((L+1)*N)]
212223
return Ψ
213224
end
214225

@@ -225,8 +236,8 @@ function Base.convert(
225236
L = num_controls
226237
N = length(vec) ÷ (L + 1) # dimension of state
227238
@assert length(vec) == (L + 1) * N
228-
grad_states = [convert(T, vec[(i-1)*N+1:i*N]) for i = 1:L]
229-
state = convert(T, vec[L*N+1:(L+1)*N])
239+
grad_states = [convert(T, vec[((i-1)*N+1):(i*N)]) for i = 1:L]
240+
state = convert(T, vec[(L*N+1):((L+1)*N)])
230241
return GradVector{num_controls,T}(state, grad_states)
231242
end
232243

@@ -236,8 +247,7 @@ function Base.Array{T}(G::GradgenOperator) where {T}
236247
𝟘 = zeros(T, N, M)
237248
μ = G.control_deriv_ops
238249
block_rows = [
239-
hcat([𝟘 for j = 1:i-1]..., Array{T}(G.G), [𝟘 for j = i+1:L]..., Array{T}(μ[i]))
240-
for i = 1:L
250+
hcat([𝟘 for j = 1:(i-1)]..., Array{T}(G.G), [𝟘 for j = (i+1):L]..., Array{T}(μ[i])) for i = 1:L
241251
]
242252
last_block_row = hcat([𝟘 for j = 1:L]..., Array{T}(G.G))
243253
return Base.Array{T}(vcat(block_rows..., last_block_row))

test/clean.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
Clean up build/doc/testing artifacts. Restore to clean checkout state
55
(distclean)
66
"""
7-
function clean(; distclean=false, _exit=true)
7+
function clean(; distclean = false, _exit = true)
88

99
_glob(folder, ending) =
10-
[name for name in readdir(folder; join=true) if (name |> endswith(ending))]
10+
[name for name in readdir(folder; join = true) if (name |> endswith(ending))]
1111
_exists(name) = isfile(name) || isdir(name)
1212
_push!(lst, name) = _exists(name) && push!(lst, name)
1313

@@ -33,12 +33,12 @@ function clean(; distclean=false, _exit=true)
3333

3434
for name in CLEAN
3535
@info "rm $name"
36-
rm(name, force=true, recursive=true)
36+
rm(name, force = true, recursive = true)
3737
end
3838
if distclean
3939
for name in DISTCLEAN
4040
@info "rm $name"
41-
rm(name, force=true, recursive=true)
41+
rm(name, force = true, recursive = true)
4242
end
4343
if _exit
4444
@info "Exiting"
@@ -48,4 +48,4 @@ function clean(; distclean=false, _exit=true)
4848

4949
end
5050

51-
distclean() = clean(distclean=true)
51+
distclean() = clean(distclean = true)

test/test_gradgen.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,20 @@ using QuantumPropagators.Controls: evaluate
8181
Ψ̃.state
8282
]
8383

84+
@test size(G̃) == size(G̃_full)
85+
@test eltype(G̃) == eltype(G̃_full)
86+
8487
# proper initialization? grad_states should be zero
8588
@test norm(Ψ̃_full) == norm(Ψ̃.state) == norm(Ψ)
8689

8790
Ψ̃_out_full = exp(-𝕚 * G̃_full * dt) * Ψ̃_full
8891
# propagation correct?
89-
@test norm(Ψ̃_out_full[2N+1:3N] - Û_Ψ) < 1e-10
92+
@test norm(Ψ̃_out_full[(2N+1):3N] - Û_Ψ) < 1e-10
9093

9194
# do we get the same results as from newton?
92-
@test norm(Ψ̃_out_full[2N+1:3N] - Ψ̃_out.state) < 1e-10
95+
@test norm(Ψ̃_out_full[(2N+1):3N] - Ψ̃_out.state) < 1e-10
9396
@test norm(Ψ̃_out_full[1:N] - Ψ̃_out.grad_states[1]) < 1e-10
94-
@test norm(Ψ̃_out_full[N+1:2N] - Ψ̃_out.grad_states[2]) < 1e-10
97+
@test norm(Ψ̃_out_full[(N+1):2N] - Ψ̃_out.grad_states[2]) < 1e-10
9598

9699
###########################################################################
97100
# Test custom expprop
@@ -108,12 +111,12 @@ using QuantumPropagators.Controls: evaluate
108111

109112
Ψ̃_out_full = exp(-𝕚 * G̃_full * dt) * Ψ̃_full
110113
# propagation correct?
111-
@test norm(Ψ̃_out_full[2N+1:3N] - Û_Ψ) < 1e-10
114+
@test norm(Ψ̃_out_full[(2N+1):3N] - Û_Ψ) < 1e-10
112115

113116
# do we get the same results as from newton?
114-
@test norm(Ψ̃_out_full[2N+1:3N] - Ψ̃_out.state) < 1e-10
117+
@test norm(Ψ̃_out_full[(2N+1):3N] - Ψ̃_out.state) < 1e-10
115118
@test norm(Ψ̃_out_full[1:N] - Ψ̃_out.grad_states[1]) < 1e-10
116-
@test norm(Ψ̃_out_full[N+1:2N] - Ψ̃_out.grad_states[2]) < 1e-10
119+
@test norm(Ψ̃_out_full[(N+1):2N] - Ψ̃_out.grad_states[2]) < 1e-10
117120

118121
###########################################################################
119122
# Test standard expprop
@@ -122,10 +125,10 @@ using QuantumPropagators.Controls: evaluate
122125
Ψ̃,
123126
G̃,
124127
[0, dt];
125-
method=:expprop,
126-
inplace=true,
127-
convert_state=Vector{ComplexF64},
128-
convert_operator=Matrix{ComplexF64}
128+
method = :expprop,
129+
inplace = true,
130+
convert_state = Vector{ComplexF64},
131+
convert_operator = Matrix{ComplexF64}
129132
)
130133
Ψ̃_out_exp = prop_step!(propagator)
131134
@test norm(Ψ̃_out_exp - Ψ̃_out) < 1e-11
@@ -159,14 +162,14 @@ using QuantumPropagators.Controls: evaluate
159162
Ψ̃_out_full2 = exp(-𝕚 * G̃_full2 * dt) * Ψ̃_full2
160163

161164
# propagation correct?
162-
@test norm(Ψ̃_out_full1[N+1:2N] - Û_Ψ) < 1e-12
163-
@test norm(Ψ̃_out_full2[N+1:2N] - Û_Ψ) < 1e-12
165+
@test norm(Ψ̃_out_full1[(N+1):2N] - Û_Ψ) < 1e-12
166+
@test norm(Ψ̃_out_full2[(N+1):2N] - Û_Ψ) < 1e-12
164167

165168
# do we get the same results as with the combined grad-gen?
166169
@test norm(Ψ̃_out_full1[1:N] - Ψ̃_out_full[1:N]) < 1e-12
167-
@test norm(Ψ̃_out_full2[1:N] - Ψ̃_out_full[N+1:2N]) < 1e-12
168-
@test norm(Ψ̃_out_full1[N+1:2N] - Ψ̃_out_full[2N+1:3N]) < 1e-12
169-
@test norm(Ψ̃_out_full2[N+1:2N] - Ψ̃_out_full[2N+1:3N]) < 1e-12
170+
@test norm(Ψ̃_out_full2[1:N] - Ψ̃_out_full[(N+1):2N]) < 1e-12
171+
@test norm(Ψ̃_out_full1[(N+1):2N] - Ψ̃_out_full[(2N+1):3N]) < 1e-12
172+
@test norm(Ψ̃_out_full2[(N+1):2N] - Ψ̃_out_full[(2N+1):3N]) < 1e-12
170173

171174
###########################################################################
172175
# Compare against Zygote

test/test_interface.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,42 @@ end
3636
@testset "GradGenerator Interface" begin
3737

3838
N = 10
39-
Ĥ₀ = random_matrix(N, hermitian=true)
40-
Ĥ₁ = random_matrix(N, hermitian=true)
41-
Ĥ₂ = random_matrix(N, hermitian=true)
39+
Ĥ₀ = random_matrix(N, hermitian = true)
40+
Ĥ₁ = random_matrix(N, hermitian = true)
41+
Ĥ₂ = random_matrix(N, hermitian = true)
4242
ϵ₁(t) = 1.0
4343
ϵ₂(t) = 1.0
4444
Ĥ_of_t = hamiltonian(Ĥ₀, (Ĥ₁, ϵ₁), (Ĥ₂, ϵ₂))
4545

46-
tlist = collect(range(0, 10; length=101))
46+
tlist = collect(range(0, 10; length = 101))
4747

4848
G̃_of_t = GradGenerator(Ĥ_of_t)
4949

5050
Ψ = random_state_vector(N)
5151
Ψ̃ = GradVector(Ψ, length(get_controls(G̃_of_t)))
5252

53-
@test check_generator(G̃_of_t; state=Ψ̃, tlist, for_gradient_optimization=false)
53+
@test check_generator(G̃_of_t; state = Ψ̃, tlist, for_gradient_optimization = false)
5454

5555
end
5656

5757

5858
@testset "GradGenerator Interface (Static)" begin
5959

6060
N = 10
61-
Ĥ₀ = SMatrix{N,N,ComplexF64}(random_matrix(N, hermitian=true))
62-
Ĥ₁ = SMatrix{N,N,ComplexF64}(random_matrix(N, hermitian=true))
63-
Ĥ₂ = SMatrix{N,N,ComplexF64}(random_matrix(N, hermitian=true))
61+
Ĥ₀ = SMatrix{N,N,ComplexF64}(random_matrix(N, hermitian = true))
62+
Ĥ₁ = SMatrix{N,N,ComplexF64}(random_matrix(N, hermitian = true))
63+
Ĥ₂ = SMatrix{N,N,ComplexF64}(random_matrix(N, hermitian = true))
6464
ϵ₁(t) = 1.0
6565
ϵ₂(t) = 1.0
6666
Ĥ_of_t = hamiltonian(Ĥ₀, (Ĥ₁, ϵ₁), (Ĥ₂, ϵ₂))
6767

68-
tlist = collect(range(0, 10; length=101))
68+
tlist = collect(range(0, 10; length = 101))
6969

7070
G̃_of_t = GradGenerator(Ĥ_of_t)
7171

7272
Ψ = SVector{N,ComplexF64}(random_state_vector(N))
7373
Ψ̃ = GradVector(Ψ, length(get_controls(G̃_of_t)))
7474

75-
@test check_generator(G̃_of_t; state=Ψ̃, tlist, for_gradient_optimization=false)
75+
@test check_generator(G̃_of_t; state = Ψ̃, tlist, for_gradient_optimization = false)
7676

7777
end

0 commit comments

Comments
 (0)