You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Transpiling nested CONDINSTR fails with a misleading error #638
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.
fromgraphiximportCircuit, Instructionfromgraphix.fundamentalsimportAxiscircuit=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() # workscircuit.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.
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.
fromgraphiximport*fromgraphix.instructionimportInstructionKindfromcollections.abcimportIteratorimportitertoolsdefflatten_condinstr(
condinstr: Instruction.CONDINSTR,
top_domain: set[int] |None=None,
) ->Iterator[Instruction.CONDINSTR]:
top_domain=top_domainorset()
# It's crucial to create a fresh copy of `top_domain` at each recursion level!top_domain=top_domain|condinstr.domainforinstrincondinstr.instructions:
ifinstr.kind==InstructionKind.CONDINSTR:
yieldfromflatten_condinstr(instr, top_domain)
else:
yieldInstruction.CONDINSTR(top_domain, (instr,))
defmerge_condinstr(condinstrs: Iterator[Instruction.CONDINSTR]) ->Iterator[Instruction.CONDINSTR]:
"""Merge consecutive conditional instructions with the same domain into a single conditional instruction."""fordomain, groupinitertools.groupby(condinstrs, lambdainstr: instr.domain):
yieldInstruction.CONDINSTR(domain, tuple(instrforcondinstringroupforinstrincondinstr.instructions))
I'll open a PR once #644 is merged, since the later introduces significant changes in the transpiler.
Activity
matulni commentedon Nov 12, 2026
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_condinstrsplits "multi conditional instructions" into a sequence of "uni conditional instructions", I suggest adding a second passmerge_condinstrto unify consecutive conditional instructions with the same domain.I'll open a PR once #644 is merged, since the later introduces significant changes in the transpiler.