Skip to content

Clean up belief propagation #57

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 35 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
63836c3
Split Into Low and High Level Functions. Introduced new Create Enviro…
JoeyT1994 Jan 6, 2023
9acd576
Renamed BeliefPropagation.jl
JoeyT1994 Jan 6, 2023
3242407
Added a function to partition to do a grouping then a partitioning on…
JoeyT1994 Jan 7, 2023
a00e47b
Modified the reduced density matrix function to be neater
JoeyT1994 Jan 7, 2023
aa9beb1
Simplied Environment Calculations. Renamed Some Filed
JoeyT1994 Jan 7, 2023
da9029b
Added function for checking environment is correct on a subset of ver…
JoeyT1994 Jan 8, 2023
27455ef
Modified Example to work with new BP changes
JoeyT1994 Jan 11, 2023
aa7b9e1
Simplified BP Further
JoeyT1994 Jan 11, 2023
5978ae4
Added Catch to Subgraph_vertices() if _npartitions=1 (KaHyPar and Met…
JoeyT1994 Jan 11, 2023
64f3d65
Updated Belief Propagation Testing
JoeyT1994 Jan 11, 2023
7121864
New Function for splitting the index of a tn along a specified subset…
JoeyT1994 Jan 11, 2023
cd7aa31
Some Formatting Changes. Added Function ability to Split_Index
JoeyT1994 Jan 11, 2023
26ff61a
Merged With Upstream/main Changes
JoeyT1994 Jan 11, 2023
cb89e6b
Formatting
JoeyT1994 Jan 11, 2023
ad5fe94
Improved Directional Capabilities of Get_Environment()
JoeyT1994 Jan 20, 2023
4173cd5
Improved Edge Splitting Function
JoeyT1994 Jan 20, 2023
89c5167
Slight Change in NPartitions=1 Catching Mechanism
JoeyT1994 Jan 20, 2023
ab39429
Merge remote-tracking branch 'upstream/main' into BeliefPropagationV2
JoeyT1994 Jan 20, 2023
b0a1e7c
Merge remote-tracking branch 'upstream/main' into BeliefPropagationV2
JoeyT1994 Jan 23, 2023
080ef53
Added Functionality to unpack vertices from a DataGraph with graphs d…
JoeyT1994 Jan 31, 2023
862ee8e
Merge remote-tracking branch 'upstream/main' into BeliefPropagationV2
JoeyT1994 Jan 31, 2023
60016a9
Added Dependency of BP Test on SplitApplyCombine.jl
JoeyT1994 Jan 31, 2023
50d82df
Commented Out Rogue Tests
JoeyT1994 Feb 1, 2023
6c9cf1f
Wrapped BP Example in a main() wrapper
JoeyT1994 Feb 1, 2023
fa848b3
Improved Partition to allow for multiple keyword arguments and then p…
JoeyT1994 Feb 2, 2023
2d7e823
Renamed Unwrap Graph Vertices and used a better method for determinin…
JoeyT1994 Feb 2, 2023
403eef9
Improvements to naming, grammar and code style
JoeyT1994 Feb 2, 2023
a728f44
Merge remote-tracking branch 'upstream/main' into BeliefPropagationV2
JoeyT1994 Feb 2, 2023
a1d4ea4
Improved Code Structure
JoeyT1994 Feb 2, 2023
b6bdf96
Formatting
JoeyT1994 Feb 2, 2023
a9c29ce
Removed Accidentally Tracked Files
JoeyT1994 Feb 2, 2023
52d0710
Untracked Files
JoeyT1994 Feb 2, 2023
335b294
Merge remote-tracking branch 'upstream/main' into BeliefPropagationV2
JoeyT1994 Feb 2, 2023
73c0559
Removed Redundant Comment
JoeyT1994 Feb 2, 2023
4096a77
Formatting
JoeyT1994 Feb 6, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ with vertex data:
3 │ Index[(dim=2|id=683|"S=1/2,Site,n=3")]

and edge data:
0-element Dictionaries.Dictionary{NamedGraphs.NamedEdge{Int64}, Vector{Index}}
0-element Dictionaries.Dictionary{NamedEdge{Int64}, Vector{Index}}

julia> tn1 = ITensorNetwork(s; link_space=2)
ITensorNetwork{Int64} with 3 vertices:
Expand Down
108 changes: 65 additions & 43 deletions examples/belief_propagation/bpexample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,69 @@ using Compat
using ITensors
using Metis
using ITensorNetworks
using Random
using SplitApplyCombine

using ITensorNetworks: construct_initial_mts, update_all_mts, get_single_site_expec

n = 4
system_dims = (n, n)
g = named_grid(system_dims)
# g = named_comb_tree(system_dims)
s = siteinds("S=1/2", g)
chi = 2

ψ = randomITensorNetwork(s; link_space=chi)

ψψ = norm_sqr_network(ψ; flatten=true, map_bra_linkinds=prime)
combiners = linkinds_combiners(ψψ)
ψψ = combine_linkinds(ψψ, combiners)

# Apply Sz to site v
v = one.(system_dims)
Oψ = copy(ψ)
Oψ[v] = apply(op("Sz", s[v]), ψ[v])
ψOψ = inner_network(ψ, Oψ; flatten=true, map_bra_linkinds=prime)
ψOψ = combine_linkinds(ψOψ, combiners)

# Get the value of sz on v via exact contraction
contract_seq = contraction_sequence(ψψ)
actual_sz = contract(ψOψ; sequence=contract_seq)[] / contract(ψψ; sequence=contract_seq)[]

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

niters = 20

nsites = 1
println("\nFirst " * string(nsites) * " sites form a subgraph")
mts = construct_initial_mts(ψψ, nsites; init=(I...) -> @compat allequal(I) ? 1 : 0)
@show get_single_site_expec(ψψ, mts, ψOψ, v)
mts = update_all_mts(ψψ, mts, niters)
@show get_single_site_expec(ψψ, mts, ψOψ, v)

nsites = 4
println("\nNow " * string(nsites) * " sites form a subgraph")
mts = construct_initial_mts(ψψ, nsites; init=(I...) -> @compat allequal(I) ? 1 : 0)
@show get_single_site_expec(ψψ, mts, ψOψ, v)
mts = update_all_mts(ψψ, mts, niters)
@show get_single_site_expec(ψψ, mts, ψOψ, v)
using ITensorNetworks:
compute_message_tensors, calculate_contraction, contract_inner, nested_graph_leaf_vertices

function main()
n = 4
dims = (n, n)
g = named_grid(dims)
s = siteinds("S=1/2", g)
chi = 2

Random.seed!(5467)

#bra
ψ = randomITensorNetwork(s; link_space=chi)
#bra-ket (but not actually contracted)
ψψ = ψ ⊗ prime(dag(ψ); sites=[])

#Site to take expectation value on
v = (1, 1)

#Now do Simple Belief Propagation to Measure Sz on Site v
nsites = 1

vertex_groups = nested_graph_leaf_vertices(
partition(partition(ψψ, group(v -> v[1], vertices(ψψ))); nvertices_per_partition=nsites)
)
mts = compute_message_tensors(ψψ; vertex_groups=vertex_groups)
sz_bp =
calculate_contraction(
ψψ, mts, [(v, 1)]; verts_tensors=ITensor[apply(op("Sz", s[v]), ψ[v])]
)[] / calculate_contraction(ψψ, mts, [(v, 1)])[]

println(
"Simple Belief Propagation Gives Sz on Site " * string(v) * " as " * string(sz_bp)
)

#Now do General Belief Propagation to Measure Sz on Site v
nsites = 4
vertex_groups = nested_graph_leaf_vertices(
partition(partition(ψψ, group(v -> v[1], vertices(ψψ))); nvertices_per_partition=nsites)
)
mts = compute_message_tensors(ψψ; vertex_groups=vertex_groups)
sz_bp =
calculate_contraction(
ψψ, mts, [(v, 1)]; verts_tensors=ITensor[apply(op("Sz", s[v]), ψ[v])]
)[] / calculate_contraction(ψψ, mts, [(v, 1)])[]

println(
"General Belief Propagation (2-site subgraphs) Gives Sz on Site " *
string(v) *
" as " *
string(sz_bp),
)

#Now do it exactly
Oψ = copy(ψ)
Oψ[v] = apply(op("Sz", s[v]), ψ[v])
sz_exact = contract_inner(Oψ, ψ) / contract_inner(ψ, ψ)

return println("The exact value of Sz on Site " * string(v) * " is " * string(sz_exact))
end

main()
16 changes: 16 additions & 0 deletions src/abstractitensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,22 @@ function combine_linkinds(tn::AbstractITensorNetwork, combiners=linkinds_combine
return combined_tn
end

function split_index(
tn::AbstractITensorNetwork,
edges_to_split;
src_ind_map::Function=identity,
dst_ind_map::Function=prime,
)
tn = copy(tn)
for e in edges_to_split
inds = commoninds(tn[src(e)], tn[dst(e)])
tn[src(e)] = replaceinds(tn[src(e)], inds, src_ind_map(inds))
tn[dst(e)] = replaceinds(tn[dst(e)], inds, dst_ind_map(inds))
end

return tn
end

function flatten_networks(
tn1::AbstractITensorNetwork,
tn2::AbstractITensorNetwork;
Expand Down
Loading