Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 committed Nov 22, 2024
2 parents 6ff0cd5 + a8c3a0b commit 34e8e5e
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 208 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensorNetworks"
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7"
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"]
version = "0.11.21"
version = "0.11.23"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down Expand Up @@ -62,8 +62,8 @@ DocStringExtensions = "0.9"
EinExprs = "0.6.4"
Graphs = "1.8"
GraphsFlows = "0.1.1"
ITensorMPS = "0.2.2"
ITensors = "0.6.8"
ITensorMPS = "0.3"
ITensors = "0.7"
IsApprox = "0.1, 1, 2"
IterTools = "1.4.0"
KrylovKit = "0.6, 0.7, 0.8"
Expand Down
144 changes: 72 additions & 72 deletions README.md

Large diffs are not rendered by default.

55 changes: 34 additions & 21 deletions src/abstractitensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Graphs:
add_edge!,
add_vertex!,
bfs_tree,
center,
dst,
edges,
edgetype,
Expand Down Expand Up @@ -40,7 +41,7 @@ using ITensorMPS: ITensorMPS, add, linkdim, linkinds, siteinds
using .ITensorsExtensions: ITensorsExtensions, indtype, promote_indtype
using LinearAlgebra: LinearAlgebra, factorize
using MacroTools: @capture
using NamedGraphs: NamedGraphs, NamedGraph, not_implemented
using NamedGraphs: NamedGraphs, NamedGraph, not_implemented, steiner_tree
using NamedGraphs.GraphsExtensions:
, directed_graph, incident_edges, rename_vertices, vertextype
using NDTensors: NDTensors, dim
Expand Down Expand Up @@ -584,37 +585,49 @@ function LinearAlgebra.factorize(tn::AbstractITensorNetwork, edge::Pair; 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...)
function orthogonalize_walk(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
return orthogonalize_walk(tn, [edge]; kwargs...)
end

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

# For ambiguity error; TODO: decide whether to use graph mutating methods when resulting graph is unchanged?
function orthogonalize_walk(
tn::AbstractITensorNetwork, edges::Vector{<: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...)
tn[src(edge)] = X
tn[dst(edge)] *= Y
for edge in edges
left_inds = uniqueinds(tn, edge)
ltags = tags(tn, edge)
X, Y = factorize(tn[src(edge)], left_inds; tags=ltags, ortho="left", kwargs...)
tn[src(edge)] = X
tn[dst(edge)] *= Y
end
return tn
end

function ITensorMPS.orthogonalize(tn::AbstractITensorNetwork, edge::AbstractEdge; kwargs...)
return _orthogonalize_edge(tn, edge; kwargs...)
function orthogonalize_walk(tn::AbstractITensorNetwork, edges::Vector{<:Pair}; kwargs...)
return orthogonalize_walk(tn, edgetype(tn).(edges); kwargs...)
end

function ITensorMPS.orthogonalize(tn::AbstractITensorNetwork, edge::Pair; kwargs...)
return orthogonalize(tn, edgetype(tn)(edge); kwargs...)
# Orthogonalize an ITensorNetwork towards a region, treating
# the network as a tree spanned by a spanning tree.
function tree_orthogonalize::AbstractITensorNetwork, region::Vector)
region_center =
length(region) != 1 ? first(center(steiner_tree(ψ, region))) : only(region)
path = post_order_dfs_edges(bfs_tree(ψ, region_center), region_center)
path = filter(e -> !((src(e) region) && (dst(e) region)), path)
return orthogonalize_walk(ψ, path)
end

# Orthogonalize an ITensorNetwork towards a source vertex, treating
# the network as a tree spanned by a spanning tree.
# TODO: Rename `tree_orthogonalize`.
function ITensorMPS.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)
end
return ψ
function tree_orthogonalize::AbstractITensorNetwork, region)
return tree_orthogonalize(ψ, [region])
end

# TODO: decide whether to use graph mutating methods when resulting graph is unchanged?
Expand Down Expand Up @@ -759,7 +772,7 @@ end
# Link dimensions
#

