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

Fix BP convergence metric for complex networks #193

Merged
merged 9 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 10 additions & 7 deletions src/caches/beliefpropagationcache.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Graphs: IsDirected
using SplitApplyCombine: group
using LinearAlgebra: diag
using LinearAlgebra: diag, dot
using ITensors: dir
using ITensorMPS: ITensorMPS
using NamedGraphs.PartitionedGraphs:
Expand All @@ -15,7 +15,6 @@ using SimpleTraits: SimpleTraits, Not, @traitfn

default_message(inds_e) = ITensor[denseblocks(delta(i)) for i in inds_e]
default_messages(ptn::PartitionedGraph) = Dictionary()
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...)
Expand All @@ -38,12 +37,16 @@ function default_cache_construction_kwargs(alg::Algorithm"bp", ψ::AbstractITens
return (; partitioned_vertices=default_partitioned_vertices(ψ))
end

function message_diff(
message_a::Vector{ITensor}, message_b::Vector{ITensor}; message_norm=default_message_norm
)
function message_diff(message_a::Vector{ITensor}, message_b::Vector{ITensor})
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
lhs, rhs = contract(message_a), contract(message_b)
norm_lhs, norm_rhs = message_norm(lhs), message_norm(rhs)
return 0.5 * norm((denseblocks(lhs) / norm_lhs) - (denseblocks(rhs) / norm_rhs))
@assert issetequal(inds(lhs), inds(rhs))
c = combiner(inds(lhs))
lhs *= c
rhs *= c
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
lhs /= norm(lhs)
rhs /= norm(rhs)
f = abs(dot(lhs, rhs))^2
return 1 - f
end

struct BeliefPropagationCache{PTN,MTS,DM}
Expand Down
15 changes: 8 additions & 7 deletions test/test_belief_propagation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ using ITensorNetworks:
tensornetwork,
update,
update_factor,
update_message
update_message,
message_diff
using ITensors: ITensors, ITensor, combiner, dag, inds, inner, op, prime, random_itensor
using ITensorNetworks.ModelNetworks: ModelNetworks
using ITensors.NDTensors: array
Expand All @@ -40,13 +41,13 @@ using Test: @test, @testset
s = siteinds("S=1/2", g)
χ = 2
rng = StableRNG(1234)
ψ = random_tensornetwork(rng, s; link_space=χ)
ψ = random_tensornetwork(rng, ComplexF64, s; link_space=χ)
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
ψψ = ψ ⊗ prime(dag(ψ); sites=[])
bpc = BeliefPropagationCache(ψψ)
bpc = update(bpc; maxiter=50, tol=1e-10)
bpc = BeliefPropagationCache(ψψ, group(v -> first(v), vertices(ψψ)))
bpc = update(bpc; maxiter=25, tol=1e-10)
#Test messages are converged
for pe in partitionedges(partitioned_tensornetwork(bpc))
@test update_message(bpc, pe)message(bpc, pe) atol = 1e-8
@test message_diff(update_message(bpc, pe), message(bpc, pe)) < 1e-8
end
#Test updating the underlying tensornetwork in the cache
v = first(vertices(ψψ))
Expand All @@ -69,8 +70,8 @@ using Test: @test, @testset
eigs = eigvals(rdm)
@test size(rdm) == (2^length(vs), 2^length(vs))

@test all(eig -> imag(eig) ≈ 0, eigs)
@test all(eig -> real(eig) >= -eps(eltype(eig)), eigs)
@test all(eig -> abs(imag(eig)) <= 1e-16, eigs)
@test all(eig -> real(eig) >= -eps(eltype(real(eig))), eigs)

#Test edge case of network which evalutes to 0
χ = 2
Expand Down
Loading