@@ -18,47 +18,59 @@ See also [`precompile_indexmanipulations`](@ref TensorKit.precompile_indexmanipu
1818"""
1919function precompile_contract (:: Type{S} ; eltypes = PRECOMPILE_ELTYPES, ndims = PRECOMPILE_NDIMS) where {S <: IndexSpace }
2020 V = unitspace (S)
21- backend = TO. DefaultBackend ()
22- allocator = TO. DefaultAllocator ()
2321 symmetric_braiding = BraidingStyle (sectortype (S)) isa SymmetricBraiding
22+ for T in eltypes, N in 1 : ndims
23+ Base. inferencebarrier (_precompile_contract)(V, T, Val (N), symmetric_braiding)
24+ end
2425 for T in eltypes
25- α, β = rand (T), rand (T)
26-
27- # contraction + permutation for each requested arity (leg count); `Val(N)`/`ntuple`
28- # keep the index tuples concrete so the machinery specializes per arity
29- for N in 1 : ndims
30- W = V^ (N - 1 ) # N-1 legs
31- # contraction of two arity-N tensors over their N-1 shared legs -> arity-2 result
32- A = randn (T, V ← W)
33- B = randn (T, W ← V)
34- pA = ((1 ,), ntuple (i -> i + 1 , Val (N - 1 )))
35- pB = (ntuple (identity, Val (N - 1 )), (N,))
36- pAB = ((1 ,), (2 ,))
37- C = TO. tensoralloc_contract (T, A, pA, false , B, pB, false , pAB, Val (false ))
26+ Base. inferencebarrier (_precompile_contract_conj_trace)(V, T, symmetric_braiding)
27+ end
28+ return nothing
29+ end
3830
39- planarcontract! (C, A, pA, B, pB, pAB, α, β, backend, allocator)
40- planarcontract! (C, A, pA, B, pB, pAB, One (), Zero (), backend, allocator)
31+ # contraction + permutation for a single arity (leg count) `N`
32+ function _precompile_contract (V, :: Type{T} , :: Val{N} , symmetric_braiding) where {T, N}
33+ backend = TO. DefaultBackend ()
34+ allocator = TO. DefaultAllocator ()
35+ α, β = rand (T), rand (T)
4136
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
46- end
37+ W = _prodspace (V, Val (N - 1 )) # N-1 legs
38+ # contraction of two arity-N tensors over their N-1 shared legs -> arity-2 result
39+ A = randn (T, V ← W)
40+ B = randn (T, W ← V)
41+ pA = ((1 ,), ntuple (i -> i + 1 , Val (N - 1 )))
42+ pB = (ntuple (identity, Val (N - 1 )), (N,))
43+ pAB = ((1 ,), (2 ,))
44+ C = TO. tensoralloc_contract (T, A, pA, false , B, pB, false , pAB, Val (false ))
4745
48- # the conjugated-operand branch (`conjA=true`) is a distinct runtime path (adjoint
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
51- A2 = randn (T, V ← V)
52- B2 = randn (T, V ← V)
53- @plansor Cc[a; c] := conj (A2[b; a]) * B2[b; c]
46+ planarcontract! (C, A, pA, B, pB, pAB, α, β, backend, allocator)
47+ planarcontract! (C, A, pA, B, pB, pAB, One (), Zero (), backend, allocator)
5448
55- # partial trace (the two traced legs are mutually dual)
56- At = randn (T, V ⊗ V' ← V)
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
61- )
49+ if symmetric_braiding
50+ TO. tensorcontract! (C, A, pA, false , B, pB, false , pAB, α, β, backend, allocator)
51+ TO. tensorcontract! (C, A, pA, false , B, pB, false , pAB, One (), Zero (), backend, allocator)
6252 end
6353 return nothing
6454end
55+
56+ function _precompile_contract_conj_trace (V, :: Type{T} , symmetric_braiding) where {T}
57+ backend = TO. DefaultBackend ()
58+ allocator = TO. DefaultAllocator ()
59+ α, β = rand (T), rand (T)
60+
61+ # the conjugated-operand branch (`conjA=true`) is a distinct runtime path (adjoint
62+ # handling) that `conj(A) * B` networks hit; `@plansor` picks the planar or non-planar
63+ # implementation depending on the braiding style, so this compiles for any sectortype
64+ A2 = randn (T, V ← V)
65+ B2 = randn (T, V ← V)
66+ @plansor Cc[a; c] := conj (A2[b; a]) * B2[b; c]
67+
68+ # partial trace (the two traced legs are mutually dual)
69+ At = randn (T, V ⊗ V' ← V)
70+ Ct = TO. tensoralloc_add (T, At, ((3 ,), ()), false , Val (false ))
71+ planartrace! (Ct, At, ((3 ,), ()), ((1 ,), (2 ,)), α, β, backend, allocator)
72+ symmetric_braiding && TO. tensortrace! (
73+ Ct, At, ((3 ,), ()), ((1 ,), (2 ,)), false , α, β, backend, allocator
74+ )
75+ return nothing
76+ end
0 commit comments