i used to use Klara
for MCMC sampler where i just call back the log density of the target function and it was working the way i wanted and now when i want to go back where i’m using it i found problems since i can’t use Klara
anymore so i decided to look for a package that has maybe the same interface of Klara
i found Turing
and many more as an active options and i have already checked the post about MCMC landscape, i decided for the moment to go with Turing
to sample arrays using MH
sampler as you can see below:
using Random
using Distributions
using StatsPlots
using QuantumRelay # a module where it has all the called functions in this code
using DataFrames
using Turing
using LinearAlgebra
function qrelay(alpha, delta, name)
n = 6
chi = fill(sqrt(0.06), n)
phi = [im * tanh(chi[i]) for i=1:n]
omega = [1.0 / prod(cosh(chi[j]))^2 for j=1:n]
syms, op = qrelay_op(n, phi, alpha, delta) #an exported function that is working in a made package
op_a, op_ab, mat, coef = op_mat(op) #an exported function that is working in a made package
op_q2 = [syms.apH[1], syms.apV[1], syms.bpH[end], syms.bpV[end]]
op_q1 = [syms.apH[2:end]..., syms.apV[2:end]..., syms.bpH[1:end-1]..., syms.bpV[1:end-1]...]
mask_q1 = [op in op_q1 for op in op_a];
mask_q2 = [op in op_q2 for op in op_a];
qq = [x in syms.apH || x in syms.bpV ? 1 : 0 for x in op_a]
pdet0 = pdet_maker(0.04, 1e-5) #a function of generating the detection probability for detectors with transmittance eta(first argument) and dark count(second one) probability
qrs = QRelaySampler(mat, coef, omega, pdet0)
targetcache = Dict{Vector{Int}, Float64}()
@model plogtarget(na) = begin
log(qrs.prob(qq, na, mask_q1) * qrs.prob(na)) #prob is the distribution function with two methods inside the QRelaySampler
end
c1=sample(plogtarget(zeros(qq)),MH(Dict{Symbol, Any}(:p=>qrs.psetproposal)),1000) #sampling using metropolis hasting sampler
funcQ(v)=qrs.prob(qq, v, mask_q2)
return qrs, c1, funcQ
end
qrelay (generic function with 1 method)
dataname="data3"
results = []
for i = 0:12
beta = i*pi/12
name = string(i)
mkpath("$dataname/$name")
qrs, c1, funcQ = qrelay(pi/4, beta, name)
println("beta:", beta)
push!(results, (qrs, c1, funcQ))
end
now when i try to run the code it shows me :
MethodError: no method matching zeros(::Array{Int64,1})
Closest candidates are:
zeros(!Matched::Union{Integer, AbstractUnitRange}...) at array.jl:440
zeros(!Matched::Type{StaticArrays.SArray{Tuple{N},T,1,N} where T}) where N at /Users/midow/.julia/packages/StaticArrays/1g9bq/src/SVector.jl:29
zeros(!Matched::Type{StaticArrays.MArray{Tuple{N},T,1,N} where T}) where N at /Users/midow/.julia/packages/StaticArrays/1g9bq/src/MVector.jl:27
...
Stacktrace:
[1] qrelay(::Float64, ::Float64, ::String) at ./In[7]:31
[2] top-level scope at ./In[9]:4774:
i know the error is in zeros(qq)
which is the initial value since i am sampling arrays .
here you will find the github link of the same code using Klara
, to see how it was working,
https://github.com/marouanehanhasse/quantum_relay_sampler/blob/master/quantumopticssampler(1).ipynb
and i do apology if this post is long but i couldn’t reduce it or simplify it since everything is related and if you want to know something that is unclear on this post please ask.