diff --git a/.gitignore b/.gitignore index 6a7a990c..5c9775de 100644 --- a/.gitignore +++ b/.gitignore @@ -225,3 +225,4 @@ cython_debug/ marimo/_static/ marimo/_lsp/ __marimo__/ +!test/data/**/*.stim diff --git a/CHANGELOG.md b/CHANGELOG.md index 37cf0ce3..8a1240c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 The fault distance of d=5 T-state cultivation (3, as found by [Chan et al.](https://arxiv.org/abs/2609.17706)) takes under a second, and that of its Z-flagged version (4) takes 5 s. +- **Determined measurements of non-Clifford circuits (experimental).** + `tsim.experimental.count_determined_measurements` and `missing_detectors` + work like Stim's methods of those names for circuits with T gates, either + exactly or from `shots` noiseless samples. `nondeterministic_detectors` lists + the declared detectors and observables that are random without noise, and + `missing_detectors` returns the parities of random detectors that stay + determined first, which repairs a circuit whose S gates became T gates. On + the d=5 cultivation circuits of [Chan et al.](https://arxiv.org/abs/2609.17706), + they find the 4 random detectors of the uncorrected circuit and none in the + corrected ones. ### Changed - The stabilizer-rank strategies compile components of the same shape once, and diff --git a/src/tsim/determined.py b/src/tsim/determined.py new file mode 100644 index 00000000..d2874321 --- /dev/null +++ b/src/tsim/determined.py @@ -0,0 +1,429 @@ +"""Parities of measurements that every noiseless shot gives the same value. + +These are the parities that can be detectors or observables. Stim finds them +for Clifford circuits with a tableau (``stim.Circuit.count_determined_measurements`` +and ``stim.Circuit.missing_detectors``); this module finds them for any circuit +that Tsim samples, in two ways. + +A parity ``c`` is determined when ``c · m`` is the same for every record ``m`` +that a noiseless shot can give, that is when ``c`` is orthogonal to the +directions ``m + m₀`` of the affine span of those records. The determined +parities are therefore a linear space, of dimension the number of measurements +minus that of the directions. + +- Exactly (``shots=None``): Tsim compiles the noiseless circuit to direct + outputs, which are constants, and statevector tapes, which are independent. + ``Branches.support`` walks both outcomes of each measurement of a tape, but + for fair coins that no rotation reads, which only add their output flips as + directions. A measurement outcome less likely than ``IMPOSSIBLE`` (1e-12) + given the ones before counts as impossible. +- From samples (``shots=n``): the directions of ``n`` noiseless shots. A parity + that flips in a fraction ``q`` of shots survives ``n`` shots with probability + ``(1 - q)^n``, so this is right with high confidence when ``n ≫ 1/q``, and it + needs no walk. + +With ``unknown_input=True`` the qubits start maximally mixed, which is a random +computational basis state: an ``X_ERROR(0.5)`` on every qubit at the start. +Its f-bits flip outputs (``A``) and rotation signs (``B``). Those in the kernel +of ``B`` only add their columns of ``A``; the exact path tries every +combination of the others and walks each tape once per distinct signs. +""" + +from __future__ import annotations + +import functools +import warnings +from typing import TYPE_CHECKING + +import numba +import numpy as np +import stim + +from tsim.compile.pipeline import compile_program +from tsim.core.graph import prepare_graph +from tsim.statevector.branches import Branches, gf2_product +from tsim.statevector.program import StatevectorComponent +from tsim.utils.gf2_span import Span, num_words, pack, pack_bytes, unpack + +if TYPE_CHECKING: + from tsim.circuit import Circuit + +MAX_SIGN_BITS = 16 +"""Most independent rotation signs that unknown inputs may set, exactly.""" + +BATCH = 1 << 16 +"""Shots drawn at a time.""" + + +def count_determined_measurements( + circuit: Circuit, + *, + unknown_input: bool = False, + shots: int | None = None, + seed: int | None = None, +) -> int: + """Count the measurements that earlier measurements predict, without noise. + + As ``stim.Circuit.count_determined_measurements``, for any circuit: the + number of independent parities of measurements that every noiseless shot + gives the same value. In a Clifford circuit that is the number of + measurements that are a parity of earlier ones, as in Stim. With + non-Clifford gates a measurement can also be a nonlinear function of + earlier ones, which this does not count, since no detector can use it. + + If ``circuit.num_detectors + circuit.num_observables`` is below the count, + some detectors may be missing (``missing_detectors``); if some declared + detectors are random, ``nondeterministic_detectors`` lists them. + + Args: + circuit: The circuit. Its noise is ignored. + unknown_input: If True, the qubits start in unknown random states + rather than in ``|0⟩``. + shots: If given, estimate from this many noiseless shots instead of + exactly; see the module docstring. + seed: The seed of the shots, if any. + + Returns: + The number of determined measurements. + + Raises: + ValueError: If the exact walk follows more than + ``tsim.statevector.branches.MAX_BRANCHES`` branches per + combination of inputs, or + unknown inputs set more than ``MAX_SIGN_BITS`` independent + rotation signs. Sampling avoids both. + + """ + directions = _directions(circuit, unknown_input, shots, seed) + return circuit.num_measurements - directions.rank + + +def missing_detectors( + circuit: Circuit, + *, + unknown_input: bool = False, + shots: int | None = None, + seed: int | None = None, +) -> stim.Circuit: + """Return detectors for the determined parities that no declaration covers. + + As ``stim.Circuit.missing_detectors``, for any circuit: the returned + ``DETECTOR`` instructions can be appended to ``circuit``, and then every + determined parity (``count_determined_measurements``) is a parity of its + determined detectors and observables. A random declared detector + (``nondeterministic_detectors``) covers nothing, but parities of random + detectors that are determined come first in the result, so it heals them: + if S gates became T gates and randomized detectors ``D1`` and ``D2`` while + ``D1 ⊕ D2`` stayed determined, that is a returned detector. Like Stim's, the + result is no particularly good basis otherwise. + + A Tsim detector is its raw parity, so a returned one whose noiseless value + is 1 fires in every shot: sample with + ``sample(..., use_detector_reference_sample=True)``. + + Args: + circuit: The circuit. Its noise is ignored. + unknown_input: As in ``count_determined_measurements``. + shots: As in ``count_determined_measurements``. + seed: As in ``count_determined_measurements``. + + Returns: + A circuit of ``DETECTOR`` instructions. + + """ + num_m = circuit.num_measurements + directions = _directions(circuit, unknown_input, shots, seed) + declared = _Declared(circuit.stim_circuit) + overlaps, random = declared.random(directions) + covered = Span(num_m) + covered.insert(np.delete(declared.words, random, axis=0)) + # NOTE: a parity of random declarations is determined when it overlaps no + # direction; the kernel's rows end at distinct declarations, so they pair + # each random one with earlier ones where they can. + kernel = Span(len(random)) + kernel.insert_bits(unpack(overlaps[random], directions.rank).T) + combinations = unpack(kernel.annihilator(), len(random)).astype(bool) + healed = np.zeros((len(combinations), num_words(num_m)), dtype=np.uint64) + for i, combination in enumerate(combinations): + healed[i] = np.bitwise_xor.reduce(declared.words[random[combination]], axis=0) + candidates = np.concatenate([healed, directions.annihilator()]) + result = stim.Circuit() + for words in candidates[covered.insert(candidates)]: + records = np.flatnonzero(unpack(words[None], num_m)[0]) + result.append("DETECTOR", [stim.target_rec(int(m) - num_m) for m in records]) + return result + + +def nondeterministic_detectors( + circuit: Circuit, + *, + unknown_input: bool = False, + shots: int | None = None, + seed: int | None = None, +) -> list[stim.DemTarget]: + """Return the declared detectors and observables that are random without noise. + + Stim refuses such a circuit when it makes a detector error model. With + non-Clifford gates they are common: replacing the S gates of a Clifford + proxy by T gates randomizes some of its detectors. A declared parity is + random when it is not a determined parity (``count_determined_measurements``). + + Args: + circuit: The circuit. Its noise is ignored. + unknown_input: As in ``count_determined_measurements``. + shots: As in ``count_determined_measurements``; then a listed detector + is certainly random, but an unlisted one may be random rarely. + seed: As in ``count_determined_measurements``. + + Returns: + The random detectors (``stim.target_relative_detector_id``) and then + the random observables (``stim.target_logical_observable_id``). + + """ + directions = _directions(circuit, unknown_input, shots, seed) + _, random = _Declared(circuit.stim_circuit).random(directions) + num_detectors = circuit.num_detectors + return [ + ( + stim.target_relative_detector_id(index) + if index < num_detectors + else stim.target_logical_observable_id(index - num_detectors) + ) + for index in random.tolist() + ] + + +class _Declared: + """The measurements of each detector and then each observable. + + Attributes: + starts: Where each declaration's measurements start in ``records``, + and the end. + records: The measurements of each declaration, sorted. + words: Each declaration as a packed vector over the measurements. + + """ + + def __init__(self, circuit: stim.Circuit) -> None: + num_m = circuit.num_measurements + num_detectors = circuit.num_detectors + # NOTE: a measurement that a declaration names twice cancels. + members: list[set[int]] = [ + set() for _ in range(num_detectors + circuit.num_observables) + ] + measured = detector = 0 + for instruction in circuit.flattened(): + assert isinstance(instruction, stim.CircuitInstruction) + if instruction.name == "DETECTOR": + row = detector + detector += 1 + elif instruction.name == "OBSERVABLE_INCLUDE": + row = num_detectors + int(instruction.gate_args_copy()[0]) + else: + measured += instruction.num_measurements + continue + for target in instruction.targets_copy(): + if not target.is_measurement_record_target: + raise ValueError( + f"{instruction.name} with a non-record target ({target}) " + "is not supported" + ) + members[row] ^= {measured + target.value} + sizes = [len(records) for records in members] + self.starts = np.cumsum([0, *sizes], dtype=np.int64) + self.records = np.array( + [m for records in members for m in sorted(records)], dtype=np.int64 + ) + self.words = np.zeros((len(members), num_words(num_m)), dtype=np.uint64) + np.bitwise_or.at( + self.words, + (np.repeat(np.arange(len(members)), sizes), self.records // 64), + np.uint64(1) << (self.records % 64).astype(np.uint64), + ) + + def random(self, directions: Span) -> tuple[np.ndarray, np.ndarray]: + """Return which directions each declaration flips, packed, and the random ones.""" + overlaps = _overlaps(directions.basis(), self.starts, self.records) + return overlaps, np.flatnonzero(overlaps.any(axis=1)) + + +@numba.njit(cache=True) +def _overlaps( + directions: np.ndarray, starts: np.ndarray, records: np.ndarray +) -> np.ndarray: + result = np.zeros((len(starts) - 1, max(1, -(-len(directions) // 64))), np.uint64) + for d in range(len(directions)): + word, bit = d // 64, np.uint64(1) << np.uint64(d % 64) + for k in range(len(starts) - 1): + parity = np.uint64(0) + for m in records[starts[k] : starts[k + 1]]: + parity ^= directions[d, m // 64] >> np.uint64(m % 64) + if parity & np.uint64(1): + result[k, word] |= bit + return result + + +def _directions( + circuit: Circuit, unknown_input: bool, shots: int | None, seed: int | None +) -> Span: + """Return the span of ``m + m₀`` over the noiseless records ``m``. + + The span is cached, since every function here needs it and compiling is + slow, but not from shots without a seed. Callers must not change it. + """ + text = str(circuit.without_noise().without_annotations().stim_circuit) + if shots is None: + return _directions_of(text, unknown_input, None, None)[0] + if shots < 1: + raise ValueError(f"shots must be at least 1, not {shots}") + find = _directions_of if seed is not None else _directions_of.__wrapped__ + span, last_growth = find(text, unknown_input, shots, seed) + if last_growth > shots // 2: + warnings.warn( + f"shot {last_growth} of {shots} still found a new direction, so some " + "parities that look determined may be random; use more shots", + stacklevel=3, + ) + return span + + +@functools.lru_cache(maxsize=8) +def _directions_of( + text: str, unknown_input: bool, shots: int | None, seed: int | None +) -> tuple[Span, int]: + """Return the directions, and the last shot that grew them, if sampled.""" + from tsim.circuit import Circuit + + noiseless = Circuit(stim.Circuit(text)) + if unknown_input and noiseless.num_qubits: + mixed = stim.Circuit() + mixed.append("X_ERROR", range(noiseless.num_qubits), 0.5) + noiseless = Circuit.from_stim_program(mixed) + noiseless + if shots is None: + return _exact_directions(noiseless), 0 + return _sampled_directions(noiseless, shots, seed) + + +def _sampled_directions( + circuit: Circuit, shots: int, seed: int | None +) -> tuple[Span, int]: + """Return the span of ``m + m₀`` over ``shots`` noiseless shots ``m``. + + Also return the last shot that grew it. + """ + num_m = circuit.num_measurements + span = Span(num_m) + sampler = circuit.compile_sampler(seed=seed) + reference = None + last_growth = 0 + for done in range(0, shots, BATCH): + count = min(BATCH, shots - done) + words = pack_bytes(sampler.sample(count, bit_packed=True), num_m) + if reference is None: + reference = words[0].copy() + rows, first = np.unique(words ^ reference, axis=0, return_index=True) + # NOTE: in shot order, so that ``last_growth`` is the last shot that grew it. + order = np.argsort(first) + grew = span.insert(rows[order]) + if grew.any(): + last_growth = done + int(first[order][grew].max()) + 1 + return span, last_growth + + +def _exact_directions(circuit: Circuit) -> Span: + """Return the span of ``m + m₀`` over every noiseless record ``m``.""" + prepared = prepare_graph(circuit, sample_detectors=False) + program = compile_program( + prepared, mode="sequential", strategy="active_statevector" + ) + num_m = prepared.num_outputs + num_f = prepared.error_transform.shape[0] + span = Span(num_m) + + # NOTE: the f-bits, set only by unknown inputs, are fair and independent. + output_map = np.zeros((num_m, num_f), dtype=np.uint8) + constant = np.zeros(num_m, dtype=np.uint8) + direct = program.output_order[: len(program.direct_f_indices)] + constant[direct] = program.direct_flips + reads = program.direct_f_indices >= 0 + output_map[direct[reads], program.direct_f_indices[reads]] = 1 + tapes: list[tuple[Branches, np.ndarray]] = [] + sign_maps = [np.zeros((0, num_f), dtype=np.uint8)] + for component in program.components: + assert isinstance(component, StatevectorComponent) + branches = Branches(component, num_detectors=0) + outputs = np.asarray(component.output_indices, dtype=np.int64) + tapes.append((branches, outputs)) + output_map[np.ix_(outputs, branches.f_selection)] ^= branches.output_rows + sign_map = np.zeros((len(branches.sign_rows), num_f), dtype=np.uint8) + sign_map[:, branches.f_selection] = branches.sign_rows + sign_maps.append(sign_map) + span.insert_bits(_embed(branches.coin_rows, outputs, num_m)) + sign_map = np.concatenate(sign_maps) + span.insert_bits(gf2_product(_kernel(sign_map), output_map)) + + choices = _independent_columns(sign_map) + if len(choices) > MAX_SIGN_BITS: + raise ValueError( + f"unknown inputs set {len(choices)} independent rotation signs, more " + f"than {MAX_SIGN_BITS}; pass shots to sample instead" + ) + f = np.zeros((1 << len(choices), num_f), dtype=np.uint8) + f[:, choices] = np.arange(len(f))[:, None] >> np.arange(len(choices)) & 1 + + # NOTE: a tape's outputs are its f-bits' flips XOR a value that depends + # only on its rotation signs: walk each distinct signs once. + bases = [] + for branches, outputs in tapes: + local = f[:, branches.f_selection] + first, inverse = _groups(gf2_product(local, branches.sign_rows)) + try: + row, values = branches.support(local[first]) + except ValueError as error: + raise ValueError(f"{error}; pass shots to sample instead") from error + firsts = values[np.searchsorted(row, np.arange(len(first)))] + within = Span(len(outputs)) + within.insert_bits(values ^ firsts[row]) + span.insert_bits(_embed(unpack(within.basis(), len(outputs)), outputs, num_m)) + base = firsts ^ gf2_product(local[first], branches.output_rows) + bases.append((inverse, base, outputs)) + points = gf2_product(f, output_map) ^ constant + for inverse, base, outputs in bases: + points[:, outputs] ^= base[inverse] + span.insert_bits(points ^ points[0]) + return span + + +def _groups(rows: np.ndarray) -> tuple[np.ndarray, np.ndarray]: + """Return the first index of each distinct row, and each row's group.""" + if rows.shape[1] == 0: + return np.zeros(1, dtype=np.int64), np.zeros(len(rows), dtype=np.int64) + _, first, inverse = np.unique(rows, axis=0, return_index=True, return_inverse=True) + return first, inverse.ravel() + + +def _embed(local: np.ndarray, outputs: np.ndarray, num_m: int) -> np.ndarray: + """Return rows over a tape's outputs as rows over all ``num_m`` measurements.""" + rows = np.zeros((len(local), num_m), dtype=np.uint8) + rows[:, outputs] = local + return rows + + +def _kernel(matrix: np.ndarray) -> np.ndarray: + """Return a basis of the vectors ``v`` with ``matrix @ v = 0``, as 0/1 rows.""" + span = Span(matrix.shape[1]) + span.insert_bits(matrix) + return unpack(span.annihilator(), matrix.shape[1]) + + +def _independent_columns(matrix: np.ndarray) -> np.ndarray: + """Return columns of ``matrix`` that form a basis of its column space.""" + span = Span(matrix.shape[0]) + return np.flatnonzero(span.insert(pack(matrix.T))) + + +__all__ = [ + "count_determined_measurements", + "missing_detectors", + "nondeterministic_detectors", +] diff --git a/src/tsim/experimental.py b/src/tsim/experimental.py index 1da6fef2..61eb335f 100644 --- a/src/tsim/experimental.py +++ b/src/tsim/experimental.py @@ -10,8 +10,17 @@ - `shortest_errors` finds a circuit's fault distance: every smallest set of faults that flips an observable without flipping a detector, as `LogicalError`s of `Fault`s. +- `count_determined_measurements`, `missing_detectors` and + `nondeterministic_detectors` find the parities of measurements that every + noiseless shot gives the same value, as Stim's methods of those names do for + Clifford circuits, and the declared detectors that are random. """ +from tsim.determined import ( + count_determined_measurements as count_determined_measurements, + missing_detectors as missing_detectors, + nondeterministic_detectors as nondeterministic_detectors, +) from tsim.noise.faults import ( Fault as Fault, LogicalError as LogicalError, @@ -29,5 +38,8 @@ "Fault", "LogicalError", "SteaneEncoder", + "count_determined_measurements", + "missing_detectors", + "nondeterministic_detectors", "shortest_errors", ] diff --git a/src/tsim/statevector/branches.py b/src/tsim/statevector/branches.py index 621e9b28..d8b56dd1 100644 --- a/src/tsim/statevector/branches.py +++ b/src/tsim/statevector/branches.py @@ -27,7 +27,7 @@ """Most measurement branches that one row of f-bits may have.""" IMPOSSIBLE = 1e-12 -"""Weight below which a measurement outcome cannot happen.""" +"""Probability below which a measurement outcome cannot happen.""" _I_POWERS = (1, 1j, -1, -1j) @@ -66,6 +66,9 @@ class Branches: sign_rows: The component f-bits that each rotation's sign reads. output_rows: The component f-bits that each output slot reads. observable: Whether each output slot is an observable. + coin_rows: The output slots that each fair coin flips, for the coins + that ``support`` fixes at 0: dormant measurements that no + rotation reads. """ @@ -88,6 +91,13 @@ def __init__(self, component: StatevectorComponent, num_detectors: int) -> None: self._outputs = np.asarray(component.output_indices, dtype=np.int64) self._num_detectors = num_detectors self.observable = self._outputs >= num_detectors + # NOTE: a dormant measurement leaves the amplitudes alone; if no rotation + # reads it either, it only flips outputs, independently of the rest. + dormant = np.flatnonzero(tape.kind == MEASURE_DORMANT) + unread = ~np.asarray(self._sign_b.any(axis=0))[self._branch[dormant]] + self._coins = np.zeros(len(tape.kind), dtype=bool) + self._coins[dormant[unread]] = True + self.coin_rows = self._out_b[:, self._branch[dormant[unread]]].T @cached_property def reference(self) -> np.ndarray: @@ -136,39 +146,72 @@ def outcomes(self, f: np.ndarray) -> list[tuple[np.ndarray, np.ndarray]]: """ f = np.asarray(f, dtype=np.uint8) flips = gf2_product(f, self.output_rows) ^ self.reference - rows, weights, deviations = [], [], [] + row, weight, outputs = self._collect(f, skip_coins=False) + deviation = outputs ^ flips[row] + bounds = np.searchsorted(row, np.arange(len(f) + 1)) + result = [] + for r in range(len(f)): + mine = slice(bounds[r], bounds[r + 1]) + values, group = np.unique(deviation[mine], axis=0, return_inverse=True) + probability = np.zeros(len(values)) + np.add.at(probability, group.ravel(), weight[mine]) + result.append((values, probability)) + return result + + def support(self, f: np.ndarray) -> tuple[np.ndarray, np.ndarray]: + """Return the output values that each row of f-bits can give. + + The walk fixes the coins of ``coin_rows`` at 0: each of them flips the + outputs by its row of ``coin_rows`` with probability 1/2, whatever + else happens. + + Args: + f: The component's f-bits, shape ``(rows, len(f_selection))``. + + Returns: + The row of ``f`` of each possible outcome, in increasing order, + and its output values, shape ``(outcomes, output slots)``. + + Raises: + ValueError: If the walk has more than ``MAX_BRANCHES`` branches + per row. + + """ + row, _, outputs = self._collect(np.asarray(f, dtype=np.uint8), skip_coins=True) + return row, outputs + + def _collect( + self, f: np.ndarray, *, skip_coins: bool + ) -> tuple[np.ndarray, np.ndarray, np.ndarray]: + """Return every branch's row, probability and outputs, sorted by row.""" + rows, weights, outputs = [], [], [] leaves = 0 - for row, weight, outputs in self._leaves(f): + for row, weight, out in self._leaves(f, skip_coins=skip_coins): + rows.append(row) + weights.append(weight) + outputs.append(out) leaves += len(row) if leaves > MAX_BRANCHES * len(f): raise ValueError( f"the tape has more than {MAX_BRANCHES} measurement branches" ) - rows.append(row) - weights.append(weight) - deviations.append(outputs ^ flips[row]) row = np.concatenate(rows) - weight = np.concatenate(weights) - deviation = np.concatenate(deviations) order = np.argsort(row, kind="stable") - bounds = np.searchsorted(row[order], np.arange(len(f) + 1)) - result = [] - for r in range(len(f)): - mine = order[bounds[r] : bounds[r + 1]] - values, group = np.unique(deviation[mine], axis=0, return_inverse=True) - probability = np.zeros(len(values)) - np.add.at(probability, group.ravel(), weight[mine]) - result.append((values, probability)) - return result + return ( + row[order], + np.concatenate(weights)[order], + np.concatenate(outputs)[order], + ) def _leaves( - self, f: np.ndarray + self, f: np.ndarray, *, skip_coins: bool = False ) -> Iterator[tuple[np.ndarray, np.ndarray, np.ndarray]]: """Yield the branches that reach the end of the tape, in batches. A batch is the row of ``f`` of each branch, its probability and its - output values. + output values. With ``skip_coins``, the coins of ``coin_rows`` land 0. """ + skip = self._coins if skip_coins else np.zeros_like(self._coins) tape = self._tape sign = np.zeros((len(f), len(tape.kind)), dtype=np.uint8) sign[:, self._rotations] = gf2_product(f, self.sign_rows) @@ -180,7 +223,7 @@ def _leaves( amps = np.zeros((len(row), self._size), dtype=np.complex128) amps[:, 0] = 1.0 bits = np.zeros((len(row), self._out_b.shape[1]), dtype=np.uint8) - yield from self._walk(0, amps, row, bits, sign, out) + yield from self._walk(0, amps, row, bits, sign, out, skip) def _walk( self, @@ -190,16 +233,19 @@ def _walk( bits: np.ndarray, sign: np.ndarray, out: np.ndarray, + skip: np.ndarray, ) -> Iterator[tuple[np.ndarray, np.ndarray, np.ndarray]]: """Run the actions from ``start`` on lanes and yield those that finish. Lane ``i`` has amplitudes ``amps[i]``, whose squared norm is its probability, belongs to row ``row[i]`` and has branch bits ``bits[i]``. ``sign`` and ``out`` hold the rows' f-bit parts of the action signs and - of the outputs. + of the outputs. The actions of ``skip`` do not run. """ tape = self._tape for a in range(start, len(tape.kind)): + if skip[a]: + continue x, z = int(tape.x_mask[a]), int(tape.z_mask[a]) # NOTE: the Pauli P = i^|x&z| X^x Z^z takes amplitudes ``amps`` to # ``amps[:, source] * phase``. @@ -213,13 +259,17 @@ def _walk( ) continue children = [] + weight = _weights(amps) for outcome in (0, 1): if tape.kind[a] == MEASURE_DORMANT: kept, child = np.arange(len(row)), amps * np.sqrt(0.5) else: moved = amps[:, source] * phase projected = (amps - moved if outcome else amps + moved) / 2 - kept = np.flatnonzero(_weights(projected) > IMPOSSIBLE) + # NOTE: an outcome is impossible given the lane, whatever + # the lane's own weight. + possible = _weights(projected) > IMPOSSIBLE * weight + kept = np.flatnonzero(possible) projected = projected[kept] # NOTE: the planner moved the measured Pauli onto the pivot, # which ends in |0>, as in ``shared._project_range``. @@ -240,7 +290,7 @@ def _walk( half = len(row) // 2 for part in (slice(0, half), slice(half, None)): yield from self._walk( - a + 1, amps[part], row[part], bits[part], sign, out + a + 1, amps[part], row[part], bits[part], sign, out, skip ) return yield row, _weights(amps), out[row] ^ _bit_parity(bits, self._out_b) diff --git a/src/tsim/utils/gf2_span.py b/src/tsim/utils/gf2_span.py new file mode 100644 index 00000000..1072c719 --- /dev/null +++ b/src/tsim/utils/gf2_span.py @@ -0,0 +1,149 @@ +"""A growing span of GF(2) vectors, packed into 64-bit words. + +``Span`` keeps one basis vector per pivot, the lowest set bit of that vector, +so inserting a vector reduces it by at most one basis vector per pivot, as +``linalg.find_basis`` does with Python integers. Its annihilator, the vectors +orthogonal to the whole span, then has one basis vector per column that is no +pivot: that column plus the pivots before it. +""" + +from __future__ import annotations + +import numba +import numpy as np + + +def num_words(width: int) -> int: + """Return the 64-bit words of a packed vector of ``width`` bits.""" + return max(1, -(-width // 64)) + + +def pack(bits: np.ndarray) -> np.ndarray: + """Return rows of 0/1 entries as rows of little-endian 64-bit words.""" + bits = np.asarray(bits, dtype=np.uint8) + rows, width = bits.shape + padded = np.zeros((rows, num_words(width) * 64), dtype=np.uint8) + padded[:, :width] = bits + packed = np.packbits(padded, axis=1, bitorder="little") + return packed.view(" np.ndarray: + """Return little-endian bit-packed bytes, ``width`` bits a row, as 64-bit words.""" + padded = np.zeros((len(rows), num_words(width) * 8), dtype=np.uint8) + padded[:, : rows.shape[1]] = rows + return padded.view(" np.ndarray: + """Return rows of 64-bit words as rows of ``width`` 0/1 entries.""" + as_bytes = np.ascontiguousarray(words).view(np.uint8) + return np.unpackbits(as_bytes, axis=1, bitorder="little")[:, :width] + + +@numba.njit(cache=True) +def _lowest(row: np.ndarray) -> int: + for w in range(len(row)): + word = row[w] + if word: + bit = 0 + while not (word >> np.uint64(bit)) & np.uint64(1): + bit += 1 + return 64 * w + bit + return -1 + + +@numba.njit(cache=True) +def _has_bit(row: np.ndarray, column: int) -> bool: + return bool((row[column // 64] >> np.uint64(column % 64)) & np.uint64(1)) + + +@numba.njit(cache=True) +def _insert(basis: np.ndarray, has: np.ndarray, rows: np.ndarray) -> np.ndarray: + """Insert ``rows`` and return, for each, whether it grew the span.""" + grew = np.zeros(len(rows), dtype=np.bool_) + v = np.empty(basis.shape[1], dtype=np.uint64) + for i in range(len(rows)): + v[:] = rows[i] + while True: + p = _lowest(v) + if p < 0: + break + if has[p]: + v ^= basis[p] + else: + basis[p] = v + has[p] = True + grew[i] = True + break + return grew + + +@numba.njit(cache=True) +def _annihilator(basis: np.ndarray, has: np.ndarray) -> np.ndarray: + """Return, per column j that is no pivot, e_j plus the pivots before it, packed.""" + width = len(has) + reduced = basis.copy() + # NOTE: clearing each pivot column from the other basis vectors, from the + # last pivot down, leaves each with its pivot and columns that are none. + for p in range(width - 1, -1, -1): + if has[p]: + for q in range(p): + if has[q] and _has_bit(reduced[q], p): + reduced[q] ^= reduced[p] + slot = np.full(width, -1, dtype=np.int64) + count = 0 + for j in range(width): + if not has[j]: + slot[j] = count + count += 1 + result = np.zeros((count, basis.shape[1]), dtype=np.uint64) + for j in range(width): + if slot[j] >= 0: + result[slot[j], j // 64] |= np.uint64(1) << np.uint64(j % 64) + for p in range(width): + if has[p]: + for j in range(p + 1, width): + if slot[j] >= 0 and _has_bit(reduced[p], j): + result[slot[j], p // 64] |= np.uint64(1) << np.uint64(p % 64) + return result + + +class Span: + """The span of the vectors inserted so far, in ``GF(2)^width``.""" + + def __init__(self, width: int) -> None: + """Start with the zero span.""" + self.width = width + self._basis = np.zeros((width, num_words(width)), dtype=np.uint64) + self._has = np.zeros(width, dtype=np.bool_) + + @property + def rank(self) -> int: + """Return the dimension of the span.""" + return int(self._has.sum()) + + def basis(self) -> np.ndarray: + """Return a basis of the span, packed.""" + return self._basis[self._has] + + def insert(self, words: np.ndarray) -> np.ndarray: + """Insert packed vectors and return, for each, whether it grew the span.""" + return _insert(self._basis, self._has, self._rows(words)) + + def insert_bits(self, bits: np.ndarray) -> np.ndarray: + """Insert vectors of 0/1 entries; see ``insert``.""" + bits = np.asarray(bits, dtype=np.uint8) + return self.insert(pack(bits[None] if bits.ndim == 1 else bits)) + + def annihilator(self) -> np.ndarray: + """Return a basis of the vectors orthogonal to the span, packed. + + Row ``i`` is a column ``j`` that is no pivot plus pivots before ``j``, + so the rows end at distinct columns, in increasing order. + """ + return _annihilator(self._basis, self._has) + + def _rows(self, words: np.ndarray) -> np.ndarray: + words = np.ascontiguousarray(words, dtype=np.uint64) + return words.reshape(-1, num_words(self.width)) diff --git a/test/data/determined/README.md b/test/data/determined/README.md new file mode 100644 index 00000000..83381a96 --- /dev/null +++ b/test/data/determined/README.md @@ -0,0 +1,9 @@ +# A cultivation circuit with T gates + +`chan_d5a19_uncorrected_T.stim`, for +`test/integration/test_determined_literature.py`, is a noiseless d=5 magic +state cultivation circuit from Chan, Strikis, Sun, Cai, arXiv:2609.17706, +[timchan0/cliffordea](https://github.com/timchan0/cliffordea) +`sim/stim_files/` at `27385c6` (MIT): an S proxy with S/S_DAG swapped for +T/T_DAG as in `circuit_text.swap_t_and_s_gates`, before the paper's d=5 +correction (`sim/d5-correction.md`). diff --git a/test/data/determined/chan_d5a19_uncorrected_T.stim b/test/data/determined/chan_d5a19_uncorrected_T.stim new file mode 100644 index 00000000..8791f574 --- /dev/null +++ b/test/data/determined/chan_d5a19_uncorrected_T.stim @@ -0,0 +1,371 @@ +QUBIT_COORDS(0, 0) 0 +QUBIT_COORDS(0, 1) 1 +QUBIT_COORDS(1, 0) 2 +QUBIT_COORDS(1, 1) 3 +QUBIT_COORDS(1, 2) 4 +QUBIT_COORDS(1, 3) 5 +QUBIT_COORDS(2, 0) 6 +QUBIT_COORDS(2, 1) 7 +QUBIT_COORDS(2, 2) 8 +QUBIT_COORDS(2, 3) 9 +QUBIT_COORDS(2, 4) 10 +QUBIT_COORDS(3, 0) 11 +QUBIT_COORDS(3, 1) 12 +QUBIT_COORDS(3, 2) 13 +QUBIT_COORDS(3, 3) 14 +QUBIT_COORDS(3, 4) 15 +QUBIT_COORDS(3, 5) 16 +QUBIT_COORDS(4, 0) 17 +QUBIT_COORDS(4, 1) 18 +QUBIT_COORDS(4, 2) 19 +QUBIT_COORDS(4, 3) 20 +QUBIT_COORDS(4, 4) 21 +QUBIT_COORDS(4, 5) 22 +QUBIT_COORDS(4, 6) 23 +QUBIT_COORDS(5, 0) 24 +QUBIT_COORDS(5, 1) 25 +QUBIT_COORDS(5, 2) 26 +QUBIT_COORDS(5, 3) 27 +QUBIT_COORDS(5, 4) 28 +QUBIT_COORDS(5, 5) 29 +QUBIT_COORDS(5, 6) 30 +QUBIT_COORDS(6, 0) 31 +QUBIT_COORDS(6, 1) 32 +QUBIT_COORDS(6, 2) 33 +QUBIT_COORDS(6, 3) 34 +QUBIT_COORDS(6, 4) 35 +QUBIT_COORDS(7, 0) 36 +QUBIT_COORDS(7, 1) 37 +QUBIT_COORDS(7, 2) 38 +QUBIT_COORDS(7, 3) 39 +QUBIT_COORDS(8, 0) 40 +QUBIT_COORDS(8, 1) 41 +R 12 8 6 +RX 7 11 13 3 2 4 18 0 5 17 +TICK +CX 7 12 11 6 13 8 +TICK +CX 7 8 11 12 +TICK +CX 7 6 13 12 +TICK +CX 2 6 4 8 18 12 +TICK +CX 6 2 8 4 12 18 +TICK +CX 3 4 17 18 +TICK +CX 3 2 18 17 +TICK +CX 4 3 +TICK +CX 2 3 +TICK +T 3 +TICK +CX 2 3 +TICK +CX 4 3 +TICK +CX 0 2 5 4 +TICK +CX 2 0 4 5 +TICK +R 6 18 8 9 +RX 2 12 4 +TICK +CX 2 6 4 8 5 9 12 18 +TICK +CX 2 0 6 11 8 13 9 5 12 7 +TICK +CX 2 3 6 7 8 9 12 13 +TICK +CX 4 3 8 7 12 11 18 17 +TICK +CX 3 4 7 8 11 12 17 18 +TICK +CX 0 2 7 12 11 6 13 8 +TICK +CX 3 2 7 6 9 8 13 12 +TICK +CX 2 6 4 8 12 18 +TICK +M 6 18 8 5 +MX 2 12 4 +DETECTOR(2, 0, 0) rec[-7] +DETECTOR(4, 1, 0) rec[-6] +DETECTOR(2, 2, 0) rec[-5] +DETECTOR(1, 3, 0) rec[-4] +DETECTOR(1, 0, 0) rec[-3] +DETECTOR(3, 1, 0) rec[-2] +DETECTOR(1, 2, 0) rec[-1] +TICK +RX 18 12 6 2 8 1 +TICK +T 3 11 13 +T_DAG 0 7 9 17 +TICK +CX 1 0 2 3 6 7 8 9 12 11 18 17 +TICK +CX 3 1 7 8 12 18 +TICK +CX 7 3 12 13 +TICK +CX 7 12 +TICK +MX 7 +DETECTOR(1, 2, 1) rec[-1] rec[-2] rec[-3] +TICK +RX 7 +TICK +CX 7 12 +TICK +CX 7 3 12 13 +TICK +CX 3 1 7 8 12 18 +TICK +CX 1 0 2 3 6 7 8 9 12 11 18 17 +TICK +T 0 7 9 17 +T_DAG 3 11 13 +TICK +MX 18 12 6 2 8 1 +DETECTOR(4, 1, 2) rec[-6] +DETECTOR(3, 1, 2) rec[-5] +DETECTOR(2, 1, 2) rec[-4] rec[-7] +DETECTOR(1, 0, 2) rec[-3] +DETECTOR(2, 2, 2) rec[-2] +DETECTOR(0, 1, 2) rec[-1] +TICK +R 19 25 32 38 40 23 15 22 21 29 34 +RX 26 33 36 16 28 27 +TICK +CX 16 22 26 19 27 34 28 21 33 38 36 40 +TICK +CX 16 15 22 23 26 25 28 29 33 32 +TICK +CX 15 16 23 22 25 26 29 28 32 33 +TICK +M 26 33 28 22 16 +DETECTOR(5, 2, 3) rec[-5] +DETECTOR(6, 2, 3) rec[-4] +DETECTOR(5, 4, 3) rec[-3] +DETECTOR(4, 5, 3) rec[-2] +DETECTOR(3, 5, 3) rec[-1] +TICK +R 41 35 33 31 22 20 18 8 6 +RX 37 28 26 24 16 14 12 4 2 +TICK +CX 2 6 4 8 12 18 14 20 16 22 24 31 26 33 28 35 37 41 +TICK +CX 3 4 7 8 11 12 13 14 15 16 17 18 19 20 21 22 25 26 27 28 32 33 34 35 36 37 40 41 +TICK +CX 3 2 7 6 9 8 13 12 15 14 19 18 21 20 23 22 25 24 27 26 29 28 32 31 34 33 38 37 +TICK +CX 0 2 7 12 9 14 11 6 13 8 17 24 19 26 21 28 25 18 27 20 29 22 32 37 36 31 38 33 +TICK +CX 4 3 8 7 12 11 14 13 16 15 18 17 20 19 22 21 26 25 28 27 33 32 35 34 37 36 41 40 +TICK +CX 2 3 6 7 8 9 12 13 14 15 18 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 37 38 +TICK +CX 2 0 6 11 8 13 12 7 14 9 18 25 20 27 22 29 24 17 26 19 28 21 31 36 33 38 37 32 +TICK +CX 2 6 4 8 12 18 14 20 16 22 24 31 26 33 28 35 37 41 +TICK +M 41 35 33 31 22 20 18 8 6 +MX 37 28 26 24 16 14 12 4 2 +DETECTOR(8, 1, 4) rec[-18] +DETECTOR(6, 4, 4) rec[-17] +DETECTOR(6, 2, 4) rec[-16] +DETECTOR(4, 5, 4) rec[-14] +DETECTOR(4, 1, 4) rec[-12] +DETECTOR(2, 2, 4) rec[-11] +DETECTOR(2, 0, 4) rec[-10] +DETECTOR(7, 1, 4) rec[-9] +DETECTOR(5, 4, 4) rec[-8] +DETECTOR(5, 2, 4) rec[-7] +DETECTOR(3, 5, 4) rec[-5] +DETECTOR(2, 1, 4) rec[-3] rec[-30] +DETECTOR(1, 2, 4) rec[-2] rec[-30] +DETECTOR(1, 0, 4) rec[-1] rec[-30] +TICK +R 41 35 33 31 22 20 18 8 6 +RX 37 28 26 24 16 14 12 4 2 +TICK +CX 2 6 4 8 12 18 14 20 16 22 24 31 26 33 28 35 37 41 +TICK +CX 3 4 7 8 11 12 13 14 15 16 17 18 19 20 21 22 25 26 27 28 32 33 34 35 36 37 40 41 +TICK +CX 3 2 7 6 9 8 13 12 15 14 19 18 21 20 23 22 25 24 27 26 29 28 32 31 34 33 38 37 +TICK +CX 0 2 7 12 9 14 11 6 13 8 17 24 19 26 21 28 25 18 27 20 29 22 32 37 36 31 38 33 +TICK +CX 4 3 8 7 12 11 14 13 16 15 18 17 20 19 22 21 26 25 28 27 33 32 35 34 37 36 41 40 +TICK +CX 2 3 6 7 8 9 12 13 14 15 18 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 37 38 +TICK +CX 2 0 6 11 8 13 12 7 14 9 18 25 20 27 22 29 24 17 26 19 28 21 31 36 33 38 37 32 +TICK +CX 2 6 4 8 12 18 14 20 16 22 24 31 26 33 28 35 37 41 +TICK +M 41 35 33 31 22 20 18 8 6 +MX 37 28 26 24 16 14 12 4 2 +DETECTOR(8, 1, 5) rec[-18] +DETECTOR(6, 2, 5) rec[-17] rec[-34] +DETECTOR(6, 0, 5) rec[-16] rec[-33] rec[-35] +DETECTOR(6, 0.5, 5) rec[-15] rec[-33] rec[-34] +DETECTOR(4, 3, 5) rec[-14] rec[-31] +DETECTOR(4, 1, 5) rec[-13] rec[-30] rec[-32] +DETECTOR(4, 2, 5) rec[-12] rec[-31] +DETECTOR(2, 0, 5) rec[-11] rec[-28] +DETECTOR(2, 2, 5) rec[-10] rec[-28] rec[-29] +DETECTOR(7, 1, 5) rec[-9] rec[-27] +DETECTOR(5, 4, 5) rec[-8] rec[-26] +DETECTOR(5, 2, 5) rec[-7] rec[-25] +DETECTOR(5, 0, 5) rec[-6] rec[-24] +DETECTOR(3, 5, 5) rec[-5] rec[-23] +DETECTOR(3, 3, 5) rec[-4] rec[-22] +DETECTOR(3, 1, 5) rec[-3] rec[-21] +DETECTOR(1, 2, 5) rec[-2] rec[-20] +DETECTOR(1, 0, 5) rec[-1] rec[-19] +TICK +R 41 35 33 31 22 20 18 8 6 +RX 37 28 26 24 16 14 12 4 2 +TICK +CX 2 6 4 8 12 18 14 20 16 22 24 31 26 33 28 35 37 41 +TICK +CX 3 4 7 8 11 12 13 14 15 16 17 18 19 20 21 22 25 26 27 28 32 33 34 35 36 37 40 41 +TICK +CX 3 2 7 6 9 8 13 12 15 14 19 18 21 20 23 22 25 24 27 26 29 28 32 31 34 33 38 37 +TICK +CX 0 2 7 12 9 14 11 6 13 8 17 24 19 26 21 28 25 18 27 20 29 22 32 37 36 31 38 33 +TICK +CX 4 3 8 7 12 11 14 13 16 15 18 17 20 19 22 21 26 25 28 27 33 32 35 34 37 36 41 40 +TICK +CX 2 3 6 7 8 9 12 13 14 15 18 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 37 38 +TICK +CX 2 0 6 11 8 13 12 7 14 9 18 25 20 27 22 29 24 17 26 19 28 21 31 36 33 38 37 32 +TICK +CX 2 6 4 8 12 18 14 20 16 22 24 31 26 33 28 35 37 41 +TICK +M 41 35 33 31 22 20 18 8 6 +MX 37 28 26 24 16 14 12 4 2 +DETECTOR(8, 1, 6) rec[-18] +DETECTOR(6, 2, 6) rec[-17] rec[-34] +DETECTOR(6, 0, 6) rec[-16] rec[-33] rec[-35] +DETECTOR(6, 0.5, 6) rec[-15] rec[-33] rec[-34] +DETECTOR(4, 3, 6) rec[-14] rec[-31] +DETECTOR(4, 1, 6) rec[-13] rec[-30] rec[-32] +DETECTOR(4, 2, 6) rec[-12] rec[-31] +DETECTOR(2, 0, 6) rec[-11] rec[-28] +DETECTOR(2, 2, 6) rec[-10] rec[-28] rec[-29] +DETECTOR(7, 1, 6) rec[-9] rec[-27] +DETECTOR(5, 4, 6) rec[-8] rec[-26] +DETECTOR(5, 2, 6) rec[-7] rec[-25] +DETECTOR(5, 0, 6) rec[-6] rec[-24] +DETECTOR(3, 5, 6) rec[-5] rec[-23] +DETECTOR(3, 3, 6) rec[-4] rec[-22] +DETECTOR(3, 1, 6) rec[-3] rec[-21] +DETECTOR(1, 2, 6) rec[-2] rec[-20] +DETECTOR(1, 0, 6) rec[-1] rec[-19] +TICK +RX 24 31 2 6 8 10 20 22 39 1 12 18 37 33 14 26 28 30 41 +TICK +T 3 11 13 25 15 27 29 36 38 +T_DAG 0 7 9 17 19 21 23 32 34 40 +TICK +CX 1 0 2 3 6 7 8 13 10 15 12 11 14 9 18 17 20 27 22 29 24 25 26 19 28 21 30 23 31 32 33 34 37 36 39 38 41 40 +TICK +CX 3 1 14 15 29 30 33 38 37 41 +TICK +CX 7 3 13 14 26 33 28 29 32 37 +TICK +CX 12 13 25 32 27 28 +TICK +CX 12 7 26 27 +TICK +CX 18 12 25 26 +TICK +CX 25 18 +TICK +MX 25 +DETECTOR(0, 1, 7) rec[-1] rec[-12] rec[-13] rec[-14] rec[-15] rec[-17] rec[-18] rec[-19] rec[-30] rec[-31] rec[-32] rec[-33] rec[-35] rec[-36] rec[-37] rec[-48] rec[-49] rec[-50] rec[-51] rec[-53] rec[-54] rec[-55] rec[-61] rec[-62] rec[-65] rec[-66] +TICK +RX 25 +TICK +CX 25 18 +TICK +CX 18 12 25 26 +TICK +CX 12 7 26 27 +TICK +CX 12 13 25 32 27 28 +TICK +CX 7 3 13 14 26 33 28 29 32 37 +TICK +CX 3 1 14 15 29 30 33 38 37 41 +TICK +CX 1 0 2 3 6 7 8 13 10 15 12 11 14 9 18 17 20 27 22 29 24 25 26 19 28 21 30 23 31 32 33 34 37 36 39 38 41 40 +TICK +T 0 7 9 17 19 21 23 32 34 40 +T_DAG 3 11 13 25 15 27 29 36 38 +TICK +MX 20 22 8 10 2 6 24 31 39 1 12 18 37 33 14 26 28 30 41 +DETECTOR(4, 3, 8) rec[-19] +DETECTOR(4, 5, 8) rec[-18] +DETECTOR(2, 2, 8) rec[-17] +DETECTOR(2, 4, 8) rec[-16] +DETECTOR(1, 0, 8) rec[-15] +DETECTOR(2, 0, 8) rec[-14] +DETECTOR(5, 1, 8) rec[-13] rec[-20] +DETECTOR(6, 0, 8) rec[-12] +DETECTOR(7, 3, 8) rec[-11] +DETECTOR(0, 1, 8) rec[-10] +DETECTOR(3, 1, 8) rec[-9] +DETECTOR(4, 1, 8) rec[-8] +DETECTOR(7, 1, 8) rec[-7] +DETECTOR(6, 2, 8) rec[-6] +DETECTOR(3, 3, 8) rec[-5] +DETECTOR(5, 2, 8) rec[-4] +DETECTOR(5, 4, 8) rec[-3] +DETECTOR(5, 6, 8) rec[-2] +DETECTOR(8, 1, 8) rec[-1] +TICK +T 3 11 13 25 15 27 29 36 38 +T_DAG 0 7 9 17 19 21 23 32 34 40 +TICK +MPP X0*X3*X7*X9*X11*X13*X15*X17*X19*X21*X23*X25*X27*X29*X32*X34*X36*X38*X40 +OBSERVABLE_INCLUDE(0) rec[-1] rec[-2] rec[-3] rec[-4] rec[-5] rec[-6] rec[-7] rec[-8] rec[-9] rec[-10] rec[-11] +TICK +T 0 7 9 17 19 21 23 32 34 40 +T_DAG 3 11 13 25 15 27 29 36 38 +TICK +MPP X0*X3*X7*X11 X9*X13*X15*X19*X21*X27 X17*X25*X32*X36 +DETECTOR(1, 0, 9) rec[-3] rec[-25] +DETECTOR(3, 3, 9) rec[-2] rec[-28] +DETECTOR(5, 0, 9) rec[-1] rec[-24] rec[-30] +TICK +MPP Z0*Z3*Z7*Z11 Z9*Z13*Z15*Z19*Z21*Z27 Z17*Z25*Z32*Z36 +DETECTOR(2, 0, 10) rec[-3] rec[-37] rec[-38] +DETECTOR(4, 1, 10) rec[-2] rec[-39] rec[-41] +DETECTOR(6, 0, 10) rec[-1] rec[-42] rec[-43] +TICK +MPP X3*X7*X9*X13 X15*X21*X23*X29 X19*X25*X27*X32*X34*X38 +DETECTOR(1, 2, 11) rec[-3] rec[-32] +DETECTOR(3, 5, 11) rec[-2] rec[-35] +DETECTOR(5, 2, 11) rec[-1] rec[-30] rec[-37] +TICK +MPP Z3*Z7*Z9*Z13 Z15*Z21*Z23*Z29 Z19*Z25*Z27*Z32*Z34*Z38 +DETECTOR(2, 0, 12) rec[-3] rec[-43] +DETECTOR(4, 3, 12) rec[-2] rec[-46] +DETECTOR(6, 0, 12) rec[-1] rec[-48] rec[-50] +TICK +MPP X7*X11*X13*X17*X19*X25 X21*X27*X29*X34 X32*X36*X38*X40 +DETECTOR(3, 1, 13) rec[-3] rec[-36] rec[-39] +DETECTOR(5, 4, 13) rec[-2] rec[-44] +DETECTOR(7, 1, 13) rec[-1] rec[-45] +TICK +MPP Z7*Z11*Z13*Z17*Z19*Z25 Z21*Z27*Z29*Z34 Z32*Z36*Z38*Z40 +DETECTOR(4, 3, 14) rec[-3] rec[-52] +DETECTOR(6, 2, 14) rec[-2] rec[-55] +DETECTOR(6, 1, 14) rec[-1] diff --git a/test/integration/test_determined_literature.py b/test/integration/test_determined_literature.py new file mode 100644 index 00000000..45188b3d --- /dev/null +++ b/test/integration/test_determined_literature.py @@ -0,0 +1,41 @@ +"""Determined measurements of a published cultivation circuit with T gates. + +The circuit and its source are in ``test/data/determined``. +""" + +from pathlib import Path + +import pytest +import stim + +from tsim.circuit import Circuit +from tsim.determined import ( + _Declared, + count_determined_measurements, + missing_detectors, + nondeterministic_detectors, +) +from tsim.utils.gf2_span import Span + +CIRCUIT = Path(__file__).parent.parent / "data" / "determined" +CIRCUIT = CIRCUIT / "chan_d5a19_uncorrected_T.stim" + + +@pytest.mark.parametrize("shots", [None, 2000]) +def test_chan_uncorrected_d5_circuit_has_random_detectors(shots): + # NOTE: Chan et al. sim/d5-correction.md: "Bell growth leaves seven + # first-round check results that control a Pauli frame", so swapping S for + # T needs feedforward first. Without it, four detectors are random, and + # D91^D97 and D91^D102 are the determined parities of them. + circuit = Circuit(CIRCUIT.read_text()) + assert circuit.num_measurements == 112 + assert count_determined_measurements(circuit, shots=shots, seed=0) == 106 + assert nondeterministic_detectors(circuit, shots=shots, seed=0) == [ + stim.target_relative_detector_id(i) for i in (69, 91, 97, 102) + ] + missing = missing_detectors(circuit, shots=shots, seed=0) + assert len(missing) == 2 + words = _Declared((circuit + missing).stim_circuit).words + span = Span(circuit.num_measurements) + span.insert(words[[107, 108]]) + assert not span.insert(words[[91, 91]] ^ words[[97, 102]]).any() diff --git a/test/unit/statevector/test_branches.py b/test/unit/statevector/test_branches.py index 5fa1d15b..53a4d2b3 100644 --- a/test/unit/statevector/test_branches.py +++ b/test/unit/statevector/test_branches.py @@ -122,3 +122,37 @@ def test_a_random_noiseless_output_has_no_reference(): ((compiled, _),) = _components(text, sample_detectors=True) with pytest.raises(ValueError, match="detector 0 is random without noise"): assert Branches(compiled, num_detectors=1).reference is not None + + +@pytest.mark.parametrize("split", [False, True]) +@pytest.mark.parametrize("name", sorted(CIRCUITS)) +def test_the_support_with_its_coins_is_every_possible_outcome(name, split, monkeypatch): + if split: + # NOTE: two-row lanes, so the walk splits them. + monkeypatch.setattr("tsim.statevector.branches.MAX_BYTES", 64) + for compiled, ops in _components(CIRCUITS[name], sample_detectors=False): + branches = Branches(compiled, num_detectors=len(compiled.output_indices)) + f = np.array(f_patterns(ops.num_f, limit=4), dtype=np.uint8) + f = f.reshape(len(f), ops.num_f) + row, values = branches.support(f) + assert np.all(np.diff(row) >= 0) + coins = branches.coin_rows + flips = np.array( + [ + np.bitwise_xor.reduce(coins[list(s)], axis=0, initial=0) + for s in _subsets(len(coins)) + ], + dtype=np.uint8, + ).reshape(-1, coins.shape[1]) + expected = [set() for _ in f] + for r, _, out in branches._leaves(f): + for i, o in zip(r.tolist(), out.tolist(), strict=True): + expected[i].add(tuple(o)) + got = [set() for _ in f] + for i, v in zip(row.tolist(), values, strict=True): + got[i] |= {tuple(int(b) for b in v ^ flip) for flip in flips} + assert got == expected + + +def _subsets(n: int) -> list[list[int]]: + return [[i for i in range(n) if mask >> i & 1] for mask in range(1 << n)] diff --git a/test/unit/test_determined.py b/test/unit/test_determined.py new file mode 100644 index 00000000..c4943695 --- /dev/null +++ b/test/unit/test_determined.py @@ -0,0 +1,336 @@ +"""Tests for ``tsim.determined``: determined measurement parities.""" + +import numpy as np +import pytest +import stim + +from tsim.circuit import Circuit +from tsim.determined import ( + _directions_of, + count_determined_measurements, + missing_detectors, + nondeterministic_detectors, +) + + +@pytest.fixture(autouse=True) +def _uncached(): + # NOTE: some tests change the limits of the exact walk, which the cached + # directions do not see. + _directions_of.cache_clear() + + +STIM_EXAMPLES = [ + "R 0\nM 0", + "R 0\nH 0\nM 0", + "M 0", + "M 0\nM 0 1\nM 0 1 2\nM 0 1 2 3", + "R 0 1\nMZZ 0 1\nMYY 0 1\nMXX 0 1", + """ + MZZ 0 1 + MYY 0 1 + MXX 0 1 + DEPOLARIZE1(0.1) 0 1 + MZZ 0 1 + MYY 0 1 + MXX 0 1 + DETECTOR rec[-1] rec[-4] + DETECTOR rec[-2] rec[-5] + DETECTOR rec[-3] rec[-6] + """, + "RX 0\nMPP X0*Z1 Y0\nMRY 0\nMY 0", +] + +GENERATED = [ + ("surface_code:rotated_memory_x", 3), + ("surface_code:unrotated_memory_z", 3), + ("repetition_code:memory", 5), + ("color_code:memory_xyz", 5), +] + + +def _generated(task: str, distance: int) -> str: + circuit = stim.Circuit.generated( + task, distance=distance, rounds=3, after_clifford_depolarization=0.01 + ) + return str(circuit) + + +def _clifford_cases() -> list[str]: + return STIM_EXAMPLES + [_generated(task, d) for task, d in GENERATED] + + +@pytest.mark.parametrize("text", _clifford_cases()) +@pytest.mark.parametrize("unknown_input", [False, True]) +@pytest.mark.parametrize("shots", [None, 2000]) +def test_a_clifford_circuit_has_stims_counts(text, unknown_input, shots): + expected = stim.Circuit(text) + circuit = Circuit(text) + options = {"unknown_input": unknown_input, "shots": shots, "seed": 0} + assert count_determined_measurements( + circuit, **options + ) == expected.count_determined_measurements(unknown_input=unknown_input) + missing = missing_detectors(circuit, **options) + # NOTE: Stim's stub says that missing_detectors returns an int. + reference = stim.Circuit( + str(expected.missing_detectors(unknown_input=unknown_input)) + ) + assert len(missing) == len(reference) + assert nondeterministic_detectors(circuit, **options) == [] + # NOTE: with the missing detectors, Stim finds none missing. + rest = (expected + missing).missing_detectors(unknown_input=unknown_input) + assert str(rest) == "" + + +@pytest.mark.parametrize("text", _clifford_cases()) +def test_the_missing_detectors_are_determined_in_every_shot(text): + bare = Circuit(text).without_noise().without_annotations() + missing = missing_detectors(bare) + assert len(missing) == count_determined_measurements(bare) + sampler = (bare + missing).compile_detector_sampler(seed=1) + assert not sampler.sample(500, use_detector_reference_sample=True).any() + + +def test_t_gates_randomize_a_measurement_and_t4_is_z(): + assert count_determined_measurements(Circuit("R 0\nH 0\nT 0\nH 0\nM 0")) == 0 + assert count_determined_measurements(Circuit("R 0\nH 0\nT 0 0 0 0\nH 0\nM 0")) == 1 + + +def test_a_nonlinear_function_of_earlier_measurements_is_not_counted(): + # NOTE: m2 = m0 AND m1 is predictable, but no parity of the three is. + circuit = Circuit("R 0 1 2\nH 0 1\nCCX 0 1 2\nM 0 1 2") + assert count_determined_measurements(circuit) == 0 + assert count_determined_measurements(circuit, shots=1000, seed=0) == 0 + + +@pytest.mark.parametrize( + "gate, value", [("Z", 0), ("S", 0), ("T", 1), ("T_DAG", 1), ("SQRT_X", 0)] +) +def test_ghz_parity_after_four_gates(gate, value): + # NOTE: Gidney, Shutty, Jones (arXiv:2409.17595) Fig. 22, "danger of using S + # as a proxy for T": XXXX of a GHZ state stays determined after T^4, but -1. + circuit = Circuit( + f"R 0 1 2 3\nH 0\nCX 0 1 0 2 0 3\n{gate} 0 1 2 3\nMPP X0*X1*X2*X3" + ) + assert str(missing_detectors(circuit)).split() == ["DETECTOR", "rec[-1]"] + samples = circuit.compile_sampler(seed=0).sample(100) + assert (samples == value).all() + + +def _fifteen_to_one(tail: str, *, flipped: int | None = None) -> Circuit: + # NOTE: Litinski, arXiv:1905.06903, Fig. 3: 15 pi/8 rotations Z_4 Z_b of the + # output qubit 4 and the checks b, which return to |+>. + lines = ["RX 0 1 2 3 4"] + for b in range(1, 16): + pairs = " ".join(f"{q} 4" for q in range(4) if b >> q & 1) + gate = "T_DAG" if b == flipped else "T" + lines += [f"CX {pairs}", f"{gate} 4", f"CX {pairs}"] + return Circuit("\n".join(lines) + "\n" + tail) + + +CHECKS = "MX 0 1 2 3\n" + "".join(f"DETECTOR rec[-{k}]\n" for k in range(1, 5)) + + +@pytest.mark.parametrize("shots", [None, 5000]) +def test_fifteen_to_one_has_four_determined_checks(shots): + circuit = _fifteen_to_one(CHECKS) + assert count_determined_measurements(circuit, shots=shots, seed=0) == 4 + assert nondeterministic_detectors(circuit, shots=shots, seed=0) == [] + assert len(missing_detectors(_fifteen_to_one("MX 0 1 2 3"), shots=shots)) == 4 + # NOTE: the output is T_DAG |+>: undoing it makes it determined. + output = CHECKS + "T 4\nMX 4\nOBSERVABLE_INCLUDE(0) rec[-1]" + assert count_determined_measurements(_fifteen_to_one(output)) == 5 + output = CHECKS + "MX 4\nOBSERVABLE_INCLUDE(0) rec[-1]" + random = nondeterministic_detectors(_fifteen_to_one(output)) + assert random == [stim.target_logical_observable_id(0)] + + +def test_a_wrong_t_gate_randomizes_checks(): + circuit = _fifteen_to_one(CHECKS, flipped=7) + assert count_determined_measurements(circuit) == 3 + assert nondeterministic_detectors(circuit) == [ + stim.target_relative_detector_id(k) for k in (1, 2, 3) + ] + + +def test_five_to_one_distillation_checks_are_random(): + # NOTE: Rodriguez et al., arXiv:2412.15165: noiseless acceptance is 1/6, so + # no syndrome is determined (``docs/tutorials/magic_state_distillation``). + theta = -np.arccos(np.sqrt(1 / 3)) / np.pi + circuit = Circuit(f""" + R 0 1 2 3 4 + R_X({theta}) 0 1 2 3 4 + T_DAG 0 1 2 3 4 + SQRT_X 0 1 4 + CZ 0 1 2 3 + SQRT_Y 0 3 + CZ 0 2 3 4 + SQRT_X_DAG 0 + CZ 0 4 1 3 + SQRT_X_DAG 0 1 2 3 4 + T 0 + R_X({-theta}) 0 + M 0 1 2 3 4 + """) + assert count_determined_measurements(circuit) == 0 + assert count_determined_measurements(circuit, shots=20_000, seed=0) == 0 + + +def test_missing_detectors_heal_random_detectors(): + # NOTE: after T, Z0 is random, and so is its copy Z1, but not Z0 Z1. + circuit = Circuit(""" + R 0 1 + H 0 + T 0 + H 0 + CX 0 1 + M 0 1 + DETECTOR rec[-1] + DETECTOR rec[-2] + """) + assert nondeterministic_detectors(circuit) == [ + stim.target_relative_detector_id(k) for k in (0, 1) + ] + assert count_determined_measurements(circuit) == 1 + assert str(missing_detectors(circuit)).split() == ["DETECTOR", "rec[-2]", "rec[-1]"] + + +def _random_circuit(rng: np.random.Generator, *, measured: bool = True) -> str: + kinds = ["H", "S", "T", "T_DAG", "CX"] + ["M", "MX", "R", "MPP"] * measured + lines = [] + for _ in range(int(rng.integers(6, 16))): + kind = rng.choice(kinds) + a, b = (int(q) for q in rng.choice(3, size=2, replace=False)) + if kind == "CX": + lines.append(f"CX {a} {b}") + elif kind == "MPP": + lines.append(f"MPP X{a}*Z{b}") + else: + lines.append(f"{kind} {a}") + return "\n".join([*lines, "M 0 1 2"]) + + +@pytest.mark.parametrize("seed", range(40)) +@pytest.mark.parametrize("measured", [True, False]) +def test_exact_counts_are_those_of_many_shots(seed, measured): + # NOTE: without measurements and resets before the end, unknown inputs + # reach the rotation signs. + rng = np.random.default_rng(seed) + circuit = Circuit(_random_circuit(rng, measured=measured)) + for unknown_input in (False, True): + exact = count_determined_measurements(circuit, unknown_input=unknown_input) + sampled = count_determined_measurements( + circuit, unknown_input=unknown_input, shots=4000, seed=seed + ) + assert exact == sampled + + +def _two_qubit_circuit(rng: np.random.Generator) -> str: + lines = [] + for _ in range(int(rng.integers(6, 14))): + kind = rng.choice(["H", "T", "T_DAG", "CX", "H"]) + a, b = (int(q) for q in rng.choice(2, size=2, replace=False)) + lines.append(f"CX {a} {b}" if kind == "CX" else f"{kind} {a}") + return "\n".join([*lines, "M 0 1", "CX 0 1", "M 1 0"]) + + +# NOTE: unknown inputs set the sign of a rotation in each of these. +SIGN_CIRCUITS = [ + "H 0\nT 0\nH 0\nT 0\nH 0\nM 0", + "H 0\nT 0\nH 0\nH 1\nT 0\nH 0\nT_DAG 1\nM 0 1\nCX 0 1\nM 1 0", + "H 0\nT 0\nH 1\nCX 1 0\nT_DAG 0\nH 0\nT_DAG 0\nH 0\nT 0\nM 0 1\nCX 0 1\nM 1 0", +] + + +@pytest.mark.parametrize("text", SIGN_CIRCUITS) +def test_unknown_inputs_that_set_rotation_signs_match_many_shots(text, monkeypatch): + circuit = Circuit(text) + exact = count_determined_measurements(circuit, unknown_input=True) + sampled = count_determined_measurements( + circuit, unknown_input=True, shots=4000, seed=0 + ) + assert exact == sampled + monkeypatch.setattr("tsim.determined.MAX_SIGN_BITS", 0) + _directions_of.cache_clear() + with pytest.raises(ValueError, match="independent rotation signs"): + count_determined_measurements(circuit, unknown_input=True) + + +def test_unknown_inputs_reach_rotation_signs(): + # NOTE: an unknown input b of qubit 0 flips the sign of the T rotation that + # H T H applies; the second measurement repeats the first either way. + circuit = Circuit("CX 0 1\nH 0\nT 0\nH 0\nM 0 0 1") + assert count_determined_measurements(circuit) == 2 + assert count_determined_measurements(circuit, unknown_input=True) == 1 + + +def test_an_unlikely_outcome_after_another_is_possible(): + # NOTE: m2 = m0 AND m1 flips only when both of chance 1e-7 land 1; that is + # unlikely overall, but each given the one before is not. + angle = 2 * np.arcsin(np.sqrt(1e-7)) / np.pi + circuit = Circuit(f"R 0 1 2\nR_X({angle}) 0 1\nCCX 0 1 2\nM 0 1 2") + assert count_determined_measurements(circuit) == 0 + + +def test_too_many_branches_are_refused(monkeypatch): + monkeypatch.setattr("tsim.statevector.branches.MAX_BRANCHES", 1) + circuit = Circuit("R 0 1\nH 0 1\nT 0 1\nH 0 1\nM 0 1") + with pytest.raises(ValueError, match="measurement branches"): + count_determined_measurements(circuit) + + +def test_too_few_shots_warn(): + circuit = Circuit( + "R 0 1 2 3 4 5 6 7 8 9\nH 0 1 2 3 4 5 6 7 8 9\nM 0 1 2 3 4 5 6 7 8 9" + ) + for function in (count_determined_measurements, missing_detectors): + with pytest.warns(UserWarning, match="use more shots") as record: + function(circuit, shots=12, seed=0) + assert record[0].filename == __file__ + with pytest.raises(ValueError, match="at least 1"): + count_determined_measurements(circuit, shots=0) + + +def test_padded_measurements_are_determined(): + # NOTE: Stim 1.16's count_determined_measurements rejects MPAD. + circuit = Circuit("MPAD 1 0\nH 0\nM 0\nDETECTOR rec[-2]") + assert count_determined_measurements(circuit) == 2 + assert str(missing_detectors(circuit)).split() == ["DETECTOR", "rec[-3]"] + + +def test_a_circuit_without_measurements(): + assert count_determined_measurements(Circuit("H 0\nT 0")) == 0 + assert len(missing_detectors(Circuit("H 0\nT 0"))) == 0 + + +def test_the_directions_are_cached_unless_shots_have_no_seed(): + circuit = Circuit("R 0\nH 0\nT 0\nH 0\nM 0\nDETECTOR rec[-1]") + count_determined_measurements(circuit) + nondeterministic_detectors(circuit, seed=3) + assert _directions_of.cache_info().hits == 1 + count_determined_measurements(circuit, shots=10) + assert _directions_of.cache_info().currsize == 1 + + +def test_declarations_of_paulis_are_refused(): + circuit = Circuit("R 0\nM 0\nOBSERVABLE_INCLUDE(0) X0") + with pytest.raises(ValueError, match="non-record target"): + missing_detectors(circuit) + + +def test_the_kernels_as_python_give_the_same_results(monkeypatch): + # NOTE: coverage cannot see inside numba kernels, so run them as Python too. + import tsim.determined + import tsim.utils.gf2_span + + circuit = Circuit("R 0 1 2\nH 0\nT 0\nCX 0 1\nH 0\nM 0 1\nDETECTOR rec[-1]\nM 2") + expected = str(missing_detectors(circuit)), nondeterministic_detectors(circuit) + for module, names in ( + (tsim.determined, ["_overlaps"]), + (tsim.utils.gf2_span, ["_lowest", "_has_bit", "_insert", "_annihilator"]), + ): + for name in names: + monkeypatch.setattr(module, name, getattr(module, name).py_func) + _directions_of.cache_clear() + got = str(missing_detectors(circuit)), nondeterministic_detectors(circuit) + assert got == expected diff --git a/test/unit/test_public_api.py b/test/unit/test_public_api.py index 372842ac..ed295645 100644 --- a/test/unit/test_public_api.py +++ b/test/unit/test_public_api.py @@ -1,5 +1,10 @@ import tsim import tsim.experimental +from tsim.determined import ( + count_determined_measurements, + missing_detectors, + nondeterministic_detectors, +) from tsim.noise.faults import Fault, LogicalError, shortest_errors from tsim.sampler import CompiledStateProbs from tsim.utils.encoder import ColorEncoder5, SteaneEncoder @@ -21,11 +26,19 @@ def test_experimental_api(): assert tsim.experimental.shortest_errors is shortest_errors assert tsim.experimental.LogicalError is LogicalError assert tsim.experimental.Fault is Fault + assert ( + tsim.experimental.count_determined_measurements is count_determined_measurements + ) + assert tsim.experimental.missing_detectors is missing_detectors + assert tsim.experimental.nondeterministic_detectors is nondeterministic_detectors assert sorted(tsim.experimental.__all__) == [ "ColorEncoder5", "CompiledStateProbs", "Fault", "LogicalError", "SteaneEncoder", + "count_determined_measurements", + "missing_detectors", + "nondeterministic_detectors", "shortest_errors", ] diff --git a/test/unit/utils/test_gf2_span.py b/test/unit/utils/test_gf2_span.py new file mode 100644 index 00000000..d4b68fdd --- /dev/null +++ b/test/unit/utils/test_gf2_span.py @@ -0,0 +1,66 @@ +"""Tests for ``tsim.utils.gf2_span``.""" + +import numpy as np +import pytest + +import tsim.utils.gf2_span +from tsim.utils.gf2_span import Span, pack, pack_bytes, unpack + + +@pytest.fixture(autouse=True, params=["numba", "python"]) +def _kernels(request, monkeypatch): + # NOTE: coverage cannot see inside numba kernels, so run them as Python too. + if request.param == "python": + for name in ("_lowest", "_has_bit", "_insert", "_annihilator"): + kernel = getattr(tsim.utils.gf2_span, name) + monkeypatch.setattr(tsim.utils.gf2_span, name, kernel.py_func) + + +def _rank(matrix: np.ndarray) -> int: + matrix = matrix.copy() % 2 + rank = 0 + for col in range(matrix.shape[1]): + rows = np.flatnonzero(matrix[rank:, col]) + rank + if not len(rows): + continue + matrix[[rank, rows[0]]] = matrix[[rows[0], rank]] + for r in np.flatnonzero(matrix[:, col]): + if r != rank: + matrix[r] ^= matrix[rank] + rank += 1 + if rank == len(matrix): + break + return rank + + +@pytest.mark.parametrize("width", [0, 1, 7, 64, 65, 130]) +def test_pack_round_trips(width): + bits = np.random.default_rng(width).integers(0, 2, (5, width), dtype=np.uint8) + assert np.array_equal(unpack(pack(bits), width), bits) + packed = np.packbits(bits, axis=1, bitorder="little") + assert np.array_equal(pack_bytes(packed, width), pack(bits)) + + +@pytest.mark.parametrize("seed", range(6)) +def test_the_annihilator_is_the_orthogonal_complement(seed): + rng = np.random.default_rng(seed) + width = int(rng.integers(1, 150)) + vectors = rng.integers(0, 2, (int(rng.integers(0, width + 5)), width)) + vectors = (vectors * (rng.random(vectors.shape) < 0.3)).astype(np.uint8) + span = Span(width) + grew = span.insert_bits(vectors) + assert span.rank == grew.sum() == _rank(vectors) + annihilator = unpack(span.annihilator(), width) + assert len(annihilator) == width - span.rank + assert not ((vectors.astype(int) @ annihilator.T.astype(int)) % 2).any() + assert _rank(annihilator) == len(annihilator) + ends = [int(np.flatnonzero(row).max()) for row in annihilator] + assert ends == sorted(set(ends)) + + +def test_the_basis_has_one_vector_per_pivot(): + span = Span(3) + grew = span.insert(pack(np.array([[0, 0, 0], [1, 1, 0], [0, 1, 1], [1, 0, 1]]))) + assert grew.tolist() == [False, True, True, False] + assert span.rank == 2 + assert unpack(span.basis(), 3).tolist() == [[1, 1, 0], [0, 1, 1]]