`PauliLCU` accepts zero-width Pauli words (and target-free scalar `SpinOperator`s) and builds a `num_system = 0` encoding

Open Beginner friendly
#58 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 8 days

@kubuyuk is already working on this.

Since Nov 24, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api

Research direction

Start in python/cudaq_algorithms/pauli_lcu.py at _terms_from_input, where the word width is taken from the first pair, and at the identity check in __init__. Reproduce with the snippet in the issue and compare with _word_pairs_from_input in the Trotter planner, which already rejects zero width. Done when a zero-width word raises a ValueError, scalar-only operators without an explicit num_qubits are rejected, and the identity check treats the empty word as identity.

Written by the indexing model from the issue text.

Description

PauliLCU's input parser does not require a positive word width, so an empty
Pauli word is accepted and produces an encoding of a zero-qubit system whose
"identity" term is not recognized as identity.

Reproduced on main (e46eea9), cudaq 0.15.1:

from cudaq_algorithms import PauliLCU
import cudaq

for h in ({"": 0.5},
          [(0.5, "")],
          0.5 * cudaq.SpinOperator.identity(),
          cudaq.SpinOperator.empty() + 0.5):
    e = PauliLCU(h)
    print(e.num_system, e.num_ancilla, e.num_terms, e.alpha, e.constant_term, e.terms)
# every line prints:  0 1 1 0.5 0.0 [(0.5, '')]

So a num_system = 0 encoding is constructed with alpha = 0.5 and
constant_term = 0.0 — the empty word is kept as a non-identity term. Only
target-free scalar operators hit this (SpinOperator.identity(),
SpinOperator.empty() + c); 0.5 * cudaq.spin.i(0) targets qubit 0 and
correctly gets width 1 ([(0.5, 'I')]).

Where (python/cudaq_algorithms/pauli_lcu.py, _terms_from_input and __init__):

  • mapping / pair input: width = len(pairs[0][1]) (L383) can be 0; the
    same-length and allowed-character loops (L385–388) pass vacuously for "";
    nothing requires width >= 1.
  • SpinOperator input: required = max(_term_qubit_extent(...)) (L363) is 0
    for a target-free operator, so get_pauli_word(0) yields "".
  • __init__: identity = set(word) == {"I"} (L492) is False for ""
    (set("") == set()), so the term is neither counted in constant_term nor
    excluded by include_identity=False.

Expected: a zero-width word should be rejected with a ValueError
(e.g. "Pauli words must have positive width; pass num_qubits for a
scalar-only operator"), consistent with the existing rejections for
inconsistent widths and unsupported characters.

Suggested fix — mirror the Trotter planner. trotter._word_pairs_from_input
already does both things PauliLCU is missing: it rejects zero width
("hamiltonian must act on at least one qubit") and detects identity with the
subset test set(word) <= {"I"} rather than set(word) == {"I"}. Porting the
positive-width check into pauli_lcu._terms_from_input (and, for consistency,
the <= identity test into __init__) closes the gap on both the mapping/pair
path and the target-free SpinOperator path, and keeps the two primitives'
input contracts aligned. For a scalar-only
SpinOperator, callers can already pass a positive explicit num_qubits
(PauliLCU(0.5 * cudaq.SpinOperator.identity(), num_qubits=2) gives
[(0.5, 'II')]); the fix is to make the implicit zero-width path fail loudly
instead of silently succeeding.

Surfaced while reviewing the agent-skill records in #46, which document this
as a "known unguarded source gap" — once fixed, that record can describe it
as a rejection.

Dominant language
Python
Stars
19
Forks
18
Avg merge
5d 18h
Merged PRs (30d)
14

Getting set up

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.

More from NVIDIA/cudaq-algorithms

All issues in NVIDIA/cudaq-algorithms

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.