Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 committed Jan 18, 2024
1 parent b9aa717 commit d1f2c6e
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 96 deletions.
6 changes: 5 additions & 1 deletion examples/apply/apply_bp/apply_bp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ function simple_update_vidal(os, ψ::ITensorNetwork; maxdim, regauge=false)
println("regauge")
ψ_symmetric, pψψ_symmetric, mts = vidal_to_symmetric_gauge(ψ, bond_tensors)
mts = belief_propagation(
pψψ_symmetric, mts; contract_kwargs=(; alg="exact"), niters=50, target_precision=1e-5
pψψ_symmetric,
mts;
contract_kwargs=(; alg="exact"),
niters=50,
target_precision=1e-5,
)
ψ, bond_tensors = vidal_gauge(ψ_symmetric, pψψ_symmetric, mts)
end
Expand Down
6 changes: 3 additions & 3 deletions examples/apply/apply_bp/apply_bp_run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ opname = "RandomUnitary"
# graph = named_comb_tree
graph = named_grid

dims = (6,6)
dims = (6, 6)

ψ_bp, pψψ_bp, mts_bp, ψ_vidal, pψψ_vidal, mts_vidal = main(;
seed=1234,
Expand All @@ -35,15 +35,15 @@ mts_bp = belief_propagation(
contract_kwargs=(; alg="exact"),
niters=50,
target_precision=1e-7,
verbose = true
verbose=true,
)
mts_vidal = belief_propagation(
pψψ_vidal,
mts_vidal;
contract_kwargs=(; alg="exact"),
niters=50,
target_precision=1e-7,
verbose = true
verbose=true,
)

sz_bp = @show expect_bp("Sz", v, ψ_bp, pψψ_bp, mts_bp)
Expand Down
38 changes: 26 additions & 12 deletions examples/belief_propagation/bpexample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ using SplitApplyCombine
using NamedGraphs

using ITensorNetworks:
belief_propagation,
approx_network_region,
contract_inner,
message_tensors
belief_propagation, approx_network_region, contract_inner, message_tensors

function main()
n = 4
Expand All @@ -31,9 +28,12 @@ function main()

#Now do Simple Belief Propagation to Measure Sz on Site v
pψψ = PartitionedGraph(ψψ, collect(values(group(v -> v[1], vertices(ψψ)))))
mts = belief_propagation(pψψ; contract_kwargs=(; alg="exact"), verbose = true, niters = 10, target_precision = 1e-3)
mts = belief_propagation(
pψψ; contract_kwargs=(; alg="exact"), verbose=true, niters=10, target_precision=1e-3
)
numerator_tensors = approx_network_region(
pψψ, mts, [(v, 1)]; verts_tensors=[apply(op("Sz", s[v]), ψ[v])])
pψψ, mts, [(v, 1)]; verts_tensors=[apply(op("Sz", s[v]), ψ[v])]
)
denominator_tensors = approx_network_region(pψψ, mts, [(v, 1)])
sz_bp = contract(numerator_tensors)[] / contract(denominator_tensors)[]

Expand All @@ -43,9 +43,12 @@ function main()

#Now do Column-wise General Belief Propagation to Measure Sz on Site v
pψψ = PartitionedGraph(ψψ, collect(values(group(v -> v[1][1], vertices(ψψ)))))
mts = belief_propagation(pψψ; contract_kwargs=(; alg="exact"), verbose = true, niters = 10, target_precision = 1e-3)
mts = belief_propagation(
pψψ; contract_kwargs=(; alg="exact"), verbose=true, niters=10, target_precision=1e-3
)
numerator_tensors = approx_network_region(
pψψ, mts, [(v, 1)]; verts_tensors=[apply(op("Sz", s[v]), ψ[v])])
pψψ, mts, [(v, 1)]; verts_tensors=[apply(op("Sz", s[v]), ψ[v])]
)
denominator_tensors = approx_network_region(pψψ, mts, [(v, 1)])
sz_gen_bp = contract(numerator_tensors)[] / contract(denominator_tensors)[]

Expand All @@ -64,14 +67,25 @@ function main()
ψOψ = combine_linkinds(ψOψ, combiners)

