-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from TensorBFS/jg/port-gtn
Port GenericTensorNetworks
- Loading branch information
Showing
10 changed files
with
154 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using .GenericTensorNetworks: generate_tensors, GraphProblem, flavors, labels | ||
|
||
export probabilistic_model | ||
|
||
""" | ||
$TYPEDSIGNATURES | ||
Convert a constraint satisfiability problem (or energy model) to a probabilistic model. | ||
### Arguments | ||
* `problem` is a `GraphProblem` instance in [`GenericTensorNetworks`](https://github.com/QuEraComputing/GenericTensorNetworks.jl). | ||
* `β` is the inverse temperature. | ||
""" | ||
function TensorInference.TensorNetworkModel(problem::GraphProblem, β::Real; evidence::Dict=Dict{Int,Int}(), | ||
optimizer=GreedyMethod(), simplifier=nothing, mars=[[l] for l in labels(problem)]) | ||
ixs = getixsv(problem.code) | ||
iy = getiyv(problem.code) | ||
lbs = labels(problem) | ||
nflavors = length(flavors(problem)) | ||
# generate tensors for x = e^β | ||
tensors = generate_tensors(exp(β), problem) | ||
factors = [Factor((ix...,), t) for (ix, t) in zip(ixs, tensors)] | ||
return TensorNetworkModel(lbs, fill(nflavors, length(lbs)), factors; openvars=iy, evidence, optimizer, simplifier, mars) | ||
end | ||
function TensorInference.MMAPModel(problem::GraphProblem, β::Real; | ||
queryvars, | ||
evidence = Dict{labeltype(problem.code), Int}(), | ||
optimizer = GreedyMethod(), simplifier = nothing, | ||
marginalize_optimizer = GreedyMethod(), marginalize_simplifier = nothing | ||
)::MMAPModel | ||
ixs = getixsv(problem.code) | ||
iy = getiyv(problem.code) | ||
nflavors = length(flavors(problem)) | ||
# generate tensors for x = e^β | ||
tensors = generate_tensors(exp(β), problem) | ||
factors = [Factor((ix...,), t) for (ix, t) in zip(ixs, tensors)] | ||
lbs = labels(problem) | ||
return MMAPModel(lbs, fill(nflavors, length(lbs)), factors; queryvars, openvars=iy, evidence, | ||
optimizer, simplifier, | ||
marginalize_optimizer, marginalize_simplifier) | ||
end | ||
|
||
@info "`TensorInference` loaded `GenericTensorNetworks` extension successfully, | ||
`TensorNetworkModel` and `MMAPModel` can be used for converting a `GraphProblem` to a probabilistic model now." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Test | ||
using GenericTensorNetworks, TensorInference | ||
|
||
@testset "marginals" begin | ||
# compute the probability | ||
β = 2.0 | ||
g = GenericTensorNetworks.Graphs.smallgraph(:petersen) | ||
problem = IndependentSet(g) | ||
model = TensorNetworkModel(problem, β; mars=[[2, 3]]) | ||
mars = marginals(model)[1] | ||
problem2 = IndependentSet(g; openvertices=[2,3]) | ||
mars2 = TensorInference.normalize!(GenericTensorNetworks.solve(problem2, PartitionFunction(β)), 1) | ||
@test mars ≈ mars2 | ||
|
||
# mmap | ||
model = MMAPModel(problem, β; queryvars=[1,4]) | ||
logp, config = most_probable_config(model) | ||
@test config == [0, 0] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters