Skip to content

Belief Propagation #17

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

Merged
merged 32 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
87f13a9
Added Belief Propagation Example
JoeyT1994 Nov 4, 2022
c748958
Belief Propagation Functions Added to src
JoeyT1994 Nov 4, 2022
9a6fdef
BoundaryMPS Function Added
JoeyT1994 Nov 4, 2022
6712f11
Functions for dealing with subgraphs added
JoeyT1994 Nov 4, 2022
1aeebf0
A few additions to utils
JoeyT1994 Nov 4, 2022
d76e020
Extra Includes
JoeyT1994 Nov 4, 2022
f34b67e
Extra Functionality for Combining and Uncombining networks
JoeyT1994 Nov 4, 2022
471f8a0
Functions for creating some specific ITensorNetworks
JoeyT1994 Nov 4, 2022
ac4d45d
Functions for creating some specific ITensorNetworks
JoeyT1994 Nov 4, 2022
9e428d1
Function for Renaming Vertices ITensorNetwork
JoeyT1994 Nov 4, 2022
da60953
Random Tensor Network Function moved out
JoeyT1994 Nov 4, 2022
70864f4
Modified Example and added a Function
JoeyT1994 Nov 4, 2022
fa119f8
Reformatted
JoeyT1994 Nov 4, 2022
3915477
Merge branch 'main' into main
mtfishman Nov 7, 2022
f3ea038
Renamed SpecialITensorNetworks Functions. Simplified Code in SpecialI…
JoeyT1994 Nov 7, 2022
961f47a
Formatting Changes
JoeyT1994 Nov 7, 2022
eb14c13
Partitioning of a graph into subgraphs is now by introducing a DataGr…
JoeyT1994 Nov 7, 2022
1e2aff8
Removed the need to pass around g, all info in psi
JoeyT1994 Nov 16, 2022
3fb6e40
Removed the need to pass around g, all info in psi
JoeyT1994 Nov 16, 2022
81c0fc0
Swapped out the BMPS contraction alg for a better one from Matt
JoeyT1994 Nov 16, 2022
c9313ed
Removed Redundant Function
JoeyT1994 Nov 16, 2022
351b20d
Added Different MT Initialisation Options
JoeyT1994 Nov 16, 2022
46f70ef
Changed rename_vertices to rename_vertices_itn to avoid a conflict
JoeyT1994 Nov 17, 2022
fbf33de
Changed rename_vertices to rename_vertices_itn to avoid a conflict
JoeyT1994 Nov 17, 2022
e9fa1ba
Added Contraction Sequence Optimisation Options
JoeyT1994 Nov 17, 2022
ff0f791
Better Initialisation Options
JoeyT1994 Nov 17, 2022
1d5ce1c
Introduced function to derive graph from itn
JoeyT1994 Nov 18, 2022
189e855
Removed need to carry around the IndsNetwork
JoeyT1994 Nov 18, 2022
77972ea
Merge remote-tracking branch 'upstream/main'
JoeyT1994 Nov 20, 2022
2342f6e
Removed Get_graph_from_itn as not needed
JoeyT1994 Nov 22, 2022
1de26a7
Fixed a bug by deepcopying
JoeyT1994 Nov 23, 2022
bab1b36
Cleaned up a bit. Added function to calculate 2 site RDM on an edge
JoeyT1994 Nov 30, 2022
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
50 changes: 50 additions & 0 deletions examples/belief_propagation/bpexample.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using ITensors
using ITensorNetworks
using ITensorNetworks:
delta_network,
formsubgraphs,
partition,
flatten_thicken_bonds,
flattened_inner_network,
construct_initial_mts,
update_all_mts,
get_single_site_expec,
iterate_single_site_expec,
contract_boundary_mps
using KaHyPar
using Compat

#nxn GRID
n = 4
g = named_grid((n, n))
s = siteinds("S=1/2", g)
chi = 3

#Random Tensor Network, Flatten it too
psi = randomITensorNetwork(s; link_space=chi)
psiflat, combiners = flatten_thicken_bonds(deepcopy(psi))

v = (1, 1)

#Apply Sz to site v and flatten that
psiflatO = flatten_thicken_bonds(psi; ops=["Sz"], vops=[v], s=s, combiners=combiners)

#Get the value of sz on v via exact contraction
actual_sz = ITensors.contract(psiflatO)[1] / ITensors.contract(psiflat)[1]

