Skip to content

Commit 5aa0ec1

Browse files
committed
Merge #21 from branch convert
2 parents a89bfc2 + b4cde6b commit 5aa0ec1

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/grad_vector.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ end
9090

9191
_exp_prop_convert_state(::GradVector) = Vector{ComplexF64}
9292

93+
94+
function Base.convert(
95+
::Type{GradVector{num_controls,T2}},
96+
Ψ̃::GradVector{num_controls,T1}
97+
) where {T1,T2,num_controls}
98+
# Used, e.g., for Cheby initialization
99+
return GradVector{num_controls,T2}(
100+
convert(T2, Ψ̃.state),
101+
T2[convert(T2, Ψ′) for Ψ′ in Ψ̃.grad_states]
102+
)
103+
end
104+
105+
93106
supports_inplace(::Type{GradVector{N,T}}) where {N,T} = supports_inplace(T)
94107

95108
supports_vector_interface(::Type{GradVector{N,T}}) where {N,T} =

test/test_interface.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using QuantumControl.Interfaces: check_generator
66
using QuantumPropagators.Interfaces:
77
check_state, check_operator, supports_matrix_interface, supports_vector_interface
88
using QuantumGradientGenerators: GradGenerator, GradVector, GradgenOperator
9-
using StaticArrays: SVector, SMatrix
9+
using StaticArrays: SVector, SMatrix, MVector
1010
using LinearAlgebra: norm, dot, mul!, I
1111

1212

@@ -31,6 +31,18 @@ end
3131

3232
@test norm(2.2 * Ψ̃ - Ψ̃ * 2.2) < 1e-14
3333

34+
Ψ̃2 = similar(Ψ̃)
35+
@test Ψ̃2 isa GradVector{2,<:MVector}
36+
37+
# We've had propagators use code like
38+
#
39+
# v0::ST = similar(Ψ::ST)
40+
#
41+
# which relies on being able to convert mutable types back to their
42+
# immutable version
43+
Ψ̃3 = convert(typeof(Ψ̃), Ψ̃2)
44+
@test typeof(Ψ̃3) == typeof(Ψ̃)
45+
3446
end
3547

3648

0 commit comments

Comments
 (0)