Skip to content

Commit a00fb9a

Browse files
leburgelclaudelkdvosJutho
authored
Keep the trees of fusion tree block manipulations in production order (#580)
* Keep the intermediate blocks of `fsbraid` in production order The bends and Artin braids of a fusion tree block used to construct their destination block in canonical order, together with a dictionary from trees to rows, at every step. The intermediate blocks of `fsbraid` now keep their trees in the order in which they are produced (`TreeRows`, `canonical = Val(false)` for `bendleft`, `bendright` and `artin_braid`, `repartition_noncanonical`), and only the result is brought into canonical order (`canonicalize`). Small blocks find rows by a linear search. In `artin_braid`, the channels are obtained by filtering `a ⊗ d` instead of intersecting two fusion products, and the symbols that do not depend on the channel are computed once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Recycle tree vectors and matrices between the braids in `fsbraid` The Artin braids keep the size of a block, so `fsbraid` passes the tree vector and the matrices of the previous step on to `artin_braid` and multiplies in place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Braid single-tree blocks as a product of scalars A block with a single tree stays a single tree under every step of `fsbraid`, so its braid is computed with the per-tree manipulations, without blocks or matrices. Their bodies move into private `_artin_braid_single` and `_bendright_single`, which take the single coefficient of each symbol with `only`; the public methods keep their `UniqueFusion` assertion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Test braiding a block with a single tree; changelog Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fill a given matrix in `artin_braid!`, and choose the single braid channel by dispatch Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Give `TreeRows` an inner constructor, so that Aqua finds no unbound type parameter Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Braid single trees through `artin_braid` itself, take the only braid channel with `only`, and filter the braid channels for every unit style Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Set the inner line and the first vertex of a braided tree outside the loops over the multiplicities Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Braid a single tree pair through the tree manipulations themselves `bendright` and `bendleft` of a tree pair no longer assert `UniqueFusion`, but rely on `only` to throw if the bent vertex has a multiplicity, which replaces their `_single` variants. The shortcut of `braid` for a symmetric braiding is restricted to `UniqueFusion`, where the `Fsymbol`s are trivial, such that a block with a single tree is braided as its tree pair. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep the trees of every fusion tree block manipulation in production order The destination blocks of bends, folds and braids are only used together with their transformation matrix, so they no longer construct and index the canonical destination block, but keep their trees in the order in which they are produced. Since `planar_trace` now also works on complete blocks, the canonical mode of `TreeRows`, `canonicalize`, `repartition_noncanonical` and `treeindex_map` are removed. The tests compare the results of manipulations up to the order of their trees. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Drop the recycling of the tree vectors in `fsbraid`, which gives no measurable speedup Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Inline the single braid channel of a tree, and presize the row index of large blocks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Construct `TreeRows` as `TreeRows{F}(n)` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Reword the completeness requirement of `FusionTreeBlock`, and drop the fallback row in `_inorder` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Update src/fusiontrees/braiding_manipulations.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Note the trivial-Fsymbol assumption of symmetric unique-fusion braids, and preallocate both matrices of the swaps in `fusiontreebraid` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: lkdvos <ldevos98@gmail.com> Co-authored-by: Jutho <Jutho@users.noreply.github.com>
1 parent 4815929 commit a00fb9a

6 files changed

Lines changed: 173 additions & 126 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
8888
- 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))
8989
- The TensorOperations ChainRules rules no longer copy or retain `C` when `β = Zero()`, as is the case for every tensor allocated by `@tensor` ([#555](https://github.com/QuantumKitHub/TensorKit.jl/pull/555))
9090
- The Enzyme `norm` rule reuses the cached primal and the `tr` rule avoids an unnecessary copy ([#554](https://github.com/QuantumKitHub/TensorKit.jl/pull/554))
91+
- Fusion tree block manipulations no longer construct and index the canonical destination block, but keep the trees of the result in the order in which they are produced; the buffers of the intermediate blocks of a braid are recycled, and blocks with a single tree are braided as their tree pair (internal)
9192

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

‎src/fusiontrees/braiding_manipulations.jl‎

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ applying `artin_braid(f′, i; inv = true)` to all the outputs `f′` of
1414
`artin_braid(f, i; inv = false)` and collecting the results should yield a single fusion
1515
tree with non-zero coefficient, namely `f` with coefficient `1`. This keyword has no effect
1616
if `BraidingStyle(sectortype(f)) isa SymmetricBraiding`.
17+
18+
For sectors without `UniqueFusion`, the braid of `f` has to be a single fusion tree, which is the
19+
case if `f` is the only fusion tree compatible with the given uncoupled and coupled sectors; this throws otherwise.
1720
"""
1821
function artin_braid(f::FusionTree{I, N}, i; inv::Bool = false) where {I, N}
1922
1 <= i < N || throw(ArgumentError(lazy"Cannot swap outputs i=$i and i+1 out of only $N outputs"))
20-
@assert FusionStyle(I) === UniqueFusion()
2123

2224
uncoupled = f.uncoupled
2325
a, b = uncoupled[i], uncoupled[i + 1]
@@ -53,7 +55,7 @@ function artin_braid(f::FusionTree{I, N}, i; inv::Bool = false) where {I, N}
5355

5456
if i == 1
5557
c = N > 2 ? inner[1] : coupled′
56-
R = oftype(oneT, (inv ? conj(Rsymbol(b, a, c)) : Rsymbol(a, b, c)))
58+
R = oftype(oneT, (inv ? conj(only(Rsymbol(b, a, c))) : only(Rsymbol(a, b, c))))
5759
f′ = FusionTree{I}(uncoupled′, coupled′, isdual′, inner, vertices)
5860
return f′ => R
5961
end
@@ -64,23 +66,33 @@ function artin_braid(f::FusionTree{I, N}, i; inv::Bool = false) where {I, N}
6466
a = inner_extended[i - 1]
6567
c = inner_extended[i]
6668
e = inner_extended[i + 1]
67-
c′ = only(a ⊗ d)
69+
c′ = only(_artin_channels(a, b, d, e))
6870
coeff = oftype(
6971
oneT,
7072
if inv
71-
conj(Rsymbol(d, c, e) * Fsymbol(d, a, b, e, c′, c)) * Rsymbol(d, a, c′)
73+
conj(only(Rsymbol(d, c, e)) * only(Fsymbol(d, a, b, e, c′, c))) * only(Rsymbol(d, a, c′))
7274
else
73-
Rsymbol(c, d, e) * conj(Fsymbol(d, a, b, e, c′, c) * Rsymbol(a, d, c′))
75+
only(Rsymbol(c, d, e)) * conj(only(Fsymbol(d, a, b, e, c′, c)) * only(Rsymbol(a, d, c′)))
7476
end
7577
)
7678
inner′ = TupleTools.setindex(inner, c′, i - 1)
7779
f′ = FusionTree{I}(uncoupled′, coupled′, isdual′, inner′)
7880
return f′ => coeff
7981
end
8082

83+
# the inner lines `c′` of `a ⊗ d → c′ → c′ ⊗ b → e`, i.e. `intersect(a ⊗ d, e ⊗ conj(b))`,
84+
# without constructing the second fusion product
85+
_artin_channels(a, b, d, e) = Iterators.filter(c′ -> !iszero(Nsymbol(c′, b, e)), a ⊗ d)
86+
8187
function artin_braid(src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where {I, N}
88+
n = length(src) # braiding a complete block keeps its size
89+
return artin_braid!(Matrix{sectorscalartype(I)}(undef, n, n), src, i; inv) # zeroed by `artin_braid!`
90+
end
91+
# `U` is overwritten with the braid matrix of `src`, a complete block
92+
function artin_braid!(U::AbstractMatrix, src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where {I, N}
8293
1 <= i < N || throw(ArgumentError("Cannot swap outputs i=$i and i+1 out of only $N outputs"))
83-
isempty(src) && return src => zeros(sectorscalartype(I), 0, 0)
94+
size(U) == (length(src), length(src)) || throw(DimensionMismatch(lazy"braid matrix of size $(size(U)) for a block of $(length(src)) trees"))
95+
isempty(src) && return src => U
8496

8597
uncoupled = src.uncoupled[1]
8698
a, b = uncoupled[i], uncoupled[i + 1]
@@ -91,12 +103,10 @@ function artin_braid(src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where
91103
isdual = src.isdual[1]
92104
isdual′ = TupleTools.setindex(isdual, isdual[i], i + 1)
93105
isdual′ = TupleTools.setindex(isdual′, isdual[i + 1], i)
94-
dst = FusionTreeBlock{I}((uncoupled′, ()), (isdual′, ()); sizehint = length(src))
106+
rows = TreeRows{fusiontreetype(I, N, 0)}(length(src))
95107

96108
oneT = one(sectorscalartype(I))
97-
98-
indexmap = treeindex_map(dst)
99-
U = zeros(sectorscalartype(I), length(dst), length(src))
109+
fill!(U, zero(oneT))
100110

101111
if isunit(a) || isunit(b) # braiding with trivial sector: simple and always possible
102112
for (col, (f, f₂)) in enumerate(fusiontrees(src))
@@ -113,10 +123,10 @@ function artin_braid(src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where
113123
vertices′ = TupleTools.setindex(vertices′, vertices[i - 1], i)
114124
end
115125
f′ = FusionTree{I}(uncoupled′, coupled′, isdual′, inner′, vertices′)
116-
row = indexmap[treeindex_data((f′, f₂))]
126+
row = row!(rows, (f′, f₂))
117127
@inbounds U[row, col] = oneT
118128
end
119-
return dst => U
129+
return FusionTreeBlock(rows.trees) => U
120130
end
121131

122132
BraidingStyle(I) isa NoBraiding &&
@@ -132,7 +142,7 @@ function artin_braid(src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where
132142
if FusionStyle(I) isa MultiplicityFreeFusion
133143
R = oftype(oneT, (inv ? conj(Rsymbol(b, a, c)) : Rsymbol(a, b, c)))
134144
f′ = FusionTree{I}(uncoupled′, coupled′, isdual′, inner, vertices)
135-
row = indexmap[treeindex_data((f′, f₂))]
145+
row = row!(rows, (f′, f₂))
136146
@inbounds U[row, col] = R
137147
else # GenericFusion
138148
μ = vertices[1]
@@ -142,7 +152,7 @@ function artin_braid(src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where
142152
iszero(R) && continue
143153
vertices′ = TupleTools.setindex(vertices, ν, 1)
144154
f′ = FusionTree{I}(uncoupled′, coupled′, isdual′, inner, vertices′)
145-
row = indexmap[treeindex_data((f′, f₂))]
155+
row = row!(rows, (f′, f₂))
146156
@inbounds U[row, col] = R
147157
end
148158
end
@@ -155,46 +165,47 @@ function artin_braid(src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where
155165
c = inner_extended[i]
156166
e = inner_extended[i + 1]
157167
if FusionStyle(I) isa MultiplicityFreeFusion
158-
for c′ in intersect(a ⊗ d, e ⊗ conj(b))
168+
R = inv ? Rsymbol(d, c, e) : Rsymbol(c, d, e)
169+
for c′ in _artin_channels(a, b, d, e)
159170
coeff = if inv
160-
conj(Rsymbol(d, c, e) * Fsymbol(d, a, b, e, c′, c)) * Rsymbol(d, a, c′)
171+
conj(R * Fsymbol(d, a, b, e, c′, c)) * Rsymbol(d, a, c′)
161172
else
162-
Rsymbol(c, d, e) * conj(Fsymbol(d, a, b, e, c′, c) * Rsymbol(a, d, c′))
173+
R * conj(Fsymbol(d, a, b, e, c′, c) * Rsymbol(a, d, c′))
163174
end
164175
iszero(coeff) && continue
165176
inner′ = TupleTools.setindex(inner, c′, i - 1)
166177
f′ = FusionTree{I}(uncoupled′, coupled′, isdual′, inner′)
167-
row = indexmap[treeindex_data((f′, f₂))]
178+
row = row!(rows, (f′, f₂))
168179
@inbounds U[row, col] = coeff
169180
end
170181
else # GenericFusion
171-
for c′ in intersect(a ⊗ d, e ⊗ conj(b))
172-
Rmat1 = inv ? Rsymbol(d, c, e)' : Rsymbol(c, d, e)
182+
Rmat1 = inv ? Rsymbol(d, c, e)' : Rsymbol(c, d, e)
183+
μ = vertices[i - 1]
184+
ν = vertices[i]
185+
for c′ in _artin_channels(a, b, d, e)
173186
Rmat2 = inv ? Rsymbol(d, a, c′)' : Rsymbol(a, d, c′)
174187
Fmat = Fsymbol(d, a, b, e, c′, c)
175-
μ = vertices[i - 1]
176-
ν = vertices[i]
177-
for σ in 1:Nsymbol(a, d, c′)
178-
for λ in 1:Nsymbol(c′, b, e)
188+
inner′ = TupleTools.setindex(inner, c′, i - 1)
189+
for σ in axes(Rmat2, 1)
190+
vertices_σ = TupleTools.setindex(vertices, σ, i - 1)
191+
for λ in axes(Fmat, 2)
179192
coeff = zero(oneT)
180-
for ρ in 1:Nsymbol(d, c, e), κ in 1:Nsymbol(d, a, c′)
193+
for ρ in axes(Rmat1, 2), κ in axes(Rmat2, 2)
181194
coeff += Rmat1[ν, ρ] * conj(Fmat[κ, λ, μ, ρ]) *
182195
conj(Rmat2[σ, κ])
183196
end
184197
iszero(coeff) && continue
185-
vertices′ = TupleTools.setindex(vertices, σ, i - 1)
186-
vertices′ = TupleTools.setindex(vertices′, λ, i)
187-
inner′ = TupleTools.setindex(inner, c′, i - 1)
198+
vertices′ = TupleTools.setindex(vertices_σ, λ, i)
188199
f′ = FusionTree{I}(uncoupled′, coupled′, isdual′, inner′, vertices′)
189-
row = indexmap[treeindex_data((f′, f₂))]
200+
row = row!(rows, (f′, f₂))
190201
@inbounds U[row, col] = coeff
191202
end
192203
end
193204
end
194205
end
195206
end
196207

197-
return dst => U
208+
return FusionTreeBlock(rows.trees) => U
198209
end
199210

200211
# braid fusion tree
@@ -216,9 +227,9 @@ braid(f::FusionTree{I, N}, p::IndexTuple{N}, levels::IndexTuple{N}) where {I, N}
216227
braid(f, (p, ()), (levels, ()))
217228
function braid(f::FusionTree{I, N}, (p, _)::Index2Tuple{N, 0}, (levels, _)::Index2Tuple{N, 0}) where {I, N}
218229
TupleTools.isperm(p) || throw(ArgumentError(lazy"not a valid permutation: $p"))
219-
@assert FusionStyle(I) isa UniqueFusion
220230
braid_style = BraidingStyle(I)
221-
if braid_style isa SymmetricBraiding # this assumes Fsymbols are 1!
231+
# TODO: assumes trivial Fsymbols, which this condition only guarantees up to gauge
232+
if FusionStyle(I) isa UniqueFusion && braid_style isa SymmetricBraiding
222233
coeff = one(sectorscalartype(I))
223234
for i in 1:N
224235
for j in 1:(i - 1)
@@ -312,43 +323,54 @@ end
312323
src::Union{FusionTreePair{I}, FusionTreeBlock{I}},
313324
p::Index2Tuple{N₁, N₂}, levels::Index2Tuple
314325
)::fusiontreebraidtype(src, p) where {I, N₁, N₂}
315-
p₁, p₂ = p
316-
l₁, l₂ = levels
317326
if src isa FusionTreePair
318-
f₁, f₂ = src
319-
p′ = linearizepermutation(p₁, p₂, length(f₁), length(f₂))
320-
levels = (l₁..., reverse(l₂)...)
321-
(f, f₀), coeff₁ = repartition((f₁, f₂), N₁ + N₂)
322-
f′, coeff₂ = braid(f, p′, levels)
323-
(f₁′, f₂′), coeff₃ = repartition((f′, f₀), N₁)
324-
return (f₁′, f₂′) => coeff₁ * coeff₂ * coeff₃
327+
return _braid_single(src, p, levels)
325328
else
329+
if length(src) == 1 # then so is every intermediate block: the braid is a product of scalars
330+
f′, coeff = _braid_single(only(fusiontrees(src)), p, levels)
331+
return FusionTreeBlock([f′]) => fill(coeff, 1, 1)
332+
end
333+
334+
p₁, p₂ = p
335+
l₁, l₂ = levels
326336
p′ = linearizepermutation(p₁, p₂, numout(src), numin(src))
327337
levels = (l₁..., reverse(l₂)...)
328338

329-
dst, U = repartition(src, numind(src))
339+
T = sectorscalartype(I)
340+
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)
330343

344+
# the swaps keep the size of the block, so that the matrices of the previous step are recycled
345+
U_tmp = similar(U)
346+
U_spare = similar(U)
331347
braid_style = BraidingStyle(I)
332348
for s in permutation2swaps(p′)
333349
_check_levels(braid_style, levels, s)
334350
inv = levels[s] > levels[s + 1]
335-
dst, U_tmp = artin_braid(dst, s; inv)
336-
U = U_tmp * U
351+
dst, _ = artin_braid!(U_tmp, dst, s; inv)
352+
U, U_spare = mul!(U_spare, U_tmp, U), U
337353
l = levels[s]
338354
levels = TupleTools.setindex(levels, levels[s + 1], s)
339355
levels = TupleTools.setindex(levels, l, s + 1)
340356
end
341357

342-
if N₂ == 0
343-
return dst => U
344-
else
345-
dst, U_tmp = repartition(dst::FusionTreeBlock{I}, N₁)
346-
U = U_tmp * U
347-
return dst => U
348-
end
358+
N₂ == 0 && return dst => U
359+
dst, U_bend = repartition(dst::FusionTreeBlock{I}, Val(N₁))
360+
return dst => U_bend * U
349361
end
350362
end
351363

364+
# also for a tree pair that is the only one with its uncoupled sectors (`only` throws otherwise)
365+
function _braid_single((f₁, f₂)::FusionTreePair, (p₁, p₂)::Index2Tuple{N₁, N₂}, (l₁, l₂)::Index2Tuple) where {N₁, N₂}
366+
p′ = linearizepermutation(p₁, p₂, length(f₁), length(f₂))
367+
levels = (l₁..., reverse(l₂)...)
368+
(f, f₀), coeff₁ = repartition((f₁, f₂), N₁ + N₂)
369+
f′, coeff₂ = braid(f, p′, levels)
370+
(f₁′, f₂′), coeff₃ = repartition((f′, f₀), N₁)
371+
return (f₁′, f₂′) => coeff₁ * coeff₂ * coeff₃
372+
end
373+
352374
CacheStyle(::typeof(fusiontreebraid), src::Union{FusionTreePair{I}, FusionTreeBlock{I}}, args...) where {I} =
353375
FusionStyle(I) isa UniqueFusion ? NoCache() : GlobalCache()
354376

0 commit comments

Comments
 (0)