Skip to content

Commit dbab923

Browse files
lkdvosclaude
andcommitted
Generalize planarcontract! to arbitrary pAB, remove _contractedspace
`@planar` allocates through `TO.tensoralloc_contract` with the raw index tuples of the planar decomposition, in which `pA` and `pB` need not be planar partitions by themselves; only `planarcontract!` rotated them into shape, and only when `pAB` could be absorbed into those rotations. Replace `reorder_indices` by `planar_contract_indices`, which canonicalizes `pA` and `pB` and remaps `pAB` instead of absorbing it, let `planarcontract!` apply a residual cyclic `pAB` with `transpose!` through an intermediate, and allocate planar destinations through the new `planaralloc_contract`. Allocate the destination of `⊗` directly, since its intermediates are non-planar by construction. With every caller passing planar partitions, `tensorcontract_structure` can again compose the intermediate spaces, which also restores contractions of operands with different `spacetype` parameters. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 7a187c4 commit dbab923

12 files changed

Lines changed: 246 additions & 104 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +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))
26+
2527
### Changed
2628
- 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))
2730

2831
### Deprecated
2932
- The type alias `ZNSpace{N}` is deprecated in favour of `Vect[ZNIrrep{N}]` or `Rep[ℤ{N}]`: a type alias cannot compute the storage type from `N`, so the two only agree for small `N`. ([#511](https://github.com/QuantumKitHub/TensorKit.jl/pull/511))

‎docs/src/man/contractions.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ Finally, the name `τ` is reserved for the braiding tensor: every literal crossi
289289
The `BraidingTensor` itself does not need to be constructed by the user; the macro figures out the appropriate spaces from the surrounding contraction.
290290
Any layout the macro cannot identify as planar is rejected at parse time with `ArgumentError("not a planar diagram expression: ...")`.
291291

292+
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.
293+
As long as the overall diagram is planar, the function `TensorKit.planar_contract_indices(A, pA, B, pB, pAB)` will return the canonical `pA′`, `pB′` and the remaining output permutation `pAB′` for which the operations are each planar.
294+
These canonical tuples are also the only ones for which the intermediate spaces exist for sector types with multiple units (i.e. multifusion categories with `GenericUnit()`).
295+
The `@planar` macro automatically applies it to the tuples it emits, but hand-written planar kernels should do the same before allocating a destination with `TensorOperations.tensoralloc_contract`.
296+
292297
To make this concrete, consider the contraction `A * B` for two anyonic tensors, written in a manifestly planar fashion:
293298

294299
```@example anyoncontraction

‎ext/TensorKitChainRulesCoreExt/tensoroperations.jl‎

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

201+
@non_differentiable TensorKit.planaralloc_contract(args...)
202+
201203
function ChainRulesCore.rrule(::typeof(TensorKit.scalar), t::AbstractTensorMap)
202204
val = scalar(t)
203205
function scalar_pullback(Δval)

‎ext/TensorKitMooncakeExt/utility.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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}
2223

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

‎src/planar/planaroperations.jl‎

Lines changed: 100 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,11 @@ function planarcontract!(
162162
end
163163

164164
@timeit_debug GLOBAL_TIMER "planarcontract!" begin
165-
codA, domA = codomainind(A), domainind(A)
166-
codB, domB = codomainind(B), domainind(B)
167-
oindA, cindA = pA
168-
cindB, oindB = pB
169-
oindA, cindA, oindB, cindB = reorder_indices(
170-
codA, domA, codB, domB, oindA, cindA, oindB, cindB, pAB...
171-
)
172-
173-
if oindA == codA && cindA == domA
165+
(oindA, cindA), (cindB, oindB), pAB′ = planar_contract_indices(A, pA, B, pB, pAB)
166+
A_in_layout = (oindA, cindA) == (codomainind(A), domainind(A))
167+
B_in_layout = (cindB, oindB) == (codomainind(B), domainind(B))
168+
169+
if A_in_layout
174170
A′ = A
175171
else
176172
A′ = @timeit_debug GLOBAL_TIMER "alloc: buffers" TO.tensoralloc_add(
@@ -179,76 +175,117 @@ function planarcontract!(
179175
transpose!(A′, A, (oindA, cindA), One(), Zero(), backend, allocator)
180176
end
181177

182-
if cindB == codB && oindB == domB
178+
if B_in_layout
183179
B′ = B
184180
else
185-
B′ = @timeit_debug GLOBAL_TIMER "alloc: buffers" TensorOperations.tensoralloc_add(
181+
B′ = @timeit_debug GLOBAL_TIMER "alloc: buffers" TO.tensoralloc_add(
186182
scalartype(B), B, (cindB, oindB), false, Val(true), allocator
187183
)
188184
transpose!(B′, B, (cindB, oindB), One(), Zero(), backend, allocator)
189185
end
190-
mul!(C, A′, B′, α, β)
191-
(oindA == codA && cindA == domA) || TO.tensorfree!(A′, allocator)
192-
(cindB == codB && oindB == domB) || TO.tensorfree!(B′, allocator)
186+
187+
if _isdirectoutput(pAB′, length(oindA))
188+
mul!(C, A′, B′, α, β)
189+
else # as in `blas_contract!`, a non-trivial `pAB` requires an intermediate
190+
AB = @timeit_debug GLOBAL_TIMER "alloc: buffers" TO.tensoralloc_contract(
191+
scalartype(C), A′, (codomainind(A′), domainind(A′)), false,
192+
B′, (codomainind(B′), domainind(B′)), false,
193+
TO.trivialpermutation(length(oindA), length(oindB)), Val(true), allocator
194+
)
195+
mul!(AB, A′, B′, One(), Zero())
196+
transpose!(C, AB, pAB′, α, β, backend, allocator)
197+
TO.tensorfree!(AB, allocator)
198+
end
199+
200+
A_in_layout || TO.tensorfree!(A′, allocator)
201+
B_in_layout || TO.tensorfree!(B′, allocator)
193202
end
194203
return C
195204
end
196205

197206
# auxiliary routines
198-
_cyclicpermute(t::Tuple) = (Base.tail(t)..., t[1])
199-
_cyclicpermute(t::Tuple{}) = ()
200-
201-
function reorder_indices(codA, domA, codB, domB, oindA, oindB, p1, p2)
202-
N₁ = length(oindA)
203-
N₂ = length(oindB)
204-
@assert length(p1) == N₁ && all(in(p1), 1:N₁)
205-
@assert length(p2) == N₂ && all(in(p2), N₁ .+ (1:N₂))
206-
oindA2 = TupleTools.getindices(oindA, p1)
207-
oindB2 = TupleTools.getindices(oindB, p2 .- N₁)
208-
indA = (codA..., reverse(domA)...)
209-
indB = (codB..., reverse(domB)...)
210-
# cycle indA to be of the form (oindA2..., reverse(cindA2)...)
211-
while length(oindA2) > 0 && indA[1] != oindA2[1]
212-
indA = _cyclicpermute(indA)
213-
end
214-
# cycle indB to be of the form (cindB2..., reverse(oindB2)...)
215-
while length(oindB2) > 0 && indB[end] != oindB2[1]
216-
indB = _cyclicpermute(indB)
217-
end
218-
for i in 2:N₁
219-
@assert indA[i] == oindA2[i]
220-
end
221-
for j in 2:N₂
222-
@assert indB[end + 1 - j] == oindB2[j]
207+
# whether a contraction with `N₁` open indices on `A` directly yields the destination
208+
function _isdirectoutput(pAB::Index2Tuple, N₁::Int)
209+
return length(pAB[1]) == N₁ && pAB == TO.trivialpermutation(pAB)
210+
end
211+
212+
# rotate `t` such that `x` comes first
213+
_rotate_to(t::Tuple, x) = TupleTools.circshift(t, 1 - something(findfirst(==(x), t)))
214+
215+
# rotate the cycle `indx` into `(head′..., reverse(tail′)...)`
216+
function _planar_rotate(indx::IndexTuple, head::IndexTuple, tail::IndexTuple)
217+
N₁, N = length(head), length(indx)
218+
# rotate the arc of `head` indices in front; note that `indx` cannot be reassigned
219+
# without boxing it in the closures below
220+
rot = if 0 < N₁ < N
221+
i = findfirst(ntuple(n -> indx[n] ∈ head && indx[mod1(n - 1, N)] ∉ head, Val(N)))
222+
TupleTools.circshift(indx, 1 - something(i))
223+
else
224+
indx
223225
end
224-
Nc = length(indA) - N₁
225-
@assert Nc == length(indB) - N₂
226-
pc = ntuple(identity, Nc)
227-
cindA2 = reverse(TupleTools.getindices(indA, N₁ .+ pc))
228-
cindB2 = TupleTools.getindices(indB, pc)
229-
return oindA2, cindA2, oindB2, cindB2
226+
head′ = ntuple(n -> rot[n], Val(N₁))
227+
tail′ = reverse(ntuple(n -> rot[N₁ + n], Val(length(tail))))
228+
TupleTools.sort(head′) == TupleTools.sort(head) ||
229+
throw(ArgumentError(lazy"$head and $tail do not partition the cycle $indx planarly"))
230+
return head′, tail′
230231
end
231232

232-
function reorder_indices(codA, domA, codB, domB, oindA, cindA, oindB, cindB, p1, p2)
233-
oindA2, cindA2, oindB2, cindB2 = reorder_indices(
234-
codA, domA, codB, domB, oindA, oindB, p1, p2
233+
"""
234+
planar_contract_indices(A, pA, B, pB, pAB) -> pA′, pB′, pAB′
235+
236+
Bring the index tuples of a planar contraction into canonical form, such that `pA′` and `pB′`
237+
are cyclic partitions of the indices of `A` and `B`, i.e. such that
238+
239+
C = transpose(transpose(A, pA′) * transpose(B, pB′), pAB′)
240+
241+
For sector types with `GenericUnit()` these are the only partitions with valid intermediate
242+
spaces, so all space computations should use them. `A` and `B` can be anything supporting
243+
`codomainind` and `domainind`, in particular `AbstractTensorMap`s and `HomSpace`s.
244+
245+
See also [`planarcontract!`](@ref) and [`planaralloc_contract`](@ref).
246+
"""
247+
function planar_contract_indices(
248+
A, (oindA, cindA)::Index2Tuple,
249+
B, (cindB, oindB)::Index2Tuple,
250+
pAB::Index2Tuple
235251
)
252+
indA = (codomainind(A)..., reverse(domainind(A))...)
253+
indB = (codomainind(B)..., reverse(domainind(B))...)
254+
oindA′, cindA′ = _planar_rotate(indA, oindA, cindA)
255+
cindB′, oindB′ = _planar_rotate(indB, cindB, oindB)
236256

237-
#if oindA or oindB are empty, then reorder indices can only order it correctly up to a cyclic permutation!
238-
if isempty(oindA2) && !isempty(cindA)
239-
# isempty(cindA) is a cornercase which I'm not sure if we can encounter
240-
hit = cindA[findfirst(==(first(cindB2)), cindB)]
241-
while hit != first(cindA2)
242-
cindA2 = _cyclicpermute(cindA2)
243-
end
257+
# if all indices are contracted, fix the residual rotation using the other tensor
258+
if isempty(oindA′) && !isempty(cindA)
259+
cindA′ = _rotate_to(cindA′, cindA[something(findfirst(==(first(cindB′)), cindB))])
244260
end
245-
if isempty(oindB2) && !isempty(cindB)
246-
hit = cindB[findfirst(==(first(cindA2)), cindA)]
247-
while hit != first(cindB2)
248-
cindB2 = _cyclicpermute(cindB2)
249-
end
261+
if isempty(oindB′) && !isempty(cindB)
262+
cindB′ = _rotate_to(cindB′, cindB[something(findfirst(==(first(cindA′)), cindA))])
250263
end
251-
@assert TupleTools.sort(cindA) == TupleTools.sort(cindA2)
252-
@assert TupleTools.sort(tuple.(cindA2, cindB2)) == TupleTools.sort(tuple.(cindA, cindB))
253-
return oindA2, cindA2, oindB2, cindB2
264+
TupleTools.sort(tuple.(cindA′, cindB′)) == TupleTools.sort(tuple.(cindA, cindB)) ||
265+
throw(ArgumentError(lazy"contraction of $cindA with $cindB is not planar"))
266+
267+
# re-express `pAB` in terms of the reordered open indices
268+
remap = (
269+
map(something, TupleTools.indexin(oindA, oindA′))...,
270+
(length(oindA) .+ map(something, TupleTools.indexin(oindB, oindB′)))...,
271+
)
272+
pAB′ = (TupleTools.getindices(remap, pAB[1]), TupleTools.getindices(remap, pAB[2]))
273+
return (oindA′, cindA′), (cindB′, oindB′), pAB′
274+
end
275+
276+
"""
277+
planaralloc_contract(TC, A, pA, B, pB, pAB, [istemp, allocator])
278+
279+
Allocate the destination of `planarcontract!(C, A, pA, B, pB, pAB, α, β)`.
280+
281+
The planar counterpart of `TensorOperations.tensoralloc_contract`: the index tuples are
282+
canonicalized with [`planar_contract_indices`](@ref) first, such that the space computation
283+
only involves valid intermediate spaces.
284+
"""
285+
function planaralloc_contract(
286+
TC, A, pA::Index2Tuple, B, pB::Index2Tuple, pAB::Index2Tuple,
287+
istemp::Val = Val(false), allocator = TO.DefaultAllocator()
288+
)
289+
pA′, pB′, pAB′ = planar_contract_indices(A, pA, B, pB, pAB)
290+
return TO.tensoralloc_contract(TC, A, pA′, false, B, pB′, false, pAB′, istemp, allocator)
254291
end

‎src/planar/postprocessors.jl‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ end
5353

5454
# TODO: replace _planarmethod with planarmethod in everything below
5555
const _PLANAR_OPERATIONS = (:planaradd!, :planartrace!, :planarcontract!)
56+
const _PLANAR_ALLOCATIONS = (:planaralloc_contract,)
5657

5758
function _insert_planar_operations(ex)
5859
if isexpr(ex, :call)
@@ -78,6 +79,14 @@ function _insert_planar_operations(ex)
7879
ex.head, GlobalRef(TensorKit, Symbol(:planartrace!)),
7980
map(_insert_planar_operations, ex.args[2:end])...
8081
)
82+
elseif ex.args[1] == GlobalRef(TensorOperations, :tensoralloc_contract)
83+
conjB = popat!(ex.args, 8)
84+
conjA = popat!(ex.args, 5)
85+
@assert !conjA && !conjB "conj flags should be disabled ($conjA), ($conjB)"
86+
return Expr(
87+
ex.head, GlobalRef(TensorKit, Symbol(:planaralloc_contract)),
88+
map(_insert_planar_operations, ex.args[2:end])...
89+
)
8190
elseif ex.args[1] in TensorOperations.tensoroperationsfunctions
8291
return Expr(
8392
ex.head, GlobalRef(TensorOperations, ex.args[1]),
@@ -116,10 +125,11 @@ end
116125
"""
117126
insertplanarallocator(ex, allocator)
118127
119-
Insert the allocator argument into the tensor operation methods `planaradd!`, `planartrace!`, and `planarcontract!`.
128+
Insert the allocator argument into the tensor operation methods `planaradd!`, `planartrace!`,
129+
`planarcontract!`, and `planaralloc_contract`.
120130
121131
See also: [`TensorOperations.insertallocator`](@ref).
122132
"""
123133
function insertplanarallocator(ex, allocator)
124-
return _insertargument(ex, allocator, _PLANAR_OPERATIONS)
134+
return _insertargument(ex, allocator, (_PLANAR_OPERATIONS..., _PLANAR_ALLOCATIONS...))
125135
end

‎src/spaces/homspace.jl‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -302,25 +302,6 @@ function compose(W::HomSpace{S}, V::HomSpace{S}) where {S}
302302
return HomSpace(codomain(W), domain(V))
303303
end
304304

305-
# workaround to permuting after composing intermediate spaces without constructing the latter
306-
function _contractedspace(
307-
A::HomSpace{S}, (oindA, cindA)::Index2Tuple,
308-
B::HomSpace{S}, (cindB, oindB)::Index2Tuple,
309-
(p₁, p₂)::Index2Tuple{N₁, N₂}
310-
) where {S, N₁, N₂}
311-
NA = length(oindA)
312-
313-
Acind = map(n -> dual(A[n]), cindA)
314-
Bcind = map(n -> B[n], cindB)
315-
Acind == Bcind || throw(SpaceMismatch(lazy"$(Acind) ≠ $(Bcind)"))
316-
317-
getopen(n) = n <= NA ? A[oindA[n]] : B[oindB[n - NA]]
318-
319-
cod = ProductSpace{S, N₁}(map(getopen, p₁))
320-
dom = ProductSpace{S, N₂}(map(n -> dual(getopen(n)), p₂))
321-
return cod ← dom
322-
end
323-
324305
function TensorOperations.tensorcontract(
325306
A::HomSpace, pA::Index2Tuple, conjA::Bool,
326307
B::HomSpace, pB::Index2Tuple, conjB::Bool,
@@ -341,7 +322,7 @@ function TensorOperations.tensorcontract(
341322
pB′ = adjointtensorindices(B, pB)
342323
TensorOperations.tensorcontract(A, pA, false, B′, pB′, false, pAB)
343324
else
344-
_contractedspace(A, pA, B, pB, pAB)
325+
permute(compose(permute(A, pA), permute(B, pB)), pAB)
345326
end
346327
end
347328

‎src/tensors/braidingtensor.jl‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,18 @@ function planarcontract!(
219219
length.(pA) == (2, 2) ||
220220
return planarcontract!(C, TensorMap(A), pA, B, pB, pAB, α, β, backend, allocator)
221221

222-
spacecheck_contract(C, A, pA, false, B, pB, false, pAB)
222+
pA′, pB′, pAB′ = planar_contract_indices(A, pA, B, pB, pAB)
223+
# a destination that needs an additional transposition is left to the generic
224+
# implementation: the braid below would resolve the cyclic move as crossings
225+
_isdirectoutput(pAB′, length(pA′[1])) ||
226+
return planarcontract!(C, TensorMap(A), pA, B, pB, pAB, α, β, backend, allocator)
227+
228+
spacecheck_contract(C, A, pA′, false, B, pB′, false, pAB′)
223229

224230
codA, domA = codomainind(A), domainind(A)
225231
codB, domB = codomainind(B), domainind(B)
226-
oindA, cindA, oindB, cindB = reorder_indices(
227-
codA, domA, codB, domB, pA..., reverse(pB)..., pAB...
228-
)
232+
oindA, cindA = pA′
233+
cindB, oindB = pB′
229234

230235
I = sectortype(C)
231236
BraidingStyle(I) isa Bosonic &&
@@ -272,13 +277,18 @@ function planarcontract!(
272277
length.(pB) == (2, 2) ||
273278
return planarcontract!(C, A, pA, TensorMap(B), pB, pAB, α, β, backend, allocator)
274279

275-
spacecheck_contract(C, A, pA, false, B, pB, false, pAB)
280+
pA′, pB′, pAB′ = planar_contract_indices(A, pA, B, pB, pAB)
281+
# a destination that needs an additional transposition is left to the generic
282+
# implementation: the braid below would resolve the cyclic move as crossings
283+
_isdirectoutput(pAB′, length(pA′[1])) ||
284+
return planarcontract!(C, A, pA, TensorMap(B), pB, pAB, α, β, backend, allocator)
285+
286+
spacecheck_contract(C, A, pA′, false, B, pB′, false, pAB′)
276287

277288
codA, domA = codomainind(A), domainind(A)
278289
codB, domB = codomainind(B), domainind(B)
279-
oindA, cindA, oindB, cindB = reorder_indices(
280-
codA, domA, codB, domB, pA..., reverse(pB)..., pAB...
281-
)
290+
oindA, cindA = pA′
291+
cindB, oindB = pB′
282292

283293
I = sectortype(C)
284294
BraidingStyle(I) isa Bosonic &&

‎src/tensors/linalg.jl‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,20 @@ is `domain(t1) ⊗ domain(t2)`.
602602
function ⊗(A::AbstractTensorMap, B::AbstractTensorMap)
603603
check_spacetype(A, B)
604604

605-
# allocate destination with correct scalartype
605+
# index tuples for the blockwise tensor product
606606
pA = ((codomainind(A)..., domainind(A)...), ())
607607
pB = ((), (codomainind(B)..., domainind(B)...))
608608
NA = numind(A)
609609
pAB = (
610610
(codomainind(A)..., (codomainind(B) .+ NA)...),
611611
(domainind(A)..., (domainind(B) .+ NA)...),
612612
)
613+
# note that we don't use `tensoralloc_contract`: its intermediate spaces are not
614+
# cyclically ordered, which is not allowed for `GenericUnit` sectors
613615
TC = TO.promote_contract(scalartype(A), scalartype(B))
614-
C = TO.tensoralloc_contract(TC, A, pA, false, B, pB, false, pAB, Val(false))
616+
TTC = TO.tensorcontract_type(TC, A, pA, false, B, pB, false, pAB)
617+
structure = (codomain(A) ⊗ codomain(B)) ← (domain(A) ⊗ domain(B))
618+
C = TO.tensoralloc(TTC, structure, Val(false))
615619
zerovector!(C)
616620

617621
# implement tensor product

‎src/tensors/tensoroperations.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ function TO.tensorcontract_structure(
176176
B::AbstractTensorMap, pB::Index2Tuple, conjB::Bool,
177177
pAB::Index2Tuple{N₁, N₂}
178178
) where {N₁, N₂}
179-
VA, pA′ = conjA ? (space(A)', adjointtensorindices(A, pA)) : (space(A), pA)
180-
VB, pB′ = conjB ? (space(B)', adjointtensorindices(B, pB)) : (space(B), pB)
181-
return _contractedspace(VA, pA′, VB, pB′, pAB)
179+
sA = TO.tensoradd_structure(A, pA, conjA)
180+
sB = TO.tensoradd_structure(B, pB, conjB)
181+
return permute(compose(sA, sB), pAB)
182182
end
183183

184184
function TO.checkcontractible(

0 commit comments

Comments
 (0)