% TODO: Make all [H] -> [t]
\chapter{Fault Tolerant QEC}

% Intro

An important challenge of \ac{qec} that was recognized early on is
the fact that the error correction machinery itself may introduce new
errors \cite[Sec.~III]{shor_scheme_1995}.
Specifically for stabilizer codes, errors may happen during the
syndrome extraction process, since it is implemented in quantum hardware itself.
We call the errors the \ac{qec} procedure is supposed to correct
\emph{input errors} and the errors introduced by the procedure itself
\emph{internal errors}.
In order to be \emph{fault-tolerant}, the procedure must be able to
address both types of errors.

% Definition of fault tolerance

% TODO: Proper consideration with number of errors
We model the possible occurrence of errors during any processing
stage as different \emph{error locations} $E_i,~i\in \{1,\ldots,N\}$
in the circuit.
$N \in \mathbb{N}$ is the total number of error locations.
The \emph{circuit error vector} $\bm{e} \in \{0,1\}^N$ is a vector
indicating which errors occurred, with
\begin{align*}
    e_i :=
    \begin{cases}
        1, & \text{Error $E_i$ occurred} \\
        0, & \text{otherwise}
    \end{cases}
    .%
\end{align*}
\autoref{fig:fault_tolerance_overview} illustrates the flow of errors.
A \ac{qec} procedure is deemed fault tolerant if
\cite[Def.~5]{gottesman_introduction_2009}
\begin{align*}
    % tex-fmt: off
    \text{A)}
    % tex-fmt: on
    \hspace{5mm} & \lVert \bm{e}_\text{output} \rVert
    \le \lVert \bm{e}_\text{internal} \rVert
    \hspace{5mm} \forall\,
    \bm{e}_\text{input}, \bm{e}_\text{internal} \in \{0,1\}^N :
    \lVert \bm{e}_\text{internal} \rVert \le t \\
    % tex-fmt: off
    \text{B)}
    % tex-fmt: on
    \hspace{5mm} & \lVert \bm{e}_\text{output} \rVert = 0
    \hspace{19.3mm} \forall\,
    \bm{e}_\text{input}, \bm{e}_\text{internal} \in \{0,1\}^N :
    \lVert \bm{e}_\text{input} \rVert + \lVert \bm{e}_\text{internal}
    \rVert \le t
    ,
\end{align*}
where $t = \lfloor (d_\text{min} -1)/2 \rfloor$ is the number of
errors the original code is able to correct.
Condition A limits the spread of input errors during the error
correction process.
Condition B means that as long as there are few enough internal and
input errors, the scheme should be able to correct all of them.

% Practical considerations

% TODO: Are the fault-tolerant QEC procedures where we don't perform
% multiple measurement rounds?

\content{We generally need to perform multiple rounds of syndrome extraction}
\content{The number of rounds of syndrome extraction is usually
chosen equal to the $d_\text{min}$ of the code}
\content{One-shot decoding property}

\begin{figure}[t]
    \centering

    \begin{tikzpicture}
        \node[rectangle, draw, fill=orange!20, minimum
        height=2cm, minimum width=2.5cm, align=center] at (0,0)
        (internal) {Internal Errors\\ $\bm{e}_\text{internal}$};

        \node[signal, draw, fill=orange!20, minimum height=2cm,
        minimum width=2.5cm, align=center, signal pointer angle=140]
        at (-2.8, 0) (input) {Input Errors \\ $\bm{e}_\text{input}$};

        \node at (1.99,0) {\huge =};

        \node[rectangle, draw, fill=orange!20, minimum height=2cm,
        minimum width=2.5cm, align=center] at (4,0) (output)
        {Output Errors\\ $\bm{e}_\text{output}$};

        \node[above] at (input.north) {\small Input State};
        \node[above] at (internal.north) {\small QEC};
        \node[above] at (output.north) {\small Output State};
    \end{tikzpicture}

    \caption{
        Sources of error in a fault-tolerant \ac{qec} system.
        Adapted from \cite[Figure~2]{derks_designing_2025}.
    }
    \label{fig:fault_tolerance_overview}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Noise Models}
\label{sec:Noise Models}

% Intro

% TODO: Different variable name for N?
We collect the probabilities of error at each location in the
\emph{noise model}, a vector $\bm{p} \in \mathbb{R}^N$, where $N \in
\mathbb{N}$ is the number of possible error locations.
There are different types of noise models, each allowing for
different error locations in the circuit.

% Figure intro

