-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contraction path optimization with EinExprs (#120)
- Loading branch information
Showing
5 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
ext/ITensorNetworksEinExprsExt/ITensorNetworksEinExprsExt.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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 | ||
VertexType = Int | ||
|
||
mapping = Dict{Set{IndexType},VertexType}() | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters