Skip to content

Commit d051a2c

Browse files
committed
Separate stochastic base functionality from timeevolution
1 parent d73f8a4 commit d051a2c

File tree

6 files changed

+101
-106
lines changed

6 files changed

+101
-106
lines changed

src/QuantumOptics.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ export qfunc, wigner, coherentspinstate, qfuncsu2, wignersu2, ylm,
1515

1616
include("phasespace.jl")
1717
module timeevolution
18-
using QuantumOpticsBase
19-
using QuantumOpticsBase: check_samebases, check_multiplicable
2018
export diagonaljumps, @skiptimechecks
2119

22-
function recast! end
23-
2420
include("timeevolution_base.jl")
2521
include("master.jl")
2622
include("schroedinger.jl")
@@ -32,9 +28,7 @@ include("timecorrelations.jl")
3228
include("spectralanalysis.jl")
3329
include("semiclassical.jl")
3430
module stochastic
35-
using QuantumOpticsBase
36-
import ..timeevolution: recast!
37-
include("timeevolution_base.jl")
31+
include("stochastic_base.jl")
3832
include("stochastic_definitions.jl")
3933
include("stochastic_schroedinger.jl")
4034
include("stochastic_master.jl")

src/mcwf.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using Random, LinearAlgebra
2-
import OrdinaryDiffEq
32

43
# TODO: Remove imports
5-
import DiffEqCallbacks, RecursiveArrayTools.copyat_or_push!
4+
import RecursiveArrayTools.copyat_or_push!
65

76
"""
87
mcwf_h(tspan, rho0, Hnh, J; <keyword arguments>)

src/stochastic_base.jl

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using QuantumOpticsBase
2+
using QuantumOpticsBase: check_samebases, check_multiplicable
3+
import ..timeevolution: recast!, QO_CHECKS, DiffArray, pure_inference
4+
5+
import DiffEqCallbacks, StochasticDiffEq, OrdinaryDiffEq
6+
7+
"""
8+
integrate_stoch(tspan::Vector{Float64}, df::Function, dg::Vector{Function}, x0::Vector{ComplexF64},
9+
state::T, dstate::T, fout::Function; kwargs...)
10+
11+
Integrate using StochasticDiffEq
12+
"""
13+
function integrate_stoch(tspan::Vector{Float64}, df::Function, dg::Function, x0::Vector{ComplexF64},
14+
state::T, dstate::T, fout::Function, n::Int;
15+
save_everystep = false, callback=nothing,
16+
alg::StochasticDiffEq.StochasticDiffEqAlgorithm=StochasticDiffEq.EM(),
17+
noise_rate_prototype = nothing,
18+
noise_prototype_classical = nothing,
19+
noise=nothing,
20+
ncb=nothing,
21+
kwargs...) where T
22+
23+
function df_(dx::Vector{ComplexF64}, x::Vector{ComplexF64}, p, t)
24+
recast!(x, state)
25+
recast!(dx, dstate)
26+
df(t, state, dstate)
27+
recast!(dstate, dx)
28+
end
29+
30+
function dg_(dx::Union{Vector{ComplexF64}, Array{ComplexF64, 2}},
31+
x::Vector{ComplexF64}, p, t)
32+
recast!(x, state)
33+
dg(dx, t, state, dstate, n)
34+
end
35+
36+
function fout_(x::Vector{ComplexF64}, t::Float64, integrator)
37+
recast!(x, state)
38+
fout(t, state)
39+
end
40+
41+
nc = isa(noise_prototype_classical, Nothing) ? 0 : size(noise_prototype_classical)[2]
42+
if isa(noise, Nothing) && n > 0
43+
if n + nc == 1
44+
noise_ = StochasticDiffEq.RealWienerProcess(0.0, 0.0)
45+
else
46+
noise_ = StochasticDiffEq.RealWienerProcess!(0.0, zeros(n + nc))
47+
end
48+
else
49+
noise_ = noise
50+
end
51+
if isa(noise_rate_prototype, Nothing)
52+
if n > 1 || nc > 1 || (n > 0 && nc > 0)
53+
noise_rate_prototype = zeros(ComplexF64, length(x0), n + nc)
54+
end
55+
end
56+
57+
out_type = pure_inference(fout, Tuple{eltype(tspan),typeof(state)})
58+
59+
out = DiffEqCallbacks.SavedValues(Float64,out_type)
60+
61+
scb = DiffEqCallbacks.SavingCallback(fout_,out,saveat=tspan,
62+
save_everystep=save_everystep,
63+
save_start = false)
64+
65+
full_cb = OrdinaryDiffEq.CallbackSet(callback, ncb, scb)
66+
67+
prob = StochasticDiffEq.SDEProblem{true}(df_, dg_, x0,(tspan[1],tspan[end]),
68+
noise=noise_,
69+
noise_rate_prototype=noise_rate_prototype)
70+
71+
sol = StochasticDiffEq.solve(
72+
prob,
73+
alg;
74+
reltol = 1.0e-3,
75+
abstol = 1.0e-3,
76+
save_everystep = false, save_start = false,
77+
save_end = false,
78+
callback=full_cb, kwargs...)
79+
80+
out.t,out.saveval
81+
end
82+
83+
"""
84+
integrate_stoch
85+
86+
Define fout if it was omitted.
87+
"""
88+
function integrate_stoch(tspan::Vector{Float64}, df::Function, dg::Function, x0::Vector{ComplexF64},
89+
state::T, dstate::T, ::Nothing, n::Int; kwargs...) where T
90+
function fout(t::Float64, state::T)
91+
copy(state)
92+
end
93+
integrate_stoch(tspan, df, dg, x0, state, dstate, fout, n; kwargs...)
94+
end

src/stochastic_master.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ...timeevolution: dmaster_h, dmaster_nh, dmaster_h_dynamic, check_master
22

33
const DecayRates = Union{Vector{Float64}, Matrix{Float64}, Nothing}
4-
const DiffArray = Union{Vector{ComplexF64}, Array{ComplexF64, 2}}
54

65
"""
76
stochastic.master(tspan, rho0, H, J, C; <keyword arguments>)

