Summary
remove_idle_qubits(in_place=False) updates the qubit count on the wrong module: the original module's num_qubits is decremented while the returned copy keeps the stale pre-removal count. The copy's AST is remapped correctly (declaration size, operand indices, dumps() output are all right) — only the counter is wrong, and it's wrong on both objects.
Reproduction
from pyqasm import loads
m = loads('''OPENQASM 3;
include "stdgates.inc";
qubit[3] q;
h q[1];
cx q[1], q[2];
''')
m.unroll()
m2 = m.remove_idle_qubits(in_place=False)
print(m2.num_qubits) # 3 (should be 2)
print(m.num_qubits) # 2 (should still be 3 — original was not supposed to change)
Root cause
src/pyqasm/modules/base.py:494 (inside remove_idle_qubits):
self._num_qubits -= len(idle_indices)
Every other mutation in the method goes through qasm_module (which is self.copy() when in_place=False); this one line uses self. Line 474 (size = self._qubit_registers[reg_name]) also reads from self — harmless today since the copy's registers are identical at that point, but it should be qasm_module for consistency.
Found during adversarial review of #332; reproduces on main and is unrelated to that PR's change.
🤖 Filed with Claude Code
Summary
remove_idle_qubits(in_place=False)updates the qubit count on the wrong module: the original module'snum_qubitsis decremented while the returned copy keeps the stale pre-removal count. The copy's AST is remapped correctly (declaration size, operand indices,dumps()output are all right) — only the counter is wrong, and it's wrong on both objects.Reproduction
Root cause
src/pyqasm/modules/base.py:494(insideremove_idle_qubits):Every other mutation in the method goes through
qasm_module(which isself.copy()whenin_place=False); this one line usesself. Line 474 (size = self._qubit_registers[reg_name]) also reads fromself— harmless today since the copy's registers are identical at that point, but it should beqasm_modulefor consistency.Found during adversarial review of #332; reproduces on
mainand is unrelated to that PR's change.🤖 Filed with Claude Code