Skip to content

Classical value bit-slicing on int / uint is unsupported #395

Description

@TheGupta2012

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestllm-assistedUsed LLMs to fine tune issue description.qasm3Related to openqasm3qasm3-coverageAdding support for qasm3 constructs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions