Skip to content

Commit 095fcbe

Browse files
lkdvosclaude
andcommitted
Drop planaralloc_contract
Now that `@planar` emits canonical index tuples, its allocations no longer need to be canonicalized at runtime and can go through `tensoralloc_contract` again. Canonicalizing before the planar operations are inserted also means that the contraction and its allocation still share a single argument layout. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 49f4884 commit 095fcbe

8 files changed

Lines changed: 22 additions & 52 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
2222

2323
### Added
2424

25-
- `planarcontract!` now supports an arbitrary (cyclic) output permutation `pAB`, in the same way as the non-planar `tensorcontract!`, along with the new internal helpers `TensorKit.planar_contract_indices` and `TensorKit.planaralloc_contract`. ([#531](https://github.com/QuantumKitHub/TensorKit.jl/pull/531))
25+
- `planarcontract!` now supports an arbitrary (cyclic) output permutation `pAB`, in the same way as the non-planar `tensorcontract!`, along with the new helper `TensorKit.planar_contract_indices`, which `@planar` uses to emit canonical index tuples. ([#531](https://github.com/QuantumKitHub/TensorKit.jl/pull/531))
2626

2727
### Changed
2828
- For sector types with `GenericUnit` such that colorings are not unique, `GradedSpace`, `ProductSpace` and `HomSpace` now check for this compatibility. In particular, this prevents the construction of `TensorMap`s with incompatible colorings, which previously either errored or produced empty tensors inconsistently. ([#515](https://github.com/QuantumKitHub/TensorKit.jl/pull/515))
29-
- `TensorOperations.tensorcontract_structure` now requires the index tuples `pA` and `pB` to be planar (cyclic) partitions for sector types with `GenericUnit()`; planar code should allocate through `TensorKit.planaralloc_contract`, which canonicalizes them. ([#531](https://github.com/QuantumKitHub/TensorKit.jl/pull/531))
29+
- `TensorOperations.tensorcontract_structure` now requires the index tuples `pA` and `pB` to be planar (cyclic) partitions for sector types with `GenericUnit()`; hand-written planar kernels should canonicalize their index tuples with `TensorKit.planar_contract_indices` before allocating. ([#531](https://github.com/QuantumKitHub/TensorKit.jl/pull/531))
3030

3131
### Deprecated
3232

‎docs/src/man/contractions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Any layout the macro cannot identify as planar is rejected at parse time with `A
291291

292292
For users writing their own planar kernels, it is important to note that the index tuples `pA = (oindA, cindA)` and `pB = (cindB, oindB)` handed to `planarcontract!` need not individually be planar partitions of the operands, even when the overall diagram is planar; only a cyclic rotation of them is.
293293
The function `TensorKit.planar_contract_indices(A, pA, B, pB, pAB)` returns the canonical `pA′`, `pB′` and the remaining output permutation `pAB′`, and these canonical tuples are the only ones for which the intermediate spaces exist for sector types with multiple units (i.e. multifusion categories with `GenericUnit()`).
294-
Consequently, destinations for planar contractions must be allocated with `TensorKit.planaralloc_contract` — which is what the `@planar` macro emits — rather than with `TensorOperations.tensoralloc_contract`.
294+
The `@planar` macro applies it to the tuples it emits, so hand-written planar kernels should do the same before allocating a destination with `TensorOperations.tensoralloc_contract`.
295295

296296
To make this concrete, consider the contraction `A * B` for two anyonic tensors, written in a manifestly planar fashion:
297297

‎ext/TensorKitChainRulesCoreExt/tensoroperations.jl‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ function ChainRulesCore.rrule(
198198
return C′, pullback
199199
end
200200

201-
@non_differentiable TensorKit.planaralloc_contract(args...)
202-
203201
function ChainRulesCore.rrule(::typeof(TensorKit.scalar), t::AbstractTensorMap)
204202
val = scalar(t)
205203
function scalar_pullback(Δval)

‎ext/TensorKitMooncakeExt/utility.jl‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Mooncake.tangent_type(::Type{<:HomSpace}) = Mooncake.NoTangent
1919
@zero_derivative DefaultCtx Tuple{typeof(TensorOperations.tensorstructure), AbstractTensorMap, Int, Bool}
2020

2121
@zero_derivative DefaultCtx Tuple{typeof(TensorOperations.tensorcontract_structure), AbstractTensorMap, Index2Tuple, Bool, AbstractTensorMap, Index2Tuple, Bool, Index2Tuple}
22-
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.planaralloc_contract), Any}
2322

2423
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.planar_trace), TensorKit.FusionTreePair, Index2Tuple, Index2Tuple}
2524
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.has_shared_permute), AbstractTensorMap, Index2Tuple}

‎src/planar/macros.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ function planarparser(planarexpr, kwargs...)
1919
temporaries = Vector{Symbol}()
2020
push!(parser.postprocessors, ex -> _annotate_temporaries(ex, temporaries))
2121
push!(parser.postprocessors, ex -> _free_temporaries(ex, temporaries))
22-
push!(parser.postprocessors, _insert_planar_operations)
2322

2423
partitions = Dict{Any, Union{Nothing, IndexPartition}}()
2524
push!(parser.postprocessors, ex -> canonicalizeplanarindices(ex, partitions))
25+
push!(parser.postprocessors, _insert_planar_operations)
2626

2727
# braiding tensors need to be instantiated before kwargs are processed
2828
push!(parser.preprocessors, _construct_braidingtensors)

‎src/planar/planaroperations.jl‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ spaces, so all space computations should use them. `A` and `B` can be anything s
256256
`codomainind` and `domainind`, in particular `AbstractTensorMap`s, `HomSpace`s and
257257
[`IndexPartition`](@ref)s.
258258
259-
See also [`planarcontract!`](@ref) and [`planaralloc_contract`](@ref).
259+
See also [`planarcontract!`](@ref).
260260
"""
261261
function planar_contract_indices(
262262
A, (oindA, cindA)::Index2Tuple,
@@ -286,20 +286,3 @@ function planar_contract_indices(
286286
pAB′ = (TupleTools.getindices(remap, pAB[1]), TupleTools.getindices(remap, pAB[2]))
287287
return (oindA′, cindA′), (cindB′, oindB′), pAB′
288288
end
289-
290-
"""
291-
planaralloc_contract(TC, A, pA, B, pB, pAB, [istemp, allocator])
292-
293-
Allocate the destination of `planarcontract!(C, A, pA, B, pB, pAB, α, β)`.
294-
295-
The planar counterpart of `TensorOperations.tensoralloc_contract`: the index tuples are
296-
canonicalized with [`planar_contract_indices`](@ref) first, such that the space computation
297-
only involves valid intermediate spaces.
298-
"""
299-
function planaralloc_contract(
300-
TC, A, pA::Index2Tuple, B, pB::Index2Tuple, pAB::Index2Tuple,
301-
istemp::Val = Val(false), allocator = TO.DefaultAllocator()
302-
)
303-
pA′, pB′, pAB′ = planar_contract_indices(A, pA, B, pB, pAB)
304-
return TO.tensoralloc_contract(TC, A, pA′, false, B, pB′, false, pAB′, istemp, allocator)
305-
end

‎src/planar/postprocessors.jl‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ end
5353

5454
# TODO: replace _planarmethod with planarmethod in everything below
5555
const _PLANAR_OPERATIONS = (:planaradd!, :planartrace!, :planarcontract!)
56-
const _PLANAR_ALLOCATIONS = (:planaralloc_contract,)
57-
const _PLANAR_CONTRACTIONS = (:planarcontract!, :planaralloc_contract)
56+
57+
# `TO.instantiate` emits these with the same argument layout, i.e. with the index tuples of
58+
# the contraction at positions 3 (`A`), 4 (`pA`), 6 (`B`), 7 (`pB`) and 9 (`pAB`)
59+
const _CONTRACTION_INSTANTIATIONS = (:tensorcontract!, :tensoralloc_contract)
5860

5961
function _insert_planar_operations(ex)
6062
if isexpr(ex, :call)
@@ -80,14 +82,6 @@ function _insert_planar_operations(ex)
8082
ex.head, GlobalRef(TensorKit, Symbol(:planartrace!)),
8183
map(_insert_planar_operations, ex.args[2:end])...
8284
)
83-
elseif ex.args[1] == GlobalRef(TensorOperations, :tensoralloc_contract)
84-
conjB = popat!(ex.args, 8)
85-
conjA = popat!(ex.args, 5)
86-
@assert !conjA && !conjB "conj flags should be disabled ($conjA), ($conjB)"
87-
return Expr(
88-
ex.head, GlobalRef(TensorKit, Symbol(:planaralloc_contract)),
89-
map(_insert_planar_operations, ex.args[2:end])...
90-
)
9185
elseif ex.args[1] in TensorOperations.tensoroperationsfunctions
9286
return Expr(
9387
ex.head, GlobalRef(TensorOperations, ex.args[1]),
@@ -103,19 +97,20 @@ end
10397
"""
10498
canonicalizeplanarindices(ex, partitions)
10599
106-
Replace the index tuples of every planar contraction in `ex` by their canonical form, as
107-
obtained from [`planar_contract_indices`](@ref) and the index partitions in `partitions`.
108-
Contractions whose operands have no known partition are left to be canonicalized at runtime.
100+
Replace the index tuples of every contraction in `ex` by their canonical form, as obtained
101+
from [`planar_contract_indices`](@ref) and the index partitions in `partitions`. Contractions
102+
whose operands have no known partition are left untouched.
109103
"""
110104
function canonicalizeplanarindices(ex, partitions)
111-
if isexpr(ex, :call) && length(ex.args) ≥ 7 && ex.args[1] isa GlobalRef &&
112-
ex.args[1].mod === TensorKit && ex.args[1].name ∈ _PLANAR_CONTRACTIONS
113-
A, pA, B, pB, pAB = ex.args[3], ex.args[4], ex.args[5], ex.args[6], ex.args[7]
105+
if isexpr(ex, :call) && length(ex.args) ≥ 9 && ex.args[1] isa GlobalRef &&
106+
ex.args[1].mod === TensorOperations &&
107+
ex.args[1].name ∈ _CONTRACTION_INSTANTIATIONS
108+
A, pA, B, pB, pAB = ex.args[3], ex.args[4], ex.args[6], ex.args[7], ex.args[9]
114109
WA, WB = get(partitions, A, nothing), get(partitions, B, nothing)
115110
if !isnothing(WA) && !isnothing(WB) && pA isa Index2Tuple &&
116111
pB isa Index2Tuple && pAB isa Index2Tuple
117112
args = copy(ex.args)
118-
args[4], args[6], args[7] = planar_contract_indices(WA, pA, WB, pB, pAB)
113+
args[4], args[7], args[9] = planar_contract_indices(WA, pA, WB, pB, pAB)
119114
return Expr(:call, args...)
120115
end
121116
end
@@ -149,11 +144,10 @@ end
149144
"""
150145
insertplanarallocator(ex, allocator)
151146
152-
Insert the allocator argument into the tensor operation methods `planaradd!`, `planartrace!`,
153-
`planarcontract!`, and `planaralloc_contract`.
147+
Insert the allocator argument into the tensor operation methods `planaradd!`, `planartrace!`, and `planarcontract!`.
154148
155149
See also: [`TensorOperations.insertallocator`](@ref).
156150
"""
157151
function insertplanarallocator(ex, allocator)
158-
return _insertargument(ex, allocator, (_PLANAR_OPERATIONS..., _PLANAR_ALLOCATIONS...))
152+
return _insertargument(ex, allocator, _PLANAR_OPERATIONS)
159153
end

‎test/tensors/planar.jl‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ end
161161
T = ComplexF64
162162

163163
@testset "backend and allocator insertion" begin
164+
PLANAR_OPERATIONS = (:planaradd!, :planartrace!, :planarcontract!)
164165
# trailing arguments of every call in `ex` whose name is in `names`
165166
function planartrailing(ex, names, out = Any[])
166167
ex isa Expr || return out
@@ -174,22 +175,17 @@ end
174175

175176
ex = @macroexpand @planar backend = MarkerBackend() C[i; j] := A[i; k l] *
176177
τ[k l; m n] * B[m n; j]
177-
trailing = planartrailing(ex, (:planaradd!, :planartrace!, :planarcontract!))
178+
trailing = planartrailing(ex, PLANAR_OPERATIONS)
178179
@test !isempty(trailing)
179180
@test all(==(:(MarkerBackend())), trailing)
180181

181182
# an allocator implies a default backend, and both land on the planar calls
182183
ex = @macroexpand @planar allocator = MarkerAllocator() C[i; j] := A[i; k l] *
183184
τ[k l; m n] * B[m n; j]
184-
trailing = planartrailing(
185-
ex, (:planaradd!, :planartrace!, :planarcontract!, :planaralloc_contract)
186-
)
185+
trailing = planartrailing(ex, PLANAR_OPERATIONS)
187186
@test !isempty(trailing)
188187
@test all(==(:(MarkerAllocator())), trailing)
189188
@test occursin("DefaultBackend", string(ex))
190-
191-
alloc_trailing = planartrailing(ex, (:planaralloc_contract,))
192-
@test !isempty(alloc_trailing)
193189
end
194190

195191
@testset "canonical index tuples" begin

0 commit comments

Comments
 (0)