Skip to content

Commit

Permalink
Add weights(::AbstractITensorNetwork), new graph partitioning inter…
Browse files Browse the repository at this point in the history
…face
  • Loading branch information
mtfishman authored Dec 23, 2022
2 parents 1c45db7 + db6fea1 commit 652e59e
Show file tree
Hide file tree
Showing 30 changed files with 793 additions and 360 deletions.
176 changes: 88 additions & 88 deletions README.md

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using ITensorNetworks
using Documenter

DocMeta.setdocmeta!(ITensorNetworks, :DocTestSetup, :(using ITensorNetworks); recursive=true)
DocMeta.setdocmeta!(
ITensorNetworks, :DocTestSetup, :(using ITensorNetworks); recursive=true
)

makedocs(;
modules=[ITensorNetworks],
Expand Down
6 changes: 5 additions & 1 deletion examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ Z̃ = contract(Z, (1, 1) => (2, 1));
#+ eval=false

using ITensorNetworks, Weave
weave(joinpath(pkgdir(ITensorNetworks), "examples", "README.jl"); doctype="github", out_path=pkgdir(ITensorNetworks))
weave(
joinpath(pkgdir(ITensorNetworks), "examples", "README.jl");
doctype="github",
out_path=pkgdir(ITensorNetworks),
)
5 changes: 1 addition & 4 deletions examples/belief_propagation/bpexample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ using ITensors
using Metis
using ITensorNetworks

using ITensorNetworks:
construct_initial_mts,
update_all_mts,
get_single_site_expec
using ITensorNetworks: construct_initial_mts, update_all_mts, get_single_site_expec

n = 4
dims = (n, n)
Expand Down
20 changes: 20 additions & 0 deletions examples/boundary.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NamedGraphs
using ITensors
using ITensorNetworks
using ITensorUnicodePlots
using Metis

tn = ITensorNetwork(named_grid((6, 3)); link_space=4)

@visualize tn

g = subgraph_vertices(tn; nvertices_per_partition=2)
sub_vs_1, sub_vs_2 = g[1], g[2]

@show (1, 1) sub_vs_1
@show (6, 3) sub_vs_2

@show boundary_edges(tn, sub_vs_1)
@show boundary_vertices(tn, sub_vs_1)
@show inner_boundary_vertices(tn, sub_vs_1)
@show outer_boundary_vertices(tn, sub_vs_1)
17 changes: 17 additions & 0 deletions examples/distances.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NamedGraphs
using ITensors
using ITensorNetworks
using ITensorUnicodePlots

g = named_grid((3, 5))
s = siteinds("S=1/2", g)
ψ = ITensorNetwork(s; link_space=4)
@visualize ψ
@show center(ψ)
@show periphery(ψ)
t = dijkstra_tree(ψ, only(center(ψ)))
@visualize t
@show a_star(ψ, (2, 1), (2, 5))
@show mincut_partitions(ψ)
@show mincut_partitions(ψ, (1, 1), (3, 5))
@show subgraph_vertices(ψ; npartitions=2)
17 changes: 17 additions & 0 deletions examples/group_partition.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ITensors
using Graphs
using NamedGraphs
using ITensorNetworks
using SplitApplyCombine
using Metis

s = siteinds("S=1/2", named_grid(8))
tn = ITensorNetwork(s; link_space=2)
Z = prime(tn; sites=[]) tn
vertex_groups = group(v -> v[1], vertices(Z))
# Create two layers of partitioning
Z_p = partition(partition(Z, vertex_groups); nvertices_per_partition=2)
# Flatten the partitioned partitions
Z_verts = [
reduce(vcat, (vertices(Z_p[vp][v]) for v in vertices(Z_p[vp]))) for vp in vertices(Z_p)
]
55 changes: 55 additions & 0 deletions examples/mincut.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using NamedGraphs
using ITensors
using ITensorNetworks
using ITensorUnicodePlots

g = named_grid(5)
s = siteinds("S=1/2", g)

ψ = ITensorNetwork(s; link_space=10)

# ρ = flatten_networks(dag(ψ), ψ')

# Or:

ss = (dag(s), s'; merge_data=union)
ρ = ITensorNetwork(ss; link_space=2)

tn = ', ρ, ψ)
tn_flattened = flatten_networks', ρ, ψ)
# tn = ρ' ⊗ ρ ⊗ ψ
@visualize tn

@show center(tn)

v = first(center(tn))

dijk_parents = dijkstra_parents(tn, v)
dijk_mst = dijkstra_mst(tn, v)
dijk_tree = dijkstra_tree(tn, v)

bfs_tree_tn = bfs_tree(tn, v)

@show eccentricity(tn, v)
@show radius(tn)
@show radius(tn)
@show diameter(tn)
@show periphery(tn)

s = dijk_tree
t = bfs_tree_tn
@visualize s
@visualize t

v1 = first(periphery(tn))
nds = neighborhood_dists(tn, v1, nv(tn))
d_and_i = findmax(vd -> vd[2], nds)
v2 = nds[d_and_i[2]][1]
@show v1, v2
p1, p2 = mincut_partitions(tn, v1, v2)
@show p1
@show p2

display(adjacency_matrix(tn_flattened))
tn_flattened_p = symrcm_permute(tn_flattened)
display(adjacency_matrix(tn_flattened_p))
40 changes: 36 additions & 4 deletions examples/partition/kahypar_vs_metis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,39 @@ using ITensorNetworks
g = grid((16,))
npartitions = 4

kahypar_partitions = partition(g, npartitions; backend="KaHyPar")
metis_partitions = partition(g, npartitions; backend="Metis")
@show kahypar_partitions, length(unique(kahypar_partitions))
@show metis_partitions, length(unique(metis_partitions))
kahypar_partitions = subgraph_vertices(g; npartitions, backend="KaHyPar")
metis_partitions = subgraph_vertices(g; npartitions, backend="Metis")
@show kahypar_partitions, length(kahypar_partitions)
@show metis_partitions, length(metis_partitions)

g_parts = partition(g; npartitions)
@show nv(g_parts) == 4
@show nv(g_parts[1]) == 4
@show nv(g_parts[2]) == 4
@show nv(g_parts[3]) == 4
@show nv(g_parts[4]) == 4
@show issetequal(metis_partitions[2], vertices(g_parts[2]))

using ITensorNetworks
tn = ITensorNetwork(named_grid((4, 2)); link_space=3);

# subgraph_vertices
tn_sv = subgraph_vertices(tn; npartitions=2) # Same as `partition_vertices(tn; nvertices_per_partition=4)`

# partition_vertices
tn_pv = partition_vertices(tn; npartitions=2);
typeof(tn_pv)
tn_pv[1]
edges(tn_pv)
tn_pv[1 => 2]

# subgraphs
tn_sg = subgraphs(tn; npartitions=2);
typeof(tn_sg)
tn_sg[1]

# partition
tn_pg = partition(tn; npartitions=2);
typeof(tn_pg)
tn_pg[1]
tn_pg[1 => 2][:edges]
2 changes: 1 addition & 1 deletion examples/partition/partitioning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ neighbor_edges = [v => nv for nv in neighbors(ψ, v)]
@show [e => linkinds(ψ, e) for e in neighbor_edges]

npartitions = 4
partitions = partition, npartitions)
partitions = partition; npartitions)

@show partitions

Expand Down
16 changes: 16 additions & 0 deletions examples/steiner_tree.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using NamedGraphs
using ITensors
using ITensorNetworks
using ITensorUnicodePlots

tn = ITensorNetwork(named_grid((3, 5)); link_space=4)

@visualize tn

terminal_vertices = [(1, 2), (1, 4), (3, 4)]
st = steiner_tree(tn, terminal_vertices)

@show has_edge(st, (1, 2) => (1, 3))
@show has_edge(st, (1, 3) => (1, 4))
@show has_edge(st, (1, 4) => (2, 4))
@show has_edge(st, (2, 4) => (3, 4))
4 changes: 3 additions & 1 deletion examples/ttns/ttn_type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ for e in post_order_dfs_edges(ψ_ortho, root_vertex)
end

@show (
contract(norm_sqr_network(ψ_ortho); sequence=contraction_sequence(norm_sqr_network(ψ_ortho)))[]
contract(
norm_sqr_network(ψ_ortho); sequence=contraction_sequence(norm_sqr_network(ψ_ortho))
)[],
)
@show (contract(norm_sqr_network(ψ); sequence=contraction_sequence(norm_sqr_network(ψ)))[])
@show norm(ψ_ortho[root_vertex])
Expand Down
14 changes: 12 additions & 2 deletions src/ITensorNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ using Suppressor
# TODO: export from ITensors
using ITensors: commontags, @Algorithm_str, Algorithm
using Graphs: AbstractEdge, AbstractGraph, Graph, add_edge!
using Graphs.SimpleGraphs # AbstractSimpleGraph
using NamedGraphs:
AbstractNamedGraph,
parent_graph,
vertex_to_parent_vertex,
parent_vertices_to_vertices,
not_implemented
using DataGraphs: vertex_data_type
using DataGraphs: edge_data_type, vertex_data_type

