Skip to content

Commit

Permalink
Removed approx_network_region
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 committed Jan 23, 2024
1 parent 64ddf7c commit 504085c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 63 deletions.
32 changes: 3 additions & 29 deletions src/beliefpropagation/beliefpropagation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
6 changes: 3 additions & 3 deletions test/test_apply.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ITensorNetworks
using ITensorNetworks:
belief_propagation,
get_environment_tensors,
environment_tensors,
contract_inner,
message_tensors,
vidal_gauge,
Expand Down Expand Up @@ -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

Expand Down
51 changes: 20 additions & 31 deletions test/test_belief_propagation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)...))
Expand Down Expand Up @@ -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

0 comments on commit 504085c

Please sign in to comment.