Directsum issue


I encountered an issue while working with ITensor, which might stem from my limited experience with the library.
I’m trying to insert a row or column of zeros into a matrix or tensor using the directsum function. Below is a simple example code that illustrates what I aim to achieve:

using ITensors

a1 = Index(2, "a1")
a2 = Index(2, "a2")
b = Index(1, "b")

A = randomITensor(a1, a2)
B = ITensor(b, a2)

@show S1, s1 = directsum(A=> a1, B=> b)
@show S2, s2 = directsum(B=> b, A=> a1)

In this code, the first use of directsum performs as expected. However, the second call, intended to insert zeros in the first row or column, fails with an error.

Could you please advise on how to resolve this issue? For reference, I am using Julia version 1.10.2 and ITensors version 0.4.0. Thank you!

[1] convert(::Type{NDTensors.EmptyNumber}, x::Int64)
@ Base ./number.jl:7
[2] one(::Type{NDTensors.EmptyNumber})
@ Base ./number.jl:347
[3] directsum_projectors!(D1::NDTensors.DenseTensor{NDTensors.EmptyNumber, 2, Tuple{Index{Int64}, Index{Int64}}, NDTensors.Dense{NDTensors.EmptyNumber, Vector{NDTensors.EmptyNumber}}}, D2::NDTensors.DenseTensor{NDTensors.EmptyNumber, 2, Tuple{Index{Int64}, Index{Int64}}, NDTensors.Dense{NDTensors.EmptyNumber, Vector{NDTensors.EmptyNumber}}})
@ ITensors ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:269
[4] directsum_projectors(elt1::Type{NDTensors.EmptyNumber}, elt2::Type{Float64}, i::Index{Int64}, j::Index{Int64}, ij::Index{Int64})
@ ITensors ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:291
[5] _directsum(IJ::Vector{Index{Int64}}, A::ITensor, I::Tuple{Index{Int64}}, B::ITensor, J::Tuple{Index{Int64}}; tags::Nothing)
@ ITensors ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:355
[6] _directsum(IJ::Vector{Index{Int64}}, A::ITensor, I::Tuple{Index{Int64}}, B::ITensor, J::Tuple{Index{Int64}})
@ ITensors ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:344
[7] _directsum(IJ::Nothing, A::ITensor, I::Tuple{Index{Int64}}, B::ITensor, J::Tuple{Index{Int64}}; tags::Vector{String})
@ ITensors ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:341
[8] _directsum
@ ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:326 [inlined]
[9] __directsum(ij::Nothing, A::ITensor, i::Index{Int64}, B::ITensor, j::Index{Int64}; tags::String)
@ ITensors ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:370
[10] __directsum
@ ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:367 [inlined]
[11] #_directsum#251
@ ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:375 [inlined]
[12] _directsum
@ ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:374 [inlined]
[13] #directsum#259
@ ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:495 [inlined]
[14] directsum
@ ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:492 [inlined]
[15] #directsum#258
@ ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:489 [inlined]
[16] directsum(A_and_I::Pair{ITensor, Index{Int64}}, B_and_J::Pair{ITensor, Index{Int64}})
@ ITensors ~/.julia/packages/ITensors/tVfAa/src/tensor_operations/tensor_algebra.jl:488
[17] macro expansion
@ show.jl:1181 [inlined]
[18] top-level scope
@ ~/Library/CloudStorage/Dropbox/Programs/TENSOR_NETWORK/ITENSOR/FTN/test_directsum.ipynb:1

Looks like a bug, thanks for the report.

For the time being you could do:

B = ITensor(0.0, b, a2)

instead. Basically, ITensor(b, a2) makes a special tensor that is supposed to act like a tensor filled with all zeros but it doesn’t store any actual data. ITensor(0.0, b, a2) constructs a dense ITensor that is actually filled with zeros.

Unfortunately it is tricky to make all operations work with those special tensors that don’t store any data so issues like this pop up. We are doing a redesign of the tensor storage backend which should solve these kinds of problems more systematically by using a new design based on FillArrays.jl, but unfortunately that isn’t ready to be used yet by end users.


Thank you so much for prompt reply!
It works!

1 Like

This is getting fixed in [ITensors] Fix issue with directsum of EmptyTensors by kmp5VT · Pull Request #1443 · ITensor/ITensors.jl · GitHub.

1 Like

Great! Thanks!