scalar() on a zero-rank tensor on GPU

hi, I stumbled upon yet another (maybe) quirky behavior of GPU tensors,
I wanted to compute the entanglement entropy of an MPS on GPU - in order to avoid scalar indexing issues, I was able to do it as (say S here is a diag_itensor of singular values)

using ITensors,  Metal

    S = mtl(diag_itensor([0.9,0.05,0.05], Index(3), Index(3)))
    S2 = S.^2
    SvN = - S2 * log.(S2) # this gives a rank-0 tensor, so I'd normally 
    SvN = scalar(SvN)  # but this gives a scalar ( :) ) indexing error 

now, of course I could scalar(NDTensors.cpu(SvN)), but I wonder if it’s the only (or at least the intended) way to do it (since I’d like my code to be architecture-agnostic, putting an extra cpu() there seems a bit overkill… )

As a matter of fact, I found out that one can cheat a bit and calculate a sum() of the GPU 0-dim tensor, which (albeit probably a bit slower) returns me directly a scalar without needing to go through the NDTensors.cpu() (I wonder if it simply does it under the hood…) - is this the best way to go about it?
thanks!

Hello,

I believe that this is actually a bug in NDTensors. I implemented the correct version and it is much more performant than the scalar function

julia> @time SvN[]
  0.000015 seconds (4 allocations: 208 bytes)
julia> @time scalar(SvN)
  0.000804 seconds (72 allocations: 1.688 KiB)

I will make a PR for this now. Thanks!

Karl

awesome, thanks Karl!