From 6985361a1dcfcc1fa2f7055f483207405868d5e2 Mon Sep 17 00:00:00 2001 From: Ryan Shaffer <3620100+rmshaffer@users.noreply.github.com> Date: Mon, 18 Sep 2023 11:51:13 -0400 Subject: [PATCH] Return correctly-sized bit register (#707) --- src/braket/experimental/autoqasm/api.py | 14 +++++++++++--- .../braket/experimental/autoqasm/test_api.py | 4 +--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/braket/experimental/autoqasm/api.py b/src/braket/experimental/autoqasm/api.py index 4a6fb0649..e6d14af9a 100644 --- a/src/braket/experimental/autoqasm/api.py +++ b/src/braket/experimental/autoqasm/api.py @@ -437,10 +437,18 @@ def _make_return_instance_from_oqpy_return_type(return_type: Any) -> Any: if not return_type: return None - return_type = aq_types.conversions.var_type_from_ast_type(return_type) - if return_type == aq_types.ArrayVar: + var_type = aq_types.conversions.var_type_from_ast_type(return_type) + if var_type == aq_types.ArrayVar: return [] - return return_type() + if var_type == aq_types.BitVar: + return var_type(size=_get_bitvar_size(return_type)) + return var_type() + + +def _get_bitvar_size(node: qasm_ast.BitType) -> Optional[int]: + if not isinstance(node, qasm_ast.BitType) or not node.size: + return None + return node.size.value def _convert_gate( diff --git a/test/unit_tests/braket/experimental/autoqasm/test_api.py b/test/unit_tests/braket/experimental/autoqasm/test_api.py index 6f79b4634..5d0973d40 100644 --- a/test/unit_tests/braket/experimental/autoqasm/test_api.py +++ b/test/unit_tests/braket/experimental/autoqasm/test_api.py @@ -483,9 +483,7 @@ def ground_state_measurements_subroutine() -> bit[3] { return __bit_0__; } qubit[6] __qubits__; -""" - # TODO: this should be `bit[3]`, but there's a bug. It's being tracked in an issue. - expected += """bit __bit_1__; +bit[3] __bit_1__ = "000"; __bit_1__ = ground_state_measurements_subroutine();""" assert ground_state_measurements_wrapper().to_ir() == expected