Skip to content

[ITensors] [BUG] ReshapeArray of integers does not convert to ITensor #1691

@algeboy

Description

@algeboy

Description of bug

eltype conversion of some abstract arrays fails in ITensor constructor, specifically when promoting some abstract arrays with integer coefficients to floating point the conversion throws an exception. This exception vanishes when beginning with floating point elements or when using concrete arrays. I have a hypothesis as the error's source and fix below.

Minimal code demonstrating the bug or unexpected behavior

Working use...

julia> using ITensors
julia> x=Index(2,"x"); y=Index(2,"y"); z=Index(2,"z");
julia> T=ITensor(reshape(randn(8), (2,2,2)), x,y,z)
ITensor ord=3 (dim=2|id=29|"x") (dim=2|id=575|"y") (dim=2|id=760|"z")
NDTensors.Dense{Float64, Vector{Float64}}

Failure case...

julia> T=ITensor(reshape(1:8, (2,2,2)), x,y,z)
ERROR: MethodError: no method matching Base.ReshapedArray{Float64, 3, UnitRange{Float64}, Tuple{}}(::Base.ReshapedArray{Int64, 3, UnitRange{Int64}, Tuple{}})
The type `Base.ReshapedArray{Float64, 3, UnitRange{Float64}, Tuple{}}` exists, but no method is defined for this combination of argument types when trying to construct it.
...

Work around (convert to concrete array)...

julia> T=ITensor(Array(reshape(1:8, (2,2,2))), x,y,z)
ITensor ord=3 (dim=2|id=29|"x") (dim=2|id=575|"y") (dim=2|id=760|"z")
NDTensors.Dense{Float64, Vector{Float64}}

Or skip reshaping

julia> T=ITensor(collect(1:8), x,y,z)
ITensor ord=3 (dim=2|id=29|"x") (dim=2|id=575|"y") (dim=2|id=760|"z")
NDTensors.Dense{Float64, Vector{Float64}}

Expected output or behavior
I would expect that since many integer based array inputs work, the reshaped arrays with integer entries should work as well.

Version information

  • Output from versioninfo():
julia> versioninfo()
Julia Version 1.12.3
Commit 966d0af0fdf (2025-12-15 11:20 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: macOS (arm64-apple-darwin24.0.0)
  CPU: 8 × Apple M3
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, apple-m3)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 4 virtual cores)
  • Output from using Pkg; Pkg.status("ITensors"):
julia> using Pkg; Pkg.status("ITensors")
  [9136182c] ITensors v0.9.15

Hypothesized Diagnosis
The issue appears to be from NDTensors.jl TypeParameterAccessors which is called to from here I believe

function (arraytype::Type{<:AbstractArray})(::NeverAlias, A::AbstractArray)
    return specify_type_parameters(arraytype, type_parameters(A))(A)
end

The NDTensors set_eltype works to change the type but the next goal is to call a constructor for that type of the form

<type>{T,N}(A::AbstractArray{S,N}) where {T,N,S}

Array has such a constructor

Array{T,N}(A::AbstractArray{S,N}) where {T,N,S}

but Reshaped arrays' don't seem to match to the same shape of constructor so it fails to match to something like this..

ReshapedArray{T,N}(A::AbstractArray{S,N}} where {T,N,S}

But maybe I lost the my way in the code so hope this isn't a wild goose chase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ITensorsIssues or pull requests related to the `ITensors` package.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions