Skip to content

Commit 866ee15

Browse files
lkdvosclaude
andcommitted
Handle adjoint operands of twist! and twist through the twister
Adjoint destinations and sources are unwrapped as in the other index manipulations: the twist mask of an adjoint destination is relabeled onto the legs of its parent with the inverse twist, and `treetwister` takes a `conjsrc` flag, with the destination space and the permutation of the source legs following from the source space. An in-place twist of an adjoint therefore uses the same transformer as the equivalent twist of its parent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 93baf98 commit 866ee15

6 files changed

Lines changed: 61 additions & 19 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
4545
- Recoupling matrices are stored such that they can leverage BLAS, also for mixed complex tensor with real recoupling coefficients.
4646
- Constructing a `FusionTreeBlock` only takes the fusion product of the shorter side, and `fusionblocks` groups the cached fusion trees of the space instead of constructing a block for every combination of sectors (internal)
4747
- Fusion tree block manipulations of (named) product sectors are computed as Kronecker products of the manipulations of their parts, splitting off the `UniqueFusion` sectors, such that they share the caches of the individual sectors ([#592](https://github.com/QuantumKitHub/TensorKit.jl/pull/592))
48-
- `twist!` and `twist` on a `TensorMap` use a cached tree transformer and the index manipulation kernel instead of a loop over all fusion trees, which removes the per-subblock bookkeeping and turns a twist of a GPU tensor into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
48+
- `twist!` and `twist` on a `TensorMap` or its adjoint use a cached tree transformer and the index manipulation kernel instead of a loop over all fusion trees, which removes the per-subblock bookkeeping and turns a twist of a GPU tensor into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
4949

5050
## [0.17.2](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.1...v0.17.2) - 2026-09-20
5151

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,11 @@ end
288288
end
289289

290290
@cached function TensorKit.treetwister(
291-
A::Type{TA}, V::TensorMapSpace, twistmask::NTuple{N, Bool}, inv::Bool
291+
A::Type{TA}, V::TensorMapSpace, conjsrc::Bool, twistmask::NTuple{N, Bool}, inv::Bool
292292
) where {TA <: AnyGPUVector, N}
293-
host = TensorKit.treetwister(Vector{scalartype(A)}, V, twistmask, inv)
294-
return device_transformer(A, host, linearize((codomainind(V), domainind(V))))
293+
host = TensorKit.treetwister(Vector{scalartype(A)}, V, conjsrc, twistmask, inv)
294+
_, p = TensorKit.twistpermutation(V, conjsrc)
295+
return device_transformer(A, host, linearize(p))
295296
end
296297

297298
const DEFAULT_DEVICECACHE_SIZE = 1000

‎src/tensors/indexmanipulations.jl‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,22 @@ function twist(t::AbstractTensorMap, inds; inv::Bool = false, copy::Bool = false
8888
return _twist!(tdst, t, inds, inv)
8989
end
9090

91+
const TensorMapOrAdjoint = Union{TensorMap, AdjointTensorMap{<:Any, <:Any, <:Any, <:Any, <:TensorMap}}
92+
9193
# `tdst = twist(tsrc, inds; inv)`, where `tdst` and `tsrc` are allowed to be the same tensor
92-
function _twist!(tdst::TensorMap, tsrc::TensorMap, inds, inv::Bool)
93-
V = space(tsrc)
94-
twistmask = ntuple(i -> i in inds, numind(V))
95-
transformer = treetwister(storagetype(tdst), V, twistmask, inv)
96-
return add_transform!(
97-
tdst, tsrc, (codomainind(V), domainind(V)), false, transformer,
98-
One(), Zero(), TO.DefaultBackend(), TO.DefaultAllocator()
99-
)
94+
function _twist!(tdst::TensorMapOrAdjoint, tsrc::TensorMapOrAdjoint, inds, inv::Bool)
95+
twistmask = ntuple(i -> i in inds, numind(tdst))
96+
# the twist acts on the legs of `tdst`, which is the inverse twist of the legs of its parent
97+
if tdst isa AdjointTensorMap
98+
tp = parent(tdst)
99+
twistmask = TupleTools.getindices(twistmask, adjointtensorindices(tp, allind(tp)))
100+
inv = !inv
101+
end
102+
p = (codomainind(tsrc), domainind(tsrc))
103+
tdst′, tsrc′, p′, _, conjsrc, α, β = unwrap_adjoints(tdst, tsrc, p, nothing, false, One(), Zero())
104+
transformer = treetwister(storagetype(tdst′), space(tsrc′), conjsrc, twistmask, inv)
105+
add_transform!(tdst′, tsrc′, p′, conjsrc, transformer, α, β, TO.DefaultBackend(), TO.DefaultAllocator())
106+
return tdst
100107
end
101108
function _twist!(tdst::AbstractTensorMap, tsrc::AbstractTensorMap, inds, inv::Bool)
102109
for (f₁, f₂) in fusiontrees(tsrc)

‎src/tensors/treetransformers.jl‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,20 @@ end
202202
return TreeTransformer(A, fusiontreetransform, p, Vdst, Vsrc, conjsrc)
203203
end
204204

205-
# a twist maps every tree pair onto itself, so this is a `UniqueTreeTransformer` for all sectors
205+
# a twist maps every tree pair onto itself, so this is a `UniqueTreeTransformer` for all sectors;
206+
# it twists the legs `twistmask` of `conjsrc ? V' : V`, reading the subblocks of a source with space `V`
206207
@cached function treetwister(
207-
A::Type{TA}, V::TensorMapSpace, twistmask::NTuple{N, Bool}, inv::Bool
208+
A::Type{TA}, V::TensorMapSpace, conjsrc::Bool, twistmask::NTuple{N, Bool}, inv::Bool
208209
)::UniqueTreeTransformer{recoupling_scalartype(A, sectorscalartype(sectortype(V))), N} where {TA, N}
210+
Vdst, p = twistpermutation(V, conjsrc)
209211
fusiontreetwister(f) = twist(f, twistmask; inv)
210-
return UniqueTreeTransformer(A, fusiontreetwister, (codomainind(V), domainind(V)), V, V, false)
212+
return UniqueTreeTransformer(A, fusiontreetwister, p, Vdst, V, conjsrc)
213+
end
214+
215+
# destination space of a twist of `conjsrc ? V' : V`, and the permutation of the legs of `V` it applies
216+
function twistpermutation(V::TensorMapSpace, conjsrc::Bool)
217+
conjsrc || return V, (codomainind(V), domainind(V))
218+
return V', adjointtensorindices(V', (codomainind(V'), domainind(V')))
211219
end
212220

213221
# default cachestyle is GlobalLRUCache

‎test/cuda/tensors.jl‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,21 @@ for V in spacelist
313313
t = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)')
314314
ht = adapt(Array, t)
315315
for inds in (1, (2, 4), [1, 3, 5]), inv in (false, true)
316-
@test adapt(Array, twist(t, inds; inv)) ≈ twist(ht, inds; inv)
317-
@test adapt(Array, twist!(copy(t), inds; inv)) ≈ twist!(copy(ht), inds; inv)
316+
ref = twist(ht, inds; inv)
317+
@test adapt(Array, twist(t, inds; inv)) ≈ ref
318+
@test adapt(Array, twist!(copy(t), inds; inv)) ≈ ref
319+
# adjoint operands
320+
@test adapt(Array, twist(t', inds; inv)) ≈ twist(copy(ht'), inds; inv)
321+
t2 = copy(t)
322+
twist!(t2', inds; inv)
323+
@test adapt(Array, t2)' ≈ twist(copy(ht'), inds; inv)
318324
end
319325
@test twist(twist(t, 2), 2; inv = true) ≈ t
320326
if !TensorKit.has_shared_twist(t, (1, 3, 5))
321327
twistmask = (true, false, true, false, true)
322-
twister = TensorKit.treetwister(storagetype(t), space(t), twistmask, false)
328+
twister = TensorKit.treetwister(storagetype(t), space(t), false, twistmask, false)
323329
@test twister isa getglobal(GPUArraysExt, :DeviceUniqueTreeTransformer)
324-
@test twister === TensorKit.treetwister(storagetype(t), space(t), twistmask, false)
330+
@test twister === TensorKit.treetwister(storagetype(t), space(t), false, twistmask, false)
325331
end
326332
end
327333
symmetricbraiding && @timedtestset "Full trace: test self-consistency" begin

‎test/tensors/indexmanipulations.jl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ for V in spacelist
173173
end
174174
end
175175
end
176+
hasbraiding && @timedtestset "Twists: adjoint operands" begin
177+
W = V1 ⊗ V2 ← V3 ⊗ V4
178+
for T in (Float64, ComplexF64)
179+
T <: Real && !(sectorscalartype(I) <: Real) && continue
180+
t = rand(T, W)
181+
tref = copy(t') # genuine TensorMap on W'
182+
for inds in (1, (2, 3), [1, 4], (1, 2, 3, 4)), inv in (false, true)
183+
ref = twist(tref, inds; inv)
184+
# adjoint source
185+
@test twist(t', inds; inv) ≈ ref
186+
# adjoint source and destination, in place
187+
t2 = copy(t)
188+
@test twist!(t2', inds; inv) ≈ ref
189+
@test t2 ≈ ref'
190+
# adjoint destination
191+
@test TensorKit._twist!(similar(t)', tref, inds, inv) ≈ ref
192+
@test twist(twist(t', inds), inds; inv = true) ≈ tref
193+
end
194+
end
195+
end
176196
symmetricbraiding && @timedtestset "Permutations: BraidingTensor source" begin
177197
τ = BraidingTensor(V1, V2)
178198
p = ((2, 1), (3, 4))

0 commit comments

Comments
 (0)