Skip to content

Fix double-unroll ValidationError when consolidate_qubits=True#297

Merged
TheGupta2012 merged 4 commits intomainfrom
fix/double-unroll-consolidate-qubits
Mar 12, 2026
Merged

Fix double-unroll ValidationError when consolidate_qubits=True#297
TheGupta2012 merged 4 commits intomainfrom
fix/double-unroll-consolidate-qubits

Conversation

@TheGupta2012
Copy link
Copy Markdown
Member

Bug Fix — Fix double-unroll crash when consolidate_qubits=True

Calling module.unroll(consolidate_qubits=True) twice on the same QasmModule raised a ValidationError because consolidate_qubit_registers() was mutating AST nodes in-place. Since self._statements holds a reference to the original program.statements, these mutations corrupted the source AST. On the second unroll, the visitor encountered barrier qubits named __PYQASM_QUBITS__[...] that did not exist in the fresh scope.

Closes #296.

Changes

  • Deep-copy: Added unrolled_stmts = deepcopy(unrolled_stmts) at the entry of consolidate_qubit_registers in src/pyqasm/transformer.py, so all in-place identifier renames operate on isolated copies and the original AST remains intact for re-processing.

Reproduction

from pyqasm import loads

qasm3_string = """OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
qreg q2[3];
barrier q2;
barrier q, q2;
"""
mod = loads(qasm3_string)
mod.unroll(consolidate_qubits=True)
mod.unroll(consolidate_qubits=True)  # Previously raised ValidationError

TheGupta2012 and others added 2 commits March 11, 2026 13:17
`consolidate_qubit_registers` mutates AST nodes in-place (renaming qubit
identifiers to `__PYQASM_QUBITS__`). Since `self._statements` holds a
reference to the original program statements, these mutations corrupt the
source AST. A subsequent `unroll(consolidate_qubits=True)` then encounters
barrier qubits named `__PYQASM_QUBITS__[...]` that do not exist in the
fresh scope, raising a `ValidationError`.

Deep-copy `unrolled_stmts` at the entry of `consolidate_qubit_registers`
so the transformer operates on isolated copies, leaving the original AST
intact for re-processing.

Closes #296

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 11, 2026

Warning

Rate limit exceeded

@TheGupta2012 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 24 minutes and 59 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 712b1d09-0d31-4917-87ef-8e3458f5071b

📥 Commits

Reviewing files that changed from the base of the PR and between 79901de and 8b132ea.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/pyqasm/transformer.py
  • tests/qasm3/test_device_qubits.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/double-unroll-consolidate-qubits

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

TheGupta2012 and others added 2 commits March 11, 2026 13:20
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Verifies that calling unroll(consolidate_qubits=True) twice on the same
module produces identical output without raising a ValidationError.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@TheGupta2012 TheGupta2012 merged commit e132c0d into main Mar 12, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Consecutive unrolls fail with consolidate_qubits=True

2 participants