\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{longtable}
\usepackage[hyphens,spaces,obeyspaces]{url}

\usepackage{amsmath,amsthm,amssymb,amsfonts}
\usepackage{booktabs}
\usepackage{array}
\usepackage{fancyhdr}
\usepackage[a4paper, margin=1in]{geometry}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{hyperref}
\graphicspath{ {./images/} }

\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\mat}[1]{\mathbf{#1}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
\newcolumntype{C}[1]{>{\PreserveBackslash\centering}p{#1}}
\newcolumntype{R}[1]{>{\PreserveBackslash\raggedleft}p{#1}}
\newcolumntype{L}[1]{>{\PreserveBackslash\raggedright}p{#1}}

\pagestyle{fancy}
\fancyhf{}
\rhead{TSE, Ho Nam}
\chead{Project \#3}
\lhead{MATH4828B}
\cfoot{\thepage}
\title{
  {\large MATH4828B Machine Learning for Natural Language Processing}\\
  \textbf{\large Project 3 -- Language Model}\\
  \textbf{Report}
  }
\author{
  \begin{tabular}{R{0.4\textwidth}L{0.4\textwidth}}
    \textbf{Name:} & TSE, Ho Nam \\
    \textbf{Student ID:} & 20423612 \\
    \textbf{Dev Set Score:} & 1.3916
  \end{tabular}
 }

\date{}

\begin{document}
\maketitle\thispagestyle{fancy}
\tableofcontents
\section{Architectures}
There are two main types of architecture that I tried, and they are (i) LSTM and (ii) TCN.

\subsection{LSTM}
Long Short Term Memory (LSTM) is a type of Recurrent Neural Network (RNN) that aims to solve the problem of gradient explosion and diminishing problems in simple RNN. Comparing with simple RNN, LSTM can be trained using a longer sequence and usually performs better. In a LSTM cell (Figure~\ref{fig:LSTM}), there are four gates \(i, f, o, g\) and two hidden states \(c_t, h_t\). The equations for forward pass is as follows, with \(\odot\) being the elementwise multiplication, \(\sigma\) being the sigmoid activation and \(\tanh\) being the \(\tanh\) activation.
\[
	\begin{bmatrix}
		i \\ f \\ o \\ g
	\end{bmatrix}
	=
	\begin{bmatrix}
		\sigma \\ \sigma \\ \sigma \\ \tanh
	\end{bmatrix}
	\mat W
	\begin{bmatrix}
		\mat h_{t-1} \\ \mat x_t
	\end{bmatrix}
	,\qquad
	c_t = f \odot c_{t-1} + i \odot g
	,\qquad
	h_t = o \odot \tanh(c_t)
\]
The intuitive idea behind these equations is that, \(i\) governs how much information of \(x_t\) should be written to \(c_t\) and \(f\) governs how much information of \(c_{t-1}\) should be forgotten. \(g\) governs how much \(x_t\) is reach revealed to \(c_t\) and \(o\) governs how much \(c_t\) is written to \(h_t\). This new model works better than simple RNN because of the gradient highway along the \(c_t\) line which help solves the problem of gradient issues.

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.5\linewidth]{LSTM}
	\caption{A LSTM cell.\protect\footnotemark}
	\label{fig:LSTM}
\end{figure}
\footnotetext{From Stanford cs231n lecture notes.}

Even LSTM is a better choice than simple RNN, it has a lot more parameters than simple RNN and generally it takes longer to train these models. Therefore, another alternative is explored for this project.
\subsection{TCN}
Temporal Convolutional Network (TCN) is a novel Convolutional Neural Network (CNN) modified to work on temporal domain introduced in early 2018. It uses a sequence of dilated convolutional layers, normalization layers, dropout layers and ReLU activations. From the authors\footnote{Bai, Shaojie, J. Zico Kolter, and Vladlen Koltun.``An empirical evaluation of generic convolutional and recurrent networks for sequence modeling.'' arXiv preprint arXiv:1803.01271 (2018).} of TCN, their experimental results show TCN outperforms simple RNN, LSTM and GRU in different sequence modelling tasks significantly. It can also exhibit a longer memory, and has stable gradients.

\begin{figure}[h!]
	\centering
	\includegraphics[width=\linewidth]{TCN}
	\caption{TCN strcuture.\protect\footnotemark}
	\label{fig:TCN}
\end{figure}
\footnotetext{From \url{https://github.com/philipperemy/keras-tcn}.}

\section{Experiments}
There were two phases of experiments. During the first phase, I used the provided sample code for generating training and validation data; while in the second phase, I reprogrammed these parts. There were in total around 200 sets of training records.

\subsection{Training with Provided Code}
There were multiple types of architectures attempted during this phase. They are constructed by LSTM, and/or TCN layers mentioned in the previous section. All models have a Embedding layer with various intermediate layer(s), then is finally fed into a Dense layer with softmax activation. The models include:

\begin{table}[h!]
	\centering
	\begin{tabular}{C{0.25\textwidth}C{0.65\textwidth}}
		\toprule
		Model Type                    & Architecture                                                      \\\midrule
		\texttt{lstm}                 & Embedding \(\to\) LSTM1 \(\to\) Dense                             \\\midrule
		\texttt{lstm2}                & Embedding \(\to\) LSTM1 \(\to\) LSTM2 \(\to\) Dense               \\\midrule
		\texttt{lstm3}                & Embedding \(\to\) LSTM1 \(\to\) LSTM2 \(\to\) LSTM3 \(\to\) Dense \\\midrule
		\texttt{lstm\_embed}          & Embedding \(\to\) LSTM1                                           \\
		                              & LSTM1 + Embedding \(\to\) Dense                                   \\\midrule
		\texttt{tcn}                  & Embedding \(\to\) TCN1 \(\to\) Dense                              \\\midrule
		\texttt{tcn\_embed}           & Embedding \(\to\) TCN1                                            \\
		                              & TCN1 + Embedding \(\to\) Dense                                    \\\midrule
		\texttt{tcn\_lstm}            & Embedding \(\to\) LSTM1                                           \\ & Embedding \(\to\) TCN1 \\ & TCN1 + LSTM1 \(\to\) Dense                                   \\\midrule
		\texttt{tcn\_lstm\_embed}     & Embedding \(\to\) LSTM1                                           \\ & Embedding \(\to\) TCN1 \\ & TCN1 + LSTM1  + Embedding \(\to\) Dense                                    \\\midrule
		\texttt{tcn\_lstm12\_embed}   & Embedding \(\to\) LSTM1 \(\to\) LSTM2                             \\ & Embedding \(\to\) TCN1 \\ & TCN1 + LSTM1 + LSTM2 + Embedding \(\to\) Dense            \\\midrule
		\texttt{tcn\_lstm2\_embed}    & Embedding \(\to\) LSTM1 \(\to\) LSTM2                             \\ & Embedding \(\to\) TCN1 \\ & TCN1 + LSTM2 + Embedding \(\to\) Dense       \\\midrule
		\texttt{tcn12\_lstm12\_embed} & Embedding \(\to\) LSTM1 \(\to\) LSTM2                             \\ & Embedding \(\to\) TCN1 \(\to\) TCN2 \\ & TCN1 + TCN2 + LSTM1 + LSTM2 + Embedding \(\to\) Dense           \\\bottomrule
	\end{tabular}
	\caption{List of attempted models.}
\end{table}

For parameter tuning, I mainly tweak the embedding size, hidden size (size of \(h_t, c_t\) in LSTM and number of kernels used in TCN) and dropout rate. Here some selected results are shown.

\begin{longtable}[c]{@{}>{\ttfamily}cccccc@{}}
	\toprule
	\textrm{Model Type}  & Embedding Size & Hidden Size & Dropout Rate & Log Loss & Score \\*
	\midrule
	\endhead
	%
	\bottomrule
	\endfoot
	%
	\endlastfoot
	%
	lstm                 & 100            & 512         & 0.5          & 1.7566   & 90\%  \\
	lstm                 & 100            & 512         & 0.2          & 1.7676   & 90\%  \\
	lstm                 & 100            & 256         & 0.2          & 1.7812   & 90\%  \\
	lstm                 & 100            & 256         & 0.5          & 1.8009   & 80\%  \\
	lstm                 & 100            & 128         & 0.2          & 1.8620   & 80\%  \\
	lstm                 & 100            & 128         & 0.5          & 1.9194   & 60\%  \\\midrule
	lstm\_embed          & 100            & 256         & 0.2          & 1.7413   & 90\%  \\
	lstm\_embed          & 100            & 128         & 0.2          & 1.7811   & 90\%  \\
	lstm\_embed          & 100            & 256         & 0.5          & 1.7887   & 80\%  \\
	lstm\_embed          & 100            & 128         & 0.5          & 1.7980   & 80\%  \\\midrule
	lstm2                & 100            & 500         & 0.3          & 1.8401   & 80\%  \\
	lstm2                & 100            & 500         & 0.4          & 1.8668   & 80\%  \\
	lstm2                & 100            & 500         & 0.5          & 1.8830   & 80\%  \\
	lstm2                & 100            & 500         & 0.2          & 1.8861   & 80\%  \\
	lstm2                & 100            & 500         & 0.6          & 1.9672   & 60\%  \\\midrule
	lstm3                & 100            & 500         & 0.3          & 1.8723   & 80\%  \\
	lstm3                & 100            & 500         & 0.5          & 1.8924   & 80\%  \\
	lstm3                & 100            & 500         & 0.2          & 1.9035   & 60\%  \\
	lstm3                & 100            & 500         & 0.4          & 1.9099   & 60\%  \\
	lstm3                & 100            & 500         & 0.6          & 1.9784   & 60\%  \\\midrule
	tcn                  & 100            & 512         & 0.5          & 1.8067   & 80\%  \\
	tcn                  & 100            & 128         & 0.2          & 1.8217   & 80\%  \\
	tcn                  & 100            & 512         & 0.2          & 1.8362   & 80\%  \\
	tcn                  & 100            & 256         & 0.2          & 1.8472   & 80\%  \\
	tcn                  & 100            & 256         & 0.5          & 1.8505   & 80\%  \\
	tcn                  & 100            & 128         & 0.5          & 1.9228   & 60\%  \\\midrule
	tcn\_embed           & 100            & 256         & 0.2          & 1.8105   & 80\%  \\
	tcn\_embed           & 100            & 256         & 0.5          & 1.8287   & 80\%  \\
	tcn\_embed           & 100            & 128         & 0.5          & 1.8428   & 80\%  \\
	tcn\_embed           & 100            & 128         & 0.2          & 1.8497   & 80\%  \\\midrule
	tcn\_lstm            & 100            & 512         & 0.5          & 1.7346   & 90\%  \\
	tcn\_lstm            & 100            & 256         & 0.2          & 1.7385   & 90\%  \\
	tcn\_lstm            & 100            & 256         & 0.5          & 1.7424   & 90\%  \\
	tcn\_lstm            & 100            & 512         & 0.2          & 1.7569   & 90\%  \\
	tcn\_lstm            & 100            & 128         & 0.2          & 1.7640   & 90\%  \\
	tcn\_lstm            & 100            & 128         & 0.5          & 1.7677   & 90\%  \\\midrule
	tcn\_lstm\_embed     & 256            & 256         & 0.5          & 1.7036   & 100\% \\
	tcn\_lstm\_embed     & 100            & 256         & 0.5          & 1.7130   & 100\% \\
	tcn\_lstm\_embed     & 100            & 512         & 0.5          & 1.7365   & 90\%  \\
	tcn\_lstm\_embed     & 128            & 256         & 0.5          & 1.7439   & 90\%  \\
	tcn\_lstm\_embed     & 100            & 128         & 0.2          & 1.7516   & 90\%  \\
	tcn\_lstm\_embed     & 100            & 256         & 0.2          & 1.7563   & 90\%  \\
	tcn\_lstm\_embed     & 100            & 512         & 0.2          & 1.7861   & 80\%  \\
	tcn\_lstm\_embed     & 100            & 128         & 0.5          & 1.7952   & 80\%  \\\midrule
	tcn\_lstm12\_embed   & 256            & 256         & 0.2          & 1.6785   & 100\% \\
	tcn\_lstm12\_embed   & 128            & 256         & 0.5          & 1.6844   & 100\% \\
	tcn\_lstm12\_embed   & 256            & 128         & 0.5          & 1.7210   & 100\% \\
	tcn\_lstm12\_embed   & 256            & 256         & 0.5          & 1.7244   & 100\% \\
	tcn\_lstm12\_embed   & 128            & 128         & 0.2          & 1.7262   & 100\% \\
	tcn\_lstm12\_embed   & 128            & 128         & 0.5          & 1.7305   & 100\% \\
	tcn\_lstm12\_embed   & 128            & 256         & 0.2          & 1.7557   & 90\%  \\
	tcn\_lstm12\_embed   & 256            & 128         & 0.2          & 1.7659   & 90\%  \\\midrule
	tcn\_lstm2\_embed    & 256            & 256         & 0.5          & 1.7322   & 100\% \\
	tcn\_lstm2\_embed    & 128            & 256         & 0.5          & 1.7373   & 90\%  \\
	tcn\_lstm2\_embed    & 256            & 128         & 0.5          & 1.7405   & 90\%  \\
	tcn\_lstm2\_embed    & 128            & 128         & 0.2          & 1.7614   & 90\%  \\
	tcn\_lstm2\_embed    & 256            & 128         & 0.2          & 1.7726   & 90\%  \\
	tcn\_lstm2\_embed    & 128            & 256         & 0.2          & 1.7785   & 90\%  \\
	tcn\_lstm2\_embed    & 256            & 256         & 0.2          & 1.7840   & 80\%  \\
	tcn\_lstm2\_embed    & 128            & 128         & 0.5          & 1.7926   & 80\%  \\\midrule
	tcn12\_lstm12\_embed & 128            & 256         & 0.5          & 1.7154   & 100\% \\
	tcn12\_lstm12\_embed & 128            & 256         & 0.2          & 1.7304   & 100\% \\
	tcn12\_lstm12\_embed & 128            & 128         & 0.2          & 1.7561   & 90\%  \\
	tcn12\_lstm12\_embed & 256            & 256         & 0.5          & 1.6899   & 100\% \\
	tcn12\_lstm12\_embed & 256            & 128         & 0.2          & 1.744    & 90\%  \\
	tcn12\_lstm12\_embed & 256            & 256         & 0.2          & 1.7633   & 90\%  \\
	tcn12\_lstm12\_embed & 256            & 128         & 0.5          & 1.7322   & 100\% \\
	tcn12\_lstm12\_embed & 128            & 128         & 0.5          & 1.749    & 90\%  \\* \bottomrule
	\caption{Selected results trained with skeleton code. All models listed are trained with 25 epochs (may be early stopped, usually stops within 10 epochs) and batch size 32.}
	\label{tb:p1-results}                                                                 \\
\end{longtable}

\noindent Some results we can see from Table~\ref{tb:p1-results} are as follows.
\begin{itemize}
	\item For all pure LSTM models, the more the layers, the worse it generally performs.
	\item Pure TCN models outperform most of the LSTM models (except a few \texttt{lstm} ones).
	\item Pure TCN/LSTM models with concatenating Embedding generally performs better than those without. This is similar to what we see with ResNet.
	\item Concatenating all TCN, LSTM and Embedding works the best among all models. \texttt{tcn\_lstm\_embed} hits 100\% score constantly.
\end{itemize}

Even I have reached 100\% multiple times, I still wish to achieve the bonus score for validation set. Hence, I started to modify the provided skeleton code for generating data.

\subsection{Training with Altered Code}
After reading through the skeleton code, I believe this is how it works for sampling training/validation data.
\begin{enumerate}
	\item Converts all words into IDs.
	\item Fixes the input temporal dimension as \(10\).
	\item Concatenates next sentences to fill in the remaining space if current sentence is too short, or break the current sentence into two if it is too long.
\end{enumerate}

One of the advantages of RNN and TCN is they support varying temporal dimension. With step 3 above, each input sentence is longer a complete sentence since either some of them are truncated, or some unrelated sentences are mixed in with them. This could affect the performance of the model since the models are looking for contexts through the whole sentence, which now the words can be unrelated. Hence, I modified the code and now it works as follows.
\begin{enumerate}
	\item Converts all words into IDs.
	\item Gets a batch of sentences and pad \(0\) to shorter sentences such that all sentences in the batch has the same temporal dimension during training.
\end{enumerate}

After the modification, the performance of the models improved by a lot. This implementation is not the best though, because ID \(0\) is given to word \texttt{L0127}, meaning in the perspective of the model, those words become \texttt{L0127}. However, this is still acceptable because the model eventually learns to neglect this word towards the end of a sentence. From the table of selected results below, we can see an improvement in performance comparing with the previous table.
\begin{longtable}[c]{@{}>{\ttfamily}ccc@{}}
	\toprule
	\textrm{Model Type}  & Log Loss & Score \\*
	\midrule
	\endfirsthead
	%
	\endhead
	%
	\bottomrule
	\endfoot
	%
	\endlastfoot
	%
	tcn\_lstm\_embed     & 1.6699   & Bonus \\\midrule
	tcn\_embed           & 1.7355   & 90\%  \\\midrule
	lstm\_embed          & 1.7165   & 100\% \\\midrule
	tcn\_lstm12\_embed   & 1.6131   & Bonus \\\midrule
	tcn12\_lstm12\_embed & 1.6053   & Bonus \\*\bottomrule
	\caption{Selected empirical results trained with altered code. All models listed are trained with 1 epoch, batch size 32, embedding size 128, hidden size 256 and dropout rate 0.2.}
	\label{tb:p2-1epoch-results}
\end{longtable}
\begin{longtable}[c]{@{}>{\ttfamily}cccccc@{}}
	\toprule
	\textrm{Model Type}  & Embedding Size & Hidden Size & Dropout Rate & Log Loss & Score \\*\midrule
	\endhead
	%
	\bottomrule
	\endfoot
	%
	\endlastfoot
	%
	lstm\_embed          & 256            & 256         & 0.2          & 1.4165   & Bonus \\
	lstm\_embed          & 256            & 128         & 0.2          & 1.4395   & Bonus \\
	lstm\_embed          & 128            & 128         & 0.2          & 1.4457   & Bonus \\
	lstm\_embed          & 128            & 256         & 0.2          & 1.422    & Bonus \\
	lstm\_embed          & 128            & 128         & 0.5          & 1.4729   & Bonus \\
	lstm\_embed          & 256            & 128         & 0.5          & 1.4641   & Bonus \\
	lstm\_embed          & 256            & 256         & 0.5          & 1.4173   & Bonus \\
	lstm\_embed          & 128            & 256         & 0.5          & 1.4414   & Bonus \\\midrule
	tcn\_embed           & 256            & 256         & 0.2          & 1.5162   & Bonus \\
	tcn\_embed           & 128            & 256         & 0.2          & 1.5249   & Bonus \\
	tcn\_embed           & 256            & 128         & 0.2          & 1.5414   & Bonus \\
	tcn\_embed           & 256            & 128         & 0.5          & 1.5772   & Bonus \\
	tcn\_embed           & 128            & 128         & 0.2          & 1.5507   & Bonus \\
	tcn\_embed           & 128            & 128         & 0.5          & 1.5785   & Bonus \\
	tcn\_embed           & 128            & 256         & 0.5          & 1.5334   & Bonus \\
	tcn\_embed           & 256            & 256         & 0.5          & 1.5479   & Bonus \\\midrule
	tcn\_lstm\_embed     & 128            & 128         & 0.5          & 1.4297   & Bonus \\
	tcn\_lstm\_embed     & 128            & 128         & 0.2          & 1.4231   & Bonus \\
	tcn\_lstm\_embed     & 128            & 256         & 0.5          & 1.4296   & Bonus \\
	tcn\_lstm\_embed     & 256            & 128         & 0.2          & 1.426    & Bonus \\
	tcn\_lstm\_embed     & 256            & 256         & 0.2          & 1.4218   & Bonus \\
	tcn\_lstm\_embed     & 128            & 256         & 0.2          & 1.4596   & Bonus \\
	tcn\_lstm\_embed     & 256            & 128         & 0.5          & 1.4223   & Bonus \\
	tcn\_lstm\_embed     & 256            & 256         & 0.5          & 1.4027   & Bonus \\\midrule
	tcn\_lstm12\_embed   & 256            & 128         & 0.2          & 1.4032   & Bonus \\
	tcn\_lstm12\_embed   & 128            & 128         & 0.2          & 1.4325   & Bonus \\
	tcn\_lstm12\_embed   & 256            & 256         & 0.2          & 1.3916   & Bonus \\
	tcn\_lstm12\_embed   & 128            & 256         & 0.2          & 1.4308   & Bonus \\
	tcn\_lstm12\_embed   & 256            & 128         & 0.5          & 1.4152   & Bonus \\
	tcn\_lstm12\_embed   & 128            & 256         & 0.5          & 1.4038   & Bonus \\
	tcn\_lstm12\_embed   & 256            & 256         & 0.5          & 1.3968   & Bonus \\
	tcn\_lstm12\_embed   & 128            & 128         & 0.5          & 1.4262   & Bonus \\\midrule
	tcn12\_lstm12\_embed & 256            & 128         & 0.2          & 1.4235   & Bonus \\
	tcn12\_lstm12\_embed & 128            & 128         & 0.2          & 1.4557   & Bonus \\
	tcn12\_lstm12\_embed & 128            & 256         & 0.2          & 1.4447   & Bonus \\
	tcn12\_lstm12\_embed & 256            & 256         & 0.2          & 1.4123   & Bonus \\
	tcn12\_lstm12\_embed & 256            & 128         & 0.5          & 1.431    & Bonus \\
	tcn12\_lstm12\_embed & 128            & 256         & 0.5          & 1.4218   & Bonus \\
	tcn12\_lstm12\_embed & 128            & 128         & 0.5          & 1.4229   & Bonus \\
	tcn12\_lstm12\_embed & 256            & 256         & 0.5          & 1.3963   & Bonus \\* \bottomrule
	\caption{Selected results trained with altered code. All models listed are trained with 100 epochs (may be early stopped, usaully stops within 10 epochs) and batch size 32.}
	\label{tb:ph2-results}
\end{longtable}
\noindent From Tables~\ref{tb:p2-1epoch-results}~and~\ref{tb:ph2-results}, we may find these observations.
\begin{itemize}
	\item All architectures performed significantly better even when trained with 1 epoch only. Some of them even got to bonus already.
	\item \texttt{lstm\_embed} outperforms \texttt{tcn\_embed} despite it is shown TCN are better. This could be because our task does not require a long memory so the advantages of TCN are somewhat lost.
	\item Combining LSTM and TCN together are better than pure LSTM/TCN.
	\item The best model seems to be \texttt{tcn\_lstm12\_embed} with embedding size 256, hidden size 256 and dropout 0.2. It would be best to check this by running the same model setups multiple times, but this is not performed due to the lack of time.
\end{itemize}
After these evaluations, \texttt{tcn\_lstm12\_embed} with embedding size 256, hidden size 256 and dropout 0.2 model setup is used for the inference of the test data.

\section{Implementations}
In this project, the following libraries are used.
\begin{center}
	\begin{tabular}{ccl}
		\toprule
		Name                  & Version                 & Descriptions                                   \\\midrule
		\texttt{Python      } & \texttt{3.6.6}          & Language used                                  \\\midrule
		\texttt{numpy       } & \texttt{1.15.3}         & For handling data matrices                     \\\midrule
		\texttt{pandas      } & \texttt{0.23.4}         & For handling \texttt{csv} files                \\\midrule
		\texttt{tensorflow}   & \texttt{1.12.0}         & Deep learning library                          \\\midrule
		\texttt{keras}        & \texttt{2.2.4}          & High level API wrapper for \texttt{tensorflow} \\\midrule
		\texttt{keras-tcn}    & commit \texttt{6d71e19} & \texttt{keras} implementation of TCN           \\\bottomrule
	\end{tabular}
\end{center}
Some notes for in the implementations are:
\begin{itemize}
	\item \texttt{keras.callbacks.EarlyStopping} is used to stop training when the validation loss does not increase for \(3\) consecutive epochs.
	\item \texttt{keras.callbacks.TensorBoard} is used to visualize the models during implemen\-tation.
	\item \texttt{Python} generators are used so data do not need to be fetched to memory before training. This speeds up the training time. In particular, \texttt{model.fit\allowbreak\_generator} is used instead of \texttt{model.fit}. Unfortunately, \texttt{model.predict\_gener\-ator} does not currently support varying dimension inference so \texttt{model.predict} is still used.
\end{itemize}

\begin{center}
	\textbf{End of Report}
\end{center}

\end{document}


%% Based on a TeXnicCenter-Template by Gyorgy SZEIDL.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%------------------------------------------------------------
%
\documentclass[a4paper]{article}
%
\usepackage{etex,etoolbox}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}%
\usepackage{amssymb}%
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{url}
%\usepackage[numbers]{natbib}
\usepackage{verbatimbox}

\usepackage[margin=1in]{geometry}

\numberwithin{equation}{section}
%--------------------------------------------------------
\begin{document}
\title{Constant-Time AES}
\author{David Rufino}
\newcommand{\nn}{\nonumber}
\newcommand{\mbbF}{\mathbb{F}}
\setlength{\parindent}{0pt}
\maketitle

\section{Introduction}

It's known that a naive software implementation of the AES symmetric cipher \cite{NISTAES} is vulnerable to timing attacks \cite{bernstein2005cache}. \cite{Hamburg2009AES} presents a constant-time implementation using SSSE3 instructions. This note simply expands on the details required to implement such a routine.

\section{Background}

Recall that the AES symmetric encryption algorithm works on a  16-byte (128-bit) block, with either a 128,192 or 256-bit key size. The bytes are considered to be elements of the finite field $\mathbb{F}_{2^8}$ of order 256, with respect to the polynomial basis associated to the canonical representation

\begin{eqnarray}
\label{aespoly}
	 \mbbF_{2^8} &\simeq& \mbbF_2\left[ X \right] / \left( m(X) \right) \, , \nn \\
          m(X) &=& X^8 + X^4 + X^3 + X + 1 \, .
\end{eqnarray}

For example

	$$ 01101010 b =  0\text{x}6A = X^6 + X^5 + X^3 + X $$

Addition is defined in the obvious way, and multiplication is defined modulo the polynomial $m(X)$. In this paper we make a distinction between the $\mbbF_2$-vector space $\mbbF_{2}^8$ and the field $\mbbF_{2^8}$. In plain terms the former does not have a preferred definition of multiplication a-priori. It's only after choosing a basis for the field $\mbbF_{2^8}$ that we can identify the two.

The ``state'' of the AES algorithm is conceptually a $4 \times 4$ matrix with elements in $\mbbF_{2^8}$. The convention is that the elements are with respect to the canonical basis, and that it is stored as a $16$-byte array in which the columns are contiguous. Each round of the encryption algorithm consists of the following transformations on the state

\begin{itemize}
\item {\bf{SubBytes}}

To each element of the state apply the transformation (in the canonical basis)

   $$ z \rightarrow A z^{-1} + b $$

 where the elements $A,b$ are defined in the original spec \cite{NISTAES} and inversion is understood to take place in the finite field discussed above.

\item {\bf{ShiftRows}}

Rotate the rows of the state matrix

\item {\bf{MixColumns}}

Pre-multiply the state matrix by the following

\[
\left( \begin{array}{cccc}
2 & 3 & 1 & 1 \\
1 & 2 & 3 & 1 \\
1 & 1 & 2 & 3 \\
3  & 1 & 1 & 2
\end{array} \right)
\]

where we identify a number with its binary expansion in the polynomial basis. For example

\begin{eqnarray*}
  5 &=& 1 + X^2 \\
  10 &=& X + X^3
\end{eqnarray*}

NB. this step is omitted on the final round.

\item {\bf{AddRoundKey}}

The key expansion (see \cite{NISTAES} for details) for this round is added (i.e. XOR-ed) with the state.

\end{itemize}

The first is non-linear and so typically implemented with lookup tables. It's discussed below that in order to avoid lookup tables it's easier to work in a different basis. For efficiency reasons then, we work consistently in the tower basis (defined below). This requires a number of changes

\begin{itemize}
\item The key expansion must be converted to the tower basis
\item The input cipher text or plaintext must be converted at the start of the algorithm
\item The output cipher text or plaintext must be converted to the polynomial basis at the end of the algorithm
\item The lookup tables (for example, scalar multiplication) must be with respect to tower basis
\end{itemize}

\section{PSHUFB Instruction}

The PSHUFB (shuffle) instruction operations on two 128-bit (16-byte) registers as follows

\begin{verbatim}
unsigned char a[16],b[16],r[16];
for (i = 0; i < 16; ++i)
  r[i] = (b[i] & 0x80) ? 0 : a[b[i] & 0x0f];
\end{verbatim}

As the choice of the register $a$ is arbitrary, due to the presence of the mask on $b$ this may be viewed as an arbitrary function

	$$ \mbbF_2^4 \rightarrow \mbbF_2^8 $$

or alternatively as an appropriately restricted function

	$$ \mbbF_2^4 \oplus \mbbF_2^4 \rightarrow \mbbF_2^8 $$

where points with the most significant bit set are mapped to zero. Implementing a function

  $$\mbbF^8_{2} \rightarrow \mbbF^8_{2}$$

is in general difficult, but if it is linear (e.g. basis change) then two shuffle operations will suffice, as with the following pseudo-code

\begin{verbatim}
	out1 = pshufb(lookup_lo, and(in,0x0f0f0f0f0f0f0f0f));
	out2 = pshufb(lookup_hi, and(in,0xf0f0f0f0f0f0f0f0)>>4);
	out = xor(out1,out2)
\end{verbatim}

\section{Inversion with Permutations}

Inversion in $\mbbF_{2^8}$ is not linear (in any basis), but \cite{Hamburg2009AES} shows it is possible to reduce this operation to a linear combination of such functions with domain $\mbbF_2^4$ by considering an alternative (``tower'') representation of $\mbbF_{2^8}$

\begin{eqnarray*}
	\mbbF_{2^4} &\simeq& \mbbF_2[\zeta] / (\zeta^4 + \zeta^3 + \zeta^2 + \zeta + 1) \\
	\mbbF_{2^8} &\simeq& \mbbF_{2^4}[t] / (t^2 + t + \zeta) \, .
\end{eqnarray*}

Note in this representation every element $z \in \mbbF_{2^8}$ may be represented uniquely as

\begin{eqnarray*}
   z &=&  x t + y \bar{t} \quad x,y \in \mbbF_4
\end{eqnarray*}

where $\bar{t} = t + 1$ is the conjugate of $t$. The addition law is clear and multiplication is given by

\begin{eqnarray*}
 (x_1 t + y_1 \bar{t}) \cdot (x_2 t + y_2 \bar{t} ) &=& x_1 x_2 t + y_1 y_2 \bar{t} + \left( x_1 + y_1 \right) \left( x_2 + y_2 \right) \zeta
\end{eqnarray*}

The inverse of a general element is given by

\begin{eqnarray*}
\left( xt + y \bar{t} \right)^{-1} = \frac{yt + x \bar{t}}{xy + \left(x^2 + y^2\right)\zeta}
\end{eqnarray*}

Alternatively it is shown in \cite{Hamburg2009AES} that inversion in $\mbbF_{2^8}$ may be expressed in terms of inversions over $\mbbF_{2^4}$  with the following formula

\begin{eqnarray}
\label{nestedinversion}
\frac{1}{xt + y \bar{t}} &=& \frac{t + \zeta}{\frac{1}{1 / y  + 1/\zeta(x+y)} + x}+\frac{\bar{t} + \zeta}{\frac{1}{1 / x  + 1/\zeta(x+y)} + y}
\end{eqnarray}

provided one is careful about dividing by zero. The advantage of this formula is it does not require any explicit multiplications, which are more difficult to implement using shuffle instructions.

To implement the inversion formula \eqref{nestedinversion} consider the four maps

	$$ \psi_i : \mbbF_{2^4}  \rightarrow \mbbF_{2^8} $$

corresponding to the inversions

\begin{eqnarray*}
 & z &\rightarrow z^{-1} \\
 & z &\rightarrow \left( \zeta z \right)^{-1} \\
 & z &\rightarrow \frac{t + \zeta}{z} \\
 & z &\rightarrow \frac{\bar{t} + \zeta}{z}
\end{eqnarray*}

In practice these are implemented with respect to the basis $\{ 1, \zeta, \zeta^2, \zeta^3 \}$ for $\mbbF_{2^4}$ and the one already noted for $\mbbF_{2^8}$. Further we ensure that $\psi_i(0) = 128$ so that $\psi_j(\psi_i(0)) = 0 $ and\eqref{nestedinversion} is correct in all cases. It's clear that together with addition (XOR) this is enough to implement equation \eqref{nestedinversion}.

\section{Basis Change}

This section discusses the conversion between canonical basis and the tower basis. The simplest way is to find a root of the AES polynomial \eqref{aespoly} in the tower basis. Using this allows one to easily construct an isomorphism between the two representation. For concreteness consider the unique isomorphism generated by

	$$ X \rightarrow \left( \zeta + \zeta^3 \right) t \,. $$

In practice we are interested in the basis change from the polynomial basis to the tower basis

\begin{eqnarray}
\{ t, \zeta t, \zeta^2 t , \zeta^3 t ,  \bar{t}, \zeta \bar{t}, \zeta^2 \bar{t} , \zeta^3 \bar{t} \} \, .
\end{eqnarray}

Call this $\hat{\phi} : \mathbb{F}^8_2 \rightarrow \mathbb{F}^8_2$. Actually this function is a linear isomorphism, and so may be decomposed as follows

\begin{eqnarray*}
	 \hat{\phi}(x) &=& \hat{\phi}_{\text{lo}} (x_{\text{lo}}) \oplus \hat{\phi}_{\text{hi}} (x_{\text{hi}}) \\
	\hat{\phi}_{\text{lo}}, \hat{\phi}_{\text{hi}} &:& \mathbb{F}^4_2 \rightarrow \mathbb{F}^8_2 \, .
\end{eqnarray*}

The two functions are then in a form which may be implemented with the nstruction PSHUFB. For future reference we note the inverse of the basis change given above is generated by

\begin{eqnarray*}
t &\rightarrow& X + X^5 + X^7 \\
\bar{t} &\rightarrow& 1 + X + X^5 + X^7 \\
\zeta &\rightarrow& X^4 + X^6 \, .
\end{eqnarray*}

It may be preferable to consistently work in the ``tower'' basis to avoid converting representations on each round. In this case the state must be converted to and from the tower basis at the start and the end of the algorithm, and they key expansion must also be converted.

\section{Affine component of SubBytes}

As the affine map is linear, it may be implemented with two shuffle instructions followed by an XOR. If it is adjacent to another linear map in the algorithm then clearly the two maps may be merged for efficiency reasons. For example in the inversion algorithm described above, the final two operations are shuffles, and the linear portion of the affine map may be incorporated directly. Note that must be careful when working in the tower basis.

\section{Shift Rows}

The ShiftRows transformation and its inverse can naturally be implemented with a single shuffle instruction.

\section{Mix Columns}

Recall that the mix columns operation corresponds to multiplication by a circulant matrix over $\mbbF_{2^8}$

\[ M :=
\left( \begin{array}{cccc}
2 & 3 & 1 & 1 \\
1 & 2 & 3 & 1 \\
1 & 1 & 2 & 3 \\
3 & 1 & 1 & 2
\end{array} \right)
\]

It may be shown that the inverse operation is multiplication by the circulant matrix

\[ M^{-1} :=
\left( \begin{array}{cccc}
14 & 11 & 13 & 9 \\
9 & 14 & 11 & 13 \\
13 & 9 & 14 & 11 \\
11 & 13 & 9 & 14
\end{array} \right)
\]

Let $P$ denote the elementary circulant matrix

\[
\left( \begin{array}{cccc}
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
1 & 0 & 0 & 0
\end{array} \right)
\]

This, and its powers, are easy to implement using the shuffle instruction. Recalling that $n \times n$ matrices form a $\mbbF_{2^8}$-algebra (with elements acting point wise), then we see

\begin{eqnarray*}
 M &=& 2 I + 3 P + P^2 + P^3 \\
     &=& \left(I + P\right)\left(2I + P\right) + P^3
\end{eqnarray*}

and

\begin{eqnarray*}
 M^{-1} &=& \left(8+4+2\right)I + \left(8+2+1\right)P + \left(8+4+1\right)P^2 + \left(8+1\right)P^3 \\
    &=& \left(I + P^2\right)\left[ 9 \cdot \left(I + P\right) +4 \cdot I \right] + 2 \cdot \left(I + P\right) + I
\end{eqnarray*}

Calculation of $M$ requires one doubling, three shuffles and three additions.

Calculation of $M^{-1}$ requires three scalar multiplications, two shuffles and five additions.

Note in the canonical basis the scalar multiplications can be calculated as follows

	$$ X^k \sum_{i=0}^7 a_i X^i = \sum_{i=0}^3 a_i X^{i+k} + X^k \left( \sum_{i=4}^7 a_i X^i \right) \quad k \leq 4 $$

The first term is a left shift and the final term may be calculated by a lookup table as with the following pseudocode.

\begin{verbatim}
         mult1 = and(x,0x0f)<<k
         mult2 = pshufb(lookup_k,and(x,0xf0)>>4)
         mult  = xor(mult1,mult2)
\end{verbatim}

Working in the tower basis requires two shuffle instructions.

\bibliographystyle{alpha}
\bibliography{bibentry}

\end{document}


\chapter{Mutating algorithms} \Label{cha:mutating}

Let us now turn our attention to another class of algorithms,
viz.\ \emph{mutating} algorithms of the \cxx Standard Library \cite[\S
28.6]{cxx-17-draft}, i.e.,
algorithms that change one or more ranges.
%
In \framac, you can explicitly specify that, e.g., entries in an array
\inl{a} may be modified by a function
\inl{f},
by including the following \emph{assigns clause} into the
\inl{f}'s specification:

\begin{lstlisting}[style=acsl-block]

     assigns a[0..length-1];
\end{lstlisting} %
The expression \inl{length-1} refers to the value of \inl{length}
when \inl{f} is entered, see
\cite[\S2.3.2]{ACSLSpec}.
Below are the algorithms we will discuss in this chapter.

\begin{itemize}

\item
In order to allow for a finer control of which parts of an array,
we introduce in \S\ref{sec:unchanged} the auxiliary predicate \Unchanged.

\item \filli in \S\ref{sec:filli}
initializes each element of an array by a given fixed value.

\item \swap in \S\ref{sec:swap} exchanges two values.

\item \swapranges
in \S\ref{sec:swapranges} exchanges the contents of the arrays of equal length, element
by element.
We use this example to present ``modular verification'',
as \swapranges reuses the verified properties of \swap.

\item \copyi
in \S\ref{sec:copyi}
copies a source array to a destination array.

\item \copybackward
in \S\ref{sec:copybackward} also
copies a source array to a destination array.
This version, however, uses another separation condition than \copyi.

\item \reversecopy and \reverse
in \S\ref{sec:reversecopy} and~\S\ref{sec:reverse}, respectively,
reverse an array.
Whereas \reversecopy copies the result to a separate destination array,
the \reverse algorithm works in place.

\item \rotatecopy
in \S\ref{sec:rotatecopy}
rotates a source array by \inl{m} positions and copies the results to a
destination array.

\item \rotatei
in \S\ref{sec:rotatei}
rotates \emph{in place} a source array by \inl{m} positions.

\item \replacecopy and \replace
in \S\ref{sec:replacecopy} and~\S\ref{sec:replace}, respectively,
substitute each occurrence of a value by a given new value.
Whereas \replacecopy copies the result to a separate array,
the \replace algorithm works in place.

\item \removecopy and \remove in \S\ref{sec:removecopy}--\S\ref{sec:remove}
\emph{filter} all occurrences of a given value from an array.
Whereas \removecopy copies the result to a separate array,
the \remove algorithm works in place.
Note that we provide altogether three versions of how to specify \removecopy.
This shall help the reader to understand that finding appropriate contracts
is an iterative process and that it is usually a good idea to \emph{not} strive
for a ``complete'' contract right from the beginning.

\item \shuffle in \S\ref{sec:shuffle} randomly reorders the elements of an array
thereby relying on the simple random number generator \randomnumber in
\S\ref{sec:randomnumber}.

\end{itemize}

\clearpage

\input{mutating/unchanged}
\input{mutating/fill}
\input{mutating/swap}
\input{mutating/swap_ranges}
\input{mutating/copy}
\input{mutating/copy_backward}
\input{mutating/reverse_copy}
\input{mutating/reverse}
\input{mutating/rotate_copy}
\input{mutating/rotate}
\input{mutating/replace_copy}
\input{mutating/replace}
\input{mutating/remove_copy}
\input{mutating/remove_copy2}
\input{mutating/remove_copy3}
\input{mutating/remove}
\input{mutating/shuffle}
\input{mutating/random_number}


\documentclass{article}
\usepackage{tocloft}
\include{common_symbols_and_format}
\renewcommand{\cfttoctitlefont}{\Large\bfseries}

\begin{document}
\logo
\rulename{Moving Average Convergence Divergence} %Argument is name of rule
\tblofcontents

\ruledescription{Moving average convergence divergence (MACD) is a trend-following momentum indicator that
shows the relationship between two moving averages of a security’s price.
The MACD is calculated by subtracting the 26-period exponential moving average (EMA) from the 12-period EMA.
}

\howtotrade
{The strategy is to identify bullish and bearish crossovers.
Bullish crossover - when MACD line cross above the signal line
Bearish Crossover - when signal line cross above the MACD line.
}

\ruleparameters
{Short term look back Length}{12}{Short term look back length used to compute EMA.}{$\lookbacklength_{s}$}
{Long term look back Length}{26}{Long term look back length used to compute EMA.}{$\lookbacklength_{l}$}
{Signal look back Length}{9}{Look back length used to generate Signal line.}{$S_{l}$}
\stoptable

\newpage
\section{Equation}
Below are the equations which govern how this specific trading rule calculates a trading position.

\begin{equation}
MACD = EMA(\lookbacklength_{s}) - EMA(\lookbacklength_{l})
\end{equation}
\\
\begin{equation}
Signal = EMA(S_{l})
\end{equation}
\\ % creates some space after equation
with:

$EMA(\lookbacklength_{s})$: is the short term exponentially weighted average.

$EMA(\lookbacklength_{l})$: is the long term exponentially weighted average.

$EMA(S_{l})$: is the exponentially weighted average computed to generate signal line.

\keyterms
\furtherlinks %The footer
\end{document}


\section{Maintenance Models}
\label{sec:MaintenanceModels}

\textbf{Maintenance Models} are models designed to model maintenance and testing from a reliability perspective.
These models are designed to optimize preventive maintenance at the system level.

Two classes of models are considered here:
\begin{itemize}
	\item Operating, i.e. model \xmlAttr{type} is \xmlString{Operating}
	\item Standby, i.e. model \xmlAttr{type} is \xmlString{Standby}
\end{itemize}

The specifications of these models must be defined within a RAVEN \xmlNode{ExternalModel}. This
XML node accepts the following attributes:
\begin{itemize}
	\item \xmlAttr{name}, \xmlDesc{required string attribute}, user-defined identifier of this model.
	\nb As with other objects, this identifier can be used to reference this specific entity from other
	input blocks in the XML.
	\item \xmlAttr{subType}, \xmlDesc{required string attribute}, defines which of the subtypes should
	be used. For maintenance models, the user must use \xmlString{SR2ML.MaintenanceModel} as subtype.
\end{itemize}
In the maintenance \xmlNode{ExternalModel} input block, the following XML subnodes are required:
\begin{itemize}
	\item \xmlNode{variable}, \xmlDesc{string, required parameter}. Comma-separated list of variable
	names. Each variable name needs to match a variable used or defined in the maintenance model or variable
	coming from other RAVEN entities (i.e., Samplers, DataObjects, and Models).
	\nb For all the maintenance models, the following outputs variables would be available. If the user
	added these output variables in the node \xmlNode{variables}, these variables would be also available to
	for use anywhere in the RAVEN input to refer to the maintenance model output variables.
	\begin{itemize}
		\item \xmlString{avail}, variable that contains the calculated availability value
		\item \xmlString{unavail}, variable that contains the calculated unavailability value
	\end{itemize}
	\nb When the external model variables are defined, at run time, RAVEN initializes
	them and tracks their values during the simulation.
	\item \xmlNode{MaintenanceModel}, \xmlDesc{required parameter}. The node is used to define the maintenance
	model, and it contains the following required XML attribute:
	\begin{itemize}
		\item \xmlAttr{type}, \xmlDesc{required string attribute}, user-defined identifier of the maintenance model.
		\nb the types for different maintenance models can be found at the beginning of this section.
	\end{itemize}
\end{itemize}
In addition, if the user wants to use the \textbf{alias} system, the following XML block can be input:
\begin{itemize}
	\item \xmlNode{alias} \xmlDesc{string, optional field} specifies alias for
	any variable of interest in the input or output space for the ExternalModel.
	%
	These aliases can be used anywhere in the RAVEN input to refer to the ExternalModel
	variables.
	%
	In the body of this node, the user specifies the name of the variable that the ExternalModel is
	going to use (during its execution).
	%
	The actual alias, usable throughout the RAVEN input, is instead defined in the
	\xmlAttr{variable} attribute of this tag.
	\\The user can specify aliases for both the input and the output space. As a sanity check, RAVEN
	requires an additional required attribute \xmlAttr{type}. This attribute can be either ``input'' or ``output.''
	%
	\nb The user can specify as many aliases as needed.
	%
	\default{None}
\end{itemize}

\subsection{Operating Model}
For an operating model, the unavailability $u$ is calculated as
\begin{equation}
	u = lambda*Tr/(1.0+lambda*Tr) + Tpm/Tm
\end{equation}
where:
\begin{itemize}
  \item lambda: is the component failure rate
  \item Tr: mean time to repair
  \item Tpm: mean time to perform preventive maintenance
  \item Tm: preventive maintenance interval
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
    <ExternalModel name="PMmodelOperating" subType="SR2ML.MaintenanceModel">
      <variables>lambda,Tm,avail,unavail</variables>
      <MaintenanceModel type="PMModel">
        <type>operating</type>

24

        <Tpm>10</Tpm>
        <lambda>lambda</lambda>
        <Tm>Tm</Tm>
      </MaintenanceModel>
    </ExternalModel>
\end{lstlisting}

\subsection{Standby Model}
For an operating model, the unavailability $u$ is calculated as:
\begin{equation}
  u = rho + 0.5*lambda*Ti + Tt/Ti + (rho+lambda*Ti)*Tr/Ti + Tpm/Tm
\end{equation}
where:
\begin{itemize}
  \item rho: failure probability per demand
  \item Ti: surveillance test interval
  \item Tr: mean time to repair
  \item Tt: test duration
  \item Tpm: mean time to perform preventive maintenance
  \item Tm: preventive maintenance interval
  \item lamb: component failure rate
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
    <ExternalModel name="PMmodelStandby" subType="SR2ML.MaintenanceModel">
      <variables>lambda,Tm,Ti,avail,unavail</variables>
      <MaintenanceModel type="PMModel">
        <type>standby</type>

24

        <Tpm>10</Tpm>
        <Tt>5</Tt>
        <rho>0.01</rho>
        <lambda>lambda</lambda>
        <Ti>Ti</Ti>
        <Tm>Tm</Tm>
      </MaintenanceModel>
    </ExternalModel>
\end{lstlisting}


\section{Systems comparison}
\label{sec:sys-cmp}
Nowadays, we can find several products on the market, developed by several companies around the world. Many of them are patented to protect the invention itself, some others are protected by corporate secrets, the remaining are available as academic publications. Commercial cameras and lasers are sufficiently powerful and accurate that the hardware could be considered negligible when we want to compare these systems. What really characterises each of them is the software, i.e. the mathematical model used to extract the wheel profile and to approximate the diameter. \\

In the following of this section, we will present some mathematical models used to evaluate the diameter of the wheels, starting from the rolling points determined by the wheels profiles. To avoid infringing patents, we will present the models without ever mentioning who the owners are. However, some of the corporations that are analysed are: % Beena Vision\footnote{ Corporation available at \url{http://www.beenavision.com/}}, Danobat\footnote{ Corporation available at \url{https://www.danobatgroup.com/en/danobat}}, Graw\footnote{ Corporation available at \url{http://www.graw.com/}}, IEM\footnote{ Corporation available at \url{http://www.iem.net/}}, KLD Labs\footnote{ Corporation available at \url{http://www.kldlabs.com/}}, MERMEC\footnote{ Corporation available at \url{http://www.mermecgroup.com/}} and MRX\footnote{ Corporation available at \url{http://www.mrxtech.com.au/}}.
  \begin{itemize}
    \item Beena Vision\footnote{\url{http://www.beenavision.com/}}
    \item Danobat\footnote{\url{https://www.danobatgroup.com/en/danobat}}
    \item Graw\footnote{\url{http://www.graw.com/en/}}
    \item IEM\footnote{\url{http://www.iem.net/}}
    \item KLD Labs\footnote{\url{http://www.kldlabs.com/}}
    \item MER MEC\footnote{\url{http://www.mermecgroup.com/}}
    \item MRX\footnote{\url{http://www.mrxtech.com.au/}}
  \end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{System \#1} % BeenaVision
The first considered system uses an approach a bit different from the others. It is based on three laser-camera pairs: two are used to reconstruct the section of the wheel, while the third takes a section of the wheel parallel to its sides (perpendicular to the wheelset axis). The two sections visible from the outer side are shown in Figure \ref{fig:cmp-sys1}. In this way, line 28 allows to collect a number of points greater than three, as suggested in \cite{wpms-giuseppe}. To simplify the fitting of this last set of point on the wheel, its section is modelled as a cylinder of equation:
  \begin{equation}
    \left( y - y_0 \right) + \left( z - z_0 \right) = r^2
    \label{eq:cylinder}
  \end{equation}
instead as a cone, but this requires some corrections in order to get the exact wheel diameter. Equation \ref{eq:cylinder} is the same as a circle, whose axis passes through the center point $\left( y_0, z_0 \right)$. This equation can be solved using only three points, but it is more precise if the number of used points is greater.
  \begin{figure}[t!]
    \centering
    \includegraphics[width=0.6\textwidth]{./images/wpms/lasers1.png}
    \caption{System \#1, outer side collected sections}
    \label{fig:cmp-sys1}
  \end{figure}
Lasers lines are extracted from the (simultaneous) acquired images, after a preprocessing. This data elaboration consists of image resizing, laser spots detection, image equalization using threshold filters and rejection of noisy points. In particular, the detection of the laser spots is performed using edge-detection algorithms instead of subpixel filters; in this case the subpixel precision is guaranteed by the image resizing. Note that line 28 generally is not perpendicular to the axis of the wheel, because of the inclination of the same with respect to the rail. This situation introduces an error when we compute the diameter. Thus, the profile has to be projected in a plane perpendicular to the axis of the wheel, before compute the Equation \ref{eq:cylinder}. This correction is called \textit{radial compensation}. \\
The presentations of the commercial products of this corporation offer a precision greater of $96\%$ for each measure of interest (diameter, flange width and height, \ldots).

%%%
% If it is the case to do that, I've some other notes about BeenaVision
%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{System \#2} % Danobat
Differently from the previous system, this one proposes a method that can be used with many different structured light projectors, even if all proposed solutions are focused on laser stripes. This proposal basically uses at least two laser-camera pair in order to reconstruct the entire profile of the wheel, and solve the occlusion problem. \\
Once the laser spots are detected from the two different acquisitions, they are converted in a common 3D reference system, where $X$ is the longitudinal direction, $Y$ is the transverse direction and $Z$ the vertical direction. Converted data are then corrected, by aligning the profile with the ideal reference system: this corrects distortions due to the wheel inclination with respect to its axis (angle $\alpha$ and $\beta$, as shown in Figure \ref{fig:cmp-sys2}).
  \begin{figure}[t!]
    \centering
    \includegraphics[width=0.7\textwidth]{./images/wpms/wheel-rotation.png}
    \caption{System \#2, wheel rotations with respect to ideal axis. $R$ is the ideal rotation axis, $1$ is the wheel and $2$ the rail.}
    \label{fig:cmp-sys2}
  \end{figure}
This is performed by correcting the rotation of the tensors parallel to the inner and outer side, with respect to the ideal ones. Furthermore this correction allows to analyse the profile in a well known position, regardless of how the profile is acquired by the camera (remember that the train is running on the sensor). At this point, for each detected laser spot, from the flange to the wheel contact surface, a radius is computed. Radius is understood as the distance between a point of light reflected on the section of the wheel and a transverse height $Y$ of the axis $R$ of the wheel, and it is computed using the equation:
  \begin{equation*}
    Radius = \sqrt{x^2 + z^2}
  \end{equation*}
where $x$ and $z$ are respectively the longitudinal and the transverse heights of each point of light of the laser line.
When all the parameters of interest are rough estimated, a Gauss-Newton algorithm is used in order to minimize the error on the computed radii, and in this way to refine the angles $\left( \alpha, \beta \right)$ and the position and orientation of each profile. At the end, the measures of interests are computed. The presentations of the commercial products of this corporation offer a precision around of $0.2 \, mm$ for measures concerning profile of the wheel (flange height and thickness, qR factor, \ldots) and a precision around of $1 \, mm$ for the diameter.

%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{System \#3} % IEM
Like the system \#2, also this approach is thought to be used both with laser beams and with structured light. Even the order with which the operations are performed is about the same:
  \begin{itemize}
    \item detection of the laser stripe;
    \item data conversion into the 3D $\left(XYZ\right)$ reference system and alignment to the ideal orientation;
    \item interpolation of the raw data using standard shape fitting algorithms;
    \item evaluation of the measures of interests.
  \end{itemize}
What distinguishes this system from others, is the approach used to detect the laser spot and to reconstruct the profile of the wheel. In fact, it uses standard geometric fitting algorithms to find each part that dial the profile, as flange, rim, wheel inner and outer sides, \ldots Once the entire profile is built from each acquired image (generally, one for each laser-camera pair used), the diameter of the wheel is determined using a standard circle fitting algorithm\footnote{Not specified in the patent} that allows to approximate the rolling circle. \\

Unfortunately, in the website of the manufacturer we have found only commercial description about the products, but no information about their precision, or accuracy. However, these informations could be meaningless. This corporation is the owner of many patents regarding laser-triangulation systems, each one of them proposes a different approach to the problem. For example, some of them are based on expert system or on neural network. Furthermore, different sensors are suggested, such as electromagnetic or acoustic devices. \\
All the patents analysed described a complete system, but avoid to study in details the algorithms used to process collected data, thus we can only speculate on how proposed systems really work.

%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{System \#4} % Mermec
Like the previous systems, also this last one is based on, at least, a couple of a laser-camera pair, that allow to reconstruct the entire profile of the wheel, similarly as shown in Figure \ref{fig:cmp-sys4}.
The laser beams are collected by the three cameras, and the spots are located using sup-pixel approximation algorithms, that increase the accuracy of the laser estimation, starting from the acquired images. Thus, the entire profile is rebuilt merging the inner and outer laser lines, and aligned to the ideal orientation. At this point, some keypoints are determined in order to evaluate the measures of interest. \\

One of the most meaningful points, is the rolling point: in fact it allows to estimate the diameter of the wheel. As in the previous case, this operation requires at least three points, taken from at least three synchronous acquisitions. Also in this case, a radial compensation is needed, in order to reduce the measurement error; if the system provides many triangulation groups, it is possible to estimate different diameters, and then average along this values. Note that, in this way it is possible to approximate the wheel circle, but it is not possible to consider wheel ovalization.
% Concerning the estimation of the diameter, given at least three different rolling points, belonging to the rebuilt profiles and taken in different positions, the Erone's formula is used:
%  \begin{equation*}
%    D = \frac{2\cdot a\cdot b\cdot c}{\sqrt{(a+b+c)(-a+b+c)(a-b+c)(a+b-c)}}
%  \end{equation*}
%where $a$, $b$ and $c$ are the sides of the triangle with vertexes the three point above. In this way it is possible to approximate the wheel circle, but it is not possible to consider wheel ovalizations.
  \begin{figure}[t!]
    \centering
    \begin{minipage}[c]{.49\textwidth}
      \centering
      %\includegraphics[width=\textwidth]{./images/wpms/mm-wpms.jpg}
      \includegraphics[width=0.8\textwidth]{./images/wpms/test2_cut.jpg}
    \end{minipage}%
    \hfill
    \begin{minipage}[c]{.49\textwidth}
      \centering
      \includegraphics[width=\textwidth]{./images/wpms/laser_pts.png}
    \end{minipage}

    \caption{System \#4, example of system configuration.}
    \label{fig:cmp-sys4}
  \end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{System \#5} % \cite{wpms-giuseppe}
This last system proposes an alternative process to extract the laser stripe. Instead of improving the accuracy of the peak detection working on sub-pixel approximation, it uses an edge detector presented in \cite{chen2013efficient} and \cite{659930}. In this way, it is possible to detect the maximum of the second derivative of the grey level perpendicular to the laser stripe. Thus, it is possible to improve the peak detection even when the beam is not perpendicular to the pixel direction in the sensor, specially when the laser line bends (e.g. in the flange side). The system boast of reaching the precision of $0.1$ pieces of pixels, regardless of the variations in the width or light intensity (i.e. grey values) of the beam. Furthermore, the approach is more robust with respect to noise.

In order to correctly determine the diameter of the wheel, the system needs to rotate the profile, so that the rim plane segment can be parallel to the y-axis of the 2D coordinate frame on the laser plane. Hence, three rim plane segments can be determined, and using the 3D coordinates in the world reference system, it is possible to obtain the equation of the wheel rim plane by fitting all of the rim plane segments:
  \begin{equation}
    \pi_rim : a_rx + b_ry + c_rz + d_r = 0
    \label{eq:sys5-plane}
  \end{equation}
At the end, the diameter can be determined by projecting the flange vertexes and at least two contact points on the plane described in the Equation \ref{eq:sys5-plane}. Each of the point $p$ is projected in the rim plane accordingly with:
  \begin{equation*}
    p' = p + t\cdot\frac{N^T}{||N||}
  \end{equation*}
where $N = \begin{bmatrix} a_r, b_r, c_r \end{bmatrix}$, and $t$ is the distance from $p$ to the wheel rim plane. Thus, a maximum likelihood criterion is used against the noise, so as the approximation of the center of the wheel is improved, and a non-linear optimization method (e.g. Levenberg–Marquardt) can be performed to solve the problem. \\
The Levenberg–Marquardt algorithm (\acs{LMA} from the names of its inventors) is used to solve generic curve-fitting problems, and it always finds a local minima at least. This algorithm is slower that the Gauss-Newton one, but it is more precise. It is based on a least-squares method: given a set of $m$ empirical datum pairs $\left( x_i, y-i \right)$ of independent and dependent variables, find the parameters $\beta$ of the model curve $f(x,\beta)$ so that the sum of the squares of the deviations is minimized. \\

%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Conclusion to the chapter
~\\

As we can see, the proposed systems are less than the number of the corporations analysed. As just mentioned before, many companies prefer to protect their products with corporation secrets instead using patents. Furthermore, many of the patents found are related to different systems or approaches (for example regarding static systems). These choices prevented us to comment all the products on the market. However, the systems discussed above, and summarised in Table \ref{tab:wpms:summaries}, are a good sample of the possible solutions for measuring the world using laser triangulation-based systems.
  \input{./src/chapters/ch3-WPMS/tab-summary.tex}
%  \begin{table}
%    \includegraphics[width=\textwidth]{./images/wpms/tab.PNG}
%    \caption{Summery of the systems above.}
%    \label{tab:wpms:summaries}
%  \end{table}


\input{def.tex}

\DeclareMathOperator{\supp}{supp}

\title{Solutions to \\ \textit{Introduction to the Theory of Distirbutions}}
\author{Yunwei Ren}
\date{}

\begin{document}
\maketitle
\tableofcontents

\vspace{1cm}

\section{Test Functions and Distributions}
\paragraph{1.2}
\begin{proof}
  It suffices to show that $f \equiv 0$ on an open set $O$ iff the restriction
  of the distribution $\langle f, \cdot\rangle$ onto $O$ is the zero
  distribution. Suppose that $\langle f, \cdot\rangle|_O \equiv 0$ since the
  other direction is obvious. Assume, to obtain a contradiction, that
  $f(x) > 0$ for some $x \in O$. Since $f$ is continuous, there is an open
  neighborhood $U \subset O$ s.t. $f > \vep$ on $U$ for some $\vep > 0$. Choose
  a small closed ball $B \subset U$ centered at $x$ and let $\psi$ be the
  cutoff function with $\supp\psi \subset U$, $0 \le \psi \le 1$ and
  $\psi \equiv 1$ on $B$. Then
  \[
    0 = \langle f, \psi\rangle = \int_U f\psi > \vep\mu(B) > 0,
  \]
  where $\mu(B)$ is the measure of $B$. Contradiction. Thus, $f \le 0$.
  Similarly, we can show $f \ge 0$. Therefore, $f \equiv 0$ on $O$.

  The result is not true for $f \in L_1^{\mrm{loc}}(\mathbb{R}^n)$ in general
  since add a function which is zero a.e. to $f$ does not change the
  distribution but will change the support of $f$.
\end{proof}

\paragraph{1.5}
\begin{proof}
  For every compact $K \subset (0, \infty)$, there is an integer $N$ s.t.
  $1/k \notin K$ for all $k > N$. Hence, for every $\phi \in C^\infty_c(0,
  \infty)$ with $\supp \phi \subset K$,
  \[
    |\langle u, \phi\rangle|
    = \left|\sum_{k=0}^N \partial^k \phi(1/k)\right|
    \le \sum_{k=0}^N \sup|\partial^k \phi|.
  \]
  Thus, $u$ is a distribution on $(0, \infty)$.

  Assume, to obtain a contradiction, that $u = v|_{(0, \infty)}$ for some
  $v \in \mscr{D}\hp(\R)$. Let $f \in C^\infty_c(\R)$ be a cutoff function
  with $f \equiv 1$ on $[-1, 1]$. Then, the distribution $fu$ (cf. Sec. 2.5)
  is of infinite order since its restriction to $1/m$ is $\delta^{(m)}$ for
  every positive integer $m$. However, since $fu$ is compactly supported, it
  must have a finite order (cf. Sec. 3.1). Contradiction.
\end{proof}

\paragraph{1.6}
\begin{proof}
  It follows immediately from the Riesz-Markov theorem.
\end{proof}

\paragraph{1.7}
  I am not sure whether the second part can be proved since if we put
  $f_\vep \equiv 0$ for some $\vep \in (0, 1)$, the asymptotic behavior will
  not change.
\begin{proof}
  It suffices to show $\int f_\vep\phi \to \phi(0)$ as $\vep\to 0$. Let
  $B_\vep = \{|x| \le \vep\}$. We have
  \begin{align*}
    \left|\int f_\vep \phi - \phi(0)\right|
    &= \left|\int_{B_\vep} f_\vep (\phi - \phi(0)) \right| \\
    &\le \sup_{x \in B_\vep}|\phi(x) - \phi(0)| \int|f_\vep| \\
    &\le \mu\sup_{x \in B_\vep}|\phi(x) - \phi(0)| .
  \end{align*}
  Since $\phi\in C^\infty(\R^n)$, $\sup_{x\in B_\vep}|\phi(x) - \phi(0)|
  \to 0$ as $\vep \to 0$. Thus, $f_\vep \to \delta$ in $\mscr{D}\hp(\R^n)$.
\end{proof}

\paragraph{1.9}
\begin{proof}
  Let $u_n(x) := \sum_{k=-n}^n c_ke^{ikx}$. For every $\phi \in
  C^\infty_c(\R)$, by repeatedly using integration by parts, we have
  \begin{align*}
    \langle u_n, \phi\rangle
    &= \int\sum_{k=-n}^n c_k e^{ikx}\phi(x)\rd x \\
    &= \sum_{k=-n}^n c_k \int e^{ikx}\phi(x)\rd x \\
    &= \sum_{k=-n}^n c_k \left(\frac{-1}{ik}\right)^{m+2}
      \int e^{ikx}\partial^{m+2}\phi(x)\rd x. \\
  \end{align*}
  Note that $c_k \left(\frac{-1}{ik}\right)^{m+2} \le O(1/k^2)$ and the
  $\int e^{ikx}\partial^{m+2}\phi(x)\rd x$ is bounded. Thus, $\lim
  \langle u_n, \phi\rangle$ converges for every $\phi$, whence $u$ converges
  in $\mscr{D}\hp(\R)$.
\end{proof}

\end{document}


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{amsmath}

\usepackage[hmargin=3cm,vmargin=6.0cm]{geometry}
%\topmargin=0cm
\topmargin=-2cm
\addtolength{\textheight}{6.5cm}
\addtolength{\textwidth}{2.0cm}
%\setlength{\leftmargin}{-5cm}
\setlength{\oddsidemargin}{0.0cm}
\setlength{\evensidemargin}{0.0cm}

%misc libraries goes here
%\usepackage{fitch}

\begin{document}

\section*{Student Information }
%Write your full name and id number between the colon and newline
%Put one empty space character after colon and before newline
Full Name : Zeynep Özalp \\
Id Number : 2237691 \\

% Write your answers below the section tags
\section*{Answer 1}
\subsection*{1.1}

\begin{table}[H]
\small
\centering
\caption{ a) Tautology }
\label{table:example}
\begin{tabular}
{|c|c|c|c|c|c|c|c|c|}	%% specify column number and vertical lines
\hline 							%% line draw
\textbf{p} & \textbf{q} & \textbf{r} & \textbf{$\neg$ r} & \textbf{p $\rightarrow$ q} & \textbf{p $\wedge$ $\neg$ r} & \textbf{(p $\rightarrow$ q) $\leftrightarrow$ (p $\wedge$ $\neg$ r)} & \textbf{$\neg$ (q $\wedge$ r)}  & \textbf{RESULT}\\
\hline
T & T & T & F & T & F & F & F & T \\			%% separate columns by &
T & T & F & T & T & T & T & T & T \\
T & F & T & F & F & F & T & T & T \\
T & F & F & T & F & T & F & T & T \\
F & T & T & F & T & F & F & F & T \\
F & T & F & T & T & F & F & T & T \\
F & F & T & F & T & F & F & T & T \\
F & F & F & T & T & F & F & T & T \\

\hline

\end{tabular}
\end{table}
\quad \textbf{RESULT:} $\textbf{((p $\rightarrow$ q) $\leftrightarrow$ (p $\wedge$ $\neg$ r))} \rightarrow \textbf{$\neg$ (p $\wedge$ r)}$

\begin{table}[H]
\small
\centering
\caption{ b) Contradiction }
\label{table:example}
\begin{tabular}
{|c|c|c|c|c|c|c|c|}	%% specify column number and vertical lines
\hline 							%% line draw
\textbf{p} & \textbf{q} & \textbf{$\neg$ p} & \textbf{p $\vee$ q} & \textbf{p $\rightarrow$ q} & \textbf{(p $\vee$ q) $\wedge$ (p $\rightarrow$ q)} & \textbf{q $\rightarrow$ $\neg$ p} & \textbf{RESULT}\\
\hline
T & T & F & T & T & T & F & F \\			%% separate columns by &
T & F & F & T & F & F & T & F \\
F & T & T & T & T & T & T & F \\
F & F & T & F & T & F & T & F \\
\hline

\end{tabular}
\end{table}
\textbf{RESULT:} \textbf{$\neg$((p $\vee$ q) $\wedge$ (p $\rightarrow$ q) $\vee$ (\textbf{q $\rightarrow$ $\neg$ p}))}

\subsection*{1.2}
\textbf{a)} This argument is \textbf{invalid}. Let $D=\{-1,1\}$ be a domain for $P(x)$ and $Q(x)$. Suppose $P(x) : x<0 $ and $Q(x) : x>0 $. Note that $P(x)$ is true for $x=-1$ and $Q(x)$ is true for $x=1$ in domain $D$. So, there exists at least one $x$ which $P(x)$ is true and there exists at least some other $x$ which $Q(x)$ is true. Note that first and second quantifiers' scopes are different on the left hand side so that one can choose different $x$ values. Thus, the argument $\exists x P(x) \wedge \exists x Q(x)$ is valid for different choices of $x$. However, since on the right hand side, one quantifier's scope is $(P(x) \wedge Q(x))$, one can choose only one $x$ for both $P(x)$ and $Q(x)$. Therefore, the argument $\exists x (P(x) \wedge Q(x))$ is invalid for $x=-1$ and $x=1$. In conclusion, the argument $\exists x P(x) \wedge \exists x Q(x) \rightarrow \exists x (P(x) \wedge Q(x))$ is invalid.\\\\
\textbf{b)} This argument is \textbf{valid}. This argument is invalid if one can prove that the left hand side is true but the right hand side is false. Choose some arbitrary constant c in the domain of $P(x)$ and suppose the left hand side is true. This means for all choices of $x$, $P(x)$ is true; moreover, $P(c)$ is true. So, there exist at least one $x$ that $P(x)$ is true. Thus, the argument $\exists x P(x)$ is true. Hence, if the left hand side is true, the right hand side must be true and the argument $\forall x P(x) \rightarrow \exists x P(x)$ is valid.

\section*{Answer 2}
\begin{table}[H]
	\begin{tabular}{*6{l}}
		$1.$ & $(\neg p \vee p) \rightarrow ((p \wedge \neg q) \rightarrow r)$ &  & Premise \\
		$2.$ & $T \rightarrow ((p \wedge \neg q) \rightarrow r)$ &  & Negation Law\\
		$3.$ & $\neg T \vee ((p \wedge \neg q) \rightarrow r)$ &  & Table 7/Line 1\\
		$4.$ & $F \vee ((p \wedge \neg q) \rightarrow r)$ &  & Negation\\
		$5.$ & $(p \wedge \neg q) \rightarrow r$ &  & Identity Law\\
		$6.$ & $\neg (p \wedge \neg q) \vee r$ &  & Table 7/Line 1\\
		$7.$ & $(\neg p \vee q) \vee r$ & & De Morgan's Law\\
		$8.$ & $\neg p \vee q \vee r$ & & Associative Law\\
		$9.$ & $q \vee r \vee \neg p$ & & Commutative Law \\
		$10.$ & $(q \vee r) \vee \neg p$ & & Associative Law \\
	\end{tabular}
\end{table}

\section*{Answer 3}
1. $\forall x (W(x) \rightarrow Has\_CS\_Degree(x))$ \\
2. $\forall x \forall y ((Phd(x) \wedge Phd(y) \wedge W(x) \wedge W(y) \wedge (x \neq y))\rightarrow Knows(x,y))$\\
3. $\forall x ((W(x) \wedge (x \neq Cenk)) \rightarrow Older(Cenk,x))$\\
4. $\forall x ((W(x) \wedge (x \neq Selen))\rightarrow Phd(x))$\\
5. $\neg (\forall x \forall y ((W(x) \wedge W(y) \wedge (x\neq y))\rightarrow Knows(x,y)))$\\
6. $(\exists x Phd(x) \wedge \exists y Phd(y) \wedge (x\neq y))\rightarrow \forall z ((z \neq x)\wedge (z \neq y)\rightarrow \neg Phd(z))$\\
7. $(\exists x \exists y \exists z ((x \neq y \neq z \neq Gizem) \wedge Older(x,Gizem) \wedge Older(y,Gizem) \wedge Older(z,Gizem)))$\\
8. $\exists x (Phd(x) \wedge W(x))\rightarrow (\forall y ((y \neq x) \rightarrow \neg(Phd(y) \wedge W(y))))$

\section*{Answer 4}
\begin{table}[H]
	\centering
	\begin{tabular}{lllllll}
		1. & & & $(p \rightarrow r) \vee (q \rightarrow r)$ & premise & & \\ \cline{3-7}
		2. & \multicolumn{1}{c|}{} & & $p \rightarrow r$ & assumed & & \multicolumn{1}{c|}{} \\ \cline{4-6}
		3. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $p \wedge q$ & assumed & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		4. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $p$ & $\wedge e, 3$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		5. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $r$ & $\rightarrow e,  2, 4$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\ \cline{4-6}
		6. & \multicolumn{1}{c|}{} & & $(p \wedge q) \rightarrow r$ & $\rightarrow i, 3-5$ & & \multicolumn{1}{c|}{} \\ \cline{3-7} \\ \cline{3-7}
		7. & \multicolumn{1}{c|}{} & & $q \rightarrow r$ & assumed & & \multicolumn{1}{c|}{} \\ \cline{4-6}
		8. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $p \wedge q$ & assumed & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		9. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $q$ & $\wedge e, 8$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		10. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $r$ & $\rightarrow e, 7, 9$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\ \cline{4-6}
		11. & \multicolumn{1}{c|}{} & & $(p \wedge q) \rightarrow r$ & $\rightarrow i, 8-10$ & & \multicolumn{1}{c|}{} \\ \cline{3-7}
		12. & & & $(p \wedge q) \rightarrow r$ & $\vee e, 1, 2-6, 7-11$ & & \\

	\end{tabular}
\end{table}

\section*{Answer 5}
\begin{table}[H]
	\centering
	\begin{tabular}{lllllll}
		1. & & & $(\neg p \vee \neg q)$ & premise & & \\ \cline{3-7}
		2. & \multicolumn{1}{c|}{} & & $p \wedge q$ & assumed & & \multicolumn{1}{c|}{} \\
		3. & \multicolumn{1}{c|}{} &  & $p$ & $\wedge e, 2$ &  & \multicolumn{1}{c|}{} \\
		4. & \multicolumn{1}{c|}{} &  & $q$ & $\wedge e, 2$ &  & \multicolumn{1}{c|}{} \\ \cline{4-6}
		5. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $\neg p$ & assumed & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		6. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $\perp$ & $\neg e, 5, 3$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		7. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $r$ & $lemma \ "\perp  \ \vdash X"$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\ \cline{4-6}
		& \multicolumn{1}{c|}{} &  &  &  &  & \multicolumn{1}{c|}{} \\ \cline{4-6}
		8. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $\neg q$ & assumed & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		9. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $\perp$ & $\neg e, 8, 4$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		10. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $r$ & $lemma \ "\perp  \ \vdash X"$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\ \cline{4-6}
		11. & \multicolumn{1}{c|}{} & & $r$ & $\vee e, 1, 5-7, 8-10$ & & \multicolumn{1}{c|}{} \\ \cline{3-7}
		12. & & & $(p \wedge q) \rightarrow r$ & $\rightarrow i, 2-11$ & & \\

	\end{tabular}
\end{table}

\textbf{Proof for $lemma \ "\perp  \ \vdash X"$ :} \\
\begin{table}[H]
	\centering
	\begin{tabular}{lllllll}
		1. & & & $\perp$ & premise & & \\ \cline{3-7}
		2. & \multicolumn{1}{c|}{} & & $\neg A$ & assumed & & \multicolumn{1}{c|}{} \\
		3. & \multicolumn{1}{c|}{} &  & $\perp$ & $copy 1$ &  & \multicolumn{1}{c|}{} \\ \cline{3-7}
		4. &  &  & $\neg \neg A$ & $\neg i, 2-3$ &  & \\
		5. &  &  & $A$ & $\neg \neg e, 4$ &  &  \\

	\end{tabular}
\end{table}

\section*{Answer 6}
\begin{table}[H]
	\centering
	\begin{tabular}{lllllll}
		1. & & & $\forall x (P(x) \rightarrow (Q(x) \rightarrow R(x)))$ & premise & & \\
		2. & & & $\exists x P(x)$ & premise & & \\
		3. & & & $\forall x (\neg R(x))$ & premise & & \\ \cline{3-7}
		4. & \multicolumn{1}{c|}{} &  & $P(c)$ & assumed &  & \multicolumn{1}{c|}{} \\
		5. & \multicolumn{1}{c|}{} & & $(P(c) \rightarrow (Q(c) \rightarrow R(c)))$ & $\forall e,1 $ &  & \multicolumn{1}{c|}{} \\
		6. & \multicolumn{1}{c|}{} &  & $Q(c) \rightarrow R(c)$ & $\rightarrow e, 5, 4$ &  & \multicolumn{1}{c|}{} \\
		7. & \multicolumn{1}{c|}{} &  & $\neg R(c)$ & $\forall e, 3$ &  & \multicolumn{1}{c|}{} \\ \cline{4-6}
		8. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $Q(c)$ & assumed & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		9. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $R(c)$ & $\rightarrow e,6,8 $ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\
		10. & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & $\perp$ & $\neg e, 9, 7$ & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} \\ \cline{4-6}
		11. & \multicolumn{1}{c|}{} & & $\neg Q(c)$ & $\neg i,8-10$ & & \multicolumn{1}{c|}{} \\
		12. & \multicolumn{1}{c|}{} & & $\exists x (\neg Q(x))$ & $\exists i,11$ & & \multicolumn{1}{c|}{} \\ \cline{3-7}
		13. & & & $\exists x (\neg Q(x))$ & $\exists e, 2,4-12$ & & \\

	\end{tabular}
\end{table}

\end{document}


\documentclass[11pt]{article}
\usepackage{setspace}
\usepackage{pxfonts}
\usepackage{graphicx}
\usepackage{geometry}

\geometry{letterpaper,left=.5in,right=.5in,top=1in,bottom=.75in,headsep=5pt,footskip=20pt}

\title{Lecture 5 -- Extensions of the Hodgkin-Huxley model}
\author{Computational Neuroscience Summer Program}
\date{June, 2011}

\begin{document}
\maketitle

\paragraph{Motivation.}  The Hodgkin-Huxley model gave us a way to explicitly model ionic currents using the expected conductances of each ion and the driving forces on those ions.  We will now go one step further by explicitly simulating the stochastic actions of individual subunits of the potassium and sodium ion channels.

\paragraph{Potassium channel review.}  The potassium channel is comprised of four identical subunits.  In the Hodgkin-Huxley model, the probability that a given channel is open is equal to $p_K = n^4$, where $n$ is the probability of a single subunit being open.  Recall that $n$ increases when the cell is depolarized and decreases when the cell is hyperpolarized.  The opening rate of each subunit is $\alpha_n$ and the closing rate is $\beta_n$:

\[
\alpha_n(V) = \frac{0.01(V + 55)}{1 - e^{-0.1(V+55)}}
\]

\[
\beta_n(V) = 0.125e^{-0.0125(V+65)}
\]

\paragraph{Stochastic model.}  In the stochastic model, we represent each potassium channel using a state diagram:

\begin{figure}[h]
\begin{center}
\includegraphics[width=0.75\textwidth]{hodgkin_huxley_advanced/K_state_diagram}
\end{center}
\end{figure}
where the labels represent the probability of transitioning between
each state in the indicated direction.  We simulate a state transition
from state $X$ to $Y$ during a time interval $dt$ if a random number,
chosen with each new timestep of the simulation, is less than the
probability of transitioning between $X$ and $Y$.  The channel is
considered to be open at time $t$ if the ion channel is in the
5$^{th}$ state at time $t$.  In simulations, we'll need to keep track
of how many of the ion channels are open vs. closed during each time
step.  As the number of channels ($N$) increases, this model becomes
arbitrarily similar to the Hodgkin-Huxley version (you'll be verifying
this in the problem set).  Note that you can compute $P_K$ for the
stochastic model at time $t$ by simply computing the fraction of
channels that are in state 5 at time $t$.

\paragraph{Sodium channel review.}  The sodium channel is comprised of three identical subunits and an inactivation gate.  The three identical subunits each open with probability $m$ (where $m$ increases as the cell is depolarized and decreases as the cell is hyperpolarized).  The opening rates for the three subunits are $\alpha_m$, and the closing rates are $\beta_m$.  The inactivation gate is open with probability $h$, where $h$ decreases as the cell is depolarized and increases when the cell is hyperpolarized.  The opening and closing rates are $\alpha_h$ and $\beta_h$, respectively.

\[
\alpha_m(V) = \frac{0.1(V+40)}{1 - e^{-.1(V+40)}}
\]
\[
\beta_m(V) = 4e^{-0.0556(V + 65)}
\]

\[
\alpha_h(V) = 0.07e^{-.05(V+65)}
\]
\[
\beta_n(V) = \frac{1}{1 + e^{-.1(V+35)}}
\]

\paragraph{Stochastic sodium channel model.}  In the Hodgkin-Huxley model, the three subunits and the inactivation gate are assumed to be independent (that's why the probabilities are multiplied into $m^3h$).  However, this is not quite true.  A more accurate description is something like the following:

\begin{figure}[h]
\begin{center}
\includegraphics[width=0.75\textwidth]{hodgkin_huxley_advanced/Na_state_diagram}
\end{center}
\end{figure}

In particular, the ball mechanism of the inactivation gate is located inside the cell membrane, and cannot be directly affected by potential across the membrane.  The inactivation gate only comes into play when at least one of the subunits is open (i.e., when the channel occupies states 2, 3, or 4 in the diagram).  In addition, according to this model, if the neuron is in the inactivate state (state 5), it can only transition to state 3.

Whereas the transitions of the three subunits between states 1, 2, 3, and 4 in the stochastic model are identical to in the Hodgkin-Huxley model, the behavior of the inactivation gate is much different -- in particular, the inactivation gate in the stochastic model depends on the states of the three subunits.

As in the stochastic potassium channel model, you can compute the
$P_{Na}$ for the stochastic sodium channel at time $t$ by computing
the fraction of sodium channels which occupy state 4 at that time.

\paragraph{Replacing the Hodgkin-Huxley channels with stochastic
  channels.}  In the Hodgkin-Huxley model, we computed $P_K = n^4$ and
$P_{Na} = m^3h$ with each time step, updating $n, m,$ and $h$ as we
stepped through the model.  In the stochastic model, we compute $P_K$
and $P_{Na}$ directly, so we no longer need to compute $n$, $m$, or
$h$.  Other than the difference in computing $P_K$ and $P_{Na}$, the
stochastic model is identical to the Hodgkin-Huxley model.

\paragraph{Some implementation suggestions.}  There are a number of
possible ways to implement the stochastic channel models, with some
methods being more efficient than others.  Particularly when the
number of channels is large, it becomes very important to code the
simulation efficiently (read: vectorize!) if the simulation is to
finish running in a reasonable amount of time.  To start you off,
we'll go through a simple two-state example.  The states are $x$
(closed) and $y$ (open).  Let's suppose that the probability of
transitioning from state $x$ to state $y$ is $p_{xy}$ and the
probability of transisitioning from $y$ to $x$ is $p_{yx}$.  To
simulate $N = 1000$ of these simple two-state channels, we could write
something like the following:
\newpage
\begin{verbatim}
dt = 0.1;
t = 0:dt:1000;
states = ones(1,N);
n_open = zeros(size(t));

pOpen = [p_xy 0];
pClose = [0 p_yx];
for i = 1:length(t)
  open_chooser = rand(size(states)) < (dt*pOpen(states));
  states(open_chooser) = states(open_chooser) + 1;

  close_chooser = rand(size(states)) < (dt*pClose(states));
  states(close_chooser) = states(close_chooser) - 1;

  n_open(i) = sum(states == 2);
end
\end{verbatim}

The potasium channel simulation is identical to the above code, but
with the \texttt{pOpen} and \texttt{pClose} variables modified as in
the state diagram.  To implement the stochastic sodium channel model,
you need to seperately compute the probabilities of opening subunits and opening the inactivation gate.

\end{document}


\section{2K Analysis (90 pts)\label{sec:6}}

    In this section a 2k3 analysis (2k analysis with 3 repetitions) is performed on throughput and response time for
    experimental parameters listed in table \ref{tab:6_setup} using single keys for GET requests. In a 2k analysis
    factors are used as parameters to infer system behaviour. Table \ref{tab:6_2k-factors} lists these factors and their
    interpretation as per sign table (this approach was chosen for the 2k analysis). It is important to note that for a
    $2^k r$ experiment with $k = 3$ (3 factors observed) and $r = 3$ (three repetitions) the data gets appropriately
    normalized for the calculation of the q-values (multiplication by $\tfrac{1}{2^3}$) and the sum of squares (SSQ) is
    multiplied by $2^3 * 3$ to be able to infer the variation explained.

    \begin{table}
        \scriptsize{
            \begin{tabular}{|l|c|}
                \hline Number of servers                & 1 and 3 \\
                \hline Number of client machines        & 3 \\
                \hline Instances of memtier per machine & 1 (1 middleware) or 2 (2 middlewares) \\
                \hline Threads per memtier instance     & 2 (1 middleware) or 1 (2 middlewares) \\
                \hline Virtual clients per thread       & 32 \\
                \hline Workload                         & Write-only and Read-only \\
                \hline Multi-Get behavior               & N/A \\
                \hline Multi-Get size                   & N/A \\
                \hline Number of middlewares            & 1 and 2 \\
                \hline Worker threads per middleware    & 8 and 32 \\
                \hline Repetitions                      & 3 or more (at least 1 minute each) \\
                \hline
            \end{tabular}
        \caption{Experimental parameters for 2k analysis.\label{tab:6_setup}}
        }
    \end{table}

    \begin{table}
        \small{
            \begin{tabular}{r l l l }
                \toprule
                Sign & Number of \srv{}s (\textbf{A}) & Number of \mw{}s (\textbf{B}) & Number of worker threads (\textbf{C})   \\
                \midrule
                -1  & 1                            & 1                           & 8   \\
                1   & 3                            & 2                           & 32  \\
                \bottomrule
            \end{tabular}
            \caption{Sign table interpretation with respective configuration value.\label{tab:6_2k-factors}}
        }
    \end{table}

    For 2k analysis two models exist, the additive and multiplicative one. Considering the factors to analyse, it
    becomes clear that increasing \textbf{A} and either \textbf{B} or \textbf{C}, an additive relationship exists
    whereas increasing \textbf{B} also modifies \textbf{C} (but not necessarily vice-versa\textemdash a
    quasi-multiplicative relationship). A mixed model would therefore be optimal under these considerations. Looking at
    the expected data and including the strength of the multiplicative model for expected values $y$ to exist only for
    a large enough threshold of $\tfrac{y_{max}}{y_{min}}$ it is reasonable to evaluate the system with an additive
    model.

    \begin{table}
          \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}%
        \footnotesize{
            \centering
            \begin{subfigure}[t!]{0.45\textwidth}
                \centering
                \begin{tabular}{l*{4}{c}}
                    \toprule
                    & \multicolumn{2}{c}{Throughput}  & \multicolumn{2}{c}{Response Time} \\
                    \cmidrule(lr){2-3}\cmidrule(lr){4-5}
                    & \multicolumn{1}{c}{Effect} & \multicolumn{1}{c}{Variation} &
                      \multicolumn{1}{c}{Effect} & \multicolumn{1}{c}{Variation} \\
                    \midrule
                    q0            & 5695 & \textemdash & 44.1  & \textemdash \\
                    \addlinespace
                    qA            & 2760 & 98.24\%     & -21.3 & 99.73\% \\
                    qB            & 173  & 0.38\%      & -0.5  & 0.05\% \\
                    qC            & 138  & 0.25\%      & -0.4  & 0.04\% \\
                    \addlinespace
                    qAB           & 175  & 0.40\%      & -0.5  & 0.06\% \\
                    qAC           & 137  & 0.24\%      & -0.4  & 0.04\% \\
                    qBC           & -136 & 0.24\%      & 0.4  & 0.04\% \\
                    \addlinespace
                    qABC          & -136 & 0.24\%      & 0.4   & 0.03\% \\
                    \addlinespace
                    Error         & \textemdash & 0.01\% & \textemdash & 0.00\% \\
                    \bottomrule
                \end{tabular}
                \caption{2k3 factors with variation for GET requests.\label{tab:6_get-factors}}
            \end{subfigure}
            \hspace{4em}
            \begin{subfigure}[t!]{0.45\textwidth}
                \centering
                \begin{tabular}{l*{4}{c}}
                    \toprule
                    & \multicolumn{2}{c}{Throughput}  & \multicolumn{2}{c}{Response Time} \\
                    \cmidrule(lr){2-3}\cmidrule(lr){4-5}
                    & \multicolumn{1}{c}{Effect} & \multicolumn{1}{c}{Variation} &
                      \multicolumn{1}{c}{Effect} & \multicolumn{1}{c}{Variation} \\
                    \midrule
                    q0            & 7775 & \textemdash & 26.4 & \textemdash \\
                    \addlinespace
                    qA            & -574 & 7.87\%      & 2.1  & 9.55\% \\
                    qB            & 1149 & 31.56\%     & -3.9 & 32.81\%\\
                    qC            & 1530 & 55.93\%     & -5.0 & 54.85\% \\
                    \addlinespace
                    qAB           & 171  & 0.70\%      & -1.0 & 2.41\% \\
                    qAC           & -177 & 0.75\%      & -0.0 & 0.00\% \\
                    qBC           & 341  & 2.77\%      & 0.3  & 0.17\% \\
                    \addlinespace
                    qABC          & 115  & 0.32\%      & -0.2 & 0.11\% \\
                    \addlinespace
                    Error         & \textemdash & 0.09\% & \textemdash & 0.10\% \\
                    \bottomrule
                \end{tabular}
                \caption{2k3 factors with variation for SET requests.\label{tab:6_set-factors}}
            \end{subfigure}
        \caption{2k3 factor analysis summaries for GET and SET requests. Numbers are rounded for throughput to integers,
                 for latency to a single decimal.\label{tab:6_factor-analysis}}
        \vspace*{-0.75\baselineskip}
        }
    \end{table}

    In the case of GET packets the factor \textbf{A} is with nearly 100\% variational effect for throughput and
    response time the only relevant factor in determining the system behaviour. Of interesting note is the switch in
    sign yet including the interpretation that high throughput implies low response times it becomes apparent why a
    change in sign must happen for corresponding factors. As previously observed we see for three \cli{}s and one
    \srv{} immediately a saturation point and the middleware has no measurable effect, either good or bad on the
    system performance for GET. This matches the results observed. Visualizing the residuals (Figure
    \ref{fig:6_r_get_tp}) and doing a QQ plot (figure \ref{fig:6_qq_get_tp}) of the measured data for the
    throughput and response time (the latter omitted for behaving similar to the former) shows very consistent
    behaviour at either ends of the request throughputs on the residual plot. A few outliers are observed but these
    cannot be ruled out in a cloud environment. The QQ plot has a very shallow line in the center where the highest
    likelihood of an observation is for a standard distribution. Both of these plots show an insufficient model
    according to the book referenced in the lecture (Box 18.1) and a multiplicative model is recommended to be observed.
    After evaluating that model no reasonable gains were able to be found and the resulting plots are apart from a change
    of axis labels very similar. Even though the results indicate problems, when including the real system behaviour,
    both numbers make sense and are not only verified by the 2k3 analysis but also empirically to correlate.

    In the case of SET requests a mixture of factors \textbf{C} and \textbf{B} shows to be mostly significant with
    around 55.9\%/54.9\% and 31.6\%/32.8\% for throughput and response time respectively. Of minor significance yet
    measurable are \textbf{A} with around 7.8\%/9.6\% for throughput and response time and \textbf{BC} for
    throughput with 2.8\% (which interprets as increasing the number of \mw{}s and worker threads simultaneously).
    The change in sign occurs again correctly. The analysis aligns with previous observations and conclusions while
    bringing up the hypothesis that worker threads result in a larger gain in performance compared to adding more
    \mw{}s. The hypothesis seems to not hold though as in Experiment 3 we expect for the case of two \mw{}s and 16
    worker threads less performance than for one \mw{} and 32 worker threads. This is clearly not the case and as
    such the belief is the model is unable to correctly differentiate between these two parameters (assuming both
    \mw{}s have the exact same throughput and response time behaviour). The factor \textbf{A}, adding more \srv{}s to
    the system, does align with results from experiments \ref{sec:3} and \ref{sec:4} where the number of \srv{}s changes
    from one to three and a decrease in throughput is observed. The factor proposes an increase in \srv{}s introduces a
    negative effect as the \mw{} is designed to share the SET operation with each \srv{} in the system. This key
    distribution introduces additional latencies into the system. Again residual (figure \ref{fig:6_r_set_tp}) and QQ
    plots (figure \ref{fig:6_qq_set_tp}) were created but only the throughput ones are presented (similar behaviour in
    either case). The residuals are scattered without anything being possible to interpret and the QQ plot shows a
    quasi-linear behaviour. Both great signs according to the book as the residual error must be IID and small (it is
    smaller by a magnitude than the x-labels) and the distribution of errors being uniform at random. As such the model
    should be good enough to describe system performance per the book. Given empirical data the trend is clearly visible
    and overlaps but the strength of individual components is questioned to be correct.

    \begin{figure*}
        \vspace*{-.5\baselineskip}
            \centering
        \begin{subfigure}[t!]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{../data_analysis/figures/6-0_throughput-set-residual.png}
            \caption{Residual plot for SET Throughput.\label{fig:6_r_set_tp}}
            \includegraphics[width=\textwidth]{../data_analysis/figures/6-0_throughput-get-residual.png}
            \caption{Residual plot for GET Throughput.\label{fig:6_r_get_tp}}
        \end{subfigure}
        \begin{subfigure}[t!]{0.45\textwidth}
            \centering
            \includegraphics[width=\textwidth]{../data_analysis/figures/6-0_throughput-set-qq.png}
            \caption{QQ-Plot for SET Throughput.\label{fig:6_qq_set_tp}}
            \includegraphics[width=\textwidth]{../data_analysis/figures/6-0_throughput-get-qq.png}
            \caption{QQ-Plot for GET Throughput.\label{fig:6_qq_get_tp}}
        \end{subfigure}
        \caption{Residual and QQ plots based on memtier measurements for experiment 6.0. Only throughput plots
                 are depicted.\label{fig:6_tp}}
    \end{figure*}


% !TEX root = ../ms.tex

\defmath\B{\mathbb B}

\vspace{-1em}
\section{Introduction~\label{sec:introduction}}

%The simulation of quantum computing circuits is important for studying noise, synthesizing and optimizing circuits and

Classical simulation of quantum computing is useful for circuit design~\cite{zulehner2017one} and studying noise resilience in the era of Noisy Intermediate-Scale Quantum (NISQ) computers~\cite{preskill2018quantum}.
Moreover, identifying classes of quantum circuits that are classically simulatable, helps in excluding regions where a quantum computational advantage cannot be obtained.
For example, circuits containing only Clifford gates (a non-universal quantum gate set), using an all-zero initial state, only compute the so-called `stabilizer states' and can be simulated in polynomial time
\cite{gottesman1998heisenberg,aaronson2008improved,gottesman1997stabilizer}.
%Stabilizer states arise when computing using only Clifford gates (a non-universal quantum gate set), and
Stabilizer states, and associated formalisms for expressing them, are fundamental to many quantum error correcting codes~\cite{gottesman1997stabilizer} and play a role in measurement-based quantum computation~\cite{raussendorf2001oneway}.
% circuit limited to the Clifford gate set stay in a tractable region of the so-called stabilizer states , which forms the basis for measurement-based quantum computation~\cite{raussendorf2001oneway} and many quantum error correction codes.
In fact, simulation of general quantum circuits is fixed-parameter tractable in the number of non-Clifford gates~\cite{bravyi2016trading}, a principle on which many modern simulators are based~\cite{bravyi2016trading,bravyi2017improved,bravyi2019simulation, huang2019approximate,kocia2018stationary,kocia2020improved}.

\begin{wrapfigure}{R}{.35\textwidth}\vspace{-1.5em}
	\includegraphics[width=.24\textwidth]{pics/venn-diagram.pdf}
    \centering
    \vspace{-1em}
	\caption{
        The set of stabilizer states and states represented as:
        poly-sized \limdds and QMDDs.
        \vspace{-\baselineskip}
	}%\vspace{-.5em}
    \label{fig:venn-diagram}
\end{wrapfigure}

Another method for simulating universal quantum computation is based on (algebraic) decision diagrams (DDs)~\cite{akers1978binary,bryant86,580054,bryant1995verification,sanner,10.1145/157485.164569,fujita1997multi,viamontes2003improving,viamontes2004high,miller2006qmdd,zulehner2018advanced}.
A DD is a directed acyclic graph (DAG) in which each path represents a quantum amplitude, enabling the succinct representation of many quantum states through the combinatorial nature of these paths.
Various manipulation operations for DDs exist which implement any quantum gate operation in polynomial time
in the size of the DD. Together with other DD operations that can be used for measurement,
strong simulation is easily implemented using a DD data structure~\cite{miller2006qmdd,zulehner2018advanced}.
Indeed, DD-based simulation was empirically shown to be competitive with state-of-the-art simulators~\cite{viamontes2004high,zulehner2018advanced} and is used in several simulator implementations~\cite{viamontes2009quantum}.
%\todo{Vedran: at-the time? I mean, simulators have advanced a lot since 2018... As I mentioned they simulated shor on 60 qubits. good luck with that in any other method.}.
DDs and the stabilizer formalism are introduced in \autoref{sec:preliminaries}.
%However, in contrast to the stabilizer formalism, little is known about which quantum circuits are efficiently simulatable with DD-based approaches.

In this paper, we show that certain stabilizer states, called cluster states \cite{briegel2000persistent}, yield exponentially large \qmdds, the currently most succinct version of DDs
(see \autoref{sec:exponential-separations}).
In order to unite the strengths of DDs and the stabilizer formalism,
in \autoref{sec:isomorphism-qmdd}, we propose \limdd: a new DD for quantum computing simulation using local invertible maps (LIMs).
Specifically, \limdds eliminate the need to store multiple states which are equivalent up to LIMs, allowing more succinct DD representations.
%\todo{Tim: please check, it might sound a bit like \limdds only can encode stabilizer states now}
We prove that the set of quantum states that can be \emph{represented} by poly-sized \limdds
is larger than those that can be expressed in either the stabilizer formalism or a poly-sized \qmdd.
\autoref{fig:venn-diagram} shows the resulting separation.
In \autoref{sec:quantum-simulation}, we give procedures for analyzing and simulating quantum
circuits using \limdds and conclude in \autoref{sec:discussion} with evidence that \limdd-based simulation can be powerful than modern techniques using low-rank stabilizer decomposition~\cite{bravyi2019simulation}.

The workhorse behind \limdds is a novel algorithm which merges two DD nodes when they are `isomorphic:'
Two quantum states $\ket{\phi}$ and $\ket{\psi}$ are isomorphic when there is a series of Pauli operators $P_j$ and a complex nonzero number $\lambda$ such that $\ket{\phi}=\lambda P_n\otimes\cdots\otimes P_1\ket \psi$.
There is a plethora of work on investigating the effect of similar local operations in the context of stabilizer states \cite{nest2005local, englbrecht2020symmetries}; we emphasize that here we consider arbitrary quantum states $\ket{\phi}, \ket{\psi}$.%\todo{Tim: should rephrase, now it looks like no-one ever thought of considering locally-equivalent states beyond stabilizer states...}
To find such an isomorphism, we compute (generators of) the stabilizer (sub)group $\Stab(\ket \psi)$ of the state $\ket \psi$ represented by each DD node, and then we exploit the fact that the set of all such isomorphisms can be expressed as the coset $\pi\cdot \Stab(\ket \psi)$ for some isomorphism $\pi$.
To make the diagram canonical ---an important property for realizing efficient manipulation operations~\cite{darwiche2002knowledge}---
our algorithm then chooses a ``lexicographically smallest'' element from a Pauli coset.

%\todo[inline]{Vedran: general comment: i am a bit worried that nothing about efficiency of algorithms for all the manipulations is said for such a long time... I lose the connection to simulation of circuits... The Introduction for me is missing a link and clarification of the relationship between representation and simulation."}

%\todo[inline]{Give another / better example of polytime simulatable QC; introduce stabilizer states before they are mentioned.}

%\todo[inline]{Process Vedran's feedback, so write something about Clifford gates that's actually true.}

%This work is organized as follows.
%After providing the necessary background in \autoref{sec:preliminaries}, we formally introduce \limdds in \autoref{sec:isomorphism-qmdd}.
%In \autoref{sec:exponential-separations}, we prove the exponential separation between QMDDs and \limdds, by showing that there is a sequence of $n$-qubit stabilizer states that can only be represented with exponentially-sized QMDDs.