\newcommand{\PPMMap}{\ensuremath{\type{PParams}}}
\newcommand{\Lmax}{\ensuremath{\mathbb{L}_{\var{max}}}}
\section{UTxO}
\label{sec:state-trans-utxo-1}

The transition rules for unspent transaction outputs are presented in
Figure~\ref{fig:rules:utxo}. The states of the UTxO transition system, along
with their types are defined in Figure~\ref{fig:defs:utxo}:
\begin{itemize}
\item we define the protocol parameters as an abstract type, this type is made
  concrete in Section~\ref{sec:update}, where the update mechanism is
  discussed.
\item The lovelace supply cap ($\fun{lovelaceCap}$) is treated as an abstract
  function in this specification. In the actual system this value equals
  $$
  45 \times 10^{15}
  $$
\end{itemize}

Functions on the types introduced in \cref{fig:rules:utxo} are defined in
\cref{fig:derived-defs:utxo}. In particular, note that in function
$\fun{minfee}$ we make use of the fact that the $\Lovelace$ type is an alias
for the set of the integers numbers ($\mathbb{Z}$).

Rule~\ref{eq:utxo-bootstrap}, models the fact that the \textbf{reserves} of
the system are set to:
$$ \fun{lovelaceCap} - \balance{utxo_0} $$
The Lovelace amount $45 \times 10^{15}$ is the initial money supply in the
system.

\begin{figure*}[htb]
  \emph{Abstract types}
  %
  \begin{equation*}
    \begin{array}{r@{~\in~}lr}
      \var{tx} & \Tx & \text{transaction}\\
      %
      \var{txid} & \TxId & \text{transaction id}\\
      %
      ix & \Ix & \text{transaction index}\\
      %
      \var{addr} & \Addr & \text{address}\\
      %
      \var{pps} & \PPMMap & \text{protocol parameters}
    \end{array}
  \end{equation*}
  %
  \emph{Derived types}
  %
  \begin{equation*}
    \begin{array}{r@{~\in~}l@{\qquad=\qquad}r@{~\in~}lr}
      \ell & \Lovelace
      & n  & \mathbb{Z}
      & \text{currency value}
      \\
      \var{txin}
      & \TxIn
      & (\var{txid}, \var{ix})
      & \TxId \times \Ix
      & \text{transaction input}
      \\
      \var{txout}
      & \type{TxOut}
      & (\var{addr}, c)
      & \Addr \times \Lovelace
      & \text{transaction output}
      \\
      \var{utxo}
      & \UTxO
      & m
      & \TxIn \mapsto \TxOut
      & \text{unspent tx outputs}
    \end{array}
  \end{equation*}
  %
  \emph{Abstract Functions}
  \begin{equation*}
    \begin{array}{r@{~\in~}lr}
      \txid{} & \Tx \to \TxId & \text{compute transaction id}\\
      %
      \fun{txbody} & \Tx \to \powerset{\TxIn} \times (\Ix \mapsto \TxOut)
                                  & \text{transaction body}\\
      %
      \fun{a} & \PPMMap \to \mathbb{Z} & \text{minimum fee constant}\\
      %
      \fun{b} & \PPMMap \to \mathbb{Z} & \text{minimum fee factor}\\
      %
      \fun{txSize} & \Tx \to \mathbb{Z} & \text{abstract size of a transaction}\\
      %
      \fun{lovelaceCap} & \Lovelace & \text{lovelace supply cap}
    \end{array}
  \end{equation*}
  %
  \emph{Constraints}
  \begin{equation}
    \label{eq:txid-injective}
    \forall \var{tx_i}, \var{tx_j} \cdot
    \txid{\var{tx_i}} = \txid{\var{tx_j}} \Rightarrow \var{tx_i} = \var{tx_j}
  \end{equation}
  \caption{Definitions used in the UTxO transition system}
  \label{fig:defs:utxo}
\end{figure*}

\begin{figure}
  \begin{align*}
    & \fun{txins} \in \Tx \to \powerset{\TxIn}
    & \text{transaction inputs} \\
    & \txins{tx} = \var{inputs} \where \txbody{tx} = (\var{inputs}, ~\wcard)
    \nextdef
    & \fun{txouts} \in \Tx \to \UTxO
    & \text{transaction outputs as UTxO} \\
    & \fun{txouts} ~ \var{tx} =
      \left\{ (\fun{txid} ~ \var{tx}, \var{ix}) \mapsto \var{txout} ~
      \middle| \begin{array}{l@{~}c@{~}l}
                 (\_, \var{outputs}) & = & \txbody{tx} \\
                 \var{ix} \mapsto \var{txout} & \in & \var{outputs}
               \end{array}
      \right\}
    \nextdef
    & \fun{balance} \in \UTxO \to \mathbb{Z}
    & \text{UTxO balance} \\
    & \fun{balance} ~ utxo = \sum_{(~\wcard ~ \mapsto (\wcard, ~c)) \in \var{utxo}} c\\
   \nextdef
   %
    & \fun{minfee} \in \PPMMap \to \Tx \to \mathbb{Z} & \text{minimum fee}\\
    & \fun{minfee}~\var{pps}~\var{tx} =
      \fun{a}~\var{pps} + \fun{b}~\var{pps} * \fun{txSize}~\var{tx}
  \end{align*}
  \caption{Functions used in UTxO rules}
  \label{fig:derived-defs:utxo}
\end{figure}

\begin{figure}
  \emph{UTxO environments}
  \begin{equation*}
    \UTxOEnv =
    \left(
      \begin{array}{r@{~\in~}lr}
        \var{utxo_0} & \UTxO & \text{genesis UTxO}\\
        \var{pps} & \PPMMap & \text{protocol parameters map}
      \end{array}
    \right)
  \end{equation*}

  \emph{UTxO states}
  \begin{equation*}
    \UTxOState =
    \left(
      \begin{array}{r@{~\in~}lr}
        \var{utxo} & \UTxO & \text{unspent transaction outputs}\\
        \var{reserves} & \Lovelace & \text{system's reserves}
      \end{array}
    \right)
  \end{equation*}

  \emph{UTxO transitions}
  \begin{equation*}
    \_ \vdash
    \var{\_} \trans{utxo}{\_} \var{\_}
    \subseteq \powerset (\UTxOEnv \times \UTxOState \times \Tx \times \UTxOState)
  \end{equation*}
  \caption{UTxO transition-system types}
  \label{fig:ts-types:utxo}
\end{figure}

\begin{figure}
  \begin{equation}\label{eq:utxo-bootstrap}
    \inference
    {
    }
    {
      {\left(\begin{array}{l}
        utxo_0\\
        pps
      \end{array}\right)}
      \vdash
      \trans{utxo}{}
      \left(
        \begin{array}{l}
          \var{utxo_0}\\
          \fun{lovelaceCap} - \balance{utxo_0}
        \end{array}
      \right)
    }
  \end{equation}
  \nextdef
  \begin{equation}\label{eq:utxo-inductive}
    \inference
    { \txins{tx} \subseteq \dom \var{utxo}\\
      \var{fee} \leteq \balance{(\txins{tx} \restrictdom \var{utxo})} - \balance{(\txouts{tx})}
      & \minfee{pps}{tx} \leq \var{fee}\\
      \txins{tx} \neq \emptyset
      & \txouts{tx} \neq \emptyset
      & \forall \wcard \mapsto (\wcard, c) \in \txouts{tx} \cdot 0 < c
    }
    {
      {\left(\begin{array}{l}
        utxo_0\\
        pps
       \end{array}\right)}
      \vdash
      \left(
          \begin{array}{l}
            \var{utxo}\\
            \var{reserves}
          \end{array}
      \right)
      \trans{utxo}{tx}
      \left(
        \begin{array}{l}
          (\txins{tx} \subtractdom \var{utxo}) \cup \txouts{tx}\\
          \var{reserves + \var{fee}}
        \end{array}
      \right)
    }
  \end{equation}
  \caption{UTxO inference rules}
  \label{fig:rules:utxo}
\end{figure}

Rule~\ref{eq:utxo-inductive} specifies under which conditions a transaction can
be applied to a set of unspent outputs, and how the set of unspent output
changes with a transaction:
\begin{itemize}
\item Each input spent in the transaction must be in the set of unspent
  outputs.
\item The minimum fee, which depends on the transaction and the protocol
  parameters, must be less or equal than the difference between the balance of
  the unspent outputs in a transaction (i.e. the total amount paid in a
  transaction) and the amount of spent inputs.
\item The set of inputs must not be empty.
\item All the transaction outputs must be positive. We do not allow $0$ value
  outputs to be consistent with the current implementation.
\item If the above conditions hold, then the new state will not have the inputs
  spent in transaction $\var{tx}$ and it will have the new outputs in
  $\var{tx}$.
\end{itemize}

\clearpage

\subsection{Witnesses}
\label{sec:witnesses}

The rules for witnesses are presented in Figure~\ref{fig:rules:utxow}. In the
initial rules note that $\var{utxoEnv}$ and $\var{utxoSt}$ are tuples, where
$\var{utxoEnv} \in \UTxOEnv$ and $\var{utxoSt} \in \UTxOState$. The definitions
used in Rule~\ref{eq:utxo-witness-inductive} are given in
Figure~\ref{fig:defs:utxow}. Note that Rule~\ref{eq:utxo-witness-inductive}
uses the transition relation defined in Figure~\ref{fig:rules:utxo}. The main
reason for doing this is to define the rules incrementally, modeling different
aspects in isolation to keep the rules as simple as possible. Also note that
the $\trans{utxo}{}$ relation could have been defined in terms of
$\trans{utxow}{}$ (thus composing the rules in a different order). The choice
here is arbitrary.

\begin{figure}[htb]
  \emph{Abstract functions}
  %
  \begin{equation*}
    \begin{array}{r@{~\in~}lr}
      \fun{wits} & \Tx \to \powerset{(\VKey \times \Sig)}
      & \text{witnesses of a transaction}\\
      \fun{hash_{vkey}} & \Addr \mapsto \KeyHash
      & \text{hash of a verifying key in an address}\\
    \end{array}
  \end{equation*}
  \caption{Definitions used in the UTxO transition system with witnesses}
  \label{fig:defs:utxow}
\end{figure}
\begin{figure}[htb]
  \begin{align*}
    & \addr{}{} \in \UTxO \to \TxIn \mapsto \Addr & \text{addresses of inputs}\\
    & \addr{utxo} = \{ i \mapsto a \mid i \mapsto (a, \wcard) \in \var{utxo} \} \\
    \nextdef
    & \fun{addr_h} \in \UTxO \to \TxIn \mapsto \KeyHash & \text{verifying key hashes}\\
    & \fun{addr_h}~utxo = \{ i \mapsto h \mid i \mapsto (a, \wcard) \in \var{utxo}
      \wedge a \mapsto h \in \fun{hash_{vkey}} \}
  \end{align*}
  \caption{Functions used in rules witnesses}
  \label{fig:derived-defs:utxow}
\end{figure}

\begin{figure}
  \emph{UTxO with witness transitions}
  \begin{equation*}
    \var{\_} \vdash
    \var{\_} \trans{utxow}{\_} \var{\_}
    \subseteq \powerset
    (\UTxOEnv \times \UTxOState \times (\Tx \times \powerset{(\VKey \times \Sig)}) \times \UTxOState)
  \end{equation*}
  \caption{UTxO with witness transition-system types}
  \label{fig:ts-types:utxow}
\end{figure}

\begin{figure}
  \begin{equation}\label{eq:utxow-bootstrap}
    \inference
    {
      {\begin{array}{l}
         utxoEnv
      \end{array}}
      \vdash
      \trans{\hyperref[fig:rules:utxo]{utxo}}{}
      \left(
        \var{utxoSt}
      \right)
    }
    {
      {\begin{array}{l}
         utxoEnv
      \end{array}}
      \vdash
      \trans{utxow}{}
      \left(
        \var{utxoSt}
      \right)
    }
  \end{equation}
  \nextdef
  \begin{equation}
    \label{eq:utxo-witness-inductive}
    \inference
    { \var{utxoEnv}
      \vdash
      {\left(
        \begin{array}{l}
          \var{utxo}\\
          \var{reserves}
        \end{array}
      \right)}
      \trans{\hyperref[fig:rules:utxo]{utxo}}{tx}
      {\left(
        \begin{array}{l}
          \var{utxo'}\\
          \var{reserves'}
        \end{array}
      \right)}
      & \var{maxTxSize} \mapsto \var{t} \in \var{pps} & \fun{txSize}~\var{tx} \leq t \\ ~ \\
      & \forall i \in \txins{tx} \cdot \exists (\var{vk}, \sigma) \in \wits{\var{tx}}
      \cdot
      \mathcal{V}^\sigma_{\var{vk}}~{\serialised{\txbody{tx}}}
      \wedge  \fun{addr_h}~{utxo}~i = \hash{vk}\\
    }
    {\var{utxoEnv} \vdash
      \left(
        \begin{array}{l}
          \var{utxo}\\
          \var{reserves}
        \end{array}
      \right)
      \trans{utxow}{tx}
      \left(
        \begin{array}{l}
          \var{utxo'}\\
          \var{reserves'}
        \end{array}
      \right)
    }
  \end{equation}
  \caption{UTxO with witnesses inference rules}
  \label{fig:rules:utxow}
\end{figure}

\clearpage

\subsection{Transaction sequences}
\label{sec:transaction-seqs}

\cref{fig:rules:utxow-seq} models the application of a sequence of
transactions. For the sake of concision we omit the types of this transition
system, since they are the same as the ones of $\trans{utxow}{}$.

\begin{figure}[htb]
  \begin{equation}\label{eq:utxows-bootstrap}
    \inference
    {
      {\begin{array}{l}
         utxoEnv
      \end{array}}
      \vdash
      \trans{\hyperref[fig:rules:utxo]{utxow}}{}
      \left(
        \var{utxoSt}
      \right)
    }
    {
      {\begin{array}{l}
         utxoEnv
      \end{array}}
      \vdash
      \trans{utxows}{}
      \left(
        \var{utxoSt}
      \right)
    }
  \end{equation}
  \nextdef
  \begin{equation}
    \label{eq:rule:utxow-seq-base}
    \inference
    {}
    {\var{utxoEnv} \vdash \var{utxoSt} \trans{utxows}{\epsilon} \var{utxoSt}}
  \end{equation}
  %
  \nextdef
  %
  \begin{equation}
    \label{eq:rule:utxow-seq-ind}
    \inference
    {
      \var{utxoEnv} \vdash \var{utxoSt} \trans{utxows}{\Gamma} \var{utxoSt'}
      &
      \var{utxoEnv} \vdash \var{utxoSt'} \trans{\hyperref[fig:rules:utxow]{utxow}}{\var{tx}} \var{utxoSt''}
    }
    {\var{utxoEnv} \vdash \var{utxoSt} \trans{utxows}{\Gamma;\var{tx}} \var{utxoSt''}}
  \end{equation}
  \caption{UTxO sequence rules}
  \label{fig:rules:utxow-seq}
\end{figure}


\documentclass[12pt,a4paper]{article}
\usepackage{mathtools}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}

\begin{document}
\inputencoding{utf8}

\title{Computational Fluid Dynamics Lab\\
\vskip 2.0em
Project Group 3 \\ \textbf{Uncertainty Quantification and 2D Navier-Stokes Equations with Arbitrary Geometries}}
\author{{Authors}: \\ {\textit{Grant Bartel}} \\
{\textit{Ionut Farcas}} \\ {\textit{Ayman Saleem}}}
\date{\vspace{-5ex}}
\maketitle
\thispagestyle{empty}
%
\newpage
\setcounter{page}{1}
\section*{}

This project incorporates the \emph{2D Navier-Stokes} Solver with Arbitrary Geometries along with \emph{non-intrusive Uncertainty Quantification} (UQ) methods.

Moreover, for the Navier-Stokes solver we developed a \emph{serial} solver as well as a \emph{parallel solver} using \emph{domain decomposition with MPI}. The UQ part was parallelized using MPI, thus offering the option to use \emph{two levels of parallelism}.

The first part is the 2D Navier-Stokes Solver with Arbitrary Geometries. The chosen scenario is \emph{flow over a step}, which was used for Worksheet 3. The solver is modified and it returns the re-attachment point--the \textbf{Quantity of Interest} (QoI)--and the time of occurrence--steady state time. This is determined by checking for the point where the direction of the horizontal-velocity-component reverses in the fluid layer above the floor. We also incorporated the option to input the dimensions of the .pbm file used for encoding the arbitrary geometries at runtime. Its purpose is to return the re-attachement point and the time when it occurs depending on the input \emph{Reynolds number} or \emph{viscosity}, and it serves as the numerical solver for the \emph{Uncertainty Quantification} part. The output of the solver is a \emph{.mc data file}, which contains the Reynolds number or the viscosity, the re-attachement point, and the time of occurrence. Each datafile is unique and is based on a unique id (\textit{see 3 in the enumeration below}). The solver was implemented using the C programming language with MPI.

The input parameters of the solver are \footnote{every input parameter should be greater or equal to 0.}:
\begin{enumerate}
\item {a \textbf{flag} with which the user decides whether the input variable is the Reynolds number or the viscosity, as follows}: \\
$\left\{
  \begin{array}{ccl}
    \text{if flag = 1, then} \ Reynolds \ number \\
    \text{else}, viscosity
  \end{array}
\right.$
\item {the \textbf{Reynolds number} or the \textbf{viscosity}, depending on the above flag};
\item {a \textbf{unique ID} for each input variable, that will be used for generating unique output data files};
\item {the \textbf{x-dimension} for the .pbm file};
\item {the \textbf{y-dimension} for the .pbm file};
\end{enumerate}
%
Thus, the \textbf{first task} is to \textbf{compile} the \textbf{solver code} so that to generate the executable (i.e., \emph{sim}) that is used in the UQ part.
\newline

After the executable for the solver is compiled we generate the UQ program. The UQ program calls the solver. In the UQ part, we implemented the \textbf{Monte Carlo} sampling (MCS) method that is used as an uncertainty propagation method in the case when the \emph{Reynolds number} or the \emph{viscosity} contains uncertainties. This is modelled as a \textbf{continuous random variable}, where the user can choose between the \textbf{normal} or \textbf{uniform}
\footnote{the random variables are taken of the form m + s*$\zeta$, where m is the \textit{mean}, s the \textit{standard deviation} and $\zeta \sim N(0,1) \ or \sim U(0,1)$;}
probability distribution functions (PDF) as options to generate the random variables. The \emph{Mersenne twister} pseudorandom number generator (PRNG) was chosen to actually generate these variables, based on the selected PDF. For each generated sample, the Navier-Stokes solver will be called via a \textbf{system call} using the above mentioned input parameters and after the solutions are obtained, the statistics (i.e., the \textbf{mean} and \textbf{variance} ) are computed.

The input parameters of the UQ program are \footnote{every input parameter should be greater or equal to 0,as in the solver case}:
\begin{enumerate}
\item {a \textbf{flag} with which the user decides which UQ method to use \footnote{this is for future development; for the moment, we have only MCS;}, as follows.}: \\
$\left\{
  \begin{array}{ccl}
    \text{if flag = 1, then} \ MCS \\
    \text{else}, another \ method
  \end{array}
\right.$
\item {a \textbf{flag} with which the user decides whether the input variable is the Reynolds number or the viscosity, as follows.}: \\
$\left\{
  \begin{array}{ccl}
    \text{if flag = 1, then} \ Reynolds \ number \\
    \text{else}, viscosity
  \end{array}
\right.$
\item {a \textbf{flag} with which the user decides whether the used distribution is normal or uniform}: \\
$\left\{
  \begin{array}{ccl}
    \text{if flag = 1, then} \ normal \\
    \text{else}, uniform
  \end{array}
\right.$
\item {a \textbf{flag} with which the user decides the solver type: serial or parallel}: \\
$\left\{
  \begin{array}{ccl}
    \text{if flag = 1, then} \ sequential\ solver \\
    \text{else}, parallel\ solver
  \end{array}
\right.$
\item {the \textbf{number of samples} used for the MC simulation};
\item {the \textbf{mean} for the random variable};
\item {the \textbf{standard deviation} for the random variable};
\item {the \textbf{x-dimension} for the .pbm file};
\item {the \textbf{y-dimension} for the .pbm file};
\end{enumerate}
Thus, the \textbf{second task} is to \textbf{compile} the \textbf{UQ code} so that to generate the executable (i.e., \emph{sim \_UQ})  that will be used to run the entire simulation.
The UQ part was implemented using the C++ programming language plus MPI. \\

\par To plot the distribution of the separation point the MATLAB script \texttt{combine\_data.m} (in the folder \emph{final\_samples/}) should be run. The \emph{\*.mc} files must be placed inside a separate folder within \emph{final\_samples/}).

It is important to mention that for the random number generating, we use the \emph{Boost} library, which is included in the submitted file. Moreover, the application code is available at \url{https://github.com/grantathon/computational_fluid_dynamics/tree/master/project}.
\end{document}


\documentclass{beamer}
\usepackage{subfig}
\usepackage{xcolor}
\usepackage[utf8]{inputenc}
\hypersetup{
	colorlinks,
	allcolors= .,
	urlcolor = red,
	citecolor = black,
}
\usepackage{color}
\urlstyle{same}
\setbeamerfont{footnote}{size=\tiny}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}

% \usepackage{utopia} %font utopia imported
\usepackage[style=authortitle,backend=bibtex]{biblatex}
\addbibresource{references.bib}

\newcommand{\blue}[1]{{\color{blue} #1}}
\newcommand{\red}[1]{{\color{red} #1}}

\usetheme{Madrid}
\usecolortheme{default}
\title[] %optional
{Optimization-based Motion Planning and Optimal Control}

\author[Jun Zeng]{Jun Zeng}

\institute[UC Berkeley]
{
    Department of Mechanical Engineering \\
    University of California, Berkeley
}

\date[February 25th 2020]
{February 25th 2020}

\AtBeginSection[]
{
  \begin{frame}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}
%The next statement creates the title page.
\frame{\titlepage}

\begin{frame}
	\frametitle{Overview}
	\tableofcontents
\end{frame}

\section{Introduction}
\begin{frame}{Where we need path planning?}
	How to consider a robotics research as a systematic problem? Let's think about autonomous driving:
	\begin{itemize}
		\item Policy and Strategy:
		\begin{itemize}
			\item Global: all safe and comfortable
			\item Local: need to stay on right side and lane change to another highway.
		\end{itemize}
		\item Vision: detect and analyze neighboring objects with semantic information
		\item Planner: Plan a collision-free and comfortable trajectory, e.g.
		\begin{itemize}
			\item Sample various candidates trajectories
			\item Select a best one according to some criteria
			\item Smooth the speed profiles to make it likely to be dynamically feasible
		\end{itemize}
		\item Control: Track trajectories with different control laws
	\end{itemize}
\end{frame}

\begin{frame}
	Planning is the bridge between high-level and low-level.
	\begin{itemize}
		\item Need to consider noise and disturbance from vision module
		\item Make the trajectory dynamically-feasible for the control module and other low-level ones.
	\end{itemize}
	The performance of path planning is generally determined by two aspects from my personal perspective:
	\begin{itemize}
		\item Refresh rate: already a huge time delay from various vision sensors, e.g. you wouldn't expect the refresh rate to be less than 10Hz for autonomous driving.
		\item Correctness: RRT without smoothness is generally hard to track...
	\end{itemize}
\end{frame}

\begin{frame}{Optimal Control vs Optimization-based Path Planning}
	\begin{itemize}
		\item Optimal control: linear–quadratic regulator (LQR), model predictive control (MPC), sliding mode control, etc.
		\item Topics in optimization-based path planning:
		\begin{itemize}
			\item Speed profile smoothing: e.g. minimum-jerk in autonomous driving.
			\item Contact force: e.g. hybrid mode switch with contact force in legged robotics.
			\item Energy efficiency: e.g. minimum-snap/jerk in aerial robotics.
		\end{itemize}
		\item They are the same in mathematics: an optimization problem is formulated to generate control strategy or trajectory/robot's motions.
		\item They are different in the implementation: optimal control is usually holding a higher refresh rate and needs to be solved very fast, while the  optimization problem in planning isn't necessarily to be solved computationally with that fast rate.
	\end{itemize}
\end{frame}

\begin{frame}{Notation}
	Generally, an optimal control problem or an optimization-based path planning problem could be described as follow,
	\begin{align*}
		u^*(t) :=& \argmin_{u(.)} \int_{t_0}^{t_f} c_t (x(t), u(t)) dt \\
		\textit{s.t.} \ & x(t_0) = x_0 \\
		& \dot{x}(t) = f(x(t), u(t)) \quad \forall t \\
		& x(t) \in \mathcal{X}_{feas} \quad \forall t \quad \text{(collision-free)} \\
		& u(t) \in \mathcal{U}_{feas} \quad \forall t \quad \text{(control limits)}
	\end{align*}
	\begin{itemize}
		\item In the view of control: generate feasible control inputs under dynamics constraints
		\item In the view of planning: generate dynamically-feasible waypoints (which will be tracked with appropriate control methods).
	\end{itemize}
\end{frame}

\section{Optimization-based path planning}
\subsection{General methods}
\begin{frame}{Mathematical methods}
	Generally, a optimization problem will satisfy a variety of constraints, this leads us to have different methods to solve them numerically.

	Some of them have explicit solutions:
	\begin{itemize}
		\item convex quadratic programming (Convex QP)
		\item LQR: For continuous-system linear system $\dot{x} = Ax + Bu$ with a quadratic cost
		$J = \int_0^{\infty} (x^T Q x + u^T R u + 2x^T N u) dt$. The feedback control law to minimize the value of the cost is $u = -Kx$, where $K = R^{-1} (B^T P + N^T)$ and $A^T P + P A - (P B + N) R^{-1} (B^T P + N^T) + Q = 0$. Here, the control law could be solved \red{explicitly}.
	\end{itemize}
\end{frame}

\begin{frame}{Numerical methods}
	Other general problems don't have explicit solutions and don't guarantee existing solution but could be solved numerically:

	An optimization-based path planning problem could be formulated as several types:
	\begin{itemize}
		\item Shooting methods: we 'shoot' out trajectories in different directions until we find a trajectory that has the desired boundary value.
		\item Collocation methods: choose a finite-dimensional space of candidate solutions (e.g. polynomials) and a number of points in the domain (called collocation points), and to select that solution which satisfies the given equation at the collocation points.
	\end{itemize}
\end{frame}

\begin{frame}{Solvers and existence of solutions}
	Most likely, all problems could be formulated into a dynamic programming problem and could be solved with various numerical solvers\footnote{More mathematical details for solving optimization problem are presented in EE 227A/B/C.}:
	\begin{itemize}
		\item for convex problems:
		\begin{itemize}
			\item software: CVX, OSQP
			\item internal solvers: Gurobi, Sedumi, Mosek
		\end{itemize}
		\item for non-convex problems:
		\begin{itemize}
			\item interior point: IPOPT, SNOPT
			\item active set method: SAS
		\end{itemize}
	\end{itemize}
	\vspace{\baselineskip}

	We are more interested in the existence of the solution of the optimization. For a convex problem, we are likely to ensure at least one solution (might have several ones if it's nonlinear or non-convex cost function, our numerical solution will depends on the initial point where we do gradient descent). But for general non-convex problem, we are \red{unable to} guarantee the existence of solution.
\end{frame}

\subsection{Examples}
\begin{frame}{Trajectory smoothing}
	One of the most applicable areas about optimization-based planning is trajectory smoothing, which intends to make trajectory generated from the sample-based methods (RRT, PRM, etc) become smoother.
	\begin{figure}
		\includegraphics[width=0.5\linewidth]{figures/trajectory-smooth.jpg}
		\caption{Make your trajectory smoother}
	\end{figure}
\end{frame}

\begin{frame}{Geometric interpolation}
	There are some traditional geometric methods for trajectory smoothing:
	\begin{itemize}
		\item Polynomial interpolation
		\item Bezier Curve \href{https://www.geogebra.org/m/r3UWE9KR}{[online playground]}
		\item Cubic Splines
		\item B-Splined
	\end{itemize}
	\begin{figure}
		\includegraphics[width=0.5\linewidth]{figures/bezier_curve.png}
		\caption{Bezier Curve}
	\end{figure}
\end{frame}

\begin{frame}{Special curves for smoothness}
	In industry, people prefer some special curves which are easy to be implemented with less complexity:
	\begin{itemize}
		\item Dubin’s Curve (e.g. lane change in autonomous driving)
		\includegraphics[width=0.5\linewidth]{figures/dubin-curve.png}
	\end{itemize}
	\red{Smoothness doesn't necessarily guarantee dynamically-feasibility.}
\end{frame}

\begin{frame}{Optimization-based methods}
	The optimization-based methods could also deal with trajectory smoothing problem. For example, minimum-time for obstacle avoidance \footcite{hauser2010fast}.
	\begin{figure}
		\includegraphics[width=0.4\linewidth]{figures/trajectory-smooth-optimal-bounded-acceleration.png}
	\end{figure}
	Here, only bounded acceleration is considered, dynamics is still excluded.
\end{frame}

\begin{frame}{Related work}
	\begin{itemize}
		\item "Time Elastic Band" planner (TEB Planner) with ROS integration is introduced \footcite{rosmann2012trajectory} \footcite{rosmann2013efficient}, where non-holonomic kinematics is considered, but roughtly. \href{https://www.youtube.com/watch?v=e1Bw6JOgHME&feature=emb_title}{[demo video in ROS]}
		\item For autonomous driving, some special techniques are also proposed \footcite{dolgov2010path} \footcite{ziegler2014trajectory} \footcite{gu2012road}.
	\end{itemize}
\end{frame}

\begin{frame}
	There are many work about this area, the pipeline of path planning is mainly considered as trajectory generation + trajectory smooth, and the problem is formulated to consider mobile robots not holding too complex dynamics (autonomous cars, robot arm, etc).

	\vspace{\baselineskip}
	How about generating a dynamically-feasible trajectory directly from an optimization-based problem? There are several tricky points to take into account.
	\begin{itemize}
		\item Obstacle avoidance and Safety
		\item Hybrid mode switch (contact force)
		\item Energy efficiency and smoothness (e.g. which kind of cost function we shall choose)
	\end{itemize}
\end{frame}

\begin{frame}{Obstacle avoidance}
	There are several ways to consider obstacle avoidance as constraints in an optimization-based problem:
	\begin{itemize}
		\item simple geometric constraints: e.g. $(x - x_{obs})^2 + (y - y_{obs})^2 \geq r^2 $
		\begin{itemize}
			\item very rough consideration: point-mass and round obstacle
			\item nonlinear and non-convex constraints
			\item it could be solved with modern solvers (IPOPT, etc) but doesn't guarantee a solution.
		\end{itemize}
	\end{itemize}
\end{frame}

\begin{frame}{Obstacle avoidance}
	To make the problem become convex, an mixed-integer formulation could be introduced \footcite{richards2002aircraft} \footcite{deits2014footstep} \footcite{deits2015efficient}, which exploits the topological property of the collision-free space. We call it as "mixed-integer", since a list of integer variables is to used to represent the areas where the mobile robot is optimized to pass through.
	\begin{figure}
		\includegraphics[width=0.5\linewidth]{figures/collision-free-mixed-integer.png}
		\caption{A trajectory in which each linear segment is required to remain entirely within one of the convex obstacle-free regions indicated by the colored boxes. This requirement ensures that the entire trajectory is collision-free.}
	\end{figure}
\end{frame}

\begin{frame}{Obstacle avoidance}
	Sometime we need to describe our robot and obstacles as rigid bodies, this leads us to define the distance between rigid body and obstacle avoidance constraint between them. \href{https://raw.githubusercontent.com/XiaojingGeorgeZhang/OBCA/master/images/TrajBack_ParkingVideo.gif}{[demo]}
	\begin{figure}
		\includegraphics[width=0.5\linewidth]{figures/parallel-parking.png}
		\caption{Dual variables are used to represent the collision-free constraints, see more details in \footcite{zhang2017optimization}.}
	\end{figure}
\end{frame}

\begin{frame}{Obstacle avoidance}
	There are many other famous work:
	\begin{itemize}
		\item Sequential Convex Optimization \footcite{schulman2014motion}
		\item STOMP \footcite{kalakrishnan2011stomp}: deployed on PR2 robot and recommended by ROS.
		\item CHOMP \footcite{zucker2013chomp}: consider waypoint optimization and trajectory smoothness
	\end{itemize}
\end{frame}

\begin{frame}{Other work}
	There are other famous work for special systems:
	\begin{itemize}
		\item Swarm path planning for UAV \footcite{roberge2012comparison}
		\item Baidu Apollo open source planner \footcite{fan2018baidu}
	\end{itemize}
\end{frame}

\begin{frame}{Contact force}
	The intuition is simple: contact force is zero or distance between contact points should be zero. This formulates a complementarity constraint.

	\vspace{\baselineskip}
	Generally, the methods is called \emph{Optimization through contact \footcite{posa2014direct}} and the problem is formulated as,
	\begin{align*}
		\text{find} \ & \ddot{q}, \lambda \\
		\text{subject to} \ & H(q) \ddot{q} + C(q, \dot{q}) + G(q) = B(q) u + J(q)^T \lambda \\
		& \phi(q) \geq 0 \\
		& \lambda \geq 0 \\
		& \phi(q)^T \lambda  = 0
	\end{align*}
	where $\lambda$ represents the contact force. \red{No longer need to consider the mode sequences.}
\end{frame}

\begin{frame}{Examples}
	Applicable cases:
	\begin{itemize}
		\item Manipulation with finger contact
		\item Legged robotics \href{https://www.youtube.com/watch?v=pH1pDXnCBx4}{[demo]}
		\begin{figure}
			\includegraphics[width=0.8\textwidth]{figures/flamingo.png}
		\end{figure}
	\end{itemize}

\end{frame}

\begin{frame}{Example}
	For example, the tension $T(t)$ in a massless cable could be considered as a contact force. Assume the length of cable is $l_0$ and the distance between two ends of the cable is $l(t)$, then we have $\phi(q) = l_0 - l(t) \geq 0$ with its complementarity constraint,
	\begin{equation*}
	T(t) (l_0 - l(t)) = 0
	\end{equation*}
	\begin{figure}
		\includegraphics[width=0.6\linewidth]{figures/hybrid_mode_aerial_manipulation.png}
		\caption{Hybrid modes switches in aerial manipulation \footcite{zeng2020differential}}
	\end{figure}
\end{frame}

\begin{frame}{Energy efficiency and smoothness}
	\begin{itemize}
		\item A cost function to minimize jerk (third-order derivative of vehicle's position) \footcite{pattacini2010experimental}.
		\item A cost function to minimize snap (fourth-order derivative of vehicle's position) \footcite{mellinger2011minimum}.
	\end{itemize}

	Actually, people find that minimum jerk or snap usually performed better than those of other orders in robots for smoothness.
\end{frame}

\begin{frame}{Discussion}
	The optimization-based path planning is still active research topic, there are many existing problems which need to be solved:
	\begin{itemize}
		\item multi-agents: e.g. ensure collision-free movements in drone swarm
		\item safety: e.g. guarantee safety-critical trajectory in a noisy environment (sensor noise, model uncertainty, algorithm bias, etc)
		\item policy: centralized/decentralized, human-robot interaction
	\end{itemize}

\end{frame}

\section{Optimal control}
\subsection{Model predictive control}
\begin{frame}{Linear–quadratic regulator (LQR)}
	The LQR algorithm reduces the amount of work done by the control systems engineer to optimize the controller.
	\begin{align*}
		J^* &= \min_{u_i} \sum_{k = 0}^{N} x_{k+1}^T Q x_{k+1} + u_k^T R u_k \\
		\text{s.t.} \ & x_{k+1} = A x_{k} + B u_{k}
	\end{align*}
	There is indeed \emph{prediction} here, but feedback control gain is constant here.
\end{frame}

\begin{frame}{Receding horizon control}
	Let's present model prediction control as follow,
	\begin{align*}
		J_t^*(x(t)) &= p(x_{t+N}) + \sum_{k = 0}^{N-1} q(x_{t+k}, u_{t+k}) \\
		\text{s.t.} & x_{t+k+1} = A x_{t+k} + B u_{t+k}, k = 0,...,N-1 \\
		& x_{t+k} \in \mathcal{X}, u_{t+k} \in \mathcal{U}, k = 0,...,N-1 \\
		& x_{t+N} \in \mathcal{X}_f \\
		& x_t = x(t)
	\end{align*}
	Truncate after a finite horizon:
	\begin{itemize}
		\item $N$: horizon
		\item $p(x_{t+N})$: terminal cost which approximate the 'tail' of the cost.
		\item $q(x_{t+k}, u_{t+k})$: staged cost
		\item $\mathcal{X}_f$: Approximates the ‘tail’ of the constraints.
	\end{itemize}
\end{frame}

\begin{frame}{Model Predictive Control}
	\begin{figure}
		\includegraphics[width=0.5\linewidth]{figures/mpc-prediction.png}
		\caption{Constrained Finite Time Optimal Control (from Prof. Borrelli)}
	\end{figure}
	Where are the \emph{Predictions}?
	\begin{itemize}
		\item At each sampling time, solve a constrainted optimal control problem.
		\item Apply the optimal input only during $[t, t+1]$.
		\item At $t + 1$, solve a CFTOC over a shifted horizon based on new state measurements.
	\end{itemize}
\end{frame}

\begin{frame}{Open-loop vs Closed-loop }
	Some definitions among researchers working on MPC:
	\begin{itemize}
		\item Open-loop trajectory: at each time step, we have a constrainted optimal control problem, which solves a feasible trajectory with predictions.
		\item Closed-loop trajectory: since we solve optimal control problem at each time step, the robot will moves along a closed-loop trajectory with this control policy.
	\end{itemize}
\end{frame}

\begin{frame}{Discussions}
	\begin{itemize}
		\item Terminal cost ensures the Lyapunov convergence, but it's not necessary.
		\item Larger horizon brings a bigger controllable set, but needs more time to calculate the optimization.
		\item The dynamics constraints could be nonlinear, which will make the problem as a dynamic programming problem.
		\item There are many variants of MPC: Robust MPC, Adaptive MPC, MPC with obstacle avoidance, etc.
		\item Recent work about Learning MPC takes safety into account. \href{https://www.youtube.com/watch?v=W4LkQaTvzhI&feature=emb_title}{[demo]}
	\end{itemize}
\end{frame}

%\begin{frame}{Connection between Optimal Control and Optimization-based Planning}
%	There are various work in recent research:
%	\begin{itemize}
%		\item Some receding horizon methods are also introduced in path planning \footcite{kogan2006optimization}.
%	\end{itemize}
%\end{frame}

%\begin{frame}{References}
%	\printbibliography
%\end{frame}
\end{document}


\documentclass[a4paper]{article}

\input{temp}

\setcounter{section}{-1}

\begin{document}

\title{Quantum Information and Computation}

\maketitle

\newpage

\tableofcontents

\newpage

\section{Miscellaneous}

All course materials downloadable from $http://www.qi.damtp.cam.ac.uk/node/272$.

Some foci of the course: why do we need \emph{quantum} computation and informatoin? What is information? Classical information is presented by bits -- boolean variable with values 0,1, and bit strings for more alternative messages. Also, what is computation? In the classical sense it is updating bit strings by prescribed sequence of steps called "program". It uses basic elementary Boolean operatons/gates, e.g. AND, OR, NOT, SWAP acting on 1 or 2 bits.

Now if information consists of bits, then what is a bit? It is not abstract maths but two distinguishable state of a physical system.

Computation (info processing) must correspond to a physical evolution of the system representing the bits. So possibilities if info storage/communicating/processing must all rest on laws of physics and cannot be determined by abstract thought/mathematics alone.

Some benefits/issues of quantum vs classical physics:\\
(a) Computing power (computational complexity). A quantum computer cannot compute anything that's not computable \emph{in principle} on a classical computer. However, computational hardness, or amount of resources needed, matters. If it's too high then the problem is uncomputable \emph{in practice}.

\begin{eg}
Task: given an integer $N$ ($n=O(\log N)$ digits), we have an input size $n$. We wish to find a polynomial time algorithm, which runs in number of steps bounded by polynomials in $n$. Such an algorithm is feasible in practice. Algorithms needing exponential time are not feasible in practice, for example, trial division, whch requires $O(\sqrt{N}) = O(2^{n/2})$ steps. The best known classifical factoring algorthm runs in $2^{O(n^{1/3}(\log n)^{1/3})}$ which is not feasible. However, there is a quantum algorithm (Shor's Algorithm) that runs in polynomial time, and is in fact only $O(n^3)$.
\end{eg}

(b) Communication/security benefits:\\
$\bullet$ Provably secure communication pososible with quantum effects, which is impossible classically;\\
$\bullet$ Novel kinds of communicator, e.g. quantum teleportation, etc.

(c) Technological issues: Moore's law: miniaturisation of classical computing components (since 1965, factor of 4 every 3.5 years). Now at atomic scale where classical physics fails!

However, building a quantum computer seems to be very difficult now, and is beyond human's capability at this point. In 2018 (this year) we expect to have a working quantum computer of size 50 qubits.

\newpage

\section{Principles of Quantum Mechanics, Dual bra-ket notation}

\subsection{Bra and ket vectors}

Let $V$ be a finite dimensional complex vector space, with inner product. Vectors are written as $|v\ket$ (rather than $\mathbf{v}$). This is called the \emph{ket vectors} or just \emph{kets}; often work in $2-$dimensional $V_2$ with chosen orthonormal basis $\{|0>,|1\ket\}$ labelled by bit values $0,1$; kets always written in components as column vectors, i.e.
\begin{equation*}
\begin{aligned}
|v\ket = a|0\ket+b|1\ket = {a \choose b}, a,b \in \C
\end{aligned}
\end{equation*}
We call the conjugate transpose, $|v\ket^+$ a \emph{bra vector}, written with "mirror image notation",$$\bra v| = |v\ket^+ = a^*\bra 0| + b^* \bra 1| = (a^* \ b^*)$$ so \emph{row vectors} in components.

If $|w\ket$ = $c|0\ket + d|1\ket$ is another ket. Inner product of $|v\ket$ with $|w\ket$ written by juxtaposing $|v\ket^+$ with $|w\ket$,
\begin{equation*}
\begin{aligned}
\bra v|w \ket = |v\ket^+ |w\ket = (a^* b^*) {c \choose d} = a^*c + b^* d
\end{aligned}
\end{equation*}
which is the usual hermitian inner product on $\C^2$.

cf common math notaiton for inner product, $\bra \mathbf{v},\mathbf{w}\ket$.

$V \otimes W$ has dimension $mn$ with orthonormal basis $\{|e_i \ket \otimes |f_j \ket \}_{i,j}$. General ket on $V \otimes W$ is $$|\xi \ket = \sum c_{ij} |e_i \ket \otimes | f_j \ket$$

We have a natural bilinear map $f:V \times W \to V \otimes W$. If $|\alpha \ket = \sum a_i | e_i \ket$, $|\beta \ket = \sum b_j |f_j \ket$, then
\begin{equation*}
\begin{aligned}
(|\alpha\ket, |\beta\ket) \xrightarrow{f} |\alpha \ket \otimes |\beta\ket &= (\sum a_i | a\ket ) \otimes (\sum b_j | f_j \ket )\\
&= \sum_{i,j} a_i b_j | e_i \ket \otimes f_j\ket
\end{aligned}
\end{equation*}
In components, $|\alpha\ket \sim a_i$'s, $|\beta \ket \sim b_j$'s, $|\alpha\ket \otimes |\beta \ket \sim a_ib_j$. Note that $\otimes$ is not commutative: in general $|\alpha\ket \otimes |\beta \ket \neq |\beta\ket \otimes |\alpha\ket$ even if $V=W$.

We often omit the $\otimes$ symbol, i.e. we write $|\alpha\ket \otimes |\beta \ket$ as $|\alpha\ket |\beta\ket$.

\subsection{Product vectors and entangled vectors}
Any vector $|\xi\ket \in V \otimes W$ of form $|\xi\ket = |\alpha\ket |\beta\ket$ (for $|\alpha\ket \in V, |\beta\ket \in W$), called a \emph{product vector}.\\
This is \emph{not} surjective; any $|\xi\ket \in V \otimes W$ that is \emph{not} a product vectors is called \emph{entangled}.

We will mostly be concerned with tensor products of $V_2$ with itself, write $K$-fold tensor product as $\otimes^k V_2 = \underbrace{V_2 \otimes ... \otimes V_2}_{k \ times}$. This has dimension $2^k$, and orthonormal basis $|i_1 \ket \otimes ... \otimes i_k\ket$, $i_1,...,i_k = 0 \& 1$ labelled by $k$-bit strings. We often write $|i_1 \ket \otimes ... \otimes |i_k\ket$ as $|i_1 \ket |i_2 \ket ... |i_k \ket$ or $|i_1i_2...i_k\ket$, a $k$-bit substring.

\begin{eg}
$|v\ket = |00\ket + |11\ket \in V_2 \otimes V_2 = \otimes^* V_2$ is entangles. To see this, suppose $|v\ket = (a|0\ket + b|1\ket) (c|0\ket + d|1\ket)$ for some $a,b,c,d.$. Now $RHS = ac|00\ket+ad|01\ket+bc|10\ket+bd|11\ket$. Comparing coefficients with $|v\ket$, $ac=1,ad=0,bc=0,bd=1$ contradiction since we can get both $abcd=1$ and $abcd = 0$.
\end{eg}

We can show (sheet 1) that for any $|v\ket = \alpha|00\ket + \beta|01\ket + \gamma|10 \ket + \delta|11\ket = \sum c_{ij} |i\ket |j \ket$, where $c_{ij} = {{\alpha\ \beta} \choose {\gamma\ \delta}}$. $|v\ket$ is entangled iff $\alpha \delta - \beta \gamma \neq 0$. \\
Note that this iff holds only in 2-dimensions.

For general dimensions $V_n \otimes V_n$, any ket $\sum_{i,j=0}^{n-1} A_{ij} |i\ket |j\ket$ is product vector iff $[A_{ij}]$ matrix has rank 1, so $\det A = 0$ is necessary but \emph{not sufficient}.

\subsection{Inner product on $V \otimes W$}
This is induced by the inner products on $V$ and $W$. For product vectors, inner product of $|\alpha_1\ket |\beta_1 \ket$ with $|\alpha_2\ket |\beta_2\ket$ is ('matching slots')
\begin{equation*}
\begin{aligned}
(\bra\beta_1|\bra\alpha_1|) (|\alpha_2\ket |\beta_2 \ket) = \bra \alpha_1 | \alpha_2 \ket \bra\beta_1|\beta_2\ket
\end{aligned}
\end{equation*}
and extend by linearity to general vectors ($(AB)^+ = B^+ A^+$)(?).

Note notation: The bra vector of $|\alpha\ket |\beta\ket$ is often written in reverse order $\bra \beta | \bra\alpha|$. It's always important to keep track of component spaces.

Sometimes to be explained we label slots, eg write $|\alpha\ket_A |\beta\ket_B$ with bra ${}_A\bra\beta|{}_B\bra\alpha|$.

Quantum principle (QM1) (physical states): states of any (isolated) physical system $S$ are represented by unit vectors in a complex vector space $V$ with inner product.

The simplest non-trivial case $V=V_2$, 2-dimensional: choose a pair of orthonormal vectors, label them with bit values viz (?) $|0\ket$ and $|1\ket$, called \emph{computational basis} or \emph{standard basis}.

General state: $|\psi\ket = a|0\ket + b|1\ket$, $\bra\psi|\psi\ket = |a|^2+|b|^2 = 1$.

We say $|\psi\ket$ is a \emph{superposition} of states $|0\ket$ and $|1\ket$, with \emph{amplitude} $a$ and $b$ respectively.

Qubit: any quantum system with $2$-dimensional state space with a choice of orthonormal basis.

Conjugate basis (or $X$-basis): given $0\ket$, $|1\ket$, we define $$|+\ket = \frac{1}{\sqrt{2}} (|0\ket+|1\ket)$$ and $$|-\ket = \frac{1}{\sqrt{2}}(|0\ket-|1\ket)$$

(QM2) (composite systems)\\
If systems $S_1$ has state space $V_1$, $S_2$ has state space $V_2$, then joint system $S_1S_2$ has state space $V_1 \otimes V_2$.

\begin{eg}
$n$ qubits have state space $V_2^{\oplus n}$ which has dimension $2^n$.\\
$\bullet$ $n$ qubit state ($\psi$): called \emph{product state} if $|\psi \ket = |v_1 \ket |v_2 \ket... |v_n \ket$. It is entangled otherwise.\\
$\bullet$ Computaitonal or standard basis: we write
\begin{equation*}
\begin{aligned}
|i_1\ket |i_2\ket ... |i_n\ket := i_1i_2...i_n\ket
\end{aligned}
\end{equation*}
where $i_1,...,i_n = 0,1$; here we have $2^n$ orthonormal states.
\end{eg}

Note (!) as number $n$ of qubits grows linearrly, full state description (eg amplitudes $|\psi\ket = \sum \bra i_1i_2...i_n | i_1i_2...i_n\ket$) grows exponentially. In contrast, in the classical case, state space of $S_1S_2$ is a \emph{Cartesian product}, so if $S$ needs $K$ parameters, then $\underbrace{SS...S}_{n \text{ times}}$ has only $nK$ parameters.

\subsection{Diract notation on linear maps}
Consider linear maps on $V_2$: suppose $|v\ket = a|0\ket + b|1\ket$, $|w\ket = c|0\ket + d|1\ket$. Then the \emph{ket-bra product} gives
\begin{equation*}
\begin{aligned}
M = |v \times w| = {a \choose b}(c^* \ d^*) = {{ac^* \ ad^*} \choose {bc^* \ bc^*}} \ (*)
\end{aligned}
\end{equation*}
a linear map on $V_2$ (for any $|x\ket = x_0 |0\ket + x_1 |1\ket$):
\begin{equation*}
\begin{aligned}
M|x\ket = (|v\times w|) |x\ket = |v\ket (\bra w | x \ket) = {{ac^* \ ad^*} \choose {bc^* \ bd^*}} = {ac^*x_0+ad^*x_1 \choose bc^*x_0+bd^*x_1}
\end{aligned}
\end{equation*}
this is a rank-1 map, with kernel being all $|x\ket$'s $\perp$ $|w\ket$, i.e. $\bra w | x\ket = 0$.

For general linear map $A = {{a\ b} \choose {c\ d}}$, note first that the ket-bra products for $|0\ket \mid |1\ket$ basis vectors,
\begin{equation*}
\begin{aligned}
|0 \times 0| &= {1 \choose 0}(1 0) = {{1\ 0} \choose {0\ 0}},\\
|0 \times 1| &= {{0\ 1} \choose {0\ 0}},\\
|1 \times 0| &= {{0\ 0} \choose {1\ 0}},\\
|1 \times 1| &= {{0\ 0} \choose {0\ 1}},
\end{aligned}
\end{equation*}
so $A = a|0\times 0| + b|0 \times 1| + c| 1 \times 0| + d| 1 \times 1|$.

Note $\bra w | v \ket = \tr (|v \times w|)$ from (*) as cyclic property of trace.

Projection operators:

Important special case of ket-bra product (*) if when $|v\ket = |w\ket$, if $|v\ket$ normalised ($\bra v|v \ket = 1$), then $\Pi_v = |v \times v|$ is operator of projection into 1-dimensional subspace spanned by $|v\ket$. For example, we have
\begin{equation*}
\begin{aligned}
\Pi_v \Pi_v = |v\times v||v \times v| = |v\ket\bra v|v\ket \bra v| = |v\times v| = \Pi_v
\end{aligned}
\end{equation*}
Also we have $\Pi_v |a\ket = 0$("=$|v\ket\bra v|a\ket$") if $|a\ket \perp |v\ket$, i.e. $\bra v |a \ket =0$.

More generally, if $E$ is any $d$-dimensional linear subspace of an $n$-dimensional $V$ and $\{|e_1 \ket,...,|e_d\ket\}$ any orthonormal basis of $E$, then $\Pi_E = |e_1 \times e_1 | + ... + |e_d \times e_d|$ is operator of projection into $E$. Note that being orthonormal means that $\bra e_i |e_j \ket = \delta_{ij}$.

\subsection{Tensor products of maps}
If $A = {{a\ b} \choose {c\ d}}$, $B = {{p\ q} \choose {r\ s}}$ are linear maps on $V_2$, then $A \otimes B: V_2 \otimes V_2 \to V_2 \otimes V_2$ defined by action on basis $|i\ket |j\ket$, $i,j=0,14$:
\begin{equation*}
\begin{aligned}
|i\ket |j\ket \xrightarrow{A \otimes B} (A|i\ket) \otimes (B|j\ket)
\end{aligned}
\end{equation*}
(on product states, $|v\ket|w\ket \to (A|v\ket)\otimes(B|w\ket)$).

It's easy to check matrix of $A \otimes B$ is given by the block structure
\begin{equation*}
\begin{aligned}
(A \otimes B) = {{aB \ bB} \choose {cB \ dB}}
\end{aligned}
\end{equation*}
Important special cases are $I \otimes A$ and $A \otimes I$.

(QM3) (Physical evolution of quantum systems)\\
Any physical (finite time) evolution of a quantum system is represented by a \emph{unitary} operation on the state space.

Recall: $U$ is unitary iff $U^{-1} = U^+$ iff $U$ maps an orthonormal basis to an orthonormal set of vectors iff the columns (rows) of the matrix of $U$ are an orthonormal set of vectors.

Partial inner products for vectors in $V \otimes W$: Any ket $|v\ket \in V$ defines a linear map $V \otimes W \to W$ called "partial IP on $V$" defined on basis vectors $|e_i \ket \oplus |f_j\ket$:
\begin{equation*}
\begin{aligned}
|e_j \ket |f_j \ket \to \bra v|e_i\ket |f_j\ket
\end{aligned}
\end{equation*}
and extend by linearity. Similarly, for $|w\ket \in W$ we have a partial IP on $W$: $V \otimes W \to V$.

\begin{eg}
For $V = V_2 = W$, i.e. 2 qubits, we have
\begin{equation*}
\begin{aligned}
|\xi\ket = a|00\ket+b|01\ket+c|10\ket+d|11\ket \in V\otimes V
\end{aligned}
\end{equation*}
can form partial IP with $|0\ket$ on either slot. To keep track of "slots", when spaces are equal we will first label the slots $A$ and $B$: for $|\xi\ket = a|00\ket_{AB} + ...$, we label $|0\ket_A |0\ket_B$.\\
Orthonormal relations $\bra i |j \ket = \delta_{ij}$ gives partial IP on $A$ slot,
\begin{equation*}
\begin{aligned}
{}_A\bra 0|\xi\ket_{AB} &= a{}_A\bra 0|0 \ket_A |0\ket_B + ... + d {}_A\bra 0|1 \ket_a |1\ket_B\\
&= a|0\ket_B + b|1\ket_B
\end{aligned}
\end{equation*}
"pick out terms of $|\xi\ket_{AB}$ with $|0\ket$ in $A$-slot.\\
Similarly
\begin{equation*}
\begin{aligned}
{}_B\bra 0|\xi\ket_{AB} = a|0\ket_A {}_B\bra 0|0\ket_B + c|1\ket_A {}_B \bra 0|0 \ket_B
\end{aligned}
\end{equation*}
\end{eg}

(QM4) Quantum measurements and Born rule.\\
Suppose we are given a single instance of a quantum state $|\psi\ket$ of physical system, state space $V$, dimension $n$. Let $\mathcal{B} = \{|e_1\ket,...,|e_n\ket \}$ be any orthonormal basis of $V$. We can measure $|\psi\ket$ relative to basis $\mathcal{B}$. Possible outcomes $j=1,2,...,n$, $Prob(j) = |\bra e_j |\psi \ket |^2 = |a_j|^2$ if $|\psi \ket = \sum_{j=1}^n a_j | e_j\ket$. If outcome $j$ is seen, then after the measurement state is no longer $|\psi\ket$, but it has been "collapse" to $\psi_j\ket = |e_j\ket$. So repeated measurement gives \emph{same} result $j$ with certainty.

This is called a \emph{complete projective} (or von Neumann) measurement: "complete" refers to \emph{one}-dimensionality of orthogonal subspaces defined by $|e_j\ket$'s.

Incomplete projective measurements:\\
Let $\{E_1,E_2,...,E_d\}$ be decomposition of $V$ into $d$ mutually orthogonal subspaces, so $V = E_1 \oplus ... \oplus E_d$. Let $\Pi_i$ be projection into $E_i$. Then the incomplete measurement of $|\psi\ket$ w.r.t. this orthogonal decomposition is the following quantum operation.

The possible outcomes are $j=1,2,...,d$. Now $Prob(j)$ is the squared length of projection of $|\psi\ket$ into $E_j$, and is then equal to $\bra \psi | \Pi_j^+ \Pi_j | \psi \ket = \bra \psi | \Pi_j | \psi \ket$ since $\Pi_j^+ = \Pi_j$ and $\Pi_j\Pi_j = \Pi_j$ for any projection operator.

The post-measurement staet is the projected ("collapsed") vector re-normalised $|\psi_j\ket = \Pi_j | \psi \ket/\sqrt{prob(j)}$.

\begin{eg}
Parity measuremnt:\\
Parity of $2$-bit string $b_1b_2$ is the mod-2 sum $b_1 \oplus b_2$, i.e. the number of 1's mod 2.\\
Parity measurement on $2$ qubits is the incomplete measurement relative to the orthogonal decomposition $E_0 = span\{|00\ket, |11\ket\}$, $E_1 = span \{|01\ket, |10\ket\}$.\\
For $|\psi\ket = a|00\ket+b|01\ket+c|10\ket+d|11\ket$, we get outcome $0$ with probaility $|a|^2+|d|^2$, and post-measurement state would be $(a|00\ket + d|11\ket) / \sqrt{|a|^2+|d|^2}$.
\end{eg}

Measurement of "quantum observables" (as in most textbook):\\
Quantum observable $\theta$ is Hermitian operator on $V$. So $\theta$ has real eigenvalues $\lambda_j, j=1,...,d$. Eigenspaces $\Lambda_j$ mutually orthogonal, $\dim(\Lambda_j) = $ multiplicity of $\lambda_j$, $V= \Lambda_1 \oplus ... \oplus \Lambda_d$.\\
'Measurement of quantum observable $\theta$' is then just the (generally incomplete) measurement relative to this orthogonal decomposition, except outcomes now labelled by eigenvalues $\lambda_j$ (not just labels $j$).\\
$\lambda_j$ values often have further physical significance (energy, spin etc), and have e.g. average value equal to $\sum_j \lambda_j prob(\lambda_j)$. But for purposes of providing information about $|\psi\ket$, choice of \emph{labelling} of outcomes is immaterial.

Extended Born rule:\\
special case of an incomplete measurement. Measuring a component part of a composite system.\\
Suppose $|\psi\ket$ is state of composite system $S_1S_2$, with staet space $V \otimes W$.

Let $\mathcal{B}_V = \{|e_1 \ket, ..., |e_n\ket\}$ be an orthonormal basi of $V$. $|\psi\ket$ can be uniquely expanded as $$|\psi\ket_{VW} = \sum_i |e_i \ket_V |\xi_i\ket_W$$
where $|\xi_i\ket$'s generally neither normalised nor orthogonal. We have $\bra \psi|\psi \ket = \sum_i \bra \xi_i | \xi_i \ket = 1$. In fact (sinice $|G\ket$ orthonormal), we have $|\xi_i\ket_W =\bra e_i | \psi\ket_{VW}$. Now we can perform a measurement of $|\psi\ket $ relative to basis $\mathcal{B}_V$ of $V$. This is the incompplete measurement on $V \otimes W$ with outcomes $i=1,2,...,n$, and corresponding orthogonal subspaces $E_i = span(|e_i \ket \otimes |\xi \ket$; any $|xi\ket \in W$), $\dim (E_i) = \dim(W)$. Corresponding projectors are

$\Pi_i = |e_i \times e_i| \otimes I_W$, so we get $prob(i) = \bra \xi_i | \xi_i\ket$. Post-measurement state for $i$ is $$|\psi_i \ket = |e_i \ket |\xi_i\ket / \sqrt{\bra \xi_i | \xi_i \ket}$$
"just pick out terms of $|\psi\ket$ with $|e_i\ket$ in $V$-slot".

\iffalse
\begin{equation*}
\begin{aligned}

\end{aligned}
\end{equation*}
\fi

\end{document}


\subsection{Smoothing Deterministic Models}
Deterministic simulators are often stochastically perturbed to increase the diversity of the achievable simulations and to fit data more effectively.
The most widespread example of this is perturbing linear dynamical systems with Gaussian noise at each timestep.
The design of the system is such that the distribution over state at each point in time is Gaussian distributed.
However, the simplistic dynamics of such a system may be insufficient for simulating more complex systems.
Examples of such systems are: stochastic models of neural dynamics~\citep{fox1997stochastic, coutin2018fractional, goldwyn2011hh, Saarinen2008hh}, econometrics~\citep{lopes2011finance}, epidemiology~\citep{allen2017primer} and mobile robotics~\citep{thrun2001robust, fallon2012efficient}.
In these examples, the simulator state is perturbed with noise drawn from a distribution and is iterated using the simulator to create discrete approximations of the distribution over state as a function of time.

\subsection{Simulator Failure}
As simulators become more complex, guaranteeing the simulator will not fail for perturbed inputs becomes more difficult, and individual function evaluations become more expensive.
\citet{lucas2013failure} and \citet{edwards2011precalibrating} establish the sensitivity of earth science models to global parameter values by building a discriminative classifier for parameters that induce failure.
\citet{gmd2019crash} take an alternative approach instead treating simulator failure as an imputation problem, fitting a function regressor to predict the outcome of the failed experiment given the neighboring experiments that successfully terminated.
However these methods are limited by the lack of clear probabilistic interpretation in terms of the originally specified joint distribution in time series models, their ability to scale to high dimensions, and their applicability to state-space models.

\subsection{State-space Inference and Model Selection}
Probabilistic models are ultimately deployed to make inferences about the world.
Hence the goal is to be able to recover distributions over unobserved states, predict future states and learn unknown parameters of the model from data.
Posterior state-space inference refers to the task of recovering the distribution $p_{\mathcal{M}}(\mathbf{x}_{0:T} | \mathbf{y}_{1:T})$, where $\mathbf{x}_{0:T}$ are the latent states, $\mathbf{y}_{1:T}$ are the observed data, and $\mathcal{M}$ denotes the model if multiple different models are available.
Inference in Gaussian perturbed linear dynamical systems can be performed using techniques such as Kalman smoothing~\citep{kalman1960new}, however, the restrictions on such techniques limit their applicability to complex simulators, and so numerical methods are often used in practice.

\begin{algorithm}[t]
 \caption{Sequential Monte Carlo}\label{alg:meth:smc_p}
 \begin{algorithmic}[1]
  \Procedure{SMC}{$p_{\mathcal{M}}(\mathbf{x}_0)$, $\overline{p}_{\mathcal{M}}(\mathbf{x}_t | \mathbf{x}_{t-1})$, $\mathbf{y}_{1:T}$, $p_{\mathcal{M}}(\mathbf{y}_t | \mathbf{x}_t)$, $N$}
    \For{$n=1:N$}
        \State $\mathbf{x}_0^{(n)} \sim p_{\mathcal{M}}(\mathbf{x}_0)$ \Comment{Initialize from prior.}
    \EndFor
    \State $L_{\mathcal{M}} \gets 0$ \Comment{Track log-evidence}
     \For{$t=1:T$}
        \For{$n=1:N$}
          \State $\tilde{\mathbf{x}}_t^{(n)} \sim \overline{p}_{\mathcal{M}}\left(\mathbf{x}_t | \mathbf{x}_{t-1}^{(n)}\right)$  \label{alg:meth:smc_p:p} \Comment{Alg \ref{alg:rs}.}
          \State $w^{(n)}_t \gets p_{\mathcal{M}}\left(\mathbf{y}_t | \tilde{\mathbf{x}}_t^{(n)}\right)$ \Comment{Score particle.} \label{alg:meth:smc_p:w}
        \EndFor
        \For{$n=1:N$} \Comment{Normalize weights.}
          \State $W^{(n)}_t \gets w^{(n)}_t / \sum_{i=1}^N w^{(i)}_t$
        \EndFor
        \For{$n=1:N$} \Comment{Apply resampling.}
          \State $a^{(n)}_t \sim \text{Discrete}\left(\mathbf{W}_t\right)$ \label{alg:meth:smc_p:a}
          \State $\mathbf{x}_{t}^{(n)} \gets \tilde{\mathbf{x}}_{t}^{\left(a^{(n)}_t\right)}$
        \EndFor
        \State $L_{\mathcal{M}} \gets L_{\mathcal{M}} + \log\left(\frac{1}{N}\sum_{i=1}^N w^{(i)}_t\right)$ \label{alg:meth:smc_p:e}
     \EndFor
     \State $\textbf{return}\ \mathbf{x}^{(1:N)}_{0:T},\ a_{1:T}^{(1:N)},\ L_{\mathcal{M}}$
  \EndProcedure
 \end{algorithmic}
\end{algorithm}

A common method for performing inference in complex, simulation based models is sequential Monte Carlo (SMC)~\citep{doucet2001introduction}.
The basic algorithm for SMC is shown in Algorithm \ref{alg:meth:smc_p}, where $p_{\mathcal{M}}(\mathbf{x}_0)$ is the prior over initial state, $\overline{p}_{\mathcal{M}}(\mathbf{x}_t | \mathbf{x}_{t-1})$ is the dynamics model, or simulator, $p_{\mathcal{M}}(\mathbf{y}_t | \mathbf{x}_t)$ is the likelihood, defining the relationship between latent states and observed data, and $N$ is the number of particles used.
On a high level, SMC produces a discrete approximation of the target distribution by iterating particles through the simulator, and then preferentially continuing those simulations that ``explain'' the observed data well.
While a detailed understanding of particle filtering is not required, the core observation required for this work is that the likelihood of failed simulations is defined as zero: $p(\mathbf{y}_t | \mathbf{x}_t = \bot) \coloneqq 0$, and hence are rejected with certainty.

Posterior inference pipelines often also provide estimates of the model evidence, $p_{\mathcal{M}}(\mathbf{y}_{1:T})$.
SMC provides such an estimate, referred to as a pseudo-marginal evidence, denoted in Algorithm~\ref{alg:meth:smc_p} as $L_{\mathcal{M}}$.
This pseudo-marginal evidence is calculated (in log space) as the sum of the expected value of the unnormalized importance weights (Algorithm \ref{alg:meth:smc_p}, Lines \ref{alg:meth:smc_p:w} and \ref{alg:meth:smc_p:e}).
This evidence can be combined with the prior probability of each model via Bayes rule to estimate the posterior probability of the model (up to a normalizing constant)~\citep{mackay2003information}.
These posteriors can be compared to perform Bayesian model selection, where the model with the highest posterior is selected and used to perform inference.
This is often referred to as marginal maximum \emph{a posteriori} parameter estimation (or model selection)~\citep{doucet2002marginal, kantas2015particle}.
Recent work investigates model selection using approximate, likelihood-free inference techniques~\citep{papamakarios2019sequential, lueckmann2019likelihood}, however, we do not consider these methods here, instead focusing on mitigating computational inefficiencies arising directly from simulator failure.


% % % % % % R

\chapter{The R Software Suite}
\label{apx:r}

For an introductory text to \R see my class notes at \url{http://www.john-ros.com/Rcourse/}.
For some process control oriented references see:
\begin{description}
\item [SPC] For SPC with R see the \rcode{qcc}, \rcode{spc} packages, the appendix in \cite{qiu_introduction_2013}, and here \\ \url{http://blog.yhathq.com/posts/quality-control-in-r.html}.
\item [DOE] For DOE with \R, see \\ \url{https://cran.r-project.org/web/views/ExperimentalDesign.html}.
\item [Reliability] For DOE with \R, see \\ \url{https://cran.r-project.org/web/views/Survival.html}
\end{description}


\section{Evaluation}
\label{sec:Evaluation}
This section contains the evaluation and the graphical representation of the measurements. The following subsections contain the measurement results of the individual tasks listed in chapter \ref{subsec:Measurements}. The measured values are listed in appendix \ref{sec:Measurements}. The aim is to determine the Planck constant. The exact descriptions of the tasks can be found in the assignment \cite{light_quantum}.

%-----------------------------------------------------------------------------------
\subsection{Photoelectric Effect: Direct Measurement}
\label{subsec:Direct_Measurement}
To determine the Planck constant $h$ the software \flqq QtiPlot\frqq\ was used. To calculate the frequency the speed of light $c$ was used. Furthermore, to calculate the Planck constant the elementary charge $e$ was used. The results are shown below:

\[
h/e = (265\pm 9)\cdot 10^{-17}\ \si{V}\cdot\si{s}
\]
\[
h = (424\pm 14)\cdot 10^{-36}\ \si{J}\cdot\si{s}
\]
\[
U_0 = (-841\pm 58)\cdot 10^{-3}\ \si{V}
\]

To obtain these results, linear regression was used on the discrete measured values. The photoelectric voltage was measured directly. This is shown in the following figure \ref{fig:direct}:

\begin{figure}[H]
	\centering
	\includegraphics[scale=1]{direct}
	\caption{This plot shows the photoelectric voltage $U$ in function of the frequency $f$. Linear regression was used on the discrete measurement values to obtain $h/e$. An additional voltage offset $U_0$ was added to obtain the in red shown fitted curve. To calculate the frequency $f$ from the wave length $\lambda$, the speed of light $c$ was used (see equation \ref{eq:wave_length_frequency}). The offset voltage $U_0$ is negative.}
	\label{fig:direct}
\end{figure}

\newpage
%-----------------------------------------------------------------------------------
\subsection{Photoelectric Effect: Counter-Field Method}
\label{subsec:Counter-Field_Method}
The Planck constant was also determined by using the counter-field method. The evaluation was carried out in the same way as the last one. The obtained results are shown below:

\[
h/e = (339\pm 32)\cdot 10^{-17}\ \si{V}\cdot\si{s}
\]
\[
h = (544\pm 51)\cdot 10^{-36}\ \si{J}\cdot\si{s}
\]
\[
U_0 = (-1.37\pm 0.22)\ \si{V}
\]

Figure \ref{fig:counter} shows the measured values and the linear fit. This time the photoelectric voltage was not measured directly. The counter-field method was used to obtain the discrete measurement values.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1]{counter}
	\caption{Measurement values of the photoelectric voltage $U$ in function of the frequency $f$. The photoelectric voltage $U$ was not measured directly. The counter-field method was used. A linear fit (red) was used to obtain $h/e$. The measurement values differ quite a bit from there supposed position on the fitted curve. The offset voltage $U_0$ is even more negative than before.}
	\label{fig:counter}
\end{figure}

\newpage
%-----------------------------------------------------------------------------------
\subsection{Light-Emitting Diode: Threshold Voltage}
\label{subsec:LED_Threshold_Voltage}
To determine the Planck constant, the threshold voltage $U$ and the wave length $\lambda$ of six different LEDs was measured. While it is relatively easy to get an accurate reading of the wave length $\lambda$, it is not easy to get a good reading of the threshold voltage $U$. This can clearly be seen in figure \ref{fig:leds}. Nevertheless, the obtained results are shown below:

\[
h/e = (478\pm 64)\cdot 10^{-17}\ \si{V}\cdot\si{s}
\]
\[
h = (766\pm 103)\cdot 10^{-36}\ \si{J}\cdot\si{s}
\]
\[
U_0 = (-0.25\pm 0.35)\ \si{V}
\]

Linear regression was used again, to obtain a linear fit. The uncertainty of the fit is quite high, since the voltage values are probably off by quite a bit.

\begin{figure}[H]
	\centering
	\includegraphics[scale=1]{leds}
	\caption{This plot shows the measured threshold voltage $U$ in function of the frequency $f$. Just like before, linear regression was used on the discrete measurement values to obtain $h/e$. Especially the measurement values with a high frequency $f$ (low wave length $\lambda$) are off by a lot. This is due to the fact, that it is very hard to determine the threshold voltage $U$ accurately. The linear fitted curve is shown in red.}
	\label{fig:leds}
\end{figure}

Figure \ref{fig:dso} shows how the threshold voltage $U$ was determined with an oscilloscope for the infrared LED. The UI characteristic curve was created by using the XY time mode. Figure \ref{fig:spectrum} shows an export from the \flqq Logger Pro\frqq\ software. The wave length $\lambda$ was determined by finding the intensity peak and then reading the wave length $\lambda$ value.

\newpage
%-----------------------------------------------------------------------------------
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{dso}
	\caption{This is a screenshot of a measurment from the oscilloscope. The threshold voltage was determined with the cursors. In this example the threshold voltage $U$ of the infrared LED was measured and resulted in 1.18 V. The other five measurements were performed in the same way.}
	\label{fig:dso}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{spectrum}
	\caption{This is an exported graphic from the \flqq Logger Pro\frqq\ software. It shows the measured wave lengths $\lambda$ for the six different LEDs. The four curves with the high peaks are the blue, green, yellow and red LEDs. The peak far right is the infrared LED and the curve with essentially two peaks (quite flat curve) is the white LED.}
	\label{fig:spectrum}
\end{figure}


% {\bf MOVE TO APPENDIX??.

% Given $\aCmd$, define
% $\linV{\aCmd}$ as follows.
% \begin{definition}
% With each shared variable $\aLoc$, we associate a thread local variable $\aLocLoc$ and two boolean variables $\aRead,\aChanged$ that will be local to the program fragment.

% \begin{align*}
% \linV{\aCmd}= & \VAR\vec{\aLocLoc} \SEMI \VAR \vec{\aRead} \SEMI \VAR \vec{\aChanged} \SEMI  \\
% & \vec{\aRead} =\vec{0} \SEMI \vec{\aChanged} = \vec{0} \SEMI \\
% & \aCmd' \SEMI   \\
% & \overline{\IF \aChanged\  \aLoc= \aLocLoc}
% \end{align*}
% where:
% $\aCmd'$ is derived from $\aCmd$ by replacing:
% \begin{itemize}
% \item  each read $\aReg = \aLoc$ by $\aRead = 1 \SEMI \IF {(\aChanged \lor\ \aRead)} \THEN\  \aReg= \aLocLoc \ELSE\  \aReg= \aLoc \SEMI \FI $,
% \item each write $\aLoc = \aExp$ by $\aChanged =1 \SEMI \aLocLoc = \aExp$
% \end{itemize}
% \end{definition}
% From the soundness and completeness of Hoare logic for sequential operational semantics (eg. see formalization in~\citet{gordonHoare}), we deduce for any $\aForm, \bForm$:
% \[ \hoare{\aForm}{\aCmd}{\bForm}  \Longleftrightarrow\  \hoare{\aForm}{\linV{\aCmd}}{\bForm} \]
% }

\newcommand{\aMem}{M}
\newcommand{\bMem}{N}
\newcommand{\upd}[3]{#1[#2 \mapsto #3]}
\newcommand{\aStore}{S}
\newcommand{\bStore}{T}
\newcommand{\config}[3]{((#1,#2), #3)}
\newcommand{\aConfig}{\config{\aMem}{\aStore}{\aCmd}}
\newcommand{\bConfig}{\config{\bMem}{\bStore}{\bCmd}}
\newcommand{\trans}{\xrightarrow}
\newcommand{\configCmd}[1]{\aCmd_{#1}}
\newcommand{\aconfigCmd}[1]{{\rm Cmd}_{\aConfig}}
\newcommand{\aConfigV}{{\mathcal C}}
\newcommand{\bConfigV}{{\mathcal D}}
\newcommand{\seqsem}[1]{{\rm SEQ}\sem{#1}}

\section{Relation to Hoare Logic}
In this section, we consider commands without synchronization, concurrency and local registers, i.e.:
\begin{align*}
  % \amode \BNFDEF& \mRLX \BNFSEP \mRA \BNFSEP \mSC
  % \\
\aCmd,\,\bCmd
\BNFDEF& \SKIP
\BNFSEP \aReg\GETS\aExp\SEMI \aCmd
\BNFSEP \aReg\GETS\aLoc^{\amode}\SEMI \aCmd
\BNFSEP \aLoc^{\amode}\GETS\aExp\SEMI \aCmd
\BNFSEP \IF{\aExp} \THEN \aCmd \ELSE \bCmd \FI
\end{align*}
where $\amode$ is only $\mRLX$.  So, in the rest of the section, we elide all access modifiers.
For the purposes of this section, we assume that registers are single assignment.

For such commands, we consider the evident sequential operational semantics relating configurations with a memory and a store; we use $\aConfigV, \bConfigV$ to range over configurations.  The transition relation is of the form
\[ \aConfig \trans{} \bConfig \] and is described below.
We write $\upd{\aMem}{\aLoc}{\aVal}$ for the memory that results by updating the value of $\aLoc$ by $\aVal$ (and leaving the other locations untouched).
\begin{align*}
\config{\aMem}{\aStore}{\aReg\GETS\aVal \SEMI \aCmd} & \trans\
\config{\aMem}{\aStore}{\aCmd[\aVal/\aReg]} \\
\config{\upd{\aMem}{\aLoc}{\aVal}}{\aStore}{\aReg\GETS\aLoc \SEMI \aCmd} & \trans\
\config{\upd{\aMem}{\aLoc}{\aVal}}{\aStore}{\aCmd[\aVal/\aReg]} \\
\config{\aMem}{\aStore}{\aLoc \GETS \aVal \SEMI \aCmd}
& \trans\
\config{\upd{\aMem}{\aLoc}{\aVal}}{\aStore}{ \aCmd} \\
\config{\aMem}{\aStore}{\IF{0==0} \THEN \aCmd \ELSE \bCmd}
& \trans\
\config{\aMem}{\aStore}{ \bCmd} \\
\config{\aMem}{\aStore}{\IF{n==0} \THEN \aCmd \ELSE \bCmd}
& \trans\
\config{\aMem}{\aStore}{ \aCmd}, n \neq 0
\end{align*}

With a configuration $\aConfigV = \aConfig$, we associate a command $\aMem \SEMI \aStore \aCmd$, that converts the initial memory and store into assingnments, and define:
\[ \sem{\aConfig} = \eqdef\sem{\aMem \SEMI \aStore \aCmd}  \]

\begin{definition}[Sequential Pomsets]
A pomset $\aPS$ is {\em sequential} if for any $\aLoc$:
\begin{itemize}
\item $\aPS$ has atmost one write to $\aLoc$, and
\item All reads of $\aLoc$ read the same value, and
\item there are no edges out of any read to $\aLoc$
\item All preconditions are true
\end{itemize}
$\seqsem{\aCmd}  = \{ \aPS \mid \aPS \in \sem{\aCmd}, \aPS\ \mbox{ is sequential} \}$
\end{definition}

\begin{lemma}\label{soundp}
If $\aConfigV \trans \bConfigV$, then $\freadsat(\seqsem{\aConfigV}) = \freadsat(\seqsem{\bConfigV})$.
\end{lemma}
\begin{proof}
The proof follows from the peephole optimizations from section~\ref{sec:opt}, where the resriction to sequential pomsets strengthens the inequalities to equalities.

\eqref{RL} becomes a equality for sequential pomsets because all reads of $\aLoc$ read the same value.

\eqref{SF} becomes a equality for sequential pomsets because there are no edges out of a read. Since there are no non-trvial preconditions, this forces the value of the read to agree with the prior write.   The proof is completed by noting that read-saturation acccounts for the read missing from the right hand side.

\eqref{DS}  becomes a equality for sequential pomsets because they only track the final write.

{\bf COmment to James.  Add law for:  r=M; C and C[M/r]}
\end{proof}

\begin{verbatim}
ReadSat(SEQ([| C }) = ReadSat(SEQ([ final memory of C --> })

So: ReadSat(SEQ([| C }) = ReadSat(SEQ([| D })

  ==> ReadSat(SEQ([ final memory of C --> })  = ReadSat(SEQ([ final memory of D --> })

From which we conclude by Hoare completeness
\end{verbatim}

\begin{corollary}
Let $\aForm$ be a conjunction of atoms of the form $\aLoc = \aVal$.  Then, for any configurations $\aConfigV, \bConfigV$:
\[ (\forall \aForm) \hoare{\TRUE}{\aConfigV}{\aForm} \Leftrightarrow\   \hoare{\TRUE}{\bConfigV}{\aForm}  \]
IFF
\[ \freadsat(\seqsem{\aConfigV}) = \freadsat(\seqsem{\bConfigV}) \]
\end{corollary}
\begin{proof}
By the soundness and completeness of standard Hoare logic, as applied to our language where sequential composition is restricted to prefixing, we deduce that:
\[ (\forall \aForm) \hoare{\TRUE}{\aConfigV}{\aForm} \Leftrightarrow\   \hoare{\TRUE}{\bConfigV}{\aForm}
\]
IFF
\[ \aConfigV \trans{} \config{\aMem}{\aStore}{\SKIP} \Leftrightarrow\  \bConfigV \trans{} \config{\aMem}{\bStore}{\SKIP} \]

Result follows from lemma~\ref{soundp} by observing that $\seqsem{\config{\aMem}{\aStore}{\SKIP}}$ is the set of writes determined by $\aMem$.
\end{proof}

\endinput

For such commands, we establish the relationship between the sequential semantics and the pomset semantics.

In this section, we explore the

Comments from Radha:

Linear is incorrect as stated. eg. skip is Hoare equiv to r=x;x =r (which is
linear) but cannot be substituted by it.  It is necessary to add the proviso
linear AND (writes only if original program writes)

Thm 7.2 to:

a. Defn of linear needs to forbid reads after writes.

The theorem does not hold for:
\begin{verbatim}
   x=1; r=x
\end{verbatim}
vs
\begin{verbatim}
   x=1
\end{verbatim}
They are both linear. Satisfy the same hoare triples.  But have different
read(C)

Note that if $\aPSS'\supseteq\aPSS$, then
$\readc(\aPSS')\supseteq \readc(\aPSS)$.

% We give an abstract characterization of valid optimizations, establishing
% sufficient conditions to replace any command $\aCmd$ by an equivalent
% $\bCmd$: If $\aCmd$ and $\bCmd$ are synchronization\hyp{}free and
% sequentially equivalent, and furthermore $\bCmd$ is \emph{linear}---performs
% at most one read and at most one write on each location in each
% execution---then $\aCmd$ can be refined to $\bCmd$.

A program is \emph{sequential} if it lacks $\!\!\PAR\!\!$, and
\emph{synchronization-free} if lacks fences and $\mRA/\mSC$ access.  We
argue that our model is fully flexible with respect to optimization of such
programs, as long as the optimizations do not introduce new writes or
``relevant'' reads.  To do so, we first formalize a
relation between Hoare triples and pomsets with preconditions.
We then isolate a fragment of our
language and show
the soundness of {\em all} transformations of synchronization-free sequential
programs into this fragment.

%
\paragraph{Pomsets for Hoare Logic}
%\label{sec:hoare}
In \textsection\ref{sec:model}, we used Hoare logic
\cite{Hoare:1969:ABC:363235.363259,gordonHoare} to reason about
\emph{dependency analysis} for sequential code.  Here, we use an alternative
interpretation of Hoare logic to establish the soundness of \emph{program
  transformations}.  We develop a pomset semantics for pairs of formulae
$\semRW{\aForm}{\bForm}$, and show a relation between $\sem{\aCmd}$ and
$\semRW{\aForm}{\bForm}$ for valid Hoare triples
$\hoare{\aForm}{\aCmd}{\bForm}$.  In \textsection\ref{sec:model},
preconditions were discharged by read events.  Here, instead, preconditions
are derived from the read events themselves.

\begin{definition}
  \label{def:prepost}
  Let $\aPS\in\semRW{\aForm\land\cForm}{\bForm}$
  %We say that $\aPS$ \emph{satisfies} precondition $\aForm$ and postcondition $\bForm$ (notation $\aPS\in\semRW{\aForm}{\bForm}$)
  when it is possible to satisfy the following:

  Let $\aLocs$ be the set of locations such that $\aLoc\in\aLocs$ exactly
  when $\aForm$ depends on $\aLoc$.  For each $\aLoc\in\aLocs$, choose
  $\bEv_\aLoc\in\Event$ that reads $\aLoc$.
  Let $\bEvs_\aLocs=\{\bEv_\aLoc\mid\aLoc\in\aLocs\}$.
  Let $\aSub$ be the substitution generated % from $\aLocs$
  as follows:
  % For $\aLoc\in\aLocs$,
  $\aLoc\aSub = \aVal$ exactly when $\bEv_\aLoc$ reads $\aVal$ from $\aLoc$.

  Let $\bLocs$ be the set of locations such that $\bLoc\in\bLocs$ exactly
  when $\bForm$ depends on $\bLoc$.  For each $\bLoc\in\bLocs$, choose
  $\aEv_\bLoc\in\Event$ that writes $\bLoc$.
  Let $\aEvs_\bLocs=\{\aEv_\bLoc\mid\bLoc\in\bLocs\}$.
  Let $\bSub$ be the substitution generated % from $\bLocs$
  as follows:
  % For $\bLoc\in\bLocs$,
  $\bLoc\bSub = \aVal$ exactly when $\aEv_\bLoc$ writes $\aVal$ to $\bLoc$.

  Require that $\aForm\aSub$ and $\bForm\bSub$ are satisfiable.

  Require that $\labelingForm(\aEv_\bLoc)$ implies $\cForm$ (for each $\aEv_\bLoc$).

  Require that if $\cEv\le\aEv_\bLoc$ and $\cEv$ is a read, then $\cEv\in\bEvs_\aLocs$.

  Require that if $\aEv_\bLoc\le\cEv$ and $\cEv$ is a write to $\bLocs$, then $\cEv\in\aEvs_\bLocs$.
\end{definition}
Pictorially, we have:
\begin{tikzdisplay}[node distance=.1ex and 2em]
  \event{r}{\bEvs_\aLocs}{}
  \event{w2}{\cForm\mid\aEvs_\bLocs}{below right=of r}
  \event{w1}{\cEvs_\bLocs}{above right=of w2}
  \po{r}{w2}
  \wk{w1}{w2}
\end{tikzdisplay}
Here, $\aEvs_\bLocs$ are the final writes to $\bLocs$, with precondition $\cForm$.
$\cEvs_\bLocs$ are other writes to $\bLocs$, which must be ordered before $\aEvs_\bLocs$.
$\bEvs_\aLocs$ are the reads that the writes depend upon.

Precondition strengthening in Hoare logic validates read introduction, which
is invalid in our semantics.  Thus, in this section we consider pomsets that
are saturated with irrelevant reads.

% Recall that postconditions are properties of \emph{completed} executions.
% For example, in $\hoare{\TRUE}{x\GETS1\SEMI x\GETS2}{x{=}2}$, the
% postcondition does not hold for the prefix $x\GETS1$.  % As a result, we
% restrict attention to completed executions.  % A pomset
% $\aPS\in\aPSS$ is \emph{maximal} if there is no $\aPS'\in\aPSS$ such that
% $\aPS$ is a proper prefix of $\aPS'$.

\labeltext{Let}{page:readsat} $\readc(\aPSS)$ be the set $\aPSS'$ where $\aPS'\in\aPSS'$ when
$\exists\aPS\in\aPSS$ and  $\exists D$ such that $\Event'= \Event'\uplus D$,
${\le'} \supseteq{\le}$, $\labeling'(\aEv) = \labeling(\aEv)$, and for every
$\bEv\in D$ there are $\aLoc$ and $\aVal$ such that
$\labelingAct'(\bEv)=(\DR[\mRLX]{\aLoc}{\aVal})$.

\todo{Let xs=ws, ys=vs be such that xi=wi not in ys vs. Then:
   {ys=vs} C {xs=ws}  <==>  [ys=vs | xs=ws] cap read(C) not= emptyset}
\begin{theorem}
  \label{thm:hoare}
  If $\aCmd$ is synchronization-free and sequential,
  $\hoare{\aForm}{\aCmd}{\bForm}\Longleftrightarrow\notdisjoint{\semRW{\aForm}{\bForm}}{\readsem{\aCmd}}$.
\begin{proof}
  The proof proceeds by induction on the derivation of the Hoare triple.

  We first consider the structural rules.  Precondition strengthening follows
  from augmentation closure.   The structural rule for
  disjunction
  {\small\begin{math}
    \frac{\hoare{\aForm_1}{\aCmd}{\bForm_1}\quad  \hoare{\aForm_2}{\aCmd}{\bForm_2}}{ \hoare{\aForm_1 \lor \aForm_2}{\aCmd}{\bForm_1\lor \bForm_2}}
  \end{math}}
follows from disjunction closure (Definition~\ref{def:dis}). The structural rule for conjunction
  {\small\begin{math}
    \frac{\hoare{\aForm_1}{\aCmd}{\bForm_1}\quad \hoare{\aForm_2}{\aCmd}{\bForm_2}}{ \hoare{\aForm_1 \land \aForm_2}{\aCmd}{\bForm_1\land \bForm_2}}
  \end{math}}
  follows from the fact that pomsets have only concurrency and no conflict.

  The remaining cases follow directly from the semantics.  The only subtlety
  is the write rule, which uses $\parallel$ to ensure disjunction closure.
\end{proof}
\end{theorem}
% To illustrate multiple writes consider:
% \begin{gather*}
%   \hoare{\TRUE}{x\GETS 1 \SEMI x \GETS 2}{x=2}
%   \\
%   \hbox{\begin{tikzinline}[node distance=1em]
%       \event{w1}{\DW{x}{1}}{}
%       \event{w2}{\DW{x}{2}}{right=of w1}
%       \wk{w1}{w2}
%     \end{tikzinline}}
% \end{gather*}

Preconditions can be placed in $\aForm$ or $\cForm$ in
Definition~\ref{def:prepost}, resulting in different pomsets:
\begin{gather*}
  \hoare{x=1}{y \GETS x}{y=1}
  \\
    \hbox{\begin{tikzinline}[node distance=1em]
        \event{r}{\DR{x}{1}}{}
        \event{w}{\DW{y}{1}}{right=of r}
        \po{r}{w}
      \end{tikzinline}}
    \qquad\qquad
    \hbox{\begin{tikzinline}[node distance=1em]
        \event{r}{\DR{x}{1}}{}
        \event{w}{x=1 \mid \DW{y}{1}}{right=1ex of r}
      \end{tikzinline}}
\end{gather*}
Control dependencies are calculated correctly:
\begin{gather*}
  \hoare{\TRUE}{\IF{x} \THEN y \GETS 1 \ELSE y \GETS 1 \FI}{y=1}
  \\
    \hbox{\begin{tikzinline}[node distance=1em]
        \event{r}{\DR{x}{1}}{}
        \event{w}{\DW{y}{1}}{right=1ex of r}
      \end{tikzinline}}
\end{gather*}
% In contrast, in a semantics that forbids load buffering, where the best that one can prove is
% $\hoare{x=v} {r \GETS x \SEMI \IF{r=1} \THEN y \GETS 1 \ELSE y \GETS 1 \FI}{y= 1}
% $.

For any consistent set of preconditions, we can always find a single pomset:
\begin{gather*}
  \hoare{x_1=1}{y_1 \GETS x_1}{y_1=1}
  \qquad
  \hoare{x_2=1}{y_2 \GETS x_2}{y_2=1}
  \\
    \hbox{\begin{tikzinline}[node distance=1em]
        \event{rx}{\DR{x_1}{1}}{}
        \event{wy}{\DW{y_1}{1}}{right=of rx}
        \event{ru}{\DR{x_2}{1}}{right=1ex of wy}
        \event{wv}{\DW{y_2}{1}}{right=of ru}
        \rf{rx}{wy}
        \rf{ru}{wv}
      \end{tikzinline}}
\end{gather*}
% \begin{corollary}
%   If $\bigwedge_{i\in I}\aForm_i$ is satisfiable,
%   \begin{math}
%     \textstyle\bigwedge_{i\in I}\hoare{\aForm_i}{\aCmd}{\bForm_i} \Longleftrightarrow
%     \notdisjoint{\textstyle\bigcap_{i\in I}\semRW{\aForm_i}{\bForm_i}}{\readsem{\aCmd}}.
%   \end{math}
% \end{corollary}

\paragraph{Linearity}
%\label{sec:linear}
A command $\aCmd$ is \emph{linear} if for every $\aPS\in\sem{\aCmd}$, (1) there is
at most one read and at most one write on any location, and (2) if there is
both a read and a write to the same location, then the reads is ordered
before the write.

Intuitively, this means that the context around $\aCmd$ is unable to
interfere with the atomic execution of $\aCmd$; dually, neither can the
atomic execution of $\aCmd$ interfere with the context.  From an arbitrary
command, it is a straightforward exercise to construct a linear command that
is sequentially equivalent.

We say that $\aCmd$ and $\aCmd'$ \emph{satisfy the same Hoare triples} when
$\hoare{\aForm}{\aCmd}{\bForm}$ if and only if
$\hoare{\aForm}{\aCmd'}{\bForm}$, for every $\aForm$ and $\bForm$.

\begin{corollary}\label{seqcompleteness}
  Let $\aCmd$ and $\aCmd'$ be synchronization-free and sequential.  Further,
  let $\aCmd'$ be linear.
  Then $\aCmd$ and $\aCmd'$ satisfy the same Hoare
  triples if and only if $\readsem{\aCmd} \supseteq \readsem{\aCmd'}$.
  % If $\aCmd$ and $\aCmd'$ satisfy the same Hoare
  % triples then $\readsem{\aCmd} \supseteq \readsem{\aCmd'}$.
%   \begin{proof}
%     The pomsets in the semantics of a linear sequential program fragment,
%     such as $\sem{\bCmd}$, are generated by the augmentation closure of
%     pomsets that have a special format that only include edges of the
%     form: \begin{tikzdisplay}[node distance=1em]
%       \event{r}{\smash{\vec{\bForm}}\mid\DR{\vec{\bLoc}}{\vec{\aVal}}}{}
%       \eventl{\aEv}{w}{\aForm\mid\DW{\aLoc}{\bVal}}{below right=of r}
%       \po{r}{w}
%     \end{tikzdisplay}
%     where $\vec{\bLoc}\GETS \vec{\aLoc}$ has no conflicting assignments to the same variable, and where each $\aLoc$ appears in at most one write event. Thus, using  theorem~\ref{hoareGen}, we deduce that $\sem{\bCmd}$ is completely determined by the Hoare triples satisfied by $\bCmd$.
%
%     By the hypothesis of this theorem, $\aCmd$ satisfies the same Hoare triples as $\bCmd$.  Using  theorem~\ref{hoareGen}, we deduce that $\sem{\bCmd} \subseteq \fsat(\sem{\aCmd})$.
% \end{proof}
\end{corollary}

The linearity restriction ensures that the context cannot interfere with the
atomic execution of the command, and, dually, that the atomic execution of
the command cannot interfere with the context.  To see the need for this,
consider that the introduction of redundant relevant reads is valid sequentially, but
not valid concurrently. For example,
$\aReg \GETS \aLoc \SEMI \IF{\aReg \NOTEQ \aReg}\THEN \bLoc \GETS 1 \FI$
cannot be refined to
$\aReg \GETS \aLoc \SEMI \bReg \GETS \aLoc \SEMI \IF{\aReg \NOTEQ \bReg}
\THEN \bLoc \GETS 1 \FI$.  In a concurrent context, the latter program may
see different values for the two reads.

Order is necessary to exclude optimizations such as
$x\GETS 1 \SEMI r \GETS x$ to $x\GETS 1$. They are both linear. Satisfy the
same Hoare triples.  \todo{But have different read(C).}

\endinput

\subsection{Full abstraction for synchronization free threads}
Our semantics is complete for reasoning about full-thread optimizations of synchronization free programs.

In the rest of this section, we only consider commands $\aCmd,\bCmd$ that are
restriction-free, composition-free (ie. single-threaded), and
synchronization-free (ie. no acquire, release, fence).

In order to develop the proof,

This closure permits us to  describe a normal form for the top-level pomsets that arise in single-threaded and synchronization free code.
\begin{definition}
$\aPS$ is in normal form if:
\begin{itemize}
% \item All preconditions on events are tautologies.
\item If $\bEv \lt \aEv$ and  $\bEv$ is a write, then $\aEv$ is a write or read on the same variable.
\item If $\bEv \lt \aEv$ and  $\aEv$ is a read, then $\bEv$ is a write on the same variable.

% \item $\aEv \gtN \bEv$ only if $ \aEv\ (\lt \cup \reco)^{\star} \  \bEv$.
\item If $\aEv, \aEv'$ have the same read action label, then there exists a write $\bEv$ on the same location such that $\aEv \gtN \bEv \gtN \aEv'$.
%\item  If $\aEv, \aEv'$ have the same write action label, then
%there exists a event $\bEv$ on the same location as $\aEv$ such
%that  $\bEv$ has a different action label and $\aEv \gtN \bEv \gtN
%\aEv'$.
\end{itemize}
\end{definition}
In a normal form pomset, the successors of a write event (resp. the predecessors of a read) are related in the coherence order to the event.  Any two events with the same read label are separated by a write in the $\reco$ order.

It suffices to consider normal forms when distinguishing single-threaded, synchronization free code at the top level.  The normal forms determine the full semantics by the closure properties of the semantics.

\begin{lemma}\label{unrhd}
Every top-level pomset of $\freadsat\sem{\aCmd}$ is a top-level pomset of $\freadsat\sem{\bCmd}$ if and only if
every top-level normal form pomset of $\freadsat\sem{\aCmd}$ is a top-level normal form pomset of $\freadsat\sem{\bCmd}$.
\begin{proof}
The proof proceeds by induction on structure.  The key case is prefixing.  The only edges $\lt$-edges out of writes  and into read actions enforced by prefixing are $\reco$ edges.  Read prefixing permits the reuse of events to ensure that distinct read events not separated by $\reco$ have distinct read labels.
\end{proof}
\end{lemma}

We develop testers for top-level pomsets in normal form.  We  follow~\citet{Plotkin:1997:TSP:266557.266600}, albeit in a concrete form appropriate to our setting.

Let $\aPS$ be a top-level pomset in normal form with events $\aEv_1, \ldots,
\aEv_n$.  For all $i$, we assume a new location $b_i$.    Let $\freshaval$ be a fresh value that does not occur in $\aPS$.

Let $\vec{\bEv}$ be all  the predecessors of $\aEv_i$ in $\lt$. Let $\vec{c}$ be the corresponding sub vector of $b_i$'s.
\begin{itemize}
\item
If $\aEv_i$ has label $\DR{\aLoc}{\aVal}$, we define a program $\testP{\aPS}{i}$ as follows:
\[
  \testP{\aPS}{i} = (
  % \vec{\aReg} \GETS \vec{c}  \SEMI
  \IF{\vec{c}=\vec{1}} \THEN x \GETS \aVal \SEMI  \FENCE \SEMI b_i \GETS 1 \FI)
\]
\item
If $\aEv_i$ has label $\DW{\aLoc}{\aVal}$, we define a program $\testP{\aPS}{i}$ as follows:
\[
  \testP{\aPS}{i} = (
  % \vec{\aReg} \GETS \vec{c}  \SEMI
  \IF{\vec{c}=\vec{1}} \THEN %\aReg = \aLoc \SEMI
  \IF{\aLoc = \aVal} \THEN \aLoc = \freshaval \SEMI \FENCE \SEMI b_i \GETS 1  \FI \FI)
\]
\end{itemize}
$\testP{\aPS}{i}$ is as expected, matching the reads (resp. writes) in $\aPS$ by writes (resp. reads).  The fresh value is used to flush out prior writes and reset to see fresh writes.

\begin{definition}[Tester for $\aPS$]\label{testAPS}
The tester context for $\aPS$, $\testP{\aPS}{}\hole{}$ is
\[
b \GETS b_{1} \land  \cdots \land b_{n}
\PAR  \testP{\aPS}{1}
\PAR   \cdots
\PAR  \testP{\aPS}{n}
\PAR \hole{}
\]
\end{definition}
The constraints of the normal form ensure that if the labels of any two events are the same, then one is a predecessor of the other.

\begin{lemma}\label{tester}
$\sem{\testP{\aPS}{}\hole{\aCmd}}$  has a pomset that sets $b$ to $1$ iff $\aPS \in \freadsat\sem{\aCmd}$.
\begin{proof}
It suffices to prove that there is a pomset that sets $b$ to $1$ in
 $\freadsat\sem{b \GETS b_{1} \land  \cdots \land b_{n}
\PAR  \testP{\aPS}{1}
\PAR   \cdots
\PAR  \testP{\aPS}{n}} \parallel \bPS$ iff $\aPS$ is an augmentation of $\bPS$.

We prove by induction on $\lt$ that $\testP{\aPS}{i}\hole{\aCmd}$  sets $b_i$ to $1$ iff the event $\aEv_i$ is enabled. Result follows.
\end{proof}
\end{lemma}

\begin{theorem}
  Let $\aCmd_1$ and $\aCmd_2$ be restriction-free, composition-free and
  synchronization free.  Then all top-level pomsets of
  $\freadsat\sem{\aCmd_1}$ are also pomsets of $\freadsat\sem{\aCmd_2}$ if
  and only if for all parallel contexts $\bCmd$, all top-level pomsets of
  $\freadsat\sem{\aCmd_1 \PAR \bCmd}$ are also pomsets of
  $\freadsat\sem{\aCmd_2 \PAR \bCmd}$
\begin{proof}
  The forward implication follows from the compositionality of the semantics.

  For the converse, pick a top level pomset in normal form in
  $\aPS \in \freadsat\sem{\aCmd_1} \setminus \freadsat\sem{\aCmd_2} $.  The
  required context is given by the tester context $\testP{\aPS}{}\hole{}$.
 \end{proof}
\end{theorem}

The full abstraction theorem applies only to full threads.  The reason for this limited statement is that our impoverished language does not permit  parallel composition to be used freely as the continuation in arbitrary sequential contexts.  A full abstraction theorem that applies also to commands can be achieved with these richer distinguishing contexts.