Skip to content
Draft
3 changes: 3 additions & 0 deletions docs/src/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
- `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
- 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
- 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
- `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) ([#600](https://github.com/QuantumKitHub/TensorKit.jl/pull/600))
- `degeneracystructure(W)` stores the sizes, strides and offsets of the splitting and fusion trees instead of those of every subblock, from which the structure of a subblock is computed on access, and the tree transformers address subblocks by the tokens of their fusion tree pairs. This makes the cached structures considerably smaller and cheaper to build (internal) ([#600](https://github.com/QuantumKitHub/TensorKit.jl/pull/600))
- Tree and trace transformers for sectors with multiple fusion channels keep the subblock tokens of all their blocks in one vector of `Int32` pairs, instead of two `Vector{Tuple{Int, Int}}` per block

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

Expand Down
3 changes: 2 additions & 1 deletion ext/TensorKitGPUArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,8 @@ function diagonal_device_transformer(A, transformer, dstdiagonal::Bool)
n = first(sz)
return UniqueTransformerBlock{typeof(coeff), 1}(
coeff, (n,), (1,), dstdiagonal ? (1,) : (sum(strides_dst),),
dstdiagonal ? offsets_dst[idst] : offset_dst, (1,), offsets_src[isrc]
dstdiagonal ? offsets_dst[TensorKit.pairposition(structure_dst, idst)] : offset_dst, (1,),
offsets_src[TensorKit.pairposition(structure_src, isrc)]
)
end
work_offsets, nwork = _work_offsets(blk.sz[1] for blk in blocks)
Expand Down
2 changes: 1 addition & 1 deletion src/TensorKit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const TO = TensorOperations

using MatrixAlgebraKit

using Dictionaries: Dictionaries, Dictionary, Indices, gettoken, gettokenvalue
using Dictionaries: Dictionaries, AbstractDictionary, AbstractIndices, Dictionary, Indices, gettoken, gettokenvalue
using OhMyThreads
using PtrArrays: PtrArray
using ScopedValues
Expand Down
7 changes: 4 additions & 3 deletions src/pullbacks/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ function _planartrace_pullback_ΔA!(
ΔA, ΔC, transformer::TraceTransformer, (p₁, p₂), (q₁, q₂), α, backend, allocator
)
lp = (p₁..., p₂...)
dims_dst = [dim(f₁.coupled) for (f₁, _) in fusiontrees(ΔC)]
dims_src = [dim(f₁.coupled) for (f₁, _) in fusiontrees(ΔA)]
trees_dst, trees_src = fusiontrees(ΔC), fusiontrees(ΔA)
dims_dst = [dim(f₁.coupled) for (f₁, _) in trees_dst]
dims_src = [dim(f₁.coupled) for (f₁, _) in trees_src]
dst, src = _transform_subblocks(ΔC, ΔA, transformer)
for (U, inds_dst, inds_src) in transformer.data, (j, isrc) in enumerate(inds_src)
# the traced diagonal of the source subblock, with the remaining legs permuted by `p`
Expand All @@ -112,7 +113,7 @@ function _planartrace_pullback_ΔA!(
parent(c), (size(c)..., sz_traced...), (strides(c)..., map(zero, sz_traced)...),
c.offset, c.op
)
coeff = conj(α * U[i, j]) * (dims_dst[idst] / dims_src[isrc])
coeff = conj(α * U[i, j]) * (dims_dst[pairposition(trees_dst, idst)] / dims_src[pairposition(trees_src, isrc)])
TO.tensoradd!(diag, ΔCi, pdiag, false, coeff, One(), backend, allocator)
end
end
Expand Down
57 changes: 40 additions & 17 deletions src/spaces/homspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ See also [`degeneracystructure`](@ref), [`subblockstructure`](@ref).
blockstructure(W::HomSpace) = Dictionary(blocksectors(W), degeneracystructure(W).blockstructure)

"""
fusiontrees(W::HomSpace) -> Indices{Tuple{F₁,F₂}}
fusiontrees(W::HomSpace) -> FusionTreePairs{I,F₁,F₂}

Return an `Indices` of all valid fusion tree pairs `(f₁, f₂)` for `W`, providing a
bijection to sequential integer positions via `gettoken`/`gettokenvalue`. The result is
Return the `FusionTreePairs` of all valid fusion tree pairs `(f₁, f₂)` for `W`, an
`AbstractIndices` in canonical order. `gettokenvalue` with an integer returns the pair at that
position, and `pairposition` returns the position of a pair. The result is
cached based on the sector structure of `W` (ignoring degeneracy dimensions), so
`HomSpace`s that share the same sectors, dualities, and index count will reuse the same
object.
Expand All @@ -192,14 +193,14 @@ See also [`sectorstructure`](@ref), [`subblockstructure`](@ref).
fusiontrees(W::HomSpace) = sectorstructure(W).fusiontrees

"""
subblockstructure(W::HomSpace) -> Dictionary
subblockstructure(W::HomSpace) -> AbstractDictionary

Return a `Dictionary` mapping each fusion tree pair `(f₁, f₂)` to its
Return an `AbstractDictionary` mapping each fusion tree pair `(f₁, f₂)` to its
[`StridedStructure`](@ref) `(sizes, strides, offset)`.

See also [`degeneracystructure`](@ref), [`blockstructure`](@ref).
"""
subblockstructure(W::HomSpace) = Dictionary(fusiontrees(W), degeneracystructure(W).subblockstructure)
subblockstructure(W::HomSpace) = FusionTreePairDict(fusiontrees(W), degeneracystructure(W).subblockstructure)

"""
fusionblocks(W::HomSpace)
Expand All @@ -211,19 +212,41 @@ function fusionblocks(W::HomSpace)
I = sectortype(W)
N₁, N₂ = numout(W), numin(W)
F = fusiontreetype(I, N₁, N₂)
# group the (cached) fusion trees of `W` by their uncoupled sectors,
# in the order in which these first occur
blocks = FusionTreeBlock{I, N₁, N₂, F}[]
trees_of = Dict{Tuple{NTuple{N₁, I}, NTuple{N₂, I}}, Vector{F}}()
for (f₁, f₂) in fusiontrees(W)
trees = get!(trees_of, (f₁.uncoupled, f₂.uncoupled)) do
newtrees = F[]
push!(blocks, FusionTreeBlock(newtrees))
return newtrees
ts = fusiontrees(W)
# group the (cached) splitting and fusion trees of every coupled sector of `W` by their
# uncoupled sectors: a block combines the groups of both sides with the same uncoupled
# sectors, in the order in which these first occur in `fusiontrees(W)`
parts = Vector{Tuple{Vector{Int}, Vector{Int}}}[]
part_of = Dict{Tuple{NTuple{N₁, I}, NTuple{N₂, I}}, Int}()
for (r₁, r₂) in zip(ts.ranges₁, ts.ranges₂)
g₁ = _groupbyuncoupled(NTuple{N₁, I}, ts.trees₁, r₁)
g₂ = _groupbyuncoupled(NTuple{N₂, I}, ts.trees₂, r₂)
for (u₂, is₂) in pairs(g₂), (u₁, is₁) in pairs(g₁)
k = get!(part_of, (u₁, u₂)) do
push!(parts, Tuple{Vector{Int}, Vector{Int}}[])
return length(parts)
end
push!(parts[k], (is₂, is₁))
end
push!(trees, (f₁, f₂))
end
return blocks
# every block is filled once, at its final size
return map(parts) do ps
trees = Vector{F}(undef, sum(((is₂, is₁),) -> length(is₂) * length(is₁), ps))
k = 0
for (is₂, is₁) in ps, i₂ in is₂, i₁ in is₁
trees[k += 1] = (gettokenvalue(ts.trees₁, i₁), gettokenvalue(ts.trees₂, i₂))
end
return FusionTreeBlock(trees)
end
end
# the positions in `r` of `trees`, grouped by their uncoupled sectors in the order in which
# these first occur
function _groupbyuncoupled(::Type{U}, trees, r) where {U}
groups = Dictionary{U, Vector{Int}}()
for i in r
push!(get!(Vector{Int}, groups, gettokenvalue(trees, i).uncoupled), i)
end
return groups
end

function diagonalblockstructure(W::HomSpace)
Expand Down
Loading
Loading