Skip to content

[Bug]: Transpiling nested CONDINSTR fails with a misleading error #638

Open

Description

@thierry-martinez

Issue detected and written with the help of Claude Opus 5.5.

A CONDINSTR can contain other CONDINSTR instructions, and such circuits simulate correctly. However, they can't be transpiled: Circuit.transpile_condinstr (called by Circuit.transpile) fails with an error that doesn't point to the cause.

from graphix import Circuit, Instruction
from graphix.fundamentals import Axis

circuit = Circuit(3)
circuit.m(0, Axis.Z)
circuit.m(1, Axis.Z)
# Apply X(2) if both measurement outcomes are 1.
circuit.condinstr([Instruction.CONDINSTR((Instruction.X(2),), {1})], {0})

circuit.simulate()  # works
circuit.transpile()  # ValueError: Qubit 1 is not a measured qubit.

transpile_condinstr defers all measurements to the end of the circuit and then re-adds the nested CONDINSTR unchanged, as part of the controlled body. In the new circuit, qubit 1 hasn't been measured yet, so validating the inner domain fails. Even if validation passed, instructions_to_jcz would then raise NotImplementedError, saying to call transpile_condinstr first, which is the method that just ran.

The same circuit with a single, flat CONDINSTR over domain {0, 1} transpiles fine. The problem is specific to nesting.

Activity

  1. self-assigned this
    on Nov 5, 2026
  2. matulni commented on Nov 12, 2026

    @matulni
    Contributor

    To keep a modular implementation, I suggest flattening conditional instructions before deferring the measurements. I paste a simple implementation below. Because the implementation for flatten_condinstr splits "multi conditional instructions" into a sequence of "uni conditional instructions", I suggest adding a second pass merge_condinstr to unify consecutive conditional instructions with the same domain.

    from graphix import *
    from graphix.instruction import InstructionKind
    from collections.abc import Iterator
    import itertools
    
    def flatten_condinstr(
        condinstr: Instruction.CONDINSTR,
        top_domain: set[int] | None = None,
    ) -> Iterator[Instruction.CONDINSTR]:
        top_domain = top_domain or set()
    
        # It's crucial to create a fresh copy of `top_domain` at each recursion level!
        top_domain = top_domain | condinstr.domain
    
        for instr in condinstr.instructions:
            if instr.kind == InstructionKind.CONDINSTR:
                yield from flatten_condinstr(instr, top_domain)
            else:
                yield Instruction.CONDINSTR(top_domain, (instr,))
    
    def merge_condinstr(condinstrs: Iterator[Instruction.CONDINSTR]) -> Iterator[Instruction.CONDINSTR]:
        """Merge consecutive conditional instructions with the same domain into a single conditional instruction."""
        for domain, group in itertools.groupby(condinstrs, lambda instr: instr.domain):
            yield Instruction.CONDINSTR(domain, tuple(instr for condinstr in group for instr in condinstr.instructions))

    I'll open a PR once #644 is merged, since the later introduces significant changes in the transpiler.

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions