Repository: Qiskit/qiskit

Issue:
circuit.extend produces empty circuit.qubits
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: master@9e78907d2
- **Python version**: 3.7.7
- **Operating system**: macOS

### What is the current behavior?

The `QuantumCircuit.extend` operation extends an existing circuit with another circuit. If the second circuit contains registers that are not in the first one, the register is added to the first. This operation can therefore add quantum registers. 
Before #4766 this was not a problem because the `QuantumCircuit.qubits` attribute was generated from the registers, but now that the qubits are cached, calling extend causes a mismatch between the registers and qubits, because the qubits attribute is not properly updated.

### Steps to reproduce the problem

```python
from qiskit import QuantumCircuit

empty = QuantumCircuit()
other = QuantumCircuit(1)
other.x(0)

empty.extend(other)
print(empty)
print(empty.qubits)
```

Before #4766 (1db0340eb):
```
     ┌───┐
q_0: ┤ X ├
     └───┘
[Qubit(QuantumRegister(1, 'q'), 0)]
```
After:
```
     ┌───┐
q_0: ┤ X ├
     └───┘
[]
```

### Suggested solutions

Update `self.qubits` upon calling `self.extend`.




Discussion:

Resolving patch:
diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py
--- a/qiskit/circuit/quantumcircuit.py
+++ b/qiskit/circuit/quantumcircuit.py
@@ -539,9 +539,11 @@ def extend(self, rhs):
         for element in rhs.qregs:
             if element not in self.qregs:
                 self.qregs.append(element)
+                self._qubits += element[:]
         for element in rhs.cregs:
             if element not in self.cregs:
                 self.cregs.append(element)
+                self._clbits += element[:]
 
         # Copy the circuit data if rhs and self are the same, otherwise the data of rhs is
         # appended to both self and rhs resulting in an infinite loop


Test patch:
diff --git a/test/python/circuit/test_circuit_operations.py b/test/python/circuit/test_circuit_operations.py
--- a/test/python/circuit/test_circuit_operations.py
+++ b/test/python/circuit/test_circuit_operations.py
@@ -135,7 +135,7 @@ def test_extend_circuit_different_registers(self):
         self.assertEqual(counts, target)
 
     def test_extend_circuit_fail(self):
-        """Test extending a circuits fails if registers incompatible.
+        """Test extending a circuit fails if registers incompatible.
 
         If two circuits have same name register of different size or type
         it should raise a CircuitError.
@@ -150,6 +150,15 @@ def test_extend_circuit_fail(self):
         self.assertRaises(CircuitError, qc1.__iadd__, qc2)
         self.assertRaises(CircuitError, qc1.__iadd__, qcr3)
 
+    def test_extend_circuit_adds_qubits(self):
+        """Test extending a circuits with differing registers adds the qubits."""
+        qr = QuantumRegister(1, "q")
+        qc = QuantumCircuit(qr)
+        empty = QuantumCircuit()
+        empty.extend(qc)
+
+        self.assertListEqual(empty.qubits, qr[:])
+
     def test_measure_args_type_cohesion(self):
         """Test for proper args types for measure function.
         """