function ITensors.maxlinkdim(tn::AbstractITensorNetwork)
function ITensorMPS.maxlinkdim(tn::AbstractITensorNetwork)
md = 1
for e in edges(tn)
md = max(md, linkdim(tn, e))
Expand Down
4 changes: 2 additions & 2 deletions src/apply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function ITensors.apply(
v⃗ = neighbor_vertices(ψ, o)
if length(v⃗) == 1
if ortho
ψ = orthogonalize(ψ, v⃗[1])
ψ = tree_orthogonalize(ψ, v⃗[1])
end
oψᵥ = apply(o, ψ[v⃗[1]])
if normalize
Expand All @@ -215,7 +215,7 @@ function ITensors.apply(
error("Vertices where the gates are being applied must be neighbors for now.")
end
if ortho
ψ = orthogonalize(ψ, v⃗[1])
ψ = tree_orthogonalize(ψ, v⃗[1])
end
if variational_optimization_only || !is_product_env
ψᵥ₁, ψᵥ₂ = full_update_bp(
Expand Down
15 changes: 8 additions & 7 deletions src/inner.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ITensors: inner, scalar, loginner
using ITensors: inner, scalar
using ITensorMPS: ITensorMPS, loginner
using LinearAlgebra: norm, norm_sqr

default_contract_alg(tns::Tuple) = "bp"
Expand Down Expand Up @@ -53,7 +54,7 @@ function ITensors.inner(
return scalar(tn; sequence)
end

function ITensors.loginner(
function ITensorMPS.loginner(
ϕ::AbstractITensorNetwork,
ψ::AbstractITensorNetwork;
alg=default_contract_alg((ϕ, ψ)),
Expand All @@ -62,7 +63,7 @@ function ITensors.loginner(
return loginner(Algorithm(alg), ϕ, ψ; kwargs...)
end

function ITensors.loginner(
function ITensorMPS.loginner(
ϕ::AbstractITensorNetwork,
A::AbstractITensorNetwork,
ψ::AbstractITensorNetwork;
Expand All @@ -72,13 +73,13 @@ function ITensors.loginner(
return loginner(Algorithm(alg), ϕ, A, ψ; kwargs...)
end

function ITensors.loginner(
function ITensorMPS.loginner(
alg::Algorithm"exact", ϕ::AbstractITensorNetwork, ψ::AbstractITensorNetwork; kwargs...
)
return log(inner(alg, ϕ, ψ); kwargs...)
end

function ITensors.loginner(
function ITensorMPS.loginner(
alg::Algorithm"exact",
ϕ::AbstractITensorNetwork,
A::AbstractITensorNetwork,
Expand All @@ -88,7 +89,7 @@ function ITensors.loginner(
return log(inner(alg, ϕ, A, ψ); kwargs...)
end

function ITensors.loginner(
function ITensorMPS.loginner(
alg::Algorithm"bp",
ϕ::AbstractITensorNetwork,
ψ::AbstractITensorNetwork;
Expand All @@ -99,7 +100,7 @@ function ITensors.loginner(
return logscalar(alg, tn; kwargs...)
end

function ITensors.loginner(
function ITensorMPS.loginner(
alg::Algorithm"bp",
ϕ::AbstractITensorNetwork,
A::AbstractITensorNetwork,
Expand Down
40 changes: 3 additions & 37 deletions src/solvers/alternating_update/region_update.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
#ToDo: generalize beyond 2-site
#ToDo: remove concept of orthogonality center for generality
function current_ortho(sweep_plan, which_region_update)
regions = first.(sweep_plan)
region = regions[which_region_update]
current_verts = support(region)
if !isa(region, AbstractEdge) && length(region) == 1
return only(current_verts)
end
# look forward
other_regions = filter(
x -> !(issetequal(x, current_verts)), support.(regions[(which_region_update + 1):end])
)
# find the first region that has overlapping support with current region
ind = findfirst(x -> !isempty(intersect(support(x), support(region))), other_regions)
if isnothing(ind)
# look backward
other_regions = reverse(
filter(
x -> !(issetequal(x, current_verts)), support.(regions[1:(which_region_update - 1)])
),
)
ind = findfirst(x -> !isempty(intersect(support(x), support(region))), other_regions)
end
@assert !isnothing(ind)
future_verts = union(support(other_regions[ind]))
# return ortho_ceter as the vertex in current region that does not overlap with following one
overlapping_vertex = intersect(current_verts, future_verts)
nonoverlapping_vertex = only(setdiff(current_verts, overlapping_vertex))
return nonoverlapping_vertex
end

function region_update(
projected_operator,
state;
Expand All @@ -55,14 +23,13 @@ function region_update(

# ToDo: remove orthogonality center on vertex for generality
# region carries same information
ortho_vertex = current_ortho(sweep_plan, which_region_update)
if !isnothing(transform_operator)
projected_operator = transform_operator(
state, projected_operator; outputlevel, transform_operator_kwargs...
)
end
state, projected_operator, phi = extracter(
state, projected_operator, region, ortho_vertex; extracter_kwargs..., internal_kwargs
state, projected_operator, region; extracter_kwargs..., internal_kwargs
)
# create references, in case solver does (out-of-place) modify PH or state
state! = Ref(state)
Expand All @@ -88,9 +55,8 @@ function region_update(
# drho = noise * noiseterm(PH, phi, ortho) # TODO: actually implement this for trees...
# so noiseterm is a solver
#end
state, spec = inserter(
state, phi, region, ortho_vertex; inserter_kwargs..., internal_kwargs
)
#if isa(region, AbstractEdge) &&
state, spec = inserter(state, phi, region; inserter_kwargs..., internal_kwargs)
all_kwargs = (;
which_region_update,
sweep_plan,
Expand Down
3 changes: 2 additions & 1 deletion src/solvers/contract.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Graphs: nv, vertices
using ITensors: ITensors, linkinds, sim
using ITensors: ITensors, sim
using ITensorMPS: linkinds
using ITensors.NDTensors: Algorithm, @Algorithm_str, contract
using NamedGraphs: vertextype

Expand Down
16 changes: 9 additions & 7 deletions src/solvers/extract/extract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
# insert_local_tensors takes that tensor and factorizes it back
# apart and puts it back into the network.
#
function default_extracter(state, projected_operator, region, ortho; internal_kwargs)
state = orthogonalize(state, ortho)

function default_extracter(state, projected_operator, region; internal_kwargs)
if isa(region, AbstractEdge)
other_vertex = only(setdiff(support(region), [ortho]))
left_inds = uniqueinds(state[ortho], state[other_vertex])
#ToDo: replace with call to factorize
# TODO: add functionality for orthogonalizing onto a bond so that can be called instead
vsrc, vdst = src(region), dst(region)
state = orthogonalize(state, vsrc)
left_inds = uniqueinds(state[vsrc], state[vdst])
U, S, V = svd(
state[ortho], left_inds; lefttags=tags(state, region), righttags=tags(state, region)
state[vsrc], left_inds; lefttags=tags(state, region), righttags=tags(state, region)
)
state[ortho] = U
state[vsrc] = U
local_tensor = S * V
else
state = orthogonalize(state, region)
local_tensor = prod(state[v] for v in region)
end
projected_operator = position(projected_operator, state, region)
Expand Down
25 changes: 10 additions & 15 deletions src/solvers/insert/insert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
function default_inserter(
state::AbstractTTN,
phi::ITensor,
region,
ortho_vert;
region;
normalize=false,
maxdim=nothing,
mindim=nothing,
Expand All @@ -16,16 +15,14 @@ function default_inserter(
)
state = copy(state)
spec = nothing
other_vertex = setdiff(support(region), [ortho_vert])
if !isempty(other_vertex)
v = only(other_vertex)
e = edgetype(state)(ortho_vert, v)
indsTe = inds(state[ortho_vert])
if length(region) == 2
v = last(region)
e = edgetype(state)(first(region), last(region))
indsTe = inds(state[first(region)])
L, phi, spec = factorize(phi, indsTe; tags=tags(state, e), maxdim, mindim, cutoff)
state[ortho_vert] = L

state[first(region)] = L
else
v = ortho_vert
v = only(region)
end
state[v] = phi
state = set_ortho_region(state, [v])
Expand All @@ -36,16 +33,14 @@ end
function default_inserter(
state::AbstractTTN,
phi::ITensor,
region::NamedEdge,
ortho;
region::NamedEdge;
cutoff=nothing,
maxdim=nothing,
mindim=nothing,
normalize=false,
internal_kwargs,
)
v = only(setdiff(support(region), [ortho]))
state[v] *= phi
state = set_ortho_region(state, [v])
state[dst(region)] *= phi
state = set_ortho_region(state, [dst(region)])
return state, nothing
end
Loading

0 comments on commit 34e8e5e

Please sign in to comment.