qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Control flow instructions don't work with `circuit_to_instruction`

Open mtreinish opened this issue 1 year ago • 6 comments

Environment

  • Qiskit Terra version: 1.0.0.dev0+ac7aca3
  • Python version: 3.11
  • Operating system: Linux

What is happening?

Trying to convert a circuit with a control flow operation in it to an instruction using qiskit.converters.circuit_to_instruction fails while processing the control flow condition. It is treating it like a c_if legacy style condition even for control flow ops and this causes it to fail with a NotImplementedError because the function is trying to call c_if.

How can we reproduce the issue?

>>> qc = QuantumCircuit(2)
>>> qc.h(0)
<qiskit.circuit.instructionset.InstructionSet object at 0x7f0813ebc280>
>>> qc.cx(0, 1)
<qiskit.circuit.instructionset.InstructionSet object at 0x7f0813ebef80>
>>> qc.measure_all()
>>> with qc.if_test((qc.clbits[0], 0)):
...     qc.x(0)
>>> circuit_to_instruction(qc)

What should happen?

We either should have a descriptive error saying we can't nest control flow operations in an instruction or update the .condition handling in circuit_to_instruction to understand a control flow operation.

Any suggestions?

No response

mtreinish avatar Dec 07 '23 16:12 mtreinish

This needs to be an error in practice; there's no way to communicate the creg / variable requirements of an Instruction's inner definition up to the main circuit, so allowing arbitrary control-flow within a semi-opaque instruction wouldn't work.

Totally agree that the error message should be way better.

jakelishman avatar Dec 07 '23 19:12 jakelishman

Really, circuit_to_instruction shouldn't even allow c_if in it - in practice, that hasn't ever worked fully correctly when registers are involved.

jakelishman avatar Dec 07 '23 19:12 jakelishman

@jakelishman This used to work well until version 0.45.0 Could you suggest what alternative should be used for this as I am writing programs to implement error correction and if_test functionality is much used in my program?

maheshwaripranav avatar Jan 30 '24 10:01 maheshwaripranav

There should be no need to use circuit_to_instruction. If you want to build up circuit parts and then combine them into a larger circuit, you can use QuantumCircuit.compose to "inline" the smaller circuit into the bigger one. QuantumCircuit.append cannot really cope with complex classical data like control-flow constructs, because the necessary classical data available properly in the QuantumCircuit resource tracking through CircuitInstruction.

jakelishman avatar Jan 30 '24 10:01 jakelishman

There's no particular guarantee that any classical handling was working correctly before if circuit_to_instruction was used (also via QuantumCircuit.append) - the circuit would silently not notice if the classical control flow was represented incorrectly.

jakelishman avatar Jan 30 '24 10:01 jakelishman

We didn't get this done for 1.0, so we'll have to revisit for 1.1 and see what's reasonable for us to do within the stability policy.

jakelishman avatar Feb 01 '24 22:02 jakelishman

@jakelishman @mtreinish I can work on this. I just need clarification if the fix would be to remove the c_if option altogether or add a check to make sure that it is not a control_flow operation and throw an Exception if it is

melechlapson avatar Aug 14 '24 16:08 melechlapson