Repository navigation
fix(visitor): pair register operands element by element when broadcasting a gate #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryanhill1
wants to merge
5
commits into
main
Choose a base branch
from
fix/register-broadcasting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89262c8
fix(visitor): pair register operands element by element when broadcas…
ryanhill1 840aed0
docs(changelog): describe register broadcasting fix (#462)
ryanhill1 a6defcf
fix(visitor): skip broadcast resolution for single-qubit operands; tr…
ryanhill1 c8c835e
Merge remote-tracking branch 'origin/main' into fix/register-broadcas…
ryanhill1 d6189d8
Merge remote-tracking branch 'origin/main' into ryan-bot/462
ryanhill1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # Copyright 2025 qBraid | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Module containing unit tests for OpenQASM 3 gate broadcasting over registers. | ||
|
|
||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from pyqasm.entrypoint import dumps, loads | ||
| from pyqasm.exceptions import ValidationError | ||
| from tests.utils import check_unrolled_qasm | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "body, expected", | ||
| [ | ||
| ( | ||
| "qubit[2] a;\nqubit[2] b;\ncx a, b;", | ||
| "qubit[2] a;\nqubit[2] b;\ncx a[0], b[0];\ncx a[1], b[1];", | ||
| ), | ||
| ( | ||
| "qubit[4] q;\ncx q[0:1], q[2:3];", | ||
| "qubit[4] q;\ncx q[0], q[2];\ncx q[1], q[3];", | ||
| ), | ||
| ( | ||
| "qubit[4] q;\nlet a = q[0:1];\nlet b = q[2:3];\ncx a, b;", | ||
| "qubit[4] q;\ncx q[0], q[2];\ncx q[1], q[3];", | ||
| ), | ||
| ( | ||
| "def f(qubit[2] a, qubit[2] b) { cx a, b; }\nqubit[2] r;\nqubit[2] s;\nf(r, s);", | ||
| "qubit[2] r;\nqubit[2] s;\ncx r[0], s[0];\ncx r[1], s[1];", | ||
| ), | ||
| ( | ||
| "qubit[2] a;\nqubit[2] b;\nswap a, b;", | ||
| "qubit[2] a;\nqubit[2] b;\nswap a[0], b[0];\nswap a[1], b[1];", | ||
| ), | ||
| ( | ||
| "qubit[2] a;\nqubit[2] b;\nctrl @ x a, b;", | ||
| "qubit[2] a;\nqubit[2] b;\ncx a[0], b[0];\ncx a[1], b[1];", | ||
| ), | ||
| ( | ||
| "qubit[3] q;\nqubit[1] r;\ncx q, r[0];", | ||
| "qubit[3] q;\nqubit[1] r;\ncx q[0], r[0];\ncx q[1], r[0];\ncx q[2], r[0];", | ||
| ), | ||
| ], | ||
| ids=["registers", "slices", "aliases", "subroutine-registers", "swap", "ctrl", "mixed"], | ||
| ) | ||
| def test_broadcast_pairs_register_operands(body, expected): | ||
| """Register operands pair element by element and single qubits repeat (issue #384).""" | ||
| header = 'OPENQASM 3.0;\ninclude "stdgates.inc";\n' | ||
| result = loads(header + body) | ||
| result.unroll() | ||
| check_unrolled_qasm(dumps(result), header + expected) | ||
|
|
||
|
|
||
| def test_broadcast_spec_example(): | ||
| """The spec's g4 example applies the gate once per index of qr1 and qr3.""" | ||
| qasm3_string = """ | ||
| 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; | ||
| """ | ||
| result = loads(qasm3_string) | ||
| result.unroll(external_gates=["g4"]) | ||
| gates = [line for line in dumps(result).splitlines() if line.startswith("g4")] | ||
| assert gates == [ | ||
| "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];", | ||
| ] | ||
|
|
||
|
|
||
| def test_broadcast_register_length_mismatch_raises(): | ||
| """Register operands of different lengths cannot be paired.""" | ||
| qasm3_string = 'OPENQASM 3.0;\ninclude "stdgates.inc";\nqubit[2] a;\nqubit[3] b;\ncx a, b;\n' | ||
| with pytest.raises(ValidationError, match="different lengths"): | ||
| loads(qasm3_string).unroll() | ||
|
|
||
|
|
||
| def _cx_lines(qasm_string): | ||
| module = loads(qasm_string) | ||
| module.unroll() | ||
| return [line for line in dumps(module).splitlines() if line.startswith("cx")] | ||
|
|
||
|
|
||
| def test_broadcast_qasm2_registers(): | ||
| """OpenQASM 2 pairs register operands the same way.""" | ||
| qasm2_string = 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\nqreg r[2];\ncx q, r;\n' | ||
| assert _cx_lines(qasm2_string) == ["cx q[0], r[0];", "cx q[1], r[1];"] | ||
|
|
||
|
|
||
| def test_broadcast_duplicate_qubit_raises(): | ||
| """Broadcasting a register against itself pairs each qubit with itself.""" | ||
| qasm3_string = 'OPENQASM 3.0;\ninclude "stdgates.inc";\nqubit[2] q;\ncx q, q;\n' | ||
| with pytest.raises(ValidationError, match="Duplicate qubit"): | ||
| loads(qasm3_string).unroll() | ||
|
|
||
|
|
||
| def test_broadcast_length_one_slice_is_a_register(): | ||
| """A slice naming one qubit is still a register operand, so its length must match.""" | ||
| qasm3_string = 'OPENQASM 3.0;\ninclude "stdgates.inc";\nqubit[3] q;\ncx q[0:1], q[2:2];\n' | ||
| with pytest.raises(ValidationError, match=r"different lengths \[1, 2\]"): | ||
| loads(qasm3_string).unroll() | ||
|
|
||
|
|
||
| def test_broadcast_empty_operand_raises(): | ||
| """An empty slice is reported as a QASM error, not an IndexError.""" | ||
| qasm3_string = 'OPENQASM 3.0;\ninclude "stdgates.inc";\nqubit[4] q;\ncx q[0:1], q[3:2];\n' | ||
| with pytest.raises(ValidationError, match="empty operand"): | ||
| loads(qasm3_string).unroll() | ||
|
|
||
|
|
||
| def test_broadcast_single_qubit_repeats(): | ||
| """A single qubit beside a register is repeated for each register element.""" | ||
| qasm3_string = 'OPENQASM 3.0;\ninclude "stdgates.inc";\nqubit x;\nqubit[2] q;\ncx x, q;\n' | ||
| assert _cx_lines(qasm3_string) == ["cx x[0], q[0];", "cx x[0], q[1];"] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[L3] Missing OpenQASM 2 and duplicate-qubit tests — Implementation · Low
Rationale: OpenQASM 2
cx q, r;gavecx q[0], q[1]; cx r[0], r[1];on main, and this PR fixes it, but no test covers it.cx q, q;now raisesDuplicate qubitthrough the broadcast path, and nothing tests that either. Both tests below pass on head.Change requested:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added both in a6defcf, plus three more:
test_broadcast_length_one_slice_is_a_registertest_broadcast_empty_operand_raisestest_broadcast_single_qubit_repeatsAfter merging
main, the suite is at 1033 passed, 3 skipped, and format-check is clean.