qrisp.QuantumCircuit.parity#
- QuantumCircuit.parity(clbits, expectation=0, observable=False)[source]#
Instructs a parity operation on a set of classical bits.
This method creates a parity check (XOR) on measurement results, useful for quantum error correction and when interfacing with Stim. When the circuit is converted to Stim (via
to_stim()), this creates either aDETECTORinstruction (ifobservable=False) or anOBSERVABLE_INCLUDEinstruction (ifobservable=True).- Parameters:
- clbitslist[Clbit] or Clbit
The classical bits to compute parity over. Can be a single Clbit or a list of Clbits representing measurement results.
- expectationint, optional
The expected parity value (0 or 1). 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
ParityHandleobject representing the parity result. This handle can be used as a key to look up detector/observable indices in the maps returned byto_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