qrisp.QuantumCircuit.parity#

QuantumCircuit.parity(clbits: Clbit | Sequence[Clbit], expectation: int = 0, observable: bool = False) ParityHandle[source]#

Append a parity (XOR) check over classical bits to the circuit.

Computes p = b_0 b_1 b_{n-1} expectation, so p = 0 whenever the measured parity matches the expected value. This is useful for quantum error correction and when interfacing with Stim. When the circuit is converted via to_stim(), a parity instruction becomes either a DETECTOR (if observable=False) or an OBSERVABLE_INCLUDE (if observable=True) instruction.

Parameters:
clbitsClbit or Sequence[Clbit]

The classical bit(s) to compute parity over. A single Clbit measures the parity of one bit; any sequence (list, tuple, range, …) computes the XOR of all elements.

expectationint, optional

The expected parity value (0 or 1), XORed into the result so that p = 0 when the measured parity equals the expectation. Default is 0.

observablebool, optional

If True, this parity is treated as a Stim observable rather than a detector. Default is False.

Returns:
ParityHandle

A handle representing the parity result. Use it as a key to look up detector/observable indices in the maps returned by to_stim().

See also

qrisp.parity()

The gate function version for use in QuantumSessions

to_stim()

Convert to Stim circuit with detector/observable maps

qrisp.jasp.ParityHandle

Documentation of the ParityHandle class

Examples

Create a simple detector checking that two qubits have even parity:

>>> from qrisp import QuantumCircuit
>>> qc = QuantumCircuit(2, 2)
>>> qc.h(0)
>>> qc.cx(0, 1)
>>> qc.measure([0, 1], [0, 1])
>>> handle = qc.parity([qc.clbits[0], qc.clbits[1]], expectation=0)
>>> print(handle)
ParityHandle(Clbit(cb_2), Clbit(cb_3))

Convert to Stim and check the detector:

>>> stim_circuit, meas_map, det_map = qc.to_stim(
...     return_measurement_map=True,
...     return_detector_map=True
... )
>>> det_map[handle]  # Get the Stim detector index
0