%% LaTeX2e Template by Stephen Iota (https://stepheniota.com/)
%% last updated: May 2019
\documentclass[11pt]{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm,physics}
\usepackage{mathtools} % for boxed answers in align environments
\usepackage{cancel}
\usepackage{graphicx}
%\usepackage[shortlabels]{enumitem} % change labels in enum/item environments
\usepackage[labelfont=bf,font=small]{caption}
\usepackage[dvipsnames]{xcolor}
%\usepackage[big]{titlesec} % [small,medium,big]
\usepackage{fancyhdr} %http://tug.ctan.org/tex-archive/macros/latex/contrib/fancyhdr/fancyhdr.pdf
%\usepackage[noadjust]{cite}
%\usepackage{lipsum}
\usepackage[
	colorlinks=true,
	citecolor=NavyBlue!90!black,
	linkcolor=green!50!black,
	urlcolor=RoyalBlue,
	hypertexnames=false]{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% My commands & environments %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\email}[1]{\texttt{\href{mailto:#1}{#1}}}
\renewcommand{\d}[1]{\ensuremath{\operatorname{d}\!{#1}}}
%\newcommange{\ave}[1]{\ensuremath{\langle {#1} \rangle}}

%\newenvironment{question}[0]{
%\bigbreak
%\noindent
%}
\theoremstyle{definition}
\newtheorem{question}{Part}[section]
\newtheorem*{solution}{Solution}
%\newenvironment{solution}[0]{
%\bigbreak
%\noindent
%\textbf{Solution:}
%}

%%%%%%%%%%%%%%%%%%
%% Front Matter %%
%%%%%%%%%%%%%%%%%%
\pagestyle{fancy}
\fancyhead[L]{\footnotesize{\leftmark}}
\fancyhead[C]{}
\fancyhead[R]{\footnotesize{ IOTA \textbf{\thepage}}}
\fancyfoot[L,C,R]{}
\thispagestyle{plain} % no hf on first page
%\pagenumbering{gobble} % no page numbers
%\setcounter{section}{-1}
\graphicspath{{figures/}} % set directory for figures
\numberwithin{equation}{section}
\numberwithin{figure}{section}

%%%%%%%%%%%%%
%%% Title %%%
%%%%%%%%%%%%%
\begin{document}

\begin{center}
{\LARGE \textsc{Statistical Mechanics}: \textbf{Final Exam}}
\end{center}
\bigbreak

%%%%%%%%%%
%% INFO %%
%%%%%%%%%%
\begin{tabular}{rl}
\textsc{Name}:&		Stephen Iota (\email{siota001@ucr.edu})
\\
\textsc{Course}:&		Physics 133 (Spring 2019), Prof.~Kuhlman
\\
\textsc{Date}:&		\today
\end{tabular}
\bigbreak

%%%%%%%%%%%%%%
%% PROBLEMS %%
%%%%%%%%%%%%%%

\noindent
Sethna problems 2.1, 3.11, 5.2, 5.10, 7.3 and \textbf{An unlikely event}. Final answers are \boxed{\text{boxed}}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% RANDOM WALKS IN GRADE SPACE %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Random walks in grade space}
Consider a multiple-choice exam with ten questions of ten points each. Each problem on the exam is equally difficult. Students score a mean of $70$ on the exam. Assume all students are identical, and each question is answered at random with a probability of $0.7$ of being correct.

%%%%%%%%%%%%
%% PART A %%
%%%%%%%%%%%%
\begin{question}
\textbf{(a)}~What is the expected mean and standard deviation for the exam?
\end{question}

\begin{solution}
Let $p_+ = 0.7$ be the probability of a correct answer and $1 - p_+ = p_-$ be the probability of an incorrect answer; let $s \in \mathbb{S}$ be an exam score of a student in the set $\mathbb{S}$ of all possible student scores, and let $N$ be the number of questions on the exam. The mean score $\expval{s}$ is given by
\begin{equation}
\expval{s_N} = \sum_{n = 0}^N \binom{N}{n} \ (N-n)p_-\cdot(N-n)p_+\cdot10n \ \text{pts}
\end{equation}
and the rms score $\expval{s^2}$is
\begin{equation}
\expval{s_N^2} = \sum_{n = 0}^N \binom{N}{n} \ (N-n)p_-\cdot(N-n)p_+\cdot10n \ \text{pts}^2.
\end{equation}
Standard deviation $\sigma$ is related to the mean and rms by
\begin{equation}
\sigma = \sqrt{\expval{s^2} - \expval{s}^2}.
\end{equation}

First lets consider $N = 1$ questions on the exam. The mean, rms and std dev in this case are
\begin{align}
\expval{s_1} &= p_+(10 \ \text{pts}) = 7 \ \text{pts}
\\
\expval{s_1^2} &= p_+(10 \ \text{pts})^2 = 70 \ \text{pts}
\\
\sigma &= \sqrt{70 - 7^2} = \sqrt{21} \ \text{pts}
\end{align}
We can use the properties of random walks to see the evolution of these parameters as the number of questions on the exam $N$ increases. The mean score $\expval{s_N}$ will be $7N$ pts. The std dev scales by $\sqrt{N}$. This means that $\sigma_N = \sqrt{21N}$ pts.

For an exam of 10 questions, we find a mean of 10 pts and std dev of $\sqrt{210}$.
\begin{align}
\Aboxed{\expval{s_{10}} &= 10 \ \text{pts}}
\\
\Aboxed{\expval{s_{10}^2} &= \sqrt{210} \ \text{pts} \approx 14.5 \ \text{pts}}
\end{align}
\end{solution}

\newpage
%%%%%%%%%%%%
%% PART B %%
%%%%%%%%%%%%
\begin{question}
\textbf{(b)}~A typical exam with mean 70 might have an observed std dev of about 15. What physical interpretation do you make of the ratio of the random standard deviation and the observed one?
\end{question}

\begin{solution}
For a completely random model with 10 questions, the mean score is $\expval{s} = 50$ pts and the std dev is $\sigma_\text{random} = \sqrt{250}$.
\begin{equation}
\frac{\sigma}{\sigma_\text{random}} = \frac{\sqrt{210}}{\sqrt{250}} \approx \frac{14.5}{15.8} \approx 0.92
\end{equation}
Students in our model have a smaller spread of scores than in a completely random model. In our model, we made our students ``smarter.'' In a real exam setting, students rarely randomly guess on all questions. They have a higher probability of getting each question correct, which is why our model is slightly more accurate than the random model.
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%
%% Maxwell Relations %%
%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Maxwell relations}

Consider the microcanonical formula for the equilibrium energy $E(S,V,N)$ of some general system. One knows that the second derivatives of energy are symmetric; at fixed $N$, we get the same answer whichever order we take partial derivatives with respect to $S$ and $V$.

\begin{question}
\textbf{(a)} Use this to show the Maxwell relation
\begin{equation}
\pdv{T}{V}\eval_{S,N} = - \pdv{P}{S}\eval_{V,N}.\label{eq:2.0}
\end{equation}
Generate two other similar formul{\ae} by taking other second partial derivatives of $E$.
\end{question}

\begin{solution}
Consider the fundamental thermodynamic relation
\begin{equation}
\d{E} = T\d{S} - P\d{V} + \mu\d{N}.
\end{equation}
Keeping $N$ constant, take the following derivatives.
\begin{align}
\pdv{E}{V}\eval_{S,N} &= -P
\\
\pdv{S}\eval_{V,N} \pdv{E}{V}\eval_{S,N} &= - \pdv{P}{S}\eval_{V,N}\label{eq:2.1}
\end{align}
Since the second derivative of energy is symmetric, we can switch the order of integration in the second step.
\begin{align}
\pdv{V}\eval_{S,N} \pdv{E}{S}\eval_{V,N} &= \pdv{T}{S}\eval_{V,N}\label{eq:2.2}
\end{align}
From eq.~\eqref{eq:2.1} and \eqref{eq:2.2}, it follows that
\begin{equation}
\boxed{\pdv{T}{V}\eval_{S,N} = - \pdv{P}{S}\eval_{V,N}.}
\end{equation}

Using the same recipe, we can construct more Maxwell relations. Another relationship is given by taking partials of energy with respect to volume and number of particles.
\begin{align}
\pdv{E}{V}\eval_{S,N} &= -P
\\
\pdv{N}\eval_{S,V} \pdv{E}{V}\eval_{S,N} &= -\pdv{P}{N}\eval_{S,V}
\\
\pdv{V}\eval_{S,N} \pdv{E}{N}\eval_{S,V} &= \pdv{\mu}{N}\eval_{S,V}
\\
\Aboxed{\pdv{\mu}{V}\eval_{S,N} &= -\pdv{P}{N}\eval_{S,V}}
\end{align}
Another one by differentiating energy by entropy and number of particles.
\begin{align}
	\pdv{E}{S}\eval_{V,N} &= T
	\\
	\pdv{N}\eval_{S,V} \pdv{E}{S}\eval_{V,N} &= -\pdv{T}{N}\eval_{S,V}
	\\
	\pdv{S}\eval_{V,N} \pdv{E}{N}\eval_{S,V} &= \pdv{\mu}{S}\eval_{V,N}
	\\
	\Aboxed{\pdv{T}{N}\eval_{S,V} &= \pdv{\mu}{S}\eval_{V,N}}
\end{align}
\end{solution}

\begin{question}
\textbf{(b)}~Statistical mechanics check of the Maxwell relation. Using
\begin{equation}
S(E,V,N) = \frac{5}{2} Nk_B + Nk_B\log\Bigg[\frac{V}{Nh^3}\bigg( \frac{4\pi m E}{3N} \bigg)^{3/2}\Bigg]\label{eq:2b1}
\end{equation}
derive formul{\ae} for $E(S,V,N)$, $T(S,V,N) = (\pdv*{E}{S})|_{V,N}$, and $P(S,V,N) = -(\pdv*{E}{V})|_{S,N}$ for the ideal gas. Show explicitly that eq.~\eqref{eq:2.0} is satisfied.
\end{question}

\begin{solution}
To find $E(S,V,N)$, rearrange eq.~\eqref{eq:2b1}.
\begin{align}
\log\Bigg[\frac{V}{Nh^3}\bigg( \frac{4\pi m E}{3N} \bigg)^{3/2}\Bigg] &= \frac{S}{Nk_B} - \frac{5}{2}
\\
\frac{V}{Nh^3}\bigg( \frac{4\pi m E}{3N} \bigg)^{3/2} &= \exp(\frac{S}{Nk_B} - \frac{5}{2})
\\
\bigg( \frac{4\pi m E}{3N} \bigg)^{3/2} &= \frac{Nh^3}{V} \exp(\frac{S}{Nk_B} - \frac{5}{2})
\\
\frac{4\pi m E}{3N} &= \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3})
\\
\Aboxed{E &= \frac{3N}{4\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3})}\label{eq:2b2}
\end{align}

To find an expression for $T(S,V,N)$, take the derivative of eq.~\eqref{eq:2b2} with respect to $S$, keeping $V,N$ constant.
\begin{align}
T(S,V,N) &= \pdv{E}{S}\eval_{V,N}
\end{align}
\begin{align}
\begin{aligned}
\pdv{E}{S} &= \pdv{S} \frac{3N}{4\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \eval_{V,N}
\\
&= \frac{3N}{4\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \pdv{S} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \eval_{V,N}
\\
&= \frac{3N}{4\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \pdv{S} \frac{2}{3}\frac{S}{Nk_B} \eval_{V,N}
\\
&= \frac{1}{2k_B\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3})
\end{aligned}
\end{align}
\begin{align}
\Aboxed{T(S,V,N) &= \frac{1}{2k_B\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3})}\label{eq:2bT}
\end{align}

Next, let's find an expression for $P(S,V,N)$.
\begin{equation}
P(S,V,N) = -\pdv{E}{V}\eval_{S,N}
\end{equation}
\begin{align}
	\begin{aligned}
\pdv{E}{V}\eval_{S,N} &= - \pdv{V} \frac{3N}{4\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \eval_{S,N}
\\
&= - \frac{3N}{4\pi m} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \pdv{V} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \eval_{S,N}
\\
&= \frac{1}{2}\frac{N^2h^3}{\pi m V^2} \Big( \frac{V}{Nh^3} \Big)^{1/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3})
\end{aligned}
\end{align}
\begin{equation}
\boxed{P(S,V,N) = \frac{1}{2}\frac{N^2h^3}{\pi m V^2} \Big( \frac{V}{Nh^3} \Big)^{1/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3})}\label{eq:2bP}
\end{equation}

Now, we can show that eq.~\eqref{eq:2.0} is valid. First take the derivative of eq.~\eqref{eq:2bT} with respect to volume.
\begin{equation}
\begin{aligned}
\pdv{T}{V}\eval_{S,N} &= \pdv{V} \frac{1}{2k_B\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \eval_{S,N}
\\
&= \frac{1}{2k_B\pi m} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \pdv{V} \Big( \frac{Nh^3}{V} \Big) ^{2/3}
\\
&= - \frac{1}{3} \frac{1}{k_B\pi m} \Big(\frac{V}{Nh^3} \Big)^{1/3} \Big( \frac{Nh^3}{V^2} \Big) \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \label{eq:2c}
\end{aligned}
\end{equation}
Now, take the derivative of eq.~\eqref{eq:2bT} with respect to $S$.
\begin{equation}
\begin{aligned}
\pdv{P}{S}\eval_{V,N} &= \pdv{S} \frac{1}{2k_B\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \eval_{V,N}
\\
&= \frac{1}{2k_B\pi m} \Big( \frac{Nh^3}{V} \Big) ^{2/3}  \pdv{S} \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \eval_{V,N}
\\
&= \frac{1}{3} \frac{Nh^3}{\pi m V^2k_B} \Big( \frac{V}{Nh^3} \Big)^{1/3}  \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3})
\\
&= \frac{1}{3} \frac{1}{k_B\pi m} \Big(\frac{V}{Nh^3} \Big)^{1/3} \Big( \frac{Nh^3}{V^2} \Big) \exp(\frac{2}{3}\frac{S}{Nk_B} - \frac{5}{3}) \label{eq:2d}
\end{aligned}
\end{equation}

We find that eq.~\eqref{eq:2c} is equal to negative eq.~\eqref{eq:2d}, thus proving the Maxwell relation eq.~\eqref{eq:2.0}. $\qedhere$
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Burning information and Maxwellian demons %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Burning information and Maxwellian demons}

\begin{figure}[p]
\centering
\includegraphics[width=0.4\linewidth]{Final_Fig1}
\caption{Minimalist digital memory tape.\label{fig:tape}}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=0.4\linewidth]{Final_Fig2}
\caption{Expanding piston.\label{fig:piston}}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=0.4\linewidth]{Final_Fig3}
\caption{Information-burning engine.\label{fig:engine}}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=0.4\linewidth]{Final_Fig4}
\caption{Pistons resetting a \textit{known} bit for free.\label{fig:zero}}
\end{figure}

Is there a minimum energy cost for taking a measurement? Let's try to find out. We start by addressing the connection between information entropy and thermodynamic entropy. Can we burn information as fuel?

Consider a really frugal digital memory tape, with one atom used to store each bit (fig~\ref{fig:tape}). The tape is a series of boxes, with each box containing one ideal gas atom. The box is split into two equal pieces by a removable central partition. If the atom is in the top half of the box, the tape reads one; else the tape reads zero. The side walls are frictionless pistons that may be used to push the atom around.

\emph{If} we know the atom position in the $n$th box, we can move the other side wall in, remove the partition, and gradually retract the piston to its original position (fig~\ref{fig:piston})--destroying our information about where the atom is, but extracting useful work.

\begin{question}
\textbf{(a)~Burning the information.} Assuming the gas expands at a constant temperature $T$, how much work $\int P\d{V}$ is done by the atom as the piston retracts?
\end{question}

\begin{solution}
This is an isothermal process; $T$ and $N$ are constant. Work in thermodynamic processes is given by $\int P\d{V}$, where $P = Nk_BT/V$. Before the piston retracts, $P_iV_i$ is equal to $Nk_BT$. We make this substitution in the integral. Finally we integrate to find the magnitude of the result. The final answer is \emph{negative}; work is being done \emph{by} the environment \emph{on} the atom and gas.
\begin{align}
\begin{aligned}
W& = - \int_{V_i}^{V_f} P \d{V}
= - P_iV_i \int_{V_i}^{V_f} \frac{\d{V}}{V}
\\
& = \boxed{P_iV_i \log \! \Bigg[ \frac{V_i}{V_f} \Bigg]}\label{eq:work}
\end{aligned}
\end{align}
\end{solution}

\newpage
\begin{question}
\textbf{(b)~Rewriting a bit.} Give a sequence of particle insertion, partition removal, and adiabatic side-wall motions that will reversibly convert a bit zero into a bit one, with no net work done on the system.
\end{question}

\begin{solution}
~
\\
\begin{enumerate}
\item Place the atom into the bottom half of the partitioned box. Our bit is now in state zero.

\item Adiabatically snap the top piston down to the position of the partition. Atom still bit zero. There is no time for heat transfer; entropy gain zero. This motion costs us work $W_1$ equal to what we calculated in part \textbf{(a)} (eq.~\ref{eq:work}).

\item Remove the partition from underneath the top piston and place it just above the bottom piston. The atom is now in the ``top'' part of the box (bit one).

\item Adiabatically snap the bottom piston down the same distance the top piston traveled; the volume of the box is now back to the initial value. The atom is in bit one state. This work done by the atom $W_2$ is equal to $-W_1$, thus we have successfully changed to atom from bit zero to bit one with zero net work done on the system.
\end{enumerate}
\end{solution}

\begin{question}
\textbf{(c)~Forgetting a bit.} Suppose the atom location in the $n$th box is initially known. What is the change in entropy, if the partition is removed and the available volume doubles? Give both the thermodynamic entropy and the information entropy
\end{question}

\begin{solution}
\textit{Thermodynamic entropy.} If we define thermodynamic entropy change to be the ratio of heat flow to temperature, we can argue $\Delta S_\text{thermo} = 0$ if we double the volume by adiabatically drawing the pistons back. In addition, it is difficult to define temperature for a single atom. However, we can still think of $\Delta S_\text{thermo}$ as entropy change of mixing.

We can write down the entropy of mixing as
\begin{align}
S& = k_B \log \Omega \label{eq:S},
\end{align}
where $\Omega$ is the configurational energy-shell volume. For $N$ atoms in a box, the number of configurations in a volume $V$ is given by $V^N/N!$. Thus for our atom in a box, $\Omega_i = V$ and $\Omega_f = 2\Omega_i$
\begin{align}
\Aboxed{\Delta S_\text{thermo} & = S_f - S_i = k_B \log \frac{\Omega_f}{\Omega_i} = k_B \log 2} \label{eq:DSthermo}
\end{align}

\textit{Information entropy.} We can write down information entropy generally as
\begin{align}
S_\text{info} & = - k_S \sum_k p_k \log p_k = - \sum_k p_k \log_2 p_k\label{eq:Sinfo},
\end{align}
where $p_k$ is the probability of each possible state $k$ of a bit. Before the volume is doubled, we already know the position of the atom in the box. Suppose we know it is in a bit zero state. The initial probability of the atom being in bit zero state is one; probability of bit one state is zero. Once we remove the partition and double the volume, we lose information about the atom's state. Each state becomes equally probable. We use this change in probabilities to write down $\Delta S_\text{info}$.\footnote{eqs.~\eqref{eq:DSthermo} \& \eqref{eq:DSinfo} differ by nothing more than a normalization factor. We write thermodynamic entropy in units of energy per temperature, hence the Boltzmann factor $k_B$. When speaking about the entropy of bits, it makes more sense to present our results in base 2.}
\begin{align}
S_i &= -k_S \cdot(1) \cdot \log 1 = 0
\\
S_f &= -k_S \cdot(2) \cdot \frac{1}{2} \log \frac{1}{2} = \log_2{2}
\\
\Aboxed{
\Delta S_\text{info}  &= S_f - S_i = k_S \log{2} = 1}\label{eq:DSinfo}
\end{align}
\end{solution}

\begin{question}
\textbf{(d)~Demonic states.} What prevents a Maxwellian demon from using an atom in an unknown state to extract work? The demon must first measure which side of the box the atom is on; this measurement needs no energy expenditure. (i) After the bit has been burned (see fig~\ref{fig:engine}), is the demon in a known state?\footnote{The demon can be thought of as a two-state system (e.g.~another partitioned box). If the demon starts in a known state, one can copy the state of the box into the demon with no cost in entropy or energy, by adiabatically turning on an appropriate coupling. The demon has now measured the atom position, and can extract work from the pistons.}
(ii) What is its entropy? (iii) How much energy would it take to return the demon to its original state, at temperature T? (iv) Is the second law violated?
\end{question}

\begin{solution}
~\\
\begin{enumerate}
\item[(i)] \boxed{\text{Yes,}} after the bit has been burned, the demon is in a known state. The information from the bit has been copied to the demon prior to the bit's burning-for-work.

\item[(ii)] Burning the bit, however, costs us entropy. Initially the bit was in a low entropy state. The cost of ``forgetting a bit'' is given by $\boxed{k_B \log{2}}$ (see eqs.~\ref{eq:DSthermo} \& \ref{eq:DSinfo}).

\item[(iii)] It would take \boxed{0} energy to return the demon to its original state, at temperature $T$. Its original state was a random state. We can wipe a bit by removing the partition and moving both pistons adiabatically in the same direction for a certain distance (see fig~\ref{fig:zero}).

\item[(iv)] The Second Law of Thermodynamics states that the entropy of a closed system never decreases over time. This law is \boxed{\text{not violated.}} We can extract an unlimited amount of work with a demon by copying information from one bath to another. However, whenever we take energy from a bath, we have to pay for it with entropy. This is a fundamental truth about the Universe.
\end{enumerate}
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ENTROPY INCREASES: DIFFUSION %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Entropy increases: diffusion}

