@@ -214,6 +214,37 @@ function TK.trace_permute!(
214214 return tdst
215215end
216216
217+ # TensorKit's `planartrace!` only goes through `trace_permute!` for bosonic sectors, the others
218+ # write into the subblocks of `tdst`, which loses what belongs to blocks a sparse `tdst` doesn't have
219+ function TK. planartrace! (
220+ tdst:: AbstractBlockTensorMap , tsrc:: AbstractBlockTensorMap ,
221+ (p₁, p₂):: Index2Tuple , (q₁, q₂):: Index2Tuple ,
222+ α:: Number , β:: Number , backend, allocator
223+ )
224+ TK. check_spacetype (tdst, tsrc)
225+ (N₃ = length (q₁)) == length (q₂) ||
226+ throw (IndexError (" number of trace indices does not match" ))
227+
228+ @boundscheck begin
229+ space (tdst) == TK. select (space (tsrc), (p₁, p₂)) ||
230+ throw (SpaceMismatch (" trace: tsrc = $(codomain (tsrc)) ←$(domain (tsrc)) ,
231+ tdst = $(codomain (tdst)) ←$(domain (tdst)) , p₁ = $(p₁) , p₂ = $(p₂) " ))
232+ all (i -> space (tsrc, q₁[i]) == dual (space (tsrc, q₂[i])), 1 : N₃) ||
233+ throw (SpaceMismatch (" trace: tsrc = $(codomain (tsrc)) ←$(domain (tsrc)) ,
234+ q₁ = $(q₁) , q₂ = $(q₂) " ))
235+ end
236+
237+ isone (β) || (iszero (β) ? zerovector! (tdst) : scale! (tdst, β))
238+ @inbounds for (Isrc, vsrc) in nonzero_pairs (tsrc)
239+ TT. getindices (Isrc. I, q₁) == TT. getindices (Isrc. I, q₂) || continue
240+ Idst = CartesianIndex (TT. getindices (Isrc. I, (p₁... , p₂... )))
241+ _addblock! (tdst, Idst, One (), allocator) do c, γ
242+ TK. planartrace! (c, vsrc, (p₁, p₂), (q₁, q₂), α, γ, backend, allocator)
243+ end
244+ end
245+ return tdst
246+ end
247+
217248# PlanarOperations
218249# ----------------
219250
0 commit comments