Skip to content

Commit dff6aa1

Browse files
Improve ensemble generation of ssesolve and change parameters on stochastic processes (#403)
1 parent 350904e commit dff6aa1

File tree

4 files changed

+157
-199
lines changed

4 files changed

+157
-199
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Fix `smesolve` for specifying initial state as density matrix. ([#395])
1414
- Add more generic solver for `steadystate_floquet` to allow more linear solvers. ([#396])
1515
- Fix time evolution output when using `saveat` keyword argument. ([#398])
16+
- Improve ensemble generation of `ssesolve` and change parameters handling on stochastic processes. ([#403])
1617

1718
## [v0.26.0]
1819
Release date: 2025-02-09
@@ -128,3 +129,4 @@ Release date: 2024-11-13
128129
[#395]: https://github.com/qutip/QuantumToolbox.jl/issues/395
129130
[#396]: https://github.com/qutip/QuantumToolbox.jl/issues/396
130131
[#398]: https://github.com/qutip/QuantumToolbox.jl/issues/398
132+
[#403]: https://github.com/qutip/QuantumToolbox.jl/issues/403

src/time_evolution/smesolve.jl

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ _smesolve_ScalarOperator(op_vec) =
1717
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
1818
sc_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
1919
e_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
20-
params::NamedTuple = NamedTuple(),
20+
params = NullParameters(),
2121
rng::AbstractRNG = default_rng(),
2222
progress_bar::Union{Val,Bool} = Val(true),
2323
kwargs...,
@@ -51,7 +51,7 @@ Above, ``\hat{C}_i`` represent the collapse operators related to pure dissipatio
5151
- `c_ops`: List of collapse operators ``\{\hat{C}_i\}_i``. It can be either a `Vector` or a `Tuple`.
5252
- `sc_ops`: List of stochastic collapse operators ``\{\hat{S}_n\}_n``. It can be either a `Vector` or a `Tuple`.
5353
- `e_ops`: List of operators for which to calculate expectation values. It can be either a `Vector` or a `Tuple`.
54-
- `params`: `NamedTuple` of parameters to pass to the solver.
54+
- `params`: `NullParameters` of parameters to pass to the solver.
5555
- `rng`: Random number generator for reproducibility.
5656
- `progress_bar`: Whether to show the progress bar. Using non-`Val` types might lead to type instabilities.
5757
- `kwargs`: The keyword arguments for the ODEProblem.
@@ -74,7 +74,7 @@ function smesolveProblem(
7474
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
7575
sc_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
7676
e_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
77-
params::NamedTuple = NamedTuple(),
77+
params = NullParameters(),
7878
rng::AbstractRNG = default_rng(),
7979
progress_bar::Union{Val,Bool} = Val(true),
8080
kwargs...,
@@ -110,16 +110,23 @@ function smesolveProblem(
110110
end
111111
D = DiffusionOperator(D_l)
112112

113-
p = (progr = progr, times = tlist, Hdims = dims, n_sc_ops = length(sc_ops), params...)
114-
115113
kwargs2 = _merge_saveat(tlist, e_ops, DEFAULT_SDE_SOLVER_OPTIONS; kwargs...)
116114
kwargs3 = _generate_se_me_kwargs(e_ops, makeVal(progress_bar), tlist, kwargs2, SaveFuncMESolve)
117115

118116
tspan = (tlist[1], tlist[end])
119117
noise =
120118
RealWienerProcess!(tlist[1], zeros(length(sc_ops)), zeros(length(sc_ops)), save_everystep = false, rng = rng)
121119
noise_rate_prototype = similar(ρ0, length(ρ0), length(sc_ops))
122-
prob = SDEProblem{true}(K, D, ρ0, tspan, p; noise_rate_prototype = noise_rate_prototype, noise = noise, kwargs3...)
120+
prob = SDEProblem{true}(
121+
K,
122+
D,
123+
ρ0,
124+
tspan,
125+
params;
126+
noise_rate_prototype = noise_rate_prototype,
127+
noise = noise,
128+
kwargs3...,
129+
)
123130

124131
return TimeEvolutionProblem(prob, tlist, dims)
125132
end
@@ -132,7 +139,7 @@ end
132139
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
133140
sc_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
134141
e_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
135-
params::NamedTuple = NamedTuple(),
142+
params = NullParameters(),
136143
rng::AbstractRNG = default_rng(),
137144
ntraj::Int = 1,
138145
ensemble_method = EnsembleThreads(),
@@ -170,11 +177,11 @@ Above, ``\hat{C}_i`` represent the collapse operators related to pure dissipatio
170177
- `c_ops`: List of collapse operators ``\{\hat{C}_i\}_i``. It can be either a `Vector` or a `Tuple`.
171178
- `sc_ops`: List of stochastic collapse operators ``\{\hat{S}_n\}_n``. It can be either a `Vector` or a `Tuple`.
172179
- `e_ops`: List of operators for which to calculate expectation values. It can be either a `Vector` or a `Tuple`.
173-
- `params`: `NamedTuple` of parameters to pass to the solver.
180+
- `params`: `NullParameters` of parameters to pass to the solver.
174181
- `rng`: Random number generator for reproducibility.
175182
- `ntraj`: Number of trajectories to use.
176183
- `ensemble_method`: Ensemble method to use. Default to `EnsembleThreads()`.
177-
- `prob_func`: Function to use for generating the ODEProblem.
184+
- `prob_func`: Function to use for generating the SDEProblem.
178185
- `output_func`: a `Tuple` containing the `Function` to use for generating the output of a single trajectory, the (optional) `ProgressBar` object, and the (optional) `RemoteChannel` object.
179186
- `progress_bar`: Whether to show the progress bar. Using non-`Val` types might lead to type instabilities.
180187
- `kwargs`: The keyword arguments for the ODEProblem.
@@ -197,7 +204,7 @@ function smesolveEnsembleProblem(
197204
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
198205
sc_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
199206
e_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
200-
params::NamedTuple = NamedTuple(),
207+
params = NullParameters(),
201208
rng::AbstractRNG = default_rng(),
202209
ntraj::Int = 1,
203210
ensemble_method = EnsembleThreads(),
@@ -207,7 +214,8 @@ function smesolveEnsembleProblem(
207214
kwargs...,
208215
) where {StateOpType<:Union{KetQuantumObject,OperatorQuantumObject}}
209216
_prob_func =
210-
isnothing(prob_func) ? _ensemble_dispatch_prob_func(rng, ntraj, tlist, _stochastic_prob_func) : prob_func
217+
isnothing(prob_func) ?
218+
_ensemble_dispatch_prob_func(rng, ntraj, tlist, _stochastic_prob_func; n_sc_ops = length(sc_ops)) : prob_func
211219
_output_func =
212220
output_func isa Nothing ?
213221
_ensemble_dispatch_output_func(ensemble_method, progress_bar, ntraj, _stochastic_output_func) : output_func
@@ -244,7 +252,7 @@ end
244252
sc_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
245253
alg::StochasticDiffEqAlgorithm = SRA1(),
246254
e_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
247-
params::NamedTuple = NamedTuple(),
255+
params = NullParameters(),
248256
rng::AbstractRNG = default_rng(),
249257
ntraj::Int = 1,
250258
ensemble_method = EnsembleThreads(),
@@ -283,11 +291,11 @@ Above, ``\hat{C}_i`` represent the collapse operators related to pure dissipatio
283291
- `sc_ops`: List of stochastic collapse operators ``\{\hat{S}_n\}_n``. It can be either a `Vector` or a `Tuple`.
284292
- `alg`: The algorithm to use for the stochastic differential equation. Default is `SRA1()`.
285293
- `e_ops`: List of operators for which to calculate expectation values. It can be either a `Vector` or a `Tuple`.
286-
- `params`: `NamedTuple` of parameters to pass to the solver.
294+
- `params`: `NullParameters` of parameters to pass to the solver.
287295
- `rng`: Random number generator for reproducibility.
288296
- `ntraj`: Number of trajectories to use.
289297
- `ensemble_method`: Ensemble method to use. Default to `EnsembleThreads()`.
290-
- `prob_func`: Function to use for generating the ODEProblem.
298+
- `prob_func`: Function to use for generating the SDEProblem.
291299
- `output_func`: a `Tuple` containing the `Function` to use for generating the output of a single trajectory, the (optional) `ProgressBar` object, and the (optional) `RemoteChannel` object.
292300
- `progress_bar`: Whether to show the progress bar. Using non-`Val` types might lead to type instabilities.
293301
- `kwargs`: The keyword arguments for the ODEProblem.
@@ -311,7 +319,7 @@ function smesolve(
311319
sc_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
312320
alg::StochasticDiffEqAlgorithm = SRA1(),
313321
e_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
314-
params::NamedTuple = NamedTuple(),
322+
params = NullParameters(),
315323
rng::AbstractRNG = default_rng(),
316324
ntraj::Int = 1,
317325
ensemble_method = EnsembleThreads(),
@@ -351,7 +359,6 @@ function smesolve(
351359
_sol_1 = sol[:, 1]
352360
_expvals_sol_1 = _se_me_sse_get_expvals(_sol_1)
353361

354-
normalize_states = Val(false)
355362
dims = ens_prob.dimensions
356363
_expvals_all = _expvals_sol_1 isa Nothing ? nothing : map(i -> _se_me_sse_get_expvals(sol[:, i]), eachindex(sol))
357364
expvals_all = _expvals_all isa Nothing ? nothing : stack(_expvals_all)

0 commit comments

Comments
 (0)