Repository: Qiskit/qiskit

Issue:
QASM conversion fails when a gate is conditioned on a individual bit of a classical register
### Environment

- **Qiskit Terra version**: 0.21.2
- **Python version**: 3.7.7
- **Operating system**: Mac


### What is happening?

A quantum circuit with a gate conditioned on a single bit value of a classical register cannot be converted to a QASM string.


### How can we reproduce the issue?

```python
q = QuantumRegister(2, 'q')
c = ClassicalRegister(2, 'c')
qc = QuantumCircuit(q, c)
qc.h(0)
qc.cx(0, 1)
qc.measure(0, 0)
qc.x(1).c_if(c[0], 1)
print(qc)
qc.qasm()
```

/opt/miniconda3/lib/python3.7/site-packages/qiskit/circuit/instruction.py in _qasmif(self, string)
    438         if self.condition is None:
    439             return string
--> 440         return "if(%s==%d) " % (self.condition[0].name, self.condition[1]) + string
    441 
    442     def qasm(self):

AttributeError: 'Clbit' object has no attribute 'name'


### What should happen?

Expect to get a QASM string 

### Any suggestions?

_No response_


Discussion:
The error message could be better here, but `QuantumCircuit.qasm` produces OpenQASM 2, and there's no way to represent single-bit conditions in OpenQASM 2.  This should work with the OpenQASM 3.0 exporter (in `qiskit.qasm3`), though OQ 3 support is still under active development, and isn't fully complete yet.

Resolving patch:
diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py
--- a/qiskit/circuit/instruction.py
+++ b/qiskit/circuit/instruction.py
@@ -40,6 +40,7 @@
 from qiskit.circuit.exceptions import CircuitError
 from qiskit.circuit.quantumregister import QuantumRegister
 from qiskit.circuit.classicalregister import ClassicalRegister, Clbit
+from qiskit.qasm.exceptions import QasmError
 from qiskit.qobj.qasm_qobj import QasmQobjInstruction
 from qiskit.circuit.parameter import ParameterExpression
 from qiskit.circuit.operation import Operation
@@ -438,6 +439,10 @@ def _qasmif(self, string):
         """Print an if statement if needed."""
         if self.condition is None:
             return string
+        if not isinstance(self.condition[0], ClassicalRegister):
+            raise QasmError(
+                "OpenQASM 2 can only condition on registers, but got '{self.condition[0]}'"
+            )
         return "if(%s==%d) " % (self.condition[0].name, self.condition[1]) + string
 
     def qasm(self):


Test patch:
diff --git a/test/python/circuit/test_circuit_qasm.py b/test/python/circuit/test_circuit_qasm.py
--- a/test/python/circuit/test_circuit_qasm.py
+++ b/test/python/circuit/test_circuit_qasm.py
@@ -450,3 +450,12 @@ def test_circuit_qasm_with_duplicate_invalid_identifiers(self):
         names = ["invalid??", "invalid[]"]
         for idx, instruction in enumerate(base._data):
             self.assertEqual(instruction.operation.name, names[idx])
+
+    def test_circuit_raises_on_single_bit_condition(self):
+        """OpenQASM 2 can't represent single-bit conditions, so test that a suitable error is
+        printed if this is attempted."""
+        qc = QuantumCircuit(1, 1)
+        qc.x(0).c_if(0, True)
+
+        with self.assertRaisesRegex(QasmError, "OpenQASM 2 can only condition on registers"):
+            qc.qasm()