Skip to content

Commit 2ab9c1c

Browse files
leburgelclaude
andauthored
Share equal recoupling matrices between tree transformers (#630)
* Share equal recoupling matrices between tree transformers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Cache the recoupling matrices of braids and transposes in the scalar type of the transformers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Share the recoupling matrices by value in the transforms instead of the tree transformers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Put the scalar type first in the cached transforms, and annotate the return type of recouplingmatrix Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Define recouplingmatrix in long form Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7f0c873 commit 2ab9c1c

7 files changed

Lines changed: 66 additions & 43 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
4242
- `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))
4343
- `foreachblock` with several tensors is type stable, which removes the dynamic dispatch on every call to it and to the factorizations that go through it
4444
- `taskforeach` with a single worker is a plain loop without an atomic counter, which lowers the cost per subblock of single-threaded index manipulations and planar traces
45+
- Tree transformers for sectors with multiple fusion channels share equal recoupling matrices, which only depend on the sectors, instead of storing one copy per transformer
46+
- The cached braids and transposes of fusion tree blocks keep their recoupling matrices in the scalar type of the tree transformers that use them, instead of a copy in that type for every transformer
4547

4648
## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30
4749

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ end
142142
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any) = nothing
143143
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any, ::Any) = nothing
144144
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any, ::Any, ::Any) = nothing
145-
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontreebraid), ::Any, ::Any, ::Any) = nothing
145+
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontreebraid), ::Any, ::Any, ::Any, ::Any) = nothing
146146
@inline EnzymeRules.inactive(::typeof(TensorKit.artin_braid), ::Any, ::Any) = nothing
147-
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontreetranspose), ::Any, ::Any) = nothing
147+
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontreetranspose), ::Any, ::Any, ::Any) = nothing
148148
@inline EnzymeRules.inactive(::typeof(TensorKit.product_transform), ::Any...) = nothing
149149
@inline EnzymeRules.inactive(::typeof(TensorKit.treetransposer), ::Any...) = nothing
150150
@inline EnzymeRules.inactive(::typeof(TensorKit.treebraider), ::Any...) = nothing

‎src/fusiontrees/braiding_manipulations.jl‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -291,55 +291,59 @@ respectively, which determines how indices braid. In particular, if `i` and `j`
291291
levels[j]`. This does not allow to encode the most general braid, but a general braid can
292292
be obtained by combining such operations.
293293
"""
294-
function braid(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple, levels::Index2Tuple)
294+
braid(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple, levels::Index2Tuple) =
295+
_braid(sectorscalartype(sectortype(src)), src, p, levels)
296+
297+
# the braid with the recoupling matrix of a block in scalar type `T`, which is cached as such
298+
function _braid(::Type{T}, src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple, levels::Index2Tuple) where {T}
295299
@assert numind(src) == length(p[1]) + length(p[2])
296300
@assert numout(src) == length(levels[1]) && numin(src) == length(levels[2])
297301
@assert TupleTools.isperm((p[1]..., p[2]...))
298-
return fusiontreebraid(src, p, levels)
302+
return fusiontreebraid(T, src, p, levels)
299303
end
300-
function braid(src::ProductSectorBlock, p::Index2Tuple, levels::Index2Tuple)
301-
isempty(src) && return fusiontreebraid(src, p, levels)
304+
function _braid(::Type{T}, src::ProductSectorBlock, p::Index2Tuple, levels::Index2Tuple) where {T}
305+
isempty(src) && return fusiontreebraid(T, src, p, levels)
302306
# the return type is asserted since inference gives up on the recursion on older Julia versions
303-
return product_transform(sectorscalartype(sectortype(src)), src) do x
307+
dst, U = product_transform(T, src) do x
304308
return braid(x, p, levels)
305-
end::fusiontreebraidtype(src, p)
309+
end::fusiontreebraidtype(T, src, p)
310+
return dst => recouplingmatrix(U)
306311
end
307312

308313
# Braiding can promote real fusion coefficients to complex values. Keep the result
309314
# type explicit so all braid paths remain inferred, including anyonic sectors.
310315
Base.@assume_effects :foldable function fusiontreebraidtype(
311-
src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, ::Index2Tuple{N₁, N₂}
312-
) where {I, N₁, N₂}
313-
T = sectorscalartype(I)
316+
::Type{T}, src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, ::Index2Tuple{N₁, N₂}
317+
) where {T, I, N₁, N₂}
314318
if src isa FusionTreePair
315-
return Pair{fusiontreetype(I, N₁, N₂), T}
319+
return Pair{fusiontreetype(I, N₁, N₂), sectorscalartype(I)}
316320
else
317321
F = Tuple{fusiontreetype(I, N₁), fusiontreetype(I, N₂)}
318322
return Pair{FusionTreeBlock{I, N₁, N₂, F}, Matrix{T}}
319323
end
320324
end
321325

322326
@cached function fusiontreebraid(
323-
src::Union{FusionTreePair{I}, FusionTreeBlock{I}},
327+
::Type{T}, src::Union{FusionTreePair{I}, FusionTreeBlock{I}},
324328
p::Index2Tuple{N₁, N₂}, levels::Index2Tuple
325-
)::fusiontreebraidtype(src, p) where {I, N₁, N₂}
329+
)::fusiontreebraidtype(T, src, p) where {T, I, N₁, N₂}
326330
if src isa FusionTreePair
327331
return _braid_single(src, p, levels)
328332
else
329333
if length(src) == 1 # then so is every intermediate block: the braid is a product of scalars
330334
f′, coeff = _braid_single(only(fusiontrees(src)), p, levels)
331-
return FusionTreeBlock([f′]) => fill(coeff, 1, 1)
335+
return FusionTreeBlock([f′]) => recouplingmatrix(fill(convert(T, coeff), 1, 1))
332336
end
333337

334338
p₁, p₂ = p
335339
l₁, l₂ = levels
336340
p′ = linearizepermutation(p₁, p₂, numout(src), numin(src))
337341
levels = (l₁..., reverse(l₂)...)
338342

339-
T = sectorscalartype(I)
343+
Tₛ = sectorscalartype(I)
340344
dst, U = repartition(src, Val(numind(src)))
341-
# the bends have `fusionscalartype(I)`, which can be narrower than `T`
342-
U = convert(Matrix{T}, U)
345+
# the bends have `fusionscalartype(I)`, which can be narrower than `Tₛ`
346+
U = convert(Matrix{Tₛ}, U)
343347

344348
# the swaps keep the size of the block, so that the matrices of the previous step are recycled
345349
U_tmp = similar(U)
@@ -355,9 +359,9 @@ end
355359
levels = TupleTools.setindex(levels, l, s + 1)
356360
end
357361

358-
N₂ == 0 && return dst => U
362+
N₂ == 0 && return dst => recouplingmatrix(convert(Matrix{T}, U))
359363
dst, U_bend = repartition(dst::FusionTreeBlock{I}, Val(N₁))
360-
return dst => U_bend * U
364+
return dst => recouplingmatrix(convert(Matrix{T}, U_bend * U))
361365
end
362366
end
363367

@@ -371,7 +375,7 @@ function _braid_single((f₁, f₂)::FusionTreePair, (p₁, p₂)::Index2Tuple{N
371375
return (f₁′, f₂′) => coeff₁ * coeff₂ * coeff₃
372376
end
373377

374-
CacheStyle(::typeof(fusiontreebraid), src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, args...) where {I} =
378+
CacheStyle(::typeof(fusiontreebraid), ::Type, src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, args...) where {I} =
375379
FusionStyle(I) isa UniqueFusion ? NoCache() : GlobalCache()
376380

377381
"""

‎src/fusiontrees/duality_manipulations.jl‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -484,45 +484,49 @@ outgoing (`t1`) and incoming sectors (`t2`) respectively (with identical coupled
484484
repartitioning and permuting the tree such that sectors `p1` become outgoing and sectors
485485
`p2` become incoming.
486486
"""
487-
function Base.transpose(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple)
487+
Base.transpose(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple) =
488+
_transpose(fusionscalartype(sectortype(src)), src, p)
489+
490+
# the transpose with the recoupling matrix of a block in scalar type `T`, which is cached as such
491+
function _transpose(::Type{T}, src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple) where {T}
488492
N = numind(src)
489493
N == length(p[1]) + length(p[2]) || throw(ArgumentError("invalid permutation p = $p of length N = $N"))
490494
p′ = linearizepermutation(p..., numout(src), numin(src))
491495
iscyclicpermutation(p′) || throw(ArgumentError("invalid cyclic or planar permutation p = $p"))
492-
return fusiontreetranspose(src, p)
496+
return fusiontreetranspose(T, src, p)
493497
end
494-
function Base.transpose(src::ProductSectorBlock, p::Index2Tuple)
495-
isempty(src) && return fusiontreetranspose(src, p)
498+
function _transpose(::Type{T}, src::ProductSectorBlock, p::Index2Tuple) where {T}
499+
isempty(src) && return fusiontreetranspose(T, src, p)
496500
# the return type is asserted since inference gives up on the recursion on older Julia versions
497-
return product_transform(fusionscalartype(sectortype(src)), src) do x
501+
dst, U = product_transform(T, src) do x
498502
return transpose(x, p)
499-
end::fusiontreetransposetype(src, p)
503+
end::fusiontreetransposetype(T, src, p)
504+
return dst => recouplingmatrix(U)
500505
end
501506

502507
Base.@assume_effects :foldable function fusiontreetransposetype(
503-
src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, ::Index2Tuple{N₁, N₂}
504-
) where {I, N₁, N₂}
505-
T = fusionscalartype(I)
508+
::Type{T}, src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, ::Index2Tuple{N₁, N₂}
509+
) where {T, I, N₁, N₂}
506510
if src isa FusionTreePair
507-
return Pair{fusiontreetype(I, N₁, N₂), T}
511+
return Pair{fusiontreetype(I, N₁, N₂), fusionscalartype(I)}
508512
else
509513
F = Tuple{fusiontreetype(I, N₁), fusiontreetype(I, N₂)}
510514
return Pair{FusionTreeBlock{I, N₁, N₂, F}, Matrix{T}}
511515
end
512516
end
513517

514518
@cached function fusiontreetranspose(
515-
src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, p::Index2Tuple{N₁, N₂}
516-
)::fusiontreetransposetype(src, p) where {I, N₁, N₂}
519+
::Type{T}, src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, p::Index2Tuple{N₁, N₂}
520+
)::fusiontreetransposetype(T, src, p) where {T, I, N₁, N₂}
517521
p₁, p₂ = p
518522

519523
N = N₁ + N₂
520524
p = linearizepermutation(p₁, p₂, numout(src), numin(src))
521525

522526
dst, U = repartition(src, N₁)
523-
length(p) == 0 && return dst => U
527+
length(p) == 0 && return dst => _withscalartype(T, U)
524528
i1 = findfirst(==(1), p)::Int
525-
i1 == 1 && return dst => U
529+
i1 == 1 && return dst => _withscalartype(T, U)
526530

527531
Nhalf = N >> 1
528532
while 1 < i1 ≤ Nhalf
@@ -536,10 +540,14 @@ end
536540
i1 = mod1(i1 + 1, N)
537541
end
538542

539-
return dst => U
543+
return dst => _withscalartype(T, U)
540544
end
541545

542-
CacheStyle(::typeof(fusiontreetranspose), src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, args...) where {I} =
546+
# the recoupling matrix of a block in scalar type `T`, shared by value, the coefficient of a tree pair as is
547+
_withscalartype(::Type{T}, U::AbstractMatrix) where {T} = recouplingmatrix(convert(Matrix{T}, U))
548+
_withscalartype(::Type, coeff::Number) = coeff
549+
550+
CacheStyle(::typeof(fusiontreetranspose), ::Type, src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, args...) where {I} =
543551
FusionStyle(I) isa UniqueFusion ? NoCache() : GlobalCache()
544552

545553
# COMPOSITE DUALITY MANIPULATIONS PART 2: Planar traces

‎src/fusiontrees/fusiontrees.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ function Base.show(io::IO, t::FusionTree{I}) where {I <: Sector}
424424
end
425425
end
426426

427+
# `U`, or an equal matrix returned before: a recoupling matrix only depends on the sectors, so transforms share them
428+
@cached function recouplingmatrix(U::Matrix{T})::Matrix{T} where {T}
429+
return U
430+
end
431+
MemoizationKit.default_cache_settings(::typeof(recouplingmatrix)) = (; measure = "bytes", maxsize = 2^28)
432+
427433
# Fusion tree iterators
428434
include("iterator.jl")
429435

‎src/tensors/treetransformers.jl‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ _cache_structure_size(t) = sizeof(t.structure_src) +
7676

7777
MemoizationKit.cachesize(t::UniqueTreeTransformer) = sizeof(t) + _cache_payload_size(t.data) + _cache_structure_size(t)
7878

79-
MemoizationKit.cachesize(t::GenericTreeTransformer) = _cache_generic_size(t)
79+
# The recoupling matrices are shared with other transformers and counted by the cache of `recouplingmatrix`.
80+
MemoizationKit.cachesize(t::GenericTreeTransformer) = _cache_generic_size(t; matrices = false)
8081

81-
function _cache_generic_size(t)
82+
function _cache_generic_size(t; matrices::Bool = true)
8283
bytes = sizeof(t) + sizeof(t.data) + _cache_structure_size(t)
8384
for (U, inds_dst, inds_src) in t.data
84-
bytes += _cache_payload_size(U) + sizeof(inds_src) +
85+
bytes += (matrices ? _cache_payload_size(U) : 0) + sizeof(inds_src) +
8586
(inds_src === inds_dst ? 0 : sizeof(inds_dst))
8687
end
8788
return bytes
@@ -209,15 +210,17 @@ end
209210
# levels are attached to the legs, so they follow the same relabeling as the permutation
210211
levels′ = conjsrc ? TupleTools.getindices(levels, adjointtensorindices(Vsrc′, allind(Vsrc′))) : levels
211212
levels″ = (TupleTools.getindices(levels′, codomainind(Vsrc′)), TupleTools.getindices(levels′, domainind(Vsrc′)))
212-
fusiontreebraider(f) = braid(f, p′, levels″)
213+
T = recoupling_scalartype(A, sectorscalartype(sectortype(Vdst)))
214+
fusiontreebraider(f) = _braid(T, f, p′, levels″)
213215
return TreeTransformer(A, fusiontreebraider, p, Vdst, Vsrc, conjsrc)
214216
end
215217

216218
@cached function treetransposer(
217219
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple, conjsrc::Bool
218220
)::treetransformertype(A, Vdst, Vsrc) where {TA}
219221
p′ = conjsrc ? adjointtensorindices(Vsrc, p) : p
220-
fusiontreetransform(f) = transpose(f, p′)
222+
T = recoupling_scalartype(A, sectorscalartype(sectortype(Vdst)))
223+
fusiontreetransform(f) = _transpose(T, f, p′)
221224
return TreeTransformer(A, fusiontreetransform, p, Vdst, Vsrc, conjsrc)
222225
end
223226

‎test/other/caches.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ end
3131
@test 0 < bytes <= Base.summarysize(value)
3232
end
3333
for src in TensorKit.fusionblocks(W)
34-
result = TensorKit.fusiontreebraid(src, p, ((1, 2), (3, 4)))
34+
result = TensorKit.fusiontreebraid(sectorscalartype(I), src, p, ((1, 2), (3, 4)))
3535
@test 0 < (@constinferred MemoizationKit.cachesize(result)) <= Base.summarysize(result)
3636
end
3737

0 commit comments

Comments
 (0)