-
Notifications
You must be signed in to change notification settings - Fork 25
fix: reject barrier as an OpenQASM 2 conditional body #339
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
4
commits into
main
Choose a base branch
from
fix-qasm2-conditional-barrier
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.
+113
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ddb79e4
test: cover barrier as an OpenQASM 2 conditional body
ryanhill1 3ca979d
fix: reject barrier as an OpenQASM 2 conditional body
ryanhill1 7c769c0
test: cover non-qop conditional bodies, make nested test actually nest
ryanhill1 785b20f
fix: whitelist the QASM 2 <qop> production for conditional bodies
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,78 @@ | ||
| # 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 what OpenQASM 2.0 allows as a conditional body | ||
|
|
||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from pyqasm.entrypoint import loads | ||
| from pyqasm.exceptions import ValidationError | ||
|
|
||
| QASM2_PREAMBLE = """OPENQASM 2.0; | ||
| include "qelib1.inc"; | ||
| qreg q[2]; | ||
| creg m[1]; | ||
| creg c[1]; | ||
| measure q[0] -> m[0]; | ||
| """ | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("operation", ["barrier q;", "barrier q[0];", "barrier q[0], q[1];"]) | ||
| def test_conditional_barrier_rejected(operation): | ||
| """Test that a barrier is rejected as the body of a conditional. The QASM 2 grammar | ||
| admits only a <qop> there, and barrier is a separate production.""" | ||
| module = loads(QASM2_PREAMBLE + f"if(m==1) {operation}\n") | ||
| with pytest.raises(ValidationError, match="barrier"): | ||
| module.validate() | ||
|
|
||
|
|
||
| def test_conditional_barrier_rejected_when_nested(): | ||
| """Test that a barrier reached only through a nested conditional is also rejected, | ||
| exercising the recursive descent rather than just the outer body""" | ||
| module = loads(QASM2_PREAMBLE + "if(m==1) if(m==0) barrier q;\n") | ||
| with pytest.raises(ValidationError, match="barrier"): | ||
| module.validate() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "operation", | ||
| [ | ||
| "delay[10ns] q;", | ||
| "box {x q[0];}", | ||
| ], | ||
| ) | ||
| def test_conditional_non_qop_rejected(operation): | ||
| """Test that any statement which is not a <qop> is rejected as a conditional body, | ||
| not just barrier. These parse but have no QASM 2 syntax at all.""" | ||
| module = loads(QASM2_PREAMBLE + f"if(m==1) {operation}\n") | ||
| with pytest.raises(ValidationError, match="body of an 'if'"): | ||
| module.validate() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "operation", ["x q[1];", "reset q[1];", "measure q[1] -> c[0];", "cx q[0], q[1];"] | ||
| ) | ||
| def test_conditional_qop_accepted(operation): | ||
| """Test that the operations QASM 2 does allow as a conditional body still validate""" | ||
| module = loads(QASM2_PREAMBLE + f"if(m==1) {operation}\n") | ||
| module.validate() | ||
|
|
||
|
|
||
| def test_unconditional_barrier_accepted(): | ||
| """Test that a barrier outside a conditional is unaffected""" | ||
| module = loads(QASM2_PREAMBLE + "barrier q;\n") | ||
| module.validate() |
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.