qldpc.experimental.surgery.circuit

Stim surgery circuit construction (single-PPM and joint-PPM).

References

Cain et al. arXiv:2603.28627 §B.1 — single-PPM measurement protocol. Webster, Smith, Cohen arXiv:2511.15989 — gadget Eqs. (1)-(4) observable.

Copyright 2026 The qLDPC Authors

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Functions

build_joint_ppm_circuit(→ tuple[stim.Circuit, ...)

Joint-PPM circuit measuring a logical Pauli product across two gadgets via a bridge.

build_single_ppm_circuit(→ stim.Circuit)

Single-PPM measurement circuit for gadget, assembled per Cain et al. arXiv:2603.28627.

keep_only_observable(→ stim.Circuit)

Return a copy of circuit with all OBSERVABLE_INCLUDE entries dropped except one.

logical_state_init(→ str)

Per-qubit data_init string preparing a Pauli logical state on logical qubit log_idx.

Module Contents

qldpc.experimental.surgery.circuit.build_joint_ppm_circuit(g_l: qldpc.experimental.surgery.gadget.GadgetLayout, g_r: qldpc.experimental.surgery.gadget.GadgetLayout, bridge: qldpc.experimental.surgery.bridge.Bridge, *, rounds: int, noise_model: qldpc.circuits.noise_model.NoiseModel | None = None, data_init: str | tuple[str, ...] | list[str] | None = None) → tuple[stim.Circuit, qldpc.codes.common.CSSCode][source]

Joint-PPM circuit measuring a logical Pauli product across two gadgets via a bridge.

Emits two OBSERVABLE_INCLUDE entries (see _surgery_observable for full semantics):

  • obs0 — Single-round joint readout via the bridged-product identity of Webster, Smith, Cohen arXiv:2511.15989 §II B 2 (prose, crediting Cross et al. arXiv:2407.18393), ∏_{v ∈ support_l ∪ support_r} A_v = X̄_l ⊗ X̄_r (or Z̄_l ⊗ Z̄_r for basis=Z): the XOR of the last QEC round’s meas-check outcomes on both patches, argued but not tested to be decoding-equivalent to Cain et al.’s first-cycle readout (arXiv:2603.28627 App. D).

  • obs1 — Direct destructive M on support_l ∪ support_r; noiseless cross-check, not a physical protocol.

For LER / noisy runs, use keep_only_observable(circuit, keep_idx=0).

data_init (optional): override the per-code data init.

  • str — per-physical-qubit (or len-1 broadcast). For intercode, positions [0:n_l) are left, [n_l:n_l+n_r) are right; for intracode, length is n_l. See _surgery_state_prep for the char-to-state mapping.

  • tuple[str, str] (intercode only) — per-code logical-init spec. data_init=("0", "+") → c_l in |0⟩_L, c_r in |+⟩_L.

If either code is initialized in the basis complementary to the measured logical, the joint observable is intentionally random. Its detectors remain deterministic, but the circuit does not define a detector error model; use compile_sampler for such truth-table experiments.

Raises:
  • ValueError – rounds < 1; the bridge was built from different gadget layouts; the finalized joint code fixes anything other than one logical degree of freedom; a tuple data_init on an intracode pair, which has a single data set; a tuple of length != 2; or a per-code spec whose length matches neither 1 nor that code’s data-qubit count.

  • TypeError – data_init is not a str, tuple, list, or None, or a tuple entry is not a str.

qldpc.experimental.surgery.circuit.build_single_ppm_circuit(gadget: qldpc.experimental.surgery.gadget.GadgetLayout, *, rounds: int, noise_model: qldpc.circuits.noise_model.NoiseModel | None = None, data_init: str | None = None) → stim.Circuit[source]

Single-PPM measurement circuit for gadget, assembled per Cain et al. arXiv:2603.28627.

Emits two OBSERVABLE_INCLUDE entries (see _surgery_observable for full semantics):

  • obs0 — Single-round Z̄ = ∏_{v ∈ support} A_v readout, or X̄ for basis=X (Webster, Smith, Cohen arXiv:2511.15989 §II A, gadget Eqs. (1)-(4)): the XOR of the last QEC round’s meas-check outcomes, argued but not tested to be decoding-equivalent to Cain et al.’s first-cycle readout (arXiv:2603.28627 App. D).

  • obs1 — Direct destructive M on support qubits; noiseless cross-check, not a physical protocol.

For LER / noisy runs, use keep_only_observable(circuit, keep_idx=0).

data_init (optional): per-data-qubit init override; see _surgery_state_prep for the character-to-state mapping. A state complementary to the measured logical basis makes obs0/obs1 intentionally random; use compile_sampler for that truth-table experiment, not detector_error_model or Sinter.

Raises:

ValueError – rounds < 1; the finalized gadget fixes anything other than one logical degree of freedom; or data_init is neither length 1 nor length gadget.code.num_qudits, or contains a character outside “01+-“.

qldpc.experimental.surgery.circuit.keep_only_observable(circuit: stim.Circuit, keep_idx: int) → stim.Circuit[source]

Return a copy of circuit with all OBSERVABLE_INCLUDE entries dropped except one.

Keeps only the observable whose first argument equals keep_idx. Recurses into REPEAT blocks so observables inside loops are filtered the same way.

For surgery PPM circuits pass keep_idx=0: obs1 measures the data on V_0 directly and is not part of any physical protocol, so an LER run that kept it would sample the wrong distribution. sinter also expects exactly one observable per task.

qldpc.experimental.surgery.circuit.logical_state_init(code: qldpc.codes.common.CSSCode, state: str, *, log_idx: int) → str[source]

Per-qubit data_init string preparing a Pauli logical state on logical qubit log_idx.

state ∈ {“0”, “1”, “+”, “-“}:
  • “0” → "0" * n — |0⟩^n projects to |0⟩_L^{⊗k} for any CSS code

  • “1” → “1” on supp(X̄_{log_idx}), “0” elsewhere — flips logical qubit log_idx from |0⟩_L to |1⟩_L; other logical qubits stay at |0⟩_L

  • “+” → "+" * n — |+⟩^n projects to |+⟩_L^{⊗k} for any CSS code

  • “-” → “-” on supp(Z̄_{log_idx}), “+” elsewhere — flips logical qubit log_idx from |+⟩_L to |-⟩_L; other logical qubits stay at |+⟩_L

X̄_{log_idx} and Z̄_{log_idx} come from code.get_logical_ops(Pauli.X)[log_idx] and [Pauli.Z][log_idx], which qldpc guarantees anti-commute, so the prep is correct for any CSS code whatever the parity of wt(X̄) / wt(Z̄) — which a naive "1" * n broadcast is not.

For a meaningful PPM truth-table check, log_idx must match the logical qubit the gadget measures — its seed operator should be code.get_logical_ops(Pauli.Z)[log_idx] (or [Pauli.X] for basis=X). This is not verified; if the indices disagree the prep targets a logical qubit the gadget does not measure, and the obs0 outcome is silently random.

The returned string has length code.num_qudits; pass it as data_init to build_single_ppm_circuit, or inside a tuple to build_joint_ppm_circuit. Initializing in the basis complementary to the measured logical is useful for raw-sampler truth tables, but makes the logical observable intentionally random; such a circuit has deterministic detectors but cannot define a detector error model for LER decoding.

Raises:
  • ValueError – state is not one of “0”, “1”, “+”, “-“.

  • IndexError – log_idx is out of range for code.dimension.