Skip to content

Commit 4b61569

Browse files
leburgelclaude
andcommitted
Store the fusion trees of both sides instead of every fusion tree pair
`fusiontrees(W)` is a `FusionTreePairs`, an `AbstractIndices` holding the splitting and fusion trees of every coupled sector; the position of a pair follows from those of its trees, and is its token. `subblockstructure(W)` shares these tokens. `fusionblocks` groups the trees of each side by their uncoupled sectors, combines the groups in the order in which they first occur, and fills every block once at its final size; `degeneracystructure` loops over the trees of both sides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8f7af24 commit 4b61569

4 files changed

Lines changed: 142 additions & 100 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+
- `fusiontrees(W)` stores the splitting and fusion trees of every coupled sector instead of every pair, and `fusionblocks` combines the trees of both sides with the same uncoupled sectors (internal)
4647

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

‎src/TensorKit.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const TO = TensorOperations
118118

119119
using MatrixAlgebraKit
120120

121-
using Dictionaries: Dictionaries, Dictionary, Indices, gettoken, gettokenvalue
121+
using Dictionaries: Dictionaries, AbstractDictionary, AbstractIndices, Dictionary, Indices, gettoken, gettokenvalue
122122
using LRUCache
123123
using OhMyThreads
124124
using ScopedValues

‎src/spaces/homspace.jl‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,11 @@ See also [`degeneracystructure`](@ref), [`subblockstructure`](@ref).
179179
blockstructure(W::HomSpace) = Dictionary(blocksectors(W), degeneracystructure(W).blockstructure)
180180

181181
"""
182-
fusiontrees(W::HomSpace) -> Indices{Tuple{F₁,F₂}}
182+
fusiontrees(W::HomSpace) -> FusionTreePairs{F₁,F₂}
183183
184-
Return an `Indices` of all valid fusion tree pairs `(f₁, f₂)` for `W`, providing a
185-
bijection to sequential integer positions via `gettoken`/`gettokenvalue`. The result is
184+
Return the `FusionTreePairs` of all valid fusion tree pairs `(f₁, f₂)` for `W`, an
185+
`AbstractIndices` providing a bijection to sequential integer positions via
186+
`gettoken`/`gettokenvalue`. The result is
186187
cached based on the sector structure of `W` (ignoring degeneracy dimensions), so
187188
`HomSpace`s that share the same sectors, dualities, and index count will reuse the same
188189
object.
@@ -192,14 +193,14 @@ See also [`sectorstructure`](@ref), [`subblockstructure`](@ref).
192193
fusiontrees(W::HomSpace) = sectorstructure(W).fusiontrees
193194

194195
"""
195-
subblockstructure(W::HomSpace) -> Dictionary
196+
subblockstructure(W::HomSpace) -> AbstractDictionary
196197
197-
Return a `Dictionary` mapping each fusion tree pair `(f₁, f₂)` to its
198+
Return an `AbstractDictionary` mapping each fusion tree pair `(f₁, f₂)` to its
198199
[`StridedStructure`](@ref) `(sizes, strides, offset)`.
199200
200201
See also [`degeneracystructure`](@ref), [`blockstructure`](@ref).
201202
"""
202-
subblockstructure(W::HomSpace) = Dictionary(fusiontrees(W), degeneracystructure(W).subblockstructure)
203+
subblockstructure(W::HomSpace) = FusionTreePairDict(fusiontrees(W), degeneracystructure(W).subblockstructure)
203204

204205
"""
205206
fusionblocks(W::HomSpace)
@@ -211,19 +212,41 @@ function fusionblocks(W::HomSpace)
211212
I = sectortype(W)
212213
N₁, N₂ = numout(W), numin(W)
213214
F = fusiontreetype(I, N₁, N₂)
214-
# group the (cached) fusion trees of `W` by their uncoupled sectors,
215-
# in the order in which these first occur
216-
blocks = FusionTreeBlock{I, N₁, N₂, F}[]
217-
trees_of = Dict{Tuple{NTuple{N₁, I}, NTuple{N₂, I}}, Vector{F}}()
218-
for (f₁, f₂) in fusiontrees(W)
219-
trees = get!(trees_of, (f₁.uncoupled, f₂.uncoupled)) do
220-
newtrees = F[]
221-
push!(blocks, FusionTreeBlock(newtrees))
222-
return newtrees
215+
ts = fusiontrees(W)
216+
# group the (cached) splitting and fusion trees of every coupled sector of `W` by their
217+
# uncoupled sectors: a block combines the groups of both sides with the same uncoupled
218+
# sectors, in the order in which these first occur in `fusiontrees(W)`
219+
parts = Vector{Tuple{Vector{Int}, Vector{Int}}}[]
220+
part_of = Dict{Tuple{NTuple{N₁, I}, NTuple{N₂, I}}, Int}()
221+
for (r₁, r₂) in zip(ts.ranges₁, ts.ranges₂)
222+
g₁ = _groupbyuncoupled(NTuple{N₁, I}, ts.trees₁, r₁)
223+
g₂ = _groupbyuncoupled(NTuple{N₂, I}, ts.trees₂, r₂)
224+
for (u₂, is₂) in pairs(g₂), (u₁, is₁) in pairs(g₁)
225+
k = get!(part_of, (u₁, u₂)) do
226+
push!(parts, Tuple{Vector{Int}, Vector{Int}}[])
227+
return length(parts)
228+
end
229+
push!(parts[k], (is₂, is₁))
223230
end
224-
push!(trees, (f₁, f₂))
225231
end
226-
return blocks
232+
# every block is filled once, at its final size
233+
return map(parts) do ps
234+
trees = Vector{F}(undef, sum(((is₂, is₁),) -> length(is₂) * length(is₁), ps))
235+
k = 0
236+
for (is₂, is₁) in ps, i₂ in is₂, i₁ in is₁
237+
trees[k += 1] = (gettokenvalue(ts.trees₁, i₁), gettokenvalue(ts.trees₂, i₂))
238+
end
239+
return FusionTreeBlock(trees)
240+
end
241+
end
242+
# the positions in `r` of `trees`, grouped by their uncoupled sectors in the order in which
243+
# these first occur
244+
function _groupbyuncoupled(::Type{U}, trees, r) where {U}
245+
groups = Dictionary{U, Vector{Int}}()
246+
for i in r
247+
push!(get!(Vector{Int}, groups, gettokenvalue(trees, i).uncoupled), i)
248+
end
249+
return groups
227250
end
228251

229252
function diagonalblockstructure(W::HomSpace)

‎src/spaces/structure.jl‎

