forked from QuantumKitHub/TensorKit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.jl
More file actions
43 lines (39 loc) · 1.47 KB
/
utility.jl
File metadata and controls
43 lines (39 loc) · 1.47 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
# Utility
# -------
trivtuple(N) = ntuple(identity, N)
function _repartition(p::IndexTuple, N₁::Int)
length(p) >= N₁ ||
throw(ArgumentError("cannot repartition $(typeof(p)) to $N₁, $(length(p) - N₁)"))
return p[1:N₁], p[(N₁ + 1):end]
end
_repartition(p::Index2Tuple, N₁::Int) = _repartition(linearize(p), N₁)
function _repartition(p::Union{IndexTuple,Index2Tuple}, ::Index2Tuple{N₁}) where {N₁}
return _repartition(p, N₁)
end
function _repartition(p::Union{IndexTuple,Index2Tuple},
::AbstractTensorMap{<:Any,N₁}) where {N₁}
return _repartition(p, N₁)
end
TensorKit.block(t::ZeroTangent, c::Sector) = t
ChainRulesCore.ProjectTo(::T) where {T<:AbstractTensorMap} = ProjectTo{T}()
function (::ProjectTo{T1})(x::T2) where {S,N1,N2,T1<:AbstractTensorMap{<:Any,S,N1,N2},
T2<:AbstractTensorMap{<:Any,S,N1,N2}}
T1 === T2 && return x
y = similar(x, scalartype(T1))
for (c, b) in blocks(y)
p = ProjectTo(b)
b .= p(block(x, c))
end
return y
end
function (::ProjectTo{T1})(x::T2) where {S,NumType,StorType,
T1<:DiagonalTensorMap{NumType,S,StorType},
T2<:AbstractTensorMap{<:Any,S,1,1}}
T1 === T2 && return x
y = DiagonalTensorMap{NumType,S,StorType}(undef, space(x, 1))
for (c, b) in blocks(y)
p = ProjectTo(b)
b .= p(block(x, c))
end
return y
end