-
Notifications
You must be signed in to change notification settings - Fork 672
Simple OpenQASM 3.0 expressions and casts #7593
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Astral Cai <[email protected]>
…-be-executed' into feature/rebased-simple-gates-can-be-executed
- improve docstring - fix test name (spelling) - initialize wrapper variable
- Debug unrelated MBQC test failure in CI
pennylane/io/qasm_interpreter.py
Outdated
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[0]: | ||
self.vars[name].val = value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[1]: | ||
self.vars[name].val = self.vars[name].val + 1 | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[2]: | ||
self.vars[name].val += value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[3]: | ||
self.vars[name].val -= value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[4]: | ||
self.vars[name].val = self.vars[name].val * value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[5]: | ||
self.vars[name].val = self.vars[name].val / value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[6]: | ||
self.vars[name].val = self.vars[name].val & value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[7]: | ||
self.vars[name].val = self.vars[name].val | value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[8]: | ||
self.vars[name].val = self.vars[name].val ^ value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[9]: | ||
self.vars[name].val = self.vars[name].val << value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[10]: | ||
self.vars[name].val = self.vars[name].val >> value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[11]: | ||
self.vars[name].val = self.vars[name].val % value | ||
if node.op.name == ASSIGNMENT_CLASSICAL_OPERATORS[12]: | ||
self.vars[name].val = self.vars[name].val ** value |
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.
LOL maybe a switch-case is better here?
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.
haha sure if you prefer :')
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.
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.
What's your Python version?
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.
3.10.16
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.
NVM yeah each case must be a literal, it cannot be dynamically indexed from a list. I think literals make the code a lot more readable here anyways.
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.
alright
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.
Python should have called visit()
instead of always interpreting a literal! haha
pennylane/io/qasm_interpreter.py
Outdated
if node.op.name in ASSIGNMENT_CLASSICAL_OPERATORS: | ||
if node.op.name == "=": | ||
self.vars[name].val = value | ||
if node.op.name == "++": | ||
self.vars[name].val = self.vars[name].val + 1 | ||
if node.op.name == "+=": | ||
self.vars[name].val += value | ||
if node.op.name == "-=": | ||
self.vars[name].val -= value | ||
if node.op.name == "*=": | ||
self.vars[name].val = self.vars[name].val * value | ||
if node.op.name == "/=": | ||
self.vars[name].val = self.vars[name].val / value | ||
if node.op.name == "&=": | ||
self.vars[name].val = self.vars[name].val & value | ||
if node.op.name == "|=": | ||
self.vars[name].val = self.vars[name].val | value | ||
if node.op.name == "^=": | ||
self.vars[name].val = self.vars[name].val ^ value | ||
if node.op.name == "<<=": | ||
self.vars[name].val = self.vars[name].val << value | ||
if node.op.name == ">>=": | ||
self.vars[name].val = self.vars[name].val >> value | ||
if node.op.name == "%=": | ||
self.vars[name].val = self.vars[name].val % value | ||
if node.op.name == "**=": | ||
self.vars[name].val = self.vars[name].val ** value |
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.
if node.op.name in ASSIGNMENT_CLASSICAL_OPERATORS: | |
if node.op.name == "=": | |
self.vars[name].val = value | |
if node.op.name == "++": | |
self.vars[name].val = self.vars[name].val + 1 | |
if node.op.name == "+=": | |
self.vars[name].val += value | |
if node.op.name == "-=": | |
self.vars[name].val -= value | |
if node.op.name == "*=": | |
self.vars[name].val = self.vars[name].val * value | |
if node.op.name == "/=": | |
self.vars[name].val = self.vars[name].val / value | |
if node.op.name == "&=": | |
self.vars[name].val = self.vars[name].val & value | |
if node.op.name == "|=": | |
self.vars[name].val = self.vars[name].val | value | |
if node.op.name == "^=": | |
self.vars[name].val = self.vars[name].val ^ value | |
if node.op.name == "<<=": | |
self.vars[name].val = self.vars[name].val << value | |
if node.op.name == ">>=": | |
self.vars[name].val = self.vars[name].val >> value | |
if node.op.name == "%=": | |
self.vars[name].val = self.vars[name].val % value | |
if node.op.name == "**=": | |
self.vars[name].val = self.vars[name].val ** value | |
match node.op.name: | |
case "=": | |
self.vars[name].val = value | |
case "++": | |
self.vars[name].val = self.vars[name].val + 1 | |
case "+=": | |
self.vars[name].val += value | |
case "-=": | |
self.vars[name].val -= value | |
case "*=": | |
self.vars[name].val = self.vars[name].val * value | |
case "/=": | |
self.vars[name].val = self.vars[name].val / value | |
case "&=": | |
self.vars[name].val = self.vars[name].val & value | |
case "|=": | |
self.vars[name].val = self.vars[name].val | value | |
case "^=": | |
self.vars[name].val = self.vars[name].val ^ value | |
case "<<=": | |
self.vars[name].val = self.vars[name].val << value | |
case ">>=": | |
self.vars[name].val = self.vars[name].val >> value | |
case "%=": | |
self.vars[name].val = self.vars[name].val % value | |
case "**=": | |
self.vars[name].val = self.vars[name].val ** value | |
case _: | |
raise SyntaxError(...) |
This should work.
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.
Should indeed!
pennylane/io/qasm_interpreter.py
Outdated
if node.op.name in NON_ASSIGNMENT_CLASSICAL_OPERATORS: | ||
match node.op.name: | ||
case "==": | ||
ret = lhs == rhs |
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 not
ret = lhs == rhs | |
return lhs == rhs |
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.
That is a fine suggestion. No problem with that.
Co-authored-by: Astral Cai <[email protected]>
- use default switch case - return within cases - typo in comment
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7593 +/- ##
==========================================
- Coverage 99.68% 99.66% -0.02%
==========================================
Files 531 531
Lines 51979 52091 +112
==========================================
+ Hits 51815 51919 +104
- Misses 164 172 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Context: We would like to support manipulating classical and quantum variables with simple expressions and casts in the OpenQASM 3.0 interpreter.
Description of the Change: Adds support for binary and unary expressions as well as casts.
Benefits: Allows more classical arithmetic to be done in QASM programs.
Possible Drawbacks: Does not support everything in the QASM 3.0 spec as of yet.
Related ShortCut Stories: [sc-92514]