Tullio.jl icon indicating copy to clipboard operation
Tullio.jl copied to clipboard

syntax error when array and index shares the same symbol

Open ho-oto opened this issue 5 years ago • 1 comments

This is the minimal example of the error.

a, b = randn(ComplexF64, 10, 10), randn(ComplexF64, 10, 10)
@tullio # (verbose = false, fastmath = true, threads = true, grad = :Base, avx = true, cuda = 256, tensor = true)
@tullio out[a, b] := a[a, c] * b[c, b] # works
@tullio out[A, B] := a[A, C] * conj(b[C, B]) # works
@tullio out[a, b] := a[a, c] * conj(b[c, b]) # syntax error

When @tullio replaced by @tensor in TensorOperations.jl, the last expression works well. Is this a bug of the @tullio macro?

ho-oto avatar Oct 07 '20 11:10 ho-oto

Right, this won't work. The immediate error is that it tries to define a function rhs(a,b,a,c,b) = a[a, c] * conj(b[c, b]) (to infer the element type), but later on it will write for b in axes(b,2) ... acc += a[a, c] * conj(b[c, b]; ... which also won't work.

It wouldn't be all that easy to check & correct for this, as you are allowed to write things like this:

@tullio out[a, b] := exp(im * pi * a /  size(A,1)) * A[a, c] * B[c, b]

TensorOperations doesn't allow this, and throws away the index letters. In fact you can write @tensor out[1,2] := a[1,3] * conj(b[3,2]).

One simple work-around is that you can add primes to indices, ascii i' is normalised to unicode i′ (when inside [...]), so this works:

@tullio out[a', b'] := a[a', c] * conj(b[c, b'])

mcabbott avatar Oct 07 '20 11:10 mcabbott