Skip to content

Update for most recent versions of NamedGraphs and DataGraphs #22

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 15 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
shell: julia --project=dev {0}
run: |
using Pkg;
pkg"dev https://github.com/mtfishman/DataGraphs.jl.git ."
pkg"dev ."
- name: Run the tests
shell: julia --project=dev {0}
run: |
Expand Down
21 changes: 13 additions & 8 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
name = "ITensorNetworks"
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7"
authors = ["Matthew Fishman <mfishman@flatironinstitute.org> and contributors"]
version = "0.1.0"
version = "0.2.0"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
DataGraphs = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MultiDimDictionaries = "87ff4268-a46e-478f-b30a-76b83dd64e3c"
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"

[compat]
Compat = "3, 4"
DataGraphs = "0.1.1"
Dictionaries = "0.3.15"
Graphs = "1.6.0"
DocStringExtensions = "0.8, 0.9"
Graphs = "1.6"
ITensors = "0.3"
MultiDimDictionaries = "0.0.1"
NamedGraphs = "0.0.1"
Requires = "1.3.0"
Suppressor = "0.2.1"
julia = "1.6"
NamedGraphs = "0.1.3"
Requires = "1.3"
SimpleTraits = "0.9"
SplitApplyCombine = "1.2"
Suppressor = "0.2"
julia = "1.7"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
244 changes: 119 additions & 125 deletions README.md

Large diffs are not rendered by default.

41 changes: 19 additions & 22 deletions examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@

#' ## Installation
#'
#' This package relies on a few unregistered packages. To install, you will need to do:
#'
#' You can install this package through the Julia package manager:
#' ```julia
#' julia> using Pkg
#'
#' julia> Pkg.add(url="https://github.com/mtfishman/MultiDimDictionaries.jl")
#'
#' julia> Pkg.add(url="https://github.com/mtfishman/NamedGraphs.jl")
#'
#' julia> Pkg.add(url="https://github.com/mtfishman/DataGraphs.jl")
#'
#' julia> Pkg.add(url="https://github.com/mtfishman/ITensorNetworks.jl")
#' julia> ] add ITensorNetworks
#' ```

#+ echo=false; term=false

using Random
using ITensors
Random.seed!(ITensors.index_id_rng(), 1234);

#' ## Examples
#'
#' Here are is an example of making a tensor network on a chain graph (a tensor train or matrix product state):
#+ term=true

using ITensors
using ITensorNetworks
tn = ITensorNetwork(named_grid((4,)); link_space=2)
tn = ITensorNetwork(named_grid(4); link_space=2)
tn[1]
tn[2]
neighbors(tn, 1)
Expand All @@ -38,16 +35,16 @@ neighbors(tn, 4)

tn = ITensorNetwork(named_grid((2, 2)); link_space=2)
tn[1, 1]
neighbors(tn, 1, 1)
neighbors(tn, 1, 2)
tn_1 = tn[1, :]
tn_2 = tn[2, :]
neighbors(tn, (1, 1))
neighbors(tn, (1, 2))
tn_1 = subgraph(v -> v[1] == 1, tn)
tn_2 = subgraph(v -> v[1] == 2, tn)

#' Networks can also be merged/unioned:
#+ term=true

using ITensorUnicodePlots
s = siteinds("S=1/2", named_grid((3,)))
s = siteinds("S=1/2", named_grid(3))
tn1 = ITensorNetwork(s; link_space=2)
tn2 = ITensorNetwork(s; link_space=2)
@visualize tn1;
Expand All @@ -59,9 +56,9 @@ Z̃ = contract(Z, (1, 1) => (2, 1));
@visualize Z̃;

#' ## Generating this README
#'

#' This file was generated with [weave.jl](https://github.com/JunoLab/Weave.jl) with the following commands:
#' ```julia
#' using ITensorNetworks, Weave
#' weave(joinpath(pkgdir(ITensorNetworks), "examples", "README.jl"); doctype="github", out_path=pkgdir(ITensorNetworks))
#' ```
#+ eval=false

using ITensorNetworks, Weave
weave(joinpath(pkgdir(ITensorNetworks), "examples", "README.jl"); doctype="github", out_path=pkgdir(ITensorNetworks))
21 changes: 0 additions & 21 deletions examples/TTN/ttn_basics.jl

This file was deleted.

65 changes: 33 additions & 32 deletions examples/belief_propagation/bpexample.jl
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
using Compat
using ITensors
using Metis
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
get_single_site_expec

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

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

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

