Skip to content

Commit

Permalink
upgrade polynomial (#70)
Browse files Browse the repository at this point in the history
* update Polynomials to 4, fix serveral bugs

* update

* bump version and try to fix tag bot
  • Loading branch information
GiggleLiu authored Aug 16, 2023
1 parent 44eb45f commit f2fbeca
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 27 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ on:
types:
- created
workflow_dispatch:
inputs:
lookback:
default: 3
permissions:
actions: read
checks: read
contents: write
deployments: read
issues: read
discussions: read
packages: read
pages: read
pull-requests: read
repository-projects: read
security-events: read
statuses: read
jobs:
TagBot:
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
Expand All @@ -12,4 +28,6 @@ jobs:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
# Edit the following line to reflect the actual name of the GitHub Secret containing your private key
ssh: ${{ secrets.DOCUMENTER_KEY }}
# ssh: ${{ secrets.NAME_OF_MY_SSH_PRIVATE_KEY_SECRET }}
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GenericTensorNetworks"
uuid = "3521c873-ad32-4bb4-b63d-f4f178f42b49"
authors = ["GiggleLiu <[email protected]> and contributors"]
version = "1.3.5"
version = "1.3.6"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down Expand Up @@ -35,7 +35,7 @@ Graphs = "1.7"
LuxorGraphPlot = "0.2"
Mods = "1.3"
OMEinsum = "0.7"
Polynomials = "2.0, 3"
Polynomials = "4"
Primes = "0.5"
Requires = "1"
SIMDTypes = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion src/arithematics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ function invert_polynomial(poly::Polynomial{BS,X}) where {BS,X}
return LaurentPolynomial{BS,X}(poly.coeffs[end:-1:1], -length(poly.coeffs)+1)
end
function invert_polynomial(poly::LaurentPolynomial{BS,X}) where {BS,X}
return LaurentPolynomial{BS,X}(poly.coeffs[end:-1:1], -poly.m[]-length(poly.coeffs)+1)
return LaurentPolynomial{BS,X}(poly.coeffs[end:-1:1], -poly.order[]-length(poly.coeffs)+1)
end

# for finding all solutions
Expand Down
3 changes: 3 additions & 0 deletions src/bitvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ function StaticBitVector(x::AbstractVector)
N = length(x)
StaticBitVector{N,_nints(N,1)}((convert(BitVector, x).chunks...,))
end
# to void casting StaticBitVector itself
StaticBitVector(x::StaticBitVector) = x

function Base.convert(::Type{StaticBitVector{N,C}}, x::AbstractVector) where {N,C}
@assert length(x) == N
StaticBitVector(x)
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ end

function solve(gp::ReducedProblem, property::AbstractProperty; T=Float64, usecuda=false)
res = solve(target_problem(gp), property; T, usecuda)
return asarray(extract_result.(Ref(gp), res), res)
return asarray(extract_result(gp).(res), res)
end

# raise an error if the property for problem can not be computed
Expand Down
10 changes: 10 additions & 0 deletions src/networks/Reduced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ function target_problem end

"""
extract_result(p::ReducedProblem, output)
extract_result(p::ReducedProblem)
Post process the output of the target problem to get an output to the source problem.
If the output is not provided, it will return a function instead.
The result extraction rule is determined by the output type.
e.g. If the output type is `Tropical`, it will be interpreted as the energy.
If the output is a `Vector`, it will be interpreted as a configuration.
"""
function extract_result end

# the fallback, this interface is designed for GPU compatibility
function extract_result(r::ReducedProblem, res)
return extract_result(r)(res)
end

# fixedvertices(r::ReducedProblem) = fixedvertices(target_problem(r))
# labels(r::ReducedProblem) = labels(target_problem(r))
# terms(r::ReducedProblem) = terms(target_problem(r))
Expand Down
28 changes: 12 additions & 16 deletions src/networks/SpinGlass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,21 @@ end
target_problem(sg::SpinGlass) = sg.target

# the energy should be shifted by sum(J)/2 - sum(h)
for ET in [:Tropical, :ExtendedTropical, :TruncatedPoly, :CountingTropical]
@eval function extract_result(sg::SpinGlass, res::T) where T <: $(ET)
sumJ = sum(i->sg.J[i], 1:ne(sg.target.graph))
sumh = sum(i->sg.h[i], 1:nv(sg.target.graph))
function extract_result(sg::SpinGlass)
sumJ = sum(i->sg.J[i], 1:ne(sg.target.graph))
sumh = sum(i->sg.h[i], 1:nv(sg.target.graph))
function extractor(res::T) where T <: Union{Tropical, ExtendedTropical, TruncatedPoly, CountingTropical}
# the cut size is always even if the input J is integer
return res * _x(T; invert=false) ^ (-sumJ + sumh)
end
end
function extract_result(sg::SpinGlass, res::Union{Polynomial{BS,X}, LaurentPolynomial{BS,X}}) where {BS,X}
sumJ = sum(i->sg.J[i], 1:ne(sg.target.graph))
sumh = sum(i->sg.h[i], 1:nv(sg.target.graph))
# the cut size is always even if the input J is integer
lres = LaurentPolynomial{BS,X}(res)
return lres * LaurentPolynomial{BS,X}([one(eltype(res.coeffs))], -sumJ + sumh)
end

# the configurations are not changed, vectors are treated as configurations
for ET in [:SumProductTree, :ConfigSampler, :ConfigEnumerator, :Real, :AbstractVector]
@eval extract_result(sg::SpinGlass, res::T) where T <: $(ET) = res
function extractor(res::T) where {BS, X, T <: Union{Polynomial{BS,X}, LaurentPolynomial{BS,X}}}
lres = LaurentPolynomial{BS,X}(res)
return lres * LaurentPolynomial{BS,X}([one(eltype(res.coeffs))], -sumJ + sumh)
end
function extractor(res::T) where T<:Union{SumProductTree, ConfigSampler, ConfigEnumerator, Real, AbstractVector}
return res
end
return extractor
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/networks/networks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function _pow(x::LaurentPolynomial{BS,X}, i) where {BS,X}
return x^i
else
@assert length(x.coeffs) == 1
return LaurentPolynomial(x.coeffs .^ i, x.m[]*i)
return LaurentPolynomial(x.coeffs .^ i, x.order[]*i)
end
end

Expand Down
11 changes: 10 additions & 1 deletion test/cuda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ end
@test res12.c.data res13.c.data
@test res14.maxorder == 4 && res14.coeffs[1]==30 && res14.coeffs[2] == 30 && res14.coeffs[3]==5
@test res18 res2
end
end

@testset "spinglass" begin
g = Graphs.smallgraph("petersen")
gp = SpinGlass(g)
usecuda=true
@test solve(gp, CountingMax(); usecuda) isa CuArray
gp2 = SpinGlass(g; openvertices=(2,))
@test solve(gp2, CountingMax(); usecuda) isa CuArray
end
6 changes: 3 additions & 3 deletions test/networks/HyperSpinGlass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ using GenericTensorNetworks, Test, Graphs
@test solve(gp, ConfigsMin())[].n sorted_energies[1]
@test solve(gp, CountingAll())[] 1024
poly = solve(gp, GraphPolynomial(; method=:laurent))[]
@test poly.m[] == sorted_energies[1]
@test poly.n[] == sorted_energies[end]
@test poly.order[] == sorted_energies[1]
@test poly.order[] + length(poly.coeffs) - 1 == sorted_energies[end]
end

@testset "auto laurent" begin
Expand All @@ -51,4 +51,4 @@ end
problem = HyperSpinGlass(num_vertices, graph; weights);
poly = solve(problem, GraphPolynomial())[]
@test poly isa LaurentPolynomial
end
end
4 changes: 2 additions & 2 deletions test/networks/SpinGlass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ using GenericTensorNetworks, Test, Graphs
@test solve(gp, ConfigsMin())[].n sorted_energies[1]
@test solve(gp, CountingAll())[] 1024
poly = solve(gp, GraphPolynomial())[]
@test poly.m[] == sorted_energies[1]
@test poly.n[] == sorted_energies[end]
@test poly.order[] == sorted_energies[1]
@test poly.order[] + length(poly.coeffs) - 1 == sorted_energies[end]
end

0 comments on commit f2fbeca

Please sign in to comment.