Skip to content

remove_idle_qubits(in_place=False) decrements num_qubits on the original module instead of the copy #334

Description

@ryanhill1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions