Skip to content

[Miscompile] Incorrect defunctionalization #3768

Open

Description

@amcasey

Courtesy of copilot. Confirmed via QIR generation in VS Code

"""
`Choose(true)` should return `H`, so the QIR should apply `h` to qubit 0. The
defunctionalized branch tree remaps both `b` guards to the same caller expression
and keeps the last inner default, so the QIR applies `x` instead. There's no panic.

Exits 0 while the miscompile reproduces. Found by opus-b17 in the panic hunt
(qsc_fir_transforms/src/defunctionalize/rewrite.rs, build_branch_tree).
"""

import re
import sys

from qdk import qsharp

SOURCE = """
namespace Demo {
    function Choose(b : Bool) : (Qubit => Unit) {
        if b { H } else { if b { X } else { Z } }
    }
    operation Main() : Result {
        use q = Qubit();
        let arr = [1, 2, 3];
        let x = Length(arr) > 1;
        let f = Choose(x);
        f(q);
        MResetZ(q)
    }
}
"""

qsharp.init(target_profile=qsharp.TargetProfile.Adaptive_RIF)
qsharp.eval(SOURCE)
qir = str(qsharp.compile("Demo.Main()"))
gates = re.findall(r"@__quantum__qis__(\w+)__body\(%Qubit\* (?:null|inttoptr \(i64 0 to %Qubit\*\))", qir)
print("gates on qubit 0:", gates)

if "x" in gates and "h" not in gates:
    print("REPRODUCED: QIR applies X where Choose(true) should give H")
    sys.exit(0)
print("NOT REPRODUCED")
sys.exit(1)

Activity

  1. changed the title [-]Miscompile in defunctionalization[/-] [+][Miscompile] Incorrect defunctionalization[/+] on Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions