@@ -138,7 +138,51 @@ Base.@constprop :aggressive function TK.removeunit(
138138 return tdst
139139end
140140
141+ # twist!, twist
142+ # -------------
143+ # the in-place `twist!(t, inds)` twists each block in place
141144function TK. twist! (t:: AbstractBlockTensorMap , is; inv:: Bool = false )
142145 foreach (x -> twist! (x, is; inv), nonzero_values (t))
143146 return t
144147end
148+
149+ # `twist` and `twist!(tdst, tsrc, inds, α, β)` unwrap adjoints and then call `_twist!`
150+
151+ # unwrap just `tsrc`, see `_braid_block!`
152+ # unlike in `_braid!`, `twistmask` refers to the legs of `tsrc`, so it's relabeled along with it
153+ @propagate_inbounds function _twist_block! (
154+ tdst, tsrc, p, conjsrc, twistmask, inv, α, β, backend, allocator
155+ )
156+ tsrc′, p′, twistmask′, conjsrc′ = TK. _unwrap_source (tsrc, p, twistmask, conjsrc)
157+ return TK. _twist! (tdst, tsrc′, p′, conjsrc′, twistmask′, inv, α, β, backend, allocator)
158+ end
159+
160+ @propagate_inbounds function TK. _twist! (
161+ tdst:: BlockTensorMap , tsrc:: BlockTensorMap , p:: Index2Tuple , conjsrc:: Bool ,
162+ twistmask:: Tuple{Vararg{Bool}} , inv:: Bool , α, β, backend, allocator
163+ )
164+ @boundscheck TK. spacecheck_transform (TK. permute, space (tdst), space (tsrc), p, conjsrc)
165+ dstdata = parent (tdst)
166+ srcdata = permutedims (StridedView (parent (tsrc)), (p[1 ]. .. , p[2 ]. .. ))
167+ @inbounds for I in eachindex (dstdata, srcdata)
168+ dstdata[I] = _twist_block! (
169+ dstdata[I], srcdata[I], p, conjsrc, twistmask, inv, α, β, backend, allocator
170+ )
171+ end
172+ return tdst
173+ end
174+ @propagate_inbounds function TK. _twist! (
175+ tdst:: AbstractBlockTensorMap , tsrc:: AbstractBlockTensorMap , p:: Index2Tuple , conjsrc:: Bool ,
176+ twistmask:: Tuple{Vararg{Bool}} , inv:: Bool , α, β, backend, allocator
177+ )
178+ @boundscheck TK. spacecheck_transform (TK. permute, space (tdst), space (tsrc), p, conjsrc)
179+ p_lin = (p[1 ]. .. , p[2 ]. .. )
180+ _scale_untouched! (tdst, tsrc, β, p_lin)
181+ @inbounds for (I, v) in nonzero_pairs (tsrc)
182+ I′ = CartesianIndex (TT. getindices (I. I, p_lin))
183+ _addblock! (tdst, I′, β, allocator) do c, γ
184+ _twist_block! (c, v, p, conjsrc, twistmask, inv, α, γ, backend, allocator)
185+ end
186+ end
187+ return tdst
188+ end
0 commit comments