@@ -29,21 +29,20 @@ function LindbladJumpAffect!(integrator)
2929 random_n = internal_params. random_n
3030 jump_times = internal_params. jump_times
3131 jump_which = internal_params. jump_which
32+ traj_rng = internal_params. traj_rng
3233 ψ = integrator. u
3334
3435 @inbounds for i in eachindex (weights_mc)
3536 mul! (cache_mc, c_ops[i], ψ)
3637 weights_mc[i] = real (dot (cache_mc, cache_mc))
3738 end
3839 cumsum! (cumsum_weights_mc, weights_mc)
39- collaps_idx = getindex (1 : length (weights_mc), findfirst (> (rand () * sum (weights_mc)), cumsum_weights_mc))
40+ collaps_idx = getindex (1 : length (weights_mc), findfirst (> (rand (traj_rng ) * sum (weights_mc)), cumsum_weights_mc))
4041 mul! (cache_mc, c_ops[collaps_idx], ψ)
4142 normalize! (cache_mc)
4243 copyto! (integrator. u, cache_mc)
4344
44- # push!(jump_times, integrator.t)
45- # push!(jump_which, collaps_idx)
46- random_n[] = rand ()
45+ random_n[] = rand (traj_rng)
4746 jump_times[internal_params. jump_times_which_idx[]] = integrator. t
4847 jump_which[internal_params. jump_times_which_idx[]] = collaps_idx
4948 internal_params. jump_times_which_idx[] += 1
@@ -59,8 +58,11 @@ LindbladJumpDiscreteCondition(u, t, integrator) = real(dot(u, u)) < integrator.p
5958
6059function _mcsolve_prob_func (prob, i, repeat)
6160 internal_params = prob. p
62- seeds = internal_params. seeds
63- ! isnothing (seeds) && Random. seed! (seeds[i])
61+
62+ global_rng = internal_params. global_rng
63+ seed = internal_params. seeds[i]
64+ traj_rng = typeof (global_rng)()
65+ seed! (traj_rng, seed)
6466
6567 prm = merge (
6668 internal_params,
@@ -69,7 +71,8 @@ function _mcsolve_prob_func(prob, i, repeat)
6971 cache_mc = similar (internal_params. cache_mc),
7072 weights_mc = similar (internal_params. weights_mc),
7173 cumsum_weights_mc = similar (internal_params. weights_mc),
72- random_n = Ref (rand ()),
74+ traj_rng = traj_rng,
75+ random_n = Ref (rand (traj_rng)),
7376 progr_mc = ProgressBar (size (internal_params. expvals, 2 ), enable = false ),
7477 jump_times_which_idx = Ref (1 ),
7578 jump_times = similar (internal_params. jump_times),
122125 e_ops::Union{Nothing,AbstractVector,Tuple}=nothing,
123126 H_t::Union{Nothing,Function,TimeDependentOperatorSum}=nothing,
124127 params::NamedTuple=NamedTuple(),
128+ rng::AbstractRNG=default_rng(),
125129 jump_callback::TJC=ContinuousLindbladJumpCallback(),
126130 kwargs...)
127131
@@ -169,7 +173,7 @@ If the environmental measurements register a quantum jump, the wave function und
169173- `e_ops::Union{Nothing,AbstractVector,Tuple}`: List of operators for which to calculate expectation values.
170174- `H_t::Union{Nothing,Function,TimeDependentOperatorSum}`: Time-dependent part of the Hamiltonian.
171175- `params::NamedTuple`: Dictionary of parameters to pass to the solver.
172- - `seeds::Union{Nothing, Vector{Int}} `: List of seeds for the random number generator. Length must be equal to the number of trajectories provided .
176+ - `rng::AbstractRNG `: Random number generator for reproducibility .
173177- `jump_callback::LindbladJumpCallbackType`: The Jump Callback type: Discrete or Continuous.
174178- `kwargs...`: Additional keyword arguments to pass to the solver.
175179
@@ -194,7 +198,7 @@ function mcsolveProblem(
194198 e_ops:: Union{Nothing,AbstractVector,Tuple} = nothing ,
195199 H_t:: Union{Nothing,Function,TimeDependentOperatorSum} = nothing ,
196200 params:: NamedTuple = NamedTuple (),
197- seeds :: Union{Nothing,Vector{Int}} = nothing ,
201+ rng :: AbstractRNG = default_rng () ,
198202 jump_callback:: TJC = ContinuousLindbladJumpCallback (),
199203 kwargs... ,
200204) where {MT1<: AbstractMatrix ,TJC<: LindbladJumpCallbackType }
@@ -238,8 +242,7 @@ function mcsolveProblem(
238242 e_ops_mc = e_ops2,
239243 is_empty_e_ops_mc = is_empty_e_ops_mc,
240244 progr_mc = ProgressBar (length (t_l), enable = false ),
241- seeds = seeds,
242- random_n = Ref (rand ()),
245+ traj_rng = rng,
243246 c_ops = get_data .(c_ops),
244247 cache_mc = cache_mc,
245248 weights_mc = weights_mc,
@@ -361,7 +364,7 @@ If the environmental measurements register a quantum jump, the wave function und
361364- `e_ops::Union{Nothing,AbstractVector,Tuple}`: List of operators for which to calculate expectation values.
362365- `H_t::Union{Nothing,Function,TimeDependentOperatorSum}`: Time-dependent part of the Hamiltonian.
363366- `params::NamedTuple`: Dictionary of parameters to pass to the solver.
364- - `seeds::Union{Nothing, Vector{Int}} `: List of seeds for the random number generator. Length must be equal to the number of trajectories provided .
367+ - `rng::AbstractRNG `: Random number generator for reproducibility .
365368- `ntraj::Int`: Number of trajectories to use.
366369- `ensemble_method`: Ensemble method to use.
367370- `jump_callback::LindbladJumpCallbackType`: The Jump Callback type: Discrete or Continuous.
@@ -391,10 +394,10 @@ function mcsolveEnsembleProblem(
391394 e_ops:: Union{Nothing,AbstractVector,Tuple} = nothing ,
392395 H_t:: Union{Nothing,Function,TimeDependentOperatorSum} = nothing ,
393396 params:: NamedTuple = NamedTuple (),
397+ rng:: AbstractRNG = default_rng (),
394398 ntraj:: Int = 1 ,
395399 ensemble_method = EnsembleThreads (),
396400 jump_callback:: TJC = ContinuousLindbladJumpCallback (),
397- seeds:: Union{Nothing,Vector{Int}} = nothing ,
398401 prob_func:: Function = _mcsolve_prob_func,
399402 output_func:: Function = _mcsolve_dispatch_output_func (ensemble_method),
400403 progress_bar:: Union{Val,Bool} = Val (true ),
@@ -413,6 +416,7 @@ function mcsolveEnsembleProblem(
413416
414417 # Stop the async task if an error occurs
415418 try
419+ seeds = map (i -> rand (rng, UInt64), 1 : ntraj)
416420 prob_mc = mcsolveProblem (
417421 H,
418422 ψ0,
@@ -421,8 +425,8 @@ function mcsolveEnsembleProblem(
421425 alg = alg,
422426 e_ops = e_ops,
423427 H_t = H_t,
424- params = params,
425- seeds = seeds ,
428+ params = merge ( params, (global_rng = rng, seeds = seeds)) ,
429+ rng = rng ,
426430 jump_callback = jump_callback,
427431 kwargs... ,
428432 )
447451 e_ops::Union{Nothing,AbstractVector,Tuple} = nothing,
448452 H_t::Union{Nothing,Function,TimeDependentOperatorSum} = nothing,
449453 params::NamedTuple = NamedTuple(),
450- seeds::Union{Nothing,Vector{Int}} = nothing ,
454+ rng::AbstractRNG = default_rng() ,
451455 ntraj::Int = 1,
452456 ensemble_method = EnsembleThreads(),
453457 jump_callback::TJC = ContinuousLindbladJumpCallback(),
@@ -501,7 +505,7 @@ If the environmental measurements register a quantum jump, the wave function und
501505- `e_ops::Union{Nothing,AbstractVector,Tuple}`: List of operators for which to calculate expectation values.
502506- `H_t::Union{Nothing,Function,TimeDependentOperatorSum}`: Time-dependent part of the Hamiltonian.
503507- `params::NamedTuple`: Dictionary of parameters to pass to the solver.
504- - `seeds::Union{Nothing, Vector{Int}} `: List of seeds for the random number generator. Length must be equal to the number of trajectories provided .
508+ - `rng::AbstractRNG `: Random number generator for reproducibility .
505509- `ntraj::Int`: Number of trajectories to use.
506510- `ensemble_method`: Ensemble method to use.
507511- `jump_callback::LindbladJumpCallbackType`: The Jump Callback type: Discrete or Continuous.
@@ -532,7 +536,7 @@ function mcsolve(
532536 e_ops:: Union{Nothing,AbstractVector,Tuple} = nothing ,
533537 H_t:: Union{Nothing,Function,TimeDependentOperatorSum} = nothing ,
534538 params:: NamedTuple = NamedTuple (),
535- seeds :: Union{Nothing,Vector{Int}} = nothing ,
539+ rng :: AbstractRNG = default_rng () ,
536540 ntraj:: Int = 1 ,
537541 ensemble_method = EnsembleThreads (),
538542 jump_callback:: TJC = ContinuousLindbladJumpCallback (),
@@ -541,10 +545,6 @@ function mcsolve(
541545 progress_bar:: Union{Val,Bool} = Val (true ),
542546 kwargs... ,
543547) where {MT1<: AbstractMatrix ,T2,TJC<: LindbladJumpCallbackType }
544- if ! isnothing (seeds) && length (seeds) != ntraj
545- throw (ArgumentError (" Length of seeds must match ntraj ($ntraj ), but got $(length (seeds)) " ))
546- end
547-
548548 ens_prob_mc = mcsolveEnsembleProblem (
549549 H,
550550 ψ0,
@@ -554,7 +554,7 @@ function mcsolve(
554554 e_ops = e_ops,
555555 H_t = H_t,
556556 params = params,
557- seeds = seeds ,
557+ rng = rng ,
558558 ntraj = ntraj,
559559 ensemble_method = ensemble_method,
560560 jump_callback = jump_callback,
0 commit comments