Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions recirq/qec_memory/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,26 @@ 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 = False,
) -> 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to add an explanation of why this matters, and what values to use in what circumstances. Something that brings up the fact that PyMatching ignores error mechanisms with more than two detection events unless they are decomposed, so True would be appropriate for surface codes.


Returns:
The detector error model.
"""
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(
Comment thread
mhucka marked this conversation as resolved.
Resolved
decompose_errors=decompose_errors
)


def extract_detection_events(
Expand Down Expand Up @@ -167,26 +172,43 @@ 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 = False,
) -> 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.
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:
NotImplementedError: If the circuit contains multiple observables.
ValueError: If the circuit does not contain any observables.
"""
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.")
Comment thread
mhucka marked this conversation as resolved.
Resolved
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
return float(decoded_obs.mean())
if per_observable:
return decoded_obs.mean(axis=0)
return float(np.any(decoded_obs, axis=1).mean())
114 changes: 112 additions & 2 deletions recirq/qec_memory/decoding_test.py
Comment thread
mhucka marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add an assertion with per_observable=True in test_get_logical_error_probability involving 1 observable, to verify that it returns a 1D np.ndarray of shape (1,) rather than squeezing to a scalar?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the 13-item keys list now appears three times in this file. It would be a good idea to make it into a module-level constant to remove the copies and make it clear that they are all meant to be the same.

Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,119 @@ 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_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
)


@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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this last test succeed for any Boolean matrix? If so, it would pass even if something went wrong with the preceding calculations.

Loading