#Apply Sz to site v and flatten that
psiflatO = flatten_thicken_bonds(psi; ops=["Sz"], vops=[v], s=s, combiners=combiners)
# Apply Sz to site v
v = one.(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
actual_sz = ITensors.contract(psiflatO)[1] / ITensors.contract(psiflat)[1]
# 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))

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
niters = 20

iterate_single_site_expec(psiflat, psiflatO, mts, dg_subgraphs, niters, v)
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("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)
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)
5 changes: 3 additions & 2 deletions examples/contraction_sequence/contraction_sequence.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NamedGraphs
using ITensors
using ITensorNetworks
using Random
Expand All @@ -13,7 +14,7 @@ s = siteinds("S=1/2", g)
χ = 10
ψ = randomITensorNetwork(s; link_space=χ)

tn = norm_network(ψ)
tn = norm_sqr_network(ψ)

# Contraction sequence for exactly computing expectation values
# contract_edges = map(t -> (1, t...), collect(keys(cartesian_to_linear(dims))))
Expand All @@ -26,7 +27,7 @@ using OMEinsumContractionOrders

println("greedy")
seq_greedy = @time contraction_sequence(tn; alg="greedy")
res_greedy = @time contract(tn; alg=res_greedy)
res_greedy = @time contract(tn; sequence=seq_greedy)

println("tree_sa")
seq_tree_sa = @time contraction_sequence(tn; alg="tree_sa")
Expand Down
4 changes: 2 additions & 2 deletions examples/examples.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using ITensors
using ITensorNetworks
using ITensorUnicodePlots
using NamedGraphs

χ, d = 5, 2
dims = (4, 4)
vertices = vec([(i, j) for i in 1:dims[1], j in 1:dims[2]])
g = NamedDimGraph(grid(dims), vertices)
g = named_grid(dims)

# Network of indices
is = IndsNetwork(g; link_space=χ, site_space=d)
Expand Down
4 changes: 2 additions & 2 deletions examples/mps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ e = edge_data(is)

@visualize ψ̃

ψψ = ⊗(ψ̃, ψ; new_dim_names=("bra", "ket"))
ψψ = ("bra" => ψ̃) ⊗ ("ket" => ψ)

@visualize ψψ

Expand All @@ -38,6 +38,6 @@ inner_res = contract(ψψ; sequence)[]

@show inner_res

sub = ψψ[[("bra", 1), ("ket", 1), ("bra", 2), ("ket", 2)]]
sub = subgraph(ψψ, [(1, "bra"), (1, "ket"), (2, "bra"), (2, "ket")])

@visualize sub
8 changes: 4 additions & 4 deletions examples/partition/kahypar_vs_metis.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Graphs
using ITensorNetworks
using KaHyPar
using Metis
using ITensorNetworks

g = grid((16,))
npartitions = 2
npartitions = 4

kahypar_partitions = partition(g, npartitions; backend="KaHyPar")
metis_partitions = partition(g, npartitions; backend="Metis")
@show kahypar_partitions
@show metis_partitions
@show kahypar_partitions, length(unique(kahypar_partitions))
@show metis_partitions, length(unique(metis_partitions))
5 changes: 2 additions & 3 deletions examples/peps/ising_tebd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Random.seed!(1234)

ITensors.disable_warn_order()

dims = (8, 8)
dims = (6, 6)
n = prod(dims)
g = named_grid(dims)

Expand Down Expand Up @@ -59,8 +59,7 @@ println("maxdim = $χ")
@show ortho

# Contraction sequence for exactly computing expectation values
contract_edges = map(t -> (1, t...), collect(keys(cartesian_to_linear(dims))))
inner_sequence = reduce((x, y) -> [x, y], contract_edges)
inner_sequence = reduce((x, y) -> [x, y], vec(Tuple.(CartesianIndices(dims))))

println("\nFirst run TEBD without orthogonalization")
ψ = @time tebd(
Expand Down
1 change: 1 addition & 0 deletions examples/TTN/comb_tree.jl → examples/ttns/comb_tree.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NamedGraphs
using ITensors
using ITensorNetworks
using ITensorUnicodePlots
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using NamedGraphs
using ITensors
using ITensorNetworks
using ITensorUnicodePlots

s = siteinds("S=1/2", named_grid((4, 4)))
ψ = ITensorNetwork(s; link_space=3)

@visualize ψ

# Gives a snake pattern
t_dfs = dfs_tree(g, 1, 1)
t_dfs = dfs_tree(ψ, (1, 1))

@visualize t_dfs

# Gives a comb pattern
t_bfs = bfs_tree(g, 1, 1)
t_bfs = bfs_tree(ψ, (1, 1))

@visualize t_bfs

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

g = named_binary_tree(3)

@show g
filter_vertices(v, v1, v2) = length(v) ≥ 2 && v[1] == v1 && v[2] == v2
@show subgraph(v -> filter_vertices(v, 1, 1), g)
@show subgraph(v -> filter_vertices(v, 1, 2), g)
@visualize g

s = siteinds("S=1/2", g)
ψ = ITensorNetwork(s; link_space=3)

@visualize ψ

bfs_tree_ψ = bfs_tree(ψ, (1, 2))
dfs_tree_ψ = dfs_tree(ψ, (1, 2))

nothing
Loading