Skip to content

loqs.codepacks.codepack_surf17_surgery

Lattice-surgery machinery for the Surface-17/13/10 codepack.

This module implements merge/split lattice surgery between two d=3 rotated surface-code patches built from codepack_surf17_tomita2014, on top of the multi-patch foundation in codepack_surf17_multipatch. Every builder below takes a PatchGeometry bundling its patches' labels, qubit lists, and seam(s) -- not to be confused with the "Geometry" section just below, which describes this module's own internal check/tile layout constants (SEAM_GEOMETRIES and friends), an unrelated concept.

Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

Classes

Attributes

Name Type Value Doc
BASE_H_X np.array([[1, 1, 0, 1, 1, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 0]])
BASE_H_Z np.array([[1, 0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 1, 0, 0, 1]])
MERGED_NUM_DATA 21
SEAM_GEOMETRY_ZZ SeamGeometry {'kind': 'ZZ', 'orientation': 'vertical', 'seam_prep_basis': 'X', 'new_check_type': 'Z', 'new_checks': [{'support': [('A', 7), ('A', 8), ('S', 1), ('S', 2)], 'tile': [('A', 7), ('A', 8), ('S', 1), ('S', 2)]}, {'support': [('S', 0), ('S', 1), ('B', 0), ('B', 1)], 'tile': [('S', 0), ('S', 1), ('B', 0), ('B', 1)]}, {'support': [('A', 6), ('S', 0)], 'tile': [None, ('A', 6), None, ('S', 0)]}, {'support': [('S', 2), ('B', 2)], 'tile': [('S', 2), None, ('B', 2), None]}], 'grown_check_type': 'X', 'grown_checks': {'A': {'check_row': 3, 'old_support': [('A', 6), ('A', 7)], 'seam_pair': [('S', 0), ('S', 1)], 'support': [('A', 6), ('A', 7), ('S', 0), ('S', 1)], 'tile': [('A', 6), ('A', 7), ('S', 0), ('S', 1)]}, 'B': {'check_row': 1, 'old_support': [('B', 1), ('B', 2)], 'seam_pair': [('S', 1), ('S', 2)], 'support': [('S', 1), ('S', 2), ('B', 1), ('B', 2)], 'tile': [('S', 1), ('S', 2), ('B', 1), ('B', 2)]}}, 'parity_support': [('A', 6), ('A', 7), ('A', 8), ('B', 0), ('B', 1), ('B', 2)], 'parity_frame_bit_type': 'X', 'telescope_reference_checks': {'A': [0, 2], 'B': [1, 3]}, 'byproduct_seam_indices': [0, 1, 2], 'byproduct_patch': 'B', 'byproduct_logical': 'Z', 'byproduct_frame_support': [0, 4, 8], 'merged_Z_L': [('A', 0), ('A', 4), ('A', 8)], 'merged_X_L': [('A', 0), ('A', 3), ('A', 6), ('S', 0), ('B', 0), ('B', 3), ('B', 6)]}
SEAM_GEOMETRY_XX SeamGeometry {'kind': 'XX', 'orientation': 'horizontal', 'seam_prep_basis': 'Z', 'new_check_type': 'X', 'new_checks': [{'support': [('A', 2), ('A', 5), ('S', 0), ('S', 1)], 'tile': [('A', 2), ('S', 0), ('A', 5), ('S', 1)]}, {'support': [('S', 1), ('S', 2), ('B', 3), ('B', 6)], 'tile': [('S', 1), ('B', 3), ('S', 2), ('B', 6)]}, {'support': [('S', 0), ('B', 0)], 'tile': [None, None, ('S', 0), ('B', 0)]}, {'support': [('A', 8), ('S', 2)], 'tile': [('A', 8), ('S', 2), None, None]}], 'grown_check_type': 'Z', 'grown_checks': {'A': {'check_row': 3, 'old_support': [('A', 5), ('A', 8)], 'seam_pair': [('S', 1), ('S', 2)], 'support': [('A', 5), ('A', 8), ('S', 1), ('S', 2)], 'tile': [('A', 5), ('S', 1), ('A', 8), ('S', 2)]}, 'B': {'check_row': 0, 'old_support': [('B', 0), ('B', 3)], 'seam_pair': [('S', 0), ('S', 1)], 'support': [('S', 0), ('S', 1), ('B', 0), ('B', 3)], 'tile': [('S', 0), ('B', 0), ('S', 1), ('B', 3)]}}, 'parity_support': [('A', 2), ('A', 5), ('A', 8), ('B', 0), ('B', 3), ('B', 6)], 'parity_frame_bit_type': 'Z', 'telescope_reference_checks': {'A': [2, 3], 'B': [0, 1]}, 'byproduct_seam_indices': [0, 1, 2], 'byproduct_patch': 'B', 'byproduct_logical': 'X', 'byproduct_frame_support': [2, 4, 6], 'merged_X_L': [('A', 2), ('A', 4), ('A', 6)], 'merged_Z_L': [('A', 0), ('A', 1), ('A', 2), ('S', 0), ('B', 0), ('B', 1), ('B', 2)]}
SEAM_GEOMETRIES dict[str, SeamGeometry] {'ZZ': SEAM_GEOMETRY_ZZ, 'XX': SEAM_GEOMETRY_XX}
TEMPLATE_QUBITS {layout: layout_qubits(layout) for layout in ('surf17', 'surf13', 'surf10')} Template qubit labels per layout -- sourced from codepack_surf17_tomita2014.layout_qubits, the single canonical definition.

