Skip to content

Chained calls to c_if overwrite previous condition #7247

@jakelishman

Description

@jakelishman

Information

  • Qiskit Terra version: main
  • Python version: any
  • Operating system: any

What is the current behavior?

Instruction.c_if overwrites the previous condition, regardless of whether or not a condition existed. This means that chained calls do not produce nested if statements, but instead replace the call.

Steps to reproduce the problem

In [1]: from qiskit.circuit import QuantumCircuit, ClassicalRegister, Qubit
   ...: from qiskit.circuit.library import XGate
   ...: cr = ClassicalRegister(2)
   ...:
   ...: cond1 = (cr[0], False)
   ...: cond2 = (cr[1], True)
   ...:
   ...: qc = QuantumCircuit([Qubit()], cr)
   ...: qc.x(0).c_if(*cond1).c_if(*cond2)
   ...: qc.draw()
Out[1]:
         ┌───┐
   0: ───┤ X ├────
         └─╥─┘
      ┌────╨─────┐
c0: 2/c0_1 = T ╞
      └──────────┘

In [2]: XGate().c_if(*cond2) == qc.data[0][0]
Out[2]: True

What is the expected behavior?

There's no longer any reference to the first condition, whereas the documentation for c_if says it adds a condition, not overwrites it.

Suggested solutions

Fix the documentation of c_if. While a nesting behaviour would likely be more expected, there's no real need to do that since QASM 2 doesn't support it and we'd have to completely overhaul how conditions work. The new QASM 3-ish control-flow modes (#7123) are already coming, and they support arbitrary nesting. In the long term, we can probably deprecate c_if.

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