ITensor / ITensor/ITensorMPS.jl
[ENHANCEMENT] Have `op_term(::Tuple{Number})` return the identity
Nobody has claimed this yet.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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