-
Notifications
You must be signed in to change notification settings - Fork 9
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
feature: add support for typecasting #27
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
66d5459
feature: add support for typecasting
abidart 8a09bdd
fix: changes to int typecasting
abidart 3f63229
fix: add support for args and kwargs in typecasting)
abidart 17cd488
fix: add unit tests for int typecasting
abidart 0ca1252
fix: update typecasting tests
abidart 16fe026
fix: remove unused dependency
abidart c477d76
fix: update operator to force casting in the correct location
abidart d64bfad
fix: type hint
abidart 638d3dd
fix: add cosmetic changes
abidart 129af14
Merge branch 'main' into main
rmshaffer a158117
Update test/unit_tests/autoqasm/test_operators.py
rmshaffer f71190d
fix: add xfail test
abidart 52f99f1
Merge branch 'main' into main
rmshaffer f50afec
fix: clarify test name
abidart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 we raise an error, instead?
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.
Hello @laurencap, thank you so much for the review! My goal with this test was to have a "normal" call to
int
within a program to check that the built-inint
function gets called—correctly, with all passed arguments—whenever the first input does not involve a QASM type. Could you explain me how can I change it?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.
I believe this is working as intended, but maybe the confusion could be cleared up by changing the name of the test function to make it more clear what is being tested - e.g.
test_int_typecasting_on_python_string_not_captured