Skip to content

Commit 1eaef5c

Browse files
authored
Merge pull request #1 from c-allergic/dev_pr
Changes based on new `ProblemReductions`
2 parents 88705e0 + c10652a commit 1eaef5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+458
-1705
lines changed

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Updates in v3.0
2+
3+
1. The solution size of `Coloring`/`Satisfiability` is now defined as the number of violations of colors/clauses. The smaller the better now.
4+
2. Rename `best_solutions` to `largest_solutions`, `best2_solutions` to `largest2_solutions` and `bestk_solutions` to `largestk_solutions`.
5+
3. Remove the weights from `PaintShop`.
6+
4. Remove the weights on vertices from `MaxCut`.
7+
5. `SpinGlass` is no longer specified by cliques. It is now specified by graphs or hypergraphs. Weights can be defined on both edges and vertices.
8+
6. Remove `unit_disk_graph`, replace it with `UnitDiskGraph` from `ProblemReductions`.

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LuxorGraphPlot = "1f49bdf2-22a7-4bc4-978b-948dc219fbbc"
1515
OMEinsum = "ebe7aa44-baf0-506c-a96f-8464559b3922"
1616
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
1717
Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae"
18-
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
18+
ProblemReductions = "899c297d-f7d2-4ebf-8815-a35996def416"
1919
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2020
SIMDTypes = "94e857df-77ce-4151-89e5-788b33177be4"
2121
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
@@ -41,7 +41,7 @@ LuxorGraphPlot = "0.5"
4141
OMEinsum = "0.8"
4242
Polynomials = "4"
4343
Primes = "0.5"
44-
Printf = "1"
44+
ProblemReductions = "0.2"
4545
Random = "1"
4646
SIMDTypes = "0.1"
4747
Serialization = "1"

docs/make.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Pkg
22
using GenericTensorNetworks
3-
using GenericTensorNetworks: TropicalNumbers, Polynomials, OMEinsum, OMEinsum.OMEinsumContractionOrders, LuxorGraphPlot
3+
using GenericTensorNetworks: TropicalNumbers, Polynomials, OMEinsum, OMEinsum.OMEinsumContractionOrders, LuxorGraphPlot, ProblemReductions
44
using Documenter
55
using DocThemeIndigo
66
using Literate
@@ -17,7 +17,7 @@ indigo = DocThemeIndigo.install(GenericTensorNetworks)
1717
DocMeta.setdocmeta!(GenericTensorNetworks, :DocTestSetup, :(using GenericTensorNetworks); recursive=true)
1818

