Skip to content

Commit c0576c8

Browse files
Changes based on new version of ProblemReductions (#88)
* "Dependences: maybe different with paths" * "New codes in src/" * "New codes in test/" * docs and example * fix tests * fix documents * fix documents * rm Printf * fix set-packing test * correct . * deprecate GraphProblem, chweights,ZeroWeight and UnitWeight * fix test * fix cuda tests --------- Co-authored-by: GiggleLiu <cacate0129@gmail.com>
1 parent 88705e0 commit c0576c8

Some content is hidden

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

58 files changed

+422
-1769
lines changed

Project.toml

+2-2
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

+2-3
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

+26-19
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

examples/Coloring.jl

+4-3
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

+2-2
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

+5-5
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

+1-1
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

+1-1
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).

examples/SpinGlass.jl

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ show_graph(graph, locations; format=:svg)
2626

2727
# ## Generic tensor network representation
2828
# An anti-ferromagnetic spin glass problem can be defined with the [`SpinGlass`](@ref) type as
29-
spinglass = SpinGlass(graph, fill(1, ne(graph)))
29+
spinglass = SpinGlass(graph, fill(1, ne(graph)), zeros(Int, nv(graph)))
3030

3131
# The tensor network representation of the set packing problem can be obtained by
3232
problem = GenericTensorNetwork(spinglass)
@@ -81,8 +81,10 @@ partition_function = solve(problem, GraphPolynomial())[]
8181
# The ground state of the spin glass problem can be found by the [`SingleConfigMin`](@ref) solver.
8282
ground_state = read_config(solve(problem, SingleConfigMin())[])
8383

84-
# The energy of the ground state can be verified by the [`spinglass_energy`](@ref) function.
85-
Emin_verify = spinglass_energy(spinglass, ground_state)
84+
# The energy of the ground state can be verified by the [`energy`](@ref) function.
85+
# Note the output of the ground state can not be directly used as the input of the `energy` function.
86+
# It needs to be converted to the spin configurations.
87+
Emin_verify = energy(problem.problem, 1 .- 2 .* Int.(ground_state))
8688

8789
# You should see a consistent result as above `Emin`.
8890

@@ -117,7 +119,7 @@ weights = [-1, 1, -1, 1, -1, 1, -1, 1];
117119
# \end{align*}
118120
# ```
119121
# A spin glass problem can be defined with the [`SpinGlass`](@ref) type as
120-
hyperspinglass = SpinGlass(num_vertices, hyperedges, weights)
122+
hyperspinglass = SpinGlass(HyperGraph(num_vertices, hyperedges), weights, zeros(Int, num_vertices))
121123

122124
# The tensor network representation of the set packing problem can be obtained by
123125
hyperproblem = GenericTensorNetwork(hyperspinglass)
@@ -155,8 +157,8 @@ poly = solve(hyperproblem, GraphPolynomial())[]
155157
# The ground state of the spin glass problem can be found by the [`SingleConfigMin`](@ref) solver.
156158
ground_state = read_config(solve(hyperproblem, SingleConfigMin())[])
157159

158-
# The energy of the ground state can be verified by the [`spinglass_energy`](@ref) function.
160+
# The energy of the ground state can be verified by the [`energy`](@ref) function.
159161

160-
Emin_verify = spinglass_energy(hyperspinglass, ground_state)
162+
Emin_verify = energy(hyperproblem.problem, 1 .- 2 .* Int.(ground_state))
161163

162164
# You should see a consistent result as above `Emin`.

examples/weighted.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# # Weighted problems
22
# Let us use the maximum independent set problem on Petersen graph as an example.
33

4-
using GenericTensorNetworks, Graphs
4+
using GenericTensorNetworks, GenericTensorNetworks.ProblemReductions, Graphs
55

66
graph = Graphs.smallgraph(:petersen)
77

88
# The following code constructs a weighted MIS problem instance.
99
problem = GenericTensorNetwork(IndependentSet(graph, collect(1:10)));
10-
GenericTensorNetworks.get_weights(problem)
10+
GenericTensorNetworks.weights(problem)
1111

1212
# The tensor labels that associated with the weights can be accessed by
13-
GenericTensorNetworks.energy_terms(problem)
13+
ProblemReductions.local_solution_spec(problem.problem)
1414

1515
# Here, the `weights` keyword argument can be a vector for weighted graphs or `UnitWeight()` for unweighted graphs.
1616
# Most solution space properties work for unweighted graphs also work for the weighted graphs.

src/GenericTensorNetworks.jl

+19-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using TropicalNumbers
55
using OMEinsum
66
using OMEinsum: contraction_complexity, timespace_complexity, timespacereadwrite_complexity, getixsv, NestedEinsum, getixs, getiy, DynamicEinCode
77
using Graphs, Random
8-
using DelimitedFiles, Serialization, Printf
8+
using DelimitedFiles, Serialization
99
using LuxorGraphPlot
1010
using LuxorGraphPlot.Luxor.Colors: @colorant_str
1111
using LuxorGraphPlot: Layered
@@ -15,6 +15,12 @@ using FFTW
1515
using Primes
1616
using DocStringExtensions
1717
using Base.Cartesian
18+
using ProblemReductions
19+
import ProblemReductions: ConstraintSatisfactionProblem, AbstractSatisfiabilityProblem, UnitWeight, hard_constraints, is_satisfied, local_solution_spec, solution_size, energy_mode, LargerSizeIsBetter, SmallerSizeIsBetter
20+
import ProblemReductions: @bv_str, StaticElementVector, StaticBitVector, onehotv, _nints, hamming_distance
21+
import ProblemReductions: is_set_covering, is_vertex_coloring, is_set_packing, is_dominating_set, is_matching, is_maximal_independent_set, cut_size, is_independent_set
22+
import ProblemReductions: num_paint_shop_color_switch, spin_glass_from_matrix, CNF, CNFClause, BoolVar, satisfiable, @bools, , ¬,
23+
import ProblemReductions: flavors, set_weights, weights, is_weighted, num_flavors, variables, energy
1824
import AbstractTrees: children, printnode, print_tree
1925
import StatsBase
2026

@@ -34,28 +40,32 @@ export CountingTropicalF64, CountingTropicalF32, TropicalF64, TropicalF32, Exten
3440
export generate_samples, OnehotVec
3541

3642
# Graphs
43+
export HyperGraph, SimpleGraph, UnitDiskGraph
3744
export random_regular_graph, diagonal_coupled_graph
38-
export square_lattice_graph, unit_disk_graph, random_diagonal_coupled_graph, random_square_lattice_graph
45+
export square_lattice_graph, random_diagonal_coupled_graph, random_square_lattice_graph
3946
export line_graph
4047

48+
49+
# Problems
50+
export AbstractProblem, ConstraintSatisfactionProblem
51+
export hard_constraints, is_satisfied, local_solution_spec, solution_size, energy_mode, LargerSizeIsBetter, SmallerSizeIsBetter
4152
# Tensor Networks (Graph problems)
42-
export GraphProblem, GenericTensorNetwork, optimize_code, UnitWeight, ZeroWeight
43-
export flavors, labels, nflavor, get_weights, fixedvertices, chweights, energy_terms
53+
export GenericTensorNetwork, optimize_code, UnitWeight
54+
export flavors, variables, num_flavors, weights, fixedvertices, set_weights, is_weighted
4455
export IndependentSet, mis_compactify!, is_independent_set
4556
export MaximalIS, is_maximal_independent_set
4657
export cut_size, MaxCut
47-
export spinglass_energy, spin_glass_from_matrix, SpinGlass
48-
export PaintShop, paintshop_from_pairs, num_paint_shop_color_switch, paint_shop_coloring_from_config, paint_shop_from_pairs
58+
export energy, spin_glass_from_matrix, SpinGlass
59+
export PaintShop, paintshop_from_pairs, num_paint_shop_color_switch
4960
export Coloring, is_vertex_coloring
5061
export Satisfiability, CNF, CNFClause, BoolVar, satisfiable, @bools, , ¬,
5162
export DominatingSet, is_dominating_set
5263
export Matching, is_matching
5364
export SetPacking, is_set_packing
5465
export SetCovering, is_set_covering
55-
export OpenPitMining, is_valid_mining, print_mining
5666

5767
# Interfaces
58-
export solve, SizeMax, SizeMin, PartitionFunction, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin, Single
68+
export solve, SizeMax, SizeMin, PartitionFunction, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin, Single, AllConfigs
5969

6070
# Utilities
6171
export save_configs, load_configs, hamming_distribution, save_sumproduct, load_sumproduct
@@ -73,10 +83,8 @@ project_relative_path(xs...) = normpath(joinpath(dirname(dirname(pathof(@__MODUL
7383
include("Mods.jl/src/Mods.jl")
7484
using .Mods
7585

76-
include("utils.jl")
77-
include("bitvector.jl")
7886
include("arithematics.jl")
79-
include("networks/networks.jl")
87+
include("networks.jl")
8088
include("graph_polynomials.jl")
8189
include("configurations.jl")
8290
include("graphs.jl")

0 commit comments

Comments
 (0)