Functions

Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_merge_instruction

build_merge_instruction(kind: str, geometry: PatchGeometry, num_merge_rounds: int = 3, circuit_backend: type[BasePhysicalCircuit] = PyGSTiPhysicalCircuit, idle_layout: Literal['surf17', 'surf13', 'surf10'] | None = None, gate_durations: dict[str, int | float] | None = None, idle_gates: dict[int | float, str] | None = None, name: str | None = None) -> Instruction

Merge two patches through a seam for num_merge_rounds SE rounds.

Composite of seam preparation (|+> for ZZ, |0> for XX), the merged syndrome-extraction circuit repeated num_merge_rounds times, and a bookkeeping step that extends the per-patch namespaced syndrome histories (with the grown boundary checks substituted in place), records the four new seam checks, and registers a PatchRelation between the two patches (grown_rows/ byproduct/repair_key) for a later build_split_byproduct_repair_instruction() to read. Must be followed by the matching build_split_instruction() in the same shot.

Parameters:

Name Type Description Default
kind str

"ZZ" (vertical merge measuring Z_L Z_L) or "XX" (horizontal).

required
geometry PatchGeometry

A PatchGeometry with roles "a"/"b" (for a ZZ merge, "a" is the top patch, "b" the bottom; for XX, "a" is left, "b" right), each assigned a full patch qubit list (template order D0..D8 + ancillas), and one seam: the 3 seam qubits, left-to-right for ZZ, top-to-bottom for XX. All patches must share one layout.

required
num_merge_rounds int

Number of merged syndrome-extraction rounds (default 3 = d).

3
circuit_backend type[BasePhysicalCircuit]

The circuit backend. Default is PyGSTiPhysicalCircuit.

PyGSTiPhysicalCircuit
idle_layout Literal['surf17', 'surf13', 'surf10'] | None

See _surgery_metadata; idle padding is off by default.

None
gate_durations Literal['surf17', 'surf13', 'surf10'] | None

See _surgery_metadata; idle padding is off by default.

None
idle_gates Literal['surf17', 'surf13', 'surf10'] | None

See _surgery_metadata; idle padding is off by default.

None
name str | None

Name for logging purposes.

None

Returns:

Type Description
Instruction

The composite merge instruction.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_merged_check_matrices

build_merged_check_matrices(kind: str) -> tuple[np.ndarray, np.ndarray, list[str], list[str]]

