diff --git a/src/beliefpropagation/beliefpropagation.jl b/src/beliefpropagation/beliefpropagation.jl index cfbafdf6..a7f2f9a6 100644 --- a/src/beliefpropagation/beliefpropagation.jl +++ b/src/beliefpropagation/beliefpropagation.jl @@ -142,7 +142,7 @@ end """ Given a subet of partitionvertices of a ptn get the incoming message tensors to that region """ -function get_environment_tensors(ptn::PartitionedGraph, mts, verts::Vector) +function environment_tensors(ptn::PartitionedGraph, mts, verts::Vector) partition_verts = partitionvertices(ptn, verts) central_verts = vertices(ptn, partition_verts) @@ -156,34 +156,8 @@ function get_environment_tensors(ptn::PartitionedGraph, mts, verts::Vector) return vcat(env_tensors, central_tensors) end -function get_environment_tensors( +function environment_tensors( ptn::PartitionedGraph, mts, partition_verts::Vector{<:PartitionVertex} ) - return get_environment_tensors(ptn, mts, vertices(ptn, partition_verts)) -end - -""" -Calculate the contraction of a tensor network centred on the vertices verts. Using message tensors. -Defaults to using tn[verts] as the local network but can be overriden -""" -function approx_network_region( - ptn::PartitionedGraph, - mts, - verts::Vector; - verts_tensors=ITensor[(unpartitioned_graph(ptn))[v] for v in verts], -) - environment_tensors = get_environment_tensors(ptn, mts, verts) - - return vcat(environment_tensors, verts_tensors) -end - -function approx_network_region( - ptn::PartitionedGraph, - mts, - partition_verts::Vector{<:PartitionVertex}; - verts_tensors=ITensor[ - (unpartitioned_graph(ptn))[v] for v in vertices(ptn, partition_verts) - ], -) - return approx_network_region(ptn, mts, vertices(ptn, partition_verts); verts_tensors) + return environment_tensors(ptn, mts, vertices(ptn, partition_verts)) end diff --git a/test/test_apply.jl b/test/test_apply.jl index 55bca9aa..51587cac 100644 --- a/test/test_apply.jl +++ b/test/test_apply.jl @@ -1,7 +1,7 @@ using ITensorNetworks using ITensorNetworks: belief_propagation, - get_environment_tensors, + environment_tensors, contract_inner, message_tensors, vidal_gauge, @@ -32,14 +32,14 @@ using SplitApplyCombine #Simple Belief Propagation Grouping pψψ_SBP = PartitionedGraph(ψψ, group(v -> v[1], vertices(ψψ))) mtsSBP = belief_propagation(pψψ_SBP; contract_kwargs=(; alg="exact"), niters=50) - envsSBP = get_environment_tensors(pψψ_SBP, mtsSBP, PartitionVertex.([v1, v2])) + envsSBP = environment_tensors(pψψ_SBP, mtsSBP, PartitionVertex.([v1, v2])) ψ_vidal, bond_tensors = vidal_gauge(ψ, pψψ_SBP, mtsSBP) #This grouping will correspond to calculating the environments exactly (each column of the grid is a partition) pψψ_GBP = PartitionedGraph(ψψ, group(v -> v[1][1], vertices(ψψ))) mtsGBP = belief_propagation(pψψ_GBP; contract_kwargs=(; alg="exact"), niters=50) - envsGBP = get_environment_tensors(pψψ_GBP, mtsGBP, [(v1, 1), (v1, 2), (v2, 1), (v2, 2)]) + envsGBP = environment_tensors(pψψ_GBP, mtsGBP, [(v1, 1), (v1, 2), (v2, 1), (v2, 2)]) ngates = 5 diff --git a/test/test_belief_propagation.jl b/test/test_belief_propagation.jl index ffcd2430..a6976807 100644 --- a/test/test_belief_propagation.jl +++ b/test/test_belief_propagation.jl @@ -2,11 +2,11 @@ using ITensorNetworks using ITensorNetworks: ising_network, belief_propagation, - approx_network_region, split_index, contract_inner, contract_boundary_mps, - message_tensors + message_tensors, + environment_tensors using Test using Compat using ITensors @@ -38,13 +38,11 @@ ITensors.disable_warn_order() pψψ = PartitionedGraph(ψψ, group(v -> v[1], vertices(ψψ))) mts = belief_propagation(pψψ) - numerator_tensors = approx_network_region( - pψψ, mts, [PartitionVertex(v)]; verts_tensors=[ψ[v], op("Sz", s[v]), dag(prime(ψ[v]))] - ) - denominator_tensors = approx_network_region(pψψ, mts, [PartitionVertex(v)]) - bp_sz = contract(numerator_tensors)[] / contract(denominator_tensors)[] + env_tensors = environment_tensors(pψψ, mts, [PartitionVertex(v)]) + numerator = contract(vcat(env_tensors, ITensor[ψ[v], op("Sz", s[v]), dag(prime(ψ[v]))]))[] + denominator = contract(vcat(env_tensors, ITensor[ψ[v], op("I", s[v]), dag(prime(ψ[v]))]))[] - @test abs.(bp_sz - exact_sz) <= 1e-14 + @test abs.((numerator / denominator) - exact_sz) <= 1e-14 #Now test on a tree, should also be exact g = named_comb_tree((4, 4)) @@ -63,13 +61,11 @@ ITensors.disable_warn_order() pψψ = PartitionedGraph(ψψ, group(v -> v[1], vertices(ψψ))) mts = belief_propagation(pψψ) - numerator_tensors = approx_network_region( - pψψ, mts, [PartitionVertex(v)]; verts_tensors=[ψ[v], op("Sz", s[v]), dag(prime(ψ[v]))] - ) - denominator_tensors = approx_network_region(pψψ, mts, [PartitionVertex(v)]) - bp_sz = contract(numerator_tensors)[] / contract(denominator_tensors)[] + env_tensors = environment_tensors(pψψ, mts, [PartitionVertex(v)]) + numerator = contract(vcat(env_tensors, ITensor[ψ[v], op("Sz", s[v]), dag(prime(ψ[v]))]))[] + denominator = contract(vcat(env_tensors, ITensor[ψ[v], op("I", s[v]), dag(prime(ψ[v]))]))[] - @test abs.(bp_sz - exact_sz) <= 1e-14 + @test abs.((numerator / denominator) - exact_sz) <= 1e-14 #Now test two-site expec taking on the partition function of the Ising model. Not exact, but close g_dims = (3, 4) @@ -87,15 +83,12 @@ ITensors.disable_warn_order() pψψ = PartitionedGraph(ψψ; nvertices_per_partition=2, backend="Metis") mts = belief_propagation(pψψ; niters=20) - numerator_network = approx_network_region( - pψψ, mts, vs; verts_tensors=ITensor[ψOψ[v] for v in vs] - ) - denominator_network = approx_network_region(pψψ, mts, vs) - bp_szsz = - ITensors.contract(numerator_network)[] / ITensors.contract(denominator_network)[] + env_tensors = environment_tensors(pψψ, mts, vs) + numerator = contract(vcat(env_tensors, ITensor[ψOψ[v] for v in vs]))[] + denominator = contract(vcat(env_tensors, ITensor[ψψ[v] for v in vs]))[] - @test abs.(bp_szsz - actual_szsz) <= 0.05 + @test abs.((numerator / denominator) - actual_szsz) <= 0.05 #Test forming a two-site RDM. Check it has the correct size, trace 1 and is PSD g_dims = (3, 3) @@ -110,13 +103,9 @@ ITensors.disable_warn_order() mts = belief_propagation(pψψ; niters=20) ψψsplit = split_index(ψψ, NamedEdge.([(v, 1) => (v, 2) for v in vs])) + env_tensors = environment_tensors(pψψ, mts, [(v, 2) for v in vs]) rdm = ITensors.contract( - approx_network_region( - pψψ, - mts, - [(v, 2) for v in vs]; - verts_tensors=ITensor[ψψsplit[vp] for vp in [(v, 2) for v in vs]], - ), + vcat(env_tensors, ITensor[ψψsplit[vp] for vp in [(v, 2) for v in vs]]) ) rdm = array((rdm * combiner(inds(rdm; plev=0)...)) * combiner(inds(rdm; plev=1)...)) @@ -151,12 +140,12 @@ ITensors.disable_warn_order() ), ) - numerator_tensors = approx_network_region(pψψ, mts, [v]; verts_tensors=ITensor[ψOψ[v]]) - denominator_tensors = approx_network_region(pψψ, mts, [v]) - bp_sz = ITensors.contract(numerator_tensors)[] / ITensors.contract(denominator_tensors)[] + env_tensors = environment_tensors(pψψ, mts, [v]) + numerator = contract(vcat(env_tensors, ITensor[ψOψ[v]]))[] + denominator = contract(vcat(env_tensors, ITensor[ψψ[v]]))[] exact_sz = contract_boundary_mps(ψOψ; cutoff=1e-16) / contract_boundary_mps(ψψ; cutoff=1e-16) - @test abs.(bp_sz - exact_sz) <= 1e-5 + @test abs.((numerator / denominator) - exact_sz) <= 1e-5 end