diff --git a/src/grad_vector.jl b/src/grad_vector.jl index fe4d529..276249b 100644 --- a/src/grad_vector.jl +++ b/src/grad_vector.jl @@ -90,6 +90,19 @@ end _exp_prop_convert_state(::GradVector) = Vector{ComplexF64} + +function Base.convert( + ::Type{GradVector{num_controls,T2}}, + Ψ̃::GradVector{num_controls,T1} +) where {T1,T2,num_controls} + # Used, e.g., for Cheby initialization + return GradVector{num_controls,T2}( + convert(T2, Ψ̃.state), + T2[convert(T2, Ψ′) for Ψ′ in Ψ̃.grad_states] + ) +end + + supports_inplace(::Type{GradVector{N,T}}) where {N,T} = supports_inplace(T) supports_vector_interface(::Type{GradVector{N,T}}) where {N,T} = diff --git a/test/test_interface.jl b/test/test_interface.jl index 5f037bf..e0f60f0 100644 --- a/test/test_interface.jl +++ b/test/test_interface.jl @@ -6,7 +6,7 @@ using QuantumControl.Interfaces: check_generator using QuantumPropagators.Interfaces: check_state, check_operator, supports_matrix_interface, supports_vector_interface using QuantumGradientGenerators: GradGenerator, GradVector, GradgenOperator -using StaticArrays: SVector, SMatrix +using StaticArrays: SVector, SMatrix, MVector using LinearAlgebra: norm, dot, mul!, I @@ -31,6 +31,18 @@ end @test norm(2.2 * Ψ̃ - Ψ̃ * 2.2) < 1e-14 + Ψ̃2 = similar(Ψ̃) + @test Ψ̃2 isa GradVector{2,<:MVector} + + # We've had propagators use code like + # + # v0::ST = similar(Ψ::ST) + # + # which relies on being able to convert mutable types back to their + # immutable version + Ψ̃3 = convert(typeof(Ψ̃), Ψ̃2) + @test typeof(Ψ̃3) == typeof(Ψ̃) + end