Gate broadcasting over multiple registers unrolls to the wrong circuit
メンテナーはふだん 1 日以内に返信
評価
この issue はまだ評価されていません。
説明
Limitation
Broadcasting defines register arguments as an
element-wise shorthand: every register operand must have the same length, and the gate is
applied once per index, pairing operands positionally. Single-qubit operands are repeated.
The spec's own example:
qubit[1] qr0;
qubit[3] qr1;
qubit[2] qr2;
qubit[3] qr3;
g4 qr0[0], qr1, qr2[0], qr3; // ok
is stated to be equivalent to
g4 qr0[0], qr1[0], qr2[0], qr3[0];
g4 qr0[0], qr1[1], qr2[0], qr3[1];
g4 qr0[0], qr1[2], qr2[0], qr3[2];
pyqasm instead flattens every operand into one list and slices it into consecutive chunks
of the gate's arity. For a two-register call this produces a different circuit with no
error raised, and for the mixed register/qubit form it raises a spurious validation error.
Example QASM failure
Wrong output, silently:
OPENQASM 3.0;
include "stdgates.inc";
qubit[4] q;
qubit[4] r;
cx q, r;
>>> import pyqasm
>>> m = pyqasm.loads(qasm); m.unroll(); print(pyqasm.dumps(m))
OPENQASM 3.0;
include "stdgates.inc";
qubit[4] q;
qubit[4] r;
cx q[0], q[1];
cx q[2], q[3];
cx r[0], r[1];
cx r[2], r[3];
Expected per spec:
cx q[0], r[0];
cx q[1], r[1];
cx q[2], r[2];
cx q[3], r[3];
Spurious rejection of the mixed form:
OPENQASM 3.0;
include "stdgates.inc";
qubit[4] q;
qubit[1] r;
cx q, r[0];
// ValidationError: Invalid number of qubits 5 for operation cx
OPENQASM 3.0;
gate g4 a, b, c, d { }
qubit[1] qr0; qubit[3] qr1; qubit[2] qr2; qubit[3] qr3;
g4 qr0[0], qr1, qr2[0], qr3;
// ValidationError: Qubit count mismatch for gate 'g4'. Expected 4 qubits, but got 8 instead.
Change Requested
Replace the flatten-and-chunk expansion with spec broadcasting:
- Resolve each operand to a list of qubits, keeping operand boundaries.
- Let
nbe the length of the register operands. All register operands must share the same
n; otherwise raise aValidationErrornaming the two mismatched operands. - Emit
ngate applications; at iterationitake elementifrom each register operand
and repeat single-qubit operands unchanged. - If there are no register operands, emit exactly one application.
Implementation Details
-
The defect is in
Qasm3Visitor._unroll_multiple_target_qubits
(src/pyqasm/visitor.py:1039). It callsself._get_op_bits(operation, qubits=True), which
already discards operand boundaries, then chunks linearly:for i in range(0, len(op_qubits), gate_qubit_count): qubit_subsets.append(op_qubits[i : i + gate_qubit_count]) -
_get_op_bitsneeds a variant that returnslist[list[...]](one inner list per operand)
so the boundary information survives._broadcast_gate_operation(visitor.py:1067)
consumes the result and needs no change. -
This is a deliberate behaviour change, not a pure bug fix. The current docstring
documents flatten-and-chunk as intended:'cx q[0], q[1], q[2], q[3]'→
[[q[0], q[1]], [q[2], q[3]]]. That calling form — four single-qubit operands for a
two-qubit gate — is not valid OpenQASM 3 broadcasting in the first place. Maintainers
should decide whether to keep it as a documented pyqasm extension or reject it; either way
the register case above must change. -
The same expansion path serves the modifier route at
visitor.py:1241andvisitor.py:1463,
soctrl @/inv @broadcasts are affected identically and need coverage. -
Tests: add
tests/qasm3/test_gate.pycases for two-registercx, mixed register/qubit,
the specg4example, the size-mismatch error, and a broadcast underctrl @.
- 主要言語
- Python
- スター
- 29
- フォーク
- 33
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 15
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
qBraid/pyqasm のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 63/100
メンテナーはふだん 1 日以内に返信
-
enhancement error-handling program-validation
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
Move the QASM 2 printer and its branch pre-processing out of qasm2.py対応中かも @danielgaskins が 7 日前に担当しました。 オープンenhancement qasm2
難易度 4/5 3〜5日 初心者へのやさしさ 22/100
qBraid/pyqasm#447 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
[BUG] dumps() emits extern calls without a semicolon, producing unparsable QASM対応中かも @Jah-yee が 16 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 25/100
qBraid/pyqasm#438 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Parse failures raise ValidationError with an empty message; the ANTLR diagnostic is lost対応中かも @tonycoder-hub が 9 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 22/100
qBraid/pyqasm#436 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
-
[Bug]: Queen's own Ollama model loses num_ctx when a running session switches to it対応中かも @anasamin26 が今日担当しました。 オープンbug enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
aden-hive/hive#7508 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
llm translation
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
BerriAI/litellm#47884 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
bug 🐛
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
netalertx/NetAlertX#1856 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信