Skip to content

Commit 038d7e1

Browse files
lkdvosclaude
andauthored
Factorize fusion tree block manipulations of product sectors (#592)
* Factorize fusion tree block manipulations of product sectors Braid and transpose a block of a ProductSector through its head and tail factors: the recoupling matrix is the Kronecker product of those of the factors, so blocks that only differ in their abelian charges share the cached manipulations of the non-abelian factors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Use the Kronecker order as the canonical order of product sector blocks Construct a FusionTreeBlock of a ProductSector as the Kronecker product of its head and tail blocks, such that the factorized manipulations produce canonical blocks without reordering. Bound the tuple elements of the product signatures by Sector, which fixes dispatch to the two-factor tailsector method. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Split mixed product sectors into their UniqueFusion and other factors A product with both UniqueFusion and other factors is split into its other factors and its UniqueFusion factors, instead of its first and remaining factors. The UniqueFusion part has a single tree pair, such that block construction and manipulation take a single step instead of recursing through every factor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Move the product sector fusion tree code to its own file, and handle the UniqueFusion factors in one step Construct the UniqueFusion part of a mixed product block first, such that fusionblocks skips the non-abelian construction for blocks with mismatching abelian charges. Construct and manipulate blocks of UniqueFusion products, which hold a single tree pair, directly instead of recursing over their factors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Handle UniqueFusion parts in product_transform, and support named product sectors Transform the single tree pair of the UniqueFusion part of a mixed product, and every tree pair of a UniqueFusion product, directly in product_transform, instead of special-casing UniqueFusion blocks in fsbraid and fstranspose. Split and join NamedSector products through the corresponding ProductSector. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Test named product sectors through the generic tests instead of a factorized testset Replace `FermionParity ⊠ U1Irrep ⊠ SU2Irrep` in the sector list by a named Hubbard-like product sector, and drop the testset comparing with the unfactorized manipulations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Skip splitting the vertex labels for UniqueFusion tails, and add a changelog entry Also drop a redundant assignment in `jointree`, avoid the internal `TensorKitSectors._sectors`, and fix two comments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix inference of nested product manipulations on Julia 1.10, and mark product_transform inactive for Enzyme Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Rename the product sector helpers, and derive splitting and joining from the part positions The two parts of a product are now determined once by `_partpositions`, from which `productparts`, `productpartstypes` and `_joinparts` follow, for sectors and fusion trees alike. Generic helper names are made private. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Use the deconstruct/reconstruct terminology for splitting and joining product sectors and trees Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Reword the changelog entry for product sector manipulations Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Use TupleTools.getindices and divrem, and replace comprehensions with explicit loops and broadcasts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 58056cb commit 038d7e1

8 files changed

Lines changed: 191 additions & 2 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
4343

4444
- Recoupling matrices are stored such that they can leverage BLAS, also for mixed complex tensor with real recoupling coefficients.
4545
- 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)
46+
- 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))
4647

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

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ end
147147
@inline EnzymeRules.inactive(::typeof(TensorKit.artin_braid), ::Any, ::Any) = nothing
148148
@inline EnzymeRules.inactive(::typeof(TensorKit.fstranspose), ::Any) = nothing
149149
@inline EnzymeRules.inactive(::typeof(TensorKit.fstranspose), ::Any, ::Any) = nothing
150+
@inline EnzymeRules.inactive(::typeof(TensorKit.product_transform), ::Any...) = nothing
150151
@inline EnzymeRules.inactive(::typeof(TensorKit.treetransposer), ::Any...) = nothing
151152
@inline EnzymeRules.inactive(::typeof(TensorKit.treebraider), ::Any...) = nothing
152153
@inline EnzymeRules.inactive(::typeof(TensorKit.insertleftunit), ::HomSpace, ::Any) = nothing

‎src/fusiontrees/braiding_manipulations.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ function braid(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tuple, leve
286286
@assert TupleTools.isperm((p[1]..., p[2]...))
287287
return fsbraid((src, p, levels))
288288
end
289+
function braid(src::ProductSectorBlock, p::Index2Tuple, levels::Index2Tuple)
290+
isempty(src) && return fsbraid((src, p, levels))
291+
# the return type is asserted since inference gives up on the recursion on older Julia versions
292+
return product_transform(sectorscalartype(sectortype(src)), src) do x
293+
return braid(x, p, levels)
294+
end::_fsdicttype(typeof((src, p, levels)))
295+
end
289296

290297
const FSPBraidKey{I, N₁, N₂} = Tuple{FusionTreePair{I}, Index2Tuple{N₁, N₂}, Index2Tuple}
291298
const FSBBraidKey{I, N₁, N₂} = Tuple{FusionTreeBlock{I}, Index2Tuple{N₁, N₂}, Index2Tuple}

‎src/fusiontrees/duality_manipulations.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,13 @@ function Base.transpose(src::Union{FusionTreePair, FusionTreeBlock}, p::Index2Tu
526526
iscyclicpermutation(p′) || throw(ArgumentError("invalid cyclic or planar permutation p = $p"))
527527
return fstranspose((src, p))
528528
end
529+
function Base.transpose(src::ProductSectorBlock, p::Index2Tuple)
530+
isempty(src) && return fstranspose((src, p))
531+
# the return type is asserted since inference gives up on the recursion on older Julia versions
532+
return product_transform(fusionscalartype(sectortype(src)), src) do x
533+
return transpose(x, p)
534+
end::_fsdicttype(typeof((src, p)))
535+
end
529536

530537
const FSPTransposeKey{I, N₁, N₂} = Tuple{FusionTreePair{I}, Index2Tuple{N₁, N₂}}
531538
const FSBTransposeKey{I, N₁, N₂} = Tuple{FusionTreeBlock{I}, Index2Tuple{N₁, N₂}}

‎src/fusiontrees/fusiontrees.jl‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ function FusionTreeBlock{I}(
127127
isdual::Tuple{NTuple{N₁, Bool}, NTuple{N₂, Bool}};
128128
sizehint::Int = 0
129129
) where {I <: Sector, N₁, N₂}
130+
return fusiontreeblock(I, uncoupled, isdual; sizehint)
131+
end
132+
# construct the block by enumerating its coupled sectors and fusion trees
133+
function fusiontreeblock(
134+
::Type{I}, uncoupled::Tuple{NTuple{N₁, I}, NTuple{N₂, I}},
135+
isdual::Tuple{NTuple{N₁, Bool}, NTuple{N₂, Bool}}; sizehint::Int = 0
136+
) where {I <: Sector, N₁, N₂}
130137
F = fusiontreetype(I, N₁, N₂)
131138
trees = Vector{F}(undef, 0)
132139
sizehint > 0 && sizehint!(trees, sizehint)
@@ -379,6 +386,9 @@ end
379386
# Fusion tree iterators
380387
include("iterator.jl")
381388

389+
# Fusion trees of product sectors
390+
include("producttrees.jl")
391+
382392
# Manipulate fusion trees
383393
include("basic_manipulations.jl")
384394
include("duality_manipulations.jl")

‎src/fusiontrees/producttrees.jl‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Product sectors
2+
# ---------------
3+
# Fusion tree manipulations of a product sector are computed from those of two parts of the product,
4+
# as the Kronecker product of their recoupling matrices.
5+
6+
# products of at least two sectors, also with names
7+
const _MultiSectorTuple = Tuple{Sector, Sector, Vararg{Sector}}
8+
const ProductSectorType = Union{
9+
ProductSector{<:_MultiSectorTuple}, NamedSector{<:NamedTuple{<:Any, <:_MultiSectorTuple}},
10+
}
11+
const ProductSectorBlock = FusionTreeBlock{<:ProductSectorType}
12+
13+
# The two parts of a product are its non-`UniqueFusion` and its `UniqueFusion` sectors if it has both,
14+
# and otherwise its first and its remaining sectors, on which we recurse.
15+
Base.@assume_effects :foldable function _partpositions(::Type{ProductSector{T}}) where {T}
16+
positions = ntuple(identity, fieldcount(T))
17+
isunique = map(i -> FusionStyle(fieldtype(T, i)) isa UniqueFusion, positions)
18+
any(isunique) && !all(isunique) &&
19+
return Base.filter(i -> !isunique[i], positions), Base.filter(i -> isunique[i], positions)
20+
return (first(positions),), Base.tail(positions)
21+
end
22+
# parts with a single sector are not wrapped in a `ProductSector`, to share the caches of that sector
23+
_sectorproduct(sectors::Tuple{Sector}) = only(sectors)
24+
_sectorproduct(sectors::Tuple) = ProductSector(sectors)
25+
_sectortuple(s::ProductSector) = s.sectors
26+
_sectortuple(s::Sector) = (s,)
27+
28+
function deconstructsector(s::ProductSector)
29+
positions₁, positions₂ = _partpositions(typeof(s))
30+
return _sectorproduct(TupleTools.getindices(s.sectors, positions₁)),
31+
_sectorproduct(TupleTools.getindices(s.sectors, positions₂))
32+
end
33+
Base.@assume_effects :foldable function deconstructsectortype(::Type{ProductSector{T}}) where {T}
34+
return map(_partpositions(ProductSector{T})) do positions
35+
return length(positions) == 1 ? fieldtype(T, only(positions)) :
36+
ProductSector{Tuple{map(i -> fieldtype(T, i), positions)...}}
37+
end
38+
end
39+
function reconstructsector(::Type{ProductSector{T}}, s₁::Sector, s₂::Sector) where {T}
40+
sectors = (_sectortuple(s₁)..., _sectortuple(s₂)...)
41+
positions = TupleTools.invperm(TupleTools.vcat(_partpositions(ProductSector{T})...))
42+
return ProductSector{T}(TupleTools.getindices(sectors, positions))
43+
end
44+
45+
# named products are split and joined as the corresponding `ProductSector`
46+
_productsectortype(::Type{NamedSector{NamedTuple{K, T}}}) where {K, T} = ProductSector{T}
47+
deconstructsector(s::NamedSector) = deconstructsector(ProductSector(s))
48+
deconstructsectortype(::Type{I}) where {I <: NamedSector} = deconstructsectortype(_productsectortype(I))
49+
reconstructsector(::Type{I}, s₁::Sector, s₂::Sector) where {I <: NamedSector} =
50+
I(reconstructsector(_productsectortype(I), s₁, s₂))
51+
52+
# the vertex labels of a product are the linear indices of the labels of its parts, with the second
53+
# part running fastest, such that a `UniqueFusion` second part leaves the labels of the first unchanged
54+
_vertexlines(f::FusionTree) = (f.uncoupled[1], f.innerlines..., f.coupled)
55+
_vertexmultiplicities(f::FusionTree) =
56+
ntuple(i -> Nsymbol(_vertexlines(f)[i], f.uncoupled[i + 1], _vertexlines(f)[i + 1]), length(f) - 1)
57+
58+
_maptree(proj, f::FusionTree, vertices) = FusionTree(
59+
map(proj, f.uncoupled), proj(f.coupled), f.isdual, map(proj, f.innerlines), vertices
60+
)
61+
function deconstructtree(f::FusionTree)
62+
vertices₁ = vertices₂ = f.vertices
63+
if FusionStyle(last(deconstructsectortype(sectortype(f)))) isa UniqueFusion
64+
vertices₂ = map(Returns(1), f.vertices)
65+
elseif FusionStyle(f) isa GenericFusion && length(f) > 1
66+
ns = _vertexmultiplicities(_maptree(last ∘ deconstructsector, f, map(Returns(1), f.vertices)))
67+
vertices₁₂ = map((μ, n) -> (divrem(μ - 1, n) .+ 1), f.vertices, ns)
68+
vertices₁ = first.(vertices₁₂)
69+
vertices₂ = last.(vertices₁₂)
70+
end
71+
return _maptree(first ∘ deconstructsector, f, vertices₁),
72+
_maptree(last ∘ deconstructsector, f, vertices₂)
73+
end
74+
function reconstructtree(::Type{I}, f₁::FusionTree, f₂::FusionTree) where {I}
75+
vertices = if FusionStyle(I) isa GenericFusion && length(f₁) > 1 && !(FusionStyle(f₂) isa UniqueFusion)
76+
map((μ, ν, n) -> ν + (μ - 1) * n, f₁.vertices, f₂.vertices, _vertexmultiplicities(f₂))
77+
else
78+
f₁.vertices
79+
end
80+
join(a, b) = reconstructsector(I, a, b)
81+
return fusiontreetype(I, length(f₁))(
82+
map(join, f₁.uncoupled, f₂.uncoupled), join(f₁.coupled, f₂.coupled), f₁.isdual,
83+
map(join, f₁.innerlines, f₂.innerlines), vertices
84+
)
85+
end
86+
87+
# the canonical order of a product block is that of the Kronecker product of the blocks of its
88+
# parts, with the first part running fastest
89+
function productblock(::Type{I}, block₁::FusionTreeBlock, block₂::FusionTreeBlock) where {I}
90+
trees = fusiontreetype(I, numout(block₁), numin(block₁))[]
91+
sizehint!(trees, length(block₁) * length(block₂))
92+
for (g₁, g₂) in fusiontrees(block₂)
93+
for (f₁, f₂) in fusiontrees(block₁)
94+
push!(trees, (reconstructtree(I, f₁, g₁), reconstructtree(I, f₂, g₂)))
95+
end
96+
end
97+
return FusionTreeBlock(trees)
98+
end
99+
100+
function FusionTreeBlock{I}(
101+
uncoupled::Tuple{NTuple{N₁, I}, NTuple{N₂, I}},
102+
isdual::Tuple{NTuple{N₁, Bool}, NTuple{N₂, Bool}};
103+
sizehint::Int = 0
104+
) where {I <: ProductSectorType, N₁, N₂}
105+
# blocks of `UniqueFusion` sectors are enumerated directly
106+
FusionStyle(I) isa UniqueFusion && return fusiontreeblock(I, uncoupled, isdual; sizehint)
107+
108+
# the second part holds the `UniqueFusion` sectors of a mixed product, which are cheaper to construct
109+
I₁, I₂ = deconstructsectortype(I)
110+
block₂ = FusionTreeBlock{I₂}(map.(last ∘ deconstructsector, uncoupled), isdual)
111+
isempty(block₂) && return FusionTreeBlock(Vector{fusiontreetype(I, N₁, N₂)}())
112+
113+
block₁ = FusionTreeBlock{I₁}(map.(first ∘ deconstructsector, uncoupled), isdual)
114+
return productblock(I, block₁, block₂)
115+
end
116+
117+
# the distinct tree pairs, and the position of every tree pair among them
118+
function _distincttrees(trees)
119+
distinct = unique(trees)
120+
return distinct, indexin(trees, distinct)
121+
end
122+
# the tree pairs of the two parts of a tree pair
123+
_deconstructtrees((f₁, f₂)) = map(tuple, deconstructtree(f₁), deconstructtree(f₂))
124+
125+
# apply `transform` to the block `src`, where every tree pair of a `UniqueFusion` block maps to a
126+
# single tree pair
127+
function _unique_transform(transform::F, ::Type{T}, src::FusionTreeBlock) where {F, T}
128+
FusionStyle(src) isa UniqueFusion || return transform(src)
129+
dst = map(transform, fusiontrees(src))
130+
return FusionTreeBlock(map(first, dst)) => LinearAlgebra.diagm(convert.(T, last.(dst)))
131+
end
132+
133+
# apply `transform` to the block `src` through its parts, with the recoupling matrix with scalar type `T`
134+
function product_transform(transform::F, ::Type{T}, src::FusionTreeBlock{I}) where {F, T, I}
135+
if FusionStyle(I) isa UniqueFusion
136+
return _unique_transform(transform, T, src)
137+
elseif FusionStyle(last(deconstructsectortype(I))) isa UniqueFusion && numind(src) > 0
138+
# the second part holds the `UniqueFusion` sectors of a mixed product, with a single tree pair
139+
(g₁, g₂), coeff = transform(last(_deconstructtrees(first(fusiontrees(src)))))
140+
dst₁, U₁ = transform(FusionTreeBlock(map(first ∘ _deconstructtrees, fusiontrees(src))))
141+
dst = FusionTreeBlock(
142+
map(((f₁, f₂),) -> (reconstructtree(I, f₁, g₁), reconstructtree(I, f₂, g₂)), fusiontrees(dst₁))
143+
)
144+
U = Matrix{T}(undef, size(U₁))
145+
U .= coeff .* U₁
146+
return dst => U
147+
end
148+
src₁, cols₁ = _distincttrees(map(first ∘ _deconstructtrees, fusiontrees(src)))
149+
src₂, cols₂ = _distincttrees(map(last ∘ _deconstructtrees, fusiontrees(src)))
150+
dst₁, U₁ = _unique_transform(transform, T, FusionTreeBlock(src₁))
151+
dst₂, U₂ = _unique_transform(transform, T, FusionTreeBlock(src₂))
152+
153+
dst = productblock(I, dst₁, dst₂)
154+
U = Matrix{T}(undef, length(dst), length(src))
155+
for (col, i, j) in zip(axes(U, 2), cols₁, cols₂)
156+
row = 0
157+
for j₂ in axes(U₂, 1), j₁ in axes(U₁, 1)
158+
U[row += 1, col] = U₁[j₁, i] * U₂[j₂, j]
159+
end
160+
end
161+
return dst => U
162+
end

‎test/setup.jl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ uniquefusionsectorlist = (
158158
)
159159
simplefusionsectorlist = (
160160
CU1Irrep, SU2Irrep, FibonacciAnyon, IsingAnyon,
161-
FermionParity ⊠ U1Irrep ⊠ SU2Irrep, FermionParity ⊠ SU2Irrep ⊠ SU2Irrep,
161+
@NamedSector{f::FermionParity, s::SU2Irrep, c::U1Irrep}, # named Hubbard-like
162+
FermionParity ⊠ SU2Irrep ⊠ SU2Irrep,
162163
ZNElement{6, 3} ⊠ SU2Irrep, # complex F symbols, anyonic braiding
163164
Z3Element{1} ⊠ FibonacciAnyon ⊠ FibonacciAnyon, # complex F symbols, no braiding
164165
)

‎test/symmetries/doubletree.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using TensorKitSectors
1212

1313
@timedtestset "Double fusion trees for $(TensorKit.type_repr(I))" verbose = true for I in (fast_tests ? fast_sectorlist : sectorlist)
1414
Istr = TensorKit.type_repr(I)
15-
N = I <: ProductSector ? 3 : 4
15+
N = I <: Union{ProductSector, NamedSector} ? 3 : 4
1616

1717
if UnitStyle(I) isa SimpleUnit
1818
out = random_fusion(I, Val(N))

0 commit comments

Comments
 (0)