From 8f2f40c71caa623838f4fb3d5923e1169a316fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Wed, 10 Jan 2024 21:11:17 +0100 Subject: [PATCH 01/49] Implement `contraction_sequence` with EinExprs optimizers --- Project.toml | 2 ++ src/contraction_sequences.jl | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Project.toml b/Project.toml index 4b9eee8c..d68c415d 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889" ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5" @@ -40,6 +41,7 @@ DataStructures = "0.18" Dictionaries = "0.3.15" Distributions = "0.25.86" DocStringExtensions = "0.8, 0.9" +EinExprs = "0.5" Graphs = "1.8" GraphsFlows = "0.1.1" ITensors = "0.3.23" diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index c32239a5..91d07091 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -119,3 +119,17 @@ function contraction_sequence(::Algorithm"kahypar_bipartite", tn; kwargs...) tn; optimizer=OMEinsumContractionOrders.KaHyParBipartite(; kwargs...) ) end + +function contraction_sequence(::Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive()) + is = IndsNetwork(tn) + size_dict = Dict( + Symbol(id(index)) => dim(index) for + index in Iterators.map(i -> only(is[i]), vertices(is)) + ) + + tensors = map(vertex_data(ψ)) do tensor + EinExpr(collect(map(Symbol ∘ id, inds(tensor)))) + end + + return tn = einexpr(optimizer, EinExpr(tensors, size_dict)) +end From 21220ebdc3ad543e7cb10bda00745c7bd92f1207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Wed, 10 Jan 2024 23:31:14 +0100 Subject: [PATCH 02/49] Refactor code --- src/contraction_sequences.jl | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index 91d07091..195a8ef9 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -122,14 +122,17 @@ end function contraction_sequence(::Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive()) is = IndsNetwork(tn) - size_dict = Dict( - Symbol(id(index)) => dim(index) for - index in Iterators.map(i -> only(is[i]), vertices(is)) - ) tensors = map(vertex_data(ψ)) do tensor - EinExpr(collect(map(Symbol ∘ id, inds(tensor)))) + _inds = collect(map(Symbol ∘ id, inds(tensor))) + _size = Dict(_inds .=> size(tensor)) + EinExpr(_inds, _size) end - return tn = einexpr(optimizer, EinExpr(tensors, size_dict)) + # TODO (mofeing) list open indices + expr = sum(tensors; skip=...) + path = einexpr(optimizer, expr) + + # TODO (mofeing) convert EinExpr to contraction sequence + return nothing end From 6692b1309b2a9c4c85fd018150eb68489d5bbcdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 11 Jan 2024 17:24:17 +0100 Subject: [PATCH 03/49] Skip contraction of `externalinds` --- src/contraction_sequences.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index 195a8ef9..24a92f07 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -129,8 +129,8 @@ function contraction_sequence(::Algorithm"einexpr", tn; optimizer=EinExprs.Exhau EinExpr(_inds, _size) end - # TODO (mofeing) list open indices - expr = sum(tensors; skip=...) + _openinds = collect(map(Symbol ∘ id, externalinds(tn))) + expr = sum(tensors; skip=_openinds) path = einexpr(optimizer, expr) # TODO (mofeing) convert EinExpr to contraction sequence From cd3ea8f2ba26f62f8c27160470e1037a783d9eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 11 Jan 2024 17:24:43 +0100 Subject: [PATCH 04/49] Fix typo --- src/contraction_sequences.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index 24a92f07..e683aed4 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -123,7 +123,7 @@ end function contraction_sequence(::Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive()) is = IndsNetwork(tn) - tensors = map(vertex_data(ψ)) do tensor + tensors = map(vertex_data(tn)) do tensor _inds = collect(map(Symbol ∘ id, inds(tensor))) _size = Dict(_inds .=> size(tensor)) EinExpr(_inds, _size) From d4b0ea18be62b0512b8360d64665350180a51afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 11 Jan 2024 17:45:57 +0100 Subject: [PATCH 05/49] Import `EinExprs` symbols --- src/contraction_sequences.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index e683aed4..b72da7e5 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -1,3 +1,5 @@ +using EinExprs: EinExprs, EinExpr, einexpr + function contraction_sequence(tn::Vector{ITensor}; alg="optimal", kwargs...) return contraction_sequence(Algorithm(alg), tn; kwargs...) end From 11f2df0a8bc17b4545a0f61fc38d944140ed4faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 11 Jan 2024 17:46:22 +0100 Subject: [PATCH 06/49] Fix resulting type of `map` on tensors --- src/contraction_sequences.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index b72da7e5..a0580392 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -123,9 +123,7 @@ function contraction_sequence(::Algorithm"kahypar_bipartite", tn; kwargs...) end function contraction_sequence(::Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive()) - is = IndsNetwork(tn) - - tensors = map(vertex_data(tn)) do tensor + tensors = map(tokenized(vertex_data(tn))) do tensor _inds = collect(map(Symbol ∘ id, inds(tensor))) _size = Dict(_inds .=> size(tensor)) EinExpr(_inds, _size) From c412bb8917bf2600af84a4f8a2666b9bcc037d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 11 Jan 2024 18:25:48 +0100 Subject: [PATCH 07/49] Map `EinExpr` to expected contraction sequence format by `ITensorNetworks` --- src/contraction_sequences.jl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index a0580392..892af423 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -123,16 +123,23 @@ function contraction_sequence(::Algorithm"kahypar_bipartite", tn; kwargs...) end function contraction_sequence(::Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive()) - tensors = map(tokenized(vertex_data(tn))) do tensor - _inds = collect(map(Symbol ∘ id, inds(tensor))) - _size = Dict(_inds .=> size(tensor)) - EinExpr(_inds, _size) - end + tensor_map = IdDict( + map(pairs(vertex_data(tn))) do (key, tensor) + _inds = collect(map(Symbol ∘ id, inds(tensor))) + _size = Dict(_inds .=> size(tensor)) + EinExpr(_inds, _size) => key + end, + ) + tensors = collect(keys(tensor_map)) _openinds = collect(map(Symbol ∘ id, externalinds(tn))) expr = sum(tensors; skip=_openinds) path = einexpr(optimizer, expr) - # TODO (mofeing) convert EinExpr to contraction sequence - return nothing + function _convert_to_contraction_sequence(subpath) + length(subpath.args) == 0 && return tensor_map[subpath] + return map(_convert_to_contraction_sequence, subpath.args) + end + + return _convert_to_contraction_sequence(path) end From 8e83fb2f11c6e2602e61918af989f0fbba73fc17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 11 Jan 2024 19:01:51 +0100 Subject: [PATCH 08/49] Move code to package extension --- Project.toml | 7 ++++++- ext/ITensorNetworksEinExprsExt.jl | 30 ++++++++++++++++++++++++++++++ src/contraction_sequences.jl | 24 ------------------------ 3 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 ext/ITensorNetworksEinExprsExt.jl diff --git a/Project.toml b/Project.toml index d68c415d..2c1c6673 100644 --- a/Project.toml +++ b/Project.toml @@ -12,7 +12,6 @@ DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" -EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889" ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5" @@ -32,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" diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl new file mode 100644 index 00000000..8b97648f --- /dev/null +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -0,0 +1,30 @@ +module ITensorNetworksEinExprsExt + +using ITensorNetworks +using EinExprs: EinExprs, EinExpr, einexpr + +function ITensorNetworks.contraction_sequence( + ::ITensorNetworks.Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive() +) + tensor_map = IdDict( + map(pairs(vertex_data(tn))) do (key, tensor) + _inds = collect(map(Symbol ∘ id, inds(tensor))) + _size = Dict(_inds .=> size(tensor)) + EinExpr(_inds, _size) => key + end, + ) + tensors = collect(keys(tensor_map)) + + _openinds = collect(map(Symbol ∘ id, externalinds(tn))) + expr = sum(tensors; skip=_openinds) + path = einexpr(optimizer, expr) + + function _convert_to_contraction_sequence(subpath) + length(subpath.args) == 0 && return tensor_map[subpath] + return map(_convert_to_contraction_sequence, subpath.args) + end + + return _convert_to_contraction_sequence(path) +end + +end diff --git a/src/contraction_sequences.jl b/src/contraction_sequences.jl index 892af423..c32239a5 100644 --- a/src/contraction_sequences.jl +++ b/src/contraction_sequences.jl @@ -1,5 +1,3 @@ -using EinExprs: EinExprs, EinExpr, einexpr - function contraction_sequence(tn::Vector{ITensor}; alg="optimal", kwargs...) return contraction_sequence(Algorithm(alg), tn; kwargs...) end @@ -121,25 +119,3 @@ function contraction_sequence(::Algorithm"kahypar_bipartite", tn; kwargs...) tn; optimizer=OMEinsumContractionOrders.KaHyParBipartite(; kwargs...) ) end - -function contraction_sequence(::Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive()) - tensor_map = IdDict( - map(pairs(vertex_data(tn))) do (key, tensor) - _inds = collect(map(Symbol ∘ id, inds(tensor))) - _size = Dict(_inds .=> size(tensor)) - EinExpr(_inds, _size) => key - end, - ) - tensors = collect(keys(tensor_map)) - - _openinds = collect(map(Symbol ∘ id, externalinds(tn))) - expr = sum(tensors; skip=_openinds) - path = einexpr(optimizer, expr) - - function _convert_to_contraction_sequence(subpath) - length(subpath.args) == 0 && return tensor_map[subpath] - return map(_convert_to_contraction_sequence, subpath.args) - end - - return _convert_to_contraction_sequence(path) -end From 16a0fcdb2f3311383f70307d2d1d2d5b5191367a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Fri, 26 Jan 2024 15:07:53 +0100 Subject: [PATCH 09/49] Update EinExprs to v0.6 --- Project.toml | 2 +- ext/ITensorNetworksEinExprsExt.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 2c1c6673..0ee65a49 100644 --- a/Project.toml +++ b/Project.toml @@ -46,7 +46,7 @@ DataStructures = "0.18" Dictionaries = "0.3.15" Distributions = "0.25.86" DocStringExtensions = "0.8, 0.9" -EinExprs = "0.5" +EinExprs = "0.6" Graphs = "1.8" GraphsFlows = "0.1.1" ITensors = "0.3.23" diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl index 8b97648f..c1d44f27 100644 --- a/ext/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -8,14 +8,14 @@ function ITensorNetworks.contraction_sequence( ) tensor_map = IdDict( map(pairs(vertex_data(tn))) do (key, tensor) - _inds = collect(map(Symbol ∘ id, inds(tensor))) + _inds = collect(ITensorNetworks.inds(tensor)) _size = Dict(_inds .=> size(tensor)) EinExpr(_inds, _size) => key end, ) tensors = collect(keys(tensor_map)) - _openinds = collect(map(Symbol ∘ id, externalinds(tn))) + _openinds = collect(ITensorNetworks.externalinds(tn)) expr = sum(tensors; skip=_openinds) path = einexpr(optimizer, expr) From 92fcf8183f0028b9e9fbde66d270029a6ca3a2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Mon, 29 Jan 2024 10:19:02 +0100 Subject: [PATCH 10/49] Fixes --- ext/ITensorNetworksEinExprsExt.jl | 35 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl index c1d44f27..5d4ef6d3 100644 --- a/ext/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -1,27 +1,34 @@ module ITensorNetworksEinExprsExt using ITensorNetworks -using EinExprs: EinExprs, EinExpr, einexpr +using ITensorNetworks: Index +using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr function ITensorNetworks.contraction_sequence( - ::ITensorNetworks.Algorithm"einexpr", tn; optimizer=EinExprs.Exhaustive() -) - tensor_map = IdDict( - map(pairs(vertex_data(tn))) do (key, tensor) - _inds = collect(ITensorNetworks.inds(tensor)) - _size = Dict(_inds .=> size(tensor)) - EinExpr(_inds, _size) => key - end, - ) - tensors = collect(keys(tensor_map)) + ::ITensorNetworks.Algorithm"einexpr", + tn::ITensorNetwork{T}; + optimizer=EinExprs.Exhaustive(), +) where {T} + IndexType = Any + + tensors = EinExpr{IndexType}[] + tensor_map = Dict{Set{IndexType},T}() + sizedict = Dict{IndexType,Int}() + + for (key, tensor) in pairs(vertex_data(tn)) + _inds = collect(ITensorNetworks.inds(tensor)) + push!(tensors, EinExpr{IndexType}(; head=_inds)) + tensor_map[Set(_inds)] = key + merge!(sizedict, Dict(_inds .=> size(tensor))) + end _openinds = collect(ITensorNetworks.externalinds(tn)) - expr = sum(tensors; skip=_openinds) + expr = SizedEinExpr(sum(tensors; skip=_openinds), sizedict) path = einexpr(optimizer, expr) function _convert_to_contraction_sequence(subpath) - length(subpath.args) == 0 && return tensor_map[subpath] - return map(_convert_to_contraction_sequence, subpath.args) + EinExprs.nargs(subpath) == 0 && return tensor_map[Set(subpath.head)] + return map(_convert_to_contraction_sequence, EinExprs.args(subpath)) end return _convert_to_contraction_sequence(path) From b0fa2ab8dbd912241f19b8a1f3fb9f621f8b3a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Mon, 29 Jan 2024 17:47:44 +0100 Subject: [PATCH 11/49] Refactor `externalinds` reference --- ext/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl index 5d4ef6d3..256dc736 100644 --- a/ext/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -22,7 +22,7 @@ function ITensorNetworks.contraction_sequence( merge!(sizedict, Dict(_inds .=> size(tensor))) end - _openinds = collect(ITensorNetworks.externalinds(tn)) + _openinds = collect(externalinds(tn)) expr = SizedEinExpr(sum(tensors; skip=_openinds), sizedict) path = einexpr(optimizer, expr) From 21c5728dc8927d57f541fc2eb00ed0cff2085f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 11:00:37 +0100 Subject: [PATCH 12/49] Refactor code --- ext/ITensorNetworksEinExprsExt.jl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl index 256dc736..2fc78af6 100644 --- a/ext/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -4,11 +4,7 @@ using ITensorNetworks using ITensorNetworks: Index using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr -function ITensorNetworks.contraction_sequence( - ::ITensorNetworks.Algorithm"einexpr", - tn::ITensorNetwork{T}; - optimizer=EinExprs.Exhaustive(), -) where {T} +function EinExprs.einexpr(tn::ITensorNetwork{T}; optimizer::EinExprs.Optimizer) where {T} IndexType = Any tensors = EinExpr{IndexType}[] @@ -24,7 +20,16 @@ function ITensorNetworks.contraction_sequence( _openinds = collect(externalinds(tn)) expr = SizedEinExpr(sum(tensors; skip=_openinds), sizedict) - path = einexpr(optimizer, expr) + + return einexpr(optimizer, expr) +end + +function ITensorNetworks.contraction_sequence( + ::ITensorNetworks.Algorithm"einexpr", + tn::ITensorNetwork{T}; + optimizer=EinExprs.Exhaustive(), +) where {T} + path = einexpr(tn; optimizer) function _convert_to_contraction_sequence(subpath) EinExprs.nargs(subpath) == 0 && return tensor_map[Set(subpath.head)] From 7bf59be972e0fdaf5e07ebcdfed10e73013bf022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 14:47:50 +0100 Subject: [PATCH 13/49] Test `EinExprs` code --- test/Project.toml | 1 + test/test_contraction_sequence.jl | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/test/Project.toml b/test/Project.toml index f9f1d11f..05386c5c 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -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" diff --git a/test/test_contraction_sequence.jl b/test/test_contraction_sequence.jl index fd732f3f..2184db22 100644 --- a/test/test_contraction_sequence.jl +++ b/test/test_contraction_sequence.jl @@ -3,6 +3,7 @@ using ITensorNetworks using OMEinsumContractionOrders using Random using Test +using EinExprs: Exhaustive, Greedy, HyPar Random.seed!(1234) @@ -23,7 +24,19 @@ 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)[] + seq_einexprs_kahypar = contraction_sequence(tn; alg="einexpr", optimizer=HyPar()) + res_einexprs_kahypar = contract(tn; sequence=seq_einexprs_kahypar)[] + @test res_optimal ≈ + res_greedy ≈ + res_tree_sa ≈ + res_sa_bipartite ≈ + res_einexprs_exhaustive ≈ + res_einexprs_greedy ≈ + res_einexprs_kahypar if !Sys.iswindows() # KaHyPar doesn't work on Windows From e405493697845114688acf6994e50fc89b1f486f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 14:49:06 +0100 Subject: [PATCH 14/49] Fix `contraction_sequence` interface --- ext/ITensorNetworksEinExprsExt.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl index 2fc78af6..ca9bcfe7 100644 --- a/ext/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -1,5 +1,6 @@ module ITensorNetworksEinExprsExt +using ITensors using ITensorNetworks using ITensorNetworks: Index using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr @@ -25,10 +26,9 @@ function EinExprs.einexpr(tn::ITensorNetwork{T}; optimizer::EinExprs.Optimizer) end function ITensorNetworks.contraction_sequence( - ::ITensorNetworks.Algorithm"einexpr", - tn::ITensorNetwork{T}; - optimizer=EinExprs.Exhaustive(), -) where {T} + ::ITensorNetworks.Algorithm"einexpr", tn::Vector{ITensor}; optimizer=EinExprs.Exhaustive() +) + tn = ITensorNetwork(tn) path = einexpr(tn; optimizer) function _convert_to_contraction_sequence(subpath) From 5b2fe0d0ca810c10adda63bd74fdb49a1ac65974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 14:53:35 +0100 Subject: [PATCH 15/49] Duplicate code to preserve `tensor_map` --- ext/ITensorNetworksEinExprsExt.jl | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl index ca9bcfe7..788b04a6 100644 --- a/ext/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -29,7 +29,23 @@ function ITensorNetworks.contraction_sequence( ::ITensorNetworks.Algorithm"einexpr", tn::Vector{ITensor}; optimizer=EinExprs.Exhaustive() ) tn = ITensorNetwork(tn) - path = einexpr(tn; optimizer) + IndexType = Any + + tensors = EinExpr{IndexType}[] + tensor_map = Dict{Set{IndexType},T}() + sizedict = Dict{IndexType,Int}() + + for (key, tensor) in pairs(vertex_data(tn)) + _inds = collect(ITensorNetworks.inds(tensor)) + push!(tensors, EinExpr{IndexType}(; head=_inds)) + tensor_map[Set(_inds)] = key + merge!(sizedict, Dict(_inds .=> size(tensor))) + end + + _openinds = collect(externalinds(tn)) + expr = SizedEinExpr(sum(tensors; skip=_openinds), sizedict) + + path = einexpr(optimizer, expr) function _convert_to_contraction_sequence(subpath) EinExprs.nargs(subpath) == 0 && return tensor_map[Set(subpath.head)] From ad6d896c112c5b989e33e687a3ed0d441b42c4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 15:04:19 +0100 Subject: [PATCH 16/49] Lasf fixes --- ext/ITensorNetworksEinExprsExt.jl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt.jl index 788b04a6..2c8b180e 100644 --- a/ext/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt.jl @@ -26,9 +26,16 @@ function EinExprs.einexpr(tn::ITensorNetwork{T}; optimizer::EinExprs.Optimizer) end function ITensorNetworks.contraction_sequence( - ::ITensorNetworks.Algorithm"einexpr", tn::Vector{ITensor}; optimizer=EinExprs.Exhaustive() + alg::ITensorNetworks.Algorithm"einexpr", tn::Vector{ITensor}; kwargs... ) - tn = ITensorNetwork(tn) + return contraction_sequence(alg, ITensorNetwork(tn); kwargs...) +end + +function ITensorNetworks.contraction_sequence( + ::ITensorNetworks.Algorithm"einexpr", + tn::ITensorNetwork{T}; + optimizer=EinExprs.Exhaustive(), +) where {T} IndexType = Any tensors = EinExpr{IndexType}[] From ead70adc97d498cfdbc798d588532ca448e327b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 15:42:38 +0100 Subject: [PATCH 17/49] Restructure package extension --- .../src}/ITensorNetworksEinExprsExt.jl | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ext/{ => ITensorNetworksEinExprsExt/src}/ITensorNetworksEinExprsExt.jl (100%) diff --git a/ext/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl similarity index 100% rename from ext/ITensorNetworksEinExprsExt.jl rename to ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl From e9acbf462b8911d7fd89eb136ee240922afcfbb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 15:51:57 +0100 Subject: [PATCH 18/49] Remove redundant package specification --- .../src/ITensorNetworksEinExprsExt.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 2c8b180e..7f59bbbf 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -13,7 +13,7 @@ function EinExprs.einexpr(tn::ITensorNetwork{T}; optimizer::EinExprs.Optimizer) sizedict = Dict{IndexType,Int}() for (key, tensor) in pairs(vertex_data(tn)) - _inds = collect(ITensorNetworks.inds(tensor)) + _inds = collect(inds(tensor)) push!(tensors, EinExpr{IndexType}(; head=_inds)) tensor_map[Set(_inds)] = key merge!(sizedict, Dict(_inds .=> size(tensor))) @@ -43,7 +43,7 @@ function ITensorNetworks.contraction_sequence( sizedict = Dict{IndexType,Int}() for (key, tensor) in pairs(vertex_data(tn)) - _inds = collect(ITensorNetworks.inds(tensor)) + _inds = collect(inds(tensor)) push!(tensors, EinExpr{IndexType}(; head=_inds)) tensor_map[Set(_inds)] = key merge!(sizedict, Dict(_inds .=> size(tensor))) From e3ca80f26c19245d8779ba4bce12745678dc8ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 15:53:25 +0100 Subject: [PATCH 19/49] Apply suggestion by @mtfishman --- .../src/ITensorNetworksEinExprsExt.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 7f59bbbf..99e8a6a1 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -5,8 +5,9 @@ using ITensorNetworks using ITensorNetworks: Index using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr -function EinExprs.einexpr(tn::ITensorNetwork{T}; optimizer::EinExprs.Optimizer) where {T} +function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) IndexType = Any + T = vertextype(tn) tensors = EinExpr{IndexType}[] tensor_map = Dict{Set{IndexType},T}() From 6d6eea224f3c9cf415da92c1574b2e258df6334e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 15:57:33 +0100 Subject: [PATCH 20/49] Apply suggestion by @mtfishman --- .../src/ITensorNetworksEinExprsExt.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 99e8a6a1..15fd8dbc 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -36,8 +36,9 @@ function ITensorNetworks.contraction_sequence( ::ITensorNetworks.Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive(), -) where {T} +) IndexType = Any + T = vertextype(tn) tensors = EinExpr{IndexType}[] tensor_map = Dict{Set{IndexType},T}() From 55169513d35098658395bc83c1bd5c7e050ab20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:02:10 +0100 Subject: [PATCH 21/49] Apply suggestion by @mtfishman --- .../src/ITensorNetworksEinExprsExt.jl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 15fd8dbc..d1e99a24 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -1,8 +1,14 @@ module ITensorNetworksEinExprsExt -using ITensors -using ITensorNetworks -using ITensorNetworks: Index +using ITensors: Index, ITensor +using ITensorNetworks: + ITensorNetworks, + ITensorNetwork, + vertextype, + vertex_data, + externalinds, + contraction_sequence, + Algorithm using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) @@ -27,15 +33,13 @@ function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) end function ITensorNetworks.contraction_sequence( - alg::ITensorNetworks.Algorithm"einexpr", tn::Vector{ITensor}; kwargs... + alg::Algorithm"einexpr", tn::Vector{ITensor}; kwargs... ) return contraction_sequence(alg, ITensorNetwork(tn); kwargs...) end function ITensorNetworks.contraction_sequence( - ::ITensorNetworks.Algorithm"einexpr", - tn::ITensorNetwork{T}; - optimizer=EinExprs.Exhaustive(), + ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) IndexType = Any T = vertextype(tn) From 33da278b4773a27a0f4f8a56988fe9b7f63b76d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:05:15 +0100 Subject: [PATCH 22/49] More suggestions by @mtfishman --- .../src/ITensorNetworksEinExprsExt.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index d1e99a24..d3f641cb 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -1,22 +1,21 @@ module ITensorNetworksEinExprsExt -using ITensors: Index, ITensor +using ITensors: Index, ITensor, Algorithm using ITensorNetworks: ITensorNetworks, ITensorNetwork, vertextype, vertex_data, externalinds, - contraction_sequence, - Algorithm + contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) IndexType = Any - T = vertextype(tn) + VertexType = vertextype(tn) tensors = EinExpr{IndexType}[] - tensor_map = Dict{Set{IndexType},T}() + tensor_map = Dict{Set{IndexType},VertexType}() sizedict = Dict{IndexType,Int}() for (key, tensor) in pairs(vertex_data(tn)) @@ -42,10 +41,10 @@ function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) IndexType = Any - T = vertextype(tn) + VertexType = vertextype(tn) tensors = EinExpr{IndexType}[] - tensor_map = Dict{Set{IndexType},T}() + tensor_map = Dict{Set{IndexType},VertexType}() sizedict = Dict{IndexType,Int}() for (key, tensor) in pairs(vertex_data(tn)) From 053cf87dd18f70fbb78a58bb6fbc410859bafdad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:14:42 +0100 Subject: [PATCH 23/49] Apply style suggestions by @mtfishman --- .../src/ITensorNetworksEinExprsExt.jl | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index d3f641cb..b146b482 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -18,15 +18,16 @@ function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) tensor_map = Dict{Set{IndexType},VertexType}() sizedict = Dict{IndexType,Int}() - for (key, tensor) in pairs(vertex_data(tn)) - _inds = collect(inds(tensor)) - push!(tensors, EinExpr{IndexType}(; head=_inds)) - tensor_map[Set(_inds)] = key - merge!(sizedict, Dict(_inds .=> size(tensor))) + for v in vertices(tn) + tensor_v = tn[v] + inds_v = collect(inds(tensor_v)) + push!(tensors, EinExpr{IndexType}(; head=inds_v)) + tensor_map[Set(inds_v)] = key + merge!(sizedict, Dict(inds_v .=> size(tensor_v))) end - _openinds = collect(externalinds(tn)) - expr = SizedEinExpr(sum(tensors; skip=_openinds), sizedict) + externalinds_tn = collect(externalinds(tn)) + expr = SizedEinExpr(sum(tensors; skip=externalinds_tn), sizedict) return einexpr(optimizer, expr) end @@ -47,15 +48,16 @@ function ITensorNetworks.contraction_sequence( tensor_map = Dict{Set{IndexType},VertexType}() sizedict = Dict{IndexType,Int}() - for (key, tensor) in pairs(vertex_data(tn)) - _inds = collect(inds(tensor)) - push!(tensors, EinExpr{IndexType}(; head=_inds)) - tensor_map[Set(_inds)] = key - merge!(sizedict, Dict(_inds .=> size(tensor))) + for v in vertices(tn) + tensor_v = tn[v] + inds_v = collect(inds(tensor_v)) + push!(tensors, EinExpr{IndexType}(; head=inds_v)) + tensor_map[Set(inds_v)] = key + merge!(sizedict, Dict(inds_v .=> size(tensor_v))) end - _openinds = collect(externalinds(tn)) - expr = SizedEinExpr(sum(tensors; skip=_openinds), sizedict) + externalinds_tn = collect(externalinds(tn)) + expr = SizedEinExpr(sum(tensors; skip=externalinds_tn), sizedict) path = einexpr(optimizer, expr) From 05f5c3f3107cc487b1f6d15e0ad57ea95b1969e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:15:07 +0100 Subject: [PATCH 24/49] Remove duplicated code --- .../src/ITensorNetworksEinExprsExt.jl | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index b146b482..85da5b38 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -10,7 +10,7 @@ using ITensorNetworks: contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr -function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) +function prepare_einexpr(tn::ITensorNetwork) IndexType = Any VertexType = vertextype(tn) @@ -29,6 +29,11 @@ function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) externalinds_tn = collect(externalinds(tn)) expr = SizedEinExpr(sum(tensors; skip=externalinds_tn), sizedict) + return expr, tensor_map +end + +function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) + expr, _ = prepare_einexpr(tn) return einexpr(optimizer, expr) end @@ -41,25 +46,7 @@ end function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) - IndexType = Any - VertexType = vertextype(tn) - - tensors = EinExpr{IndexType}[] - tensor_map = Dict{Set{IndexType},VertexType}() - sizedict = Dict{IndexType,Int}() - - for v in vertices(tn) - tensor_v = tn[v] - inds_v = collect(inds(tensor_v)) - push!(tensors, EinExpr{IndexType}(; head=inds_v)) - tensor_map[Set(inds_v)] = key - merge!(sizedict, Dict(inds_v .=> size(tensor_v))) - end - - externalinds_tn = collect(externalinds(tn)) - expr = SizedEinExpr(sum(tensors; skip=externalinds_tn), sizedict) - - path = einexpr(optimizer, expr) + path, tensor_map = prepare_einexpr(tn) function _convert_to_contraction_sequence(subpath) EinExprs.nargs(subpath) == 0 && return tensor_map[Set(subpath.head)] From f25bb137978c59008cbdb0ae0cf3e821d2355764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:17:20 +0100 Subject: [PATCH 25/49] Rename `tensors` to `tensor_exprs` --- .../src/ITensorNetworksEinExprsExt.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 85da5b38..450e34f2 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -14,20 +14,20 @@ function prepare_einexpr(tn::ITensorNetwork) IndexType = Any VertexType = vertextype(tn) - tensors = EinExpr{IndexType}[] + tensor_exprs = EinExpr{IndexType}[] tensor_map = Dict{Set{IndexType},VertexType}() sizedict = Dict{IndexType,Int}() for v in vertices(tn) tensor_v = tn[v] inds_v = collect(inds(tensor_v)) - push!(tensors, EinExpr{IndexType}(; head=inds_v)) + push!(tensor_exprs, EinExpr{IndexType}(; head=inds_v)) tensor_map[Set(inds_v)] = key merge!(sizedict, Dict(inds_v .=> size(tensor_v))) end externalinds_tn = collect(externalinds(tn)) - expr = SizedEinExpr(sum(tensors; skip=externalinds_tn), sizedict) + expr = SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), sizedict) return expr, tensor_map end From 864dd597d2c7b1ee21bfe8c7fdb31bdbc1c29fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:20:11 +0100 Subject: [PATCH 26/49] Define `to_contraction_sequence` in the global scope --- .../src/ITensorNetworksEinExprsExt.jl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 450e34f2..b2a7e5ab 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -47,13 +47,12 @@ function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) path, tensor_map = prepare_einexpr(tn) + return to_contraction_sequence(tensor_map, path) +end - function _convert_to_contraction_sequence(subpath) - EinExprs.nargs(subpath) == 0 && return tensor_map[Set(subpath.head)] - return map(_convert_to_contraction_sequence, EinExprs.args(subpath)) - end - - return _convert_to_contraction_sequence(path) +function to_contraction_sequence(tensor_map, path) + EinExprs.nargs(path) == 0 && return tensor_map[Set(path.head)] + return map(_convert_to_contraction_sequence, EinExprs.args(path)) end end From 7c92cc1cacce3b093cbe4bc860e460d6af0152e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:21:13 +0100 Subject: [PATCH 27/49] Apply style suggestion by @mtfishman --- .../src/ITensorNetworksEinExprsExt.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index b2a7e5ab..924a582b 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -16,18 +16,18 @@ function prepare_einexpr(tn::ITensorNetwork) tensor_exprs = EinExpr{IndexType}[] tensor_map = Dict{Set{IndexType},VertexType}() - sizedict = Dict{IndexType,Int}() + inds_dims = Dict{IndexType,Int}() for v in vertices(tn) tensor_v = tn[v] inds_v = collect(inds(tensor_v)) push!(tensor_exprs, EinExpr{IndexType}(; head=inds_v)) tensor_map[Set(inds_v)] = key - merge!(sizedict, Dict(inds_v .=> size(tensor_v))) + merge!(inds_dims, Dict(inds_v .=> size(tensor_v))) end externalinds_tn = collect(externalinds(tn)) - expr = SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), sizedict) + expr = SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), inds_dims) return expr, tensor_map end From cad581d97fda5470b007df16b80e6c5a975b0465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:22:36 +0100 Subject: [PATCH 28/49] Rename `tensor_map` to `tensor_inds_to_vertex` --- .../src/ITensorNetworksEinExprsExt.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 924a582b..f34fc32e 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -15,21 +15,21 @@ function prepare_einexpr(tn::ITensorNetwork) VertexType = vertextype(tn) tensor_exprs = EinExpr{IndexType}[] - tensor_map = Dict{Set{IndexType},VertexType}() + tensor_inds_to_vertex = Dict{Set{IndexType},VertexType}() inds_dims = Dict{IndexType,Int}() for v in vertices(tn) tensor_v = tn[v] inds_v = collect(inds(tensor_v)) push!(tensor_exprs, EinExpr{IndexType}(; head=inds_v)) - tensor_map[Set(inds_v)] = key + tensor_inds_to_vertex[Set(inds_v)] = key merge!(inds_dims, Dict(inds_v .=> size(tensor_v))) end externalinds_tn = collect(externalinds(tn)) expr = SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), inds_dims) - return expr, tensor_map + return expr, tensor_inds_to_vertex end function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) @@ -46,12 +46,12 @@ end function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) - path, tensor_map = prepare_einexpr(tn) - return to_contraction_sequence(tensor_map, path) + path, tensor_inds_to_vertex = prepare_einexpr(tn) + return to_contraction_sequence(tensor_inds_to_vertex, path) end -function to_contraction_sequence(tensor_map, path) - EinExprs.nargs(path) == 0 && return tensor_map[Set(path.head)] +function to_contraction_sequence(tensor_inds_to_vertex, path) + EinExprs.nargs(path) == 0 && return tensor_inds_to_vertex[Set(path.head)] return map(_convert_to_contraction_sequence, EinExprs.args(path)) end From e9b4aea481e11ead181d5d09fd57641d7f663b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:27:11 +0100 Subject: [PATCH 29/49] Apply style suggestion by @mtfishman --- .../src/ITensorNetworksEinExprsExt.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index f34fc32e..40b25898 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -46,13 +46,13 @@ end function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) - path, tensor_inds_to_vertex = prepare_einexpr(tn) - return to_contraction_sequence(tensor_inds_to_vertex, path) + expr, tensor_inds_to_vertex = to_einexpr(tn) + return to_contraction_sequence(tensor_inds_to_vertex, expr) end -function to_contraction_sequence(tensor_inds_to_vertex, path) - EinExprs.nargs(path) == 0 && return tensor_inds_to_vertex[Set(path.head)] - return map(_convert_to_contraction_sequence, EinExprs.args(path)) +function to_contraction_sequence(tensor_inds_to_vertex, expr) + EinExprs.nargs(expr) == 0 && return tensor_inds_to_vertex[Set(expr.head)] + return map(_convert_to_contraction_sequence, EinExprs.args(expr)) end end From 43341eee51db4316f15b6549a0a7f6a2d45a3295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:28:13 +0100 Subject: [PATCH 30/49] Reorder args in `to_contraction_sequence` --- .../src/ITensorNetworksEinExprsExt.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 40b25898..3ebd0fae 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -47,10 +47,10 @@ function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) expr, tensor_inds_to_vertex = to_einexpr(tn) - return to_contraction_sequence(tensor_inds_to_vertex, expr) + return to_contraction_sequence(expr, tensor_inds_to_vertex) end -function to_contraction_sequence(tensor_inds_to_vertex, expr) +function to_contraction_sequence(expr, tensor_inds_to_vertex) EinExprs.nargs(expr) == 0 && return tensor_inds_to_vertex[Set(expr.head)] return map(_convert_to_contraction_sequence, EinExprs.args(expr)) end From d43f7717cbdc7de212b60b3602e1eb31d458f39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:28:50 +0100 Subject: [PATCH 31/49] Rename `prepare_einexpr` to `to_einexpr` --- .../src/ITensorNetworksEinExprsExt.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 3ebd0fae..6a4d92c8 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -10,7 +10,7 @@ using ITensorNetworks: contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr -function prepare_einexpr(tn::ITensorNetwork) +function to_einexpr(tn::ITensorNetwork) IndexType = Any VertexType = vertextype(tn) @@ -33,7 +33,7 @@ function prepare_einexpr(tn::ITensorNetwork) end function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) - expr, _ = prepare_einexpr(tn) + expr, _ = to_einexpr(tn) return einexpr(optimizer, expr) end From f399b78bbc6a5a3213121cba2cdb20f8f59bd6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 6 Feb 2024 16:58:38 +0100 Subject: [PATCH 32/49] Fix call to renamed function --- .../src/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 6a4d92c8..9af3060a 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -52,7 +52,7 @@ end function to_contraction_sequence(expr, tensor_inds_to_vertex) EinExprs.nargs(expr) == 0 && return tensor_inds_to_vertex[Set(expr.head)] - return map(_convert_to_contraction_sequence, EinExprs.args(expr)) + return map(to_contraction_sequence, EinExprs.args(expr)) end end From 6c48bbd55a43c5406536a4163c49e0e55eb4272a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= <15837247+mofeing@users.noreply.github.com> Date: Wed, 7 Feb 2024 02:31:15 +0100 Subject: [PATCH 33/49] Update ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl Co-authored-by: Matt Fishman --- .../src/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 9af3060a..2c37b2b6 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -52,7 +52,7 @@ end function to_contraction_sequence(expr, tensor_inds_to_vertex) EinExprs.nargs(expr) == 0 && return tensor_inds_to_vertex[Set(expr.head)] - return map(to_contraction_sequence, EinExprs.args(expr)) + return map(expr -> to_contraction_sequence(expr, tensor_inds_to_vertex), EinExprs.args(expr)) end end From cc19993c686b60cb7b14b1799f513d77df3a0ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Wed, 7 Feb 2024 16:33:46 +0100 Subject: [PATCH 34/49] Move out vertex mapping from `to_einexpr` to `tensor_inds_to_vertex` --- .../src/ITensorNetworksEinExprsExt.jl | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 2c37b2b6..5767be8e 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -10,30 +10,37 @@ using ITensorNetworks: contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr -function to_einexpr(tn::ITensorNetwork) +function to_einexpr(tn::AbstractITensorNetwork) IndexType = Any - VertexType = vertextype(tn) tensor_exprs = EinExpr{IndexType}[] - tensor_inds_to_vertex = Dict{Set{IndexType},VertexType}() inds_dims = Dict{IndexType,Int}() for v in vertices(tn) tensor_v = tn[v] inds_v = collect(inds(tensor_v)) push!(tensor_exprs, EinExpr{IndexType}(; head=inds_v)) - tensor_inds_to_vertex[Set(inds_v)] = key merge!(inds_dims, Dict(inds_v .=> size(tensor_v))) end externalinds_tn = collect(externalinds(tn)) - expr = SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), inds_dims) + return SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), inds_dims) +end + +function tensor_inds_to_vertex(tn::AbstractITensorNetwork) + mapping = Dict{Set{IndexType},VertexType}() + + for v in vertices(tn) + tensor_v = tn[v] + inds_v = collect(inds(tensor_v)) + mapping[Set(inds_v)] = v + end - return expr, tensor_inds_to_vertex + return mapping end function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) - expr, _ = to_einexpr(tn) + expr = to_einexpr(tn) return einexpr(optimizer, expr) end @@ -46,8 +53,8 @@ end function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() ) - expr, tensor_inds_to_vertex = to_einexpr(tn) - return to_contraction_sequence(expr, tensor_inds_to_vertex) + expr = einexpr(tn; optimizer) + return to_contraction_sequence(expr, tensor_inds_to_vertex(tn)) end function to_contraction_sequence(expr, tensor_inds_to_vertex) From c2d9f9d59d1784308219ac691fac1e3196ffe713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Wed, 7 Feb 2024 21:49:25 +0100 Subject: [PATCH 35/49] Apply suggestions by @JoeyT1994 --- .../src/ITensorNetworksEinExprsExt.jl | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 5767be8e..81984f45 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -1,37 +1,30 @@ module ITensorNetworksEinExprsExt -using ITensors: Index, ITensor, Algorithm +using ITensors: Index, ITensor, Algorithm, noncommoninds using ITensorNetworks: - ITensorNetworks, - ITensorNetwork, - vertextype, - vertex_data, - externalinds, - contraction_sequence + ITensorNetworks, ITensorNetwork, vertextype, vertex_data, contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr -function to_einexpr(tn::AbstractITensorNetwork) +function to_einexpr(ts::Vector{ITensor}) IndexType = Any tensor_exprs = EinExpr{IndexType}[] inds_dims = Dict{IndexType,Int}() - for v in vertices(tn) - tensor_v = tn[v] + 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 = collect(externalinds(tn)) + externalinds_tn = collect(noncommoninds(ts...)) return SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), inds_dims) end -function tensor_inds_to_vertex(tn::AbstractITensorNetwork) +function tensor_inds_to_vertex(ts::Vector{ITensor}) mapping = Dict{Set{IndexType},VertexType}() - for v in vertices(tn) - tensor_v = tn[v] + for (v, tensor_v) in enumerate(ts) inds_v = collect(inds(tensor_v)) mapping[Set(inds_v)] = v end From 1d43c4b978a7d3e7f5c79542c77bba448625cefc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Wed, 7 Feb 2024 21:54:57 +0100 Subject: [PATCH 36/49] Apply suggestion by @JoeyT1994 --- .../src/ITensorNetworksEinExprsExt.jl | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index 81984f45..ab8f9e4c 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -32,27 +32,19 @@ function tensor_inds_to_vertex(ts::Vector{ITensor}) return mapping end -function EinExprs.einexpr(tn::ITensorNetwork; optimizer::EinExprs.Optimizer) - expr = to_einexpr(tn) - return einexpr(optimizer, expr) -end - -function ITensorNetworks.contraction_sequence( - alg::Algorithm"einexpr", tn::Vector{ITensor}; kwargs... -) - return contraction_sequence(alg, ITensorNetwork(tn); kwargs...) -end - function ITensorNetworks.contraction_sequence( - ::Algorithm"einexpr", tn::ITensorNetwork{T}; optimizer=EinExprs.Exhaustive() + ::Algorithm"einexpr", tn::Vector{ITensor}; optimizer=EinExprs.Exhaustive() ) - expr = einexpr(tn; optimizer) - return to_contraction_sequence(expr, tensor_inds_to_vertex(tn)) + expr = to_einexpr(tn) + path = einexpr(expr; kwargs...) + 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)) + return map( + expr -> to_contraction_sequence(expr, tensor_inds_to_vertex), EinExprs.args(expr) + ) end end From 8ed06795238b06d1093741c260aa2cc5964bfeb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= <15837247+mofeing@users.noreply.github.com> Date: Thu, 8 Feb 2024 00:30:11 +0100 Subject: [PATCH 37/49] Update ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl Co-authored-by: Matt Fishman --- .../src/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl index ab8f9e4c..cf3d0866 100644 --- a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl @@ -17,7 +17,7 @@ function to_einexpr(ts::Vector{ITensor}) merge!(inds_dims, Dict(inds_v .=> size(tensor_v))) end - externalinds_tn = collect(noncommoninds(ts...)) + externalinds_tn = reduce(noncommoninds, ts) return SizedEinExpr(sum(tensor_exprs; skip=externalinds_tn), inds_dims) end From f07754d3d741df67fc4bdad4c856eb278453353a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 8 Feb 2024 00:32:22 +0100 Subject: [PATCH 38/49] Refactor test as suggested by @mtfishman --- test/test_contraction_sequence.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test/test_contraction_sequence.jl b/test/test_contraction_sequence.jl index 2184db22..1271bfb5 100644 --- a/test/test_contraction_sequence.jl +++ b/test/test_contraction_sequence.jl @@ -30,13 +30,12 @@ ITensors.disable_warn_order() res_einexprs_greedy = contract(tn; sequence=seq_einexprs_exhaustive)[] seq_einexprs_kahypar = contraction_sequence(tn; alg="einexpr", optimizer=HyPar()) res_einexprs_kahypar = contract(tn; sequence=seq_einexprs_kahypar)[] - @test res_optimal ≈ - res_greedy ≈ - res_tree_sa ≈ - res_sa_bipartite ≈ - res_einexprs_exhaustive ≈ - res_einexprs_greedy ≈ - res_einexprs_kahypar + @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 + @test res_einexprs_kahypar ≈ res_optimal if !Sys.iswindows() # KaHyPar doesn't work on Windows From 8c8328dc410d1620ddcfb3471955b8ba8f62a255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Thu, 8 Feb 2024 21:54:53 +0100 Subject: [PATCH 39/49] Duplicate weakdeps in extras to support Julia <1.9 --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 0ee65a49..e53a4b80 100644 --- a/Project.toml +++ b/Project.toml @@ -67,6 +67,7 @@ julia = "1.7" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5" [targets] test = ["Test"] From acd7a03384b73ff880057db4808924ffa68412ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Mon, 12 Feb 2024 15:26:59 +0000 Subject: [PATCH 40/49] Try fix for package extension path --- .../{src => }/ITensorNetworksEinExprsExt.jl | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ext/ITensorNetworksEinExprsExt/{src => }/ITensorNetworksEinExprsExt.jl (100%) diff --git a/ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl similarity index 100% rename from ext/ITensorNetworksEinExprsExt/src/ITensorNetworksEinExprsExt.jl rename to ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl From 7e533f2ff30bee5f78aca107374eedd0abc607fa Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Mon, 12 Feb 2024 11:29:31 -0500 Subject: [PATCH 41/49] Bring `@Algorithm_str` into the extension namespace --- ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl index cf3d0866..01b933b9 100644 --- a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl @@ -1,6 +1,6 @@ module ITensorNetworksEinExprsExt -using ITensors: Index, ITensor, Algorithm, noncommoninds +using ITensors: Index, ITensor, Algorithm, @Algorithm_str, noncommoninds using ITensorNetworks: ITensorNetworks, ITensorNetwork, vertextype, vertex_data, contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr From 739e75296060dca6a8d10adb5739634763d46426 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Mon, 12 Feb 2024 11:39:53 -0500 Subject: [PATCH 42/49] Remove `Algorithm` from extension namespace --- ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl index 01b933b9..06d34763 100644 --- a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl @@ -1,6 +1,6 @@ module ITensorNetworksEinExprsExt -using ITensors: Index, ITensor, Algorithm, @Algorithm_str, noncommoninds +using ITensors: Index, ITensor, @Algorithm_str, noncommoninds using ITensorNetworks: ITensorNetworks, ITensorNetwork, vertextype, vertex_data, contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr From 6acf91ab7a0ff42826f8cf5976587b4017bea043 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Mon, 12 Feb 2024 13:03:21 -0500 Subject: [PATCH 43/49] Bring `inds` into the extension namespace --- ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl index 06d34763..cdca6c3d 100644 --- a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl @@ -1,6 +1,6 @@ module ITensorNetworksEinExprsExt -using ITensors: Index, ITensor, @Algorithm_str, noncommoninds +using ITensors: Index, ITensor, @Algorithm_str, inds, noncommoninds using ITensorNetworks: ITensorNetworks, ITensorNetwork, vertextype, vertex_data, contraction_sequence using EinExprs: EinExprs, EinExpr, einexpr, SizedEinExpr From bd132eb6bf2ae68e1a28378b7aa274f5a5c2a314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 13 Feb 2024 13:10:53 +0000 Subject: [PATCH 44/49] Fix typo --- ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl index cdca6c3d..ebddb989 100644 --- a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl @@ -36,7 +36,7 @@ function ITensorNetworks.contraction_sequence( ::Algorithm"einexpr", tn::Vector{ITensor}; optimizer=EinExprs.Exhaustive() ) expr = to_einexpr(tn) - path = einexpr(expr; kwargs...) + path = einexpr(optimizer, expr) return to_contraction_sequence(path, tensor_inds_to_vertex(tn)) end From d965b14e5610d8ea1616ea48db574a02aa273f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 13 Feb 2024 15:32:25 +0000 Subject: [PATCH 45/49] Fix KaHyPar tests with EinExprs --- test/test_contraction_sequence.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_contraction_sequence.jl b/test/test_contraction_sequence.jl index 1271bfb5..5470dabe 100644 --- a/test/test_contraction_sequence.jl +++ b/test/test_contraction_sequence.jl @@ -28,14 +28,11 @@ ITensors.disable_warn_order() 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)[] - seq_einexprs_kahypar = contraction_sequence(tn; alg="einexpr", optimizer=HyPar()) - res_einexprs_kahypar = contract(tn; sequence=seq_einexprs_kahypar)[] @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 - @test res_einexprs_kahypar ≈ res_optimal if !Sys.iswindows() # KaHyPar doesn't work on Windows @@ -46,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 From 47fddf616f93581b8a3315f96ca380979e03ede0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= <15837247+mofeing@users.noreply.github.com> Date: Tue, 13 Feb 2024 15:40:03 +0000 Subject: [PATCH 46/49] Fix EinExprs version Co-authored-by: Matt Fishman --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e53a4b80..04096e54 100644 --- a/Project.toml +++ b/Project.toml @@ -46,7 +46,7 @@ DataStructures = "0.18" Dictionaries = "0.3.15" Distributions = "0.25.86" DocStringExtensions = "0.8, 0.9" -EinExprs = "0.6" +EinExprs = "0.6.4" Graphs = "1.8" GraphsFlows = "0.1.1" ITensors = "0.3.23" From b73ae82abf8d53c1c13cd70c291908c3995fb1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 13 Feb 2024 15:42:55 +0000 Subject: [PATCH 47/49] Fix missing type definition --- ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl index ebddb989..516469da 100644 --- a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl @@ -22,6 +22,8 @@ function to_einexpr(ts::Vector{ITensor}) end function tensor_inds_to_vertex(ts::Vector{ITensor}) + IndexType = Any + mapping = Dict{Set{IndexType},VertexType}() for (v, tensor_v) in enumerate(ts) From 4c22571a5fa551eca185439c54f6211efa76c23c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 13 Feb 2024 17:04:31 +0000 Subject: [PATCH 48/49] Fix missing type definition --- ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl index 516469da..73d7a847 100644 --- a/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl +++ b/ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl @@ -23,6 +23,7 @@ end function tensor_inds_to_vertex(ts::Vector{ITensor}) IndexType = Any + VertexType = Int mapping = Dict{Set{IndexType},VertexType}() From 3ec86f2c3cf6b2a6892561b86eff3155397b41b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= Date: Tue, 13 Feb 2024 18:34:37 +0000 Subject: [PATCH 49/49] Fix package extensions on Julia <1.9 --- Project.toml | 3 ++- src/ITensorNetworks.jl | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 04096e54..e4375c24 100644 --- a/Project.toml +++ b/Project.toml @@ -21,6 +21,7 @@ KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19" Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0" +PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Requires = "ae029012-a4dd-5104-9daa-d747884805df" SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" @@ -66,8 +67,8 @@ TupleTools = "1.4" julia = "1.7" [extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] test = ["Test"] diff --git a/src/ITensorNetworks.jl b/src/ITensorNetworks.jl index 24fea311..18b4c88e 100644 --- a/src/ITensorNetworks.jl +++ b/src/ITensorNetworks.jl @@ -22,6 +22,7 @@ using LinearAlgebra using NamedGraphs using Observers using Observers.DataFrames: select! +using PackageExtensionCompat using Printf using Requires using SimpleTraits @@ -130,6 +131,7 @@ include(joinpath("treetensornetworks", "solvers", "tree_sweeping.jl")) include("exports.jl") function __init__() + @require_extensions @require OMEinsumContractionOrders = "6f22d1fd-8eed-4bb7-9776-e7d684900715" include( joinpath("requires", "omeinsumcontractionorders.jl") )