Bug: unwanted addition of classical bits via `measure_all`
MattePalte opened this issue · comments
Where does the issue happen?
https://qiskit.org/textbook/ch-appendix/qiskit.html
What is the content issue?
Environment
- qiskit.version: 0.22.0
- Python version: 3.8.0
- Operating system: Ubuntu 18.04
What is happening?
In the notebook qiskit.ipynb the measure_all
api call creates a register even if the qc
already has a classical register of size 5
. This is due to the default behavior of measure_all
which always insert a new register if not explicitly mentioned otherwise.
Screenshots
How can we reproduce the issue?
Run the cell with the following code:
qc = QuantumCircuit(5,5)
qc.x(0)
for q in range(4):
qc.cx(0,q+1)
qc.measure_all()
qc.draw()
Output:
┌───┐ ░ ┌─┐
q_0: ┤ X ├──■────■────■────■───░─┤M├────────────
└───┘┌─┴─┐ │ │ │ ░ └╥┘┌─┐
q_1: ─────┤ X ├──┼────┼────┼───░──╫─┤M├─────────
└───┘┌─┴─┐ │ │ ░ ║ └╥┘┌─┐
q_2: ──────────┤ X ├──┼────┼───░──╫──╫─┤M├──────
└───┘┌─┴─┐ │ ░ ║ ║ └╥┘┌─┐
q_3: ───────────────┤ X ├──┼───░──╫──╫──╫─┤M├───
└───┘┌─┴─┐ ░ ║ ║ ║ └╥┘┌─┐
q_4: ────────────────────┤ X ├─░──╫──╫──╫──╫─┤M├
└───┘ ░ ║ ║ ║ ║ └╥┘
c: 5/═════════════════════════════╬══╬══╬══╬══╬═
║ ║ ║ ║ ║
meas: 5/═════════════════════════════╩══╩══╩══╩══╩═
0 1 2 3 4
Where two circuits are present: c
and meas
. The first one is the one we created, the second one is the one created by measure_all
.
What should happen?
I would expect the existing classical register to be used instead to avoid waste of space.
Additional information
Any suggestions?
Following the documentation of the measure_all
method, it is possible to measure the qubits directly, without adding an extra register. The code should be changed to:
qc.measure_all(add_bits=False)
I can handle this, should be a quick fix
@MattePalte Can you link the corresponding page on the new textbook, or move this issue over to the old textbook repository https://github.com/qiskit-community/qiskit-textbook
If you mean the link to the actual file, you can find it by clicking the filename qiskit.ipynb in my issue or here.
If I misunderstood, please let me know which specific link you need.
Thanks in advance
just for clarification
in regards to the legacy textbook:
the notebook for this page: https://qiskit.org/textbook/ch-appendix/qiskit.html
is located here: https://github.com/qiskit-community/qiskit-textbook/blob/main/content/ch-appendix/qiskit.ipynb
for the (newer) textbook beta:
this notebook: https://github.com/Qiskit/platypus/blob/main/notebooks/ch-appendix/qiskit.ipynb
is for this page: https://learn.qiskit.org/course/ch-appendix/basic-qiskit-syntax
issues for the legacy textbook should be added to the repo: https://github.com/qiskit-community/qiskit-textbook
and issue for the newer textbook should be added in this repo
This repository is no longer maintained. It has been superseded by IBM Quantum Learning.