Repository: Qiskit/qiskit

Issue:
MPL drawer hangs on dynamic circuits when fold=-1
### Environment

- **Qiskit version**: latest
- **Python version**: 
- **Operating system**: OSX


### What is happening?

```python

from qiskit import *
from qiskit.quantum_info import random_unitary
# Create a random state for qubit C
random_1q_gate = random_unitary(2).to_instruction()

# Create a register for each qubit of interest
qrA = QuantumRegister(1, 'qA')
qrB = QuantumRegister(1, 'qB')
qrC = QuantumRegister(1, 'qC')
anc_top = QuantumRegister(1, 'ancilla_top')
anc_bottom = QuantumRegister(1, 'ancilla_bottom')

# Create a single classical register
cr = ClassicalRegister(2, 'c')
cr_ancilla = ClassicalRegister(1, 'ancilla_bits')
# Create circuit
qc = QuantumCircuit(anc_top, qrC, qrA, qrB, anc_bottom, cr, cr_ancilla)
qc.reset(range(5))
# Start in the |1> state
qc.x(qrC)
# Make C qubit in random quantum state
qc.compose(random_1q_gate, qrC, inplace=True)
# Form Bell pair between A and B
qc.h(qrA)
qc.cx(qrA, qrB)
qc.barrier()

# Bell measurement
qc.cx(qrC, qrA)
qc.h(qrC)
qc.measure([qrC[0],qrA[0]], [0,1])

qc.barrier()
# Corrective Pauli gates
with qc.switch(cr) as case:
    # If returned bit-string is 00 = 0
    with case(0):
        pass
    # If returned bit-string is 01 = 1
    with case(1):
        qc.z(qrB)
    # If returned bit-string is 10 = 2
    with case(2):
        qc.x(qrB)
# If returned bit-string is 11 = 3
    with case(3):
        qc.z(qrB)
        qc.x(qrB)

qc.barrier()
# Apply inverse of unitary that generated state C
qc.compose(random_1q_gate.inverse(), qrB, inplace=True)
# We should be back in the ground state
qc.cx(qrB, anc_bottom)
qc.measure(anc_bottom, cr_ancilla[0])
#qc.measure(qrB, 3)

qc.draw('mpl', fold=-1)
```

will hang.  Removing `fold=-1` works.

### How can we reproduce the issue?

run the above

### What should happen?

It should not hang.

### Any suggestions?

_No response_


Discussion:
Thanks for finding this, @nonhermitian. I have a one-line fix for it and I'll submit a PR.

Resolving patch:
diff --git a/qiskit/visualization/circuit/matplotlib.py b/qiskit/visualization/circuit/matplotlib.py
--- a/qiskit/visualization/circuit/matplotlib.py
+++ b/qiskit/visualization/circuit/matplotlib.py
@@ -1544,7 +1544,7 @@ def _flow_op_gate(self, node, node_data, glob_data):
         while end_x > 0.0:
             x_shift = fold_level * self._fold
             y_shift = fold_level * (glob_data["n_lines"] + 1)
-            end_x = xpos + box_width - x_shift
+            end_x = xpos + box_width - x_shift if self._fold > 0 else 0.0
 
             if isinstance(node.op, IfElseOp):
                 flow_text = "  If"


Test patch:
diff --git a/test/visual/mpl/circuit/references/control_flow_fold_minus_one.png b/test/visual/mpl/circuit/references/control_flow_fold_minus_one.png
new file mode 100644
Binary files /dev/null and b/test/visual/mpl/circuit/references/control_flow_fold_minus_one.png differ
diff --git a/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py b/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py
--- a/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py
+++ b/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py
@@ -2228,6 +2228,27 @@ def test_control_flow_nested_layout(self):
         )
         self.assertGreaterEqual(ratio, self.threshold)
 
+    def test_control_flow_with_fold_minus_one(self):
+        """Test control flow works with fold=-1. Qiskit issue #12012"""
+        qreg = QuantumRegister(2, "qr")
+        creg = ClassicalRegister(2, "cr")
+        circuit = QuantumCircuit(qreg, creg)
+        with circuit.if_test((creg[1], 1)):
+            circuit.h(0)
+            circuit.cx(0, 1)
+
+        fname = "control_flow_fold_minus_one.png"
+        self.circuit_drawer(circuit, output="mpl", filename=fname, fold=-1)
+
+        ratio = VisualTestUtilities._save_diff(
+            self._image_path(fname),
+            self._reference_path(fname),
+            fname,
+            FAILURE_DIFF_DIR,
+            FAILURE_PREFIX,
+        )
+        self.assertGreaterEqual(ratio, self.threshold)
+
     def test_annotated_operation(self):
         """Test AnnotatedOperations and other non-Instructions."""
         circuit = QuantumCircuit(3)