Skip to content

[NDTensors] [BUG] In-place mul! does not work for BlockSparseTensors #1628

@AFeuerpfeil

Description

@AFeuerpfeil

Description of bug

The function mul!(C, A, B) errors, when using BlockSparseTensors. It, however, works for dense tensors.

Minimal code demonstrating the bug or unexpected behavior

julia> i=Index(10)
julia> j=Index(10)
julia> A=random_itensor(i,dag(j))
julia> B=random_itensor(j)
julia> C=A*B
julia> mul!(C,A,B)

julia> i=Index([QN()=>10])
julia> j=Index([QN()=>10])
julia> A=random_itensor(i,dag(j))
julia> B=random_itensor(j)
julia> C=A*B
julia> mul!(C,A,B)

Expected output or behavior

Describe what you expected to happen.

The line mul!(C,A,B) should change C in-place. (As we already initialized it with the correct entries (and block-structure), this function should not change C in this specific case.)

Actual output or behavior

The dense code works as expected and overwrites C, the block-sparse code errors.

If you provided a minimal code that demonstrates the bug or unexpected behavior, provide the output you get from that code. If the code leads to an error or warning, include the full error or warning below.

Output of minimal runnable code

(dim=10|id=688)

(dim=10|id=684)

ITensor ord=2 (dim=10|id=688) (dim=10|id=684)
NDTensors.Dense{Float64, Vector{Float64}}

ITensor ord=1 (dim=10|id=684)
NDTensors.Dense{Float64, Vector{Float64}}

ITensor ord=1 (dim=10|id=688)
NDTensors.Dense{Float64, Vector{Float64}}

ITensor ord=1 (dim=10|id=688)
NDTensors.Dense{Float64, Vector{Float64}}

(dim=10|id=700) <Out>
 1: QN() => 10

(dim=10|id=346) <Out>
 1: QN() => 10

ITensor ord=2
(dim=10|id=700) <Out>
 1: QN() => 10
(dim=10|id=346) <In>
 1: QN() => 10
NDTensors.BlockSparse{Float64, Vector{Float64}, 2}

ITensor ord=1
(dim=10|id=346) <Out>
 1: QN() => 10
NDTensors.BlockSparse{Float64, Vector{Float64}, 1}

ITensor ord=1
(dim=10|id=700) <Out>
 1: QN() => 10
NDTensors.BlockSparse{Float64, Vector{Float64}, 1}

ERROR: MethodError: no method matching empty(::Bool)
The function `empty` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  empty(::DataStructures.IntSet)
   @ DataStructures ~/.julia/packages/DataStructures/95DJa/src/int_set.jl:10
  empty(::DataStructures.SparseIntSet)
   @ DataStructures ~/.julia/packages/DataStructures/95DJa/src/sparse_int_set.jl:17
  empty(::BitSet)
   @ Base bitset.jl:45
  ...

Stacktrace:
  [1] (::NDTensors.var"#178#179"{Bool})(::Block{1})
    @ NDTensors ~/.julia/packages/NDTensors/ETUhZ/src/blocksparse/contract_generic.jl:90
  [2] map!(f::NDTensors.var"#178#179"{}, out::Dictionaries.Dictionary{…}, d::Dictionaries.Indices{…})
    @ Dictionaries ~/.julia/packages/Dictionaries/tq7Jt/src/map.jl:57
  [3] map
    @ ~/.julia/packages/Dictionaries/tq7Jt/src/map.jl:92 [inlined]
  [4] contract!(R::NDTensors.BlockSparseTensor{…}, labelsR::Tuple{…}, tensor1::NDTensors.BlockSparseTensor{…}, labelstensor1::Tuple{…}, tensor2::NDTensors.BlockSparseTensor{…}, labelstensor2::Tuple{…}, contraction_plan::Bool, executor::Bool)
    @ NDTensors ~/.julia/packages/NDTensors/ETUhZ/src/blocksparse/contract_generic.jl:90
  [5] contract!(output_tensor::NDTensors.Expose.Exposed{…}, labelsoutput_tensor::Tuple{…}, tensor1::NDTensors.Expose.Exposed{…}, labelstensor1::Tuple{…}, tensor2::NDTensors.Expose.Exposed{…}, labelstensor2::Tuple{…}, α::Bool, β::Bool)
    @ NDTensors ~/.julia/packages/NDTensors/ETUhZ/src/tensoroperations/generic_tensor_operations.jl:165
  [6] _contract!!(output_tensor::NDTensors.BlockSparseTensor{…}, labelsoutput_tensor::Tuple{…}, tensor1::NDTensors.BlockSparseTensor{…}, labelstensor1::Tuple{…}, tensor2::NDTensors.BlockSparseTensor{…}, labelstensor2::Tuple{…}, α::Int64, β::Int64)
    @ NDTensors ~/.julia/packages/NDTensors/ETUhZ/src/tensoroperations/generic_tensor_operations.jl:143
  [7] _contract!!(output_tensor::NDTensors.BlockSparseTensor{…}, labelsoutput_tensor::Tuple{…}, tensor1::NDTensors.BlockSparseTensor{…}, labelstensor1::Tuple{…}, tensor2::NDTensors.BlockSparseTensor{…}, labelstensor2::Tuple{…})
    @ NDTensors ~/.julia/packages/NDTensors/ETUhZ/src/tensoroperations/generic_tensor_operations.jl:131
  [8] contract!!(output_tensor::NDTensors.BlockSparseTensor{…}, labelsoutput_tensor::Tuple{…}, tensor1::NDTensors.BlockSparseTensor{…}, labelstensor1::Tuple{…}, tensor2::NDTensors.BlockSparseTensor{…}, labelstensor2::Tuple{…}, α::Int64, β::Int64)
    @ NDTensors ~/.julia/packages/NDTensors/ETUhZ/src/tensoroperations/generic_tensor_operations.jl:219
  [9] contract!!(output_tensor::NDTensors.BlockSparseTensor{…}, labelsoutput_tensor::Tuple{…}, tensor1::NDTensors.BlockSparseTensor{…}, labelstensor1::Tuple{…}, tensor2::NDTensors.BlockSparseTensor{…}, labelstensor2::Tuple{…})
    @ NDTensors ~/.julia/packages/NDTensors/ETUhZ/src/tensoroperations/generic_tensor_operations.jl:188
 [10] _contract!!(C::NDTensors.BlockSparseTensor{…}, A::NDTensors.BlockSparseTensor{…}, B::NDTensors.BlockSparseTensor{…})
    @ ITensors ~/.julia/packages/ITensors/Zs2nC/src/tensor_operations/tensor_algebra.jl:194
 [11] contract!(C::ITensor, A::ITensor, B::ITensor)
    @ ITensors ~/.julia/packages/ITensors/Zs2nC/src/tensor_operations/tensor_algebra.jl:201
 [12] mul!(::ITensor, ::ITensor, ::ITensor)
    @ ITensors ~/.julia/packages/ITensors/Zs2nC/src/itensor.jl:1853
 [13] top-level scope
    @ ~/git/codes/julia/RuO2/DMRG/post_processing/plot_1D_density.jl:19
Some type information was truncated. Use `show(err)` to see complete types.

Version information

  • Output from versioninfo():
julia> versioninfo()
Julia Version 1.11.3
Commit d63adeda50d (2025-01-21 19:42 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × 13th Gen Intel(R) Core(TM) i7-1360P
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, goldmont)
Threads: 12 default, 0 interactive, 6 GC (on 12 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 12
  • Output from using Pkg; Pkg.status("ITensors"):
julia> using Pkg; Pkg.status("ITensors")
Status `~/.julia/environments/FQHE/Project.toml`
  [9136182c] ITensors v0.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    NDTensorsRequires changes to the NDTensors.jl library.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions