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

[NDTensors] [BUG] A * B contraction of NDTensors #1201

Closed
ArtemStrashko opened this issue Sep 21, 2023 · 4 comments
Closed

[NDTensors] [BUG] A * B contraction of NDTensors #1201

ArtemStrashko opened this issue Sep 21, 2023 · 4 comments
Labels
bug Something isn't working NDTensors Requires changes to the NDTensors.jl library.

Comments

@ArtemStrashko
Copy link

Description of bug

A * B syntax where A and B are NDTensors leads to standard matrix multiplication, which in many cases has nothing to do with tensor contraction. @brian-dellabetta

Minimal code demonstrating the bug or unexpected behavior

Minimal runnable code

index1 = Index(2)
index2 = Index(3)
A = randomTensor((index1, index2))
B = randomTensor((index2, index1))
C = randomTensor((index1, index2))
A * B # leads to a matrix (tensors with two identical indices) rather than to a scalar
A * C # errors because of not matching dimensions

Expected output or behavior

Expected tensor contraction as for ITensors. Operations above should return a 0-dim tensor.

Possible workaround

To resolve this issue, we commented out this function and added the following code:

Workaround

using ITensors
import Base: *
function (a::Tensor * b::Tensor)
  labels_a, labels_b = ITensors.compute_contraction_labels(inds(a), inds(b))
  return contract(a, labels_a, b, labels_b)
end

Ideally we wanted to add this code inside generic_tensor_operations, but we were not able to do so because compute_contraction_labels is defined inside ITensors and not exported to NDTensors.

Version information

  • Output from versioninfo():
julia> versioninfo()
Julia Version 1.9.3
Commit bed2cd540a1 (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
  Threads: 2 on 8 virtual cores
Environment:
  JULIA_PKG_USE_CLI_GIT = true

Click
  • Output from using Pkg; Pkg.status("ITensors"):
julia> using Pkg; Pkg.status("ITensors")
Status `~/.julia/environments/v1.9/Project.toml`
  [9136182c] ITensors v0.3.43 `~/Documents/ITensors.jl`
@ArtemStrashko ArtemStrashko added bug Something isn't working NDTensors Requires changes to the NDTensors.jl library. labels Sep 21, 2023
@mtfishman
Copy link
Member

We never intended * to work as tensor contraction for NDTensors.Tensors, it is meant to be matrix multiplication. NDTensors.Tensors are meant to be agnostic about whether or not the indices have identifiers (like the Index). The syntax for performing tensor contraction of NDTensors.Tensors is contract(a, labels_a, b, labels_b). More broadly, NDTensors.Tensors are meant to have the same syntax and interface as other AbstractArrays.

@ArtemStrashko
Copy link
Author

OK, I see, thanks!

@mtfishman
Copy link
Member

Something that would be nice would be to allow more general labels. Currently we use the ncon convention of just positive and negative integers (https://arxiv.org/abs/1402.0939). We could still use that internally but for high level interfaces like contract we could allow any objects that define equality to be labels (and compile that down to the ncon label convention). That would allow someone to write:

contract(A, inds(A), B, inds(B))

which would then work if inds(A) and inds(B) had modes with identifiers, like Index.

@emstoudenmire
Copy link
Collaborator

It's a good idea. I just made an issue so we can remember to add this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working NDTensors Requires changes to the NDTensors.jl library.
Projects
None yet
Development

No branches or pull requests

3 participants