Skip to content

Commit

Permalink
Improve SciMLOperator handling of sesolveProblem (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytdHuang authored Nov 3, 2024
2 parents 54ded38 + 7a0806e commit 70209fe
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/time_evolution/sesolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function _generate_sesolve_kwargs(e_ops, progress_bar::Val{false}, tlist, kwargs
return _generate_sesolve_kwargs_with_callback(tlist, kwargs)
end

_sesolve_make_U_QobjEvo(H::QuantumObjectEvolution{<:MatrixOperator}) =
QobjEvo(MatrixOperator(-1im * H.data.A), dims = H.dims, type = Operator)
_sesolve_make_U_QobjEvo(H) = QobjEvo(H, -1im)

@doc raw"""
sesolveProblem(
H::Union{AbstractQuantumObject{DT1,OperatorQuantumObject},Tuple},
Expand Down Expand Up @@ -88,7 +92,7 @@ function sesolveProblem(

tlist = convert(Vector{_FType(ψ0)}, tlist) # Convert it to support GPUs and avoid type instabilities for OrdinaryDiffEq.jl

H_evo = QobjEvo(H, -1im) # pre-multiply by -i
H_evo = _sesolve_make_U_QobjEvo(H) # Multiply by -i
isoper(H_evo) || throw(ArgumentError("The Hamiltonian must be an Operator."))
check_dims(H_evo, ψ0)

Expand Down
4 changes: 2 additions & 2 deletions src/time_evolution/time_evolution_dynamical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,8 @@ function _DSF_mcsolve_Affect!(integrator)
@. e_ops0 = get_data(e_ops2)
@. c_ops0 = get_data(c_ops2)
H_nh = lmul!(convert(eltype(ψt), 0.5im), mapreduce(op -> op' * op, +, c_ops0))
# By doing this, we are assuming that the system is time-independent and f is a ScaledOperator
copyto!(integrator.f.f.L.A, H(op_l2, dsf_params).data - H_nh)
# By doing this, we are assuming that the system is time-independent and f is a MatrixOperator
copyto!(integrator.f.f.A, lmul!(-1im, H(op_l2, dsf_params).data - H_nh))
return u_modified!(integrator, true)
end

Expand Down
2 changes: 1 addition & 1 deletion test/core-test/time_evolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
sol_mc_string = sprint((t, s) -> show(t, "text/plain", s), sol_mc)
sol_sse_string = sprint((t, s) -> show(t, "text/plain", s), sol_sse)
@test prob_me.f.f isa MatrixOperator
@test prob_mc.f.f isa ScaledOperator # TODO: can be optimized as MatrixOperator
@test prob_mc.f.f isa MatrixOperator
@test sum(abs.(sol_mc.expect .- sol_me.expect)) / length(t_l) < 0.1
@test sum(abs.(sol_mc2.expect .- sol_me.expect)) / length(t_l) < 0.1
@test sum(abs.(vec(expect_mc_states_mean) .- vec(sol_me.expect))) / length(t_l) < 0.1
Expand Down

0 comments on commit 70209fe

Please sign in to comment.