Skip to content

Commit f99f9db

Browse files
lkdvosclaude
andauthored
Cached treetwister for twist! (#599)
* Implement `twist!` through a cached `treetwister` and the transform kernel `twist!` looped over all fusion trees and scaled every subblock separately, with a cost that grows with the number of subblocks rather than the data size, and one kernel launch per subblock for GPU tensors (#595). - Add `twist(src, inds; inv)` for fusion tree pairs and blocks, returning the scalar twist factor, with `inds` given as indices or as a `Bool` mask. - Add the cached `treetwister`, a `UniqueTreeTransformer` for all sectors since a twist maps every tree pair onto itself, keyed on a fixed-length mask. - `twist!` and `twist` on a `TensorMap` go through `add_transform!` (in place for `twist!`, a single pass for `twist`); other tensor types keep the loop. - Move the shortcut for trivial permutations from `add_transform!` into `_braid!` and `_transpose!`, so `add_transform!` always applies the transformer. - Cache a device `treetwister` in the GPUArrays extension. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * 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> * Make `twist!` mirror `braid!` with a public `twist!(tdst, tsrc, inds, α, β, ...)` `twist!(t, inds)` and `twist(t, inds)` are now thin wrappers around the three-argument form, which goes through `unwrap_adjoints` and a `_twist!` kernel entry like `_braid!`. Adds `twist_pb` with Mooncake and Enzyme rules, and backend/allocator keywords for `twist`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix `spacecheck_transform` ambiguity and Enzyme crash of in-place `twist!` on Julia 1.10 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Return `t` from the in-place `twist!` wrapper to avoid an Enzyme crash on Julia 1.10 Enzyme aborts in `fixup_callconv!` when compiling the wrapper while it returned the result of the three-argument `twist!`, which also broke the fermionic contraction rules. The in-place Enzyme rules call the wrapper again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Support twisting `DiagonalTensorMap`s on GPU and test diagonal twists GPU diagonal sources go through a cached `diagonal_treetwister`, analogous to `_braid!` and `_transpose!` for diagonal tensors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 49049d9 commit f99f9db

18 files changed

Lines changed: 503 additions & 32 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
2323
### Added
2424

2525
- Enzyme rules for `planartrace!`, with a pullback that also supports adjoint tensors
26+
- `twist(src, inds; inv = false)` for fusion tree pairs and blocks, returning `src` together with the scalar twist factor of the legs `inds`, which can be given as indices or as a `Bool` mask
27+
- `twist!(tdst, tsrc, inds, α = 1, β = 0, [backend], [allocator]; inv = false)` computing `tdst = β * tdst + α * twist(tsrc, inds; inv)`, in line with `permute!` and `braid!`, together with Mooncake and Enzyme rules; `twist` now also accepts `backend` and `allocator` keywords
2628

2729
### Changed
2830

@@ -36,6 +38,8 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
3638

3739
### Performance
3840

41+
- `twist!` and `twist` on a `TensorMap`, a `DiagonalTensorMap` or their adjoints 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, diagonal or not, into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
42+
3943
## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30
4044

4145
### Added

‎docs/src/lib/fusiontrees.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ Base.transpose(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple)
4949
braid(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple, levels::Index2Tuple)
5050
permute(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple)
5151
planar_trace(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple, q::Index2Tuple)
52+
twist(src::Union{FusionTreePair, FusionTreeBlock}, inds)
5253
```

‎ext/TensorKitEnzymeExt/indexmanipulations.jl‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,144 @@ function EnzymeRules.forward(
186186
end
187187
end
188188

189+
function EnzymeRules.augmented_primal(
190+
config::EnzymeRules.RevConfigWidth{1},
191+
func::Const{typeof(TK.twist!)},
192+
::Type{RT},
193+
C::Annotation{<:AbstractTensorMap},
194+
A::Annotation{<:AbstractTensorMap},
195+
inds::Const,
196+
α::Annotation{<:Number},
197+
β::Annotation{<:Number},
198+
ba::Const...;
199+
inv::Bool = false
200+
) where {RT}
201+
C_cache = !isa(β, Const) ? copy(C.val) : nothing
202+
A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
203+
# if we need to compute Δa, it is faster to allocate an intermediate twisted A
204+
# and store that instead of repeating the twist in the pullback each time.
205+
# effectively, we replace `twist!` by `add ∘ twist`.
206+
Ap = if !isa(α, Const)
207+
Ap = twist(A.val, inds.val; inv, copy = true)
208+
add!(C.val, Ap, α.val, β.val)
209+
Ap
210+
else
211+
bavs = map(a -> a.val, ba)
212+
TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv)
213+
nothing
214+
end
215+
cache = (C_cache, A_cache, Ap)
216+
primal = EnzymeRules.needs_primal(config) ? C.val : nothing
217+
shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing
218+
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
219+
end
220+
function EnzymeRules.reverse(
221+
config::EnzymeRules.RevConfigWidth{1},
222+
func::Const{typeof(TK.twist!)},
223+
::Type{RT},
224+
cache,
225+
C::Annotation{<:AbstractTensorMap},
226+
A::Annotation{<:AbstractTensorMap},
227+
inds::Const,
228+
α::Annotation{<:Number},
229+
β::Annotation{<:Number},
230+
ba::Const...;
231+
inv::Bool = false
232+
) where {RT}
233+
C_cache, A_cache, Ap = cache
234+
Cval = something(C_cache, C.val)
235+
Aval = something(A_cache, A.val)
236+
bavs = map(a -> a.val, ba)
237+
# ΔA
238+
if !isa(A, Const) && !isa(C, Const)
239+
TK.twist_pb(A.dval, Aval, C.dval, C.val, inds.val, α.val, bavs...; inv)
240+
end
241+
Δαr = pullback_dα(α, C, Ap)
242+
Δβr = pullback_dβ(β, C, Cval)
243+
!isa(C, Const) && pullback_dC!(C.dval, β.val)
244+
return nothing, nothing, nothing, Δαr, Δβr, map(Returns(nothing), ba)...
245+
end
246+
function EnzymeRules.forward(
247+
config::EnzymeRules.FwdConfigWidth{1},
248+
func::Const{typeof(TK.twist!)},
249+
::Type{RT},
250+
C::Annotation{<:AbstractTensorMap},
251+
A::Annotation{<:AbstractTensorMap},
252+
inds::Annotation,
253+
α::Annotation{<:Number},
254+
β::Annotation{<:Number},
255+
ba::Const...;
256+
inv::Bool = false
257+
) where {RT}
258+
bavs = map(a -> a.val, ba)
259+
if !isa(C, Const)
260+
if isa(β, Const)
261+
scale!(C.dval, β.val)
262+
else
263+
add!(C.dval, C.val, β.dval, β.val)
264+
end
265+
!isa(α, Const) && TK.twist!(C.dval, A.val, inds.val, α.dval, One(), bavs...; inv)
266+
!isa(A, Const) && TK.twist!(C.dval, A.dval, inds.val, α.val, One(), bavs...; inv)
267+
end
268+
TK.twist!(C.val, A.val, inds.val, α.val, β.val, bavs...; inv)
269+
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
270+
return C
271+
elseif EnzymeRules.needs_primal(config)
272+
return C.val
273+
elseif EnzymeRules.needs_shadow(config)
274+
return C.dval
275+
else
276+
return nothing
277+
end
278+
end
279+
280+
# in-place twist: the source and destination are the same tensor
281+
function EnzymeRules.augmented_primal(
282+
config::EnzymeRules.RevConfigWidth{1},
283+
func::Const{typeof(TK.twist!)},
284+
::Type{RT},
285+
t::Annotation{<:AbstractTensorMap},
286+
inds::Const;
287+
inv::Bool = false
288+
) where {RT}
289+
TK.twist!(t.val, inds.val; inv)
290+
primal = EnzymeRules.needs_primal(config) ? t.val : nothing
291+
shadow = EnzymeRules.needs_shadow(config) ? t.dval : nothing
292+
return EnzymeRules.AugmentedReturn(primal, shadow, nothing)
293+
end
294+
function EnzymeRules.reverse(
295+
config::EnzymeRules.RevConfigWidth{1},
296+
func::Const{typeof(TK.twist!)},
297+
::Type{RT},
298+
cache,
299+
t::Annotation{<:AbstractTensorMap},
300+
inds::Const;
301+
inv::Bool = false
302+
) where {RT}
303+
!isa(t, Const) && TK.twist!(t.dval, inds.val; inv = !inv)
304+
return (nothing, nothing)
305+
end
306+
function EnzymeRules.forward(
307+
config::EnzymeRules.FwdConfigWidth{1},
308+
func::Const{typeof(TK.twist!)},
309+
::Type{RT},
310+
t::Annotation{<:AbstractTensorMap},
311+
inds::Annotation;
312+
inv::Bool = false
313+
) where {RT}
314+
TK.twist!(t.val, inds.val; inv)
315+
!isa(t, Const) && TK.twist!(t.dval, inds.val; inv)
316+
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
317+
return t
318+
elseif EnzymeRules.needs_primal(config)
319+
return t.val
320+
elseif EnzymeRules.needs_shadow(config)
321+
return t.dval
322+
else
323+
return nothing
324+
end
325+
end
326+
189327
function EnzymeRules.augmented_primal(
190328
config::EnzymeRules.RevConfigWidth{1},
191329
func::Const{typeof(flip)},

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ end
151151
@inline EnzymeRules.inactive(::typeof(TensorKit.treetransposer), ::Any...) = nothing
152152
@inline EnzymeRules.inactive(::typeof(TensorKit.treebraider), ::Any...) = nothing
153153
@inline EnzymeRules.inactive(::typeof(TensorKit.treetracer), ::Any...) = nothing
154+
@inline EnzymeRules.inactive(::typeof(TensorKit.treetwister), ::Any...) = nothing
154155
@inline EnzymeRules.inactive(::typeof(TensorKit.insertleftunit), ::HomSpace, ::Any) = nothing
155156
@inline EnzymeRules.inactive(::typeof(TensorKit.insertrightunit), ::HomSpace, ::Any) = nothing
156157
@inline EnzymeRules.inactive(::typeof(TensorKit.removeunit), ::HomSpace, ::Any) = nothing

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ end
394394
return device_transformer(A, host, p, q)
395395
end
396396

397+
@cached function TensorKit.treetwister(
398+
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
399+
conjsrc::Bool, twistmask::NTuple{N, Bool}, inv::Bool
400+
) where {TA <: AnyGPUVector, N}
401+
host = TensorKit.treetwister(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, twistmask, inv)
402+
return device_transformer(A, host, linearize(p))
403+
end
404+
397405
const DEFAULT_DEVICECACHE_SIZE = 1000
398406

399407
function __init__()
@@ -404,7 +412,9 @@ function __init__()
404412
Symbol("TensorKitGPUArraysExt.GLOBAL_TREETRANSPOSER_CACHE") => GLOBAL_TREETRANSPOSER_CACHE,
405413
Symbol("TensorKitGPUArraysExt.GLOBAL_DIAGONAL_TREEBRAIDER_CACHE") => GLOBAL_DIAGONAL_TREEBRAIDER_CACHE,
406414
Symbol("TensorKitGPUArraysExt.GLOBAL_DIAGONAL_TREETRANSPOSER_CACHE") => GLOBAL_DIAGONAL_TREETRANSPOSER_CACHE,
415+
Symbol("TensorKitGPUArraysExt.GLOBAL_DIAGONAL_TREETWISTER_CACHE") => GLOBAL_DIAGONAL_TREETWISTER_CACHE,
407416
Symbol("TensorKitGPUArraysExt.GLOBAL_TREETRACER_CACHE") => GLOBAL_TREETRACER_CACHE,
417+
Symbol("TensorKitGPUArraysExt.GLOBAL_TREETWISTER_CACHE") => GLOBAL_TREETWISTER_CACHE,
408418
)
409419
)
410420

@@ -413,7 +423,9 @@ function __init__()
413423
resize!(GLOBAL_TREETRANSPOSER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
414424
resize!(GLOBAL_DIAGONAL_TREEBRAIDER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
415425
resize!(GLOBAL_DIAGONAL_TREETRANSPOSER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
426+
resize!(GLOBAL_DIAGONAL_TREETWISTER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
416427
resize!(GLOBAL_TREETRACER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
428+
resize!(GLOBAL_TREETWISTER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
417429

418430
return nothing
419431
end
@@ -694,6 +706,14 @@ end
694706
return diagonal_device_transformer(A, host, dstdiagonal)
695707
end
696708

709+
@cached function diagonal_treetwister(
710+
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
711+
conjsrc::Bool, twistmask::NTuple{N, Bool}, inv::Bool, dstdiagonal::Bool
712+
) where {TA <: AnyGPUVector, N}
713+
host = TensorKit.treetwister(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, twistmask, inv)
714+
return diagonal_device_transformer(A, host, dstdiagonal)
715+
end
716+
697717
function _add_diagonal!(tdst::GPUDiagonalTensorMap, tsrc, conjsrc, transformer, α, β)
698718
_launch_unique!(tdst.data, tsrc.data, conjsrc ? conj : identity, transformer, α, β, Val(1))
699719
return tdst
@@ -729,6 +749,18 @@ Base.@propagate_inbounds function TensorKit._transpose!(
729749
return _add_diagonal!(tdst, tsrc, conjsrc, transformer, α, β)
730750
end
731751

752+
Base.@propagate_inbounds function TensorKit._twist!(
753+
tdst::Union{GPUTensorMap, GPUDiagonalTensorMap}, tsrc::GPUDiagonalTensorMap,
754+
p::Index2Tuple, conjsrc::Bool, twistmask::Tuple{Vararg{Bool}}, inv::Bool, α, β, backend, allocator
755+
)
756+
Vdst, Vsrc = space(tdst), space(tsrc)
757+
@boundscheck TensorKit.spacecheck_transform(permute, Vdst, Vsrc, p, conjsrc)
758+
transformer = diagonal_treetwister(
759+
storagetype(tdst), Vdst, Vsrc, p, conjsrc, twistmask, inv, tdst isa DiagonalTensorMap
760+
)
761+
return _add_diagonal!(tdst, tsrc, conjsrc, transformer, α, β)
762+
end
763+
732764
function TensorKit.add_transform_kernel!(
733765
dst::AnyGPUVector, src::AnyGPUVector, p, conjsrc::Bool,
734766
device::DeviceUniqueTreeTransformer, α, β, backend, allocator, ntasks::Int

‎ext/TensorKitMooncakeExt/indexmanipulations.jl‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,93 @@ function Mooncake.rrule!!(
160160
return t_Δt, twist_pullback
161161
end
162162

163+
# both are needed for correctly capturing every dispatch
164+
@is_primitive(
165+
DefaultCtx,
166+
ReverseMode,
167+
Tuple{
168+
typeof(TK.twist!),
169+
AbstractTensorMap,
170+
AbstractTensorMap, Any,
171+
Number, Number, Vararg{Any},
172+
}
173+
)
174+
@is_primitive(
175+
DefaultCtx,
176+
ReverseMode,
177+
Tuple{
178+
typeof(Core.kwcall), @NamedTuple{inv::Bool}, typeof(TK.twist!),
179+
AbstractTensorMap,
180+
AbstractTensorMap, Any,
181+
Vararg{Any},
182+
}
183+
)
184+
185+
function Mooncake.rrule!!(
186+
::CoDual{typeof(TK.twist!)},
187+
C_ΔC::CoDual{<:AbstractTensorMap},
188+
A_ΔA::CoDual{<:AbstractTensorMap}, inds_Δinds::CoDual,
189+
α_Δα::CoDual{<:Number}, β_Δβ::CoDual{<:Number},
190+
ba_Δba::CoDual...
191+
)
192+
C_ΔC, pullback = twist!_rrule(C_ΔC, A_ΔA, inds_Δinds, false, (α_Δα, β_Δβ, ba_Δba...))
193+
twist!_pullback(Δ::NoRData) = (NoRData(), pullback(Δ)...)
194+
return C_ΔC, twist!_pullback
195+
end
196+
function Mooncake.rrule!!(
197+
::CoDual{typeof(Core.kwcall)}, kwargs_Δkwargs::CoDual{@NamedTuple{inv::Bool}}, ::CoDual{typeof(TK.twist!)},
198+
C_ΔC::CoDual{<:AbstractTensorMap},
199+
A_ΔA::CoDual{<:AbstractTensorMap}, inds_Δinds::CoDual,
200+
args_Δargs::CoDual...
201+
)
202+
inv = primal(kwargs_Δkwargs).inv
203+
C_ΔC, pullback = twist!_rrule(C_ΔC, A_ΔA, inds_Δinds, inv, args_Δargs)
204+
twist!_pullback(Δ::NoRData) = (NoRData(), NoRData(), NoRData(), pullback(Δ)...)
205+
return C_ΔC, twist!_pullback
206+
end
207+
208+
# `args_Δargs` holds the optional `α`, `β`, backend and allocator, where a missing `α` or `β` is constant
209+
function twist!_rrule(C_ΔC, A_ΔA, inds_Δinds, inv, args_Δargs)
210+
# prepare arguments
211+
C, ΔC = arrayify(C_ΔC)
212+
A, ΔA = arrayify(A_ΔA)
213+
inds = primal(inds_Δinds)
214+
args = primal.(args_Δargs)
215+
α = length(args) >= 1 ? args[1] : One()
216+
β = length(args) >= 2 ? args[2] : Zero()
217+
ba = length(args) >= 3 ? args[3:end] : ()
218+
219+
C_cache = copy(C)
220+
221+
# if we need to compute Δa, it is faster to allocate an intermediate twisted A
222+
# and store that instead of repeating the twist in the pullback each time.
223+
# effectively, we replace `twist!` by `add ∘ twist`.
224+
Ap = if _needs_tangent(α)
225+
Ap = twist(A, inds; inv, copy = true)
226+
add!(C, Ap, α, β)
227+
Ap
228+
else
229+
TK.twist!(C, A, inds, α, β, ba...; inv)
230+
nothing
231+
end
232+
233+
function pullback(::NoRData)
234+
copy!(C, C_cache)
235+
236+
TK.twist_pb(ΔA, A, ΔC, C, inds, α, ba...; inv)
237+
ΔAr = NoRData()
238+
239+
Δαr = length(args) >= 1 ? pullback_dα(α, ΔC, Ap) : nothing
240+
Δβr = length(args) >= 2 ? pullback_dβ(β, ΔC, C) : nothing
241+
ΔCr = pullback_dC!(ΔC, β) # this typically returns NoRData()
242+
243+
Δargsr = (Δαr, Δβr, map(Returns(NoRData()), ba)...)[1:length(args)]
244+
return ΔCr, ΔAr, NoRData(), Δargsr...
245+
end
246+
247+
return C_ΔC, pullback
248+
end
249+
163250
# both are needed for correctly capturing every dispatch
164251
@is_primitive DefaultCtx ReverseMode Tuple{typeof(flip), AbstractTensorMap, Any}
165252
@is_primitive DefaultCtx ReverseMode Tuple{typeof(Core.kwcall), @NamedTuple{inv::Bool}, typeof(flip), AbstractTensorMap, Any}

‎src/auxiliary/caches.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050

5151
# category of the miss-path (construction) timer section of an `@cached` function
5252
function _cached_category(fname::Symbol)
53-
return fname in (:fsbraid, :fstranspose, :treebraider, :treetransposer) ?
53+
return fname in (:fsbraid, :fstranspose, :treebraider, :treetransposer, :treetwister) ?
5454
"symmetry" : "bookkeeping"
5555
end
5656

‎src/fusiontrees/braiding_manipulations.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,23 @@ function permute(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple)
367367
return braid(src, p, (levels1, levels2))
368368
end
369369

370+
"""
371+
twist(src::Union{FusionTreePair, FusionTreeBlock}, inds; inv::Bool = false) -> (src, θ)
372+
373+
Apply a twist to the uncoupled legs `inds` of the fusion-splitting tree pair or block `src`,
374+
where `inds` is either a collection of leg indices or an `NTuple{N, Bool}` mask over all legs.
375+
The twist is diagonal in the fusion tree basis, so this returns `src` itself together with the
376+
scalar factor `θ`, which is shared by all trees in a block. If `inv = true`, use the inverse twist.
377+
"""
378+
function twist(src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, inds; inv::Bool = false) where {I}
379+
uncoupled₁, uncoupled₂ = src isa FusionTreeBlock ? src.uncoupled : (src[1].uncoupled, src[2].uncoupled)
380+
uncoupled = (uncoupled₁..., uncoupled₂...)
381+
N = length(uncoupled)
382+
mask = inds isa NTuple{N, Bool} ? inds : ntuple(i -> i in inds, N)
383+
θ = prod(map((m, a) -> m ? twist(a) : one(twist(a)), mask, uncoupled); init = one(sectorscalartype(I)))
384+
return src, inv ? θ' : θ
385+
end
386+
370387
"""
371388
flip((f₁, f₂)::FusionTreePair, i::Int; inv::Bool = false)
372389
-> SingletonDict{FusionTreePair, <:Number}

‎src/pullbacks/indexmanipulations.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ for transform in (:permute, :transpose)
1515
return
1616
end
1717
end
18+
function twist_pb(ΔA, A, ΔC, C, inds, α, ba...; inv::Bool = false)
19+
TC = promote_twist(VectorInterface.promote_scale(ΔC, α), sectortype(ΔC))
20+
if scalartype(ΔA) <: Real && !(TC <: Real)
21+
ΔAc = similar(ΔC, TC)
22+
twist!(ΔAc, ΔC, inds, conj(α), Zero(), ba...; inv = !inv)
23+
add!(ΔA, real(ΔAc))
24+
else
25+
twist!(ΔA, ΔC, inds, conj(α), One(), ba...; inv = !inv)
26+
end
27+
return
28+
end
1829
function braid_pb(ΔA, A, ΔC, C, p, levels, α, ba...)
1930
ip = invperm(linearize(p))
2031
pΔA = TO.repartition(ip, numout(A))

0 commit comments

Comments
 (0)