Skip to content

fix: end subroutine at a return nested in control flow - #443

Open
KainanP wants to merge 1 commit into
amazon-braket:mainfrom
KainanP:fix-subroutine-return
Open

KainanP wants to merge 1 commit into
amazon-braket:mainfrom
KainanP:fix-subroutine-return

Conversation

@KainanP

@KainanP KainanP commented Nov 30, 2026

Copy link
Copy Markdown

Issue #, if available: #437

Description of changes:
Only a return at the top level of a subroutine body ended the call; one inside if/for/while was evaluated and ignored, so later statements ran and a later return replaced the value. return now raises an internal signal that the call catches, like break/continue.
Inside control flow that depends on a mid-circuit measurement, paths can disagree on whether they returned, so that case now raises NotImplementedError instead of running the rest of the body. This also applies to a return that was the last statement there, which worked before.

Testing done:
New test_subroutine_nested_return (if, for, void + gate) fails on main (f(1) gives 7). New test_return_in_mcm_dependent_control_flow: the if/for cases fail on main (no error raised); the while case hangs on main. The issue's first program now always measures 1. Full test/unit_tests passes (2177 passed, 71 xfailed); ruff check src clean.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KainanP
KainanP requested a review from a team as a code owner November 30, 2026 16:20

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant