ITensor/ITensors.jl

[NDTensors] [BUG] Issue with constructing an ITensor from a sliced Tensor

Closed this issue · 3 comments

Looks like the function ITensorMPOCompression.get_subtensor_wrapper(::DenseTensor, inds, ::UnitRange{Int}...) defined in ITensorMPOCompression.jl here:
https://github.com/JanReimers/ITensorMPOCompression.jl/blob/e1aa17a7f7900d092f147f69a0c5631e610025ad/src/subtensor.jl#L171-L175
is hitting a bug in NDTensors.jl. Here is a minimal example reproducing the bug:

using ITensors: Index, ITensor, randomITensor
using NDTensors: Tensor
i, j = Index.((4, 4))
it = randomITensor(i, j)
t = Tensor(it)
t_sub = t[1:2, 1:2]
i_sub, j_sub = Index.((2, 2))
ITensor(t_sub, (i_sub, j_sub))

which outputs:

ERROR: LoadError: Type parameter position not defined for type `NDTensors.DenseTensor{Float64, 2, Tuple{Int64, Int64}, NDTensors.Dense{Float64, Base.ReshapedArray{Float64, 1, SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, UnitRange{Int64}}, false}, Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}}}` and position name `eltype`.
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] position(type::Type, pos::Function)
    @ NDTensors.TypeParameterAccessors ~/.julia/packages/NDTensors/IQ76r/src/lib/TypeParameterAccessors/src/position.jl:8
  [3] _broadcast_getindex_evalf
    @ ./broadcast.jl:709 [inlined]
  [4] _broadcast_getindex
    @ ./broadcast.jl:692 [inlined]
  [5] (::Base.Broadcast.var"#31#32"{Base.Broadcast.Broadcasted{}})(k::Int64)
    @ Base.Broadcast ./broadcast.jl:1118
  [6] ntuple
    @ ./ntuple.jl:49 [inlined]
  [7] copy
    @ ./broadcast.jl:1118 [inlined]
  [8] materialize
    @ ./broadcast.jl:903 [inlined]
  [9] set_type_parameters(type::Type, positions::Tuple{…}, params::Tuple{…})
    @ NDTensors.TypeParameterAccessors ~/.julia/packages/NDTensors/IQ76r/src/lib/TypeParameterAccessors/src/set_parameters.jl:42
 [10] set_eltype(::Type{NDTensors.TypeParameterAccessors.IsWrappedArray{…}}, type::Type{NDTensors.DenseTensor{…}}, param::Type)
    @ NDTensors.TypeParameterAccessors ~/.julia/packages/NDTensors/IQ76r/src/lib/TypeParameterAccessors/src/base/abstractarray.jl:62
 [11] set_eltype
    @ ~/.julia/packages/SimpleTraits/l1ZsK/src/SimpleTraits.jl:331 [inlined]
 [12] ITensor(as::NDTensors.NeverAlias, eltype::Type{…}, A::NDTensors.DenseTensor{…}, inds::Tuple{…}; kwargs::@Kwargs{})
    @ ITensors ~/.julia/packages/ITensors/Gg1Hv/src/itensor.jl:360
 [13] ITensor(as::NDTensors.NeverAlias, eltype::Type{…}, A::NDTensors.DenseTensor{…}, inds::Tuple{…})
    @ ITensors ~/.julia/packages/ITensors/Gg1Hv/src/itensor.jl:348
 [14] ITensor(as::NDTensors.NeverAlias, A::NDTensors.DenseTensor{…}, is::Tuple{…}; kwargs::@Kwargs{})
    @ ITensors ~/.julia/packages/ITensors/Gg1Hv/src/itensor.jl:414
 [15] ITensor(as::NDTensors.NeverAlias, A::NDTensors.DenseTensor{…}, is::Tuple{…})
    @ ITensors ~/.julia/packages/ITensors/Gg1Hv/src/itensor.jl:411
 [16] ITensor(A::NDTensors.DenseTensor{…}, is::Tuple{…}; kwargs::@Kwargs{})
    @ ITensors ~/.julia/packages/ITensors/Gg1Hv/src/itensor.jl:424
 [17] ITensor(A::NDTensors.DenseTensor{Float64, 2, Tuple{…}, NDTensors.Dense{…}}, is::Tuple{Index{…}, Index{…}})
    @ ITensors ~/.julia/packages/ITensors/Gg1Hv/src/itensor.jl:423
 [18] top-level scope
    @ ~/Simons Foundation Dropbox/Matthew Fishman/Documents/workdir/ITensors.jl/issues/itensor_slicing_bug/itensor_slicing_bug.jl:8
 [19] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [20] top-level scope
    @ REPL[4]:1
in expression starting at /Users/mfishman/Simons Foundation Dropbox/Matthew Fishman/Documents/workdir/ITensors.jl/issues/itensor_slicing_bug/itensor_slicing_bug.jl:8
Some type information was truncated. Use `show(err)` to see complete types.

with versions:

julia> versioninfo()
Julia Version 1.10.3
Commit 0b4590a5507 (2024-04-30 10:59 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

julia> using Pkg; Pkg.status()
Status `.../Project.toml`
  [9136182c] ITensors v0.6.1
  [23ae76d9] NDTensors v0.3.4

@kmp5VT

As discussed with @kmp5VT, probably we should not expect to support this syntax anyway, i.e. it doesn't make much sense to call ITensor(::Tensor, inds).

Instead, ITensorMPOCompression.jl should be extracting the storage:

using ITensors: Index, ITensor, randomITensor
using NDTensors: Tensor
i, j = Index.((4, 4))
it = randomITensor(i, j)
t = Tensor(it)
t_sub = t[1:2, 1:2]
i_sub, j_sub = Index.((2, 2))
ITensor(storage(t_sub), (i_sub, j_sub))

However, that appears to hit some bugs of its own: #1438

@kmp5VT @JanReimers

@mtfishman Is this issue solved? Thanks!

Superseded by #1438 which was closed by #1441.