Skip to content

Commit

Permalink
QuantumCircuit.extend updates cached bits (#5039)
Browse files Browse the repository at this point in the history
* extend updates the cached qubits

* add reno
  • Loading branch information
Cryoris authored Sep 8, 2020
1 parent 43b436b commit 353918b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Extending circuits with differing registers updated the ``qregs`` and
``cregs`` properties accordingly, but not the ``qubits`` and ``clbits``
lists. As these are no longer generated from the registers but are cached
lists, this lead to a discrepancy of registers and bits. This has been
fixed and the ``extend`` method explicitly updates the cached bit lists.
11 changes: 10 additions & 1 deletion test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
"""
Expand Down

0 comments on commit 353918b

Please sign in to comment.