Skip to content

Commit d397cb2

Browse files
lkdvosclaude
andcommitted
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>
1 parent 941044b commit d397cb2

6 files changed

Lines changed: 61 additions & 126 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
4444

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)
47-
- Braiding fusion tree blocks no longer constructs and indexes the canonical intermediate blocks of every step: these keep their trees in the order in which they are produced, their buffers are recycled, and blocks with a single tree are braided as a product of scalars (internal)
47+
- 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)
4848

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

‎src/fusiontrees/braiding_manipulations.jl‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ function artin_braid(src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false) where
9393
return artin_braid!(Matrix{sectorscalartype(I)}(undef, n, n), src, i; inv) # zeroed by `artin_braid!`
9494
end
9595
# `U` is overwritten with the braid matrix of `src`, a complete block; the trees of the result are
96-
# in canonical order or in the order in which they are produced, which recycles `trees` if given
96+
# stored in `trees` if given
9797
function artin_braid!(
98-
U::AbstractMatrix, src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false,
99-
canonical::Val = Val(true), trees = nothing
98+
U::AbstractMatrix, src::FusionTreeBlock{I, N, 0}, i; inv::Bool = false, trees = nothing
10099
) where {I, N}
101100
1 <= i < N || throw(ArgumentError("Cannot swap outputs i=$i and i+1 out of only $N outputs"))
102101
size(U) == (length(src), length(src)) || throw(DimensionMismatch(lazy"braid matrix of size $(size(U)) for a block of $(length(src)) trees"))
@@ -111,7 +110,7 @@ function artin_braid!(
111110
isdual = src.isdual[1]
112111
isdual′ = TupleTools.setindex(isdual, isdual[i], i + 1)
113112
isdual′ = TupleTools.setindex(isdual′, isdual[i + 1], i)
114-
rows = TreeRows(I, (uncoupled′, ()), (isdual′, ()), length(src), canonical, trees)
113+
rows = TreeRows(fusiontreetype(I, N, 0), length(src), trees)
115114

116115
oneT = one(sectorscalartype(I))
117116
fill!(U, zero(oneT))
@@ -335,9 +334,7 @@ end
335334
p = linearizepermutation(p1, p2, numout(src), numin(src))
336335
levels = (l1..., reverse(l2)...)
337336

338-
# the intermediate blocks keep their trees in the order in which they are produced,
339-
# and only the result is brought into canonical order
340-
dst, U = repartition_noncanonical(src, Val(numind(src)))
337+
dst, U = repartition(src, Val(numind(src)))
341338

342339
# the swaps keep the size of the block, so that the tree vector and the matrices of the
343340
# previous step are recycled (except for the trees of `src`, which are part of the key)
@@ -348,7 +345,7 @@ end
348345
for s in permutation2swaps(p)
349346
_check_levels(braid_style, levels, s)
350347
inv = levels[s] > levels[s + 1]
351-
dst′, _ = artin_braid!(U_tmp, dst, s; inv, canonical = Val(false), trees)
348+
dst′, _ = artin_braid!(U_tmp, dst, s; inv, trees)
352349
trees = dst.trees === src.trees ? nothing : dst.trees
353350
dst = dst′
354351
U_spare, U = U, U_spare isa typeof(U_tmp) ? mul!(U_spare, U_tmp, U) : U_tmp * U
@@ -357,13 +354,9 @@ end
357354
levels = TupleTools.setindex(levels, l, s + 1)
358355
end
359356

360-
if N₂ == 0
361-
return canonicalize(dst, U)
362-
else
363-
dst, U_tmp = repartition_noncanonical(dst::FusionTreeBlock{I}, Val(N₁))
364-
U = U_tmp * U
365-
return canonicalize(dst, U)
366-
end
357+
N₂ == 0 && return dst => U
358+
dst, U_tmp = repartition(dst::FusionTreeBlock{I}, Val(N₁))
359+
return dst => U_tmp * U
367360
end
368361
end
369362

‎src/fusiontrees/duality_manipulations.jl‎

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,14 @@ function bendright((f₁, f₂)::FusionTreePair)
6565

6666
return (f₁′, f₂′) => coeff
6767
end
68-
function bendright(src::FusionTreeBlock, canonical::Val = Val(true))
68+
function bendright(src::FusionTreeBlock)
6969
I = sectortype(src)
7070
N₁ = numout(src)
7171
N₂ = numin(src)
7272
@assert N₁ > 0
73-
uncoupled_dst = (
74-
TupleTools.front(src.uncoupled[1]),
75-
(src.uncoupled[2]..., dual(src.uncoupled[1][N₁])),
76-
)
77-
isdual_dst = (
78-
TupleTools.front(src.isdual[1]),
79-
(src.isdual[2]..., !(src.isdual[1][N₁])),
80-
)
81-
82-
rows = TreeRows(I, uncoupled_dst, isdual_dst, length(src), canonical)
83-
# the canonical destination block has more trees than `src` if `src` holds only some of the
84-
# trees with its uncoupled sectors (as in `planar_trace`)
85-
U = zeros(fusionscalartype(I), max(length(rows.trees), length(src)), length(src))
73+
74+
rows = TreeRows(fusiontreetype(I, N₁ - 1, N₂ + 1), length(src))
75+
U = zeros(fusionscalartype(I), length(src), length(src))
8676

8777
for (col, (f₁, f₂)) in enumerate(fusiontrees(src))
8878
(a, b, c), (f₁′, f₂′) = _bendright_treepair((f₁, f₂))
@@ -144,24 +134,14 @@ end
144134

145135
# !! note that this is more or less a copy of bendright through
146136
# (f1, f2) => conj(coeff) for ((f2, f1), coeff) in bendleft(src)
147-
function bendleft(src::FusionTreeBlock, canonical::Val = Val(true))
137+
function bendleft(src::FusionTreeBlock)
148138
I = sectortype(src)
149139
N₁ = numout(src)
150140
N₂ = numin(src)
151141
@assert N₂ > 0
152-
uncoupled_dst = (
153-
(src.uncoupled[1]..., dual(src.uncoupled[2][N₂])),
154-
TupleTools.front(src.uncoupled[2]),
155-
)
156-
isdual_dst = (
157-
(src.isdual[1]..., !(src.isdual[2][N₂])),
158-
TupleTools.front(src.isdual[2]),
159-
)
160-
161-
rows = TreeRows(I, uncoupled_dst, isdual_dst, length(src), canonical)
162-
# the canonical destination block has more trees than `src` if `src` holds only some of the
163-
# trees with its uncoupled sectors (as in `planar_trace`)
164-
U = zeros(fusionscalartype(I), max(length(rows.trees), length(src)), length(src))
142+
143+
rows = TreeRows(fusiontreetype(I, N₁ + 1, N₂ - 1), length(src))
144+
U = zeros(fusionscalartype(I), length(src), length(src))
165145

166146
for (col, (f₁, f₂)) in enumerate(fusiontrees(src))
167147
(a, b, c), (f₂′, f₁′) = _bendright_treepair((f₂, f₁))
@@ -236,17 +216,11 @@ function foldright((f₁, f₂)::FusionTreePair)
236216
end
237217

238218
function foldright(src::FusionTreeBlock)
239-
uncoupled_dst = (
240-
Base.tail(src.uncoupled[1]),
241-
(dual(first(src.uncoupled[1])), src.uncoupled[2]...),
242-
)
243-
isdual_dst = (Base.tail(src.isdual[1]), (!first(src.isdual[1]), src.isdual[2]...))
244219
I = sectortype(src)
245220
N₁ = numout(src)
246221
@assert N₁ > 0
247222

248-
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst; sizehint = length(src))
249-
indexmap = treeindex_map(dst)
223+
rows = TreeRows(fusiontreetype(I, N₁ - 1, numin(src) + 1), length(src))
250224

