From 6efa094364ee009a722c6d3321f9d92b87d2719f Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 26 Nov 2026 13:00:13 +0100 Subject: [PATCH] Fix handling of permute_twist and TreeTransformer for types which overload tensoradd --- src/tensors/tensoroperations.jl | 1 + src/tensors/treetransformers.jl | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/tensors/tensoroperations.jl b/src/tensors/tensoroperations.jl index 8ca3c63b6..0f4b61dfe 100644 --- a/src/tensors/tensoroperations.jl +++ b/src/tensors/tensoroperations.jl @@ -392,6 +392,7 @@ end # `tdst = twist(permute(tsrc, p), twistmask)`, with the twist folded into the coefficients of the permutation function permute_twist!(tdst::AbstractTensorMap, tsrc::AbstractTensorMap, p::Index2Tuple, twistmask, backend, allocator) + any(twistmask) || return TO.tensoradd!(tdst, tsrc, p, false, One(), Zero(), backend, allocator) tsrc′, p′, _, conjsrc = _unwrap_source(tsrc, p, nothing, false) return _braid!(tdst, tsrc′, p′, conjsrc, allind(tsrc′), twistmask, One(), Zero(), backend, allocator) end diff --git a/src/tensors/treetransformers.jl b/src/tensors/treetransformers.jl index 37d9df181..ea3913dd6 100644 --- a/src/tensors/treetransformers.jl +++ b/src/tensors/treetransformers.jl @@ -34,6 +34,12 @@ function recoupling_scalartype(::Type{A}, ::Type{Tₛ}) where {A, Tₛ <: Number return Tₛ <: Real ? real(T) : complex(T) end +function _treeindex(trees, f) + found, (_, i) = gettoken(trees, f) + found || throw(ArgumentError(lazy"fusion tree pair $f not found among the trees of the space")) + return i +end + # (coefficient, destination position, source position) const UniqueTransformerData{T} = Tuple{T, Int, Int} @@ -99,7 +105,7 @@ function UniqueTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::Ho @timeit_debug GLOBAL_TIMER "symmetry: tree transform" for (isrc, (f₁, f₂)) in enumerate(src_trees) f_dst, coeff = transform(conjsrc ? (f₂, f₁) : (f₁, f₂)) - _, (_, idst) = gettoken(dst_trees, f_dst) + idst = _treeindex(dst_trees, f_dst) data[isrc] = (convert(T, coeff), idst, isrc) end @@ -136,12 +142,10 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::H @timeit_debug GLOBAL_TIMER "bookkeeping: subblock positions" begin # the token into the fusion tree `Indices` is the subblock position inds_src = map(fusiontrees(fs_src)) do (f₁, f₂) - _, (_, isrc) = gettoken(src_trees, conjsrc ? (f₂, f₁) : (f₁, f₂)) - return isrc + return _treeindex(src_trees, conjsrc ? (f₂, f₁) : (f₁, f₂)) end inds_dst = map(fusiontrees(fs_dst)) do f - _, (_, idst) = gettoken(dst_trees, f) - return idst + return _treeindex(dst_trees, f) end end data[i] = (convert(Matrix{T}, U), inds_dst, inds_src) @@ -280,7 +284,7 @@ function TraceTransformer(::Type{A}, Vdst::HomSpace, Vsrc::HomSpace, p::Index2Tu for (isrc, f) in enumerate(src_trees) trace_sectors_match(f, q) || continue f′, coeff = planar_trace(f, p, q) - _, (_, idst) = gettoken(dst_trees, f′) + idst = _treeindex(dst_trees, f′) addcolumn!([idst], [convert(T, coeff)], isrc) end else @@ -288,11 +292,10 @@ function TraceTransformer(::Type{A}, Vdst::HomSpace, Vsrc::HomSpace, p::Index2Tu trace_sectors_match(fs_src, q) || continue fs_dst, U = planar_trace(fs_src, p, q) inds_dst = map(fusiontrees(fs_dst)) do f - _, (_, idst) = gettoken(dst_trees, f) - return idst + return _treeindex(dst_trees, f) end for (i, f) in enumerate(fusiontrees(fs_src)) - _, (_, isrc) = gettoken(src_trees, f) + isrc = _treeindex(src_trees, f) addcolumn!(inds_dst, convert(Vector{T}, U[:, i]), isrc) end end