forked from lkdvos/BlockTensorKit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensoroperations.jl
More file actions
131 lines (120 loc) · 4.66 KB
/
tensoroperations.jl
File metadata and controls
131 lines (120 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# TensorOperations
# ----------------
function TO.tensoradd_type(
TC, A::AbstractBlockTensorMap, ::Index2Tuple{N₁, N₂}, ::Bool
) where {N₁, N₂}
S = spacetype(A)
M = TK.similarstoragetype(A, TK.promote_permute(TC, sectortype(S)))
return if issparse(A)
sparseblocktensormaptype(S, N₁, N₂, M)
else
blocktensormaptype(S, N₁, N₂, M)
end
end
function TO.tensoradd_type(TC, A::AdjointBlockTensorMap, pA::Index2Tuple, conjA::Bool)
return TO.tensoradd_type(TC, A', adjointtensorindices(A, pA), !conjA)
end
function TO.tensorscalar(t::AbstractBlockTensorMap{T, S, 0, 0}) where {T, S}
return nonzero_length(t) == 0 ? zero(T) : TO.tensorscalar(only(nonzero_values(t)))
end
# tensoralloc_contract
# --------------------
for TTA in (:AbstractTensorMap, :AbstractBlockTensorMap), TTB in (:AbstractTensorMap, :AbstractBlockTensorMap)
TTA == TTB == :AbstractTensorMap && continue
@eval function TO.tensorcontract_type(
TC,
A::$TTA, ::Index2Tuple, ::Bool,
B::$TTB, ::Index2Tuple, ::Bool,
::Index2Tuple{N₁, N₂},
) where {N₁, N₂}
S = TK.check_spacetype(A, B)
TC′ = TK.promote_permute(TC, sectortype(S))
M = TK.promote_storagetype(TK.similarstoragetype(A, TC′), TK.similarstoragetype(B, TC′))
return if issparse(A) && issparse(B)
sparseblocktensormaptype(S, N₁, N₂, M)
else
blocktensormaptype(S, N₁, N₂, M)
end
end
end
function similarblocktype(::Type{A}, ::Type{TT}) where {A, TT}
return Core.Compiler.return_type(similar, Tuple{A, Type{TT}, NTuple{numind(TT), Int}})
end
function TO.tensoralloc(
::Type{BT}, structure::TensorMapSumSpace, istemp::Val, allocator = TO.DefaultAllocator()
) where {BT <: AbstractBlockTensorMap}
C = BT(undef_blocks, structure)
issparse(C) && return C # don't fill up sparse blocks
blockallocator(V) = TO.tensoralloc(eltype(C), V, istemp, allocator)
map!(blockallocator, parent(C), eachspace(C))
return C
end
# tensorfree!
# -----------
function TO.tensorfree!(t::BlockTensorMap, allocator = TO.DefaultAllocator())
foreach(Base.Fix2(TO.tensorfree!, allocator), parent(t))
return nothing
end
function TO.tensorfree!(t::SparseBlockTensorMap, allocator = TO.DefaultAllocator())
foreach(Base.Fix2(TO.tensorfree!, allocator), nonzero_values(t))
return nothing
end
function TK.trace_permute!(
tdst::AbstractBlockTensorMap,
tsrc::AbstractBlockTensorMap,
(p₁, p₂)::Index2Tuple,
(q₁, q₂)::Index2Tuple,
α::Number, β::Number,
backend::AbstractBackend = TO.DefaultBackend(),
)
# some input checks
TK.check_spacetype(tdst, tsrc)
if !(BraidingStyle(sectortype(tdst)) isa SymmetricBraiding)
throw(
SectorMismatch(
"only tensors with symmetric braiding rules can be contracted; try `@planar` instead",
),
)
end
(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
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₂...)))
tdst[Idst] = TensorKit.trace_permute!(
tdst[Idst], vsrc, (p₁, p₂), (q₁, q₂), α, One(), backend
)
end
return tdst
end
# PlanarOperations
# ----------------
function TK.BraidingTensor(
V1::SumSpace{S}, V2::SumSpace{S}, adjoint::Bool = false
) where {S}
T = BraidingStyle(sectortype(S)) isa SymmetricBraiding ? Float64 : ComplexF64
return TK.BraidingTensor{T, S}(V1, V2, adjoint)
end
function TK.BraidingTensor{T, S}(
V1::SumSpace{S}, V2::SumSpace{S}, adjoint::Bool = false
) where {T, S}
τtype = BraidingTensor{T, S}
tdst = SparseBlockTensorMap{τtype}(undef, V2 ⊗ V1, V1 ⊗ V2)
Vs = eachspace(tdst)
@inbounds for I in CartesianIndices(tdst)
if I[1] == I[4] && I[2] == I[3]
V = Vs[I]
tdst[I] = TK.BraidingTensor{T, S}(V[2], V[1], adjoint)
end
end
return tdst
end