Skip to content

Commit 82b7439

Browse files
committed
Max Cut
1 parent a7c59d3 commit 82b7439

File tree

6 files changed

+40
-32
lines changed

6 files changed

+40
-32
lines changed

src/bitvector.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ function Base.iterate(x::StaticBitVector{N,C}, state=1) where {N,C}
4444
end
4545
end
4646

47+
Base.show(io::IO, t::StaticBitVector) = Base.print(io, "$(join(Int.(t), ""))")

src/configurations.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ function optimalsolutions(gp::GraphProblem; all=false, usecuda=false)
1212
if all && usecuda
1313
throw(ArgumentError("ConfigEnumerator can not be computed on GPU!"))
1414
end
15-
T = (all ? bitstringset_type : bitstringsampler_type)(CountingTropical{Int64}, length(labels(gp.code)))
16-
syms = labels(gp.code)
15+
syms = symbols(gp)
16+
T = (all ? bitstringset_type : bitstringsampler_type)(CountingTropical{Int64}, length(syms))
1717
vertex_index = Dict([s=>i for (i, s) in enumerate(syms)])
1818
xst = generate_tensors(l->TropicalF64(1.0), gp)
1919
ymask = trues(fill(2, length(OMEinsum.getiy(flatten(gp.code))))...)
@@ -53,7 +53,13 @@ end
5353

5454
# return a mapping from label to variable `x`
5555
function fx_solutions(gp::GraphProblem, ::Type{BT}, all::Bool) where BT
56-
T = (all ? bitstringset_type : bitstringsampler_type)(BT, length(labels(gp.code)))
57-
vertex_index = Dict([s=>i for (i, s) in enumerate(labels(gp.code))])
56+
syms = symbols(gp)
57+
T = (all ? bitstringset_type : bitstringsampler_type)(BT, length(syms))
58+
vertex_index = Dict([s=>i for (i, s) in enumerate(syms)])
5859
return l->onehotv(T, vertex_index[l])
59-
end
60+
end
61+
for GP in [:Independence, :Matching, :MaximalIndependence]
62+
@eval symbols(gp::$GP) = labels(gp.code)
63+
end
64+
symbols(gp::MaxCut) = collect(OMEinsum.getixs(OMEinsum.flatten(gp.code)))
65+
# TODO: coloring

src/graph_polynomials.jl

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,16 @@ end
197197

198198
graph_polynomial_maxorder(mi::MaximalIndependence; usecuda) = Int(sum(contractx(mi, TropicalF64(1.0); usecuda=usecuda)).n)
199199

200-
### spin glass problem ###
201-
function generate_tensors(fx, gp::SpinGlass{2})
200+
### max cut/spin glass problem ###
201+
function generate_tensors(fx, gp::MaxCut)
202202
flatten_code = flatten(gp.code)
203203
ixs = getixs(flatten_code)
204-
n = length(labels(flatten_code))
205-
T = typeof(fx(ixs[1][1]))
206204
return Tuple(map(enumerate(ixs)) do (i, ix)
207-
if i <= n
208-
spinglassv(one(T))
209-
else
210-
spinglassb(fx(ix)) # if n!=2, it corresponds to set packing problem.
211-
end
205+
maxcutb(fx(ix)) # if n!=2, it corresponds to set packing problem.
212206
end)
213207
end
214-
function spinglassb(expJ::T) where T
208+
function maxcutb(expJ::T) where T
215209
return T[one(T) expJ; expJ one(T)]
216210
end
217-
spinglassv(h::T) where T = T[one(T), h]
218211

219-
graph_polynomial_maxorder(mi::SpinGlass; usecuda) = Int(sum(contractx(mi, TropicalF64(1.0); usecuda=usecuda)).n)
212+
graph_polynomial_maxorder(mi::MaxCut; usecuda) = Int(sum(contractx(mi, TropicalF64(1.0); usecuda=usecuda)).n)

src/networks.jl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export Independence, MaximalIndependence, Matching, Coloring, optimize_code, set_packing, SpinGlass
1+
export Independence, MaximalIndependence, Matching, Coloring, optimize_code, set_packing, MaxCut
22
const EinTypes = Union{EinCode,NestedEinsum}
33

