Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/tensors/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,37 @@ function TK.trace_permute!(
return tdst
end

# TensorKit's `planartrace!` only goes through `trace_permute!` for bosonic sectors, the others
# write into the subblocks of `tdst`, which loses what belongs to blocks a sparse `tdst` doesn't have
function TK.planartrace!(
tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap,
(p₁, pβ‚‚)::Index2Tuple, (q₁, qβ‚‚)::Index2Tuple,
Ξ±::Number, Ξ²::Number, backend, allocator
)
TK.check_spacetype(tdst, tsrc)
(N₃ = length(q₁)) == length(qβ‚‚) ||
throw(IndexError("number of trace indices does not match"))

@boundscheck begin
space(tdst) == TK.select(space(tsrc), (p₁, pβ‚‚)) ||
throw(SpaceMismatch("trace: tsrc = $(codomain(tsrc))←$(domain(tsrc)),
tdst = $(codomain(tdst))←$(domain(tdst)), p₁ = $(p₁), pβ‚‚ = $(pβ‚‚)"))
all(i -> space(tsrc, q₁[i]) == dual(space(tsrc, qβ‚‚[i])), 1:N₃) ||
throw(SpaceMismatch("trace: tsrc = $(codomain(tsrc))←$(domain(tsrc)),
q₁ = $(q₁), qβ‚‚ = $(qβ‚‚)"))
end

isone(Ξ²) || (iszero(Ξ²) ? zerovector!(tdst) : scale!(tdst, Ξ²))
@inbounds for (Isrc, vsrc) in nonzero_pairs(tsrc)
TT.getindices(Isrc.I, q₁) == TT.getindices(Isrc.I, qβ‚‚) || continue
Idst = CartesianIndex(TT.getindices(Isrc.I, (p₁..., pβ‚‚...)))
_addblock!(tdst, Idst, One(), allocator) do c, Ξ³
TK.planartrace!(c, vsrc, (p₁, pβ‚‚), (q₁, qβ‚‚), Ξ±, Ξ³, backend, allocator)
end
end
return tdst
end

# PlanarOperations
# ----------------

Expand Down
20 changes: 20 additions & 0 deletions test/linalg/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ end
end
end

# for non-bosonic sectors `planartrace!` doesn't go through `trace_permute!`
@testset "planartrace! (fermionic, B sparse = $Bsparse, C sparse = $Csparse)" for Bsparse in (false, true),
Csparse in (false, true)
F = Vect[FermionParity]
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)
T = ComplexF64
B = Bsparse ? sprand(T, V1 βŠ— V2 ← V3 βŠ— V2, 0.6) : randn(T, V1 βŠ— V2 ← V3 βŠ— V2)
Bd = convert(TensorMap, B)
@planar C1[a; b] := B[a c; b c]
@planar C2[a; b] := Bd[a c; b c]
@test convert(TensorMap, C1) β‰ˆ C2
for Ξ² in (zero(T), one(T), randn(T))
Ξ± = randn(T)
C = Csparse ? sprand(T, V1 ← V3, 0.5) : randn(T, V1 ← V3)
Cd = TensorKit.planartrace!(convert(TensorMap, C), Bd, ((1,), (3,)), ((2,), (4,)), Ξ±, Ξ²)
@test TensorKit.planartrace!(C, B, ((1,), (3,)), ((2,), (4,)), Ξ±, Ξ²) === C
@test convert(TensorMap, C) β‰ˆ Cd
end
end

##

@testset "tensortrace" begin
Expand Down
Loading