From bbeb70ede5dcc38475fc9b01d688a045403338fd Mon Sep 17 00:00:00 2001 From: Boris De Vos Date: Fri, 27 Nov 2026 12:45:09 +0100 Subject: [PATCH] blockwise _twist! to support twist and non-inplace twist! --- src/tensors/indexmanipulations.jl | 44 +++++++++++++++++++++++++++++++ test/linalg/indexmanipulations.jl | 19 +++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/tensors/indexmanipulations.jl b/src/tensors/indexmanipulations.jl index 65d5afd..2b801c3 100644 --- a/src/tensors/indexmanipulations.jl +++ b/src/tensors/indexmanipulations.jl @@ -138,7 +138,51 @@ Base.@constprop :aggressive function TK.removeunit( return tdst end +# twist!, twist +# ------------- +# the in-place `twist!(t, inds)` twists each block in place function TK.twist!(t::AbstractBlockTensorMap, is; inv::Bool = false) foreach(x -> twist!(x, is; inv), nonzero_values(t)) return t end + +# `twist` and `twist!(tdst, tsrc, inds, α, β)` unwrap adjoints and then call `_twist!` + +# unwrap just `tsrc`, see `_braid_block!` +# unlike in `_braid!`, `twistmask` refers to the legs of `tsrc`, so it's relabeled along with it +@propagate_inbounds function _twist_block!( + tdst, tsrc, p, conjsrc, twistmask, inv, α, β, backend, allocator + ) + tsrc′, p′, twistmask′, conjsrc′ = TK._unwrap_source(tsrc, p, twistmask, conjsrc) + return TK._twist!(tdst, tsrc′, p′, conjsrc′, twistmask′, inv, α, β, backend, allocator) +end + +@propagate_inbounds function TK._twist!( + tdst::BlockTensorMap, tsrc::BlockTensorMap, p::Index2Tuple, conjsrc::Bool, + twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator + ) + @boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc) + dstdata = parent(tdst) + srcdata = permutedims(StridedView(parent(tsrc)), (p[1]..., p[2]...)) + @inbounds for I in eachindex(dstdata, srcdata) + dstdata[I] = _twist_block!( + dstdata[I], srcdata[I], p, conjsrc, twistmask, inv, α, β, backend, allocator + ) + end + return tdst +end +@propagate_inbounds function TK._twist!( + tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, p::Index2Tuple, conjsrc::Bool, + twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator + ) + @boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc) + p_lin = (p[1]..., p[2]...) + _scale_untouched!(tdst, tsrc, β, p_lin) + @inbounds for (I, v) in nonzero_pairs(tsrc) + I′ = CartesianIndex(TT.getindices(I.I, p_lin)) + _addblock!(tdst, I′, β, allocator) do c, γ + _twist_block!(c, v, p, conjsrc, twistmask, inv, α, γ, backend, allocator) + end + end + return tdst +end diff --git a/test/linalg/indexmanipulations.jl b/test/linalg/indexmanipulations.jl index 45e01eb..1e253f3 100644 --- a/test/linalg/indexmanipulations.jl +++ b/test/linalg/indexmanipulations.jl @@ -41,6 +41,25 @@ transform!(f, C, A, p, α, β) = f === braid! ? f(C, A, p, (1, 2, 3, 4, 5), α, @test dense(transform!(f, C, A, p, One(), Zero())) ≈ transform!(f, zerovector(dense(C)), Ad, p, One(), Zero()) end +# `twist` and `twist!(tdst, tsrc, ...)` go through `_twist!`, unlike the in-place `twist!(t, inds)` +@testset "twist ($(sectortype(V[1])), C sparse = $Csparse, A sparse = $Asparse, adjoint = $adj)" for V in (Vtr, Vf), + Csparse in (false, true), Asparse in (false, true), adj in (false, true) + T = ComplexF64 + W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5] + A = maketensor(Asparse, T, adj ? W' : W) + A = adj ? A' : A + Ad = adj ? dense(A')' : dense(A) + inds = (1, 4) + for β in (Zero(), One(), randn(T)), inv in (false, true) + α = randn(T) + C = maketensor(Csparse, T, space(A)) + Cd = twist!(dense(C), Ad, inds, α, β; inv) + @test twist!(C, A, inds, α, β; inv) === C + @test dense(C) ≈ Cd + end + Csparse || @test dense(twist(A, inds)) ≈ twist(Ad, inds) +end + @testset "permute of adjoint ($(sectortype(V[1])), sparse = $sparse)" for V in (Vtr, Vf), sparse in (false, true) W = V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5] t = maketensor(sparse, ComplexF64, W)