-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into mangle-openqasm-reserved-words
- Loading branch information
Showing
5 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,63 @@ | ||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file 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. | ||
|
||
"""Converters for aritmetic operator nodes""" | ||
|
||
import ast | ||
|
||
import gast | ||
from malt.core import ag_ctx, converter | ||
from malt.pyct import templates | ||
|
||
ARITHMETIC_OPERATORS = { | ||
gast.FloorDiv: "ag__.floor_div", | ||
} | ||
|
||
|
||
class ArithmeticTransformer(converter.Base): | ||
"""Transformer for arithmetic nodes.""" | ||
|
||
def visit_BinOp(self, node: ast.stmt) -> ast.stmt: | ||
"""Transforms a BinOp node. | ||
Args : | ||
node(ast.stmt) : AST node to transform | ||
Returns : | ||
ast.stmt : Transformed node | ||
""" | ||
node = self.generic_visit(node) | ||
op_type = type(node.op) | ||
if op_type not in ARITHMETIC_OPERATORS: | ||
return node | ||
|
||
template = f"{ARITHMETIC_OPERATORS[op_type]}(lhs_,rhs_)" | ||
|
||
new_node = templates.replace( | ||
template, | ||
lhs_=node.left, | ||
rhs_=node.right, | ||
original=node, | ||
)[0].value | ||
|
||
return new_node | ||
|
||
|
||
def transform(node: ast.stmt, ctx: ag_ctx.ControlStatusCtx) -> ast.stmt: | ||
"""Transform arithmetic nodes. | ||
Args: | ||
node(ast.stmt) : AST node to transform | ||
ctx (ag_ctx.ControlStatusCtx) : Transformer context. | ||
Returns : | ||
ast.stmt : Transformed node. | ||
""" | ||
|
||
return ArithmeticTransformer(ctx).visit(node) |
This file contains 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 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,81 @@ | ||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file 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. | ||
|
||
"""Operators for arithmetic operators: // """ | ||
|
||
from __future__ import annotations | ||
|
||
from autoqasm import program | ||
from autoqasm import types as aq_types | ||
|
||
from .utils import _register_and_convert_parameters | ||
|
||
|
||
def floor_div( | ||
num: aq_types.IntVar | aq_types.FloatVar | int | float, | ||
den: aq_types.IntVar | aq_types.FloatVar | int | float, | ||
) -> int | aq_types.IntVar: | ||
"""Functional form of "//". | ||
Args: | ||
num (IntVar | FloatVar | int | float) : The numerator of the integer division | ||
den (IntVar | FloatVar | int | float) : The denominator of the integer division | ||
Returns : | ||
int | IntVar : integer division, IntVar if either numerator or denominator | ||
are QASM types, else int | ||
""" | ||
if aq_types.is_qasm_type(num) or aq_types.is_qasm_type(den): | ||
return _oqpy_floor_div(num, den) | ||
else: | ||
return _py_floor_div(num, den) | ||
|
||
|
||
def _oqpy_floor_div( | ||
num: aq_types.IntVar | aq_types.FloatVar | int | float, | ||
den: aq_types.IntVar | aq_types.FloatVar | int | float, | ||
) -> aq_types.IntVar | aq_types.FloatVar: | ||
num, den = _register_and_convert_parameters(num, den) | ||
oqpy_program = program.get_program_conversion_context().get_oqpy_program() | ||
num_is_float = isinstance(num, (aq_types.FloatVar, float)) | ||
den_is_float = isinstance(den, (aq_types.FloatVar, float)) | ||
|
||
# if either is a FloatVar, then both must be FloatVar | ||
if num_is_float and not den_is_float: | ||
den_float_var = aq_types.FloatVar() | ||
oqpy_program.declare(den_float_var) | ||
oqpy_program.set(den_float_var, den) | ||
den = den_float_var | ||
if den_is_float and not num_is_float: | ||
num_float_var = aq_types.FloatVar() | ||
oqpy_program.declare(num_float_var) | ||
oqpy_program.set(num_float_var, num) | ||
num = num_float_var | ||
|
||
# if either is a FloatVar, then the result will be a FloatVar | ||
result = aq_types.IntVar() | ||
oqpy_program.declare(result) | ||
oqpy_program.set(result, num / den) | ||
|
||
if num_is_float or den_is_float: | ||
float_result = aq_types.FloatVar() | ||
oqpy_program.declare(float_result) | ||
oqpy_program.set(float_result, result) | ||
return float_result | ||
|
||
return result | ||
|
||
|
||
def _py_floor_div( | ||
num: int | float, | ||
den: int | float, | ||
) -> int | float: | ||
return num // den |
This file contains 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 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