Skip to content

fix(unroll): invert any standard gate exactly under inv @ - #492

Open
TheGupta2012 wants to merge 4 commits into
mainfrom
bugfix-473-476-inverse
Open

TheGupta2012 wants to merge 4 commits into
mainfrom
bugfix-473-476-inverse

Conversation

@TheGupta2012

Copy link
Copy Markdown
Member

Why

inv @ gave a wrong circuit, with no error, for 13 standard gates such as pswap, iswap and cp00. It raised on 19 more, such as sx, p, u and rxx. The inverse path kept hand-written tables per gate: two-qubit and three-qubit gates mapped back to their forward form, and gates outside the tables raised.

What changed

  • inverse_gate_list inverts any decomposition: it reverses the gate list and inverts each primitive (negated angles for rx, ry, rz, p and gphase, daggers for s, t and sx). It raises ValidationError on a primitive it cannot invert.
  • inv @ and pow(-n) @ now invert the forward decomposition. The hand-written inverse tables and InversionOp are gone.
  • The inv @ ctrl @ path from fix: support ctrl @ on any single-qubit standard gate #459 is unchanged.

Scope

Blast Radius

Unrolled text changes only for the gates that were wrong or raised, plus ecr, cswap, c3x, c4x and rccx. Those five now emit the reversed decomposition, which is still exact, but their depth next to other gates can differ. qbraid and qbraid-qir import none of the removed names, and their inv @ test programs unroll byte-identical.

Verification

  • A sweep compares each gate's unrolled unitary with the adjoint, up to global phase. On main, 48 of 80 gates were exact. On this branch, all 80 are exact for inv @, inv @ inv @ and pow(-1) @. inv @ ctrl @ output is byte-identical to main for all 80.
  • New tests: 67 fail on main. Full suite: 1386 passed with openqasm3 1.0.2, and 1387 passed with 1.0.1. tox -e format-check passes.
  • An independent review found nothing blocking. Its findings are applied: changelog accuracy, a direct test of inverse_gate_list, and float angles in inv @ u2.

Closes #473
Closes #476

TheGupta2012 and others added 4 commits November 19, 2026 12:42
Parametrize over every gate in the unroll maps and compare the unitary of
`inv @ g` with the adjoint of `g`, plus `inv @ inv @ g`, `pow(-1) @ g`,
the literal repros from issues #473 and #476, and `inv @` on a custom gate
whose body uses decomposed gates. The test simulator learns `swap`, `sxdg`
and `nop` so `xy` and `xx_plus_yy` can be checked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`map_qasm_inv_op_to_callable` returned the forward decomposition for every
two- and three-qubit gate, so `inv @ pswap`, `iswap`, `ms`, `ryy`, `xy`,
`xx_plus_yy` and the `cp00`/`cp01`/`cp10` family silently unrolled to the
gate itself (#473). It raised on every gate outside its small inverse
tables, including `sx`, `sxdg`, `p`, `u`, `prx`, `gpi` and `gpi2` (#476),
and `inv @ rxx` / `rzz` crashed on the `gphase` their decompositions emit.

`inv @ g` now takes g's own decomposition, reverses it and inverts each
emitted gate: self-inverse gates stay, `s`/`t`/`sx` swap with their dagger,
and `rx`/`ry`/`rz`/`p`/`gphase` negate their angle. The U-form path from
#459 still handles `inv @` on controlled gates, so `inv @ ctrl @ g`
unrolls exactly as before for every gate. Self-inverse composites such as
`c3x`, `cswap` and `ecr` now emit their decomposition reversed; the unitary
is unchanged.

Fixes #473, fixes #476.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…laces

`u3_inv_gate`, `u2_inv_gate`, `U_INV_ROTATION_MAP`, `MULTI_CONTROLLED_X_INV_MAP`
and `InversionOp` are dead now that `inv @` inverts the forward decomposition.
`_rc3x_dg_gate` was that same reversal written out by hand, so `c4x` now
calls `inverse_gate_list(rc3x_gate(...))` instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@argus-eye

argus-eye Bot commented Nov 19, 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: 6
  • Diff lines (±): 364
  • Historical avg: ~337.1k tokens · ~$1.17 · across last 18 review(s)

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

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.05882% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/pyqasm/maps/gates.py 96.42% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

inv @ raises on sx, sxdg, p, u, prx, gpi and gpi2 unroll() ignores inv @ on decomposed gates (cphaseshift, cv, pswap)

2 participants