Skip to content

Commit a0e3460

Browse files
Yue-ZhengyuanJutholkdvos
authored
Add DiagonalTensorMap constructors and converters (#212)
* Update README upgrade guide; add TensorMap to DiagonalTensorMap conversion * Minor update to TensorMap to DiagonalTensorMap conversion * Add suggested changes * Refactoring * implement changes as discussed * restore tensor.jl * Restore README.md * Restore README.md 2 * use `isdiag(::AbstractTensorMap)` Co-authored-by: Lukas Devos <[email protected]> * Fix diagonal.jl * remove ambiguity in convert methods --------- Co-authored-by: Jutho <[email protected]> Co-authored-by: Lukas Devos <[email protected]>
1 parent 450ff9b commit a0e3460

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ end
136136
TensorKit.jl is a package that provides types and methods to represent and manipulate
137137
tensors with symmetries. The emphasis is on the structure and functionality needed to build
138138
tensor network algorithms for the simulation of quantum many-body systems. Such tensors are
139-
typically invariant under a symmetry group which acts via specific representions on each of
139+
typically invariant under a symmetry group which acts via specific representations on each of
140140
the indices of the tensor. TensorKit.jl provides the functionality for constructing such
141141
tensors and performing typical operations such as tensor contractions and decompositions,
142142
thereby preserving the symmetries and exploiting them for optimal performance.

src/tensors/diagonal.jl

+24-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ function DiagonalTensorMap(data::DenseVector{T}, V::IndexSpace) where {T}
5252
return DiagonalTensorMap{T}(data, V)
5353
end
5454

55+
function DiagonalTensorMap(t::AbstractTensorMap{T,S,1,1}) where {T,S}
56+
isa(t, DiagonalTensorMap) && return t
57+
domain(t) == codomain(t) ||
58+
throw(SpaceMismatch("DiagonalTensorMap requires equal domain and codomain"))
59+
A = storagetype(t)
60+
d = DiagonalTensorMap{T,S,A}(undef, space(t, 1))
61+
for (c, b) in blocks(d)
62+
bt = block(t, c)
63+
# TODO: rewrite in terms of `diagview` from MatrixAlgebraKit.jl
64+
copy!(b.diag, view(bt, LinearAlgebra.diagind(bt)))
65+
end
66+
return d
67+
end
68+
5569
# TODO: more constructors needed?
5670

5771
# Special case adjoint:
@@ -73,12 +87,17 @@ end
7387
TensorMap(d::DiagonalTensorMap) = copy!(similar(d), d)
7488
Base.convert(::Type{TensorMap}, d::DiagonalTensorMap) = TensorMap(d)
7589

76-
function Base.convert(::Type{DiagonalTensorMap{T,S,A}},
77-
d::DiagonalTensorMap{T,S,A}) where {T,S,A}
78-
return d
79-
end
8090
function Base.convert(D::Type{<:DiagonalTensorMap}, d::DiagonalTensorMap)
81-
return DiagonalTensorMap(convert(storagetype(D), d.data), d.domain)
91+
return (d isa D) ? d : DiagonalTensorMap(convert(storagetype(D), d.data), d.domain)
92+
end
93+
Base.convert(::Type{DiagonalTensorMap}, t::DiagonalTensorMap) = t
94+
function Base.convert(::Type{DiagonalTensorMap}, t::AbstractTensorMap)
95+
LinearAlgebra.isdiag(t) ||
96+
throw(ArgumentError("DiagonalTensorMap requires input tensor that is diagonal"))
97+
return DiagonalTensorMap(t)
98+
end
99+
function Base.convert(::Type{DiagonalTensorMap}, d::Dict{Symbol,Any})
100+
return convert(DiagonalTensorMap, convert(TensorMap, d))
82101
end
83102

84103
# Complex, real and imaginary parts

test/diagonal.jl

+5
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@ diagspacelist = ((ℂ^4)', ℂ[Z2Irrep](0 => 2, 1 => 3),
8989
@timedtestset "Tensor conversion" begin
9090
t = @constinferred DiagonalTensorMap(undef, V)
9191
rand!(t.data)
92+
# element type conversion
9293
tc = complex(t)
9394
@test convert(typeof(tc), t) == tc
9495
@test typeof(convert(typeof(tc), t)) == typeof(tc)
96+
# to and from generic TensorMap
97+
td = DiagonalTensorMap(TensorMap(t))
98+
@test t == td
99+
@test typeof(td) == typeof(t)
95100
end
96101
I = sectortype(V)
97102
if BraidingStyle(I) isa SymmetricBraiding

0 commit comments

Comments
 (0)