Entropy technically does not increase for a closed system. However, in most of the coarse-grained theories, entropy does increase; when we integrate out degrees of freedom, we provide a means for the information about the initial conditions to be destroyed.

Let $\rho(x,t)$ obey the one-dimensional diffusion equation $\pdv*{\rho}{t} = D\pdv*[2]{p}{x}$. Assume that the density $\rho$ and all its gradients die away rapidly at $x = \pm \infty$.\footnote{Also, assume $\pdv*[n]{\rho}{x} \log{\rho}$ goes to zero at $x = \pm \infty$, even though $\log{\rho}$ goes to $-\infty$.}

\begin{question}
Derive a formula for the time derivative of the entropy $S = -k_B \int \rho(x)\log{\rho(x)} \d{x}$ and show that it is strictly increasing in time.
\end{question}

\begin{solution}
We start by taking the time derivative of the definition of entropy.
\begin{align}
S &= -k_B \int_{\text{all space}} \rho \log{\rho} \d{x}
\\
\pdv{S}{t} &= -k_B \int \dot{\rho} \log{\rho} + \dot{\rho} \d{x}
\end{align}
Since $\rho$ obeys the diffusion equation, we can make the following substitution.
\begin{align}
\dot{S} &= -Dk_b \int \rho'' \log{\rho} + \rho'' \d{x}
\\
\dot{S} &= -Dk_B \bigg[\cancelto{0}{\rho'\eval_{-\infty}^{\infty}} + \int  \rho'' \log{\rho} \d{x}\bigg]
\\
\dot{S} &= -Dk_B \int \rho'' \log{\rho} \d{x}
\end{align}
The $\rho''$ term dies out when integrated over all space because we are assuming the probability density and all its gradients die out at the boundaries.

Next, we will try to get the remaining integrand into a form that is always positive. We will use the \textsc{di} method for integration by parts.

\begin{center}
\begin{tabular}{lrl}
		& 	D		& 	I
\\
+		&		$\log{\rho}$		&		$\rho''$
\\
-		&		$\frac{1}{\rho}\rho'$		&		$\rho'$
\end{tabular}
\end{center}
\begin{align}
\dot{S} &= -Dk_B \int \rho'' \log{\rho} \d{x} = -Dk_B \bigg[ \cancelto{0}{\rho'\log{\rho}\eval_{-\infty}^{\infty}} - \int \frac{(\rho')^2}{\rho} \d{x}    \bigg]
\end{align}
After integrating by parts, our first term dies again because of our initial assumption regarding the probability density (see footnote). We are left with the following integrand.
\begin{equation}
\dot{S} = Dk_B \int \frac{(\rho')^2}{\rho} \d{x}\label{eq:4.0}
\end{equation}
We cannot evaluate this integral, but we don't need to. First, consider the $1/\rho$ term. $\rho(x,t) \in [0,1]$ is a probability density, it cannot physically be negative. Next, consider the $(\rho')^2$ term. The square of any number is always positive. So although we do not know what $\rho'$ looks like, we only care about its square. Therefore, we know that the integrand in eq.~\eqref{eq:4.0} will always be positive for all values of $x$ that we are integrating over.

We can confidently say that the time derivative of entropy is always increasing.
\begin{align}
\Aboxed{
\pdv{S}{t} &\geq 0}
\end{align}
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PHASE SPACE UNITS AND THE ZERO OF ENTROPY %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Phase-space units and the zero of entropy}

In classical mechanics, entropy goes to minus infinity at the temperature is lowered to zero. In quantum mechnics the entropy per particle goes to zero, because states are quantized and the ground state is the only one populated.

The classical phase-space shell volume $\Omega(E)\var E$ has units of ([momentum]$\cdot$[distance])$^{3N}$. We can make it unitless with Planck's constant $h^{3N}$; if we measure phase-space volume in units of $h$ per dimension, $\Omega(E)\var E$ will be dimensionless. Of course, the correct dimension could be a constant times $h$, like $\hbar \ldots$

\begin{question}
\textbf{(a)}~\textbf{Arbitrary zero of the classical entropy.} Show that the width of the energy shell $\var E$ in the definition of $\Omega(E)$ does not change the microcanonical entropy per particle
\begin{equation}
S/N = k_B\log{\Omega(E})/N\label{eq:5.0}
\end{equation}
in a large system. Show that the choice of units in phase space does change the classical entropy per particle.
\end{question}

\begin{solution}
The definition of classical phase-space is
\begin{equation}
\Omega(E) = \frac{1}{\var E}\int_{H < E < H+ \var E } \d{\mathbb{P}} \d{\mathbb{Q}}
\end{equation}
For our ensemble, we assume particles are non-interacting, so we do not have to integrate over configurations $\mathbb{Q}$. The integral over momenta $\mathbb{P}$ is equal to the difference in volumes of $3N$ dimensional spheres of radii $\sqrt{2m(E + \delta E)}$ and $\sqrt{2mE}$. When we take the logarithm of the different of phase-space volumes for each sphere, we find that the exponents are eliminated thanks to the nifty properties of logarithms, and the $\var E$'s cancel out.
\begin{align}
\Omega(E) &= \frac{1}{\var E} \frac{\pi^{3N/2}}{(3N/2)!} \Bigg[ (2m(E + \delta E))^{3N/2} -  (2mE)^{3N/2}\Bigg]
\\
S &= k_B \log(\frac{1}{\var E} \frac{\pi^{3N/2}}{(3N/2)!} \Bigg[ (2m(E + \delta E))^{3N/2} -  (2mE)^{3N/2}\Bigg])
\\
&= k_B \Bigg[ \log{\frac{\pi^{3N/2}}{(3N/2)!}} + 3N/2\log( 2m(E + \var E) - 2mE) - \log(\var E)   \Bigg]
\\
S/N &= k_B \Bigg[ \frac{1}{N} \log{\frac{\pi^{3N/2}}{(3N/2)!}} + 3/2\log{2m} + 3/2\log{\var E} -\log{\var E}    \Bigg]
\end{align}
The value of $1/2 \log{\var E}$ is very small, so in the end we have shown that $S/N$ is independent of $\var E$.

However, if we chose the normalize our phase-space volume with $h^{3N}$ or $\hbar^{3N}$, we would get a different result. Therefore, choice of units in phase-space does matter.
\end{solution}

\begin{question}
\textbf{(b)}~\textbf{Phase-space density of states for a  particle in a one-dimensional box.} Show, or note, that the quantum momentum-space density of states for a free quantum particle in a one-dimensional box of length $L$ with periodic boundary conditions is $L/h$. Draw a picture of the classical phase space of this box $(p,x)$, and draw a rectangle of length $L$ for each quantum eigenstate. Is the phase-space area per eigenstate equal to $h$, as assumed?
\end{question}

\begin{solution}
In quantum mechanics, energy states are quantized. The energy eigenstates for a 1-$D$ particle in a box are $E_n = \frac{n^2 h^2}{8m^2 L^2}$. Therefore, the momentum per eigenstate are $p_n = \frac{nL}{2L}$. Taking into account the periodic boundary conditions (accounts for the factor of 2), we find that the phase space volume is indeed $L/h$.
\begin{figure}[h!]
\centering
\includegraphics[width=.3\linewidth]{Final_Fig5}
\caption{Phase-space volume of a 1-D particle in a box.}
\end{figure}
The phase-space volume per eigenstate is indeed equal to $h$.
\end{solution}

\begin{question}
\textbf{(c)}~\textbf{Phase-space density of states for $N$ particles in a box.} Show that the density of states for $N$ free particles in a cubical box of volume $V$ with periodic boundary conditions is $V^N/h^{3N}$, and hence that the phase-space volume per state is $h^{3N}$.
\end{question}

\begin{solution}
The energy eigenstates are given by $E_{nx,ny,nz} = \frac{\hbar^2 \vb{k}^2}{2m}$ and we find that each 3D wave vector is given by
$k = \frac{n_x \pi}{L} + \frac{n_y \pi}{L} + \frac{n_z \pi}{L}$. Therefore the phase space volume for one particle is given by $V/h^3$. If we have $N$ particles in a box, each occupies a respective volume, so we take our answer and raise it to the power of $N$. The phase-space volume then turns out to be $V^N/h^{3N}$, and the volume per state is $h^{3N}$.
\end{solution}

\begin{question}
\textbf{(d)}~\textbf{Phase-space density of states for a harmonic oscillator.} Consider a harmonic oscillator with Hamiltonian $\mathcal{H} = p^2/2m + 1/2 mw^2q^2$. Draw a picture of the energy surface with energy $E$, and find the volume of phase space enclosed. What is the volume per energy state?
\end{question}

\begin{solution}
For a fixed energy, the phase-space volume turns out to be elliptical. The volume (or rather area) is given by $\pi p x$.

\begin{figure}[h!]
	\centering
	\includegraphics[width=.3\linewidth]{Final_Fig6}
	\caption{Phase-space volume of a quantum harmonic oscillator with energy $E$.}
\end{figure}

\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%
%% AN UNLIKELY EVENT %%
%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{An unlikely event}

On June 12, 2019 at 6:59 pm, you are exiting the elevator, walking toward \textsc{ms\&e} to hand in your final take home exam for Physics 133. Just a few moments before 6:59 pm, a small meteor falls from the sky and crashes into a physics textbook on the desk of a philosopher trying to understand the meaning of entropy. The meteor is burning at such a high temperature that it instantly incinerates the book and continues burning through floor after floor of the building until coming to rest near a water line adjacent to a 50 year-old water fountain. The pipe becomes super heated, compressing the water in the plumbing and causing a catastrophic failure of the building’s pipes. The compression shock shakes the foundation of the building and, just as you exit the elevator, a micro-earthquake rumbles across the entire campus. Your exam slips from your hands and falls like an autumn leaf right between the crack that separates the elevator from the third floor's surface. The exam can't be recovered. Assume that the observation of this event is extremely unlikely. How much information is contained in such an event?

\begin{question}
\textbf{(a)} From Shannon, we know that the entropy is in fact a measure of \textit{information}. Suppose that any event, including that described above, occurs with probability $p$. We can associate with the observation of this event a function that quantifies the amount of information, call it $I(p)$. Find the functional form of $I(p)$ such that the following conditions are met:
\begin{enumerate}
\item Information is \textit{non-negative}.
\item If two events occur independently so that their joint probability is the product of event 1 and event 2, then their information is \textit{additive}.
\item Information is a continuous function of the probability $p$.
\item There is \textit{no information} content to an event that is always observed.
\end{enumerate}
\end{question}

\begin{solution}
We need a function that satisfies the following four criteria.
\begin{enumerate}
\item Information is non-negative.
\begin{align}
I(p) & \geq 0
\end{align}

\item For two independent events $i$ and $j$ where the joint probability that they both occur is $p_i \cdot p_j$, the total information of the events is given by $I(p_i) + I(p_j)$.
\begin{align}
I(p_i) + I(p_j) & = I(p_i + p_j)
\end{align}

\item Information is a continuous function of the probability $p$. A function $I$ is continuous if and only if the limit
\begin{equation}
\lim_{p \to a} I(p) = I(a)
\end{equation}
exists.

\item If an event $i$ is always observed, that event contains no information such that
\begin{align}
\text{for an event $i$, if $p_i = 1$, then}\  I(p_i)& = 0.
\end{align}
\end{enumerate}
A simple function that has these four properties is the logarithm function.\footnote{Proofs left as an exercise for the reader. Or see \url{https://en.wikipedia.org/wiki/Logarithm}.} Let
\begin{align}
\Aboxed{I(p) & = - \log{p} = \log{\frac{1}{p}}}\label{eq:info}
\end{align}
where $p \in [0,1]$ is the probability of a given event occurring.
\end{solution}

\begin{question}
\textbf{(b)} Find the value of $I(p)$ for the event described above (i.e.~the probability of observing such an event approaches zero). How much information is contained in an event as unlikely as this one?
\end{question}

\begin{solution}
The chances of such an event happening are \textbf{unlikely}. Let the probability of the unlikely event occurring go to zero. The amount of information contained in the event is given by
\begin{equation}
\boxed{I(p) = \lim_{p \to 0}  \log\frac{1}{p} = \infty.}
\end{equation}
The amount of information contained in this event is infinite.
\end{solution}

\end{document}


% Copyright (c) Microsoft Corporation. All rights reserved.
% Licensed under the MIT License.

%!TEX program=xelatex
% This cheatsheet is based on the template
% provided at https://gist.github.com/alexander-yakushev/c773543bf9a957749f79.
\documentclass[10pt,english,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage[landscape]{geometry}
\usepackage{color,graphicx,overpic}
\usepackage{amsmath}

% Workaround for left brackets not rendering
% https://tex.stackexchange.com/questions/520847/left-matrix-bracket-not-rendered-properly

\newbox\matrixrepresentationright
\sbox\matrixrepresentationright{\parbox{\textwidth}{
  \footnotesize
  $\qquad \underrightarrow{n \text{ columns}} \\
  \rotatebox[origin=c]{270}{$\underrightarrow{m \text{ rows}}$}
  \begin{bmatrix}
    a_{0,0}   & \cdots & a_{0,n-1} \\
    \vdots    & \ddots & \vdots \\
    a_{m-1,0} & \cdots & a_{m-1,n-1}
  \end{bmatrix}$
}}

\newbox\vectorrepresentationright
\sbox\vectorrepresentationright{
  \footnotesize
  $\begin{bmatrix}
    a_0 \\ \vdots \\ a_{n-1}
  \end{bmatrix}$
}

\newbox\matrixadditionleft
\sbox\matrixadditionleft{\parbox{\textwidth}{
  \footnotesize
  Addition \linebreak
  $\begin{bmatrix}
    a & b \\
    c & d
    \end{bmatrix} +
  \begin{bmatrix}
    e & f \\
    g & h
  \end{bmatrix}$
}}

\newbox\matrixadditionright
\sbox\matrixadditionright{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix}
    a + e & b + f \\
    c + g & d + h
  \end{bmatrix} $
}}

\newbox\matrixscalarleft
\sbox\matrixscalarleft{\parbox{\textwidth}{
  \footnotesize
  Scalar product \newline
  $ a  \cdot \begin{bmatrix}
    b & c \\
    d & e
  \end{bmatrix} $
}}

\newbox\matrixscalarright
\sbox\matrixscalarright{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix}
    a \cdot b & a \cdot c \\
    a \cdot d & a \cdot e
  \end{bmatrix} $
}}

