Skip to content
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

Usability: Warning when user calls "return" from their autoqasm entry point function #661

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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 return statement nodes."""

import ast
import warnings

from braket.experimental.autoqasm import program
from braket.experimental.autoqasm.autograph.converters import return_statements
from braket.experimental.autoqasm.autograph.core import ag_ctx, converter


class ReturnValidator(converter.Base):
def visit_Return(self, node: ast.stmt) -> ast.stmt:
aq_context = program.get_program_conversion_context()
if not aq_context.subroutines_processing and node.value is not None:
warnings.warn("Return value from top level function is ignored.")
return node


def transform(
node: ast.stmt, ctx: ag_ctx.ControlStatusCtx, default_to_null_return: bool = True
) -> ast.stmt:
"""Handle AutoQASM-specific return statement functionality before
passing control to AutoGraph.
"""
ReturnValidator(ctx).visit(node)
return return_statements.transform(node, ctx)
laurencap marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 1 addition & 2 deletions src/braket/experimental/autoqasm/transpiler/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
functions,
lists,
logical_expressions,
return_statements,
slices,
variables,
)
Expand All @@ -59,7 +58,7 @@
reaching_definitions,
)
from braket.experimental.autoqasm.autograph.tf_utils import tf_stack
from braket.experimental.autoqasm.converters import assignments, break_statements
from braket.experimental.autoqasm.converters import assignments, break_statements, return_statements


class PyToOqpy(transpiler.PyToPy):
Expand Down
22 changes: 22 additions & 0 deletions test/unit_tests/braket/experimental/autoqasm/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,25 @@ def ghz(n: int):
cnot __qubits__[0], __qubits__[4];"""

assert make_ghz(5).to_ir() == expected


def test_main_return():
@aq.function
def main() -> int:
return 1

with pytest.warns(UserWarning, match="Return value from top level function is ignored"):
main()


def test_main_no_return():
@aq.function
def tester(x: int) -> int:
return measure(x)

@aq.function(num_qubits=3)
def main():
x = 3
tester(x)

main()