-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0de3ca3
commit ee820b7
Showing
3 changed files
with
111 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,65 @@ | ||
""" | ||
Monte-Carlo sampling of any objective function without storage in memory. | ||
The function must return Tuple{Number,Number} or Tuple{VecOrMat{<:Number},VecOrMat{<:Number}} | ||
# Arguments | ||
- `samplingfunction::Function`: The function to be sampled | ||
- `M::Int`: Monte-Carlo sampling size | ||
# Returns | ||
- `Tuple{Number,Number}`: The mean and variance of the sampled function | ||
- `Tuple{VecOrMat{<:Number},VecOrMat{<:Number}}`: The mean and variance of the sampled function | ||
# Example | ||
```julia | ||
f(x) = x^2 | ||
sampling(f, 1000) | ||
``` | ||
# Reference | ||
https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods | ||
""" | ||
function serialsampling(samplingfunction::Function, M::Int)::Union{Tuple{Number,Number},Tuple{VecOrMat{<:Number},VecOrMat{<:Number}}} | ||
A = samplingfunction() | ||
A isa Array ? A .= 0 : A = 0 | ||
Q = copy(A) | ||
for k in 1:M | ||
x = samplingfunction()::Union{Number,VecOrMat{<:Number}} | ||
Q = Q + (k - 1) / k * abs.(x - A) .^ 2 | ||
A = A + (x - A) / k | ||
end | ||
return A, Q / (M - 1) | ||
end | ||
|
||
""" | ||
Multi-threaded Monte-Carlo sampling of any objective function. | ||
The function must return Tuple{Number,Number} or Tuple{VecOrMat{<:Number},VecOrMat{<:Number}} | ||
""" | ||
function parallelsampling(samplingfunction::Function, M::Int)::Union{Tuple{Number,Number},Tuple{VecOrMat{<:Number},VecOrMat{<:Number}}} | ||
obj = samplingfunction() | ||
if obj isa Number | ||
cache = zeros(typeof(obj), M) | ||
@threads for i in 1:M | ||
cache[i] = samplingfunction() | ||
end | ||
A = mean(cache) | ||
Q = var(cache) | ||
return A, Q | ||
elseif obj isa Vector | ||
cache = zeros(typeof(obj).parameters[1], length(obj), M) | ||
@threads for i in 1:M | ||
cache[:, i] .= samplingfunction() | ||
end | ||
A = dropdims(mean(cache, dims=2),dims=2) | ||
Q = dropdims(var(cache, dims=2),dims=2) | ||
return A, Q | ||
elseif obj isa Matrix | ||
cache = zeros(typeof(obj).parameters[1], size(obj)..., M) | ||
@threads for i in 1:M | ||
cache[:, :, i] .= samplingfunction() | ||
end | ||
A = dropdims(mean(cache, dims=3),dims=3) | ||
Q = dropdims(var(cache, dims=3),dims=3) | ||
return A, Q | ||
else | ||
error("The objective function must return `VecOrMat{<:Number}` or `Number`") | ||
end | ||
end | ||
""" | ||
Monte-Carlo sampling of any objective function without storage in memory. | ||
The function must return Tuple{Number,Number} or Tuple{VecOrMat{<:Number},VecOrMat{<:Number}} | ||
# Arguments | ||
- `samplingfunction::Function`: The function to be sampled | ||
- `M::Int`: Monte-Carlo sampling size | ||
# Returns | ||
- `Tuple{Number,Number}`: The mean and variance of the sampled function | ||
- `Tuple{VecOrMat{<:Number},VecOrMat{<:Number}}`: The mean and variance of the sampled function | ||
# Example | ||
```julia | ||
f(x) = x^2 | ||
sampling(f, 1000) | ||
``` | ||
# Reference | ||
https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods | ||
""" | ||
function serialsampling(samplingfunction::Function, M::Int)::Union{Tuple{Number,Number},Tuple{VecOrMat{<:Number},VecOrMat{<:Number}}} | ||
A = samplingfunction() | ||
A isa Array ? A .= 0 : A = 0 | ||
Q = copy(A) | ||
for k in 1:M | ||
x = samplingfunction()::Union{Number,VecOrMat{<:Number}} | ||
Q = Q + (k - 1) / k * abs.(x - A) .^ 2 | ||
A = A + (x - A) / k | ||
end | ||
return A, Q / (M - 1) | ||
end | ||
|
||
""" | ||
Multi-threaded Monte-Carlo sampling of any objective function. | ||
The function must return Tuple{Number,Number} or Tuple{VecOrMat{<:Number},VecOrMat{<:Number}} | ||
""" | ||
function parallelsampling(samplingfunction::Function, M::Int)::Union{Tuple{Number,Number},Tuple{VecOrMat{<:Number},VecOrMat{<:Number}}} | ||
obj = samplingfunction() | ||
if obj isa Number | ||
cache = zeros(typeof(obj), M) | ||
@threads for i in 1:M | ||
cache[i] = samplingfunction() | ||
end | ||
A = mean(cache) | ||
Q = var(cache) | ||
return A, Q | ||
elseif obj isa Vector | ||
cache = zeros(typeof(obj).parameters[1], length(obj), M) | ||
@threads for i in 1:M | ||
cache[:, i] .= samplingfunction() | ||
end | ||
A = dropdims(mean(cache, dims=2),dims=2) | ||
Q = dropdims(var(cache, dims=2),dims=2) | ||
return A, Q | ||
elseif obj isa Matrix | ||
cache = zeros(typeof(obj).parameters[1], size(obj)..., M) | ||
@threads for i in 1:M | ||
cache[:, :, i] .= samplingfunction() | ||
end | ||
A = dropdims(mean(cache, dims=3),dims=3) | ||
Q = dropdims(var(cache, dims=3),dims=3) | ||
return A, Q | ||
else | ||
error("The objective function must return `VecOrMat{<:Number}` or `Number`") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
using SpinShuttling | ||
using Statistics | ||
import SpinShuttling.sampling | ||
|
||
T=4; L=10; σ = sqrt(2) / 20; M = Int(1e6); N=999; κₜ=1;κₓ=1; | ||
B=OrnsteinUhlenbeckField(0,[κₜ,κₓ],σ) | ||
model=OneSpinModel(T,L,N,B) | ||
|
||
function f(model::ShuttlingModel, randseq::Vector{<:Real}; isarray::Bool=false)::Complex | ||
return exp(im*sum(model.R(randseq))) | ||
end | ||
|
||
println("Benchmark parallel sampling:") | ||
@time sampling(model, f, M, isparallel=true) | ||
|
||
println("Benchmark fidelity:") | ||
@time sampling(model, statefidelity, M, isparallel=true) | ||
|
||
|
||
println("Benchmark sequential sampling:") | ||
@time sampling(model, f, M, isparallel=false) | ||
|
||
function f(model::ShuttlingModel)::Complex | ||
return exp(im*sum(model.R())) | ||
end | ||
|
||
println(typeof(model.R.L)) | ||
A=model.R.L|>collect | ||
sample=zeros(Complex{Float64},M) | ||
randpool=randn(M,N) | ||
println("Benchmark standard sampling:") | ||
@time for i in 1:M | ||
sample[i]=cos(sum(A*randpool[i,:])) | ||
end | ||
|
||
mean(sample),var(sample) | ||
using SpinShuttling | ||
using Statistics | ||
import SpinShuttling.sampling | ||
|
||
T=4; L=10; σ = sqrt(2) / 20; M = Int(1e6); N=999; κₜ=1;κₓ=1; | ||
B=OrnsteinUhlenbeckField(0,[κₜ,κₓ],σ) | ||
model=OneSpinModel(T,L,N,B) | ||
|
||
function f(model::ShuttlingModel, randseq::Vector{<:Real}; isarray::Bool=false)::Complex | ||
return exp(im*sum(model.R(randseq))) | ||
end | ||
|
||
println("Benchmark parallel sampling:") | ||
@time sampling(model, f, M, isparallel=true) | ||
|
||
println("Benchmark fidelity:") | ||
@time sampling(model, statefidelity, M, isparallel=true) | ||
|
||
|
||
println("Benchmark sequential sampling:") | ||
@time sampling(model, f, M, isparallel=false) | ||
|
||
function f(model::ShuttlingModel)::Complex | ||
return exp(im*sum(model.R())) | ||
end | ||
|
||
println(typeof(model.R.L)) | ||
A=model.R.L|>collect | ||
sample=zeros(Complex{Float64},M) | ||
randpool=randn(M,N) | ||
println("Benchmark standard sampling:") | ||
@time for i in 1:M | ||
sample[i]=cos(sum(A*randpool[i,:])) | ||
end | ||
|
||
mean(sample),var(sample) |