Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,18 @@ Major non-breaking changes include:

### Transferring `TensorMap` data from older versions to v0.13:

To export `TensorMap` data from TensorKit.jl v0.12.7 or earlier, you should first export the
data there in a format that is explicit about how tensor data is associated with the
structural part of the tensor, i.e. the splitting and fusion tree pairs. Therefore, on the
older version of TensorKit.jl, use the following code to save the data
To export `TensorMap` data from TensorKit.jl v0.12.7 or earlier, please use a format that is explicit about all "blocks" of the tensor, i.e. all coupled sectors and their corresponding matrix blocks. Therefore, on the
older version of TensorKit.jl, use the following code to save the data:

```julia
using JLD2
filename = "choose_some_filename.jld2"
t_dict = Dict(:space => space(t), :data => Dict((f₁, f₂) => t[f₁, f₂] for (f₁, f₂) in fusiontrees(t)))
jldsave(filename; t_dict)
t_dict = Dict(
:codomain => repr(codomain(t)),
:domain => repr(domain(t)),
:data => Dict(repr(c) => Array(b) for (c, b) in blocks(t))
)
save_object(filename, t_dict)
```

If you have already upgraded to TensorKit.jl v0.13, you can still install the old version in
Expand All @@ -123,20 +125,15 @@ data and reconstruct the tensor as follows:
```julia
using JLD2
filename = "choose_some_filename.jld2"
t_dict = jldload(filename)
T = eltype(valtype(t_dict[:data]))
t = TensorMap{T}(undef, t_dict[:space])
for ((f₁, f₂), val) in t_dict[:data]
t[f₁, f₂] .= val
end
t = convert(TensorMap, load_object(filename))
```

## Overview

TensorKit.jl is a package that provides types and methods to represent and manipulate
tensors with symmetries. The emphasis is on the structure and functionality needed to build
tensor network algorithms for the simulation of quantum many-body systems. Such tensors are
typically invariant under a symmetry group which acts via specific representions on each of
typically invariant under a symmetry group which acts via specific representations on each of
the indices of the tensor. TensorKit.jl provides the functionality for constructing such
tensors and performing typical operations such as tensor contractions and decompositions,
thereby preserving the symmetries and exploiting them for optimal performance.
Expand Down
9 changes: 9 additions & 0 deletions src/tensors/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ function DiagonalTensorMap(data::DenseVector{T}, V::IndexSpace) where {T}
return DiagonalTensorMap{T}(data, V)
end

function DiagonalTensorMap(t::AbstractTensorMap{T,S,1,1}) where {T,S}
isa(t, DiagonalTensorMap) && return t
@assert domain(t) == codomain(t) "Domain and codomain of the input tensor are different."
@assert all(Diagonal(b) == b for (k, b) in blocks(t)) "Input tensor is not diagonal."
data = vcat((LinearAlgebra.diag(b) for (k, b) in blocks(t))...)
return DiagonalTensorMap(data, space(t, 1))
end

# TODO: more constructors needed?

# Special case adjoint:
Expand All @@ -72,6 +80,7 @@ function Base.copy!(t::AbstractTensorMap, d::DiagonalTensorMap)
end
TensorMap(d::DiagonalTensorMap) = copy!(similar(d), d)
Base.convert(::Type{TensorMap}, d::DiagonalTensorMap) = TensorMap(d)
Base.convert(::Type{DiagonalTensorMap}, t::AbstractTensorMap) = DiagonalTensorMap(t)

function Base.convert(::Type{DiagonalTensorMap{T,S,A}},
d::DiagonalTensorMap{T,S,A}) where {T,S,A}
Expand Down
12 changes: 3 additions & 9 deletions src/tensors/tensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,9 @@ Base.copy(t::TensorMap) = typeof(t)(copy(t.data), t.space)
# Conversion between TensorMap and Dict, for read and write purpose
#------------------------------------------------------------------
function Base.convert(::Type{Dict}, t::AbstractTensorMap)
d = Dict{Symbol,Any}()
d[:codomain] = repr(codomain(t))
d[:domain] = repr(domain(t))
data = Dict{String,Any}()
for (c, b) in blocks(t)
data[repr(c)] = Array(b)
end
d[:data] = data
return d
return Dict(:codomain => repr(codomain(t)),
:domain => repr(domain(t)),
:data => Dict(repr(c) => Array(b) for (c, b) in blocks(t)))
end
function Base.convert(::Type{TensorMap}, d::Dict{Symbol,Any})
try
Expand Down
5 changes: 5 additions & 0 deletions test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ diagspacelist = ((ℂ^4)', ℂ[Z2Irrep](0 => 2, 1 => 3),
@timedtestset "Tensor conversion" begin
t = @constinferred DiagonalTensorMap(undef, V)
rand!(t.data)
# element type conversion
tc = complex(t)
@test convert(typeof(tc), t) == tc
@test typeof(convert(typeof(tc), t)) == typeof(tc)
# to and from generic TensorMap
td = DiagonalTensorMap(TensorMap(t))
@test t == td
@test typeof(td) == typeof(t)
end
I = sectortype(V)
if BraidingStyle(I) isa SymmetricBraiding
Expand Down