\documentclass[a4paper,twoside, 10pt]{article}
\usepackage[%CJKbookmarks=true,
	unicode=true,
	hyperindex=true,
	pdfstartview=FitH,
	bookmarksnumbered=true,	%注释掉此行则书签前没有数字编号
	bookmarksopen=true,  	%注释掉此行则默认不展开书签
	colorlinks=true, 	%注释掉此项则交叉引用为彩色边框(将colorlinks和pdfborder同时注释掉)
	pdfborder=001,   	%注释掉此项则交叉引用为彩色边框
	citecolor=red
]{hyperref}

\usepackage{graphicx}		%图像宏包
\usepackage{amsmath}		%ams数学宏包
\usepackage{algorithm}		% algorithm package
\usepackage{algpseudocode}	% algorithmicx package is included therein
\usepackage{simplewick}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsthm}
\DeclareMathOperator{\Tr}{Tr}

\newtheorem{theorem}{Theorem}
\theoremstyle{wick}
\newtheorem*{wick}{Wick's Theorem}

\newtheorem{lemma}[theorem]{Lemma}

\usepackage{extarrows}		%长等号、等号上下
\usepackage{textcomp}		%摄氏度
\usepackage{multirow,makecell}
\usepackage{mhchem}
\usepackage{pifont}
\usepackage{subfig}		%subfloat

\newcommand{\mr}{\mathrm}
\newcommand{\tr}{\textrm}
\newcommand{\tN}{\tr{N}}
\newcommand{\md}{\mathrm{d}}
\newcommand{\mi}{\mathrm{i}}
\newcommand{\me}{\mathrm{e}}
\newcommand{\mg}{\mathrm{g}}
\newcommand{\br}{\bm{r}}
\newcommand{\bR}{\bm{R}}
\newcommand{\bt}{\bm{\tau}}
\newcommand{\bk}{\bm{k}}
\newcommand{\bK}{\bm{K}}
\newcommand{\ba}{\bm{a}}
\newcommand{\bb}{\bm{b}}
\newcommand{\bG}{\bm{G}}
\newcommand{\bq}{\bm{q}}
\newcommand{\bB}{\bm{B}}
\newcommand{\bx}{\bm{x}}
\newcommand{\vD}{\varDelta}
\newcommand{\Ao}{\mathring{\mr{A}}}
\newcommand{\tc}{\textcelsius}
\newcommand{\oo}{$\ddot{\mathrm{o}}$}
\newcommand{\hh}{\hat{E}}
\newcommand{\mcH}{\mathcal{H}}
\newcommand{\mcS}{\mathcal{S}}
\newcommand{\mcM}{\mathcal{M}}
\newcommand{\psik}{\psi_{\bm{k}}(\bm{r})}
\newcommand{\vpr}{V^{\mr{psd}}(\bm{r})}
\newcommand{\vpq}{V^{\mr{psd}}(q)}
\newcommand{\rlh}{\rightleftharpoons}
\newcommand{\II}{\tr{\textit{II}}}
\newcommand{\IV}{\tr{\textit{IV}}}
\newcommand{\oscf}{$\varOmega$-SCF}
\newcommand{\sscf}{$\sigma$-SCF}
\newcommand{\Oo}{\varOmega(\omega)}
\newcommand{\mcF}{\mathcal{F}}

\newcommand{\sff}{\sffamily}

\newcommand{\cd}{c^{\dagger}}
\newcommand{\ad}{a^{\dagger}}
\newcommand{\exphf}[1]{\langle{}#1\rangle_0}
\newcommand{\mn}{ij\lambda\sigma}
\newcommand{\mnp}{i'j'\lambda'\sigma'}
\newcommand{\ti}{\langleij|\lambda\sigma\rangle}
\newcommand{\tiv}{\langleij|\sigma\lambda\rangle}
\newcommand{\tip}{\langlei'j'|\lambda'\sigma'\rangle}
\newcommand{\tipv}{\langlei'j'|\sigma'\lambda'\rangle}
\newcommand{\ex}[1]{\langle{}#1\rangle}
\newcommand{\ket}[1]{|#1\rangle}
\newcommand{\bra}[1]{\langle{}#1|}
\newcommand{\var}{\sigma_H^2}
\newcommand{\Ns}{N_{s}}
\newcommand{\Nt}{N_{t}}
\newcommand{\sums}[1]{\sum_{#1}^{s}}
\newcommand{\sumt}[1]{\sum_{#1}^{t}}
\newcommand{\mat}[1]{\mathbf{#1}}
\newcommand{\mmp}{ii'}
\newcommand{\nnp}{jj'}
\newcommand{\llp}{\lambda\lambda'}
\newcommand{\ssp}{\sigma\sigma'}
\newcommand{\pfrac}[2]{\frac{\partial{}#1}{\partial{}#2}}

\usepackage{amssymb}
\usepackage{color}
\usepackage{xcolor}		%颜色
\usepackage{bm}			%数学矢量宏包
\usepackage{mathrsfs}
\pagestyle{plain}
\usepackage[raggedright]{titlesec}
\usepackage[procnames]{listings}

\usepackage{pict2e}
\usepackage{keyval}
%\usepackage{fp}
\usepackage{diagbox}		%这几个宏包用于画表头的斜线
\usepackage{booktabs}		%三种粗细不同的线：\toprule、\midrule 和 \bottomrule 对应顶部、中部、底部
\usepackage[font=small,labelfont=bf,width=1.0\textwidth]{caption}
\captionsetup{tablename=Tab. }
\captionsetup{figurename=Fig.}

\usepackage[font=small,labelfont=bf,width=1.0\textwidth]{caption}
\usepackage{multirow}
\usepackage{cases}

\usepackage{appendix}		%附录

\bibliographystyle{unsrt}
\newcommand{\upcite}[1]{\textsuperscript{\cite{#1}}}	%upcite命令

\usepackage{geometry}
\geometry{left=2.7cm,right=2.6cm,top=2.5cm,bottom=2.5cm}	%页边距

%%\linespread{1.2}				%行距

\title{Basic Exercises for Scientific Programming}
\author{Hongzhou Ye}
\date{January 1, 2018}							%开启我则不显示日期

\newcommand{\ttf}{\ttfamily}
\newcommand{\ttt}{\texttt}

\begin{document}
	\maketitle{}

	\begin{abstract}
		Scientific programming involves many topics of applied math, varying from basic linear algebra to optimization algorithms. This file is a collection of several basic exercises that help you practice some of the essential skills.
	\end{abstract}

	\section{Linear least square fitting}

	In the most general sense, the task of least square fitting is to find an approximate mapping $\tilde{f}$ to a given mapping
	\begin{equation}
	\begin{split}
		f:&\, \mathbb{X} \rightarrow \mathbb{Y}	\\
		  &\, \bm{x} \rightarrow \bm{y} = f(\bm{x})
	\end{split}
	\end{equation}
	by minimizing the least square loss
	\begin{equation}
		\mathcal{L}
			= \sum_{i} \big\|\tilde{f}(\bm{x}^{(i)}) - \bm{y}^{(i)}\big\|_2^2
	\end{equation}
	based on a set of known data $\{(\bm{x}^{(i)}, \bm{y}^{(i)})\}$, where
	\begin{equation}
		\|\bm{a}\|_2
			= \sqrt{\bm{a} \cdot \bm{a}}
			= \bigg(\sum_{\mu} a_{\mu}^2\bigg)^{1/2}
	\end{equation}
	is the $2$-norm of vector $\bm{a}$.

	As a special case, linear least square fitting assumes a linear functional form for the approximate mapping $\tilde{f}$, i.e.
	\begin{equation}	\label{eq:lls_ansatz}
		\tilde{f}(\bm{x})
			= \mat{A}^{\tr{T}}\bm{x} + \bm{b},
	\end{equation}
	where $\mat{A}$ and $\bm{b}$ are determined from experimental data. In this exercise, we are going to explore the properties of linear least square fitting.

	\begin{enumerate}
		\item First let us consider an even simpler case, $\bm{b} = \bm{0}$. Show that $\mat{A}$ is determined by the following equation
		\begin{equation}	\label{eq:lls_equation}
			\mat{X} \mat{X}^{\tr{T}} \mat{A}
				= \mat{X} \mat{Y}^{\tr{T}}
		\end{equation}
		where
		\begin{equation}	\label{eq:data_set_def}
			\mat{X}
				= [\bm{x}^{(1)}, \bm{x}^{(2)}, \cdots{}, \bm{x}^{(N)}],\quad{}
			\mat{Y}
				= [\bm{y}^{(1)}, \bm{y}^{(2)}, \cdots{}, \bm{y}^{(N)}].
		\end{equation}

		\textit{Hint}: $\mathcal{L}$ is a function of $\mat{A}$
		\begin{equation}	\label{eq:lsloss}
		\begin{split}
			\mathcal{L}
				&= \sum_i \| \mat{A}^{\tr{T}} \bm{x}^{(i)} - \bm{y}^{(i)} \|^2_2
				= \sum_i (\mat{A}^{\tr{T}} \bm{x}^{(i)} - \bm{y}^{(i)})^{\tr{T}}
				(\mat{A}^{\tr{T}} \bm{x}^{(i)} - \bm{y}^{(i)})	\\
				\iffalse
				&= \mat{A}^{\tr{T}} \bigg[\sum_{i} \bm{x}^{(i)} \bm{x}^{(i)\tr{T}}\bigg] \mat{A}
				- \mat{A}^{\tr{T}} \bigg[\sum_{i} \bm{x}^{(i)} \bm{y}^{(i)\tr{T}}\bigg]
				- \bigg[\sum_{i} \bm{y}^{(i)} \bm{x}^{(i)\tr{T}} \bigg] \mat{A}
				+ \sum_i \bm{y}^{(i)\tr{T}} \bm{y}^{(i)}
				\fi
		\end{split}
		\end{equation}
		You might need the following trick
		\begin{equation}
		\begin{split}
			\sum_i \bm{x}^{(i)\tr{T}} \mat{A} \mat{A}^{\tr{T}} \bm{x}^{(i)}
				&= \sum_i \sum_{\mu\nu\lambda} x^{(i)}_{\mu} A_{\mu\nu}
				A^{\tr{T}}_{\nu\lambda} x^{(i)}_{\lambda}
				= \sum_{\mu\nu\lambda} A^{\tr{T}}_{\nu\lambda} \bigg[\sum_i x^{(i)}_{\lambda}
				x^{(i)}_{\mu}\bigg] A_{\mu\nu}	\\
				&= \sum_{\mu\nu\lambda} A^{\tr{T}}_{\nu\lambda} (XX^{\tr{T}})_{\lambda \mu} A_{\mu\nu}
				= \mat{A}^{\tr{T}} \mat{X} \mat{X}^{\tr{T}} \mat{A}
		\end{split}
		\end{equation}
		Same trick can be played to all other terms in the expansion. Then you can take derivative with $\mat{A}^{\tr{T}}$ and obtain the desired equation (you can treat $\mat{A}$ and $\mat{A}^{\tr{T}}$ two independent variables).

		\item We now have the equation and let us see how it works! In \ttt{data/least\_square} you can find the data file \ttt{X1.txt} and \ttt{Y1.txt}, which contains $20$ data points. Fit a linear equation with zero $\bm{b}$ and plot the fitted curve along with the original data. Your result should be something like the following
		\begin{figure}[H]
			\centering
			\includegraphics[width=0.6\linewidth]{plot/least_square/XY1.png}
		\end{figure}

		\item The example above is one-dimensional and trivial. A non-trivial multi-dimensional data set can be found in \ttt{X2.txt} and \ttt{Y2.txt}, where $\bm{x}^{(i)} \in \mathbb{R}^{5}$ and $\bm{y}^{(i)} \in \mathbb{R}^3$ and there are $20$ of them. Find an $\mat{A} \in \mathbb{R}^{5 \times{} 3}$ that minimizes the square loss. If you do the math correctly, the least square loss as defined in eqn (\ref{eq:lsloss}) should be $3.431402129330746$.
	\end{enumerate}

	\section{Non-linear fitting in terms of LLS}

	The reason why linear least square (LLS) is important is that one can go beyond linear fitting by introducing basis functions.

	To be specific, let us first specify the dimension for each quantity:
	\begin{equation}
		\bm{x} \in \mathbb{R}^n,\quad{}
		\bm{y} \in \mathbb{R}^m,\quad{}
		\tr{data set: }\{(\bm{x}^{(i)}, \bm{y}^{(i)})\}_{i = 1}^N.
	\end{equation}
	Now we introduce a basis function:
	\begin{equation}
	\begin{split}
		\phi:&\, \mathbb{R}^n \to \mathbb{R}^l		\\
			 &\, \bm{x} \to \bm{\phi} = \phi(\bm{x})
	\end{split}
	\end{equation}
	For example, $\phi(x) = [x_1^2, x_2^2, x_1x_2]^{\tr{T}}$ is a basis function transforming a $\mathbb{R}^2$ vector $\bm{x} = [x_1, x_2]^{\tr{T}}$ to a $\mathbb{R}^3$ vector $\bm{\phi}$. With this in hands, our ansatz in eqn (\ref{eq:lls_ansatz}) needs to be modified
	\begin{equation}	\label{eq:lls_ansatz_with_basis}
		\tilde{f}(\bm{x})
			= \mat{A}^{\tr{T}} \phi(\bm{x}) + \bm{b}
	\end{equation}
	and $\mat{A} \in \mathbb{R}^{l \times{} m}$.

	Though eqn (\ref{eq:lls_ansatz_with_basis}) is still linear in $\bm{\phi}$, it does not need to be linear in $\bm{x}$ because the basis function could be non-linear. In this way, one can achieve non-linearity within the framework of LLS.

	\begin{enumerate}
		\item In a way similar to the derivation of eqn (\ref{eq:lls_equation}), show that $\mat{A}$ in eqn (\ref{eq:lls_ansatz_with_basis}) is determined by solving
		\begin{equation}
			\mat{\Phi} \mat{\Phi}^{\tr{T}} \mat{A}
				= \mat{\Phi} \mat{Y}^{\tr{T}}
		\end{equation}
		where $\mat{Y}$ is defined in eqn (\ref{eq:data_set_def}), and
		\begin{equation}
			\mat{\Phi}
				= [\bm{\phi}^{(1)}, \bm{\phi}^{(2)}, \cdots{}, \bm{\phi}^{(N)}].
		\end{equation}

		\item \textit{Polynomial basis}. LLS with basis function
		\begin{equation}
			\phi(x)
				= [1, x, x^2, \cdots{}, x^{l-1}]
		\end{equation}
		is equivalent to fitting a polynomial of order $l-1$. Perform LLS fit with $l = 2, 4, 6$ and $8$ for data sets \ttt{X1.txt} and \ttt{Y1.txt} in \ttt{data/non\_linear\_least\_square}. Then
		\begin{enumerate}
			\item report the loss for each $l$
			\item report the RMS of $\mat{A}$ for each $l$
			\begin{equation}
				\tr{RMS}\,\mat{A}
					= \frac{1}{nm} \sum_{\mu}^n \sum_{\nu}^m A_{\mu\nu}^2
					= \ttt{mean(sum(sum(A.*A)))}\qquad{}\tr{(In MATLAB)}
			\end{equation}
			\item plot all fitted curves along with the data points
		\end{enumerate}
		What trend(s) do you observe?

		\textit{Hint}: wrapping the fitting and plotting processes each in a function would make your life much easier. Specifically, they could be like
		\begin{verbatim}
		    A = lls_poly(X, Y, l)
		        % X/Y: data sets; l: polynomial order
		        % A: LLS coefficient matrix.
		\end{verbatim}
		and
		\begin{verbatim}
		    y = lls_poly_plot(x, A, l)
		        % x: grid points for plotting (e.g. x = [-1.5:0.1:2.5])
		        % A: optimized LLS coefficient matrix
		        % l: polynomial order
		        % y: function values on grid x, so that you can plot with plot(x, y)
		\end{verbatim}

		\item \textit{Polynomial basis (cnt'd)}. The data set above is generated as follows
		\begin{equation}
			y
				= 1 - 2x + x^3 + g
		\end{equation}
		for $x$ randomly drawn from $-1.5$ to $2$, where $g \sim \mathcal{N}(0, 1)$ is a Gaussian random variable that mimics a random noise (from, e.g.\ measure error). Now plot your fitted curves and the original data points along with the ``real" solution (set $g = 0$).
		\begin{enumerate}
			\item Does it approach the ``real" solution for larger $l$?
			\item Does a small loss always mean a \emph{good} fitting?
		\end{enumerate}

		\item \textit{Polynomial basis (cnt'd)}. In the example above, we see how increasing the \emph{model complexity} (in this case, $l$) can result in \emph{overfitting}. However, by using more data points, we can have a more complex model without overfitting. Data sets \ttt{X2.txt} and \ttt{Y2.txt} are generated in the same way, but of a much larger size! Now re-do all fittings with the new data and plot the fitted curves along with the ``real" solution. What new trend(s) do you observe?

		\item \textit{Sinusoidal basis}. Copy everything from \ttt{lls\_poly} to a new function named \ttt{lls\_sin} that uses the following basis
		\begin{equation}
			[1, \sin x, \cos x, \cdots{}, \sin (l-1)x, \cos (l-1)x]^{\tr{T}}
		\end{equation}
		for an order-$l$ fitting. Then re-do the fitting for $l = 2, 4, 6$ and $8$. Report variation of the loss as the model complexity increases. Plot all fitted curves along with the ``real" solution. Do you notice any artificial feature for large $l$?
	\end{enumerate}

\end{document}


\documentclass[11pt]{article}
%============Macros==================%
% you don't need to change anything. start editing from main body.
\usepackage{amsmath,amsfonts,amssymb,amsthm, cancel, float, enumitem, hyperref}
\usepackage{qcircuit}
\usepackage[margin=1in]{geometry}
%--------------Cosmetic----------------%
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage{microtype}
\usepackage{xspace}
\usepackage[svgnames]{xcolor}
\usepackage[sc]{mathpazo}
\usepackage{enumitem}
\setlist[enumerate]{itemsep=1pt,topsep=2pt}
\setlist[itemize]{itemsep=1pt,topsep=2pt}
%--------------Header------------------%
\def\course{CS 410/510 Introduction to Quantum Computing}
\def\term{Portland State U, Spring 2017}
\def\prof{Lecturer: Fang Song}
\newcommand{\handout}[5]{
   \renewcommand{\thepage}{\arabic{page}}
   \begin{center}
   \framebox{
      \vbox{
    \hbox to 5.78in { \hfill \large{\course} \hfill }
       \vspace{2mm}
       \hbox to 6in { {\Large \hfill #5  \hfill} }
       \vspace{2mm}
       \hbox to 6in { \term \hfill \emph{#2}}
       \hbox to 6in { {#3 \hfill \emph{#4}}}
      }
   }
   \end{center}
   \vspace*{4mm}
}
\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{#4}{{Lecture #1}}}

\def\complex{\mathbb{C}}
\def\real{\mathbb{R}}
\def\natural{\mathbb{N}}
\def\integer{\mathbb{Z}}

\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\snorm}[1]{\lVert#1\rVert}
\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}
\newcommand{\ceil}[1]{\left\lceil #1 \right\rceil}
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
\newcommand{\set}[1]{\left\{ #1 \right\}}
\newcommand{\vecb}[1]{\boldsymbol{\vec{#1}}}
\newcommand{\conj}[1]{\overline{#1}}
\newcommand{\op}[2]{#1#2#1^\dag}

\newcommand{\h}{\operatorname{H}}
\newcommand{\tf}{\operatorname{Tf}}
\newcommand{\pr}{\operatorname{Pr}}
\newcommand{\poly}{\operatorname{poly}}
\newcommand{\sign}{\operatorname{sign}}
\newcommand{\rank}{\operatorname{rank}}
\renewcommand{\det}{\operatorname{Det}}

\newcommand{\bqp}{\operatorname{BQP}}
\newcommand{\bpp}{\operatorname{BPP}}
\newcommand{\p}{\operatorname{P}}
\newcommand{\np}{\operatorname{NP}}
\newcommand{\pspace}{\operatorname{PSPACE}}
\renewcommand{\exp}{\operatorname{EXP}}
\newcommand{\pp}{\operatorname{PP}}
\newcommand{\qma}{\operatorname{QMA}}
\newcommand{\qnp}{\operatorname{QNP}}
\newcommand{\ma}{\operatorname{MA}}
\newcommand{\ip}{\operatorname{IP}}
\newcommand{\qip}{\operatorname{QIP}}
\newcommand{\gnm}{\operatorname{GNM}}

\newcommand{\bra}[1]{\langle #1 \rvert}
\newcommand{\ket}[1]{\lvert #1 \rangle}
\newcommand{\bret}[2]{\langle{#1}|{#2}\rangle}
\newcommand{\dbret}[3]{\langle{#1}|{#2}|{#3}\rangle}
\newcommand{\kret}[2]{\ket{#1}\bra{#2}}
\newcommand{\kera}[1]{\kret{#1}{#1}}
\newcommand{\Example}[1]{{\bf Example #1}:}

\def\X{\mathcal{X}}
\def\Y{\mathcal{Y}}
\def\Z{\mathcal{Z}}
\def\W{\mathcal{W}}
\def\A{\mathcal{A}}
\def\B{\mathcal{B}}
\def\V{\mathcal{V}}
\def\U{\mathcal{U}}
\def\C{\mathcal{C}}
\def\D{\mathcal{D}}
\def\E{\mathcal{E}}
\def\F{\mathcal{F}}
\def\M{\mathcal{M}}
\def\R{\mathcal{R}}
\def\P{\mathcal{P}}
\def\Q{\mathcal{Q}}
\def\S{\mathcal{S}}
\def\T{\mathcal{T}}
\def\K{\mathcal{K}}
\def\L{\mathcal{L}}

\def\fill{   \hfill}

%=============Main Doc=================%
\begin{document}
%-----Specs: change accordingly--------%
\def\lecdate{May 25, 2017} % put lecture date here
\def\scribe{Scribe: Enis K. Inan} % put your name here
\def\lecnum{16} % change the lecture number

\lecture{\lecnum}{\lecdate}{\prof}{\scribe}%

\begin{center}
{\textsc{Version: \today}}
\end{center}

\noindent Last time we talked about quantum complexity classes. Recall
that we used the quantum circuit model (1) as the basis for doing our
quantum computations instead of quantum Turing machines (2). Both
models are equivalent, it's just easier to use (1) instead of
(2). With (1), we have a collection of quantum circuits
$Q = \set{Q_x \: | \: x \in \set{0, 1}^*}$, i.e. $Q$ contains one
quantum circuit for each string $x$. We say that $Q$ is
\emph{polynomial-time uniform} or \emph{polynomial-time generated} if
there exists a polynomial algorithm that outputs a classical
description of $Q_x$ given input $x$. We then proceeded to state some
important complexity classes.

Of interest is $\bqp$, which states that a promise problem
$A \in \bqp$ if there exists a collection of quantum circuits $Q$ such
that for all $Q_x \in Q$ where $x$ is a valid input,
$\abs{Q_x} = \poly(\abs{x})$, i.e. the circuit $Q_x$ is polynomial in
the size of the input,
\begin{itemize}
\item If $x \in A_{y}$, then $\dbret{1}{Q_x}{1} \geq 2/3$.

\item If $x \in A_{n}$, then $\dbret{1}{Q_x}{1} \leq 1/3$
\end{itemize}
where the fancy $\dbret{1}{Q_x}{1}$ is understood as follows. When we run $Q_x$, we do not start with any zeros. When $Q_x$ finishes its computation, we will end up with a 1-qubit mixed state (ignoring ancilla qubits) that we also denote as $Q_x$ since the circuit $Q_x$ produces it. Then $\dbret{1}{Q_x}{1}$ is the probability that $Q_x$ will measure the outcome $1$, i.e. output that input $x$ is a YES instance. Thus, Case 1 is saying that if $x$ is a YES instance, then $Q_x$ should indicate it is so with at least $2/3$ probability. Case 2 states that if $x$ is a NO instance, then $Q_x$ should give a false-positive YES result with \emph{at-most} $1/3$ probability. Note that Cases 1 and 2 are known as the \emph{completeness} and \emph{soundness} conditions, respectively.

\fill

\noindent \Example{1} Integer factorization is in $\bqp$.
\begin{itemize}
\item {\bf Input:} $(N, k)$, $2 \leq k \leq N$
\item {\bf Output:} Whether there exists a proper factorization of $N$ where each factor $a \in \set{2, \dots, k}$. Using Shor's algorithm, we can answer this question in $O(\poly(\log(N)))$ time.
\end{itemize}

\fill

\noindent Note that $\p \subseteq \bqp$ and $\bpp \subseteq \bqp$, but we do not know if $\bqp \subseteq \bpp$, i.e. if $\bqp = \bpp$. We know the first two statements are true because we have showed in the beginning of class that we can turn \emph{any} classical circuit into a quantum circuit in a polynomial number of steps with a polynomial change in size.

%==============================%
\section{$\bqp \in \exp$}
%==============================%
We will now prove that $\bqp \in \exp$. Intuitively, this containment is formalizing the notion that a classical computer can simulate a quantum computer in exponential time. The proof idea is to take a quantum circuit $Q_x$ for an input $x$ that outputs $1$ with probability $p_1$ and $0$ with probability $p_0$ and create a classical algorithm which simulates it, i.e., given an input $x$, it outputs 1 with probability $\widetilde{p_1}$ and 0 with probability $\widetilde{p_0}$ such that $p_1 \approx \widetilde{p_1}$ and $p_0 \approx \widetilde{p_0}$. Remember that a classical algorithm and a classical circuit are the same thing, so what we really want to do is to create a classical circuit that sufficiently approximates our quantum circuit.

\fill

\noindent Now we proceed to the proof. First, note that \emph{any} quantum circuit can be approximated by a combination of Toffoli and Hadamard gates. As a brief recap, here's what these gates do:
\begin{itemize}
\item $\h$ acts on a single qubit, and induces the following mappings:
\begin{equation*}
\begin{split}
\h\ket{0} &\rightarrow \frac{1}{\sqrt{2}}\left(\ket{0} + \ket{1}\right) \\
\h\ket{1} &\rightarrow \frac{1}{\sqrt{2}}\left(\ket{0} - \ket{1}\right)
\end{split}
\end{equation*}

\item $\tf$ acts on three qubits. Given a three qubit state $\ket{abc}$, we get the following mapping:
\begin{equation*}
\tf\ket{abc} \rightarrow \ket{ab}\ket{(a \land b) \oplus c}
\end{equation*}
where the first two qubits do not change while the last qubit gets flipped if the first two qubits are both 1.
\end{itemize}

\fill

\noindent The first thing we do is reconstruct $Q_x$ so that it consists of \emph{only} $\h$ and $\tf$ gates that are sequenced one after the other. For example if we want to apply $H^{\otimes 2}$, we break it up into the equivalent sequence $(H \otimes I)(I \otimes H)$. This can be done in polynomial time. From here on out when we reference $Q_x$, assume that it is the reconstructed version. Now pretend we are a classical computer trying to simulate $Q_x$ starting with an initial input $\ket{\psi_0}$. We go through $Q_x$ one gate at a time. If we encounter an $\h_x$ gate, where $\h_x$ is applying $\h$ to the $x^{th}$ qubit, then we branch off two computation paths: one with the $x^{th}$ qubit set to 0, and the other with the $x^{th}$ qubit set to 1, keeping track of the resulting amplitudes for each. This makes sense upon examining our description of $\h$ above, where we see that it turns a classical bit to a superposition -- forking off two distinct computational paths \emph{captures} that superposition in the classical world. If we encounter a $\tf_{xyz}$ gate, where $\tf_{xyz}$ is applying $\tf$ to the $x^{th}$, $y^{th}$ and $z^{th}$ qubits, then we follow our description of $\tf$ above: leave $x$ and $y$ alone but flip $z$ iff $x \land y = 1$. With $\tf$ then, we see that we do not need to fork any additional paths as it does not create a new superposition. Thus our simulation of $Q_x$ is really maintaining a \emph{tree} where the nodes represent intermediate stages in the computation. Assume that we have $m$ gates in our circuit. Then each level $i$ of the tree represents the resulting superposition after applying the first $i$ gates, where $0 \leq i \leq m$. The nodes of the tree are the components of the superposition at their corresponding level. Thus for each node, we would like to maintain two pieces of information: the binary representation of that specific component, and its corresponding amplitude. In fact because we are applying only $\h$ and $\tf$ gates, every node will have the same amplitude as any other node on that level; this amplitude is $\frac{1}{\sqrt{2^m}}$ where $m$ is the number of nodes on that level. The leaves of our tree will contain the final superposition. Thus, a path from the root to \emph{a} leaf represents \emph{one} possible outcome assuming that we measure all of our qubits. Because we are using $\tf$ and $\h$ gates, it is possible to have two \emph{different} computation paths that produce the same final result (i.e. nodes on the same level can have the same superposition component). The exponential time complexity comes from the size of our tree, where we need to compute each superposition component in the internal nodes

\fill

\noindent \Example{2} Say our circuit consists of the gates $H_1$, $H_2$, $\tf$, and $H_1$ in that order. Assume that we start with an initial state of $\ket{000}$. Then our resulting computation tree will be: %NOTE: I couldn't create the tree from lecture, for some reason I was getting a ''no driver specified at all'' error when using qtree. So I have put this handwritten tree instead:

\includegraphics[scale = 0.7]{tree}

\fill

\noindent Notice in our tree that we have two possible computation paths that result in $000$ as the final outcome. These are $000, 000, 000, 000, 000$ and $000, 100, 100, 100, 000$.

\fill

\noindent Assume without any loss of generality that the first qubit in our final superposition contains our desired output, i.e. that $\dbret{1}{Q_x}{1} \approx \widetilde{p_1} = \pr(\text{First qubit} = 1)$ -- the remaining qubits are ancilla qubits. Then, we see that $\widetilde{p_1}$ is just the absolute value squared of the sum of the amplitudes of all the qubit-leaves where the first qubit is $1$. Because each path from root to leaf is unique, we can represent our final superposition at the leaves using the individual paths as our orthonormal basis. Let $P$ be the set of computation paths. Then our superposition is expressed as:
\begin{equation} \label{eq:bqp-psp}
\sum_{p \in P} \left(\frac{1}{\sqrt{2}}\right)^{\abs{P}} \sign(p) \ket{p}
\end{equation}
where $\sign(p)$ is the signum function that indicates if the final superposition component upon traversing the path $p$ has negative or positive amplitude. Let $\alpha_x$ be the amplitude that represents outcome $x$ where $x \in \set{0, 1}^m$, with $m$ being the number of qubits at our leaf nodes. Then we see that our final superposition is also:
\begin{equation} \label{eq:bqp-sp}
\sum_{x \in \set{0, 1}^m} \alpha_x \ket{x}
\end{equation}
where
\begin{equation} \label{eq:bqp-alph}
\alpha_x =
\sum_{p \in P \text{ s.t. } p = x}  \left(\frac{1}{\sqrt{2}}\right)^{\abs{P}} \sign(p)
\end{equation}
where $p = x$ means that the path $p$ leads to the outcome $x$ -- remember that multiple paths can have the same outcome, which is why Eqn. \ref{eq:bqp-alph} is a summation. Thus, we see that we can calculate $\widetilde{p_1}$ as:
\begin{equation} \label{eq:bqp-p1tilde}
\widetilde{p_1} = \abs{\sum_{x \text{ s.t. } x_1 = 1} \alpha_x}^2
\end{equation}
where $x \in \set{0, 1}^m$ and $x_1 = 1$ indicates that $x$'s first qubit is $1$. Equation \ref{eq:bqp-p1tilde} is mathematically expressing what we said earlier.

\fill

\noindent Because new computational paths are only forked when we apply $\h$, our worst-case complexity for the size of our tree is when $Q_x$ has $h$ Hadamard gates. Here $|P| = 2^h$ so that there are $2^h$ leaves in the tree and hence $2^h$ possible paths, resulting in an exponential worst-case time. Thus, $\bqp \subseteq \exp$.

\fill

\noindent In fact if we reuse the space when calculating the amplitudes, it turns out that we only need polynomial space to calculate $\widetilde{p_1}$ so that $\bqp \subseteq \pspace$. We can adapt the proof even further to show that $\bqp \subseteq \pp$.

%==============================%
\section{$\qma$}
%==============================%
We will now present the quantum analogue of $\np$, $\qma$. We use $\ma$ instead of $\np$ because the quantum world is random. Like $\np$ for a classical computer, $\qma$ represents problems that are efficiently verifiable by a quantum computer.

\fill

\noindent We say that a promise $A \in \qma$ if there exists a collection of quantum circuits $Q$ such that for all $Q_x \in Q$ where $x$ is a valid input, $\abs{Q_x} = \poly(\abs{x})$,
\begin{itemize}
\item \emph{Completeness:} If $x \in A_y$ then there exists a state $\rho$ on $\poly(\abs{x})$ qubits such that
\begin{equation*}
\dbret{1}{Q_x(\rho)}{1} \geq \frac{2}{3}
\end{equation*}
We say that $\rho$ is a ``quantum certificate'' which verifies that $x \in A_y$.

\item \emph{Soundness:} If $x \in A_n$ then for all states $\rho$ on $\poly(\abs{x})$ qubits:
\begin{equation*}
\dbret{1}{Q_x(\rho)}{1} \leq \frac{1}{3}
\end{equation*}
\end{itemize}
We assume that in $Q_x$, the first qubit yields the answer to our decision problem.

\fill

\noindent Here are some things we can say about $\qma$.
\begin{enumerate}
\item $\bqp \subseteq \qma$ and $\bpp \subseteq \qma$ since we do not need a certificate to decide a $\bqp$ or $\bpp$ instance -- we can just directly solve the problem by running $x$ through our circuit $Q_x$.

\item $\np \subseteq \ma \subseteq \qma$. We know why $\np \subseteq \ma$. $\ma \subseteq \qma$ for the same reason as $\bpp \subseteq \bqp$ -- we can create a polynomial-size reversible quantum circuit from any classical circuit, and encode any classical certificate as a corresponding quantum certificate.

\item $\qma \subseteq \pp$
\end{enumerate}
For more information on all the complexity classes out there, see the \href{https://complexityzoo.uwaterloo.ca/Complexity_Zoo}{Complexity Zoo}.

%==============================%
\section{Interactive Proofs}
%==============================%
There is a recurrent pattern in the complexity classes $\np$, $\ma$ and $\qma$. In each case, we have an all-powerful ``prover'' (think computing machine) that prepares a certificate $c$ which we pass along to a verifier. $c$ represents a possible ``solution'' candidate to a problem instance encoded by some input $x$. $A$ is the general category that these problems fall under, where $A_y$ represents instances that do have a solution and $A_n$ those instances that don't. For example if $A$ is integer factorization, then $x$ is the encoded input $(N, k)$. If $x$ \emph{has} a solution, i.e. if $x \in A_y$, then our verifier will accept any $c$ that \emph{is} a solution -- we don't care what it does for those $c$s that don't correspond to valid solutions. However if our problem does not have a solution, i.e. if $x \in A_n$, then our verifier will reject \emph{all} certificates $c$ -- it cannot be fooled by the prover into accepting a solution candidate for an unsolvable problem. Our verifier must makes its decision in polynomial time with respect to the input size, represented by the sizes of $x$ and $c$. What we have just now (informally) described is the complexity class $\np$. For $\ma$ and $\qma$, our verifier can make mistakes; the probability that it does so is bounded. In $\qma$ for example, we require our verifier to accept a solution certificate $c$ for $x \in A_y$ with at least $2/3$ probability (1), while ensuring that any certificate $c$ for an $x \in A_n$ is accepted with probability at most $1/3$ (2). The mistake in (1) is rejecting a solution certificate $c$ while in (2), it is accepting any certificate $c$. In both cases, the mistake probability is bounded above by $1/3$.

\fill

\noindent We can generalize the interaction between the prover and the verifier by letting the prover send multiple certificates (messages) instead of a single certificate to the verifier, which is still required to make its decision in polynomial time. Here, we say that the prover and the verifier form an \emph{interactive proof system}. The complexity class $\ip$ represents those problems $A$ that have an interactive proof system; $\qip$ is similar to $\ip$ except that the prover can send quantum messages to the verifier in addition to classical ones. From our preceding discussion, $\np$ and $\ma$ are special cases of $\ip$ while $\qma$ is a special case of $\qip$. Several breakthrough results have shown that $\ip = \qip = \pspace$.

\fill

\noindent {\bf Aside:} You might be wondering why we coined the term $\ma$. The answer is from the tale of King Arthur. There, Merlin was an all-powerful wizard while Arthur was an ordinary mortal. In $\ip$, we have an ``all-powerful'' prover (Merlin) and a ``puny'' verifier (Arthur) -- putting Merlin and Arthur together, we get the name $\ma$.

%==============================%
\section{Group Non-membership}
%==============================%
We now present the group non-membership (GNM) problem. This is a problem that we know is in $\qma$ but not if it is in $\ma$. Experts think that it probably isn't. Here is our problem statement.
\begin{itemize}
\item {\bf Input:} $\left<g_1, \dots, g_k\right> = H \leq G$ and $x \in G$
\item {\bf Output:} If $x \notin H$, then YES. Otherwise, NO.
\end{itemize}
where $g_1, \dots, g_k$ are the generators of some subgroup $H$ of a group $G$ -- remember from group theory that we can specify any group $G$ using only its generators. Informally, given a subgroup $H$ and an element $x$ from the subsuming group $G$, $\gnm$ asks if $x \notin  H$. We will now prove that $\gnm \in \qma$ by constructing our verifier.

\fill

\noindent We will represent the subgroup $H$ as a uniform superposition over its group elements, defined as:
\begin{equation} \label{eq:gnm-h}
\ket{H} = \frac{1}{\sqrt{\abs{H}}} \sum_{h \in H} \ket{h}
\end{equation}
Now consider $\ket{H_x}$ defined below:
\begin{equation} \label{eq:gnm-hx}
\ket{H_x} = \frac{1}{\sqrt{\abs{H}}} \sum_{h \in H} \ket{h\bullet x}
\end{equation}
where $H_x = \set{h_1x, h_2x, \dots} = Hx$ is a coset of $H$ in $G$. If $x \in H$, then we know from group theory that $H_x = H$ implying that $\ket{H_x} = \ket{H}$. Otherwise if $x \notin H_x$, then $H_x \cap H = \emptyset$. In quantum terms, this means that $\ket{H_x}$ is orthogonal to $\ket{H}$, i.e. that $\bret{H_x}{H} = 0$. This makes sense since here, $H_x$ and $H$ are independent sets and orthogonality captures independence. Now consider the following circuit that will form our verifier:

\centerline{
	\Qcircuit @C=1em @R=0.75em {
		\lstick{\ket{0}} & \gate{\h} & \ctrl{1} & \gate{\h} & \meter \\
		\lstick{\ket{H}} & \qw & \gate{M_x} & \qw & \qw
	}
}

where $M_x$ is the controlled unitary that's defined as follows:
\begin{equation*} \label{eq:gnm-mx}
\begin{split}
M_x\left(\ket{0}\ket{H}\right) &\rightarrow \ket{0}\ket{H} \\
M_x\left(\ket{1}\ket{H}\right) &\rightarrow \ket{1}\ket{H_x}
\end{split}
\end{equation*}
Let's see what this circuit does starting with the initial state $\ket{0}\ket{H}$. After applying the first Hadamard gate to the first qubit, we get:
\begin{equation*}
\frac{1}{\sqrt{2}}(\ket{0}\ket{H} + \ket{1}\ket{H})
\end{equation*}
Applying $M_x$, we get:
\begin{equation*}
\frac{1}{\sqrt{2}}(\ket{0}\ket{H} + \ket{1}\ket{H_x})
\end{equation*}
Finally applying the last Hadamard gate to the first qubit, we get:
\begin{equation} \label{eq:gnm-final}
\frac{1}{2}
  \ket{0}\left(\ket{H} + \ket{H_x}\right)
+ \frac{1}{2}
\ket{1}\left(\ket{H} - \ket{H_x}\right)
\end{equation}
We now have two cases to consider:
\begin{enumerate}
\item $x \in H$ so that $H_x = H$. Here, Eqn. \ref{eq:gnm-final} reduces to $\ket{0}\ket{H}$ so that we will measure $0$ with certainty.

\item $x \notin H$. Then examining Eqn. \ref{eq:gnm-final}, we see that there is a $1/2$ chance of measuring $0$, and a $1/2$ chance of measuring $1$.
\end{enumerate}
Looking at Cases 1 and 2, we will say that our verifier outputs YES when we measure a $1$ on the first qubit, otherwise it outputs NO. Here's why we do this. When $x \in H$ and our prover gives us $\ket{H}$ as a certificate, we know for sure that our circuit will measure $0$ from Case 1 so we output NO to correctly reject $\ket{H}$ -- the case when the prover gives us a certificate that isn't $\ket{H}$ will be considered shortly.

\fill

\noindent Consider the \emph{completeness} requirement. Here, when $x \notin H$ and we are given $\ket{H}$ as our certificate, we want our verifier to output YES with probability $p \geq 2/3$. Unfortunately from Case 2, $p = 1/2$ so our verifier is not good enough. However, all is not lost. We can tweak our verifier to instead run the circuit at most $t$ times. This is OK since our prover is all powerful, so it can generate as many copies of $\ket{H}$ as we need. This time, our verifier will output YES as soon as it measures a $1$. Now, our probability $p$ is the chance that we measure $1$ \emph{at least} once within $t$ trials, which we can calculate using a binomial distribution as:
\begin{equation*}
p = 1 - \left(\frac{1}{2}\right)^t
\end{equation*}
Setting $t = 2$ will yield $p = 3/4$, which is good enough.

\fill

\noindent Now we consider the \emph{soundness} requirement, using our tweaked verifier from above. Here we know that $x \in H$. We must show that our verifier will output NO for \emph{any} certificate given to it by the prover with probability \emph{at least} $2/3$ (complement of outputting YES with probability at most 1/3). We have two cases to consider here. If the prover gives us $\ket{H}$ as the certificate, then Case 1 from our analysis shows that we will always measure a $0$ in all $t$ iterations of running our circuit so that we correctly output NO with certainty. However if the prover gives us a certificate $c$ that isn't $\ket{H}$, we cannot pass $c$ into our verifier as-is because our circuit is only defined for $c = \ket{H}$. But we can construct a new circuit $T$ that does the following. If $c = \ket{H}$, it will run our verifier as described in the completeness proof above. Otherwise, $T$ will output NO. Thus, $T$ becomes our new verifier. The question now is how to test that $c = \ket{H}$. We cannot just check the generators of $H$ because the check is not always guaranteed to complete in polynomial time.

\fill

\noindent What we can do, however, is use a slightly modified version of the main circuit as a part of our testing procedure. This circuit is below:

\centerline{
	\Qcircuit @C=1em @R=0.75em {
		\lstick{\ket{0}} & \gate{\h} & \ctrl{1} & \gate{\h} & \meter \\
		\lstick{c} & \qw & \gate{M_z} & \qw & \qw
	}
}

\noindent where $M_z$ is the same as $M_x$ except that $z$ is a randomly chosen element of $H$, and we compute the coset $cz$ instead of $Hz$. From Case 1 above, if $c = \ket{H}$ then we will measure $0$ with certainty. Thus when we see a $1$, we know that $c \ne \ket{H}$ so that our test can output NO to correctly reject $c$. To ensure that our test works with the required probability for rejection, we can repeat our testing procedure several times using different, random elements of $z$ -- remember that our prover is all-powerful, so it can generate as many copies of $c$ as we need to do this. For more details on the analysis for this case, see \href{https://www.cs.cmu.edu/~odonnell/quantum15/lecture24.pdf}{these lectures notes}.

\fill

\noindent The above argument establishes that our new verifier exhibits the correct behavior for the soundness case; it is also polynomial in the size of the input. Further, our completeness argument still holds with this new verifier because the testing procedure will always measure $0$ when $c = \ket{H}$ so that the probability of outputting YES still depends only on the main circuit.

\fill

\noindent This completes our proof that $\gnm \in \qma$.

\end{document}


\documentclass[twocolumn,fleqn,layout]{article}
\usepackage{alltt}

\pagestyle{plain}
\flushbottom
\sloppy
\begin{document}

\input{macros}
\sloppy

\twocolumn[{\scriptsize \parbox{100mm}{Preprint of a paper
to appear in the Proceedings of the 1991 International Tutorial
and Workshop on the HOL Theorem Proving System, 27--30 August 1991,
Davis California (IEEE Computer Society Press).}}\vskip10mm
\begin{center}
{\Large\bf A Package for Inductive Relation Definitions in HOL}\\
\vskip24pt
T.\ F.\ Melham\\
\vskip12pt
University of Cambridge Computer Laboratory\\
New Museums Site, Pembroke Street\\
Cambridge, CB2 3QG, England.\\
\mbox{}\\
\vskip3mm
\end{center}]

\subsection*{\centering Abstract}

{\it\sloppy This paper describes a set of theorem proving tools based on a new
derived principle of definition in HOL, namely the introduction of relations
inductively defined by a set of rules.  Such inductive definitions abound in
computer science. Example application areas \mbox{include} reasoning about
structured operational semantics, type judgements, transition relations for
process algebras, \mbox{reduction} relations, and compositional proof systems.
The package described in this paper automates the derivation of certain
inductive definitions involved in these applications and provides the basic
tools needed for reasoning about the relations introduced by them.}

\vskip12pt
\section{Introduction}

The HOL user community has a strong tradition of taking a purely {\it
definitional\/} approach to using higher order logic. That is, the syntax of
the logic is extended with new notation not simply by postulating axioms to
give meaning to it, but rather by defining it in terms of existing expressions
of the logic that already have the required semantics.  The advantage of this
approach, as opposed to the axiomatic method, is that each of the primitive
rules of definition in the {\small HOL} logic---namely, constant definition,
constant specification, and type definition---is guaranteed to preserve
consistency.  The disadvantage is that these rules admit only
\mbox{definitions} that satisfy certain very restrictive rules of formation.
Definitions expressed in any other form must always be justified formally by
deriving them from equivalent, but possibly rather complex, primitive
definitions.

The {\small ML} metalanguage allows users to implement derived inference rules
in the {\small HOL} system and thus provides a facility for automating proofs
that justify derived rules of definition.  For example, recursive
\mbox{definitions} are not admitted by the primitive rules of definition of the
{\small HOL} logic.  But certain recursive type definitions and function
definitions are supported in the system by derived inference rules written in
{\small ML}~\cite{description,melham}.  The details of the primitive
definitions that underlie these rules are hidden from the user, and their
{\small ML} implementations are highly optimized. So these \mbox{derived}
principles of definition may simply be regarded as primitive by most users of
the system.

This paper describes a set of theorem-proving tools based on a new derived
principle of definition in {\small HOL} for defining relations inductively by a
set of rules. \mbox{Sections~\ref{ind-defs}} and~\ref{in-logic} give a general
introduction to the class of inductive definitions handled by the package and
explain the logical basis for these definitions.  The remaining sections
describe the {\small ML} functions provided by the package and briefly mention
some applications for which the package can be used.

\section{Inductive definitions}\label{ind-defs}

The following is a simple but typical example of a relation defined inductively
by a set of rules. (This example is taken from~\cite{pitts}.) Let $R \subseteq
A \times A$ be a binary relation on a set $A$.  The reflexive-transitive
closure of $R$ can be defined to be the least relation $R^{*} \subseteq A
\times A$ for which the following deduction rules hold.

\medskip

\[ \begin{array}[t]{@{}l}
   \hbox{{\small\bf R}\bf 1}\quad\Rule{}{R^{*}(x,y)}\;\; R(x,y) \\[6mm]
   \hbox{{\small\bf R}\bf 2}\quad\Rule{}{R^{*}(x,x)}\\[8mm]
   \hbox{{\small\bf R}\bf 3}\quad\Rule{R^{*}(x,z)\qquad R^{*}(z,y)}
         {R^{*}(x,y)}
\end{array} \]

\medskip

\noindent These rules state precisely the properties required of the
reflexive-transitive closure of the relation $R$.  Rule {{\small\bf R}\bf 1}
states that it must contain at least all the values in $R$; rule {{\small\bf
R}\bf 2} states that it must be reflexive; and rule {{\small\bf R}\bf 3} states
that it must be transitive.  The reflexive-transitive closure $R^{*}$ may
therefore simply be {\it defined\/} to be the least relation that satisfies
these conditions.  It then follows simply by definition that the rules
{{\small\bf R}\bf 1}, {{\small\bf R}\bf 2} and {{\small\bf R}\bf 3} are in fact
satisfied by $R^{*}$.  Moreover, it follows immediately that $R^{*}$ is a
subset of any other relation that satisfies these rules, since $R^{*}$ is
defined to be the {\it least\/} such relation.  This means that $R^{*}$
contains only those pairs of values that it must contain by virtue of
satisfying the rules. As will be discussed below, this property gives rise to
an induction principle for reasoning about the relation $R^{*}$.

The definition given above is valid because the rules {{\small\bf R}\bf 1},
{{\small\bf R}\bf 2}, and {{\small\bf R}\bf 3} make only positive statements
about the elements of $R^{*}$.  This guarantees that the least relation
satisfying these rules does exist.  In particular, if the rules have this form,
then one can show that the intersection of any set of relations that satisfy
the rules also satisfies the rules.  Moreover, at least one binary relation
satisfies the rules, namely the maximal relation $A \times A$. The `least' or
smallest relation that satisfies the rules may therefore legitimately be
defined to be the intersection of all such relations.

In general, an inductive definition of an $n$-place \mbox{relation} $R$
consists of a set of rules of the form:

\smallskip

\[ \Rule{R(t_1^1,\dots,t_n^1) \quad \cdots \quad R(t_1^i,\dots,t_n^i)}
        {R(t_1,\dots,t_n)}\;\;C_1\:\cdots\:C_j \]

\smallskip

\noindent The terms above the line are the {\it premisses\/} of the rule, each
of which makes a positive assertion of membership in the relation $R$.  The
term below the line, called the {\it conclusion\/} of the rule, likewise
asserts membership in $R$. The terms $C_1$,\dots,$C_j$ are {\it side
conditions\/} on the rule; these may be arbitrary propositions not involving
the relation $R$ being defined. A relation $R$ is {\it closed\/} under such a
rule if whenever the premisses and side conditions hold, the conclusion also
holds.  The relation {\it inductively defined\/} by a collection of such rules
is the least relation closed under all the rules.

\subsection{Rule induction}\label{rule-ind}

By virtue of its definition as the {\it least\/} relation closed under a set of
rules, every inductively defined relation comes with an associated induction
principle.  This principle of {\it rule induction\/} is essential for many
proofs involving such relations. (The term `rule induction' was coined by Glynn
Winskel in~\cite{winskel}).

The principle of rule induction for an inductively defined relation may be
stated briefly as follows. Let $R$ be an $n$-place relation inductively defined
by a set of rules, and suppose we wish to show that every element in $R$ has a
certain property $P$:

{\samepage
\begin{equation}\label{fact1}
\hbox{\rm
if}\;\;R(x_1,\dots,x_n)\;\;\hbox{then}\;\;P[x_1,\dots,x_n]
\end{equation}

\noindent Since $R$ is} \pagebreak[3] the least relation closed under the
rules, any relation $S$ which is also closed under the rules has the property
that $R \subseteq S$.  Now, let

\[S = \{(x_1,\dots,x_n) \mid P[x_1,\dots,x_n]\}\]

\noindent Then to prove the desired property of $R$, it suffices to show that
the relation $S$ is closed under the rules that define $R$. For if the relation
$S$ in fact is closed under the rules, then we have that $R \subseteq S$ and
therefore that every element of $R$ has the defining property of $S$---i.e.\
statement~(\ref{fact1}) holds of the relation $R$.

For the relation $R^{*}$ defined above, the principle of rule induction is
stated as follows. In order to prove that a property $P[x,y]$ holds for all
$x$ and $y$ for which $R^{*}(x,y)$, it suffices to show that:

\begin{itemize}

\item for all $x$ and $y$, $R(x,y)$ implies $P[x,y]$

\item for all $x$, $P[x,x]$

\item for all $x$, $y$, and $z$, $P[x,z]$ and $P[z,y]$ imply $P[x,y]$

\end{itemize}

\noindent This is an inductive form of argument: if the property $P$ holds in
the `base cases' corresponding to rules {{\small\bf R}\bf 1} and {{\small\bf
R}\bf 2}, and if $P$ is preserved by the rule {{\small\bf R}\bf 3} (the `step
case' of the induction), then every pair in $R^{*}$ has the property $P$.  A
similar induction principle holds for every relation inductively defined by a
set of rules.

\section{Inductive definitions in logic}\label{in-logic}

Inductive definitions are based on the concept of a relation being closed under
a set of rules.  Since rules are essentially implications---{\it if\/} the
premisses and side conditions hold, {\it then\/} the conclusion holds---it is
straightforward to express this concept in logic.

Consider, for example, the rules given above for reflexive-transitive closure.
Let $R : \alpha{\rightarrow}\alpha{\rightarrow}bool$ be a fixed but arbitrary
 relation on $\alpha$. (Here, a relation is represented by a curried function;
but we shall continue to speak loosely of a pair of values $x$ and $y$ as being
`in' the relation $R$ when $R\;x\;y$ holds.)  The following formula then
asserts that a relation $P : \alpha{\rightarrow}\alpha{\rightarrow}bool$ is
closed under the rules defining the reflexive-transitive closure of $R$:

\smallskip
\[\begin{array}[t]{@{}l}
 (\forall x\:y.\:R\;x\;y \supset P\;x\;y)\; \wedge \\
 (\forall x.\:P\;x\;x)\; \wedge \\
 (\forall x\:y.\:(\exists z.\:P\;x\;z \wedge P\;z\;y) \supset P\;x\;y)
\end{array}\]
\smallskip

\noindent Each rule is expressed by a quantified implication of its conclusion
by the conjunction of its premisses and side conditions.  A rule with no side
conditions or premisses is just represented by a universally quantified
assertion of its conclusion. Closure of a relation under any set of rules of
the form discussed above can be expressed in logic in a similar way.

Using this method of expressing the notion of \mbox{closure} under a set of
rules, one can define the {\it least\/} relation closed under a set of rules
simply by taking the intersection of all such relations.  For example, a
function

\[ {\sf Rtc} : (\alpha{\rightarrow}\alpha{\rightarrow}bool) \rightarrow
(\alpha{\rightarrow}\alpha{\rightarrow}bool) \]

\noindent that maps an arbitrary relation $R :
\alpha{\rightarrow}\alpha{\rightarrow}bool$ to its reflexive-transitive closure
${\sf Rtc}\;R$ can be defined in the {\small HOL} logic by the constant
definition:

\[\vdash \begin{array}[t]{@{}l}
\forall R\:x\:y.\:{\sf Rtc}\;R\;x\;y\;= \\
\quad\forall P.\:(\begin{array}[t]{@{}l}
  (\forall x\: y.\:R\;x\;y \supset P\;x\;y)\; \wedge \\
  (\forall x.\:P\;x\;x)\; \wedge \\
  (\forall x\: y.\:(\exists z.\:P\;x\;z \wedge P\;z\;y) \supset P\;x\;y)) \\
\quad\supset\\
P\;x\;y
\end{array}\end{array}\]

\noindent This definition states that a pair $x$ and $y$ is in the relation
${\sf Rtc}\;R$ exactly when it is in every relation $P$ closed under the rules
for reflexive-transitive closure. That is, ${\sf Rtc}\;R$ is \mbox{defined} to
be the intersection of all relations closed under these rules.  As will be
discussed in the section that follows, this indeed makes ${\sf Rtc}\;R$ the
least such relation, as required.

\subsection{Deriving the rules and rule induction}

Any relation intended to be defined inductively by a set of rules can be
defined formally in the {\small HOL} logic by a constant definition of the kind
illustrated by the {\sf Rtc} example given above.  Such a definition, however,
merely introduces the relation as the intersection of all relations that
satisfy the desired set of rules.  The proof obligations of a derived principle
of inductive definition are, first of all, to show that the resulting relation
in fact does satisfy these rules, and secondly to show that it is indeed the
least such relation.  It is these proof obligations which are automated by the
{\small HOL} inference rule described below in section~\ref{newind}.

In the case of the simple reflexive-transitive closure example, the first proof
obligation is to show that:

\[ \begin{array}[t]{@{}l}
   \vdash \forall R\:x\:y.\:R\;x\;y \supset {\sf Rtc}\;R\;x\;y \\[2mm]
   \vdash \forall R\:x.\:{\sf Rtc}\;R\;x\;x \\[2mm]
   \vdash \forall R\:x\:y.\:
        (\exists z.\:{\sf Rtc}\;R\;x\;z \wedge {\sf Rtc}\;R\;z\;y) \supset
        {\sf Rtc}\;R\;x\;y
\end{array}\]

\noindent That is, one must prove that the rules {{\small\bf R}\bf 1},
{{\small\bf R}\bf 2}, and {{\small\bf R}\bf 3} follow from the somewhat
indirect formal definition of the relation ${\sf Rtc}\;R$ given in the previous
section.  The second proof obligation is to show that ${\sf Rtc}\;R$ is the
least relation that satisfies these rules:

\[\vdash \begin{array}[t]{@{}l}
\forall R\:P.\:\begin{array}[t]{@{}l}
  (\begin{array}[t]{@{}l}
  (\forall x\:y.\:R\;x\;y \supset P\;x\;y)\; \wedge \\
  (\forall x.\:P\;x\;x)\; \wedge \\
  (\forall x\:y.\:(\exists z.\:P\;x\;z \wedge P\;z\;y)
    \supset P\;x\;y))\end{array}\\
\quad\supset\\
\forall x\:y.\: {\sf Rtc}\;R\;x\;y \supset P\;x\;y
\end{array}\end{array}\]

\noindent This is the principle of rule induction for ${\sf Rtc}\;R$.  These
four theorems constitute a complete statement of the defining properties of
reflexive-transitive closure. All four can be proved fully automatically in
{\small HOL} by the derived inference rule described in the next section.

\section{Automation}\label{newind}

The main component of the inductive definitions package is an {\small ML}
function that takes as an argument a list of rules and automatically proves the
defining properties of the \mbox{relation} inductively defined by them.  More
precisely, this derived {\small HOL} inference rule builds a term that denotes
the least relation closed under the rules using the intersection construction
described in the previous section.  A constant is then introduced (via a
constant specification) to name this relation.  The result is a set of theorems
stating that the newly-defined relation is the least relation closed under the
rules supplied by the user.

The {\small ML} function that implements this principle of inductive definition
is:

\medskip

\noindent\begin{tabular}{@{\hskip\mathindent}l@{\hskip4.6mm}l@{}}
\verb!new_inductive_definition! & \mbox{} \\
\verb! : bool ->! & ({\it infix flag\/})\\
\verb!   string ->! & ({\it defn.\ name\/})\\
\verb!   (term # term list) ->! & ({\it pattern\/})\\
\verb!   (term list # term) list ->! & ({\it rules\/})\\
\verb!   (thm list # thm)! & ({\it result\/})
\end{tabular}

\medskip

\noindent The first argument to this function is a boolean flag which indicates
if the constant that is defined is to have infix syntactic status.  The second
argument is the name under which the resulting definition will be saved on
disk.  The third argument is a `pattern' that supplies information which is
needed because this {\small ML} function can be used to define classes of
inductively defined relations, rather than just single instances of these
relations. Details of the purpose and format of this pattern will be explained
later.  The final argument is a list of rules, each of which is represented by
a pair of the form:

\[ \hbox{\verb!([!}\,
\hbox{\it premisses and side conditions\/}\,\hbox{\verb!], !}
 \hbox{\it conclusion\/}\hbox{\verb!)!} \]

\noindent The first component is a list of the premisses and side conditions,
which may be arranged in any order.  The second component is the conclusion of
the rule.  Side conditions can be arbitrary boolean terms, provided they do not
mention the relation being defined.  The premisses and conclusion must be
positive assertions of membership in the relation being defined.  The precise
form that these assertions must take is explained later, but roughly speaking
the premisses and conclusion of a rule must be terms of form
\verb!"!$R\;\,t_1\;\,\dots\;\,t_n$\verb!"!, where

\[ R\; \hbox{\verb!:!}\; \sigma_1 \;\hbox{\verb!->!}\; \dots
\;\hbox{\verb!->!}\; \sigma_n \;\hbox{\verb!->!}\; \hbox{\verb!bool!}\]

\noindent is a variable representing the $n$-place relation that is to be
defined, and each $t_i \hbox{\verb!:!} \sigma_i$ is an arbitrary term not
containing $R$.

Given an infix flag, a name, a pattern, and a list of rules, the {\small ML}
function \verb!new_inductive_definition! automatically proves the existence of
the least relation that satisfies these rules.  A constant is then introduced
to denote this relation using a constant specification, the result of which is
saved on disk under the supplied name. The value returned is a pair consisting
of a list of theorems which state that the newly-defined relation satisfies the
rules, together with a \mbox{theorem} asserting rule induction for the
relation.  These theorems give a complete statement of the defining properties
for the least relation closed under the specified set of rules.

\subsection{A simple example}

The following example {\small HOL} session shows how the function
\verb!new_inductive_definition! can be used to inductively define the set of
even natural numbers.

\begin{session}\begin{verbatim}
#let (rules,ind) =
   let Even = "Even:num->bool" in
   new_inductive_definition false `Even`
   ("^Even n", [])

   [ [
     % ----------------------------- % ],
                 "^Even 0"           ;

     [           "^Even n"
     % ----------------------------- % ],
               "^Even (n+2)"        ];;
\end{verbatim}\end{session}

\noindent The first rule in this definition states that \verb!0! is an even
natural number, and the second rule states that if \verb!n! is even then
\verb!n+2! is also even.  (Antiquotation and {\small ML} comments are used to
give a readable presentation of these rules.)  Since the even natural numbers
are \mbox{exactly} those numbers obtainable from zero by adding multiples of
two, these rules inductively define `\verb!Even n!' such that it holds
\mbox{precisely} when \verb!n! is even.

The value supplied for the pattern in this example is the pair
\verb!("Even n",[])!.  The first component of this pair indicates that
the constant to be defined, namely \verb!Even!, is a one-place
function with typical argument \verb!n!.  In general, the second
component of a pattern is a non-empty list only when a {\it class\/}
of relations is being defined (see below).  In this example,
{\verb!Even!} is a single inductively-defined predicate, and the list
component of the pattern is therefore empty.

When the definition shown in box 1 is evaluated,
\verb!new_inductive_definition! automatically proves the existence of the least
predicate closed under the given list of rules and then defines the constant
\verb!Even! to denote this predicate.  The following automatically-proved
theorems about \verb!Even! are then returned:

\begin{session}\begin{alltt}
rules =
[\(\vdash\) Even 0;
 \(\vdash\) \(\forall\,\)n. Even n \(\supset\) Even(n + 2)] : thm list
ind =
\(\vdash\) \(\forall\,\)P. P 0 \(\wedge\) (\(\forall\,\)n. P n \(\supset\) P(n + 2)) \(\supset\)
      \(\,\)(\(\forall\,\)n. Even n \(\supset\) P n)
\end{alltt}\end{session}

\noindent The theorems bound to the {\small ML} identifier \verb!rules! state
that the required rules hold of the predicate \verb!Even!. And the rule
induction theorem bound to \verb!ind! states that the set of numbers for which
\verb!Even! holds is the least set that satisfies these rules.

An analogous set of defining theorems can be proved automatically for any
particular relation inductively defined by a list of rules.  The next section
shows how this derived principle of inductive definition in {\small HOL} can
also be used to define a parameterized class of relations.

\subsection{Defining a class of relations}

The constant {\sf Rtc} defined in section~\ref{in-logic} is not itself an
inductively-defined relation, but rather a function that maps an arbitrary
relation $R$ to an inductively-defined relation ${\sf Rtc}\;R$.  The function
{\sf Rtc} therefore represents an entire class of inductively-defined
relations, one for each possible value of $R$.

The information that is required by the derived rule
\verb!new_inductive_definition! in order to handle the definition of such
functions is supplied by its pattern argument.  In the general case, a pattern
is a pair of the following form:

\[\hbox{\verb!("!}R\;v_1\;\dots\;v_n\hbox{\verb!",!}\;
 \hbox{\verb!["!}v_i\hbox{\verb!";!}\dots\hbox{\verb!;"!}v_j\hbox{\verb!"])!}
\]

\noindent The first component of the pattern is an application of the $n$-place
curried function that is to be defined (in this case, $R$) to $n$ distinct
variables $v_1$, \dots, $v_n$.  The second component is a list of those
variables that occur at the positions in this application which correspond to
the parameters of class of inductively-defined relations, rather than to the
actual arguments to these relations.

An example of the role of the pattern argument in defining a class of relations
is provided by the following definition of reflexive-transitive closure in
{\small HOL}.

\begin{session}\begin{verbatim}
#let (rules,ind) =
   let Rtc = "Rtc:(*->*->bool)->*->*->bool"
 in
   new_inductive_definition false `Rtc`
   ("^Rtc R x y", ["R:*->*->bool"])

   [ [      "R (x:*) (y:*):bool"
     % ----------------------------- % ],
               "^Rtc R x y"          ;

     [
     %------------------------------ % ],
               "^Rtc R x x"          ;

     [  "^Rtc R x z";  "^Rtc R z y"
     %------------------------------ % ],
               "^Rtc R x y"          ];;
\end{verbatim}\end{session}

\noindent The pattern in this case is the pair:

\medskip

\noindent\hskip\mathindent\verb!("Rtc R x y", ["R:*->*->bool"])!

\medskip

\noindent The first component of this pattern specifies that the function
\verb!Rtc! is to take three arguments in total---a \mbox{relation} \verb!R!,
and two values \verb!x! and \verb!y!.  The \mbox{second} part of the pattern
(the list containing just \verb!R!) specifies that the relation argument
\verb!R! is to be a parameter to the class of inductively-defined relations
that will be represented by \verb!Rtc!.  The remaining variables \verb!x! and
\verb!y! are assumed to indicate the positions of actual arguments to the
predicate that represents these relations.

The result of evaluating this inductive definition in {\small HOL} is the
following collection of theorems:

\begin{session}\begin{alltt}
rules =
[\(\vdash\) \(\forall\,\)R x y. R x y \(\supset\) Rtc R x y;
 \(\vdash\) \(\forall\,\)R x. Rtc R x x;
 \(\vdash\) \(\forall\,\)R x y. (\(\exists\,\)z. Rtc R x z \(\wedge\) Rtc R z y)
              \(\supset\)
            Rtc R x y] : thm list
ind =
\(\vdash\) \(\forall\,\)R P.
   (\(\forall\,\)x y. R x y \(\supset\) P x y) \(\wedge\)
   (\(\forall\,\)x. P x x) \(\wedge\)
   (\(\forall\,\)x y. (\(\exists\,\)z. P x z \(\wedge\) P z y) \(\supset\) P x y)
      \(\supset\)
   (\(\forall\,\)x y. Rtc R x y \(\supset\) P x y)
\end{alltt}\end{session}

\noindent Here, the {\small ML} variable \verb!rules! has been bound to a list
of theorems which state the three rules that inductively define the
reflexive-transitive closure of a relation.  The \mbox{theorem} \verb!ind!
states the corresponding principle of rule induction for an inductively-defined
relation \verb!Rtc R!.

\subsection{Stating premisses and conclusions}

In addition to the use of the pattern argument, the \verb!Rtc! example also
illustrates a restriction on the form in which the premisses and conclusions of
rules must be supplied to \verb!new_inductive_definition!.  As was mentioned
above, premisses and conclusions must be positive assertions of membership of
the form

\medskip

\noindent\hskip\mathindent\verb!"!$R\;\,t_1\;\,\dots\;\,t_n$\verb!"!

\medskip

\noindent where $R$ is a variable that stands for the function to be defined.
The restriction is that some of the terms among the arguments $t_1$, \dots,
$t_n$ in such an \mbox{assertion} must be variables---namely, the terms that
occur at \mbox{positions} which, according to the supplied pattern,
\mbox{correspond} to the parameters of a class of relations.  In particular,
the terms that occur at these positions must be the same variables given in the
pattern itself.

The rules for reflexive-transitive closure shown in box 3 conform to
this restriction.  Here, the pattern indicates that in the typical
assertion of membership \verb!"Rtc R x y"! (i.e.\ the first component
of the pattern), the variable \verb!R! marks the position of a
parameter to the class of relations to be defined.  Every premiss and
conclusion mentioned in the rules must therefore be a term of the form
$\hbox{\verb!"Rtc R!}\;\,t_1\;\,t_2\hbox{\verb!"!}$, where the
arguments $t_1$ and $t_2$ may be arbitrary terms but the parameter
\verb!R! must be the variable given in the pattern.

\section{A tactic for rule induction}

The inductive definitions package in {\small HOL} includes a number of
auxiliary functions that support reasoning about inductively-defined relations,
in addition to the derived rule of definition itself.  The most important of
these is the following general tactic for goal-directed proofs by rule
induction:

\medskip

\noindent\begin{tabular}{@{\hskip\mathindent}l@{\hskip12.7mm}l@{}}
\verb!RULE_INDUCT_THEN! & \mbox{} \\
\verb! : thm ->! & ({\it induction thm\/})\\
\verb!   (thm -> tactic) ->! & ({\it premiss cont.\/})\\
\verb!   (thm -> tactic) ->! & ({\it side cond.\ cont.\/})\\
\verb!   tactic! & ({\it result\/})
\end{tabular}

\medskip

\noindent The first argument to this function is the rule \mbox{induction}
theorem returned by \verb!new_inductive_definition! for a given
inductively-defined relation.  Like the general structural induction tactic in
{\small HOL}, the rule induction tactic is parameterized by functions that
determine what is done with induction hypotheses. These may be either premisses
or side conditions, and the user may wish to treat these two kinds of induction
hypotheses differently. Two separate theorem continuations are therefore
supplied as the second and third arguments to the function
\verb!RULE_INDUCT_THEN!.

Given the rule induction theorem for an inductively-defined $n$-ary relation
$R$, the function described above returns a specialized rule induction tactic
that reduces goals of the form:

\[ \hbox{\verb!"!}\forall x_1\;\dots\;x_n\hbox{\verb!.!}\;
  R \;x_1\;\dots\;x_n \supset P[x_1,\dots,x_n]\hbox{\verb!"!} \]

\noindent to the subgoal(s) of proving that the property $P$ is preserved by
the rules that inductively define $R$.  The rule induction theorem for
\verb!Rtc!, for example, is:

\begin{session}\begin{alltt}
#ind;;
\(\vdash\) \(\forall\,\)R P.
   (\(\forall\,\)x y. R x y \(\supset\) P x y) \(\wedge\)
   (\(\forall\,\)x. P x x) \(\wedge\)
   (\(\forall\,\)x y. (\(\exists\,\)z. P x z \(\wedge\) P z y) \(\supset\) P x y)
      \(\supset\)
   (\(\forall\,\)x y. Rtc R x y \(\supset\) P x y)
\end{alltt}\end{session}

\noindent A rule induction tactic for \verb!Rtc! can be constructed from this
theorem by making the simple {\small ML} definition:

\begin{session}\begin{alltt}
#let Rtc_INDUCT_TAC =
   RULE_INDUCT_THEN ind
      ASSUME_TAC ASSUME_TAC;;
Rtc_INDUCT_TAC = - : tactic
\end{alltt}\end{session}

\noindent The use of \verb!ASSUME_TAC! in this definition means that the
induction hypotheses arising from the premisses and side conditions of the
rules are to be added to the assumptions of the subgoals that are generated.
The resulting rule induction tactic for \verb!Rtc!  is described by:

\bigskip

\noindent\begin{tabular}{@{\hskip\mathindent}c@{}}
$\Gamma\;\:$\verb!?-!$\;\:\forall x\:y$\verb!. Rtc!$\;R\;x\;y \supset P[x,y]$\\
\trule{62mm}\\
$\Gamma \cup \{R\;x\;y\}\;\:$\verb!?-!$\;\:P[x,y]$\\[1mm]
$\Gamma\;\:$\verb!?-!$\;\:\forall x$\verb!.!$\;P[x,x]$\\[1mm]
$\Gamma\;{\cup}\;\{P[x,z],\:P[z,y]\}\;\:$\verb!?-!$\;\:P[x,y]$
\end{tabular}

\bigskip

\noindent This tactic implements the induction scheme described above in
section~\ref{rule-ind}. It reduces the goal of proving that a property $P[x,y]$
holds for all pairs $x$ and $y$ related by $\hbox{\verb!Rtc!}\;R$ to showing
that this property is preserved by the rules that inductively define this
relation.

\subsection{An example}

The following session shows how the rule induction tactic for \verb!Rtc!
constructed in the previous section can be used to prove a simple theorem about
this relation.  The aim is to show that the reflexive-transitive closure of a
symmetric relation is also symmetric.  The proof begins by using the {\small
HOL} subgoal package (see~\cite{description}) to set up an appropriate goal to
be proved:

\smallskip

\begin{session}\begin{alltt}
#set_goal
   (["\(\forall\,\)x:*. \(\forall\,\)y. R x y \(\supset\) R y x"],
     "\(\forall\,\)x:*. \(\forall\,\)y. Rtc R x y \(\supset\) Rtc R y x");;
"\(\forall\,\)x y. Rtc R x y \(\supset\) Rtc R y x"
    [ "\(\forall\,\)x y. R x y \(\supset\) R y x" ]

() : void
\end{alltt}\end{session}

\smallskip

\noindent The assumption of the goal is that the relation \verb!R! is
symmetric, and the conclusion states that the closure \verb!Rtc R! is also
symmetric.  The conclusion of the goal is in precisely the right form for a
proof by rule induction using the induction tactic described above.  Applying
this tactic results in:

\smallskip

\begin{session}\begin{alltt}
#expand Rtc_INDUCT_TAC;;
OK..
3 subgoals
"Rtc R y x"                       {\rm({\it{subgoal 1\/}})}
    [ "\(\forall\,\)x y. R x y \(\supset\) R y x" ]
    [ "Rtc R z x" ]
    [ "Rtc R y z" ]

"\(\forall\,\)x. Rtc R x x"                   \(\!\){\rm({\it{subgoal 2\/}})}
    [ "\(\forall\,\)x y. R x y \(\supset\) R y x" ]

"Rtc R y x"                       {\rm({\it{subgoal 3\/}})}
    [ "\(\forall\,\)x y. R x y \(\supset\) R y x" ]
    [ "R x y" ]

() : void
\end{alltt}\end{session}

\smallskip

\noindent Subgoals 1 and 2 are trivial, since the relation \verb!Rtc R! is
transitive and reflexive by definition. The tactic proofs for these subgoals
can simply use the rules shown above in box~4. The proof of subgoal 3 is also
easy. The proposition \verb!"R y x"! follows immediately from the two
assumptions of the subgoal; and this proposition \mbox{together} with the fact
that

\bigskip

\noindent\hskip\mathindent$\vdash\forall\,$\verb!R x y. R x y !$\supset
$\verb! Rtc R x y!

\bigskip

\noindent directly entail the required conclusion.

The proof sketched above is a trivial example of the kind of reasoning
sometimes referred to as induction over the structure (or the depth) of
derivations in a deductive system stated by a set of rules.  This form of
inductive \mbox{argument}, which is very common in certain \mbox{areas} of
theory (for example, operational semantics and process algebras), is made
directly accessible in {\small HOL} by the tactic described in this section.

\section{Tactics and inference rules}

In addition to the rule induction tactic described above, the inductive
definitions package also provides mechanized support for generating tactics
from the theorems that state the rules for an inductively-defined relation.
This takes the form of an {\small ML} function:

\medskip

\noindent\hskip\mathindent\verb!RULE_TAC : thm -> tactic!

\medskip

\noindent The theorem argument to this function is expected to be a rule
expressed in the form proved by the derived principle of inductive definition
described in section~\ref{newind}. Given such a theorem, \verb!RULE_TAC!
constructs a tactic that inverts the rule stated by it.  The resulting tactic
reduces goals that match the conclusion of the rule to subgoals consisting of
the corresponding instances of its premisses and side conditions.

Consider, for example, the theorem which states the transitivity rule for
\verb!Rtc!:

\[\vdash \forall\,\hbox{\verb!R x y. !}\begin{array}[t]{@{}l}%
\hbox{\verb!(!}\exists\,
\hbox{\verb!z. Rtc R x z !}\wedge\hbox{\verb! Rtc R z y)!}\\
\quad \supset \\
\hbox{\verb!Rtc R x y!}
\end{array} \]

\noindent When applied to this theorem, the function \verb!RULE_TAC! returns
the tactic described by:

\bigskip

\noindent\begin{tabular}{@{\hskip\mathindent}c@{}}
$\Gamma\;\:$\verb!?-!$\;\:$\verb!Rtc!$\;R\;x\;y$ \\
\trule{62mm}\\
$\Gamma\;\:$\verb!?-!$\;\:\exists z
$\verb!.!$\;$\verb!Rtc!$\;R\;x\;z\;\:\wedge\;\;$\verb!Rtc!$\;R\;z\;y$
\end{tabular}

\bigskip

\noindent This tactic can then be used in goal-directed proofs about membership
in the inductively-defined relation {\verb!Rtc!$\;R$}.  The other two rules
that define \verb!Rtc!$\;R$ can also be converted into tactics using the
function \verb!RULE_TAC!.  The result is a complete set of {\small HOL} tactics
for goal-directed proofs in the deductive system comprising the three rules
that define reflexive-transitive closure.

It is intended that the inductive definitions package will also include a
function that maps rules stated as theorems to forward inference rules in
{\small HOL} (i.e.\ to {\small ML} functions).  For example, the transitivity
theorem shown above can be used to implement the following derived
inference rule:

\bigskip

\noindent\begin{tabular}{@{\hskip\mathindent}c@{}}
$\Gamma_1\;{\vdash}\;$\verb!Rtc!$\;R\;x\;z$\qquad
$\Gamma_2\;{\vdash}\;$\verb!Rtc!$\;R\;z\;y$\\
\rrule{62mm}\\
$\Gamma_1 \cup \Gamma_2\;{\vdash}\;$\verb!Rtc!$\;R\;x\;y$
\end{tabular}

\bigskip

\noindent Any rule expressed as a theorem of the form proved by the derived
principle of inductive definitions can likewise be converted into a forward
inference rule.  A function that \mbox{automatically} constructs such rules has
not yet been implemented, partly because it has not been found necessary for
the applications done so far (see section~\ref{appl}).  For completeness,
however, the author intends in future to add this function to the inductive
definitions package.

\section{Case analysis}

The final major component of the {\small HOL} package for inductive definitions
is an {\small ML} function that proves an \mbox{exhaustive} case analysis
theorem for any given relation inductively defined by a set of rules.  The name
and type of this function are:

\medskip

\noindent\hskip\mathindent\verb!derive_cases_thm : (thm list # thm) -> thm!

\medskip

\noindent The arguments to this function are the list of rules satisfied by an
inductively defined relation, together with its rule induction theorem. (These
are precisely the defining theorems which are proved and returned by
\verb!new_inductive_definition!.)  When supplied with these theorems,
\verb!derive_cases_thm! proves that if an assertion of membership in the
relation holds, then it holds only by virtue of the fact that one of the rules
can be used to derive it.  This allows one to drive the rules that define a
relation `backwards', inferring from the conclusion of one of the rules that
the premisses and side conditions hold.

The following interaction with the {\small HOL} system shows the theorem proved
by \verb!derive_cases_thm! for the \verb!Rtc! example introduced above.  The
{\small ML} variables \verb!rules! and \verb!ind! are assumed to have the
bindings shown above in box~4.

\smallskip

\begin{session}\begin{alltt}
#derive_cases_thm (rules,ind);;
\(\vdash\) \(\forall\,\)R x y.
    Rtc R x y \(\supset\)
      R x y   \(\vee\)
      (y = x) \(\vee\)
      (\(\exists\,\)z. Rtc R x z \(\wedge\) Rtc R z y)
\end{alltt}\end{session}

\smallskip

\noindent Roughly speaking, the resulting theorem states that if
\verb!Rtc R x y! holds, then either:

\begin{itemize}

\item it is derivable by the inclusion rule {{\small\bf R}\bf 1}, in which
case \verb!x! and \verb!y! are related by \verb!R!; or

\item it is derivable by the reflexivity rule {{\small\bf R}\bf 2}, in which
case \verb!x! and \verb!y! are equal; or

\item it is derivable by the transitivity rule {{\small\bf R}\bf 3}, in which
case there must be an intermediate value \verb!z! such that \verb!Rtc R x z!
and \verb!Rtc R z y!.

\end{itemize}

\noindent A similar theorem can be proved automatically for any relation
defined inductively using the package. Work is currently underway to strengthen
this theorem from an implication to an equation, so that it can be used for
rewriting.

\section{Applications}\label{appl}

In a joint project with Juanito Camilleri, a set of example proofs has
been developed to illustrate the potential for applications of the inductive
\mbox{definitions} package.  These examples include: the definition of an
operational semantics for a simple programming language and a proof that its
evaluation relation is \mbox{deterministic}; the definition of a reduction
relation for combinatory logic and a proof that it has the Church-Rosser
property; a definition of \mbox{provability} in a Hilbert style proof system
for minimal intuitionistic logic; the definition of a type system for
combinatory logic and a proof of the Curry-Howard isomorphism for typed
combinatory logic and minimal intuitionistic logic; and definitions of the
trace and transition semantics for a simple process algebra, \mbox{together}
with the proof of a formal statement of the relationship between them. A report
on this work is in preparation, and the {\small HOL} source code for the
examples will be made available to interested users.

\newpage

\begin{thebibliography}{9}

\bibitem{description}
DSTO, The University of Cambridge, and SRI
\mbox{International}, {\it The HOL System: DESCRIPTION} (1991).

\bibitem{melham}
T.\ F.\ Melham, `Automating Recursive Type Definitions
in Higher Order Logic',
in: {\it Current Trends in Hardware Verification and
Automated Theorem Proving\/}, edited by G.\ Birtwistle
and P.A.\ Subrahmanyam
(Springer-Verlag, 1989), pp.\ 341--386.

\bibitem{pitts}
A.\ M.\ Pitts, `Semantics of Programming Languages',
unpublished lecture notes, University of Cambridge Computer Laboratory
(October 1989).

\bibitem{winskel}
G.\ Winskel, `Introduction to the Formal Semantics of
Programming Languages', unpublished lecture notes, University of Cambridge
Computer Laboratory (October 1985).

\end{thebibliography}

\end{document}


\documentclass{beamer}
%
% Choose how your presentation looks.
%
% For more themes, color themes and font themes, see:
% http://deic.uab.es/~iblanes/beamer_gallery/index_by_theme.html
%
\usetheme{Ilmenau}      % or try Darmstadt, Madrid, Warsaw, ...
\usecolortheme{seahorse} % or try albatross, beaver, crane, ...
\usefonttheme{default}  % or try serif, structurebold, ...
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{caption}[numbered]
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgf}
\usepackage{units}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[mode=buildnew]{standalone}% requires -shell-escape
\usepackage{amsmath}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\setbeamertemplate{footline}[frame number]

\title{Building an end-to-end speech recognizer}
\author{Moritz Wolter}

\date{01.02.2017}

\begin{document}
\begin{frame}
  \titlepage
\end{frame}

% Uncomment these lines for an automatically generated outline.
\begin{frame}{Outline}
  \tableofcontents
\end{frame}

\section{Introduction}
\begin{frame}{The problem}
	\begin{itemize}
		\item What is being said?
		\item What is interesting?
		\item What model architecture?
		\item Which hyper-parameters?
		\item How to ensure generalization?
	\end{itemize}
\end{frame}

\subsection{Listen Attend and Spell}
\begin{frame}{Long Short Term Memory}
	\begin{figure}
		\begin{tikzpicture}
		    \node[anchor=south west,inner sep=0] at (0,0) {\includestandalone[height=6.5 cm]{../tikz/lstm}};
		    %\draw[red,ultra thick,rounded corners] (0.0,0.0) rectangle (5.0,2.5);
		\end{tikzpicture}
		\caption{LSTM cell architecture visualization.}
	\end{figure}
\end{frame}

\begin{frame}{The LAS-Architecture}
	\begin{figure}
		\begin{tikzpicture}
		    \node[anchor=south west,inner sep=0] at (0,0) {\includestandalone[height=6.5 cm]{../tikz/lasArcBottomUp}};
		    %\draw[red,ultra thick,rounded corners] (0.0,0.0) rectangle (5.0,2.5);
		\end{tikzpicture}
		\caption{Listener and speller.}
	\end{figure}
\end{frame}

\begin{frame}{The LAS-Equations}
Attend and spell cell computations:
\begin{align}
	 \mathbf{s}_i &= \text{RNN}(\mathbf{s}_{i-1}, \mathbf{y}_{i-1}, \mathbf{c}_{i-1}), \\
	 \mathbf{c}_i &= \text{AttentionContext}(\mathbf{s}_i,\mathbf{H}), \\
	  P(\mathbf{y}_i|\mathbf{x}, \mathbf{y}_{<i}) &= \text{CharacterDistribution}(\mathbf{s}_i,\textbf{c}_i).
\end{align}

AttentionContext computations:
\begin{align}
	e_{i,u} = \phi(\mathbf{s}_i)^T \psi(\mathbf{h_u}), \\
	\alpha_{i,u} = \frac{ \exp(e_{i,u})}{ \sum\limits_{u} \exp(e_{i,u})}, \\
	\label{eq:alphas}
	\mathbf{c}_i = \sum\limits_{u} \alpha_{i,u} \mathbf{h}_u.
\end{align}
\end{frame}

\section{Methodology}
\begin{frame}{Used methods}
	\begin{itemize}
		\item Object oriented programming, python.
			\begin{itemize}
				\item data encapsulation, partly replaced by \texttt{tf.variable\_scope}("...").
				\item code structure and re-usability.
			\end{itemize}
		\item Shape invariants, tensorflow.
			\begin{itemize}
				\item Programmer must explicitly state changing dimensions in loop.
				\item Makes code easier to read, and prevents bugs.
			\end{itemize}
		\item Code quality, pylint.
			\begin{itemize}
				\item if the python foundation' style guide is being followed.
				\item looks for errors in the code.
			\end{itemize}
		\item Version control, git.
	\end{itemize}
\end{frame}

\section{Implementation}
\begin{frame}{Attend and spell cell layout}
	\begin{figure}
	\includestandalone[height=6.5 cm]{../tikz/asCellType1}
	\caption{Attend and spell cell flow chart.}
	\end{figure}
\end{frame}

\begin{frame}[fragile]
\frametitle{Decoding loop logic}
\begin{block}{body loop logic}
	\begin{semiverbatim}
	while keep_working:
	  not_done_count = reduce_sum( logical_not( d ))
	  done = equal(not_done_count, 0)
	  stop = logical_or(done, greater(time, max_steps))
	  keep_working = logical_not(stop)
	\end{semiverbatim}
\end{block}
\begin{block}{setting the sequence length}
	\begin{semiverbatim}
	  decoded = decode(logits)
	  mask = tf.equal(decoded, <eos>)
	  time_vec = ones(self.batch_size)*(time+1)
	  sequence_lengths = select(d,
	                            logits_sequence_length,
	                            time_vec)
	  d = logical_or(mask, d)
	\end{semiverbatim}
\end{block}
\end{frame}

\begin{frame}{Dropout}
	\begin{figure}
		\includestandalone[height=6.5 cm]{../tikz/las_dropout}
		\caption{Input dropout - light red. Hidden dropout - dark red.}
	\end{figure}
\end{frame}

\section{Results}
\begin{frame}{The Listener with CTC}
	\begin{figure}
		\includestandalone[width=0.49\textwidth]{../tikz/CTC_Listener_plot_e10_loss}
		\includestandalone[width=0.49\textwidth]{../tikz/CTC_Listener_plot_e10_error}
		\caption{Listener with attached CTC.}
	\end{figure}
\end{frame}

\subsection{Custom attention}

\begin{frame}{Greedy Decoding}
	\begin{figure}
	\includestandalone[width=0.49\linewidth]{../tikz/LAS_no_reg_e40_p07_loss}
	\includestandalone[width=0.49\linewidth]{../tikz/LAS_no_reg_e40_p07_error}
	\caption{The training progress shown for the full las architecture with greedy decoding, over 40 epochs, network output reuse probability 0.7 and input noise standard deviation 0.65 .}
	\label{fig:lasGreedy}
	\end{figure}
\end{frame}

\begin{frame}{Alignment plots}
	\begin{figure}
	\centering
	\includestandalone[width=0.49\linewidth]{../tikz/alpha}
	\includestandalone[width=0.42\linewidth]{../tikz/align}
	\caption{Plot of the alignment vectors computed by the network for all 45 labels assigned to timit utterance \texttt{fmld0\_sx295} (left), and alignments assigned by a human listener (right).}
	\label{fig:fullAttention}
	\end{figure}
\end{frame}

\begin{frame}{Alignment plots}
	\begin{block}{Target labels}
		\begin{semiverbatim}
		<sos>  sil  ih  f  sil  k  eh  r  l  sil  k  ah  m  z
		       sil  t  ah  m  aa  r  ah  hh  ae  v  er  r  ey
		       n  jh  f  er  m  iy  dx  iy  ng  ih
		       sil  t  uw  sil
		<eos>
		\end{semiverbatim}
	\end{block}

	\begin{figure}
	\centering
	\includestandalone[height=0.26\linewidth]{../tikz/alphaZoom}
	\includestandalone[height=0.26\linewidth]{../tikz/alphaZoom2}
	\includestandalone[height=0.26\linewidth]{../tikz/alphaZoom3}
	\caption{Attention weights $\alpha$ and network output.}
	\label{fig:attention3}
	\end{figure}
\end{frame}

\begin{frame}
	\begin{figure}
	\centering
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p02_loss}
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p02_error}
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p04_loss}
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p04_error}
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p06_loss}
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p06_error}
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p08_loss}
	\includestandalone[width=0.24\linewidth]{../tikz/LAS_no_reg_e10_p08_error}
	\caption{Repetitions of the same experiment with increasing network output reuse probabilities $0.2, 0.4, 0.6, 0.8$, one experiment per row.}
	\label{fig:lasGreedy2468}
	\end{figure}
\end{frame}

\begin{frame}{Dropout las with beam search}
	\begin{figure}
	\includestandalone[width=0.49\textwidth]{../tikz/LAS_dropout0805_in00_p06_e120_double_loss}
	\includestandalone[width=0.49\textwidth]{../tikz/LAS_dropout0805_in00_p06_e120_double_error}
	\caption{Dropout las results.}
	\end{figure}
\end{frame}

\begin{frame}{Default attention}
TODO
\end{frame}

\begin{frame}{Conclusion}
TODO
\end{frame}

\section{Questions}
\begin{frame}{Questions}
	Thank's for your attention. Questions? \\
	Now, or later \texttt{moritz@wolter.tech}.
\end{frame}

\end{document}