ITensor / ITensor/ITensorMPS.jl

[ENHANCEMENT] Have `op_term(::Tuple{Number})` return the identity

Open
#154 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Julia
Stars
85
Forks
27
Avg merge
22m
Merged PRs (30d)
1

Description

When programatically constructing OpSums I sometimes encounter terms that are simply proportional to the identity and would hence be represented by the empty tuple (if the prefactor is 1) or by a tuple just containing a number. Currently, constructing an op_term from such a tuple fails with a BoundsError as shown below:

julia> using ITensors

julia> ITensors.Ops.op_term((2.0, ))
ERROR: BoundsError: attempt to access 0-element Vector{Int64} at index [1]
Stacktrace:
 [1] throw_boundserror(A::Vector{Int64}, I::Tuple{Int64})
   @ Base ./essentials.jl:14
 [2] getindex(A::Vector{Int64}, i::Int64)
   @ Base ./essentials.jl:916
 [3] first
   @ ./abstractarray.jl:452 [inlined]
 [4] split(f::Function, t::Tuple{})
   @ ITensors.Ops ~/.julia/packages/ITensors/VGXV2/src/lib/Ops/src/op.jl:24
 [5] op_term(a::Tuple{})
   @ ITensors.Ops ~/.julia/packages/ITensors/VGXV2/src/lib/Ops/src/op.jl:262
 [6] op_term(a::Tuple{Float64})
   @ ITensors.Ops ~/.julia/packages/ITensors/VGXV2/src/lib/Ops/src/op.jl:245
 [7] top-level scope
   @ REPL[6]:1

The desired behaviour would be something along the lines of

julia> ITensors.Ops.op_term((2.0, ))
2.0 Id()

which would be consistent with the empty product being 1.

My current work-around is to explicitly pass in (2.0, "Id", site)

julia> ITensors.Ops.op_term((2.0, "Id", 1))
2.0 Id(1,)

but this requires writing an extra if noperators == 0 check in my code.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/Ops/src/op.jl, especially the op_term methods shown in the stack trace. Reproduce the one-number tuple case and verify that it returns the identity representation while preserving the existing explicit Id form.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
api
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.