pψψ = PartitionedGraph(ψψ, collect(values(group(v -> v[1], vertices(ψψ)))))
mts = belief_propagation(pψψ; itensor_constructor=inds_e -> ITensor[dense(delta(i)) for i in inds_e], contract_kwargs=(;alg="density_matrix",output_structure=path_graph_structure,maxdim = 8,contraction_sequence_alg="optimal"))
numerator_tensors = approx_network_region(
pψψ, mts, [v]; verts_tensors=[ψOψ[v]])
mts = belief_propagation(
pψψ;
itensor_constructor=inds_e -> ITensor[dense(delta(i)) for i in inds_e],
contract_kwargs=(;
alg="density_matrix",
output_structure=path_graph_structure,
maxdim=8,
contraction_sequence_alg="optimal",
),
)
numerator_tensors = approx_network_region(pψψ, mts, [v]; verts_tensors=[ψOψ[v]])
denominator_tensors = approx_network_region(pψψ, mts, [v])
sz_MPS_bp = contract(numerator_tensors)[] / contract(denominator_tensors)[]

println(
"Column-Wise MPS Belief Propagation Gives Sz on Site " * string(v) * " as " * string(sz_gen_bp)
"Column-Wise MPS Belief Propagation Gives Sz on Site " *
string(v) *
" as " *
string(sz_gen_bp),
)

#Now do it exactly
Expand Down
15 changes: 8 additions & 7 deletions examples/belief_propagation/bpsequences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ using Graphs
using NamedGraphs

using ITensorNetworks:
belief_propagation,
approx_network_region,
contract_inner,
message_tensors,
edge_sequence
belief_propagation, approx_network_region, contract_inner, message_tensors, edge_sequence

function main()
g_labels = [
Expand Down Expand Up @@ -51,7 +47,9 @@ function main()
contract_kwargs=(; alg="exact"),
target_precision=1e-10,
niters=100,
edges=[PartitionEdge.(e) for e in edge_sequence(partitioned_graph(pψψ); alg="parallel")],
edges=[
PartitionEdge.(e) for e in edge_sequence(partitioned_graph(pψψ); alg="parallel")
],
verbose=true,
)
print("Sequential updates (sequence is default edge list of the message tensors): ")
Expand All @@ -61,7 +59,10 @@ function main()
contract_kwargs=(; alg="exact"),
target_precision=1e-10,
niters=100,
edges=PartitionEdge.([e for e in vcat(edges(partitioned_graph(pψψ)), reverse.(edges(partitioned_graph(pψψ))))]),
edges=PartitionEdge.([
e for
e in vcat(edges(partitioned_graph(pψψ)), reverse.(edges(partitioned_graph(pψψ))))
]),
verbose=true,
)
print("Sequential updates (sequence is our custom sequence finder): ")
Expand Down
6 changes: 4 additions & 2 deletions examples/belief_propagation/sqrt_bp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function main(; n, niters, network="ising", β=nothing, h=nothing, χ=nothing)

mts = @time belief_propagation(pψψ; niters, contract_kwargs=(; alg="exact"))
numerator_network = approx_network_region(
pψψ, mts, [(v, 1)]; verts_tensors=ITensor[apply(op("Sz", s[v]), ψ[v])])
pψψ, mts, [(v, 1)]; verts_tensors=ITensor[apply(op("Sz", s[v]), ψ[v])]
)
denominator_network = approx_network_region(pψψ, mts, [(v, 1)])
sz_bp =
ITensors.contract(
Expand All @@ -52,7 +53,8 @@ function main(; n, niters, network="ising", β=nothing, h=nothing, χ=nothing)
mts_sqrt = @time sqrt_belief_propagation(pψψ; niters)

numerator_network = approx_network_region(
pψψ, mts_sqrt, [(v, 1)]; verts_tensors=ITensor[apply(op("Sz", s[v]), ψ[v])])
pψψ, mts_sqrt, [(v, 1)]; verts_tensors=ITensor[apply(op("Sz", s[v]), ψ[v])]
)
denominator_network = approx_network_region(pψψ, mts_sqrt, [(v, 1)])
sz_sqrt_bp =
contract(
Expand Down
7 changes: 4 additions & 3 deletions examples/boundary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ tn = ITensorNetwork(named_grid((6, 3)); link_space=4)

@visualize tn


ptn = PartitionedGraph(tn, NamedGraphs.partition_vertices(underlying_graph(tn); nvertices_per_partition = 2))
sub_vs_1, sub_vs_2 =vertices(ptn, PartitionVertex(1)), vertices(ptn, PartitionVertex(2))
ptn = PartitionedGraph(
tn, NamedGraphs.partition_vertices(underlying_graph(tn); nvertices_per_partition=2)
)
sub_vs_1, sub_vs_2 = vertices(ptn, PartitionVertex(1)), vertices(ptn, PartitionVertex(2))

@show (1, 1) sub_vs_1
@show (6, 3) sub_vs_2
Expand Down
7 changes: 3 additions & 4 deletions examples/dynamics/heavy_hex_ising_real_tebd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@ hummingbird_processor_graph() = ibm_processor_graph(2, 4)
osprey_processor_graph() = ibm_processor_graph(6, 12)

"""Take the expectation value of o on an ITN using belief propagation"""
function expect_state_SBP(
o::ITensor, ψ::AbstractITensorNetwork, pψψ::PartitionedGraph, mts
)
function expect_state_SBP(o::ITensor, ψ::AbstractITensorNetwork, pψψ::PartitionedGraph, mts)
= apply(o, ψ; cutoff=1e-16)
ψ = copy(ψ)
s = siteinds(ψ)
vs = vertices(s)[findall(i -> (length(commoninds(s[i], inds(o))) != 0), vertices(s))]
vs_braket = [(v, 1) for v in vs]

numerator_tensors = approx_network_region(
pψψ, mts, vs_braket; verts_tensors=ITensor[Oψ[v] for v in vs])
pψψ, mts, vs_braket; verts_tensors=ITensor[Oψ[v] for v in vs]
)
denominator_tensors = approx_network_region(pψψ, mts, vs_braket)
num_seq = contraction_sequence(numerator_tensors; alg="optimal")
den_seq = contraction_sequence(denominator_tensors; alg="optimal")
Expand Down
20 changes: 16 additions & 4 deletions examples/gauging/gauging_itns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ using NamedGraphs: add_edges!, rem_vertex!, hexagonal_lattice_graph
using Graphs

