Skip to content

Commit 81a7a80

Browse files
authored
fix: add planar precompilation, avoid errors with non-symmetric braiding (#557)
1 parent 01311b5 commit 81a7a80

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

‎src/precompile/contract.jl‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function precompile_contract(::Type{S}; eltypes = PRECOMPILE_ELTYPES, ndims = PR
2020
V = unitspace(S)
2121
backend = TO.DefaultBackend()
2222
allocator = TO.DefaultAllocator()
23+
symmetric_braiding = BraidingStyle(sectortype(S)) isa SymmetricBraiding
2324
for T in eltypes
2425
α, β = rand(T), rand(T)
2526

@@ -32,27 +33,31 @@ function precompile_contract(::Type{S}; eltypes = PRECOMPILE_ELTYPES, ndims = PR
3233
B = randn(T, W ← V)
3334
pA = ((1,), ntuple(i -> i + 1, Val(N - 1)))
3435
pB = (ntuple(identity, Val(N - 1)), (N,))
35-
C = TO.tensoralloc_contract(T, A, pA, false, B, pB, false, ((1,), (2,)), Val(false))
36-
# both scalar-type paths: generic `(α,β)` and the identity `(One(), Zero())` fast path
37-
TO.tensorcontract!(C, A, pA, false, B, pB, false, ((1,), (2,)), α, β, backend, allocator)
38-
TO.tensorcontract!(C, A, pA, false, B, pB, false, ((1,), (2,)), One(), Zero(), backend, allocator)
39-
40-
# a non-trivial permutation exercises the repartition/braid/transform machinery at
41-
# arity N (the contraction above takes the no-copy view path for its natural partition)
42-
permute(A, (ntuple(i -> N - i + 1, Val(N)), ()))
36+
pAB = ((1,), (2,))
37+
C = TO.tensoralloc_contract(T, A, pA, false, B, pB, false, pAB, Val(false))
38+
39+
planarcontract!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
40+
planarcontract!(C, A, pA, B, pB, pAB, One(), Zero(), backend, allocator)
41+
42+
if symmetric_braiding
43+
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, α, β, backend, allocator)
44+
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, One(), Zero(), backend, allocator)
45+
end
4346
end
4447

4548
# the conjugated-operand branch (`conjA=true`) is a distinct runtime path (adjoint
46-
# handling) that `conj(A) * B` networks hit; `@tensor` handles the space bookkeeping
49+
# handling) that `conj(A) * B` networks hit; `@plansor` picks the planar or non-planar
50+
# implementation depending on the braiding style, so this compiles for any sectortype
4751
A2 = randn(T, V ← V)
4852
B2 = randn(T, V ← V)
49-
@tensor Cc[a; c] := conj(A2[b; a]) * B2[b; c]
53+
@plansor Cc[a; c] := conj(A2[b; a]) * B2[b; c]
5054

5155
# partial trace (the two traced legs are mutually dual)
5256
At = randn(T, V ⊗ V' ← V)
53-
TO.tensortrace!(
54-
TO.tensoralloc_add(T, At, ((3,), ()), false, Val(false)),
55-
At, ((3,), ()), ((1,), (2,)), false, α, β, backend, allocator
57+
Ct = TO.tensoralloc_add(T, At, ((3,), ()), false, Val(false))
58+
planartrace!(Ct, At, ((3,), ()), ((1,), (2,)), α, β, backend, allocator)
59+
symmetric_braiding && TO.tensortrace!(
60+
Ct, At, ((3,), ()), ((1,), (2,)), false, α, β, backend, allocator
5661
)
5762
end
5863
return nothing

‎src/precompile/indexmanipulations.jl‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ See also [`precompile_contract`](@ref TensorKit.precompile_contract), [`precompi
1717
"""
1818
function precompile_indexmanipulations(::Type{S}; eltypes = PRECOMPILE_ELTYPES, ndims = PRECOMPILE_NDIMS) where {S <: IndexSpace}
1919
V = unitspace(S)
20+
symmetric_braiding = BraidingStyle(sectortype(S)) isa SymmetricBraiding
21+
has_braiding = !(BraidingStyle(sectortype(S)) isa NoBraiding)
2022
for T in eltypes
2123
# `Val(N)`/`ntuple` keep the index tuples concrete so the machinery specializes per arity
2224
for N in 1:ndims
@@ -30,8 +32,13 @@ function precompile_indexmanipulations(::Type{S}; eltypes = PRECOMPILE_ELTYPES,
3032
p2 = ntuple(i -> perm[i + N₁], Val(N - N₁))
3133

3234
# `permute` and `braid` funnel through `add_transform!` with different transformers
33-
permute(t, (p1, p2))
34-
braid(t, (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
35+
if symmetric_braiding
36+
permute(t, (p1, p2))
37+
permute(t', (p1, p2))
38+
elseif has_braiding
39+
braid(t, (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
40+
braid(t', (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
41+
end
3542

3643
# canonical transpose `(reverse(domain), reverse(codomain))` is always a valid cyclic transpose
3744
tp1 = ntuple(i -> N - i + 1, Val(N - N₁))
@@ -42,6 +49,7 @@ function precompile_indexmanipulations(::Type{S}; eltypes = PRECOMPILE_ELTYPES,
4249
repartition(t, N₁ < N ? N₁ + 1 : N₁ - 1)
4350

4451
twist(t, 1)
52+
twist(t', 1)
4553
end
4654
end
4755
return nothing

‎src/precompile/precompile.jl‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ module Precompilation
33
export precompile_indexmanipulations, precompile_contract, precompile_factorizations
44

55
using ..TensorKit
6-
using ..TensorKit: TO
6+
using ..TensorKit: TO, planarcontract!, planartrace!
77
using VectorInterface: One, Zero
8-
using TensorOperations: @tensor
98
using PrecompileTools: @setup_workload, @compile_workload
109
using Preferences: @load_preference
1110

0 commit comments

Comments
 (0)