Skip to content

Commit 45a4f71

Browse files
authored
Hyper Spin-glass model and Partition function (#55)
* hyper spin glass * new hyper spin glass model doc * fix doc make
1 parent 75f4fa5 commit 45a4f71

24 files changed

+270
-27
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
88
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
99
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
1010
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
11+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1112
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
1213
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
1314
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -25,8 +26,9 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
2526
TropicalNumbers = "b3a74e9c-7526-4576-a4eb-79c0d4c32334"
2627

2728
[compat]
28-
AbstractTrees = "0.4"
29+
AbstractTrees = "0.3, 0.4"
2930
CUDA = "3"
31+
DocStringExtensions = "0.8, 0.9"
3032
FFTW = "1.4"
3133
Graphs = "1.7"
3234
LuxorGraphPlot = "0.1"

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ makedocs(;
5757
"Satisfiability problem" => "generated/Satisfiability.md",
5858
"Set covering problem" => "generated/SetCovering.md",
5959
"Set packing problem" => "generated/SetPacking.md",
60+
"Hyper Spin glass problem" => "generated/HyperSpinGlass.md",
6061
#"Other problems" => "generated/Others.md",
6162
],
6263
"Topics" => [

docs/serve.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ function serve(;host::String="0.0.0.0", port::Int=8000)
99
LiveServer.servedocs(;
1010
doc_env=true,
1111
skip_dirs=[
12-
joinpath("docs", "src", "assets"),
13-
joinpath("docs", "src", "generated"),
14-
joinpath("docs", "src", "notebooks"),
12+
#joinpath("docs", "src", "generated"),
13+
joinpath("docs", "src"),
1514
joinpath("docs", "build"),
15+
joinpath("docs", "Manifest.toml"),
1616
],
1717
literate="examples",
1818
host=\"$host\",

docs/src/ref.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Matching
99
Coloring
1010
DominatingSet
1111
SpinGlass
12+
HyperSpinGlass
1213
MaxCut
1314
PaintShop
1415
Satisfiability
@@ -46,6 +47,7 @@ is_set_packing
4647
4748
cut_size
4849
spinglass_energy
50+
hyperspinglass_energy
4951
num_paint_shop_color_switch
5052
paint_shop_coloring_from_config
5153
mis_compactify!
@@ -65,6 +67,7 @@ print_mining
6567

6668
## Properties
6769
```@docs
70+
PartitionFunction
6871
SizeMax
6972
SizeMin
7073
CountingAll
@@ -117,6 +120,7 @@ hamming_distribution
117120
optimize_code
118121
getixsv
119122
getiyv
123+
contraction_complexity
120124
timespace_complexity
121125
timespacereadwrite_complexity
122126
estimate_memory

examples/HyperSpinGlass.jl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# # Hyper-Spin-glass problem
2+
3+
# !!! note
4+
# It is highly recommended to read the [Independent set problem](@ref) chapter before reading this one.
5+
6+
# ## Problem definition
7+
# The hyper-spin-glass problem is a natural generalization of the spin-glass problem from simple graph to hypergraph.
8+
# A hyper-spin-glass problem of hypergraph ``H = (V, E)`` can be characterized by the following energy function
9+
# ```math
10+
# E = \sum_{c \in E} w_{c} \prod_{v\in c} S_v
11+
# ```
12+
# where ``S_v \in \{-1, 1\}``, ``w_c`` is coupling strength associated with hyperedge ``c``.
13+
# In the program, we use boolean variable ``s_v = \frac{1-S_v}{2}`` to represent a spin configuration.
14+
15+
using GenericTensorNetworks
16+
17+
# In the following, we are going to defined an spin glass problem for the following hypergraph.
18+
num_vertices = 15
19+
20+
hyperedges = [[1,3,4,6,7], [4,7,8,12], [2,5,9,11,13],
21+
[1,2,14,15], [3,6,10,12,14], [8,14,15],
22+
[1,2,6,11], [1,2,4,6,8,12]]
23+
24+
weights = [-1, 1, -1, 1, -1, 1, -1, 1];
25+
26+
# The energy function is
27+
# ```math
28+
# \begin{align*}
29+
# E = &-s_1s_3s_4s_6s_7 + s_4s_7s_8s_{12} - s_2s_5s_9s_{11}s_{13} +\\
30+
# &s_1s_2s_{14}s_{15} - s_3s_6s_{10}s_{12}s_{14} + s_8s_{14}s_{15} +\\
31+
# &s_1s_2s_6s_{11} - s_1s_2s_4s_6s_8s_{12}
32+
# \end{align*}
33+
# ```
34+
35+
# ## Generic tensor network representation
36+
# We define an anti-ferromagnetic spin glass problem as
37+
problem = HyperSpinGlass(num_vertices, hyperedges; weights);
38+
39+
# ### Theory (can skip)
40+
# Let ``H = (V, E)`` be a hypergraph. The tensor network for the partition function of the energy model for ``H`` can be defined as the following tiple of (alphabet of labels, input tensors, output labels).
41+
# ```math
42+
# \begin{cases}
43+
# \Lambda &= \{s_v \mid v \in V\}\\
44+
# \mathcal{T} &= \{B^{(c)}_{s_{N(c, 1),N(c, 2),\ldots,N(c, d(c))}} \mid c \in E\} \cup \{W^{(v)}_{s_v} \mid v \in V\}\\
45+
# \sigma_o &= \varepsilon
46+
# \end{cases}
47+
# ```
48+
# where ``s_v \in \{0, 1\}`` is the boolean degreen associated to vertex ``v``,
49+
# ``N(c, k)`` is the ``k``th vertex of hyperedge ``c``, and ``d(c)`` is the degree of ``c``.
50+
# The edge tensor ``B^{(c)}`` is defined as
51+
# ```math
52+
# B^{(c)} = \begin{cases}
53+
# x^{w_c} & (\sum_{v\in c} s_v) \;is\; even, \\
54+
# x^{-w_c} & otherwise.
55+
# \end{cases}
56+
# ```
57+
# and the vertex tensor ``W^{(v)}`` (used to carry labels) is defined as
58+
# ```math
59+
# W^{(v)} = \left(\begin{matrix}1_v\\ 1_v\end{matrix}\right)
60+
# ```
61+
62+
# ## Solving properties
63+
# ### Minimum and maximum energies
64+
# Its ground state energy is -8.
65+
Emin = solve(problem, SizeMin())[]
66+
# While the state correspond to the highest energy has the ferromagnetic order.
67+
Emax = solve(problem, SizeMax())[]
68+
69+
# In this example, the spin configurations can be choosen to make all hyperedges having even or odd spin parity.
70+
71+
# ### Counting properties
72+
# ##### partition function and graph polynomial
73+
# The graph polynomial defined for the hyper-spin-glass problem is a Laurent polynomial
74+
# ```math
75+
# Z(G, w, x) = \sum_{k=E_{\rm min}}^{E_{\rm max}} c_k x^k,
76+
# ```
77+
# where ``E_{\rm min}`` and ``E_{\rm max}`` are minimum and maximum energies,
78+
# ``c_k`` is the number of spin configurations with energy ``k``.
79+
# Let the inverse temperature ``\beta = 2``, the partition function is
80+
β = 2.0
81+
Z = solve(problem, PartitionFunction(β))[]
82+
83+
# The infinite temperature partition function is the counting of all feasible configurations
84+
solve(problem, PartitionFunction(0.0))[]
85+
86+
# Let ``x = e^\beta``, it corresponds to the partition function of a spin glass at temperature ``\beta^{-1}``.
87+
poly = solve(problem, GraphPolynomial())[]
88+
89+
# ### Configuration properties
90+
# ##### finding a ground state
91+
ground_state = solve(problem, SingleConfigMin())[].c.data
92+
93+
Emin_verify = hyperspinglass_energy(hyperedges, ground_state; weights)
94+
95+
# You should see a consistent result as above `Emin`.

examples/IndependentSet.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ maximum_independent_set_size.n
7171
# We can count all independent sets with the [`CountingAll`](@ref) property.
7272
count_all_independent_sets = solve(problem, CountingAll())[]
7373

74-
# The return value has type `Float64`.
74+
# The return value has type `Float64`. The counting of all independent sets is equivalent to the infinite temperature partition function
75+
solve(problem, PartitionFunction(0.0))[]
7576

7677
# We can count the maximum independent sets with [`CountingMax`](@ref).
7778
count_maximum_independent_sets = solve(problem, CountingMax())[]

src/GenericTensorNetworks.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Polynomials
1111
using Polynomials: Polynomial, LaurentPolynomial, printpoly, fit
1212
using FFTW
1313
using Mods, Primes
14+
using DocStringExtensions
1415
using Base.Cartesian
1516
import AbstractTrees: children, printnode, print_tree
1617
import StatsBase
@@ -42,6 +43,7 @@ export IndependentSet, mis_compactify!, is_independent_set
4243
export MaximalIS, is_maximal_independent_set
4344
export cut_size, MaxCut
4445
export spinglass_energy, SpinGlass
46+
export hyperspinglass_energy, HyperSpinGlass
4547
export PaintShop, paintshop_from_pairs, num_paint_shop_color_switch, paint_shop_coloring_from_config
4648
export Coloring, is_vertex_coloring
4749
export Satisfiability, CNF, CNFClause, BoolVar, satisfiable, @bools, , ¬,
@@ -52,7 +54,7 @@ export SetCovering, is_set_covering
5254
export OpenPitMining, is_valid_mining, print_mining
5355

5456
# Interfaces
55-
export solve, SizeMax, SizeMin, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin, Single
57+
export solve, SizeMax, SizeMin, PartitionFunction, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin, Single
5658

5759
# Utilities
5860
export save_configs, load_configs, hamming_distribution, save_sumproduct, load_sumproduct

src/arithematics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ This algebra maps
198198
199199
Fields
200200
------------------------
201-
* `orders` is a vector of [`Tropical`](@ref) ([`CoutingTropical`](@ref)) numbers as the largest-K solution sizes (solutions).
201+
* `orders` is a vector of [`Tropical`](@ref) ([`CountingTropical`](@ref)) numbers as the largest-K solution sizes (solutions).
202202
203203
Examples
204204
------------------------------

src/interfaces.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ Counting the total number of sets. e.g. for the [`IndependentSet`](@ref) problem
4444
"""
4545
struct CountingAll <: AbstractProperty end
4646

47+
"""
48+
$TYPEDEF
49+
$FIELDS
50+
51+
Compute the partition function for the target problem.
52+
53+
* The corresponding tensor element type is `T`.
54+
"""
55+
struct PartitionFunction{T} <: AbstractProperty
56+
beta::T
57+
end
58+
4759
"""
4860
CountingMax{K} <: AbstractProperty
4961
CountingMax(K=Single)
@@ -213,12 +225,15 @@ Positional Arguments
213225
* `problem` is the graph problem with tensor network information,
214226
* `property` is string specifying the task. Using the maximum independent set problem as an example, it can be one of
215227
228+
* [`PartitionFunction`](@ref)`()` for computing the partition function,
229+
216230
* [`SizeMax`](@ref)`(k=Single)` for finding maximum-``k`` set sizes,
217231
* [`SizeMin`](@ref)`(k=Single)` for finding minimum-``k`` set sizes,
218232
219233
* [`CountingMax`](@ref)`(k=Single)` for counting configurations with maximum-``k`` sizes,
220234
* [`CountingMin`](@ref)`(k=Single)` for counting configurations with minimum-``k`` sizes,
221235
* [`CountingAll`](@ref)`()` for counting all configurations,
236+
* [`PartitionFunction`](@ref)`()` for counting all configurations,
222237
* [`GraphPolynomial`](@ref)`(; method=:finitefield, kwargs...)` for evaluating the graph polynomial,
223238
224239
* [`SingleConfigMax`](@ref)`(k=Single; bounded=false)` for finding one maximum-``k`` configuration for each size,
@@ -247,6 +262,8 @@ function solve(gp::GraphProblem, property::AbstractProperty; T=Float64, usecuda=
247262
return asarray(post_invert_exponent.(res), res)
248263
elseif property isa CountingAll
249264
return contractx(gp, one(T); usecuda=usecuda)
265+
elseif property isa PartitionFunction
266+
return contractx(gp, exp(property.beta); usecuda=usecuda)
250267
elseif property isa CountingMax{Single}
251268
return contractx(gp, _x(CountingTropical{T,T}; invert=false); usecuda=usecuda)
252269
elseif property isa CountingMin{Single}
@@ -353,29 +370,29 @@ function has_noninteger_weights(problem::GraphProblem)
353370
end
354371

355372
"""
356-
max_size(problem; usecuda=false)
373+
$TYPEDSIGNATURES
357374
358375
Returns the maximum size of the graph problem.
359376
A shorthand of `solve(problem, SizeMax(); usecuda=false)`.
360377
"""
361-
max_size(m::GraphProblem; usecuda=false) = Int(sum(solve(m, SizeMax(); usecuda=usecuda)).n) # floating point number is faster (BLAS)
378+
max_size(m::GraphProblem; usecuda=false)::Int = Int(sum(solve(m, SizeMax(); usecuda=usecuda)).n) # floating point number is faster (BLAS)
362379

363380
"""
364-
max_size_count(problem; usecuda=false)
381+
$TYPEDSIGNATURES
365382
366383
Returns the maximum size and the counting of the graph problem.
367384
It is a shorthand of `solve(problem, CountingMax(); usecuda=false)`.
368385
"""
369-
max_size_count(m::GraphProblem; usecuda=false) = (r = sum(solve(m, CountingMax(); usecuda=usecuda)); (Int(r.n), Int(r.c)))
386+
max_size_count(m::GraphProblem; usecuda=false)::Tuple{Int,Int} = (r = sum(solve(m, CountingMax(); usecuda=usecuda)); (Int(r.n), Int(r.c)))
370387

371388
########## memory estimation ###############
372389
"""
373-
estimate_memory(problem, property; T=Float64)
390+
$TYPEDSIGNATURES
374391
375392
Memory estimation in number of bytes to compute certain `property` of a `problem`.
376393
`T` is the base type.
377394
"""
378-
function estimate_memory(problem::GraphProblem, property::AbstractProperty; T=Float64)
395+
function estimate_memory(problem::GraphProblem, property::AbstractProperty; T=Float64)::Real
379396
_estimate_memory(tensor_element_type(T, length(labels(problem)), nflavor(problem), property), problem)
380397
end
381398
function estimate_memory(problem::GraphProblem, property::Union{SingleConfigMax{K,BOUNDED},SingleConfigMin{K,BOUNDED}}; T=Float64) where {K, BOUNDED}
@@ -419,6 +436,7 @@ function _estimate_memory(::Type{ET}, problem::GraphProblem) where ET
419436
end
420437

421438
for (PROP, ET) in [
439+
(:(PartitionFunction{T}), :(T)),
422440
(:(SizeMax{Single}), :(Tropical{T})), (:(SizeMin{Single}), :(Tropical{T})),
423441
(:(SizeMax{K}), :(ExtendedTropical{K,Tropical{T}})), (:(SizeMin{K}), :(ExtendedTropical{K,Tropical{T}})),
424442
(:(CountingAll), :T), (:(CountingMax{Single}), :(CountingTropical{T,T})), (:(CountingMin{Single}), :(CountingTropical{T,T})),

src/networks/Coloring.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
fixedvertices=Dict()
66
)
77
8-
The [Vertex Coloring](https://psychic-meme-f4d866f8.pages.github.io/dev/generated/Coloring.html) problem.
8+
The [Vertex Coloring](https://queracomputing.github.io/GenericTensorNetworks.jl/dev/generated/Coloring/) problem.
99
1010
Positional arguments
1111
-------------------------------

0 commit comments

Comments
 (0)