251225
f₁, f₂ = first(fusiontrees(src))
252226
a::I = f₁.uncoupled[1]
@@ -260,7 +234,7 @@ function foldright(src::FusionTreeBlock)
260234
c::I = f₁.coupled
261235
cache₃ = Dict((b, c) => Asymbol(a, b, c))
262236

263-
U = zeros(eltype(coeff₁), length(dst), length(src))
237+
U = zeros(eltype(coeff₁), length(src), length(src))
264238
for (col, (f₁, f₂)) in enumerate(fusiontrees(src))
265239
f₁′s, coeffs₁ = get!(cache₁, f₁) do
266240
multi_Fmove(f₁)
@@ -280,12 +254,12 @@ function foldright(src::FusionTreeBlock)
280254
if isduala
281255
coeff *= κₐ
282256
end
283-
row = indexmap[treeindex_data((f₁′, f₂′))]
257+
row = row!(rows, (f₁′, f₂′))
284258
@inbounds U[row, col] += coeff
285259
end
286260
end
287261
end
288-
return dst => U
262+
return FusionTreeBlock(rows.trees) => U
289263
end
290264

291265

@@ -319,21 +293,12 @@ function foldleft((f₁, f₂)::FusionTreePair)
319293
end
320294

321295
function foldleft(src::FusionTreeBlock)
322-
uncoupled_dst = (
323-
(dual(first(src.uncoupled[2])), src.uncoupled[1]...),
324-
Base.tail(src.uncoupled[2]),
325-
)
326-
isdual_dst = (
327-
(!first(src.isdual[2]), src.isdual[1]...),
328-
Base.tail(src.isdual[2]),
329-
)
330296
I = sectortype(src)
331297
N₁ = numin(src)
332298
N₂ = numout(src)
333299
@assert N₁ > 0
334300