44
abstract type GraphProblem end
@@ -20,20 +20,18 @@ function Independence(g::SimpleGraph; outputs=(), kwargs...)
2020
end
2121

2222
"""
23-
SpinGlass{Q, CT<:EinTypes} <: GraphProblem
24-
SpinGlass{Q}(graph; kwargs...)
25-
SpinGlass(graph; kwargs...)
23+
MaxCut{CT<:EinTypes} <: GraphProblem
24+
MaxCut(graph; kwargs...)
2625
27-
Q-state spin glass problem (or Potts model). For `kwargs`, check `optimize_code` API.
28-
When Q=2, it corresponds to the {0, 1} spin glass model.
26+
Max cut problem (or spin glass problem). For `kwargs`, check `optimize_code` API.
2927
"""
30-
struct SpinGlass{Q, CT<:EinTypes} <: GraphProblem
28+
struct MaxCut{CT<:EinTypes} <: GraphProblem
3129
code::CT
3230
end
33-
34-
SpinGlass(g::SimpleGraph; outputs=(), kwargs...) = SpinGlass{2}(g; outputs=outputs, kwargs...)
35-
SpinGlass{Q}(g::SimpleGraph; outputs=(), kwargs...) where Q = SpinGlass{Q}(Independence(g; outputs=outputs, kwargs...).code)
36-
SpinGlass{Q}(code::EinTypes) where Q = SpinGlass{Q,typeof(code)}(code)
31+
function MaxCut(g::SimpleGraph; outputs=(), kwargs...)
32+
rawcode = EinCode(([minmax(e.src,e.dst) for e in LightGraphs.edges(g)]...,), outputs) # labels for edge tensors
33+
MaxCut(optimize_code(rawcode; kwargs...))
34+
end
3735

3836
"""
3937
MaximalIndependence{CT<:EinTypes} <: GraphProblem
@@ -127,11 +125,10 @@ end
127125

128126
OMEinsum.timespace_complexity(gp::GraphProblem) = timespace_complexity(gp.code, uniformsize(gp.code, bondsize(gp)))
129127

130-
for T in [:Independence, :Matching, :MaximalIndependence]
128+
for T in [:Independence, :Matching, :MaximalIndependence, :MaxCut]
131129
@eval bondsize(gp::$T) = 2
132130
end
133131
bondsize(gp::Coloring{K}) where K = K
134-
bondsize(gp::SpinGlass{Q}) where Q = Q
135132

136133
"""
137134
set_packing(sets; kwargs...)

test/configurations.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ end
5252
@test BitVector(Bool[0,0,1,1,0]) res.c.data
5353
@test BitVector(Bool[1,0,0,1,0]) res.c.data
5454
@test BitVector(Bool[0,1,1,0,0]) res.c.data
55+
end
56+
57+
@testset "enumerating - max cut" begin
58+
g = SimpleGraph(5)
59+
for (i,j) in [(1,2),(2,3),(3,4),(4,1),(1,5),(2,4)]
60+
add_edge!(g, i, j)
61+
end
62+
code = MaxCut(g; optmethod=:greedy)
63+
res = optimalsolutions(code; all=true)[]
64+
@test length(res.c.data) == 2
65+
@test sum(res.c.data[1]) == 5
5566
end

test/graph_polynomials.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ end
7474
for (i,j) in [(1,2),(2,3),(3,4),(4,1),(1,5),(2,4)]
7575
add_edge!(g, i, j)
7676
end
77-
@test graph_polynomial(SpinGlass{2}, Val(:polynomial), g)[] == Polynomial([2,2,4,12,10,2])
78-
@test graph_polynomial(SpinGlass{2}, Val(:finitefield), g)[] == Polynomial([2,2,4,12,10,2])
77+
@test graph_polynomial(MaxCut, Val(:polynomial), g)[] == Polynomial([2,2,4,12,10,2])
78+
@test graph_polynomial(MaxCut, Val(:finitefield), g)[] == Polynomial([2,2,4,12,10,2])
7979
end

0 commit comments

Comments
 (0)