We will illustrate the most widely used types of error models on the
example of the three-qubit repetition code for $X$ errors.
This code has stabilizers $Z_1Z_2$ and $Z_2Z_3$.
Figure \autoref{fig:pure_syndrome_extraction} shows the respective
check matrix and syndrome extraction circuit.
Note that this is a concrete implementation using CNOT gates, as
opposed to the system-level view introduced in
\autoref{subsec:Stabilizer Codes}.
We visualize the different types of noise models in
\autoref{fig:noise_model_types}.

% Data and ancilla qubits

\content{Introduce data qubits}

\content{\textbf{TODO:} Write something about the code/circuit distance}

% Bit-flip noise

The simplest type of noise model is \emph{bit-flip} noise.
This corresponds to the classical \ac{bsc}, i.e., only $X$ errors on the
data qubits are possible \cite[Appendix~A]{gidney_new_2023}.
Note that we cannot use bit-flip noise to develop fault-tolerant
systems, as it doesnt't account for errors during the syndrome extraction.
This is shown in \autoref{subfig:bit_flip}. \\
\content{Some more words on bit-flip noise}
\content{\textbf{TODO}: What is this useful for? Just as a first step?}

% Depolarizing channel

Extending bit-flip noise to consider $X,Z$ or $Y$ instead of just $X$ errors,
we obtain the \emph{depolarizing channel}
\cite[Sec.~7.6]{gottesman_stabilizer_1997}, depicted in
\autoref{subfig:depolarizing}. \\
\content{Some more words on the depolarizing channel}
\content{\textbf{TODO}: What does this model? Memory experiment with
ideal syndrome extraction?}
\content{\textbf{TODO}: Why is it called depolarizing?}

\content{\textbf{TODO:} Write something about ``code capacity'' noise models}

% Phenomenological noise

The \emph{phenomenological noise model} is the first type of noise model we
examine that accounts for faults during the syndrome extraction.
Here, we consider multiple rounds of syndrome measurements with a
depolarizing channel before each round.
Additionally, we allow for measurement errors by having $X$ error
locations right before each measurement \cite[Appendix~A]{gidney_new_2023}.
Note that it is enough to only consider $X$ errors at this point,
since that is the only type of error directly affecting the
measurement outcomes.
This model is depicted in \autoref{subfig:phenomenological}.\\
\content{\textbf{TODO}: Why is this useful? Derks et al. mentioned
something about it being useful to derive fault-tolerant circuits}

% Circuit-level noise

The most general type of noise model is \emph{circuit-level noise}.
Here we not only consider noise inbetween syndrome extraction rounds
and at the measurements, but at each gate.
Specifically, we allow arbitrary for $n$-qubit Pauli errors after
each $n$-qubit gate.
An $n$-qubit Pauli error is simply a series of correlated Pauli
errors on each individual related qubit.
Circuit-level noise is shown in \autoref{subfig:circuit_level}. \\
\content{\textbf{TODO}: Why do we need this? Derks et al. mentioned
    something about needing it for actual simulations, even when using
phenomenological noise for derivations.}

% Different noise models for circuit-level noise

\content{Comparison from Gidney's paper}
\content{In this work we only consider standard circuit-based
depolarizing noise}

\begin{figure}[t]
    \centering

    \begin{minipage}{0.5\textwidth}
        \begin{align*}
            \bm{H} =
            \left[
                \begin{array}{ccc|ccc}
                    0 & 0 & 0 & 0 & 0 & 0 \\
                    0 & 0 & 0 & 0 & 0 & 0 \\
                    0 & 0 & 0 & 1 & 1 & 0 \\
                    0 & 0 & 0 & 0 & 1 & 1
                \end{array}
            \right]
        \end{align*}
    \end{minipage}%
    \begin{minipage}{0.5\textwidth}
        % tex-fmt: off
        \begin{quantikz}%[row sep=4mm, column sep=4mm]
            \lstick[3]{$\ket{\psi}_\text{L}$} & \ctrl{3} &          &          &          &           &                 \\
                                              &          & \ctrl{2} & \ctrl{3} &          &           &                 \\
                                              &          &          &          & \ctrl{2} &           &                 \\
            \lstick{$\ket{0}_{\text{A}_1}$}   & \targ{}  & \targ{}  &          &          &  \meter{} & \setwiretype{c} \\
            \lstick{$\ket{0}_{\text{A}_2}$}   &          &          & \targ{}  & \targ{}  &  \meter{} & \setwiretype{c}
        \end{quantikz}
        % tex-fmt: on
    \end{minipage}%

    \caption{
        Syndrome extraction circuit for the three-qubit repetition
        code under bit-flip noise.
    }
    \label{fig:pure_syndrome_extraction}
\end{figure}

\begin{figure}[t]
    \centering

    \newcommand{\xerr}{\gate[style={fill=KITblue!50}]{\phantom{1}}}
    \newcommand{\xyzerr}{\gate[style={
                draw=black,
                fill=KITred,
                path picture={
                    % tex-fmt: off
                    \fill[KITblue!60]
                    ($(path picture bounding box.south west)+(0,0)$)
                    -- ($(path picture bounding box.north west)+(0,0)$)
                    -- ($(path picture bounding box.north west)+(0.28,0)$)
                    -- cycle;
                    \fill[KITorange!60]
                    ($(path picture bounding box.north east)+(0,0)$)
                    -- ($(path picture bounding box.south east)+(0,0)$)
                    -- ($(path picture bounding box.south east)+(-0.28,0)$)
                    -- cycle;
                    \fill[KITred!60]
                    ($(path picture bounding box.north east)+(0,0)$)
                    -- ($(path picture bounding box.south east)+(-0.28,0)$)
                    -- ($(path picture bounding box.south west)+(0,0)$)
                    -- ($(path picture bounding box.north west)+(0.28,0)$)
                    -- cycle;
                    % tex-fmt: on
                }
    }]{\phantom{1}}}

    \begin{minipage}{0.7\textwidth}
        \begin{minipage}{\textwidth}
            \centering
            % tex-fmt: off
            \begin{quantikz}[row sep=4mm, column sep=4mm]
                \lstick[3]{$\ket{\psi}_\text{L}$}        & \xerr & \ctrl{3} &            &          &           &           &                 \\
                                                & \xerr &          & \ctrl{2}   & \ctrl{3} &           &           &                 \\
                                                & \xerr &          &            &          & \ctrl{2}  &           &                 \\
                \lstick{$\ket{0}_{\text{A}_1}$} &       & \targ{}  & \targ{}    &          &           &  \meter{} & \setwiretype{c} \\
                \lstick{$\ket{0}_{\text{A}_2}$} &       &          &            & \targ{}  & \targ{}   &  \meter{} & \setwiretype{c}
            \end{quantikz}
            % tex-fmt: on

            \subcaption{Bit-flip noise.}
            \label{subfig:bit_flip}
        \end{minipage}

        \vspace*{5mm}

        \begin{minipage}{\textwidth}
            \centering
            % tex-fmt: off
            \begin{quantikz}[row sep=4mm, column sep=4mm]
                \lstick[3]{$\ket{\psi}_\text{L}$} & \xyzerr & \ctrl{3} &            &          &           &           &                 \\
                                                  & \xyzerr &          & \ctrl{2}   & \ctrl{3} &           &           &                 \\
                                                  & \xyzerr &          &            &          & \ctrl{2}  &           &                 \\
                \lstick{$\ket{0}_{\text{A}_1}$}   &         & \targ{}  & \targ{}    &          &           &  \meter{} & \setwiretype{c} \\
                \lstick{$\ket{0}_{\text{A}_2}$}   &         &          &            & \targ{}  & \targ{}   &  \meter{} & \setwiretype{c}
            \end{quantikz}
            % tex-fmt: on

            \subcaption{Depolarizing channel.}
            \label{subfig:depolarizing}
        \end{minipage}

        \vspace*{5mm}

        \begin{minipage}{\textwidth}
            \centering
            % tex-fmt: off
            \begin{quantikz}[row sep=4mm, column sep=4mm]
                \lstick[3]{$\ket{\psi}_\text{L}$} & \xyzerr & \ctrl{3} &           &          &          &       &           & \xyzerr        & & \setwiretype{n} &                                       \\
                                                  & \xyzerr &          & \ctrl{2}  & \ctrl{3} &          &       &           & \xyzerr         & & \setwiretype{n} & \gate[style={left,draw=none}]{\cdots} \\
                                                  & \xyzerr &          &           &          & \ctrl{2} &       &           & \xyzerr         & & \setwiretype{n} &                                       \\
                \lstick{$\ket{0}_{\text{A}_1}$}   &         & \targ{}  & \targ{}   &          &          & \xerr &  \meter{} & \setwiretype{c}                                                              \\
                \lstick{$\ket{0}_{\text{A}_2}$}   &         &          &           & \targ{}  & \targ{}  & \xerr &  \meter{} & \setwiretype{c}
            \end{quantikz}
            % tex-fmt: on

            \subcaption{Phenomenological noise.}
            \label{subfig:phenomenological}
        \end{minipage}

        \vspace*{5mm}

        \begin{minipage}{\textwidth}
            \centering
            % tex-fmt: off
            \begin{quantikz}[row sep=4mm, column sep=2mm]
                \lstick[3]{$\ket{\psi}_\text{L}$} & \xyzerr & \ctrl{3} & \xyzerr \wire[d][3]{q} &           &                        &          &                        &          &                        &       &           & \xyzerr          & & \setwiretype{n} &                                                    \\
                                                  & \xyzerr &          &                        & \ctrl{2}  & \xyzerr \wire[d][2]{q} & \ctrl{3} & \xyzerr \wire[d][3]{q} &          &                        &       &           & \xyzerr          & & \setwiretype{n} & \gate[style={left,draw=none,xshift=3.5mm}]{\cdots} \\
                                                  & \xyzerr &          &                        &           &                        &          &                        & \ctrl{2} & \xyzerr \wire[d][2]{q} &       &           & \xyzerr          & & \setwiretype{n} &                                                    \\
                \lstick{$\ket{0}_{\text{A}_1}$}   & \xyzerr & \targ{}  & \xyzerr                & \targ{}   & \xyzerr                &          &                        &          &                        & \xerr &  \meter{} & \setwiretype{c}                                                                           \\
                \lstick{$\ket{0}_{\text{A}_2}$}   & \xyzerr &          &                        &           &                        & \targ{}  & \xyzerr                & \targ{}  & \xyzerr                & \xerr &  \meter{} & \setwiretype{c}
            \end{quantikz}
            % tex-fmt: on

            \subcaption{Circuit-level noise.}
            \label{subfig:circuit_level}
        \end{minipage}
    \end{minipage}%
    \hfill%
    \begin{minipage}{0.23\textwidth}
        \centering
        % tex-fmt: off
        \begin{quantikz}[row sep=4mm, column sep=2mm]
            \setwiretype{n} & \xerr                            & \gate[style={right, draw=none, xshift=-15mm}]{\text{X error}}            \\
            \setwiretype{n} & \xyzerr                          & \gate[style={right, draw=none, xshift=-15mm}]{\text{X,Z, or Y error}}    \\
            \setwiretype{n} & \gate{\phantom{1}}\wire[d][1]{q} & \gate[style={right, draw=none, xshift=-15mm},2]{\text{Correlated error}} \\
            \setwiretype{n} & \gate{\phantom{1}}               &
        \end{quantikz}
        % tex-fmt: on
    \end{minipage}

    \caption{Types of noise models.}
    \label{fig:noise_model_types}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Detector Error Models}
