\documentclass[unknownkeysallowed]{beamer}

\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{subcaption}

\definecolor{highlight-colour}{RGB}{153, 255, 204}

\newcommand{\highlight}[1]{%
  \colorbox{highlight-colour}{$\displaystyle#1$}}

\usetheme{Antibes}
\usecolortheme{lily}

\mode<presentation>

\title[Agency in disease modelling]{Introducing features of agency into computational models of infectious disease}
\author{Ethan Kelly\\ \texttt{e.kelly.1@research.gla.ac.uk}}
\date{27 May 2021}

\begin{document}

% =============================================================================
% Title slide.
{
  \usebackgroundtemplate{\includegraphics[height=\paperheight]{assets/title-slide}}

  \begin{frame}[plain]
    \setbeamercolor{title}{fg=white}
    \setbeamercolor{author}{fg=white}
    \setbeamercolor{date}{fg=white}

    \begin{columns}

      \begin{column}[l]{3cm}
      \end{column}

      \begin{column}[r]{7cm}
        \titlepage
      \end{column}

    \end{columns}
  \end{frame}
}

% Table of contents.
\begin{frame}
  \frametitle{Outline}
  \tableofcontents
\end{frame}

% =============================================================================
\section{Introduction to Computational Disease Modelling}

% -----------------------------------------------------------------------------

\begin{frame}
  \frametitle{Outline}
  \tableofcontents[currentsection]
\end{frame}

% -----------------------------------------------------------------------------
\subsection{Graph Theory}

\begin{frame}{What is a Graph?}

What is a graph (network)?

  \begin{itemize}
    \pause
  \item A set of objects where some pairs are related.
    \pause
  \item Objects are called {\it vertices} or {\it nodes}
    \pause
  \item Relations between objects are called {\it edges}.
  \end{itemize}
%  \pause
%  \centering
%  \includegraphics[width=0.5\textwidth]{assets/mini-tree}

\end{frame}
% -----------------------------------------------------------------------------
%\begin{frame}{Examples of Graphs}
%
%\begin{columns}
%
%\begin{column}{.6\textwidth}
%\includegraphics[width=.8\linewidth,valign=t]{assets/er}
%\end{column}
%
%\begin{column}{.4\textwidth}
%\textbf{Example: Erd\H{o}s-R\'{e}nyi}
%
%\vspace{5pt}
%
%Random graph with a given number of vertices (in this case, 5) and a probability with which an edge exists between any two vertices.
%
%\end{column}
%
%\end{columns}
%\end{frame}
% -----------------

\begin{frame}{Examples of Graphs}

\begin{columns}

\begin{column}{.6\textwidth}
\includegraphics[width=.8\linewidth,valign=t]{assets/tree}
\end{column}

\begin{column}{.4\textwidth}
\textbf{Example: Tree}

\vspace{5pt}

A tree is a special type of graph where {\it any two vertices are connected by exactly one path.}
\end{column}

\end{columns}

\end{frame}

% -----------------

\begin{frame}{Examples of Graphs}

\begin{columns}

\begin{column}{.6\textwidth}
\includegraphics[width=.8\linewidth,valign=t]{assets/florentine}
\end{column}

\begin{column}{.4\textwidth}
\textbf{Example: Florentine Families Graph}
\vspace{5pt}

Depicts the marital alliances between Renaissance Florentine families %\cite{padgett_1993}.

\end{column}

\end{columns}

\end{frame}

% -----------------------------------------------------------------------------
\subsection{Games on Graphs}

\begin{frame}{Using Graphs to Model Disease Spread}

\textbf{The Firefighter Problem ({\scshape Firefighter})} %\cite{hartnell_1995}
models an outbreak of fire with a firefighter strategically blocking its path.

\begin{itemize}
	\pause
	\item At $t=0$, a fire breaks out at some vertex in the graph.
	\pause
	\item Firefighter then `protects' some other vertex.
	\pause
	\item Fire spreads to any adjacent vertices neither protected nor burnt.
	\pause
	\item Firefighter protects another vertex, the fire spreads again and so on.
\end{itemize}

\end{frame}

% -----------------

\begin{frame}{Example}
\centering\includegraphics[height=0.8\textheight]{assets/eg-fire/0}
\end{frame}

\begin{frame}{Example}
\centering\includegraphics[height=0.8\textheight]{assets/eg-fire/1}
\end{frame}

\begin{frame}{Example}
\centering\includegraphics[height=0.8\textheight]{assets/eg-fire/2}
\end{frame}

\begin{frame}{Example}
\centering\includegraphics[height=0.8\textheight]{assets/eg-fire/3}
\end{frame}

\begin{frame}{Example}
\centering\includegraphics[height=0.8\textheight]{assets/eg-fire/4}
\end{frame}

\begin{frame}{Example}
\centering\includegraphics[height=0.8\textheight]{assets/eg-fire/5}
\end{frame}

\begin{frame}{Example}
\centering\includegraphics[height=0.8\textheight]{assets/eg-fire/6}
\end{frame}

% -----------------

\begin{frame}{Firefighter as a Model for Disease Spread}

Benefits:
\begin{itemize}
	\pause
	\item Previous work means lots of results we can use.
	\pause
	\item We can change graphs to suit context - e.g. small world, preferential attachment, particular density.
\end{itemize}
\pause
Limitations:
\begin{itemize}
	\pause
	\item Fairly rudimentary model for disease spread but already NP-hard.
	\pause
	\item Defence and infection are discrete but epidemic propagation is a stochastic process.
	\pause
	\item Only interventions in halting disease spread are {\it external}, no way for individuals to avoid contraction personally.
\end{itemize}

\end{frame}

% =============================================================================
\section{Extending existing graph models to account for agency}

% -----------------------------------------------------------------------------
\begin{frame}
  \frametitle{Outline}
  \tableofcontents[currentsection]
\end{frame}
% -----------------------------------------------------------------------------
\subsection{Attributes of Agency}
\begin{frame}{Attributes of Agency}

If each vertex in the graph represents an individual agent, we can start to assign agency-related attributes. We are particularly interested in each agent's {\it internal inclination towards protection}. In context, this might involve:
\begin{itemize}
	\pause
	\item Wearing PPE correctly
	\pause
	\item Hand hygiene
	\pause
	\item Strict physical distancing
\end{itemize}

\end{frame}

\begin{frame}
\centering\includegraphics[height=.9\textheight]{assets/flowcharts/protection}
\end{frame}

% -----------------------------------------------------------------------------
\begin{frame}{Defence Strategies}

What do these amendments mean for how the game is played?\pause

\vspace{5pt}

In usual formulation, general rule of thumb: {\it for sparse graphs, defend based on proximity to fire (break ties on degree); for dense graphs, defend based on highest degree (break ties on proximity)}. \pause

\vspace{5pt}

However, in our adjusted formulation we have more candidates for defence strategies. One such novel strategy is to defend based on highest agent protection rating.

\end{frame}

% -----------------------------------------------------------------------------
\subsection{Protection Rating Allocation and Defence Strategies}

\begin{frame}
\centering\includegraphics[height=.9\textheight]{assets/flowcharts/defence}
\end{frame}

% -----------------

\begin{frame}{Deterministic Protection}
\centering\includegraphics[height=0.85\textheight]{assets/charts/percent_infected/Deterministic.jpg}
\end{frame}

% -----------------
%
%\begin{frame}{Deterministic Protection}
%\centering\includegraphics[height=0.8\textheight]{assets/charts/winners/Deterministic.jpg}
%\end{frame}

% -----------------

\begin{frame}{Mixed Protection}
\centering\includegraphics[height=0.8\textheight]{assets/charts/percent_infected/Mixed.jpg}
\end{frame}

% -----------------

%\begin{frame}{Mixed Protection}
%\centering\includegraphics[height=0.8\textheight]{assets/charts/winners/Mixed.jpg}
%\end{frame}

% -----------------

\begin{frame}{Random Protection}
\centering\includegraphics[height=0.8\textheight]{assets/charts/percent_infected/Random.jpg}
\end{frame}

% -----------------
%
%\begin{frame}{Random Protection}
%\centering\includegraphics[height=0.8\textheight]{assets/charts/winners/Random.jpg}
%\end{frame}

% =============================================================================
%\section{Using Compartmental Frameworks}
%
%% -----------------------------------------------------------------------------
%\begin{frame}
%  \frametitle{Outline}
%  \tableofcontents[currentsection]
%\end{frame}
%
%% -----------------------------------------------------------------------------
%%\subsection{Standard $SIR$ model with fixed population}
%%
%%\begin{frame}
%%
%%The $SIR$ Model is a compartmental epidemiological model with three compartments (`states') related to an epidemic: {\it susceptible, infected} and {\it recovered.}
%%\begin{itemize}
%%	\pause
%%	\item $S(t)$ - people who don't have (but are able to contract) the infection at time $t$,
%%	\pause
%%	\item $I(t)$ - individuals who currently have the disease and are infectious,
%%	\pause
%%	\item $R(t)$ - people who have had the disease and have since recovered
%%	\pause
%%	\item For fixed population $N$, we have $S(t) + I(t) + R(t) = N$ - i.e. fixed population.
%%\end{itemize}
%%
%%\end{frame}
%%
%%% -----------------
%%
%%\begin{frame}
%%
%%Then, for rates of infection and recovery $\beta$ and $\gamma$ respectively, the standard $SIR$ model without vital dynamics is:
%%
%%\begin{align}
%%	\frac{dS}{dt} & = -\beta \frac{SI}{N} \label{dS}\\
%%	\frac{dI}{dt} & = \beta\frac{SI}{N} \gamma I \label{dI}\\
%%	\frac{dR}{dt} & = \gamma I - \mu R \label{dR}
%%\end{align}
%%
%%\end{frame}
%
%
%% -----------------------------------------------------------------------------
%\subsection{$SIR$ graph model}
%\begin{frame}
%
%Examine probability of an agent being in a given state:
%\begin{itemize}
%	\pause
%	\item $\langle A_i \rangle$ - time-independent probability of $i$ being in state $A$
%	\pause
%	\item $\langle A_i B_i \rangle$ - probability of $i$ and $j$ being in states $A$ and $B$ respectively.
%	\pause
%	\item $\beta_i$ - per-link infection rate for individual $i$.
%	\pause
%	\item $\gamma_i$ - recovery rate for $i$.
%	\pause
%	\item For transmission matrix $T$, assign $T_{ij}=\beta_i$ if there is a route of infection between $i$ and $j$ ($T_{ij}=0$ otherwise).
%	\pause
%	\item Often, we will consider unweighted and undirected graphs, but in general $T_{ij}$ may not equal $T_{ji}$.
%\end{itemize}
%
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%
%We can replace $\beta_i$ with a term involving a transmission matrix of a network to begin extending the usual $SIR$ model into a network realm.
%%Using the substitution $ \beta_i \frac{SI}{N} = \sum^{N}_{j=1}T_{ij} \langle S_i I_j \rangle,$ the equations become
%\begin{align*}
%\dot{\langle S_i \rangle} & = -\sum^{N}_{j=1}T_{ij} \langle S_i I_j \rangle\\
%\dot{\langle I_i \rangle} & =\sum^{N}_{j=1}T_{ij}\langle S_i I_j \rangle - \gamma_i \langle I \rangle \\
%\dot{\langle R_i \rangle} & = \gamma_i \langle I \rangle,
%\end{align*}
%which are the evolution equations given in \cite{kiss_2014}.
%
%\end{frame}
%
%% ----------------------------------------------------------------------------
%
%\subsection{Adding a new state}
%
%\begin{frame}{Protected State}
%\begin{itemize}
%	\item $\zeta_i$ - probability that we defend individual $i$,
%	\item $\alpha_i$ - efficacy of the protection measure for individual $i$ (may decay over time and vary from person to person).
%\end{itemize}
%\pause
%Using these rates of protection and effectiveness, for fixed population size the differential equations become:
%\pause
%\begin{align}
%\dot{\langle S_i \rangle} & = \alpha_i \langle P_i \rangle - \sum^{N}_{j=1}T_{ij} \langle S_i I_j \rangle - \zeta_i\langle S_i \rangle\\
%\dot{\langle I \rangle} & =\sum^{N}_{j=1}T_{ij}\langle S_i I_j \rangle - \gamma_i \langle I \rangle \\
%\dot{\langle R_i \rangle} & = \gamma_i \langle I \rangle \\
%\dot{\langle P_i \rangle} & = \zeta_i \langle S_i \rangle - \alpha_i \langle P_i \rangle.
%\end{align}
%
%\end{frame}

% -----------------

% =============================================================================

%\section{Describing Compartmental Graph Models Exactly}
%
%% -----------------------------------------------------------------------------
%\begin{frame}
%  \frametitle{Outline}
%  \tableofcontents[currentsection]
%\end{frame}
%
%% -----------------------------------------------------------------------------
%\subsection{Required Equations}
%
%\begin{frame}{Numbers of Equations}
%
%\begin{columns}
%\begin{column}{0.5\textwidth}
%Consider the triangle graph. The equations required to precisely express the system $SIR$ dynamics of this network are as below \cite{kiss_2014}.
%\end{column}
%
%\begin{column}{0.5\textwidth}
%\centering\includegraphics[width=0.4\textwidth]{assets/triangle}
%\end{column}
%\end{columns}
%
%\begin{align}
%\text{6 singles: } & \dot{\langle S_1 \rangle}, \dot{\langle S_2 \rangle}, \dot{\langle S_3 \rangle}, \dot{\langle I_1 \rangle}, \dot{\langle I_2 \rangle}, \dot{\langle I_3 \rangle}.\label{eq:SIRsingle}\\
%\text{6 doubles: } & \dot{\langle S_1 I_2 \rangle},\dot{\langle I_1 S_2 \rangle}, \dot{\langle S_1 I_3 \rangle}, \dot{\langle I_1 S_3 \rangle}, \dot{\langle S_2 I_3\rangle}, \dot{\langle I_2 S_3 \rangle}.\label{eq:SIRdouble}\\
%\text{6 triples: } & \dot{\langle S_1 I_2 I_3 \rangle}, \dot{\langle S_1 I_2 S_3 \rangle}, \dot{\langle S_1 S_2 I_3 \rangle}, \dot{\langle I_1 S_2 S_3 \rangle}, \dot{\langle I_1 I_2 S_3 \rangle}, \dot{\langle I_1 S_2 I_3 \rangle}. \label{eq:SIRtriple}
%\end{align}
%
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%
%Using the equations for the $SIRP$ model, we have the following equation requirements:
%\begin{align*}
%\text{9 singles: (} \ref{eq:SIRsingle} \text{) and } & \dot{\langle P_1 \rangle}, \dot{\langle P_2 \rangle}, \dot{\langle P_3 \rangle}.\\
%\text{18 doubles: (} \ref{eq:SIRdouble} \text{) and }& \dot{\langle S_1 P_2 \rangle},\dot{\langle P_1 S_2 \rangle}, \dot{\langle I_1 P_2 \rangle}, \dot{\langle P_1 I_2 \rangle}, \dot{\langle S_1 P_3\rangle}, \dot{\langle P_1 S_3 \rangle},\\ & \dot{\langle I_1 P_3 \rangle}, \dot{\langle P_1 I_3 \rangle}, \dot{\langle S_2 P_3 \rangle}, \dot{\langle P_2 S_3 \rangle}, \dot{\langle I_2 P_3 \rangle}, \dot{\langle P_2 I_3 \rangle}. \\
%\text{24 triples: (} \ref{eq:SIRtriple} \text{) and } & \dot{\langle S_1 S_2 P_3 \rangle}, \dot{\langle S_1 P_2 S_3 \rangle}, \dot{\langle S_1 I_2 P_3\rangle}, \dot{\langle S_1 P_2 I_3 \rangle}, \dot{\langle S_1 P_2 P_3\rangle}, \\
%& \dot{\langle I_1 S_2 P_3 \rangle}, \dot{\langle I_1 P_2 S_3 \rangle}, \dot{\langle I_1 I_2 P_3 \rangle}, \dot{\langle I_1 P_2 I_3 \rangle}, \dot{\langle I_1 P_2 P_2 \rangle},\\
%& \dot{\langle P_1 S_2 S_3 \rangle}, \dot{\langle P_1 S_2 I_3 \rangle}, \dot{\langle P_1 I_2 S_3 \rangle}, \dot{\langle P_1 I_2 I_3 \rangle}, \dot{\langle P_1 P_2 I_3 \rangle}, \\
%& \dot{\langle P_1 P_2 S_3 \rangle}, \dot{\langle P_1 S_2 P_3 \rangle},  \dot{\langle P_1 I_2 P_3\rangle}.
%\end{align*}
%
%\end{frame}

% -----------------------------------------------------------------------------
%\subsection{Closures}
%
%\begin{frame}{What are closures?}
%
%\begin{itemize}
%	\pause
%	\item In the previous section, we saw equations for singles
%	\pause
%	\item We have also seen equations needed for the triangle network (including doubles and triples)
%	\pause
%	\item How many equations do we need in general?
%\end{itemize}
%\pause
%The number of other equations will usually increase when we require closures, as closure equations will sometimes require equations that we don't usually consider as they are dynamically uninteresting, e.g. $\langle S_i S_j \rangle$ where $i$ and $j$ are adjacent.
%
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%
%Consider the equations below for calculating singles and pairs:
%\begin{align*}
%\langle \dot{S_i} \rangle & = -\sum^N_{j=1}T_{ij}\langle S_iI_j\rangle,\\
%\langle\dot{I_i}\rangle & = \sum^N_{j=1}T_{ij}\langle S_iI_j\rangle - \gamma_i\langle I_i \rangle,\\
%\langle\dot{S_iI_j}\rangle & = \sum^N_{k=1,k\neq i}T_{jk}\langle S_i S_j I_k \rangle - \sum^N_{k=1,k\neq j}T_ik\langle I_k S_i I_j \rangle \\
%			    	      & ~~~~~~~~~~~~~~~~~- T_{ij}\langle S_iI_j \rangle - \gamma_i\langle S_i I_j \rangle,\\
%\langle \dot{S_i S_j}\rangle & = - \sum^N_{k=1,k\neq j}T_{ik}\langle I_k S_i S_j \rangle - \sum^N_{k=1,k\neq i}T_{jk}\langle S_i S_j I_k \rangle,
%\end{align*}
%
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%
%\begin{align*}
%\langle \dot{S_i} \rangle & = -\sum^N_{j=1}T_{ij}\langle S_iI_j\rangle,\\
%\langle\dot{I_i}\rangle & = \sum^N_{j=1}T_{ij}\langle S_iI_j\rangle - \gamma_i\langle I_i \rangle,\\
%\langle\dot{S_iI_j}\rangle & = \sum^N_{k=1,k\neq i}T_{jk}\highlight{\langle S_i S_j I_k \rangle} - \sum^N_{k=1,k\neq j}T_ik\highlight{\langle I_k S_i I_j \rangle} \\
%			    	      & ~~~~~~~~~~~~~~~~~- T_{ij}\langle S_iI_j \rangle - \gamma_i\langle S_i I_j \rangle,\\
%\langle \dot{S_i S_j}\rangle & = - \sum^N_{k=1,k\neq j}T_{ik}\highlight{\langle I_k S_i S_j \rangle} - \sum^N_{k=1,k\neq i}T_{jk}\highlight{\langle S_i S_j I_k \rangle},
%\end{align*}
%
%This is {\it not} a closed system - we require equations for triples.
%
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%\centering\includegraphics[height=0.9\textheight]{assets/eg-cutvertex/0}
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%\centering\includegraphics[height=0.9\textheight]{assets/eg-cutvertex/1}
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%\centering\includegraphics[height=0.9\textheight]{assets/eg-cutvertex/2}
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%\centering\includegraphics[height=0.9\textheight]{assets/eg-cutvertex/3}
%\end{frame}
%
%% -----------------
%
%\begin{frame}{Calculating Closures from Cut-Vertex Sets}
%
%Let $G = \{V,E\}$ be a graph on $N$ vertices. \pause Consider a connected subset of vertices $F = {v_1,v_2,\dots,v_k}\subset V$ \pause and assume $\exists v_{i^\ast} \in F$ a cut-vertex in $G$, such that $F \setminus \{v_{i^\ast}\}$ is partitioned into at least two disjointed components with vertices $F_1 = {v_1, v_2,\dots, v_{i-1}}$ and $F_2 = {v_{i+1}, v_{i+2},\dots, v_k}$ belonging to any such two, distinct and disjointed components or subnetworks. \pause Then the following equation holds:
%
%\begin{align*}
%\langle Z_{v_1} Z_{v_2},& \dots Z_{v_{i-1}} S_{v_i^\ast} Z_{v_{i+1}} \dots Z_{v_k} \rangle (t) = \\
%&\frac{\langle Z_{v_1} Z_{v_2} \dots Z_{v_{i-1}} S_{v_i^\ast}\rangle(t)\langle S_{v_i^\ast} Z_{v_{i+1}} \dots Z_{v_k} \rangle}{\langle S_{v_i^\ast} \rangle (t)}
%\end{align*}
%
%where $Z_{v_i} \in \{S, I\}~\forall v_i \neq v_{i^\ast}$. \cite{kiss_2014}
%
%\end{frame}
%
%% -----------------
%
%\begin{frame}{Bounding}
%Denote by $\text{Ind}(v_{i_j})$ for $j=1,2,\dots,L$ the number of subgraph $v_{i_j}$ belongs to.\\
%\textbf{Upper bound:}
%\begin{equation*}
%N_{EQ}(G) = \sum^P_{i=1}m_if_i-2\sum^L_{j=1}(\text{Ind}(v_{i_j})-1).
%\end{equation*}
%We take a sum across number of equations for all subgraphs and subtract unnecessary multiplications from cut-vertices being replaced into produced sub-graphs. \cite{kiss_2014}
%
%\end{frame}
%
%% -----------------
%
%\begin{frame}{Bounding}
%\textbf{Upper bound:}
%\begin{equation*}
%N_{EQ}(G) = \sum^P_{i=1}\highlight{m_i}f_i-2\sum^L_{j=1}(\text{Ind}(v_{i_j})-1).
%\end{equation*}
%Note: $m_i$ denotes the number of equations required for subnetwork $i$, e.g.
%\begin{itemize}
%	\item An edge requires 7 equations (4 for singles, 3 for pairs)
%	\item A triangle requires 22 equations
%	\item A cycle on 4 vertices requires 45 equations
%\end{itemize}
%\end{frame}
%
%% -----------------
%
%\begin{frame}
%We can in fact bound these multipliers. From the requirements of a subgraph (connected, no cut vertices), for a number of vertices $n$ our worst-case scenario is the complete graph (everything is connected to everything else) and our best-case scenario is the cycle on $n$ vertices.
%
%%\pause
%%\begin{figure}
%%\centering
%%  \begin{subfigure}{0.5\textwidth}
%%    \centering
%%    \includegraphics[width=0.45\textwidth]{assets/complete}
%%    \caption{Number of equations: X}
%%  \end{subfigure}%
%%\pause
%%  \begin{subfigure}{.5\textwidth}
%%  \centering
%%    \includegraphics[width=0.49\textwidth]{assets/cycle}
%%    \caption{Number of Equations: Y}
%%  \end{subfigure}
%% \caption{Best and Worst Case Scenarios}
%%\end{figure}
%
%\end{frame}

% =============================================================================
\section{Conclusion}

% -----------------------------------------------------------------------------
\begin{frame}
\frametitle{Outline}
\tableofcontents[currentsection]
\end{frame}

% -----------------------------------------------------------------------------
\begin{frame}

We discussed:

\begin{itemize}
	\pause
	\item Some fundamentals of Graph Theory
	\pause
	\item Using {\scshape Firefighter} as a model for disease spread
	\pause
	\item Failings of this approach
	\pause
	\item How agency can address these issues
	\pause
	\item What agency-based modelling looks like
\end{itemize}

\end{frame}

%\begin{frame}{References}
%\bibliographystyle{siam}
%\bibliography{slides-bib}
%\end{frame}

% =============================================================================
% Back slide.
{
  \usebackgroundtemplate{\includegraphics[height=\paperheight]{assets/title-slide}}

  \begin{frame}[plain]

    \begin{columns}

      \begin{column}[l]{.4\textwidth}
      \end{column}

      \begin{column}[r]{.6\textwidth}
        \centering\textcolor{white}{
          Thanks for listening!\\
          }
          \vspace{20pt}
          \centering\textcolor{white}{
          Any questions?
        }
      \end{column}

    \end{columns}
  \end{frame}
}

\end{document}


%% LyX 2.0.8.1 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{graphicx}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% A simple dot to overcome graphicx limitations
\newcommand{\lyxdot}{.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{algorithm} % http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{mathtools}
\newcommand\tab[1][1cm]{\hspace*{#1}}

\makeatother

\usepackage{babel}
\begin{document}

\title{GAntenna Cover Algorithm}

\maketitle

\section*{Problem Statement}

Given the following:
\begin{itemize}
\item An unlimited supply of sensors, each of which can have multiple antennas,
such that :

\begin{itemize}
\item All antennas have the same aperture angle.
\item A sensor has at least one antenna.
\item A given sensor can have multiple antennas.
\item Each antenna belonging to a given sensor has the same sensitivity
but each antenna assigned to a sensor may have a different roatation
(azimuth) angle.
\item Different sensors may have antennas having different sensitivities.
\item Sensors can each have different numbers of antennas.
\end{itemize}
\item A set of \textit{possible\_locations} in 2-d space where sensors may
be placed sorted in z-order (also known as Morton order). Call the
lines joining these points in order the \textit{Coastal Line} (CL).
\item An set of \textit{interference\_knot\_points} sorted in z-order. Call
the lines joining these points the \textit{Interference Contour} (IC).
\item CL and IC do not intersect.
\end{itemize}
We call the simple polygon constructed by adding edges from the extremities
of IC to CL, the \textit{Coverage Region} (CR). Our goal is to cover
CR such that:
\begin{itemize}
\item The the entire area of CR is covered to within a defined tolerance
(i.e. the tolerance is some small number < 1 denoting the fraction
of the area not covered by any antenna).
\item The region outside CR covered by the antennas is minimized. We call
this the \textit{Excess Region} (ER).
\item The total area of the cover (i.e. the sum of all the areas covered
by each antenna) is minimized.
\item No two sensors are closer to each other than some defined minimum
distance \textit{min\_distance}
\end{itemize}
Determine the following:
\begin{itemize}
\item Optimal placement of the sensors
\item Orientation of the antennas (i.e. azimuth angle with respoect to the
horizontal).
\item Calibration of the antennas (i.e. \textquotedbl{}optimum\textquotedbl{}
detection range of the antennas at each location where they are placed).
\end{itemize}

\section*{Algorithm High Level Description}

\textbf{Inputs}:
\begin{itemize}
\item The sorted set of \textit{possible\_centers}
\item The sorted set of \textit{interference\_knot\_points }
\item The \textit{min\_distance} specifying the minimum separation between
two sensors.
\item A family of concetric \textit{detection\_coverage} curves definining
the coverage regions for different antenna sensitivities all oriented
in the same direction. These antenna curves are closed convex curves
centered at \textit{(0,0)} that, in general do not pack into a circle
without overlap.
\end{itemize}

\paragraph{Algorithm}
\begin{enumerate}
\item Construct the \textit{coverage\_region} (CR):

\begin{enumerate}
\item Constuct a multi-line segment consisting of the points in \textit{possible\_centers}
joined in order.
\item Construct a multi-line segment consisting of the \textit{interference\_knot\_points}
joined in order.
\item Complete the simple polygon to generate CR.
\end{enumerate}
\item Generate a rectangular grid of points such that each point of the
grid is entirely within the region CR. ( We start with some default
grid spacing. This may be adjusted in subsequent steps.) We call the
grid of points thus generated the \textit{interference\_set}.
\item Generate a greedy circle cover (isotropic antenna) for \textit{(possible\_centers,
interference\_set, minum\_separation) }using algorithm \textit{min\_area\_cover\_greedy}.
This returns a set of centers chosen from \textit{possible\_centers}
and a set of radii of circles that cover the\textit{ interference\_set}
such that antennas are spaced apart by at least \textit{min\_distance}
and the set of circles completely covers \textit{interference\_set}
\item Place the antenna lobes (chosen from \textit{detection\_coverage}
) within the circles returned from step 3 such that the antenna lobes
completely cover the intersection area between the circles and CR
using algorithm \textit{find\_antenna\_overlay\_for\_sector}.
\item Using simulated annealing with a cost function defined as the area
of the convex hull of the cover, rotate the antenna cover lobes on
their assigned sensors so that the cost function is minimized.
\item Eliminate redundant antenna lobes.
\end{enumerate}
We now present the non-obvious steps in greater detail:

\include{algo}

Pictorially, after step 3, we achieve the following circle cover.

\includegraphics{/home/mranga/circle-cover/figures/estuary_areacover}

The detection\_coverage curves are depicted pictorially as follows:

\includegraphics{/home/mranga/circle-cover/figures/DetectionCoverage_60deg}

After overlaying the detection coverage curves on the circle\_cover
and overlaying, we get the following result. Note that small regions
are uncovered because we eliminate lobes that cover less than threshold
points on our interference\_set :

\includegraphics{/home/mranga/circle-cover/figures/estuary_antenna_60}

After applying simulated annealing on the figure above, we get:

\includegraphics{/home/mranga/circle-cover/figures/estuaryanneal_antenna_60}
\end{document}


%%%%%%%%%%%%%%% PLEASE NOTE THE FOLLOWING STYLE RESTRICTIONS %%%%%%%%%%%%%%%

%%  1. There are no new tags.  Existing LaTeX tags have been formatted to match
%%     the Preprint series style.
%%
%%  2. Do not change the margins or page size!  Do not change from the default
%%     text font!
%%
%%  3. You must use \cite in the text to mark your reference citations and
%%     \bibitem in the listing of references at the end of your chapter. See
%%     the examples in the following file. If you are using BibTeX, please
%%     supply the bst file with the manuscript file.
%%
%%  4. This macro is set up for two levels of headings (\section and
%%     \subsection). The macro will automatically number the headings for you.
%%
%%  5. No running heads are to be used for this volume.
%%
%%  6. Theorems, Lemmas, Definitions, Equations, etc. are to be double numbered,
%%     indicating the section and the occurrence of that element
%%     within that section. (For example, the first theorem in the second
%%     section would be numbered 2.1. The macro will
%%     automatically do the numbering for you.
%%
%%  7. Figures and Tables must be single-numbered.
%%     Use existing LaTeX tags for these elements.
%%     Numbering will be done automatically.
%%
%%  8. Page numbering is no longer included in this macro.
%%     Pagination will be set by the program committee.
%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[twoside,leqno,twocolumn]{article}

% Comment out the line below if using A4 paper size
\usepackage[letterpaper]{geometry}

\usepackage{ltexpprt}
\usepackage{hyperref}

\begin{document}

%
%\newcommand\relatedversion{}
%\renewcommand\relatedversion{\thanks{The full version of the paper can be accessed at \protect\url{https://arxiv.org/abs/1902.09310}}} % Replace URL with link to full paper or comment out this line

%\setcounter{chapter}{2} % If you are doing your chapter as chapter one,
%\setcounter{section}{3} % comment these two lines out.

\title{\Large FMshop: Domain Specific Language and Code Generation for Finite Element and Finite Volume in Julia}
%\author{}

\date{}

\maketitle

% Copyright Statement
% When submitting your final paper to a SIAM proceedings, it is requested that you include
% the appropriate copyright in the footer of the paper.  The copyright added should be
% consistent with the copyright selected on the copyright form submitted with the paper.
% Please note that "20XX" should be changed to the year of the meeting.

% Default Copyright Statement
%\fancyfoot[R]{\scriptsize{Copyright \textcopyright\ 2022 by SIAM\\
%Unauthorized reproduction of this article is prohibited}}

% Depending on which copyright you agree to when you sign the copyright form, the copyright
% can be changed to one of the following after commenting out the default copyright statement
% above.

%\fancyfoot[R]{\scriptsize{Copyright \textcopyright\ 20XX\\
%Copyright for this paper is retained by authors}}

%\fancyfoot[R]{\scriptsize{Copyright \textcopyright\ 20XX\\
%Copyright retained by principal author's organization}}

%\pagenumbering{arabic}
%\setcounter{page}{1}%Leave this line commented out.

\begin{abstract} \small\baselineskip=9pt
We introduce FMshop, a Julia-based domain specific language(DSL) for solving partial differential equations using finite element and finite volume methods. A key focus of the framework is code generation for either internal numerical solvers or external targets. Internal options use a modular set of solvers written in Julia to provide a direct, real-time solution from within the DSL itself. In contrast, the external code generation produces a set of code files that can be compiled or run in an external context such as Matlab, for smaller problems or prototypes, or the C++ framework Dendro for large-scale problems needing scalability. By generating code for external targets, we are able to take advantage of their capabilities without needlessly duplicating them, and provide a range of solutions tailored to the specific problem or the needs of the domain scientist. The modular design of FMshop allows the ongoing development of these target modules resulting an a more extensible framework and a broader set of suitable applications. The inclusion of both finite element and finite volume methods also contribultes to this goal. Another focus is on complex systems containing many PDEs that could be challenging to code by hand, but are relatively simple to specify using the DSL. In this paper we present the key features of FMshop that set it apart from many other DSL options, outline the current capabilities as well as several that are under development, and present some examples of it in action.
\end{abstract}

\section{Introduction}
Solving partial differential equations(PDEs) numerically on a large scale involves a compromise between highly optimized code that exploits details of the problem and hardware to build the most efficient solution, and extensible code that can be adapted to variations in the problem and the computing environment. Rapidly evolving technology and a shift to heterogeneous systems places a higher value on the latter, prompting a move away from hand-written code made by experts in high performance computing, to generated code produced through a domain specific language(DSL). Another factor in this change is the realm of medium-scale problems where the cost of developing optimal code may not be justified. At this scale it is up to domain scientists to develop their own software or piece it together from more general-purpose libraries.

In response to these issues, several DSLs for solving PDEs have been developed. On one end of the spectrum are general-purpose, high-level options such as Matlab Toolboxes, Comsol, and other commercial software. These tools are accessible to domain scientists without a high level of programming proficiency, but may lack customizability and performance tuning. At the opposite end are lower-level libraries such as Nektar++\cite{nektar} and deal.II\cite{dealii} that provide customizable components that are optimized for their specific purpose, but require much more programming input and skill from the user and therefore suffer from some of the limitations of hand-written code. There is also a middleground where almost all of a specified problem is handled within the scope of a moderately high-level DSL, providing an accessible and extensible option, while also offering some low-level customization. Some examples of these include the elegant Python interface of Fenics\cite{fenics} and Firedrake\cite{firedrake} for finite element methods, OpenFOAM\cite{openfoam} for finite volume methods, Devito\cite{devito} for finite difference methods, and many other options focused on a specific type of problem or technique. Of relevance to this work are tools recently developed for the Julia programming language including DifferentialEquations.jl\cite{diffeqjl} which has branched into a broad environment of ordinary differential equation solvers tied together in a Julia DSL.

This work introduces FMshop, a DSL for solving PDEs using the finite element(FE) or finite volume(FV) method. The goal is to provide a tool that a domain scientist can use to easily create efficient code for medium scale problems on simple hardware, and just as easily produce scalable code for larger problems on modern supercomputers. Two of the key ideas to achieving this goal are a modular software design and generation for external software frameworks. Rather than depending on a single, general-purpose code that may lack the flexibility to adapt to specific problem requirements or hardware resources, a range of more specific modules can be used, and the development of additional modules will allow the code to remain extensible. Code generation modules for external software targets allow it to leverage the capabilities of various existing software frameworks that are well suited to a particular type of problem. For example, the Dendro library\cite{dendro} provides an adaptive octree framework that is suitable for very large scale problems. FMshop provides a much simpler, more intuitive interface to this powerful resource.

FMshop is written completely in Julia, which offers a flexible, easy-to-use environment along with speed comparable to low-level languages such as C\cite{juliabench}. This allows a simplified interface without resorting to external C/C++/Fortran libraries as is common with Python-based DSLs. The metaprogramming features and wide selection of useful libraries also make Julia a good choice for FMshop.

\section{Related Work}
Code generation in combination with a DSL has become a common technique for producing high performance FE codes. Some techniques exploit tensor product construction for high order FE\cite{yateto}\cite{mcrae}\cite{homolya}. Others use the independent nature of Discontinuous Galerkin methods to utilize GPUs\cite{seisol} or vectorization\cite{kempf}. There are also options for FV\cite{pietro} and FD\cite{macia}, though perhaps fewer than for FE.

Another DSL that bears a close resemblance to FMshop is FEniCS. They share some DSL design concepts and since FMshop was originally developed for FE, the interface design was likely influenced by FEniCS. However, below the surface FMshop's focus is on code generation, and the tools provided differ significantly.

\section{Domain Specific Language}
The range of problems that can be solved with FMshop and the techniques used are increasing steadily with ongoing development. Here is an outline of the types of problems that are currently supported.\\
\textbf{FE problems:}
\begin{itemize}
\item 1, 2, or 3 spatial dimensions
\item Linear to high-order CG or DG projections
\item Scalar, vector, tensor, or indexed variables
\item Unstructured meshes of simplex or quad/hex elements
\end{itemize}
\textbf{FV problems:}
\begin{itemize}
\item 1, 2, or 3 spatial dimensions
\item Conservation type equations
\item Various low to high-order flux options
\item Custom flux specification
\item Variable and mesh options similar to FE above
\end{itemize}

Some of the available options are not supported by all generation targets, and currently the internal Julia target is the most complete in terms of breadth of capabilities.

\subsection{Interface}
Typically a script will be written, but it is also possible to work interactively. A set of functions or macros are used to a)Set up the configuration. b)Specify the problem and mesh. c)Process data for output.

As an example, the following commands could be used to configure a 2D unstructured grid using a fourth-order polynomial function space based on Lobatto-Gauss nodes, and generate code for a target specified in the external\_target\_module.jl file.
\begin{verbatim}
  generateFor("external_target_module.jl")
  domain(2, grid=UNSTRUCTURED)
  functionSpace(space=LEGENDRE, order=4)
  nodeType(LOBATTO)
\end{verbatim}
In contrast to this, a user who is content with the defaults could provide as little as \texttt{domain(2)}.

Problem specification should start with a mesh. There are some simple mesh generation options built in. For example, to construct a uniform 20x4x4 grid of hexahedral elements in a long rectangular prism domain with a separate boundary ID for each face, the following command could be used.
\begin{verbatim}
  mesh(HEXMESH, elsperdim=[20,4,4],
       bids=6, interval=[0,1,0,0.2,0,0.2])
\end{verbatim}
For more practical problems, external mesh generating software can be used to create a mesh file that is then imported into FMshop. Currently the GMSH formats(.msh) are supported.

Once the mesh is in place, entities such as variables, test functions and coefficient functions are created along with their boundary and initial conditions. In the following example note that functions are input as string expressions of coordinates x, y, z, t, and other entities. We consider a coefficient as a known quantity depending only on coordinates.
\begin{verbatim}
  u = variable("u", VECTOR)
  testSymbol("v", VECTOR)
  coefficient("k", "sin(pi*x)*(t/1+t)")
  boundary(u,1,NEUMANN, [0,0,"1-(u[2]-u[1])"])
  initial(u, [0, 0, "x*y-sin(pi*z)"])
\end{verbatim}

The next step is to specify the differential equations. When using FE, this is done by writing the weak form of the equation in residual form. We have tried to keep the syntax in a form that closely resembles the mathematics. Note that integration over the volume is implied, and surface integrals can be specified by placing those terms in \texttt{surf(...)}.\\
\\
\begin{tabular}{r|l}
Original PDE & $\nabla \cdot (a \nabla u) - f = 0$\\
Weak form & $-(a\nabla u , \nabla v) - (f,v) = 0$\\
FMshop input & \texttt{-a*dot(grad(u),grad(v)) - f*v}
\end{tabular}
\\

When using FV, it is assumed that the equations are in a conservation form. This restriction will be removed in a future release to allow more flexibility. The source and flux terms are given as input, and the time derivative of the variables is implied as shown in the following.\\
\\
\begin{tabular}{r|l}
PDE & $\int_V \frac{du}{dt}dx = \int_V g(u,x)dx - \int_{\partial V} \textbf{f}(u,x)\cdot \textbf{n}ds$\\
Source& \texttt{g(u,x)}\\
Flux & \texttt{f(u,x)}
\end{tabular}
\\

A useful feature for writing out these expressions is the ability to define custom operators. In addition to the standard operators such as \texttt{dot} and \texttt{grad} above, a user can define new operators that act on entities such as variables. One could build, for example, a set of differential geometry operators to use for general relativity, and import them into FMshop to more easily write out a weak form expression.

\subsection{Symbolic Representation}
After entering the problem, it is transformed into an intermediate symbolic representation. The entity symbols are replaced with arrays of corresponding tensor components and the operators discussed previously are applied to create a more verbose set of symbolic expressions. These expressions go through processing stages to separate known and unknown terms, simplify them, identify time dependent terms, etc. The resulting symbolic terms are in the form of computational graphs, based on Julia Expr trees, containing symbolic entity objects. These graphs will then be passed to the code generation utilities.

A simple example using the weak form input from above in 2D will look like this.\\

\begin{tabular}{c}
\texttt{-a*dot(grad(u), grad(v)) - f*v}\\
$\downarrow$\\
\texttt{-[\_a\_1]*dot\_op(grad\_op([\_u\_1]),}\\ \texttt{grad\_op([\_v\_1]))-[\_f\_1]*[\_v\_1]}\\
$\downarrow$\\
\texttt{[-(\_a\_1*D\_1\_\_u\_1*D\_1\_\_v\_1+}\\ \texttt{\_a\_1*D\_2\_\_u\_1*D\_2\_\_v\_1)]+[-\_f\_1*\_v\_1]}\\
$\downarrow$\\
\begin{tabular}{ll}
bilinear: & \texttt{[-\_a\_1*D\_1\_\_u\_1*D\_1\_\_v\_1+}\\ & \texttt{-\_a\_1*D\_2\_\_u\_1*D\_2\_\_v\_1]}\\
linear: & \texttt{[-\_f\_1 * \_v\_1]}\\
entities: & \texttt{D\_1\_\_u\_1} = $\frac{d}{dx}u_1$\\
 & \texttt{D\_2\_\_u\_1} = $\frac{d}{dy}u_1$\\
 & etc.
\end{tabular}
\end{tabular}

\subsection{Code Generation}
The code generation step is where the process diverges. One path is to generate Julia code to be used internally by FMshop's included solvers. The code is placed in functions that are generated and compiled on the fly, but there are also options to export and import the functions for inspection or modification.

The alternative is generating code for external software targets. In this case a target-specific module must be provided. The module must perform two main tasks. One is to turn the computational graphs from the symbolic representation into numerical code. The form that this code will take will look very different depending on the target, but mathematically it should be the same. The second task is to generate all of the needed code and make files to create a full program. This will draw from all of the configuration, mesh and problem details.

A number of these target modules are under current development. The ones that are available now, Matlab and Dendro FE targets, are continually being upgraded to add functionality and improve performance.

\subsection{Modifying Generated Code}
Advanced users may wish to inspect the generated code and make modifications by hand. For this purpose the code can be exported and imported before either running the calculation internally or generating the full code package for external targets. Use one of the following commands after entering the equations, but before solving.
\begin{verbatim}
  exportCode("code_file_name")
  importCode("code_file_name")
\end{verbatim}

As an example, if one wished to use a specific parallelization strategy or integrate a library that is not available directly through FMshop, this would allow such modifications. TODO: more details, example

\section{Performance Opportunities}
Since one of the goals of FMshop is to take advantage of the capabilities of good libraries, there are numerous parallelization, adaptivity, and structuring opportunities to achive high performance. A great example of this is the Dendro target which offers distributed memory parallelism through MPI, adaptive mesh refinement, and other performance enhancing features. It is the task of the code generation target module to take advantage of these features, and the task of FMshop to provide an interface to the user for customizing them.

\subsection{Parallelism Within FMshop}
The performance focus is not limited to external tools. There are features built into FMshop as well. The obvious place to look first is the elemental loop in which the linear system is assembled or matrix vector products are performed in the case of assembly-free methods. Julia provides very simple multithreading capability that can be applied to these elemental loops and provides basic shared memory parallelism. There is also a multiprocessing package as part of the Julia standard library to offer distributed memory parallel capability, though it requires a little more care to fully take advantage of and support for this is still being developed within FMshop.

GPU support has also recently become well supported within Julia\cite{juliagpu}, primarily through CUDA. We are exploring the possibility of adding GPU options for internal FMshop targets as well.

\subsection{Cache Simulation}
There are other ways to improve performance within FMshop besides the major parallelism techniques. The organization of data structures and the elemental loop ordering can be arranged to optimize cache use. To aid with this development and provide a means for automated tuning based on architecture, FMshop employs a cache simulator. Pycachesim\cite{pycachesim} was chosen because it is light-weight and although it was developed for use in Python, the backend is written in C. The C library behind pycachesim is utilized directly by FMshop.

It would not be practical to run the cache simulator in the background during a real computation, and when there are a large number of elements it may not be practical to simulate the full system. Instead the cache simulator is essentially another target for code generation. Rather than performing the mathematical computation, the approximate sequence of memory accesses is fed into the simlator. At the end of the computation the cache statistics are recorded and analyzed. This presents FMshop with a tool for tuning and measuring the effectiveness of changes in data structures and loop arrangements.

To demonstrate this, a number of data organization options are available. Beginning with a mesh from the built-in mesh generation utility, which initially provides a two-layer lexicographic ordering, meaning nodes are ordered lexicographically within each element and elements are also ordered lexicographically. In order to improve spatial locality, the elements are rearranged either into a space-filling curve, such as a Hilbert or Morton curve, or into tiles. A random ordering was also included to demonstrate a very poor arrangement. A comparison of cache simulation results for these various configurations shows a clear difference in the number of cache misses. Although the differences are not very large, this represents an inexpensive way to get some improvement.
\\

TODO: add plot
\\

\section{Demonstrations}
Some example applications are outlined here. The focus is more toward the DSL aspect and the types of problems that this tool is designed for, rather than performance benchmarks. Since external code generation relys on the parallel capabilities of the target framework, we refer the reader to the demonstrated performance measurements of those. For example, the Dendro framework has shown competetive scalability for large scale simulations\cite{dendro}\cite{dendro2}. For FMshop's internal performance, parallelization strategies are currently under development, and competetive benchmarks have not yet been established.

\subsection{TODO: example problem}
An example showing ...

\subsection{Complex Systems of PDEs}
A key principle of this DSL is to simplify the translation from mathematical expressions to code. Complex systems of equations present a challenge in this respect due to the need to manage a large number of components and interactions. A good example of this is the phonon Boltzman transport equation. We refer the reader to \cite{bte2} and \cite{bte3} for the physical background and derivation of the equations. There are a number of different approaches to solving these equations. A deterministic approach that has lent itself well to parallelization is outlined in \cite{bte}, and we will adopt that formulation here. The resulting conservation-type FV equation in terms of the phonon intensity $I_{i,j}$ corresponding to directional vector $\textbf{s}_{i}$ and frequency band $\omega_{j}$ will be Eq.\ref{eq-bte}.
\begin{equation}
\label{eq-bte}
\frac{\partial \bar{I}_{i,j}}{\partial t} = \frac{1}{\tau}(\bar{I}_{0,j} - \bar{I}_{i,j}) - \frac{|v_{g}|}{V} \int_{S} I_{i,j} \textbf{s}_{i}\cdot \textbf{n} dA
\end{equation}
Where $v_{g}$, $\tau$, $I_0$ and $V$ are the group velocity, scattering time scale, equilibrium intensity, and cell volume. Integration is done over each element, or cell, and $\bar{I}$ is the cell average. The different directions are coupled through the boundary conditions via reflection as in Eq.\ref{eq-bte-bdry} with $\textbf{s}_{s}$ being the direction of specular reflection and $\alpha$ the degree of specularity.
\begin{equation}
\label{eq-bte-bdry}
I(\textbf{s}_{i}) = \alpha I(\textbf{s}_{s}) + \frac{1-\alpha}{\pi}\int_{\textbf{s}_{j}\cdot\textbf{n}<0} I(\textbf{s}_{j}) \textbf{s}_{j}\cdot\textbf{n} d\Omega_{j}
\end{equation}

The coupling between frequency bands comes from integrating over all intensities to get the equilibrium values and is done once per time step. A useful discretization may include tens of frequency bands and hundreds of directions\cite{bte}, which results in thousands of equations that are only coupled through their boundary conditions.

To reiterate, successful parallel implementations exist for this problem, but they represent a substantial programming effort resulting in a product that would be challenging to maintain. We argue that by using FMshop this problem will be more accessible and adaptable.

For representing the quantities $I_{i,j}$ and $\textbf{s}_{i}$, where $i$ is the directional discretization and $j$ is for the frequency band, an indexed type variable will be created. As the name suggests, this is an entity that uses a single symbol, but represents an arbitrary number of components that are linked to an indexing object. This serves two purposes. First, the input expressions can then be written in terms of \texttt{I[direction, band]} and \texttt{S[direction]}, where \texttt{direction} and \texttt{band} refer to indexing objects that are involved in the assembly loops. This convenient syntax extends to other expressions such as the boundary conditions, which in this case couple all of the directions. Second, the elemental loops and loops over these indices can be transformed and different parallelization strategies can easily be applied.

The following commands illustrate how this problem could be written in the DSL. For brevity the commands defining constants and the directional vector array have been omitted. The last command defines the loop structure with bands being the outermost loop and elements being innermost. This arrangement naturally allows working on the nearly independent bands in parallel.
\begin{verbatim}
  dir  = index("dir", range=[1,400])
  band = index("band", range=[1,40])
  I = variable("I", VAR_ARRAY, CELL,
               index = [dir, band])
  s = coefficient("s", s_vector_array,
               VAR_ARRAY, CELL, index = dir)

  source(I, "(Ie - I[dir,band]) / tau")
  flux(I, "vg*I[dir,band]*dot(s[dir],normal)")

  assemblyLoops(I, [band, dir, "elements"])
\end{verbatim}

\section{Conclusion and Ongoing Development}
FMshop is a new and rapidly evolving toolkit that enables domain scientists to swiftly build numerical PDE solvers while taking advantage of sophisticated parallel software frameworks. It also provides an extensible resource that is designed to adapt to changes in technology, computing environment, and needs. It also adds a new set of capabilities to the Julia software ecosystem that will hopefully be adopted and expanded as new code generation targets and mathematical techniques are developed.

The list of goals and desired features is considerable, but some important ones that are either under current development or are planned for the near future include:\\
\begin{itemize}
\item Support for distributed parallelism and improved use of multithreading within FMshop.
\item GPU utilization for suitable applications.
\item Addition of new code generation targets to provide optimal solutions for a broader range of applications and user preferances.
\item Direct use of certain external targets from within FMshop to streamline the process.
\item Addition of profiling utilities to finely tune internal solution strategies.
\item Interaction with more of the great Julia libraries available to take full advantage of this scintific computing ecosystem.
\item Development of practical experiments in collaboration with domain scintists to test effectiveness and usability.
\end{itemize}

Of these items, the immediate effort will be focused on improving the internal parallel features and expanding the external generation options to make FMshop competetive in terms of performance.

The code for FMshop, along with a set of detailed examples and usage information, can be found at https://github.com/paralab/femshop

\begin{thebibliography}{99}

\bibitem{dendro}
Milinda Fernando, David Neilsen, Hari Sundar, ``A scalable framework for Adaptive Computational General Relativity on Heterogeneous Clusters”, {\em ACM International Conference on Supercomputing, ICS’19}, 2019
\\
\bibitem{yateto}
Carsten Uphoff, Michael Bader, ``Yet Another Tensor Toolbox for Discontinuous Galerkin Methods and Other Applications", {\em ACM Trans. Math. Softw.}, vol. 46, no. 4, art. 34, 2020
\\
\bibitem{mcrae}
A. T. T. McRae, G.-T. Bercea, L. Mitchell, D. A. Ham, C. J. Cotter, ``Automated Generation and Symbolic Manipulation of Tensor Product Finite Elements", {\em SIAM J. Sci. Comput.}, vol. 38, no. 5, pp.~25--47, 2016
\\
\bibitem{homolya}
Miklós Homolya, Robert C. Kirby, David A. Ham, ``Exposing and exploiting structure: optimal code generation for high-order finite element methods", (preprint) arXiv:1711.02473, 2017
\\
\bibitem{seisol}
Ravil Dorozhinskii, Michael Bader, ``SeisSol on Distributed Multi-GPU Systems: CUDA Code Generation for the Modal Discontinuous Galerkin Method", {\em HPC Asia 2021: The International Conference on High Performance Computing in Asia-Pacific Region}, January 2021 pp.~69--82
\\
\bibitem{kempf}
Dominic Kempf, René Heß, Steffen Müthing, Peter Bastian, ``Automatic Code Generation for High-performance Discontinuous Galerkin Methods on Modern Architectures", {\em ACM Trans. Math. Softw.}, vol. 47, no. 1, art. 6, 2020
\\
\bibitem{pietro}
Daniele A. Di Pietro, Jean-Marc Gratien, Florian Häberlein, Anthony Michel, Christophe Prud'homme, ``Basic concepts to design a DSL for parallel finite volume applications: extended abstract", {\em POOSC '09: Proceedings of the 8th workshop on Parallel/High-Performance Object-Oriented Scientific Computing}, no.3, 2009
\\
\bibitem{macia}
Sandra Macià, Pedro J. Martínez-Ferrer, Sergi Mateo, Vicenç Beltran, Eduard Ayguadé, ``Assembling a High-Productivity DSL for Computational Fluid Dynamics", {\em PASC '19: Proceedings of the Platform for Advanced Scientific Computing Conference}, no. 11, 2019
\\
\bibitem{dendro2}
Milinda Fernando, David Neilsen, Hyun Lim, Eric Hirschmann, Hari Sundar, ``Massively Parallel Simulations of Binary Black Hole Intermediate-Mass-Ratio Inspirals” {\em SIAM Journal on Scientific Computing}, 2019
\\
\bibitem{bte}
Syed Ashraf Ali, Gautham Kollu, Sandip Mazumder, P. Sadayappan, Arpit Mittal,
``Large-scale parallel computation of the phonon Boltzmann Transport Equation",
{\em International Journal of Thermal Sciences}, vol. 86, pp.~341--351, 2014
\\
\bibitem{bte2}
Chang-Lin Tien, Arunava Majumdar, Frank M. Gerner (Editors), {\em Microscale Energy Transport}, Taylor
and Francis, 1998
\\
\bibitem{bte3}
Arunava Majumdar, ``Microscale Heat Conduction in Dialectric Thin Films", {\em Journal of Heat Transfer}, vol. 115, no. 1, pp.~7--16, 1993
\\
\bibitem{juliagpu}
Tim Besard, Christophe Foket, Bjorn De Sutter. ``Effective extensible programming: Unleashing Julia on GPUs." {\em IEEE Transactions on Parallel and Distributed Systems}, 2018
\\
\bibitem{pycachesim}
Julian Hammer, pycachesim: Python Cache Hierarchy Simulator, URL https://github.com/RRZE-HPC/pycachesim
\\
\bibitem{nektar}
Nektar++ URL https://www.nektar.info
\\
\bibitem{dealii}
%deal.II URL https://www.dealii.org
W. Bangerth, T. Heister, L. Heltai, G. Kanschat, M. Kronbichler, M. Maier, B. Tur-
cksin, and T. D. Young, ``The deal. II library, version 8.2", {\em Arch. Numer. Software}, vol. 3, 2015
\\
\bibitem{fenics}
%FEniCS URL https://fenicsproject.org
Martin S. Aln{\ae}s, Jan Blechta, Johan Hake, August Johansson, Benjamin Kehlet, Anders Logg, Chris Richardson, Johannes Ring, Marie E. Rognes, Garth N. Wells, ``The FEniCS Project Version 1.5", {\em Archive of Numerical Software}, vol. 3, no. 100, pp.~9--23, 2015
\\
\bibitem{firedrake}
%Firedrake URL https://www.firedrakeproject.org
Florian Rathgeber, David A. Ham, Lawrence Mitchell, Michael Lange, Fabio Luporini, Andrew T. T. Mcrae, Gheorghe-Teodor Bercea, Graham R. Markall, and Paul H. J. Kelly. ``Firedrake: automating the finite element method by composing abstractions". {\em ACM Trans. Math. Softw.}, 43(3):24:1–24:27, 2016
\\
\bibitem{openfoam}
OpenFOAM URL https://www.openfoam.com
\\
\bibitem{devito}
%Devito URL https://www.devitoproject.org
M. Louboutin, M. Lange, F. Luporini, N. Kukreja, P.A. Witte, F.J. Herrmann, P. Velesko,  G.J. Gorman, ``Devito (v3.1.0): an embedded domain-specific language for finite differences and geophysical exploration", {\em  Geoscientific Model Development}, vol. 12, no. 3, pp.~1165--1187, 2019
\\
\bibitem{diffeqjl}
DifferentialEquations.jl URL\\
https://diffeq.sciml.ai/dev/index.html
\\
\bibitem{juliabench}
Julia benchmarks URL\\
https://julialang.org/benchmarks
\\
\end{thebibliography}
\end{document}


% vim: ft=tex iskeyword=@,48-57,_,-,192-255,\: dictionary=bibkeys.lst,labels.lst:
\chapter{Optimization for a Perfect Entangler}
\label{chap:pe}

The implementation of a universal quantum computer requires at least one
entangling two-qubit gate~\cite{DeutschPRSA1995, ZhangPRL2003}, together with
arbitrary single-qubit gates. However, as
discussed in chapter~\ref{chap:quantum}, this need not be one of the
``standard'' gates, such as CNOT.
The construct of the Weyl chamber, see section~\ref{sec:C_LI} in
chapter~\ref{chap:quantum}, shows that for any two-qubit gate, there is an
infinite number of equivalent quantum gates that differ only by additional local
operations. For example the CPHASE gate is equivalent in this sense to CNOT, see
Eq.~\eqref{eq:CNOT_CPHASE_equivalent}.

In an optimization context, this has far-reaching consequences. Not every
Hamiltonian allows for the implementation of arbitrary two-qubit gates. For
example, the trapped Rydberg atoms discussed in chapter~\ref{chap:robust} can
only implement diagonal gates, with a fixed global phase. The optimization
target must be chosen to properly reflect this. Reachability is not the only
consideration, however. Equally important is the question whether one two-qubit
gate may be ``easier'' to implement than some other (possibly equivalent)
two-qubit gate, in terms of the minimum required pulse duration, the required
pulse energy, or the difficulty of keeping the population in the logical
subspace in a multi-level system. For example, a Hamiltonian containing among
other terms an exchange interaction $(\SigmaPlus\SigmaMinus
+ \SigmaMinus\SigmaPlus)$ will likely find it easier to implement the
$\sqrtISWAP$ gate than the locally equivalent M-gate. The latter is
generated by a term $(3\SigmaX\SigmaX + \SigmaY\SigmaY)$,
cf.~Table~\ref{tab:appendixGates}, that would be part of the Lie algebra only
indirectly. Likewise, the implementation of iSWAP would likely require more time
than $\sqrtISWAP$, even though both gates generate the same amount of
entanglement.

Using an optimization functional such as Eq.~\eqref{eq:JTsm} or
Eq.~\eqref{eq:JTre} that targets a specific quantum gate is therefore
unnecessarily restrictive. A far better approach is to formulate a more general
optimization functional, and allow optimal control to select the specific gate
that most effectively fits the objective. The geometric theory of local
invariants has been combined with optimal control~\cite{ReichDipl10,
MullerPRA11}, using a functional that minimizes the Cartesian distance to
a target point in the Weyl chamber $(c_1, c_2, c_3)$, see
Eq.~\eqref{eq:cartan_decomposition}, respectively a set of local invariants
$(g_1, g_2, g_3)$, see Eq.~\eqref{eq:li_from_c}.

This chapter, adapted from~\cite{PE1, PE2}, addresses the realization of perfect
entanglers, i.e., quantum gates that are capable of transforming some separable
states into maximally entangled states. This recognizes the fundamental role of
entanglement, as the resource that two-qubit gates contribute to the gate model
of universal quantum computing. Moreover, since nearly 85\% of gates in $\SU(4)$
are perfect entanglers~\cite{MuszPRA2013, WattsE2013}, optimizing for an
arbitrary perfect entangler gives a much broader target.

Extending the idea of optimization in the Weyl chamber, a functional for an
arbitrary perfect entangler has been formulated~\cite{PE1}. As shown in
Fig.~\ref{fig:weylchamber}, the perfect entanglers form a polyhedron bounded by
the planes
\begin{equation}
  \label{eq:pe_planes}
  c_1 + c_2 = \frac{\pi}{2}, \quad
  c_1 - c_2 = \frac{\pi}{2}, \quad
  c_2 + c_3 = \frac{\pi}{2}.
\end{equation}
Thus, an optimization could simply minimize the distance to the nearest wall of
that polyhedron.

Both the underlying local-invariants functional and the newly developed perfect
entanglers functionals are reviewed in section~\ref{sec:pe_functional}. An
optimization in the Weyl chamber, either towards a specific point or towards
a general perfect entangler is most useful for quantum computing implementations
that can generate a large number of gates, such as the superconducting circuits
presented in chapter~\ref{chap:transmon}. In order to obtain some insight in
the structure of the dynamics generated by a typical Hamiltonian,
section~\ref{sec:pe_controllability} shows the controllability for an effective
description of two qubits, truncated to two levels.
Section~\ref{sec:pe_transmon_oct} then applies the method to a specific example
of transmon qubits, illustrating the power of an optimization towards a perfect
entangler in comparison to the optimization towards a local equivalence class.

While the perfect entanglers functional is tested on a Hilbert space
description, the ultimate motivation for using a more general functional is to
counter decoherence. First, dissipation might affect the processes implementing
different gates differently. In such a situation, a direct optimization might
result in very different fidelities. Optimizing for a general perfect entanlger,
on the other hand, would automatically yield the gate that is implementable with
highest fidelity under dissipation.
In a situation where dissipation cannot be circumvented, the only option is to
perform the gate on a faster time scale than that of the decoherence. The
equation of the quantum speed limit then becomes crucial. Since the speed limit
may differ for various gates, the perfect entanglers functional allows to
identify the optimal gate in such cases. Thus, even without going to the
formalism of Liouville space, the possibility to implement \emph{fast} gates
provides an answer to the problem of implementing gates in an open quantum
system.
The message of this chapter therefore connects to that of
chapter~\ref{chap:robust}, in that the key to finding robust quantum gates with
optimal control is to encode all relevant requirements, and \emph{only} the
relevant requirements in the optimization functional.

\section{The Perfect Entanglers Functional}
\label{sec:pe_functional}

\subsection{Formulation in $c$-space}
\label{subsec:pe_in_c_space}

The Cartan decomposition
\index{Cartan decomposition}%
\begin{equation}
  \Op{U} = \Op{k}_1 \Op{A}_U \Op{k}_2\,, \quad
  \Op{A}_u = \exp\left[\frac{\ii}{2}
                       \sum_{j=1}^{3} c_j \Op{\sigma}_j \Op{\sigma}_j \right]\,,
\end{equation}
cf.~Eq.~\eqref{eq:cartan_decomposition}, allows to determine the ``true''
two-qubit part $\Op{A}_U$ for an arbitrary two-qubit gate $\Op{U}$. The
``local'' components $\Op{k}_1, \Op{k}_2$ can be written in terms of
single-qubit gates
\begin{equation}
  \Op{k}_i = \Op{U}_i^{(1)} \otimes \unity + \unity \otimes \Op{U}_i^{(2)}\,,
  \quad
  \Op{U}_i^{(1,2)} \in \SU(2)\,,
\end{equation}
whereas $\Op{A}_U$ allows for no such decomposition. The entanglement power of
$\Op{U}$ is provided only by the non-local $\Op{A}_U$.

Therefore, the optimization for a general perfect entangler starts from the
prerequisite of eliminating all non-local operations from the figure of merit.
Replacing the target gate $\Op{O}$ and achieved gate $\Op{U}$ in the
functional~\eqref{eq:JTre} by only their non-local components $\Op{A}_O$,
$\Op{A}_U$ yields an optimization functional $\errLec = 1 - F_{\lec}$ with
\begin{equation}
  F_{\lec} = \frac{1}{4} \Re\left(
                \tr\left[ \Op{A}_{O}^\dagger \Op{A}_U \right]
             \right)\,.
 \label{eq:Flec_A}
\end{equation}
The functional $\errLec$ takes its minimum value of zero if and only if $\Op{O}$
and $\Op{U}$ have the same non-local component, as determined by the Weyl
chamber coordinates $(c_1, c_2, c_3)$. Rewriting Eq.~\eqref{eq:Flec_A}
explicitly in these coordinates yields~\cite{PE1}
\begin{equation}
   F_{\lec}
   = \cos\frac{\Delta c_1}{2}
     \cos\frac{\Delta c_2}{2}
     \cos\frac{\Delta c_3}{2}\,, \qquad
   \Delta c_{i} \equiv c_{i,O} - c_{i,U}\,.
  \label{eq:Flec}
\end{equation}
\index{local equivalence class functional!in $c$-space}%
Thus, the optimization can be interpreted as minimizing the geometric distance
$(\Delta c_1, \Delta c_2, \Delta c_3)$ to a target point in the Weyl chamber.

This may now be extended further to obtain a functional that optimizes for
a general perfect entangler, by minimizing the distance not to a specific point
in the Weyl chamber, but to the polyhedron of perfect entanglers.
For any gate $\Op{U}$, we identify the closest of the planes bounding the
perfect entanglers, Eq.~\eqref{eq:pe_planes}. Then, the distance of
$(c_{1,U}, c_{2,U}, c_{3,U})$ and its projection onto that plane is minimized,
resulting in~\cite{PE1}
\begin{equation}
  F_{\PE}(\Op{U})=\begin{cases}
    \cos^2\frac{c_{U,1}+c_{U,2}-\frac{\pi}{2}}{4} &  c_1+c_2\leq\frac{\pi}{2}\\
    \cos^2\frac{c_{U,2}+c_{U,3}-\frac{\pi}{2}}{4} &  c_2+c_3\geq\frac{\pi}{2}\\
    \cos^2\frac{c_{U,1}-c_{U,2}-\frac{\pi}{2}}{4} &  c_1-c_2\geq\frac{\pi}{2}\\
    1                                             &  \text{otherwise.}
  \end{cases}
  \label{eq:FPE}
\end{equation}
Both $F_{\lec}$ and $F_{\PE}$ take values in $[0,1]$ and can thus be interpreted
as fidelities.
\index{perfect entangler functional!in $c$-space}

Generally, the logical two-qubit subspace is embedded in a larger Hilbert space,
such that while the dynamics in the total Hilbert space are unitary, the
dynamics in the subspace may not be. In this case,
a closest unitary $\Op{U}$ can be derived from the non-unitary (projected) gate
$\tildeOp{U}$: If $\tildeOp{U}$ has the singular value decomposition
\index{singular value decomposition}%
\begin{equation}
  \tildeOp{U} = \Op{V} \, \Op{\Sigma} \, \Op{W}^{\dagger}\,,
\end{equation}
then the closest unitary is
\begin{equation}
  \Op{U} = \arg \min_{u} \Norm{\tildeOp{U} - u}
         = \Op{V} \, \Op{W}^\dagger\,.
\end{equation}
\index{closest unitary}%
The distance between $\Op{U}$ and $\tildeOp{U}$ is a measure of unitarity.
\index{unitarity measure}%
The total local-equivalence-class and perfect-entangler fidelities then become
\begin{align}
  F_{\lec}(\tildeOp{U})
  &
  = F_{\lec}(\Op{U}) - \Norm{\tildeOp{U} - \Op{U}}\,,
  \label{eq:FUtilde-lec}
  \\
  F_{\PE}(\tildeOp{U})
  &
  = F_{\PE}(\Op{U}) -\Norm{\tildeOp{U} - \Op{U}}\,.
  \label{eq:FUtilde-pE}
\end{align}
The optimization goal is to find that $\tildeOp{U} = \Op{U}$ which minimizes
$1-F_{\lec}$ or $1-F_{\PE}$.

\subsection{Formulation in $g$-space}
\label{subsec:pe_in_g_space}

The formulation of the local equivalence class and perfect entanglers
functionals in terms of the Weyl chamber coordinates $(c_1, c_2, c_3)$,
Eq.~\eqref{eq:FUtilde-lec} and Eq.~\eqref{eq:FUtilde-pE}, have the disadvantage
that there is no way to evaluate them \emph{analytically} for a given gate
$\Op{U}$. This is because calculation of the Weyl coordinates themselves relies
on numerical diagonalization of $\Op{U}$, as discussed in
chapter~\ref{chap:quantum}. Therefore, use of the functionals $F_{\lec}$ and
$F_{\PE}$ is restricted to optimization methods that are not gradient-based, such
as the CRAB algorithm presented in section~\ref{subsec:simplex} of
chapter~\ref{chap:numerics}. An application of the functional using this
algorithm can be found in Ref.~\cite{PE2}.

For the other optimal control approaches discussed in
chapter~\ref{chap:numerics}, the gradient of the optimization functional needs
to be evaluated. We must therefore use an equivalent
functional, based not on the Weyl space coordinates $(c_1, c_2, c_3)$, but on
the local invariants $(g_1, g_2, g_3)$, see
Eq.~\eqref{eq:local_invariants}, which depend analytically on $\Op{U}$ and
therefore can be differentiated either with respect to the field (GRAPE) or with
respect to the states (Krotov).

For the local invariants, an appropriate functional can be postulated
as~\cite{ReichDipl10, MullerPRA11}
\begin{equation}
  J_{\LI}(\Op{U}) = (\Delta g_1)^2 + (\Delta g_2)^2 + (\Delta g_3)^2\,,
  \label{eq:J_T_LI_g}
\end{equation}
\index{local equivalence class functional!in $g$-space}%
where $\Delta g_i$ is the Euclidean distance between local invariant $g_i$ of
the obtained unitary $\Op{U}$ and the optimal gate $\Op{O}$.
Eq.~\eqref{eq:J_T_LI_g} takes its minimum value zero if and only if the gates
$\Op{U}$ and $\Op{O}$ are identical up to local transformations. This makes it
slightly more general than Eq.~\eqref{eq:Flec}, as there are some gates
that are locally equivalent but have different Weyl-chamber coordinates, such as
the points $Q$ and $M$ in Fig.~\ref{fig:weylchamber}.

\begin{figure}[tb]
  \centering
  \includegraphics{fpe_topology}
  \caption{%
  Value for the perfect-entanglers $J_{\PE}$ as defined in
  Eq.~\eqref{eq:J_T_PE_g} for sampling points in the Weyl chamber. On the left,
  values for points in the regions $W_0$, $W_0^*$, and $W_1$ (outside of the
  perfect entanglers polyhedron). The values are in the range
  $-2 \le J_{\PE} \le 2$.
  On the right, values $-0.07 \lesssim J_{\PE} \lesssim 0.07$ inside the
  perfect-entanglers polyhedron. The functional takes slightly positive values
  in the center top region, and slightly negative values near the outer bottom
  regions, behind both the $M$ point and the $Q$ point (not visible).
  }
  \label{fig:fpe_topology}
\end{figure}
In order to derive a functional based on the local invariants $(g_1, g_2, g_3)$
for an arbitrary perfect entangler, Eq.~\eqref{eq:li_from_c} is used to rewrite
Eq.~\eqref{eq:FPE}, resulting in~\cite{PE1}
\begin{equation}
  J_{\PE}(\Op{U}) =  g_3 \sqrt{g_1^{2} + g_2^{2}} - g_1\,.
  \label{eq:J_T_PE_g}
\end{equation}
\index{perfect entangler functional!in $g$-space}%
Eq.~\eqref{eq:J_T_PE_g} takes the value zero at the boundary to the perfect
entanglers. The value of $J_{\PE}$ for gates throughout the Weyl chamber is
shown in Fig.~\ref{fig:fpe_topology}. It is important to note that $J_{\PE}$ can
also take negative values. Typically, an optimization will start from a guess
pulse that generates little entanglement, locally equivalent to a gate close to
the identity. In the Weyl chamber, these are defined as the regions $W_0$
(delimited by $O$, $L$, $Q$, and $P$) and $W_0^*$ (delimited by $A_1$, $L$, $M$,
and $N$). In these regions, the functional goes smoothly from its maximum value
of 2 to 0 on the boundary of the perfect entanglers. The sign of the functional
is reversed if the optimization were to start from near the SWAP gate at $A_3$.
This region is denoted as $W_2$, delimited by $A_3, A_2, P, N$. Typically, this
is not a problem. Thinking of entanglement as a resource that is generated by an
interaction acting over a certain period of time, continuing to apply the same
interaction after the entanglement has reached its maximum value of 1 will now
disentangle the entangled states. For example, a
$$\SigmaX\SigmaX + \SigmaY\SigmaY + \SigmaZ\SigmaZ$$ interaction will generate
gates along the $O-P-A_3$ line (see appendix~\ref{AppendixGates}), building up
entanglement as it goes towards the $\sqrt{\text{SWAP}}$ perfect entangler and
then removing it again until the non-entangling SWAP gate is reached. Thus,
guess pulses would be expected to start in $W_2$ only for extremely simple
systems; when this occurs, the sign of the functional must be reversed.

Inside the polyhedron of perfect entanglers, $J_{\PE}$ can also take non-zero
values; it is zero on the planes $L$--$N$--$A_2$ and
$L$--$P$--$A_2$, as well as on all boundaries of the polyhedron.
Thus, if an optimization is allowed to continue after passing through the
perfect-entanglers boundary, it will be pushed towards the center of the
sub-polyhedron delimited by the point $L$, $M$, $A_2$, and $N$ on the right; or
$L$, $Q$, $A_2$ and $P$ on the left.

Like before, we must take into account non-unitarity due to projection onto the
logical subspace. Just as for the value of the functional, $F_{\lec}$ and
$F_{\PE}$, the expression $\Norm{\tildeOp{U} - \Op{U}}$ used to evaluate
unitarity in section~\ref{subsec:pe_in_c_space} cannot
easily be differentiated. As an alternative, we minimize the loss of population
from the logical subspace,
\begin{equation}
p_{\loss} = \frac{1}{4} \Tr\left[ \tildeOp{U}^\dagger \tildeOp{U} \right]
\end{equation}
as an alternative measure of unitarity.
\index{unitarity measure}

In total, the optimization functional for the local invariants and perfect
entanglers then reads
% paragraph ended to improve page break

\begin{align}
  J_{\LI}(\tildeOp{U})
  &
  = w J_{\LI}(\Op{U}) + (w-1) \left(
       1 - \frac{1}{4} \Tr\left[ \tildeOp{U}^\dagger \tildeOp{U} \right]
    \right)\,,
  \label{eq:J_LI_tilde}
  \\
  J_{\PE}(\tildeOp{U})
  &
  = w J_{\PE}(\Op{U}) + (w-1) \left(
      1 - \frac{1}{4} \Tr\left[ \tildeOp{U}^\dagger \tildeOp{U} \right]
   \right)\,.
\label{eq:J_PE_tilde}
\end{align}
In order to weight the relative importance of the Weyl-chamber optimization and
the unitarity, the factor $w \in [0,1]$ is used. This factor can be adaptively
changed during the optimization in order to improve convergence.

\section{Controllability of Superconducting Qubits}
\label{sec:pe_controllability}

Optimization towards an arbitrary perfect entangler is most meaningful
if the system dynamics allows the polyhedron of perfect entanglers to
be approached from more than one direction or, more generally, for
optimization paths in the Weyl chamber that explore more than one
dimension.  We therefore investigate the corresponding requirements on
a generic two-qubit Hamiltonian,
\begin{equation}
\begin{split}
  \Op{H}[u_{1}(t), u_{2}(t)]
  &
  =   \sum_{\alpha=1,2} \frac{\omega_{\alpha}}{2} \SigmaZ^{(\alpha)}
      + u_1(t) \left( \SigmaX^{(1)} + \lambda \SigmaX^{(2)}
      \right)
  + \\ & \quad
     + u_2(t) \left(\SigmaX^{(1)} \SigmaX^{(2)}
         +\SigmaY^{(1)} \SigmaY^{(2)}\right)\,.
\end{split}
  \label{eq:H_truncated}
\end{equation}
Here, $\Op{\sigma}_{i}^{(\alpha)}$ is the $i$'th Pauli operator
acting on the $\alpha$'th qubit of transition frequency
$\omega_{\alpha}$, $u_1(t)$ the
single-qubit control field, where $\lambda$ describes how strongly $u_1(t)$
couples to the second qubit relative to the first one, and $u_2(t)$ is the
two-qubit interaction control field.

The Hamiltonian in Eq.~\eqref{eq:H_truncated} is of the form typical for an
effective description of superconducting qubits, truncated to two levels.
In principle, truncation of the Hamiltonian can have significant influence on
controllability. For example, a two-level system is fully controllable, whereas
and infinite harmonic oscillator is not. For superconducting qubits, however, we
do not expect controllability to be enhanced by truncation. Therefore, an
analysis still provides valuable insight into how the Hamiltonian acts in the
Weyl chamber.
\index{controllability}

We analyze the solutions to the differential equation
\begin{equation}
  \frac{\partial}{\partial t}\Op{U}(t)
  = -\ii \Op{H}\left[u(t)\right] \Op{U} \left(t\right)\,,
  \quad \Op{U}(0) = \unity\,,
\label{eq:U_dgl}
\end{equation}
for the unitary transformations $\Op{U}$ generated by the
Hamiltonian~\eqref{eq:H_truncated}.  The reachable set of unitary
transformations for a Hamiltonian is given in terms of the
corresponding dynamical Lie algebra, see section~\ref{subsec:controllability} in
chapter~\ref{chap:quantum}. It can be generated by taking
the terms in \eqref{eq:H_truncated} as a basis (neglecting
orthonormalization for simplicity),
$$
  \SigmaZ^{(1)} \,,\,
  \SigmaZ^{(2)} \,,\,
  \SigmaX^{(1)}  + \lambda \SigmaX^{(2)} \,,\,
  \SigmaX^{(1)} \SigmaX^{(2)} + \SigmaY^{(1)} \SigmaY^{(2)}\,,
$$
and constructing the repeated Lie brackets of these operators. This quickly
yields all 15 canonical basis operators of $SU(4)$, consisting of the
single-qubit operators
  $\SigmaX^{(1)}$,
  $\SigmaX^{(2)}$,
  $\SigmaY^{(1)}$,
  $\SigmaY^{(2)}$,
  $\SigmaZ^{(1)}$, and
  $\SigmaZ^{(2)}$,
as well as the entangling operators
  $\SigmaX^{(1)}\SigmaY^{(2)}$,
  $\SigmaY^{(1)}\SigmaX^{(2)}$,
  $\SigmaY^{(1)}\SigmaZ^{(2)}$,
  $\SigmaZ^{(1)}\SigmaY^{(2)}$,
  $\SigmaX^{(1)}\SigmaZ^{(2)}$,
  $\SigmaZ^{(1)}\SigmaX^{(2)}$,
  $\SigmaX^{(1)}\SigmaX^{(2)}$,
  $\SigmaY^{(1)}\SigmaY^{(2)}$, and
  $\SigmaZ^{(1)}\SigmaZ^{(2)}$.
Hence the system is completely controllable, and any point in the Weyl
chamber can be reached.

\begin{figure}[tb]
  \centering
  \includegraphics{controllability}
  \caption{Sampling of reachable points in the Weyl
    chamber, obtained by solving Eq.~\eqref{eq:U_dgl}
    for  the Hamiltonian~\eqref{eq:H_truncated} with $\lambda=1$.
    In panel (a),
    $u_{1}(t)$ is a random pulse $\in [0,1]$, $u_{2}(t) \equiv 10^{-3}$,
    and $\omega_1 = 1.0 \neq \omega_2=1.1$.
    In panel (b), in addition
    $\omega_1 = \omega_2 = 1.0$.
    In panel (c),
    $u_{1}(t)$ and $u_{2}(t)$ are both independently random $\in [0,1]$, and
    $\omega_1 = \omega_2 = 0$.
    In panel (d), in addition $u_{1}(t) \equiv u_{2}(t)$.
    The red line in panel (d) is obtained for $u_1(t) \equiv 0$ and $u_{2}(t)$
    random $\in [0,1]$ ($\omega_1 = \omega_2 = 0$).
  }
  \label{fig:controllability}
\end{figure}
The complete controllability can be verified numerically, by solving
Eq.~\eqref{eq:U_dgl} for a random sequence of pulse values. The gates obtained
within 1000 steps are shown in panel (a) of Fig.~\ref{fig:controllability}, and
demonstrate full controllability, since there are points in all regions of the
Weyl chamber.
Continuing the procedure to infinity would eventually fill the entire chamber.
Neither setting $u_2(t)$ constant nor choosing $\lambda=0$ places any
restrictions on the controllability -- indeed it is sufficient if either
the single qubit terms or the interaction term is controllable.
While the controllability in this example was analyzed for
arbitrary values of the parameters,
the form of the Hamiltonian and the ratio between $\omega_{1,2}$ and $u_2$ fits
the description of superconducting transmon qubits, with qubit energies in the
GHz range and static qubit-qubit-coupling in the MHz range.

Introducing symmetries in the Hamiltonian~\eqref{eq:H_truncated} reduces the
controllability. First, we consider a situation in which
the two qubits operate at the same frequency $\omega_1 = \omega_2$. In this
case, the dynamic Lie algebra consists of only 9 instead of 15
operators. Consequently, not every
two-qubit gate can be implemented. However, the nine operators include
$\SigmaX^{(1)} \SigmaX^{(2)}$,
$\SigmaY^{(1)} \SigmaY^{(2)}$,
$\SigmaZ^{(1)} \SigmaZ^{(2)}$,
which are sufficient to reach every point in the Weyl chamber, cf.\
Eq.~\eqref{eq:cartan_decomposition}. This is illustrated
in panel (b) of Fig.~\ref{fig:controllability}. Despite
the reduced controllability, the Weyl chamber is more evenly filled after the
same 1000 propagation steps as in panel (a). This counterintuitive
finding is due to the lower dimension of the random walk, with no
resources being ``wasted'' on the missing six single-qubit directions.

The set of gates that can be implemented with Hamiltonian~\eqref{eq:H_truncated} is
more severely restricted if both qubits are
completely degenerate, $\omega_1 = \omega_2 = 0$. This is typical for
superconducting charge qubits operated at the  ``charge degeneracy
point''. Without any drift term, the Lie algebra consists of
only four generators,
$\SigmaZ^{(1)} \SigmaY^{(2)} + \SigmaY^{(1)} \SigmaZ^{(2)}$ and
$\SigmaY^{(1)} \SigmaY^{(2)} - \SigmaZ^{(1)} \SigmaZ^{(2)}$ in addition
to the two original terms.
The implications for controllability in the Weyl
chamber are not immediately obvious since three
generators can be sufficient to obtain full Weyl chamber
controllability. The
easiest approach is to perform a numerical analysis, the results of which are
shown in panel (c) of Fig.~\ref{fig:controllability}. Two independent randomized
pulses $u_1(t)$ and $u_2(t)$ were used. The reachable points
lie on a plane, which due to the reflection symmetries
appears as two triangular branches, indicated by the shaded triangles,
$O$--$(\frac{2\pi}{3},\frac{\pi}{3}, \frac{\pi}{3})$--$A_2$ and
$A_1$--$(\frac{\pi}{3},\frac{\pi}{3}, \frac{\pi}{3})$--$A_2$.
Note that almost none of the common two-qubit gates are included in this set.

If only a single pulse is available to drive both the single-qubit and
two-qubit terms, $u_1(t) \equiv u_2(t)$, and the qubits are
degenerate, $\omega_1 = \omega_2 = 0$, there is a single
generator for the dynamics. This situation is shown
in panel (d) of Fig.~\ref{fig:controllability}.
Although there is only a single generator for the dynamics, a
two-dimensional subset of the Weyl chamber can be reached. However,
the subset is no longer the full plane as it is for two independent
pulses, panel (c). Without single-qubit
control, the center of the plane is not longer reachable.
It is important to remember that while a single
generator yields points on a line in the Weyl chamber (not necessarily
a straight one), it can still fill
an arbitrary subset of the Weyl chamber, due to
reflections at the boundaries.
A similar example, restricted to the ground plane of the
Weyl chamber, has been analyzed in Ref.~\cite{ZhangPRA03}.

Lastly, if there is no control over the individual qubits at all,
$u_1(t) \equiv 0$, the only remaining generator is
$\SigmaX^{(1)} \SigmaX^{(2)}+ \SigmaY^{(1)} \SigmaY^{(2)}$.
This corresponds to the straight line
$O$--$A_2$ in the Weyl chamber, shown in red in panel (d) of
Fig.~\ref{fig:controllability}. The line is reflected back onto itself
at the $A_2$ point. Thus, in this case only a truly one-dimensional subset
of reachable gates in the Weyl chamber can be realized.

For a Hamiltonian that allows for  a one-dimensional search-space only,
optimal control calculations with a functional targeting all perfect
entanglers will not yield results better than direct gate
optimization.
In the above example, the perfect entangler that can be generated with the
shortest gate duration is the $\sqrtISWAP$ at the point $Q$.

In contrast, for Hamiltonians allowing for two or three search directions in
the Weyl chamber, especially panels (a) and (b) in
Fig.~\ref{fig:controllability}, the polyhedron of perfect
entanglers may be approached from several different angles.  Optimization with a
functional targeting all perfect entanglers is then non-trivial. In
such a search, the optimized solution will depend on additional constraints
in the functional and the initial guess field.

\section{Optimization of Transmon Qubits}
\label{sec:pe_transmon_oct}

\subsection{Model}

\begin{table}[tb]
  \centering
  \begin{tabular}{llrl}
  \toprule
  left qubit frequency           &  $\omega_1$  & 4.380 &GHz \\
  right qubit frequency          &  $\omega_2$  & 4.614 &GHz \\
  left qubit anharmonicity       &  $\alpha_1$  & -210  &MHz \\
  right qubit anharmonicity      &  $\alpha_2$  & -215  &MHz \\
  effective qubit-qubit coupling &  $J^{\eff}$  & -3.0  &MHz \\
  relative coupling strength     &  $\lambda$   & 1.03  &~\\
  \bottomrule
  \end{tabular}
  \caption{Parameters for the transmon Hamiltonian Eq.~\eqref{eq:PE_H2T}}.
  \label{tab:pe_transmon_parameters}
\end{table}
Having verified that the Hamiltonian in Eq.~\eqref{eq:H_truncated} allows for
full controllability, we now extend the discussion to a more realistic
description of two transmons, i.e., anharmonic multi-level systems whose
interaction is mediated by a cavity. This is described by  a generalized
Jaynes-Cummings Hamiltonian, see chapter~\ref{chap:transmon}.
The energy of each transmon qubit transition given by $\omega_1$, $\omega_2$ for
the first (``left'') and second (``right'') transmon respectively.
Following a Duffing oscillator model, higher levels show an anharmonicity of
$\alpha_1$, respectively $\alpha_2$. Each qubit couples to the cavity with
coupling strength $g_1$, $g_2$.
In the dispersive limit $\Abs{\omega_i -\omega_r} \gg |g{i}|$, $i=1,2$, with
$\omega_r$ the cavity frequency, the cavity can be eliminated and an effective
two-transmon Hamiltonian is obtained. The coupling between each transmon and the
cavity turns into an effective qubit-qubit coupling
\begin{equation}
J^{\eff}
\approx
    \frac{g_1 g_2}{(\omega_1-\omega_r)}
  + \frac{g_1 g_2}{(\omega_2-\omega_r)}\,.
\end{equation}
In most current setups, $J^{\eff} \ll \Abs{\omega_2 - \omega_1}$,
and the two-transmon Hamiltonian can be approximated as \cite{PolettoPRL2012}
\begin{equation}
\begin{split}
  \Op{H}_{2T}
  &
  \approx
    \sum_{i=1,2} \left(
        \left( \omega_i + \frac{\alpha_i}{2}\right)
        \Op{b}_i^{\dagger} \Op{b}_i
        - \frac{\alpha_i}{2} \left( \Op{b}_i^{\dagger} \Op{b}_i \right)^2
    \right)
  + \\ \qquad &
  + J^{\eff} \left( \Op{b}_1^\dagger \Op{b}_2
                  + \Op{b}_1 \Op{b}_2^\dagger
            \right)
  + \Omega(t) \left( \Op{b}_1 + \Op{b}_1^\dagger
                    + \lambda \Op{b}_2 + \lambda \Op{b}_2^\dagger \right)\,,
\end{split}
\label{eq:PE_H2T}
\end{equation}
cf.~Eq.~\eqref{eq:tm_jc_eff},
where $\Omega(t)$ is the driving field in the rotating wave approximation, see
Eq.~\eqref{eq:tm_Omega_t}.
Typical parameters are given in Table~{\ref{tab:pe_transmon_parameters}}.
A two-level truncation of this Hamiltonian corresponds to
Eq.~\eqref{eq:H_truncated}.

\subsection{Krotov's Method}

The optimization is performed using Krotov's method,
section~\ref{subsec:Krotov} of chapter~\ref{chap:numerics}, for both the local
invariants functional~\eqref{eq:J_LI_tilde} and the perfect entanglers
functional~\eqref{eq:J_PE_tilde}. No dissipation is taken into account.
The basis states for the forward propagation are the Bell states
\begin{align}
  \Ket{\phi_1} &=  \frac{1}{\sqrt{2}} \left(    \ket{00} - \ii \ket{11}\right)\,,&
  \Ket{\phi_2} &= -\frac{1}{\sqrt{2}} \left(\ii \ket{01} -     \ket{10}\right)\,,\\
  \Ket{\phi_3} &= -\frac{1}{\sqrt{2}} \left(\ii \ket{01} +     \ket{10}\right)\,,&
  \Ket{\phi_4} &=  \frac{1}{\sqrt{2}} \left(    \ket{00} + \ii \ket{11}\right)\,,
\end{align}
\index{magic basis}%
as required for the calculation of the local invariants,
cf.~Eq.\eqref{eq:qmagic}. The equations of motion are given by the standard
Schrödinger equation, see Eq.~\eqref{eq:fw_eqm} and Eq.~\eqref{eq:bw_eqm}.

The boundary conditions for the backward propagation,
\begin{equation}
  \Ket{\chi_k^{(i)}(T)}
   = - \krotovdifquo[J_T]{\Bra{\phi_k}}{\phi_k^{(i)}(T)}\,,
\end{equation}
cf.~Eq.~\eqref{eq:chi_boundary}, are straightforward to
calculate, although tedious. For $J_{\LI}$, they can be found in
Ref.\cite{ReichDipl10}. For $J_{\PE}$, they are listed in
appendix~\ref{AppendixPE}.

Both $J_{\LI}$ and $J_{\PE}$ are highly non-convex functionals. Therefore, they
require the second order contribution in the Krotov update equation. With the
standard constraint~\eqref{eq:g_a_delta}, this update equation is
\begin{equation}
\begin{split}
  \Delta\Omega(t)
  &=
  \frac{S(t)}{\lambda_a} \Im \left[
    \sum_{k=1}^{N}
    \Bigg\langle
      \chi_k^{(i)}(t)
    \Bigg\vert
      \Bigg(\dHdOmega \Bigg)
    \Bigg\vert
      \phi_k^{(i+1)}(t)
    \Bigg\rangle
 + \right. \\ & \qquad \left.
    + \frac{1}{2} \sigma(t)
    \Bigg\langle
      \Delta\phi_k(t)
    \Bigg\vert
      \Bigg(\dHdOmega\Bigg)
    \Bigg\vert
      \phi_k^{(i+1)}(t)
    \Bigg\rangle
  \right]\,,
\end{split}
\label{eq:krotov_second_order_update}
\end{equation}
\index{Krotov!second order update equation}%
cf.~Eq.~\eqref{eq:krotov_proto_update}, with $\sigma(t)$ given
by Eq.~\eqref{eq:sigma_A}.
The optimization is carried out
for different gate durations between \SI{25}{ns} and \SI{400}{ns}, using
a sine-squared pulse of \SI{35}{MHz} peak amplitude as the guess pulse
$\Omega^{(0)}(t)$.

\subsection{Optimization Results}

\begin{figure}[tb]
  \centering
  \includegraphics{weyl_paths}
  \caption{Optimized gates in the Weyl chamber, for two transmon qubits,
  optimized with Krotov's method for the perfect-entangler (PE) functional in
  Eq.~\eqref{eq:J_PE_tilde}.
  The point at which each optimization enters the PE polyhedron, or the
  end point of the optimization if no PE can be obtained,
  is shown by a black dot and labeled with the gate duration.
  The entire optimization paths for  $T=50\,$ns and $T=400\,$ns are
  shown in light blue and dark purple, respectively, with the
  starting points labeled by 50$^*$ and 400$^*$.}
  \label{fig:transmon_weyl_paths}
\end{figure}
Figure~\ref{fig:transmon_weyl_paths} shows the results of the
optimization in the Weyl chamber. The point at which each optimization
enters the perfect entanglers
polyhedron is indicated by a black dot and labeled
with the gate duration. For $T<50\,$ns, no perfect entangler can be
reached -- defining heuristically the quantum speed limit (QSL) for this
transformation.
\index{quantum speed limit}%
In order to illustrate how the optimization proceeds,
the optimization paths for $T=50\,$ns, i.e., the gate at the
QSL, and a high-fidelity gate ($T=400\,$ns) are traced in light blue
and dark purple, respectively. Both optimizations start
in the $W_0^*$ region (near the $A_1$ point). The gate obtained with
the guess pulse for
$T=50\,$ns is significantly farther away from the surface of the
polyhedron of PE than that for the guess pulse with $T=400\,$ns.
Optimization for $T=400\,$ns therefore
moves directly towards the $W_0^*$ surface of the
PE polyhedron, whereas the optimization for $T=50$~ns enters the
ground plane and emerges in the $W_0$ region, before finally
reaching the $W_0$ surface of the polyhedron of perfect entanglers. The jump
from $W_0^*$ to $W_0$ is indicated by the light blue arrow. We find
the optimization to enter $W_0$ from $W_0^*$ for
durations $< 100\,$ns,  whereas for longer gate duration the optimizations
stay within $W_0^*$ entirely. The different optimization paths are a
result of the competition between the two objectives -- to reach a
perfect entangler, and to implement a gate that is unitary in the
logical subspace (the points shown in
Fig.~\ref{fig:transmon_weyl_paths} are the Weyl chamber
coordinates of the unitary $U$ closest to the actual time evolution
$\tilde{U}$). The latter objective is more difficult to realize
for shorter gate durations, resulting in a more indirect approach to
the polyhedron of perfect entanglers than one might expect when
considering that objective alone.

\begin{figure}[tb]
  \centering
  \includegraphics{tm_conv_LI}
  \caption{Comparison of optimization success for the
    PE functional compared to the local invariants (LI) functional
    for several points in the Weyl chamber. The
    optimization success using Krotov's method
    is measured in $c$-space, although the optimization functionals are
    defined in $g$-space (see text for details). For the
    LI-optimization, the results are fully converged. For the PE-optimization,
    the results are converged to a relative change below
    $10^{-2}$ (black solid curve) and $10^{-3}$ (gray dash-dash-dotted curve).
  }
  \label{fig:tm_conv_LI}
\end{figure}
It is instructive to compare the optimization success of the perfect entangler
functional, Eq.~\eqref{eq:J_PE_tilde} with the optimization under the
local-invariants functional, Eq.~\eqref{eq:J_LI_tilde} for a few select points
of the Weyl chamber. This is shown in Fig.~\ref{fig:tm_conv_LI}.
While the optimization was driven by the $g$-space formulation of the
functionals, the fidelities $F_{\lec}$ and $F_{\PE}$ define a more intuitive
figure of merit for the analysis.
As defined in Eq.~\eqref{eq:FUtilde-lec} and Eq.~\eqref{eq:FUtilde-pE}, the
fidelity is reduced by non-unitarity.

The results of Fig.~\ref{fig:tm_conv_LI} show how for different gate
durations, different gates are easiest to reach. In agreement
with the results of Fig.~\ref{fig:transmon_weyl_paths}, for durations
$< 50$~ns, the jump in the optimization error indicates a speed limit.
For short gate durations, $50~\text{ns} \le T \le 100~\text{ns}$, optimization
towards the point $Q$ in the Weyl chamber is most successful. This
matches the optimized gates for $T \le 100\,$ns in
Fig.~\ref{fig:transmon_weyl_paths} being near the $Q$ point. Also
correspondingly, the longer gate durations end near the $N$ point. The
failure to reach the point $Q$ for longer durations is due to the
symmetry structure of the Weyl chamber.
Namely, for the ground plane of the chamber, there is a mirror axis defined
by the line through $L$ and $A_2$, where mirrored points are in the same local
equivalence class. Both the $Q$-point and the $M$ point have local
invariants of $g_1 = \frac{1}{4}, g_2=0, g_3=1$. Since the
optimization was performed in $g$-space, these two points are not
distinguishable; indeed, for long gate
durations, the $Q$-optimization successfully reached the $M$
point.

In comparison with the local invariants optimization, the perfect
entanglers functional shows excellent performance. It automatically identifies
the optimal gate for a given gate duration and reaches significantly better
fidelities. This is due to the fact that the desired entangling power of $U$ can
usually be obtained in just a few tens of iterations of the algorithm, and the
remainder of the optimization
then focuses on improving the unitarity of the obtained gate $\tilde{U}$.
Most strikingly, we find that for the optimization towards a specific local
equivalence class, the convergence rate becomes extremely small as the optimum
is approached. All the results shown in Fig.~\ref{fig:tm_conv_LI} are
converged to a relative change below $10^{-4}$, such that no measurable
improvement can be expected within a reasonable number of iterations.
While in principle (due to the full controllability
of the system), the direct optimizations should yield arbitrarily
small gate errors, as long as the gate duration is above the quantum
speed limit, in practice this depends
on numerical parameters such as the weight $\lambda_a$ in Krotov's
method and may take a extremely large number of iterations or stagnate,
as we observe here. The perfect
entangler optimization shows remarkable robustness with respect to
this issue. We observed very little slow-down in convergence. The black
curve in Fig.~\ref{fig:tm_conv_LI} for the PE-optimization already yields
a significantly smaller optimization error than any of the LI-optimizations, but
is only converged to a relative change of $10^{-2}$. Even the gray
dash-dash-dotted curve, labeled PE$^*$, is only converged to a relative change
of $10^{-3}$, and thus the optimization would still yield considerably better
results if it were to be continued.

\begin{figure}[tb]
  \centering
  \includegraphics{tm_conv_favg}
  \caption{Analysis of the sources of error for PE of
    2 transmon qubits:
    Population loss from the logical subspace (light red squares),
    concurrence error of the closest unitary gate $\Op{U}$ in the logical
    subspace (blue circles), and average gate error,
    $\varepsilon_{avg}=1-F_{\avg}$,  with which $\Op{U}$ is
    implemented (red circles).
}
  \label{fig:tm_conv_favg}
\end{figure}
The values of the optimization error in Fig.~\ref{fig:tm_conv_LI} of $10^{-3}$
or $10^{-2}$ should not be understood to indicate a gate error above the quantum
error correction threshold.
Whereas the optimization error relates only to a figure of merit used for
optimization, the relevant physical quantity that would be determined in
an experiment is the average gate fidelity, Eq.~\eqref{eq:Favg}.

Figure~\ref{fig:tm_conv_favg} shows the
generated entanglement as measured by the concurrence and the average
gate error, $\varepsilon_{avg}=1-F_{avg}$,
together with the population loss from the logical
subspace. $\Op O$ is taken to be the unitary that is closest
to the projection of the realized operation from the full Hilbert space
onto the logical subspace. For $T> 50\,$ns, the gate errors are at or
below $10^{-4}$.
For shorter gate durations, insufficient entanglement is generated,
cf.\ blue dashed curve in Fig.~\ref{fig:tm_conv_favg}. Once $T$ is
sufficiently large to generate the desired entanglement,
the only source of error is loss of population from the
logical subspace, shown in light red in
Fig.~\ref{fig:tm_conv_favg}. This loss does not depend on the choice
of the weight $w$ in Eq.~\eqref{eq:J_PE_tilde}. When the gate duration
is increased, optimization yields gates that are exponentially more
unitary, as indicated by the linear decrease of the average gate error
in our semi-log plot.
The difficulty to ensure unitarity on the logical subspace is typical
for weakly anharmonic ladders, as found in superconducting transmon or
phase qubits. Optimal control can be successfully employed to tackle
the problem of ensuring unitarity in the logical subspace,
in addition to generating entanglement.