Skip to content

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

Merged
Cryoris merged 1 commit into
stable/2.5from
mergify/bp/stable/2.5/pr-16729
Aug 11, 2026
Merged

Fix OptimizeCliffordT producing non-unitarily-equivalent output when basis excludes sx/sxdg (backport #16729)#16735
Cryoris merged 1 commit into
stable/2.5from
mergify/bp/stable/2.5/pr-16729

Conversation

@mergify

@mergify mergify Bot commented Aug 11, 2026

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)

This is an automatic backport of pull request #16729 done by [Mergify](https://mergify.com).

…basis excludes sx/sxdg (#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>
(cherry picked from commit 9499a6a)
@mergify
mergify Bot requested a review from a team as a code owner August 11, 2026 08:01
@mergify
mergify Bot requested a review from alexanderivrii August 11, 2026 08:01
@qiskit-bot

Copy link
Copy Markdown
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@github-actions github-actions Bot added Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. mod: transpiler Issues and PRs related to Transpiler labels Aug 11, 2026
@github-actions github-actions Bot added this to the 2.5.2 milestone Aug 11, 2026
@github-actions github-actions Bot added the fault tolerance related to fault tolerance compilation label Aug 11, 2026
@Cryoris
Cryoris enabled auto-merge August 11, 2026 08:20
@Cryoris
Cryoris added this pull request to the merge queue Aug 11, 2026
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 31471417832

Coverage increased (+0.009%) to 87.685%

Details

  • Coverage increased (+0.009%) from the base build.
  • Patch coverage: 2 of 2 lines across 1 file are fully covered (100%).
  • 11 coverage regressions across 4 files.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

11 previously-covered lines in 4 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/lex.rs 6 91.52%
crates/circuit/src/parameter/symbol_expr.rs 3 76.03%
crates/circuit/src/parameter/parameter_expression.rs 1 90.36%
crates/qasm2/src/expr.rs 1 93.88%

Coverage Stats

Coverage Status
Relevant Lines: 128757
Covered Lines: 112900
Line Coverage: 87.68%
Coverage Strength: 997449.96 hits per line

💛 - Coveralls

Merged via the queue into stable/2.5 with commit 256a199 Aug 11, 2026
32 checks passed
@Cryoris
Cryoris deleted the mergify/bp/stable/2.5/pr-16729 branch August 11, 2026 08:51
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. fault tolerance related to fault tolerance compilation mod: transpiler Issues and PRs related to Transpiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants