Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to generate tensor network model by optimized EinCode #90

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ function TensorNetworkModel(
TensorNetworkModel(collect(LT, vars), code, tensors, evidence, mars)
end

"""
$(TYPEDSIGNATURES)
"""
function TensorNetworkModel(
model::UAIModel{T}, code;
evidence = Dict{Int,Int}(),
mars = [[i] for i=1:model.nvars],
vars = [1:model.nvars...]
)::TensorNetworkModel where{T}
@debug "constructing tensor network model from code"
tensors = Array{T}[[ones(T, [model.cards[i] for i in mar]...) for mar in mars]..., [t.vals for t in model.factors]...]

return TensorNetworkModel(vars, code, tensors, evidence, mars)
end

"""
$(TYPEDSIGNATURES)

Expand Down
12 changes: 12 additions & 0 deletions test/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ using Test
using OMEinsum
using TensorInference

@testset "load from code" begin
model = problem_from_artifact("uai2014", "MAR", "Promedus", 14)

tn1 = TensorNetworkModel(read_model(model);
evidence=read_evidence(model),
optimizer = TreeSA(ntrials = 3, niters = 2, βs = 1:0.1:80))

tn2 = TensorNetworkModel(read_model(model), tn1.code, evidence=read_evidence(model))

@test tn1.code == tn2.code
end

@testset "gradient-based tensor network solvers" begin
model = problem_from_artifact("uai2014", "MAR", "Promedus", 14)

Expand Down
Loading