Build the merged-code check matrices for a ZZ or XX merge.

Column convention: 0-8 = A.D0-D8, 9-11 = S0-S2, 12-20 = B.D0-D8. Row order groups patch checks first (A rows 0-3, B rows 4-7, with the grown boundary checks substituted in place at their patch row) followed by the four new seam checks (for the seam-check type only).

Parameters:

Name Type Description Default
kind str

"ZZ" (vertical merge, 8 X checks / 12 Z checks) or "XX" (horizontal merge, 12 X checks / 8 Z checks).

required

Returns:

Type Description
tuple[ndarray, ndarray, list[str], list[str]]

(H_X_merged, H_Z_merged, x_check_labels, z_check_labels).


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_mzz_bell_corrections_instruction

build_mzz_bell_corrections_instruction(patch_a_label: str, tgt_patch_label: str, name: str = 'M_ZZ Bell prep corrections') -> Instruction

The conditional-correction step of the M_ZZ Bell preparation.

Applies X_L(tgt)^m_zz (frame X on the X_L support D2, D4, D6), with m_zz the most recent surgery_parity_zz_{patch_a_label}_{tgt_patch_label} value anywhere in the history, and records the applied bit under mzz_bell_correction_{patch_a_label}_{tgt_patch_label}.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_mzz_bell_prep_sequence

build_mzz_bell_prep_sequence(geometry: PatchGeometry, mode: str = 'ft', num_merge_rounds: int = 3, circuit_backend: type[BasePhysicalCircuit] = PyGSTiPhysicalCircuit, idle_layout: Literal['surf17', 'surf13', 'surf10'] | None = None, gate_durations: dict[str, int | float] | None = None, idle_gates: dict[int | float, str] | None = None, name: str | None = None) -> list

Stack-entry sequence for Bell prep by a direct M_ZZ merge.

Measures Z_L(A) Z_L(B) on the |+>_L (x) |+>_L product state with a single merge/split through one 3-qubit seam (no ancilla patch), then injects the outcome-conditioned frame correction X_L(B)^m_zz, leaving the Bell state (|00> + |11>)/sqrt(2). The caller preps both patches in |+>_L and runs QEC before and after the returned entries. geometry: see build_merge_instruction() (roles "a"/"b", one seam).

Measurement flags for the caller (cf. build_surgery_cnot_sequence): both patches are |+>-prepped so their round-0 Z syndrome layer is random (use reference_round_mode_Z="guarded_diff" for FT Z measures), but the round-0 X layer is DETERMINISTIC — use reference_round_mode_X="raw" (the default) for FT X measures so faults in the prep window stay detectable; the grown-X-check history rewrite at split is absorbed by the exported round-0 offset key, not by dropping round 0.

idle_layout, gate_durations, idle_gates: see _surgery_metadata.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_split_byproduct_repair_instruction

build_split_byproduct_repair_instruction(patch_a_label: str, patch_b_label: str, num_merge_rounds: int = 3, num_post_split_rounds: int = 3, fire_rule: str = 'both', defect_decode_mode: Literal['heuristic', 'matching'] = 'heuristic', name: str | None = None) -> Instruction

Post-split repair of the surgery byproduct (seam-fault FT hole).

Place AFTER at least one QEC block on both patches following the merge/split of the same geometry (the QEC block must add num_post_split_rounds syndrome rounds per patch). Detects the grown-check defect signature of a single effective seam-conjugate error on the seam qubit whose byproduct flip is NOT compensated by the downstream termination decoders, and applies one extra byproduct logical Pauli frame, restoring single-fault tolerance of the logical correlation in the byproduct-sensitive basis. Records the applied bit under the repair_key registered by the matching build_merge_instruction() call's PatchRelation (see below) -- no kind/qubits_a/qubits_b/seam_qubits/layout here: this instruction has no physical circuit of its own, and reads its bookkeeping (grown_rows/byproduct/repair_key) from that relation instead of independently recomputing _surgery_metadata.

fire_rule selects the uncompensated signature for the context: "both" when both patches get a byproduct-sensitive termination decode (mzz Bell prep; the surgery CNOT's XX merge - the ancilla's Z decode feeds m_anc into the X_L(tgt) correction), "b_only" when only patch A does (the surgery CNOT's ZZ merge, whose patch B is the ancilla, terminated in the conjugate basis). defect_decode_mode selects the fire-decision mechanism ("heuristic" default, "matching" a real per-row decode fixing a "b_only" false-fire -- see _split_byproduct_repair_apply_fn).

Without this step a single fault on the middle seam qubit (or a hook error reaching it) flips the decoded logical XOR: the raw byproduct record is wrong AND both patches' termination decoders independently boundary-match the resulting grown-check defects - three net flips (see _split_byproduct_repair_apply_fn).


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_split_instruction

build_split_instruction(kind: str, geometry: PatchGeometry, mode: str = 'simple', circuit_backend: type[BasePhysicalCircuit] = PyGSTiPhysicalCircuit, idle_layout: Literal['surf17', 'surf13', 'surf10'] | None = None, gate_durations: dict[str, int | float] | None = None, idle_gates: dict[int | float, str] | None = None, name: str | None = None) -> Instruction

Split a merged patch pair and extract the joint logical parity.

Composite of the seam measurement (X basis for ZZ, Z basis for XX) and a bookkeeping step that stores the joint parity under the frame key surgery_parity_zz_{patch_a_label}_{patch_b_label} / surgery_parity_xx_{patch_a_label}_{patch_b_label}, applies the post-split boundary-check flips to the per-patch histories, and injects the conditional logical byproduct into one patch's Pauli frame. kind and geometry must match the preceding build_merge_instruction().

mode="simple" majority-votes the seam-check product with per-patch decoding untouched; mode="ft" additionally decodes the merge window on the merged matching graph.

The split rewrites the grown-check rows of both patches' stored syndrome histories (see _split_bookkeeping_apply_fn), leaving a residue only in the round-0 absolute detector layer, exported to the affected patch's own tracked data (patch.data["syndrome_round0_offset_X"] / ["syndrome_round0_offset_Z"]). Subsequent FT logical measurements in the grown-check basis must either pass reference_round_mode_X="guarded_diff" after a ZZ surgery (reference_round_mode_Z="guarded_diff" after XX) or - when the prep makes that check type deterministic in round 0 - keep round 0 as a detector layer ("raw", the default), in which case the terminating decode subtracts the offset; measurements in the other basis are unaffected.

geometry (see build_merge_instruction()), idle_layout, gate_durations, idle_gates: must match the preceding build_merge_instruction() call's values.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_surgery_cnot_corrections_instruction

build_surgery_cnot_corrections_instruction(ctrl_patch_label: str, tgt_patch_label: str, anc_patch_label: str, name: str = 'Surgery CNOT corrections') -> Instruction

The conditional-correction step of the surgery CNOT.

Must be placed immediately after the ancilla patch's destructive FT Logical Z Measure (its logical_measurement value is read from history[-1]); the surgery_parity_zz_{ctrl_patch_label}_{anc_patch_label} / surgery_parity_xx_{anc_patch_label}_{tgt_patch_label} values are taken as the most recent occurrences anywhere in the history (matching the M_ZZ(ctrl, anc) / M_XX(anc, tgt) pairing in build_surgery_cnot_sequence()). Applies Z_L(ctrl)^m_xx (frame Z on the Z_L support D0, D4, D8) and X_L(tgt)^(m_zz ^ m_anc) (frame X on the X_L support D2, D4, D6), and records the applied pair under surgery_cnot_corrections_{ctrl_patch_label}_{tgt_patch_label}.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_surgery_cnot_sequence

build_surgery_cnot_sequence(geometry: PatchGeometry, mode: str = 'ft', num_merge_rounds: int = 3, num_post_split_rounds: int = 3, circuit_backend: type[BasePhysicalCircuit] = PyGSTiPhysicalCircuit, idle_layout: Literal['surf17', 'surf13', 'surf10'] | None = None, gate_durations: dict[str, int | float] | None = None, idle_gates: dict[int | float, str] | None = None, name: str | None = None) -> list

Stack-entry sequence for a logical CNOT via lattice surgery.

Implements Horsman et al. 2012: the ancilla patch is prepared in |+>, M_ZZ(ctrl, anc) runs through a vertical seam (ctrl on top), M_XX(anc, tgt) through a horizontal seam (anc on the left), the ancilla is destructively measured in the Z basis, and the outcome- conditioned logical Paulis Z_L(ctrl)^m_xx, X_L(tgt)^(m_zz^m_anc) are injected into the Pauli frames.

geometry is a PatchGeometry with roles "ctrl"/"tgt"/ "anc" (each assigned a full patch qubit list, template order D0..D8 + ancillas) and two named seams, "zz" (ctrl/anc, vertical) and "xx" (anc/tgt, horizontal); subset() derives the two-role geometry each underlying merge/split call needs.

In ft mode each merge is followed by its split-byproduct repair (see build_split_byproduct_repair_instruction()) after the post-split QEC blocks of its two patches: the ZZ repair before the XX merge (the ancilla's conditional Z_L frame must be settled before its X_L is consumed), and the XX repair - which needs post-split syndrome rounds on the TARGET as well - after an extra ("QEC", tgt) entry inserted for that purpose. num_post_split_rounds must equal the number of syndrome rounds one QEC stack entry appends (the program's num_qec_rounds).

Returns a list of stack entries (mix of patch-scoped names and bare instructions) to splice into a program stack with *. The three patches must already be initialized (Init Patch), ctrl/tgt prepped and QEC'd by the caller; the ancilla is prepped here. The ancilla's destructive measure uses reference_round_mode_Z="guarded_diff" because the XX split rewrites its Z-check history (grown-check basis change).

Post-CNOT measurement flags for the caller: the ZZ merge grows an X check on ctrl (use reference_round_mode_X="guarded_diff" for an FT X measure of ctrl) and the XX merge grows a Z check on tgt (use reference_round_mode_Z="guarded_diff" for an FT Z measure of tgt). Independently of the surgery, the usual prep-basis rule still applies: a patch prepped in the conjugate basis of the measurement has a random round-0 syndrome layer, so e.g. an FT Z measure of a |+>-prepped ctrl also needs reference_round_mode_Z="guarded_diff" (without it the decoder matches the round-0 layer as real defects and applies a random logical correction, which destroys e.g. Bell correlations).

Two FT holes formerly open here are now fixed: an ancilla data fault during the M_ZZ merged-SE rounds could miscorrect X_L(anc)'s contribution to m_xx (fixed in _split_bookkeeping_apply_fn), and the ZZ repair's "b_only" fire rule could false-fire on an ordinary bulk fault (fixed via defect_decode_mode="matching", see _split_byproduct_repair_apply_fn).


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_surgery_parity_instruction

build_surgery_parity_instruction(kind: str, geometry: PatchGeometry, mode: str = 'simple', num_merge_rounds: int = 3, circuit_backend: type[BasePhysicalCircuit] = PyGSTiPhysicalCircuit, idle_layout: Literal['surf17', 'surf13', 'surf10'] | None = None, gate_durations: dict[str, int | float] | None = None, idle_gates: dict[int | float, str] | None = None, name: str | None = None) -> Instruction

Full merge+split lattice-surgery joint parity measurement.

Measures Z_L(A) Z_L(B) (kind="ZZ") or X_L(A) X_L(B) (kind="XX") non-destructively via num_merge_rounds rounds of merged syndrome extraction; the result is stored under the frame key surgery_parity_zz_{patch_a_label}_{patch_b_label} / surgery_parity_xx_{patch_a_label}_{patch_b_label}. See build_merge_instruction() and build_split_instruction() for geometry and bookkeeping details.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

build_surgery_parity_instruction_sequence

build_surgery_parity_instruction_sequence(kind: str, geometry: PatchGeometry, mode: str = 'ft', num_merge_rounds: int = 3, circuit_backend: type[BasePhysicalCircuit] = PyGSTiPhysicalCircuit, idle_layout: Literal['surf17', 'surf13', 'surf10'] | None = None, gate_durations: dict[str, int | float] | None = None, idle_gates: dict[int | float, str] | None = None, name: str | None = None) -> list[Instruction]

The surgery parity measurement as a flat instruction list.

Semantically identical to build_surgery_parity_instruction() but returns [seam prep, merged SE (x num_merge_rounds), merge bookkeeping, seam measurement, split bookkeeping] as separate stack-ready instructions, so error-injection tools (which attach error_injections kwargs to individual stack entries) can target a single merged-SE round or the seam circuits directly.

geometry, idle_layout, gate_durations, idle_gates: see build_merge_instruction()/_surgery_metadata.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

get_surgery_stage_circuits

get_surgery_stage_circuits(kind: str, geometry: PatchGeometry, circuit_backend: type[BasePhysicalCircuit] = PyGSTiPhysicalCircuit, idle_layout: Literal['surf17', 'surf13', 'surf10'] | None = None, gate_durations: dict[str, int | float] | None = None, idle_gates: dict[int | float, str] | None = None) -> dict

The three physical circuits of one merge/split surgery, for drawing.

Returns {"seam_prep": ..., "merged_se": ..., "seam_measure": ...}: the seam preparation circuit (|+> for ZZ, |0> for XX), ONE round of the merged syndrome-extraction circuit (repeated num_merge_rounds times at runtime), and the destructive seam measurement circuit (X basis for ZZ, Z basis for XX). These are the same circuit objects the merge/split instructions execute; the classical bookkeeping steps (history extension, decode, byproduct frames) have no circuit representation.

geometry, idle_layout, gate_durations, idle_gates: see build_merge_instruction()/_surgery_metadata.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

merged_index

merged_index(support_elem: tuple[str, int]) -> int

Map an ('A'|'S'|'B', idx) support element to its merged-code column.


Geometry

Each patch is a 3x3 data grid (row-major D0..D8) with the Tomita-Svore check layout. The codepack's logical representatives are Z_L = Z0 Z4 Z8 and X_L = X2 X4 X6, with the boundary-string equivalences (by stabilizer multiplication):

  • Z_L == Z0 Z1 Z2 (top row) == Z6 Z7 Z8 (bottom row)
  • X_L == X0 X3 X6 (left column) == X2 X5 X8 (right column)

A ZZ merge therefore stacks the patches vertically (patch A on top, a 3-qubit seam row, patch B below): the four new Z checks that span the seam multiply out to Z_L(A) (x) Z_L(B). Seam qubits are prepared in |+> and split-measured in the X basis. Two X checks grow across the seam (A's bottom-boundary check and B's top-boundary check).

An XX merge places the patches side by side (A left, seam column, B right) with everything X<->Z dual: seams in |0>, Z-basis split, four new X checks multiplying to X_L(A) (x) X_L(B), two grown Z checks.

Merged code (either orientation): 21 data qubits, 20 independent checks, 1 logical qubit.

Supports below are given as (patch, index) tuples where patch is "A", "B" (data-qubit template index 0-8) or "S" (seam index 0-2). Tiles are 4-slot [NW, NE, SW, SE] lists (with None for boundary checks) matching the codepack's syndrome-extraction template slot order [a, b, c, d].

pymatching_merged_window_decode

pymatching_merged_window_decode(H: ndarray, syndrome_window: Sequence[Sequence[int]], prev_round: Sequence[int], fresh_rows: Sequence[int], fresh_start: int = 0, noisy_prev: bool = False, ref_rows: Sequence[int] = (), continuing_rows: dict[int, int] | None = None, settled_rows: dict[int, int] | None = None) -> np.ndarray

Decode a syndrome window on the 3D space-time graph of an arbitrary H.

Generalizes the codepack's global-measurement graph construction (node t * num_checks + row; weight-2 columns become spatial edges, weight-1 columns boundary edges, fault id = column; temporal edges weight 0.9) to any check matrix, so it can run on the 21-qubit merged code.

Parameters:

Name Type Description Default
H ndarray

Binary check matrix (num_checks x num_data). Columns must have weight 1 or 2 (true for surface-code CSS sectors).

required
syndrome_window Sequence[Sequence[int]]

Raw check outcomes, one length-num_checks row per round.

required
prev_round Sequence[int]

Baseline outcomes for the round preceding the window; the round-0 detector of a continuing check diffs against this.

required
fresh_rows Sequence[int]

Rows of checks first measured inside the window (the new seam checks). Their first measured round is a pure time boundary: no detector, no node. Consequences encoded in the graph: a fresh check's first-round reference absorbs one endpoint of any data error it watches in that round (so a column shared with a continuing check becomes a boundary edge on the continuing node), and a measurement error on it in that round is a boundary edge at its next-round node.

required
fresh_start int

Window layer at which the fresh rows are first measured. Before this layer fresh rows do not exist: their detectors are 0 and they participate in no edges (data columns they watch fall back to the continuing rows only, reproducing separate-patch physics for a window that extends back before the merge).

0
noisy_prev bool

Set when prev_round holds raw measured values (e.g. the first QEC round after a prep that leaves this check type random) rather than trusted values. A measurement flip in that baseline round leaves a single defect at layer 0; escape edges (measurement weight) let the matcher explain it without a spurious data correction.

False
ref_rows Sequence[int]

Rows whose prev_round value the CALLER consumes as a reference (e.g. the telescoped-to-canonical conversion checks). Their layer-0 escape edges carry fault ids num_data + 1 + k (k the position in ref_rows): when the matcher routes a layer-0 defect on such a row to the time boundary it is asserting the reference value itself was mismeasured, and the caller must flip its read. This keeps the observable consistent for BOTH interpretations of the ambiguous single-defect signature (baseline measurement flip vs. a data error just after the baseline that the matcher chose to absorb).

()
continuing_rows dict[int, int] | None

Rows -> the window layer at which each one's own pre-window history stops being reachable (e.g. a shallower-history second patch that only starts contributing partway through a window sized to a deeper block's reach). Unlike fresh_rows (checks with no prior existence at all), a continuing row DOES have real history before its start layer; prev_round there is trusted the same (untrusted, noisy_prev-style) way as at layer 0, just time-shifted. No node/edges before the start layer.

None
settled_rows dict[int, int] | None

Same shape as continuing_rows, but for a baseline that is CONFIRMED rather than noisy (e.g. a row an earlier merge's own repair already examined and took full responsibility for). No escape edge: a new defect after it must be explained by a real correction, not cheaply blamed on the settled baseline -- using continuing_rows here would let the matcher absorb a genuine new defect for free instead.

None
Notes

The correction is referenced to the LAST window round (the joint parity is read there): every spatial edge carries its column as a fault id since a data error at any window time flips the last round's seam-check product. Only FRESH rows get a future-boundary edge at the last round: a final-round seam measurement error is undetectable inside the window yet flips the recorded product, so that edge carries the virtual fault id num_data. Continuing rows get no future boundary — their final-round measurement errors are caught by the patch QEC that resumes after the split, and giving them a cheap escape here misprices staggered mid-window defects (patch check and seam check firing one round apart) whose correct matching must flip the parity.

Returns:

Type Description
ndarray

Length num_data + 1 binary indicator: data-error corrections plus the virtual observable flip at index num_data.