Skip to content

Commit b9a7f8c

Browse files
authored
fix laurent polynomial switch (#63)
* fix laurent polynomial switch * bump version * remove debugger dep that commited by mistake
1 parent b1febf5 commit b9a7f8c

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GenericTensorNetworks"
22
uuid = "3521c873-ad32-4bb4-b63d-f4f178f42b49"
33
authors = ["GiggleLiu <cacate0129@gmail.com> and contributors"]
4-
version = "1.3.3"
4+
version = "1.3.4"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/interfaces.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,15 @@ function solve(gp::GraphProblem, property::AbstractProperty; T=Float64, usecuda=
276276
return asarray(post_invert_exponent.(res), res)
277277
elseif property isa GraphPolynomial
278278
ws = get_weights(gp)
279-
if ws isa NoWeight || ws isa ZeroWeight || all(>=(0), ws)
279+
if !(eltype(ws) <: Integer)
280+
@warn "Input weights are not Integer types, try casting to weights of `Int64` type..."
281+
gp = chweights(gp, Int.(ws))
282+
ws = get_weights(gp)
283+
end
284+
n = length(terms(gp))
285+
if ws isa NoWeight || ws isa ZeroWeight || all(i->all(>=(0), get_weights(gp, i)), 1:n)
280286
return graph_polynomial(gp, Val(graph_polynomial_method(property)); usecuda=usecuda, T=T, property.kwargs...)
281-
elseif all(<=(0), ws)
287+
elseif all(i->all(<=(0), get_weights(gp, i)), 1:n)
282288
res = graph_polynomial(chweights(gp, -ws), Val(graph_polynomial_method(property)); usecuda=usecuda, T=T, property.kwargs...)
283289
return asarray(invert_polynomial.(res), res)
284290
else

src/networks/HyperSpinGlass.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ $(TYPEDSIGNATURES)
2929
function HyperSpinGlass(n::Int, cliques::AbstractVector; weights=NoWeight(), openvertices=(), fixedvertices=Dict{Int,Int}(), optimizer=GreedyMethod(), simplifier=nothing)
3030
clqs = collect(collect.(cliques))
3131
@assert weights isa NoWeight || length(weights) == length(clqs)
32+
@assert all(c->all(b->1<=b<=n, c), cliques) "vertex index out of bound 1-$n, got: $cliques"
3233
rawcode = EinCode([clqs..., [[i] for i=1:n]...], collect(Int, openvertices)) # labels for edge tensors
3334
HyperSpinGlass(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), n, clqs, weights, Dict{Int,Int}(fixedvertices))
3435
end
@@ -42,7 +43,7 @@ fixedvertices(gp::HyperSpinGlass) = gp.fixedvertices
4243
# weights interface
4344
get_weights(c::HyperSpinGlass) = c.weights
4445
get_weights(gp::HyperSpinGlass, i::Int) = [-gp.weights[i], gp.weights[i]]
45-
chweights(c::HyperSpinGlass, weights) = HyperSpinGlass(c.code, c.cliques, weights, c.fixedvertices)
46+
chweights(c::HyperSpinGlass, weights) = HyperSpinGlass(c.code, c.n, c.cliques, weights, c.fixedvertices)
4647

4748
function generate_tensors(x::T, gp::HyperSpinGlass) where T
4849
ixs = getixsv(gp.code)

test/networks/HyperSpinGlass.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,20 @@ using GenericTensorNetworks, Test, Graphs
3535
poly = solve(gp, GraphPolynomial(; method=:laurent))[]
3636
@test poly.m[] == sorted_energies[1]
3737
@test poly.n[] == sorted_energies[end]
38+
end
39+
40+
@testset "auto laurent" begin
41+
hyperDim = 2
42+
blockDim = 3
43+
graph = [[2, 12], [3, 11], [1, 5], [2, 4], [4, 5], [4, 8], [5, 7], [1, 9], [3, 7], [5, 9], [6, 8], [4, 9], [6, 7], [7, 12], [9, 10], [10, 12], [4, 6], [5, 6], [10, 11], [1, 2, 12], [1, 3, 11], [1, 11, 12], [2, 3, 10], [2, 10, 12], [3, 10, 11], [4, 8, 12], [4, 9, 11], [5, 7, 12], [7, 8, 12], [6, 7, 11], [7, 9, 11], [7, 11, 12], [5, 9, 10], [6, 8, 10], [8, 9, 10], [8, 10, 12], [9, 10, 11], [1, 2, 9], [1, 3, 8], [1, 8, 9], [2, 3, 7], [2, 7, 9], [3, 7, 8], [1, 2, 3], [4, 5, 6], [7, 8, 9]]
44+
num_vertices = blockDim* 2^hyperDim
45+
weights = ones(Int, length(graph));
46+
problem = HyperSpinGlass(num_vertices, graph; weights);
47+
poly = solve(problem, GraphPolynomial())[]
48+
@test poly isa LaurentPolynomial
49+
50+
weights = ones(length(graph));
51+
problem = HyperSpinGlass(num_vertices, graph; weights);
52+
poly = solve(problem, GraphPolynomial())[]
53+
@test poly isa LaurentPolynomial
3854
end

0 commit comments

Comments
 (0)