Skip to content

Commit

Permalink
add partial initialize to RandomFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
EigenSolver committed Nov 8, 2024
1 parent ee820b7 commit 7462577
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/SpinShuttling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export ShuttlingModel, OneSpinModel, TwoSpinModel,
TwoSpinSequentialModel, TwoSpinParallelModel,
RandomFunction, CompositeRandomFunction,
OrnsteinUhlenbeckField, PinkLorentzianField
export statefidelity, sampling, characteristicfunction, characteristicvalue
export statefidelity, sampling, initialize!, characteristicfunction, characteristicvalue
export dephasingmatrix, covariance, covariancematrix
export W

Expand Down Expand Up @@ -73,12 +73,12 @@ with arbitrary shuttling path x(t).
- `x::Function`: Shuttling path
"""
function OneSpinModel::Vector{<:Complex}, T::Real, N::Int,
B::GaussianRandomField, x::Function)
B::GaussianRandomField, x::Function; initialize::Bool=true)

t = range(0, T, N)
f(x::Function, t::Real) = (t, x(t)...)
P = f.(x, t)
R = RandomFunction(P, B)
R = RandomFunction(P, B, initialize=initialize)
model = ShuttlingModel(1, Ψ, T, N, B, [x], R)
return model
end
Expand Down Expand Up @@ -128,13 +128,13 @@ with arbitrary shuttling paths x₁(t), x₂(t).
- `x₂::Function`: Shuttling path for the second spin
"""
function TwoSpinModel::Vector{<:Complex}, T::Real, N::Int,
B::GaussianRandomField, x₁::Function, x₂::Function)
B::GaussianRandomField, x₁::Function, x₂::Function; initialize::Bool=true)

X = [x₁, x₂]
t = range(0, T, N)
f(x::Function, t::Real) = (t, x(t)...)
P = vcat(f.(x₁, t), f.(x₂, t))
R = RandomFunction(P, B)
R = RandomFunction(P, B, initialize=initialize)
model = ShuttlingModel(2, Ψ, T, N, B, X, R)
return model
end
Expand Down
19 changes: 15 additions & 4 deletions src/stochastics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ a Gaussian random process traced from a Gaussian random field.
- `Σ::Symmetric{<:Real}`: covariance matrices
- `L::Matrix{<:Real}`: lower triangle matrix of Cholesky decomposition
"""
struct RandomFunction
mutable struct RandomFunction
μ::Vector{<:Real}
P::Vector{<:Point} # sample trace
Σ::Symmetric{<:Real} # covariance matrices
L::Matrix{<:Real} # Lower triangle matrix of Cholesky decomposition
function RandomFunction(P::Vector{<:Point}, process::GaussianRandomField)
L::Union{Matrix{<:Real}, Nothing} # Lower triangle matrix of Cholesky decomposition
function RandomFunction(P::Vector{<:Point}, process::GaussianRandomField; initialize::Bool=true)
μ = process.μ isa Function ? process.μ(P) : repeat([process.μ], length(P))
Σ = covariancematrix(P, process)
L = collect(cholesky(Σ).L)
if initialize
L = collect(cholesky(Σ).L)
else
L = nothing
end

return new(μ, P, Σ, L)
end

Expand All @@ -55,6 +60,12 @@ struct RandomFunction
end
end

"""
Initialize the Cholesky decomposition of the covariance matrix of a random function.
"""
function initialize!(R::RandomFunction)
R.L = collect(cholesky(R.Σ).L)
end

"""
Divide the covariance matrix of a direct summed random function into partitions.
Expand Down
3 changes: 2 additions & 1 deletion test/teststochastics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ visualize=true
t=range(0, T, N)
P=collect(zip(t, v.*t))
B=OrnsteinUhlenbeckField(0,[κₜ,κₓ],σ)
R=RandomFunction(P , B)
R=RandomFunction(P , B, initialize=false)
initialize!(R)
@test R() isa Vector{<:Real}
@test R.Σ isa Symmetric

Expand Down

0 comments on commit 7462577

Please sign in to comment.