Skip to content

Commit

Permalink
black_box_Strategies enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Doolittle committed Nov 27, 2020
1 parent 62dd7b4 commit 2dcea2d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
36 changes: 36 additions & 0 deletions src/LocalPolytope/vertices.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export vertices, num_vertices

export black_box_strategies

"""
vertices(
scenario :: LocalSignaling;
Expand Down Expand Up @@ -63,6 +65,9 @@ function vertices(scenario :: LocalSignaling;
vertices
end

function vertices(scenario :: BipartiteNonSignaling)
end

"""
num_vertices( scenario :: LocalSignaling; rank_d_only = false :: Bool ) :: Int64
Expand All @@ -73,3 +78,34 @@ function num_vertices(scenario :: LocalSignaling; rank_d_only = false :: Bool) :
lower_dits_bound = rank_d_only ? scenario.d : 1
sum(map(i -> QMath.stirling2(scenario.X, i)*binomial(scenario.B, i)*factorial(i), lower_dits_bound:scenario.d))
end


"""
black_box_strategies(scenario :: BlackBox) :: Vector{Matrix{Int64}}
black_box_strategies(num_out :: Int64, num_in :: Int64) :: Vector{Matrix{Int64}}
Enumerates the set of deterministic strategies for the specified `BlackBox`.
"""
black_box_strategies(
num_out :: Int64,
num_in :: Int64
) = black_box_strategies(BlackBox(num_out,num_in))

function black_box_strategies(scenario :: BlackBox) :: Vector{Matrix{Int64}}
num_in = scenario.num_in
num_out = scenario.num_out

strategies = (num_out == 1) ? Matrix{Int64}[fill(1,(1,num_in))] : map( i -> begin
m = zeros(Int64, num_out, num_in)
base_n_array = digits(i, base = num_out, pad = num_in) .+ 1

for j in 1:num_in
m[base_n_array[j],j] = 1
end

m
end, 0:(num_out^num_in - 1))

strategies
end
14 changes: 7 additions & 7 deletions src/scenarios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ An abstract type to represent general black-box scenarios.
abstract type Scenario end

"""
BlackBox(num_in :: Int64, num_out :: Int64) <: Scenario
BlackBox(num_out :: Int64, num_in :: Int64) <: Scenario
A black-box scenario considering a single device. A black-box is an uncharacterized
device with a finite number of classical inputs and outputs.
A `DomainError` is throw if parameters `num_in` or `num_out` is less than 1.
A `DomainError` is throw if parameters `num_out` or `num_in` is less than 1.
"""
struct BlackBox <: Scenario
num_in :: Int64
num_out :: Int64
num_in :: Int64
BlackBox(
num_in::Int64,
num_out::Int64
) = ((num_in >= 1) && (num_out >= 1)) ? new(num_in, num_out) : throw(
DomainError((num_in, num_out), "inputs must be ≥ 1")
num_out::Int64,
num_in::Int64
) = ((num_out >= 1) && (num_in >= 1)) ? new(num_out, num_in) : throw(
DomainError((num_out, num_in), "inputs must be ≥ 1")
)
end

Expand Down
4 changes: 2 additions & 2 deletions src/strategies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct Strategy <: AbstractStrategy{Float64}
) = Strategy(QMath.Conditionals(conditionals), scenario)
Strategy( conditionals :: Matrix{<:Real} ) = new(
QMath.Conditionals(conditionals),
BlackBox(reverse(size(conditionals))...)
BlackBox(size(conditionals)...)
)
end

Expand Down Expand Up @@ -140,7 +140,7 @@ struct DeterministicStrategy <: AbstractStrategy{Int64}
)
DeterministicStrategy(
conditionals :: Matrix{<:Real}
) = is_deterministic(conditionals) ? new(conditionals, BlackBox(reverse(size(conditionals))...)) : throw(
) = is_deterministic(conditionals) ? new(conditionals, BlackBox(size(conditionals)...)) : throw(
DomainError(conditionals, "conditionals are not a deterministic (`{0,1}`) distribution.")
)
end
Expand Down
19 changes: 19 additions & 0 deletions test/unit/LocalPolytope/vertices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ end
end
end

@testset "black_box_strategies()" begin
@test LocalPolytope.black_box_strategies(BlackBox(1,5)) == [[1 1 1 1 1]]
@test LocalPolytope.black_box_strategies(BlackBox(2,2)) == [
[1 1;0 0],[0 1;1 0],[1 0;0 1],[0 0;1 1]
]
@test LocalPolytope.black_box_strategies(BlackBox(2,3)) == [
[1 1 1;0 0 0],[0 1 1;1 0 0],[1 0 1;0 1 0],[0 0 1;1 1 0],
[1 1 0;0 0 1],[0 1 0;1 0 1],[1 0 0;0 1 1],[0 0 0;1 1 1]
]

@testset "spot checks" begin
strategies = LocalPolytope.black_box_strategies(BlackBox(5,7))

@test length(strategies) == 5^7
@test length(unique(strategies)) == 5^7
@test all(s -> is_deterministic(s), strategies)
end
end

end
2 changes: 1 addition & 1 deletion test/unit/games.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end

@testset "IEQ -> BellGames" begin
gen_ieq = IEQ(inequalities = [1 1 0 0 0 1 2;1 1 0 0 0 0 1] )
scenario = BlackBox(2,3)
scenario = BlackBox(3,2)

bell_games = convert(Vector{BellGame}, gen_ieq, scenario, rep="generalized")

Expand Down
4 changes: 2 additions & 2 deletions test/unit/scenarios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using BellScenario
@testset "BlackBox()" begin
BB = BlackBox(2,3)

@test BB.num_in == 2
@test BB.num_out == 3
@test BB.num_in == 3
@test BB.num_out == 2

@test BB isa BlackBox

Expand Down
10 changes: 5 additions & 5 deletions test/unit/strategies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ using BellScenario

@test S == 1/2*[1 1 1;1 1 1]

@test S.scenario == BlackBox(3,2)
@test S.scenario == BlackBox(2,3)
end

@testset "Strategy DomainError" begin
scenario_error = BlackBox(4,3)
scenario_error = BlackBox(3,4)

@test_throws DomainError Strategy([1 0;0 1], scenario_error)
@test_throws DomainError Strategy([0.5 0.5;1 0.5])
Expand Down Expand Up @@ -173,15 +173,15 @@ end

@testset "strategy_dims()" begin
@testset "black box scenario" begin
@test strategy_dims(BlackBox(2,5)) == (5,2)
@test strategy_dims(BlackBox(5,2)) == (5,2)
end

@testset "prepare and measure" begin
@test strategy_dims(LocalSignaling(3,5,2)) == (5,3)
end

@testset "bipartite scenario" begin
@test strategy_dims(Bipartite((3,1),(1,4))) == (4,3)
@test strategy_dims(Bipartite((3,1),(1,4))) == (3,4)
@test strategy_dims(Bipartite((2,2),(2,2))) == (4,4)
end
end
Expand All @@ -192,7 +192,7 @@ end
@test D isa DeterministicStrategy
@test D.conditionals isa Matrix{Int64}
@test D == [1 0;0 1;0 0]
@test D.scenario == BlackBox(2,3)
@test D.scenario == BlackBox(3,2)

@test_throws DomainError DeterministicStrategy([0.5 0.5;0.5 0.5])
@test_throws DomainError DeterministicStrategy([1 0;0 1], BlackBox(3,2))
Expand Down

0 comments on commit 2dcea2d

Please sign in to comment.