println("Actual value of Sz on site " * string(v) * " is " * string(actual_sz))

nsites = 1
println("First " * string(nsites) * " sites form a subgraph")
dg_subgraphs = formsubgraphs(g, Int(n * n / nsites))
mts = construct_initial_mts(psiflat, dg_subgraphs; init=(I...) -> allequal(I) ? 1 : 0)
niters = 5

iterate_single_site_expec(psiflat, psiflatO, mts, dg_subgraphs, niters, v)

nsites = 4
println("Now " * string(nsites) * " sites form a subgraph")
dg_subgraphs = formsubgraphs(g, Int(n * n / nsites))
mts = construct_initial_mts(psiflat, dg_subgraphs; init=(I...) -> allequal(I) ? 1 : 0)

iterate_single_site_expec(psiflat, psiflatO, mts, dg_subgraphs, niters, v)
5 changes: 5 additions & 0 deletions src/ITensorNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ include("expect.jl")
include("models.jl")
include("tebd.jl")
include("itensornetwork.jl")
include("specialitensornetworks.jl")
include("renameitensornetwork.jl")
include("boundarymps.jl")
include("subgraphs.jl")
include("beliefpropagation.jl")
include(joinpath("treetensornetwork", "treetensornetwork.jl"))

include("exports.jl")
Expand Down
74 changes: 74 additions & 0 deletions src/abstractitensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,80 @@ end
# TODO: rename `sqnorm` to match https://github.com/JuliaStats/Distances.jl?
norm2(ψ::AbstractITensorNetwork; sequence) = contract_inner(ψ, ψ; sequence)

#Run over all edges of an ITENSORNETWORK and combine any duplicated indices into one
#Can take pre-existing combiners as an argument
function combine_link_indices(psi::AbstractITensorNetwork; combiners=nothing)
if (combiners == nothing)
outcombiners = Dict{NamedDimEdge{Tuple},ITensor}()
end
es = edges(psi)
new_psi = deepcopy(psi)

for e in es
v1, v2 = src(e), dst(e)
if (combiners == nothing)
is = commoninds(psi, e)
C = combiner(is; tags=tags(is[1]))
else
C = combiners[e]
end
new_psi[v1] = new_psi[v1] * C
new_psi[v2] = new_psi[v2] * C
if (combiners == nothing)
outcombiners[e] = C
end
end

if (combiners == nothing)
return new_psi, outcombiners
else
return new_psi
end
end

#Run over all edges of an ITENSORNETWORK and uncombine any indices which were previously combined
#Can take pre-existing combiners as an argument
function uncombine_link_indices(
psi::AbstractITensorNetwork, combiners=Dict{NamedDimEdge{Tuple},ITensor}()
)
es = edges(psi)
new_psi = deepcopy(psi)

for e in es
v1, v2 = src(e), dst(e)
if (haskey(combiners, e))
C = combiners[e]
new_psi[v1] = new_psi[v1] * C
new_psi[v2] = new_psi[v2] * C
end
end

return new_psi
end

#GIVEN AN ITENSOR NETWORK WITH DANGLING (PHYSICAL) BONDS, CONTRACT IT ONTO ITS CONJUGATE AND COMBINE ALL
#DOUBLE INDICES. RETURN THE COMBINERS USED ON THE LINK INDICES
#CAN APPLY LOCAL OPS ALONG THE PHYSICAL INDICES OF THE ITENSORNETWORK BEFORE CONTRACTION (ops is a list of strings, vops is a list of vertices, s is the indsnetwork)
#CAN ALSO PASS COMBINERS FROM BEFORE TO ENSURE THE INDICES ARE CONSISTENT BETWEEN DIFFERENT APPLICATIONS OF THIS FUNCTION
function flatten_thicken_bonds(
psi::AbstractITensorNetwork; ops=nothing, vops=nothing, s=nothing, combiners=nothing
)
psiin = deepcopy(psi)
outcombiners = Dict{NamedDimEdge{Tuple},ITensor}()

if (ops != nothing)
for (op, v) in zip(ops, vops)
O = ITensor(Op(op, v), s)
psiin[v] = noprime!(O * psiin[v])
end
end

flatpsi = flattened_inner_network(psiin, psi)
flatpsi = rename_vertices_itn(flatpsi, Dictionary(vertices(flatpsi), vertices(psi)))

return combine_link_indices(flatpsi; combiners=combiners)
end

#
# Printing
#
Expand Down
Loading