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, sop = 0whenever the measured parity matches the expected value. This is useful for quantum error correction and when interfacing with Stim. When the circuit is converted viato_stim(), a parity instruction becomes either aDETECTOR(ifobservable=False) or anOBSERVABLE_INCLUDE(ifobservable=True) instruction.- Parameters:
- clbitsClbit or Sequence[Clbit]
The classical bit(s) to compute parity over. A single
Clbitmeasures the parity of one bit; any sequence (list,tuple,range, …) computes the XOR of all elements.- expectationint, optional
The expected parity value (
0or1), XORed into the result so thatp = 0when the measured parity equals the expectation. Default is0.- observablebool, optional
If
True, this parity is treated as a Stim observable rather than a detector. Default isFalse.
- Returns:
ParityHandleA 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.ParityHandleDocumentation 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