Skip to content

Commit 57ac631

Browse files
Fix nasty inplace bug and factorise out _bfw_init
1 parent 1d39800 commit 57ac631

4 files changed

Lines changed: 46 additions & 73 deletions

File tree

examples/GHZ.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ measurements = [[(σ[1] - v[i, 1] * σ[2] - v[i, 2] * σ[3] - v[i, 3] * σ[4]) /
1919
p = tensor_correlation(rho, measurements, N; marg = false) # the marginals vanish in this cas
2020

2121
# Frank-Wolfe
22-
lower_bound, upper_bound, local_model, bell_inequality = nonlocality_threshold(p; sym = false)
22+
lower_bound, upper_bound, local_model, bell_inequality = nonlocality_threshold(p)
2323

2424
println("Correlation tensor")
2525
display(p[:, :, 1]) # only printing part of the tensor

src/bell_frank_wolfe.jl

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,14 @@ function bell_frank_wolfe(
6767
kwargs...,
6868
) where {T <: Number, N}
6969
Random.seed!(seed)
70-
if !prob
71-
LMO = BellCorrelationsLMO
72-
DS = BellCorrelationsDS
73-
m = collect(size(p))
74-
if o === nothing
75-
o = zeros(T, size(p))
76-
o[end] = marg
77-
end
78-
reynolds = reynolds_permutedims
79-
build_deflate_inflate = build_deflate_inflate_permutedims
80-
else
81-
LMO = BellProbabilitiesLMO
82-
DS = BellProbabilitiesDS
83-
m = collect(size(p)[(N ÷ 2 + 1):end])
84-
if o === nothing
85-
o = ones(T, size(p)) / prod(size(p)[1:(N ÷ 2)])
86-
end
87-
reynolds = reynolds_permutelastdims
88-
build_deflate_inflate = build_deflate_inflate_permutelastdims
89-
end
90-
# symmetry detection
91-
if sym === nothing
92-
if all(diff(m) .== 0) && p reynolds(p) && (v0 == 1 || o reynolds(o))
93-
deflate, inflate = build_deflate_inflate(p)
94-
sym = true
95-
else
96-
sym = false
97-
end
98-
end
70+
LMO, DS, m, o, sym, deflate, inflate = _bfw_init(p, v0, prob, marg, o, sym, deflate, inflate, verbose)
9971
if verbose > 0
10072
println("Visibility: ", v0)
10173
end
10274
# choosing the point on the line between o and p according to the visibility v0
103-
ro = deflate(o)
104-
rp = deflate(p)
75+
ro = deflate(copy(o))
76+
rp = deflate(copy(p))
10577
vp = v0 * rp + (one(T) - v0) * ro
106-
if verbose > 1
107-
println(" #Inputs: ", all(diff(m) .== 0) ? m[end] - (marg && !prob) : m .- (marg && !prob))
108-
println(" Symmetric: ", sym)
109-
println(" Dimension: ", length(vp))
110-
end
11178
# create the LMO
11279
if sym
11380
lmo = FrankWolfe.SubspaceLMO(LMO(p, vp; mode, nb, marg), deflate, inflate)

src/nonlocality_threshold.jl

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,17 @@ function nonlocality_threshold(
2424
sym = nothing,
2525
deflate = identity,
2626
inflate = identity,
27+
verbose = 0,
2728
kwargs...,
2829
) where {T <: Number, N}
29-
if !prob
30-
m = collect(size(p))
31-
if o === nothing
32-
o = zeros(T, size(p))
33-
o[end] = marg
34-
end
35-
reynolds = reynolds_permutedims
36-
build_deflate_inflate = build_deflate_inflate_permutedims
37-
else
38-
m = collect(size(p)[(N ÷ 2 + 1):end])
39-
if o === nothing
40-
o = ones(T, size(p)) / prod(size(p)[1:(N ÷ 2)])
41-
end
42-
reynolds = reynolds_permutelastdims
43-
build_deflate_inflate = build_deflate_inflate_permutelastdims
44-
end
45-
# symmetry detection
46-
if sym === nothing
47-
if all(diff(m) .== 0) && p reynolds(p) && (v0 == 1 || o reynolds(o))
48-
deflate, inflate = build_deflate_inflate(p)
49-
sym = true
50-
else
51-
sym = false
52-
end
53-
end
30+
_, _, _, o, sym, deflate, inflate = _bfw_init(p, 0, prob, marg, o, sym, deflate, inflate, verbose)
5431
lower_bound = zero(T)
5532
upper_bound = one(T)
5633
local_model = nothing
5734
bell_inequality = nothing
5835
while upper_bound - lower_bound > 10.0^(-precision)
59-
x, ds, primal, dual_gap, as, M, β = bell_frank_wolfe(
60-
p;
61-
v0,
62-
epsilon,
63-
marg,
64-
o,
65-
sym,
66-
deflate,
67-
inflate,
68-
kwargs...,
69-
)
36+
res = bell_frank_wolfe(p; v0, epsilon, marg, o, sym, deflate, inflate, kwargs...)
37+
x, ds, primal, dual_gap, as, M, β = res
7038
if primal > 10epsilon && dual_gap > 10epsilon
7139
@warn "Please increase nb or max_iteration"
7240
end

src/utils.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,3 +1120,41 @@ function move_marg(FC::AbstractArray{T, N}, sense = -1) where {T, N}
11201120
return circshift(FC, ntuple(i -> sense, Val(N)))
11211121
end
11221122
export move_marg
1123+
1124+
function _bfw_init(p::Array{T, N}, v0, prob, marg, o, sym, deflate, inflate, verbose) where {T <: Number, N}
1125+
if !prob
1126+
LMO = BellCorrelationsLMO
1127+
DS = BellCorrelationsDS
1128+
m = collect(size(p))
1129+
if o === nothing
1130+
o = zeros(T, size(p))
1131+
o[end] = marg
1132+
end
1133+
reynolds = reynolds_permutedims
1134+
build_deflate_inflate = build_deflate_inflate_permutedims
1135+
else
1136+
LMO = BellProbabilitiesLMO
1137+
DS = BellProbabilitiesDS
1138+
m = collect(size(p)[(N ÷ 2 + 1):end])
1139+
if o === nothing
1140+
o = ones(T, size(p)) / prod(size(p)[1:(N ÷ 2)])
1141+
end
1142+
reynolds = reynolds_permutelastdims
1143+
build_deflate_inflate = build_deflate_inflate_permutelastdims
1144+
end
1145+
# symmetry detection
1146+
if sym === nothing
1147+
if all(diff(m) .== 0) && p reynolds(p) && (v0 == 1 || o reynolds(o))
1148+
deflate, inflate = build_deflate_inflate(p)
1149+
sym = true
1150+
else
1151+
sym = false
1152+
end
1153+
end
1154+
if verbose > 1
1155+
println(" #Inputs: ", all(diff(m) .== 0) ? m[end] - (marg && !prob) : m .- (marg && !prob))
1156+
println(" Symmetric: ", sym)
1157+
println(" Dimension: ", length(deflate(p)))
1158+
end
1159+
return LMO, DS, m, o, sym, deflate, inflate
1160+
end

0 commit comments

Comments
 (0)