Skip to content

Commit 2a04bdc

Browse files
lkdvosclaude
andauthored
Intercept TO.tensoradd! instead of TensorKit.add_transform! (#78)
* Intercept `TO.tensoradd!` instead of `TensorKit.add_transform!` The blockwise `tensoradd` implementations were only reachable through `TensorKit`'s internals: `TO.tensoradd!` delegated to `permute!`, and the `add_transform!` methods defined here caught the kernel. Neither is a contract `TensorKit` owes us, and both stopped holding on TensorKit's index-manipulation refactor (QuantumKitHub/TensorKit.jl#526), where `TO.tensoradd!` calls the braid kernel directly and `add_transform!` gained a `conjsrc` argument. Nothing errors in that case: dense block tensors survive via TensorKit's generic subblock fallback, while sparse ones silently produce zeros, because the fallback writes through blocks a sparse container never materialized. Implement `TO.tensoradd!` for the block tensor types instead. That is the public entry point `@tensor` lowers to, so it is reachable no matter how TensorKit arranges its kernels, and `conjA` is handled explicitly rather than relying on it having been unwrapped into an adjoint beforehand. The mixed block/plain methods take the concrete `TensorMap` so that `(BlockTensorMap, SparseBlockTensorMap)` and the reverse resolve to the general method rather than being ambiguous. The added testset calls `TO.tensoradd!` directly rather than through `@tensor`, so this stays covered independently of TensorKit's routing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Pin the planar entry points in the tests `planaradd!`, `planartrace!` and `planarcontract!` are reached through `transpose!`, `trace_permute!` and `contract!`, so they currently land on methods defined here and are not affected by TensorKit#526. They rest on the same undocumented delegation that broke `tensoradd!` though, and there was no planar coverage here at all, so pin them directly. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 2304c56 commit 2a04bdc

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

‎src/tensors/tensoroperations.jl‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
11
# TensorOperations
22
# ----------------
3+
4+
# tensoradd!
5+
# ----------
6+
# `TensorKit` is free to reorganize its own index manipulation kernels, and `TO.tensoradd!` does
7+
# not necessarily route through `permute!` or `add_transform!`. Intercepting the public
8+
# `TO.tensoradd!` entry point instead keeps the blockwise implementations reachable regardless.
9+
function TO.tensoradd!(
10+
C::BlockTensorMap, A::BlockTensorMap, pA::Index2Tuple, conjA::Bool,
11+
α::Number, β::Number, backend, allocator
12+
)
13+
Cdata = parent(C)
14+
Adata = permutedims(StridedView(parent(A)), (pA[1]..., pA[2]...))
15+
@inbounds for I in eachindex(Cdata, Adata)
16+
Cdata[I] = TO.tensoradd!(Cdata[I], Adata[I], pA, conjA, α, β, backend, allocator)
17+
end
18+
return C
19+
end
20+
function TO.tensoradd!(
21+
C::AbstractBlockTensorMap, A::AbstractBlockTensorMap, pA::Index2Tuple, conjA::Bool,
22+
α::Number, β::Number, backend, allocator
23+
)
24+
scale!(C, β)
25+
p_lin = (pA[1]..., pA[2]...)
26+
@inbounds for (I, v) in nonzero_pairs(A)
27+
I′ = CartesianIndex(TT.getindices(I.I, p_lin))
28+
C[I′] = TO.tensoradd!(C[I′], v, pA, conjA, α, One(), backend, allocator)
29+
end
30+
return C
31+
end
32+
# adjoints are absorbed into the conjugation flag and the permutation
33+
function TO.tensoradd!(
34+
C::AbstractBlockTensorMap, A::AdjointBlockTensorMap, pA::Index2Tuple, conjA::Bool,
35+
α::Number, β::Number, backend, allocator
36+
)
37+
return TO.tensoradd!(C, A', adjointtensorindices(A, pA), !conjA, α, β, backend, allocator)
38+
end
39+
# a block tensor holding a single block is interchangeable with that block
40+
function TO.tensoradd!(
41+
C::TensorMap, A::BlockTensorMap, pA::Index2Tuple, conjA::Bool,
42+
α::Number, β::Number, backend, allocator
43+
)
44+
return TO.tensoradd!(C, only(A), pA, conjA, α, β, backend, allocator)
45+
end
46+
function TO.tensoradd!(
47+
C::BlockTensorMap, A::TensorMap, pA::Index2Tuple, conjA::Bool,
48+
α::Number, β::Number, backend, allocator
49+
)
50+
TO.tensoradd!(only(C), A, pA, conjA, α, β, backend, allocator)
51+
return C
52+
end
53+
354
function TO.tensoradd_type(
455
TC, A::AbstractBlockTensorMap, ::Index2Tuple{N₁, N₂}, ::Bool
556
) where {N₁, N₂}

‎test/linalg/tensoroperations.jl‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using BlockTensorKit
33
using TensorKit
44
using TensorOperations
55
using Random
6+
using LinearAlgebra: norm
67

78
##
89
Vtr = (
@@ -50,6 +51,64 @@ end
5051
end
5152
##
5253

54+
# `TO.tensoradd!` is called directly here rather than through `@tensor`, so that the blockwise
55+
# implementations stay covered independently of how TensorKit routes its index manipulations.
56+
@testset "tensoradd! entry point" begin
57+
for T in (Float32, ComplexF32), Asparse in (false, true), p in (((3, 2, 1, 5, 4), ()), ((4, 5), (1, 3, 2))),
58+
conjA in (false, true)
59+
A = !Asparse ? randn(T, W) : sprand(T, W, 0.5)
60+
C = TensorOperations.tensoralloc_add(T, A, p, conjA, Val(false))
61+
Cref = TensorOperations.tensoralloc_add(T, convert(TensorMap, A), p, conjA, Val(false))
62+
TensorOperations.tensoradd!(
63+
C, A, p, conjA, one(T), zero(T),
64+
TensorOperations.DefaultBackend(), TensorOperations.DefaultAllocator()
65+
)
66+
TensorOperations.tensoradd!(
67+
Cref, convert(TensorMap, A), p, conjA, one(T), zero(T),
68+
TensorOperations.DefaultBackend(), TensorOperations.DefaultAllocator()
69+
)
70+
@test convert(TensorMap, C) ≈ Cref
71+
@test norm(C) ≈ norm(A)
72+
end
73+
end
74+
##
75+
76+
# The planar entry points are reached through `transpose!`, `trace_permute!` and `contract!`
77+
# rather than through methods defined here, so they are pinned separately: a change in how
78+
# TensorKit routes them would otherwise go unnoticed until it reached users.
79+
@testset "planar entry points" begin
80+
for T in (Float32, ComplexF32), Asparse in (false, true)
81+
A = !Asparse ? randn(T, W) : sprand(T, W, 0.5)
82+
Ad = convert(TensorMap, A)
83+
# `(p₁..., reverse(p₂)...)` must be a cyclic rotation of (1, 2, 3, 5, 4) for this `W`
84+
for p in (((1, 2, 3), (4, 5)), ((1, 2), (4, 5, 3)), ((2, 3, 5), (1, 4)), ((3, 5, 4), (2, 1)))
85+
C = TensorOperations.tensoralloc_add(T, A, p, false, Val(false))
86+
Cd = TensorOperations.tensoralloc_add(T, Ad, p, false, Val(false))
87+
TensorKit.planaradd!(C, A, p, one(T), zero(T))
88+
TensorKit.planaradd!(Cd, Ad, p, one(T), zero(T))
89+
@test convert(TensorMap, C) ≈ Cd
90+
@test norm(C) ≈ norm(A)
91+
end
92+
93+
# the planar order of a 2 <- 2 tensor is (1, 2, 4, 3), so legs 2 and 4 are the
94+
# cyclically adjacent pair that can be traced
95+
WB = W[1] ⊗ W[2] ← W[3] ⊗ W[2]
96+
B = !Asparse ? randn(T, WB) : sprand(T, WB, 0.7)
97+
@planar C1[a; b] := B[a c; b c]
98+
@planar C2[a; b] := convert(TensorMap, B)[a c; b c]
99+
@test convert(TensorMap, C1) ≈ C2
100+
101+
WA, WB = W[1] ← W[3], W[3] ← W[2]'
102+
D = !Asparse ? randn(T, WA) : sprand(T, WA, 0.7)
103+
E = !Asparse ? randn(T, WB) : sprand(T, WB, 0.7)
104+
@planar F1[a; b] := D[a; c] * E[c; b]
105+
@planar F2[a; b] := convert(TensorMap, D)[a; c] * convert(TensorMap, E)[c; b]
106+
@test convert(TensorMap, F1) ≈ F2
107+
end
108+
end
109+
110+
##
111+
53112
@testset "tensortrace" begin
54113
for T in (Float32, ComplexF32)
55114
A = randn(T, W[1] ⊗ W[2] ← W[2] ⊗ W[3])

0 commit comments

Comments
 (0)