Skip to content
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

fix: mangle OpenQASM keywords #28

Merged

Conversation

atharva-satpute
Copy link
Contributor

@atharva-satpute atharva-satpute commented Jun 7, 2024

Issue #12

Description of changes:
OpenQASM reserved keywords names will be mangled to <keyword>_

Testing done: Yes

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@rmshaffer
Copy link
Contributor

rmshaffer commented Jun 7, 2024

@atharva-satpute The unit test failures you're seeing (failures here) are actually a great thing - these were tests that were using variable names like input and qubit which are technically reserved words in OpenQASM. Now that you're (correctly) appending _ to these, can you update those tests so that their expected OpenQASM strings match (i.e., you can change input -> input_ and qubit -> qubit_, etc., until the tests are passing).

src/autoqasm/api.py Outdated Show resolved Hide resolved
src/autoqasm/api.py Outdated Show resolved Hide resolved
1. Fixed few existing test cases that got affected due to this change
2. Added reserved keyword list
Copy link

codecov bot commented Jun 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (360cab1) to head (cd088a6).
Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #28   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           46        47    +1     
  Lines         2043      2057   +14     
  Branches       342       343    +1     
=========================================
+ Hits          2043      2057   +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -35,6 +35,63 @@
from autoqasm.program.gate_calibrations import GateCalibration
from autoqasm.types import QubitIdentifierType as Qubit

reserved_keywords = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a reference/link to the source of this list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please include a permalink reference as a comment in the code here? Perhaps you can use this URL: https://github.com/openqasm/openqasm/blob/909379e450f5e1b6cdd67b8a96683caca7b13e4d/source/grammar/qasm3Lexer.g4

Also, it would be ideal to move this list (and the function is_reserved_keyword) to a separate file (e.g. reserved_keywords.py) and then just import the function is_reserved_keyword here.

Copy link
Contributor

@yitchen-tim yitchen-tim Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think in long term, it would be better that this list is not hard-coded. Rather, the list depends on the qasm3lexer from the lexer file. But I think it does not necessary need to be implement in this PR. Could you add the reference link as an inline comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think in long term, it would be better that this list is not hard-coded. Rather, the list depends on the qasm3lexer from the lexer file. But I think it does not necessary need to be implement in this PR. Could you add the reference link as an inline comment?

Yes, initially, I had done it that way.

@@ -35,6 +35,63 @@
from autoqasm.program.gate_calibrations import GateCalibration
from autoqasm.types import QubitIdentifierType as Qubit

reserved_keywords = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please include a permalink reference as a comment in the code here? Perhaps you can use this URL: https://github.com/openqasm/openqasm/blob/909379e450f5e1b6cdd67b8a96683caca7b13e4d/source/grammar/qasm3Lexer.g4

Also, it would be ideal to move this list (and the function is_reserved_keyword) to a separate file (e.g. reserved_keywords.py) and then just import the function is_reserved_keyword here.

src/autoqasm/api.py Outdated Show resolved Hide resolved
src/autoqasm/api.py Outdated Show resolved Hide resolved
src/autoqasm/api.py Outdated Show resolved Hide resolved
test/unit_tests/autoqasm/test_types.py Show resolved Hide resolved
1. Created a new file for the reserved keywords
Copy link
Contributor

@rmshaffer rmshaffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few very minor suggestions, but otherwise this looks good to me! Thank you for sticking with this!

test/unit_tests/autoqasm/test_types.py Show resolved Hide resolved
src/autoqasm/api.py Outdated Show resolved Hide resolved
src/autoqasm/api.py Outdated Show resolved Hide resolved
src/autoqasm/reserved_keywords.py Outdated Show resolved Hide resolved
src/autoqasm/api.py Outdated Show resolved Hide resolved
src/autoqasm/reserved_keywords.py Outdated Show resolved Hide resolved
rmshaffer
rmshaffer previously approved these changes Jun 11, 2024
Copy link
Contributor

@rmshaffer rmshaffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

Copy link
Contributor

@jcjaskula-aws jcjaskula-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just wondering if we can improve one error message to avoid confusion since we modify variable name in the background.

test/unit_tests/autoqasm/test_api.py Show resolved Hide resolved
src/autoqasm/reserved_keywords.py Outdated Show resolved Hide resolved
jcjaskula-aws
jcjaskula-aws previously approved these changes Jun 12, 2024
Copy link
Contributor

@jcjaskula-aws jcjaskula-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks a lot for your contribution.

rmshaffer
rmshaffer previously approved these changes Jun 12, 2024
@jcjaskula-aws jcjaskula-aws dismissed stale reviews from rmshaffer and themself via cd088a6 June 12, 2024 21:24
Co-authored-by: Lauren Capelluto <[email protected]>
@rmshaffer rmshaffer merged commit f83bb15 into amazon-braket:main Jun 13, 2024
15 checks passed
@atharva-satpute
Copy link
Contributor Author

LGTM, thanks a lot for your contribution.

Thanks to you guys

@rmshaffer
Copy link
Contributor

Thanks to you guys

Thank you for all the work on this! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AutoQASM program transpilation should mangle OpenQASM reserved keywords (if they're not Python keywords)
5 participants