Skip to content

Commit

Permalink
Account for BP edge case where network evaluates to 0 (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 authored May 14, 2024
1 parent 9d10b82 commit 43ff72e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
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)
message_norm = norm(updated_messages)
if !iszero(message_norm)
updated_messages /= message_norm
end
return ITensor[updated_messages]
end
@traitfn default_bp_maxiter(g::::(!IsDirected)) = is_tree(g) ? 1 : nothing
Expand Down
1 change: 1 addition & 0 deletions src/contract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function logscalar(
denominator_terms
end

any(iszero, denominator_terms) && return -Inf
return sum(log.(numerator_terms)) - sum(log.((denominator_terms)))
end

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

0 comments on commit 43ff72e

Please sign in to comment.