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

未关闭
#395 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@TheGupta2012 已经在做这个了。

开始于 2026年8月24日。

  • #408 来自 @TheGupta2012 —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
冷清
技术栈
python
领域
compilers

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
Python
星标
29
派生
33
平均合并
1 天 20 小时
30 天内合并 PR
8

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

qBraid/pyqasm 的其他 Issue

查看 qBraid/pyqasm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。