Skip to content

Commit 62e8956

Browse files
authored
Error users earlier, add Single type (#43)
* output array consistently * save * Redesign ConfigsMax(K) * error on k<=0
1 parent c7c8eb5 commit 62e8956

File tree

6 files changed

+155
-81
lines changed

6 files changed

+155
-81
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"julia.environmentPath": "/home/leo/.julia/dev/GenericTensorNetworks"
3+
}

examples/open.jl

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
# # Open degrees of freedom
2-
# Open degrees of freedom is useful when the graph under consideration is an induced subgraph of another graph.
3-
# The vertices connected to the rest part of the parent graph can not be summed over directly, which can be specified with the `openvertices` keyword argument in the graph problem constructor.
1+
# # Open and fixed degrees of freedom
2+
# Open degrees of freedom is useful when one want to get the marginal about certain degrees of freedom.
3+
# When one specifies the `openvertices` keyword argument in [`solve`](@ref) function as a tuple of vertices, the output will be a tensor that can be indexed by these degrees of freedom.
44
# Let us use the maximum independent set problem on Petersen graph as an example.
55
#
66
using GenericTensorNetworks, Graphs
77

88
graph = Graphs.smallgraph(:petersen)
99

1010
# The following code computes the MIS tropical tensor (reference to be added) with open vertices 1, 2 and 3.
11-
problem = IndependentSet(graph; openvertices=[1,2,3])
11+
problem = IndependentSet(graph; openvertices=[1,2,3]);
1212

13-
mis_tropical_tensor = solve(problem, SizeMax())
13+
marginal = solve(problem, SizeMax())
1414

1515
# The return value is a rank-3 tensor, with its elements being the MIS sizes under different configuration of open vertices.
1616
# For the maximum independent set problem, this tensor is also called the MIS tropical tensor, which can be useful in the MIS tropical tensor analysis (reference to be added).
17+
18+
# One can also specify the fixed degrees of freedom by providing the `fixedvertices` keyword argument as a `Dict`, which can be used to get conditioned probability.
19+
# For example, we can use the following code to do the same calculation as using `openvertices`.
20+
problem = IndependentSet(graph; fixedvertices=Dict(1=>0, 2=>0, 3=>0));
21+
22+
output = zeros(TropicalF64,2,2,2);
23+
24+
marginal_alternative = map(CartesianIndices((2,2,2))) do ci
25+
problem.fixedvertices[1] = ci.I[1]-1
26+
problem.fixedvertices[2] = ci.I[2]-1
27+
problem.fixedvertices[3] = ci.I[3]-1
28+
output[ci] = solve(problem, SizeMax())[]
29+
end
30+
31+
# One can easily check this one also gets the correct marginal on vertices 1, 2 and 3.
32+
# As a reminder, their computational hardness can be different, because the contraction order optimization program can optimize over open degrees of freedom.

src/GenericTensorNetworks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export SetPacking, is_set_packing
4343
export SetCovering, is_set_covering
4444

4545
# Interfaces
46-
export solve, SizeMax, SizeMin, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin
46+
export solve, SizeMax, SizeMin, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin, Single
4747

4848
# Utilities
4949
export save_configs, load_configs, hamming_distribution, save_sumproduct, load_sumproduct

src/configurations.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ function best_solutions(gp::GraphProblem; all=false, usecuda=false, invert=false
3535
T = config_type(CountingTropical{T,T}, length(labels(gp)), nflavor(gp); all, tree_storage)
3636
xs = generate_tensors(_x(T; invert), gp)
3737
ret = bounding_contract(AllConfigs{1}(), gp.code, xst, ymask, xs)
38-
return invert ? post_invert_exponent.(ret) : ret
38+
return invert ? asarray(post_invert_exponent.(ret), ret) : ret
3939
else
4040
@assert ndims(ymask) == 0
4141
t, res = solution_ad(gp.code, xst, ymask)
4242
ret = fill(CountingTropical(asscalar(t).n, ConfigSampler(StaticBitVector(map(l->res[l], 1:length(res))))))
43-
return invert ? post_invert_exponent.(ret) : ret
43+
return invert ? asarray(post_invert_exponent.(ret), ret) : ret
4444
end
4545
end
4646

@@ -63,7 +63,7 @@ function solutions(gp::GraphProblem, ::Type{BT}; all::Bool, usecuda::Bool=false,
6363
end
6464
T = config_type(BT, length(labels(gp)), nflavor(gp); all, tree_storage)
6565
ret = contractx(gp, _x(T; invert); usecuda=usecuda)
66-
return invert ? post_invert_exponent.(ret) : ret
66+
return invert ? asarray(post_invert_exponent.(ret), ret) : ret
6767
end
6868

6969
"""
@@ -79,7 +79,7 @@ function bestk_solutions(gp::GraphProblem, k::Int; invert::Bool=false, tree_stor
7979
T = config_type(TruncatedPoly{k,T,T}, length(labels(gp)), nflavor(gp); all=true, tree_storage)
8080
xs = generate_tensors(_x(T; invert), gp)
8181
ret = bounding_contract(AllConfigs{k}(), gp.code, xst, ymask, xs)
82-
return invert ? post_invert_exponent.(ret) : ret
82+
return invert ? asarray(post_invert_exponent.(ret), ret) : ret
8383
end
8484

8585
"""

0 commit comments

Comments
 (0)