Skip to content

Fix OptimizeCliffordT producing non-unitarily-equivalent output when basis excludes sx/sxdg - #16729

Merged
Shelly Garion (ShellyGarion) merged 7 commits into
Qiskit:mainfrom
Ronaldoengenhariadacomputacao:Ronaldoengenhariadacomputacao-patch-1
Aug 11, 2026
Merged

Shelly Garion (ShellyGarion) merged 7 commits into
Qiskit:mainfrom
Ronaldoengenhariadacomputacao:Ronaldoengenhariadacomputacao-patch-1

Conversation

@Ronaldoengenhariadacomputacao

Copy link
Copy Markdown
Contributor

Summary

OptimizeCliffordT's apply_ty closure re-expresses a Y-axis T-rotation without sx/sxdg using S, H, T(dg), H, Sdg -- but S and Sdg are swapped relative to the correct decomposition. As a result, the pass can emit a circuit that is not unitarily equivalent to its input (not even up to global phase) whenever basis_gates excludes sx/sxdg -- a normal, common Clifford+T target basis, not an edge case. No exception or warning is raised.

Verified two ways, not just with a custom offdiag computation: Qiskit's own Operator.equiv() (handles global phase correctly), and process_fidelity() (1.0 expected, 0.067-0.26 observed on affected cases).

Root cause

In the non-SX branch of apply_ty (see diff), S and Sdg are swapped at the two ends of the sequence. Verified numerically: SX*T*SXdg matches Sdg*H*T*H*S (offdiag 5.5e-17, correct) but not S*H*T*H*Sdg (offdiag 0.7071, wrong) -- and the same holds for the flip=true (Tdg) case.

Affected versions

Introduced in #15625 (merged 2026-03-19), first released in 2.4.0rc1. Confirmed affected: 2.4.0, 2.4.1, 2.4.2, 2.5.0, 2.5.0rc1, 2.5.1, and main prior to this fix. Not affected: 2.3.1 and earlier (the basis-aware Clifford+T pipeline didn't exist yet).

Fix verified end-to-end

Built from source (not just the algebraic argument) and re-ran every known reproduction at every optimization_level (0-3): all now come out Operator.equiv() == True (previously False at levels 1, 2, and/or 3 depending on the circuit).

Test plan

Added test_clifford_t_transpile_is_unitarily_equivalent to test_clifford_t_passmanager.py, parametrized over all four optimization levels, asserting Operator(qc).equiv(Operator(transpiled)). None of the existing tests in that file check unitary equivalence (only gate-set membership and T-count), which is consistent with how this went unnoticed across several releases.

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description: Claude (Anthropic), OpenCode, ChatGPT (OpenAI), Gemini (Google)
  • I used the following tool to help generate or modify code: Claude (Anthropic), OpenCode, ChatGPT (OpenAI), Gemini (Google)

Added regression test for OptimizeCliffordT to ensure unitary equivalence of transpiled circuits across optimization levels.
Fixed a bug in OptimizeCliffordT that affected circuit equivalence for specific basis gates. The issue was due to a transcription error in the internal re-expression of a T-rotation.
@CLAassistant

CLAassistant commented Aug 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ShellyGarion Shelly Garion (ShellyGarion) added mod: transpiler Issues and PRs related to Transpiler theme: fault tolerance related to fault tolerance compilation Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. labels Aug 10, 2026
@ShellyGarion Shelly Garion (ShellyGarion) added this to the 2.5.2 milestone Aug 11, 2026

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.

LGTM, thanks for finding and fixing this bug!

@ShellyGarion Shelly Garion (ShellyGarion) added the stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. label Aug 11, 2026
@ShellyGarion
Shelly Garion (ShellyGarion) added this pull request to the merge queue Aug 11, 2026
Merged via the queue into Qiskit:main with commit 9499a6a Aug 11, 2026
31 checks passed
Mohamedma96 pushed a commit to Mohamedma96/qiskit that referenced this pull request Aug 13, 2026
…basis excludes sx/sxdg (Qiskit#16729)

* Replace S with Sdg and Sdg with S in optimization

* Add regression test for OptimizeCliffordT

Added regression test for OptimizeCliffordT to ensure unitary equivalence of transpiled circuits across optimization levels.

* Fix bug in OptimizeCliffordT for specific basis gates

Fixed a bug in OptimizeCliffordT that affected circuit equivalence for specific basis gates. The issue was due to a transcription error in the internal re-expression of a T-rotation.

* Fix indentation for test_clifford_t_transpile

espaçamento 196

* Shorten reno

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
xmirya pushed a commit to qollabxyz/qiskit that referenced this pull request Aug 13, 2026
…basis excludes sx/sxdg (Qiskit#16729) (Qiskit#16735)

* Replace S with Sdg and Sdg with S in optimization

* Add regression test for OptimizeCliffordT

Added regression test for OptimizeCliffordT to ensure unitary equivalence of transpiled circuits across optimization levels.

* Fix bug in OptimizeCliffordT for specific basis gates

Fixed a bug in OptimizeCliffordT that affected circuit equivalence for specific basis gates. The issue was due to a transcription error in the internal re-expression of a T-rotation.

* Fix indentation for test_clifford_t_transpile

espaçamento 196

* Shorten reno

---------


(cherry picked from commit 9499a6a)

Co-authored-by: Ronaldo Rodrigues <126127042+Ronaldoengenhariadacomputacao@users.noreply.github.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
Ronaldo Rodrigues (Ronaldoengenhariadacomputacao) added a commit to Ronaldoengenhariadacomputacao/autoq-qec that referenced this pull request Aug 19, 2026
A real bug in Qiskit's own OptimizeCliffordT pass (swapped S/Sdg gates
for Clifford+T bases excluding SX/SXdg, Qiskit 2.4.0-2.5.1) was fixed
upstream in Qiskit 2.5.2 (Qiskit/qiskit#16729). Since _count_t_gates() uses
exactly that basis internally, this adds 23 regression tests and confirms,
by running the full suite against both Qiskit 2.5.1 and 2.5.2 in isolated
environments, that T-count/n_physical_gates/depth_physical are bit-for-bit
identical between them -- the bug never affected any T-count autoq-qec has
computed or published. No code change was needed in autoq_qec/ itself.
VedantMahadik-qc pushed a commit to VedantMahadik-qc/qiskit that referenced this pull request Oct 21, 2026
…basis excludes sx/sxdg (Qiskit#16729)

* Replace S with Sdg and Sdg with S in optimization

* Add regression test for OptimizeCliffordT

Added regression test for OptimizeCliffordT to ensure unitary equivalence of transpiled circuits across optimization levels.

* Fix bug in OptimizeCliffordT for specific basis gates

Fixed a bug in OptimizeCliffordT that affected circuit equivalence for specific basis gates. The issue was due to a transcription error in the internal re-expression of a T-rotation.

* Fix indentation for test_clifford_t_transpile

espaçamento 196

* Shorten reno

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. mod: transpiler Issues and PRs related to Transpiler stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. theme: fault tolerance related to fault tolerance compilation

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants