Skip to content

Commit

Permalink
add cg2fp
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoms committed Nov 19, 2024
1 parent 1962b4b commit 194286d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
87 changes: 74 additions & 13 deletions src/nonlocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ end
export tsirelson_bound

"""
fp2cg(V::Array{T,4}) where {T <: Real}
fp2cg(V::Array{T,4}, behaviour::Bool = false) where {T <: Real}
Takes a bipartite Bell functional `V` in full probability notation and transforms it
to Collins-Gisin notation.
to Collins-Gisin notation. If `behaviour` is `true` do instead the transformation for behaviours. Doesn't assume normalization.
"""
function fp2cg(V::AbstractArray{T,4}) where {T<:Real}
function fp2cg(V::AbstractArray{T,4}, behaviour::Bool = false) where {T}
oa, ob, ia, ib = size(V)
alice_pars = ia * (oa - 1) + 1
bob_pars = ib * (ob - 1) + 1
Expand All @@ -131,21 +131,82 @@ function fp2cg(V::AbstractArray{T,4}) where {T<:Real}

CG = zeros(T, alice_pars, bob_pars)

CG[1, 1] = sum(V[oa, ob, :, :])
for a = 1:oa-1, x = 1:ia
CG[aindex(a, x), 1] = sum(V[a, ob, x, :] - V[oa, ob, x, :])
end
for b = 1:ob-1, y = 1:ib
CG[1, bindex(b, y)] = sum(V[oa, b, :, y] - V[oa, ob, :, y])
end
for a = 1:oa-1, b = 1:ob-1, x = 1:ia, y = 1:ib
CG[aindex(a, x), bindex(b, y)] = V[a, b, x, y] - V[a, ob, x, y] - V[oa, b, x, y] + V[oa, ob, x, y]
if ~behaviour
CG[1, 1] = sum(V[oa, ob, :, :])
for a = 1:oa-1, x = 1:ia
CG[aindex(a, x), 1] = sum(V[a, ob, x, :] - V[oa, ob, x, :])
end
for b = 1:ob-1, y = 1:ib
CG[1, bindex(b, y)] = sum(V[oa, b, :, y] - V[oa, ob, :, y])
end
for a = 1:oa-1, b = 1:ob-1, x = 1:ia, y = 1:ib
CG[aindex(a, x), bindex(b, y)] = V[a, b, x, y] - V[a, ob, x, y] - V[oa, b, x, y] + V[oa, ob, x, y]
end
else
CG[1, 1] = sum(V) / (ia * ib)
for x = 1:ia, a = 1:oa-1
CG[aindex(a, x), 1] = sum(V[a, b, x, y] for b = 1:ob, y = 1:ib) / ib
end
for y = 1:ib, b = 1:ob-1
CG[1, bindex(b, y)] = sum(V[a, b, x, y] for a = 1:oa, x = 1:ia) / ia
end
for x = 1:ia, y = 1:ib
CG[aindex(1, x):aindex(oa - 1, x), bindex(1, y):bindex(ob - 1, y)] = V[1:oa-1, 1:ob-1, x, y]
end
end

return CG
end
export fp2cg

"""
cg2fp(CG::Matrix, 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 cg2fp(CG::Matrix{T}, scenario::Vector{<:Integer}, behaviour::Bool = false) where {T}
oa, ob, ia, ib = scenario
aindex(a, x) = 1 + a + (x - 1) * (oa - 1)
bindex(b, y) = 1 + b + (y - 1) * (ob - 1)

V = Array{T,4}(undef, (oa, ob, ia, ib))

if ~behaviour
for x = 1:ia, y = 1:ib
V[oa, ob, x, y] = CG[1, 1] / (ia * ib)
end
for x = 1:ia, y = 1:ib, b = 1:ob-1
V[oa, b, x, y] = CG[1, 1] / (ia * ib) + CG[1, bindex(b, y)] / ia
end
for x = 1:ia, y = 1:ib, a = 1:oa-1
V[a, ob, x, y] = CG[1, 1] / (ia * ib) + CG[aindex(a, x), 1] / ib
end
for x = 1:ia, y = 1:ib, a = 1:oa-1, b = 1:ob-1
V[a, b, x, y] =
CG[1, 1] / (ia * ib) +
CG[aindex(a, x), 1] / ib +
CG[1, bindex(b, y)] / ia +
CG[aindex(a, x), bindex(b, y)]
end
else
for x = 1:ia, y = 1:ib
V[1:oa-1, 1:ob-1, x, y] = CG[aindex(1, x):aindex(oa - 1, x), bindex(1, y):bindex(ob - 1, y)]
V[1:oa-1, ob, x, y] =
CG[aindex(1, x):aindex(oa - 1, x), 1] -
sum(CG[aindex(1, x):aindex(oa - 1, x), bindex(1, y):bindex(ob - 1, y)]; dims = 2)
V[oa, 1:ob-1, x, y] =
CG[1, bindex(1, y):bindex(ob - 1, y)] -
vec(sum(CG[aindex(1, x):aindex(oa - 1, x), bindex(1, y):bindex(ob - 1, y)]; dims = 1))
V[oa, ob, x, y] =
CG[1, 1] - sum(CG[aindex(1, x):aindex(oa - 1, x), 1]) -
sum(CG[1, bindex(1, y):bindex(ob - 1, y)]) +
sum(CG[aindex(1, x):aindex(oa - 1, x), bindex(1, y):bindex(ob - 1, y)])
end
end
return V
end
export cg2fp

"""
probability_tensor(rho::Hermitian, all_Aax::Vector{Measurement}...)
Expand Down
4 changes: 4 additions & 0 deletions test/nonlocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ end
Aax = povm(mub(2))
@test correlation_tensor(state_phiplus(), Aax, 2) Diagonal([1, 1, -1, 1])
@test correlation_tensor(state_phiplus(), Aax, 2; marg = false) Diagonal([1, 1, -1])
scenario = [2,3,4,5]
cg = randn(scenario[3]*(scenario[1]-1) + 1, scenario[4]*(scenario[2]-1) + 1)
@test fp2cg(cg2fp(cg,scenario)) cg
@test fp2cg(cg2fp(cg,scenario,true),true) cg
end

0 comments on commit 194286d

Please sign in to comment.