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

`flip(::DiagonalTensorMap, is)` returns `TensorMap`

Open lkdvos opened this issue 10 months ago • 6 comments

Currently, flipping the legs of a diagonal tensor loses that the diagonal information:

julia> d = DiagonalTensorMap{Float64}(undef, ℂ^3)
DiagonalTensorMap(ℂ^3 ← ℂ^3):
 0.0   ⋅                 ⋅ 
  ⋅   6.798626232e-314   ⋅ 
  ⋅    ⋅                0.0


julia> flip(d, 1)
TensorMap((ℂ^3)' ← ℂ^3):
 0.0  0.0               0.0
 0.0  6.798626232e-314  0.0
 0.0  0.0               0.0

lkdvos avatar Apr 15 '25 18:04 lkdvos

Diagonal only exists if domain == codomain, so the problem is that only flip(d, (1,2)) can preserve the DiagonalTensorMap structure.

Jutho avatar Apr 15 '25 21:04 Jutho

transpose(d) could be used to flip the domain and codomain, but this might not have the same semantics in terms of preserving contractions.

Jutho avatar Apr 15 '25 21:04 Jutho

Oh, that's also true... I think we can work around this in this particular case by doing the svd in the "other direction", but this is definitely a bit of an annoying problem. We could consider checking that flip(d, (1, 2)) contains either zero or both indices, and throw an error to tell the user to explicitly convert to TensorMap if they want a different result, but that also feels very opinionated and clunky...

lkdvos avatar Apr 15 '25 21:04 lkdvos

How about an additional flip!(dst, src, I; inv) method?

lkdvos avatar Apr 15 '25 21:04 lkdvos

Oh, that's also true... I think we can work around this in this particular case by doing the svd in the "other direction", but this is definitely a bit of an annoying problem. We could consider checking that flip(d, (1, 2)) contains either zero or both indices, and throw an error to tell the user to explicitly convert to TensorMap if they want a different result, but that also feels very opinionated and clunky...

We could also support DiagonalTensorMap with isomorphic domain and codomain; I don't think there is a strong argument for having domain == codomain.

Jutho avatar Apr 15 '25 23:04 Jutho

Adding on to this: transpose(::DiagonalTensorMap) without specifying indices can be special-cased to output a DiagonalTensorMap.

lkdvos avatar Dec 15 '25 14:12 lkdvos