\label{sec:Detector Error Models}

\content{\textbf{TODO}: Look up how Derks et al. introduce DEMs}

% Different ways of implementing fault tolerance

\content{Ways of implementing fault tolerance different from DEMs}

% Core idea

\content{Construct ``circuit code'' from original code}

% Benefits

\content{Benefits of this approach \cite[Sec.~4.2]{derks_designing_2025}}

%%%%%%%%%%%%%%%%
\subsection{Measurement Syndrome Matrix}
\label{subsec:Measurement Syndrome Matrix}

% Core idea

\content{Core idea: Matrix describes parity checks \\
    $\rightarrow$ A column shows which parity checks the
    corresponding VN contributes to \\
    $\rightarrow$ View columns as syndromes corresponding to error
    locations in the circuit
}

% Multiple rounds of syndrome extraction

% TODO: First introduce syndrome measurement matrix, mathematically
% (consult Derks et al.'s paper). Then use the three-qubit repetition
% code as an example only
\autoref{fig:rep_code_multiple_rounds_bit_flip} shows a circuit
performing three rounds of syndrome extraction for the three-qubit
repetition code introduced earlier.
We are only considering bit-flip noise at this point.
For each syndrome extraction round, we get an additional set of
syndrome measurements.
We combine these measurements by stacking them in a new vector $\bm{s}
\in \mathbb{F}_2^{n_\text{rounds}\cdot(n-k)}$.
To model this behavior mathematically, we append additional rows to
the check matrix.
We call this matrix the \emph{measurement syndrome matrix}
$\bm{\Omega}$.

\begin{figure}[H]
    \centering

    \begin{minipage}{0.3\textwidth}
        \centering
        \begin{tikzpicture}
            \node{$%
                \bm{\Omega} =
                \begin{pmatrix}
                    1 & 1 & 0 \\
                    0 & 1 & 1 \\
                    1 & 1 & 0 \\
                    0 & 1 & 1 \\
                    1 & 1 & 0 \\
                    0 & 1 & 1
                \end{pmatrix}%
                $
            };

            \draw [
                line width=1pt,
                decorate,
                decoration={brace,mirror,amplitude=3mm,raise=5mm}
            ]
            (1,0.55) -- (1,1.4)
            node[midway,right,xshift=10mm]{$\text{SE}_1$};

            \draw [
                line width=1pt,
                decorate,
                decoration={brace,mirror,amplitude=3mm,raise=5mm}
            ]
            (1,-0.4) -- (1,0.45)
            node[midway,right,xshift=10mm]{$\text{SE}_2\hspace{2mm},$};

            \draw [
                line width=1pt,
                decorate,
                decoration={brace,mirror,amplitude=3mm,raise=5mm}
            ]
            (1,-1.38) -- (1,-0.5)
            node[midway,right,xshift=10mm]{$\text{SE}_3$};
        \end{tikzpicture}
    \end{minipage}%
    \begin{minipage}{0.3\textwidth}
        \centering
        \vspace*{-6mm}
        \begin{gather*}
            \bm{s} \in \text{span} \mleft\{ \bm{\Omega} \mright\}
        \end{gather*}
    \end{minipage}

    \newcommand{\preperr}[1]{
        \gate[style={fill=blue!20}]{\scriptstyle #1}
    }

    \vspace*{5mm}

    \begin{quantikz}[
            row sep=4mm, column sep=4mm,
            wire types={q,q,q,q,q,n,n,n,n},
            execute at end picture={
                \draw [
                    line width=1pt,
                    decorate,
                    decoration={brace,amplitude=3mm,raise=9mm}
                ]
                (\tikzcdmatrixname-4-19.north east)
                --
                (\tikzcdmatrixname-5-19.south east)
                node[midway,right,xshift=14mm]{$\text{SE}_1$};
                \draw [
                    line width=1pt,
                    decorate,
                    decoration={brace,amplitude=3mm,raise=9mm}
                ]
                (\tikzcdmatrixname-6-19.north east)
                --
                (\tikzcdmatrixname-7-19.south east)
                node[midway,right,xshift=14mm]{$\text{SE}_2$};
                \draw [
                    line width=1pt,
                    decorate,
                    decoration={brace,amplitude=3mm,raise=9mm}
                ]
                (\tikzcdmatrixname-8-19.north east)
                --
                (\tikzcdmatrixname-9-19.south east)
                node[midway,right,xshift=14mm]{$\text{SE}_3$};
            }
        ]
            % tex-fmt: off
            \lstick[3]{$\ket{\psi}_\text{L}$}        & \preperr{E_0} & \ctrl{3} &           &          &          &                                 &                 & \ctrl{5} &           &          &          &                                 &                 & \ctrl{7} &           &          &          &           &                 \\
                                            & \preperr{E_1} &          & \ctrl{2}  & \ctrl{3} &          &                                 &                 &          &  \ctrl{4} & \ctrl{5} &          &                                 &                 &          & \ctrl{6}  & \ctrl{7} &          &           &                 \\
                                            & \preperr{E_2} &          &           &          & \ctrl{2} &                                 &                 &          &           &          & \ctrl{4} &                                 &                 &          &           &          & \ctrl{6} &           &                 \\
            \lstick{$\ket{0}_{\text{A}_1}$} &               & \targ{}  & \targ{}   &          &          &                                 &                 &          &           &          &          &                                 &                 &          &           &          &          &  \meter{} & \setwiretype{c} \\
            \lstick{$\ket{0}_{\text{A}_2}$} &               &          &           & \targ{}  & \targ{}  &                                 &                 &          &           &          &          &                                 &                 &          &           &          &          &  \meter{} & \setwiretype{c} \\
                                            &               &          &           &          &          & \lstick{$\ket{0}_{\text{A}_3}$} & \setwiretype{q} & \targ{}  &  \targ{}  &          &          &                                 &                 &          &           &          &          &  \meter{} & \setwiretype{c} \\
                                            &               &          &           &          &          & \lstick{$\ket{0}_{\text{A}_4}$} & \setwiretype{q} &          &           &  \targ{} & \targ{}  &                                 &                 &          &           &          &          &  \meter{} & \setwiretype{c} \\
                                            &               &          &           &          &          &                                 &                 &          &           &          &          & \lstick{$\ket{0}_{\text{A}_5}$} & \setwiretype{q} & \targ{}  &  \targ{}  &          &          &  \meter{} & \setwiretype{c} \\
                                            &               &          &           &          &          &                                 &                 &          &           &          &          & \lstick{$\ket{0}_{\text{A}_6}$} & \setwiretype{q} &          &           &  \targ{} & \targ{}  &  \meter{} & \setwiretype{c}
            % tex-fmt: on
    \end{quantikz}

    \caption{
        Repeated syndrome extraction circuit for the three-qubit
        repetition code under bit flip noise.
    }
    \label{fig:rep_code_multiple_rounds_bit_flip}
\end{figure}

\begin{figure}[H]
    \begin{gather*}
        \hspace*{-33.3mm}%
        \begin{array}{c}
            E_6 \\
            \downarrow
        \end{array}
    \end{gather*}
    \vspace*{-8mm}
    \begin{gather*}
        \bm{\Omega} =
        \left(
            \begin{array}{
                    cccccc%
                    >{\columncolor{red!20}}c%
                    cccccccc
                }
                1 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0
                & 0 & 0 & 0 & 0 & 0  \\
                0 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0
                & 0 & 0 & 0 & 0 & 0  \\
                1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 1 & 0
                & 0 & 0 & 0 & 0 & 0  \\
                0 & 1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 1
                & 0 & 0 & 0 & 0 & 0  \\
                1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0
                & 1 & 1 & 0 & 1 & 0  \\
                0 & 1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0
                & 0 & 1 & 1 & 0 & 1
            \end{array}
        \right),
        \hspace*{7mm}
        \bm{s} \in \text{span} \mleft\{
        \bm{\Omega} \mright\}
    \end{gather*}

    \vspace*{5mm}

    \newcommand{\preperr}[1]{
        \gate[style={fill=blue!20}]{\scriptstyle #1}
    }
    \newcommand{\measerr}{\gate[style={fill=blue!20}]{\phantom{1}}}

    \newcommand{\noise}{
        \gate[style={noisy}]{\text{\small X}}%
        \setwiretype{n}%
        \wire[l][1]{q}
    }
    \newcommand{\redwire}[1]{
        \wire[r][#1][style={draw=red, line width=1.5pt}]{q}
    }
    \newcommand{\redtarg}{
        \targ[style={draw=red}]{}%
        \setwiretype{n}%
        \wire[l][1]{q}
    }
    \newcommand{\redctrl}[1]{
        \ctrl[style={draw=red,fill=red,line width=1.5pt}]{#1}
    }
    \newcommand{\redmeter}{\meter[style={draw=red,fill=red!20}]{}}

    \tikzset{
        noisy/.style={
            starburst,
            starburst point height=2.5mm,
            fill=red!25, draw=red!85!black,
            line width=1.5pt,
            inner xsep=-2pt, inner ysep=-2pt
        },
    }

    \centering

    % tex-fmt: off
    \begin{quantikz}[row sep=4mm, column sep=3mm, wire types={q,q,q,q,q,n,n,n,n}]
        \lstick[3]{$\ket{\psi}_\text{L}$} & \preperr{E_0}         & \ctrl{3} &           &          &          & \preperr{E_5}                   &                 & \ctrl{5} &                      &                     &          & \preperr{E_{10}}                &                 & \ctrl{7} &                     &                     &          &                  &                                                                    &                 \\
                                          & \preperr{E_1}         &          & \ctrl{2}  & \ctrl{3} &          & \noise\redwire{14}              &                 &          & \redctrl{4}          & \redctrl{5}         &          & \preperr{E_{11}}                &                 &          & \redctrl{6}         & \redctrl{7}         &          &                  &                                                                    &                 \\
                                          & \preperr{E_2}         &          &           &          & \ctrl{2} & \preperr{E_7}                   &                 &          &                      &                     & \ctrl{4} & \preperr{E_{12}}                &                 &          &                     &                     & \ctrl{6} &                  &                                                                    &                 \\
        \lstick{$\ket{0}_{\text{A}_1}$}   &                       & \targ{}  & \targ{}   &          &          &                                 &                 &          &                      &                     &          &                                 &                 &          &                     &                     &          & \preperr{E_3}    & \meter{}                                                           & \setwiretype{c} \\
        \lstick{$\ket{0}_{\text{A}_2}$}   &                       &          &           & \targ{}  & \targ{}  &                                 &                 &          &                      &                     &          &                                 &                 &          &                     &                     &          & \preperr{E_4}    & \meter{}                                                           & \setwiretype{c} \\
                                          &                       &          &           &          &          & \lstick{$\ket{0}_{\text{A}_3}$} & \setwiretype{q} & \targ{}  & \redtarg\redwire{10} &                     &          &                                 &                 &          &                     &                     &          & \preperr{E_8}    & \redmeter\wire[r][1][style={draw=red,double, line width=1.5pt}]{q} & \setwiretype{n} \\
                                          &                       &          &           &          &          & \lstick{$\ket{0}_{\text{A}_4}$} & \setwiretype{q} &          &                      & \redtarg\redwire{9} & \targ{}  &                                 &                 &          &                     &                     &          & \preperr{E_9}    & \redmeter\wire[r][1][style={draw=red,double, line width=1.5pt}]{q} & \setwiretype{n} \\
                                          &                       &          &           &          &          &                                 &                 &          &                      &                     &          & \lstick{$\ket{0}_{\text{A}_5}$} & \setwiretype{q} & \targ{}  & \redtarg\redwire{4} &                     &          & \preperr{E_{13}} & \redmeter\wire[r][1][style={draw=red,double, line width=1.5pt}]{q} & \setwiretype{n} \\
                                          &                       &          &           &          &          &                                 &                 &          &                      &                     &          & \lstick{$\ket{0}_{\text{A}_6}$} & \setwiretype{q} &          &                     & \redtarg\redwire{3} & \targ{}  & \preperr{E_{14}} & \redmeter\wire[r][1][style={draw=red,double, line width=1.5pt}]{q} & \setwiretype{n}
    \end{quantikz}
    % tex-fmt: on

    \caption{
        Repeated syndrome extraction circuit for the three-qubit
        repetition code under phenomenological noise.
    }
\end{figure}

%%%%%%%%%%%%%%%%
\subsection{Detector Error Matrix}
\label{subsec:Detector Error Matrix}

% Core idea

% TODO: Make this a proper definition?
Instead of using the measurements as parity indicators directly, we
may wish to combine them in some way.
We call such combinations \emph{detectors}.
Formally, a detector is a parity constraint on a set of measurement
outcomes \cite[Def.~2.1]{derks_designing_2025}.

\content{Detector matrix}

\content{Detector error matrix}

\content{One way of defining the detectors is ...}

\begin{figure}[H]
    \centering

    \tikzset{
        gate/.style={
            draw, %line width=1pt,
            minimum height=2cm,
        }
    }

    % tex-fmt: off
    \begin{quantikz}[row sep=2mm, column sep=4mm, wire types={q,q,q,n,n,n}]
        \lstick[3]{$\ket{\psi}_\text{L}$} & \gate[3]{\text{SE}_1}          &                & \gate[3]{\text{SE}_2}                   &                & \gate[3]{\text{SE}_3}                   &                & \gate[3]{\text{SE}_4}                   &                            \\
                                          &                                &                &                                         &                &                                         &                &                                         & & \setwiretype{n} & \ldots \\
                                          & \wire[d][3]{c}                 &                & \wire[d][1]{c}                          &                & \wire[d][1]{c}                          &                & \wire[d][1]{c}                          &                            \\
                                          & \ctrl[wire=c]{0}\wire[r][1]{c} & \wire[d][1]{c} & \ctrl[vertical wire=c]{1}\wire[r][1]{c} & \wire[d][1]{c} & \ctrl[vertical wire=c]{1}\wire[r][1]{c} & \wire[d][1]{c} & \ctrl[vertical wire=c]{1}\wire[r][1]{c} &                            \\
                                          &                                & \wire[r][1]{c} & \targ{}\wire[d][1]{c}                   & \wire[r][1]{c} & \targ{}\wire[d][1]{c}                   & \wire[r][1]{c} & \targ{}\wire[d][1]{c}                   &                            \\
                                          & \gate[1]{\bm{D}_1}             &                & \gate[1]{\bm{D}_2}                      &                & \gate[1]{\bm{D}_3}                      &                & \gate[1]{\bm{D}_4}                      &                            \\
    \end{quantikz}
    % tex-fmt: on

    \caption{Construction of detectors from measurements in the general case.}
\end{figure}

\content{The three-qubit repetition code as an exmaple}

\begin{figure}[H]
    \centering

    \hspace*{-5mm}
    \begin{minipage}{0.42\textwidth}
        \newcommand{\redwire}[1]{
            \wire[r][#1][style={draw=red, line width=1.5pt, double}]{q}
        }
        \newcommand{\inwire}{
            \wire[l][1][style={draw=red, line width=1.5pt}]{q}
        }
        \newcommand{\redtarg}{
            \targ[style={draw=red,line width=1.5pt}]{}%
            \setwiretype{n}%
        }
        \newcommand{\redctrl}[1]{
            \ctrl[style={draw=red,fill=red, line width=1.5pt}]{0}%
            \wire[d][#1][style={draw=red, line width=1.5pt, double}]{q}
        }
        \newcommand{\redmeter}{\meter[style={draw=red,fill=red!20}]{}}
        \newcommand{\redgate}[1]{\gate[style={draw=red,fill=red!20}]{\textcolor{red}{#1}}}

        % tex-fmt: off
        \begin{quantikz}[row sep=4mm, column sep=3mm, wire types={n,n,n,n,n,n}]
            & \meter{}\wire[l][1]{q}\wire[r][1]{c} & \setwiretype{c}               &                           &             & \ctrl[vertical wire=c]{2} &                           & \gate{D_1} \\
            & \meter{}\wire[l][1]{q}\wire[r][1]{c} & \setwiretype{c}               &                           &             &                           & \ctrl[vertical wire=c]{2} & \gate{D_2} \\
            & \redmeter{}\inwire\redwire{6} &      & \redctrl{2}                   &                           & \targ{}     &                           & \redgate{D_3} \\
            & \redmeter{}\inwire\redwire{6} &      &                               & \redctrl{2}               &             & \targ{}                   & \redgate{D_4} \\
            & \redmeter{}\inwire\redwire{2} &      & \redtarg\wire[r][4]{c}        &                           &             &                           & \gate{D_5} \\
            & \redmeter{}\inwire\redwire{3} &      &                               & \redtarg\wire[r][3]{c}    &             &                           & \gate{D_6}
        \end{quantikz}
        % tex-fmt: on
    \end{minipage}%
    \begin{minipage}{0.56\textwidth}
        \newcommand\cc{\cellcolor{orange!20}}

        \begin{align*}
            \bm{H} =
            % tex-fmt: off
            \left(\begin{array}{ccccccccccccccc}
                1      & 1      & 0      & 1      & 0      & 0      & 0      & 0      & 0      & 0      & 0 & 0 & 0 & 0 & 0  \\
                0      & 1      & 1      & 0      & 1      & 0      & 0      & 0      & 0      & 0      & 0 & 0 & 0 & 0 & 0  \\
                \cc{0} & \cc{0} & \cc{0} & \cc{1} & \cc{0} & 1      & 1      & 0      & 1      & 0      & 0 & 0 & 0 & 0 & 0  \\
                \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{1} & 0      & 1      & 1      & 0      & 1      & 0 & 0 & 0 & 0 & 0  \\
                \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{1} & \cc{0} & 1 & 1 & 0 & 1 & 0  \\
                \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{0} & \cc{1} & 0 & 1 & 1 & 0 & 1
            \end{array}\right)
            % tex-fmt: on
        \end{align*}
    \end{minipage}

    \caption{Construction of detectors from the measurements of a
    three-qubit repetition code.}
    \label{fig:Construction of the detectors from the measurements}
\end{figure}

%%%%%%%%%%%%%%%%
\subsection{Detector Error Models}
\label{subsec:Detector Error Models}

\content{Combination of detector error matrix and noise model}
\content{Contains all information necessary for decoding
\cite[Intro.]{derks_designing_2025}}
\content{Not only useful for decoding, but also for ... (Derks et al.)}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Practical Considerations}
\label{sec:Practical Considerations}

% Intro

\content{Intro}

%%%%%%%%%%%%%%%%
\subsection{Practical Methodology}
\label{subsec:Practical Methodology}

\content{Per-round-LER explanation}

%%%%%%%%%%%%%%%%
\subsection{Stim}
\label{subsec:Stim}

\content{Circuit code heavily depends on the exact circuit construction}
\content{Not easy to predict how errors at different locations
propagate through the circuit an what detectors they affect}

\content{Stim is a software package that generates DEMs from circuits}
\content{The user still has to define the circuit themselves, and
especially the detectors \cite[Sec~2.5]{derks_designing_2025}}

