Skip to content

Commit 80cffac

Browse files
authored
update generic tensor network to version 2.0 (#92)
* update gtn * fix docs * fix makefile * update
1 parent c6cd869 commit 80cffac

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
JL = julia --project
2+
3+
default: init test
4+
5+
init:
6+
$(JL) -e 'using Pkg; Pkg.precompile(); Pkg.activate("docs"); Pkg.develop(path="."), Pkg.precompile()'
7+
8+
update:
9+
$(JL) -e 'using Pkg; Pkg.update(); Pkg.precompile(); Pkg.activate("docs"); Pkg.update(); Pkg.precompile()'
10+
11+
test:
12+
$(JL) -e 'using Pkg; Pkg.test()'
13+
14+
coverage:
15+
$(JL) -e 'using Pkg; Pkg.test(; coverage=true)'
16+
17+
serve:
18+
$(JL) -e 'using Pkg; Pkg.activate("docs"); using LiveServer; servedocs(;skip_dirs=["docs/src/assets", "docs/src/generated"], literate_dir="examples")'
19+
20+
clean:
21+
rm -rf docs/build
22+
find . -name "*.cov" -type f -print0 | xargs -0 /bin/rm -f
23+
24+
.PHONY: init test coverage serve clean update

Project.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TensorInference"
22
uuid = "c2297e78-99bd-40ad-871d-f50e56b81012"
33
authors = ["Jin-Guo Liu", "Martin Roa Villescas"]
4-
version = "0.4.2"
4+
version = "0.4.3"
55

66
[deps]
77
Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
@@ -17,10 +17,13 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1717
TropicalNumbers = "b3a74e9c-7526-4576-a4eb-79c0d4c32334"
1818

1919
[compat]
20+
Artifacts = "1"
2021
CUDA = "4, 5"
2122
DocStringExtensions = "0.8.6, 0.9"
22-
GenericTensorNetworks = "1"
23-
OMEinsum = "0.7"
23+
GenericTensorNetworks = "2"
24+
LinearAlgebra = "1"
25+
OMEinsum = "0.8"
26+
Pkg = "1"
2427
PrecompileTools = "1"
2528
Requires = "1"
2629
StatsBase = "0.34"

examples/hard-core-lattice-gas/main.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515

1616
a, b = (1, 0), (0.5, 0.5*sqrt(3))
1717
Na, Nb = 10, 10
18-
sites = [a .* i .+ b .* j for i=1:Na, j=1:Nb]
18+
sites = vec([50 .* (a .* i .+ b .* j) for i=1:Na, j=1:Nb])
1919

2020
# There exists blockade interactions between hard-core particles.
2121
# We connect two lattice sites within blockade radius by an edge.
2222
# Two ends of an edge can not both be occupied by particles.
23-
blockade_radius = 1.1
23+
blockade_radius = 55
2424
using GenericTensorNetworks: show_graph, unit_disk_graph
2525
using GenericTensorNetworks.Graphs: edges, nv
2626
graph = unit_disk_graph(vec(sites), blockade_radius)
27-
show_graph(graph; locs=sites, texts=fill("", length(sites)))
27+
show_graph(graph, sites; texts=fill("", length(sites)))
2828

2929
# These constraints defines an independent set problem that characterized by the following energy based model.
3030
# Let $G = (V, E)$ be a graph, where $V$ is the set of vertices and $E$ is the set of edges.
@@ -39,7 +39,7 @@ show_graph(graph; locs=sites, texts=fill("", length(sites)))
3939
# The independent set problem involves finding a set of vertices in a graph such that no two vertices in the set are adjacent (i.e., there is no edge connecting them).
4040
# One can create a tensor network based modeling of an independent set problem with package [`GenericTensorNetworks.jl`](https://github.com/QuEraComputing/GenericTensorNetworks.jl).
4141
using GenericTensorNetworks
42-
problem = IndependentSet(graph; optimizer=GreedyMethod());
42+
problem = IndependentSet(graph)
4343

4444
# There are plenty of discussions related to solution space properties in the `GenericTensorNetworks` [documentaion page](https://queracomputing.github.io/GenericTensorNetworks.jl/dev/generated/IndependentSet/).
4545
# In this example, we show how to use `TensorInference` to use probabilistic inference for understand the finite temperature properties of this statistical model.
@@ -59,15 +59,14 @@ partition_func[]
5959

6060
# The marginal probabilities can be computed with the [`marginals`](@ref) function, which measures how likely a site is occupied.
6161
mars = marginals(pmodel)
62-
show_graph(graph; locs=sites, vertex_colors=[(b = mars[[i]][2]; (1-b, 1-b, 1-b)) for i in 1:nv(graph)], texts=fill("", nv(graph)))
62+
show_graph(graph, sites; vertex_colors=[(b = mars[[i]][2]; (1-b, 1-b, 1-b)) for i in 1:nv(graph)], texts=fill("", nv(graph)))
6363
# The can see the sites at the corner is more likely to be occupied.
6464
# To obtain two-site correlations, one can set the variables to query marginal probabilities manually.
6565
pmodel2 = TensorNetworkModel(problem, β; mars=[[e.src, e.dst] for e in edges(graph)])
6666
mars = marginals(pmodel2);
6767

6868
# We show the probability that both sites on an edge are not occupied
69-
show_graph(graph; locs=sites, edge_colors=[(b = mars[[e.src, e.dst]][1, 1]; (1-b, 1-b, 1-b)) for e in edges(graph)], texts=fill("", nv(graph)),
70-
edge_line_widths=edge_colors=[8*mars[[e.src, e.dst]][1, 1] for e in edges(graph)])
69+
show_graph(graph, sites; edge_colors=[(b = mars[[e.src, e.dst]][1, 1]; (1-b, 1-b, 1-b)) for e in edges(graph)], texts=fill("", nv(graph)), config=GraphDisplayConfig(; edge_line_width=5))
7170

7271
# ## The most likely configuration
7372
# The MAP and MMAP can be used to get the most likely configuration given an evidence.
@@ -78,7 +77,7 @@ mars = marginals(pmodel3)
7877
logp, config = most_probable_config(pmodel3)
7978

8079
# The log probability is 102. Let us visualize the configuration.
81-
show_graph(graph; locs=sites, vertex_colors=[(1-b, 1-b, 1-b) for b in config], texts=fill("", nv(graph)))
80+
show_graph(graph, sites; vertex_colors=[(1-b, 1-b, 1-b) for b in config], texts=fill("", nv(graph)))
8281
# The number of particles is
8382
sum(config)
8483

@@ -87,7 +86,7 @@ pmodel3 = TensorNetworkModel(problem, β; evidence=Dict(1=>0))
8786
logp2, config2 = most_probable_config(pmodel)
8887

8988
# The log probability is 99, which is much smaller.
90-
show_graph(graph; locs=sites, vertex_colors=[(1-b, 1-b, 1-b) for b in config2], texts=fill("", nv(graph)))
89+
show_graph(graph, sites; vertex_colors=[(1-b, 1-b, 1-b) for b in config2], texts=fill("", nv(graph)))
9190
# The number of particles is
9291
sum(config2)
9392

src/generictensornetworks.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ Convert a constraint satisfiability problem (or energy model) to a probabilistic
1212
* `problem` is a `GraphProblem` instance in [`GenericTensorNetworks`](https://github.com/QuEraComputing/GenericTensorNetworks.jl).
1313
* `β` is the inverse temperature.
1414
"""
15-
function TensorInference.TensorNetworkModel(problem::GraphProblem, β::Real; evidence::Dict=Dict{Int,Int}(),
16-
optimizer=GreedyMethod(), simplifier=nothing, mars=[[l] for l in labels(problem)])
17-
ixs = getixsv(problem.code)
18-
iy = getiyv(problem.code)
15+
function TensorInference.TensorNetworkModel(problem::GraphProblem, β::Real; evidence::Dict=Dict{eltype(labels(problem)),Int}(),
16+
optimizer=GreedyMethod(), openvars=empty(labels(problem)), simplifier=nothing, mars=[[l] for l in labels(problem)])
17+
ixs = [GenericTensorNetworks.energy_terms(problem)..., GenericTensorNetworks.extra_terms(problem)...]
1918
lbs = labels(problem)
2019
nflavors = length(flavors(problem))
2120
# generate tensors for x = e^β
2221
tensors = generate_tensors(exp(β), problem)
2322
factors = [Factor((ix...,), t) for (ix, t) in zip(ixs, tensors)]
24-
return TensorNetworkModel(lbs, fill(nflavors, length(lbs)), factors; openvars=iy, evidence, optimizer, simplifier, mars)
23+
return TensorNetworkModel(lbs, fill(nflavors, length(lbs)), factors; openvars, evidence, optimizer, simplifier, mars)
2524
end
2625

2726
"""
@@ -43,18 +42,18 @@ end
4342

4443
function TensorInference.MMAPModel(problem::GraphProblem, β::Real;
4544
queryvars,
46-
evidence = Dict{labeltype(problem.code), Int}(),
45+
openvars = empty(labels(problem)),
46+
evidence = Dict{eltype(labels(problem)), Int}(),
4747
optimizer = GreedyMethod(), simplifier = nothing,
4848
marginalize_optimizer = GreedyMethod(), marginalize_simplifier = nothing
4949
)::MMAPModel
50-
ixs = getixsv(problem.code)
51-
iy = getiyv(problem.code)
50+
ixs = [GenericTensorNetworks.energy_terms(problem)..., GenericTensorNetworks.extra_terms(problem)...]
5251
nflavors = length(flavors(problem))
5352
# generate tensors for x = e^β
5453
tensors = generate_tensors(exp(β), problem)
5554
factors = [Factor((ix...,), t) for (ix, t) in zip(ixs, tensors)]
5655
lbs = labels(problem)
57-
return MMAPModel(lbs, fill(nflavors, length(lbs)), factors; queryvars, openvars=iy, evidence,
56+
return MMAPModel(lbs, fill(nflavors, length(lbs)), factors; queryvars, openvars, evidence,
5857
optimizer, simplifier,
5958
marginalize_optimizer, marginalize_simplifier)
6059
end

test/generictensornetworks.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ using GenericTensorNetworks, TensorInference
88
problem = IndependentSet(g)
99
model = TensorNetworkModel(problem, β; mars=[[2, 3]])
1010
mars = marginals(model)[[2, 3]]
11-
problem2 = IndependentSet(g; openvertices=[2,3])
12-
mars2 = TensorInference.normalize!(GenericTensorNetworks.solve(problem2, PartitionFunction(β)), 1)
11+
problem2 = IndependentSet(g)
12+
mars2 = TensorInference.normalize!(GenericTensorNetworks.solve(GenericTensorNetwork(problem2; openvertices=[2, 3]), PartitionFunction(β)), 1)
1313
@test mars mars2
1414

1515
# update temperature

0 commit comments

Comments
 (0)