Skip to content

Commit e4e1e5a

Browse files
committed
make ProblemReductions a dependency
2 parents 75a021f + 849a28b commit e4e1e5a

File tree

22 files changed

+401
-200
lines changed

22 files changed

+401
-200
lines changed

CITATION.bib

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@article{Roa-Villescas2023,
2+
doi = {10.21105/joss.05700},
3+
url = {https://doi.org/10.21105/joss.05700},
4+
year = {2023},
5+
publisher = {The Open Journal},
6+
volume = {8},
7+
number = {90},
8+
pages = {5700},
9+
author = {Martin Roa-Villescas and Jin-Guo Liu},
10+
title = {TensorInference: A Julia package for tensor-based probabilistic inference},
11+
journal = {Journal of Open Source Software}
12+
}

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: 9 additions & 5 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.0"
4+
version = "0.4.2"
55

66
[deps]
77
Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
@@ -10,23 +10,27 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
OMEinsum = "ebe7aa44-baf0-506c-a96f-8464559b3922"
1111
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1212
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
13+
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
14+
ProblemReductions = "899c297d-f7d2-4ebf-8815-a35996def416"
1315
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1416
TropicalNumbers = "b3a74e9c-7526-4576-a4eb-79c0d4c32334"
1517

1618
[weakdeps]
1719
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
18-
GenericTensorNetworks = "3521c873-ad32-4bb4-b63d-f4f178f42b49"
1920

2021
[extensions]
2122
TensorInferenceCUDAExt = "CUDA"
22-
TensorInferenceGTNExt = "GenericTensorNetworks"
2323

2424
[compat]
25+
Artifacts = "1"
2526
CUDA = "4, 5"
2627
DocStringExtensions = "0.8.6, 0.9"
27-
GenericTensorNetworks = "1"
28-
OMEinsum = "0.7"
28+
LinearAlgebra = "1"
29+
OMEinsum = "0.8"
30+
Pkg = "1"
2931
PrecompileTools = "1"
32+
PrettyTables = "2"
33+
ProblemReductions = "0.3"
3034
StatsBase = "0.34"
3135
TropicalNumbers = "0.5.4, 0.6"
3236
julia = "1.9"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ comprehensive introduction to the package read the
5151
## Citing
5252

5353
If you use TensorInference as part of your research, teaching, or other
54-
activities, please consider citing the following publication: [(TBA)]().
54+
activities, please consider [citing our work](CITATION.bib).
5555

5656
## Questions and Contributions
5757

benchmark/bench_map.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Artifacts
66

77
const SUITE = BenchmarkGroup()
88

9-
problem = problem_from_artifact("uai2014", "MAR" "Promedus", 14)
9+
problem = problem_from_artifact("uai2014", "MAR", "Promedus", 14)
1010

1111
optimizer = TreeSA(ntrials = 1, niters = 2, βs = 1:0.1:40)
1212
tn = TensorNetworkModel(read_model(problem); optimizer, evidence=get_evidence(problem))

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33
GenericTensorNetworks = "3521c873-ad32-4bb4-b63d-f4f178f42b49"
44
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
55
LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589"
6+
ProblemReductions = "899c297d-f7d2-4ebf-8815-a35996def416"
67
TensorInference = "c2297e78-99bd-40ad-871d-f50e56b81012"
78
TikzPictures = "37f6aa50-8035-52d0-81c2-5a1d08754b2d"

docs/src/api/public.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ read_td_file
6868
sample
6969
update_evidence!
7070
update_temperature
71+
random_matrix_product_state
7172
```

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

ext/TensorInferenceGTNExt.jl

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/Core.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ function TensorNetworkModel(
175175
TensorNetworkModel(collect(LT, vars), code, tensors, evidence, mars)
176176
end
177177

178+
"""
179+
$(TYPEDSIGNATURES)
180+
"""
181+
function TensorNetworkModel(
182+
model::UAIModel{T}, code;
183+
evidence = Dict{Int,Int}(),
184+
mars = [[i] for i=1:model.nvars],
185+
vars = [1:model.nvars...]
186+
)::TensorNetworkModel where{T}
187+
@debug "constructing tensor network model from code"
188+
tensors = Array{T}[[ones(T, [model.cards[i] for i in mar]...) for mar in mars]..., [t.vals for t in model.factors]...]
189+
190+
return TensorNetworkModel(vars, code, tensors, evidence, mars)
191+
end
192+
178193
"""
179194
$(TYPEDSIGNATURES)
180195
@@ -189,7 +204,8 @@ Get the cardinalities of variables in this tensor network.
189204
"""
190205
function get_cards(tn::TensorNetworkModel; fixedisone = false)::Vector
191206
vars = get_vars(tn)
192-
[fixedisone && haskey(tn.evidence, vars[k]) ? 1 : length(tn.tensors[k]) for k in eachindex(vars)]
207+
size_dict = OMEinsum.get_size_dict(getixsv(tn.code), tn.tensors)
208+
[fixedisone && haskey(tn.evidence, vars[k]) ? 1 : size_dict[vars[k]] for k in eachindex(vars)]
193209
end
194210

195211
chevidence(tn::TensorNetworkModel, evidence) = TensorNetworkModel(tn.vars, tn.code, tn.tensors, evidence)

0 commit comments

Comments
 (0)