"""Eager Gauging"""
function eager_gauging::ITensorNetwork, pψψ::PartitionedGraph, bond_tensors::DataGraph, mts)
function eager_gauging(
ψ::ITensorNetwork, pψψ::PartitionedGraph, bond_tensors::DataGraph, mts
)
isometries = vidal_itn_isometries(ψ, bond_tensors)

ψ = copy(ψ)
Expand All @@ -32,7 +34,9 @@ function eager_gauging(ψ::ITensorNetwork, pψψ::PartitionedGraph, bond_tensors
pe = NamedGraphs.partition_edge(pψψ, NamedEdge((vsrc, 1) => (vdst, 1)))
normalize!(isometries[e])
normalize!(isometries[reverse(e)])
mts[pe], mts[PartitionEdge(reverse(NamedGraphs.parent(pe)))] = ITensorNetwork(isometries[e]),
mts[pe], mts[PartitionEdge(reverse(NamedGraphs.parent(pe)))] = ITensorNetwork(
isometries[e]
),
ITensorNetwork(isometries[reverse(e)])
end

Expand Down Expand Up @@ -70,12 +74,20 @@ function benchmark_state_gauging(
)
else
times_iters[i] = @elapsed mts, _ = belief_propagation_iteration(
pψψ, mts; contract_kwargs=(; alg="exact"), edges=[PartitionEdge.(e) for e in edge_sequence(partitioned_graph(pψψ); alg="parallel")])
pψψ,
mts;
contract_kwargs=(; alg="exact"),
edges=[
PartitionEdge.(e) for e in edge_sequence(partitioned_graph(pψψ); alg="parallel")
],
)
end

times_gauging[i] = @elapsed ψ, bond_tensors = vidal_gauge(ψinit, pψψ, mts)
elseif mode == "eager"
times_iters[i] = @elapsed ψ, bond_tensors, mts = eager_gauging(ψ, pψψ, bond_tensors, mts)
times_iters[i] = @elapsed ψ, bond_tensors, mts = eager_gauging(
ψ, pψψ, bond_tensors, mts
)
else
times_iters[i] = @elapsed begin
for e in edges(ψ)
Expand Down
21 changes: 10 additions & 11 deletions src/approx_itensornetwork/partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,18 @@ function findfirst_on_edges(f::Function, graph::AbstractDataGraph)
end

function subgraphs(g::AbstractSimpleGraph, subgraph_vertices)
return subgraphs(NamedGraph(g), subgraph_vertices)
return subgraphs(NamedGraph(g), subgraph_vertices)
end
"""
subgraphs(g::AbstractGraph, subgraph_vertices)
Return a collection of subgraphs of `g` defined by the subgraph
vertices `subgraph_vertices`.
"""

"""
subgraphs(g::AbstractGraph, subgraph_vertices)
Return a collection of subgraphs of `g` defined by the subgraph
vertices `subgraph_vertices`.
"""
function subgraphs(g::AbstractGraph, subgraph_vertices)
return map(vs -> subgraph(g, vs), subgraph_vertices)
return map(vs -> subgraph(g, vs), subgraph_vertices)
end


