Skip to content

Commit

Permalink
fix some typo and add runtests of show for TimeEvolutionSol
Browse files Browse the repository at this point in the history
  • Loading branch information
ytdHuang committed Jun 13, 2024
1 parent d9d897f commit ede351a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/time_evolution/mesolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function mesolveProblem(
params...,
)

saveat = is_empty_e_ops ? t_l : []
saveat = is_empty_e_ops ? t_l : [t_l[end]]
default_values = (abstol = 1e-7, reltol = 1e-5, saveat = saveat)
kwargs2 = merge(default_values, kwargs)
if !isempty(e_ops) || progress_bar
Expand Down
24 changes: 12 additions & 12 deletions src/time_evolution/sesolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function sesolve_td_dudt!(du, u, p, t)
return mul!(du, H_t, u, -1im, 1)
end

"""
@doc raw"""
sesolveProblem(H::QuantumObject,
ψ0::QuantumObject,
t_l::AbstractVector;
Expand All @@ -38,23 +38,23 @@ Generates the ODEProblem for the Schrödinger time evolution of a quantum system
# Arguments
- `H::QuantumObject`: The Hamiltonian of the system.
- `ψ0::QuantumObject`: The initial state of the system.
- `t_l::AbstractVector`: The time list of the evolution.
- `alg::OrdinaryDiffEq.OrdinaryDiffEqAlgorithm`: The algorithm used for the time evolution.
- `e_ops::AbstractVector`: The list of operators to be evaluated during the evolution.
- `H_t::Union{Nothing,Function,TimeDependentOperatorSum}`: The time-dependent Hamiltonian of the system. If `nothing`, the Hamiltonian is time-independent.
- `params::NamedTuple`: The parameters of the system.
- `progress_bar::Bool`: Whether to show the progress bar.
- `kwargs...`: The keyword arguments passed to the `ODEProblem` constructor.
- `H::QuantumObject`: The Hamiltonian of the system.
- `ψ0::QuantumObject`: The initial state of the system.
- `t_l::AbstractVector`: The time list of the evolution.
- `alg::OrdinaryDiffEq.OrdinaryDiffEqAlgorithm`: The algorithm used for the time evolution.
- `e_ops::AbstractVector`: The list of operators to be evaluated during the evolution.
- `H_t::Union{Nothing,Function,TimeDependentOperatorSum}`: The time-dependent Hamiltonian of the system. If `nothing`, the Hamiltonian is time-independent.
- `params::NamedTuple`: The parameters of the system.
- `progress_bar::Bool`: Whether to show the progress bar.
- `kwargs...`: The keyword arguments passed to the `ODEProblem` constructor.
Note that the default tolerances in `kwargs` are given as `reltol=1e-5` and `abstol=1e-7`.
For more details about `alg` and extra `kwargs`, please refer to [`DifferentialEquations.jl`](https://diffeq.sciml.ai/stable/)
# Returns
- `prob`: The `ODEProblem` for the Schrödinger time evolution of the system.
- `prob`: The `ODEProblem` for the Schrödinger time evolution of the system.
"""
function sesolveProblem(
H::QuantumObject{MT1,OperatorQuantumObject},
Expand Down Expand Up @@ -90,7 +90,7 @@ function sesolveProblem(
params...,
)

saveat = is_empty_e_ops ? t_l : []
saveat = is_empty_e_ops ? t_l : [t_l[end]]
default_values = (abstol = 1e-7, reltol = 1e-5, saveat = saveat)
kwargs2 = merge(default_values, kwargs)
if !isempty(e_ops) || progress_bar
Expand Down
20 changes: 20 additions & 0 deletions test/time_evolution_and_partial_trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@
t_l = LinRange(0, 1000, 1000)
e_ops = [a_d * a]
sol = sesolve(H, psi0, t_l, e_ops = e_ops, alg = Vern7(), progress_bar = false)
sol_string = sprint((t, s) -> show(t, "text/plain", s), sol)
@test sum(abs.(sol.expect[1, :] .- sin.(η * t_l) .^ 2)) / length(t_l) < 0.1
@test ptrace(sol.states[end], 1) ptrace(ket2dm(sol.states[end]), 1)
@test ptrace(sol.states[end]', 1) ptrace(sol.states[end], 1)
@test sol_string ==
"Solution of time evolution\n" *
"(return code: $(sol.retcode))\n" *
"--------------------------\n" *
"num_states = $(length(sol.states))\n" *
"num_expect = $(size(sol.expect, 1))\n" *
"ODE alg.: $(sol.alg)\n" *
"abstol = $(sol.abstol)\n" *
"reltol = $(sol.reltol)\n"

a = destroy(N)
a_d = a'
Expand All @@ -24,7 +34,17 @@
t_l = LinRange(0, 100, 1000)
sol_me = mesolve(H, psi0, t_l, c_ops, e_ops = e_ops, alg = Vern7(), progress_bar = false)
sol_mc = mcsolve(H, psi0, t_l, c_ops, n_traj = 500, e_ops = e_ops, progress_bar = false)
sol_me_string = sprint((t, s) -> show(t, "text/plain", s), sol_me)
@test sum(abs.(sol_mc.expect .- sol_me.expect)) / length(t_l) < 0.1
@test sol_me_string ==
"Solution of time evolution\n" *
"(return code: $(sol_me.retcode))\n" *
"--------------------------\n" *
"num_states = $(length(sol_me.states))\n" *
"num_expect = $(size(sol_me.expect, 1))\n" *
"ODE alg.: $(sol_me.alg)\n" *
"abstol = $(sol_me.abstol)\n" *
"reltol = $(sol_me.reltol)\n"

sp1 = kron(sigmap(), qeye(2))
sm1 = sp1'
Expand Down

0 comments on commit ede351a

Please sign in to comment.