\newbox\matrixmultiplicationleft
\sbox\matrixmultiplicationleft{\parbox{\textwidth}{
  \footnotesize
  Matrix product \newline
  $ \begin{bmatrix}
    a & b & c \\
    d & e & f
  \end{bmatrix} \cdot
  \begin{bmatrix}
    x \\
    y \\
    z
  \end{bmatrix} $
}}

\newbox\matrixmultiplicationright
\sbox\matrixmultiplicationright{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix}
    a \cdot x + b \cdot y + c \cdot z \\
    d \cdot x + e \cdot y + f \cdot z
  \end{bmatrix} $
}}

\newbox\matrixtransposeleft
\sbox\matrixtransposeleft{\parbox{\textwidth}{
  \footnotesize
  Transpose \newline
  $ \begin{bmatrix}
    a & b & c\\
    d & e & f
  \end{bmatrix}^T $
}}

\newbox\matrixtransposeright
\sbox\matrixtransposeright{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix}
     a & d \\
    b & e \\
    c & f
  \end{bmatrix} $
}}

\newbox\matrixadjointleft
\sbox\matrixadjointleft{\parbox{\textwidth}{
  \footnotesize
  Adjoint \newline
  $ \begin{bmatrix}
    a & b & c \\
    d & e & f
  \end{bmatrix}^\dagger $
}}

\newbox\matrixadjointright
\sbox\matrixadjointright{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix}
     \overline{a} & \overline{e} \\
    \overline{b} & \overline{d} \\
     \overline{c} & \overline{f}
  \end{bmatrix} $
}}

\newbox\outerproductleft
\sbox\outerproductleft{\parbox{\textwidth}{
  \footnotesize
  Outer product \newline
  $ \begin{bmatrix}
    a \\
    b
  \end{bmatrix} $  and $
  \begin{bmatrix}
    x \\
    y \\
    z
    \end{bmatrix} $
}}

\newbox\innerproductleft
\sbox\innerproductleft{\parbox{\textwidth}{
  \footnotesize
  Inner product \newline
  $ \left\langle
    \begin{bmatrix}
      a \\
      b
    \end{bmatrix} ,
    \begin{bmatrix}
      c \\
      d
    \end{bmatrix}
  \right\rangle $
}}

\newbox\innerproductright
\sbox\innerproductright{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix}
    a \\
    b
  \end{bmatrix}^\dagger
  \begin{bmatrix}
    c \\
    d
  \end{bmatrix} =
  \begin{bmatrix}
    \overline{a} &  \overline{b}
  \end{bmatrix}
  \begin{bmatrix}
    c \\
    d
  \end{bmatrix} =
  \overline{a} c + \overline{b} d $
}}

\newbox\outerproductright
\sbox\outerproductright{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix}
    a \\
    b
  \end{bmatrix}
  \begin{bmatrix}
    x \\
    y \\
    z
  \end{bmatrix}^\dagger =
  \begin{bmatrix}
    a \\
    b
  \end{bmatrix}
  \begin{bmatrix}
    \overline{x} & \overline{y} & \overline{z}
  \end{bmatrix} = $ \newline
  $ = \begin{bmatrix}
        a \cdot \overline{x} & a \cdot \overline{y} & a \cdot \overline{z} \\
        b \cdot \overline{x} & b \cdot \overline{y} & b \cdot \overline{z} \\
      \end{bmatrix} $
}}

\newbox\xgate
\sbox\xgate{\parbox{\textwidth}{
  \footnotesize
   $ \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} $
}}

\newbox\ygate
\sbox\ygate{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} $
}}

\newbox\zgate
\sbox\zgate{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} $
}}

\newbox\igate
\sbox\igate{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} $
}}

\newbox\hgate
\sbox\hgate{\parbox{\textwidth}{
  \footnotesize
  $ \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} $
}}

\newbox\sgate
\sbox\sgate{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix} $
}}

\newbox\tgate
\sbox\tgate{\parbox{\textwidth}{
  \footnotesize
   $ \begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix} $
}}

\newbox\rxgate
\sbox\rxgate{\parbox{\textwidth}{
  \footnotesize
   $\begin{bmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} &  \cos\frac{\theta}{2} \end{bmatrix} $
}}

\newbox\rygate
\sbox\rygate{\parbox{\textwidth}{
  \footnotesize
   $ \begin{bmatrix} \cos\frac{\theta}{2}  & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix} $
}}

\newbox\rzgate
\sbox\rzgate{\parbox{\textwidth}{
  \footnotesize
  $ \begin{bmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{bmatrix} $
}}

\newbox\rgate
\sbox\rgate{\parbox{\textwidth}{
  \footnotesize
   $ \begin{bmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{bmatrix} $
}}

\newbox\cnotgate
\sbox\cnotgate{\parbox{\textwidth}{
  \footnotesize
   $ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \mathbf{0} & \mathbf{1} \\ 0 & 0 & \mathbf{1} & \mathbf{0} \end{bmatrix} $
}}

\newbox\swapgate
\sbox\swapgate{\parbox{\textwidth}{
  \footnotesize
   $ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \mathbf{0} & \mathbf{1} & 0 \\ 0 & \mathbf{1} & \mathbf{0} & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} $
}}

\newbox\controlledugate
\sbox\controlledugate{\parbox{\textwidth}{
  \footnotesize
  $\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & a_{0,0} & a_{0,1} \\ 0 & 0 & a_{1,0} & a_{1,1} \end{bmatrix} $
}}

\newbox\ccnotgate
\sbox\ccnotgate{\parbox{\textwidth}{
  \footnotesize
  $\begin{bmatrix}
    1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
    0 & 0 &0  & 0 & 0 & 1 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & \mathbf{0} & \mathbf{1} \\
    0 & 0 & 0 & 0 & 0 & 0 & \mathbf{1} & \mathbf{0}
\end{bmatrix}$
}}

\newbox\braketcx
\sbox\braketcx{\parbox{\textwidth}{
  \footnotesize
    $ |00\rangle\langle00| + |01\rangle\langle01| + |11\rangle\langle10| + |10\rangle\langle11| \newline \text{or} \newline |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes X $
}}

\newbox\braketcu
\sbox\braketcu{\parbox{\textwidth}{
  \footnotesize
    $  |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes U $
}}

\newbox\braketccx
\sbox\braketccx{\parbox{\textwidth}{
  \footnotesize
    $ |00\rangle\langle00| + |10\rangle\langle01| + a_{0,0}|10\rangle\langle10| + \newline a_{0,1}|10\rangle\langle11| + a_{1,0}|10\rangle\langle11| + a_{1,1}|11\rangle\langle11| \newline \text{Or} \newline |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes U $
}}

% https://tex.stackexchange.com/a/192067
\usepackage{fontawesome}
  \newfontfamily{\FA}{[FontAwesome.otf]}

% Known issue in {fontspec}: https://github.com/wspr/fontspec/issues/312#issuecomment-342125206
% Since {fontspec} is included only when compiling with XeTeX, we guard
% our fix accordingly.
\usepackage{ifxetex}
    \ifxetex
        \let\latinencoding\relax
        \usepackage{fontspec}
        \setmainfont{Segoe UI}
        \setmonofont{Consolas}
    \fi

% \usepackage[T1]{fontenc}
% \usepackage[bitstream-charter]{mathdesign}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{amsfonts}
\usepackage{array,booktabs}
\usepackage{textcomp}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{menukeys}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{environ}
\usepackage{braket}

\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm}

\pagestyle{empty} % Turn off header and footer

% \renewcommand\rmdefault{phv} % Arial
% \renewcommand\sfdefault{phv} % Arial

% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
  {-1ex plus -.5ex minus -.2ex}%
  {0.5ex plus .2ex}%x
  {\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
  {-1explus -.5ex minus -.2ex}%
  {0.5ex plus .2ex}%
  {\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
  {-1ex plus -.5ex minus -.2ex}%
  {1ex plus .2ex}%
  {\normalfont\small\bfseries}}
\makeatother

\setcounter{secnumdepth}{0} % Don't print section numbers
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}

\definecolor{TableHead}{rgb}{0.353, 0.329, 0.667}
\definecolor{TableRow}{rgb}{0.816, 0.812, 0.902}

% Turn off warnings about slightly overfull boxes
% https://tex.stackexchange.com/questions/50830/do-i-have-to-care-about-bad-boxes/50850

\hfuzz=10pt

\NewEnviron{keysref}[1]{
  % \begin{center}
  \smallskip
  \begin{tikzpicture}
      \rowcolors{1}{}{TableRow}

      \node (tbl) [inner sep=0pt] {
        \begin{tabular}{p{2.5cm}p{5.05cm}}
          \rowcolor{TableHead}
          \multicolumn{2}{l}{\normalsize\textbf{\color{white}{#1}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\
          \BODY
          \arrayrulecolor{TableHead}\specialrule{.17em}{0em}{.2em}
        \end{tabular}};
      \begin{pgfonlayer}{background}
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
      \end{pgfonlayer}
    \end{tikzpicture}
  % \end{center}
}

\NewEnviron{keysrefGates}[1]{
  % \begin{center}
  \smallskip
  \begin{tikzpicture}
      \rowcolors{1}{}{TableRow}

      \node (tbl) [inner sep=0pt] {
        \begin{tabular}{p{0.85cm}p{3.55cm}p{4.05cm}p{5.55cm}p{2.95cm}p{2.95cm}p{3.05cm}}
          \rowcolor{TableHead}
          \multicolumn{7}{l}{\normalsize\textbf{\color{white}{#1}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\
          \BODY
          \arrayrulecolor{TableHead}\specialrule{.17em}{0em}{.2em}
        \end{tabular}};
      \begin{pgfonlayer}{background}
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
      \end{pgfonlayer}
    \end{tikzpicture}
  % \end{center}
}

\NewEnviron{keysrefMultiGates}[1]{
  % \begin{center}
  \smallskip
  \begin{tikzpicture}
      \rowcolors{1}{}{TableRow}

      \node (tbl) [inner sep=0pt] {
        \begin{tabular}{p{1.55cm}p{3,75cm}p{5,35cm}p{6,05cm}p{2.95cm}p{3.75cm}}
          \rowcolor{TableHead}
          \multicolumn{6}{l}{\normalsize\textbf{\color{white}{#1}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\
          \BODY
          \arrayrulecolor{TableHead}\specialrule{.17em}{0em}{.2em}
        \end{tabular}};
      \begin{pgfonlayer}{background}
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
        \draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
        ($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
      \end{pgfonlayer}
    \end{tikzpicture}
  % \end{center}
}

% https://tex.stackexchange.com/a/102523
\newcommand{\forceindent}[1]{\leavevmode{\parindent=#1\indent}}

%% CUSTOM NOTATION %%

\newcommand{\qs}{Q\#}
\newcommand{\unixlike}{\hfill\faApple\faLinux}
\newcommand{\ctrllike}{\hfill\faWindows\faLinux}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\raggedright\

\Large{\qs~0.15 Language Quick Reference}

\footnotesize
\begin{multicols}{3}

  \begin{keysref}{Primitive Types}
    64-bit integers         & \texttt{Int} \\
    Double-precision \newline floats & \texttt{Double} \\
    Booleans                & \texttt{Bool}   \newline
                              e.g.: \texttt{true} or \texttt{false} \\
    Qubits                  & \texttt{Qubit}  \\
    Pauli basis             & \texttt{Pauli}  \newline
                              e.g.: \texttt{PauliI}, \texttt{PauliX}, \texttt{PauliY}, or \texttt{PauliZ} \\
    Measurement \newline results     & \texttt{Result} \newline
                              e.g.: \texttt{Zero} or \texttt{One} \\
    Sequences of \newline integers   & \texttt{Range}  \newline
                              e.g.: \texttt{1..10} or \texttt{5..-1..0} \\
    Strings                 & \texttt{String} \newline
                              e.g.: \texttt{"Hello Quantum!"} \\
    "Return no \newline information" type   & \texttt{Unit} \newline e.g.: \texttt{()} \\
  \end{keysref}

  \begin{keysref}{Derived Types}
    Arrays                  & \texttt{\emph{elementType}[]} \\
    Tuples                  & \texttt{(\emph{type0}, \emph{type1}, ...)} \newline
                              e.g.: \texttt{(Int, Qubit)} \\
    Functions               & \texttt{\emph{input} -> \emph{output}} \newline
                              e.g.: \texttt{ArcCos : (Double) -> Double} \\
    Operations              & \texttt{\emph{input} => \emph{output} is \emph{variants}} \newline
                              e.g.: \texttt{H : (Qubit => Unit is Adj)} \\
  \end{keysref}

  \begin{keysref}{User-Defined Types}
  Declare UDT with \newline anonymous items & \texttt{newtype \emph{Name} = (Type, Type);} \newline
                                     e.g.: \texttt{newtype \emph{Pair} = (Int, Int);} \\
  Define UDT literal               & \texttt{\emph{Name}(\emph{baseTupleLiteral})} \newline
                                     e.g.: \texttt{let origin = Pair(0, 0);} \\
  Unwrap operator \texttt{!} \newline (convert UDT to \newline underlying type)
                                   & \texttt{\emph{VarName}!} \newline
                                     e.g.: \texttt{let originTuple = origin!;} \newline \hphantom{..........}(now \texttt{originTuple = (0, 0)}) \\
  Declare UDT with \newline named items     & \texttt{newtype \emph{Name} =}   \newline \texttt{\hphantom{....}(\emph{Name1}: Type, \emph{Name2}: Type);} \newline
                                     e.g.: \texttt{newtype Complex =} \newline \texttt{\hphantom{....}(Re : Double, Im : Double);} \\
  Accessing named \newline items of UDTs    & \texttt{\emph{VarName}::\emph{ItemName}} \newline
                                     e.g.: \texttt{complexVariable::Re} \\
  Update-and-\newline reassign for named \newline UDT items & \texttt{set \emph{VarName} w/= \emph{ItemName} <- \emph{val};} \newline
                                            e.g.: \texttt{mutable p = Complex(0., 0.);} \newline
                                                  \texttt{\hphantom{....}set p w/= Re <- 1.0;} \\
  \end{keysref}

  \begin{keysref}{Symbols and Variables}
    Declare immutable \newline symbol           & \texttt{let \emph{varName} = \emph{value}} \\
    Declare mutable \newline symbol (variable)  & \texttt{mutable \emph{varName} = \emph{initialValue}} \\
    Update mutable \newline symbol (variable)   & \texttt{set \emph{varName} = \emph{newValue}} \\
    Apply-and-reassign                          & \texttt{set \emph{varName} \emph{operator}= \emph{expression}} \newline
                                                  e.g.: \texttt{set counter += 1;} \\
  \end{keysref}

  \columnbreak

  \begin{keysref}{Functions and Operations}
    Define function \newline (classical routine)
              & \texttt{function \emph{Name}(\emph{in0} : \emph{type0}, ...) \newline
                : \emph{returnType} \{} \newline
                \texttt{\hphantom{....}// \emph{function body}} \newline
                \texttt{\}} \\
    Call function & \texttt{\emph{Name}(\emph{parameters})} \newline
                    e.g.: \texttt{let two = Sqrt(4.0);} \\
    Define operation \newline (quantum routine) \newline with explicitly \newline specified body, \newline controlled and \newline adjoint variants
               & \texttt{operation \emph{Name}(\emph{in0} : \emph{type0}, ...) \newline
                 : \emph{returnType} \{} \newline
                 \texttt{\hphantom{....}body \{ ... \}} \newline
                 \texttt{\hphantom{....}adjoint \{ ... \}} \newline
                 \texttt{\hphantom{....}controlled \{ ... \}} \newline
                 \texttt{\hphantom{....}adjoint controlled \{ ... \}} \newline
                 \texttt{\}} \\
    Define operation \newline with automatically \newline generated adjoint \newline and controlled \newline variants
               & \texttt{operation \emph{Name}(\emph{in0} : \emph{type0}, ...) \newline
                 : \emph{returnType} is Adj + Ctl \{} \newline
                 \texttt{\hphantom{....}... \newline
                 \} } \\
    Call operation & \texttt{\emph{Name}(\emph{parameters})} \newline
                     e.g.: \texttt{Ry(0.5 * PI(), q);} \\
    Call adjoint \newline operation & \texttt{Adjoint \emph{Name}(\emph{parameters})} \newline
                     e.g.: \texttt{Adjoint Ry(0.5 * PI(), q);} \\
    Call controlled \newline operation & \texttt{Controlled \emph{Name}(\emph{controlQubits}, \newline \hphantom{....}\emph{parameters})} \newline
                     e.g.: \texttt{Controlled Ry(controls, \newline \hphantom{....}(0.5 * PI(), target));} \\
  \end{keysref}

  \begin{keysref}{Control Flow}
    Iterate over \newline a range of numbers
                       & \texttt{for \emph{index} in \emph{range} \{ }\newline
                         \texttt{\hphantom{....}// Use integer \emph{index}} \newline
                         \texttt{\hphantom{....}...} \newline
                         \texttt{\}} \newline
                         e.g.: \texttt{for i in 0..N-1 \{ ... \}} \\
    While loop \newline (within functions)
                       & \texttt{while (\emph{condition}) \{} \newline
                         \texttt{\hphantom{....}...} \newline
                         \texttt{\}} \\
    Iterate over \newline an array & \texttt{for \emph{val} in \emph{array} \{ }\newline
                                     \texttt{\hphantom{....}// Use value \emph{val}} \newline
                                     \texttt{\hphantom{....}...} \newline
                                     \texttt{\}} \newline
                                     e.g.: \texttt{for q in register \{ ... \}} \\
    Repeat-until- \newline success loop      & \texttt{repeat \{ ... \} \newline until \emph{condition} \newline fixup \{ ... \}} \\
    Conditional \newline statement & \texttt{if \emph{cond1} \{ ... \}} \newline
                                     \texttt{elif \emph{cond2} \{ ... \}} \newline
                                     \texttt{else \{ ... \}} \\
    Ternary operator   & \texttt{\emph{condition} ? \emph{caseTrue} | \emph{caseFalse} } \\
    Return a value     & \texttt{return \emph{value}} \\
    Stop with an error & \texttt{fail "\emph{Error message}"} \\
    Conjugations \newline ($ABA^\dagger$ pattern)
                       & \texttt{within \{ ... \}} \newline
                         \texttt{apply \{ ... \}} \\
  \end{keysref}

  \columnbreak

  \begin{keysref}{Arrays}
    Allocate array       & \texttt{mutable \emph{name} = new \emph{Type}[\emph{length}]} \newline
                           e.g.: \texttt{mutable b = new Bool[2];} \\
    Get array length     & \texttt{Length(\emph{name})} \\
    Access k-th element  & \texttt{\emph{name}[k]} \newline NB: indices are 0-based  \\
    Assign k-th element \newline (copy-and-update)
                         & \texttt{set \emph{name} w/= \emph{k} <- \emph{value}} \newline
                           e.g.: \texttt{set b w/= 0 <- true;} \\
    Array literal        & \texttt{[\emph{value0}, \emph{value1}, ...]} \newline
                           e.g.: \texttt{let b = [true, false, true];} \\
    Array concatenation  & \texttt{\emph{array1} + \emph{array2}} \newline
                           e.g.: \texttt{let t = [1, 2, 3] + [4, 5];} \\
    Slicing (subarray)   & \texttt{\emph{name}[\emph{sliceRange}]} \newline
                           e.g.: if \texttt{t = [1, 2, 3, 4, 5]}, then \newline
                           \texttt{\hphantom{..}t[1 .. 3]\hphantom{....}} is \texttt{[2, 3, 4]} \newline
                           \texttt{\hphantom{..}t[3 ...]\hphantom{.....}} is \texttt{[4, 5]} \newline
                           \texttt{\hphantom{..}t[... 1]\hphantom{.....}} is \texttt{[1, 2]} \newline
                           \texttt{\hphantom{..}t[0 .. 2 ...]\hphantom{}} is \texttt{[1, 3, 5]} \newline
                           \texttt{\hphantom{..}t[...-1...]\hphantom{..}} is \texttt{[5, 4, 3, 2, 1]} \\
  \end{keysref}

  \begin{keysref}{Debugging (classical)}
    Print a string     & \texttt{Message("Hello Quantum!")} \\
    Print an \newline interpolated string
                       & \texttt{Message(\$"Value = \{\emph{val}\}")} \\
  \end{keysref}

  \section{Resources}

  \begin{keysref}{Documentation}
    Quantum \newline Development Kit & \url{https://docs.microsoft.com/azure/quantum} \\
    QDK user guides & \url{https://docs.microsoft.com/azure/quantum/user-guide} \\
    \qs~Libraries \newline Reference & \url{https://docs.microsoft.com/qsharp/api} \\
  \end{keysref}

  \begin{keysref}{\qs~Code Repositories}
    QDK Samples                                & \url{https://github.com/microsoft/quantum} \\
    QDK Libraries                              & \url{https://github.com/microsoft/QuantumLibraries} \\
    Quantum Katas \newline (tutorials)         & \url{https://github.com/microsoft/QuantumKatas} \\
    \qs~compiler and \newline extensions       & \url{https://github.com/microsoft/qsharp-compiler} \\
    Simulation \newline framework              & \url{https://github.com/microsoft/qsharp-runtime} \\
    Jupyter kernel and \newline Python host    & \url{https://github.com/microsoft/iqsharp} \\
    Source code for \newline the documentation & \url{https://github.com/MicrosoftDocs/quantum-docs} \\
  \end{keysref}

  \columnbreak

  \begin{keysref}{Qubit Allocation}
    Allocate a register \newline of $N$ qubits
                         & \texttt{use \emph{reg} = Qubit[\emph{N}]; } \newline
                           \texttt{//} Qubits in \texttt{\emph{reg}} start in $\ket{0}$. \newline
                           \texttt{...} \newline
                           \texttt{//} Qubits must be returned to $\ket{0}$. \\
    Allocate one qubit   & \texttt{use \emph{one} = Qubit(); } \newline
    						 \texttt{...} \\
    Allocate a mix of \newline qubit registers and \newline individual qubits
                         & \texttt{use (\emph{x}, \emph{y}, ... ) = } \newline
                           \texttt{\hphantom{...}(Qubit[N], Qubit(), ... ); } \newline
                           \texttt{...} \\
  \end{keysref}

  \begin{keysref}{Debugging (quantum)}
    Print amplitudes \newline of wave function               & \texttt{DumpMachine("dump.txt")} \\
    Assert that a qubit is in $\ket{0}$ or $\ket{1}$ state   & \texttt{AssertQubit(Zero, \emph{zeroQubit})} \newline
                                                               \texttt{AssertQubit(One, \emph{oneQubit})} \\
  \end{keysref}

  \begin{keysref}{Measurements}
    Measure qubit in \newline Pauli $Z$ basis & \texttt{M(\emph{oneQubit})} \newline yields a \texttt{Result} (\texttt{Zero} or \texttt{One}) \\
    Reset qubit to $\ket{0}$     & \texttt{Reset(\emph{oneQubit})} \\
    Reset an array of \newline qubits to $\ket{0..0}$ & \texttt{ResetAll(\emph{register})} \\
  \end{keysref}

  \section{Working with \qs~from command line}

  \begin{keysref}{Command Line Basics}
    Change directory    & \texttt{cd \emph{dirname}} \\
    Go to home          & \texttt{cd \textasciitilde} \\
    Go up one directory & \texttt{cd ..} \\
    Make new directory  & \texttt{mkdir \emph{dirname}} \\
    Open current \newline directory in VS Code & \texttt{code .} \\
  \end{keysref}

  \begin{keysref}{Working with \qs~Projects}
    Create new project & \texttt{dotnet new console -lang Q\# \newline
                         --output \emph{project-dir}} \\
    Change directory to \newline project directory & \texttt{cd \emph{project-dir}} \\
    Build project                     & \texttt{dotnet build} \\
    Run all unit tests                & \texttt{dotnet test} \\
  \end{keysref}

  \columnbreak

  \section{Math reference}

  \begin{keysref}{Complex Arithmetic}
    $i^2 $                         & $-1$ \\
    $ (a + bi) + (c + di) $        & $ (a + c) + (b + d)i $ \\
    $ (a + bi)  (c + di) $         & $ a \cdot c + a \cdot di + b \cdot ci + (b \cdot d)i^2 =$ \newline
                                     $ = (a \cdot c - b \cdot d) + (a \cdot d + b \cdot c)i $ \\
	  Complex conjugate              & $ \overline{a + bi} = a - bi $ \\
    Division $ \frac{a+bi}{c+di} $ & $ \frac{a+bi}{c+di} \cdot 1 = \frac{a+bi}{c+di} \cdot \frac{c-di}{c-di} = \frac{(a+bi)(c-di)}{c^2 + d^2} $ \\
    Modulus $|a + bi|$             & $ \sqrt{a^2 + b^2} $ \\
    $e^{i\theta} $                 & $ \cos \theta + i \sin \theta $ \\
    $ e^{a + bi} $                 & $ e^a \cdot e^{bi} = e^a \cos b  + i e^a \sin b $  \\
    $ r^{a+bi} $                   & $ r^a \cdot r^{bi} = r^a \cdot e^{bi\ln r} = $ \newline
                                     $ = r^a \cos(b\ln r) + i \cdot r^a \sin(b\ln r) $ \\
    Polar form $re^{i\theta}$ to \newline Cartesian form $a + bi$ & $ a = r \cos \theta \newline b = r \sin \theta $ \\
    Cartesian form $a + bi$ \newline to polar form $re^{i\theta}$ & $ r = \sqrt{a^2 + b^2}  \newline  \theta = \arctan(\frac{b}{a}) $ \\
  \end{keysref}

  \begin{keysref}{Linear Algebra}

    $m \times n$ matrix & \usebox\matrixrepresentationright \\

    Vector of size $n$ & \usebox\vectorrepresentationright \\

    \usebox\matrixadditionleft & \usebox\matrixadditionright \\

    \usebox\matrixscalarleft &\usebox\matrixscalarright \\

    \usebox\matrixmultiplicationleft & \usebox\matrixmultiplicationright \\

    \usebox\matrixtransposeleft & \usebox\matrixtransposeright \\

    \usebox\matrixadjointleft & \usebox\matrixadjointright \\

    \usebox\innerproductleft & \usebox\innerproductright \\

    \usebox\outerproductleft & \usebox\outerproductright \\

  \end{keysref}

  \newpage
  \end{multicols}
  \section{Gates reference}

  \begin{keysrefGates}{Single Qubit gates}

    Gate &
    Matrix representation  &
    Ket-bra representation &
    Applying to $ \ket{\psi} = \alpha\ket{0} + \beta\ket{1} $ &
    Applying to basis states: & $\ket{0},\: \ket{1},\: \ket{+},\: \ket{-}\:$ and  &
    $\ket{\pm i} = \frac{1}{\sqrt2}(\ket{0} \pm i\ket{1})$ \\

    X &
    \usebox\xgate & $ \ket{0}\bra{1} + \ket{1}\bra{0}$ &
    $ \ket{\psi} = \alpha\ket{1} + \beta\ket{0} $ &
    $ X\ket{0} = \ket{1} \newline X\ket{1} = \ket{0} $ &
    $ X\ket{+} = \ket{+} \newline   X\ket{-} = -\ket{-}  $ &
    $ X\ket{i} = i\ket{-i} \newline X\ket{-i} = -i\ket{i} $    \\

    Y &
    \usebox\ygate &
    $ i(\ket{1}\bra{0} - \ket{0}\bra{1})$ &
    $ Y\ket{\psi} = i\big(\alpha\ket{1} - \beta\ket{0}\big)$ &
    $Y\ket{0} = i\ket{1} \newline Y\ket{1} = -i\ket{0} $ &
    $ Y\ket{+} = -i\ket{-}  \newline Y\ket{-} = i\ket{+} $ &
    $ Y\ket{i} = \ket{i} \newline Y\ket{-i} = -\ket{-i}$   \\

    Z &
    \usebox\zgate &
    $ \ket{0}\bra{0} - \ket{1}\bra{1} $ &
    $ Z\ket{\psi} = \alpha\ket{0} - \beta\ket{1} $ &
    $ Z\ket{0} = \ket{0} \newline Z\ket{1} = -\ket{1} $ &
    $  Z\ket{+} = \ket{-} \newline Z\ket{-} = \ket{+} $ &
    $ Z\ket{i} = \ket{-i} \newline Z\ket{-i} = \ket{i} $  \\

    I &
    \usebox\igate &
    $ \ket{0}\bra{0} + \ket{1}\bra{1} $  &
    $ I\ket{\psi} = \ket{\psi} $ & & & \\

    H &
    \usebox\hgate &
    $ \ket{0}\bra{+} + \ket{1}\bra{-} $ &
    $ H\ket{\psi} = \alpha\ket{+} + \beta\ket{-} = \frac{\alpha + \beta}{\sqrt{2}}\ket{0} + \frac{\alpha - \beta}{\sqrt{2}}\ket{1} $ &
    $ H\ket{0} = \ket{+}   \newline H\ket{1} = \ket{-} $ &
    $ H\ket{+} = \ket{0} \newline H\ket{-} = \ket{1} $ &
    $ H\ket{i} = e^{i\pi/4}\ket{-i} \newline H\ket{-i} = e^{-i\pi/4}\ket{i} $ \\

    S &
    \usebox\sgate &
    $ \ket{0}\bra{0} + i\ket{1}\bra{1} $ &
    $ S\ket{\psi} = \alpha\ket{0} + i\beta\ket{1} $ &
    $ S\ket{0} = \ket{0} \newline S\ket{1} = i\ket{1} $ &
    $ S\ket{+} = \ket{i} \newline S\ket{-} = \ket{-i} $ &
    $ S\ket{i} = \ket{-} \newline S\ket{-i} = \ket{+} $  \\

    T &
    \usebox\tgate &
    $ \ket{0}\bra{0} + e^{i\pi/4}\ket{1} \bra{1} $ &
    $ T\ket{\psi} = \alpha\ket{0} + e^{i\pi/4} \beta \ket{1} $ &
    $ T\ket{0} = \ket{0}  $ &
    $ T\ket{1} = e^{i\pi/4}\ket{1} $ &  \\

    $ R_x(\theta) $ &
    \usebox\rxgate &
    $ \cos\frac{\theta}{2}\ket{0}\bra{0}  -i\sin\frac{\theta}{2}\ket{1}\bra{0} - \newline
      -i\sin\frac{\theta}{2}\ket{0} \bra{1} + \cos\frac{\theta}{2}\ket{1}\bra{1} $ &
    $ R_x(\theta)\ket{\psi} = \ (\alpha\cos\frac{\theta}{2} - i\beta\sin\frac{\theta}{2})\ket{0} + \newline
      + (\beta\cos\frac{\theta}{2} - i\alpha\sin\frac{\theta}{2})\ket{1} $ &
    $ R_x(\theta)\ket{0} = \newline
      = \cos\frac{\theta}{2}\ket{0} - i\sin\frac{\theta}{2}\ket{1} $ &
    $ R_x(\theta)\ket{1} = \newline
      = \cos\frac{\theta}{2}\ket{1} - i\sin\frac{\theta}{2}\ket{0} $ & \\

    $ R_y(\theta) $ &
    \usebox\rygate   &
    $ \cos\frac{\theta}{2}\ket{0}\bra{0} + \sin\frac{\theta}{2} \ket{1}\bra{0} - \newline
      -\sin\frac{\theta}{2}\ket{0}\bra{1} + \cos\frac{\theta}{2}\ket{1}\bra{1}$ &
    $ R_y(\theta)\ket{\psi} =  (\alpha\cos\frac{\theta}{2} - \beta\sin\frac{\theta}{2})\ket{0} + \newline
      + (\beta\cos\frac{\theta}{2} + \alpha\sin\frac{\theta}{2})\ket{1} $ &
    $ R_y(\theta)\ket{0} = \newline
      = \cos\frac{\theta}{2}\ket{0} + \sin\frac{\theta}{2}\ket{1} $ &
    $ R_y(\theta)\ket{1} = \newline
      = \cos\frac{\theta}{2}\ket{1} - \sin\frac{\theta}{2}\ket{0} $  &  \\

    $ R_z(\theta) $ & \usebox\rzgate &
    $  e^{-i\theta/2}\ket{0}\bra{0} + e^{i\theta/2}\ket{1} \bra{1} $ &
    $ R_z(\theta)\ket{\psi} = \alpha e^{-i\theta/2}\ket{0} + \beta e^{i\theta/2}\ket{1} $ &
    $ R_z(\theta)\ket{0} = e^{-i\theta/2}\ket{0} $ &
    $ R_z(\theta)\ket{1} = e^{i\theta/2}\ket{1} $  &  \\

    $ R_1(\theta) $ &
    \usebox\rgate &
    $  \ket{0}\bra{0} + e^{i\theta}\ket{1} \bra{1} $ &
    $ R_1(\theta)\ket{\psi} =  \alpha\ket{0} + \beta e^{i\theta}\ket{1} $ &
    $ R_1(\theta)\ket{0} = \ket{0} $ &
    $ R_1(\theta)\ket{1} = e^{i\theta}\ket{1} $  &  \\

  \end{keysrefGates}

  \begin{keysrefMultiGates}{Two-qubit gates}

    Gate &
    Matrix Representation &
    Ket-Bra Representation &
    Applying to $\ket{\psi} = \alpha\ket{00} + \beta\ket{01} + \gamma\ket{10} + \delta\ket{11}$ &
    Applying to basis states & \\

    CNOT &
    \usebox\cnotgate &
    \usebox\braketcx &
    $ \text{CNOT}\ket{\psi} = \alpha\ket{00} + \beta\ket{01} + $ {\boldmath $ \delta\ket{10} + \gamma\ket{11} $} &
    $ \text{CNOT}\ket{00} = \ket{00} \newline
      \text{CNOT}\ket{01} = \ket{01} $ &
    $ \text{CNOT}\ket{10} = \ket{11} \newline
      \text{CNOT}\ket{11} = \ket{10} $ \\

    SWAP &
    \usebox\swapgate &
    $ \ket{00}\bra{00} + \ket{01}\bra{10} + \ket{10}\bra{01} + \ket{11}\bra{11} $ &
    $ \text{SWAP}\ket{\psi} = \alpha\ket{00} + $ {\boldmath $ \gamma\ket{01} + \beta\ket{10} $} $+ \delta\ket{11}$ &
    $ \text{SWAP}\ket{00} = \ket{00} \newline
      \text{SWAP}\ket{01} = \ket{10} $ &
    $\text{SWAP}\ket{10} = \ket{01} \newline
      \text{SWAP}\ket{11} = \ket{11} $ \\

    Controlled U &
    \usebox\controlledugate  &
    \usebox\braketcu &
    $ \text{CU} \ket{\psi} =  \alpha\ket{00} + \beta\ket{01} + \newline
      + (\gamma  a_{0,0} + \delta  a_{0,1}) \ket{10} + (\gamma  a_{1,0} + \delta  a_{1,1}) \ket{11}  $ &
    $ \text{CU} \ket{00} = \ket{00} \newline
      \text{CU} \ket{01} = \ket{01} $ &
    $\text{CU} \ket{10} = a_{0,0} \ket{10} + a_{1,0} \ket{11} \newline
     \text{CU} \ket{11} = a_{0,1} \ket{10} + a_{1,1} \ket{11} $ \\
  \end{keysrefMultiGates}

  \begin{keysrefMultiGates}{Toffoli (CCNOT) gate}

    Gate &
    Matrix Representation &
    Ket-Bra Representation &
    Applying to $\ket{\psi} = \alpha\ket{000} + \beta\ket{001} + \gamma\ket{010} + \delta\ket{011} + \epsilon\ket{100} + \lambda\ket{101}  + \eta\ket{110} + \kappa\ket{111} $ &
    Applying to basis states & \\

    CCNOT &
    \usebox\ccnotgate &
    $ (I_2 - \ket{11}\bra{11}) \otimes I_1 + \ket{11}\bra{11} \otimes X $ &  %$ \ket{000}\bra{000} + \ket{001}\bra{001} + \ket{010}\bra{010} + \ket{011}\bra{011} + \ket{100}\bra{100} + \ket{101}\bra{101} + \ket{110}\bra{111} + \ket{111}\bra{110} $ &
    $ \text{CCNOT} \ket{\psi} = \alpha\ket{000} + \beta\ket{001} + \gamma\ket{010} + \delta\ket{011} + \epsilon\ket{100} + \lambda\ket{101}  + $ \boldmath$ \kappa \ket{110} + \eta\ket{111} $ &
    $ \text{CCNOT} \ket{000} = \ket{000} \newline \text{CCNOT} \ket{001} = \ket{001}
      \newline \text{CCNOT} \ket{010} = \ket{010} \newline \text{CCNOT} \ket{011} = \ket{011} $ &
     $ \text{CCNOT} \ket{100} = \ket{100} \newline \text{CCNOT} \ket{101} = \ket{101} \newline
       \text{CCNOT} \ket{110} = \ket{111} \newline \text{CCNOT} \ket{111} = \ket{110} $\\

  \end{keysrefMultiGates}

\end{document}


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

\begin{document}

\chapter{Learning Methods Survey} \label{learning_methods_survey}

In this chapter, we outline a set of ideal characteristics and desired behavior of an ideal candidate classification algorithm, and conduct a survey of potential unsupervised learning methods. We then evaluate each of these surveyed methods against our selection criteria, and determine the optimal method with which to proceed.

\section{Evaluation Criteria}

Despite being not entirely objective, existing classification heuristics have a range of desirable behavior that we would wish to replicate with our candidate clustering algorithm. Additionally, we would want to replicate this behavior while also maintaining objectivity and stability in our new heuristic by utilizing a highly nonparametric learning method.

In particular, we would be extremely interested in preserving the nested hierarchical clustering behavior of the current schemes. That is, to be able to classify a market into sectors, and in turn those sectors into subsectors. Furthermore, it would be desirable to be able to determine these nested sub-sectors in the context of the greater market, rather than in isolated analysis of a particular sector.

Additionally, we would also like to vary the number of resulting sectors of our algorithm while maintaining stability. That is, if we were to request two sectors from a heuristic that was initially resulting in four sectors, the two sectors would be some combination of the initial four sectors, as opposed to an entirely new segmentation profile. This behavior is reflective of the real world, where economic sectors often exhibit nesting, as opposed to independent clustering.

Finally, as per RG-1 (see Section~\ref{research_goals:specific_research_goals}), we are extremely motivated to design a heuristic that is either entirely non-parametric, or parameterized with highly objective, quantitatively derived criteria. In addition to preserving mathematical objectivity of our results, a nonparametric approach would ensure that no personal biases - either explicit or implicit - are introduced to the final learned sectors.

\section{Candidate Learning Methods}

Given the required behavior outlined above, we evaluated three major families of clustering algorithms. We empirically evaluate each clustering technique through the lens of the requirements outlined above.

\subsection{$K$-means Clustering}

$K$-Means Clustering is a method of partitioning $n$-dimensional data into a set of $K$ distinct clusters. The basic algorithm is outlined below\citeFormat{\cite{Lloyd1982LeastPCM}}:

\begin{gather*}
    \text{Let $C_1, C_2, \ldots, C_K$} = \text{Set of K possible clusters} \\
    \text{Let $W(C_k)$} = \text{Measure of pariwise difference of observations in a cluster} \\
    \text{Let $x_{ij}$} = \text{$j^\text{th}$ feature in cluster $i$ with coordinates $x$} \\
    \Rightarrow W(C_k) = \frac{1}{|C_k|} \sum_{i, i^\prime \in C_k} \sum_{j = 1}^p (x_{ij} - x_{i^\prime j})^2 \\
    \\
    \Rightarrow \text{$K$-Means Clusters}
    = \underset{C_1, \ldots, C_k}{\text{minimize}} \sum_{k=1}^K W(C_k)
    = \underset{C_1, \ldots, C_k}{\text{minimize}} \sum_{k=1}^K \frac{1}{|C_k|} \sum_{i, i^\prime \in C_k} \sum_{j = 1}^p (x_{ij} - x_{i^\prime j})^2
\end{gather*}

Notice that in the algorithm outlined above, the $K$-means clustering process requires two sets of parameters at initialization. First, it requires the number of target clusters, $K$, as well as a set of random initializations for cluster centroids, $\frac{1}{|C_k} \sum_{i \in C_k} x_{ij}$. This high level of parameterization, coupled with the clear lack of congruity of assignment across varying values of $K$ make this family of algorithms poorly suited to the task of sector classification, as per the constraints detailed above.

\subsection{Support Vector Classifier}

The support vector classifier is based on the notion of finding a set of hyperplanes in a higher dimensional feature space that optimally divides a set of data into classes. Data is mapped to a higher dimensional space to ensure orthogonal hyperplanes in the divisions of the clusters. The support vector classifier objective function is outlined below\citeFormat{\cite{Ben-Hur2001SupportClustering}}:

\begin{equation*}
    \begin{aligned}
        & \underset{R, a, \boldsymbol{\alpha}}{\text{minimize}} & & R^2 - \sum_i \alpha_i (R^2 - ||x_i - a||^2) \\
        & \text{subject to}
        & & \alpha_i \geq 0 \\
        & & & (R^2 - ||x_i - a||^2) = 0 \; \forall \; i \; \text{(KKT Condition)}
    \end{aligned}
\end{equation*}

As indicated by the literature, and through inspection of the objective function, it is clear that the support vector classifier is parameterized on the kernel used for optimization, as well as the specific loss function employed during model training. Furthermore, this model also optimizes to a fixed numbed of sectors, as opposed to a dynamic number. Therefore, this method too is inappropriate as per the evaluation criteria.

\subsection{Hierarchical Cluster Analysis} \label{learning_methods_survey:hca}

Hierarchical clustering is a greedy algorithm which seeks to build clusters following either an agglomerative, or a divisive approach.\citeFormat{\cite{Ward1963HierarchicalFunction}} Agglomerative clustering is \textit{bottom-up}, with each observation starting it its own cluster, whereas divisive is \textit{top-down}, with all observations starting in one cluster and splits performed recursively at each level. The clusters output by this algorithm are determined by two model settings; the distance metric (i.e. the algorithm for computation of pairwise distance between observations), and the linkage method, which specifies the algorithm governing the dissimilarity of entire sets, as a function of the pairwise distances of observations in those sets.

This method has the distinct advantage of being entirely additively hierarchical, with groups being nested as described in the evaluation criteria. Furthermore, this method is entirely nonparametric, with the sole exception being the choice of linkage and distance metric. Additionally, it is extremely stable with varying sector counts. This is a direct result of the greedy nature of the algorithm, as it does not recompute the hierarchy each time a new cluster arity is extracted, but rather just changes the level of extraction from the same hierarchy.

As per the evaluation of the different families of learning methods detailed in this chapter, we selected Hierarchical Clustering to be the basis of our Learned Sectors classification heuristic.

\end{document}


\section{Performance Analysis}\label{sec:analysis}

In this section we analyze and discuss the overhead of the CCVPN design with respect
to the additional processing time and state consumption needed to handle traffic.

\subsection{State Consumption}
The CCVPN design has an immediate impact on the FIB and PIT size of a gateway.
(The content store size remains unaffected since only decapsulated content objects
are ever cached.) Let $F_S$ be the total size of a standard forwarder
FIB in terms of bytes and
$N_F$ be the number of entries in the FIB. For simplicity, we will assume that
each name prefix in the FIB has a constant size of $64$B. In practice we expect
this to be a comfortable upper bound. Thus, $F_S = N_Fs$, where $s$ is the size of
each FIB entry. Here, $s$ includes a name prefix (of size $64$B) and a bit vector
that identifies the matching links for the interface. We assume that a gateway has
$128$ links which, again, is a comfortable upper bound. Therefore, $F_S = 80N_F$B.
Now consider the FIB size $F_G$ for a CCVPN gateway. Some entries in these FIBs will
point to ``private'' prefixes, i.e., other domains, and therefore have a larger size
to account for the corresponding prefix and key material that must be stored.
For both public- and symmetric-key encryption, the key size is the same: $32$B \cite{sodiumGithub}.
Therefore, by taking into account two both the FIB entry prefix key, translation
prefix, encryption key, and corresponding bit vector, the total size of one ``private''
FIB entry will be $176$B, meaning that $F_G = 176N_F$B. By comparing $F_S$ to $F_G$, we
see that, in the worst case, the CCVPN FIB is at most $F_G/F_S = 176/80 = 2.2$ times larger
than the standard FIB. In practice, however, we expect this to be much smaller, since the
fraction of public to private FIB entries in a gateway will be non-zero.

We will now apply the same analysis to the PIT size. A standard PIT entry includes a
complete name and ingress bit vector. (They may also include the optional {\tt KeyId}
and {\tt ContentId}, but since they are included in the gateway PIT as well we omit them
from this analysis.) A gateway PIT entry will contain the same elements of a standard
PIT entry but also a symmetric encryption key ($32$B), nonce ($12$B), and an
encapsulation name ($64$B + $32$B).
The encapsulation name is the name of an encapsulated interest and includes an additional
$32$B {\tt PayloadID} segment to identify the encapsulated value in the payload. Let
$P_S$ and $P_G$ be the sizes of the standard and gateway PIT, respectively, and let $N_P$
be the number of PIT entries in one such table. Based on the above discussion, and assuming
again that a name is at most $64$B, a standard PIT entry is of the size $80$B. In contrast,
a gateway PIT entry is of size $204$B. Therefore, in the worst case, the CCVPN PIT
will be at most $P_G / P_S = 204/80 = 2.55$B larger than the standard PIT. Assuming
a steady state size of approximately $1e^5$ entries \cite{carofiglio2015pending},
this means that the PIT will be $20.4$MB, which is well within the capacity of
modern systems.

\subsection{Processing Overhead}
In terms of processing overhead, the gateway adds a number of new steps to the data
path of a packet. The main computational burdens are packet encapsulation and decapsulation.
In the public-key variant of CCVPN, interests are processed using public-key encryption,
whereas content is always processed using symmetric-key encryption. Let $T_E^P(n)$ and $T_D^P(n)$
be the time to encrypt and decrypt $n$B of data using a suitable public-key encryption scheme.
Similarly, let $T_E^S(n)$ and $T_D^S(n)$ be the time to encrypt and decrypt $n$B of data
using a symmetric-key encryption scheme. Then, the latency in a single interest-content
exchange is increased by $T = T_E^P(n_I) + T_D^P(n_I) + T_E^S(n_C) + T_D^S(n_C)$, where
$n_I$ and $n_C$ are the original interest and content sizes, respectively. As a rough
estimate, \cite{benchmarks} lists the cost of AES-GCM to be $2.946\mu s$ for setup
followed by $102$MiB/second Intel Core 2 1.83 GHz processor under Windows Vista in
32-bit mode (with AES ISA support). For packets that are at most $1500$B,
the total processing time is roughly $17\mu s$. Moreover, The public-key encryption and
decryption operations will always be at least as expensive, so the total latency is
increased by at least $T = 4 \times 17\mu s = 68 \mu s$. In comparison to the network
latency for a single packet this may not be noticable, but for a steady arrival state of
approximatey $1e^5$, this would lead to an instable system that would quickly overflow.
(This is because $65 \mu s \times 1e^5 = 6.8s$.) Therefore, there is an upper bound on
the number of private packets a gateway can process per second. This bound is entirely
dependent on the system configuration and network conditions.

Another performance deficiency comes from the fact that gateways cannot process packets
without allocating memory. Specifically, each packet requires either an encryption
or decryption. However, this cannot be done entirely in-place, the gateway must allocate some amount
of memory for every processed packet, e.g., to store the MAC tag, to account for
ciphertext expansion, or to apply padding, if desired. This overhead can outweigh
the cryptographic computations if the packet arrival rate is high enough. Therefore,
when implementing CCVPN, special care must be taken to ensure that memory allocations
are minimized or avoided where possible.


\documentclass{article}
\usepackage{hyperref}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[section]{placeins}
\renewcommand{\thesubsection}{\thesection.\alph{subsection}}
\usepackage{listings}

\title{\bf{CSE397: Assignment \#1}}
\author{Nicholas Malaya \\ Institute for Computational Engineering and Sciences \\ University of Texas at Austin} \date{}

\begin{document}
\maketitle

\newpage
\section{Problem 1}

\begin{figure}[!htb]
  \includegraphics[scale=.5]{plots/data.pdf}
  \label{fig:data}
  \caption{The data (after applying the filter) with and without
 normally distributed noise. }
\end{figure}

After applying the new filter $k(x)$ and adding gaussian noise, the data
used as input for the inverse problem is plotted in figure 1.

Notice that this filter is significant: the raw data generated after
passing through the filter (even without statistical noise) has lost
several features of the underlying ``true'' signal.

\subsection{$T_{\text{SVD}}$}

\begin{figure}[!htb]
        \centering
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/tsvd0001.pdf}
                \caption{$\alpha=0.0001$}
        \end{subfigure}%
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/tsvd001.pdf}
                \caption{$\alpha=0.001$}
        \end{subfigure}
        \centering
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/tsvd01.pdf}
                \caption{$\alpha=0.1$}
        \end{subfigure}%
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/tsvd1.pdf}
                \caption{$\alpha=1.0$}
        \end{subfigure}
        \caption{$T_{\text{SVD}}$ at varying values of $\alpha$.}
        \label{fig:svd}
\end{figure}

Figure \ref{fig:svd} plots the solution to the inverse problem using
Truncated SVD at several different values of $\alpha$, the
regularization parameter. We can see that actually, the filter nicely
regularizing the inverse problem even for small values of $\alpha$. It
is only for the largest value of $\alpha$ that the parameter is obviously
too large, at which point almost all the frequencies are filtered out,
and the solution to the inverse problem is constant and zero.

As expected, none of the solutions capture any of the very fine (high
frequency) features of the original data.

\subsection{Tikhanov Filter}

\begin{figure}[!htb]
        \centering
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/reconstruct0001.pdf}
                \caption{$\alpha=0.0001$}
        \end{subfigure}%
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/reconstruct001.pdf}
                \caption{$\alpha=0.001$}
        \end{subfigure}
        \centering
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/reconstruct01.pdf}
                \caption{$\alpha=0.1$}
        \end{subfigure}%
        \begin{subfigure}[bh]{0.45\textwidth}
                \includegraphics[width=\textwidth]{plots/reconstruct1.pdf}
                \caption{$\alpha=1.0$}
        \end{subfigure}
        \caption{Solutions to the inverse problem using Tikhanov
 regularization at varying values of $\alpha$.}
 \label{fig:tik}
\end{figure}

Figure \ref{fig:tik} plots the solution to the inverse problem using
Tikhanov regularization at several different values of $\alpha$, the
regularization parameter.

The Tikhanov filter is more sensitive, with (a) and (b) (low values of
$\alpha]$) completely being dominated by noise. None of the underlying
signal is regained, as all the high frequency content blown up and
dominating.

For higher levels of the regularization parameter, we regain a solution
similar to in the TSVD cases, where we capture the underlying trend of
the data, but none of the sharp features. Also, the highest value of the
filter does not overwhelm the signal completely, as in the TSVD case,
which is interesting.

\subsection{L-Curve}

\begin{figure}[!htb]
  \includegraphics[scale=.6]{plots/L-curve.pdf}
  \caption{The L-curve}
 \label{fig:lcurve}
\end{figure}

Figure \ref{fig:lcurve} plots the results of the L-curve criterion. The
red dot plotted is at 0.01. This is approximately the ``optimal'' value
of regularization, which is generally consistent with the observations
of the previous plots.

\subsection{Morozov's Discrepancy Criterion}

\begin{figure}[!htb]
  \includegraphics[scale=.5]{plots/morozov.pdf}
  \caption{Morozov discrepancy Criterion}
 \label{fig:moro}
\end{figure}

Figure \ref{fig:moro} plots the results of Morozov discrepancy
criterion. I could not get the plots to draw a horizontal line, so that
red dot instead dictates the limit of $\delta$, the norm of the
error. This occurs at $\approx 4e-2$. This is smaller than the value
obtained using the L-curve, but not so much smaller that the value
appears suspect.

\subsection{Actual Error}

\begin{figure}[!htb]
  \includegraphics[scale=.5]{plots/true1d.pdf}
  \caption{The actual error in the reconstruction as a function of
 alpha. }
 \label{fig:actual}
\end{figure}

Figure \ref{fig:actual} plots actual error in the reconstruction,
e.g. the norm of the difference between the reconstruction and the
actual (true) image, as a function of alpha. You can see that this
plot forms a rotated ``S'' curve, with the minimum occuring
approximately at the red dot. The value plotted is 0.01,
which actually agrees with the L-curve estimate.

In any case, the values arrived at using the L-curve, Morozov's
discrepancy criterion and the actual error all agree within at least an
order of magnitude. So we are in the right ``ballpark''. As an aside, I
was actually quite pleased with the result, I would not have expected
these criterion to so closely arrive at values that are reasonable
guesses, even without knowledge of the true underlying signal. While
this is a toy problem, it does show that these criterion can be
effective means of approximating $\alpha$. Given the similarities
between the TSVD and Tikhanov, I would not be surprised to find that
these heuristics can be more generally useful for other regularizations as well.

\newpage
\section{Problem 2}

% \begin{figure}[!htb]
%   \includegraphics[scale=.5]{plots/longhorn.png}
%   \label{fig:true}
%   \caption{The true image.}
% \end{figure}

% Figure \ref{fig:true} plots the true image, that we will be attempting
% to reconstruct through inversion.

\subsection{Memory Requirement}

How much memory would be required to form the entire matrix, $K$?
We have $N^2$ model parameters. K lives in $\mathbb{R}^{N_2 * N_2}$.
With $N=128$, then
\begin{equation}
\frac{N^2 * N^2 * 8 \text{Bytes}}{(1024)^3} = 2 \text{GB}
\end{equation}
So while this is not inaccessible on modern HPC systems, it is certainly
not efficient.

\subsection{L-Curve}

\begin{figure}[!htb]
  \includegraphics[scale=.5]{plots/L-curve2d.pdf}
  \caption{The L-curve for the 2d problem.}
  \label{fig:l2d}
\end{figure}

Figure \ref{fig:l2d} plots the L-curve. The red dot is approximately at
the location of the optimal value of $\alpha$, which is found to be
$\approx 0.05 - 0.001$.

\subsection{Actual Error}

\begin{figure}[!htb]
  \includegraphics[scale=.5]{plots/2d-true.pdf}
  \caption{The actual error in the reconstruction as a function of
 alpha, for the 2d problem.}
  \label{fig:2dt}
\end{figure}

Figure \ref{fig:2dt} plots the true error. Surprisingly, the error curve
appears to flatten out at lower values of alpha, implying a constant
error. Clearly any values less than $1e-3$ appear to be prefered, as the
error increases at higher values of $\alpha$.

To calculate the error, I only needed to solve the conjugate gradient
(as before) and then calculate the norm of the misfit against $I$, the
true image:
\begin{lstlisting}
 I_alpha = pcg(@(in)apply(in,K1,K2,N1,N2,alpha),K_Ibn(:),1e-6,1500);
 misfit(k) = norm((K1 * (K2 * reshape(I_alpha,N2,N1))')' - I);
\end{lstlisting}
\begin{figure}[!htb]
        \centering
        \begin{subfigure}[bh]{0.65\textwidth}
                \includegraphics[width=\textwidth]{plots/tikrecon0001.pdf}
                \caption{$\alpha=0.0001$}
        \end{subfigure}%
        \begin{subfigure}[bh]{0.65\textwidth}
                \includegraphics[width=\textwidth]{plots/tikrecon001.pdf}
                \caption{$\alpha=0.001$}
        \end{subfigure}
 \caption{The reconstructed image.}
\label{fig:tik}
\end{figure}

The ``true'' image would imply then that the L-curve is too conservative
an estimate, and smaller values of $\alpha$ are acceptable. However, I
don't believe that. Figure 9 plots the Tikhanov
reconstruction for $\alpha = 0.001$ and $\alpha = 0.0001$. Clearly the
true image is recognizable in the right hand side, and not at all in the
left. So I believe that I either have a bug in my calculation of the
actual error, or the L-curve is, somehow, a more reliable estimate of
``good'' values of $\alpha$, here.

\end{document}