"""
subgraphs(g::AbstractGraph; npartitions::Integer, kwargs...)
Expand Down Expand Up @@ -195,4 +194,4 @@ function _commoninds(partition::DataGraph)
outinds = noncommoninds(network...)
allinds = mapreduce(t -> [i for i in inds(t)], vcat, network)
return Vector(setdiff(allinds, outinds))
end
end
41 changes: 28 additions & 13 deletions src/beliefpropagation/beliefpropagation.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function message_tensors(
ptn::PartitionedGraph;
itensor_constructor=inds_e -> ITensor[dense(delta(inds_e))]
ptn::PartitionedGraph; itensor_constructor=inds_e -> ITensor[dense(delta(inds_e))]
)
mts = Dict()
for e in PartitionEdge.(edges(partitioned_graph(ptn)))
Expand All @@ -23,19 +22,27 @@ function update_message_tensor(
contract_kwargs=(; alg="density_matrix", output_structure=path_graph_structure, maxdim=1),
)
incoming_messages = [
mts[PartitionEdge(e_in)] for e_in in setdiff(boundary_edges(partitioned_graph(ptn), [NamedGraphs.parent(src(edge))]; dir=:in), [reverse(NamedGraphs.parent(edge))])
mts[PartitionEdge(e_in)] for e_in in setdiff(
boundary_edges(partitioned_graph(ptn), [NamedGraphs.parent(src(edge))]; dir=:in),
[reverse(NamedGraphs.parent(edge))],
)
]
incoming_messages = reduce(vcat, incoming_messages; init=ITensor[])

contract_list = ITensor[incoming_messages; ITensor(unpartitioned_graph(subgraph(ptn, [src(edge)])))]
contract_list = ITensor[
incoming_messages
ITensor(unpartitioned_graph(subgraph(ptn, [src(edge)])))
]

if contract_kwargs.alg != "exact"
mt, _ = contract(ITensorNetwork(contract_list); contract_kwargs...)
else
mt = contract(contract_list; sequence=contraction_sequence(contract_list; alg="optimal"))
mt = contract(
contract_list; sequence=contraction_sequence(contract_list; alg="optimal")
)
end

if isa(mt, ITensor)
if isa(mt, ITensor)
mt = ITensor[mt]
elseif isa(mt, ITensorNetwork)
mt = ITensor(mt)
Expand All @@ -61,8 +68,7 @@ function belief_propagation_iteration(
new_mts[e] = update_message_tensor(ptn, e, new_mts; contract_kwargs)

if compute_norm
LHS, RHS = ITensors.contract(mts[e]),
ITensors.contract(new_mts[e])
LHS, RHS = ITensors.contract(mts[e]), ITensors.contract(new_mts[e])
#This line only makes sense if the message tensors are rank 2??? Should fix this.
LHS /= sum(diag(LHS))
RHS /= sum(diag(RHS))
Expand Down Expand Up @@ -133,7 +139,11 @@ function belief_propagation(
return mts
end

function belief_propagation(ptn::PartitionedGraph; itensor_constructor=inds_e -> ITensor[dense(delta(inds_e))], kwargs...)
function belief_propagation(
ptn::PartitionedGraph;
itensor_constructor=inds_e -> ITensor[dense(delta(inds_e))],
kwargs...,
)
mts = message_tensors(ptn; itensor_constructor)
return belief_propagation(ptn, mts; kwargs...)
end
Expand All @@ -148,10 +158,15 @@ function get_environment(ptn::PartitionedGraph, mts, verts::Vector; dir=:in)
return get_environment(ptn, mts, setdiff(vertices(ptn), verts))
end

env_tensors = [mts[PartitionEdge(e)] for e in boundary_edges(partitioned_graph(ptn), NamedGraphs.parent.(partition_vertices); dir=:in)]
env_tensors = reduce(vcat, env_tensors; init = ITensor[])
central_tensors = ITensor[(unpartitioned_graph(ptn))[v] for v in setdiff(vertices(ptn, partition_vertices), verts)]

env_tensors = [
mts[PartitionEdge(e)] for e in
boundary_edges(partitioned_graph(ptn), NamedGraphs.parent.(partition_vertices); dir=:in)
]
env_tensors = reduce(vcat, env_tensors; init=ITensor[])
central_tensors = ITensor[
(unpartitioned_graph(ptn))[v] for v in setdiff(vertices(ptn, partition_vertices), verts)
]

return vcat(env_tensors, central_tensors)
end

Expand Down
Loading

0 comments on commit d1f2c6e

Please sign in to comment.