-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check the existence of AncillaQubits before adding #7450
Conversation
Also appears to be blocked by #7457 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but maybe @kdk could have a look too 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking at this, it looks sensible to me. I left one comment about an extra test that would be nice (it'll already pass), and please could you add a small "bug fix" release note?
def test_add_existing_bits(self): | ||
"""Test add registers whose bits have already been added.""" | ||
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could we add a test that calling add_register
with two different AncillaRegister
s containing the same bits, without calling add_bits
at all, also adds the correct number of bits? With this PR it will succeed, because AncillaRegister
derives from QuantumRegister
, but it'd be good to ensure that it's always the case - we can't rely on add_bits
having been called by the user first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. It has been added with the name test_overlapped_add_register_and_add_register
Pull Request Test Coverage Report for Build 1731098913
💛 - Coveralls |
@jakelishman Sure. The release note had been added. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks for the fix!
* 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)
* 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>
Summary
The bits in an
AncillaRegister
are appended toQuantumCircuit._ancilla
without checking:https://github.com/Qiskit/qiskit-terra/blob/9cc9225f249d3a895d9f3944175ea7b681d248c0/qiskit/circuit/quantumcircuit.py#L1354-L1355
This caused the issue described in #7445.
Details and comments
I fix this problem by checking the existence of
AncillaQubit
before the appending, which is similar to the operation regardingQuantumRegister
. A test case is added to demonstrate the change. The test failed without the modification.close #7445