Repository: Qiskit/qiskit

Issue:
`to_instruction()` of a loop-body circuit fails if the loop-body has `if_test` with `clbit` condition.
### Environment

- **Qiskit Terra version**: main (f85cd87247be95572d28f038ac19b2bab7636346)
- **Python version**: Any
- **Operating system**: Any


### What is happening?

`UnboundLocalError` is thrown when a loop-body circuit calls `to_instruction()`.

```
  File "error.py", line 14, in <module>
    loop_body.to_instruction()
  File "site-packages/qiskit/circuit/quantumcircuit.py", line 1482, in to_instruction
    return circuit_to_instruction(self, parameter_map, label=label)
  File "site-packages/qiskit/converters/circuit_to_instruction.py", line 123, in circuit_to_instruction
    rule[0].condition = (c[idx + list(cond_reg).index(reg)], val)
UnboundLocalError: local variable 'cond_reg' referenced before assignment
```

The loop-body has `if_test` and its test expression is a tuple of `Clbit` and a value.
If the tuple consists of `ClassicalRegister` and a value, this error does not occur.

### How can we reproduce the issue?

```
from qiskit import QuantumCircuit
from qiskit.circuit import QuantumRegister, ClassicalRegister

qreg = QuantumRegister(4)
creg = ClassicalRegister(1)
circ = QuantumCircuit(qreg, creg)

with circ.for_loop(range(1)):
    with circ.if_test((circ.clbits[0], 0)):
        circ.h(0)

for_loop_inst, _, _ = circ._data[0]
loop_body = for_loop_inst.params[2]
loop_body.to_instruction()
```

### What should happen?

No error.

### Any suggestions?

_No response_


Discussion:
This isn't an issue with the control-flow builders particularly, more just with `circuit_to_instruction` with registerless bits:
```python
from qiskit.circuit import QuantumCircuit, Qubit, Clbit

qc = QuantumCircuit([Qubit(), Clbit()])
qc.h(0).c_if(qc.clbits[0], 0)
qc.to_instruction()
```
will produce the same error.

Resolving patch:
diff --git a/qiskit/converters/circuit_to_instruction.py b/qiskit/converters/circuit_to_instruction.py
--- a/qiskit/converters/circuit_to_instruction.py
+++ b/qiskit/converters/circuit_to_instruction.py
@@ -113,14 +113,7 @@ def circuit_to_instruction(circuit, parameter_map=None, equivalence_library=None
         if condition:
             reg, val = condition
             if isinstance(reg, Clbit):
-                idx = 0
-                for creg in circuit.cregs:
-                    if reg not in creg:
-                        idx += creg.size
-                    else:
-                        cond_reg = creg
-                        break
-                rule[0].condition = (c[idx + list(cond_reg).index(reg)], val)
+                rule[0].condition = (clbit_map[reg], val)
             elif reg.size == c.size:
                 rule[0].condition = (c, val)
             else:


Test patch:
diff --git a/test/python/converters/test_circuit_to_instruction.py b/test/python/converters/test_circuit_to_instruction.py
--- a/test/python/converters/test_circuit_to_instruction.py
+++ b/test/python/converters/test_circuit_to_instruction.py
@@ -16,7 +16,7 @@
 
 from qiskit.converters import circuit_to_instruction
 from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
-from qiskit.circuit import Qubit, Clbit
+from qiskit.circuit import Qubit, Clbit, Instruction
 from qiskit.circuit import Parameter
 from qiskit.test import QiskitTestCase
 from qiskit.exceptions import QiskitError
@@ -186,6 +186,23 @@ def test_parameter_map(self):
         self.assertEqual(inst.definition[2][0].params, [gamma, phi, 0])
         self.assertEqual(str(inst.definition[3][0].params[0]), "gamma + phi")
 
+    def test_registerless_classical_bits(self):
+        """Test that conditions on registerless classical bits can be handled during the conversion.
+
+        Regression test of gh-7394."""
+        expected = QuantumCircuit([Qubit(), Clbit()])
+        expected.h(0).c_if(expected.clbits[0], 0)
+        test = circuit_to_instruction(expected)
+
+        self.assertIsInstance(test, Instruction)
+        self.assertIsInstance(test.definition, QuantumCircuit)
+
+        self.assertEqual(len(test.definition.data), 1)
+        test_instruction, _, _ = test.definition.data[0]
+        expected_instruction, _, _ = expected.data[0]
+        self.assertIs(type(test_instruction), type(expected_instruction))
+        self.assertEqual(test_instruction.condition, (test.definition.clbits[0], 0))
+
 
 if __name__ == "__main__":
     unittest.main(verbosity=2)