Skip to content

Break and continue support#3451

Open
idavis wants to merge 4 commits into
mainfrom
iadavis/break-cont
Open

Break and continue support#3451
idavis wants to merge 4 commits into
mainfrom
iadavis/break-cont

Conversation

@idavis

@idavis idavis commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Closes #1835

@idavis idavis self-assigned this Jul 10, 2026
@idavis
idavis force-pushed the iadavis/break-cont branch from 01bb261 to 5c93ad6 Compare July 10, 2026 21:08
@idavis
idavis force-pushed the iadavis/break-cont branch from 5c93ad6 to 5af8087 Compare July 13, 2026 18:03
@idavis
idavis force-pushed the iadavis/break-cont branch from 5af8087 to 6e53058 Compare July 13, 2026 18:22
@idavis
idavis marked this pull request as ready for review July 13, 2026 18:40
@amcasey

amcasey commented Jul 13, 2026

Copy link
Copy Markdown
Member

This is probably supposed to report that break isn't allowed there, rather than that the type couldn't be inferred

operation Main() : Unit {
    mutable x = 1;
    while (true) {
        () -> break;
    }
    x += 1;
}
QdkDiagnostics: type error: insufficient type information to infer type

help: provide a type annotation at code:4:15

@amcasey

amcasey commented Jul 13, 2026

Copy link
Copy Markdown
Member

This reports an RIR timeout

operation Main() : Unit {
    use q = Qubit();
    for p in [q] {
        for p in [q] {
            for p in [q] {
                for p in [q] {
                    for p in [q] {
                        for p in [q] {
                            break
                        }
                    }
                }
            }
        }
    }
}

"patterns": [
{
"name": "keyword.control.qsharp",
"match": "\\b(use|borrow|mutable|let|set|if|elif|else|repeat|until|fixup|for|in|while|return|fail|within|apply)\\b"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you expect the local playground to pick this up? I'm not seeing it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amcasey I think monaco uses their own language def based on ours. @billti - do you remember?

@amcasey

amcasey commented Jul 14, 2026

Copy link
Copy Markdown
Member

I'm seeing an LS panic (and a possibly downstream RIR generation failure) for

@EntryPoint(Adaptive)
operation Main() : Result {
    use qq = Qubit[2];
    repeat {
        H(qq[0]);
        Controlled X([qq[break]], qq[1]);
    } until (M(qq[1]) == One)
    fixup {
        ResetAll(qq);
    }
    let r = M(qq[0]);
    ResetAll(qq);
    r
}
[error] [wasm] Wasm panic occurred: panicked at source\compiler\qsc_partial_eval\src\lib.rs:2722:18:
invalid kind of value for index
RuntimeError: unreachable

Related: with Controlled X([break], qq[1]);, I see

QdkDiagnostics: partial evaluation error: partial evaluation failed with error: index out of range: 0 at code:6:23

Interestingly, Controlled X(break, qq[1]); appears to work (i.e. emit QIR and run)

@amcasey

amcasey commented Jul 14, 2026

Copy link
Copy Markdown
Member

LS panic for

@EntryPoint(Adaptive)
operation Main() : Result {
    use qq = Qubit[2];
    repeat {
        H(qq[0]);
        use rr = Qubit[break];
        Controlled X(break );
    } until (M(qq[1]) == One)
    fixup {
        ResetAll(qq);
    }
    let r = M(qq[0]);
    ResetAll(qq);
    r
}
Wasm panic occurred: panicked at source\compiler\qsc_rca\src\core.rs:2807:13:
expected tuple expression

I was about to type a comma after the break in the Controlled

@amcasey

amcasey commented Jul 14, 2026

Copy link
Copy Markdown
Member

I don't think this needs to work, but it stands out from a bunch of other nonsensical code that does work

@EntryPoint(Adaptive)
operation Main() : Result {
    use qq = Qubit[2];
    repeat {
        H(qq[break]);
        Controlled X(continue, qq[1]);
    } until (M(qq[1]) == One)
    fixup {
        ResetAll(qq);
    }
    let r = M(qq[0]);
    ResetAll(qq);
    r
}
main.qs@(4,5): cannot use a dynamically-sized array
help: using an array whose size depends on a measurement result is not supported by the configured target profile

In particular, the same example emits QIR with H(qq[continue]) (though I'm pretty sure it would loop infinitely?)

@idavis
idavis requested a review from ScottCarda-MS as a code owner July 14, 2026 16:14
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.

Language Feature: break / continue support for Q# and OpenQASM

2 participants