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

Account for BP edge case where network evaluates to 0 #178

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/caches/beliefpropagationcache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ default_message_norm(m::ITensor) = norm(m)
function default_message_update(contract_list::Vector{ITensor}; kwargs...)
sequence = optimal_contraction_sequence(contract_list)
updated_messages = contract(contract_list; sequence, kwargs...)
updated_messages /= norm(updated_messages)
n = norm(updated_messages)
if !iszero(n)
updated_messages /= norm(updated_messages)
end
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
return ITensor[updated_messages]
end
@traitfn default_bp_maxiter(g::::(!IsDirected)) = is_tree(g) ? 1 : nothing
Expand Down
6 changes: 5 additions & 1 deletion src/contract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ function logscalar(
denominator_terms
end

return sum(log.(numerator_terms)) - sum(log.((denominator_terms)))
if any(t -> iszero(t), denominator_terms)
return -Inf
else
return sum(log.(numerator_terms)) - sum(log.((denominator_terms)))
end
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
end

function ITensors.scalar(alg::Algorithm"bp", tn::AbstractITensorNetwork; kwargs...)
Expand Down
11 changes: 9 additions & 2 deletions test/test_belief_propagation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Compat: Compat
using Graphs: vertices
# Trigger package extension.
using GraphsFlows: GraphsFlows
using ITensorNetworks:
ITensorNetworks,
BeliefPropagationCache,
Expand All @@ -18,6 +17,7 @@ using ITensorNetworks:
message,
partitioned_tensornetwork,
random_tensornetwork,
scalar,
siteinds,
split_index,
tensornetwork,
Expand All @@ -28,7 +28,7 @@ using ITensors: ITensors, ITensor, combiner, dag, inds, inner, op, prime, random
using ITensorNetworks.ModelNetworks: ModelNetworks
using ITensors.NDTensors: array
using LinearAlgebra: eigvals, tr
using NamedGraphs: NamedEdge
using NamedGraphs: NamedEdge, NamedGraph
using NamedGraphs.NamedGraphGenerators: named_comb_tree, named_grid
using NamedGraphs.PartitionedGraphs: PartitionVertex, partitionedges
using Random: Random
Expand Down Expand Up @@ -75,5 +75,12 @@ using Test: @test, @testset

@test all(eig -> imag(eig) ≈ 0, eigs)
@test all(eig -> real(eig) >= -eps(eltype(eig)), eigs)

#Test edge case of network which evalutes to 0
χ = 2
g = named_grid((3, 1))
ψ = random_tensornetwork(ComplexF64, g; link_space=χ)
ψ[(1, 1)] = 0.0 * ψ[(1, 1)]
@test iszero(scalar(ψ; alg="bp"))
end
end
Loading