Skip to content

Additions needed for sweeping algorithms on trees #18

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 20 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c81a211
Sweeping algorithms for tree tensor networks: draft
Oct 31, 2022
b3ca106
Switch off numerical truncation by default in OpSum->TTNO converter
leburgel Nov 1, 2022
b389589
Merge branch 'indsnetwork_additions' into tree_sweeping
leburgel Nov 3, 2022
e00c345
Remove functionalities that were merged into NamedGraphs.jl
leburgel Nov 3, 2022
74dc679
Merge branch 'indsnetwork_additions' into tree_sweeping
leburgel Nov 3, 2022
f883124
Merge remote-tracking branch 'origin' into tree_sweeping
leburgel Nov 4, 2022
0eb9ef9
Remove temporary `expect` in favor or `expect.jl` merged from main
leburgel Nov 4, 2022
d337f7c
Use `convert_eltype` instead of `convert_leaf_eltype` where appropria…
leburgel Nov 28, 2022
77be11c
Change `linkdims` to return `NamedDimDataGraph`
leburgel Nov 28, 2022
d6b9633
Update
leburgel Dec 12, 2022
26ec817
Merge branch 'main' into tree_sweeping
leburgel Dec 12, 2022
40a9975
Move implementation to `loginner`, make `logdot` the alias.
leburgel Dec 21, 2022
a04d3cf
No more mutables; remove many in-place operations.
leburgel Dec 22, 2022
152db53
Remove in-place operations.
leburgel Jan 9, 2023
d490aed
Merge remote-tracking branch 'origin' into tree_sweeping
leburgel Jan 9, 2023
e9f0c7a
Remove unnecessary comment.
leburgel Jan 9, 2023
914b711
Merge branch 'main' into tree_sweeping
mtfishman Jan 9, 2023
6cb3e36
Fix imports.jl
mtfishman Jan 9, 2023
591cd3c
Update directory name from treetensornetwork to treetensornetworks
mtfishman Jan 9, 2023
105cec6
Fix some warnings
mtfishman Jan 9, 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
Prev Previous commit
Next Next commit
Remove in-place operations.
  • Loading branch information
leburgel committed Jan 9, 2023
commit 152db5368946d6edad5838f9b3de169611510c86
1 change: 1 addition & 0 deletions src/ITensorNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ include(joinpath("treetensornetwork", "opsum_to_ttno.jl"))
include(joinpath("treetensornetwork", "abstractprojttno.jl"))
include(joinpath("treetensornetwork", "projttno.jl"))
include(joinpath("treetensornetwork", "projttnosum.jl"))
include(joinpath("treetensornetwork", "projttno_apply.jl"))
include("utility.jl")
include("specialitensornetworks.jl")
include("renameitensornetwork.jl")
Expand Down
66 changes: 25 additions & 41 deletions src/abstractitensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,11 @@ function tags(tn::AbstractITensorNetwork, edge)
return commontags(is)
end