335-
dst = FusionTreeBlock{I}(uncoupled_dst, isdual_dst; sizehint = length(src))
336-
indexmap = treeindex_map(dst)
301+
rows = TreeRows(fusiontreetype(I, N₂ + 1, N₁ - 1), length(src))
337302

338303
f₁, f₂ = first(fusiontrees(src))
339304
a::I = f₂.uncoupled[1]
@@ -347,7 +312,7 @@ function foldleft(src::FusionTreeBlock)
347312
c::I = f₂.coupled
348313
cache₃ = Dict((b, c) => Asymbol(a, b, c))
349314

350-
U = zeros(eltype(coeff₂), length(dst), length(src))
315+
U = zeros(eltype(coeff₂), length(src), length(src))
351316
for (col, (f₁, f₂)) in enumerate(fusiontrees(src))
352317
f₂′s, coeffs₂ = get!(cache₂, f₂) do
353318
multi_Fmove(f₂)
@@ -367,12 +332,12 @@ function foldleft(src::FusionTreeBlock)
367332
if isduala
368333
coeff *= conj(κₐ)
369334
end
370-
row = indexmap[treeindex_data((f₁′, f₂′))]
335+
row = row!(rows, (f₁′, f₂′))
371336
@inbounds U[row, col] += coeff
372337
end
373338
end
374339
end
375-
return dst => U
340+
return FusionTreeBlock(rows.trees) => U
376341
end
377342

378343
# clockwise cyclic permutation while preserving (N₁, N₂): foldright & bendleft
@@ -478,7 +443,7 @@ return dst, U
478443
# it, so that it is visible to the generator's world age. Otherwise a precompile workload that
479444
# first triggers `repartition` fails with `UndefVarError: _repartition_body` (at runtime the
480445
# first call happens at a later world age, which masks the ordering issue).
481-
function _repartition_body(N, canonical = true)
446+
function _repartition_body(N)
482447
if N == 0
483448
ex = quote
484449
T = fusionscalartype(sectortype(src))
@@ -491,14 +456,13 @@ function _repartition_body(N, canonical = true)
491456
end
492457
else
493458
f = N < 0 ? bendleft : bendright
494-
args = canonical ? () : (Val(false),)
495459
ex_rep = Expr(:block)
496460
for _ in 1:(abs(N) - 1)
497-
push!(ex_rep.args, :((dst, Utmp) = $f(dst, $(args...))))
461+
push!(ex_rep.args, :((dst, Utmp) = $f(dst)))
498462
push!(ex_rep.args, :(U = Utmp * U))
499463
end
500464
ex = quote
501-
dst, U = $f(src, $(args...))
465+
dst, U = $f(src)
502466
$ex_rep
503467
return dst => U
504468
end
@@ -508,10 +472,6 @@ end
508472
@generated function repartition(src::Union{FusionTreePair, FusionTreeBlock}, ::Val{N}) where {N}
509473
return _repartition_body(numout(src) - N)
510474
end
511-
# the same for a block, with the trees of the result in the order in which they are produced
512-
@generated function repartition_noncanonical(src::FusionTreeBlock, ::Val{N}) where {N}
513-
return _repartition_body(numout(src) - N, false)
514-
end
515475

