Skip to content

fix: emit distinct x gates with fresh operands in negctrl expansion - #357

Open
ryanhill1 wants to merge 3 commits into
mainfrom
fix-negctrl-aliasing
Open

fix: emit distinct x gates with fresh operands in negctrl expansion#357
ryanhill1 wants to merge 3 commits into
mainfrom
fix-negctrl-aliasing

Conversation

@ryanhill1

@ryanhill1 ryanhill1 commented Aug 7, 2026

Copy link
Copy Markdown
Member

Fixes #350

The negctrl @ expansion placed the same QuantumGate object at both the leading and trailing position of the emitted statement list. In-place transformations therefore reached the same operand nodes twice: reverse_qubit_order() raised KeyError on any unrolled negctrl gate, and remove_idle_qubits() survived only via the visited_node_ids guard.

The two x gates are now built as distinct statements whose operands go through fresh_qubits(), matching the fix #335 made for decomposition-emitted statements.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed negative-control gate expansion to generate correct leading and trailing x operations.
    • Resolved failures when reversing qubit order after unrolling negative-control gates.
  • Tests

    • Added coverage for single- and multi-control negative-control operations.
    • Added regression testing for expected gate sequences and qubit remapping.
  • Documentation

    • Added a changelog entry describing the fix.

The negctrl modifier expansion placed the same QuantumGate object at both
the leading and trailing position of the emitted statement list, so
in-place transformations reached the same operand nodes twice and
reverse_qubit_order() raised KeyError on any unrolled negctrl gate.

Fixes #350
@ryanhill1
ryanhill1 requested a review from TheGupta2012 as a code owner August 7, 2026 13:04
@argus-eye

argus-eye Bot commented Aug 7, 2026

Copy link
Copy Markdown

Argus review

Auto-review is off for this repo. Tick the box below to run a review on this PR.

  • Trigger Argus review

Estimated cost

  • Files changed: 3
  • Diff lines (±): 43
  • Historical avg: ~318.9k tokens · ~$1.35 · across last 6 review(s)

Tip: you can also comment @argus-eye review at any time.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4dacf0b8-0ac6-4dce-b586-ab29bf256749

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The negative-control expansion now creates separate leading and trailing X-gate statements with fresh operand nodes. Tests cover single- and multi-qubit controls and verify successful qubit-order reversal.

Changes

Negative-control expansion

Layer / File(s) Summary
Fresh negative-control expansion operands
src/pyqasm/visitor.py
Negative-control conversion uses fresh_qubits and creates distinct operand nodes for the generated X gates.
Transformation regression coverage
tests/qasm3/test_transformations.py, CHANGELOG.md
Tests cover negative-control unrolling and qubit-order reversal. The changelog records the fix.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: thegupta2012

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the primary fix: emitting distinct X gates with fresh operands during negctrl expansion.
Linked Issues check ✅ Passed The PR satisfies #350 by creating distinct statements and fresh operands, with regression coverage for unrolling and qubit-order reversal; #335 is supporting context.
Out of Scope Changes check ✅ Passed The visitor fix, changelog entry, and regression tests directly support the negctrl aliasing fix and linked issue objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-negctrl-aliasing

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.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ryanhill1

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pyqasm/visitor.py`:
- Around line 1614-1618: Update the nested _neg_x_gates function with a concise
docstring describing that it creates X gates for the negative-control qubits and
returns the resulting list of qasm3_ast.QuantumGate objects; note that it takes
no parameters.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 30436573-a75a-409f-8e45-04afa54dfe69

📥 Commits

Reviewing files that changed from the base of the PR and between 7e05f4c and 4e60a7c.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/pyqasm/visitor.py
  • tests/qasm3/test_transformations.py

Comment thread src/pyqasm/visitor.py
Comment on lines +1614 to +1618
def _neg_x_gates() -> list[qasm3_ast.QuantumGate]:
return [
qasm3_ast.QuantumGate([], qasm3_ast.Identifier("x"), [], fresh_qubits(ctrl))
for ctrl in negctrls
]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add a docstring to _neg_x_gates.

The new nested function has no docstring. Add a concise description of its purpose and return value.

As per coding guidelines, every Python function must have a docstring explaining its purpose, parameters, and return values.

Proposed fix
         def _neg_x_gates() -> list[qasm3_ast.QuantumGate]:
+            """Create X gates for the negative controls.
+
+            Returns:
+                list[qasm3_ast.QuantumGate]: The generated X gates.
+            """
             return [
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def _neg_x_gates() -> list[qasm3_ast.QuantumGate]:
return [
qasm3_ast.QuantumGate([], qasm3_ast.Identifier("x"), [], fresh_qubits(ctrl))
for ctrl in negctrls
]
def _neg_x_gates() -> list[qasm3_ast.QuantumGate]:
"""Create X gates for the negative controls.
Returns:
list[qasm3_ast.QuantumGate]: The generated X gates.
"""
return [
qasm3_ast.QuantumGate([], qasm3_ast.Identifier("x"), [], fresh_qubits(ctrl))
for ctrl in negctrls
]
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pyqasm/visitor.py` around lines 1614 - 1618, Update the nested
_neg_x_gates function with a concise docstring describing that it creates X
gates for the negative-control qubits and returns the resulting list of
qasm3_ast.QuantumGate objects; note that it takes no parameters.

Source: Coding guidelines

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.

negctrl expansion emits the same QuantumGate object twice, aliasing operand nodes

2 participants