Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

[BUG] Issues slicing dense arrays by graded unit ranges #9

@mtfishman

Description

@mtfishman

Ideally, we could do something like this:

using GradedUnitRanges
using SymmetrySectors

a = [0 1; 0 0]
r = gradedrange([U1(0) => 1, U1(1) => 1])
b = a[r, dual(r)]

and b would be a BlockSparseArray with axes (r, dual(r)) and one stored block.

In practice the output is:

julia> b = a[r, dual(r)]
2×2-blocked 2×2 BlockArrays.BlockedMatrix{Int64, Matrix{Int64}, Tuple{GradedUnitRanges.GradedOneTo{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}, Vector{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}}}, GradedUnitRanges.GradedOneTo{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}, Vector{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}}}}}:
 01
 ───┼───
 00

julia> axes(b)
(GradedOneTo[U(1)[0] => 1:1, U(1)[1] => 2:2], GradedOneTo[U(1)[0] => 1:1, U(1)[1] => 2:2])

which has a few issues:

  1. It is a BlockArrays.BlockedMatrix, not a BlockSparseArrays.BlockSparseMatrix (because it is falling back to generic BlockArrays.jl indexing code).
  2. The dual information is dropped.

Fixing this would be very helpful for writing generic code for constructing dense or (abelian) symmetry states/operators, see ITensor/QuantumOperatorDefinitions.jl#17.

There is also an issue slicing BitVector by GradedOneTo (at least in Julia 1.11, it doesn't appear in Julia 1.10):

julia> BitMatrix([0 1; 0 0])[r, dual(r)]
2×2-blocked 2×2 BlockArrays.BlockMatrix{Bool, Matrix{Matrix{Bool}}, Tuple{GradedUnitRanges.GradedOneTo{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}, Vector{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}}}, GradedUnitRanges.GradedOneTo{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}, Vector{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}}}}}:
 01
 ───┼───
 00

julia> BitVector([1, 0])[r]
ERROR: MethodError: no method matching LabelledNumbers.LabelledInteger{Int64, U1{Int64}}(::Int64)
The type `LabelledNumbers.LabelledInteger{Int64, U1{Int64}}` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  (::Type{LabelledNumbers.LabelledInteger{Value, Label}} where {Value<:Integer, Label})(::Any, ::Any)
   @ LabelledNumbers ~/.julia/packages/LabelledNumbers/xPJrg/src/labelledinteger.jl:2
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:900
  (::Type{T})(::BigFloat) where T<:Integer
   @ Base mpfr.jl:403
  ...

Stacktrace:
 [1] convert(::Type{LabelledNumbers.LabelledInteger{Int64, U1{Int64}}}, x::Int64)
   @ Base ./number.jl:7
 [2] _prechecked_iterate(iter::GradedUnitRanges.GradedOneTo{…}, i::LabelledNumbers.LabelledInteger{…})
   @ Base ./multidimensional.jl:893
 [3] _unsafe_getindex!
   @ ./multidimensional.jl:937 [inlined]
 [4] _unsafe_getindex(::IndexLinear, A::BitVector, I::GradedUnitRanges.GradedOneTo{LabelledNumbers.LabelledInteger{…}, Vector{…}})
   @ Base ./multidimensional.jl:929
 [5] _getindex
   @ ./multidimensional.jl:915 [inlined]
 [6] getindex(A::BitVector, I::GradedUnitRanges.GradedOneTo{LabelledNumbers.LabelledInteger{Int64, U1{…}}, Vector{LabelledNumbers.LabelledInteger{…}}})
   @ Base ./abstractarray.jl:1312
 [7] top-level scope
   @ REPL[12]:1
Some type information was truncated. Use `show(err)` to see complete types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions