Skip to content

Commit

Permalink
nothing wrong with tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoms committed Dec 7, 2024
1 parent 3069876 commit f66127e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
5 changes: 3 additions & 2 deletions ext/KetMATLAB/KetMATLAB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module KetMATLAB

import Ket
import MATLAB
import Base.AbstractVecOrTuple

function Ket.tsirelson_bound(CG::Matrix{<:Real}, scenario::Vector{<:Integer}, level::Integer)
function Ket.tsirelson_bound(CG::Matrix{<:Real}, scenario::AbstractVecOrTuple{<:Integer}, level::Integer)
CG = Float64.(CG)
scenario = Float64.(scenario)
scenario = collect(Float64.(scenario))
return MATLAB.mxcall(:mtk_tsirelson, 1, CG, scenario, level)
end

Expand Down
1 change: 1 addition & 0 deletions src/Ket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Nemo
import JuMP
const MOI = JuMP.MOI
import Hypatia, Hypatia.Cones
import Base.AbstractVecOrTuple

include("basic.jl")
include("states.jl")
Expand Down
18 changes: 9 additions & 9 deletions src/nonlocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ function _update_odometer!(ind::AbstractVector{<:Integer}, upper_lim::Integer)
end

"""
tsirelson_bound(CG::Matrix, scenario::Vector, level::Integer)
tsirelson_bound(CG::Matrix, scenario::AbstractVecOrTuple, level::Integer)
Upper bounds the Tsirelson bound of a bipartite Bell funcional game `CG`, written in Collins-Gisin notation.
`scenario` is a vector detailing the number of inputs and outputs, in the order [oa, ob, ia, ib].
`level` is an integer determining the level of the NPA hierarchy.
This function requires [Moment](https://github.com/ajpgarner/moment). It is only available if you first do "import MATLAB" or "using MATLAB".
"""
function tsirelson_bound(CG::Matrix{<:Real}, scenario::Vector{<:Integer}, level)
function tsirelson_bound(CG::Matrix{<:Real}, scenario::AbstractVecOrTuple{<:Integer}, level)
return error("This function requires MATLAB. Do `import MATLAB` or `using MATLAB` in order to enable it.")
end
export tsirelson_bound
Expand Down Expand Up @@ -157,18 +157,18 @@ function tensor_collinsgisin(p::AbstractArray{T, N2}, behaviour::Bool = false) w

p2cg(a, x) = (a .!= outs) .* (a + (x .- 1) .* (outs .- 1)) .+ 1

cgdesc = ins .* (outs .- 1) .+ 1
cgsizes = ins .* (outs .- 1) .+ 1
cgprodsizes = ones(Int, N)
for i in 1:N
cgprodsizes[i] = prod(cgdesc[1:(i - 1)])
cgprodsizes[i] = prod(cgsizes[1:(i - 1)])
end
cgindex(posvec) = cgprodsizes' * (posvec .- 1) + 1
prodsizes = ones(Int, 2 * N)
for i in 1:(2 * N)
prodsizes[i] = prod(scenario[1:(i - 1)])
end
pindex(posvec) = prodsizes' * (posvec .- 1) + 1
CG = zeros(T, cgdesc...)
CG = zeros(T, cgsizes...)

for inscalar in 0:(num_ins - 1)
invec = 1 .+ _digits_mixed_basis(inscalar, ins)
Expand Down Expand Up @@ -196,13 +196,13 @@ end
export tensor_collinsgisin

"""
tensor_probability(CG::Array{T, N}, scenario::Vector, behaviour::Bool = false)
tensor_probability(CG::Array{T, N}, scenario::AbstractVecOrTuple, behaviour::Bool = false)
Takes a bipartite Bell functional `CG` in Collins-Gisin notation and transforms it to full probability notation.
`scenario` is a vector detailing the number of inputs and outputs, in the order [oa, ob, ia, ib].
If `behaviour` is `true` do instead the transformation for behaviours. Doesn't assume normalization.
"""
function tensor_probability(CG::AbstractArray{T, N}, scenario::Vector{<:Integer}, behaviour::Bool = false) where {T, N}
function tensor_probability(CG::AbstractArray{T, N}, scenario::AbstractVecOrTuple{<:Integer}, behaviour::Bool = false) where {T, N}
p = zeros(T, scenario...)

