\chapter{Fault Tolerant QEC}
\section{Fault Tolerance}
\section{Noise Models}
\subsection{Depolarizing Channel}
\subsection{Phenomenological Noise}
\subsection{Circuit-Level Noise}
\section{Detector Error Models}
\subsection{Measurement Syndrome Matrix}
\subsection{Detector Error Matrix}
\subsection{Detector Error Models}
\section{Practical Considerations}
\subsection{Practical Methodology}
\subsection{Stim}

\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{subfigure}{0.48\textwidth}
        \centering
        % tex-fmt: off
        \begin{quantikz}[row sep=4mm, column sep=4mm]
                                            & \xyzerr & \ctrl{3} &            &          &           &           &                 \\
            \lstick{$\ket{\psi}$}           & \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

        \caption{Depolarizing channel.}
    \end{subfigure}%
    \begin{subfigure}{0.48\textwidth}
        \centering
        % tex-fmt: off
        \begin{quantikz}[row sep=4mm, column sep=4mm]
                                            & \xerr & \ctrl{3} &           &          &          &       &           &                 \\
            \lstick{$\ket{\psi}$}           & \xerr &          & \ctrl{2}  & \ctrl{3} &          &       &           &                 \\
                                            & \xerr &          &           &          & \ctrl{2} &       &           &                 \\
            \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

        \caption{Phenomenological noise.}
    \end{subfigure}

    \vspace*{3mm}
    \centering
    \begin{subfigure}[c]{0.75\textwidth}
        \centering
        % tex-fmt: off
        \begin{quantikz}[row sep=4mm, column sep=2mm]
                                            & \xyzerr & \ctrl{3} & \xyzerr \wire[d][3]{q} &           &                        &          &                        &          &                        &       &           &                 \\
            \lstick{$\ket{\psi}$}           & \xyzerr &          &                        & \ctrl{2}  & \xyzerr \wire[d][2]{q} & \ctrl{3} & \xyzerr \wire[d][3]{q} &          &                        &       &           &                 \\
                                            & \xyzerr &          &                        &           &                        &          &                        & \ctrl{2} & \xyzerr \wire[d][2]{q} &       &           &                 \\
            \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

        \caption{Circuit-level noise.}
    \end{subfigure}%
    \hfill%
    \begin{subfigure}[c]{0.25\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{subfigure}

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

\begin{figure}[t]
    \centering

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

        \node[signal, draw, fill=orange!20, minimum height=2cm,
        minimum width=2.5cm, align=left, signal pointer angle=140]
        at (-2.45, 0) (input) {Input\\ Errors};

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

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

        \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.}
\end{figure}

\begin{figure}[t]
    \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=orange!20}]{\scriptstyle #1}
    }

    \vspace*{10mm}

    \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}$}        & \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 two-qubit
        repetition code under bit flip noise.
    }
\end{figure}

\begin{figure}[t]
    \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*{10mm}

    \newcommand{\preperr}[1]{
        \gate[style={fill=orange!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

    \hspace*{-7mm}
    % tex-fmt: off
    \begin{quantikz}[row sep=4mm, column sep=4mm, wire types={q,q,q,q,q,n,n,n,n}]
        \lstick[3]{$\ket{\psi}$}        & \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 two-qubit
        repetition code under phenomenological noise.
    }
\end{figure}

