Negative indices and negative ranges are rejected on arrays, registers and aliases
I maintainer di solito rispondono entro 1 giorno
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
Limitation
OpenQASM 3 indexes from the end with negative integers, where -1 is the last element. The
spec uses this in Arrays,
Classical value bit slicing and
Register concatenation and slicing.
pyqasm treats every index as non-negative. Negative indices either produce an
out-of-range error or a generic initialisation error.
Example QASM failure
Arrays — straight from the spec's Arrays section:
OPENQASM 3.0;
array[int[32], 5] myArray = {0, 1, 2, 3, 4};
int[32] alsoLastElem = myArray[-1]; // 4
// ValidationError: Invalid initialization value for variable 'alsoLastElem'
OPENQASM 3.0;
array[float[32], 3, 2] multiDim = {{1.1, 1.2}, {2.1, 2.2}, {3.1, 3.2}};
float[32] alsoLastLastElem = multiDim[-1, -1]; // 3.2
// ValidationError: Invalid initialization value for variable 'x'
Negative index as an assignment target:
OPENQASM 3.0;
array[int[32], 5] a = {0, 1, 2, 3, 4};
a[-1] = 10;
// ValidationError: Invalid index for variable 'a'
Qubit registers:
OPENQASM 3.0;
include "stdgates.inc";
qubit[4] q;
h q[-1];
// ValidationError: Index -1 out of range for register of size 4 in qubit
Negative ranges, from the spec's Register concatenation and slicing section:
OPENQASM 3.0;
qubit[6] two;
let last_three = two[-4:-1];
// ValidationError: Index -4 out of range for register of size 6 in qubit
Bit registers:
OPENQASM 3.0;
bit[4] b = "1010";
bit c = b[-1];
// ValidationError: Invalid initialization value for variable 'c'
Change Requested
- Normalise a negative index
ion a dimension of sizentoi + nbefore the bounds
check, everywhere an index is resolved. - Apply the same normalisation to both endpoints of a range expression, including the
stepped form[-4:2:-1]. - Support this uniformly across arrays (including multi-dimensional and assignment targets),
bit[n],qubit[n], andletaliases. - After normalisation, an index still outside
[0, n)must raise the existing out-of-range
ValidationError— reporting the index as written in the source, not the normalised value.
Implementation Details
- The single most valuable change is a shared helper, e.g.
Qasm3Analyzer.normalize_index(index: int, size: int, span) -> int, applied at every index
resolution site so the behaviour cannot drift between paths. - Call sites to update in
src/pyqasm/analyzer.py:analyze_classical_indicesand the qubit
index validation that produces"Index -1 out of range for register of size 4". - Range handling lives in
Qasm3Transformer.get_qubits_from_range_definition
(src/pyqasm/transformer.py:129) andget_target_qubits
(src/pyqasm/transformer.py:383). Both endpoints need normalising before the range is
expanded, and the step sign must still be honoured afterwards. - Watch the interaction with
remove_idle_qubits/reverse_qubit_order, which rewrite
indices post-unroll. Since normalisation happens during resolution, they should see only
concrete non-negative indices — worth an explicit test to confirm. - Note the spec's reversed form
myInt[-1:-16], which is a descending range. Confirm the
intended iteration order and cover it. - Tests: extend
tests/qasm3/array, alias and measurement tests with negative reads, negative
writes, negative ranges, stepped negative ranges, and an out-of-range negative index that
must still error.
- Lingua principale
- Python
- Stelle
- 29
- Fork
- 33
- Merge medio
- 1g 20h
- PR unite (30g)
- 8
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di qBraid/pyqasm
-
dumps() prints the original program when unroll() leaves at most one statementForse già presa @ShiveshDragon l’ha presa 9 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
qBraid/pyqasm#493 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 63/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement error-handling program-validation
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 43/100
I maintainer di solito rispondono entro 1 giorno
-
dependencies enhancement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di qBraid/pyqasm
Issue simili
-
docs(contributing): the two things that silently block a PRForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertaavailable documentation good first issue
Difficoltà 1/5 1-3 ore Idoneità per principianti 95/100
Nixitov/Riptide-Native#944 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
JTHCode/secretguard#24 ·
I maintainer di solito rispondono entro 1 giorno
-
area: middleware documentation good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
good first issue
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
omarelshamy25x/shamy-hijri#63 ·
I maintainer di solito rispondono entro 1 giorno