if ~behaviour
Expand Down Expand Up @@ -231,10 +231,10 @@ function tensor_probability(CG::AbstractArray{T, N}, scenario::Vector{<:Integer}

p2cg(a, x) = (a .!= outs) .* (a + (x .- 1) .* (outs .- 1)) .+ 1

cgdesc = size(CG)
cgsizes = size(CG)
cgprodsizes = ones(Int, N)
for i in 1:N
cgprodsizes[i] = prod(cgdesc[1:(i - 1)])
cgprodsizes[i] = prod(cgsizes[1:(i - 1)])
end
cgindex(posvec) = cgprodsizes' * (posvec .- 1) + 1

Expand Down
4 changes: 2 additions & 2 deletions src/seesaw.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
seesaw(CG::Matrix, scenario::Vector, d::Integer)
seesaw(CG::Matrix, scenario::AbstractVecOrTuple, d::Integer)
Maximizes bipartite Bell functional in Collins-Gisin notation `CG` using the seesaw heuristic. `scenario` is a vector detailing the number of inputs and outputs, in the order [oa, ob, ia, ib].
`d` is an integer determining the local dimension of the strategy.
Expand All @@ -9,7 +9,7 @@ If `oa` == `ob` == 2 the heuristic reduces to a bunch of eigenvalue problems. Ot
References: Pál and Vértesi, [arXiv:1006.3032](https://arxiv.org/abs/1006.3032),
section II.B.1 of Tavakoli et al., [arXiv:2307.02551](https://arxiv.org/abs/2307.02551)
"""
function seesaw(CG::Matrix{T}, scenario::Vector{<:Integer}, d::Integer) where {T <: Real}
function seesaw(CG::Matrix{T}, scenario::AbstractVecOrTuple{<:Integer}, d::Integer) where {T <: Real}
R = _solver_type(T)
CG = R.(CG)
T2 = Complex{R}
Expand Down
19 changes: 11 additions & 8 deletions test/nonlocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@test local_bound(chsh(Int64, 3)) == 6
@test local_bound(cglmp(Int64, 4)) == 9
Random.seed!(1337)
@test seesaw(tensor_collinsgisin(cglmp()), [3, 3, 2, 2], 3)[1] (15 + sqrt(33)) / 24
@test seesaw(inn22(), [2, 2, 3, 3], 2)[1] 1.25
@test seesaw(tensor_collinsgisin(cglmp()), (3, 3, 2, 2), 3)[1] (15 + sqrt(33)) / 24
@test seesaw(inn22(), (2, 2, 3, 3), 2)[1] 1.25
for T in [Float64, Double64, Float128, BigFloat]
@test eltype(chsh(T)) <: T
@test eltype(cglmp(T)) <: T
Expand All @@ -25,9 +25,9 @@ end
fc_ghz[[1, 6, 18, 21, 43, 48, 60, 63]] .= [1, 1, 1, 1, 1, -1, -1, -1]
@test tensor_correlation(state_ghz(Complex{T}), Aax, 3) fc_ghz
@test tensor_correlation(state_ghz(Complex{T}), Aax, 3; marg = false) fc_ghz[2:end, 2:end, 2:end]
scenario = [2,2,2,2,3,4]
p = randn(T, scenario...)
mfc = randn(T, scenario[4] + 1, scenario[5] + 1, scenario[6] + 1)
scenario = (2,2,2,2,3,4)
p = randn(T, scenario)
mfc = randn(T, scenario[4:6] .+ 1)
@test dot(mfc, tensor_correlation(p, true)) dot(tensor_probability(mfc, false), p)
pfc = mfc
m = p
Expand All @@ -40,12 +40,15 @@ end
Aax = povm(mub(Complex{T}, 2))
cg_phiplus = [1.0 0.5 0.5 0.5; 0.5 0.5 0.25 0.25; 0.5 0.25 0.5 0.25; 0.5 0.25 0.25 0.0]
@test tensor_collinsgisin(state_phiplus(Complex{T}, ), Aax, 2) cg_phiplus
scenario = [2, 3, 4, 5]
p = randn(T, scenario...)
mcg = randn(T, scenario[3] * (scenario[1] - 1) + 1, scenario[4] * (scenario[2] - 1) + 1)
scenario = (2, 3, 4, 5)
p = randn(T, scenario)
mcg = randn(T, scenario[3:4] .* (scenario[1:2] .- 1) .+ 1)
@test dot(mcg, tensor_collinsgisin(p, true)) dot(tensor_probability(mcg, scenario, false), p)
pcg = mcg
m = p
@test dot(tensor_collinsgisin(m, false), pcg) dot(m, tensor_probability(pcg, scenario, true))
scenario = (2, 3, 4, 5, 6, 7)
cg = randn(T, scenario[4:6] .* (scenario[1:3] .- 1) .+ 1)
@test tensor_collinsgisin(tensor_probability(cg, scenario, true), true) cg
end
end

0 comments on commit f66127e

Please sign in to comment.