[1]:
%%capture
%pip install qldpc
%pip install matplotlib
Benchmarking state preparation circuits
Logical state preparation is an essential primitive for computing with a quantum error-correcting code. But not all such circuits are created equal: some perform better than others. One way to quantify the performance of a logical state preparation circuit is to consider the probability with which physical errors in this circuit propagate to logical errors. We call this probability the logical error rate of the circuit.
More precisely, we consider:
An error-correcting code.
A circuit to prepare a logical state of that code.
A noise model that specifies how physical errors occur in the circuit.
A decoder that maps syndromes to inferred logical errors.
The decoder tries to infer which logical errors have occurred from syndrome data obtained in a follow-up error correction cycle (as well as possibly from syndromes provided by the state preparation circuit). The logical error rate of the state preparation circuit is then the probability with which the decoder makes an incorrect inference about the occurrence of a logical error, so that after active error correction we end up in the wrong logical state.
The qldpc.circuits.benchmarking sub-module provides methods to compute the logical error rates of logical state preparation circuits.
imports and plotting code
[2]:
import os
import matplotlib.pyplot as plt
import numpy as np
import sinter
import stim
from qldpc import circuits, codes, decoders
%matplotlib inline
[3]:
def plot_sinter_results(stats: sinter.TaskStats) -> tuple[plt.Figure, plt.Axes]:
"""Plot sinter simulation results."""
with_discard = any(_stats.discards for _stats in stats)
if with_discard:
figure, axes = plt.subplots(1, 2, sharex=True, figsize=(7, 3))
axis_errors, axis_discard = axes
else:
figure, axis_errors = plt.subplots(figsize=(4, 3))
axes = [axis_errors]
grouped_stats = sinter.group_by(stats, key=lambda stat: stat.json_metadata.get("label", None))
colors = iter(plt.rcParams["axes.prop_cycle"].by_key()["color"])
for label, group_stats in grouped_stats.items():
color = next(colors)
plot_kwargs = dict(
stats=group_stats,
x_func=lambda _stats: _stats.json_metadata["p"],
plot_args_func=lambda index, curve_id: dict(label=label, color=color),
)
sinter.plot_error_rate(ax=axis_errors, **plot_kwargs)
if with_discard and any(_stats.discards for _stats in group_stats):
sinter.plot_discard_rate(ax=axis_discard, **plot_kwargs)
axis_errors.axline(
(0, 0),
slope=1,
color="k",
linestyle=":",
label=r"$p_{\mathrm{log}}=p_{\mathrm{phys}}$",
)
for axis in axes:
axis.set_xlabel("physical error rate")
axis.loglog()
axis.grid(which="both")
axis_errors.set_ylabel("logical error rate")
if with_discard:
axis_discard.set_ylabel("discard rate")
axis_errors.legend(loc="best", framealpha=1)
figure.tight_layout()
return figure, axis
Steane code
A potential point of confusion: the decoder used to compute logical error rates below should be a sinter.Decoder that uses syndromes to infer which observables have been flipped, not a qldpc.decoders.Decoder that uses syndromes to infer which physical errors have occurred.
[4]:
code = codes.SteaneCode()
# physical error rates to consider, and single-parameter family of noise models
error_rates = np.logspace(-3, -1, 5)
noise_model_family = circuits.DepolarizingNoiseModel
# use a lookup table decoder for these simulations
sinter_decoder = decoders.SinterDecoder(with_lookup=True, max_weight=1)
[5]:
circuit = circuits.get_encoding_circuit(code)
# build sinter tasks to perform our simulations
tasks = circuits.get_state_prep_diagnostic_tasks(code, circuit, error_rates, noise_model_family)
# run simulations and collect statistics on error rates
stats = sinter.collect(
tasks=tasks,
decoders=["custom"],
custom_decoders={"custom": sinter_decoder},
num_workers=os.cpu_count() - 2,
max_shots=10**6,
max_errors=100,
)
# plot simulation results!
plot_sinter_results(stats)
plt.show()
Fault-tolerant state preparation and post-selection
In the example above, the logical error rate always exceeded the physical error rate. That is because the state preparation circuit is not fault-tolerant. Fortunately, the Steane code has well-known circuits for fault-tolerant logical state preparation. The circuit we use below is taken from a 2016 paper by Goto (Figure 1c), and was used for hardware experiments in arXiv:2603.04584.
Fault-tolerant state preparation routines are often repeat-until-success protocols, which use ancilla qubits to “flag” errors that may have occurred during state preparation. If any flag qubit measurement has the wrong value (1 instead of 0), the state preparation routine restarts the protocol from scratch. To identify the logical error rate of successful protocols, we need to post-select on the flag measurement outcomes.
qldpc supports post-selection with the post_select argument to circuits.get_state_prep_diagnostic_tasks. If post_select=True, then sinter will post-select on 0 outcomes of all measurements that are not addressed by any detectors in the provided circuit. Fine control over post-selection can be achieved by manually adding detectors to the circuit and passing post_select=list_of_post_selection_detectors.
[6]:
circuit = stim.Circuit("""
# non-fault-tolerant state preparation
H 0 2 4
CX 0 3 2 1 4 5
CX 0 1 2 6 4 3
CX 2 5 3 6
# flag errors in a logical Z representative
H 7
CZ 7 1 7 3 7 5
MX 7
""")
# build sinter tasks to perform our simulations
tasks = circuits.get_state_prep_diagnostic_tasks(
code,
circuit,
error_rates,
noise_model_family,
post_select=True,
)
# run simulations and collect statistics on error rates
stats = sinter.collect(
tasks=tasks,
decoders=["custom"],
custom_decoders={"custom": sinter_decoder},
num_workers=os.cpu_count() - 2,
max_shots=10**6,
max_errors=100,
)
# plot simulation results!
plot_sinter_results(stats)
plt.show()
Magic state preparation
Here we consider a “Clifford proxy” of the |H> state preparation circuit in Figure 3(b) of arXiv:2603.04584. The Clifford proxy modifies a few gates in the circuit so as to prepare |+> instead of |H>, making the circuit amenable to stim and sinter simulations.
[7]:
hadamard_prep_circuit = """
H 1 2 3 7
CX 1 0 3 5 7 6
CX 1 4 2 6
CX 2 0 1 5 3 6
########################################################
TPP X1*X6 # "T-like" pi/4 rotation about a Pauli string
########################################################
CX 7 6 6 4
MX 7
S_DAG 0 1 2 3 4 5 6 # logical S gate
##################################################################
# logical stabilizer measurement
H 8
CX 8 9
###########################
# controlled-Hadamard gates
TPP_DAG Y0 Y1 Y2 Y3 Y4 Y5 Y6
CZ 8 3 9 5
CZ 8 0 9 2
CZ 8 1 9 6
CZ 8 4
TPP Y0 Y1 Y2 Y3 Y4 Y5 Y6
###########################
CX 8 9
MX 8
MZ 9
##################################################################
# code stabilizer measurements
H 11
CX 3 10 11 2
CX 2 10 11 3
CX 1 10 11 4
CX 4 10 11 1
MZ 10
MX 11
"""
circuit = stim.Circuit(hadamard_prep_circuit.replace("TPP", "SPP"))
# build sinter tasks to perform our simulations
tasks = circuits.get_state_prep_diagnostic_tasks(
code, circuit, error_rates, noise_model_family, metadata=dict(label="no PS")
)
tasks += circuits.get_state_prep_diagnostic_tasks(
code,
circuit,
error_rates,
noise_model_family,
post_select=True,
metadata=dict(label="with PS"),
)
# run simulations and collect statistics on error rates
stats = sinter.collect(
tasks=tasks,
decoders=["custom"],
custom_decoders={"custom": sinter_decoder},
num_workers=os.cpu_count() - 2,
max_shots=10**6,
max_errors=100,
)
# plot simulation results!
plot_sinter_results(stats)
plt.show()
Targeted logical error and discard rates
For convenience, we also provide the following methods to compute one-off logical error and discard rates:
circuits.get_state_prep_diagnostic_circuit, andcircuits.get_logical_error_and_discard_rate.
The circuits.get_state_prep_diagnostic_tasks method that we used in the examples above is essentially a helper function that wraps circuits.get_state_prep_diagnostic_circuit.
We pass add_flags=True to circuits.get_state_prep_diagnostic_circuit to add a detector for each flag measurement. We then post-select on these flag detectors by passing post_select=range(circuit.num_measurements) to circuits.get_logical_error_and_discard_rate.
[8]:
diagnostic_circuit, _ = circuits.get_state_prep_diagnostic_circuit(code, circuit, add_flags=True)
noisy_diagnostic_circuit = noise_model_family(1e-3).noisy_circuit(diagnostic_circuit)
logical_error_rate, discard_rate = circuits.get_logical_error_and_discard_rate(
noisy_diagnostic_circuit,
sinter_decoder,
num_samples=10**6,
post_select=range(circuit.num_measurements),
)
print(logical_error_rate, discard_rate)
6.511394415113997e-05 0.047823
[ ]: