-
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.
feature: add support for typecasting (#27)
* feature: add support for typecasting * fix: changes to int typecasting * fix: add support for args and kwargs in typecasting) * fix: add unit tests for int typecasting * fix: update typecasting tests * fix: remove unused dependency * fix: update operator to force casting in the correct location * fix: type hint * fix: add cosmetic changes * Update test/unit_tests/autoqasm/test_operators.py * fix: add xfail test * fix: clarify test name --------- Co-authored-by: Ryan Shaffer <[email protected]>
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 deletions.
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,67 @@ | ||
# 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 integer casting nodes.""" | ||
|
||
import ast | ||
|
||
from malt.core import ag_ctx, converter | ||
from malt.pyct import templates | ||
|
||
TYPECASTING_OPERATORS = { | ||
"int": "ag__.int_", | ||
} | ||
|
||
|
||
class TypecastTransformer(converter.Base): | ||
def visit_Call(self, node: ast.stmt) -> ast.stmt: | ||
"""Converts type casting operations to their AutoQASM counterpart. | ||
Args: | ||
node (ast.stmt): AST node to transform. | ||
Returns: | ||
ast.stmt: Transformed node. | ||
""" | ||
node = self.generic_visit(node) | ||
|
||
if ( | ||
hasattr(node, "func") | ||
and hasattr(node.func, "id") | ||
and node.func.id in TYPECASTING_OPERATORS | ||
): | ||
template = f"{TYPECASTING_OPERATORS[node.func.id]}(argument)" | ||
new_node = templates.replace( | ||
template, | ||
argument=node.args, | ||
original=node, | ||
) | ||
new_node = new_node[0].value | ||
else: | ||
new_node = node | ||
return new_node | ||
|
||
|
||
def transform(node: ast.stmt, ctx: ag_ctx.ControlStatusCtx) -> ast.stmt: | ||
"""Transform int cast nodes. | ||
Args: | ||
node (ast.stmt): AST node to transform. | ||
ctx (ag_ctx.ControlStatusCtx): Transformer context. | ||
Returns: | ||
ast.stmt: Transformed node. | ||
""" | ||
|
||
return TypecastTransformer(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,48 @@ | ||
# 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 int cast statements.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from autoqasm import program | ||
from autoqasm import types as aq_types | ||
|
||
|
||
def int_(argument: Any, *args, **kwargs) -> aq_types.IntVar | int: | ||
"""Functional form of "int". | ||
Args: | ||
argument (Any): object to be converted into an int. | ||
Returns: | ||
IntVar | int : IntVar object if argument is QASM type, else int. | ||
""" | ||
if aq_types.is_qasm_type(argument): | ||
return _oqpy_int(argument) | ||
else: | ||
return _py_int(argument, *args, **kwargs) | ||
|
||
|
||
def _oqpy_int(argument: Any) -> aq_types.IntVar: | ||
oqpy_program = program.get_program_conversion_context().get_oqpy_program() | ||
result = aq_types.IntVar() | ||
oqpy_program.declare(result) | ||
oqpy_program.set(result, argument) | ||
return result | ||
|
||
|
||
def _py_int(argument: Any, *args, **kwargs) -> int: | ||
return int(argument, *args, **kwargs) |
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