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
i[k] on an int[n] / uint[n] yields bit, with index 0 the least-significant bit.
i[a:b] and i[a:step:b] yield bit[k], following the spec's inclusive-range convention.
i[a:b] = <bit[k]> writes back into the integer's bit representation.
- The same forms work on an array element:
arr[i][k] and arr[i][a:b].
- 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.
Limitation
Classical value bit slicing allows
indexing and slicing the bit representation of an integer, both as a read and as an assignment
target.
pyqasmsupports neither.Example QASM failure
The spec's example, verbatim:
Each read fails:
The slice assignment fails with a misleading cast message:
uintbehaves identically:Bit-slicing an array element, also from the spec:
Change Requested
i[k]on anint[n]/uint[n]yieldsbit, with index 0 the least-significant bit.i[a:b]andi[a:step:b]yieldbit[k], following the spec's inclusive-range convention.i[a:b] = <bit[k]>writes back into the integer's bit representation.arr[i][k]andarr[i][a:b].[0, n)after negative-index normalisation raises aValidationErrornaming 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
Qasm3Analyzer.analyze_classical_indicesinsrc/pyqasm/analyzer.py.It currently assumes the indexed variable is a container; an integer scalar reaches it and
produces the generic initialisation failure.
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]mustbe valid and yield
0."Cannot cast 'str' to 'IntType'"errorshows the RHS
bit[k]reaching the scalar-assignment check unmodified.intArr[0][0]requires the index resolver to handle a secondIndexExpressionapplied to the result of the first, rather than flattening both into one index list — which
is what produces
"Invalid index for variable 'intArr'"today.bit[n]operators issue; sequence thatone first.
myInt[-1:-16]and cover itexplicitly.
tests/qasm3/test_expressions.py— single-bit read, stepped slice read, sliceassignment, array-element bit access,
uintvariants, and an out-of-range index error.