Observables & Metrics
A PauliString is an 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
python
from qliff import PauliString
p = PauliString.parse("-XYZ")Constructors
| Constructor | Description |
|---|---|
PauliString(x, z, phase=0) | from explicit |
PauliString.parse(s) | from a string: "+XYZ", "-Y", "+iXZ", "X_Z" (_ = identity) |
PauliString.identity(n) | the |
PauliString.from_sparse(n, ops, phase=0) | from {qubit: 'X'|'Y'|'Z'}, others identity |
Properties and methods
| Property/Method | Description |
|---|---|
x, z, phase | the 0..3) |
n | number of qubits |
commutes_with(other) | whether the two Paulis commute |
a * b | Pauli product, tracking the |
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"}) # +XIZReading 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".
| Call | Returns | Description |
|---|---|---|
sim.peek(P) | -1 | 0 | +1 | |
expectation(sim, P) | -1 | 0 | +1 | free-function form of the above |
sim.measure(P, force=None) | (value, random) | measure value in {+1,-1} |
peekreturnsand never collapses. A 0means the state is not an eigenstate ofP.measurecollapses 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=-1pins 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 on a copy of b, so the result is always
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