From 00f5d711029cccec70d2267c4ef5436190abb3aa Mon Sep 17 00:00:00 2001 From: lkdvos Date: Thu, 22 Jan 2026 10:04:51 -0500 Subject: [PATCH 1/4] `convert(TensorMap, t)` retains storagetype --- src/tensors/tensor.jl | 3 ++- test/cuda/tensors.jl | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/tensors/tensor.jl b/src/tensors/tensor.jl index 300654632..526f4e489 100644 --- a/src/tensors/tensor.jl +++ b/src/tensors/tensor.jl @@ -532,7 +532,8 @@ end #--------------------------- Base.convert(::Type{TensorMap}, t::TensorMap) = t function Base.convert(::Type{TensorMap}, t::AbstractTensorMap) - return copy!(TensorMap{scalartype(t)}(undef, space(t)), t) + A = storagetype(t) + return copy!(TensorMapWithStorage{scalartype(A), A}(undef, space(t)), t) end function Base.convert(::Type{TensorMapWithStorage{T, A}}, t::TensorMap) where {T, A} diff --git a/test/cuda/tensors.jl b/test/cuda/tensors.jl index e495eef08..4a82e58cb 100644 --- a/test/cuda/tensors.jl +++ b/test/cuda/tensors.jl @@ -258,14 +258,14 @@ for V in spacelist end end end - @timedtestset "Tensor conversion" begin # TODO adjoint conversion methods don't work yet + @timedtestset "Tensor conversion" begin W = V1 ⊗ V2 t = @constinferred CUDA.randn(W ← W) - #@test typeof(convert(TensorMap, t')) == typeof(t) # TODO Adjoint not supported yet + @test typeof(convert(TensorMap, t')) == typeof(t) tc = complex(t) @test convert(typeof(tc), t) == tc @test typeof(convert(typeof(tc), t)) == typeof(tc) - # @test typeof(convert(typeof(tc), t')) == typeof(tc) # TODO Adjoint not supported yet + @test typeof(convert(typeof(tc), t')) == typeof(tc) @test Base.promote_typeof(t, tc) == typeof(tc) @test Base.promote_typeof(tc, t) == typeof(tc + t) end From ae5b569a4415414b2d56489d40080f66f754e367 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 22 Jan 2026 10:17:07 -0500 Subject: [PATCH 2/4] A little more adjoint TLC --- src/tensors/adjoint.jl | 2 ++ src/tensors/linalg.jl | 2 +- test/cuda/tensors.jl | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/tensors/adjoint.jl b/src/tensors/adjoint.jl index ca484e77b..7c2262aca 100644 --- a/src/tensors/adjoint.jl +++ b/src/tensors/adjoint.jl @@ -50,6 +50,8 @@ Base.@propagate_inbounds function subblock(t::AdjointTensorMap, (f₁, f₂)::Tu return permutedims(conj(data), (domainind(tp)..., codomainind(tp)...)) end +to_cpu(t::AdjointTensorMap) = AdjointTensorMap(convert(TensorMapWithStorage{scalartype(t), similarstoragetype(scalartype(t))}, parent(t))) + # Show #------ function Base.showarg(io::IO, t::AdjointTensorMap, toplevel::Bool) diff --git a/src/tensors/linalg.jl b/src/tensors/linalg.jl index d174bef71..28a7df4d4 100644 --- a/src/tensors/linalg.jl +++ b/src/tensors/linalg.jl @@ -201,7 +201,7 @@ LinearAlgebra.isdiag(t::AbstractTensorMap) = all(LinearAlgebra.isdiag ∘ last, function Base.copy!(tdst::AbstractTensorMap, tsrc::AbstractTensorMap) space(tdst) == space(tsrc) || throw(SpaceMismatch("$(space(tdst)) ≠ $(space(tsrc))")) for ((c, bdst), (_, bsrc)) in zip(blocks(tdst), blocks(tsrc)) - copy!(bdst, bsrc) + bdst .= bsrc end return tdst end diff --git a/test/cuda/tensors.jl b/test/cuda/tensors.jl index 4a82e58cb..a31015a4a 100644 --- a/test/cuda/tensors.jl +++ b/test/cuda/tensors.jl @@ -261,7 +261,8 @@ for V in spacelist @timedtestset "Tensor conversion" begin W = V1 ⊗ V2 t = @constinferred CUDA.randn(W ← W) - @test typeof(convert(TensorMap, t')) == typeof(t) + @test typeof(convert(typeof(t), t')) == typeof(t) + @test typeof(TensorKit.to_cpu(t')) == typeof(TensorKit.to_cpu(t)') tc = complex(t) @test convert(typeof(tc), t) == tc @test typeof(convert(typeof(tc), t)) == typeof(tc) From 970dcfa2b0af60a01841ae618670cf6c2b6a2b53 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 22 Jan 2026 10:18:40 -0500 Subject: [PATCH 3/4] Restore copy --- src/tensors/linalg.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tensors/linalg.jl b/src/tensors/linalg.jl index 28a7df4d4..d174bef71 100644 --- a/src/tensors/linalg.jl +++ b/src/tensors/linalg.jl @@ -201,7 +201,7 @@ LinearAlgebra.isdiag(t::AbstractTensorMap) = all(LinearAlgebra.isdiag ∘ last, function Base.copy!(tdst::AbstractTensorMap, tsrc::AbstractTensorMap) space(tdst) == space(tsrc) || throw(SpaceMismatch("$(space(tdst)) ≠ $(space(tsrc))")) for ((c, bdst), (_, bsrc)) in zip(blocks(tdst), blocks(tsrc)) - bdst .= bsrc + copy!(bdst, bsrc) end return tdst end From 14ef1110e41b15b50300257eae2e0a978b88f54f Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 22 Jan 2026 16:21:00 +0100 Subject: [PATCH 4/4] Update src/tensors/adjoint.jl Co-authored-by: Lukas Devos --- src/tensors/adjoint.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tensors/adjoint.jl b/src/tensors/adjoint.jl index 7c2262aca..dfc1a4471 100644 --- a/src/tensors/adjoint.jl +++ b/src/tensors/adjoint.jl @@ -50,7 +50,7 @@ Base.@propagate_inbounds function subblock(t::AdjointTensorMap, (f₁, f₂)::Tu return permutedims(conj(data), (domainind(tp)..., codomainind(tp)...)) end -to_cpu(t::AdjointTensorMap) = AdjointTensorMap(convert(TensorMapWithStorage{scalartype(t), similarstoragetype(scalartype(t))}, parent(t))) +to_cpu(t::AdjointTensorMap) = adjoint(to_cpu(adjoint(t))) # Show #------