Circuit.append("R_PAULI", -PauliString, a) drops the sign and rotates the wrong way #228
Copy link
Copy link
Open
Labels
Description
Circuit.append("R_PAULI", pauli_string, angle) ignores the sign of a stim.PauliString. Stim's own append drops PauliString signs (even for MPP), and tsim passes the targets straight through. For a rotation the sign matters: exp(-i a π/2 (-P)) = exp(+i a π/2 P), so R_PAULI(a) about -P is R_PAULI(-a) about P. tsim instead silently applies the rotation in the wrong direction.
Reproducer
import numpy as np
import stim
import tsim
# exp(-i a pi/2 (-X)) is exp(+i a pi/2 X): R_PAULI about -X should equal R_PAULI(-a) about X.
c = tsim.Circuit()
c.append("R_PAULI", stim.PauliString("-X"), 0.3)
print("append(-X):", repr(str(c)))
print("expected: ", repr(str(tsim.Circuit("R_PAULI(-0.3) X0"))))
# The sign is observable: after S, R_PAULI about X tilts |+i> towards +Z or -Z depending on the sign.
def p1(circuit):
circuit.append("M", [0])
return np.asarray(circuit.compile_sampler(seed=1).sample(100000)).mean()
a = tsim.Circuit("H 0\nS 0")
a.append("R_PAULI", stim.PauliString("-X"), 0.3)
b = tsim.Circuit("H 0\nS 0\nR_PAULI(-0.3) X0")
print(f"P(M=1): append(-X) {p1(a):.3f}, R_PAULI(-0.3) X0 {p1(b):.3f}")Output on main (be0e8af):
append(-X): 'R_PAULI(0.3) X0'
expected: 'R_PAULI(-0.3) X0'
P(M=1): append(-X) 0.095, R_PAULI(-0.3) X0 0.905
Possible fix
In the R_PAULI branch of Circuit.append, when targets is a stim.PauliString with sign -1, negate the angle (or mark the first factor with !, which the parser already handles). A sign of ±i (an anti-Hermitian product) should raise. The same applies to R_XX/R_YY/R_ZZ if they ever accept PauliStrings.
Reactions are currently unavailable
Activity