include("imports.jl")

# TODO: Move to `DataGraphs.jl`
edge_data_type(::AbstractNamedGraph) = Any
isassigned(::AbstractNamedGraph, ::Any) = false
function iterate(::AbstractDataGraph)
return error(
"Iterating data graphs is not yet defined. We may define it in the future as iterating through the vertex and edge data.",
)
end

include("utils.jl")
include("visualize.jl")
include("graphs.jl")
Expand All @@ -47,7 +58,6 @@ include("itensornetwork.jl")
include("specialitensornetworks.jl")
include("renameitensornetwork.jl")
include("boundarymps.jl")
include("subgraphs.jl")
include("beliefpropagation.jl")
include(joinpath("treetensornetwork", "treetensornetwork.jl"))

Expand Down
19 changes: 7 additions & 12 deletions src/abstractindsnetwork.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
abstract type AbstractIndsNetwork{V,I} <:
AbstractDataGraph{V,Vector{I},Vector{I}} end
abstract type AbstractIndsNetwork{V,I} <: AbstractDataGraph{V,Vector{I},Vector{I}} end

# Field access
data_graph(graph::AbstractIndsNetwork) = not_implemented()
Expand All @@ -11,6 +10,9 @@ is_directed(::Type{<:AbstractIndsNetwork}) = false
vertex_data(graph::AbstractIndsNetwork, args...) = vertex_data(data_graph(graph), args...)
edge_data(graph::AbstractIndsNetwork, args...) = edge_data(data_graph(graph), args...)

# TODO: Define a generic fallback for `AbstractDataGraph`?
edge_data_type(::Type{<:AbstractIndsNetwork{V,I}}) where {V,I} = Vector{I}

#
# Index access
#
Expand All @@ -27,18 +29,11 @@ function uniqueinds(is::AbstractIndsNetwork, edge::Pair)
return uniqueinds(is, edgetype(is)(edge))
end

function union(
tn1::AbstractIndsNetwork,
tn2::AbstractIndsNetwork;
kwargs...,
)
return IndsNetwork(union(data_graph(tn1), data_graph(tn2)); kwargs...)
function union(tn1::AbstractIndsNetwork, tn2::AbstractIndsNetwork; kwargs...)
return IndsNetwork(union(data_graph(tn1), data_graph(tn2); kwargs...))
end

function rename_vertices(
f::Function,
tn::AbstractIndsNetwork,
)
function rename_vertices(f::Function, tn::AbstractIndsNetwork)
return IndsNetwork(rename_vertices(f, data_graph(tn)))
end

Expand Down
Loading

0 comments on commit 652e59e

Please sign in to comment.