Skip to content
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

Contraction path optimization with EinExprs #120

Merged
merged 49 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
8f2f40c
Implement `contraction_sequence` with EinExprs optimizers
mofeing Jan 10, 2024
21220eb
Refactor code
mofeing Jan 10, 2024
6692b13
Skip contraction of `externalinds`
mofeing Jan 11, 2024
cd3ea8f
Fix typo
mofeing Jan 11, 2024
d4b0ea1
Import `EinExprs` symbols
mofeing Jan 11, 2024
11f2df0
Fix resulting type of `map` on tensors
mofeing Jan 11, 2024
c412bb8
Map `EinExpr` to expected contraction sequence format by `ITensorNetw…
mofeing Jan 11, 2024
8e83fb2
Move code to package extension
mofeing Jan 11, 2024
16a0fcd
Update EinExprs to v0.6
mofeing Jan 26, 2024
92fcf81
Fixes
mofeing Jan 29, 2024
b0fa2ab
Refactor `externalinds` reference
mofeing Jan 29, 2024
21c5728
Refactor code
mofeing Feb 6, 2024
7bf59be
Test `EinExprs` code
mofeing Feb 6, 2024
e405493
Fix `contraction_sequence` interface
mofeing Feb 6, 2024
5b2fe0d
Duplicate code to preserve `tensor_map`
mofeing Feb 6, 2024
ad6d896
Lasf fixes
mofeing Feb 6, 2024
ead70ad
Restructure package extension
mofeing Feb 6, 2024
e9acbf4
Remove redundant package specification
mofeing Feb 6, 2024
e3ca80f
Apply suggestion by @mtfishman
mofeing Feb 6, 2024
6d6eea2
Apply suggestion by @mtfishman
mofeing Feb 6, 2024
5516951
Apply suggestion by @mtfishman
mofeing Feb 6, 2024
33da278
More suggestions by @mtfishman
mofeing Feb 6, 2024
053cf87
Apply style suggestions by @mtfishman
mofeing Feb 6, 2024
05f5c3f
Remove duplicated code
mofeing Feb 6, 2024
f25bb13
Rename `tensors` to `tensor_exprs`
mofeing Feb 6, 2024
864dd59
Define `to_contraction_sequence` in the global scope
mofeing Feb 6, 2024
7c92cc1
Apply style suggestion by @mtfishman
mofeing Feb 6, 2024
cad581d
Rename `tensor_map` to `tensor_inds_to_vertex`
mofeing Feb 6, 2024
e9b4aea
Apply style suggestion by @mtfishman
mofeing Feb 6, 2024
43341ee
Reorder args in `to_contraction_sequence`
mofeing Feb 6, 2024
d43f771
Rename `prepare_einexpr` to `to_einexpr`
mofeing Feb 6, 2024
f399b78
Fix call to renamed function
mofeing Feb 6, 2024
6c48bbd
Update ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl
mofeing Feb 7, 2024
cc19993
Move out vertex mapping from `to_einexpr` to `tensor_inds_to_vertex`
mofeing Feb 7, 2024
c2d9f9d
Apply suggestions by @JoeyT1994
mofeing Feb 7, 2024
1d43c4b
Apply suggestion by @JoeyT1994
mofeing Feb 7, 2024
8ed0679
Update ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl
mofeing Feb 7, 2024
f07754d
Refactor test as suggested by @mtfishman
mofeing Feb 7, 2024
8c8328d
Duplicate weakdeps in extras to support Julia <1.9
mofeing Feb 8, 2024
acd7a03
Try fix for package extension path
mofeing Feb 12, 2024
7e533f2
Bring `@Algorithm_str` into the extension namespace
mtfishman Feb 12, 2024
739e752
Remove `Algorithm` from extension namespace
mtfishman Feb 12, 2024
6acf91a
Bring `inds` into the extension namespace
mtfishman Feb 12, 2024
bd132eb
Fix typo
mofeing Feb 13, 2024
d965b14
Fix KaHyPar tests with EinExprs
mofeing Feb 13, 2024
47fddf6
Fix EinExprs version
mofeing Feb 13, 2024
b73ae82
Fix missing type definition
mofeing Feb 13, 2024
4c22571
Fix missing type definition
mofeing Feb 13, 2024
3ec86f2
Fix package extensions on Julia <1.9
mofeing Feb 13, 2024
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
8 changes: 8 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"

[weakdeps]
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"

[extensions]
ITensorNetworksEinExprsExt = "EinExprs"

[compat]
AbstractTrees = "0.4.4"
Combinatorics = "1"
Expand All @@ -40,6 +46,7 @@ DataStructures = "0.18"
Dictionaries = "0.3.15"
Distributions = "0.25.86"
DocStringExtensions = "0.8, 0.9"
EinExprs = "0.6.4"
Graphs = "1.8"
GraphsFlows = "0.1.1"
ITensors = "0.3.23"
Expand All @@ -60,6 +67,7 @@ julia = "1.7"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"

[targets]
test = ["Test"]
52 changes: 52 additions & 0 deletions ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module ITensorNetworksEinExprsExt

using ITensors: Index, ITensor, @Algorithm_str, inds, noncommoninds
using ITensorNetworks:
ITensorNetworks, ITensorNetwork, vertextype, vertex_data, contraction_sequence
using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr

function to_einexpr(ts::Vector{ITensor})
IndexType = Any

tensor_exprs = EinExpr{IndexType}[]
inds_dims = Dict{IndexType,Int}()

for tensor_v in ts
inds_v = collect(inds(tensor_v))
push!(tensor_exprs, EinExpr{IndexType}(; head=inds_v))
merge!(inds_dims, Dict(inds_v .=> size(tensor_v)))
end

externalinds_tn = reduce(noncommoninds, ts)
return SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), inds_dims)
end

function tensor_inds_to_vertex(ts::Vector{ITensor})
IndexType = Any

mapping = Dict{Set{IndexType},VertexType}()
mofeing marked this conversation as resolved.
Show resolved Hide resolved

for (v, tensor_v) in enumerate(ts)
inds_v = collect(inds(tensor_v))
mapping[Set(inds_v)] = v
end

return mapping
end

function ITensorNetworks.contraction_sequence(
::Algorithm"einexpr", tn::Vector{ITensor}; optimizer=EinExprs.Exhaustive()
)
expr = to_einexpr(tn)
path = einexpr(optimizer, expr)
return to_contraction_sequence(path, tensor_inds_to_vertex(tn))
end

function to_contraction_sequence(expr, tensor_inds_to_vertex)
EinExprs.nargs(expr) == 0 && return tensor_inds_to_vertex[Set(expr.head)]
return map(
expr -> to_contraction_sequence(expr, tensor_inds_to_vertex), EinExprs.args(expr)
)
end

end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
Expand Down
14 changes: 13 additions & 1 deletion test/test_contraction_sequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using ITensorNetworks
using OMEinsumContractionOrders
using Random
using Test
using EinExprs: Exhaustive, Greedy, HyPar

Random.seed!(1234)

Expand All @@ -23,7 +24,15 @@ ITensors.disable_warn_order()
res_tree_sa = contract(tn; sequence=seq_tree_sa)[]
seq_sa_bipartite = contraction_sequence(tn; alg="sa_bipartite")
res_sa_bipartite = contract(tn; sequence=seq_sa_bipartite)[]
@test res_optimal ≈ res_greedy ≈ res_tree_sa ≈ res_sa_bipartite
seq_einexprs_exhaustive = contraction_sequence(tn; alg="einexpr", optimizer=Exhaustive())
res_einexprs_exhaustive = contract(tn; sequence=seq_einexprs_exhaustive)[]
seq_einexprs_greedy = contraction_sequence(tn; alg="einexpr", optimizer=Greedy())
res_einexprs_greedy = contract(tn; sequence=seq_einexprs_exhaustive)[]
@test res_greedy ≈ res_optimal
@test res_tree_sa ≈ res_optimal
@test res_sa_bipartite ≈ res_optimal
@test res_einexprs_exhaustive ≈ res_optimal
@test res_einexprs_greedy ≈ res_optimal

if !Sys.iswindows()
# KaHyPar doesn't work on Windows
Expand All @@ -34,5 +43,8 @@ ITensors.disable_warn_order()
seq_kahypar_bipartite = contraction_sequence(tn; alg="kahypar_bipartite", sc_target=200)
res_kahypar_bipartite = contract(tn; sequence=seq_kahypar_bipartite)[]
@test res_optimal ≈ res_kahypar_bipartite
seq_einexprs_kahypar = contraction_sequence(tn; alg="einexpr", optimizer=HyPar())
res_einexprs_kahypar = contract(tn; sequence=seq_einexprs_kahypar)[]
@test res_einexprs_kahypar ≈ res_optimal
end
end
Loading