Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into subspace-exp-new-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
b-kloss committed Jul 28, 2024
2 parents 87e1ed9 + 806d897 commit 833ed21
Show file tree
Hide file tree
Showing 61 changed files with 985 additions and 709 deletions.
22 changes: 15 additions & 7 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.10.4"
version = "0.11.15"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand All @@ -13,11 +13,13 @@ Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
ITensorMPS = "0d1a4710-d33b-49a5-8f18-73bdf49b47e2"
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
IsApprox = "28f27b66-4bd8-47e7-9110-e2746eb8bed7"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
Expand All @@ -34,19 +36,22 @@ TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"

[weakdeps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"
OMEinsumContractionOrders = "6f22d1fd-8eed-4bb7-9776-e7d684900715"
Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"

[extensions]
ITensorNetworksAdaptExt = "Adapt"
ITensorNetworksEinExprsExt = "EinExprs"
ITensorNetworksGraphsFlowsExt = "GraphsFlows"
ITensorNetworksObserversExt = "Observers"
ITensorNetworksOMEinsumContractionOrdersExt = "OMEinsumContractionOrders"
ITensorNetworksObserversExt = "Observers"

[compat]
AbstractTrees = "0.4.4"
Adapt = "4"
Combinatorics = "1"
Compat = "3, 4"
DataGraphs = "0.2.3"
Expand All @@ -57,14 +62,16 @@ DocStringExtensions = "0.9"
EinExprs = "0.6.4"
Graphs = "1.8"
GraphsFlows = "0.1.1"
ITensors = "0.4, 0.5"
ITensorMPS = "0.2.2"
ITensors = "0.6.8"
IsApprox = "0.1"
IterTools = "1.4.0"
KrylovKit = "0.6, 0.7"
NamedGraphs = "0.6.0"
MacroTools = "0.5"
NDTensors = "0.3"
Observers = "0.2"
NamedGraphs = "0.6.0"
OMEinsumContractionOrders = "0.8.3"
Observers = "0.2.4"
PackageExtensionCompat = "1"
SerializedElementArrays = "0.1"
SimpleTraits = "0.9"
Expand All @@ -78,10 +85,11 @@ TupleTools = "1.4"
julia = "1.10"

[extras]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"
OMEinsumContractionOrders = "6f22d1fd-8eed-4bb7-9776-e7d684900715"
Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
Expand Down
28 changes: 0 additions & 28 deletions TODO.md

This file was deleted.

14 changes: 14 additions & 0 deletions ext/ITensorNetworksAdaptExt/ITensorNetworksAdaptExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ITensorNetworksAdaptExt
using Adapt: Adapt, adapt
using ITensorNetworks: AbstractITensorNetwork, map_vertex_data_preserve_graph
function Adapt.adapt_structure(to, tn::AbstractITensorNetwork)
# TODO: Define and use:
#
# @preserve_graph map_vertex_data(adapt(to), tn)
#
# or just:
#
# @preserve_graph map(adapt(to), tn)
return map_vertex_data_preserve_graph(adapt(to), tn)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Returns a [`NestedEinsum`](@ref) instance.
```jldoctest
julia> using ITensors, ITensorContractionOrders
julia> i, j, k, l = Index(4), Index(5), Index(6), Index(7);
julia> x, y, z = randomITensor(i, j), randomITensor(j, k), randomITensor(k, l);
julia> x, y, z = random_itensor(i, j), random_itensor(j, k), random_itensor(k, l);
julia> net = optimize_contraction([x, y, z]; optimizer=TreeSA());
```
"""
Expand Down
4 changes: 3 additions & 1 deletion src/ITensorNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ include("solvers/local_solvers/linsolve.jl")
include("solvers/local_solvers/expand.jl")
include("treetensornetworks/abstracttreetensornetwork.jl")
include("treetensornetworks/treetensornetwork.jl")
include("treetensornetworks/opsum_to_ttn.jl")
include("treetensornetworks/opsum_to_ttn/matelem.jl")
include("treetensornetworks/opsum_to_ttn/qnarrelem.jl")
include("treetensornetworks/opsum_to_ttn/opsum_to_ttn.jl")
include("treetensornetworks/projttns/abstractprojttn.jl")
include("treetensornetworks/projttns/projttn.jl")
include("treetensornetworks/projttns/projttnsum.jl")
Expand Down
10 changes: 7 additions & 3 deletions src/abstractindsnetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,13 @@ function insert_linkinds(
for e in edges
# TODO: Change to check if it is empty.
if !isassigned(indsnetwork, e)
iₑ = Index(link_space, edge_tag(e))
# TODO: Allow setting with just `Index`.
indsnetwork[e] = [iₑ]
if !isnothing(link_space)
iₑ = Index(link_space, edge_tag(e))
# TODO: Allow setting with just `Index`.
indsnetwork[e] = [iₑ]
else
indsnetwork[e] = []
end
end
end
return indsnetwork
Expand Down
108 changes: 56 additions & 52 deletions src/abstractitensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ using ITensors:
settags,
sim,
swaptags
using ITensors.ITensorMPS: ITensorMPS, add, linkdim, linkinds, siteinds
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.GraphsExtensions:
, directed_graph, incident_edges, rename_vertices, vertextype
Expand Down Expand Up @@ -138,6 +139,30 @@ function setindex_preserve_graph!(tn::AbstractITensorNetwork, value, vertex)
return tn
end

# TODO: Move to `BaseExtensions` module.
function is_setindex!_expr(expr::Expr)
return is_assignment_expr(expr) && is_getindex_expr(first(expr.args))
end
is_setindex!_expr(x) = false
is_getindex_expr(expr::Expr) = (expr.head === :ref)
is_getindex_expr(x) = false
is_assignment_expr(expr::Expr) = (expr.head === :(=))
is_assignment_expr(expr) = false

# TODO: Define this in terms of a function mapping
# preserve_graph_function(::typeof(setindex!)) = setindex!_preserve_graph
# preserve_graph_function(::typeof(map_vertex_data)) = map_vertex_data_preserve_graph
# Also allow annotating codeblocks like `@views`.
macro preserve_graph(expr)
if !is_setindex!_expr(expr)
error(
"preserve_graph must be used with setindex! syntax (as @preserve_graph a[i,j,...] = value)",
)
end
@capture(expr, array_[indices__] = value_)
return :(setindex_preserve_graph!($(esc(array)), $(esc(value)), $(esc.(indices)...)))
end

function ITensors.hascommoninds(tn::AbstractITensorNetwork, edge::Pair)
return hascommoninds(tn, edgetype(tn)(edge))
end
Expand All @@ -148,7 +173,7 @@ end

function Base.setindex!(tn::AbstractITensorNetwork, value, v)
# v = to_vertex(tn, index...)
setindex_preserve_graph!(tn, value, v)
@preserve_graph tn[v] = value
for edge in incident_edges(tn, v)
rem_edge!(tn, edge)
end
Expand Down Expand Up @@ -297,12 +322,12 @@ function ITensors.replaceinds(
@assert underlying_graph(is) == underlying_graph(is′)
for v in vertices(is)
isassigned(is, v) || continue
setindex_preserve_graph!(tn, replaceinds(tn[v], is[v] => is′[v]), v)
@preserve_graph tn[v] = replaceinds(tn[v], is[v] => is′[v])
end
for e in edges(is)
isassigned(is, e) || continue
for v in (src(e), dst(e))
setindex_preserve_graph!(tn, replaceinds(tn[v], is[e] => is′[e]), v)
@preserve_graph tn[v] = replaceinds(tn[v], is[e] => is′[e])
end
end
return tn
Expand Down Expand Up @@ -361,13 +386,31 @@ end

LinearAlgebra.adjoint(tn::Union{IndsNetwork,AbstractITensorNetwork}) = prime(tn)

#dag(tn::AbstractITensorNetwork) = map_vertex_data(dag, tn)
function ITensors.dag(tn::AbstractITensorNetwork)
tndag = copy(tn)
for v in vertices(tndag)
setindex_preserve_graph!(tndag, dag(tndag[v]), v)
function map_vertex_data(f, tn::AbstractITensorNetwork)
tn = copy(tn)
for v in vertices(tn)
tn[v] = f(tn[v])
end
return tn
end

# TODO: Define `@preserve_graph map_vertex_data(f, tn)`
function map_vertex_data_preserve_graph(f, tn::AbstractITensorNetwork)
tn = copy(tn)
for v in vertices(tn)
@preserve_graph tn[v] = f(tn[v])
end
return tndag
return tn
end

function Base.conj(tn::AbstractITensorNetwork)
# TODO: Use `@preserve_graph map_vertex_data(f, tn)`
return map_vertex_data_preserve_graph(conj, tn)
end

function ITensors.dag(tn::AbstractITensorNetwork)
# TODO: Use `@preserve_graph map_vertex_data(f, tn)`
return map_vertex_data_preserve_graph(dag, tn)
end

# TODO: should this make sure that internal indices
Expand Down Expand Up @@ -442,9 +485,7 @@ function NDTensors.contract(
for n_dst in neighbors_dst
add_edge!(tn, merged_vertex => n_dst)
end

setindex_preserve_graph!(tn, new_itensor, merged_vertex)

@preserve_graph tn[merged_vertex] = new_itensor
return tn
end

Expand Down Expand Up @@ -533,13 +574,8 @@ function LinearAlgebra.factorize(
add_edge!(tn, X_vertex => nX)
end
add_edge!(tn, Y_vertex => dst(edge))

# tn[X_vertex] = X
setindex_preserve_graph!(tn, X, X_vertex)

# tn[Y_vertex] = Y
setindex_preserve_graph!(tn, Y, Y_vertex)

@preserve_graph tn[X_vertex] = X
@preserve_graph tn[Y_vertex] = Y
return tn
end

Expand Down Expand Up @@ -663,38 +699,6 @@ function split_index(
return tn
end

function flatten_networks(
tn1::AbstractITensorNetwork,
tn2::AbstractITensorNetwork;
map_bra_linkinds=sim,
flatten=true,
combine_linkinds=true,
kwargs...,
)
@assert issetequal(vertices(tn1), vertices(tn2))
tn1 = map_bra_linkinds(tn1; sites=[])
flattened_net = (tn1, tn2; kwargs...)
if flatten
for v in vertices(tn1)
flattened_net = contract(flattened_net, (v, 2) => (v, 1); merged_vertex=v)
end
end
if combine_linkinds
flattened_net = ITensorNetworks.combine_linkinds(flattened_net)
end
return flattened_net
end

function flatten_networks(
tn1::AbstractITensorNetwork,
tn2::AbstractITensorNetwork,
tn3::AbstractITensorNetwork,
tn_tail::AbstractITensorNetwork...;
kwargs...,
)
return flatten_networks(flatten_networks(tn1, tn2; kwargs...), tn3, tn_tail...; kwargs...)
end

function inner_network(x::AbstractITensorNetwork, y::AbstractITensorNetwork; kwargs...)
return BilinearFormNetwork(x, y; kwargs...)
end
Expand Down
9 changes: 7 additions & 2 deletions src/apply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using ITensors:
unioninds,
uniqueinds
using ITensors.ContractionSequenceOptimization: optimal_contraction_sequence
using ITensors.ITensorMPS: siteinds
using ITensorMPS: siteinds
using KrylovKit: linsolve
using LinearAlgebra: eigen, norm, svd
using NamedGraphs: NamedEdge, has_edge
Expand Down Expand Up @@ -266,7 +266,12 @@ function ITensors.apply(
end

function ITensors.apply(
o⃗::Scaled, ψ::AbstractITensorNetwork; normalize=false, ortho=false, apply_kwargs...
o⃗::Scaled,
ψ::AbstractITensorNetwork;
cutoff=nothing,
normalize=false,
ortho=false,
apply_kwargs...,
)
return maybe_real(Ops.coefficient(o⃗)) *
apply(Ops.argument(o⃗), ψ; cutoff, maxdim, normalize, ortho, apply_kwargs...)
Expand Down
2 changes: 1 addition & 1 deletion src/boundarymps.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ITensors: inner
using ITensors.ITensorMPS: ITensorMPS
using ITensorMPS: ITensorMPS

#Given an ITensorNetwork on an Lx*Ly grid with sites indexed as (i,j) then perform contraction using a sequence of mps-mpo contractions
function contract_boundary_mps(tn::ITensorNetwork; kwargs...)
Expand Down
Loading

0 comments on commit 833ed21

Please sign in to comment.