function svd!(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return svd!(tn, edgetype(tn)(edge))
function svd(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return svd(tn, edgetype(tn)(edge))
end

function svd!(
function svd(
tn::AbstractITensorNetwork,
edge::AbstractEdge;
U_vertex=src(edge),
Expand All @@ -356,10 +356,11 @@ function svd!(
v_tags=tags(tn, edge),
kwargs...,
)
tn = copy(tn)
left_inds = uniqueinds(tn, edge)
U, S, V = svd(tn[src(edge)], left_inds; lefttags=u_tags, right_tags=v_tags, kwargs...)

rem_vertex!(tn, src(edge)) # TODO: avoid this if we can?
rem_vertex!(tn, src(edge))
add_vertex!(tn, U_vertex)
tn[U_vertex] = U

Expand All @@ -372,22 +373,19 @@ function svd!(
return tn
end

function svd(tn::AbstractITensorNetwork, edge; kwargs...)
return svd!(copy(tn), edge; kwargs...)
end

function qr!(
function qr(
tn::AbstractITensorNetwork,
edge::AbstractEdge;
Q_vertex=src(edge),
R_vertex=(edge, "R"),
tags=tags(tn, edge),
kwargs...,
)
tn = copy(tn)
left_inds = uniqueinds(tn, edge)
Q, R = factorize(tn[src(edge)], left_inds; tags, kwargs...)

rem_vertex!(tn, src(edge)) # TODO: avoid this if we can?
rem_vertex!(tn, src(edge))
add_vertex!(tn, Q_vertex)
tn[Q_vertex] = Q

Expand All @@ -397,11 +395,7 @@ function qr!(
return tn
end

function qr(tn::AbstractITensorNetwork, edge; kwargs...)
return qr!(copy(tn), edge; kwargs...)
end

function factorize!(
function factorize(
tn::AbstractITensorNetwork,
edge::AbstractEdge;
X_vertex=src(edge),
Expand All @@ -417,7 +411,7 @@ function factorize!(
left_inds = uniqueinds(tn, edge)
X, Y = factorize(tn[src(edge)], left_inds; tags, kwargs...)

rem_vertex!(tn, src(edge)) # TODO: avoid this if we can?
rem_vertex!(tn, src(edge))
add_vertex!(tn, X_vertex)
add_vertex!(tn, Y_vertex)

Expand All @@ -436,16 +430,13 @@ function factorize!(
return tn
end

function factorize(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
return factorize!(copy(tn), edge; kwargs...)
end

# For ambiguity error; TODO: decide whether to use graph mutating methods when resulting graph is unchanged?
function _orthogonalize_edge!(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
# factorize!(tn, edge; kwargs...)
function _orthogonalize_edge(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
# tn = factorize(tn, edge; kwargs...)
# # TODO: Implement as `only(common_neighbors(tn, src(edge), dst(edge)))`
# new_vertex = only(neighbors(tn, src(edge)) ∩ neighbors(tn, dst(edge)))
# return contract(tn, new_vertex => dst(edge))
tn = copy(tn)
left_inds = uniqueinds(tn, edge)
ltags = tags(tn, edge)
X, Y = factorize(tn[src(edge)], left_inds; tags=ltags, ortho="left", kwargs...)
Expand All @@ -454,31 +445,28 @@ function _orthogonalize_edge!(tn::AbstractITensorNetwork, edge::AbstractEdge; kw
return tn
end

function orthogonalize!(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
return _orthogonalize_edge!(tn, edge; kwargs...)
function orthogonalize(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
return _orthogonalize_edge(tn, edge; kwargs...)
end

function orthogonalize!(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return orthogonalize!(tn, edgetype(tn)(edge); kwargs...)
function orthogonalize(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return orthogonalize(tn, edgetype(tn)(edge); kwargs...)
end

# Orthogonalize an ITensorNetwork towards a source vertex, treating
# the network as a tree spanned by a spanning tree.
# TODO: Rename `tree_orthogonalize`.
function orthogonalize!(ψ::AbstractITensorNetwork, source_vertex)
function orthogonalize(ψ::AbstractITensorNetwork, source_vertex)
spanning_tree_edges = post_order_dfs_edges(bfs_tree(ψ, source_vertex), source_vertex)
for e in spanning_tree_edges
orthogonalize!(ψ, e)
ψ = orthogonalize(ψ, e)
end
return ψ
end

function orthogonalize(tn::AbstractITensorNetwork, args...; kwargs...)
return orthogonalize!(copy(tn), args...; kwargs...)
end

# TODO: decide whether to use graph mutating methods when resulting graph is unchanged?
function _truncate_edge!(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
function _truncate_edge(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
tn = copy(tn)
left_inds = uniqueinds(tn, edge)
ltags = tags(tn, edge)
U, S, V = svd(tn[src(edge)], left_inds; lefttags=ltags, ortho="left", kwargs...)
Expand All @@ -487,16 +475,12 @@ function _truncate_edge!(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs.
return tn
end

function truncate!(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
return _truncate_edge!(tn, edge; kwargs...)
end

function truncate!(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return truncate!(tn, edgetype(tn)(edge); kwargs...)
function truncate(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
return _truncate_edge(tn, edge; kwargs...)
end

function truncate(tn::AbstractITensorNetwork, edge; kwargs...)
return truncate!(copy(tn), edge; kwargs...)
function truncate(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return truncate(tn, edgetype(tn)(edge); kwargs...)
end

function Base.:*(c::Number, ψ::AbstractITensorNetwork)
Expand Down
1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export AbstractITensorNetwork,
TTNO,
ProjTTNO,
ProjTTNOSum,
ProjTTNOApply,
set_nsite,
position,
finite_state_machine,
Expand Down
2 changes: 0 additions & 2 deletions src/imports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ import ITensors:
# contraction
contract,
orthogonalize,
orthogonalize!,
isortho,
inner,
loginner,
norm,
lognorm,
expect,
# truncation
truncate!,
truncate,
replacebond!,
replacebond,
Expand Down
2 changes: 1 addition & 1 deletion src/treetensornetwork/abstractprojttno.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ end
function position(
P::AbstractProjTTNO{V}, psi::TTNS{V}, pos::Union{Vector{<:V},NamedEdge{V}}
) where {V}
# shift position; TODO: update for immutable struct
# shift position
P = shift_position(P, pos)
# invalidate environments corresponding to internal edges
for e in internal_edges(P)
Expand Down
55 changes: 55 additions & 0 deletions src/treetensornetwork/projttno_apply.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
struct ProjTTNOApply{V} <: AbstractProjTTNO{V}
pos::Union{Vector{<:V},NamedEdge{V}}
psi0::TTNS{V}
H::TTNO{V}
environments::Dictionary{NamedEdge{V},ITensor}
end

function ProjTTNOApply(psi0::TTNS, H::TTNO)
return ProjTTNOApply(vertextype(H)[], psi0, H, Dictionary{edgetype(H),ITensor}())
end

function copy(P::ProjTTNOApply)
return ProjTTNOApply(P.pos, copy(P.psi0), copy(P.H), copy(P.environments))
end

function set_nsite(P::ProjTTNOApply, nsite)
return P
end

function shift_position(P::ProjTTNOApply, pos)
return ProjTTNOApply(pos, P.psi0, P.H, P.environments)
end

function make_environment!(P::ProjTTNOApply{V}, psi::TTNS{V}, e::NamedEdge{V})::ITensor where {V}
# invalidate environment for opposite edge direction if necessary
reverse(e) ∈ incident_edges(P) || unset!(P.environments, reverse(e))
# do nothing if valid environment already present
if haskey(P.environments, e)
env = environment(P, e)
else
if is_leaf(underlying_graph(P), src(e))
# leaves are easy
env = P.psi0[src(e)] * P.H[src(e)] * dag(psi[src(e)])
else
# construct by contracting neighbors
neighbor_envs = ITensor[]
for n in setdiff(neighbors(underlying_graph(P), src(e)), [dst(e)])
push!(neighbor_envs, make_environment!(P, psi, edgetype(P)(n, src(e))))
end
# manually heuristic for contraction order: two environments, site tensors, then
# other environments
frst, scnd, rst = _separate_first_two(neighbor_envs)
itensor_map = vcat(P.psi0[src(e)], frst, scnd, P.H[src(e)], dag(psi[src(e)]), rst)
# TODO: actually use optimal contraction sequence here
env = reduce(*, itensor_map)
end
# cache
set!(P.environments, e, env)
end
@assert(
hascommoninds(environment(P, e), psi[src(e)]),
"Something went wrong, probably re-orthogonalized this edge in the same direction twice!"
)
return env
end
1 change: 0 additions & 1 deletion test/test_ttns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ using Random
# dense array constructor from Vector{Index} and NamedDimGraph
@disable_warn_order s4 = TTNS(AS, sites_s, c; vertex_order, cutoff)
# see if this actually worked
@show ortho_center(s1)
root_vertex = only(ortho_center(s1))
@disable_warn_order begin
S1 = contract(s1, root_vertex)
Expand Down