Skip to content

Commit

Permalink
Check the existence of AncillaQubits before adding (#7450) (#7555)
Browse files Browse the repository at this point in the history
* Check the existance of AncillaQubits before adding

* Add a test case that calls add_register twice

* Add a release note

* Reword release note

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Kevin Krsulich <kevin.krsulich@ibm.com>
(cherry picked from commit 84c3985)

Co-authored-by: Jintao YU <yjt98765@gmail.com>
  • Loading branch information
mergify[bot] and yjt98765 authored Jan 22, 2022
1 parent 5adc33e commit 673e54d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,9 @@ def add_register(self, *regs: Union[Register, int, Sequence[Bit]]) -> None:
raise CircuitError('register name "%s" already exists' % register.name)

if isinstance(register, AncillaRegister):
self._ancillas.extend(register)
for bit in register:
if bit not in self._qubit_indices:
self._ancillas.append(bit)

if isinstance(register, QuantumRegister):
self.qregs.append(register)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
:meth:`.QuantumCircuit.add_register` will no longer cause duplicate
:class:`.AncillaQubit` references in a circuit when given an
:class:`.AncillaRegister` whose bits are already present.
36 changes: 36 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from qiskit.circuit.classicalregister import Clbit
from qiskit.circuit.exceptions import CircuitError
from qiskit.circuit.quantumcircuit import BitLocations
from qiskit.circuit.quantumregister import AncillaQubit, AncillaRegister, Qubit
from qiskit.test import QiskitTestCase
from qiskit.circuit.library.standard_gates import SGate
from qiskit.quantum_info import Operator
Expand Down Expand Up @@ -948,6 +949,41 @@ def test_compare_a_circuit_with_none(self):

self.assertFalse(qc1 == qc2)

def test_overlapped_add_bits_and_add_register(self):
"""Test add registers whose bits have already been added by add_bits."""
qc = QuantumCircuit()
for bit_type, reg_type in (
[Qubit, QuantumRegister],
[Clbit, ClassicalRegister],
[AncillaQubit, AncillaRegister],
):
bits = [bit_type() for _ in range(10)]
reg = reg_type(bits=bits)
qc.add_bits(bits)
qc.add_register(reg)

self.assertEqual(qc.num_qubits, 20)
self.assertEqual(qc.num_clbits, 10)
self.assertEqual(qc.num_ancillas, 10)

def test_overlapped_add_register_and_add_register(self):
"""Test add registers whose bits have already been added by add_register."""
qc = QuantumCircuit()
for bit_type, reg_type in (
[Qubit, QuantumRegister],
[Clbit, ClassicalRegister],
[AncillaQubit, AncillaRegister],
):
bits = [bit_type() for _ in range(10)]
reg1 = reg_type(bits=bits)
reg2 = reg_type(bits=bits)
qc.add_register(reg1)
qc.add_register(reg2)

self.assertEqual(qc.num_qubits, 20)
self.assertEqual(qc.num_clbits, 10)
self.assertEqual(qc.num_ancillas, 10)

def test_deprecated_measure_function(self):
"""Test that the deprecated version of the loose 'measure' function works correctly."""
from qiskit.circuit.measure import measure
Expand Down

0 comments on commit 673e54d

Please sign in to comment.