src/stochastic_schroedinger.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import ...timeevolution: dschroedinger, dschroedinger_dynamic, check_schroedinger
22

3-
import DiffEqCallbacks
4-
53
"""
64
stochastic.schroedinger(tspan, state0, H, Hs[; fout, ...])
75

src/timeevolution_base.jl

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import OrdinaryDiffEq, DiffEqCallbacks, StochasticDiffEq
1+
using QuantumOpticsBase
2+
using QuantumOpticsBase: check_samebases, check_multiplicable
23

3-
export @skiptimechecks
4+
import OrdinaryDiffEq, DiffEqCallbacks
45

56
const DiffArray = Union{Vector{ComplexF64}, Array{ComplexF64, 2}}
67

8+
function recast! end
9+
710
"""
811
integrate(tspan::Vector{Float64}, df::Function, x0::Vector{ComplexF64},
912
state::T, dstate::T, fout::Function; kwargs...)
@@ -86,98 +89,6 @@ function (c::SteadyStateCondtion)(rho,t,integrator)
8689
end
8790

8891

89-
90-
"""
91-
integrate_stoch(tspan::Vector{Float64}, df::Function, dg::Vector{Function}, x0::Vector{ComplexF64},
92-
state::T, dstate::T, fout::Function; kwargs...)
93-
94-
Integrate using StochasticDiffEq
95-
"""
96-
function integrate_stoch(tspan::Vector{Float64}, df::Function, dg::Function, x0::Vector{ComplexF64},
97-
state::T, dstate::T, fout::Function, n::Int;
98-
save_everystep = false, callback=nothing,
99-
alg::StochasticDiffEq.StochasticDiffEqAlgorithm=StochasticDiffEq.EM(),
100-
noise_rate_prototype = nothing,
101-
noise_prototype_classical = nothing,
102-
noise=nothing,
103-
ncb=nothing,
104-
kwargs...) where T
105-
106-
function df_(dx::Vector{ComplexF64}, x::Vector{ComplexF64}, p, t)
107-
recast!(x, state)
108-
recast!(dx, dstate)
109-
df(t, state, dstate)
110-
recast!(dstate, dx)
111-
end
112-
113-
function dg_(dx::Union{Vector{ComplexF64}, Array{ComplexF64, 2}},
114-
x::Vector{ComplexF64}, p, t)
115-
recast!(x, state)
116-
dg(dx, t, state, dstate, n)
117-
end
118-
119-
function fout_(x::Vector{ComplexF64}, t::Float64, integrator)
120-
recast!(x, state)
121-
fout(t, state)
122-
end
123-
124-
nc = isa(noise_prototype_classical, Nothing) ? 0 : size(noise_prototype_classical)[2]
125-
if isa(noise, Nothing) && n > 0
126-
if n + nc == 1
127-
noise_ = StochasticDiffEq.RealWienerProcess(0.0, 0.0)
128-
else
129-
noise_ = StochasticDiffEq.RealWienerProcess!(0.0, zeros(n + nc))
130-
end
131-
else
132-
noise_ = noise
133-
end
134-
if isa(noise_rate_prototype, Nothing)
135-
if n > 1 || nc > 1 || (n > 0 && nc > 0)
136-
noise_rate_prototype = zeros(ComplexF64, length(x0), n + nc)
137-
end
138-
end
139-
140-
out_type = pure_inference(fout, Tuple{eltype(tspan),typeof(state)})
141-
142-
out = DiffEqCallbacks.SavedValues(Float64,out_type)
143-
144-
scb = DiffEqCallbacks.SavingCallback(fout_,out,saveat=tspan,
145-
save_everystep=save_everystep,
146-
save_start = false)
147-
148-
full_cb = OrdinaryDiffEq.CallbackSet(callback, ncb, scb)
149-
150-
prob = StochasticDiffEq.SDEProblem{true}(df_, dg_, x0,(tspan[1],tspan[end]),
151-
noise=noise_,
152-
noise_rate_prototype=noise_rate_prototype)
153-
154-
sol = StochasticDiffEq.solve(
155-
prob,
156-
alg;
157-
reltol = 1.0e-3,
158-
abstol = 1.0e-3,
159-
save_everystep = false, save_start = false,
160-
save_end = false,
161-
callback=full_cb, kwargs...)
162-
163-
out.t,out.saveval
164-
end
165-
166-
"""
167-
integrate_stoch
168-
169-
Define fout if it was omitted.
170-
"""
171-
function integrate_stoch(tspan::Vector{Float64}, df::Function, dg::Function, x0::Vector{ComplexF64},
172-
state::T, dstate::T, ::Nothing, n::Int; kwargs...) where T
173-
function fout(t::Float64, state::T)
174-
copy(state)
175-
end
176-
integrate_stoch(tspan, df, dg, x0, state, dstate, fout, n; kwargs...)
177-
end
178-
179-
180-
18192
const QO_CHECKS = Ref(true)
18293
"""
18394
@skiptimechecks

0 commit comments

Comments
 (0)