-
Notifications
You must be signed in to change notification settings - Fork 30
Support OpenQASM end statements #424
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
danielgaskins
wants to merge
2
commits into
qBraid:main
Choose a base branch
from
danielgaskins:fix/end-statement-support
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.
+271
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| # Copyright 2026 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. | ||
|
|
||
| """Tests for terminating an OpenQASM 3 program with ``end``.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from pyqasm.entrypoint import dumps, loads | ||
| from tests.utils import check_unrolled_qasm | ||
|
|
||
|
|
||
| def test_end_stops_global_unrolling_and_bookkeeping(): | ||
| """Statements after a global ``end`` are unreachable.""" | ||
| module = loads(""" | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit q; | ||
| h q; | ||
| end; | ||
| x q; | ||
| qubit[2] unreachable; | ||
| """) | ||
|
|
||
| module.validate() | ||
| module.unroll() | ||
|
|
||
| expected = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit[1] q; | ||
| h q[0]; | ||
| end; | ||
| """ | ||
| check_unrolled_qasm(dumps(module), expected) | ||
| assert module.num_qubits == 1 | ||
| assert module.depth() == 1 | ||
|
|
||
| round_tripped = loads(dumps(module)) | ||
| round_tripped.unroll() | ||
| check_unrolled_qasm(dumps(round_tripped), expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "control_flow", | ||
| [ | ||
| "if (true) { h q; end; x q; }", | ||
| "if (false) { x q; } else { h q; end; x q; }", | ||
| "for int i in [0:2] { h q; end; x q; }", | ||
| "int i = 1; switch (i) { case 1 { h q; end; x q; } default { x q; } }", | ||
| ], | ||
| ) | ||
| def test_end_propagates_from_static_control_flow(control_flow): | ||
| """A reachable ``end`` in static control flow terminates the program.""" | ||
| module = loads(f""" | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit q; | ||
| {control_flow} | ||
| x q; | ||
| """) | ||
|
|
||
| module.unroll() | ||
| output = dumps(module) | ||
|
|
||
| assert output.count("h q[0];") == 1 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use already built test validators in our utilities? It seems like we already have a pretty extensive utility module built for these assertions. |
||
| assert output.count("end;") == 1 | ||
| assert "x q[0];" not in output | ||
| assert output.rstrip().endswith("end;") | ||
|
|
||
|
|
||
| def test_end_stops_while_loop_and_program(): | ||
| """A terminating while-loop body is not expanded again.""" | ||
| module = loads(""" | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit q; | ||
| int i = 0; | ||
| while (i < 2) { | ||
| h q; | ||
| end; | ||
| i += 1; | ||
| } | ||
| x q; | ||
| """) | ||
|
|
||
| module.unroll(max_loop_iters=2) | ||
| output = dumps(module) | ||
|
|
||
| assert output.count("h q[0];") == 1 | ||
| assert output.count("end;") == 1 | ||
| assert "x q[0];" not in output | ||
|
|
||
|
|
||
| def test_end_propagates_from_inlined_subroutine(): | ||
| """An ``end`` reached in an inlined subroutine terminates its caller.""" | ||
| module = loads(""" | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| def stop(qubit q) { | ||
| h q; | ||
| end; | ||
| x q; | ||
| } | ||
| qubit q; | ||
| stop(q); | ||
| x q; | ||
| """) | ||
|
|
||
| module.unroll() | ||
| output = dumps(module) | ||
|
|
||
| assert output.count("h q[0];") == 1 | ||
| assert output.count("end;") == 1 | ||
| assert "x q[0];" not in output | ||
|
|
||
|
|
||
| def test_end_propagates_from_box(): | ||
| """A box preserves its ``end`` and terminates the surrounding block.""" | ||
| module = loads(""" | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit q; | ||
| box { | ||
| h q; | ||
| end; | ||
| x q; | ||
| } | ||
| x q; | ||
| """) | ||
|
|
||
| module.unroll() | ||
|
|
||
| expected = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit[1] q; | ||
| box { | ||
| h q[0]; | ||
| end; | ||
| } | ||
| """ | ||
| check_unrolled_qasm(dumps(module), expected) | ||
|
|
||
|
|
||
| def test_runtime_conditional_end_remains_conditional(): | ||
| """A runtime-dependent ``end`` does not truncate the surrounding block.""" | ||
| module = loads(""" | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit q; | ||
| bit[1] c; | ||
| if (c[0]) { | ||
| end; | ||
| x q; | ||
| } | ||
| h q; | ||
| """) | ||
|
|
||
| module.unroll() | ||
|
|
||
| expected = """ | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| qubit[1] q; | ||
| bit[1] c; | ||
| if (c[0] == true) { | ||
| end; | ||
| } | ||
| h q[0]; | ||
| """ | ||
| check_unrolled_qasm(dumps(module), expected) | ||
|
|
||
|
|
||
| def test_end_in_both_runtime_branches_terminates_program(): | ||
| """Later statements are unreachable when every runtime branch terminates.""" | ||
| module = loads(""" | ||
| OPENQASM 3.0; | ||
| include "stdgates.inc"; | ||
| bit[1] c; | ||
| qubit q; | ||
| if (c[0]) { | ||
| h q; | ||
| end; | ||
| } else { | ||
| z q; | ||
| end; | ||
| } | ||
| x q; | ||
| qubit unreachable; | ||
| """) | ||
|
|
||
| module.unroll() | ||
| output = dumps(module) | ||
|
|
||
| assert "h q[0];" in output | ||
| assert "z q[0];" in output | ||
| assert output.count("end;") == 2 | ||
| assert "x q[0];" not in output | ||
| assert "unreachable" not in output | ||
| assert module.num_qubits == 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Why do you need all statements in this method as a parameter? I think we can just isolate the processing inside the
analyser.pyand refactor