Repository navigation
fix(expressions): divide int variables as integers #474
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: main
Are you sure you want to change the base?
Changes from all commits
daab4b3
fbd624c
1f1b865
5f7f857
77f81aa
5bf4fa5
53e310e
cd58d64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ | |
| IntegerLiteral, | ||
| ) | ||
| from openqasm3.ast import IntType as Qasm3IntType | ||
| from openqasm3.ast import SizeOf, Statement, StretchType, UnaryExpression | ||
| from openqasm3.ast import SizeOf, Statement, StretchType, UintType, UnaryExpression | ||
|
|
||
| from pyqasm.analyzer import Qasm3Analyzer | ||
| from pyqasm.elements import Variable | ||
|
|
@@ -142,6 +142,29 @@ def _check_var_type(cls, var_name, reqd_type, expression): | |
| span=expression.span, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _is_integer_typed(cls, expression) -> bool: | ||
| """Whether an expression is built only from integer literals and int or uint variables.""" | ||
| if isinstance(expression, IntegerLiteral): | ||
| # OpenQASM 2 has no integer type: every classical expression is real | ||
| assert cls.visitor_obj | ||
| return not cls.visitor_obj._module._real_literal_division | ||
| if isinstance(expression, Identifier): | ||
| assert cls.visitor_obj | ||
| var = cls.visitor_obj._scope_manager.get_from_visible_scope(expression.name) | ||
| return ( | ||
| var is not None | ||
| and not var.dims | ||
| and isinstance(var.base_type, (Qasm3IntType, UintType)) | ||
| ) | ||
| if isinstance(expression, UnaryExpression): | ||
| return expression.op.name == "-" and cls._is_integer_typed(expression.expression) | ||
| if isinstance(expression, BinaryExpression): | ||
| return expression.op.name in ("+", "-", "*", "/", "%") and all( | ||
| cls._is_integer_typed(operand) for operand in (expression.lhs, expression.rhs) | ||
| ) | ||
| return False | ||
|
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. [L1] Other integer-typed operands still true-divide — Implementation · Low Impact: RationaleCasts to Change requested: open a follow-up issue that lists these operands, and link it from the PR description instead of listing them as intended behavior. Completed when: the issue exists and the PR description links it. |
||
|
|
||
| @classmethod | ||
| def _check_var_initialized(cls, var_name, var_value, expression): | ||
| """Checks if a variable is initialized and raises an error if it is not. | ||
|
|
@@ -471,6 +494,12 @@ def _get_external_function_return_type(expression): | |
| return (None, []) | ||
|
|
||
| statements.extend(rhs_statements) | ||
| if expression.op.name == "/" and cls._is_integer_typed(expression): | ||
| # int / int is integer division, truncated toward zero as in C99 | ||
| quotient = abs(lhs_value) // abs(rhs_value) | ||
| return _check_and_return_value( | ||
| quotient if (lhs_value < 0) == (rhs_value < 0) else -quotient | ||
| ) | ||
| return _check_and_return_value( | ||
| qasm3_expression_op_map(expression.op.name, lhs_value, rhs_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.
[H1] Integer literals must divide as integers in this PR — Implementation · High
Impact:
rx(1 / 2),rx(b / 2)andrx(3 / a)still give 0.5, 1.5 and 1.5. The spec gives 0, 1 and 1. So/stays inconsistent on integer operands:b / ais 1, butb / 2is 1.5.Rationale
Integer literals are integers (spec
types.rst, "Literals")./on integer types is integer division (classical.rst, "Integers":b / a; // 1). So every+ - * / %tree of integer literals andint/uintvariables is integer-typed. The decision for this PR is to include literals now, not in a follow-up.A prototype that marks
IntegerLiteralas integer-typed shows three places where a Pythonintreaches the evaluator but is not an OpenQASM integer:gate g(t) r { rx(t / 2) r; }theng(3) q[0];(org(n))rx(1)rx(1.5)angle(gates.rst).transform_gate_paramssubstitutesIntegerLiteral(3)fort.rx(1/2) q[0];rx(0)rx(0.5)rx(1/2)throughto_qasm3(), thenunroll()rx(0)rx(0.5)to_qasm3()keepsrx(1 / 2)verbatim and relabels the program as 3.0.Durations already behave:
delay[d / 2]stays 500.5ns.Change requested:
_is_integer_typed, treatIntegerLiteralas integer-typed only for OpenQASM 3 modules. AQasm2Modulemust keep true division. The prototype gated oncls.visitor_obj._module.original_program.version.QasmVisitor._visit_custom_gate_operation(visitor.py,param_map), sotransform_gate_paramssubstitutes aFloatLiteral:gate g(t) r { rx(t) r; }withg(3)dumpsrx(3.0)instead ofrx(3). No existing test changes because of it.to_qasm3()keep OpenQASM 2 semantics. For example, emit integer literals inside classical expressions as float literals (1.0 / 2).tests/qasm3/resources/qasm/custom_gate_nested.qasm:17(2 + 3 - 1/5). Either write1.0/5and keep 4.8 intests/utils.py:548,550, or expect 5.rx(0),rx(1)andrx(1). Addrx(3 / 4 * pi)→rx(0), an OpenQASM 2rx(1/2)→rx(0.5)row, and ato_qasm3()row. Keep bothg(3)andg(n)rows at 1.5.intoruintvariables". Call out the user-visible changesrx(1/2)→rx(0)and3/4*pi→ 0.The prototype of steps 1 and 2 leaves the suite at 1059 passed and 4 failed. The 4 failures are exactly the updates in steps 4 and 5.
Completed when: with
openqasm31.0.1,rx(1 / 2)→rx(0),rx(b / 2)→rx(1),g(3)withrx(t / 2)→rx(1.5), OpenQASM 2rx(1/2)and itsto_qasm3()output →rx(0.5), and the full suite passes.