Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

[Panic] Ctl operation that calls an element of an operation-array parameter panics

Open
#3,974 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
compilers

Research direction

Start with the reported invariant failure in qsc_fir_transforms/src/invariants.rs:1926, then inspect the defunctionalization evaluation and index order changes from #3893. Use the Q# example and Python reproduction in the issue to compare indexed calls under Ctl and Adj controls. Done means the reproductions compile without panicking across the listed profiles while the control cases continue to compile.

Written by the indexing model from the issue text.

Description

bug needs triage
operation W(q : Qubit, ops : (Qubit => Unit is Ctl)[]) : Unit is Ctl {
    ops[0](q);
}
operation Main() : Result {
    use q = Qubit();
    W(q, [S]);
    MResetZ(q)
}
  • Failure: every profile, including Base, panics in qsc_fir_transforms/src/invariants.rs:1926:
    PostArgPromote/PostAll call invariant violation: Expr 67 passes Expr 66 with type Tuple([Array(Prim(Qubit)), Prim(Qubit)]) to callee Expr 65 expecting input type Prim(Qubit). The program never uses Controlled, so the
    panic probably comes from the generated controlled specialization of W, which passes (ctls, q) to an array
    element.
  • What triggers it: is Ctl or is Adj + Ctl, together with indexing (ops[i](q)). It compiles with is Adj
    alone, with for op in ops { op(q); }, or with a single operation parameter instead of an array.
  • Regression: a bisect over the commits between merge base 7c6a2b51 and main gives 6adfda105 (#3891)
    compiles it, and 84d1ec2e4 "Defunctionalization: evaluation and index order (#3893)" panics. Branches without
    #3893 (f05438eb, ca2fe553) compile it correctly.
  • Related: #3966 (open) reports the same invariant violation when
    Controlled is applied to a callable-array closure through the Python callable API. This is a much simpler
    trigger in plain Q#, with no closure, no Controlled call, and no Python, so it's likely the same root cause.
  • Impact: this accounts for half of all panics in the run, including all 8 Base panics, and stops every program
    with a W gate-array operation from compiling.
"""Panic: an `is Ctl` operation that calls an element of an operation-array parameter.

Every profile, including Base, panics in qsc_fir_transforms (invariants.rs) with
"PostArgPromote/PostAll call invariant violation ... passes ... Tuple([Array(Prim(Qubit)), Prim(Qubit)]) to callee
... expecting input type Prim(Qubit)". The program never uses `Controlled`, so the panic probably comes from the
generated controlled specialization of `W`. It's a regression: a bisect over upstream/main gives 6adfda105 (#3891) good,
84d1ec2e4 "Defunctionalization: evaluation and index order (#3893)" bad. Related: #3966 (the same invariant through the
Python callable API with a callable-array closure).

The controls show what the trigger needs: `is Ctl` (or `is Adj + Ctl`) and indexing into the array. They compile.

Requires only the `qdk` package. Exits 0 while the panic reproduces.
"""

import sys

from qdk import Context, TargetProfile

PROFILES = ("Base", "Adaptive_RI", "Adaptive_RIF", "Adaptive")

MAIN = """
operation Main() : Result {
    use q = Qubit();
    W(q, [S]);
    MResetZ(q)
}
"""


def w(functors: str, body: str) -> str:
    return f"operation W(q : Qubit, ops : (Qubit => Unit is {functors})[]) : Unit is {functors} {{ {body} }}"


PROGRAMS = {
    "Ctl, indexed (bug)": w("Ctl", "ops[0](q);"),
    "Adj + Ctl, indexed (bug)": w("Adj + Ctl", "ops[0](q);"),
    "Adj only, indexed (control)": w("Adj", "ops[0](q);"),
    "Ctl, for loop (control)": w("Ctl", "for op in ops { op(q); }"),
}


def compile_status(source: str, profile: str) -> str:
    ctx = Context(target_profile=getattr(TargetProfile, profile))
    try:
        ctx.eval(source)
        ctx.compile("Main()")
        return "ok"
    except BaseException as e:  # pyo3's PanicException derives from BaseException
        kind = "PANIC" if type(e).__name__ == "PanicException" else "error"
        return f"{kind}: {' '.join(str(e).split())[:110]}"


reproduced = False
for label, w in PROGRAMS.items():
    print(label)
    for profile in PROFILES:
        status = compile_status(w + MAIN, profile)
        print(f"  {profile:13s} {status}")
        if "(bug)" in label and "PostArgPromote" in status:
            reproduced = True

print("REPRODUCED" if reproduced else "NOT REPRODUCED")
sys.exit(0 if reproduced else 1)
Dominant language
Rust
Stars
1k
Forks
219
Avg merge
4d 1h
Merged PRs (30d)
88

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/qdk

All issues in microsoft/qdk

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.