\documentclass[12pt,english]{article}
\usepackage{mathptmx}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{geometry}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\definecolor{byublue}     {RGB}{0.  ,30. ,76. }
\definecolor{darkblue}    {RGB}{0.  ,0.  ,139.}
\definecolor{dukeblue}    {RGB}{0.  ,0.  ,156.}
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\usepackage{amsmath}
\usepackage[authoryear]{natbib}
\usepackage{minted}
\usepackage{mathtools}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\usepackage[backref=page]{hyperref}                                              % Always add hyperref (almost) last
\hypersetup{unicode=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=3,
 breaklinks=true,pdfborder={0 0 0},colorlinks,citecolor=darkblue,filecolor=darkblue,linkcolor=darkblue,urlcolor=darkblue,pagebackref=true}
\usepackage[all]{hypcap}                                            % Links point to top of image, builds on hyperref
\usepackage{breakurl}

\begin{document}

\title{Problem Set 5}
\author{ECON 6343: Econometrics III\\
Prof. Tyler Ransom\\
University of Oklahoma}
\date{Due: September 30, 9:00 AM}

\maketitle
Directions: Answer all questions. Each student must turn in their own copy, but you may work in groups. Clearly label all answers. Show all of your code. Turn in jl-file(s), output files and writeup via GitHub. Your writeup may simply consist of comments in jl-file(s). If applicable, put the names of all group members at the top of your writeup or jl-file.

You may need to install and load the following package:
\begin{itemize}
    \item[~] \texttt{DataFramesMeta}
\end{itemize}

You will also need to load the following previously installed packages:
\begin{itemize}
    \item[~] \texttt{Optim}
    \item[~] \texttt{HTTP}
    \item[~] \texttt{GLM}
    \item[~] \texttt{LinearAlgebra}
    \item[~] \texttt{Random}
    \item[~] \texttt{Statistics}
    \item[~] \texttt{DataFrames}
    \item[~] \texttt{CSV}
\end{itemize}
\pagebreak
In this problem set, we will explore a simplified version of the Rust (1987, \textit{Econometrica}) bus engine replacement model. Let's start by reading in the data.

\begin{minted}[bgcolor=bg]{julia}
using DataFrames
using CSV
using HTTP
url = "https://raw.githubusercontent.com/OU-PhD-Econometrics/fall-2021/
master/ProblemSets/PS5-ddc/busdataBeta0.csv"
df = CSV.read(HTTP.get(url).body, DataFrame)
\end{minted}

\subsubsection*{Static estimation}

\begin{enumerate}
\item Reshape the data into ``long'' panel format, calling your long dataset \texttt{df\_long}. I have included code on how to do this in the \texttt{PS5starter.jl} file that accompanies this problem set.

\item The model we would like to estimate is Harold Zurcher's decision to run buses in his fleet. Zurcher's flow utility of running (i.e. not replacing) a bus is
\begin{align}\label{eq:flowutil}
    u_{1}\left(x_{1t},b\right) &= \theta_0 + \theta_1 x_{1t} + \theta_2 b
\end{align}
where $x_{1t}$ is the mileage on the bus's odometer (in 10,000s of miles) and $b$ is a dummy variable indicating whether the bus is branded (meaning its manufacturer is high-end). The choice set is $\{0,1\}$ where $0$ denotes replacing the engine.

Estimate the $\theta$ parameters assuming Zurcher is completely myopic. This amounts to estimating a simple binary logit model. (\textbf{Note:} you may estimate this any way you wish. I would recommend using the \texttt{GLM} package, but you may also use \texttt{Optim} with your own log likelihood function.)
\end{enumerate}

\subsubsection*{Dynamic estimation}

Now I will walk you through how to estimate the dynamic version of this model using backwards recursion. With discount factor $\beta$, the differenced conditional value function for running the bus (relative to replacing it) is
\begin{align}\label{eq:condv}
    v_{1t}\left(x_{t},b\right)-v_{0t}\left(x_{t},b\right) &= \theta_0 + \theta_1 x_{1t} + \theta_2 b + \beta \int V_{t+1}\left(x_{t+1},b\right) \mathrm{d}F\left(x_{t+1}\vert x_{t}\right)
\end{align}
where $V_{t+1}$ is the value function and the integral is over transitions in the mileage states $x_{t}$.

We will approximate the integral with a summation, which means that we will specify a discrete mass function for $f\left(x_{t+1}\vert x_{t}\right)$. This probability mass function depends on the current odometer reading ($x_{1t}$), whether the engine is newly replaced (i.e. $d_{t-1} = 0$), and on the value of another state variable $x_2$ which measures the usage intensity of the bus's route (i.e. high values of $x_2$ imply a low usage intensity and vice versa).

We discretize the mileage transitions into 1,250-mile bins (i.e. 0.125 units of $x_{1t}$). We specify $x_2$ as a discrete uniform distribution ranging from 0.25 to 1.25 with 0.01 unit increments.

Formally, we are discretely (but not discreetly!) approximating an exponential distribution:
\begin{align}\label{eq:trans}
f_{j}\left(x_{1,t+1}\vert x_{1,t},x_{2}\right) &= \begin{cases}
e^{-x_2(x_{1,t+1}-x_{1t})}-e^{-x_2(x_{1,t+1}+0.125-x_{1t})} & \textrm{  if  } j=1 \textrm{  and  } x_{1,t+1}\geq x_{1,t} \\
e^{-x_2(x_{1,t+1})}-e^{-x_2(x_{1,t+1}+0.125)} & \textrm{  if  } j=0 \textrm{  and  } x_{1,t+1}\geq 0 \\
0 & \textrm{ otherwise} \end{cases}
\end{align}
You will not need to program \eqref{eq:trans}; I will provide code for this part.

Under this formulation, \eqref{eq:condv} can be written as
\begin{align}\label{eq:condv2}
\begin{split}
    v_{1t}\left(x_{t},b\right)-v_{0t}\left(x_{t},b\right) &= \theta_0 + \theta_1 x_{1t} + \theta_2 b + \\
    &\phantom{\text{===}}\beta \sum_{x_{1,t+1}} V_{t+1}\left(x_{t+1},b\right)\left[f_{1}\left(x_{1,t+1}\vert x_{1,t},x_{2}\right) - f_{0}\left(x_{1,t+1}\vert x_{1,t},x_{2}\right)\right]
    \end{split}
\end{align}

Finally, we can simplify \eqref{eq:condv2} since we know that $V_{t+1} = \log\left(\sum_{k} \exp\left(v_{k,t+1}\right)\right)$ when we assume that unobserved utility is drawn from a T1EV distribution (as we do here):

\begin{align}\label{eq:condv3}
\begin{split}
    v_{1t}\left(x_{t},b\right)-v_{0t}\left(x_{t},b\right) &= \theta_0 + \theta_1 x_{1t} + \theta_2 b + \\
    &\phantom{\text{===}}\beta \sum_{x_{1,t+1}} \log\left\{\exp\left(v_{0,t+1}\left(x_{t+1},b\right)\right)+\exp\left(v_{1,t+1}\left(x_{t+1},b\right)\right)\right\}\times\\
    &\phantom{\text{===}}\left[f_{1}\left(x_{1,t+1}\vert x_{1,t},x_{2}\right) - f_{0}\left(x_{1,t+1}\vert x_{1,t},x_{2}\right)\right]
    \end{split}
\end{align}

Estimation of our dynamic model now requires two steps:

\paragraph{Solving the model} First, we need to solve the value functions for a given value of our parameters $\theta$. The way we do this is by backwards recursion. We know that $V_{t+1} = 0$ in our final period (i.e. when $t=T$). Then we work backwards to obtain the future value at every possible state in our model. This will include many states that do not actually show up in our data.

\paragraph{Estimating the model} Second, once we've solved the value functions, we use maximum likelihood to estimate the parameters $\theta$. The log likelihood function in this case is simply
\begin{align}\label{eq:loglike}
    \ell &= \sum_{i=1}^N\sum_{j=0}^1\sum_{t=1}^Td_{ijt}\log P_{ijt}
\end{align}
where
\begin{align}\label{eq:ps}
\begin{split}
    P_{i1t} &= \frac{\exp\left( v_{1t}-v_{0t}\right)}{1+\exp\left(v_{1t}-v_{0t}\right)} \\
    P_{i0t} &= 1-P_{i1t}
\end{split}
\end{align}

\begin{enumerate}
\setcounter{enumi}{2}
\item Now estimate the $\theta$'s assuming that Zurcher discounts the future with discount factor $\beta = 0.9$. I will walk you through specific steps for how to do this:
    \begin{enumerate}
        \item \textbf{Read in the data} for the dynamic model. This can be found at the same URL as listed at the top of p. 2, but remove the \texttt{"Beta0"} from the CSV filename.

        Rather than reshaping the data to ``long'' format as in question 1, we want to keep the data in ``wide'' format. Thus, columns \texttt{:Y1} through \text{:Y20} should be converted to an array labeled \texttt{Y} which has dimension $1000 \times 20$ where $N = 1000$ and $T = 20$. And similarly for columns starting with \texttt{:Odo} and \texttt{:Xst}. Variables \texttt{:Xst*} and \texttt{:Zst} keep track of which discrete bin of the $f_j$'s the given observation falls into.

        \item \textbf{Construct the state transition matrices}, which are the $f_j$'s in \eqref{eq:trans}. To do so, simply run the following code:

        \begin{minted}[bgcolor=bg]{julia}
zval,zbin,xval,xbin,xtran = create_grids()
        \end{minted}

        \texttt{zval} and \texttt{xval} are the grids defined at the bottom of p. 2, which respectively correspond to the route usage and odometer reading. \texttt{zbin} and \texttt{xbin} are the number of bins in \texttt{zval} and \texttt{xval}, respectively. \texttt{xtran} is a (\texttt{zbin*xbin})$\times$\texttt{xbin} Markov transition matrix\footnote{A Markov transition matrix is a matrix where each row sums to 1 and moving from e.g. column 1 to column 4 within a row gives the probability of moving from state 1 to state 4. Check out the Wikipedia page for more information} that gives the probability of falling into each $x_{1,t+1}$ bin given values of $x_{1,t}$ and $x_2$, according to the formula in \eqref{eq:trans}.

        \item \textbf{Compute the future value terms} for all possible states of the model.
        \begin{itemize}
            \item First, initialize the future value array, which should be a 3-dimensional array of zeros. The size of the first dimension should be the total number of grid points (i.e. the number of rows of \texttt{xtran}). The second dimension should be 2, which is the possible outcomes of \texttt{:Branded}. The third dimension should be $T+1$.
            \item Now write four nested \texttt{for} loops over each of the possible states:
            \begin{itemize}
                \item Loop backwards over \texttt{t} from $T+1$ to 1
                \item Loop over the two possible brand states $\{0,1\}$
                \item Loop over the possible permanent route usage states (i.e. from 1 to \texttt{zbin})
                \item Loop over the possible odometer states (i.e. from 1 to \texttt{xbin})
            \end{itemize}
            \item Inside all of the for loops, make the following calculations
            \begin{itemize}
                \item Create an object that marks the row of the transition matrix that we need to be looking at (based on the loop values of the two gridded state variables). This will be \texttt{x + (z-1)*xbin} (where \texttt{x} indexes the mileage bin and \texttt{z} indexes the route usage bin), given how the \texttt{xtran} matrix was constructed in the \texttt{create\_grids()} function.
                \item Create the conditional value function for driving the bus ($v_{1t}$) based on the values of the state variables in the loop (not the values observed in the data). For example, for the mileage ($x_{1t}$), you should plug in \texttt{xval[x]} rather than \texttt{:Odo}.

                \medskip

                The first component of $v_{1t}$ should be the flow utility, which is listed in \eqref{eq:flowutil}.

                \medskip

                The difficult part of $v_{1t}$ is the discrete summation over the state transitions. For this, you need to grab the appropriate row (and all columns) of the \texttt{xtran} matrix, and then take the dot product of that with all possible $x_{1t}$ rows of the FV matrix for the given value of $x_{2}$.

                You should end up with something like
                \begin{minted}[bgcolor=bg]{julia}
xtran[row,:]'*FV[(z-1)*xbin+1:z*xbin,b+1,t+1]
                \end{minted}
                where \texttt{b} indexes the branded dummy and \texttt{t} indexes time periods.\footnote{We need to index by \texttt{b+1} because \texttt{b} takes on values 0 or 1, but in Julia it is illegal to reference the 0th element of an array, so element 1 of the index corresponds to \texttt{b=0} while element 2 of the index corresponds to \texttt{b=1}.}
                \item Now create the conditional value function for replacing the engine ($v_{0t}$). For this, we repeat the same process as with $v_{1t}$ except the $\theta$'s are normalized to be 0. The code for the expected future value is the same as for $v_{1t}$ with the exception that mileage resets to 0 after replacement, so instead of grabbing \texttt{xtran[row,:]} we want \texttt{xtran[1+(z-1)*xbin,:]}.
                \item Finally, update the future value array in period $t$ by storing $\beta\log\left(\exp\left(v_{0t}\right)+\exp\left(v_{1t}\right)\right)$ in the $t$th slice of the 3rd dimension of the array. This will be the new future value term for period $t-1$. Remember to set $\beta=0.9$
            \end{itemize}
        \end{itemize}

        \item \textbf{Construct the log likelihood} using the future value terms from the previous step and only using the observed states in the data. This will entail a \texttt{for} loop over buses and time periods.
        \begin{itemize}
            \item Initialize the log likelihood value to be 0. (We will iteratively add to it as we loop over observations in the data)
            \item Create a variable that indexes the state transition matrix rows for the case where the bus has been replaced. This will be the same \texttt{1+(z-1)*xbin} as in the conditional value function $v_{0t}$ above. However, we need to plug in \texttt{:Zst} from the data rather than a hypothetical value \texttt{z}.
            \item Create a variable that indexes the state transition matrix rows for the case where the bus has not been replaced. This will be the same \texttt{x + (z-1)*xbin} as in $v_{1t}$ above, except we substitute \texttt{:Xst} and \texttt{:Zst} for \texttt{x} and \texttt{z}.
            \item Now create the flow utility component of $v_{1t}-v_{0t}$ using the actual observed data on mileage and branding.
            \item Next, we need to add the appropriate discounted future value to round out our calculation of $v_{1t}-v_{0t}$. Here, we can difference the $f_j$'s as in \eqref{eq:condv3}. You should get something like
                \begin{minted}[bgcolor=bg]{julia}
(xtran[row1,:].-xtran[row0,:])'*FV[row0:row0+xbin-1,B[i]+1,t+1]
                \end{minted}
            \item Finally, create the choice probabilities for choosing each option as written in \eqref{eq:ps} and then create the log likelihood according to the summation in \eqref{eq:loglike}.
        \end{itemize}
    \item Wrap all of the code you wrote in (c) and (d) into a function and set up the function so that it can be passed to \texttt{Optim}. For example, you will need to return the negative of the log likelihood and you will need to have the first argument be the $\theta$ vector that we are trying to estimate
    \item On the same line as the function, prepend the function declaration with the macros so that your code says \texttt{@views @inbounds function myfun()} rather than \texttt{function myfun()}. This will give you more performant code. On my machine, it cut the computation time in half.
    \item Wrap all of your code in an empty function as you've done with other problem sets
    \item Try executing your script to estimate the likelihood function. This took about 4 minutes on my machine when I started from the estimates of the static model in Question 2.
    \item Pat yourself on the back and grab a beverage of your choice, because that was a lot of work!
    \end{enumerate}

\end{enumerate}
\end{document}


\documentclass[a4paper]{article}

\input{temp}

\setcounter{section}{-1}

\begin{document}

\title{Quantum Computation}

\maketitle

\newpage

\tableofcontents

\newpage

\section{Introduction}
asdasd

\newpage

Exercise classes: Sat 3 Nov 11am MR4, Sat 24 Nov 11am MR4, early next term (tba).\\
Thursday 8 November lecture is moved to Saturday 10 November 11am (still MR4).

---Lecture 2---

\section{1}

Recall that we have an oracle $U_f$ for $f:\Z_M \to \Z_\N$ periodic, with period $r$, $A=M/r$. We want to find $r$ in $O(poly(m))$ time where $m=\log M$.

\subsection{The quantum algorithm}
Work on state space $\mathcal{H}_M \otimes \mathcal{N}$ with basis $\{|i\ket |k\ket\}_{i \in \Z_M, k \in \Z_N}$.\\
$\bullet$ Step 1. Make staet $\frac{1}{\sqrt{M}}\sum_{i=0}^{M-1} |i\ket|0\ket$.\\
$\bullet$ Step 2. Apply $U_f$ to get $\frac{1}{\sqrt{M}} \sum_{i=0}^{M-1} |i\ket |f(i)\ket$.\\
$\bullet$ Step 3. Measure the 2nd register to get a result $y$. By Born rule, the first register collapses to all those $i$'s (and only those) with $f(i)$ equal to the seen $y$, i.e. $i=x_0,x_0+r,...,x_0+(A-1)r$, where $0 \leq x_0 < r$ in 1st period has $f(m)=y$.\\
Discard 2nd register to get $|per\ket = \frac{1}{\sqrt{A}} \sum_{j=0}^{A-1}|x_0+jr\ket$.\\
Note: each of the $r$ possible function values $y$ occurs with same probability $1/r$, so $0 \leq x_0 < r$ has been chosen uniformly at random.\\
If we now measure $|per\ket$, we'd get a value $x_0+jr$ for uniformly random $j$, i.e. random element $(x_0^{th})$ of a random period $(j^{th})$, i.e. random element of $\Z_m$, so we could get no information about $r$.\\
$\bullet$ Step 4. Apply quantum Fourier transform mod $M$ (QFT) to  $|per\ket$. Recall the definition of QFT: $QFT: |x\ket \to \sum_{y=0}^{M-1} \omega^{xy} |y\ket$ for all $x \in \Z_M$ where $\omega = e^{2\pi i/M}$ is the $M$th root of unity. The existing result is that QFT mod $M$ can be implemented in $O(M^2)$ time.\\
Then we get
\begin{equation*}
    \begin{aligned}
    QFT |per\ket &= \frac{1}{\sqrt{MA}} \sum_{j=0}^{A-1} \left(\sum_{y=0}^{M-1} \omega^{(x_0+jr)y}|y\ket\right)\\
    &= \frac{1}{\sqrt{MA}} \sum_{y=0}^{M-1} \omega^{x_0y} \left[\sum_{j=0}^{A-1} \omega^{jry}\right] |y\ket \ (*)
    \end{aligned}
\end{equation*}
where we group all the terms with the same $|y\ket$ together. One good thing is that the sum inside the square bracket is a geometric series, with ratio $\alpha = \omega^{ry} = e^{2\pi iry/M} = (e^{2\pi i/A})^y$.\\
Hence term inside bracket $=A$ if $\alpha=1$, i.e. $y=kA = k \frac{M}{r}$, $k=0,1,...,(r-1)$, and equals $0$ otherwise when $\alpha \neq 1$. Now\\
\begin{equation*}
    \begin{aligned}
        QFT |per\ket = \sqrt{\frac{A}{M}} \sum_{k=0}^{r-1} \omega^{x_0 k \frac{M}{r}} |k \frac{M}{r}\ket
    \end{aligned}
\end{equation*}
The random shift $x_0$ now appears only in phase, so measurement probabilities are now independent of $x_0$!

Measuring $QFT |per\ket$ gives a value $c$, where $c = k_0 \frac{M}{r}$ with $0 \leq k_0 \leq r-1$ chosen uniformly at random. Thus $\frac{k_0}{r} = \frac{c}{M}$, note that $c,M$ are known, $r$ is unknown (what we want), and $k_0$ is unknown but uniformly random.

So note that if we are lucky and get a $k_0$ that is coprime to $r$ then we could just simplify $\frac{c}{M}$ to get $r$. Obviously we cannot be always lucky every time, but by theorem in number theory, the number of integers $<r$ coprime to $r$ grows as $O(r/\log\log r)$ for large $r$, so we know probability of $k_0$ coprime to $r$ is $O(\frac{1}{\log\log r})$.

Then by some probability calculation we know that $O(1/p)$ trials are enough to achieve $1-\varepsilon$ probability of success.

So afer Step 4, cancel $c/M$ to the lowest terms $a/b$, giving $r$ as denominator $b$ (if $k_0$ is coprime to $r$). Check $b$ value by computing $f(0)$ and $f(b)$, since $b=r$ iff $f(0) = f(b)$.

Repeating $K=O(\log\log r)$ times gives $r$ with any desired probability.

Further insights into utility of QFT here:\\
Write $R = \{0,r,2r,...,(A-1)r\} \subseteq \Z_M$. $|R\ket = \frac{1}{\sqrt{A}} \sum_{k=0}^{A-1} |kr\ket$, and $|per\ket = |x_0 + R\ket = \frac{1}{\sqrt{A}} \sum_{k=0}^{A-1} |x_0+br \ket$ where $x_0$ is the random shift that caused problem previously.\\
For each $x_0 \in \Z_M$, consider mapping $k \to k+x_0$ (shift by $x_0$) on $\Z_M$, which is a 1-1 invertible map.

So linear map $U(x_0)$ on $\mathcal{H}_M$ defined by $U(x_0): |k\ket \to |k+x_0\ket$ is unitary, and $|x_0+R \ket = U(x_0) |R\ket$.

Since $(\Z_M,+)$ is abelian, $U(x_0)U(x_1) = U(x_0+x_1) =U(x_1)U(x_0)$ i.e. all $U(x_0)$'s commute as operators on $\mathcal{H}_M$.\\
So we have orthonormal basis of common eigenvectors $|\chi_k\ket\}_{k \in \Z_M}$, called \emph{shift invariant states}.

$U(x_0)|\chi_k\ket = \omega(x_0,k)|\chi_k\ket$ for all $x_0,k \in \Z_M$ with $|\omega(x_0,k)| = 1$. Now consider $|R\ket$ written in $|\chi\ket$ basis,\\
$|R\ket = \sum_{k=0}^{M-1} a_k | \chi_k\ket$ where $a_k$'s depending on $r$ (not $x_0$).\\
Then $|per\ket = U(x_0) |R\ket = \sum_{k=0}^{M-1}a_k \omega(x_0,k) |\chi_k\ket$, and measurement in the $\chi$-basis has $prob(k) = |a_k \omega(x_0,k)|^2 = |a_k|^2$ which is independent of $x_0$, i.e. giving information about $r$!

---Lecture 3---

Recall last time we had $\mathcal{H}_M$: shift operations $U(x_0) |y \ket = |y+x_0\ket$ for $x_0,y \in \Z_M$, which all permute, so have a common eigenbasis (shift invariant states) $\{|\chi_k\ket \}_{k \in \Z_M}$, $U(x_0) | x_k\ket = \omega(x_0,k) |\chi_k\ket$.\\
Measurement of $|x_0+R \ket = \frac{1}{\sqrt{A}} \sum_{l=0}^{A-1} |x_0 + l_r \ket = U(x_0) |R\ket$ in $|\chi\ket$ basis has output distribution independent of $x_0$, therefore gives information about $r$.

Introduce QFT as the unitary mapping that rotates $\chi$-basis to standard basis, i.e. define $QFT|\chi_k \ket = |k\ket$. So QFT followed by measurement implements $\chi$-basis measurement.

Explicit form of $|\chi_k\ket$ eigenspaces (!): consider
\begin{equation*}
    \begin{aligned}
        |\chi_k\ket = \frac{1}{\sqrt{M}}\sum_{l=0}^{M-1} e^{-2\pi i kl/M}|l\ket
    \end{aligned}
\end{equation*}
Then
\begin{equation*}
    \begin{aligned}
        U(x_0) |\chi_k \ket &= \frac{1}{\sqrt{M}} \sum_{l=0}^{M-1} e^{-2\pi i kl/M}|l+x_0\ket \\
        &= \frac{1}{\sqrt{M}} \sum_{\tilde{l}=0}^{M-1} e^{-2\pi i k (\tilde{l}-x_0)/M} | \tilde{l} \ket \text { where } \tilde{l} = l+x_0\\
        &= e^{2\pi i k x_0 / M} \cdot |\chi_k\ket
    \end{aligned}
\end{equation*}
i.e. these are the shift invariant staets, eigenvalues $\omega(x_0,k) = e^{2\pi i k x_0/M}$.

Matrix of QFT: So
\begin{equation*}
    \begin{aligned}
        [QFT^{-1}]_{lk} = \frac{1}{\sqrt{M}} e^{-2\pi i lk/M}
    \end{aligned}
\end{equation*}
(componets of $|\chi_k \ket = QFT^{-1} |k\ket$ as $k^{th}$ column). So
\begin{equation*}
    \begin{aligned}
        [QFT]_{kl} = \frac{1}{\sqrt{M}} e^{2\pi i lk / M}
    \end{aligned}
\end{equation*}
as expected.

\newpage

\section{The hidden subgroup problem (HSP)}

Let $G$ be a finite group of size $|G|$. Given (oracle for) function $f:G \to X$ ($X$ is some set), and promise that there is a subgroup $K < G$ such that $f$ is constant on (left) cosets of $K$ in $G$, and $f$ is distinct on distinct cosets.\\
The problem: determine the \emph{hidden subgroup} $K$ (e.g. output a set of generators, or sample uniformly from $K$).\\
We want to solve in time $O(poly(\log |G|))$ (an efficient algorithm) with any constant probability $1-\varepsilon$.

Examples of problems that can be cast(?) as HSPs:\\
(i) periodicity: $f:\Z_M \to X$, periodic with period $r$. Let $G=(\Z_m,+)$, the hidden subgroup is $K=\{0,r,2r,...\} < G$, cosets $x_0+K = \{x_0,x_0+r,x_0+2r,...\}$. The period $r$ is generator of $K$.\\
(ii) discrete logarithm: for prime $p$, $\Z_p^* = \{1,2,...,p-1\}$ with multiplication mod $p$. $g \in \Z_p^*$ is a generator (or primitive root mod $p$). If powers generate all of $\Z_p^*$, $\Z_p^* = \{g^0 = 1,g^1,...,g^{p-2}\}$, then also $g^{p-1} \equiv 1 \pmod p$ (easy number theory).\\
Fact: the generator always exists if $p$ is prime. So any $x \in \Z_p^*$ can be written $x = g^y$ for some $y \in \Z_{p-1}$, write $y=\log_g x$ called the discrete log of $x$ to base $g$.

Discrete log problem: given a generator $g$ and $x \in \Z^*_p$, compute $y=\log_g x$ (classically hard).\\
To express as HSP, consider $f:\Z_{p-1} \times \Z_{p-1} \to \Z_p^*$: $f(a,b) = g^a x^{-b} \ mod \ p = g^{a-yb} \ mod \ p$.\\
Then check: $f(a_1,b_1) = f(a_2,b_2)$ iff $(a_2,b_2) = (a_1,b_1)+\lambda(y,1)$ where $\lambda \in \Z_{p-1}$.

So if $G = \Z_{p-1} \times \Z_{p-1}$, $K=\{\lambda(y,1):\lambda \in \Z_{p-1}\} < G$. Then $f$ is constant and distinct on the cosets of $K$ in $G$, and generator $(y,1)$ gives $y=\log_g x$.\\

(iii) graph problems ($G$ non-abelian now): consider undirected graph $A = \{V,E\}$, $|V| =n$, with at most one edge between any two vertices. Label vertices by $[n] = \{1,2,...,n\}$.\\
Introduce the permutation group $\mathcal{P}_n$ of $[n]$. Define $Aut(A)$ to be the group of automorphisms of $A$, which is a subgroup of $\mathcal{P}_n$, containing exactly the permutations $\pi \in \mathcal{P}_n$ such that for all $i,j \in [n]$, $(i,j) \in E \iff (\pi(i),\pi(j)) \in E$, i.e. the labelled graph $\pi(A)$ obtained by permuting labels of $A$ by $\pi$ is the same \emph{labelled} graph as $A$.

Associated HSP: Take $G = \mathcal{P}_n$. Let $X$ be set of all labelled graphs on $n$ vertices. Given $A$, consider $f_A: \mathcal{P}_n \to X$ by $f_A(\pi) = \pi(A)$, $A$ with labels permuted by $\pi$. The associated hiiden subroup is $Aut(A) = K$.

Application: if we can sample uniformly from this $K$, then we can solve graph isomorphism problem (GI): two labelled graphs $A,B$ are isomorphic if there is 1-1 map $\pi:[n] \to [n]$ such that for all $i,j \in [n]$, $i,j$ is an edge in $A$ iff $\pi(i),\pi(j)$ is an edge in $B$, i.e. $A$ and $B$ are the same graph but just labelled differently.

---Lecture 4---

Let's come back to the graph isomorphism problem.

Problem: given $A,B$, decide if $A \cong B$ or not. This can be expressed as anon-abelian HSP (on example sheet), no known classical polynomial time algorithm. However it is in NP, but it is not believed to be NP-complete.\\
Recent result (2017): a quasi-poly time classical algorithm (L.Babai).

Quantum algorithm for finite \emph{abelian} HSP:\\
Write group $(G,+)$ additively.

Construction of shift invariant states and FT for $G$:\\
Let's introduce some representation theory for abelian group $G$. Consider mapping $\chi:G \to \C^* = (\C \setminus \{0\}, \cdot)$ satisfying $\chi(g_1+g_2) = \chi(g_1)\chi(g_2)$, i.e. $\chi$ is a group homomorphism. Such $\chi$'s are called \emph{irreducible} representations of $G$.\\
We have the following properties (without proof), which we'll call Theorem A later when we refer to it:\\
(i) any value $\chi(g)$ is a $|G|^{th}$ root of unity (so $\chi$: $G \to S^1$ = unit circle in $\C$);\\
(ii) (Schur's lemma, orthogonality): If $\chi_i$ and $\chi_j$ are representations, then $\sum_{g \in G} \chi_i (g) \bar{\chi}_j(g) =\delta_{ij}|G|$;\\
(iii) there are always exactly $|G|$ different representations $\chi$ (well, this is a special case of general representation theory).

By (iii), we can label $\chi$'s as $\chi_g$ for $g \in G$. For example, $\chi(g) = 1$ for all $g \in G$ is always an irreducible representation (the trivial representation), labelled $\chi_0$;\\
Then by orthogonality (ii) for any $\chi \neq \chi_0$ gives $\sum_{g \in G} \chi(g) = 0$.

Shift invariant states: in space $\mathcal{H}_{|G|}$ with basis $\{|g\ket\}_{g \in G}$, introduce \emph{shift operators} $U(k)$ for $k \in G$ defined by $U(k): |g\ket \to |g+k\ket$. Clearly these all commute, so there is simultaneous eigenbasis:\\
For each $\chi_k$, $k\in G$, consider state $|\chi_k \ket = \frac{1}{\sqrt{|G|}} \sum_{g \in G} \bar{\chi}_k (g)|g\ket$. Then theorem A(ii) implies these form orthonormal basis, and $U(g) |\chi_k\ket = \chi_k(g)|\chi_k\ket$.
\begin{proof}
    \begin{equation*}
        \begin{aligned}
            U(g)|\chi_k\ket &= \frac{1}{\sqrt{|G|}} \sum_{h \in G} \bar {\chi_k(h)} |h+g\ket\\
            &\stackrel{h'=h+g}{=} \frac{1}{\sqrt{|G}} \sum_{h' \in G} \bar{\chi_k(h'-g)} |h' \ket
        \end{aligned}
    \end{equation*}
    This implies that
    \begin{equation*}
        \begin{aligned}
            \chi_k*-g) = (\chi_k(g))^{-1} = \bar{\chi_k(g)},\\
            \bar{\chi_k(h'-g)} = \bar{\chi_k(h')}\bar{\chi_k(-g)} = \chi_k(h')\chi_k(g)
        \end{aligned}
    \end{equation*}
    So
    \begin{equation*}
        \begin{aligned}
            U(g) | \chi_k \ket = \frac{1}{\sqrt{|G|}} = \sum_{h' \in G} \chi_k(g) \bar{\chi}_k (h') | h' \ket = \chi_k (g) | \chi_k \ket
        \end{aligned}
    \end{equation*}
\end{proof}

So $|\chi_k \ket$'s are common eigenspaces, called \emph{shift-invariant states}.\\
    Introduce (define) Fourier transform QFT for group $G$ as the unitary that $QFT|\chi_g\ket = |g\ket$ for all $g \in G$.\\
    In $|g\ket-$basis matrices, $k^{th}$ column of $(QFT^{-1}) = $components of $|\chi_k\ket$, i.e. $\frac{1}{\sqrt{|G|}} \bar{\chi_k}(g) = [QFT^{-1}]_{gk}$.\\
    So $[QFT]_{kg}^\dagger = \frac{1}{\sqrt{|G|}}\chi_k(g)$, and so $QFT|g\ket = \frac{1}{\sqrt{|G|}} \sum_{k \in G} \chi_k (g) |k\ket$.

\begin{eg}
    $G=\Z_M$. Check $\chi_a(b) = e^{2\pi iab/M}$, $a,b \in \Z_M$ is a representation.\\
    Similarly, for $G=\Z_{M_1} \times ... \times \Z_{M_r}$, $(a_1,...,a_r) = g_1, (b_1,...,b_r) = g_2$ where $g_1,g_2 \in G$, $$\chi_{g_1}(g_2) \stackrel{def}{=} e^{2\pi i \left(\frac{a_1b_1}{M_1} + ... + \frac{a_rb_r}{M_r}\right)}$$ is a representation of $G$. And we get $$QFT_G =QFT_{M_1} \otimes ... \otimes QFT_{M_r}$$ on $\mathcal{H}_{|G|} = \mathcal{H}_{M_1} \otimes ... \otimes \mathcal{H}_{M_r}$.\\
    This is exhaustive, since by classification theorem, every finite abelian group $G$ is isomorphic to a direct product of the form $G \cong \Z_{M_1} \times ... \times \Z_{M_r}$. Furthermore, we can insist that $M_i$ are prime powers $p_i^{s_i}$, where $p_i$ are not necessarily distinct.
\end{eg}

Quantum algorithm for finite abelian HSP:\\
Let $f:G \to X$, hidden subgroup $K<G$. We have cosets $K=0+K,g_2+K,...,g_m+K$, where $m=|G|/|K|$. State space as usual, with basis $\{|g\ket, |x\ket \}_{g \in G, x \in X}$.\\
$\bullet$ make the state $\frac{1}{\sqrt{|G|}} \sum_{g \in G} |g\ket |0\ket$;\\
$\bullet$ Apply oracle $U_f$, get $\frac{1}{\sqrt{|G|}} \sum_{g \in G} |g\ket |f(g)\ket$;\\
measure second register to see a value $f(g_0)$.\\
Then first register gives coset state (remember the function is constant on each coset). $|g_0+K \ket = \frac{1}{\sqrt{|K|}} \sum_{k \in K} |g_0 + K \ket = U(g_0) |K\ket$.\\
Apply QFT and measure to obtain result $g \in G$.

---Lecture 5---

Last time we disccused how to solve the abelian HSP problem. Now how does the output $g$ related to $K$?\\
$\bullet$ the output distribution of $g$ is independent of $g_0$, so same as that obtained from $QFT|K \ket$ (i.e. $g_0 = 0$) since:\\
write $|K \ket $ in shift invariant basis $|\chi_g \ket$'s, $|K\ket = \sum_{g} a_g | \chi_g \ket$, then $|g_0+K \ket = U(g_0) |K\ket = \sum a_g \underbrace{\chi_g(g_0) | \chi_g \ket}_{ = U(g_0) | \chi_g \ket}$; but $QFT|\chi_g \ket = |g\ket$, so $Prob(g) = |a_g \chi_g (g_0)|^2 = |a_g|^2$ as $\chi_g(g_0)| = 1$.

Thus look at $QFT|K \ket$. Recall $QFT|k \ket = \frac{1}{\sqrt{|G|}} \sum_{l \in G} \chi_l(k) |l\ket$, so $QFT |K\ket = \frac{1}{\sqrt{|G|}} \frac{1}{\sqrt{|K|}} \sum_{l \in G} \left[\sum_{k \in K} \chi_l(k) \right] |l\ket$.\\
The terms in $[...]$ involves irreducible representation $\chi_l$ of $G$ restricted to subgroup $K < G$, which is an irreducible representation of $K$. Hence
\begin{equation*}
    \begin{aligned}
        \sum_{k \in K} \chi_l(k) = \left\{
            \begin{array}{ll}
                |K| & \chi_l \text{ restricts to trivial irreducible representation on } K\\
                0 & \text{ otherwise}
            \end{array}
        \right.
    \end{aligned}
\end{equation*}
and
$$QFT|K\ket = \sqrt{\frac{|K|}{|G|}} \sum_{l \in G \text{ with }\chi_l\text{ reducing to trivial irreducible representation of }K} |l\ket$$
So measurement gives a uniformly random choice of $l$ such that $\chi_l(k) = 1$  for all $k \in K$.\\
e.g. If $K$ has generators $k_1,k_2,...,k_M$, $M=O(\log |K|) = O(\log |G|)$, then output has $\chi_l(k_i) = 1$ for all $i$.

It can be shown that if $O(\log|G|)$ such $l$'s are chosen uniformly at random, then with probability $>2/3$ they suffice to determine a generating set for $K$ via equations $\chi_l(k) = 1$.\\
(see example sheet 1 for particular examples).

\begin{eg}
    If $G = \Z_{M_1} \times ... \times \Z_{M_q}$.\\
    We had for $l=(l_1,...,l_q)$, $g \in (b_1,...,b_q) \in G$,
    $$\chi_l(g) = e^{2\pi i (\frac{l_1k_1}{M_1} + ... + \frac{l_qb_q}{M_q})}$$
    So for $k=(k_1,...,k_q)$, $\chi_l(k) = 1$ becomes
    $$\frac{l_1k_1}{M_1} + ... + \frac{l_qk_q}{M_q} \equiv 0 \pmod 1$$
    (i.e. is an integer), a homogeneous linear equation on $K$, and $O(\log |K|)$ is independent such that equations determine $K$ as null space.
\end{eg}

Some remarks on HSP for non-abelian groups $G$ (write multiplicatively):\\
As before, can easily generate coset states
$$ |g_0 K \ket = \frac{1}{\sqrt{|K|}} \sum_{k \in K} |g_0 K\ket$$
where $g_0$'s are randomly chosen. But problems arise with QFT construction, because now there's no basis of shift-invariant states exists! (this is since $U(g_0)$'s don't commute anymore, so no common full eigenbasis).

Construction of non-abelian Fourier Transform (some more representation theory):\\
$\bullet$ $d$-dimensional representation of $G$ is a group homomorphism $\chi: G \to U(d)$ where $U(d)$ is the space of $d \times d$ unitary matrices acting on $\C^d$, by $\chi(g_1 g_2) \chi(g_1)\chi(g_2)$. (see part II representation theory for the general form)\\
$\bullet$ $\chi$ is irreducible representation if no subspace of $\C^d$ is left invariant under $\chi(g)$ for all $g \in G$ (i.e. cannot simultaneously block diagonalise all $\chi(g)$'s by a basis change).\\
$\bullet$ a complete set of irreducible representation: set $\chi_1,...,\chi_m$ such that any irreducible representation is unitarily equivalent to one of them (equivalence $\chi \to \chi' = V\chi V^T$).

\begin{thm} (non-abelian version of theorem A -- properties of representations)\\
    If $d_1,...,d_m$ are dimensions of a complete set of irreducible representations $\chi_1,...,\chi_m$, then:\\
    (i) $d_1^2+...+d_m^2 = |G|$;\\
    (ii) Write $\chi_i(g)_{jk}$ for the $(j,k)^{th}$ entry of matrix $\chi_i(g)$, where $j,k = 1,...,d_i$. Then (Schur orthogonality):
    $$\sum_g \chi_i(g)_{jk} \bar{\chi}_{i'}(g)_{j'k'} = |G| \delta_{ii'} \delta{jj'}\delta{kk'}$$
    Hence states
    $$|\chi_{i,jk} \ket \equiv \frac{1}{\sqrt{|G|}} \sum_{g \in G} \bar{\chi}_i(g)_{jk} |g\ket$$
    is an orthonomal basis.
\end{thm}

$\bullet$ QFT on $G$ \emph{defined} to be the unitary that rotates $\{|\chi_{ijk}\ket\}$ basis into standard basis $\{|g\ket\}$. However, $|\chi_{ijk}\ket$ are \emph{not} shift invariant for all $U(g_0)$'s, and consequently measurement of coset state $|g_0 K\ket$ in $|\chi \ket$-basis gives an output distribution \emph{not} independent of $g_0$.

However, \emph{partial} shift invariance survives: Consider the incomplete measurement $M_{rep}$ on $|g_0 K\ket$ that distinguishes only the irreducible representations (i.e. $i$ values) and not all $(i,j,k)$'s.\\
i.e. with measurement outcome $i$ associated to $d_i^2$-dimensional orthogonal subspaces spanned by $\{|\chi_{(i),jk}\ket\}_{j,k = 1,...,d_i}$.\\
Then $\chi_i(g_1,g_2) = \chi_i(g_1) \chi_i(g_2)$ implies output distribution of $i$ values is independent of $g_0$, giving direct, albeit imcomplete, information about $K$.\\
E.g. conjugate subgroups $K$ and $ = g_0 K g_0^{-1}$ for some $g_0 \in G$ give \emph{same} output distribution.

---Lecture 6---\\
Non-abelian HSP/FT remarks:\\
For efficient HSP algorithm, we also need QFT to be efficiently implementable, i.e. $poly(\log|G|)$-time.\\
This is true for any abelian $G$ and some non-ablien $G$'s (such as $\mathcal{P}_n$), but even in latter case there's no known efficient HSP algorithm.

Some known result:\\
for normal subgroups, i.e. $gK= Kg$ for all $g \in G$:
\begin{thm} (Hallgrer, Russell, Tashma, SIAM J.Comp 32 p916-934 (2003))\\
    Suppose $G$ has efficient QFT. Then if hidden subgroup $K$ is normal, then there is an efficient HSP quantum algorithm.\\
    (Construct coset state $|g_0 K\ket$, perform $M_{rep}$ on it.)\\
    Repeat $O(\log|G|)$ times. Then $K$ normal implies outputs suffice to determine $K$.
\end{thm}

\begin{thm} (Ettinger, Hoyer, Knill)\\
    For general non-abelian HSP, $M=O(poly(\log|G|))$ random coset states $|g_1K\ket$,...,$g_M K\ket$ suffice to determine $K$ from $M$ coset states, but it's not efficient.\\
    See example sheet for a proof -- construct a measurement procedure on $|g_1K\ket \otimes ... \otimes g_MK\ket$ to determine $K$, but it takes exponential time in $\log|G|$.
\end{thm}

The phase estimation algorithm:\\
$\bullet$ a unifying principle for quantum algorithms, uses $QFT_{2^n}$ again.\\
$\bullet$ many applications, e.g. an alternative efficient factoring algorithm (A.Kitaev).

Given unitary operator $\mathcal{U}$ and eigenstate $|v_\phi \ket \cdot \mathcal{U} | v_{\phi}\ket = e^{2\pi i\phi} | v_\phi\ket$, we want to estimate phase $\phi$, where $0 \leq \phi < 1$ (to some precision, say to $n$ binary digits).

We'll need \emph{controlled-$U^k$} for integers $k$, writte $C-U^k$, which satisfies $c-U^k |0\ket |\xi\ket = |0\ket |\xi\ket$, $C-U^k |1\ket |\xi\ket = |1\ket U^k|\xi\ket$, where $|\xi\ket$ in general has dimension $d$.\\
Note $U^k |v_\phi\ket = e^{2\pi ik\phi} | v_\phi\ket$, $C-(U^k) = (C-U)^k$.

\begin{rem}
    Given $U$ as a formula or (arant?) description, we can readily implement $C-U$, e.g. just control each gate of $U$'s circuit.\\
    However, if $U$ is given as a \emph{black box}, we need further info:\\
    $\bullet$ it suffices to have an eigenstate $|\alpha\ket$ with known eigenvalue $U|\alpha\ket = e^{i\alpha} |\alpha\ket$:\\
    We can consider

    \includegraphics[scale=0.5]{image/QC_01.png}
    Where we get $CU|a\ket |\xi\ket$ at the first two row and the third row $|\alpha\ket$ is always unchanged.\\
    To see how it works, just check circuit action. (...)
\end{rem}

We'll actually want \emph{generalised controlled-$U$} with $|x\ket |\xi\ket \to |x\ket U^x |\xi\ket$, where $|x\ket$ has $n$ qubits, i.e. $x \in \Z_{2^n}$.\\
We can make this thing from $C-(U^k)$ as follows:

\includegraphics[scale=0.5]{image/QC_02.png}

We get $|x\ket U^x |\xi\ket$, where $x=x_{n-1}...x_1x_0$ binary, $U^x = U^{2^{x_{n-1}}} ... U^{2^{x_1}}U^{2^{x_0}}$.\\
Note: if input $|\xi\ket = |v_\phi\ket$, then get $e^{2\pi i \phi x}|v_\phi\ket$.

Now suppose over all $x=0,1,...,2^{n-1}$ and use $|\xi\ket = |v_\phi\ket$,

\includegraphics[scale=0.5]{image/QC_03.png}

Where the output is $\frac{1}{\sqrt{2^n}} \sum_x e^{2\pi i\phi x} |x\ket$, we call this state $|A\ket$.

Finally apply $QFT_{2^n}^{-1}$ to $|A\ket$ and measure to see $y_0,...,y_{n-1}$ on lines $0,1,...,n-1$. Then output $0.y_0...y_{n-1} = \frac{y_0}{2}+...+\frac{y_{n-1}}{2^{n-1}}$, as the estimate of $\phi$.\\
That's the phase estimation algorithm (for given $U$ and $V_\phi\ket$).

Suppose $\phi$ actually had only $n$ binary digits, i.e. $\phi$ exactly equals $0.z_0z_1...z_{n-1}$ for some $z_k=0,1$ for all $k$.

Then $\phi = \frac{z_0...z_{n-1}}{2^n} = \frac{z}{2^n}$ where $z$ is $n$-bit integer in $\Z_{2^n}$, and
$$ |A\ket = \frac{1}{\sqrt{2^n}} \sum_x e^{2\pi ixz/2^n} |x\ket$$
\emph{is} $QFT_{2^n}$ of $|z\ket$.\\
So $QFT^{-1} |A\ket = |z\ket$ and get $\phi$ exactly, with certainty.\\
In this case the algorithm up to (not including) final measurements is a unitary operation, mapping $|0\ket ... |0\ket |v_\phi \ket \to |z_0 \ket ... |z_{n-1} \ket |v_\phi\ket$.

---Lecture 7---
Phase Estimation (continued):\\

$U$ is a $d \times d$ unitary operation/matrix with eigenstate $U|v_\phi \ket = 2^{2\pi i\phi} | v_\phi \ket$, and we want to estimate $\phi$.\\
$U$ as a quantum physical operation is equivalent to $\tilde{U} = e^{i \alpha} U$ for any $\alpha$ and $\tilde{U}$ has $\phi \to \phi+\alpha/2\pi$.\\
So if $U$ given as quantum physical operation alone, we cannot determine $\phi$.\\
But controlled versions different: $C-U$ and $C-\tilde{U}$ are different as physical operations (set $\{e^{i\alpha} C-U\}_\alpha \neq \{e^{i\alpha} C-\tilde{U}\}_\alpha$), and $C-U/\tilde{U}$ \emph{does} fix $\phi$ associatied to choice of phase $\alpha$.\\
So quantum phase estimation algorithm use $C-U$ ($C-U^{2^k}$) physical operations (not just $U$'s).

We had $\underbrace{|0\ket...|0\ket}_{n} |v_\phi\ket \stackrel[C-U's]{\text{unitary}}{\rightarrow} |A\ket = \frac{1}{\sqrt{2^n}} \sum_{x=0}^{2^n-1} e^{2\pi i\phi x} |x\ket$ ($n$ qubits).\\
Apply $QFT^{-1}$ we get $QFT^{-1}|A\ket$, measure to see $y_0,...,y_{n-1}$; output $\phi = \frac{(y_0y_1...y_{n-1})}{2^n}$, $0 \leq y < 2^{n-1}$, where the numerator is a $n$-bit integer.\\
If $\phi = \frac{z}{2^n}$ for integer $0 \leq z < 2^n$, i.e. $\phi$ has exactly $n$ binary digits, then $|A\ket = QFT|z\ket$, so we get $z$ with certainty in the measurement.

Now suppose $\phi$ has \emph{more} than $n$ bits, say $\phi = 0.z_0z_1z_2...z_{n-1} | z_nz_{n+1}...$. Then we have:

\begin{thm} (PE)
    If measurement in above algorithm give $y_0,...y_{n-1}$ (so output is $\theta = 0.y_0...y_{n-1}$), then\\
    (a) $\P(\theta$ is closet $n$ binary digit approximate to $\phi) \geq {4}{\pi^2}$;\\
    (b) $\P(|\theta-\phi| \geq \varepsilon)$ is at most $P(\frac{1}{2^n \varepsilon})$ (we'll show it's at most $\frac{1}{2^{n+1} \varepsilon}$).

    \begin{rem}
        In (a), we have probability $\frac{4}{\pi^2}$ that all $n$ lines of $n$-line QPE process are \emph{good}.\\
        But, if we want $\phi$ accurate to $m$ bits with probability $1-\eta$, then we use theorem (PE) (b) with $\varepsilon = 1/2^m$. Then we'll use $n>m$ lines with
        $$\frac{1}{2^{n+1}} \varepsilon = \eta, \varepsilon = \frac{1}{2^m}$$
        i.e. $n=m+\log(1/\eta)+1$. In words, number of lines needed is only number of bits wanted with good probability $1-\eta$ plus a modest polynomial increase for exponetial reduction in $\eta$.
    \end{rem}

    \begin{proof}
        We have
        \begin{equation*}
            \begin{aligned}
                QFT^{-1}|x\ket = \frac{1}{\sqrt{2^n}} \sum_{y=0}^{2^n-1} e^{-2\pi i yx / 2^n} |y\ket
            \end{aligned}
        \end{equation*}
        So
        \begin{equation*}
            \begin{aligned}
                QFT^{-1} |A\ket =\frac{1}{2^n} \sum_y \left[\sum_x e^{2\pi i(\phi - y/2^n)x}\right] |y\ket
            \end{aligned}
        \end{equation*}
        So for measurement,
        \begin{equation*}
            \begin{aligned}
                \P(\text{see } n- \text{ bit integer } y=y_0y_1...y_{n-1}) = \frac{1}{2^{2n}} \left|\sum_{x=0}^{2^n-1} e^{2\pi i \underbrace{(\phi-\frac{y}{2^n})}_{:=\delta(y)} x}\right|^2
            \end{aligned}
        \end{equation*}
        Note that this is a geometric series $e^{2\pi i \delta(y)}$, so
        \begin{equation*}
            \begin{aligned}
                \P(\text{see } y) = \frac{1}{2^{2n}} \left|\frac{1-e^{2^n 2\pi i \delta(y)}}{1-e^{2\pi i\delta(y)}} \right|^2
            \end{aligned}
        \end{equation*}
        Let's call this equation (P) (maybe for \emph{phase}).\\
        We want to bound/estimate this expression.\\
        For (a): Let $y=a=a_0a_1...a_{n-1}$ give \emph{closest} $n$-bit approximation to $\phi$, i.e. $|\phi-\frac{a}{2^n}| \leq \frac{1}{2^{n+1}}$, i.e. $\delta(a) \leq \frac{1}{2^{n+1}}$.\\
        Now we bounds:\\
        (i) $|1-e^{i\alpha}| = |2\sin \frac{\alpha}{2}| \geq \frac{2}{\pi} |\alpha|$ if $|\alpha| < \pi$;\\
        (ii) $|1 - e^{2\pi i \beta}| \leq 2\pi \beta$.

        In equation (P), use (i) with $\alpha =2^n \cdot 2\pi \delta(a) \leq 2^n 2\pi \frac{1}{2^{n+1}} \leq \pi$ to lower bound top line, and (ii) with $\beta = \delta(a)$ to upper bound bottom line, get
        \begin{equation*}
            \begin{aligned}
                \P(\text{see } a) \geq \frac{1}{2^{2n}} \left(\frac{2^{n+1}\delta(a)}{2\pi \delta(a)}\right)^2 = \frac{4}{\pi^2}
            \end{aligned}
        \end{equation*}
        For (b), we want to upper bound equaiton (P): for top line, $|1-e^{i\alpha}| \leq 2$ for any $\alpha$; for bottom, use (i) get $|1-e^{2\pi i\delta(y)} | \geq 4\delta(y)$. So
        \begin{equation*}
            \begin{aligned}
                \P(y) \leq \frac{1}{2^{2n}} \left(\frac{2}{4\delta(y)}\right)^2 = \frac{1}{2^{2n+2}} \delta(y)^2
            \end{aligned}
        \end{equation*}
        Now sum this for all $|\delta(y)| > \varepsilon$, $\delta(y)$ values spaced by $1/2^n$'s. Let $\delta_+$ be first $\delta(y)$ (jumps?) with $\delta(y) \geq \varepsilon$, $\delta_-$ be that with $\delta(y) \leq -\varepsilon$. So $|\delta_+|,|\delta_-| \geq \varepsilon$.\\
        Then if $|\delta(y)| \geq \varepsilon$, we have $\delta(y) = \delta_+ + \frac{k}{2^n}$, $k=0,1,...$, or $=\delta_- - \frac{k}{2^n}$, $k=0,1,...$.\\
        So $|\delta(y)| \geq \varepsilon + \frac{k}{2^n}$ with $k=0,1,2,...$ in each case.\\
        So
        \begin{equation*}
            \begin{aligned}
                \P(|\delta(y)| > \varepsilon) &\leq 2 \sum_{k=0}^\infty \frac{1}{2^{2n+2}} \frac{1}{(\varepsilon+\frac{k}{2^n})^2}\\
                &\leq \frac{1}{2} \int_0^\infty \frac{1}{(2^n \varepsilon+k)^2} dk\\
                &= \int_{2^n \varepsilon}^\infty \frac{dk}{k^2}\\
                &= \frac{1}{2^{n+1} \varepsilon}
            \end{aligned}
        \end{equation*}
    \end{proof}
\end{thm}

Further remarks on QPE algorithm:\\
(1) If $C-U^{2^k}$ is implemented as $(C-U)^{2^k}$, the QPE algorithm needs exponential time in $n$ as we have $1+2+...+2^{n-1} = 2^n-1$ $(C-U)$ gates.\\
However, for some special $U$'s, $C-U^{2^k}$ can be implemented in $poly(k)$ time, so we get a poly time QPE algorithm.\\
It can be used to provide alternative facoring (order finding) algorithm (due to A. Kitaev) using PE.

---Lecture 8---

First exercise class: Saturday 3 Nov 11am MR4.

(2) If instead of $|v_\phi\ket$, use general input state $|\xi\ket$:
$$|\xi \ket = \sum_j c_j | v_{\phi_j}\ket$$
$$U|v_{\phi_j} \ket = e^{2\pi i \phi_j} | v_{\phi_j} \ket$$
Then we get in QPE (before final measurement) a unitary process $U_{PE}$ with (lecturer had \emph{that}) effect
$$|0...0\ket | \xi\ket \xrightarrow{U_{PE}} \sum_j c_j | \phi_j\ket |v_{\phi_j}\ket$$
and final measurement will give a choice of $\phi_j$'s (or approximation) chosen with probabilities $|c_j|^2$.

\begin{eg}
    Implement $QFT_{\mathcal{Q}}$ for $\mathcal{Q}$ not a power of $2$, with a quantum curcuit of $1-$ and $2-$ qubit gates of circuit size $O(poly(\log \mathcal{Q}))$ (Kitaev's method).
\end{eg}

\begin{rem}
    For $\mathcal{Q}=2^m$, we have explicit known circuit of $O(m^2)$. $H$ and $C$-phase gate to implement $QFT_{2^m}$ exactly (cf part II QIC Notes).
\end{rem}

For $QFT_{\mathcal{Q}}$: Introduce
$$|\eta_a \ket = QFT_{\mathcal{Q}} |a\ket = \frac{1}{\sqrt{\mathcal{Q}}} \sum_{b=0}^{\mathcal{Q}-1} \omega^{ab} |b\ket, a \in \Z_{\mathcal{Q}}, \omega = e^{2\pi i/\mathcal{Q}}$$
It suffices to make circuit hat does $|a \ket \to |\eta_a\ket$ (*).\\
Let $2^{m-1} < \mathcal{Q} < 2^m$, and set $M=2^m$, view $\mathcal{H}_{\mathcal{Q}}$ as subspace of $m$ qubits (spanned by $|a\ket: 0 \leq a < \mathcal{Q}-1 < 2^m$).\\
To achieve (*), consider instead on $\mathcal{H}_{\mathcal{Q}} \otimes \mathcal{H}_{\mathcal{Q}}$
$$|a\ket |0\ket \xrightarrow{(1)} |a\ket |\eta_a\ket \xrightarrow{(2)} |0\ket |\eta_a\ket$$
(1): get $\eta_a\ket$ from $|a\ket$ while \emph{remembering} $|a \ket$;\\
(2): \emph{erase/forget} $|a\ket$.\\
For (1), first do $|0\ket \to |\xi\ket = \frac{1}{\sqrt{\mathcal{Q}}} \sum_{b=0}^{\mathcal{Q}-1} |b\ket$ as follows:\\
on $m$ qubits $\mathcal{H}^{\otimes m}$ gives $\frac{1}{\sqrt{M}} \sum_{x=0}^{2^m-1} |x\ket$. Then consider the step function $f(x) = 0$ if $x<\mathcal{Q}$ and 1 if $x \geq \mathcal{Q}$. It's classically efficiently computable, so can efficiently implement $U_f$ on $(m+1)$ qubits.\\
So applying $U_\rho$ to $(H^{\otimes m} |0\ket) |0\ket$ and measure output $(m+1^{st})$ qubit to get $|\xi\ket$ on first $n$ qubits if measurement result is 0.\\
Note that $prob(0) > 1/2$ as $\mathcal{Q} > 2^{m-1} = 2^m / 2$, so we can use multiple trials to give $|\xi\ket$.\\
We can do offline: failures/re-tries do not affect state to which we want to apply $QFT_{\mathcal{Q}}$. So now we have $|\tilde{\xi} = |a\ket \left(\frac{1}{\sqrt{\mathcal{Q}}} \sum_{b=0}^{\mathcal{Q}-1} |b\ket\right)$.\\
Next consider $V |a\ket |b\ket = \omega^{ab} |a\ket |b\ket$.\\
Then $V|\tilde{\xi} \ket = |a\ket |\eta_a\ket$ as we want for (1).

To implement $V$, consider
$$U:|b\ket \to \omega^b |b\ket$$
If $|b\ket$ in $m$ qubits given by $|b_{m-1} \ket ... |b_0\ket$, i.e. $b = b_{m-1} ... b_0$ in binary, then $\omega^b = \omega^{b_{m-1} 2^{m-1}}...\omega^{b_02^0}$. So $U$ is product of $1$-qubit phase gates
$$P(\omega^{2^{m-1}}) \otimes ... \otimes \P(\omega^{2^0})$$
where $P(\xi) = Diag(1,\xi)$, $|\xi|=1$ is a phase gate.

Similarly, for $C-U^{2^k}$ (starting with $U \to U^{2^k}$ i.e. $\omega^b \to \omega^{2^kb}$), and $V$ = \emph{generalised $C-U$}:
$$|a\ket |b\ket \xrightarrow{V} |a\ket U^a |b\ket$$
which is constructed as before, from $C-U^{2^k}$'s.\\
So now we have $|a\ket |0\ket \xrightarrow{(1)} |a\ket |\eta_a\ket$.

For (2), i.e. $|a\ket |\eta_a\ket \xrightarrow{(2)} |0\ket |\eta_a\ket$, \emph{if} we had $U$ with eigenstates $|\eta_a\ket$, eigenvalues $\omega^a = e^{2\pi i a/\mathcal{Q}}$, then $U_{PE}$ would give
$$|0\ket |\eta_a\ket \xrightarrow{U_{PE}} |a\ket |\eta_a\ket$$
(we are a bit loose on how information is presented -- writing eigenvalue output as $a$, and note we are assuming that PE works exactly)\\
Hence $U^{-1}_{PE}$ (\emph{inverse gates taken in reverse order}) would give desired (2)!

Consider $U:|x\ket \to |x-1\ mod\ \mathcal{Q}\ket$, and check that $U |\eta_a\ket = \omega^a |\eta_a\ket$ as wanted.\\
Now note $x \to x-k\ mod\ \mathcal{Q}$ for $k \in \Z_{\mathcal{Q}}$ is classically computable in $poly(\log\mathcal{Q})$-time, thus we also have $U^k: |x\ket \to |x-k\ mod\ \mathcal{Q}\ket$, and PE algotirhm with $m=O(\log(Q))$ lines.\\
Then implementing (1) then (2) gives $poly(\log\mathcal{Q})$ sized circuit for $QFT_{\mathcal{Q}}$.

But PE is not exact. However, using more qubit lines ($O(\log 1/\varepsilon)$ lines), we can achieve (by theorem PE(b))
$$|0\ket |\eta_a \ket \xrightarrow{U_{PE}} (\sqrt{1-\varepsilon} |a\ket + \sqrt{\varepsilon} |a^\perp \ket) |\eta_a\ket$$
(where $a^\perp$ is a state orthogonal to $|a\ket$) for any (small) deserved $\varepsilon$. Then
$$||\ |a\ket - \sqrt{1-\varepsilon} |a\ket + \sqrt{\varepsilon} | a^\perp \ket\ || = O(\sqrt{\varepsilon})$$
So
$$||\ U_{PE}^{-1} |a\ket |\eta_a\ket - |0\ket |\eta_a\ket\ || = O(\sqrt{\varepsilon})$$
(as unitaries preserve lengths). So we can approximate $QFT_{\mathcal{Q}}$ to any desired precision (omit details).

\newpage

\section{Amplitude Amplification}
Note that this is a very good name -- a fifth order literation (both starting with \emph{Ampli}).\\
Apothesis of technique in Grover's algorithm.

Some background:\\
We'll make much use of \emph{reflection operators}.

---Lecture 9---\\
A reminder that we don't have lecture next thursday.

Reflection operators:\\
$\bullet$ State $|\alpha\ket$ in $\mathcal{H}_d$ $\to$ 1-dimensional subspace $L_\alpha$ and $(d-1)$-dimensional orthogonal complement $L_\alpha^\perp$
$$I_{|\alpha\ket} \stackrel{def}{=} I-2|\alpha \times \alpha|$$
has $I_{|\alpha\ket} |\alpha\ket = -|\alpha\ket$, $I_{|\alpha\ket} |\beta\ket = |\beta\ket$ for any $|\beta \ket \perp |\alpha\ket$.\\
So $I_{|\alpha\ket}$ is reflection in $(d-1)$-dimensional subspace $L_{\alpha}^\perp$.

Note that for any unitary $U$, $U I_{|\alpha\ket} U^\dagger = I_{U|\alpha\ket}$, since $U|\alpha \times \alpha|Y^\dagger = |\xi \times \xi|$ ofr $\xi = U|\alpha\ket$ (basically a change of basis).

$\bullet$ Take $k$-dimensional subspace $A \subseteq \mathcal{H}_d$, and any orthonormal basis $|a_1\ket,...,|a_k\ket$. Then $P_A = \sum_{i=1}^k |a_i \times a_i|$ is projection operator into $A$.\\
Define $I_A = I-2P_A$. Then we have $I_A |\xi\ket = |\xi\ket$ if $|\xi\ket \in A^\perp$, and $I_A|\xi\ket = -|\xi\ket$ if $|\xi\ket \in A$.\\
So $I_A$ is reflection in $(d-k)$ dimensional mirror $A^\perp$.

Recap of Grover's algorithm (part II notes page 68-73):\\
$\bullet$ search for unique \emph{good} item in unstructured database of $N=2^n$ items formalised as: (write $B_n$ to be the set of all $n$-bit strings, $N=2^n$):
Given oracle for $f:B_n \to B$, promised that there is unique $x_0 \in B_n$ with $f(x_0) = 1$, and we wish to find $x_0$.\\
This is closely related to class NP and Boolean satisfiability problem (see part II notes p 67-68).\\
Using one query to $(n+1)$-qubit $\mathcal{U}_f$, we can implement reflection operator $I_{|x_0\ket} : |x\ket \to |x\ket$ if $x \neq x_0$, and to $-|x\ket$ if $x=x_0$.\\
(viz. apply $\mathcal{U}_f$ to $|x\ket (\frac{|0\ket - |1\ket}{\sqrt{2}})$ and discard the last qubit.)\\
Then consider \emph{Grover iteration operator} on $n$ qubits:\\
$$Q \stackrel{def}{=} -H_n I_{|0...0\ket} H_n I_{|x_0\ket} = -I_{|\psi_0\ket} I_{|x_0\ket}$$
here $H_n = H\otimes H \otimes ... \otimes H = H_n^{\dagger}$, and $|\psi_0\ket = H^n |0...0\ket = \frac{1}{\sqrt{2^n}} \sum_{x \in B_n} |x\ket$.\\
So one application of $Q$ uses 1 query to $\mathcal{U}_f$.

\begin{thm} (Grover, 1996)\\
    In 2-dimensional span of $|\psi_0\ket$ and (unknown) $|x_0\ket$, the action of $Q$ is rotation by angle $2\alpha$ where $\sin\alpha = \frac{1}{\sqrt{N}}$.\\
\end{thm}

Hence (Grover's algorithm) to find $x_0$ given $U_f$:\\
1. Make $|\psi_0\ket$;\\
2. Apply $Q$ $m$ times where $m = \frac{\arccos(\frac{1}{\sqrt{N}})}{2\arctan(frac{1}{\sqrt{N}})}$ to rotate $|\psi_0\ket$ very close to $|x_0\ket$.\\
3. Measure to see $x_0$ with high probability $\sim 1-\frac{1}{N}$.

For large $N$, $\arccos(\frac{1}{\sqrt{N}}) \approx \pi/2$, $\arcsin(\frac{1}{\sqrt{N}}) \approx \frac{1}{\sqrt{N}}$ so $m = \frac{\pi}{4} \sqrt{N}$ iterations/queries to $U_f$ suffice.\\
Classically we need $O(N)$ queries to see $x_0$ with any \emph{constant} probability (independent of $N$), so get \emph{square-root} speed up quantumly.

Amplitude Amplification:\\
Let $G$ be any subspace (\emph{good subspace}) of state space $\mathcal{H}$, and $G^\perp$ is orthogonal complement (\emph{bad subspace}) $\mathcal{J} = G \oplus G^\perp$.\\
Given any $|\psi \ket \in \mathcal{H}$, we have unique decompoisiton with \emph{real positive} coefficients
$$|\psi\ket = \sin\theta |g\ket + \cos\theta |b\ket$$
where $|g\ket \in G$, $|b\ket \in G^\perp$ normalised. Introduce reflections: flip $|\psi\ket $ and good vectors: $I_{|\psi\ket} = I-2|\psi \times \psi|$, $I_G = I-2P_G$ (projection into $G$), so $\sin\theta = ||P_G |\psi\ket||$ is the length of good projection.\\
Introduce $Q \stackrel{def}{=} -I_{|\psi\ket} I_G$.

\begin{thm} (Amplitude Amplification)\\
In the 2-dimensional subspace spanned by $|g\ket$ and $|\psi\ket$ (or equivalently by orthonormal vectors $|g\ket$ and $|b\ket$), $Q$ is rotation by $2\theta$ where $\sin\theta$ is the length of good projection of $|\psi\ket$.
    \begin{proof}
        We have $I_G|g\ket = -|g\ket$, $I_G|b\ket = |b\ket$. So $Q|g\ket = +I_{|\psi\ket} |g\ket$, $Q|b\ket = -I_{|\psi\ket} |b\ket$.\\
        Now
        \begin{equation*}
            \begin{aligned}
                I_{|\psi\ket} &= I-2(\sin\theta|g\ket + \cos\theta|b\ket)(\sin\theta\bra g| + \cos\theta\bra b|)\\
                &= I-2[\sin^2\theta |g\times g| + \sin\theta\cos\theta |g\times b| + \sin\theta\cos\theta|b \times g| + \cos^2\theta |b\times b|] |b\ket
            \end{aligned}
        \end{equation*}
        And direct calculation (using $\bra g| b\ket =0$, $\bra g|g\ket = \bra b|b\ket = 1$) gives
        \begin{equation*}
            \begin{aligned}
                Q|b\ket &= I_{|\psi\ket} |b\ket\\
                &=2\sin\theta\cos\theta |g\ket - (1-2\cos^2\theta) |b\ket\\
                &= \cos 2\theta |b\ket + \sin 2\theta|g\ket
            \end{aligned}
        \end{equation*}
        and $Q|g\ket = +I_{|\psi\ket} |g\ket = -\sin 2\theta |b\ket + \cos 2\theta |g\ket$.\\
        So in $\{|b\ket,|g\ket\}$ basis, matrix of $Q$ is exactly the matrix of rotation by $2\theta$.
    \end{proof}
\end{thm}

---Lecture 10---

Let's continue on Amplitude Amplification.

Last time we showed that $Q=-I_{|\psi\ket} I_g$ is the rotation through $2\theta$ in the plane of $|\psi\ket$ and $|g\ket$, i.e. in $|b\ket$ and $|g\ket$ (orthonormal).\\
So $Q^n|\psi\ket = \sin(2n+1)\theta |g\ket + \cos(2n+1)\theta |b\ket$, and if we measure $Q^n |\psi\ket$ for good vs bad, we get $prob(good) = \sin^2(2n+1)\theta$.\\
We want to maximize this: it is maximised when $(2n+1)\theta = \pi/2$, i.e. $n=\frac{\pi^2}{4\theta} - \frac{1}{2}$.

\begin{eg}
    \emph{If} we had $\theta =4/6$, then $n=\frac{\pi}{4\theta} - \frac{1}{2} = 1$ is an exact integer. So $Q^1$ rotates $|\psi\ket$ \emph{exactly} onto $|g\ket$, so we see good result with certainty!
\end{eg}

Generally, for given $\theta$, $n$ is not an integer. So we use $n$ to be the nearest integer to $(\frac{\pi}{4\theta} - \frac{1}{2}) \approx \frac{\pi}{4\theta}$ (for small $\theta$), which equals $O(\frac{1}{\theta} = O(\frac{1}{\sin\theta}) = O(\frac{1}{||good\ proj\ of\ |\psi||})$), and $Q^n|\psi\ket$ will be within angle $\pm \theta$ of $|g\ket$, so probability of good result is at least $\cos^2\theta \approx 1-O(\theta^2)$.

All this can be implemented oif $I_{|\psi\ket}$ and $I_G$ can be implemented. See example sheet -- for $I_G$, suffices for $G$ to be spanned by computational basis states $|x\ket$'s, and indicator function $f(x)=1$ for $x$ good and $0$ for $x$ bad efficiently computable.\\
For $I_{|\psi\ket}$, usally have $|\psi\ket =H_n|00...0\ket$ ($H$ is the Hadamard gate). Then $I_{|\psi\ket}$ can be implemented in linear $O(n)$ time.

Notes:\\
(1) In AA process, relative amplitudes of good labels in $|g\ket$ stay \emph{same} as they were in $|\psi\ket = \sin\theta|g\ket + \cos\theta |b\ket$.\\
(2) Final state is generally not exactly $|g\ket$, but \emph{if} $\sin\theta$ is \emph{known}, then we can modify AA process to make it exact, i.e. giving $|g\ket$ state exactly (see example sheet).

Applications of AA:\\
(1) \emph{Grover Search} with one or \emph{more} ($k$) good items in $N$:\\
\begin{equation*}
    \begin{aligned}
        |\psi\ket &= |\psi_0\ket = \frac{1}{\sqrt{2^n}} \sum_{x \in B_n} |x\ket\\
        &= \sqrt{\frac{k}{N}} \left(\frac{1}{\sqrt{k}} \sum_{good\ |g\ket} |x\ket\right) + \sqrt{\frac{N-k}{N}} \left(\frac{1}{\sqrt{N-k}} \sum_{x\ bad}|x\ket\right)
    \end{aligned}
\end{equation*}
$G$ spanned by good $x$'s, $\sin\theta = \frac{k}{n}$ so $Q$ is rotation through $2\theta$, $\theta=\arcsin\sqrt{k/N} \approx \sqrt{k/N}$, where $k\ll N$; and we only need $O(\sqrt{N/k})$ queries.

Note: for $2$-bit case, $N=4$ with $k=1$ good item; we have $\theta = \arcsin(1/2) = \pi/6$, so one application of $Q$ rotates $|\psi_0\ket$ exactly onto $|x_{good}\ket$, i.e. a \emph{single} query suffices to find a unique good item in four, \emph{with certainty}!

(2) Square-root sppedup of general quantum algorithms:\\
Let $A$ be a quantum algorithm/circuit (sequence of unitary gates). on input, say $|0...0\ket$. So final state is $A|0...0\ket$.\\
Good labels = desired computational outcomes
$$A|0.000\ket = \alpha|a\ket + \beta|b\ket, \alpha=\sin\theta$$
where $|a\ket$ is normalised, genrally unequal superposition $\sum_{good\ x} c_x|x\ket$.\\
So $Prob(\text{success in 1 run}) = |\alpha|^2$, so $O(\frac{1}{|\alpha|^2})$ repetitions of $A$ needed to succeed with any \emph{constant} high probability $1-\varepsilon$.

Instead use AA: assumed we can check if answer is good or bad (e.g. factoring).\\
So we can then implement $I_G:|x\ket \to -|x\ket$ if $x$ is good, and $\to |x\ket$ if $x$ is bad.\\
Consider $|\psi\ket = A|0...0\ket$ and $Q=-I_{A|0...0\ket} I_G = -(AI_{|0...0\ket} A^\dagger) I_G$. All parts are implementable ($A$ is the algorithm, $A^\dagger$ is inverse gate in reverse order, and $I_{|0...0\ket}$ see example sheet).\\
By AA theorem, $Q$ is rotation through $2\theta$, where $\sin\theta=|\alpha|$. So after $n\approx \frac{\pi}{4\theta} = O(\frac{1}{\theta}) = O(\frac{1}{\sin\theta}) = O(\frac{1}{|\alpha|})$ (for small $|\alpha|$).\\
Repetitions $A|0...0\ket$ will be rotate very near to $|g\ket$, and final measurement will succeed with high probability.\\
Each application of $\mathcal{Q}$ needs one $A$ and one $A^\dagger$; $A^\dagger$ is the \emph{inverse gate in reverse order}, i.e. the time complexity is the same, i.e. $O(\frac{1}{|\alpha|})$ repetition of $Q$ gives square root time speed up over direct method.

Also, if success probability of $A$ (i.e. $|\alpha|^2$) is known, then \emph{improved} modification of the AA process that is \emph{exact} can be applied; we convert probabilistic algorithm $A$ into \emph{deterministic} one, giving a good outcome with certainty.

\newpage

\section{Quantum Counting}

Given $f:B_n \to B$ a boolean function with an unknown number $k$ good $x$'s, we want ot \emph{estimate} $k$ (rather than just find some good $x$).

Recall that Grover operator $Q_G$ for $f$ is rotaion through $2\theta$ in 2-dimensional space of $|\psi_0 = \frac{1}{\sqrt{2^n}} \sum_{x \in B_n} |x\ket$ and its good projection $|g\ket = \frac{1}{\sqrt{k}} \sum_{good\ x} |x\ket$, with $\sin\theta = \sqrt{k/N} \approx \theta$ for $k \ll N$.

---Lecture 11---

(Did I miss a lecture?)

\subsection{Hamiltonian Simulation}

$k$-local Halmiltonians:

$H$ on $n$ qubits is a $2^n \times 2^n$ Hermitian matrix. We'll want to simulate $U=e^{-iHt}$ with a circuit of $poly(n,t)$ basic unitary gates, i.e. efficient simulation.\\
Not all $H$'s can be efficently simulated, but some physically important classes can --

\begin{defi}
    $H$ is $k$-local ($k$ is a fixed constant) on $n$ qubits if $H=\sum_{j=1}^m H_j$ where each $H_j$ is a Hermitian matrix acting on at most $k$ qubits (not necessarily contiguous).\\
    i.e. each $H_j = \tilde{H}_j \otimes I$ (on some $k$ qubits, and identity on rest of qubits).

    So $m \leq {n \choose i} =O(n^k) = poly(n)$ terms in $H$.
\end{defi}

\begin{eg}
    (1) $H=X \otimes I \otimes I - 5 Z \otimes I \otimes Y$ is $2$-local on 3 qubits.\\
    (2) Write $M_{(j)}$ to denote operator $M$ acting on $j^{th}$ qubit (and $I$ on all others). Physically important spin-spin interactions:\\
    Ising model on $n\times n$ square lattice of qubits, $H=J \sum_{i,j=1}^{n-1} Z_{(i,j)} Z_{(i,j+1)} + Z_{(i,j)}Z_{(i+1,j)}$, i.e. all nearest neighbours on square.\\
    Heisenberg model on a line: $H=\sum_{i=1}^{n-1} J_x X_{(i)} X_{(i+1)} + J_y Y_{(i)} Y_{(i+1)} + J_z Z_{(i)} Z_{(i+1)}$ (where $J,J_x,J_y,J_z$ are all real constants). This is very relevatn on chemistry (in studying covalence bonds?)
\end{eg}

Note: in general, $e^{-i\sum_{j} H_j t} \neq \prod_j (e^{-i H_j t})$ -- if the $H_j$'s don't commute.

But $e^{-iH_j t}$'s \emph{are} local unitary gates (acting on $k$ qubits each), and we'll simulate $U(t_0) = e^{-i \sum_j H_i t_0}$ in terms of these (for suitable $t$'s), and we'll have a $poly(n,t_0)$-sized circuit too.

If we want to use some standard universal gate set (to further express the above gates), then use: \emph{Solovay-Kitaev theorem}: Let $U$ be a unitary operator on $k$ (const) qubits, and $S$ any universal set of quantum gates ($S$ is the set that have property that if we look at all circuit and all finite sets, then it's dense in all circuits?? Lecturer didn't write down).\\
Then $U$ can be approximated to within $\varepsilon$ using $O(\log^c(1/\varepsilon))$ gates from $S$ with $c < 4$ (it's actually exponential in $k$, but here we consider $k$ constant).\\
So all $S$-products of length $O(\log^c(1/\varepsilon))$ get within $\varepsilon$ of any element of $U(k)$.

We'll also need a lemma about accumulation of errors (c.f. Example sheet).\\
We call this lemmma A: let $\{u_i\}, \{v_i\}$ be sets of $m$ unitary operators, with $||u_i - v_i|| < \varepsilon$ for all $i=1,2,...,m$, then
$$||u_m...u_1 - v_m...v_1|| \leq m\varepsilon$$
i.e. errors accumulate linearly.\\
Proof is an easy exercise -- induction on $m$.

Warm up: the (easy) commuting case.
\begin{prop} ($k$-local Hamiltonioans with commuting terms)\\
    $H=\sum_{j=1}^m H_j$ with $H_j$ commuting -- any local Hamiltonian with commuting terms.\\
    Then for any $t$, $e^{-iHt}$ can be approximated to within $\varepsilon$ by a circuit of $O(m\ poly(\log \frac{m}{\varepsilon}))$ gates from any given universal set.\\
    Note, as $m=O(n^k)$ this is $poly(n,\log\frac{1}{\varepsilon})$ too. Also, $\log(\frac{1}{\varepsilon})$ is the number of digits of precision in the approximation.
    \begin{proof}
        $H_j$'s commute implies that $e^{-i \sum_j H_j t} = \prod_{j=1}^m (e^{-i H_j t})$. Then SK theorem implies, for each $e^{-iH_j t}$ can be approximated to within $\varepsilon/m$ within $O(poly(\log\frac{m}{\varepsilon}))$ gates, so lemma $A$ then implies that the full product $\prod_{j=1}^m$ is then approximated to within $m(\varepsilon/m) = \varepsilon$, with a total of $O(m\ poly(\log \frac{m}{\varepsilon}))$ gates (from the universal set).
    \end{proof}
\end{prop}

Now let's look at the full non-commuting case: for any matrix $X$, write $X+O(\varepsilon)$ for $X+E$ where $||E|| = O(\varepsilon)$.

\begin{lemma} (B, Lie-Trotter product formula)\\
    Let $A,B$ be matrices with $||A|| \leq K$, $||B|| \leq k$ and $k<1$ (small).\\
    Then $e^{-iA} e^{-iB} = e^{-i(A+B)} + O(k^2)$.
    \begin{proof}
        \begin{equation*}
            \begin{aligned}
                e^{-iA} &= I-iA + \sum_{k=2}^\infty \frac{(-iA)^k}{k!}\\
                &= I-iA + (iA)^2 \sum_{k=0}^\infty \frac{(-iA)^k}{(k+2)!}\\
                &= I-iA + O(k^2)
            \end{aligned}
        \end{equation*}
        since $||(iA)^2|| < k^2$, and the remainder term is at most $1$. So
        \begin{equation*}
            \begin{aligned}
                e^{-iA} e^{-iB} &= (I-iA + O(k^2)) (I-iB + O(k^2))\\
                &= I-i(A+B) + O(k^2)\\
                &= e^{-i(A+B)} + O(k^2)
            \end{aligned}
        \end{equation*}
        by applying the inverse of above
    \end{proof}
\end{lemma}
Now apply this repeatedly to accumulate sums of $H_1,...,H_m$ in exponent. Note thta if each $||H_i|| < k$, then $||H_1+...+H_l|| < lk$, we want this to be $<1$ for all $l\leq m$.\\
So \emph{for now}, we'll assume $||H_i|| < \frac{1}{m}$ to have Lie-Trotter for all stages. Also take $t=1$ \emph{for now}.\\
Then consider
\begin{equation*}
    \begin{aligned}
        e^{-iH_1} e^{-iH_2}...e^{-iH_m} &= \left[e^{-i(H_1+H_2)}+O(k^2)\right] e^{-iH^3} ... e^{-iH_m}\\
        &= e^{-i(H_1+H_2)} e^{-iH_3} ... e^{-iH_m} + O(k^2)\\
        &= ...\\
        &= e^{-i(H_1+H_2+...+H_m)} +O(k^2)+O((2k)^2)+...+O(((m-1)k)^2)\\
        &= e^{-i(H_1+H_2+...+H_m)} + O(m^3k^2)\ (1)
    \end{aligned}
\end{equation*}
where in the second equality we used that $||AU|| = ||A||$ for any unitary $U$, and note that the sum of squares up to $m$ is of order $m^3$.

---Lecture 12---

For general finite $||H_j||$'s and $t$ values, $||H_j t|| < kt$ can be large, so interoduce $N$ (large-ish, fix later), and note
$$\frac{H_j t}{N} \text{ has } \tilde{K} = ||\frac{H_j t}{N}|| < \frac{kt}{N}\ (*)$$
can be suitably small, i.e. divide $t$ into (small) $\frac{1}{N}$ intervals,
$$U=e^{i(H_1+...+H_m)t} = [e^{i(\frac{H_1t}{N} + ... + \frac{H_mt}{N})}]^N$$
We want final error for $U$ to be $<\varepsilon$, so by lemma $A$, we want error for $[...]$ to be $<\frac{\varepsilon}{N}$.\\
So by (1) and (*), $Cm^3 \tilde{k}^2 < \frac{\varepsilon}{N}$, i.e. $Cm^3 \frac{K^2t^2}{N^2} < \frac{\varepsilon}{N}$, i.e. $N>\frac{Cm^3K^2t^2}{\varepsilon}$ (2).\\
Then
$$||e^{-iH_1t/N} e^{-iH_2t/N}...e^{-iH_mt/N} - e^{-i\frac{(H_1+...+H_m)t}{N}}|| < \frac{\varepsilon}{N}$$
so by lemma $A$ again,
$$||(e^{-iH_1t/N}...e^{-iH_mt/N})^N - e^{-i(H_1+...+H_m)t}||< \varepsilon$$
(this is $Nm$ gates if form $e^{-iH_jt/N}$, and $N$ given by (2)). So circuit size is $O(\frac{m^4(Kt)^2}{\varepsilon})$.\\
Recall for $n$ qubits and $k$-local Hamiltonians, $m=O(n^k)$, so circuit size is $O(n^{4k} \frac{(Kt)^2}{\varepsilon}) = O(n^{4k},t^2,\frac{1}{\varepsilon})$.

We have circuit $\mathcal{C}$ of size $|\mathcal{C}| = O(m^4 \frac{(Kt)^2}{\varepsilon})$ gates of form $e^{-i\tilde{H}_j/N}$ approximate to $O(t)$.\\
If we want to use standard universal set, lemma A gives each of these gates needs to be approximate to $O(\varepsilon/|\mathcal{C}|)$ to maintain an overall $O(\varepsilon)$ level approximation to $U$.\\
So by SK theorem, need $O(\log^c(|\mathcal{C}|/\varepsilon))$ gates from the universal set for each, i.e. get (modest) multiplicative factor of $O(\log^c \frac{m^4(Kt)^2}{\varepsilon^2})$ in $|\mathcal{C}|$.\\

For fixed $n,\varepsilon$, and variable $t$, the quantum process $e^{iHt}$ runs for time $t$; but our circuit simulation runs for time $O(t^2)$. By refining/improving Lie-Trotter formula, it can be shown that this can be improved to $O(t^{1+\delta})$ for any $\delta>0$ (See example sheet 2 for an example).

Harrow-Hassidim-Lloyd (HHL) quantum algorithm for linear systems of equations:\\
We'll want to solve a linear system of equations $A \mathbf{x} = \mathbf{b}$, where $\mathbf{b},\mathbf{x} \in \C^N$, and dimension $N$ is potentially \emph{very} large (we could set $N=2^n$, where $2^n$ is the least power of 2 that is greater than $N$).\\
Rather than outputting the full solution $\mathbf{x}$ itself, which would take $O(N)$ time, we instead want to compute (suitable approximates to) the value of properties of the solution, such as quadratic expressions $\mathbf{x}^T M \mathbf{x}$ e.g. total weight of some subset of components.

Very large lineary systems are important in applications: data mining/machine learning on data sets of increasingly large size to discover pattern properties in data; in science/engineering , we have numerical solutions of PDEs, where discretisation techniques (finite element methods) lead to linear systems far larger than original problem description.

The best known classical techniques take $poly(N)$ time to solve such problems.

Important parameters (for both classical and quantum algorithms):\\
$\bullet$ the system size $N$;\\
$\bullet$ the desired approximate tolerance $\varepsilon$;\\
$\bullet$ the \emph{condition number} $\kappa$ of matrix $A$, defined as ratio of largest to smallest eigenvalue size $\kappa = |\frac{\lambda_{max}}{\lambda_{min}}|$;\\
It provides intrinsic scale of the linear transformation $A$, and is a measure of how close $A$ is to being non-invertible.\\
If renormalize $A$ to have $\lambda_{max} = 1$, then $|\lambda_{min}| = 1/\kappa$, and numerical computation of $A^{-1}$ becomes less stable with increasing $\kappa$, needing more significant digits and correspondingly longer runtime.

Preliminary requirements for the HHL algorithm:\\
Aim: to compute $\varepsilon$-approximate to properties of the solution of an $N$-dimensional system $A\mathbf{x} = \mathbf{b}$ in time $poly(\log N) = poly(n)$. We'll take the property to be a quadratic expression $\mu = \mathbf{x}^T M \mathbf{x}$.

Immediate issue is how the defining ingredients viz $A$ and $\mathbf{b}$ are actually given (as reading them componently would already have taken $O(poly(N))$ time). Thus we'll need a different presentation of the problem (cf below) that is still available in applications.

The $poly(\log N)$ run time will be achieved by using only $O(\log N)$ qubits and it will never be required to write down all of $A$ or $\mathbf{b}$ or $\mathbf{x}$ in the course of the algorithm (as that would be impossible given the complexity constraints).

\newpage

\section{Example Class 1}

\subsection{Question 1}
Basic representation theory exercises.\\
For details see Part II Representation Theory.

\subsection{Question 2}
Let $G=(\Z_2^n, \oplus)$ where $\oplus$ is componentwise-addition. Subgroup $K$ generated by $a_1...a_k$, $K=\{b_1a_1 \oplus ... \oplus b_ka_k:b_1,...,b_k \in \Z_2\}$. Note that $K$ has size $2^k$ if $a_i$'s are LI, and so does any coset of $K$.\\
Then $f(x) = f(x \oplus a_i)$ for all $a_i$'s $\equiv$ $f$ constant on cosets of $K$. $f$ is $2^k$-to-$1$: $a_k$'s all linearly independent and $f$ different on different cosets.

Shift invariant states: For $\Z_2$ irreps are $\chi_a(x) = (-1)^{ax)}$, $a,x \in \Z_2$ ($-1$ is the 2nd root of unity). So irreps on $(\Z_2)^n$ are $\chi_a(x) =(-1)^{a_1x_1} ... (-1)^{a_nx_n}$ where $a=a_1...a_n$, $x=x_1...x_n$ are in $\Z_2^n$.\\
We also introduce a dot product $a \cdot x = a_1x_1 \oplus ... \oplus a_n x_n \in \Z_2$ where $\oplus$ here is $+$ modulo 2.\\
So shift invariant states
$$|\chi\ket = \frac{1}{\sqrt{|G|}} \sum_g \overline{\chi(g)} |g\ket$$
So
$$|\chi_a\ket = \frac{1}{\sqrt{2^n}} \sum_{b \in \Z_2^n} (-1)^{a\cdot b} |b\ket$$
in $n$ qubits. So
$$(QFT)_{alr(?)} = \frac{1}{\sqrt{2^n}} (-1)^{a \cdot b} = \frac{1}{\sqrt{2^n}} (-1)^{a_1b_1}...(-1)^{a_nb_n}$$
so $QFT = H \otimes H \otimes ... \otimes H$ where $H$ is the Hadamard gate.\\
For second part, it's just calculation:\\
Probability that first string is LI is $1-2^{-m}$ (just exclude $0...0$);\\
Probability that first 2 strings are LI given the first is is $1-2/2^m$ (i.e. as 1st string $x_1$ spans 2 strings, namely $0...0$ and $x_1$).\\
...Probability that first $j$ strings are LI given the first $j-1$ are is $1-2^{j-1}/2^m$.\\
So by Bayes rule, probability that all of them are LI is $(1-1/2^m)...(1-2^{m-2}/2^m)(1-2^{m-1}/2^m)$. Use the hint given we get that is at least $\frac{1}{2}(1-1/2) =1/4$.

Stanrdard HSP algorithm:\\
1. query to $f$, get random coset state $|y \oplus K \ket = \frac{1}{\sqrt{2^n}} \sum_{x \in K} | y \oplus x\ket, y \in \Z_2^n$.\\
Apply $QFT=H^{\otimes n}$ and measure; our theory assures that output is then uniformly random $c\in(\Z_2)^n$ s.t. irrep $\chi_c$ of $G$ restricted to $K$ is \emph{trivial irrep} of $K$, i.e .$\chi_c(a) = 1$ for all $a \in K$.\\
So $(-1)^{a \cdot c} = 1$ for all $a \in K$, i.e. $c \cdot a = 0 \pmod 2$ for all $a=K$, i.e. $c_1a_1\oplus ... \oplus c_na_n=0$ for $a=a_1...a_n$.\\
We know $K$ viewed as subspace of $(\Z_2)^n$ ($n$ dimensional vector space over field $\Z_2$) has dimension $k$. So $(n-k)$ LI $c_i$'s with $c_1 \cdot a=0$ suffice to determine $K$ as null spaceof linear system of $(n-k)$ equations.\\
So run HSP algorithm $(n-k)$ times: by (b) we'll get $(n-k)$ LI $c's$ with probability at least 1/4, and we can solve for elements of $K$.

\end{document}


\documentclass[../psets.tex]{subfiles}

\pagestyle{main}
\renewcommand{\leftmark}{Problem Set \thesection}
\setcounter{section}{4}

\begin{document}

\section{Thermodynamics}
\begin{enumerate}[label={\arabic*)}]
    \item \marginnote{8/23:}\textcite{bib:YoungFreedman}: Problem 36.21.\par
    An interference pattern is produced by light of wavelength $\SI{580}{\nano\meter}$ from a distant source incident on two identical parallel slits separated by a distance (between centers) of $\SI{0.530}{\milli\meter}$.
    \begin{enumerate}
        \item If the slits are very narrow, what would be the angular positions of the first-order and second-order, two-slit interference maxima?
        \item Let the slits have width $\SI{0.320}{\milli\meter}$. In terms of the intensity $I_0$ at the center of the central maximum, what is the intensity at each of the angular positions in part (a)?
    \end{enumerate}
    \item \textcite{bib:YoungFreedman}: Problem 36.44.\par
    \textbf{Observing Jupiter.} You are asked to design a space telescope for Earth orbit. When Jupiter is $\SI{5.93e8}{\kilo\meter}$ away (its closest approach to Earth), the telescope is to resolve, by Rayleigh's criterion, features on Jupiter that are $\SI{250}{\kilo\meter}$ apart. What minimum-diameter mirror is required? Assume a wavelength of $\SI{500}{\nano\meter}$.
    \item \textcite{bib:YoungFreedman}: Problem 18.6.\par
    You have several identical balloons. You experimentally determine that a balloon will break if its volume exceeds $\SI{0.900}{\liter}$. The pressure of the gas inside the balloon equals air pressure ($\SI{1.00}{\atmosphere}$).
    \begin{enumerate}
        \item If the air inside the balloon is at a constant $\SI{22.0}{\celsius}$ and behaves as an ideal gas, what mass of air can you blow into one of the balloons before it bursts?
        \item Repeat part (a) if the gas is helium rather than air.
    \end{enumerate}
    \item \textcite{bib:YoungFreedman}: Problem 18.21.\par
    Modern vacuum pumps make it easy to attain pressures of the order of $\SI{e-13}{\atmosphere}$ in the laboratory. Consider a volume of air and treat the air as an ideal gas.
    \begin{enumerate}
        \item At a pressure of $\SI{9.00e-14}{\atmosphere}$ and an ordinary temperature of $\SI{300.0}{\kelvin}$, how many molecules are present in a volume of $\SI{1.00}{\cubic\centi\meter}$?
        \item How many molecules would be present at the same temperature but at $\SI{1.00}{\atmosphere}$ instead?
    \end{enumerate}
    \item \textcite{bib:YoungFreedman}: Problem 18.38.\par
    Perfectly rigid containers each hold $n$ moles of an ideal gas, one being hydrogen (\ce{H2}) and the other being neon (\ce{Ne}). If it takes $\SI{300}{\joule}$ of heat to increase the temperature of the hydrogen by $\SI{2.50}{\celsius}$, by how many degrees will the same amount of heat raise the temperature of the neon?
    \item \textcite{bib:YoungFreedman}: Problem 19.10.\par
    Five moles of an ideal monatomic gas with an initial temperature of $\SI{127}{\celsius}$ expand and, in the process, absorb $\SI{1500}{\joule}$ of heat and do $\SI{2100}{\joule}$ of work. What is the final temperature of the gas?
    \item \textcite{bib:YoungFreedman}: Problem 19.43.\par
    The following figure shows a $pV$-diagram for $\SI{0.0040}{\mole}$ of \emph{ideal} \ce{H2} gas. The temperature of the gas does not change during segment $bc$.
    \begin{center}
        \begin{tikzpicture}[
            xscale=2.5,
            pics/point/.style args={#1:#2}{code={
                \node [circle,fill,inner sep=1.5pt,label={#1:$#2$}] {};
            }}
        ]
            \footnotesize
            \draw (-0.2,0) -- (1,0) node[right,label={[xshift=-4pt]right:$(\si{\liter})$}]{$V$};
            \draw (0,-0.3) -- (0,2.5) node[above,label={[xshift=-4pt]right:$(\si{\atmosphere})$}]{$p$};
            \node [below left] {$O$};

            \coordinate (a) at (0.2,0.5);
            \coordinate (b) at (0.2,2);
            \coordinate (c) at (0.8,0.5);

            \draw [very thin,dashed] (a) -- (a |- 0,0) node[below]{$0.20$};
            \draw [very thin,dashed] (a) -- (a -| 0,0) node[left]{$0.50$};
            \draw [very thin,dashed] (b) -- (b -| 0,0) node[left]{$2.0$};

            \draw [blx,very thick,line join={bevel},postaction={decorate},decoration={
                markings,
                mark=at position 0.15 with \arrow{latex},
                mark=at position 0.53 with \arrow{latex},
                mark=at position 0.9 with \arrow{latex}
            }] (a) -- plot[domain=0.2:0.8] (\x,{0.4/\x}) -- cycle;

            \pic at (a) {point={[xshift=1pt,yshift=1pt]below left:a}};
            \pic at (b) {point=above:b};
            \pic at (c) {point={[xshift=-1pt,yshift=1pt]below right:c}};
        \end{tikzpicture}
    \end{center}
    \begin{enumerate}
        \item What volume does this gas occupy at point $c$?
        \item Find the temperature of the gas at points $a$, $b$, and $c$.
        \item How much heat went into or out of the gas during segments $ab$, $ca$, and $bc$? Indicate whether the heat has gone into or out of the gas.
        \item Find the change in the internal energy of this hydrogen during segments $ab$, $bc$, and $ca$. Indicate whether the internal energy increased or decreased during each segment.
    \end{enumerate}
    \item You hear the weather report on the radio. However, the announcer forgets to say what scale is being used: Celsius or Fahrenheit. If it doesn't matter, how cold is it outside?
    \item A pinhole camera can produce a surprisingly sharp image. The key is using a small hole so only a narrow bundle of rays is allowed through. However, if the pinhole is too small, then diffraction will limit the sharpness of the image. The optimum pinhole size is one that makes the fuzziness due to bundle size comparable to the fuzziness due to diffraction. Assume the distance from pinhole to screen is 1 foot, and the wavelength of the light is $\SI{5500}{\angstrom}$. What is the optimum size of the pinhole?
\end{enumerate}

\end{document}


\documentclass[10pt,a4paper]{article}
\usepackage{fullpage}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{todonotes}
\usepackage{multirow,matlab-prettifier}
\usepackage[T1]{fontenc}

\hyphenpenalty=5000
\tolerance=1000

\newcommand{\qmax}[0]{Q_{\textrm{max}}}
\newcommand{\nuab}[0]{$\nu_{ab}$}

\newcommand{\nuee}[0]{$\nu_{ee}$}
\newcommand{\nuei}[0]{$\nu_{ei}$}
\newcommand{\nues}[0]{$\nu_{es}$}
\newcommand{\nuse}[0]{$\nu_{se}$}
\newcommand{\nusr}[0]{$\nu_{sr}$}
\newcommand{\nusn}[0]{$\nu_{sn}$}
\newcommand{\nure}[0]{$\nu_{re}$}
\newcommand{\nurs}[0]{$\nu_{rs}$}

\newcommand{\gab}[0]{$G_{ab}$}

\newcommand{\gabcd}[0]{$G_{abcd}$}
\newcommand{\gese}[0]{$G_{ese}$}
\newcommand{\gesre}[0]{$G_{esre}$}
\newcommand{\gsrs}[0]{$G_{srs}$}

\newcommand{\xyz}[0]{$XY\!Z$}

\newcommand{\gee}[0]{$G_{ee}$}
\newcommand{\gei}[0]{$G_{ei}$}
\newcommand{\ges}[0]{$G_{es}$}
\newcommand{\gse}[0]{$G_{se}$}
\newcommand{\gsr}[0]{$G_{sr}$}
\newcommand{\gsn}[0]{$G_{sn}$}
\newcommand{\gre}[0]{$G_{re}$}
\newcommand{\grs}[0]{$G_{rs}$}
\newcommand{\e}[1]{\times 10^{#1}}
\newcommand{\phia}[0]{$\phi_a$}
\newcommand{\phie}[0]{$\phi_e$}
\newcommand{\phii}[0]{$\phi_i$}
\newcommand{\phir}[0]{$\phi_r$}
\newcommand{\phis}[0]{$\phi_s$}
\newcommand{\phies}[0]{$\phi_{es}$}
\newcommand{\phirs}[0]{$\phi_{rs}$}
\newcommand{\phin}[0]{$\phi_n$}
\newcommand{\vsnl}[0]{$V^{(2)}_s(\mathbf{k},\omega)$}

\usepackage{setspace}
\onehalfspacing

\begin{document}

\section{MCMC Alternate Models}
{\bf Romesh Abeysuriya} \today
\sffamily

This document outlines the different models available for the MCMC fitting. In terms of code, {\tt template.m} is the abstract class that provides the standard interface for all model objects.

\begin{align}
\mathcal{F}(D_\alpha) &= \left(1 - \frac{i\omega}{\alpha}\right)\left(1 - \frac{i\omega}{\beta}\right)\\[16pt]
\mathcal{F}(D_a) &= \left( 1-\frac{i\omega}{\gamma}\right)^2 + k^2r^2
\end{align}

\subsection{Full model}

Implemented in {\tt full.m}. Includes EMG.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.4\textwidth]{full_model}
\caption{Full model schematic. A factor of $L$ is included for each connection marked with a red dot.}
\label{fig:full}
\end{center}
\end{figure}

This model implements the full EIRS system, in terms of the gains. Volume condution is incorporated with

\begin{align}
\mathbf{k} &= \left( \frac{2\pi m}{L_x},\frac{2\pi n}{L_y} \right),\\
F(k) &= e^{-k^2/k_0^2}.
\end{align}

The power spectrum is given by

\begin{align}
 q^2r_e^2 &= \left( 1-\frac{i\omega}{\gamma_e} \right)^2   - \frac{1}{1-G_{ei}L} \left\lbrace LG_{ee} + \frac{\left[ L^2G_{ese}  + L^3 G_{erse}\right]e^{i\omega t_0}}{1 - L^2 G_{srs}}  \right\rbrace \\
T &=  \frac{L^2G_{esn}e^{i\omega t_0 /2}}{(1 - L^2 G_{srs})(1-G_{ei}L)} \frac{1}{k^2r_e^2 + q^2r_e^2}\\
 P(\omega) &= \sum_{m = -\infty}^{\infty}\sum_{n = -\infty}^{\infty} \Delta k_x \Delta k_y |T(\mathbf{k},\omega)|^2||\phi_n(\mathbf{k},\omega)|^2F(k)\\
k^2 &=  \left( \frac{2\pi m}{L_x} \right)^2 + \left( \frac{2\pi n}{L_y}\right)^2
\end{align}

Stability is identified by negative real part zero crossings of the dispersion relation

\begin{align}
\label{eqn:dispersion_relation}
\left( \left[ \left( 1-\frac{i\omega}{\gamma_e} \right)^2 + k^2r_e^2 \right](1-G_{ei}L) - LG_{ee} \right) (1 - L^2 G_{srs}) - (L^2G_{ese}  + L^3 G_{erse})e^{i\omega t_0} &= 0
\end{align}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_full}
\caption{Example fit with full model.}
\label{fig:full}
\end{center}
\end{figure}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_full_k0}
\caption{Example fit with full model, with $k=0$ only.}
\label{fig:full}
\end{center}
\end{figure}

\clearpage

\subsection{Reduced model with L}
Implemented in {\tt reduced\_l.m}. This is a new model, and bears the most (implementation) similarity to the full model. In this model, $L_{sr}$ is moved to the connection $rs$. Thus the $srs$ loop still gets two factors of $L$, but now $L_{ese} = L_{erse}$. This enables factorization of the $L$ terms in $Y$. However, separate parameters are still required for $G_{ee}$ and $G_{ei}$.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.4\textwidth]{reduced_l}
\caption{Full model schematic. A factor of $L$ is included for each connection marked with a red dot.}
\label{fig:full}
\end{center}
\end{figure}

The power spectrum is given by

\begin{align}
 q^2r_e^2 &=  \left( 1-\frac{i\omega}{\gamma_e} \right)^2 - \frac{LG_{ee}}{1-LG_{ei}} - \frac{L^2(Y\left( 1+Z'\right)(1-G_{ei}))e^{i\omega t_0}}{\left( 1+Z' L^2 \right)(1-LG_{ei})}\\
 T &=  \frac{L^2G_{esn}e^{i\omega t_0 /2}}{(1 - L^2 G_{srs})(1-G_{ei}L)} \frac{1}{k^2r_e^2 + q^2r_e^2}\\
 P(\omega) &= \sum_{m = -\infty}^{\infty}\sum_{n = -\infty}^{\infty} \Delta k_x \Delta k_y |T(\mathbf{k},\omega)|^2||\phi_n(\mathbf{k},\omega)|^2F(k)\\
k^2 &=  \left( \frac{2\pi m}{L_x} \right)^2 + \left( \frac{2\pi n}{L_y}\right)^2
\end{align}

Stability is identified by negative real part zero crossings of the dispersion relation

\begin{align}
\label{eqn:dispersion_relation}
\left( \left[ \left( 1-\frac{i\omega}{\gamma_e} \right)^2 + k^2r_e^2 \right](1-G_{ei}L) - LG_{ee} \right) \left( 1+Z' L^2 \right) - L^2 Y\left( 1+Z' \right)\left( 1-G_{ei} \right) e^{i\omega t_0} &= 0
\end{align}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced_l}
\caption{Example fit with \texttt{reduced\_l}.}
\label{fig:full}
\end{center}
\end{figure}

\clearpage

\subsection{Reduced model}
Implemented in {\tt reduced.m}

Normal reduced model including EMG. Equations are derived by substituting $L = 1$ into the full model except in $(1-G_{srs}L^2)$.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.4\textwidth]{reduced}
\caption{Reduced model schematic. A factor of $L$ is included for each connection marked with a red dot.}
\label{fig:full}
\end{center}
\end{figure}

Key parameters are $X$, $Y$, $Z$. Stability is given using the dispersion relation
\begin{align}
	\left[ \left(1-\frac{i\omega}{\gamma}\right)^2 - X + k^2r_e^2 \right] \left( 1+Z' L^2 \right) - Y\left( 1+Z' \right) e^{i\omega t_0} &= 0, %JAMES DISPERSION RELATION
\end{align}

with $k=0$. The power spectrum is given by

\begin{align}
	q^2r_e^2 &= \left( 1 - \frac{i \omega}{\gamma}\right)^2  - X - \frac{Y(1+Z')}{1+Z'L^2}e^{i\omega t_0},\\[14pt]
	P(\omega) &= \sum_{m = -\infty}^{\infty}\sum_{n = -\infty}^{\infty} P_0 \left| \frac{1}{(1+Z'L^2)(k^2r_e^2+q^2r_e^2)}\right|^2,\\[14pt]
	Z' &= Z \frac{(\alpha+\beta)^2}{\alpha\beta},
\end{align}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced}
\caption{Example fit with reduced model. }
\label{fig:full}
\end{center}
\end{figure}

\clearpage

\subsection{Reduced model with $L$ in prefactor}
Implemented in {\tt reduced\_ln.m}. This model is the same as \texttt{reduced} except that the factor of $L^2$ is preserved in the prefactor. The power spectrum is then given by:
\begin{align}
	P(\omega) &= \sum_{m = -\infty}^{\infty}\sum_{n = -\infty}^{\infty} P_0 \left| \frac{L^2}{(1+Z'L^2)(k^2r_e^2+q^2r_e^2)}\right|^2,\\[14pt]
\end{align}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced_ln}
\caption{Example fit with reduced model.}
\label{fig:full}
\end{center}
\end{figure}

The primary effect of this modification is to greatly decrease the background power at high frequencies. The harmonic peaks at high frequencies are suppressed a little, but not quite enough. The rolloff at high frequency can be partially fixed with the EMG component but note that the power diverges even for the beta peak, when the EMG component should not have a significant effect.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced_ln_emg_low}
\caption{Example of \texttt{reduced\_ln} with medium levels of EMG correction. The distribution shows larger values of $A_{EMG}$ may be favored. }
\label{fig:full}
\end{center}
\end{figure}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced_ln_emg_high}
\caption{Example of \texttt{reduced\_ln} with high levels of EMG correction. The distribution shows an EMG peak, yet the spectrum is still a poor fit near 20\,Hz. }
\label{fig:full}
\end{center}
\end{figure}

\clearpage

\subsection{Reduced model with $L$ in the $Y$ term}
Implemented in {\tt reduced\_ly.m}. Includes EMG. Equations are derived by substituting $L = 1$ into the full model except in $(1-G_{srs}L^2)$.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.4\textwidth]{reduced_ly}
\caption{Reduced model schematic. A factor of $L$ is included for each connection marked with a red dot.}
\label{fig:full}
\end{center}
\end{figure}

Key parameters are $X$, $Y$, $Z$. Stability is given using the dispersion relation
\begin{align}
	\left[ \left(1-\frac{i\omega}{\gamma}\right)^2 - X + k^2r_e^2 \right] \left( 1+Z' L^2 \right) - L^2Y\left( 1+Z' \right) e^{i\omega t_0} &= 0, %JAMES DISPERSION RELATION
\end{align}

with $k=0$. The power spectrum is given by

\begin{align}
	q^2r_e^2 &= \left( 1 - \frac{i \omega}{\gamma}\right)^2  - X - \frac{L^2Y(1+Z')}{1+Z'L^2}e^{i\omega t_0},\\[14pt]
	P(\omega) &= \sum_{m = -\infty}^{\infty}\sum_{n = -\infty}^{\infty} P_0 \left| \frac{L^2}{(1+Z'L^2)(k^2r_e^2+q^2r_e^2)}\right|^2,\\[14pt]
	Z' &= Z \frac{(\alpha+\beta)^2}{\alpha\beta},
\end{align}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced_ly}
\caption{Example fit for \texttt{reduced\_ly}. Note that the stability criteria are now different so the tent surface corresponds to a different model.}
\label{fig:full}
\end{center}
\end{figure}

This model shows good suppression of features at high frequencies but the EMG component is unable to correct the rolloff at high frequencies.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced_ly_emg_high}
\caption{Example of \texttt{reduced\_ly} with high levels of EMG correction. Again, the fit at very high frequencies is promoted in favour of discrepencies at lower frequencies.}
\label{fig:full}
\end{center}
\end{figure}

\clearpage

\subsection{Reduced LY with no $L$ prefactor}
Implemented in {\tt reduced\_ly\_no\_n.m}. Includes EMG. The same as \texttt{reduced\_ly} except the $L^2$ is replaced by $1$, the same as in the reduced model i.e.

\begin{align}
	P(\omega) &= \sum_{m = -\infty}^{\infty}\sum_{n = -\infty}^{\infty} P_0 \left| \frac{1}{(1+Z'L^2)(k^2r_e^2+q^2r_e^2)}\right|^2,\\[14pt]
\end{align}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.8\textwidth]{example_reduced_ly_no_n}
\caption{Example fit \texttt{reduced\_ly\_no\_n}. No EMG component was included (multiplied by 0 internally)}
\label{fig:full}
\end{center}
\end{figure}

This idea didn't seem to work very well.

\clearpage
\section{Spatial variations}
This program uses a different approach to calculate the power spectrum based on matrix inversion. The model is the same as the full model up to this point:

\begin{align}
\phi_e \left(D_{e}(1-G_{ei}L) - LG_{ee} - \frac{\left[ L^2G_{ese}  + L^3 G_{erse}\right]e^{i\omega t_0}}{1 - L^2 G_{srs}} \right) &=  \frac{L^2G_{esn}e^{i\omega t_0 /2}\phi_n}{1 - L^2 G_{srs}}\\
\label{spec_spatial}
\phi_e \left(D_{e} - LG_{ee} - \frac{\left[ L^2G_{ese}  + L^3 G_{erse}\right]e^{i\omega t_0}}{(1-G_{ei}L)(1 - L^2 G_{srs})} \right) &=  \frac{L^2G_{esn}e^{i\omega t_0 /2}\phi_n}{(1-G_{ei}L)(1 - L^2 G_{srs})}
\end{align}
where some of the parameters ($G_{ab}$, $t_0$, $L$, others) may depend on position. This equation is of the form
\begin{align}
A(\mathbf{r},\omega) \phi_e(\mathbf{r},\omega) = B(\mathbf{r},\omega) \phi_n(\mathbf{r},\omega)
\end{align}

Taking the Fourier transform of both sides results in a convolution
\begin{align}
A(\mathbf{k},\omega) \ast \phi_e(\mathbf{k},\omega) = B(\mathbf{k},\omega) \ast \phi_n(\mathbf{k},\omega)
\end{align}

Now, the critical step is that any discrete convolution can be written as an equivalent matrix multiplication. This is a nontrivial result. The convolution matrix consists of entries from the original matrix, but rearranged appropriately. The right side of the convolution is reshaped into a column vector, and then the left side of the convolution becomes a square matrix. Each row corresponds to one of the spatial modes. In 1D, the convolution matrix is a circulant matrix. In 2D, the matrix is block circulant. Obtaining the appropriate convolution matrix can be achieved by a very neat result that relates the convolution matrix to the DFT of the identity matrix. In 1D, the arrangement of the convolution matrix can be obtained with

\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily]
Q = fft(eye(n_modes))
Lm = fft(1:n_modes)
T = round(Q'*diag(Lm)*Q/numel(Lm))
\end{lstlisting}

In 2D, the convolution matrix can be obtained using the Kronecker product
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily]
Qm = fft(eye(n_modes))
Q = kron(Qm,Qm)
Lm = fft2(reshape(1:n_modes^2,[n_modes,n_modes]))
T = round(Q'*diag(Lm(:))*Q/numel(Lm))
\end{lstlisting}

The output from these commands corresponds to the indexes of the original matrix being convolved. Thus the equivalence between convolution and matrix multiplication is established via a command like:

\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily]
conv(A,phie)
A(T)*phie(:)
\end{lstlisting}

It is most convenient in this case to keep the traditional ordering of the frequency components in the DFT - that is, prior to performing an fftshift. The first entry in the matrices thus corresponds to the zero frequency component, rather than the middle entry.

Finally, the spatial operator in $A$ applies only to spatially uniform modes. Therefore, its' Fourier transform $k^2$ is added to the diagonal of the convolution matrix after it has been generated. It is omitted from $D_e$ when computing $A(\mathbf{r},w)$. \emph{Note that this is the reason why the form \eqref{spec_spatial} is preferred - there is no multiplicative factor on $D_e$ which means that $k^2$ can just be directly added, remembering that the other terms can have more complex spatial and frequency dependence.}

Having obtained the convolution matrices $A_c$ and $B_c$, $\phi_e$ can be obtained by matrix inversion

\begin{align}
\phi_e(\mathbf{k},\omega) = A_c^{-1}B_c\phi_n(\mathbf{k},\omega)
\end{align}

Noting that both $\phi_e$ and $\phi_n$ are arranged as column matrices. They can be reshaped into the original 2D matrices afterwards.

The spectrum $P(\mathbf{k},\omega)$ can be obtained directly from here. Obtaining $P(\mathbf{r},\omega)$ is somewhat more involved, because it requires averaging over the random phases of the white noise input. It can be obtained by assigning $M = A_c^{-1}B_c$. Then

\begin{align}
P(\mathbf{r},\omega) = |\phi_n(\omega)^2| \sum_{\mu,\nu} \exp[i(\mathbf{k}_\mu - \mathbf{k}_\nu) \cdot r] MM\dag
\end{align}

where $\mu$ and $\nu$ range over each of the spatial modes present in the calculation. This formula bears a resemblance to a DFT, but it is not identical. One advantage of this formulation is that it can be computed for an arbitrary set of positions.

\subsection{Spatial variations in  $t_0$ front-to-back}
Implemented in {\tt spatial\_t0\_2d.m}.

This calculation is essentially the same as the uniform full model, except that $t_0$ is allowed to vary with an amplitude up to $\pm t_0$ along the $Y$-direction of the model. The spatial variation is limited to one spatial wavelength (i.e. one complete oscillation).

\end{document}


\documentclass{article}

\usepackage{blindtext}
\usepackage{amsmath}
\usepackage{ragged2e}

\newcommand{\ir}[1]{_{\mathrm{#1}}}

\begin{document}

\section{Here it works!}

\emph{\blindtext}

\begin{align}
G(s) = \frac{i(s)}{u(s)} = \frac{ 1 }{R_{42} + s L_{1984} } \:.
\end{align}

\section{Here everything gets screwed up!}

\begin{align}
G(s) = \frac{i(s)}{u(s)} = \frac{ 1 }{R\ir{Cu} + s L\ir{eff}(s)} \:.
\end{align}

\section{Here it doesn't work}

\emph{\blindtext}

\end{document}


\section{Mechanics}
  We now trace $Alice$'s steps from joining the network to successfully completing a purchase. Suppose initially all her
  coins, say $10\bitcoin$, are under her exclusive control.

  Two trustworthy friends, $Bob$ and $Charlie$, persuade her to try out Trust Is Risk. She installs the Trust Is Risk wallet
  and migrates the $10\bitcoin$ from her regular wallet, entrusting $2\bitcoin$ to $Bob$ and $5\bitcoin$ to $Charlie$. She now
  exclusively controls $3\bitcoin$. She is risking $7\bitcoin$ to which she has full but not exclusive access in exchange for
  being part of the network.

  A few days later, she discovers an online shoes shop owned by $Dean$, also a member of Trust Is Risk. She finds a nice pair
  of shoes that costs $1\bitcoin$ and checks $Dean$'s trustworthiness through her new wallet. Suppose $Dean$ is deemed
  trustworthy up to $5\bitcoin$. Since $1\bitcoin < 5\bitcoin$, she confidently proceeds to purchase the shoes with her new
  wallet.

  She can then see in her wallet that her exclusive coins have remained $3\bitcoin$, the coins entrusted to $Charlie$ have
  been reduced to $4\bitcoin$ and $Dean$ is entrusted $1\bitcoin$, equal to the value of the shoes. Also, her purchase is
  marked as pending. If she checks her trust towards $Dean$, it still is $5\bitcoin$. Under the hood, her wallet redistributed
  her entrusted coins in a way that ensures $Dean$ is directly entrusted with coins equal to the value of the purchased item
  and that her reported trust towards him has remained invariant.

  Eventually all goes well and the shoes reach $Alice$. $Dean$ chooses to redeem $Alice$'s entrusted coins, so her wallet
  does not show any coins entrusted to $Dean$. Through her wallet, she marks the purchase as successful. This lets the system
  replenish the reduced trust to $Bob$ and $Charlie$, setting the entrusted coins to $2\bitcoin$ and $5\bitcoin$ respectively
  once again. $Alice$ now exclusively owns $2\bitcoin$. Thus, she can now use a total of $9\bitcoin$, which is expected,
  since she had to pay $1\bitcoin$ for the shoes.