516476
"""
517477
transpose((f₁, f₂)::FusionTreePair{I}, p::Index2Tuple{N₁, N₂}) where {I, N₁, N₂}

‎src/fusiontrees/fusiontrees.jl‎

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ const FusionTreePair{I, N₁, N₂} = Tuple{FusionTree{I, N₁}, FusionTree{I, N
117117
118118
Collection of fusion-splitting tree pairs that share the same uncoupled sectors.
119119
Mostly internal structure to speed up non-`UniqueFusion` fusiontree manipulation computations.
120-
Braiding a block requires it to be complete, i.e. to contain all the fusion-splitting tree pairs
121-
with its uncoupled sectors and duality flags, as `FusionTreeBlock{I}(uncoupled, isdual)` does.
120+
Manipulating a block requires it to be complete, i.e. to contain all the fusion-splitting tree
121+
pairs with its uncoupled sectors and duality flags, as `FusionTreeBlock{I}(uncoupled, isdual)`
122+
does. The resulting block holds its trees in the order in which they are produced, rather than in
123+
the canonical order of `FusionTreeBlock{I}(uncoupled, isdual)`.
122124
"""
123125
struct FusionTreeBlock{I, N₁, N₂, F <: FusionTreePair{I, N₁, N₂}}
124126
trees::Vector{F}
@@ -273,38 +275,23 @@ function treeindex_data((f₁, f₂))
273275
return false
274276
end
275277
end
276-
function treeindex_map(fs::FusionTreeBlock)
277-
I = sectortype(fs)
278-
return fusiontreedict(I)(treeindex_data(f) => ind for (ind, f) in enumerate(fusiontrees(fs)))
279-
end
280278

281-
# The trees of the destination block of a block manipulation, and the row of each of them in the
282-
# transformation matrix. With `Val(true)`, these are the trees of the destination block in their
283-
# canonical order; with `Val(false)`, the trees are added in the order in which they are produced,
284-
# which avoids constructing the destination block. The latter is used for the intermediate blocks
285-
# of a composite manipulation, of which only the result is brought into canonical order; the tree
286-
# vector of an intermediate block that is no longer needed can then be passed on as `buffer`.
287-
# With `Val(false)`, the source block has to be complete: the destination block then has as many
288-
# trees (`n`), and an incomplete source block raises an error if its destination block has more.
279+
# The trees of the destination block of a block manipulation, in the order in which they are
280+
# produced, and the row of each of them in the transformation matrix. The source block has to be
281+
# complete, such that the destination block has as many trees (`n`); the tree vector of a block
282+
# that is no longer needed can be passed on as `buffer`.
289283
struct TreeRows{F, D}
290284
trees::Vector{F}
291285
index::Union{D, Nothing} # `nothing`: few trees, the rows are found by a linear search
292286
n::Int
293287
# no default outer constructor, of which `D` would be unbound if `index === nothing`
294288
TreeRows{F, D}(trees::Vector{F}, index, n::Int) where {F, D} = new{F, D}(trees, index, n)
295289
end
296-
function TreeRows(
297-
::Type{I}, uncoupled, isdual, n::Int, ::Val{canonical}, buffer = nothing
298-
) where {I, canonical}
299-
F = fusiontreetype(I, length(uncoupled[1]), length(uncoupled[2]))
300-
trees = if canonical
301-
fusiontrees(FusionTreeBlock{I}(uncoupled, isdual; sizehint = n))
302-
else
303-
buffer isa Vector{F} ? empty!(buffer) : sizehint!(Vector{F}(), n)
304-
end
305-
K = Base.promote_op(treeindex_data, eltype(trees))
306-
index = n > 32 ? Dict{K, Int}(treeindex_data(f) => i for (i, f) in enumerate(trees)) : nothing
307-
return TreeRows{eltype(trees), Dict{K, Int}}(trees, index, n)
290+
function TreeRows(::Type{F}, n::Int, buffer = nothing) where {F}
291+
trees = buffer isa Vector{F} ? empty!(buffer) : sizehint!(Vector{F}(), n)
292+
K = Base.promote_op(treeindex_data, F)
293+
index = n > 32 ? Dict{K, Int}() : nothing
294+
return TreeRows{F, Dict{K, Int}}(trees, index, n)
308295
end
309296
function row!(rows::TreeRows, f)
310297
key = treeindex_data(f)
@@ -315,22 +302,12 @@ function row!(rows::TreeRows, f)
315302
end
316303
isnothing(i) || return i
317304
length(rows.trees) < rows.n ||
318-
throw(ArgumentError("braiding requires a complete `FusionTreeBlock`"))
305+
throw(ArgumentError("manipulating a block requires it to be complete"))
319306
push!(rows.trees, f)
320307
isnothing(rows.index) || (rows.index[key] = length(rows.trees))
321308
return length(rows.trees)
322309
end
323310

