Skip to content

Commit e8c16eb

Browse files
committed
fix rng reproducibility
1 parent 823beb3 commit e8c16eb

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

docs/src/users_guide/time_evolution/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ We also provide the following functions for statistical analysis of multi-trajec
104104
| [`average_expect(sol)`](@ref average_expect) | Return the trajectory-averaged expectation values at each time point. |
105105
| [`std_expect(sol)`](@ref std_expect) | Return the trajectory-wise standard deviation of the expectation values at each time point. |
106106

107-
Multi-trajectory solutions also keep the random number generator (`rng`) to allow recomputing the results:
107+
Multi-trajectory solutions also keep the initial random number generator (`rng`) to allow recomputing the results:
108108

109109
```julia
110110
rng = sol.rng # this can be specified as a keyword argument (`rng = rng`) to the solvers

src/time_evolution/mcsolve.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ function mcsolveEnsembleProblem(
235235
output_func::Union{Tuple,Nothing} = nothing,
236236
kwargs...,
237237
) where {TJC<:LindbladJumpCallbackType}
238-
_prob_func = isnothing(prob_func) ? _ensemble_dispatch_prob_func(rng, ntraj, tlist, _mcsolve_prob_func) : prob_func
238+
copied_rng = copy(rng) # use the copied rng, and keep the initial one to pass it directly to solution later
239+
240+
_prob_func =
241+
isnothing(prob_func) ? _ensemble_dispatch_prob_func(copied_rng, ntraj, tlist, _mcsolve_prob_func) : prob_func
239242
_output_func =
240243
output_func isa Nothing ?
241244
_ensemble_dispatch_output_func(ensemblealg, progress_bar, ntraj, _mcsolve_output_func) : output_func
@@ -247,7 +250,7 @@ function mcsolveEnsembleProblem(
247250
c_ops;
248251
e_ops = e_ops,
249252
params = params,
250-
rng = rng,
253+
rng = copied_rng,
251254
jump_callback = jump_callback,
252255
kwargs...,
253256
)

src/time_evolution/smesolve.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,12 @@ function smesolveEnsembleProblem(
239239
store_measurement::Union{Val,Bool} = Val(false),
240240
kwargs...,
241241
) where {StateOpType<:Union{Ket,Operator,OperatorKet}}
242+
copied_rng = copy(rng) # use the copied rng, and keep the initial one to pass it directly to solution later
243+
242244
_prob_func =
243245
isnothing(prob_func) ?
244246
_ensemble_dispatch_prob_func(
245-
rng,
247+
copied_rng,
246248
ntraj,
247249
tlist,
248250
_stochastic_prob_func;
@@ -261,7 +263,7 @@ function smesolveEnsembleProblem(
261263
sc_ops;
262264
e_ops = e_ops,
263265
params = params,
264-
rng = rng,
266+
rng = copied_rng,
265267
progress_bar = Val(false),
266268
store_measurement = makeVal(store_measurement),
267269
kwargs...,

src/time_evolution/ssesolve.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,12 @@ function ssesolveEnsembleProblem(
233233
store_measurement::Union{Val,Bool} = Val(false),
234234
kwargs...,
235235
)
236+
copied_rng = copy(rng) # use the copied rng, and keep the initial one to pass it directly to solution later
237+
236238
_prob_func =
237239
isnothing(prob_func) ?
238240
_ensemble_dispatch_prob_func(
239-
rng,
241+
copied_rng,
240242
ntraj,
241243
tlist,
242244
_stochastic_prob_func;
@@ -254,7 +256,7 @@ function ssesolveEnsembleProblem(
254256
sc_ops;
255257
e_ops = e_ops,
256258
params = params,
257-
rng = rng,
259+
rng = copied_rng,
258260
progress_bar = Val(false),
259261
store_measurement = makeVal(store_measurement),
260262
kwargs...,

src/time_evolution/time_evolution.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ A structure storing the results and some information from solving quantum trajec
103103
- `times::AbstractVector`: The time list of the evolution.
104104
- `states::Vector{Vector{QuantumObject}}`: The list of result states in each trajectory and each time point in `times`.
105105
- `expect::Union{AbstractArray,Nothing}`: The expectation values corresponding to each trajectory and each time point in `times`.
106-
- `rng::AbstractRNG`: Random number generator for reproducibility.
106+
- `rng::AbstractRNG`: Initial random number generator for reproducibility.
107107
- `col_times::Vector{Vector{Real}}`: The time records of every quantum jump occurred in each trajectory.
108108
- `col_which::Vector{Vector{Int}}`: The indices of which collapse operator was responsible for each quantum jump in `col_times`.
109109
- `converged::Bool`: Whether the solution is converged or not.
@@ -176,7 +176,7 @@ A structure storing the results and some information from solving trajectories o
176176
- `times::AbstractVector`: The time list of the evolution.
177177
- `states::Vector{Vector{QuantumObject}}`: The list of result states in each trajectory and each time point in `times`.
178178
- `expect::Union{AbstractArray,Nothing}`: The expectation values corresponding to each trajectory and each time point in `times`.
179-
- `rng::AbstractRNG`: Random number generator for reproducibility.
179+
- `rng::AbstractRNG`: Initial random number generator for reproducibility.
180180
- `measurement::Union{AbstractArray,Nothing}`: Measurements for each trajectories and stochastic collapse operators (`sc_ops`).
181181
- `converged::Bool`: Whether the solution is converged or not.
182182
- `alg`: The algorithm which is used during the solving process.

0 commit comments

Comments
 (0)