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
|
Joint-PPM circuit measuring a logical Pauli product across two gadgets via a bridge. |
|
Single-PPM measurement circuit for gadget, assembled per Cain et al. arXiv:2603.28627. |
|
Return a copy of |
|
Per-qubit |
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_observablefor 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_prepfor 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_samplerfor 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_initon 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_initis 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_observablefor 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
supportqubits; 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_prepfor the character-to-state mapping. A state complementary to the measured logical basis makes obs0/obs1 intentionally random; usecompile_samplerfor that truth-table experiment, notdetector_error_modelor Sinter.- Raises:
ValueError – rounds < 1; the finalized gadget fixes anything other than one logical degree of freedom; or
data_initis neither length 1 nor lengthgadget.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
circuitwith 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_initstring preparing a Pauli logical state on logical qubitlog_idx.state∈ {“0”, “1”, “+”, “-“}:“0” →
"0" * n—|0⟩^nprojects to|0⟩_L^{⊗k}for any CSS code“1” → “1” on supp(X̄_{log_idx}), “0” elsewhere — flips logical qubit
log_idxfrom|0⟩_Lto|1⟩_L; other logical qubits stay at|0⟩_L“+” →
"+" * n—|+⟩^nprojects to|+⟩_L^{⊗k}for any CSS code“-” → “-” on supp(Z̄_{log_idx}), “+” elsewhere — flips logical qubit
log_idxfrom|+⟩_Lto|-⟩_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" * nbroadcast is not.For a meaningful PPM truth-table check,
log_idxmust match the logical qubit the gadget measures — its seed operator should becode.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 asdata_inittobuild_single_ppm_circuit, or inside a tuple tobuild_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 –
stateis not one of “0”, “1”, “+”, “-“.IndexError –
log_idxis out of range forcode.dimension.