Skip to content

Commit a8eb8fb

Browse files
authored
fix: blockwise planartrace! for non-bosonic sectors (#96)
1 parent 1048ff1 commit a8eb8fb

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎src/tensors/tensoroperations.jl‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,37 @@ function TK.trace_permute!(
214214
return tdst
215215
end
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

‎test/linalg/tensoroperations.jl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ end
107107
end
108108
end
109109

110+
# for non-bosonic sectors `planartrace!` doesn't go through `trace_permute!`
111+
@testset "planartrace! (fermionic, B sparse = $Bsparse, C sparse = $Csparse)" for Bsparse in (false, true),
112+
Csparse in (false, true)
113+
F = Vect[FermionParity]
114+
V1, V2, V3 = F(0 => 1, 1 => 1) ⊞ F(0 => 2), F(1 => 1) ⊞ F(0 => 1, 1 => 2), F(0 => 2, 1 => 1) ⊞ F(1 => 1)
115+
T = ComplexF64
116+
B = Bsparse ? sprand(T, V1 ⊗ V2 ← V3 ⊗ V2, 0.6) : randn(T, V1 ⊗ V2 ← V3 ⊗ V2)
117+
Bd = convert(TensorMap, B)
118+
@planar C1[a; b] := B[a c; b c]
119+
@planar C2[a; b] := Bd[a c; b c]
120+
@test convert(TensorMap, C1) ≈ C2
121+
for β in (zero(T), one(T), randn(T))
122+
α = randn(T)
123+
C = Csparse ? sprand(T, V1 ← V3, 0.5) : randn(T, V1 ← V3)
124+
Cd = TensorKit.planartrace!(convert(TensorMap, C), Bd, ((1,), (3,)), ((2,), (4,)), α, β)
125+
@test TensorKit.planartrace!(C, B, ((1,), (3,)), ((2,), (4,)), α, β) === C
126+
@test convert(TensorMap, C) ≈ Cd
127+
end
128+
end
129+
110130
##
111131

112132
@testset "tensortrace" begin

0 commit comments

Comments
 (0)