Lines changed: 100 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,101 @@ depending only on which sectors appear (not their degeneracy dimensions). Shared
1212
1313
## Fields
1414
- `blocksectors`: `Indices` of all coupled sectors `c::I`.
15-
- `fusiontrees`: `Indices` of all valid fusion tree pairs `(f₁, f₂)`, in canonical order.
15+
- `fusiontrees`: `FusionTreePairs` of all valid fusion tree pairs `(f₁, f₂)`, in canonical order.
1616
1717
See also [`sectorstructure`](@ref), [`DegeneracyStructure`](@ref).
1818
"""
19-
struct SectorStructure{I <: Sector, F <: FusionTreePair{I}}
19+
struct SectorStructure{I <: Sector, F <: FusionTreePair{I}, T <: AbstractIndices{F}}
2020
blocksectors::Indices{I}
21-
fusiontrees::Indices{F}
21+
fusiontrees::T
2222
end
2323

24+
"""
25+
FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
26+
27+
The fusion tree pairs `(f₁, f₂)` of a `HomSpace`, ordered by coupled sector, then `f₂`, then `f₁`,
28+
stored as the splitting trees `f₁` and the fusion trees `f₂` of every coupled sector. The token of
29+
a pair is its position.
30+
"""
31+
struct FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
32+
trees₁::Indices{F₁} # splitting trees, grouped by coupled sector
33+
trees₂::Indices{F₂} # fusion trees, grouped by coupled sector
34+
blocks₁::Vector{Int} # the coupled sector of every splitting tree
35+
blocks₂::Vector{Int} # the coupled sector of every fusion tree
36+
ranges₁::Vector{UnitRange{Int}} # the splitting trees of every coupled sector
37+
ranges₂::Vector{UnitRange{Int}} # the fusion trees of every coupled sector
38+
offsets::Vector{Int} # the number of pairs before every coupled sector, and in total
39+
end
40+
41+
# from the trees of both sides of every coupled sector
42+
function FusionTreePairs{F₁, F₂}(sides₁, sides₂) where {F₁, F₂}
43+
lengths(ss) = map(length, ss)
44+
ranges(ss) = map((n, l) -> (n - l + 1):n, cumsum(lengths(ss)), lengths(ss))
45+
blocks(ss) = Int[b for (b, ts) in enumerate(ss) for _ in ts]
46+
offsets = cumsum(vcat(0, lengths(sides₁) .* lengths(sides₂)))
47+
return FusionTreePairs{F₁, F₂}(
48+
Indices(F₁[f for ts in sides₁ for f in ts]), Indices(F₂[f for ts in sides₂ for f in ts]),
49+
blocks(sides₁), blocks(sides₂), ranges(sides₁), ranges(sides₂), offsets
50+
)
51+
end
52+
53+
Base.length(t::FusionTreePairs) = last(t.offsets)
54+
Dictionaries.istokenizable(::FusionTreePairs) = true
55+
Dictionaries.tokentype(::FusionTreePairs) = Int
56+
Dictionaries.iteratetoken(t::FusionTreePairs, i::Int = 0) = i < length(t) ? (i + 1, i + 1) : nothing
57+
# tokens as returned by `gettoken` look like those of `Indices`, with the position second
58+
function Dictionaries.gettoken(t::FusionTreePairs, (f₁, f₂))
59+
found₁, (_, i₁) = gettoken(t.trees₁, f₁)
60+
found₂, (_, i₂) = gettoken(t.trees₂, f₂)
61+
(found₁ && found₂ && t.blocks₁[i₁] == t.blocks₂[i₂]) || return false, (0, 0)
62+
b = t.blocks₁[i₁]
63+
r₁, r₂ = t.ranges₁[b], t.ranges₂[b]
64+
return true, (0, t.offsets[b] + (i₂ - first(r₂)) * length(r₁) + (i₁ - first(r₁)) + 1)
65+
end
66+
Dictionaries.gettokenvalue(t::FusionTreePairs, (_, i)::Tuple{Int, Int}) = gettokenvalue(t, i)
67+
function Dictionaries.gettokenvalue(t::FusionTreePairs, i::Int)
68+
b = searchsortedlast(t.offsets, i - 1)
69+
r₁, r₂ = t.ranges₁[b], t.ranges₂[b]
70+
j₂, j₁ = divrem(i - 1 - t.offsets[b], length(r₁))
71+
return (gettokenvalue(t.trees₁, first(r₁) + j₁), gettokenvalue(t.trees₂, first(r₂) + j₂))
72+
end
73+
Base.iterate(t::FusionTreePairs) = iterate(t, (1, 0, 1))
74+
function Base.iterate(t::FusionTreePairs, (b, i₁, i₂))
75+
while b <= length(t.ranges₁)
76+
r₁, r₂ = t.ranges₁[b], t.ranges₂[b]
77+
i₁ += 1
78+
i₁ > length(r₁) && (i₁ = 1; i₂ += 1)
79+
i₂ <= length(r₂) && !isempty(r₁) &&
80+
return (gettokenvalue(t.trees₁, first(r₁) + i₁ - 1), gettokenvalue(t.trees₂, first(r₂) + i₂ - 1)), (b, i₁, i₂)
81+
b, i₁, i₂ = b + 1, 0, 1
82+
end
83+
return nothing
84+
end
85+
86+
# values for every fusion tree pair, sharing the tokens of `FusionTreePairs`
87+
struct FusionTreePairDict{K, V, T <: FusionTreePairs} <: AbstractDictionary{K, V}
88+
keys::T
89+
values::Vector{V}
90+
end
91+
FusionTreePairDict(keys::T, values::Vector{V}) where {T <: FusionTreePairs, V} =
92+
FusionTreePairDict{eltype(T), V, T}(keys, values)
93+
Base.keys(d::FusionTreePairDict) = d.keys
94+
Dictionaries.istokenizable(::FusionTreePairDict) = true
95+
Dictionaries.istokenassigned(::FusionTreePairDict, _) = true
96+
Dictionaries.gettokenvalue(d::FusionTreePairDict, i::Int) = d.values[i]
97+
Dictionaries.gettokenvalue(d::FusionTreePairDict, (_, i)::Tuple{Int, Int}) = d.values[i]
98+
2499
Base.@assume_effects :foldable function sectorstructuretype(key::Hashed{S}) where {S <: HomSpace}
25100
I = sectortype(S)
26-
F = fusiontreetype(I, numout(S), numin(S))
27-
return SectorStructure{I, F}
101+
F₁, F₂ = fusiontreetype(I, numout(S)), fusiontreetype(I, numin(S))
102+
return SectorStructure{I, Tuple{F₁, F₂}, FusionTreePairs{F₁, F₂}}
28103
end
29104

30105
"""
31106
sectorstructure(W::HomSpace) -> SectorStructure
32107
33-
Return the [`SectorStructure`](@ref) for `W`, containing the coupled sectors and fusion tree
34-
pairs as `Indices`. The result is cached based on the sector structure of `W` (ignoring
108+
Return the [`SectorStructure`](@ref) for `W`, containing the coupled sectors as `Indices` and the
109+
fusion tree pairs as `FusionTreePairs`. The result is cached based on the sector structure of `W` (ignoring
35110
degeneracy dimensions).
36111
37112
See also [`degeneracystructure`](@ref), [`fusiontrees`](@ref), [`blocksectors`](@ref).
@@ -40,35 +115,12 @@ sectorstructure(W::HomSpace) = sectorstructure(Hashed(W, sectorhash, sectorequal
40115

41116
@cached function sectorstructure(key::Hashed{S})::sectorstructuretype(key) where {S <: HomSpace}
42117
W = parent(key)
43-
codom, dom = codomain(W), domain(W)
44-
45-
I = sectortype(S)
46-
F = fusiontreetype(I, numout(S), numin(S))
47-
bs = Vector{I}()
48-
trees = Vector{F}()
49-
50-
for c in _blocksectors(W)
51-
push!(bs, c)
52-
offset = length(trees)
53-
n₁ = 0
54-
for f₂ in fusiontrees(dom, c)
55-
if n₁ == 0
56-
# First f₂ for this sector: enumerate codomain trees and record how many there are.
57-
for f₁ in fusiontrees(codom, c)
58-
push!(trees, (f₁, f₂))
59-
end
60-
n₁ = length(trees) - offset
61-
else
62-
# Subsequent f₂s: the codomain trees are already in the list at
63-
# offset .+ (1:n₁), so read them back instead of recomputing.
64-
for j in offset .+ (1:n₁)
65-
push!(trees, (trees[j][1], f₂))
66-
end
67-
end
68-
end
69-
end
70-
71-
return SectorStructure{I, F}(Indices(bs), Indices(trees))
118+
# the trees of both sides of every coupled sector; the pairs are not stored
119+
bs = collect(_blocksectors(W))
120+
F₁, F₂ = fusiontreetype(sectortype(S), numout(S)), fusiontreetype(sectortype(S), numin(S))
121+
sides₁ = map(c -> collect(fusiontrees(codomain(W), c)), bs)
122+
sides₂ = map(c -> collect(fusiontrees(domain(W), c)), bs)
123+
return sectorstructuretype(key)(Indices(bs), FusionTreePairs{F₁, F₂}(sides₁, sides₂))
72124
end
73125

74126
CacheStyle(::typeof(sectorstructure), ::Hashed{<:HomSpace}) = GlobalLRUCache()
@@ -117,72 +169,38 @@ See also [`sectorstructure`](@ref), [`blockstructure`](@ref), [`subblockstructur
117169
N = length(codom) + length(dom)
118170

119171
ss = sectorstructure(W)
120-
treelist = ss.fusiontrees
121-
L = length(treelist)
122-
structurevalues = sizehint!(Vector{StridedStructure{N}}(), L)
172+
ts = ss.fusiontrees
173+
structurevalues = Vector{StridedStructure{N}}(undef, length(ts))
123174
blockvalues = Vector{Tuple{Tuple{Int, Int}, UnitRange{Int}}}(undef, length(ss.blocksectors))
124175

125-
# temporary data structures
126-
splittingstructure = Vector{NTuple{numout(W), Int}}()
127-
176+
# the subblocks of every coupled sector, from the degeneracies of the trees of both sides
128177
blockoffset = 0
129-
tree_index = 1
130-
block_index = 1
131-
while tree_index <= L
132-
f₁, f₂ = gettokenvalue(treelist, tree_index)
133-
c = f₁.coupled
134-
135-
# compute subblock structure
136-
# splitting tree data
137-
empty!(splittingstructure)
138-
offset₁ = 0
139-
for i in tree_index:L
140-
f₁′, f₂′ = gettokenvalue(treelist, i)
141-
f₂′ == f₂ || break
142-
s₁ = f₁′.uncoupled
143-
d₁s = dims(codom, s₁)
144-
d₁ = prod(d₁s)
145-
offset₁ += d₁
146-
push!(splittingstructure, d₁s)
147-
end
148-
blockdim₁ = offset₁
149-
n₁ = length(splittingstructure)
178+
tree_index = 0
179+
for (block_index, (r₁, r₂)) in enumerate(zip(ts.ranges₁, ts.ranges₂))
180+
d₁ss = map(i -> dims(codom, gettokenvalue(ts.trees₁, i).uncoupled), r₁)
181+
d₂ss = map(i -> dims(dom, gettokenvalue(ts.trees₂, i).uncoupled), r₂)
182+
blockdim₁ = sum(prod, d₁ss; init = 0)
183+
blockdim₂ = sum(prod, d₂ss; init = 0)
150184
strides = (1, blockdim₁)
151-
152-
# fusion tree data and combine
153185
offset₂ = 0
154-
n₂ = 0
155-
for i in tree_index:n₁:L
156-
f₁′, f₂′ = gettokenvalue(treelist, i)
157-
f₂′.coupled == c || break
158-
n₂ += 1
159-
s₂ = f₂′.uncoupled
160-
d₂s = dims(dom, s₂)
186+
for d₂s in d₂ss
161187
d₂ = prod(d₂s)
162188
offset₁ = 0
163-
for d₁s in splittingstructure
189+
for d₁s in d₁ss
164190
d₁ = prod(d₁s)
165191
totaloffset = blockoffset + offset₂ * blockdim₁ + offset₁
166192
subsz = (d₁s..., d₂s...)
167193
@assert !any(==(0), subsz)
168194
substr = _subblock_strides(subsz, (d₁, d₂), strides)
169-
push!(structurevalues, (subsz, substr, totaloffset))
195+
structurevalues[tree_index += 1] = (subsz, substr, totaloffset)
170196
offset₁ += d₁
171197
end
172198
offset₂ += d₂
173199
end
174-
175-
# compute block structure
176-
blockdim₂ = offset₂
177200
blockrange = (blockoffset + 1):(blockoffset + blockdim₁ * blockdim₂)
178201
blockvalues[block_index] = ((blockdim₁, blockdim₂), blockrange)
179-
180-
# reset
181202
blockoffset = last(blockrange)
182-
tree_index += n₁ * n₂
183-
block_index += 1
184203
end
185-
@assert length(structurevalues) == L
186204

187205
return DegeneracyStructure(blockoffset, blockvalues, structurevalues)
188206
end

0 commit comments

Comments
 (0)