Skip to content

Commit f2fbeca

Browse files
authored
upgrade polynomial (#70)
* update Polynomials to 4, fix serveral bugs * update * bump version and try to fix tag bot
1 parent 44eb45f commit f2fbeca

File tree

11 files changed

+63
-27
lines changed

11 files changed

+63
-27
lines changed

.github/workflows/TagBot.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ on:
44
types:
55
- created
66
workflow_dispatch:
7+
inputs:
8+
lookback:
9+
default: 3
10+
permissions:
11+
actions: read
12+
checks: read
13+
contents: write
14+
deployments: read
15+
issues: read
16+
discussions: read
17+
packages: read
18+
pages: read
19+
pull-requests: read
20+
repository-projects: read
21+
security-events: read
22+
statuses: read
723
jobs:
824
TagBot:
925
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
@@ -12,4 +28,6 @@ jobs:
1228
- uses: JuliaRegistries/TagBot@v1
1329
with:
1430
token: ${{ secrets.GITHUB_TOKEN }}
31+
# Edit the following line to reflect the actual name of the GitHub Secret containing your private key
1532
ssh: ${{ secrets.DOCUMENTER_KEY }}
33+
# ssh: ${{ secrets.NAME_OF_MY_SSH_PRIVATE_KEY_SECRET }}

Project.toml

Lines changed: 2 additions & 2 deletions
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.5"
4+
version = "1.3.6"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -35,7 +35,7 @@ Graphs = "1.7"
3535
LuxorGraphPlot = "0.2"
3636
Mods = "1.3"
3737
OMEinsum = "0.7"
38-
Polynomials = "2.0, 3"
38+
Polynomials = "4"
3939
Primes = "0.5"
4040
Requires = "1"
4141
SIMDTypes = "0.1"

src/arithematics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ function invert_polynomial(poly::Polynomial{BS,X}) where {BS,X}
898898
return LaurentPolynomial{BS,X}(poly.coeffs[end:-1:1], -length(poly.coeffs)+1)
899899
end
900900
function invert_polynomial(poly::LaurentPolynomial{BS,X}) where {BS,X}
901-
return LaurentPolynomial{BS,X}(poly.coeffs[end:-1:1], -poly.m[]-length(poly.coeffs)+1)
901+
return LaurentPolynomial{BS,X}(poly.coeffs[end:-1:1], -poly.order[]-length(poly.coeffs)+1)
902902
end
903903

904904
# for finding all solutions

src/bitvector.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ function StaticBitVector(x::AbstractVector)
129129
N = length(x)
130130
StaticBitVector{N,_nints(N,1)}((convert(BitVector, x).chunks...,))
131131
end
132+
# to void casting StaticBitVector itself
133+
StaticBitVector(x::StaticBitVector) = x
134+
132135
function Base.convert(::Type{StaticBitVector{N,C}}, x::AbstractVector) where {N,C}
133136
@assert length(x) == N
134137
StaticBitVector(x)

src/interfaces.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ end
336336

337337
function solve(gp::ReducedProblem, property::AbstractProperty; T=Float64, usecuda=false)
338338
res = solve(target_problem(gp), property; T, usecuda)
339-
return asarray(extract_result.(Ref(gp), res), res)
339+
return asarray(extract_result(gp).(res), res)
340340
end
341341

342342
# raise an error if the property for problem can not be computed

src/networks/Reduced.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ function target_problem end
1616

1717
"""
1818
extract_result(p::ReducedProblem, output)
19+
extract_result(p::ReducedProblem)
1920
2021
Post process the output of the target problem to get an output to the source problem.
22+
If the output is not provided, it will return a function instead.
23+
The result extraction rule is determined by the output type.
24+
e.g. If the output type is `Tropical`, it will be interpreted as the energy.
25+
If the output is a `Vector`, it will be interpreted as a configuration.
2126
"""
2227
function extract_result end
2328

29+
# the fallback, this interface is designed for GPU compatibility
30+
function extract_result(r::ReducedProblem, res)
31+
return extract_result(r)(res)
32+
end
33+
2434
# fixedvertices(r::ReducedProblem) = fixedvertices(target_problem(r))
2535
# labels(r::ReducedProblem) = labels(target_problem(r))
2636
# terms(r::ReducedProblem) = terms(target_problem(r))

src/networks/SpinGlass.jl

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,21 @@ end
4343
target_problem(sg::SpinGlass) = sg.target
4444

4545
# the energy should be shifted by sum(J)/2 - sum(h)
46-
for ET in [:Tropical, :ExtendedTropical, :TruncatedPoly, :CountingTropical]
47-
@eval function extract_result(sg::SpinGlass, res::T) where T <: $(ET)
48-
sumJ = sum(i->sg.J[i], 1:ne(sg.target.graph))
49-
sumh = sum(i->sg.h[i], 1:nv(sg.target.graph))
46+
function extract_result(sg::SpinGlass)
47+
sumJ = sum(i->sg.J[i], 1:ne(sg.target.graph))
48+
sumh = sum(i->sg.h[i], 1:nv(sg.target.graph))
49+
function extractor(res::T) where T <: Union{Tropical, ExtendedTropical, TruncatedPoly, CountingTropical}
5050
# the cut size is always even if the input J is integer
5151
return res * _x(T; invert=false) ^ (-sumJ + sumh)
5252
end
53-
end
54-
function extract_result(sg::SpinGlass, res::Union{Polynomial{BS,X}, LaurentPolynomial{BS,X}}) where {BS,X}
55-
sumJ = sum(i->sg.J[i], 1:ne(sg.target.graph))
56-
sumh = sum(i->sg.h[i], 1:nv(sg.target.graph))
57-
# the cut size is always even if the input J is integer
58-
lres = LaurentPolynomial{BS,X}(res)
59-
return lres * LaurentPolynomial{BS,X}([one(eltype(res.coeffs))], -sumJ + sumh)
60-
end
61-
62-
# the configurations are not changed, vectors are treated as configurations
63-
for ET in [:SumProductTree, :ConfigSampler, :ConfigEnumerator, :Real, :AbstractVector]
64-
@eval extract_result(sg::SpinGlass, res::T) where T <: $(ET) = res
53+
function extractor(res::T) where {BS, X, T <: Union{Polynomial{BS,X}, LaurentPolynomial{BS,X}}}
54+
lres = LaurentPolynomial{BS,X}(res)
55+
return lres * LaurentPolynomial{BS,X}([one(eltype(res.coeffs))], -sumJ + sumh)
56+
end
57+
function extractor(res::T) where T<:Union{SumProductTree, ConfigSampler, ConfigEnumerator, Real, AbstractVector}
58+
return res
59+
end
60+
return extractor
6561
end
6662

6763
"""

src/networks/networks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ function _pow(x::LaurentPolynomial{BS,X}, i) where {BS,X}
198198
return x^i
199199
else
200200
@assert length(x.coeffs) == 1
201-
return LaurentPolynomial(x.coeffs .^ i, x.m[]*i)
201+
return LaurentPolynomial(x.coeffs .^ i, x.order[]*i)
202202
end
203203
end
204204

test/cuda.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ end
5454
@test res12.c.data res13.c.data
5555
@test res14.maxorder == 4 && res14.coeffs[1]==30 && res14.coeffs[2] == 30 && res14.coeffs[3]==5
5656
@test res18 res2
57-
end
57+
end
58+
59+
@testset "spinglass" begin
60+
g = Graphs.smallgraph("petersen")
61+
gp = SpinGlass(g)
62+
usecuda=true
63+
@test solve(gp, CountingMax(); usecuda) isa CuArray
64+
gp2 = SpinGlass(g; openvertices=(2,))
65+
@test solve(gp2, CountingMax(); usecuda) isa CuArray
66+
end

test/networks/HyperSpinGlass.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ using GenericTensorNetworks, Test, Graphs
3333
@test solve(gp, ConfigsMin())[].n sorted_energies[1]
3434
@test solve(gp, CountingAll())[] 1024
3535
poly = solve(gp, GraphPolynomial(; method=:laurent))[]
36-
@test poly.m[] == sorted_energies[1]
37-
@test poly.n[] == sorted_energies[end]
36+
@test poly.order[] == sorted_energies[1]
37+
@test poly.order[] + length(poly.coeffs) - 1 == sorted_energies[end]
3838
end
3939

4040
@testset "auto laurent" begin
@@ -51,4 +51,4 @@ end
5151
problem = HyperSpinGlass(num_vertices, graph; weights);
5252
poly = solve(problem, GraphPolynomial())[]
5353
@test poly isa LaurentPolynomial
54-
end
54+
end

0 commit comments

Comments
 (0)