1919
makedocs(;
20-
modules=[GenericTensorNetworks, TropicalNumbers, OMEinsum, OMEinsumContractionOrders, LuxorGraphPlot],
20+
modules=[GenericTensorNetworks, ProblemReductions, TropicalNumbers, OMEinsum, OMEinsumContractionOrders, LuxorGraphPlot],
2121
authors="Jinguo Liu",
2222
repo="https://github.com/QuEraComputing/GenericTensorNetworks.jl/blob/{commit}{path}#{line}",
2323
sitename="GenericTensorNetworks.jl",
@@ -40,7 +40,6 @@ makedocs(;
4040
"Satisfiability problem" => "generated/Satisfiability.md",
4141
"Set covering problem" => "generated/SetCovering.md",
4242
"Set packing problem" => "generated/SetPacking.md",
43-
#"Other problems" => "generated/Others.md",
4443
],
4544
"Topics" => [
4645
"Gist" => "gist.md",

docs/src/ref.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# References
2-
## Graph problems
2+
## Constraint Satisfaction Problems
33
```@docs
44
solve
55
GenericTensorNetwork
6-
GraphProblem
6+
ConstraintSatisfactionProblem
77
IndependentSet
88
MaximalIS
99
Matching
@@ -15,28 +15,35 @@ PaintShop
1515
Satisfiability
1616
SetCovering
1717
SetPacking
18-
OpenPitMining
1918
```
2019

21-
#### Graph Problem Interfaces
20+
#### Constraint Satisfaction Problem Interfaces
2221

23-
To subtype [`GraphProblem`](@ref), a new type must contain a `code` field to represent the (optimized) tensor network.
24-
Interfaces [`GenericTensorNetworks.generate_tensors`](@ref), [`labels`](@ref), [`flavors`](@ref) and [`get_weights`](@ref) are required.
25-
[`nflavor`](@ref) is optional.
22+
To subtype [`ConstraintSatisfactionProblem`](@ref), a new type must contain a `code` field to represent the (optimized) tensor network.
23+
Interfaces [`GenericTensorNetworks.generate_tensors`](@ref), [`flavors`](@ref) and [`weights`](@ref) are required.
24+
[`num_flavors`](@ref) is optional.
2625

2726
```@docs
2827
GenericTensorNetworks.generate_tensors
29-
labels
30-
energy_terms
3128
flavors
32-
get_weights
33-
chweights
34-
nflavor
29+
weights
30+
set_weights
31+
is_weighted
32+
num_flavors
3533
fixedvertices
3634
```
3735

38-
#### Graph Problem Utilities
36+
#### Constraint Satisfaction Problem Utilities
3937
```@docs
38+
hard_constraints
39+
is_satisfied
40+
local_solution_spec
41+
solution_size
42+
energy_mode
43+
LargerSizeIsBetter
44+
SmallerSizeIsBetter
45+
energy
46+
4047
is_independent_set
4148
is_maximal_independent_set
4249
is_dominating_set
@@ -46,10 +53,7 @@ is_set_covering
4653
is_set_packing
4754
4855
cut_size
49-
spinglass_energy
5056
num_paint_shop_color_switch
51-
paint_shop_coloring_from_config
52-
mis_compactify!
5357
5458
CNF
5559
CNFClause
@@ -60,8 +64,7 @@ satisfiable
6064
¬
6165
6266
63-
is_valid_mining
64-
print_mining
67+
mis_compactify!
6568
```
6669

6770
## Properties
@@ -145,7 +148,12 @@ MergeGreedy
145148

146149
## Others
147150
#### Graph
151+
Except the `SimpleGraph` defined in [Graphs](https://github.com/JuliaGraphs/Graphs.jl), `GenericTensorNetworks` also defines the following types and functions.
152+
148153
```@docs
154+
HyperGraph
155+
UnitDiskGraph
156+
149157
show_graph
150158
show_configs
151159
show_einsum
@@ -162,7 +170,6 @@ render_locs
162170
163171
diagonal_coupled_graph
164172
square_lattice_graph
165-
unit_disk_graph
166173
line_graph
167174
168175
random_diagonal_coupled_graph

docs/src/tensornetwork.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# An introduction to tensor networks
2+
3+
Let $G = (V, E)$ be a hypergraph, where $V$ is the set of vertices and $E$ is the set of hyperedges. Each vertex $v \in V$ is associated with a local variable, e.g. "spin" and "bit". A hyperedge $e \in E$ is a subset of vertices $e \subseteq V$. On top of which, we can define a local Hamiltonian $H$ as a sum of local terms $h_e$ over all hyperedges $e \in E$:
4+
5+
```math
6+
H(\sigma) = \sum_{e \in E} h_e(\sigma_e)
7+
```
8+
9+
where $\sigma_e$ is the restriction of the configuration $\sigma$ to the vertices in $e$.
10+
11+
The following solution space properties are of interest:
12+
13+
* The partition function,
14+
```math
15+
Z = \sum_{\sigma} e^{-\beta H(\sigma)}
16+
```
17+
where $\beta$ is the inverse temperature.
18+
* The maximum/minimum solution sizes,
19+
```math
20+
\max_{\sigma} H(\sigma), \min_{\sigma} H(\sigma)
21+
```
22+
* The number of solutions at certain sizes,
23+
```math
24+
N(k) = \sum_{\sigma} \delta(k, H(\sigma))
25+
```
26+
* The enumeration of solutions at certain sizes.
27+
```math
28+
S = \{ \sigma | H(\sigma) = k \}
29+
```
30+
* The direct sampling of solutions at certain sizes.
31+
```math
32+
\sigma \sim S
33+
```
34+
35+
## Tensor network representation
36+
37+
### Partition function
38+
It is well known that the partition function of an energy model can be represented as a tensor network[^Levin2007]. The partition function can be written in a sum-product form as
39+
```math
40+
Z = \sum_{\sigma} e^{-\beta H(\sigma)} = \sum_{\sigma} \prod_{e \in E} T_e(\sigma_e)
41+
```
42+
where $T_e(\sigma_e) = e^{-\beta h_e(\sigma_e)}$ is a tensor associated with the hyperedge $e$.
43+
44+
This sum-product form is directly related to a tensor network $(V, \{T_{\sigma_e} \mid e\in E\}, \emptyset)$, where $T_{\sigma_e}$ is a tensor labeled by $\sigma_e \subseteq V$, and its elements are defined by $T_{\sigma_e}= T_e(\sigma_e)$. $\emptyset$ is the set of open vertices in a tensor network, which are not summed over.
45+
46+
### Maximum/minimum solution sizes
47+
The maximum/minimum solution sizes can be represented as a tensor network as well. The maximum solution size can be written as
48+
```math
49+
\max_{\sigma} H(\sigma) = \max_{\sigma} \sum_{e \in E} h_e(\sigma_e)
50+
```
51+
which can be represented as a tropical tensor network[^Liu2021] $(V, \{h_{\sigma_e} \mid e\in E\}, \emptyset)$, where $h_{\sigma_e}$ is a tensor labeled by $\sigma_e \subseteq V$, and its elements are defined by $h_{\sigma_e}= h_e(\sigma_e)$.
52+
53+
## Problems
54+
The independent set problem on graph $G=(V, E)$ is characterized by the Hamiltonian
55+
```math
56+
H(\sigma) = U \sum_{(i, j) \in E} n_i n_j - \sum_{i \in V} n_i
57+
```
58+
where $n_i \in \{0, 1\}$ is a binary variable associated with vertex $i$, and $U\rightarrow \infty$ is a large constant. The goal is to find the maximum independent set, i.e. the maximum number of vertices such that no two vertices are connected by an edge.
59+
The partition function for an independent set problem is
60+
```math
61+
Z = \sum_{\sigma} e^{-\beta H(\sigma)} = \sum_{\sigma} \prod_{(i, j) \in E} e^{-\beta U n_in_j} \prod_{i \in V} e^{\beta n_i}
62+
```
63+
64+
Let $x = e^{\beta}$, the partition function can be written as
65+
```math
66+
Z = \sum_{\sigma} \prod_{(i, j) \in E} B_{n_in_j} \prod_{i \in V} W_{n_i}
67+
```
68+
where $B_{n_in_j} = \lim_{U \rightarrow \infty} e^{-U \beta n_in_j}=\begin{cases}0, \quad n_in_j = 1\\1,\quad n_in_j = 0\end{cases}$ and $W_{n_i} = x^{n_i}$ are tensors associated with the hyperedge $(i, j)$ and the vertex $i$, respectively.
69+
70+
The tensor network representation for the partition function is
71+
```math
72+
\mathcal{N}_{IS} = (\Lambda, \{B_{n_in_j} \mid (i, j)\in E\} \cup \{W_{n_i} \mid i\in \Lambda\}, \emptyset)
73+
```
74+
where $\Lambda = \{n_i \mid i \in V\}$ is the set of binary variables, $B_{n_in_j}$ is a tensor associated with the hyperedge $(i, j)$ and $W_{n_i}$ is a tensor associated with the vertex $i$. The tensors are defined as
75+
```math
76+
W = \left(\begin{matrix}
77+
1 \\
78+
x
79+
\end{matrix}\right)
80+
```
81+
where $x$ is a variable associated with $v$.
82+
```math
83+
B = \left(\begin{matrix}
84+
1 & 1\\
85+
1 & 0
86+
\end{matrix}\right).
87+
```
88+
89+
The contraction of the tensor network $\mathcal{N}_{IS}$ gives the partition function $Z$. It is implicitly assumed that the tensor elements are real numbers.
90+
91+
However, by replacing the tensor elements with tropical numbers, the tensor network $\mathcal{N}_{IS}$ can be used to compute the maximum independent set size and its degeneracy[^Liu2021].
92+
93+
An algebra can be defined by
94+
```math
95+
\begin{align*}
96+
\oplus &= \max\\
97+
\otimes &= +
98+
\end{align*}
99+
```
100+
101+
[^Levin2007]: Levin, M., Nave, C.P., 2007. Tensor renormalization group approach to two-dimensional classical lattice models. Physical Review Letters 99, 1–4. https://doi.org/10.1103/PhysRevLett.99.120601
102+
[^Liu2021]: Liu, J.-G., Wang, L., Zhang, P., 2021. Tropical Tensor Network for Ground States of Spin Glasses. Phys. Rev. Lett. 126, 090506. https://doi.org/10.1103/PhysRevLett.126.090506

examples/Coloring.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ problem = GenericTensorNetwork(coloring)
4646

4747
# ## Solving properties
4848
# ##### counting all possible coloring
49-
num_of_coloring = solve(problem, CountingMax())[]
49+
# The size of a coloring problem is the number of violations of the coloring constraint.
50+
num_of_coloring = solve(problem, CountingMin())[]
5051

5152
# ##### finding one best coloring
52-
single_solution = solve(problem, SingleConfigMax())[]
53+
single_solution = solve(problem, SingleConfigMin())[]
5354
read_config(single_solution)
5455

5556
is_vertex_coloring(graph, read_config(single_solution))
@@ -68,7 +69,7 @@ show_graph(linegraph, [(locations[e.src] .+ locations[e.dst])
6869
# Let us construct the tensor network and see if there are solutions.
6970
lineproblem = Coloring{3}(linegraph);
7071

71-
num_of_coloring = solve(GenericTensorNetwork(lineproblem), CountingMax())[]
72+
num_of_coloring = solve(GenericTensorNetwork(lineproblem), CountingMin())[]
7273
read_size_count(num_of_coloring)
7374

7475
# You will see the maximum size 28 is smaller than the number of edges in the `linegraph`,

examples/PaintShop.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# In the following, we use a character to represent a car,
1313
# and defined a binary paint shop problem as a string that each character appear exactly twice.
1414

15-
using GenericTensorNetworks, Graphs
15+
using GenericTensorNetworks, Graphs, GenericTensorNetworks.ProblemReductions
1616

1717
sequence = collect("iadgbeadfcchghebif")
1818

@@ -88,7 +88,7 @@ best_configs = solve(problem, ConfigsMin())[]
8888

8989
# One can see to identical bitstrings corresponding two different vertex configurations, they are related to bit-flip symmetry.
9090

91-
painting1 = paint_shop_coloring_from_config(pshop, read_config(best_configs)[1])
91+
painting1 = ProblemReductions.paint_shop_coloring_from_config(pshop, read_config(best_configs)[1])
9292

9393
show_graph(graph, locations; format=:svg, texts=string.(sequence),
9494
edge_colors=[sequence[e.src] == sequence[e.dst] ? "blue" : "black" for e in edges(graph)],

examples/Satisfiability.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# One can specify a satisfiable problem in the [conjunctive normal form](https://en.wikipedia.org/wiki/Conjunctive_normal_form).
99
# In boolean logic, a formula is in conjunctive normal form (CNF) if it is a conjunction (∧) of one or more clauses,
1010
# where a clause is a disjunction (∨) of literals.
11-
using GenericTensorNetworks
11+
using GenericTensorNetworks, GenericTensorNetworks.ProblemReductions
1212

1313
@bools a b c d e f g
1414

@@ -48,15 +48,15 @@ problem = GenericTensorNetwork(sat)
4848

4949
# ## Solving properties
5050
# #### Satisfiability and its counting
51-
# The size of a satisfiability problem is defined by the number of satisfiable clauses.
52-
num_satisfiable = solve(problem, SizeMax())[]
51+
# The size of a satisfiability problem is defined by the number of unsatisfied clauses.
52+
num_satisfiable = solve(problem, SizeMin())[]
5353

5454
# The [`GraphPolynomial`](@ref) of a satisfiability problem counts the number of solutions that `k` clauses satisfied.
5555
num_satisfiable_count = read_size_count(solve(problem, GraphPolynomial())[])
5656

5757
# #### Find one of the solutions
58-
single_config = read_config(solve(problem, SingleConfigMax())[])
58+
single_config = read_config(solve(problem, SingleConfigMin())[])
5959

6060
# One will see a bit vector printed.
6161
# One can create an assignment and check the validity with the following statement:
62-
satisfiable(cnf, Dict(zip(labels(problem), single_config)))
62+
satisfiable(cnf, Dict(zip(ProblemReductions.symbols(problem.problem), single_config)))

examples/SetCovering.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ min_configs = read_config(solve(problem, ConfigsMin())[])
7373
# Hence the two optimal solutions are ``\{z_1, z_3, z_5, z_6\}`` and ``\{z_2, z_3, z_4, z_5\}``.
7474
# The correctness of this result can be checked with the [`is_set_covering`](@ref) function.
7575

76-
all(c->is_set_covering(sets, c), min_configs)
76+
all(c->is_set_covering(problem.problem, c), min_configs)
7777

7878
# Similarly, if one is only interested in computing one of the minimum set coverings,
7979
# one can use the graph property [`SingleConfigMin`](@ref).

examples/SetPacking.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ max_configs = read_config(solve(problem, ConfigsMax())[])
7474
# Hence the only optimal solution is ``\{z_1, z_3, z_6\}`` that has size 3.
7575
# The correctness of this result can be checked with the [`is_set_packing`](@ref) function.
7676

77-
all(c->is_set_packing(sets, c), max_configs)
77+
all(c->is_set_packing(problem.problem, c), max_configs)
7878

7979
# Similarly, if one is only interested in computing one of the maximum set packing,
8080
# one can use the graph property [`SingleConfigMax`](@ref).

0 commit comments

Comments
 (0)