`measure_all` assigns surplus classical resources
Pre-Report Checklist
- [x] I am running the latest versions of pyQuil and the Forest SDK
- [x] I checked to make sure that this bug has not already been reported
Issue Description
program.measure_all() defines classical bits more than required based on the highest qubit index value in the circuit. Instead it should use max(qubitindex) - min(qubitindex) + 1 (Suggestion).
The following circuit has only one gate on one qubit but is assigned 6555 classical bits.
from pyquil import get_qc, Program
p = Program('X 65555')
p.measure_all()
print(p)
qc = get_qc("9q-square-qvm")
result = qc.run(p.wrap_in_numshots_loop(1000))
result.readout_data.get("ro")
print(result)
The bug is also a potential security issue where it could crash the quantum device / simulator. For example the same circuit with any arbitrary integer for the qubit index.
I was able to crash the QVM by running the following circuit in parallel which resulted in following state: from pyquil import get_qc, Program
p = Program('X 65537')
p.measure_all()
qc = get_qc("9q-square-qvm")
result = qc.run(p.wrap_in_numshots_loop(1000))
result.readout_data.get("ro")
print(result)
<135>1 2024-10-11T02:30:39Z lux.local qvm 12260 - - [127.0.0.1 Session:26] Got "multishot" request from API key/User ID: NIL / NIL
<135>1 2024-10-11T02:30:39Z lux.local qvm 12260 - - [127.0.0.1 Session:26] Mapping qubits: 65537 -> 0
<135>1 2024-10-11T02:30:39Z lux.local qvm 12260 - - [127.0.0.1 Session:26] Making qvm of 1 qubit
<135>1 2024-10-11T02:30:39Z lux.local qvm 12260 - - [127.0.0.1 Session:26] Running experiment with 1000 trials on PURE-STATE-QVM
<135>1 2024-10-11T02:30:42Z lux.local qvm 12260 - - [127.0.0.1 Session:27] Got "version" request from API key/User ID: NIL / NIL
<134>1 2024-10-11T02:30:42Z lux.local qvm 12260 - - 127.0.0.1 - [2024-10-10 22:30:42] "POST / HTTP/1.1" 200 16 "-" "-"
<135>1 2024-10-11T02:30:42Z lux.local qvm 12260 - - [127.0.0.1 Session:28] Got "multishot" request from API key/User ID: NIL / NIL
<135>1 2024-10-11T02:30:42Z lux.local qvm 12260 - - [127.0.0.1 Session:28] Mapping qubits: 65537 -> 0
<135>1 2024-10-11T02:30:42Z lux.local qvm 12260 - - [127.0.0.1 Session:28] Making qvm of 1 qubit
<135>1 2024-10-11T02:30:42Z lux.local qvm 12260 - - [127.0.0.1 Session:28] Running experiment with 1000 trials on PURE-STATE-QVM
assertion failed: state update should occur from waiters' queue
(ThreadContextRegisterState.cpp:992 host_fpr_state_from_guest_state)
With qvm stuck in unresponsive state.
Environment Context
Operating System:
Python Version (python -V): 3.11
Quilc Version (quilc --version): 1.23.0
QVM Version (qvm --version): 1.17.1
Latest version of pyquil.