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

Optimized tensor network contraction using einexpr when specifying output indices #59

Open
bartdv123 opened this issue May 25, 2024 · 3 comments

Comments

@bartdv123
Copy link

bartdv123 commented May 25, 2024

Hi there!

I'm been using Tenet the past year during my master's thesis and I stumbled upon a problem when trying to optimize a partial tensor network contraction, instead of a full contraction. The documentation states in einexpr.jl to specify the open indices in the path. But even when specifying the open indices in the einexpr with the outptus = inds(....) - keyword Tenet.contract(network, path=cpath) contracts the whole network?

Example:

output_inds =[Symbol("7"), :v2, Symbol("12"), Symbol("9"), :v3, :v1, Symbol("8"), :v5, Symbol("14"), :v4, Symbol("15")]

cpath = einexpr(transformed_2_mps_network, outputs = output_inds)
mps_network = Tenet.contract(mps_network, path=cpath)
println(typeof(mps_network)) --> return not a tensor network but a single tensor, so contracts all indices and doesn't leave the specified indices open?

See this image for definition of what the indices mean:
image

@bartdv123
Copy link
Author

Found a work around for my specific use case

@jofrevalles
Copy link
Member

Hi, @bartdv123! Thanks for noticing this potential issue.

I managed to find a reproducing example of this behavior:

julia> using Tenet

julia> using EinExprs

julia> tn = TensorNetwork([
           Tensor(rand(2, 2, 2), [:i, :j, :k]),
           Tensor(rand(2, 2), [:j, :l]),
           Tensor(rand(2, 2, 2, 2), [:l, :k, :m, :n]),
           Tensor(rand(2, 2, 2), [:n, :o, :p])])
TensorNetwork (#tensors=4, #inds=8)

julia> output_inds = inds(tn, :open)
4-element Vector{Symbol}:
 :m
 :o
 :p
 :i

julia> contract_all = einexpr(tn)
SizedEinExpr{Symbol}(EinExpr{Symbol}([:i, :m, :o, :p], EinExpr{Symbol}[EinExpr{Symbol}([:n, :o, :p], EinExpr{Symbol}[]), EinExpr{Symbol}([:i, :m, :n], EinExpr{Symbol}[EinExpr{Symbol}([:i, :k, :l], EinExpr{Symbol}[EinExpr{Symbol}([:i, :j, :k], EinExpr{Symbol}[]), EinExpr{Symbol}([:j, :l], EinExpr{Symbol}[])]), EinExpr{Symbol}([:l, :k, :m, :n], EinExpr{Symbol}[])])]), Dict(:l => 2, :m => 2, :o => 2, :p => 2, :n => 2, :j => 2, :k => 2, :i => 2))

julia> t1 = contract(tn, path=cpath) |> inds
4-element Vector{Symbol}:
 :o
 :p
 :i
 :m

julia> cpath = einexpr(tn, outputs=[:i, :p])
SizedEinExpr{Symbol}(EinExpr{Symbol}([:i, :m, :o, :p], EinExpr{Symbol}[EinExpr{Symbol}([:n, :o, :p], EinExpr{Symbol}[]), EinExpr{Symbol}([:i, :m, :n], EinExpr{Symbol}[EinExpr{Symbol}([:i, :k, :l], EinExpr{Symbol}[EinExpr{Symbol}([:i, :j, :k], EinExpr{Symbol}[]), EinExpr{Symbol}([:j, :l], EinExpr{Symbol}[])]), EinExpr{Symbol}([:l, :k, :m, :n], EinExpr{Symbol}[])])]), Dict(:l => 2, :m => 2, :o => 2, :p => 2, :n => 2, :j => 2, :k => 2, :i => 2))

julia> t2 = contract(tn, path=cpath) |> inds
4-element Vector{Symbol}:
 :o
 :p
 :i
 :m

@mofeing It seems that Tenet.einexpr is not finding the proper EinExpr that has the correct outputs indices.

@jofrevalles jofrevalles reopened this May 27, 2024
@mofeing
Copy link
Member

mofeing commented May 28, 2024

hi @bartdv123
nice to see a new user!

The documentation states in einexpr.jl to specify the open indices in the path. But even when specifying the open indices in the einexpr with the outptus = inds(....) - keyword Tenet.contract(network, path=cpath) contracts the whole network?

mmm nope. what it does is it's just selecting and contracting the tensors involved in the EinExpr. the resulting tensor is the result of contracting the sub-Tensor Network.

It seems that Tenet.einexpr is not finding the proper EinExpr that has the correct outputs indices.

@jofrevalles Well, the EinExpr is mostly correct: It just needs to modify the head in the top EinExpr.

I'm moving the issue to EinExprs.jl because that is where the issue has its origin.

@mofeing mofeing transferred this issue from bsc-quantic/Tenet.jl May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants