-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add converter for comparison statements. Add new tests
- Loading branch information
Showing
8 changed files
with
285 additions
and
33 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/braket/experimental/autoqasm/converters/comparisons.py
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,70 @@ | ||
# 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 comparison nodes.""" | ||
|
||
import ast | ||
|
||
import gast | ||
|
||
from braket.experimental.autoqasm.autograph.core import ag_ctx, converter | ||
from braket.experimental.autoqasm.autograph.pyct import templates | ||
|
||
COMPARISON_OPERATORS = { | ||
gast.Lt: "ag__.lt_", | ||
gast.LtE: "ag__.lteq_", | ||
gast.Gt: "ag__.gt_", | ||
gast.GtE: "ag__.gteq_", | ||
} | ||
|
||
|
||
class ComparisonTransformer(converter.Base): | ||
"""Transformer for comparison nodes.""" | ||
|
||
def visit_Compare(self, node: ast.stmt) -> ast.stmt: | ||
"""Transforms a comparison node. | ||
Args: | ||
node (ast.stmt): AST node to transform. | ||
Returns: | ||
ast.stmt: Transformed node. | ||
""" | ||
node = self.generic_visit(node) | ||
|
||
op_type = type(node.ops[0]) | ||
if op_type not in COMPARISON_OPERATORS: | ||
return node | ||
|
||
template = f"{COMPARISON_OPERATORS[op_type]}(lhs_, rhs_)" | ||
|
||
return templates.replace( | ||
template, | ||
lhs_=node.left, | ||
rhs_=node.comparators[0], | ||
original=node, | ||
)[0].value | ||
|
||
|
||
def transform(node: ast.stmt, ctx: ag_ctx.ControlStatusCtx) -> ast.stmt: | ||
"""Transform comparison nodes. | ||
Args: | ||
node (ast.stmt): AST node to transform. | ||
ctx (ag_ctx.ControlStatusCtx): Transformer context. | ||
Returns: | ||
ast.stmt: Transformed node. | ||
""" | ||
|
||
return ComparisonTransformer(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
137 changes: 137 additions & 0 deletions
137
src/braket/experimental/autoqasm/operators/comparisons.py
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,137 @@ | ||
# 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 comparison operators: <, <=, >, and >=.""" | ||
|
||
from collections.abc import Callable | ||
from typing import Any, Union | ||
|
||
import oqpy.base | ||
|
||
from braket.experimental.autoqasm import program | ||
from braket.experimental.autoqasm import types as aq_types | ||
|
||
from .utils import _convert_parameters | ||
|
||
|
||
def lt_(a: Any, b: Any) -> Union[bool, aq_types.BoolVar]: | ||
"""Functional form of "<". | ||
Args: | ||
a (Any): Callable that returns the first expression. | ||
b (Any): Callable that returns the second expression. | ||
Returns: | ||
Union[bool, BoolVar]: Whether the first expression is less than the second. | ||
""" | ||
if aq_types.is_qasm_type(a) or aq_types.is_qasm_type(b): | ||
return _aq_lt(a, b) | ||
else: | ||
return a < b | ||
|
||
|
||
def _aq_lt(a: Any, b: Any) -> aq_types.BoolVar: | ||
program_conversion_context = program.get_program_conversion_context() | ||
program_conversion_context.register_args([a, b]) | ||
a, b = _convert_parameters(a, b) | ||
|
||
oqpy_program = program_conversion_context.get_oqpy_program() | ||
result = aq_types.BoolVar() | ||
oqpy_program.declare(result) | ||
oqpy_program.set(result, a < b) | ||
return result | ||
|
||
|
||
def lteq_(a: Any, b: Any) -> Union[bool, aq_types.BoolVar]: | ||
"""Functional form of "<=". | ||
Args: | ||
a (Any): Callable that returns the first expression. | ||
b (Any): Callable that returns the second expression. | ||
Returns: | ||
Union[bool, BoolVar]: Whether the first expression is less than or equal to the second. | ||
""" | ||
if aq_types.is_qasm_type(a) or aq_types.is_qasm_type(b): | ||
return _aq_lteq(a, b) | ||
else: | ||
return a <= b | ||
|
||
|
||
def _aq_lteq(a: Any, b: Any) -> aq_types.BoolVar: | ||
program_conversion_context = program.get_program_conversion_context() | ||
program_conversion_context.register_args([a, b]) | ||
a, b = _convert_parameters(a, b) | ||
|
||
oqpy_program = program_conversion_context.get_oqpy_program() | ||
result = aq_types.BoolVar() | ||
oqpy_program.declare(result) | ||
oqpy_program.set(result, a <= b) | ||
return result | ||
|
||
|
||
def gt_(a: Any, b: Any) -> Union[bool, aq_types.BoolVar]: | ||
"""Functional form of ">". | ||
Args: | ||
a (Any): Callable that returns the first expression. | ||
b (Any): Callable that returns the second expression. | ||
Returns: | ||
Union[bool, BoolVar]: Whether the first expression greater than the second. | ||
""" | ||
if aq_types.is_qasm_type(a) or aq_types.is_qasm_type(b): | ||
return _aq_gt(a, b) | ||
else: | ||
return a > b | ||
|
||
|
||
def _aq_gt(a: Any, b: Any) -> aq_types.BoolVar: | ||
program_conversion_context = program.get_program_conversion_context() | ||
program_conversion_context.register_args([a, b]) | ||
a, b = _convert_parameters(a, b) | ||
|
||
oqpy_program = program_conversion_context.get_oqpy_program() | ||
result = aq_types.BoolVar() | ||
oqpy_program.declare(result) | ||
oqpy_program.set(result, a > b) | ||
return result | ||
|
||
|
||
def gteq_(a: Any, b: Any) -> Union[bool, aq_types.BoolVar]: | ||
"""Functional form of ">=". | ||
Args: | ||
a (Any): Callable that returns the first expression. | ||
b (Any): Callable that returns the second expression. | ||
Returns: | ||
Union[bool, BoolVar]: Whether the first expression greater than or equal to the second. | ||
""" | ||
if aq_types.is_qasm_type(a) or aq_types.is_qasm_type(b): | ||
return _aq_gteq(a, b) | ||
else: | ||
return a >= b | ||
|
||
|
||
def _aq_gteq(a: Any, b: Any) -> aq_types.BoolVar: | ||
program_conversion_context = program.get_program_conversion_context() | ||
program_conversion_context.register_args([a, b]) | ||
a, b = _convert_parameters(a, b) | ||
|
||
oqpy_program = program_conversion_context.get_oqpy_program() | ||
result = aq_types.BoolVar() | ||
oqpy_program.declare(result) | ||
oqpy_program.set(result, a >= b) | ||
return result |
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,41 @@ | ||
# 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. | ||
|
||
|
||
"Utility methods for operators." | ||
|
||
from braket.circuits import FreeParameter | ||
from braket.experimental.autoqasm import program | ||
from braket.experimental.autoqasm import types as aq_types | ||
|
||
|
||
def _convert_parameters(*args: list[FreeParameter]) -> list[aq_types.FloatVar]: | ||
"""Converts FreeParameter objects to FloatVars through the program conversion context | ||
parameter registry. | ||
FloatVars are more compatible with the program conversion operations. | ||
Args: | ||
args (list[FreeParameter]): FreeParameter objects. | ||
Returns: | ||
list[FloatVar]: FloatVars for program conversion. | ||
""" | ||
result = [] | ||
for arg in args: | ||
if isinstance(arg, FreeParameter): | ||
var = program.get_program_conversion_context().get_parameter(arg.name) | ||
result.append(var) | ||
else: | ||
result.append(arg) | ||
return result[0] if len(result) == 1 else result |
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
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