324-
# bring a block whose trees are not in canonical order, together with the transformation matrix
325-
# whose rows correspond to these trees, into canonical order
326-
function canonicalize(src::FusionTreeBlock{I}, U::AbstractMatrix) where {I}
327-
length(src) == 1 && return src => U
328-
rows = TreeRows(I, src.uncoupled, src.isdual, length(src), Val(true))
329-
U′ = zeros(eltype(U), length(rows.trees), size(U, 2))
330-
U′[map(f -> row!(rows, f), fusiontrees(src)), :] = U
331-
return FusionTreeBlock(rows.trees) => U′
332-
end
333-
334311

335312
# Facilitate getting correct fusion tree types
336313
Base.@assume_effects :foldable function fusiontreetype(::Type{I}, N::Int) where {I <: Sector}

‎test/setup.jl‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export sectorlist, fast_sectorlist
99
export default_spacelist, factorization_spacelist, ad_spacelist
1010
export VIBM, VIBMRepA4
1111
export test_ad_rrule
12-
export _isunitary, _isone
12+
export _isunitary, _isone, _inorder
1313

1414
using Random
1515
using Test: @test
1616
using TensorKit
17-
using TensorKit: ℙ, PlanarTrivial
17+
using TensorKit: ℙ, PlanarTrivial, FusionTreeBlock
1818
using TensorKitSectors
1919
using TensorOperations: IndexTuple, Index2Tuple
2020
using Base.Iterators: take, product
@@ -149,6 +149,15 @@ _isunitary(x::Number; kwargs...) = isapprox(x * x', one(x); kwargs...)
149149
_isunitary(x; kwargs...) = isunitary(x; kwargs...)
150150
_isone(x; kwargs...) = isapprox(x, one(x); kwargs...)
151151

152+
# the rows of the transformation matrix `U` onto the trees of `dst`, in the order of the trees of
153+
# `ref`, as block manipulations keep their trees in the order in which they are produced
154+
_inorder(U::Number, dst, ref) = (@test dst == ref; U)
155+
function _inorder(U::AbstractMatrix, dst::FusionTreeBlock, ref::FusionTreeBlock)
156+
rows = indexin(fusiontrees(ref), fusiontrees(dst))
157+
@test length(dst) == length(ref) && !any(isnothing, rows)
158+
return U[something.(rows, 1), :]
159+
end
160+
152161

153162
uniquefusionsectorlist = (
154163
Z2Irrep, Z3Irrep, Z4Irrep, Z3Irrep ⊠ Z4Irrep, U1Irrep,

0 commit comments

Comments
 (0)