Skip to content

Decompose ccx, id and p when rebasing to ROTATIONAL_CX / CLIFFORD_T - #468

Merged
TheGupta2012 merged 1 commit into
qBraid:mainfrom
tonycoder-hub:fix-ccx-rebase
Oct 28, 2026
Merged

TheGupta2012 merged 1 commit into
qBraid:mainfrom
tonycoder-hub:fix-ccx-rebase

Conversation

@tonycoder-hub

Copy link
Copy Markdown
Contributor

Summary of changes

rebase(BasisSet.ROTATIONAL_CX) and rebase(BasisSet.CLIFFORD_T) failed on a plain ccx, because unroll keeps it as a single gate and neither basis had a rule for it. The same happened for id, and for the qelib1 c3x under ROTATIONAL_CX, which unrolls into p(...) gates that weren't handled.

Fixes #467

Changes:

  • Add the standard 6-CNOT Toffoli decomposition to both basis sets (h/t/tdg for Clifford+T, the ry/rx/rz equivalents for rotational-CX). Rules can now address a third operand through a new AppliedQubit.QUBIT3, and _get_qubits_for_gate picks qubits by the enum value instead of the two-qubit if/else. Existing two-qubit rules behave the same.
  • id is dropped in both basis sets.
  • p(theta) is rz(theta) up to a global phase, so ROTATIONAL_CX emits rz with the same argument. For CLIFFORD_T it goes through the existing exact rotation path, so multiples of pi/4 work and anything else still raises RebaseError (so c3x -> Clifford+T is still rejected, as it should be with its pi/8 phases).
  • CHANGELOG entry under Fixed.

Test plan

New tests in tests/qasm3/test_rebase.py, checked numerically with the existing unitary helpers (equal up to global phase):

  • ccx in both basis sets, in default order and with the target on a middle qubit, against mcx_unitary
  • id in both basis sets leaves no gates
  • qelib1 c3x rebased to ROTATIONAL_CX against mcx_unitary(4)

All 7 new cases fail on main with RebaseError and pass with this change.

  • pytest tests: 1022 passed, 3 skipped
  • black --check, isort --check-only: clean
  • pylint src/pyqasm tests/qasm3/test_rebase.py: 10.00/10
  • mypy src/pyqasm: same single pre-existing error as on main, nothing new

rebase() raised RebaseError for ccx (kept as a single gate by unroll),
for id, and for the p gates that the qelib1 c3x expands into.

- add a 6-CNOT Toffoli rule for both basis sets, addressing the third
  operand through a new AppliedQubit.QUBIT3
- drop id in both basis sets
- map p to rz (equal up to global phase); for Clifford+T reuse the exact
  rotation path, which still rejects non pi/4 multiples
- pick rule qubits by AppliedQubit value instead of a two-qubit if/else

Signed-off-by: Tony Coder <407243179@qq.com>
@argus-eye

argus-eye Bot commented Oct 28, 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: 4
  • Diff lines (±): 137
  • Historical avg: ~336.9k tokens · ~$1.17 · across last 18 review(s)

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

@coderabbitai

coderabbitai Bot commented Oct 28, 2026

Copy link
Copy Markdown

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
  • Source mode: Auto
  • Repository: qBraid/pyqasm
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: a742796
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5ce4df6c-39c6-4ed4-bd18-ce0d5d73e2d9

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
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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!

@TheGupta2012 TheGupta2012 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @tonycoder-hub for working on this, lgtm!

@TheGupta2012
TheGupta2012 merged commit dc1b320 into qBraid:main Oct 28, 2026
14 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] Missing ccx decomposition in rebase for ROTATIONAL_CX and CLIFFORD_T

3 participants