Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Bitwise, shift and index operations on `bit[n]` registers raise raw `TypeError`

オープン
#385 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@TheGupta2012 がすでに取り組んでいます。

2026年8月21日 から。

  • #404 @TheGupta2012 による — オープン

評価

この issue はまだ評価されていません。

説明

bug error-handling llm-assisted qasm3

Limitation

The spec defines bit[n] as a register of bits supporting
bitwise and shift operators and
indexing / slicing.

pyqasm stores a bit[n] value as a Python str, so every one of these operations reaches
a Python operator that str does not implement. The result is an uncaught TypeError
escaping the public API instead of either a correct value or a ValidationError.

Example QASM failure

import pyqasm

for op in ["a | b", "a & b", "a ^ b"]:
    m = pyqasm.loads(f'''
    OPENQASM 3.0;
    bit[4] a = "1010";
    bit[4] b = "0101";
    bit[4] c = {op};
    ''')
    m.validate()
TypeError: unsupported operand type(s) for |: 'str' and 'str'
TypeError: unsupported operand type(s) for &: 'str' and 'str'
TypeError: unsupported operand type(s) for ^: 'str' and 'str'

Shifts fail the same way:

OPENQASM 3.0;
bit[4] a = "1010";
bit[4] c = a << 1;
// TypeError: unsupported operand type(s) for <<: 'str' and 'int'

Indexing and slicing also fail, and these are plain reads:

OPENQASM 3.0;
bit[8] b = "00101010";
bit c = b[0];
// TypeError: string indices must be integers, not 'tuple'
OPENQASM 3.0;
bit[8] b = "00101010";
bit[4] c = b[0:3];
// TypeError: string indices must be integers, not 'tuple'

Related, and blocked by the same representation:

OPENQASM 3.0;
bit[4] a = "1010";
bit[4] c = ~a;
// ValidationError: Invalid initialization value for variable 'c'

Change Requested

  1. Give bit[n] an internal representation that supports the spec's operator set. A
    fixed-width integer plus an explicit width is the natural choice; the str form is then
    produced only at serialisation time.
  2. Support |, &, ^, ~, <<, >> on bit[n] operands, with the spec rule that both
    operands of a binary bitwise operator must have equal width.
  3. Support b[i] (yielding bit) and b[a:b] / b[a:step:b] (yielding bit[k]).
  4. No operation on a bit[n] should ever surface a raw Python exception; anything genuinely
    invalid must raise ValidationError with the offending span.

Implementation Details

  • Bit values are produced by the classical declaration path in
    Qasm3Visitor._visit_classical_declaration (src/pyqasm/visitor.py:~1991) and consumed by
    Qasm3ExprEvaluator in src/pyqasm/expressions.py.
  • The binary/unary operator dispatch in src/pyqasm/maps/expressions.py applies Python
    operators directly to the stored value. Bit-typed operands need to be converted to their
    integer form before the operator is applied, and the result re-widened and masked to n
    bits.
  • Indexing is handled by Qasm3Analyzer.analyze_classical_indices
    (src/pyqasm/analyzer.py); it currently assumes an indexable non-str container, which is
    where string indices must be integers, not 'tuple' originates.
  • A shared helper pair — bits_to_int(value, width) / int_to_bits(value, width) — keeps the
    conversion in one place and makes the serialiser's job unambiguous.
  • Once this lands, the rotl / rotr / popcount built-ins (separate issue) become
    straightforward, since they are defined on bit[n].
  • Tests: tests/qasm3/test_expressions.py — each operator, width-mismatch errors, single-bit
    and ranged reads, and a round-trip through dumps() confirming the "1010" literal form
    is preserved.
主要言語
Python
スター
29
フォーク
33
平均マージ
2日 4時間
マージ済み PR(30日)
15

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

qBraid/pyqasm のほかの issue

qBraid/pyqasm の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。