From 6721e8fdf618e5ee71bc8e72576cfd06f74c04af Mon Sep 17 00:00:00 2001 From: Eliott Rosenberg Date: Mon, 2 Nov 2026 00:01:09 +0000 Subject: [PATCH 1/3] Support multiple logical observables and error decomposition in qec_memory decoding --- recirq/qec_memory/decoding.py | 41 ++++++++++++++++------- recirq/qec_memory/decoding_test.py | 52 +++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 13 deletions(-) diff --git a/recirq/qec_memory/decoding.py b/recirq/qec_memory/decoding.py index a40d5f54..5926aa35 100644 --- a/recirq/qec_memory/decoding.py +++ b/recirq/qec_memory/decoding.py @@ -108,13 +108,16 @@ def cirq_to_stim_hardware_circuit(hardware_circuit: cirq.Circuit) -> stim.Circui def create_dem( - hardware_stim_circuit: stim.Circuit, si_1000_error_rate: float + hardware_stim_circuit: stim.Circuit, + si_1000_error_rate: float, + decompose_errors: bool = True, ) -> stim.DetectorErrorModel: """Create a detector error model for a given circuit, assuming the si1000 noise model. Args: hardware_stim_circuit: The circuit that was run on hardware, converted to a stim circuit. si_1000_error_rate: The parameter p in stimflow.NoiseModel.si1000. + decompose_errors: Whether to decompose errors into graphlike components. Returns: The detector error model. @@ -122,7 +125,9 @@ def create_dem( noisy_hardware_stim_circuit = stimflow.NoiseModel.si1000( p=si_1000_error_rate ).noisy_circuit(hardware_stim_circuit) - return noisy_hardware_stim_circuit.detector_error_model() + return noisy_hardware_stim_circuit.detector_error_model( + decompose_errors=decompose_errors + ) def extract_detection_events( @@ -167,26 +172,38 @@ def decode(detection_events: np.ndarray, dem: stim.DetectorErrorModel) -> np.nda def get_logical_error_probability( - data: pd.DataFrame, circuit: cirq.Circuit, si_1000_error_rate: float -) -> float: + data: pd.DataFrame, + circuit: cirq.Circuit, + si_1000_error_rate: float, + *, + per_observable: bool = False, + decompose_errors: bool = True, +) -> float | np.ndarray: """Decode using pymatching and get the logical error probability. Args: data: The measurement results, formatted as in cirq.Result.data. circuit: The circuit that was run on hardware. si_1000_error_rate: The parameter p in stimflow.NoiseModel.si1000. + per_observable: If True, return a 1D array of logical error probabilities + for each observable individually. If False (default), return the joint + logical error probability (probability that any observable flipped). + decompose_errors: Whether to decompose errors into graphlike components + when constructing the detector error model. Returns: - The logical error probability. - - Raises: - NotImplementedError: If the circuit contains multiple observables. + The joint logical error probability as a float if `per_observable` is False, + or a 1D numpy array of per-observable logical error probabilities if True. """ hardware_stim_circuit = cirq_to_stim_hardware_circuit(circuit) - dem = create_dem(hardware_stim_circuit, si_1000_error_rate) + dem = create_dem( + hardware_stim_circuit, + si_1000_error_rate, + decompose_errors=decompose_errors, + ) dets_arr, obs_arr = extract_detection_events(data, hardware_stim_circuit) - if obs_arr.shape[1] != 1: - raise NotImplementedError("Multiple observables are not currently supported.") predictions = decode(dets_arr, dem) decoded_obs = predictions ^ obs_arr - return float(decoded_obs.mean()) + if per_observable: + return decoded_obs.mean(axis=0) + return float(np.any(decoded_obs, axis=1).mean()) diff --git a/recirq/qec_memory/decoding_test.py b/recirq/qec_memory/decoding_test.py index f9b0cc3d..bc76e333 100644 --- a/recirq/qec_memory/decoding_test.py +++ b/recirq/qec_memory/decoding_test.py @@ -80,9 +80,59 @@ def test_get_logical_error_probability(): "repetition_code:memory", rounds=5, distance=3 ) circuit = stimcirq.stim_circuit_to_cirq_circuit(stim_circuit) - for error_rate, expected_lep in zip([0.0, 0.01, 0.1, 0.5], [0.0, 0.0, 0.0, 0.7]): + for error_rate, expected_lep in zip([0.0, 0.01, 0.1, 0.5], [0.0, 0.0, 0.0, 0.6]): data = _generate_fake_qec_data(keys, error_rate, shots, rng) assert ( qec_decoding.get_logical_error_probability(data, circuit, 0.001) == expected_lep ) + + +@pytest.mark.skipif( + not hasattr(cirq.transformers, "apply_lazy_args_on_circuit_operation"), + reason="Requires pre-release version of Cirq", +) +def test_get_logical_error_probability_multi_observable(): + rng = np.random.default_rng(0) + keys = [ + "0", + "1", + "2[0]", + "2[1]", + "2[2]", + "2[3]", + "3[0]", + "3[1]", + "3[2]", + "3[3]", + "10", + "11", + "12", + ] + shots = 10 + stim_circuit = stim.Circuit.generated( + "repetition_code:memory", rounds=5, distance=3 + ) + stim_circuit.append("OBSERVABLE_INCLUDE", [stim.target_rec(-2)], 1) + circuit = stimcirq.stim_circuit_to_cirq_circuit(stim_circuit) + + data_noiseless = _generate_fake_qec_data(keys, 0.0, shots, rng) + assert ( + qec_decoding.get_logical_error_probability(data_noiseless, circuit, 0.001) + == 0.0 + ) + np.testing.assert_array_equal( + qec_decoding.get_logical_error_probability( + data_noiseless, circuit, 0.001, per_observable=True + ), + np.array([0.0, 0.0]), + ) + + data_noisy = _generate_fake_qec_data(keys, 0.5, shots, rng) + joint_lep = qec_decoding.get_logical_error_probability(data_noisy, circuit, 0.001) + per_obs_lep = qec_decoding.get_logical_error_probability( + data_noisy, circuit, 0.001, per_observable=True + ) + assert isinstance(joint_lep, float) + assert per_obs_lep.shape == (2,) + assert np.max(per_obs_lep) <= joint_lep <= np.sum(per_obs_lep) From 55a0048538ec699ea4bf0f11b5ea1cde35fabf4b Mon Sep 17 00:00:00 2001 From: Eliott Rosenberg Date: Mon, 2 Nov 2026 02:20:12 +0000 Subject: [PATCH 2/3] fix test --- recirq/serialization_utils_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recirq/serialization_utils_test.py b/recirq/serialization_utils_test.py index 9679bf70..79dac895 100644 --- a/recirq/serialization_utils_test.py +++ b/recirq/serialization_utils_test.py @@ -37,7 +37,7 @@ def test_bits_roundtrip(): b = recirq.BitArray(bitstring) buffer = io.StringIO() - cirq.to_json(b, buffer) + cirq.to_json(b, buffer, indent=2) buffer.seek(0) text = buffer.read() From 1f85252a63987c83d77ff34683a052253d9757b4 Mon Sep 17 00:00:00 2001 From: Eliott Rosenberg Date: Mon, 9 Nov 2026 06:26:35 +0000 Subject: [PATCH 3/3] Address review comments on PR #501 --- recirq/qec_memory/decoding.py | 9 ++++- recirq/qec_memory/decoding_test.py | 64 +++++++++++++++++++++++++++++- recirq/serialization_utils_test.py | 2 +- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/recirq/qec_memory/decoding.py b/recirq/qec_memory/decoding.py index 5926aa35..884585f7 100644 --- a/recirq/qec_memory/decoding.py +++ b/recirq/qec_memory/decoding.py @@ -110,7 +110,7 @@ def cirq_to_stim_hardware_circuit(hardware_circuit: cirq.Circuit) -> stim.Circui def create_dem( hardware_stim_circuit: stim.Circuit, si_1000_error_rate: float, - decompose_errors: bool = True, + decompose_errors: bool = False, ) -> stim.DetectorErrorModel: """Create a detector error model for a given circuit, assuming the si1000 noise model. @@ -177,7 +177,7 @@ def get_logical_error_probability( si_1000_error_rate: float, *, per_observable: bool = False, - decompose_errors: bool = True, + decompose_errors: bool = False, ) -> float | np.ndarray: """Decode using pymatching and get the logical error probability. @@ -194,6 +194,9 @@ def get_logical_error_probability( Returns: The joint logical error probability as a float if `per_observable` is False, or a 1D numpy array of per-observable logical error probabilities if True. + + Raises: + ValueError: If the circuit does not contain any observables. """ hardware_stim_circuit = cirq_to_stim_hardware_circuit(circuit) dem = create_dem( @@ -202,6 +205,8 @@ def get_logical_error_probability( decompose_errors=decompose_errors, ) dets_arr, obs_arr = extract_detection_events(data, hardware_stim_circuit) + if obs_arr.shape[1] == 0: + raise ValueError("The circuit must contain at least one observable.") predictions = decode(dets_arr, dem) decoded_obs = predictions ^ obs_arr if per_observable: diff --git a/recirq/qec_memory/decoding_test.py b/recirq/qec_memory/decoding_test.py index bc76e333..ed6728f7 100644 --- a/recirq/qec_memory/decoding_test.py +++ b/recirq/qec_memory/decoding_test.py @@ -80,11 +80,71 @@ def test_get_logical_error_probability(): "repetition_code:memory", rounds=5, distance=3 ) circuit = stimcirq.stim_circuit_to_cirq_circuit(stim_circuit) - for error_rate, expected_lep in zip([0.0, 0.01, 0.1, 0.5], [0.0, 0.0, 0.0, 0.6]): + for error_rate, expected_lep_false, expected_lep_true in zip( + [0.0, 0.01, 0.1, 0.5], + [0.0, 0.0, 0.0, 0.7], + [0.0, 0.0, 0.0, 0.6], + ): data = _generate_fake_qec_data(keys, error_rate, shots, rng) assert ( qec_decoding.get_logical_error_probability(data, circuit, 0.001) - == expected_lep + == expected_lep_false + ) + assert ( + qec_decoding.get_logical_error_probability( + data, circuit, 0.001, decompose_errors=False + ) + == expected_lep_false + ) + assert ( + qec_decoding.get_logical_error_probability( + data, circuit, 0.001, decompose_errors=True + ) + == expected_lep_true + ) + + +@pytest.mark.skipif( + not hasattr(cirq.transformers, "apply_lazy_args_on_circuit_operation"), + reason="Requires pre-release version of Cirq", +) +def test_get_logical_error_probability_no_observables(): + rng = np.random.default_rng(0) + keys = [ + "0", + "1", + "2[0]", + "2[1]", + "2[2]", + "2[3]", + "3[0]", + "3[1]", + "3[2]", + "3[3]", + "10", + "11", + "12", + ] + shots = 10 + stim_circuit = stim.Circuit.generated( + "repetition_code:memory", rounds=5, distance=3 + ) + stim_circuit_no_obs = stim.Circuit() + for inst in stim_circuit: + if inst.name != "OBSERVABLE_INCLUDE": + stim_circuit_no_obs.append(inst) + circuit_no_obs = stimcirq.stim_circuit_to_cirq_circuit(stim_circuit_no_obs) + data = _generate_fake_qec_data(keys, 0.1, shots, rng) + + hardware_stim_circuit = qec_decoding.cirq_to_stim_hardware_circuit(circuit_no_obs) + _, obs_arr = qec_decoding.extract_detection_events(data, hardware_stim_circuit) + assert obs_arr.shape == (shots, 0) + + with pytest.raises(ValueError, match="at least one observable"): + qec_decoding.get_logical_error_probability(data, circuit_no_obs, 0.001) + with pytest.raises(ValueError, match="at least one observable"): + qec_decoding.get_logical_error_probability( + data, circuit_no_obs, 0.001, per_observable=True ) diff --git a/recirq/serialization_utils_test.py b/recirq/serialization_utils_test.py index 39812e98..4d8df8a1 100644 --- a/recirq/serialization_utils_test.py +++ b/recirq/serialization_utils_test.py @@ -37,7 +37,7 @@ def test_bits_roundtrip(): b = recirq.BitArray(bitstring) buffer = io.StringIO() - cirq.to_json(b, buffer, indent=2) + cirq.to_json(b, buffer) buffer.seek(0) text = buffer.read()