From 03fe5c3251d24bc3cc639460a18389e89f8f5e78 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 13:13:04 -0500 Subject: [PATCH 01/11] Classify the certifier's primitive faults as light or heavy without the oracle The certifier asked the oracle, with the threshold one, whether each distinct syndrome of a primitive fault has an error of Pauli weight at most one, in one Python call per syndrome, and likewise for each primitive fault with zero flags. Such an error is the identity or a single-qubit error by the physical weight, and the product of an X error and a Z error on at most one qubit each by the CSS weight, on any state. So the certifier now looks up the syndromes of these errors in a hash table, in the compiled search that already pairs light syndromes: one lookup per syndrome by the physical weight, and n + 1 by the CSS weight. A heavy primitive fault that no pair of light ones covers gets a fixed error from destabilizers, whose X and Z parts are replaced by single-qubit errors with the same syndromes, where there are some, by the CSS weight. The oracle answers only for the first heavy primitive fault with zero flags, which violates the bound. Which primitive faults are light, and so every verdict, is unchanged, and on CSS states the representatives are the errors that the oracle returned, so the certificates are too. The step is about two to three times faster on states of 50 to 150 qubits. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../circuits/certification/_fault_search.py | 247 ++++++++++++------ .../certification/_fault_search_test.py | 108 +++++--- .../certification/fault_propagation.md | 24 +- 3 files changed, 272 insertions(+), 107 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search.py b/src/qldpc/circuits/certification/_fault_search.py index cffa33604..094fb35f1 100644 --- a/src/qldpc/circuits/certification/_fault_search.py +++ b/src/qldpc/circuits/certification/_fault_search.py @@ -22,12 +22,16 @@ from qldpc._binary import ( get_num_words, get_rank_packed, + multiply_binary, + pack_bits, popcount_word, trailing_zeros_word, + unpack_bits, words_to_int, ) from qldpc._compilation import always_compiled from qldpc.codes.distance import CosetLeaderSearch, CosetMethod, MinWeightBound +from qldpc.codes.distance._cosets import get_unit_syndrome_words from qldpc.codes.distance._threads import get_num_threads, iterate_in_order from qldpc.codes.distance._tuning import get_tuning @@ -134,47 +138,51 @@ def find_smallest_violation( max_fault_weight = get_rank_packed(flag_rows, min(max_weight - 1, len(flag_rows))) + 1 oracle = CosetLeaderSearch(stabilizers, css_weight=css_weight, method=method) - return _search_irreducible_faults(signatures, max_fault_weight, oracle, memory_limit) + matrix = np.asarray(stabilizers, dtype=np.uint8) + return _search_irreducible_faults(signatures, max_fault_weight, oracle, matrix, memory_limit) def _search_irreducible_faults( signatures: dict[tuple[int, int], int], max_fault_weight: int, oracle: CosetLeaderSearch, + stabilizers: npt.NDArray[np.uint8], memory_limit: int = 2**30, # 1 GiB ) -> Violation | None: """Check the irreducible accepted faults that can violate the bound, by increasing fault weight. The signatures map the (flags, syndrome) of each kept primitive fault to its DEM index, and - ``max_fault_weight`` is the largest fault weight to check. Return a violating fault of the - smallest fault weight, or None if there is none. + ``max_fault_weight`` is the largest fault weight to check. ``stabilizers`` are those of the + oracle, as a binary matrix. Return a violating fault of the smallest fault weight, or None if + there is none. """ + # Split the primitive faults into heavy and light ones by their syndromes, without the oracle, + # and give each light syndrome an error of Pauli weight at most one (see "Deciding which + # primitive faults are heavy"). + syndromes = list(dict.fromkeys(syndrome for _, syndrome in signatures)) + syndrome_rows = _hashed_rows(syndromes, get_num_words(oracle.num_qubits)) + light_errors = { + syndrome: error + for syndrome, error in zip( + syndromes, _get_light_errors(syndrome_rows, stabilizers, oracle.css_weight) + ) + if error is not None + } + # Check each primitive fault with zero flags alone (see "Listing each irreducible accepted fault - # once"), and keep the others for ``FaultLister``. + # once"), and keep the others for ``FaultLister``. At fault weight one, a fault violates the + # bound exactly if it is heavy, and the oracle gives the bound of the first such fault. flagged: list[tuple[int, int, int]] = [] for (flag, syndrome), index in signatures.items(): if flag: flagged.append((flag, syndrome, index)) - continue - failure = _check_fault((index,), syndrome, oracle) - if failure is not None: - return failure - - # Split the other primitive faults into heavy and light ones. A fault that contains no heavy - # primitive fault satisfies the bound (see "A violating fault contains a heavy primitive - # fault"), so the search stops if none is heavy. Heaviness depends only on the syndrome, and - # any error with the syndrome serves as a representative (see "Checking one fault"), so the - # search queries the oracle once for each distinct syndrome. - bounds_by_syndrome = { - distinct_syndrome: oracle.get_min_weight_bound(distinct_syndrome, 1) - for distinct_syndrome in dict.fromkeys(syndrome for _, syndrome, _ in flagged) - } - heavy: list[tuple[int, int, int, int]] = [] - light: list[tuple[int, int, int, int]] = [] - for flag, syndrome, index in flagged: - bound = bounds_by_syndrome[syndrome] - primitive = (flag, syndrome, index, bound.representative) - (light if bound.within_max_weight else heavy).append(primitive) + elif syndrome not in light_errors: + return Violation((index,), syndrome, oracle.get_min_weight_bound(syndrome, 1)) + + # A fault that contains no heavy primitive fault satisfies the bound (see "A violating fault + # contains a heavy primitive fault"), so the search stops if none is heavy. + heavy = [primitive for primitive in flagged if primitive[1] not in light_errors] + light = [primitive for primitive in flagged if primitive[1] in light_errors] if not heavy: return None @@ -184,27 +192,33 @@ def _search_irreducible_faults( # light ones gets the product of their representatives. The pair depends only on the syndrome, # so the search runs once for each distinct heavy syndrome. Compiled code does it, since its # time is proportional to the number of distinct heavy syndromes times the number of light - # ones. Any other heavy primitive fault keeps the error that the oracle returned for it. - light_errors = {syndrome: error for _, syndrome, _, error in light} - light_syndromes = list(light_errors) - heavy_syndromes = list(dict.fromkeys(syndrome for _, syndrome, _, _ in heavy)) - num_words = _get_num_words_for_values(light_syndromes + heavy_syndromes) - pairs = _get_light_pairs( - _hashed_rows(heavy_syndromes, num_words), _hashed_rows(light_syndromes, num_words) + # ones. Any other heavy primitive fault gets a fixed error with its syndrome, a product of + # destabilizers (see "Deciding which primitive faults are heavy"). + syndrome_positions = {syndrome: position for position, syndrome in enumerate(syndromes)} + light_syndromes = list(dict.fromkeys(syndrome for _, syndrome, _ in light)) + heavy_syndromes = list(dict.fromkeys(syndrome for _, syndrome, _ in heavy)) + light_rows = syndrome_rows[[syndrome_positions[syndrome] for syndrome in light_syndromes]] + heavy_rows = syndrome_rows[[syndrome_positions[syndrome] for syndrome in heavy_syndromes]] + pairs = _get_light_pairs(heavy_rows, light_rows, light_rows) + fixed_errors = iter( + _get_fixed_errors(heavy_rows[pairs[:, 0] < 0], stabilizers, oracle.css_weight) ) - pair_errors = { - syndrome: light_errors[light_syndromes[first]] ^ light_errors[light_syndromes[second]] + heavy_errors = { + syndrome: ( + light_errors[light_syndromes[first]] ^ light_errors[light_syndromes[second]] + if first >= 0 + else next(fixed_errors) + ) for syndrome, (first, second) in zip(heavy_syndromes, pairs.tolist()) - if first >= 0 } - heavy = [ - (flag, syndrome, index, pair_errors.get(syndrome, error)) - for flag, syndrome, index, error in heavy - ] + representatives = light_errors | heavy_errors # Order the primitive faults with the heavy ones first, and each group in DEM order, so that the # faults to check are the ones that contain one of the first ``len(heavy)`` primitive faults. - primitives = heavy + light + primitives = [ + (flag, syndrome, index, representatives[syndrome]) + for flag, syndrome, index in heavy + light + ] # The oracle needs only small tables, so the lister may use the whole memory limit. It lists on # as many threads as the oracle's searches use. @@ -238,14 +252,81 @@ def find_violation(syndromes: npt.NDArray[np.uint64], max_weight: int) -> int: return None -def _check_fault( - indices: tuple[int, ...], syndrome: int, oracle: CosetLeaderSearch -) -> Violation | None: - """Return the fault with these indices and syndrome if it violates the bound.""" - bound = oracle.get_min_weight_bound(syndrome, len(indices)) - if bound.within_max_weight: - return None - return Violation(indices, syndrome, bound) +def _get_light_errors( + syndrome_rows: npt.NDArray[np.uint64], stabilizers: npt.NDArray[np.uint8], css_weight: bool +) -> list[int | None]: + """For each syndrome, an error of Pauli weight at most one with it, or None if there is none. + + Each row of ``syndrome_rows`` holds the linear hash of a syndrome of the stabilizers and then + its words, as in a table of syndromes, with a word for every 64 stabilizers. + """ + # Each error of Pauli weight at most one is the product of a first and a second error from two + # short lists (see "Deciding which primitive faults are heavy"), and ``_get_light_pairs`` finds, + # for each syndrome, the first such product with that syndrome. For the physical weight, the + # first list holds the identity alone, so each syndrome takes one lookup. + num_qubits = len(stabilizers) + x_errors = [1 << qubit for qubit in range(num_qubits)] + z_errors = [1 << (num_qubits + qubit) for qubit in range(num_qubits)] + # The syndrome of X on a qubit is the column of the Z parts of the stabilizers on that qubit, + # and that of Z is the column of their X parts. + x_words = pack_bits(np.ascontiguousarray(stabilizers[:, num_qubits:].T)) + z_words = pack_bits(np.ascontiguousarray(stabilizers[:, :num_qubits].T)) + identity_words = np.zeros((1, x_words.shape[1]), dtype=np.uint64) + if css_weight: + first_errors, first_words = [0, *x_errors], np.vstack([identity_words, x_words]) + second_errors, second_words = [0, *z_errors], np.vstack([identity_words, z_words]) + else: + first_errors, first_words = [0], identity_words + second_errors = [0] + for x_error, z_error in zip(x_errors, z_errors): + second_errors += [x_error, z_error, x_error | z_error] + single_words = np.stack([x_words, z_words, x_words ^ z_words], axis=1) + second_words = np.vstack([identity_words, single_words.reshape(-1, x_words.shape[1])]) + words = np.vstack([first_words, second_words]) + rows = np.hstack([_hash_words(words)[:, None], words]) + pairs = _get_light_pairs(syndrome_rows, rows[: len(first_words)], rows[len(first_words) :]) + return [ + first_errors[first] ^ second_errors[second] if first >= 0 else None + for first, second in pairs.tolist() + ] + + +def _get_fixed_errors( + syndrome_rows: npt.NDArray[np.uint64], stabilizers: npt.NDArray[np.uint8], css_weight: bool +) -> list[int]: + """For each syndrome, a fixed error with it, from destabilizers. + + Each row of ``syndrome_rows`` holds the linear hash of a syndrome of the stabilizers and then + its words, as in ``_get_light_errors``. For the CSS weight, the X and Z parts of the error + weigh separately, so the X part is replaced by an X error on at most one qubit with the same + syndrome, if there is one, and likewise the Z part (see "Deciding which primitive faults are + heavy"). + """ + # Multiply the syndromes by the destabilizers, the errors with unit syndromes for the matrix + # that takes errors to syndromes, which is the stabilizers with their halves swapped. + num_qubits = len(stabilizers) + checks = np.hstack([stabilizers[:, num_qubits:], stabilizers[:, :num_qubits]]) + syndromes = unpack_bits(np.ascontiguousarray(syndrome_rows[:, 1:]), num_qubits) + errors = multiply_binary(syndromes, get_unit_syndrome_words(checks)) + if not css_weight: + return _to_ints(pack_bits(errors)) + + # Look up the syndrome of each part among those of the identity and of the single-qubit errors + # of its type, each of which keeps its first qubit. + parts = [] + for columns in (slice(None, num_qubits), slice(num_qubits, None)): + singles = {0: 0} + column_words = pack_bits(np.ascontiguousarray(checks[:, columns].T)) + for qubit, syndrome in enumerate(_to_ints(column_words)): + singles.setdefault(syndrome, 1 << qubit) + part_syndromes = _to_ints( + pack_bits(multiply_binary(errors[:, columns], checks[:, columns].T)) + ) + part_errors = _to_ints(pack_bits(np.ascontiguousarray(errors[:, columns]))) + parts.append( + [singles.get(syndrome, error) for syndrome, error in zip(part_syndromes, part_errors)] + ) + return [x_part | (z_part << num_qubits) for x_part, z_part in zip(*parts)] #################################################################################################### @@ -759,6 +840,14 @@ def _to_words(values: Sequence[int], num_words: int) -> npt.NDArray[np.uint64]: return words.astype(np.uint64) +def _to_ints(words: npt.NDArray[np.uint64]) -> list[int]: + """Join each row of words into an integer, the inverse of ``_to_words``. + + Bytes join words faster than shifts do. + """ + return [int.from_bytes(row.tobytes(), "little") for row in words.astype(" npt.NDArray[np.uint64]: """For each integer, a row of its linear hash and then its words, as in a table of syndromes.""" words = _to_words(values, num_words) @@ -1397,44 +1486,50 @@ def _get_key_slots(keys: npt.NDArray[np.uint64]) -> npt.NDArray[np.int64]: @always_compiled def _get_light_pairs( - heavy_rows: npt.NDArray[np.uint64], light_rows: npt.NDArray[np.uint64] + syndrome_rows: npt.NDArray[np.uint64], + first_rows: npt.NDArray[np.uint64], + second_rows: npt.NDArray[np.uint64], ) -> npt.NDArray[np.int64]: - """For each heavy syndrome, two light syndromes that sum to it, if there are any. + """For each syndrome, a first and a second syndrome that sum to it, if there are any. - Each row holds the linear hash of a syndrome and then its words, and the light syndromes are - distinct. Row ``h`` of the result holds the first light syndrome ``i``, in order, such that the - sum of heavy syndrome ``h`` and light syndrome ``i`` is a light syndrome ``j``, and then ``j``, - or -1 twice if there is none. + Each row holds the linear hash of a syndrome and then its words. Row ``r`` of the result holds + the first row ``i`` of ``first_rows`` such that the sum of syndrome ``r`` and syndrome ``i`` is + the syndrome of a row of ``second_rows``, and then the first such row ``j``, or -1 twice if + there is none. """ - # Put the light syndromes in a hash map as in ``_find_syndrome_slot``, with at least eight times - # as many slots, so that most lookups of a sum that is not light end at once at an empty slot. - # This makes the search about five times faster than with half-full slots. + # Put the second syndromes in a hash map as in ``_find_syndrome_slot``, with at least eight + # times as many slots, so that most lookups of a sum that is not a second syndrome end at once + # at an empty slot. This makes the search about five times faster than with half-full slots. + # A syndrome that repeats keeps its first row. num_slots = 2 - while num_slots < 8 * len(light_rows): + while num_slots < 8 * len(second_rows): num_slots *= 2 slots = np.full(num_slots, -1, dtype=np.int64) - for row in range(len(light_rows)): - slots[_find_syndrome_slot(light_rows[row, 0], light_rows[row, 1:], light_rows, slots)] = row - - # Most sums are not light, so look each sum up by its hash first, as ``_find_syndrome_slot`` - # does, and compare its words only with those of a light syndrome with the same hash. The - # hashes are copied out of the rows, which are long for many qubits, so that the loop reads them - # in order. - light_hashes = light_rows[:, 0].copy() - num_words = heavy_rows.shape[1] - 1 - pairs = np.full((len(heavy_rows), 2), -1, dtype=np.int64) - for heavy in range(len(heavy_rows)): - heavy_hash = heavy_rows[heavy, 0] - for first in range(len(light_rows)): - hashed = heavy_hash ^ light_hashes[first] + for row in range(len(second_rows)): + found = _find_syndrome_slot(second_rows[row, 0], second_rows[row, 1:], second_rows, slots) + if slots[found] < 0: + slots[found] = row + + # Most sums are not second syndromes, so look each sum up by its hash first, as + # ``_find_syndrome_slot`` does, and compare its words only with those of a second syndrome with + # the same hash. The hashes are copied out of the rows, which are long for many qubits, so that + # the loop reads them in order. + first_hashes = first_rows[:, 0].copy() + second_hashes = second_rows[:, 0].copy() + num_words = syndrome_rows.shape[1] - 1 + pairs = np.full((len(syndrome_rows), 2), -1, dtype=np.int64) + for row in range(len(syndrome_rows)): + syndrome_hash = syndrome_rows[row, 0] + for first in range(len(first_rows)): + hashed = syndrome_hash ^ first_hashes[first] slot = _get_first_slot(hashed, num_slots) second = slots[slot] while second >= 0: - if light_hashes[second] == hashed: + if second_hashes[second] == hashed: equal = True for word in range(1, num_words + 1): - sum_word = heavy_rows[heavy, word] ^ light_rows[first, word] - if sum_word != light_rows[second, word]: + sum_word = syndrome_rows[row, word] ^ first_rows[first, word] + if sum_word != second_rows[second, word]: equal = False break if equal: @@ -1442,8 +1537,8 @@ def _get_light_pairs( slot = (slot + 1) & (num_slots - 1) second = slots[slot] if second >= 0: - pairs[heavy, 0] = first - pairs[heavy, 1] = second + pairs[row, 0] = first + pairs[row, 1] = second break return pairs diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index 47ad7c0a2..8b4b48b6c 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -362,7 +362,10 @@ def test_repeated_syndromes_are_checked_once() -> None: def test_light_pairs_match_brute_force( collide: bool, pytestconfig: pytest.Config, monkeypatch: pytest.MonkeyPatch ) -> None: - """Each heavy syndrome h gets the first light syndrome i such that h + i is a light syndrome.""" + """Each syndrome s gets the first syndrome i of a first list such that s + i is in a second. + + The second list can repeat a syndrome, and its first entry with the sum is the one returned. + """ # With zero hashes, lookups must compare syndromes. Syndromes of bits 62 to 65 span two words. if collide: monkeypatch.setattr( @@ -370,16 +373,22 @@ def test_light_pairs_match_brute_force( ) rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) for _ in range(20): - light = list(dict.fromkeys(int(ss) << 62 for ss in rng.integers(16, size=6))) - heavy = [int(ss) << 62 for ss in rng.integers(16, size=8)] - num_words = _fault_search._get_num_words_for_values(light + heavy) + first_list = [int(ss) << 62 for ss in rng.integers(16, size=4)] + second_list = [int(ss) << 62 for ss in rng.integers(16, size=6)] + syndromes = [int(ss) << 62 for ss in rng.integers(16, size=8)] + num_words = _fault_search._get_num_words_for_values(first_list + second_list + syndromes) pairs = _fault_search._get_light_pairs( - _fault_search._hashed_rows(heavy, num_words), - _fault_search._hashed_rows(light, num_words), + _fault_search._hashed_rows(syndromes, num_words), + _fault_search._hashed_rows(first_list, num_words), + _fault_search._hashed_rows(second_list, num_words), ) - for syndrome, pair in zip(heavy, pairs.tolist(), strict=True): - first = next((ii for ii, ss in enumerate(light) if syndrome ^ ss in light), -1) - expected = [first, light.index(syndrome ^ light[first])] if first >= 0 else [-1, -1] + for syndrome, pair in zip(syndromes, pairs.tolist(), strict=True): + first = next( + (ii for ii, ss in enumerate(first_list) if syndrome ^ ss in second_list), -1 + ) + expected = ( + [first, second_list.index(syndrome ^ first_list[first])] if first >= 0 else [-1, -1] + ) assert pair == expected @@ -391,23 +400,70 @@ def test_rows_hash_alike_whenever_they_are_hashed() -> None: _fault_search._hash_words(np.ones((1, num_words), dtype=np.uint64)) _fault_search._get_byte_hash_tables.cache_clear() light = _fault_search._hashed_rows([1, 2], 1) - assert _fault_search._get_light_pairs(heavy, light).tolist() == [[0, 1]] + assert _fault_search._get_light_pairs(heavy, light, light).tolist() == [[0, 1]] assert _fault_search._hashed_rows([3], 1).tolist() == heavy.tolist() +@pytest.mark.parametrize("css_weight", [False, True]) +def test_light_syndromes_match_brute_force(css_weight: bool, pytestconfig: pytest.Config) -> None: + """A syndrome gets an error of Pauli weight at most one exactly if it has one, on any state. + + Every syndrome also gets a fixed error with it, whose X and Z parts are single-qubit errors + where some have their syndromes, for the CSS weight. + """ + rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) + for num_qubits in (1, 2, 3, 4): + paulis = (np.arange(4**num_qubits)[:, None] >> np.arange(2 * num_qubits)) & 1 + x, z = paulis[:, :num_qubits], paulis[:, num_qubits:] + weights = np.maximum(x.sum(axis=1), z.sum(axis=1)) if css_weight else (x | z).sum(axis=1) + for _ in range(10): + circuit = stim.Circuit() + circuit.append("I", range(num_qubits)) + for gate in rng.integers(3, size=4 * num_qubits): + if gate == 2 and num_qubits > 1: + circuit.append("CX", rng.permutation(num_qubits)[:2].tolist()) + else: + circuit.append(("H", "S")[gate % 2], [int(rng.integers(num_qubits))]) + state = stim.Tableau.from_circuit(circuit).to_stabilizers() + stabilizers = np.array( + [np.concatenate(row.to_numpy()) for row in state], dtype=np.uint8 + ) + bits = (x @ stabilizers[:, num_qubits:].T + z @ stabilizers[:, :num_qubits].T) % 2 + syndrome_of = bits @ (1 << np.arange(num_qubits)) + + syndromes = list(range(2**num_qubits)) + errors = _fault_search._get_light_errors( + _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight + ) + for syndrome, error in zip(syndromes, errors, strict=True): + assert (error is None) == (weights[syndrome_of == syndrome].min() > 1) + assert error is None or (syndrome_of[error] == syndrome and weights[error] <= 1) + fixed_errors = _fault_search._get_fixed_errors( + _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight + ) + assert syndrome_of[fixed_errors].tolist() == syndromes + if css_weight: + # Each part is an error on at most one qubit if one has the syndrome of the part. + for shift, error in itertools.product((0, num_qubits), fixed_errors): + part = error & (((1 << num_qubits) - 1) << shift) + singles = [0] + [1 << (shift + qubit) for qubit in range(num_qubits)] + if syndrome_of[part] in syndrome_of[singles]: + assert part in singles + + def test_heavy_primitive_faults_get_products_of_light_representatives( monkeypatch: pytest.MonkeyPatch, ) -> None: """A heavy primitive fault gets the product of the representatives of its first light pair. The pair starts with the first light syndrome, in the order of the primitive faults, whose sum - with the heavy syndrome is also light. A heavy primitive fault without a pair keeps the - oracle's representative. + with the heavy syndrome is also light. Every representative has the syndrome of its primitive + fault, and that of a light primitive fault has Pauli weight at most one. """ # The stabilizers ZXX, X on qubit 2, and XZX make the syndromes 1, 4, 5, and 7 light. With the # light syndromes 5, 7, and 4 of these faults, the heavy syndrome 2, which repeats, is 5 + 7, - # the heavy syndrome 3 is 7 + 4, and the heavy syndrome 6 is no such sum. The oracle's - # representatives of 2 and 3 differ from these products. + # the heavy syndrome 3 is 7 + 4, and the heavy syndrome 6 is no such sum. The fixed error with + # syndrome 2 differs from the product for 2. stabilizers = np.array([[0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0], [1, 0, 1, 0, 1, 0]]) syndromes = [5, 7, 4, 2, 2, 3, 6] lister = mock.Mock(wraps=_fault_search.FaultLister) @@ -427,23 +483,15 @@ def syndrome_of(pauli: int) -> int: for syndrome, representative in zip(primitive_syndromes, representatives, strict=True): assert syndrome_of(representative) == syndrome - oracle = _fault_search.CosetLeaderSearch(stabilizers) - - def oracle_representative(syndrome: int) -> int: - return oracle.get_min_weight_bound(syndrome, 1).representative - - light = [5, 7, 4] - expected = [] - for syndrome in primitive_syndromes[:num_starts]: - first = next((ss for ss in light if syndrome ^ ss in light), None) - expected.append( - oracle_representative(syndrome) - if first is None - else oracle_representative(first) ^ oracle_representative(syndrome ^ first) - ) + light = dict(zip(primitive_syndromes[num_starts:], representatives[num_starts:], strict=True)) + assert list(light) == [5, 7, 4] + assert all(((pauli & 0b111) | (pauli >> 3)).bit_count() <= 1 for pauli in light.values()) assert primitive_syndromes[:num_starts] == [2, 2, 3, 6] - assert list(representatives[:num_starts]) == expected - assert oracle_representative(2) != expected[0] and oracle_representative(3) != expected[2] + expected = [light[5] ^ light[7], light[5] ^ light[7], light[7] ^ light[4]] + assert list(representatives[:3]) == expected + rows = _fault_search._hashed_rows([2], 1) + fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers.astype(np.uint8), False) + assert expected[0] != fixed_errors[0] @pytest.mark.parametrize("css_weight", [False, True]) diff --git a/src/qldpc/circuits/certification/fault_propagation.md b/src/qldpc/circuits/certification/fault_propagation.md index 92a929629..3f3d479b4 100644 --- a/src/qldpc/circuits/certification/fault_propagation.md +++ b/src/qldpc/circuits/certification/fault_propagation.md @@ -98,7 +98,7 @@ Each column of `P` for a dropped primitive fault is zero or equals the column of ### Checking one fault The search decides exactly whether `μ(S F) ≤ |F|` for a fault `F` with a minimum-weight oracle for the Pauli errors with a given syndrome. -It uses the same oracle, with the threshold one, to decide whether a primitive fault is heavy. +Whether a primitive fault is heavy takes no search ([Deciding which primitive faults are heavy](#deciding-which-primitive-faults-are-heavy)). The oracle is `qldpc.codes.distance.CosetLeaderSearch`, named for coset leaders, the lightest errors with a given syndrome. A stabilizer state is **CSS** if its stabilizer group is generated by **pure-X stabilizers**, which have no Z part, and **pure-Z stabilizers**, which have no X part. @@ -136,6 +136,28 @@ The search skips the oracle for a fault `F` in two cases, each of which cannot h It keeps them in a table, which grows within a user-specified memory limit, up to `2^31` syndromes. A full table takes no more syndromes, so the search may check some syndromes more than once. +### Deciding which primitive faults are heavy + +A primitive fault is light exactly if its syndrome is that of an error of Pauli weight at most one. +For the physical weight, these errors are the identity and the `3n` errors X, Y, and Z on one qubit. +For the CSS weight, `max(|x|, |z|) ≤ 1` means `|x| ≤ 1` and `|z| ≤ 1`, so they are the products of an X error on at most one qubit and a Z error on at most one qubit, the same qubit or another. +This holds whether or not the state is CSS. +The syndrome of X on qubit `q` is column `q` of the Z parts of `g_1, ..., g_n`, and that of Z on qubit `q` is column `q` of their X parts. +So the search looks up each distinct syndrome of a kept primitive fault in a hash table of the syndromes of these single-qubit errors and the identity. +For the physical weight, that is one lookup. +For the CSS weight, the search looks up the sum of the syndrome and that of each of the `n + 1` choices of the X error among the syndromes of the Z errors. +A light primitive fault's representative is the error of Pauli weight at most one that the lookup finds. + +A primitive fault with zero flags violates the bound exactly if it is heavy, and the search asks the oracle, with the threshold one, only for the bound of the first such fault, which it reports. +A heavy primitive fault with nonzero flags needs only a representative. +If its syndrome is the sum of the syndromes of two light primitive faults, it gets the product of their representatives, of Pauli weight at most two. +Otherwise, it gets a fixed error with its syndrome: the product of the destabilizers `d_j` for the set bits `j` of its syndrome, where `d_j` anticommutes with `g_j` alone. +The matrix `H = [W_Z | W_X]` takes an error `e = [x|z]` to its syndrome `H e`. +It has rank `n`, since `g_1, ..., g_n` are independent, so row reduction gives an invertible `T` such that `T H` is the identity on `n` pivot columns. +Then `d_j` is column `j` of `T` on the pivot columns, and zero on the others. +For the CSS weight, the X and Z parts of an error weigh separately, so the search replaces the X part of the fixed error by an X error on at most one qubit with the same syndrome, if there is one, and likewise its Z part. +The syndrome of a product is the sum of the syndromes of its factors, so the error keeps its syndrome, and its lower CSS weight lets the search skip more faults. + ### Listing each irreducible accepted fault once In this section, every fault contains only [kept primitive faults](#duplicate-and-zero-signatures-can-be-dropped). From 7843769b19e7d17167597f45fa15102a3fa8e72b Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 13:57:19 -0500 Subject: [PATCH 02/11] Test the light/heavy split on syndromes with bits in two words The tests of the classification and the fixed representatives used at most four qubits, so they could not catch code that drops the upper words of a syndrome. A test on 65 qubits now checks syndromes with bits on both sides of the word boundary, on |0...0> by either weight, and by CSS weight on a state that is not CSS: which syndromes are light, the syndromes and weights of the errors that represent them, and the violations and bounds that the search reports. The test of the products of light pairs no longer requires a fixed representative to differ from such a product, which may equal it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index 8b4b48b6c..22357ab6a 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -462,8 +462,7 @@ def test_heavy_primitive_faults_get_products_of_light_representatives( """ # The stabilizers ZXX, X on qubit 2, and XZX make the syndromes 1, 4, 5, and 7 light. With the # light syndromes 5, 7, and 4 of these faults, the heavy syndrome 2, which repeats, is 5 + 7, - # the heavy syndrome 3 is 7 + 4, and the heavy syndrome 6 is no such sum. The fixed error with - # syndrome 2 differs from the product for 2. + # the heavy syndrome 3 is 7 + 4, and the heavy syndrome 6 is no such sum. stabilizers = np.array([[0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0], [1, 0, 1, 0, 1, 0]]) syndromes = [5, 7, 4, 2, 2, 3, 6] lister = mock.Mock(wraps=_fault_search.FaultLister) @@ -489,9 +488,6 @@ def syndrome_of(pauli: int) -> int: assert primitive_syndromes[:num_starts] == [2, 2, 3, 6] expected = [light[5] ^ light[7], light[5] ^ light[7], light[7] ^ light[4]] assert list(representatives[:3]) == expected - rows = _fault_search._hashed_rows([2], 1) - fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers.astype(np.uint8), False) - assert expected[0] != fixed_errors[0] @pytest.mark.parametrize("css_weight", [False, True]) @@ -1430,11 +1426,63 @@ def test_hand_derived_dependency_cases() -> None: def test_batches_have_a_word_for_every_64_output_qubits() -> None: - """On 70 qubits, syndromes below ``2**64`` have two words in batches, as the oracle needs.""" + """Syndromes have a word for every 64 output qubits, in batches and in the light/heavy split. + + On 70 qubits, syndromes below ``2**64`` have two words in batches, as the oracle needs. On 65 + qubits, syndromes with bits in both words are split and represented by both words. + """ # Together, the two primitive faults pass post-selection and leave X on three qubits. failure = _certify_map([1, 1], [0b011, 0b100], None, 70) assert failure is not None and failure.fault_indices == (0, 1) + # On |0...0>, the syndrome of an error is its X part, and the lightest error with a syndrome is + # X on its set bits, so only syndromes of at most one bit are light, by either weight. A + # primitive fault with zero flags that leaves X on qubits 0 and 64 violates the bound alone, and + # with a primitive fault of zero syndrome, one that leaves X on qubits 0, 1, and 64 forms a + # violating fault of fault weight two. + syndromes = [1, 2**64, 2**64 + 1, 2**64 + 3] + rows = _fault_search._hashed_rows(syndromes, 2) + stabilizers = _z_state(65).astype(np.uint8) + for css_weight in (False, True): + light_errors = _fault_search._get_light_errors(rows, stabilizers, css_weight) + assert [error is None for error in light_errors] == [False, False, True, True] + for syndrome, error in zip(syndromes[:2], light_errors[:2], strict=True): + assert error is not None and error & (2**65 - 1) == syndrome + x, z = error & (2**65 - 1), error >> 65 + assert (max(x.bit_count(), z.bit_count()) if css_weight else (x | z).bit_count()) <= 1 + fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, css_weight) + assert [error & (2**65 - 1) for error in fixed_errors] == syndromes + failure = _certify_map([0], [2**64 + 1], None, 65) + assert failure is not None and failure.fault_indices == (0,) + failure = _certify_map([1, 1], [2**64 + 3, 0], None, 65) + assert failure is not None and failure.fault_indices == (0, 1) + + # With Z on qubit 0 and Y on qubit 64 as the last stabilizer, the state is not CSS. X on qubit + # 0 flips bits 0 and 64, X or Z on qubit 64 flips bit 64, and X on another qubit j flips bit j. + # By CSS weight, X on qubit 0 and Z on qubit 64 weighs one, so the syndrome 1 is light, though + # no error on one qubit has it, and 2**64 + 3 needs X on qubits 0 and 1, of CSS weight two. + stabilizers[64, [64, 65]] = 1 + + def syndrome_of(pauli: int) -> int: + x, z = pauli & (2**65 - 1), pauli >> 65 + return x & (2**64 - 1) | ((x ^ x >> 64 ^ z >> 64) & 1) << 64 + + light_errors = _fault_search._get_light_errors(rows, stabilizers, True) + fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, True) + assert [error is None for error in light_errors] == [False, False, False, True] + for syndrome, light_error, fixed_error in zip( + syndromes, light_errors, fixed_errors, strict=True + ): + assert syndrome_of(fixed_error) == syndrome + if light_error is not None: + assert syndrome_of(light_error) == syndrome + x, z = light_error & (2**65 - 1), light_error >> 65 + assert max(x.bit_count(), z.bit_count()) <= 1 + assert _fault_search.find_smallest_violation([0], [1], stabilizers, None, True) is None + failure = _fault_search.find_smallest_violation([0], [2**64 + 3], stabilizers, None, True) + assert failure is not None and failure.fault_indices == (0,) + assert failure.bound.lower_bound == 2 and syndrome_of(failure.bound.representative) == 2**64 + 3 + def test_failures_report_the_smallest_faults() -> None: """A violating fault of two primitive faults is reported before a violating fault of three.""" From 913830e6e9f8a567c31f196f6eb35f0d3e3bd58c Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 14:13:30 -0500 Subject: [PATCH 03/11] Say that light pairs have nonzero flags, and test Z parts in two words A heavy primitive fault gets the product of the representatives of two light primitive faults only if both have nonzero flags, since only those are paired, so the guide and the comment now say so. The test of syndromes in two words gains a CSS state on which the Z part of the fixed error has bits in both words, so that code that drops the upper word of a Z part fails it. The brute-force test of the light syndromes takes the three states of one qubit once each, rather than random states that repeat them. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../circuits/certification/_fault_search.py | 10 +-- .../certification/_fault_search_test.py | 83 ++++++++++++------- .../certification/fault_propagation.md | 2 +- 3 files changed, 59 insertions(+), 36 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search.py b/src/qldpc/circuits/certification/_fault_search.py index 094fb35f1..1fb1fbe0d 100644 --- a/src/qldpc/circuits/certification/_fault_search.py +++ b/src/qldpc/circuits/certification/_fault_search.py @@ -189,11 +189,11 @@ def _search_irreducible_faults( # Give each primitive fault a representative of low Pauli weight, so that ``FaultLister`` skips # more faults (see "Checking one fault"). A light primitive fault's representative has Pauli # weight at most one. A heavy primitive fault whose syndrome is the sum of the syndromes of two - # light ones gets the product of their representatives. The pair depends only on the syndrome, - # so the search runs once for each distinct heavy syndrome. Compiled code does it, since its - # time is proportional to the number of distinct heavy syndromes times the number of light - # ones. Any other heavy primitive fault gets a fixed error with its syndrome, a product of - # destabilizers (see "Deciding which primitive faults are heavy"). + # light ones with nonzero flags gets the product of their representatives. The pair depends + # only on the syndrome, so the search runs once for each distinct heavy syndrome. Compiled + # code does it, since its time is proportional to the number of distinct heavy syndromes times + # the number of light ones. Any other heavy primitive fault gets a fixed error with its + # syndrome, a product of destabilizers (see "Deciding which primitive faults are heavy"). syndrome_positions = {syndrome: position for position, syndrome in enumerate(syndromes)} light_syndromes = list(dict.fromkeys(syndrome for _, syndrome, _ in light)) heavy_syndromes = list(dict.fromkeys(syndrome for _, syndrome, _ in heavy)) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index 22357ab6a..ddebd3e3e 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -411,44 +411,49 @@ def test_light_syndromes_match_brute_force(css_weight: bool, pytestconfig: pytes Every syndrome also gets a fixed error with it, whose X and Z parts are single-qubit errors where some have their syndromes, for the CSS weight. """ + # The states of one qubit are those of X, Y, and Z, and those of more qubits are random. + states = [np.array([row], dtype=np.uint8) for row in ([1, 0], [1, 1], [0, 1])] rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) - for num_qubits in (1, 2, 3, 4): - paulis = (np.arange(4**num_qubits)[:, None] >> np.arange(2 * num_qubits)) & 1 - x, z = paulis[:, :num_qubits], paulis[:, num_qubits:] - weights = np.maximum(x.sum(axis=1), z.sum(axis=1)) if css_weight else (x | z).sum(axis=1) + for num_qubits in (2, 3, 4): for _ in range(10): circuit = stim.Circuit() circuit.append("I", range(num_qubits)) for gate in rng.integers(3, size=4 * num_qubits): - if gate == 2 and num_qubits > 1: + if gate == 2: circuit.append("CX", rng.permutation(num_qubits)[:2].tolist()) else: - circuit.append(("H", "S")[gate % 2], [int(rng.integers(num_qubits))]) + circuit.append(("H", "S")[gate], [int(rng.integers(num_qubits))]) state = stim.Tableau.from_circuit(circuit).to_stabilizers() - stabilizers = np.array( - [np.concatenate(row.to_numpy()) for row in state], dtype=np.uint8 + states.append( + np.array([np.concatenate(row.to_numpy()) for row in state], dtype=np.uint8) ) - bits = (x @ stabilizers[:, num_qubits:].T + z @ stabilizers[:, :num_qubits].T) % 2 - syndrome_of = bits @ (1 << np.arange(num_qubits)) - syndromes = list(range(2**num_qubits)) - errors = _fault_search._get_light_errors( - _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight - ) - for syndrome, error in zip(syndromes, errors, strict=True): - assert (error is None) == (weights[syndrome_of == syndrome].min() > 1) - assert error is None or (syndrome_of[error] == syndrome and weights[error] <= 1) - fixed_errors = _fault_search._get_fixed_errors( - _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight - ) - assert syndrome_of[fixed_errors].tolist() == syndromes - if css_weight: - # Each part is an error on at most one qubit if one has the syndrome of the part. - for shift, error in itertools.product((0, num_qubits), fixed_errors): - part = error & (((1 << num_qubits) - 1) << shift) - singles = [0] + [1 << (shift + qubit) for qubit in range(num_qubits)] - if syndrome_of[part] in syndrome_of[singles]: - assert part in singles + for stabilizers in states: + num_qubits = len(stabilizers) + paulis = (np.arange(4**num_qubits)[:, None] >> np.arange(2 * num_qubits)) & 1 + x, z = paulis[:, :num_qubits], paulis[:, num_qubits:] + weights = np.maximum(x.sum(axis=1), z.sum(axis=1)) if css_weight else (x | z).sum(axis=1) + bits = (x @ stabilizers[:, num_qubits:].T + z @ stabilizers[:, :num_qubits].T) % 2 + syndrome_of = bits @ (1 << np.arange(num_qubits)) + + syndromes = list(range(2**num_qubits)) + errors = _fault_search._get_light_errors( + _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight + ) + for syndrome, error in zip(syndromes, errors, strict=True): + assert (error is None) == (weights[syndrome_of == syndrome].min() > 1) + assert error is None or (syndrome_of[error] == syndrome and weights[error] <= 1) + fixed_errors = _fault_search._get_fixed_errors( + _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight + ) + assert syndrome_of[fixed_errors].tolist() == syndromes + if css_weight: + # Each part is an error on at most one qubit if one has the syndrome of the part. + for shift, error in itertools.product((0, num_qubits), fixed_errors): + part = error & (((1 << num_qubits) - 1) << shift) + singles = [0] + [1 << (shift + qubit) for qubit in range(num_qubits)] + if syndrome_of[part] in syndrome_of[singles]: + assert part in singles def test_heavy_primitive_faults_get_products_of_light_representatives( @@ -1464,8 +1469,10 @@ def test_batches_have_a_word_for_every_64_output_qubits() -> None: stabilizers[64, [64, 65]] = 1 def syndrome_of(pauli: int) -> int: - x, z = pauli & (2**65 - 1), pauli >> 65 - return x & (2**64 - 1) | ((x ^ x >> 64 ^ z >> 64) & 1) << 64 + """The syndrome of an error for the current ``stabilizers``.""" + error = np.array([(pauli >> bit) & 1 for bit in range(130)]) + bits = (stabilizers[:, 65:] @ error[:65] + stabilizers[:, :65] @ error[65:]) % 2 + return sum(int(bit) << jj for jj, bit in enumerate(bits)) light_errors = _fault_search._get_light_errors(rows, stabilizers, True) fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, True) @@ -1483,6 +1490,22 @@ def syndrome_of(pauli: int) -> int: assert failure is not None and failure.fault_indices == (0,) assert failure.bound.lower_bound == 2 and syndrome_of(failure.bound.representative) == 2**64 + 3 + # On the CSS state with stabilizers X on qubit 0, Z on qubits 2 and 64, X on qubits 1, 2, and + # 64, Z on each qubit j from 3 to 63, and X on qubit 1, the lightest errors with the syndrome + # 2**64 + 1 weigh three, and their Z parts flip bits in both words. With a primitive fault of + # zero syndrome, a primitive fault with this syndrome forms a violating fault of fault weight + # two. + stabilizers = np.zeros((65, 130), dtype=np.uint8) + stabilizers[[0, 2, 2, 2, 64], [0, 1, 2, 64, 1]] = 1 + stabilizers[[1, 1], [67, 129]] = 1 + stabilizers[range(3, 64), range(68, 129)] = 1 + rows = _fault_search._hashed_rows([2**64 + 1], 2) + fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, True) + assert syndrome_of(fixed_errors[0]) == 2**64 + 1 + failure = _fault_search.find_smallest_violation([1, 1], [2**64 + 1, 0], stabilizers, None, True) + assert failure is not None and failure.fault_indices == (0, 1) + assert failure.bound.lower_bound == 3 and syndrome_of(failure.bound.representative) == 2**64 + 1 + def test_failures_report_the_smallest_faults() -> None: """A violating fault of two primitive faults is reported before a violating fault of three.""" diff --git a/src/qldpc/circuits/certification/fault_propagation.md b/src/qldpc/circuits/certification/fault_propagation.md index 3f3d479b4..985dd02ce 100644 --- a/src/qldpc/circuits/certification/fault_propagation.md +++ b/src/qldpc/circuits/certification/fault_propagation.md @@ -150,7 +150,7 @@ A light primitive fault's representative is the error of Pauli weight at most on A primitive fault with zero flags violates the bound exactly if it is heavy, and the search asks the oracle, with the threshold one, only for the bound of the first such fault, which it reports. A heavy primitive fault with nonzero flags needs only a representative. -If its syndrome is the sum of the syndromes of two light primitive faults, it gets the product of their representatives, of Pauli weight at most two. +If its syndrome is the sum of the syndromes of two light primitive faults with nonzero flags, it gets the product of their representatives, of Pauli weight at most two. Otherwise, it gets a fixed error with its syndrome: the product of the destabilizers `d_j` for the set bits `j` of its syndrome, where `d_j` anticommutes with `g_j` alone. The matrix `H = [W_Z | W_X]` takes an error `e = [x|z]` to its syndrome `H e`. It has rank `n`, since `g_1, ..., g_n` are independent, so row reduction gives an invertible `T` such that `T H` is the identity on `n` pivot columns. From 379ed8095fb5c5722e513aae00629ac9bf2acd53 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 15:02:21 -0500 Subject: [PATCH 04/11] Test fixed errors whose Z parts act on qubits in two words The test of syndromes in two words now uses a state of X stabilizers on which the only Z error with the syndrome 2 acts on qubits 0, 1, and 64, so the Z part of its fixed error has bits in both words, and on which the single Z errors have syndromes in both words. Code that drops the upper word of a Z part, or of the syndromes of the single Z errors, fails it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index ddebd3e3e..384949d15 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -1490,21 +1490,21 @@ def syndrome_of(pauli: int) -> int: assert failure is not None and failure.fault_indices == (0,) assert failure.bound.lower_bound == 2 and syndrome_of(failure.bound.representative) == 2**64 + 3 - # On the CSS state with stabilizers X on qubit 0, Z on qubits 2 and 64, X on qubits 1, 2, and - # 64, Z on each qubit j from 3 to 63, and X on qubit 1, the lightest errors with the syndrome - # 2**64 + 1 weigh three, and their Z parts flip bits in both words. With a primitive fault of - # zero syndrome, a primitive fault with this syndrome forms a violating fault of fault weight - # two. + # On |+...+>, with the stabilizers X on qubits 0 and 1, X on qubit 64, X on each qubit j from 2 + # to 63, and X on qubits 0 and 64, Z on qubit 0 has the syndrome 2**64 + 1, and the only Z + # error with the syndrome 2 acts on qubits 0, 1, and 64, so the errors with it weigh three. + # With a primitive fault of zero syndrome, a primitive fault with the syndrome 2 forms a + # violating fault of fault weight two. stabilizers = np.zeros((65, 130), dtype=np.uint8) - stabilizers[[0, 2, 2, 2, 64], [0, 1, 2, 64, 1]] = 1 - stabilizers[[1, 1], [67, 129]] = 1 - stabilizers[range(3, 64), range(68, 129)] = 1 - rows = _fault_search._hashed_rows([2**64 + 1], 2) - fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, True) - assert syndrome_of(fixed_errors[0]) == 2**64 + 1 - failure = _fault_search.find_smallest_violation([1, 1], [2**64 + 1, 0], stabilizers, None, True) + stabilizers[[0, 0, 1, 64, 64], [0, 1, 64, 0, 64]] = 1 + stabilizers[range(2, 64), range(2, 64)] = 1 + fixed_errors = _fault_search._get_fixed_errors( + _fault_search._hashed_rows([2, 2**64 + 1], 2), stabilizers, True + ) + assert [syndrome_of(error) for error in fixed_errors] == [2, 2**64 + 1] + failure = _fault_search.find_smallest_violation([1, 1], [2, 0], stabilizers, None, True) assert failure is not None and failure.fault_indices == (0, 1) - assert failure.bound.lower_bound == 3 and syndrome_of(failure.bound.representative) == 2**64 + 1 + assert failure.bound.lower_bound == 3 and syndrome_of(failure.bound.representative) == 2 def test_failures_report_the_smallest_faults() -> None: From 3d8c7a3e4826d6dcf2fef0e74e88790beb88b36a Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 15:14:14 -0500 Subject: [PATCH 05/11] Test Z parts in two words by physical weight too The test of the state of X stabilizers on which a Z error acts on qubits in two words now runs by either weight, and checks the light error for the syndrome of Z on qubit 0. It also says that the lightest errors with the syndrome 2 weigh three, rather than all of them. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index 384949d15..eae2724cb 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -1492,19 +1492,26 @@ def syndrome_of(pauli: int) -> int: # On |+...+>, with the stabilizers X on qubits 0 and 1, X on qubit 64, X on each qubit j from 2 # to 63, and X on qubits 0 and 64, Z on qubit 0 has the syndrome 2**64 + 1, and the only Z - # error with the syndrome 2 acts on qubits 0, 1, and 64, so the errors with it weigh three. - # With a primitive fault of zero syndrome, a primitive fault with the syndrome 2 forms a - # violating fault of fault weight two. + # error with the syndrome 2 acts on qubits 0, 1, and 64, so the lightest errors with this + # syndrome weigh three, by either weight. With a primitive fault of zero syndrome, a primitive + # fault with the syndrome 2 forms a violating fault of fault weight two. stabilizers = np.zeros((65, 130), dtype=np.uint8) stabilizers[[0, 0, 1, 64, 64], [0, 1, 64, 0, 64]] = 1 stabilizers[range(2, 64), range(2, 64)] = 1 - fixed_errors = _fault_search._get_fixed_errors( - _fault_search._hashed_rows([2, 2**64 + 1], 2), stabilizers, True - ) - assert [syndrome_of(error) for error in fixed_errors] == [2, 2**64 + 1] - failure = _fault_search.find_smallest_violation([1, 1], [2, 0], stabilizers, None, True) - assert failure is not None and failure.fault_indices == (0, 1) - assert failure.bound.lower_bound == 3 and syndrome_of(failure.bound.representative) == 2 + rows = _fault_search._hashed_rows([2, 2**64 + 1], 2) + for css_weight in (False, True): + light_errors = _fault_search._get_light_errors(rows, stabilizers, css_weight) + assert light_errors[0] is None and light_errors[1] is not None + assert syndrome_of(light_errors[1]) == 2**64 + 1 + x, z = light_errors[1] & (2**65 - 1), light_errors[1] >> 65 + assert (max(x.bit_count(), z.bit_count()) if css_weight else (x | z).bit_count()) <= 1 + fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, css_weight) + assert [syndrome_of(error) for error in fixed_errors] == [2, 2**64 + 1] + failure = _fault_search.find_smallest_violation( + [1, 1], [2, 0], stabilizers, None, css_weight + ) + assert failure is not None and failure.fault_indices == (0, 1) + assert failure.bound.lower_bound == 3 and syndrome_of(failure.bound.representative) == 2 def test_failures_report_the_smallest_faults() -> None: From 7456471a72e44b94e239a058f8a905a3dcc2abd3 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 15:55:41 -0500 Subject: [PATCH 06/11] Check the light/heavy split by brute force on 70 qubits, in place of hand-written cases The brute-force test of the light syndromes and the fixed errors now also runs on three states of 70 qubits, whose syndromes and errors have bits in two words: a random state, a CSS state of X stabilizers alone, whose fixed errors are Z errors on any qubit, and a CSS state with both X and Z stabilizers, with its stabilizers in random order. It enumerates the errors of Pauli weight at most one and their syndromes, and checks the light syndromes and the representatives of these and of random syndromes. The hand-written cases on 65 qubits that it covers are gone. Two remain, which check that the search reports a violating fault with bits in both words, at fault weight one and at fault weight two. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 188 ++++++++---------- 1 file changed, 87 insertions(+), 101 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index eae2724cb..a6a0133e5 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -411,49 +411,99 @@ def test_light_syndromes_match_brute_force(css_weight: bool, pytestconfig: pytes Every syndrome also gets a fixed error with it, whose X and Z parts are single-qubit errors where some have their syndromes, for the CSS weight. """ - # The states of one qubit are those of X, Y, and Z, and those of more qubits are random. + # The states of one qubit are those of X, Y, and Z. Those of more qubits come from random + # circuits on qubits that start in |0> or, the first ``num_plus`` of them, in |+>, with their + # stabilizers in random order, and those of 70 qubits have syndromes and errors in two words. + # Circuits of CX gates alone make CSS states. On |+...+>, every fixed error is a Z error that + # can act on every qubit. With both X and Z stabilizers, the Z part of a fixed error need not + # be the lightest. states = [np.array([row], dtype=np.uint8) for row in ([1, 0], [1, 1], [0, 1])] rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) - for num_qubits in (2, 3, 4): - for _ in range(10): + clifford, cx = ["H", "S", "CX"], ["CX"] + for num_qubits, num_states, gates, num_plus in ( + (2, 10, clifford, 0), + (3, 10, clifford, 0), + (4, 10, clifford, 0), + (70, 1, clifford, 0), + (70, 1, cx, 70), + (70, 1, cx, 35), + ): + for _ in range(num_states): circuit = stim.Circuit() circuit.append("I", range(num_qubits)) - for gate in rng.integers(3, size=4 * num_qubits): - if gate == 2: + circuit.append("H", range(num_plus)) + for gate in rng.choice(gates, size=8 * num_qubits): + if gate == "CX": circuit.append("CX", rng.permutation(num_qubits)[:2].tolist()) else: - circuit.append(("H", "S")[gate], [int(rng.integers(num_qubits))]) + circuit.append(gate, [int(rng.integers(num_qubits))]) state = stim.Tableau.from_circuit(circuit).to_stabilizers() - states.append( - np.array([np.concatenate(row.to_numpy()) for row in state], dtype=np.uint8) - ) + rows = [np.concatenate(row.to_numpy()) for row in state] + states.append(np.array(rows, dtype=np.uint8)[rng.permutation(num_qubits)]) - for stabilizers in states: + def check(stabilizers: npt.NDArray[np.uint8]) -> None: + """Check the light errors and the fixed errors of a state against brute force.""" num_qubits = len(stabilizers) - paulis = (np.arange(4**num_qubits)[:, None] >> np.arange(2 * num_qubits)) & 1 - x, z = paulis[:, :num_qubits], paulis[:, num_qubits:] - weights = np.maximum(x.sum(axis=1), z.sum(axis=1)) if css_weight else (x | z).sum(axis=1) - bits = (x @ stabilizers[:, num_qubits:].T + z @ stabilizers[:, :num_qubits].T) % 2 - syndrome_of = bits @ (1 << np.arange(num_qubits)) - - syndromes = list(range(2**num_qubits)) - errors = _fault_search._get_light_errors( - _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight - ) + mask = (1 << num_qubits) - 1 + x_rows = [ + sum(int(bit) << qubit for qubit, bit in enumerate(row[:num_qubits])) + for row in stabilizers + ] + z_rows = [ + sum(int(bit) << qubit for qubit, bit in enumerate(row[num_qubits:])) + for row in stabilizers + ] + + def syndrome_of(pauli: int) -> int: + """Bit ``j`` is whether the error anticommutes with stabilizer ``j``.""" + x, z = pauli & mask, pauli >> num_qubits + return sum( + ((x & z_row ^ z & x_row).bit_count() & 1) << jj + for jj, (x_row, z_row) in enumerate(zip(x_rows, z_rows)) + ) + + def weight_of(pauli: int) -> int: + x, z = pauli & mask, pauli >> num_qubits + return max(x.bit_count(), z.bit_count()) if css_weight else (x | z).bit_count() + + # By brute force, the errors of Pauli weight at most one: X and Z on at most one qubit each, + # on the same qubit for the physical weight. + x_singles = [0] + [1 << qubit for qubit in range(num_qubits)] + z_singles = [0] + [1 << (num_qubits + qubit) for qubit in range(num_qubits)] + light_errors = [ + x | z + for x, z in itertools.product(x_singles, z_singles) + if css_weight or not x or not z or z == x << num_qubits + ] + light_syndromes = {syndrome_of(error) for error in light_errors} + + # Query every syndrome of a small state. Of a large one, query random syndromes, and those + # of the light errors, or of 1000 of them, which keeps the test fast without compilation. + if num_qubits <= 4: + syndromes = list(range(2**num_qubits)) + else: + randoms = [int.from_bytes(rng.bytes(9), "little") & mask for _ in range(100)] + sample = sorted(light_syndromes) + if len(sample) > 1000: + sample = rng.choice(sample, 1000, replace=False).tolist() + syndromes = sample + randoms + rows = _fault_search._hashed_rows(syndromes, (num_qubits + 63) // 64) + errors = _fault_search._get_light_errors(rows, stabilizers, css_weight) for syndrome, error in zip(syndromes, errors, strict=True): - assert (error is None) == (weights[syndrome_of == syndrome].min() > 1) - assert error is None or (syndrome_of[error] == syndrome and weights[error] <= 1) - fixed_errors = _fault_search._get_fixed_errors( - _fault_search._hashed_rows(syndromes, 1), stabilizers, css_weight - ) - assert syndrome_of[fixed_errors].tolist() == syndromes + assert (error is None) == (syndrome not in light_syndromes) + assert error is None or (syndrome_of(error) == syndrome and weight_of(error) <= 1) + fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, css_weight) + assert [syndrome_of(error) for error in fixed_errors] == syndromes if css_weight: # Each part is an error on at most one qubit if one has the syndrome of the part. - for shift, error in itertools.product((0, num_qubits), fixed_errors): - part = error & (((1 << num_qubits) - 1) << shift) - singles = [0] + [1 << (shift + qubit) for qubit in range(num_qubits)] - if syndrome_of[part] in syndrome_of[singles]: - assert part in singles + for singles, part_mask in ((x_singles, mask), (z_singles, mask << num_qubits)): + single_syndromes = {syndrome_of(single) for single in singles} + for error in fixed_errors: + if syndrome_of(error & part_mask) in single_syndromes: + assert error & part_mask in singles + + for stabilizers in states: + check(stabilizers) def test_heavy_primitive_faults_get_products_of_light_representatives( @@ -1431,88 +1481,24 @@ def test_hand_derived_dependency_cases() -> None: def test_batches_have_a_word_for_every_64_output_qubits() -> None: - """Syndromes have a word for every 64 output qubits, in batches and in the light/heavy split. + """Syndromes have a word for every 64 output qubits, in batches and in the search. On 70 qubits, syndromes below ``2**64`` have two words in batches, as the oracle needs. On 65 - qubits, syndromes with bits in both words are split and represented by both words. + qubits, syndromes with bits in both words decide which faults violate the bound. """ # Together, the two primitive faults pass post-selection and leave X on three qubits. failure = _certify_map([1, 1], [0b011, 0b100], None, 70) assert failure is not None and failure.fault_indices == (0, 1) - # On |0...0>, the syndrome of an error is its X part, and the lightest error with a syndrome is - # X on its set bits, so only syndromes of at most one bit are light, by either weight. A - # primitive fault with zero flags that leaves X on qubits 0 and 64 violates the bound alone, and - # with a primitive fault of zero syndrome, one that leaves X on qubits 0, 1, and 64 forms a - # violating fault of fault weight two. - syndromes = [1, 2**64, 2**64 + 1, 2**64 + 3] - rows = _fault_search._hashed_rows(syndromes, 2) - stabilizers = _z_state(65).astype(np.uint8) - for css_weight in (False, True): - light_errors = _fault_search._get_light_errors(rows, stabilizers, css_weight) - assert [error is None for error in light_errors] == [False, False, True, True] - for syndrome, error in zip(syndromes[:2], light_errors[:2], strict=True): - assert error is not None and error & (2**65 - 1) == syndrome - x, z = error & (2**65 - 1), error >> 65 - assert (max(x.bit_count(), z.bit_count()) if css_weight else (x | z).bit_count()) <= 1 - fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, css_weight) - assert [error & (2**65 - 1) for error in fixed_errors] == syndromes + # On |0...0>, a primitive fault with zero flags that leaves X on qubits 0 and 64 violates the + # bound alone. With a primitive fault of zero syndrome, one that leaves X on qubits 0, 1, and + # 64 forms a violating fault of fault weight two, which the search must not skip by the + # representative of the heavy fault. failure = _certify_map([0], [2**64 + 1], None, 65) assert failure is not None and failure.fault_indices == (0,) failure = _certify_map([1, 1], [2**64 + 3, 0], None, 65) assert failure is not None and failure.fault_indices == (0, 1) - # With Z on qubit 0 and Y on qubit 64 as the last stabilizer, the state is not CSS. X on qubit - # 0 flips bits 0 and 64, X or Z on qubit 64 flips bit 64, and X on another qubit j flips bit j. - # By CSS weight, X on qubit 0 and Z on qubit 64 weighs one, so the syndrome 1 is light, though - # no error on one qubit has it, and 2**64 + 3 needs X on qubits 0 and 1, of CSS weight two. - stabilizers[64, [64, 65]] = 1 - - def syndrome_of(pauli: int) -> int: - """The syndrome of an error for the current ``stabilizers``.""" - error = np.array([(pauli >> bit) & 1 for bit in range(130)]) - bits = (stabilizers[:, 65:] @ error[:65] + stabilizers[:, :65] @ error[65:]) % 2 - return sum(int(bit) << jj for jj, bit in enumerate(bits)) - - light_errors = _fault_search._get_light_errors(rows, stabilizers, True) - fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, True) - assert [error is None for error in light_errors] == [False, False, False, True] - for syndrome, light_error, fixed_error in zip( - syndromes, light_errors, fixed_errors, strict=True - ): - assert syndrome_of(fixed_error) == syndrome - if light_error is not None: - assert syndrome_of(light_error) == syndrome - x, z = light_error & (2**65 - 1), light_error >> 65 - assert max(x.bit_count(), z.bit_count()) <= 1 - assert _fault_search.find_smallest_violation([0], [1], stabilizers, None, True) is None - failure = _fault_search.find_smallest_violation([0], [2**64 + 3], stabilizers, None, True) - assert failure is not None and failure.fault_indices == (0,) - assert failure.bound.lower_bound == 2 and syndrome_of(failure.bound.representative) == 2**64 + 3 - - # On |+...+>, with the stabilizers X on qubits 0 and 1, X on qubit 64, X on each qubit j from 2 - # to 63, and X on qubits 0 and 64, Z on qubit 0 has the syndrome 2**64 + 1, and the only Z - # error with the syndrome 2 acts on qubits 0, 1, and 64, so the lightest errors with this - # syndrome weigh three, by either weight. With a primitive fault of zero syndrome, a primitive - # fault with the syndrome 2 forms a violating fault of fault weight two. - stabilizers = np.zeros((65, 130), dtype=np.uint8) - stabilizers[[0, 0, 1, 64, 64], [0, 1, 64, 0, 64]] = 1 - stabilizers[range(2, 64), range(2, 64)] = 1 - rows = _fault_search._hashed_rows([2, 2**64 + 1], 2) - for css_weight in (False, True): - light_errors = _fault_search._get_light_errors(rows, stabilizers, css_weight) - assert light_errors[0] is None and light_errors[1] is not None - assert syndrome_of(light_errors[1]) == 2**64 + 1 - x, z = light_errors[1] & (2**65 - 1), light_errors[1] >> 65 - assert (max(x.bit_count(), z.bit_count()) if css_weight else (x | z).bit_count()) <= 1 - fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, css_weight) - assert [syndrome_of(error) for error in fixed_errors] == [2, 2**64 + 1] - failure = _fault_search.find_smallest_violation( - [1, 1], [2, 0], stabilizers, None, css_weight - ) - assert failure is not None and failure.fault_indices == (0, 1) - assert failure.bound.lower_bound == 3 and syndrome_of(failure.bound.representative) == 2 - def test_failures_report_the_smallest_faults() -> None: """A violating fault of two primitive faults is reported before a violating fault of three.""" From 0a79532e14ec75d5dfc4cfcca1ed6aca24f57a14 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 16:20:00 -0500 Subject: [PATCH 07/11] Restore the end-to-end tests of Z parts in two words and of CSS weight on a state that is not CSS The brute-force test checks representatives before the fault lister reads them, so the test of syndromes in two words again checks, by either weight, that the search reports a violating fault whose heavy fault's lightest errors have Z parts in both words. The test of the CSS weight also checks that a state that is not CSS gets the CSS weight, on three qubits where X on one qubit and Z on another weigh one. The brute-force test runs on one random state of each size from two to four qubits, rather than ten, and says that its syndromes and X and Z parts on 70 qubits span two words. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index a6a0133e5..aa5b22691 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -413,7 +413,8 @@ def test_light_syndromes_match_brute_force(css_weight: bool, pytestconfig: pytes """ # The states of one qubit are those of X, Y, and Z. Those of more qubits come from random # circuits on qubits that start in |0> or, the first ``num_plus`` of them, in |+>, with their - # stabilizers in random order, and those of 70 qubits have syndromes and errors in two words. + # stabilizers in random order, and the syndromes of 70 qubits, and the X and Z parts of their + # errors, span two words. # Circuits of CX gates alone make CSS states. On |+...+>, every fixed error is a Z error that # can act on every qubit. With both X and Z stabilizers, the Z part of a fixed error need not # be the lightest. @@ -421,9 +422,9 @@ def test_light_syndromes_match_brute_force(css_weight: bool, pytestconfig: pytes rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) clifford, cx = ["H", "S", "CX"], ["CX"] for num_qubits, num_states, gates, num_plus in ( - (2, 10, clifford, 0), - (3, 10, clifford, 0), - (4, 10, clifford, 0), + (2, 1, clifford, 0), + (3, 1, clifford, 0), + (4, 1, clifford, 0), (70, 1, clifford, 0), (70, 1, cx, 70), (70, 1, cx, 35), @@ -1431,7 +1432,10 @@ def test_search_with_flags_matches_exhaustive_search(pytestconfig: pytest.Config def test_css_weight_search_matches_exhaustive_search(pytestconfig: pytest.Config) -> None: - """On |00++>, the search matches brute force by CSS weight for random flags and syndromes.""" + """On |00++>, the search matches brute force by CSS weight for random flags and syndromes. + + On a state that is not CSS, CSS weight one also allows X and Z errors on different qubits. + """ # The stabilizers Z0, Z1, X2, and X3 make syndrome bits 0 and 1 need X on qubits 0 and 1, and # bits 2 and 3 need Z on qubits 2 and 3, so the oracle searches both X parts and Z parts. stabilizers = np.zeros((4, 8), dtype=int) @@ -1460,6 +1464,13 @@ def css_weight(syndrome: int) -> int: representative = violation.bound.representative assert (representative & 0b0011) | ((representative >> 4) & 0b1100) == syndrome + # On the state with the stabilizers Z on qubit 0, Z on qubit 1, and Y on qubit 2, X on qubit 0 + # and Z on qubit 2 have the syndrome 5 and weigh one by CSS weight, though no error on one qubit + # has this syndrome. + stabilizers = np.zeros((3, 6), dtype=int) + stabilizers[[0, 1, 2, 2], [3, 4, 2, 5]] = 1 + assert _fault_search.find_smallest_violation([0], [5], stabilizers, None, True) is None + def test_hand_derived_dependency_cases() -> None: """Small faults, repeated and zero signatures, and ``max_weight`` give hand-derived results.""" @@ -1499,6 +1510,21 @@ def test_batches_have_a_word_for_every_64_output_qubits() -> None: failure = _certify_map([1, 1], [2**64 + 3, 0], None, 65) assert failure is not None and failure.fault_indices == (0, 1) + # On |+...+>, with the stabilizers X on qubits 0 and 1, X on qubit 64, X on each qubit j from 2 + # to 63, and X on qubits 0 and 64, the only Z error with the syndrome 2 acts on qubits 0, 1, and + # 64, so the lightest errors with this syndrome weigh three, by either weight. With a primitive + # fault of zero syndrome, a primitive fault with the syndrome 2 forms a violating fault of fault + # weight two, which the search must not skip by the Z part of the heavy fault's representative. + stabilizers = np.zeros((65, 130), dtype=np.uint8) + stabilizers[[0, 0, 1, 64, 64], [0, 1, 64, 0, 64]] = 1 + stabilizers[range(2, 64), range(2, 64)] = 1 + for css_weight in (False, True): + failure = _fault_search.find_smallest_violation( + [1, 1], [2, 0], stabilizers, None, css_weight + ) + assert failure is not None and failure.fault_indices == (0, 1) + assert failure.bound.lower_bound == 3 + def test_failures_report_the_smallest_faults() -> None: """A violating fault of two primitive faults is reported before a violating fault of three.""" From 15e22b3bfc16fb36a2fb951be95562b657e67d73 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Wed, 25 Nov 2026 23:43:36 -0500 Subject: [PATCH 08/11] Check the search against brute force on small states, in place of hand-written cases The brute-force test of the light syndromes now also runs the search on random states of three to six qubits, CSS and not, by either weight, against brute force over all their Pauli errors. For random flags and syndromes, it checks that the search reports a violating fault of the smallest fault weight with a valid bound, and that the fault lister gets the primitive faults with nonzero flags, the heavy ones first, each with a representative that has its syndrome and Pauli weight at most one if it is light, or at most two if its syndrome is the sum of those of two light ones. The hand-written cases that it covers are gone: the products of light representatives on three qubits, the CSS weight on a state that is not CSS, and the faults on 65 qubits. The case on 70 qubits remains, and now says that its heavy syndrome, below 2**64, gets a fixed error from both words. The random states come from a helper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 208 +++++++++--------- 1 file changed, 101 insertions(+), 107 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index aa5b22691..12331ddc1 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -404,43 +404,61 @@ def test_rows_hash_alike_whenever_they_are_hashed() -> None: assert _fault_search._hashed_rows([3], 1).tolist() == heavy.tolist() +def _random_state( + rng: np.random.Generator, num_qubits: int, clifford: bool, num_plus: int +) -> npt.NDArray[np.uint8]: + """The stabilizers, in random order, of a random state. + + The state comes from a random circuit on qubits that start in |0> or, the first ``num_plus`` of + them, in |+>. The circuit has H, S, and CX gates if ``clifford`` is true, and otherwise CX + gates alone, which make CSS states. + """ + circuit = stim.Circuit() + circuit.append("I", range(num_qubits)) + circuit.append("H", range(num_plus)) + gates = ["H", "S", "CX"] if clifford else ["CX"] + for gate in rng.choice(gates, size=8 * num_qubits): + if gate == "CX": + circuit.append("CX", rng.permutation(num_qubits)[:2].tolist()) + else: + circuit.append(gate, [int(rng.integers(num_qubits))]) + state = stim.Tableau.from_circuit(circuit).to_stabilizers() + rows = [np.concatenate(row.to_numpy()) for row in state] + return np.array(rows, dtype=np.uint8)[rng.permutation(num_qubits)] + + @pytest.mark.parametrize("css_weight", [False, True]) -def test_light_syndromes_match_brute_force(css_weight: bool, pytestconfig: pytest.Config) -> None: +def test_light_syndromes_match_brute_force( + css_weight: bool, pytestconfig: pytest.Config, monkeypatch: pytest.MonkeyPatch +) -> None: """A syndrome gets an error of Pauli weight at most one exactly if it has one, on any state. Every syndrome also gets a fixed error with it, whose X and Z parts are single-qubit errors - where some have their syndromes, for the CSS weight. + where some have their syndromes, for the CSS weight. On small states, the search matches brute + force for random flags and syndromes, and the lister gets the primitive faults with nonzero + flags, the heavy ones first. The representative of each has its syndrome, and has Pauli + weight at most one if it is light, or at most two if its syndrome is the sum of those of two + light ones. """ - # The states of one qubit are those of X, Y, and Z. Those of more qubits come from random - # circuits on qubits that start in |0> or, the first ``num_plus`` of them, in |+>, with their - # stabilizers in random order, and the syndromes of 70 qubits, and the X and Z parts of their - # errors, span two words. - # Circuits of CX gates alone make CSS states. On |+...+>, every fixed error is a Z error that - # can act on every qubit. With both X and Z stabilizers, the Z part of a fixed error need not - # be the lightest. + # The states of one qubit are those of X, Y, and Z. Those of more qubits are random, and the + # syndromes of 70 qubits, and the X and Z parts of their errors, span two words. On |+...+>, + # every fixed error is a Z error that can act on every qubit. With both X and Z stabilizers, + # the Z part of a fixed error need not be the lightest. On five or six qubits, most syndromes + # are heavy, and some are not the sum of two light ones. states = [np.array([row], dtype=np.uint8) for row in ([1, 0], [1, 1], [0, 1])] rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) - clifford, cx = ["H", "S", "CX"], ["CX"] - for num_qubits, num_states, gates, num_plus in ( - (2, 1, clifford, 0), - (3, 1, clifford, 0), - (4, 1, clifford, 0), - (70, 1, clifford, 0), - (70, 1, cx, 70), - (70, 1, cx, 35), + for num_qubits, clifford, num_plus in ( + (3, True, 0), + (5, True, 0), + (6, True, 0), + (6, False, 3), + (70, True, 0), + (70, False, 70), + (70, False, 35), ): - for _ in range(num_states): - circuit = stim.Circuit() - circuit.append("I", range(num_qubits)) - circuit.append("H", range(num_plus)) - for gate in rng.choice(gates, size=8 * num_qubits): - if gate == "CX": - circuit.append("CX", rng.permutation(num_qubits)[:2].tolist()) - else: - circuit.append(gate, [int(rng.integers(num_qubits))]) - state = stim.Tableau.from_circuit(circuit).to_stabilizers() - rows = [np.concatenate(row.to_numpy()) for row in state] - states.append(np.array(rows, dtype=np.uint8)[rng.permutation(num_qubits)]) + states.append(_random_state(rng, num_qubits, clifford, num_plus)) + lister = mock.Mock(wraps=_fault_search.FaultLister) + monkeypatch.setattr(_fault_search, "FaultLister", lister) def check(stabilizers: npt.NDArray[np.uint8]) -> None: """Check the light errors and the fixed errors of a state against brute force.""" @@ -480,7 +498,7 @@ def weight_of(pauli: int) -> int: # Query every syndrome of a small state. Of a large one, query random syndromes, and those # of the light errors, or of 1000 of them, which keeps the test fast without compilation. - if num_qubits <= 4: + if num_qubits <= 6: syndromes = list(range(2**num_qubits)) else: randoms = [int.from_bytes(rng.bytes(9), "little") & mask for _ in range(100)] @@ -503,47 +521,57 @@ def weight_of(pauli: int) -> int: if syndrome_of(error & part_mask) in single_syndromes: assert error & part_mask in singles - for stabilizers in states: - check(stabilizers) - - -def test_heavy_primitive_faults_get_products_of_light_representatives( - monkeypatch: pytest.MonkeyPatch, -) -> None: - """A heavy primitive fault gets the product of the representatives of its first light pair. - - The pair starts with the first light syndrome, in the order of the primitive faults, whose sum - with the heavy syndrome is also light. Every representative has the syndrome of its primitive - fault, and that of a light primitive fault has Pauli weight at most one. - """ - # The stabilizers ZXX, X on qubit 2, and XZX make the syndromes 1, 4, 5, and 7 light. With the - # light syndromes 5, 7, and 4 of these faults, the heavy syndrome 2, which repeats, is 5 + 7, - # the heavy syndrome 3 is 7 + 4, and the heavy syndrome 6 is no such sum. - stabilizers = np.array([[0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0], [1, 0, 1, 0, 1, 0]]) - syndromes = [5, 7, 4, 2, 2, 3, 6] - lister = mock.Mock(wraps=_fault_search.FaultLister) - monkeypatch.setattr(_fault_search, "FaultLister", lister) - flags = [1 << jj for jj in range(len(syndromes))] - assert _fault_search.find_smallest_violation(flags, syndromes, stabilizers, None, False) is None - _, primitive_syndromes, representatives, *_, num_starts, _ = lister.call_args.args - - def syndrome_of(pauli: int) -> int: - x = [(pauli >> qubit) & 1 for qubit in range(3)] - z = [(pauli >> (3 + qubit)) & 1 for qubit in range(3)] - return sum( - int(np.dot(x, row[3:]) + np.dot(z, row[:3])) % 2 << jj - for jj, row in enumerate(stabilizers) + def check_search(stabilizers: npt.NDArray[np.uint8]) -> None: + """Check the search on a small state against brute force over all its errors.""" + # By brute force, the syndrome and the weight of each Pauli error, packed into its index. + num_qubits = len(stabilizers) + bits = (np.arange(4**num_qubits)[:, None] >> np.arange(2 * num_qubits)) & 1 + x, z = bits[:, :num_qubits], bits[:, num_qubits:] + pauli_weights = ( + np.maximum(x.sum(axis=1), z.sum(axis=1)) if css_weight else (x | z).sum(axis=1) ) + syndrome_bits = x @ stabilizers[:, num_qubits:].T + z @ stabilizers[:, :num_qubits].T + pauli_syndromes = (syndrome_bits % 2) @ (1 << np.arange(num_qubits)) + minimum = { + ss: int(pauli_weights[pauli_syndromes == ss].min()) for ss in range(2**num_qubits) + } - for syndrome, representative in zip(primitive_syndromes, representatives, strict=True): - assert syndrome_of(representative) == syndrome + for rank in [0, 1, 2, 3] * 6: + flags = list(map(int, rng.integers(min(1, rank), 1 << rank, size=8))) + syndromes = list(map(int, rng.integers(2**num_qubits, size=8))) + violation = _fault_search.find_smallest_violation( + flags, syndromes, stabilizers, None, css_weight + ) + smallest = _smallest_violation(flags, syndromes, None, minimum.__getitem__) + assert (violation is None) == (smallest is None) + if violation is not None: + indices = violation.fault_indices + assert len(indices) == smallest and _xor(flags, indices) == 0 + assert violation.syndrome == _xor(syndromes, indices) + assert not violation.bound.within_max_weight + assert smallest < violation.bound.lower_bound <= minimum[violation.syndrome] + assert pauli_syndromes[violation.bound.representative] == violation.syndrome + + for call in lister.call_args_list: + listed_flags, listed_syndromes, representatives, *_, num_starts, _ = call.args + assert sorted(zip(listed_flags, listed_syndromes)) == sorted( + {(ff, ss) for ff, ss in zip(flags, syndromes, strict=True) if ff} + ) + heavy = [minimum[ss] > 1 for ss in listed_syndromes] + assert heavy == [True] * num_starts + [False] * (len(heavy) - num_starts) + light = set(listed_syndromes[num_starts:]) + for syndrome, representative in zip(listed_syndromes, representatives, strict=True): + assert pauli_syndromes[representative] == syndrome + if syndrome in light: + assert pauli_weights[representative] <= 1 + elif any(syndrome ^ ss in light for ss in light): + assert pauli_weights[representative] <= 2 + lister.reset_mock() - light = dict(zip(primitive_syndromes[num_starts:], representatives[num_starts:], strict=True)) - assert list(light) == [5, 7, 4] - assert all(((pauli & 0b111) | (pauli >> 3)).bit_count() <= 1 for pauli in light.values()) - assert primitive_syndromes[:num_starts] == [2, 2, 3, 6] - expected = [light[5] ^ light[7], light[5] ^ light[7], light[7] ^ light[4]] - assert list(representatives[:3]) == expected + for stabilizers in states: + check(stabilizers) + if len(stabilizers) <= 6: + check_search(stabilizers) @pytest.mark.parametrize("css_weight", [False, True]) @@ -1432,10 +1460,7 @@ def test_search_with_flags_matches_exhaustive_search(pytestconfig: pytest.Config def test_css_weight_search_matches_exhaustive_search(pytestconfig: pytest.Config) -> None: - """On |00++>, the search matches brute force by CSS weight for random flags and syndromes. - - On a state that is not CSS, CSS weight one also allows X and Z errors on different qubits. - """ + """On |00++>, the search matches brute force by CSS weight for random flags and syndromes.""" # The stabilizers Z0, Z1, X2, and X3 make syndrome bits 0 and 1 need X on qubits 0 and 1, and # bits 2 and 3 need Z on qubits 2 and 3, so the oracle searches both X parts and Z parts. stabilizers = np.zeros((4, 8), dtype=int) @@ -1464,13 +1489,6 @@ def css_weight(syndrome: int) -> int: representative = violation.bound.representative assert (representative & 0b0011) | ((representative >> 4) & 0b1100) == syndrome - # On the state with the stabilizers Z on qubit 0, Z on qubit 1, and Y on qubit 2, X on qubit 0 - # and Z on qubit 2 have the syndrome 5 and weigh one by CSS weight, though no error on one qubit - # has this syndrome. - stabilizers = np.zeros((3, 6), dtype=int) - stabilizers[[0, 1, 2, 2], [3, 4, 2, 5]] = 1 - assert _fault_search.find_smallest_violation([0], [5], stabilizers, None, True) is None - def test_hand_derived_dependency_cases() -> None: """Small faults, repeated and zero signatures, and ``max_weight`` give hand-derived results.""" @@ -1492,39 +1510,15 @@ def test_hand_derived_dependency_cases() -> None: def test_batches_have_a_word_for_every_64_output_qubits() -> None: - """Syndromes have a word for every 64 output qubits, in batches and in the search. + """On 70 qubits, syndromes below ``2**64`` have two words, wherever the search packs them. - On 70 qubits, syndromes below ``2**64`` have two words in batches, as the oracle needs. On 65 - qubits, syndromes with bits in both words decide which faults violate the bound. + Batches need them, as the oracle does, and so do the fixed errors of heavy syndromes. """ - # Together, the two primitive faults pass post-selection and leave X on three qubits. + # Together, the two primitive faults pass post-selection and leave X on three qubits. The heavy + # syndrome 3 is not the sum of two light ones, so it gets a fixed error. failure = _certify_map([1, 1], [0b011, 0b100], None, 70) assert failure is not None and failure.fault_indices == (0, 1) - # On |0...0>, a primitive fault with zero flags that leaves X on qubits 0 and 64 violates the - # bound alone. With a primitive fault of zero syndrome, one that leaves X on qubits 0, 1, and - # 64 forms a violating fault of fault weight two, which the search must not skip by the - # representative of the heavy fault. - failure = _certify_map([0], [2**64 + 1], None, 65) - assert failure is not None and failure.fault_indices == (0,) - failure = _certify_map([1, 1], [2**64 + 3, 0], None, 65) - assert failure is not None and failure.fault_indices == (0, 1) - - # On |+...+>, with the stabilizers X on qubits 0 and 1, X on qubit 64, X on each qubit j from 2 - # to 63, and X on qubits 0 and 64, the only Z error with the syndrome 2 acts on qubits 0, 1, and - # 64, so the lightest errors with this syndrome weigh three, by either weight. With a primitive - # fault of zero syndrome, a primitive fault with the syndrome 2 forms a violating fault of fault - # weight two, which the search must not skip by the Z part of the heavy fault's representative. - stabilizers = np.zeros((65, 130), dtype=np.uint8) - stabilizers[[0, 0, 1, 64, 64], [0, 1, 64, 0, 64]] = 1 - stabilizers[range(2, 64), range(2, 64)] = 1 - for css_weight in (False, True): - failure = _fault_search.find_smallest_violation( - [1, 1], [2, 0], stabilizers, None, css_weight - ) - assert failure is not None and failure.fault_indices == (0, 1) - assert failure.bound.lower_bound == 3 - def test_failures_report_the_smallest_faults() -> None: """A violating fault of two primitive faults is reported before a violating fault of three.""" From 1812613e26f01ff294d3f6209bef0d97943c6051 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Thu, 26 Nov 2026 01:07:08 -0500 Subject: [PATCH 09/11] Test the reported fault, syndromes in three words, and representatives that cancel The search reports the same violating fault in every version, so the brute-force test of the light syndromes now checks that the fault lister gets the heavy primitive faults and then the light ones, each in DEM order, and that a violating primitive fault with zero flags is the first in DEM order. Two cases on |0...0> check the reported fault of fault weight two when two violating faults compete, one for the order of the heavy primitive faults and one for that of the light ones. The test of words now runs on either side of 64 and 128 qubits, where syndromes with only their first or their last bit must be light, and on 65 qubits checks the representatives that the lister gets for two heavy syndromes in the first word, whose fixed errors need both words. The test of light pairs uses syndromes in three words, so that lookups with colliding hashes compare the third. The test of the CSS weight gains a case where a heavy primitive fault's representative is the product of two light ones in which a Z error cancels. The three states of one qubit are gone from the brute-force test, which catches the same faults in the search without them. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 143 +++++++++++++----- 1 file changed, 103 insertions(+), 40 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index 12331ddc1..f72f03c39 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -366,16 +366,25 @@ def test_light_pairs_match_brute_force( The second list can repeat a syndrome, and its first entry with the sum is the one returned. """ - # With zero hashes, lookups must compare syndromes. Syndromes of bits 62 to 65 span two words. + # With zero hashes, lookups must compare syndromes. Syndromes of bits 63, 64, 127, and 128 span + # three words. if collide: monkeypatch.setattr( _fault_search, "_hash_words", lambda words: np.zeros(len(words), np.uint64) ) rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) + + def spread(values: npt.NDArray[np.int_]) -> list[int]: + """Move bits 0 to 3 of each value to bits 63, 64, 127, and 128.""" + return [ + sum(((int(value) >> bit) & 1) << shift for bit, shift in enumerate((63, 64, 127, 128))) + for value in values + ] + for _ in range(20): - first_list = [int(ss) << 62 for ss in rng.integers(16, size=4)] - second_list = [int(ss) << 62 for ss in rng.integers(16, size=6)] - syndromes = [int(ss) << 62 for ss in rng.integers(16, size=8)] + first_list = spread(rng.integers(16, size=4)) + second_list = spread(rng.integers(16, size=6)) + syndromes = spread(rng.integers(16, size=8)) num_words = _fault_search._get_num_words_for_values(first_list + second_list + syndromes) pairs = _fault_search._get_light_pairs( _fault_search._hashed_rows(syndromes, num_words), @@ -435,28 +444,29 @@ def test_light_syndromes_match_brute_force( Every syndrome also gets a fixed error with it, whose X and Z parts are single-qubit errors where some have their syndromes, for the CSS weight. On small states, the search matches brute - force for random flags and syndromes, and the lister gets the primitive faults with nonzero - flags, the heavy ones first. The representative of each has its syndrome, and has Pauli - weight at most one if it is light, or at most two if its syndrome is the sum of those of two - light ones. + force for random flags and syndromes, and reports the first heavy primitive fault with zero + flags in DEM order if one violates the bound. The lister gets the primitive faults with + nonzero flags, the heavy ones and then the light ones, each in DEM order. The representative + of each has its syndrome, and has Pauli weight at most one if it is light, or at most two if + its syndrome is the sum of those of two light ones. """ - # The states of one qubit are those of X, Y, and Z. Those of more qubits are random, and the - # syndromes of 70 qubits, and the X and Z parts of their errors, span two words. On |+...+>, - # every fixed error is a Z error that can act on every qubit. With both X and Z stabilizers, - # the Z part of a fixed error need not be the lightest. On five or six qubits, most syndromes - # are heavy, and some are not the sum of two light ones. - states = [np.array([row], dtype=np.uint8) for row in ([1, 0], [1, 1], [0, 1])] + # The states are random, and the syndromes of 70 qubits, and the X and Z parts of their errors, + # span two words. On |+...+>, every fixed error is a Z error that can act on every qubit. With + # both X and Z stabilizers, the Z part of a fixed error need not be the lightest. On five or + # six qubits, most syndromes are heavy, and some are not the sum of two light ones. rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) - for num_qubits, clifford, num_plus in ( - (3, True, 0), - (5, True, 0), - (6, True, 0), - (6, False, 3), - (70, True, 0), - (70, False, 70), - (70, False, 35), - ): - states.append(_random_state(rng, num_qubits, clifford, num_plus)) + states = [ + _random_state(rng, num_qubits, clifford, num_plus) + for num_qubits, clifford, num_plus in ( + (3, True, 0), + (5, True, 0), + (6, True, 0), + (6, False, 3), + (70, True, 0), + (70, False, 70), + (70, False, 35), + ) + ] lister = mock.Mock(wraps=_fault_search.FaultLister) monkeypatch.setattr(_fault_search, "FaultLister", lister) @@ -551,14 +561,23 @@ def check_search(stabilizers: npt.NDArray[np.uint8]) -> None: assert not violation.bound.within_max_weight assert smallest < violation.bound.lower_bound <= minimum[violation.syndrome] assert pauli_syndromes[violation.bound.representative] == violation.syndrome + if smallest == 1: + first = next( + jj + for jj, (flag, syndrome) in enumerate(zip(flags, syndromes)) + if not flag and minimum[syndrome] > 1 + ) + assert indices == (first,) + # Each signature with nonzero flags, once, in DEM order. + signatures = [pair for pair in dict.fromkeys(zip(flags, syndromes)) if pair[0]] + heavy_signatures = [pair for pair in signatures if minimum[pair[1]] > 1] + light_signatures = [pair for pair in signatures if minimum[pair[1]] <= 1] for call in lister.call_args_list: listed_flags, listed_syndromes, representatives, *_, num_starts, _ = call.args - assert sorted(zip(listed_flags, listed_syndromes)) == sorted( - {(ff, ss) for ff, ss in zip(flags, syndromes, strict=True) if ff} - ) - heavy = [minimum[ss] > 1 for ss in listed_syndromes] - assert heavy == [True] * num_starts + [False] * (len(heavy) - num_starts) + listed = list(zip(listed_flags, listed_syndromes)) + assert listed == heavy_signatures + light_signatures + assert num_starts == len(heavy_signatures) light = set(listed_syndromes[num_starts:]) for syndrome, representative in zip(listed_syndromes, representatives, strict=True): assert pauli_syndromes[representative] == syndrome @@ -1421,11 +1440,18 @@ def _smallest_violation( def _certify_map( - flags: Sequence[int], syndromes: Sequence[int], max_weight: int | None, num_qubits: int + flags: Sequence[int], + syndromes: Sequence[int], + max_weight: int | None, + num_qubits: int, + css_weight: bool = False, ) -> _fault_search.Violation | None: - """Search primitive faults with these flags and syndromes on |0...0> against brute force.""" + """Search primitive faults with these flags and syndromes on |0...0> against brute force. + + On |0...0>, the lightest errors are X errors, which weigh the same by either weight. + """ violation = _fault_search.find_smallest_violation( - flags, syndromes, _z_state(num_qubits), max_weight, False + flags, syndromes, _z_state(num_qubits), max_weight, css_weight ) smallest = _smallest_violation(flags, syndromes, max_weight) assert (violation is None) == (smallest is None) @@ -1489,6 +1515,19 @@ def css_weight(syndrome: int) -> int: representative = violation.bound.representative assert (representative & 0b0011) | ((representative >> 4) & 0b1100) == syndrome + # On the state with the stabilizers Z0, Z1, X2, X3, and X4, the primitive faults 0 and 1, with + # the syndromes of X0 X1 and X0 X1 Z2 Z3 Z4, are heavy, and 2 and 3, with those of X0 Z2 and + # X1 Z2, are light. The representative of 0 is the product of those of 2 and 3, in which Z2 + # cancels, and {0, 1} leaves Z2 Z3 Z4, of CSS weight three. + stabilizers = np.zeros((5, 10), dtype=int) + stabilizers[[0, 1], [5, 6]] = 1 + stabilizers[[2, 3, 4], [2, 3, 4]] = 1 + failure = _fault_search.find_smallest_violation( + [1, 1, 2, 4], [3, 31, 5, 6], stabilizers, None, True + ) + assert failure is not None and failure.fault_indices == (0, 1) + assert failure.syndrome == 28 and failure.bound.lower_bound == 3 + def test_hand_derived_dependency_cases() -> None: """Small faults, repeated and zero signatures, and ``max_weight`` give hand-derived results.""" @@ -1509,24 +1548,48 @@ def test_hand_derived_dependency_cases() -> None: assert (failure is None) == certified -def test_batches_have_a_word_for_every_64_output_qubits() -> None: - """On 70 qubits, syndromes below ``2**64`` have two words, wherever the search packs them. +def test_syndromes_have_a_word_for_every_64_output_qubits(monkeypatch: pytest.MonkeyPatch) -> None: + """Syndromes have a word for every 64 output qubits, wherever the search packs them. - Batches need them, as the oracle does, and so do the fixed errors of heavy syndromes. + On either side of 64 and 128 qubits, syndromes with only their first or their last bit are + light. On 65 qubits, syndromes in the first word get both words, as batches and the oracle + need, and as do the fixed errors of heavy syndromes, which the lister gets with them. """ - # Together, the two primitive faults pass post-selection and leave X on three qubits. The heavy - # syndrome 3 is not the sum of two light ones, so it gets a fixed error. - failure = _certify_map([1, 1], [0b011, 0b100], None, 70) - assert failure is not None and failure.fault_indices == (0, 1) + # The CSS weight takes less time on many qubits, and the physical weight takes the same words. + for num_qubits in (63, 64, 65, 127, 128, 129): + syndromes = [1, 1 << (num_qubits - 1)] + assert _certify_map([0, 0], syndromes, None, num_qubits, True) is None + + # Together, the primitive faults 0 and 2 pass post-selection and leave X on three qubits. The + # heavy syndromes 3 and 5 are not sums of two light ones, so they get fixed errors. On |0...0>, + # the X part of an error with a syndrome is the syndrome. + lister = mock.Mock(wraps=_fault_search.FaultLister) + monkeypatch.setattr(_fault_search, "FaultLister", lister) + failure = _certify_map([1, 1, 1], [0b011, 0b101, 0b100], None, 65) + assert failure is not None and failure.fault_indices == (0, 2) + _, syndromes, representatives, *_ = lister.call_args.args + assert [representative & (2**65 - 1) for representative in representatives] == syndromes def test_failures_report_the_smallest_faults() -> None: - """A violating fault of two primitive faults is reported before a violating fault of three.""" + """A violating fault of two primitive faults is reported before a violating fault of three. + + Of two violating faults of the smallest fault weight, the reported one is the same in every + version: here the one with the earlier heavy primitive fault in DEM order, or, if both have the + same heavy one, with the earlier light one. + """ # The primitive faults 0-2 form an accepted fault of residual weight four, and the primitive # faults 3 and 4 the only accepted fault of fault weight two, which has residual weight three. failure = _certify_map([1, 2, 3, 4, 4], [1, 2, 12, 1, 6], None, 6) assert failure is not None and set(failure.fault_indices) == {3, 4} + # On |0...0>, the primitive faults 0 and 2 are heavy, with the larger syndrome first, and 1 and + # 3 are light, and {0, 1} and {2, 3} violate the bound. Then 0 is heavy and 1 and 2 are light, + # and {0, 1} and {0, 2} violate the bound. + for flags, syndromes in (([1, 1, 2, 2], [24, 1, 3, 4]), ([1, 1, 1], [3, 4, 8])): + failure = _certify_map(flags, syndromes, None, 5) + assert failure is not None and failure.fault_indices == (0, 1) + def test_light_primitive_faults_are_never_searched(monkeypatch: pytest.MonkeyPatch) -> None: """Without a heavy primitive fault, no fault can violate the bound, so none is listed.""" From fbed5bd32bb727c3def2dff738f8197755b99848 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Thu, 26 Nov 2026 01:41:02 -0500 Subject: [PATCH 10/11] Test words by either weight, and light pairs of lists of any length The test of words now runs by either weight on either side of 64 and 128 qubits. On |0...0>, syndromes with only their first or their last bit must be light, and the lister must get representatives with the syndromes of their primitive faults, among them the fixed error of a heavy syndrome with its last bit. Near 128 qubits, where the search takes longer by the physical weight, the light and fixed errors of the physical weight are checked alone. The test of light pairs now draws its first list, its second list, and its syndromes with lengths that vary independently, each empty, of one syndrome, or longer, so that a first list can be longer or shorter than the second, with ordinary and colliding hashes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 86 ++++++++++++------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index f72f03c39..22c7e03c4 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -365,40 +365,45 @@ def test_light_pairs_match_brute_force( """Each syndrome s gets the first syndrome i of a first list such that s + i is in a second. The second list can repeat a syndrome, and its first entry with the sum is the one returned. + Each list can be empty, or hold one syndrome, or be longer or shorter than the others. """ - # With zero hashes, lookups must compare syndromes. Syndromes of bits 63, 64, 127, and 128 span - # three words. + # With zero hashes, lookups must compare syndromes. Syndromes of bits 63, 64, and 128 span + # three words, and with only eight of them, many sums are in the second list. if collide: monkeypatch.setattr( _fault_search, "_hash_words", lambda words: np.zeros(len(words), np.uint64) ) rng = np.random.default_rng(pytestconfig.getoption("randomly_seed")) - def spread(values: npt.NDArray[np.int_]) -> list[int]: - """Move bits 0 to 3 of each value to bits 63, 64, 127, and 128.""" - return [ - sum(((int(value) >> bit) & 1) << shift for bit, shift in enumerate((63, 64, 127, 128))) - for value in values - ] + def spread(size: int) -> list[int]: + """Random syndromes of bits 63, 64, and 128.""" + values = rng.integers(8, size=size).tolist() + return [(value & 1) << 63 | (value & 2) << 63 | (value & 4) << 126 for value in values] - for _ in range(20): - first_list = spread(rng.integers(16, size=4)) - second_list = spread(rng.integers(16, size=6)) - syndromes = spread(rng.integers(16, size=8)) - num_words = _fault_search._get_num_words_for_values(first_list + second_list + syndromes) - pairs = _fault_search._get_light_pairs( - _fault_search._hashed_rows(syndromes, num_words), - _fault_search._hashed_rows(first_list, num_words), - _fault_search._hashed_rows(second_list, num_words), - ) - for syndrome, pair in zip(syndromes, pairs.tolist(), strict=True): - first = next( - (ii for ii, ss in enumerate(first_list) if syndrome ^ ss in second_list), -1 + for num_first, num_second, num_syndromes in itertools.product( + (0, 1, 4), (0, 1, 2, 6), (0, 1, 8) + ): + for _ in range(2): + first_list, second_list, syndromes = map(spread, (num_first, num_second, num_syndromes)) + num_words = _fault_search._get_num_words_for_values( + first_list + second_list + syndromes ) - expected = ( - [first, second_list.index(syndrome ^ first_list[first])] if first >= 0 else [-1, -1] + pairs = _fault_search._get_light_pairs( + _fault_search._hashed_rows(syndromes, num_words), + _fault_search._hashed_rows(first_list, num_words), + _fault_search._hashed_rows(second_list, num_words), ) - assert pair == expected + assert pairs.shape == (len(syndromes), 2) + for syndrome, pair in zip(syndromes, pairs.tolist(), strict=True): + first = next( + (ii for ii, ss in enumerate(first_list) if syndrome ^ ss in second_list), -1 + ) + expected = ( + [first, second_list.index(syndrome ^ first_list[first])] + if first >= 0 + else [-1, -1] + ) + assert pair == expected def test_rows_hash_alike_whenever_they_are_hashed() -> None: @@ -1551,14 +1556,33 @@ def test_hand_derived_dependency_cases() -> None: def test_syndromes_have_a_word_for_every_64_output_qubits(monkeypatch: pytest.MonkeyPatch) -> None: """Syndromes have a word for every 64 output qubits, wherever the search packs them. - On either side of 64 and 128 qubits, syndromes with only their first or their last bit are - light. On 65 qubits, syndromes in the first word get both words, as batches and the oracle - need, and as do the fixed errors of heavy syndromes, which the lister gets with them. + On either side of 64 and 128 qubits, by either weight, syndromes with only their first or their + last bit are light, and the representatives of light and heavy syndromes have them. On 65 + qubits, syndromes in the first word get both words, as batches and the oracle need, and as do + the fixed errors of heavy syndromes, which the lister gets with them. """ - # The CSS weight takes less time on many qubits, and the physical weight takes the same words. - for num_qubits in (63, 64, 65, 127, 128, 129): - syndromes = [1, 1 << (num_qubits - 1)] - assert _certify_map([0, 0], syndromes, None, num_qubits, True) is None + # On |0...0>, the X part of an error with a syndrome is the syndrome. The syndrome ``last + 3`` + # is heavy, but with 2 it forms a fault of residual weight two. The search runs by either + # weight near 64 qubits, and by the CSS weight near 128 qubits, where the physical weight takes + # more time, so there the light and fixed errors of the physical weight are checked alone. + lister = mock.Mock(wraps=_fault_search.FaultLister) + monkeypatch.setattr(_fault_search, "FaultLister", lister) + for num_qubits, css_weight in itertools.product((63, 64, 65, 127, 128, 129), (False, True)): + mask = 2**num_qubits - 1 + last = 1 << (num_qubits - 1) + syndromes = [1, last, last | 3, 2] + if num_qubits < 127 or css_weight: + assert _certify_map([0, 0, 1, 1], syndromes, None, num_qubits, css_weight) is None + _, listed, representatives, *_ = lister.call_args.args + assert [representative & mask for representative in representatives] == listed + else: + rows = _fault_search._hashed_rows(syndromes, (num_qubits + 63) // 64) + stabilizers = _z_state(num_qubits).astype(np.uint8) + errors = _fault_search._get_light_errors(rows, stabilizers, False) + x_parts = [None if error is None else error & mask for error in errors] + assert x_parts == [1, last, None, 2] + fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, False) + assert [error & mask for error in fixed_errors] == syndromes # Together, the primitive faults 0 and 2 pass post-selection and leave X on three qubits. The # heavy syndromes 3 and 5 are not sums of two light ones, so they get fixed errors. On |0...0>, From 926f9d2e902f6e09233d3e3f69d7e9e7c9dce990 Mon Sep 17 00:00:00 2001 From: "Michael A. Perlin" Date: Thu, 26 Nov 2026 11:06:29 -0500 Subject: [PATCH 11/11] Trim tests to the behavior this change introduces Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../certification/_fault_search_test.py | 114 ++++++------------ 1 file changed, 35 insertions(+), 79 deletions(-) diff --git a/src/qldpc/circuits/certification/_fault_search_test.py b/src/qldpc/circuits/certification/_fault_search_test.py index 22c7e03c4..820c5f103 100644 --- a/src/qldpc/circuits/certification/_fault_search_test.py +++ b/src/qldpc/circuits/certification/_fault_search_test.py @@ -364,8 +364,8 @@ def test_light_pairs_match_brute_force( ) -> None: """Each syndrome s gets the first syndrome i of a first list such that s + i is in a second. - The second list can repeat a syndrome, and its first entry with the sum is the one returned. - Each list can be empty, or hold one syndrome, or be longer or shorter than the others. + The second list can repeat a syndrome. Each list can be empty, or longer or shorter than the + others. """ # With zero hashes, lookups must compare syndromes. Syndromes of bits 63, 64, and 128 span # three words, and with only eight of them, many sums are in the second list. @@ -380,30 +380,21 @@ def spread(size: int) -> list[int]: values = rng.integers(8, size=size).tolist() return [(value & 1) << 63 | (value & 2) << 63 | (value & 4) << 126 for value in values] - for num_first, num_second, num_syndromes in itertools.product( - (0, 1, 4), (0, 1, 2, 6), (0, 1, 8) - ): - for _ in range(2): - first_list, second_list, syndromes = map(spread, (num_first, num_second, num_syndromes)) - num_words = _fault_search._get_num_words_for_values( - first_list + second_list + syndromes - ) - pairs = _fault_search._get_light_pairs( - _fault_search._hashed_rows(syndromes, num_words), - _fault_search._hashed_rows(first_list, num_words), - _fault_search._hashed_rows(second_list, num_words), + for sizes in itertools.product((0, 4), (0, 6), (0, 8)): + first_list, second_list, syndromes = map(spread, sizes) + num_words = _fault_search._get_num_words_for_values(first_list + second_list + syndromes) + pairs = _fault_search._get_light_pairs( + _fault_search._hashed_rows(syndromes, num_words), + _fault_search._hashed_rows(first_list, num_words), + _fault_search._hashed_rows(second_list, num_words), + ) + assert pairs.shape == (len(syndromes), 2) + for syndrome, (first, second) in zip(syndromes, pairs.tolist(), strict=True): + expected = next( + (ii for ii, ss in enumerate(first_list) if syndrome ^ ss in second_list), -1 ) - assert pairs.shape == (len(syndromes), 2) - for syndrome, pair in zip(syndromes, pairs.tolist(), strict=True): - first = next( - (ii for ii, ss in enumerate(first_list) if syndrome ^ ss in second_list), -1 - ) - expected = ( - [first, second_list.index(syndrome ^ first_list[first])] - if first >= 0 - else [-1, -1] - ) - assert pair == expected + assert first == expected and (second < 0) == (first < 0) + assert first < 0 or second_list[second] == syndrome ^ first_list[first] def test_rows_hash_alike_whenever_they_are_hashed() -> None: @@ -450,10 +441,10 @@ def test_light_syndromes_match_brute_force( Every syndrome also gets a fixed error with it, whose X and Z parts are single-qubit errors where some have their syndromes, for the CSS weight. On small states, the search matches brute force for random flags and syndromes, and reports the first heavy primitive fault with zero - flags in DEM order if one violates the bound. The lister gets the primitive faults with - nonzero flags, the heavy ones and then the light ones, each in DEM order. The representative - of each has its syndrome, and has Pauli weight at most one if it is light, or at most two if - its syndrome is the sum of those of two light ones. + flags in DEM order if one violates the bound. The lister gets the heavy primitive faults with + nonzero flags as its start members. The representative of each primitive fault has its + syndrome, and has Pauli weight at most one if it is light, or at most two if its syndrome is + the sum of those of two light ones. """ # The states are random, and the syndromes of 70 qubits, and the X and Z parts of their errors, # span two words. On |+...+>, every fixed error is a Z error that can act on every qubit. With @@ -469,7 +460,6 @@ def test_light_syndromes_match_brute_force( (6, False, 3), (70, True, 0), (70, False, 70), - (70, False, 35), ) ] lister = mock.Mock(wraps=_fault_search.FaultLister) @@ -574,15 +564,11 @@ def check_search(stabilizers: npt.NDArray[np.uint8]) -> None: ) assert indices == (first,) - # Each signature with nonzero flags, once, in DEM order. signatures = [pair for pair in dict.fromkeys(zip(flags, syndromes)) if pair[0]] - heavy_signatures = [pair for pair in signatures if minimum[pair[1]] > 1] - light_signatures = [pair for pair in signatures if minimum[pair[1]] <= 1] + num_heavy = sum(minimum[syndrome] > 1 for _, syndrome in signatures) for call in lister.call_args_list: - listed_flags, listed_syndromes, representatives, *_, num_starts, _ = call.args - listed = list(zip(listed_flags, listed_syndromes)) - assert listed == heavy_signatures + light_signatures - assert num_starts == len(heavy_signatures) + _, listed_syndromes, representatives, *_, num_starts, _ = call.args + assert num_starts == num_heavy light = set(listed_syndromes[num_starts:]) for syndrome, representative in zip(listed_syndromes, representatives, strict=True): assert pauli_syndromes[representative] == syndrome @@ -1520,19 +1506,6 @@ def css_weight(syndrome: int) -> int: representative = violation.bound.representative assert (representative & 0b0011) | ((representative >> 4) & 0b1100) == syndrome - # On the state with the stabilizers Z0, Z1, X2, X3, and X4, the primitive faults 0 and 1, with - # the syndromes of X0 X1 and X0 X1 Z2 Z3 Z4, are heavy, and 2 and 3, with those of X0 Z2 and - # X1 Z2, are light. The representative of 0 is the product of those of 2 and 3, in which Z2 - # cancels, and {0, 1} leaves Z2 Z3 Z4, of CSS weight three. - stabilizers = np.zeros((5, 10), dtype=int) - stabilizers[[0, 1], [5, 6]] = 1 - stabilizers[[2, 3, 4], [2, 3, 4]] = 1 - failure = _fault_search.find_smallest_violation( - [1, 1, 2, 4], [3, 31, 5, 6], stabilizers, None, True - ) - assert failure is not None and failure.fault_indices == (0, 1) - assert failure.syndrome == 28 and failure.bound.lower_bound == 3 - def test_hand_derived_dependency_cases() -> None: """Small faults, repeated and zero signatures, and ``max_weight`` give hand-derived results.""" @@ -1556,43 +1529,26 @@ def test_hand_derived_dependency_cases() -> None: def test_syndromes_have_a_word_for_every_64_output_qubits(monkeypatch: pytest.MonkeyPatch) -> None: """Syndromes have a word for every 64 output qubits, wherever the search packs them. - On either side of 64 and 128 qubits, by either weight, syndromes with only their first or their - last bit are light, and the representatives of light and heavy syndromes have them. On 65 - qubits, syndromes in the first word get both words, as batches and the oracle need, and as do - the fixed errors of heavy syndromes, which the lister gets with them. + On either side of 64 qubits, by either weight, syndromes with only their first or their last + bit are light, and the representatives of light and heavy syndromes have them. On 65 qubits, + syndromes in the first word get both words, as batches and the oracle need. """ # On |0...0>, the X part of an error with a syndrome is the syndrome. The syndrome ``last + 3`` - # is heavy, but with 2 it forms a fault of residual weight two. The search runs by either - # weight near 64 qubits, and by the CSS weight near 128 qubits, where the physical weight takes - # more time, so there the light and fixed errors of the physical weight are checked alone. + # is heavy, but with 2 it forms a fault of residual weight two. lister = mock.Mock(wraps=_fault_search.FaultLister) monkeypatch.setattr(_fault_search, "FaultLister", lister) - for num_qubits, css_weight in itertools.product((63, 64, 65, 127, 128, 129), (False, True)): - mask = 2**num_qubits - 1 + for num_qubits, css_weight in itertools.product((64, 65), (False, True)): last = 1 << (num_qubits - 1) syndromes = [1, last, last | 3, 2] - if num_qubits < 127 or css_weight: - assert _certify_map([0, 0, 1, 1], syndromes, None, num_qubits, css_weight) is None - _, listed, representatives, *_ = lister.call_args.args - assert [representative & mask for representative in representatives] == listed - else: - rows = _fault_search._hashed_rows(syndromes, (num_qubits + 63) // 64) - stabilizers = _z_state(num_qubits).astype(np.uint8) - errors = _fault_search._get_light_errors(rows, stabilizers, False) - x_parts = [None if error is None else error & mask for error in errors] - assert x_parts == [1, last, None, 2] - fixed_errors = _fault_search._get_fixed_errors(rows, stabilizers, False) - assert [error & mask for error in fixed_errors] == syndromes - - # Together, the primitive faults 0 and 2 pass post-selection and leave X on three qubits. The - # heavy syndromes 3 and 5 are not sums of two light ones, so they get fixed errors. On |0...0>, - # the X part of an error with a syndrome is the syndrome. - lister = mock.Mock(wraps=_fault_search.FaultLister) - monkeypatch.setattr(_fault_search, "FaultLister", lister) + assert _certify_map([0, 0, 1, 1], syndromes, None, num_qubits, css_weight) is None + _, listed, representatives, *_ = lister.call_args.args + assert [ + representative & (2**num_qubits - 1) for representative in representatives + ] == listed + + # Together, the primitive faults 0 and 2 pass post-selection and leave X on three qubits. failure = _certify_map([1, 1, 1], [0b011, 0b101, 0b100], None, 65) assert failure is not None and failure.fault_indices == (0, 2) - _, syndromes, representatives, *_ = lister.call_args.args - assert [representative & (2**65 - 1) for representative in representatives] == syndromes def test_failures_report_the_smallest_faults() -> None: