|
| 1 | +module QuantumPropagatorsExponentialUtilitiesExt |
| 2 | + |
| 3 | +using LinearAlgebra |
| 4 | +using TimerOutputs: @timeit_debug, TimerOutput |
| 5 | +using ExponentialUtilities |
| 6 | + |
| 7 | +using QuantumPropagators: |
| 8 | + QuantumPropagators, |
| 9 | + PWCPropagator, |
| 10 | + _pwc_get_max_genop, |
| 11 | + _pwc_get_genop, |
| 12 | + _pwc_set_genop!, |
| 13 | + _pwc_set_t!, |
| 14 | + _pwc_advance_time!, |
| 15 | + _pwc_process_parameters |
| 16 | +using QuantumPropagators.Controls: get_controls |
| 17 | +using QuantumPropagators.Interfaces: supports_inplace |
| 18 | +using QuantumPropagators.Generators: ScaledOperator |
| 19 | +import QuantumPropagators: init_prop, prop_step!, set_t! |
| 20 | + |
| 21 | +struct _SizedOp{A} |
| 22 | + A::A |
| 23 | +end |
| 24 | + |
| 25 | +Base.size(op::_SizedOp) = size(op.A) |
| 26 | +Base.size(op::_SizedOp, dim::Integer) = size(op.A)[dim] |
| 27 | +Base.eltype(op::_SizedOp) = eltype(op.A) |
| 28 | +LinearAlgebra.ishermitian(op::_SizedOp) = LinearAlgebra.ishermitian(op.A) |
| 29 | +LinearAlgebra.mul!(y, op::_SizedOp, x) = mul!(y, op.A, x) |
| 30 | + |
| 31 | +_ensure_size_dim(A) = |
| 32 | + hasmethod(size, Tuple{typeof(A), Int}) ? A : |
| 33 | + (hasmethod(size, Tuple{typeof(A)}) ? _SizedOp(A) : A) |
| 34 | + |
| 35 | + |
| 36 | +"""Propagator for Krylov expv propagation via ExponentialUtilities (`method=ExponentialUtilities`). |
| 37 | +
|
| 38 | +This is a [`PWCPropagator`](@ref). |
| 39 | +""" |
| 40 | +mutable struct ExpvPropagator{GT,OT,ST} <: PWCPropagator |
| 41 | + const generator::GT |
| 42 | + state::ST |
| 43 | + t::Float64 # time at which current `state` is defined |
| 44 | + n::Int64 # index of next interval to propagate |
| 45 | + const tlist::Vector{Float64} |
| 46 | + parameters::AbstractDict |
| 47 | + controls |
| 48 | + genop::OT |
| 49 | + backward::Bool |
| 50 | + inplace::Bool |
| 51 | + convert_state::Type |
| 52 | + convert_operator::Type |
| 53 | + expv_kwargs::NamedTuple |
| 54 | + const timing_data::TimerOutput |
| 55 | +end |
| 56 | + |
| 57 | + |
| 58 | +set_t!(propagator::ExpvPropagator, t) = _pwc_set_t!(propagator, t) |
| 59 | + |
| 60 | + |
| 61 | +_expv_convert_state(state) = typeof(state) |
| 62 | +_expv_convert_operator(::Any) = Any |
| 63 | +_expv_convert_operator(::QuantumPropagators.Generator) = Matrix{ComplexF64} |
| 64 | +_expv_convert_operator(::QuantumPropagators.Operator) = Matrix{ComplexF64} |
| 65 | +_expv_convert_operator(::QuantumPropagators.ScaledOperator) = Matrix{ComplexF64} |
| 66 | + |
| 67 | + |
| 68 | +""" |
| 69 | +```julia |
| 70 | +using ExponentialUtilities |
| 71 | +
|
| 72 | +expv_propagator = init_prop( |
| 73 | + state, |
| 74 | + generator, |
| 75 | + tlist; |
| 76 | + method=ExponentialUtilities, |
| 77 | + inplace=QuantumPropagators.Interfaces.supports_inplace(state), |
| 78 | + backward=false, |
| 79 | + verbose=false, |
| 80 | + parameters=nothing, |
| 81 | + expv_kwargs=(;), |
| 82 | + convert_state=_expv_convert_state(state), |
| 83 | + convert_operator=_expv_convert_operator(generator), |
| 84 | + _... |
| 85 | +) |
| 86 | +``` |
| 87 | +
|
| 88 | +initializes an [`ExpvPropagator`](@ref). |
| 89 | +
|
| 90 | +# Method-specific keyword arguments |
| 91 | +
|
| 92 | +* `expv_kwargs`: NamedTuple of keyword arguments forwarded to |
| 93 | + `ExponentialUtilities.expv`. |
| 94 | +* `convert_state`: Type to which to temporarily convert the state before |
| 95 | + calling `expv`. |
| 96 | +* `convert_operator`: Type to which to convert the operator before calling |
| 97 | + `expv`. |
| 98 | +""" |
| 99 | +function init_prop( |
| 100 | + state, |
| 101 | + generator, |
| 102 | + tlist, |
| 103 | + method::Val{:ExponentialUtilities}; |
| 104 | + inplace = supports_inplace(state), |
| 105 | + backward = false, |
| 106 | + verbose = false, |
| 107 | + parameters = nothing, |
| 108 | + expv_kwargs = (;), |
| 109 | + convert_state = _expv_convert_state(state), |
| 110 | + convert_operator = _expv_convert_operator(generator), |
| 111 | + _... |
| 112 | +) |
| 113 | + tlist = convert(Vector{Float64}, tlist) |
| 114 | + controls = get_controls(generator) |
| 115 | + G = _pwc_get_max_genop(generator, controls, tlist) |
| 116 | + |
| 117 | + parameters = _pwc_process_parameters(parameters, controls, tlist) |
| 118 | + timing_data = TimerOutput() |
| 119 | + |
| 120 | + n = 1 |
| 121 | + t = tlist[1] |
| 122 | + if backward |
| 123 | + n = length(tlist) - 1 |
| 124 | + t = float(tlist[n + 1]) |
| 125 | + end |
| 126 | + GT = typeof(generator) |
| 127 | + OT = typeof(G) |
| 128 | + ST = typeof(state) |
| 129 | + return ExpvPropagator{GT,OT,ST}( |
| 130 | + generator, |
| 131 | + inplace ? copy(state) : state, |
| 132 | + t, |
| 133 | + n, |
| 134 | + tlist, |
| 135 | + parameters, |
| 136 | + controls, |
| 137 | + G, |
| 138 | + backward, |
| 139 | + inplace, |
| 140 | + convert_state, |
| 141 | + convert_operator, |
| 142 | + expv_kwargs, |
| 143 | + timing_data, |
| 144 | + ) |
| 145 | +end |
| 146 | + |
| 147 | + |
| 148 | +# Aliases |
| 149 | +init_prop(state, generator, tlist, method::Val{:expv}; kwargs...) = |
| 150 | + init_prop(state, generator, tlist, Val(:ExponentialUtilities); kwargs...) |
| 151 | + |
| 152 | + |
| 153 | +function prop_step!(propagator::ExpvPropagator) |
| 154 | + @timeit_debug propagator.timing_data "prop_step!" begin |
| 155 | + if nameof(typeof(propagator.state)) == :GradVector && |
| 156 | + 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 | + )) |
| 161 | + end |
| 162 | + H = propagator.genop |
| 163 | + n = propagator.n |
| 164 | + tlist = getfield(propagator, :tlist) |
| 165 | + (0 < n < length(tlist)) || return nothing |
| 166 | + dt = tlist[n + 1] - tlist[n] |
| 167 | + if propagator.backward |
| 168 | + dt = -dt |
| 169 | + end |
| 170 | + dt_expv = complex(dt) |
| 171 | + |
| 172 | + Ψ = convert(propagator.convert_state, propagator.state) |
| 173 | + if propagator.inplace |
| 174 | + if supports_inplace(propagator.genop) |
| 175 | + _pwc_set_genop!(propagator, n) |
| 176 | + H = convert(propagator.convert_operator, propagator.genop) |
| 177 | + else |
| 178 | + H = convert(propagator.convert_operator, _pwc_get_genop(propagator, n)) |
| 179 | + end |
| 180 | + H = _ensure_size_dim(H) |
| 181 | + H = ScaledOperator(-1im, H) |
| 182 | + @timeit_debug propagator.timing_data "expv" begin |
| 183 | + Ψ = ExponentialUtilities.expv(dt_expv, H, Ψ; propagator.expv_kwargs...) |
| 184 | + end |
| 185 | + copyto!(propagator.state, convert(typeof(propagator.state), Ψ)) |
| 186 | + else |
| 187 | + H = convert(propagator.convert_operator, _pwc_get_genop(propagator, n)) |
| 188 | + H = _ensure_size_dim(H) |
| 189 | + H = ScaledOperator(-1im, H) |
| 190 | + @timeit_debug propagator.timing_data "expv" begin |
| 191 | + Ψ = ExponentialUtilities.expv(dt_expv, H, Ψ; propagator.expv_kwargs...) |
| 192 | + end |
| 193 | + setfield!(propagator, :state, convert(typeof(propagator.state), Ψ)) |
| 194 | + end |
| 195 | + |
| 196 | + _pwc_advance_time!(propagator) |
| 197 | + return propagator.state |
| 198 | + end |
| 199 | +end |
| 200 | + |
| 201 | +end |
0 commit comments