Skip to content

fix(gate-decompose): correct the sign on the first RZ of the RX sequence - #4

Open
cleitonaugusto wants to merge 2 commits into
rustnew:mainfrom
cleitonaugusto:fix-rx-decomposition-sign
Open

fix(gate-decompose): correct the sign on the first RZ of the RX sequence#4
cleitonaugusto wants to merge 2 commits into
rustnew:mainfrom
cleitonaugusto:fix-rx-decomposition-sign

Conversation

@cleitonaugusto

Copy link
Copy Markdown

Fixes the RX half of #3.

The RX arm of the decomposition table emits, in circuit order:

rz(-pi/2), sx, rz(pi + theta), sx, rz(pi/2)

That does not implement RX(theta). I compared it against RX(theta) up to global
phase at theta in {0, 0.3, 1.0, pi/2, pi, 2.2, -0.7, 3.9} and it fails at all of
them. The clearest case is theta = 0, which should be the identity and instead
comes out as

[[1,  0],
 [0, -1]]

which is Z.

Changing the first rotation to rz(+pi/2) makes the sequence equal RX(theta) up
to global phase at every angle I tried.

The test I added builds the operator from whatever the table returns rather than
from a copy of the sequence, so it keeps checking the real entry if the table
changes later. It fails on the old sign and passes on the new one. I ran the
whole workspace suite and nothing else moved.

The comment above that arm gave two different formulas, one with six gates and
one with five. I replaced it with the sequence that is actually emitted.

This is only the sign. The other half of #3, that gate-decomposition never
removes the original op, is untouched here since it needs a different change.
Happy to send that one too if you want it.

The RX arm emitted rz(-pi/2), sx, rz(pi + theta), sx, rz(pi/2), which does
not implement RX(theta) for any angle. The clearest case is theta = 0: the
identity compiled to diag(1, -1), which is Z.

Changing the first rotation to rz(+pi/2) makes the sequence equal RX(theta)
up to global phase.

Adds test_rx_decomposition_matches_rx, which builds the operator from
whatever the table returns and compares it against RX(theta) at eight
angles, so it keeps checking the real entry rather than a copy. The test
fails on the old sign and passes on the new one.
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown

Important

Review available on request

  • 🔍 Trigger review

Reviews should be triggered manually for repositories with fewer than 10 stars. Select Trigger review above or comment @coderabbitai review to review the latest changes. For a full review, comment @coderabbitai full review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 77680cbe-2fe7-45f3-89d5-32970bb0db68


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.

…order

The first version of this branch failed both CI gates: rustfmt reformatted
the matrix literals, and clippy rejected map_or under -D warnings. Both are
fixed here, and I ran fmt, clippy and the workspace suite locally this time.

The ordering comment in the rx test was also claiming something the test
could not check. Every entry in the decomposition table is a single gate, a
palindrome or a conjugation, so the product is the same in either order and
no table entry can pin the convention down.

test_composition_is_in_circuit_order does it directly, with an asymmetric
pair and an expected matrix written out by hand rather than composed. That
second part matters: comparing mmul against mmul passes even when mmul
itself is inverted, since the error cancels on both sides. With the literal
matrix the test fails when the multiplication order is flipped.

Also loosens the phase comparison to be relative to the entry size, since an
absolute bound fails on correct code when a matrix entry is near zero, and
names the sx convention the test assumes.
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.

1 participant