Skip to content

Commit 1166f24

Browse files
committed
part2 - still wip
1 parent 4877ff4 commit 1166f24

7 files changed

Lines changed: 94 additions & 72 deletions

File tree

src/auxiliary/deprecate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import Base: eltype, transpose
22
@deprecate eltype(T::Type{<:AbstractTensorMap}) scalartype(T)
33
@deprecate eltype(t::AbstractTensorMap) scalartype(t)
44

5-
@deprecate permute(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; copy::Bool=false) permutedims(t, (p1, p2); copy=copy)
5+
@deprecate permute(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; copy::Bool=false) permute(t, (p1, p2); copy=copy)
66
@deprecate transpose(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; copy::Bool=false) transpose(t, (p1, p2); copy=copy)
77
@deprecate braid(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple, levels; copy::Bool=false) braid(t, (p1, p2), levels; copy=copy)

src/planar/planaroperations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function planartrace!(C::AbstractTensorMap{S,N₁,N₂}, p::Index2Tuple{N₁,N
3838
return C
3939
end
4040

41+
4142
function planarcontract!(C::AbstractTensorMap{S,N₁,N₂}, pAB::Index2Tuple{N₁,N₂},
4243
A::AbstractTensorMap{S}, pA::Index2Tuple, B::AbstractTensorMap{S},
4344
pB::Index2Tuple, α, β, backend::Backend...) where {S,N₁,N₂}

src/planar/postprocessors.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,31 @@ end
4949

5050
# Replace the tensor operations created by `TO.instantiate` with the corresponding
5151
# planar operations, immediately inserting them with `GlobalRef`.
52+
53+
# NOTE: work around a somewhat unfortunate interface choice in TensorOperations, which we will correct in the future.
54+
_planaradd!(C, p, A, α, β, backend...) = planaradd!(C, A, p, α, β, backend...)
55+
_planartrace!(C, p, A, q, α, β, backend...) = planartrace!(C, A, p, q, α, β, backend...)
56+
_planarcontract!(C, pAB, A, pA, B, pB, α, β, backend...) = planarcontract!(C, A, pA, B, pB, pAB, α, β, backend...)
57+
# TODO: replace _planarmethod with planarmethod in everything below
58+
const _PLANAR_OPERATIONS = (:_planaradd!, :_planartrace!, :_planarcontract!)
59+
5260
function _insert_planar_operations(ex)
5361
if isexpr(ex, :call)
5462
if ex.args[1] == GlobalRef(TensorOperations, :tensoradd!)
5563
conjA = popat!(ex.args, 5)
5664
@assert conjA == :(:N) "conj flag should be `:N` ($conjA)"
57-
return Expr(ex.head, GlobalRef(TensorKit, Symbol(:planaradd!)),
65+
return Expr(ex.head, GlobalRef(TensorKit, Symbol(:_planaradd!)),
5866
map(_insert_planar_operations, ex.args[2:end])...)
5967
elseif ex.args[1] == GlobalRef(TensorOperations, :tensorcontract!)
6068
conjB = popat!(ex.args, 9)
6169
conjA = popat!(ex.args, 6)
6270
@assert conjA == conjB == :(:N) "conj flag should be `:N` ($conjA), ($conjB)"
63-
return Expr(ex.head, GlobalRef(TensorKit, Symbol(:planarcontract!)),
71+
return Expr(ex.head, GlobalRef(TensorKit, Symbol(:_planarcontract!)),
6472
map(_insert_planar_operations, ex.args[2:end])...)
6573
elseif ex.args[1] == GlobalRef(TensorOperations, :tensortrace!)
6674
conjA = popat!(ex.args, 6)
6775
@assert conjA == :(:N) "conj flag should be `:N` ($conjA)"
68-
return Expr(ex.head, GlobalRef(TensorKit, Symbol(:planartrace!)),
76+
return Expr(ex.head, GlobalRef(TensorKit, Symbol(:_planartrace!)),
6977
map(_insert_planar_operations, ex.args[2:end])...)
7078
elseif ex.args[1] in TensorOperations.tensoroperationsfunctions
7179
return Expr(ex.head, GlobalRef(TensorOperations, ex.args[1]),
@@ -77,8 +85,6 @@ function _insert_planar_operations(ex)
7785
return ex
7886
end
7987

80-
const _PLANAR_OPERATIONS = (:planaradd!, :planarcontract!, :planartrace!)
81-
8288
# Mimick `TO.insert_operationbackend` for planar operations.
8389
function insert_operationbackend(ex, backend)
8490
if isexpr(ex, :call) && ex.args[1] isa GlobalRef &&

src/tensors/braidingtensor.jl

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct BraidingTensor{S<:IndexSpace, A} <: AbstractTensorMap{S, 2, 2}
1111
V1::S
1212
V2::S
1313
adjoint::Bool
14-
function BraidingTensor(V1::S, V2::S, adjoint::Bool = false, ::Type{A} = Matrix{ComplexF64}) where
14+
function BraidingTensor{S,A}(V1::S, V2::S, adjoint::Bool = false) where
1515
{S<:IndexSpace, A<:DenseMatrix}
1616
for a in sectors(V1)
1717
for b in sectors(V2)
@@ -25,6 +25,13 @@ struct BraidingTensor{S<:IndexSpace, A} <: AbstractTensorMap{S, 2, 2}
2525
# partial construction: only construct rowr and colr when needed
2626
end
2727
end
28+
function BraidingTensor(V1::S, V2::S, adjoint::Bool = false) where {S<:IndexSpace}
29+
if BraidingStyle(sectortype(S)) isa SymmetricBraiding
30+
return BraidingTensor{S, Matrix{Float64}}(V1, V2, adjoint)
31+
else
32+
return BraidingTensor{S, Matrix{ComplexF64}}(V1, V2, adjoint)
33+
end
34+
end
2835

2936
Base.adjoint(b::BraidingTensor{S,A}) where {S<:IndexSpace, A<:DenseMatrix} =
3037
BraidingTensor(b.V1, b.V2, !b.adjoint, A)
@@ -168,12 +175,14 @@ end
168175

169176
blocks(b::BraidingTensor) = blocks(TensorMap(b))
170177

171-
function planar_contract!(α, A::BraidingTensor{S}, B::AbstractTensorMap{S},
172-
β, C::AbstractTensorMap{S},
173-
oindA::IndexTuple{2}, cindA::IndexTuple{2},
174-
oindB::IndexTuple, cindB::IndexTuple{2},
175-
p1::IndexTuple, p2::IndexTuple,
176-
syms::Union{Nothing, NTuple{3, Symbol}}) where {S}
178+
function planar_contract!(C::AbstractTensorMap{S},
179+
A::BraidingTensor{S},
180+
(oindA, cindA)::Index2Tuple{2,2},
181+
B::AbstractTensorMap{S},
182+
(cindB, oindB)::Index2Tuple{2,<:Any},
183+
(p1, p2)::Index2Tuple,
184+
α::Number, β::Number,
185+
backends...) where {S}
177186

178187
codA, domA = codomainind(A), domainind(A)
179188
codB, domB = codomainind(B), domainind(B)
@@ -214,12 +223,15 @@ function planar_contract!(α, A::BraidingTensor{S}, B::AbstractTensorMap{S},
214223
end
215224
return C
216225
end
217-
function planar_contract!(α, A::AbstractTensorMap{S}, B::BraidingTensor{S},
218-
β, C::AbstractTensorMap{S},
219-
oindA::IndexTuple, cindA::IndexTuple{2},
220-
oindB::IndexTuple{2}, cindB::IndexTuple{2},
221-
p1::IndexTuple, p2::IndexTuple,
222-
syms::Union{Nothing, NTuple{3, Symbol}}) where {S}
226+
function planar_contract!(C::AbstractTensorMap{S},
227+
A::AbstractTensorMap{S},
228+
(oindA, cindA)::Index2Tuple{<:Any,2},
229+
B::BraidingTensor{S},
230+
(cindB, oindB)::Index2Tuple{2,2},
231+
(p1, p2)::Index2Tuple,
232+
α::Number, β::Number,
233+
backends...) where {S}
234+
223235
codA, domA = codomainind(A), domainind(A)
224236
codB, domB = codomainind(B), domainind(B)
225237
oindA, cindA, oindB, cindB =
@@ -259,13 +271,15 @@ function planar_contract!(α, A::AbstractTensorMap{S}, B::BraidingTensor{S},
259271
C
260272
end
261273

262-
function planar_contract!(α, A::BraidingTensor{S}, B::AbstractTensorMap{S},
263-
β, C::AbstractTensorMap{S},
264-
oindA::IndexTuple{0}, cindA::IndexTuple{4},
265-
oindB::IndexTuple, cindB::IndexTuple{4},
266-
p1::IndexTuple, p2::IndexTuple,
267-
syms::Union{Nothing, NTuple{3, Symbol}}) where {S}
268-
274+
function planar_contract!(C::AbstractTensorMap{S},
275+
A::BraidingTensor{S},
276+
(oindA, cindA)::Index2Tuple{0,4},
277+
B::AbstractTensorMap{S},
278+
(cindB, oindB)::Index2Tuple{4,<:Any},
279+
(p1, p2)::Index2Tuple,
280+
α::Number, β::Number,
281+
backends...) where {S}
282+
269283
codA, domA = codomainind(A), domainind(A)
270284
codB, domB = codomainind(B), domainind(B)
271285
oindA, cindA, oindB, cindB =
@@ -327,13 +341,15 @@ function planar_contract!(α, A::BraidingTensor{S}, B::AbstractTensorMap{S},
327341
return C
328342
end
329343

330-
function planar_contract!(α, A::AbstractTensorMap{S}, B::BraidingTensor{S},
331-
β, C::AbstractTensorMap{S},
332-
oindA::IndexTuple, cindA::IndexTuple{4},
333-
oindB::IndexTuple{0}, cindB::IndexTuple{4},
334-
p1::IndexTuple, p2::IndexTuple,
335-
syms::Union{Nothing, NTuple{3, Symbol}}) where {S}
336-
344+
function planar_contract!(C::AbstractTensorMap{S},
345+
A::AbstractTensorMap{S},
346+
(oindA, cindA)::Index2Tuple{0,4},
347+
B::BraidingTensor{S},
348+
(cindB, oindB)::Index2Tuple{4,<:Any},
349+
(p1, p2)::Index2Tuple,
350+
α::Number, β::Number,
351+
backends...) where {S}
352+
337353
codA, domA = codomainind(A), domainind(A)
338354
codB, domB = codomainind(B), domainind(B)
339355
oindA, cindA, oindB, cindB =
@@ -395,13 +411,14 @@ function planar_contract!(α, A::AbstractTensorMap{S}, B::BraidingTensor{S},
395411
return C
396412
end
397413

398-
function planar_contract!(α, A::BraidingTensor{S}, B::AbstractTensorMap{S},
399-
β, C::AbstractTensorMap{S},
400-
oindA::IndexTuple{1}, cindA::IndexTuple{3},
401-
oindB::IndexTuple, cindB::IndexTuple{3},
402-
p1::IndexTuple, p2::IndexTuple,
403-
syms::Union{Nothing, NTuple{3, Symbol}}) where {S}
404-
414+
function planar_contract!(C::AbstractTensorMap{S},
415+
A::BraidingTensor{S},
416+
(oindA, cindA)::Index2Tuple{1,3},
417+
B::AbstractTensorMap{S},
418+
(cindB, oindB)::Index2Tuple{1,<:Any},
419+
(p1, p2)::Index2Tuple,
420+
α::Number, β::Number,
421+
backends...) where {S}
405422
codA, domA = codomainind(A), domainind(A)
406423
codB, domB = codomainind(B), domainind(B)
407424
oindA, cindA, oindB, cindB =
@@ -457,13 +474,15 @@ function planar_contract!(α, A::BraidingTensor{S}, B::AbstractTensorMap{S},
457474
return C
458475
end
459476

460-
function planar_contract!(α, A::AbstractTensorMap{S}, B::BraidingTensor{S},
461-
β, C::AbstractTensorMap{S},
462-
oindA::IndexTuple, cindA::IndexTuple{3},
463-
oindB::IndexTuple{1}, cindB::IndexTuple{3},
464-
p1::IndexTuple, p2::IndexTuple,
465-
syms::Union{Nothing, NTuple{3, Symbol}}) where {S}
466-
477+
function planar_contract!(C::AbstractTensorMap{S},
478+
A::AbstractTensorMap{S},
479+
(oindA, cindA)::Index2Tuple{<:Any,3},
480+
B::BraidingTensor{S},
481+
(cindB, oindB)::Index2Tuple{3,1},
482+
(p1, p2)::Index2Tuple,
483+
α::Number, β::Number,
484+
backends...) where {S}
485+
467486
codA, domA = codomainind(A), domainind(A)
468487
codB, domB = codomainind(B), domainind(B)
469488
oindA, cindA, oindB, cindB =
@@ -520,7 +539,3 @@ function planar_contract!(α, A::AbstractTensorMap{S}, B::BraidingTensor{S},
520539
end
521540

522541
has_shared_permute(t::BraidingTensor, args...) = false
523-
function cached_permute(sym::Symbol, t::BraidingTensor, p1, p2; copy=false)
524-
tp = TO.cached_similar_from_indices(sym, scalartype(t), p1, p2, t, :N)
525-
return add!(true, t, false, tp, p1, p2)
526-
end

src/tensors/factorizations.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Base.@deprecate(
2424
-> U, S, V, ϵ
2525
2626
Compute the (possibly truncated) singular value decomposition such that
27-
`norm(permute(t, leftind, rightind) - U * S * V) ≈ ϵ`, where `ϵ` thus represents the truncation error.
27+
`norm(permute(t, (leftind, rightind)) - U * S * V) ≈ ϵ`, where `ϵ` thus represents the truncation error.
2828
2929
If `leftind` and `rightind` are not specified, the current partition of left and right
3030
indices of `t` is used. In that case, less memory is allocated if one allows the data in
@@ -51,14 +51,14 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and `tsvd(!)`
5151
is currently only implemented for `InnerProductStyle(t) === EuclideanProduct()`.
5252
"""
5353
tsvd(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
54-
tsvd!(permute(t, p1, p2; copy = true); kwargs...)
54+
tsvd!(permute(t, (p1, p2); copy = true); kwargs...)
5555

5656
"""
5757
leftorth(t::AbstractTensorMap, leftind::Tuple, rightind::Tuple;
5858
alg::OrthogonalFactorizationAlgorithm = QRpos()) -> Q, R
5959
6060
Create orthonormal basis `Q` for indices in `leftind`, and remainder `R` such that
61-
`permute(t, leftind, rightind) = Q*R`.
61+
`permute(t, (leftind, rightind)) = Q*R`.
6262
6363
If `leftind` and `rightind` are not specified, the current partition of left and right
6464
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
@@ -76,14 +76,14 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
7676
`InnerProductStyle(t) === EuclideanProduct()`.
7777
"""
7878
leftorth(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
79-
leftorth!(permute(t, p1, p2; copy = true); kwargs...)
79+
leftorth!(permute(t, (p1, p2); copy = true); kwargs...)
8080

8181
"""
8282
rightorth(t::AbstractTensorMap, leftind::Tuple, rightind::Tuple;
8383
alg::OrthogonalFactorizationAlgorithm = LQpos()) -> L, Q
8484
8585
Create orthonormal basis `Q` for indices in `rightind`, and remainder `L` such that
86-
`permute(t, leftind, rightind) = L*Q`.
86+
`permute(t, (leftind, rightind)) = L*Q`.
8787
8888
If `leftind` and `rightind` are not specified, the current partition of left and right
8989
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
@@ -103,14 +103,14 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
103103
`InnerProductStyle(t) === EuclideanProduct()`.
104104
"""
105105
rightorth(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
106-
rightorth!(permute(t, p1, p2; copy = true); kwargs...)
106+
rightorth!(permute(t, (p1, p2); copy = true); kwargs...)
107107

108108
"""
109109
leftnull(t::AbstractTensor, leftind::Tuple, rightind::Tuple;
110110
alg::OrthogonalFactorizationAlgorithm = QRpos()) -> N
111111
112112
Create orthonormal basis for the orthogonal complement of the support of the indices in
113-
`leftind`, such that `N' * permute(t, leftind, rightind) = 0`.
113+
`leftind`, such that `N' * permute(t, (leftind, rightind)) = 0`.
114114
115115
If `leftind` and `rightind` are not specified, the current partition of left and right
116116
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
@@ -128,7 +128,7 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
128128
`InnerProductStyle(t) === EuclideanProduct()`.
129129
"""
130130
leftnull(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
131-
leftnull!(permute(t, p1, p2; copy = true); kwargs...)
131+
leftnull!(permute(t, (p1, p2); copy = true); kwargs...)
132132

133133
"""
134134
rightnull(t::AbstractTensor, leftind::Tuple, rightind::Tuple;
@@ -137,7 +137,7 @@ leftnull(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
137137
rtol::Real = eps(real(float(one(scalartype(t)))))*iszero(atol)) -> N
138138
139139
Create orthonormal basis for the orthogonal complement of the support of the indices in
140-
`rightind`, such that `permute(t, leftind, rightind)*N' = 0`.
140+
`rightind`, such that `permute(t, (leftind, rightind))*N' = 0`.
141141
142142
If `leftind` and `rightind` are not specified, the current partition of left and right
143143
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
@@ -155,7 +155,7 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
155155
`InnerProductStyle(t) === EuclideanProduct()`.
156156
"""
157157
rightnull(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
158-
rightnull!(permute(t, p1, p2; copy = true); kwargs...)
158+
rightnull!(permute(t, (p1, p2); copy = true); kwargs...)
159159

160160
"""
161161
eigen(t::AbstractTensor, leftind::Tuple, rightind::Tuple; kwargs...) -> D, V
@@ -168,7 +168,7 @@ to be destroyed/overwritten, by using `eigen!(t)`. Note that the permuted tensor
168168
`eigen!` is called should have equal domain and codomain, as otherwise the eigenvalue
169169
decomposition is meaningless and cannot satisfy
170170
```
171-
permute(t, leftind, rightind) * V = V * D
171+
permute(t, (leftind, rightind)) * V = V * D
172172
```
173173
174174
Accepts the same keyword arguments `scale`, `permute` and `sortby` as `eigen` of dense
@@ -177,7 +177,7 @@ matrices. See the corresponding documentation for more information.
177177
See also `eig` and `eigh`
178178
"""
179179
LinearAlgebra.eigen(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
180-
eigen!(permute(t, p1, p2; copy = true); kwargs...)
180+
eigen!(permute(t, (p1, p2); copy = true); kwargs...)
181181

182182
"""
183183
eig(t::AbstractTensor, leftind::Tuple, rightind::Tuple; kwargs...) -> D, V
@@ -193,15 +193,15 @@ indices of `t` is used. In that case, less memory is allocated if one allows the
193193
which `eig!` is called should have equal domain and codomain, as otherwise the eigenvalue
194194
decomposition is meaningless and cannot satisfy
195195
```
196-
permute(t, leftind, rightind) * V = V * D
196+
permute(t, (leftind, rightind)) * V = V * D
197197
```
198198
199199
Accepts the same keyword arguments `scale`, `permute` and `sortby` as `eigen` of dense matrices. See the corresponding documentation for more information.
200200
201201
See also `eigen` and `eigh`.
202202
"""
203203
eig(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple; kwargs...) =
204-
eig!(permute(t, p1, p2; copy = true); kwargs...)
204+
eig!(permute(t, (p1, p2); copy = true); kwargs...)
205205

206206
"""
207207
eigh(t::AbstractTensorMap, leftind::Tuple, rightind::Tuple) -> D, V
@@ -218,13 +218,13 @@ indices of `t` is used. In that case, less memory is allocated if one allows the
218218
which `eigh!` is called should have equal domain and codomain, as otherwise the eigenvalue
219219
decomposition is meaningless and cannot satisfy
220220
```
221-
permute(t, leftind, rightind) * V = V * D
221+
permute(t, (leftind, rightind)) * V = V * D
222222
```
223223
224224
See also `eigen` and `eig`.
225225
"""
226226
function eigh(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple)
227-
return eigh!(permute(t, p1, p2; copy=true))
227+
return eigh!(permute(t, (p1, p2); copy=true))
228228
end
229229

230230
"""
@@ -242,7 +242,7 @@ Accepts the same keyword arguments `scale`, `permute` and `sortby` as `eigen` of
242242
matrices. See the corresponding documentation for more information.
243243
"""
244244
LinearAlgebra.isposdef(t::AbstractTensorMap, p1::IndexTuple, p2::IndexTuple) =
245-
isposdef!(permute(t, p1, p2; copy = true))
245+
isposdef!(permute(t, (p1, p2); copy = true))
246246

247247
tsvd(t::AbstractTensorMap; trunc::TruncationScheme = NoTruncation(),
248248
p::Real = 2, alg::Union{SVD, SDD} = SDD()) =

src/tensors/indexmanipulations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function permute(t::TensorMap{S}, (p₁, p₂)::Index2Tuple{N₁,N₂};
3636
if !copy
3737
if p₁ === codomainind(t) && p₂ === domainind(t)
3838
return t
39-
elseif has_shared_permute(t, p)
39+
elseif has_shared_permute(t, (p₁, p₂))
4040
return TensorMap(reshape(t.data, dim(cod), dim(dom)), cod, dom)
4141
end
4242
end

0 commit comments

Comments
 (0)