Skip to content

Observables & Metrics ​

A PauliString is an n-qubit Pauli operator. Simulator reads its expectation and measures it, and compares two stabilizer states by fidelity. Together they cover the "measure something" half of the library, complementing the gates and channels.

PauliString ​

A PauliString stores iphaseP1⊗⋯⊗Pn as X and Z bit lists plus a phase in 0…3: 0→+1, 1→+i, 2→−1, 3→−i. Valid observables are Hermitian, i.e. have even phase.

python
from qliff import PauliString

p = PauliString.parse("-XYZ")

Constructors ​

ConstructorDescription
PauliString(x, z, phase=0)from explicit X/Z bit lists
PauliString.parse(s)from a string: "+XYZ", "-Y", "+iXZ", "X_Z" (_ = identity)
PauliString.identity(n)the n-qubit identity
PauliString.from_sparse(n, ops, phase=0)from {qubit: 'X'|'Y'|'Z'}, others identity

Properties and methods ​

Property/MethodDescription
x, z, phasethe X/Z bit lists and the phase (0..3)
nnumber of qubits
commutes_with(other)whether the two Paulis commute
a * bPauli product, tracking the i phase
python
from qliff import PauliString

x = PauliString.parse("X")
z = PauliString.parse("Z")

(x * z)                         # -iY
x.commutes_with(z)              # False
PauliString.from_sparse(3, {0: "X", 2: "Z"})   # +XIZ

Reading observables ​

Both live on the Simulator. expectation is a free-function alias for peek. P is a PauliString or a signed string such as "ZZ" or "-X".

CallReturnsDescription
sim.peek(P)-1 | 0 | +1⟨P⟩ without collapsing the state
expectation(sim, P)-1 | 0 | +1free-function form of the above
sim.measure(P, force=None)(value, random)measure P in place; value in {+1,-1}
  • peek returns ⟨P⟩ and never collapses. A 0 means the state is not an eigenstate of P.
  • measure collapses the state, returning the eigenvalue and whether the outcome was a coin flip. It handles multi-qubit stabilizers -- the primitive behind syndrome extraction.
  • force=+1 / force=-1 pins a random outcome, projecting onto a chosen eigenspace.
python
from qliff import Simulator

bell = Simulator(2).H(0).CX(0, 1)

bell.peek("ZZ")             # +1
bell.measure("XX")          # (1, False)
Simulator(2).measure("XX")  # (+1 or -1, True)

State fidelity ​

a.fidelity(b) returns the overlap |⟨a|b⟩|2 between two simulators' stabilizer states. It measures each stabilizer generator of a on a copy of b, so the result is always 0 or a power of 12.

python
from qliff import Simulator

Simulator(1).fidelity(Simulator(1).H(0))   # 0.5

a = Simulator(2).H(0).CX(0, 1)
a.fidelity(a.copy())                       # 1.0
a.fidelity(a.copy().Z(0))                  # 0.0