Classical value bit-slicing on `int` / `uint` is unsupported

Aperta
#395 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@TheGupta2012 ci sta già lavorando.

Dal 24/8/2026.

  • #408 di @TheGupta2012 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
python
Ambito
compilers

Direzione di ricerca

Start by reproducing the failures with the single-bit read int[32] myInt = 15; bit[1] lastBit = myInt[0]; and the chained intArr[0][0] case, then trace them through Qasm3Analyzer.analyze_classical_indices in src/pyqasm/analyzer.py. The issue depends on the separate bit[n] operators work and the negative-indexing issue, and it leaves the reversed slice myInt[-1:-16] semantics unresolved, so confirm those first. Done means the read, slice, assignment, array-element and out-of-range cases pass in tests/qasm3/test_expressions.py.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement llm-assisted qasm3 qasm3-coverage

Limitation

Classical value bit slicing allows
indexing and slicing the bit representation of an integer, both as a read and as an assignment
target.

pyqasm supports neither.

Example QASM failure

The spec's example, verbatim:

OPENQASM 3.0;
int[32] myInt = 15;                      // 0xF or 0b1111
bit[1] lastBit = myInt[0];               // 1
bit[1] signBit = myInt[31];              // 0
bit[1] alsoSignBit = myInt[-1];          // 0

bit[16] evenBits = myInt[0:2:31];        // 3
bit[16] upperBits = myInt[-16:-1];
bit[16] upperReversed = myInt[-1:-16];

myInt[4:7] = "1010";                     // myInt == 0xAF

Each read fails:

ValidationError: Invalid initialization value for variable 'lastBit'

The slice assignment fails with a misleading cast message:

ValidationError: Cannot cast 'str' to 'IntType'.
                 Invalid assignment of type 'str' to variable 'myInt' of type 'IntType'

uint behaves identically:

OPENQASM 3.0;
uint[32] i = 15;
bit b = i[0];
// ValidationError: Invalid initialization value for variable 'b'

Bit-slicing an array element, also from the spec:

OPENQASM 3.0;
array[int[32], 5] intArr = {0, 1, 2, 3, 4};
intArr[0][0] = 1;
// ValidationError: Invalid index for variable 'intArr'
bit[5] b = intArr[4][0:4];
// ValidationError: Invalid initialization value for variable 'b'

Change Requested

  1. i[k] on an int[n] / uint[n] yields bit, with index 0 the least-significant bit.
  2. i[a:b] and i[a:step:b] yield bit[k], following the spec's inclusive-range convention.
  3. i[a:b] = <bit[k]> writes back into the integer's bit representation.
  4. The same forms work on an array element: arr[i][k] and arr[i][a:b].
  5. An index outside [0, n) after negative-index normalisation raises a ValidationError
    naming the width.

Negative indices in these expressions are covered by the separate negative-indexing issue;
both are needed for the spec example to pass in full.

Implementation Details

  • Index resolution is Qasm3Analyzer.analyze_classical_indices in src/pyqasm/analyzer.py.
    It currently assumes the indexed variable is a container; an integer scalar reaches it and
    produces the generic initialisation failure.
  • Add an integer-bit-view path: convert the value to a fixed-width bit vector of the declared
    width, index or slice it, and convert back on assignment. The declared width — not the
    magnitude of the value — determines valid indices, so int[32] myInt = 15; myInt[31] must
    be valid and yield 0.
  • The assignment path needs the same treatment; the "Cannot cast 'str' to 'IntType'" error
    shows the RHS bit[k] reaching the scalar-assignment check unmodified.
  • Chained indexing intArr[0][0] requires the index resolver to handle a second IndexExpression
    applied to the result of the first, rather than flattening both into one index list — which
    is what produces "Invalid index for variable 'intArr'" today.
  • Shares the width-carrying bit representation from the bit[n] operators issue; sequence that
    one first.
  • Confirm the intended semantics of the spec's reversed slice myInt[-1:-16] and cover it
    explicitly.
  • Tests: tests/qasm3/test_expressions.py — single-bit read, stepped slice read, slice
    assignment, array-element bit access, uint variants, and an out-of-range index error.
Lingua principale
Python
Stelle
29
Fork
33
Merge medio
1g 20h
PR unite (30g)
8

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di qBraid/pyqasm

Tutte le issue di qBraid/pyqasm

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.