%======================================================================
\chapter{Methodology}
\label{chapter:methodology}
%======================================================================

%Go into detail about the methods used in doing your project/thesis. What are the relevant ideas, the sources of information, the various approaches that might be used? Discuss approaches finally chosen as well as those discarded and finally describe what was accomplished. It is important here to strike a balance between too much detail and not enough. Above all, avoid a repetitious style. This part of the report is usually covered in one or more middle chapters. A good rule-of-thumb is to break a chapter into two or more parts if it is large compared to other chapters.

This chapter will discuss the various methods and algorithms used in this project in order to generate randomized graphs and their data. Each section discusses a certain generation process, as well as its applied use cases.

\section{Midpoint Displacement}
One of the first algorithms explored was Midpoint Displacement, also known as the Diamond-square Algorithm. As seen in \autoref{figure:midpoint_landscape}, this method is typically used for 2-D mountainous landscape generation.

\hfill

\begin{figure}[hbt]
    \centering
    \includegraphics[width=250pt,keepaspectratio]{figures/body/methodology/MD_2D_landscape.PNG}
    \caption{Example Midpoint Displacement Landscape Generation \cite{acin_2016}}
    \label{figure:midpoint_landscape}
\end{figure}

\subsection{Generation}
Midpoint Displacement begins by simply initializing a straight line segment. After computing the segment's midpoint, it then displaces the \(y\) coordinate value by a ``bounded random value" \cite{acin_2016}. This process is then iterated over as many times as desired, reducing the bounded displacement value each time. This has the effect of creating twice as many segments in every iteration. Successive steps of the Midpoint Displacement algorithm result in segments similar to the image shown in \autoref{figure:midpoint_algorithm}.

\hfill

\begin{figure}[hbt]
    \centering
    \includegraphics[width=250pt,keepaspectratio]{figures/body/methodology/MD_line_segments.PNG}
    \caption{Iterations of Midpoint Displacement Algorithm \cite{acin_2016}}
    \label{figure:midpoint_algorithm}
\end{figure}

\subsection{Use Cases}
Midpoint Displacement was applied for area charts, line graphs, and error bar charts. These were chosen because their correlation of data tends to be visually similar in terms of mountainous peaks and ranges.

\hfill

To execute this, each graph was given randomly generated parameters such as the number of layers, number of iterations, vertical displacement and roughness (\autoref{section:random_number_generation}). These parameters were then fed into a Midpoint Displacement algorithm that randomly generated \(X\) and \(y\) points for each line, or layer.

\hfill

A key difference between these types of graphs is that the area charts required the \(y\) coordinates to be ``stacked" on top of each other, such that no lines crossed over each other. This stacking effect is displayed in section \autoref{figure:area_plot}. Moreover, line graphs required less ``jagged peaks" in their display, and so the range of roughness was decreased, as well as the number of iterations. As seen in \autoref{figure:line_graph} and \autoref{figure:error_bar}, this resulted in smoother-looking correlations, with less points per line. Error bar graphs were formatted in a similar manner to line graphs, but with randomly-generated lengths for the error bars at each point. It is also worth nothing that line graphs and error bar graphs had a 50\% chance of implementing Random Number Generation instead of just Midpoint Displacement, which will be further discussed in \autoref{section:random_number_generation}.

\hfill

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/MD_area.png}
        \caption{Area Plot}
        \label{figure:area_plot}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/MD_line.png}
        \caption{Line Graph}
        \label{figure:line_graph}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/MD_errorbar.png}
        \caption{Error Bar}
        \label{figure:error_bar}
    \end{subfigure}
    \caption{Midpoint Displacement Chart Generation}
    \label{figure:midpoint_generation}
\end{figure}

\section{Regression}
Both Linear and Logarithmic Regression were implemented in order to generate a correlation effect in non-categorical graphs such as scatter plots and bubble plots.

\subsection{Generation}
Linear Regression's data points visually form the shape of a line, which follows the algorithm \(y = mx +b\), where \(m\) is the given slope and \(b\) is some given intercept. The generation of positive Linear Regression was implemented through the use of \href{https://scikit-learn.org/stable/}{scikit-learn}'s \path{make_regression()} method, which returns a number of \(X\) and \(y\) values. Its input parameters of the number of samples, number of features, noise level, and tail strength were all randomly generated within a set range (\autoref{section:random_number_generation}). Negative Linear Regression follows the same process, but all \(y\) values are negated.

\hfill

Although the \(X\) values are still created with scikit-learn's \path{make_regression()} method, Logarithmic Regression requires that \(x>0\), and so all invalid data is filtered out and replaced. Furthermore, unlike Linear Regression, the creation of each \(y\) value is mapped to the logarithm of its matching \(X\) value. This results in an equation similar to \(y = log(x)\). In this project, each calculation has a random logarithmic base, which ranges between two values specified in the generation's parameters (\autoref{section:random_number_generation}). If the Logarithmic Regression results in a negative correlation, these \(y\) values will then be negated.

\subsection{Use Cases}

Linear Regression was utilized for generating both scatter plots and bubble plots. Various noise is randomly applied to each generation, resulting in the graphs ranging from seemingly straight lines to no visible correlation. This variation in noise is displayed through the scatter plots shown in \autoref{figure:scatterplot_noise_regression}.

\hfill

% Noise in Positive Linear Regression - Scatter Plot
\begin{figure}
    \centering
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/linear_scatter1.png}
        \label{figure: scatter1}
        \caption{\(x = 2.09\)}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/linear_scatter2.png}
        \label{figure: scatter2}
        \caption{\(x = 25.18\)}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/linear_scatter3.png}
        \label{figure: scatter3}
        \caption{\(x = 29.72\)}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/linear_scatter4.png}
        \label{figure: scatter4}
        \caption{\(x = 40.17\)}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/linear_scatter5.png}
        \label{figure: scatter5}
        \caption{\(x = 355.77\)}
    \end{subfigure}
    \caption{Noise in Positive Linear Regression - Scatter Plot}
    \label{figure:scatterplot_noise_regression}
\end{figure}

In addition to Linear Regression, bubble plots have a 50\% chance to follow Logarithmic Regression when being generated. This difference in pattern can be clearly seen in \autoref{figure:bubbleplot_regression}, which displays the two types of regressions as bubble plots. Other than the inclusion of logarithms, an important distinction between bubble and scatter plots is that bubble plots also required a random generation of bubble size, which is discussed later in \autoref{section:random_number_generation}.

\hfill

\begin{figure}
    \centering
     \begin{subfigure}[b]{0.21\textwidth}
         \includegraphics[width=\textwidth]{figures/body/methodology/linear_pos_bubble.png}
         \caption{Positive Linear}
         \label{figure: pos_lin}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.21\textwidth}
         \includegraphics[width=\textwidth]{figures/body/methodology/linear_neg_bubble.png}
         \caption{Negative Linear}
         \label{figure: neg_lin}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.21\textwidth}
         \includegraphics[width=\textwidth]{figures/body/methodology/log_pos_bubble.png}
         \caption{Positive Log}
         \label{figure: pos_log}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.21\textwidth}
         \includegraphics[width=\textwidth]{figures/body/methodology/log_neg_bubble.png}
         \caption{Negative Log}
         \label{figure: neg_log}
     \end{subfigure}
        \caption{Linear and Logarithmic Regression - Bubble Plot}
        \label{figure:bubbleplot_regression}
\end{figure}

\section{Distribution Sampling}
%do we need a tag here

The process of sampling values from a provided statistical distribution is known as distribution sampling. The distributions discussed below are Continuous Probability Distributions, most of which are implemented using Python's \href{https://numpy.org/}{NumPy} library. These distributions are repeatedly sampled to create a set of datapoints based on an underlying distribution. In this context, the term ``Continuous" refers to the ``probabilities of the possible values of a continuous random variable", where each random variable has a range of possible values \cite{minitab_express}. Furthermore, each type of Continuous Probability Distribution follows a Probability Density Function, which is used to calculate the probabilities of continuous random variables.

\subsection{Generation}
As many types of graphs utilize Continuous Probability Distributions for data generation, this section will discuss a wide variety of these distribution types. Specifically Gaussian Distribution, Log-normal Distribution, Gamma Distribution, Weibull Distribution, Uniform Distribution, and Multivariate Gaussian Distribution will be covered.

\subsubsection{Gaussian Distribution}
\label{subsubsection:normal_distribution}
Gaussian Distribution, also termed Normal Distribution, is a symmetrical distribution. The correlation forms a shape that is often described as a ``bell curve", which is a result of its centralized mean. With \(\mu\) as the mean and \(\sigma\) as standard deviation, Gaussian Distribution follows the Probability Density Function shown below:

\begin{displaymath}
f(x) = \frac{1}{\sigma\sqrt{2\pi}}e^{\frac{-1}{2}(\frac{x-\mu}{\sigma})^2}
\end{displaymath}

\hfill

The data generation for Gaussian Distribution was created through NumPy's \path{random.normal()} method, which takes input parameters for the mean and standard deviation of the distribution. These required values were randomly generated within a predetermined range for each graph (\autoref{section:random_number_generation}).

\subsubsection{Log-normal Distribution}
Visually similar to Gaussian Distribution (\autoref{subsubsection:normal_distribution}), Log-normal Distribution is identifiable by having a right-skewed bell curve, which results in a non-symmetrical shape. As the name suggests, a Log-normal Distribution is ``a continuous distribution of random variable \(y\) whose natural logarithm is normally distributed" \cite{KISSELL2017103}. With \(\mu\) as the mean and \(\sigma\) as standard deviation, Log-normal Distribution satisfies the following Probability Density Function:

\begin{displaymath}
f(x) = \frac{1}{\sigma\sqrt{2\pi}}e^{\frac{-1}{2}(\frac{lnx-\mu}{\sigma})^2}
\end{displaymath}

\hfill

Rather than using NumPy's \path{random.normal()} function, graphs following a Log-normal Distribution utilize \path{random.lognormal()} instead. The input parameters remain the same as the ones used for Gaussian Distribution (\autoref{subsubsection:normal_distribution}).

\subsubsection{Gamma Distribution}
Gamma Distribution is a maximum entropy probability distribution that is skewed rightwards. It fits the Probability Density Function displayed below, with \(k\) as the shape parameter, \(\theta\) as the scale parameter, and \(\Gamma\) as the gamma function, which can be defined as an ``extension of the factorial function to real (and complex) numbers" \cite{glen_2014}:

\begin{displaymath}
f(x) = \frac{1}{\Gamma(k)\theta^k}x^{k-1}e^{-\frac{x}{\theta}}
\end{displaymath}

\hfill

Data for Gamma Distribution was generated through the use of NumPy's \path{random.gamma()} method, which received randomized values within a specified range for its required input parameters of shape and scale (\autoref{section:random_number_generation}).

\subsubsection{Uniform Distribution}
\label{subsubsection:uniform_distribution}
A Uniform Distribution is one in which every result, within a certain maximum and minimum bounds, is equally likely. This results in a fairly constant-looking correlation, with no noticeable peaks or skews. With \(a\) as the minimum and \(b\) as the maximum such that \(a\le x\le b\), Uniform Distribution fits the Probability Density Function displayed below:

\begin{displaymath}
f(x) = \frac{1}{b-a}
\end{displaymath}

\hfill

Generation for Uniform Distribution was performed with NumPy's \path{random.uniform()} method. The required input parameters of the minimum and maximum were randomly generated within a specified range (\autoref{section:random_number_generation}).

\subsubsection{Weibull Distribution}
Related to exponential functions, Weibull Distribution takes \(k\) as the shape parameter and \(\lambda\) as the scale parameter in order to satisfy the following Probability Density Function:

\begin{displaymath}
f(x) = \begin{cases} \frac{k}{\lambda}(\frac{x}{\lambda})^{k-1}e^{-(\frac{x}{\lambda})^k}, & x \ge 0 \\ 0, & x < 0 \end{cases}
\end{displaymath}

\hfill

The data generation process for Weibull Distribution involved using NumPy's \path{random.uniform()} method to initially generate a Uniform Distribution (\autoref{subsubsection:uniform_distribution}). This Uniform Distribution data, represented as \(d\), was then placed into the following equation:

\begin{displaymath}
data = \lambda(-log(d))^{\frac{1}{k}}
\end{displaymath}

\hfill

As seen, this equation transforms the Uniform Distribution into a Weibull Distribution final data array through the use of NumPy's \path{log()} function, as well as the randomized input of the shape parameter \(k\), and the scale parameter \(\lambda\) (\autoref{section:random_number_generation}).

\subsubsection{Multivariate Gaussian Distribution}
Also named Multivariate Normal Distribution, Multivariate Gaussian Distribution place Gaussian Distributions (\autoref{subsubsection:normal_distribution}) into a multi-dimensional format. This is clearly visualized in \autoref{figure:multivariate_normal}.

\begin{figure}[hbt]
    \centering
    \includegraphics[width=250pt,keepaspectratio]{figures/body/methodology/multivariate_normal.png}
    \caption{Multivariate Gaussian Distribution \cite{multivariate_normal_distribution}}
    \label{figure:multivariate_normal}
\end{figure}

With \(k\) number of varieties, and \(\mu\) as the mean, Multivariate Gaussian Distribution's Probability Distribution Function is displayed below:

\begin{displaymath}
f(x) = \frac{1}{(2\pi)^{\frac{k}{2}}\abs{\Sigma}^{\frac{1}{2}}}e^{-\frac{1}{2}(x-\mu)'\Sigma^{-1}(x-\mu)}
\end{displaymath}

\hfill

In this project, data for Multivariate Gaussian Distribution was generated through NumPy's \path{random.multivariate_normal()} function. The first required input of the mean was given through randomly generating a value within specified parameters (\autoref{section:random_number_generation}). The second required input was a 2-D array represented a co-variance matrix, whose data was generated with the use of Uniform Distributions (\autoref{subsubsection:uniform_distribution}).

\subsection{Use Cases}
Histogram generation utilizes the greatest number of Continuous Probability Distributions. It has an equally random chance of using Gaussian, Lognormal, Gamma, Weibull and Uniform Distributions, all of which can be seen in \autoref{figure:histogram_distributions}.

\hfill

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/norm_hist.png}
        \label{figure: norm_hist}
        \caption{Gaussian}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/lognorm_hist.png}
        \label{figure: lognorm_hist}
        \caption{Lognormal}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/gamma_hist.png}
        \label{figure: gamma_hist}
        \caption{Gamma}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/weibull_hist.png}
        \label{figure: weibull_hist}
        \caption{Weibull}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.19\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/uniform_hist.png}
        \label{figure: uniform_hist}
        \caption{Uniform}
    \end{subfigure}
    \caption{2-D Continuous Probability Distributions - Histogram}
    \label{figure:histogram_distributions}
\end{figure}

Gaussian Distribution was also applied for kernel density estimation, box and violin plots, which resulted in graphs such as the ones displayed in \autoref{figure:normal_distribution}.

\hfill

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.29\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/norm_box.png}
        \caption{Box Plot}
        \label{figure:norm_box}
    \end{subfigure}
    \begin{subfigure}[b]{0.29\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/norm_kde.png}
        \caption{KDE Plot}
        \label{figure:kdeplot}
    \end{subfigure}
    \begin{subfigure}[b]{0.29\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/norm_violin.png}
        \caption{Violin Plot}
        \label{figure:norm_violin}
    \end{subfigure}
    \caption{Gaussian Distributions}
    \label{figure:normal_distribution}
\end{figure}

The only graph type to use Multivariate Gaussian Distribution was contour plot, as it graphs a multi-dimensional surface, demonstrated in \autoref{figure:multivariate_contour}.

\hfill

\begin{figure}[hbt]
    \centering
    \includegraphics[width=250pt,keepaspectratio]{figures/body/methodology/multi_contour.png}
    \caption{Multivariate Gaussian Distribution - Contour}
    \label{figure:multivariate_contour}
\end{figure}

\section{Geometric Brownian Motion}
Geometric Brownian Motion, also named Exponential Brownian Motion, was not implemented in this project for a variety of reasons. Not only was it difficult to implement and more predictable than other generation methods, but it also would only be used for line graphs. Nevertheless, if this project was to be expanded, then the implementation of methods such as Geometric Brownian Motion should be added. It is due to this logic that Geometric Brownian Motion's generation and possible use case shall continue to be discussed.

\begin{figure}[hbt]
    \centering
    \includegraphics[width=375pt,keepaspectratio]{figures/body/methodology/geometric_example.png}
    \caption{Geometric Brownian Motion - Line Graph \cite{geometric_brownian_motion}}
    \label{figure:geometric_example}
\end{figure}

\subsection{Generation}
Graphs that follow Geometric Brownian Motion typically start at an origin of zero and grow exponentially, with a slight visual appearance of roughness. Since Geometric Brownian Motion is a stochastic process with linear drift, its data follows a stochastic differential equation. This equation is displayed below, where \(W_t\) is the Brownian motion, \(\mu\) is the drift percentage, and \(\sigma\) is the volatility percentage:

\begin{displaymath}
dS_{t} = \mu S_{t}dt + \sigma S_{t}dW_{t}
\end{displaymath}

\hfill

To generate data using this equation, \(W_t\) would be Normally Distributed (\autoref{subsubsection:normal_distribution}), and the rest of the input would be randomly generated within specified ranges (\autoref{section:random_number_generation}).

\subsection{Use Cases}
If Geometric Brownian Motion had been implemented then it would have been used as another correlation type for line graphs. This would have resulted in generations similar to the graph displayed in \autoref{figure:geometric_example}.

\section{Perlin Noise}
Perlin Noise is an extremely powerful pseudo-random generation algorithm used for creating random patterns that appear more ``organic" or lifelike in nature. Moreover, the algorithm is heavily used for procedurally generating content as the generated patterns contain patches, or gradients, of similar values which makes them appear to be only somewhat random. This makes it extremely effective for generating terrain (\autoref{figure:perlin_noise_terrain_mesh}) as well as creating textures for objects such as cloud, marble, or fire, as demonstrated in \autoref{figure:perlin_noise_texture}.

\hfill

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/perlin_noise_terrain_mesh.png}
        \caption{Procedurally Generated Terrain \cite{prunier}}
        \label{figure:perlin_noise_terrain_mesh}
    \end{subfigure}
    \begin{subfigure}[b]{0.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/perlin_noise_texture.png}
        \caption{Perlin Noise Mapped to 2-D Texture \cite{perlin_noise_2007}}
        \label{figure:perlin_noise_texture}
    \end{subfigure}
    \caption{Example Applications of Perlin Noise}
\end{figure}

\subsection{Generation}
While Perlin Noise has a few variations, namely Improved Perlin Noise \cite{perlin_2002} and Simplex noise, the base function remains largely the same. In this particular case, Improved Perlin Noise was further examined and is the topic of discussion below. The summary below is largely influenced by the article authored by Adrian Biagioli \cite{biagioli_2014}.

\hfill

The algorithm begins with a Perlin Noise function. This function takes in an \(x\), \(y\), and \(z\) coordinate as input and outputs a value between 0.0 and 1.0 \cite{gustavson_2005}. To generate this value, the coordinates are first divided into unit cubes to ``normalize" the coordinate within the desired dimension space. A 2-D representation of this can be found in \autoref{figure:perlin_noise_unit_cube}. The 4 corner coordinates of this cube are then utilized to generate a pseudo-random gradient vector. Comparatively, if 3-D Perlin Noise is being generated, this cube would possess 8 unit coordinates rather than the 4 mentioned above. These gradient vectors, as seen in \autoref{figure:perlin_noise_gradient_vector}, will be used to calculate distance vectors from the provided point to the 4 corner points (\autoref{figure:perlin_noise_gradient_distance}).

\hfill

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.29\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/perlin_noise_unit_cube.png}
        \caption{2-D Unit Cube}
        \label{figure:perlin_noise_unit_cube}
    \end{subfigure}
    \begin{subfigure}[b]{0.29\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/perlin_noise_gradient_vector.png}
        \caption{Gradient Vectors}
        \label{figure:perlin_noise_gradient_vector}
    \end{subfigure}
    \begin{subfigure}[b]{0.29\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/perlin_noise_gradient_distance.png}
        \caption{Distance Vectors}
        \label{figure:perlin_noise_gradient_distance}
    \end{subfigure}
    \caption{Generation Steps of Perlin Noise \cite{biagioli_2014}}
    \label{figure:perlin_noise_generation}
\end{figure}

Next, the dot product between each gradient vector and it's corresponding distance vector are computed, creating an influence value. This value is positive when it is in the direction of the gradient, negative when opposite facing, and has a value of 0 if both vectors are perpendicular (\autoref{figure:perlin_noise_influence}). All influence values in the grid must then be interpolated to create a value much like a weighted average. One final step, known as easing, must be completed due to the linear interpolation, as the averaged values would otherwise look unnatural.

\hfill

\begin{figure}
    \centering
    \includegraphics[width=0.6\textwidth]{figures/body/methodology/perlin_noise_influence.png}
    \caption{2-D Representation of Perlin Noise Influence Values \cite{biagioli_2014}}
    \label{figure:perlin_noise_influence}
\end{figure}

A fade function, or ease curve, is used on the averaged coordinate values to create a smoother transition between gradients \autoref{figure:perlin_noise_ease}. By using this ease curve, the coordinates are changed more gradually when approaching integral coordinates, thus creating patterns that seem to flow together. The easing function used in the Improved Perlin Noise algorithm can be seen below, where \(t\) represents a given unit cube coordinate.

\begin{displaymath}
6t^5 - 15t^4 + 10t^3
\end{displaymath}

\begin{figure}
    \centering
    \includegraphics[width=0.55\textwidth]{figures/body/methodology/perlin_noise_ease.png}
    \caption{Improved Perlin Noise easing function}
    \label{figure:perlin_noise_ease}
\end{figure}

\subsection{Use Cases}
In the context of graph generation, Perlin Noise could be used for generating area graphs as well as contour plots. The former was attempted to be implemented, however, in testing, the data did not provide examples visually similar to a standard contour plot. However, it is still believed that Perlin Noise could be used to generate random contour plots, as the following articles by Benjamin Hauer \cite{hauer_2019} and Amit Patel \cite{patel_2015} seemingly do so, although further research must be conducted before reaching any final conclusions. As it is commonly used in terrain generation, for both 2-D and 3-D environments, it is no surprise that Perlin Noise is feasible for generating adequate area plots. Due to the aforementioned roadblocks, as well as stringent time constraints, Perlin Noise was not further researched for either contour or area graph generation.

\hfill

Additional implementations such as CONREC \cite{bourke_1987}, a subroutine for contouring a specified surface as a triangular mesh, and Simplex noise \cite{gustavson_2005}, an enhanced version of the original Perlin Noise algorithm, were also considered and subsequently discarded. The aforementioned CONREC algorithm was extremely hard to randomize and did not provide representative contour plots which is ultimately why it was rejected.

\hfill

On the other hand, the Simplex noise algorithm does alleviate some of the problems with Perlin Noise, such as computational complexity and an inequal direction distribution, however, it was abandoned for the same reasons faced by Perlin Noise. While it is believed that CONREC and simplex noise could still be used for contour generation, similar to Perlin Noise, further research must be completed before reaching a conclusion.

\section{Random Number Generation}
\label{section:random_number_generation}
Random Number Generation allows for integers and floats to be randomly generated. This is useful for data generation, stylization, theming, and randomizing input parameters.

\subsection{Generation}
Random Number Generation produces a random value, or a range of data points with seemingly no pattern or correlation in a specified range. Depending on the type of variable desired, either NumPy's \path{random.randint()} or NumPy's \path{random.uniform()} method was applied for generation, which utilizes Uniform Distributions (\autoref{subsubsection:uniform_distribution}) to return random values. Since its input parameters require a smallest possible integer and a largest possible integer, these parameters were also randomly generated within a previously specified range.

\subsection{Use Cases}
As Random Number Generation was heavily relied upon during this project, there were a large variety of factors and use cases it was responsible for. The first of which was simply to generate data with no correlation, which was utilized in line graphs, error bar graphs and bar charts. This data generation can be seen in \autoref{figure:rand_num_data_gen}.

\hfill

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/random_line.png}
        \caption{Line Graph}
        \label{figure:random_line}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/random_bar.png}
        \caption{Bar Chart}
        \label{figure:random_bar}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.32\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/random_error.png}
        \caption{Error Bar}
        \label{figure:random_error}
    \end{subfigure}
    \caption{Random Number Graph Generation}
    \label{figure:rand_num_data_gen}
\end{figure}

It is also worth noting that correlation for bar charts were generated by ordering the random data points. This resulted in the addition of positive and negative correlations, each with a random amount of noise and a random chance of displaying horizontally rather than vertically. These correlations are displayed in \autoref{figure:bar_correlations}.

\hfill

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.24\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/random_vertical_bar.png}
        \caption{Random Vertical}
        \label{figure:random_vertical_bar}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.24\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/random_horizontal_bar.png}
        \caption{Random Horizontal}
        \label{figure:random_horizontal_bar}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.24\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/pos_vertical_bar.png}
        \caption{Positive Vertical}
        \label{figure:pos_bar}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.24\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/body/methodology/neg_vertical_bar.png}
        \caption{Negative Vertical}
        \label{figure:neg_error}
    \end{subfigure}
    \caption{Bar Chart Random Correlations}
    \label{figure:bar_correlations}
\end{figure}

In addition to data points, Random Number Generation was used to generate graph-specific input, such as graph orientation and number of layers, lines, boxes, or violins. Another example of this is graphs that required specific sizes per data point, such as bubble size or error bar length. Furthermore, any random input parameters needed for generation functions were created using Random Number Generation, with the ranges predetermined by the user.

\hfill

Lastly, Random Number Generation was used for theming and stylization purposes in order to further graph type variation. Themes were randomly picked per library, and examples of random stylization include line thickness, colour (RGB values ranging between 0 and 255), as well as marker sizes. More information on theming and stylization can be found in \autoref{subsection:style_generation}.


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

      \subsection{conlin.m}

\begin{par}
\textbf{Summary:} Affine controller $u = Wx + b$ with input dimension D and control dimension E. Compute mean and covariance of the control distribution $p(u)$ from a Gaussian distributed input $x\sim\mathcal N(x|m,s)$. Moreover, the $s^{-1}cov(x,u)$ is computed.
\end{par} \vspace{1em}
\begin{verbatim}function [M, S, V, dMdm, dSdm, dVdm, dMds, dSds, dVds, dMdp, dSdp, dVdp] ...
          = conlin(policy, m, s)\end{verbatim}
\begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}
\begin{verbatim}policy    policy structure
  .p      parameters that are modified during training
    .w    linear weights                                         [ E  x  D ]
    .b    biases/offset                                          [ E       ]
m         mean of state distribution                             [ D       ]
s         covariance matrix of state distribution                [ D  x  D ]\end{verbatim}
\begin{par}
\textbf{Output arguments:}
\end{par} \vspace{1em}
\begin{verbatim}M         mean of predicted control                              [ E       ]
S         variance of predicted control                          [ E  x  E ]
C         inv(s) times input-output covariance                   [ D  x  E ]
dMdm      deriv. of mean control wrt input mean                  [ E  x  D ]
dSdm      deriv. of control covariance wrt input mean            [E*E x  D ]
dCdm      deriv. of C wrt input mean                             [D*E x  D ]
dMds      deriv. of mean control wrt input covariance            [ E  x D*D]
dSds      deriv. of control covariance wrt input covariance      [E*E x D*D]
dCds      deriv. of C wrt input covariance                       [D*E x D*D]
dMdp      deriv. of mean control wrt policy parameters           [ E  x  P ]
dSdp      deriv. of control covariance wrt policy parameters     [E*E x  P ]
dCdp      deriv. of C wrt policy parameters                      [D*E x  P ]\end{verbatim}
\begin{par}
where P = (D+1)*E is the total number of policy parameters
\end{par} \vspace{1em}
\begin{par}
Copyright (C) 2008-2013 by Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
\end{par} \vspace{1em}
\begin{par}
Last modified: 2012-07-03
\end{par} \vspace{1em}

\subsection*{High-Level Steps}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item Extract policy parameters from policy structure
   \item Predict control signal
   \item Compute derivatives if required
\end{enumerate}

\begin{lstlisting}
function [M, S, V, dMdm, dSdm, dVdm, dMds, dSds, dVds, dMdp, dSdp, dVdp] ...
  = conlin(policy, m, s)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
% 1. Extract policy parameters from policy structure
w = policy.p.w;                                 % weight matrix
b = policy.p.b;                                 % bias/offset
[E D] = size(w);                                % dim of control and state

% 2. Predict control signal
M = w*m + b;                                                        % mean
S = w*s*w'; S = (S+S')/2;                                     % covariance
V = w';                                   % inv(s)*input-output covariance

% 3. Compute derivatives if required
if nargout > 3
  dMdm = w;            dSdm = zeros(E*E,D); dVdm = zeros(D*E,D);
  dMds = zeros(E,D*D); dSds = kron(w,w);    dVds = zeros(D*E,D*D);

  X=reshape(1:D*D,[D D]); XT=X'; dSds=(dSds+dSds(:,XT(:)))/2; % symmetrize
  X=reshape(1:E*E,[E E]); XT=X'; dSds=(dSds+dSds(XT(:),:))/2;

  wTdw =reshape(permute(reshape(eye(E*D),[E D E D]),[2 1 3 4]),[E*D E*D]);
  dMdp = [eye(E) kron(m',eye(E))];
  dSdp = [zeros(E*E,E) kron(eye(E),w*s)*wTdw + kron(w*s,eye(E))];
  dSdp = (dSdp + dSdp(XT(:),:))/2;                            % symmetrize
  dVdp = [zeros(D*E,E) wTdw];
end
\end{lstlisting}


\section{Conclusion} \label{gua:sec:concl}

We have extended the cutoffs for guarded protocols of Emerson and
Kahlon~\cite{Emerson00} to support local deadlock detection, fairness
assumptions, and open systems.
In particular, our results imply the decidability of the parameterized model checking problem for this class of systems and specifications,
which to the best of our knowledge was unknown before.
%Our results allow us to model check
%guarded protocols that satisfy not
%only safety, but also liveness conditions, for an arbitrary number of
%components.
Furthermore, the cutoff results can easily be integrated into
the parameterized synthesis approach~\cite{JB14}.
%~\cite{Jacobs14,Khalimov13,Khalimov13a}.

%An approach for using cutoff results in
%synthesis has been introduced by
%Jacobs and Bloem~\cite{Jacobs14}. It has been described in detail for the
%case of
%token-passing systems. Follow-up papers have shown how to make the approach
%more efficient~\cite{KhalimovJB13b}, and how to use it for the synthesis of a
%large
%case study, the AMBA bus arbiter~\cite{BloemJK14}.

Since conjunctive guards can model atomic sections and read-write locks,
and disjunctive guards can model pairwise rendezvous
(for some classes of specifications, see~\cite{EmersonK03}),
our results apply to a wide spectrum of systems models.
But the expressive power of the model %and flexibility of the results
comes at a high cost: cutoffs are linear in the size of a process, and
are shown to be tight (with respect to this parameter).
For conjunctive systems, our new results are restricted to systems with
1-conjunctive guards, effectively only allowing to model a single shared
resource.
We conjecture that our proof methods can be extended to systems with
more general conjunctive guards, at the price of bigger cutoffs.
We leave this extension and the question of finding cutoffs that are independent of the size of processes for future research.

We did preliminary experiments~\cite{SimonThesis} by implementing the synthesizer inside our parameterized synthesizer PARTY~\cite{party}.
It is a possible future work to find and apply it to real-world applications.
%
%
%We are working on a prototype implementation (\url{https://bitbucket.org/parsy/guarded_synthesis/}), which however is currently limited to very small systems.\sj{we should re-formulate or remove the comment on implementation}
%In future work, we will try to lift the restrictions of our results for conjunctive systems, and investigate cutoffs that are independent of the size of the components' state spaces.
%\ak{remove this promise?}
\ak{note that EK have better complexities for 'for all paths' properties.
As a future work, one can look if our cutoffs can be improved.}
%This is due
%to the growth of the cutoff (linearly) and the set of possible transition
%guards (doubly exponential) in the size of process templates.
%In the future,
%we will look into cutoffs that are independent of the size of process
%templates.


\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\usepackage{amsfonts}
\hypersetup{colorlinks,urlcolor=blue}

\title{Problem 126}
\author{https://github.com/self-gautrang}
\date{April 2020}

\begin{document}

\maketitle

\section{Question}
\href{https://projecteuler.net/problem=126}{https://projecteuler.net/problem=126}.

\section{Number of blocks of a cuboid being covered by $n$ layers}
\indent Assume we have a cuboid of size a\texttt{x}b\texttt{x}c.
Suppose, for example, $a=3$, $b=1$, $c=3$.
It is challenging to visualize filling the cuboid in a 3D coordinate system, especially for $n \geq 2$.
To address this, we take the cross sections of the cuboid along one coordinate.
In the figure below, we split the cuboid to 3 pieces of size $3 \texttt{x} 1$. \\
\begin{figure}[h]
  \centering
    \includegraphics[width=0.75\textwidth]{images/p126_1.png}
  \caption{At $n=0$, we have the original $3\texttt{x}1\texttt{x}3$ cuboid being split to 3 pieces of size $3 \texttt{x} 1$.
  At $n=1$, for each $3 \texttt{x} 1$ of $n = 0$, we need to cover all of their 4 sides.
  Other than that, we also need to cover the front of the most outer $3 \texttt{x} 1$ of $n = 0$ that facing out of the paper, and to cover the back of the most inner $3 \texttt{x} 1$ of $n = 0$ that facing towards the paper.
  The newly add pieces are illustrated by the green blocks, while the original pieces are the blue blocks.
  At $n=2$, we need to cover the green blocks exposing the white blocks.
  The strategy of covering $n=1$ is the same as what we do to cover $n=0$.
  The newly add pieces are illustrated by the orange blocks.}
\end{figure}\\
\indent From the observations above, we have that, a block of size $a\texttt{x}b$ grows to $(a+2) \texttt{x} (b+2)$ minus $4 \times 1$ white blocks, which turns to $(a+4) \texttt{x} (b+4)$ minus $4 \times (1+2)$ white blocks in the next iteration.
In general, in the $k^{th}$ iteration, the original tiles of size $a \texttt{x} b$ will turn into $(a+2k)(b+2k)-4\sum_{i=0}^{k}i$, or $(a+2k)(b+2k)-2k(k+1)$.
Another observation is that, each iteration will add two new $a\texttt{x}b$ blocks, while other blocks grow. \\
\indent Let $f(a,b,c,n) \rightarrow \mathbb{N}$ be a function that calculates the number of blocks of a cuboid of size $a\texttt{x}b\texttt{x}c$ being covered by $n$ layers.
\begin{align*}
    f(a,b,c,n) &= c[(a+2n)(b+2n) - 2n(n+1)] + 2\sum_{i=0}^{n-1}[(a+2i)(b+2i)-2i(i+1)]
\end{align*}
as at iteration $n$, we have $c$ cross-sections grow from the original cuboid, as other 2 per iteration.
\section{Number of blocks needed to transform a cuboid of $n-1$ layers into $n$ layers}
\indent Let $g(a,b,c,n) \rightarrow \mathbb{N}$ be a function that calculates the number of blocks needed to transform a cuboid of size $a\texttt{x}b\texttt{x}c$ being covered by $n-1$ layers into the one covered by $n$ layers.
\begin{align*}
    g(a,b,c,n) &=f(a,b,c,n) - f(a,b,c,n-1)\\
    &=c(a+2n)(b+2n)-c(a+2n-2)(b+2n-2) \\
    & \texttt{\thinspace\thinspace\thinspace\thinspace\thinspace}- 2cn(n+1) + 2c(n-1)n + 2(a+2n-2)(b+2n-2)-4(n-1)n \\
    &=2ab + 2ac + 4an - 4a + 2bc + 4bn - 4b + 4cn - 4c + 4n^2 - 12 n + 8\\
    &=2(ab+ac+bc)+4(n-1)(a+b+c)+4(n-1)(n-2)
\end{align*}

\end{document}


\section{Phase Quality and Path-Dependent Unwrapping}

\subsection{The Itoh Phase Unwrapping Algorithm}

\code{itk::ItohPhaseUnwrappingImageFilter} (defined in \code{itkItohPhaseUnwrappingImageFilter.h}) implements the simplest possible phase unwrapping algorithm, proposed by Itoh \cite{Itoh1982}.  In this algorithm, the image is traversed linearly, once per image dimension, unwrapping each pixel relative to the previous pixel.  In the case of the uncorrupted, wrapped phase ramp (Figure~\ref{fig:Simulated_Phase_Examples}, far left), the correct unwrapped image (Figure~\ref{fig:Simulated_Phase_Examples}, center left) is obtained with one traversal along the x direction (identical to input, not shown).  Additionally traversing along the y direction has no effect.  This is true regardless of which dimension is traversed first.

However, in the case of the noise-corrupted phase (Figure~\ref{fig:Itoh}), two notable observations should be made.  First, regardless of which direction is traversed first, the original phase image is not recovered.  Rather, the noisy region causes streaks to form `downstream' of the corrupted data.  Therefore, the Itoh algorithm is inadequate for unwrapping of this (and virtually all real world) data sets.  Second,  vertical followed by horizontal traversal does not produce the same image as the reverse.  This suggests that, whereas the unwrapping of some images (e.g., the uncorrupted phase ramp) is path-independent, the unwrapping of other images (e.g., the noisy-corrupted phase) is path-dependent.

\begin{figure}[h]
\center

\includegraphics[width=0.24\textwidth]{images/2/00c_noise_wrapped.png}
\includegraphics[width=0.24\textwidth]{images/3/01a_noise_xy.png}
\includegraphics[width=0.24\textwidth]{images/3/01b_noise_yx.png}

\itkcaption[Itoh Phase Unwrapping Algorithm]{Noise-corrupted phase example ramps, unwrapped via the Itoh algorithm (\code{itk::ItohPhaseUnwrappingImageFilter}) in the horizontal then vertical directions (center) and in the vertical then horizontal directions (right).  The wrapped image (left) is reproduced for comparison.}
\label{fig:Itoh}
\end{figure}

\subsection{Phase Residues}

In order to predict when path-independent phase unwrapping is possible, it is necessary to introduce the concept of phase residues.  A residue charge is calculated for a 2x2 neighborhood of pixels and a given pair of dimensions, by summing the wrapped phase differences across a clockwise trajectory through the neighborhood and dividing the result by $2\pi$.\footnote{We here use the clockwise convention, as presented by Goldstein, et al \cite{Goldstein1988}.  However, as it is only the relative sign that is important for phase unwrapping, the clockwise/counter-clockwise convention makes no practical difference, so long as consistency is maintained.}  Effectively, this gives the number of positive discontinuities encountered less the number of negative discontinuities encountered (positive meaning that $2\pi$ would be added in order to remove it).

It has been proven that all phase residues have a value in the set $\left \{ -1, 0, 1\right \}$ \cite{Bone1991}.  Take the following minimal example (Figure~\ref{fig:Phase_Residues}):\footnote{For simplicity of calculation, the phase has been divided by $2\pi$ such that the range is from $(-0.5, 0.5]$ and the phase residues do not need to be rescaled.}

\begin{figure}[h]
\center
$\begin{matrix}

0.0 &  \rightarrow  & 0.3  &  \rightarrow & 0.3  &  \rightarrow  & 0.0 \\
\uparrow &  \odot  & \downarrow \uparrow &  & \downarrow \uparrow & \otimes & \downarrow \\
0.0 & \leftarrow   & -0.3 & \leftarrow  & -0.3 &  \leftarrow  & 0.0 \\

\end{matrix}$

\itkcaption[Phase_Residues]{Phase Residues.  Phase residues are calculated by summing the wrapped phase differences in a clockwise trajectory through a 2x2 neighborhood.  A positive residue ($\odot$) is noted in the leftmost neighborhood, a negative residue ($\otimes$) in the rightmost neighborhood, and no residue in the central neighborhood. }
\label{fig:Phase_Residues}
\end{figure}

The presence of one or more phase residues in a phase image denote path-dependent unwrapping.  In the above example, unwrapping along $(0,0) \rightarrow (1,0) \rightarrow (1,1)$ produces a value of $0.7$ for pixel $(1,1)$ ($-0.3 + 1.0 = 0.7)$, whereas unwrapping from $(0,0) \rightarrow (0,1) \rightarrow (1,1)$ produces a value of $-0.3$ because no phase wraps are encountered.\footnote{In accordance with ITK convention, the origin is taken to be the upper left corner of the image, and zero-indexing is used.}  Another useful result is that the wrapped line integral around a larger neighborhood yields the sum of the phase residues contained within that neighborhood.  In this example, the line integral is 0, which rightly reflects the presence of a single positive and a single negative residue.  Goldstein, et al \cite{Goldstein1988}, were the first to appreciate the significance of phase residues for path-dependency of phase unwrapping, by introducing the concept of residue `balancing' for 2D phase unwrapping.  They observed that, by defining branch cuts across which phase unwrapping was disallowed (either between residues of opposing sign or from a residue to the edge of the image), a subset of paths could be defined for which the wrapped line integral was zero (i.e., a subset of paths, none of which enclose an unbalanced residue).  Within this subset, any path taken will arrive at an identical unwrapped solution (to an arbitrary additive multiple of $2\pi$).

Appropriately balancing residues will ensure consistent, but not necessarily correct results \cite{Bone1991}.  All possible paths within a specific configuration yield the same result, to an additive multiple of $2\pi$.  However, results are not necessarily consistent between branch-cut configurations.  Figure~\ref{fig:Branch_Cuts} illustrates this point.  Two possible branch cut configurations are shown, each with one possible phase unwrapping path.  It is noteworthy that the values of pixels $(1,1)$ and $(2,1)$ differ between the two configurations.

\begin{figure}[h]
\center

\begin{tabular}{@{}c@{}}

  $\begin{matrix}

0.0 &  \rightarrow  & 0.3  &  \rightarrow & 0.3  &  \rightarrow  & 0.0 \\
 & \odot & \textrm{---} &  & \textrm{---} & \otimes & \downarrow \\
0.0 & \leftarrow   & -0.3 & \leftarrow  & -0.3 &  \leftarrow  & 0.0 \\

\end{matrix}$

\\
\small (a)

\end{tabular}

\vspace{\floatsep}

\begin{tabular}{@{}c@{}}

$\begin{matrix}

0.0 &  \rightarrow  & 0.3  &  & 0.3  &  \rightarrow  & 0.0 \\
\uparrow & \odot & \downarrow &  & \uparrow & \otimes & \downarrow \\
0.0 & |   & 0.7 & \rightarrow  & 0.7 &  |  & 0.0 \\

\end{matrix}$

\\
\small (b)

\end{tabular}

\itkcaption[Branch_Cuts]{Branch Cut Configurations.  The above example demonstrates two possible branch cut configurations.  Configuration (a) connects residues of opposing sign to one another.  Configuration (b) connects each residue to the image border.  For each configuration, one possible unwrapping path is demonstrated.  `---' and `$|$' represent vertical and horizontal branch cuts, respectively; arrows represent the path of phase unwrapping.}
\label{fig:Branch_Cuts}
\end{figure}

It should also be noted that the branch cutting theory so far discussed in this section is specific to two dimensional phase unwrapping.  An extension of the branch cutting concept to three dimensions has been proposed \cite{Huntley2001}, which requires phase residues to be calculated with respect to each pair of dimensions (xy, xz, yz).  To our knowledge, the branch cutting method has not been extended to higher dimensions.  We have included a discussion of residues for completeness, and because we believe that the concepts are essential to the understanding of path dependence.  However, as our interest in this submission is with phase unwrapping filters that may be implimented over images of arbitrary dimension, no branch cutting algorithms have been included.

Phase residues may be calculated using the \code{itk::PhaseResidueImageFilter} class (defined in \code{itkPhaseResidueImageFilter.h} (Figure~\ref{fig:Phase_Residues_Noise_Patch}).  The filter inherits from \code{itk::PhaseImageToImageFilter}, and uses the \code{Wrap()} method to calculate phase residues as described above.  Note that, due to the theoretical limitations described above, the filter only has a clear physical significance for 2D images.

\begin{figure}[h]
\center
\includegraphics[width=0.24\textwidth]{images/3/02a_residue.png}

\itkcaption[Phase_Residues_Noise_Patch]{Phase residues are calculated for the noise-corrupted phase ramp example using the \code{itk::PhaseResidueImageFilter} class.}
\label{fig:Phase_Residues_Noise_Patch}
\end{figure}

\subsection{The Helmholtz Decomposition}

Given a vector field $v$, if there exists some $A$ such that $v = \nabla \times A$, then $A$ is called the `vector potential' of $v$.  Given that the divergence of the curl of any vector field is the zero vector, $v$ must therefore be entirely rotational (i.e., non-conservative):

\begin{equation}
\label{eqn:Div_Curl_Identity}
\nabla \cdot v = \nabla \cdot ( \nabla \times A ) = 0
\end{equation}

Likewise, if there exists some $\psi$ such that $v = \nabla \psi$, then $\psi$ is called the `scalar potential' of $v$.  Given that the curl of the gradient of any scalar field is zero, $v$ must therefore be entirely irrotational (i.e., conservative):

\begin{equation}
\label{eqn:Curl_Grad_Identity}
\nabla \times v = \nabla \times ( \nabla \psi ) = 0
\end{equation}

In the general case, $A$ and $\psi$ cannot be found (that is, the general case of a vector field need not be entirely rotational or irrotational).  However, the Helmholtz Decomposition states that a vector field can be decomposed into rotational and irrotational components as the sum of scalar and vector potentials:

\begin{equation}
\label{eqn:Helmholtz}
v = - \nabla \psi + \nabla \times A
\end{equation}

In the context of phase unwrapping, the Helmholtz decomposition has specific implications for path dependence.  In particular, the `path-dependent' contribution is contained entirely within the rotational component, and the `path-independent' contribution within the irrotational component.  The irrotational and rotational components of the noise-corrupted phase ramp are shown in the left and right panels of Figure~\ref{fig:Helmholtz_Decomposition}.  Note in the irrotational component that corruption of the original phase extends beyond the patch of noise itself, though there are no ambiguities.

\begin{figure}
\center
\includegraphics[width=0.24\textwidth]{images/3/02b_irrotational.png}
\includegraphics[width=0.24\textwidth]{images/3/02c_rotational.png}

\itkcaption[Helmholtz_Decomposition]{The \code{itk:HelmholtzDecompositionImageFilter} classe, applied to the noise-corrupted phase ramp example.  The left panel is the irrotational (curl-free) component, and the right panel is the rotational (divergence-free) component.}
\label{fig:Helmholtz_Decomposition}
\end{figure}

The Helmholtz Decomposition is provided in the \code{itk::HelmholtzDecompositionImageFilter} class (defined in \code{itkHelmholtzDecompositionImageFilter.h}), which inherits from \code{itk::PhaseImageToImagefilter}.  The irrotational and rotational components may be retrieved through the \code{GetRotational()} and \code{GetIrrotational()} methods, respectively.

The implementation of \code{itk::HelmholtzDecompositionImageFilter} takes advantage of the fact that the irrotational component is equivalent to the unweighted, least squares solution (\code{itk::DCTPhaseUnwrappingImageFilter}, discussed in a later section), re-wrapped into the range $(-\pi, \pi]$.  The rotational component may then be obtained by subtracting the irrotational component from the original wrapped phase, and re-wrapping the result.

\subsection{Phase Derivative Variance}

It addition to phase residues, it is also useful to consider continuous measures of phase quality, of which the phase derivative variance is the most commonly used.  Phase derivative variance $\sigma$ in a single dimension $d$ is the sum of squared differences of the local wrapped phase gradients ($\Delta \phi_{i}$) and the mean wrapped phase gradient ($\overline{ \Delta \phi }$, Equation~\ref{eqn:PhaseDerivativeVariance}).

\begin{equation}
\label{eqn:PhaseDerivativeVariance}
\sigma_d = \sum_i{  \left( \Delta \phi_{i} - \overline{ \Delta \phi }     \right)^2  }
\end{equation}

The total phase derivative variance can then be found by dividing the sum of the root variance in each dimension by the square of the one-sided length of the neighborhood $k$ (Equation~\ref{eqn:PhaseDerivativeVarianceTotal}).  Phase derivative variance is a convenient measure of phase quality because it (a) is easily generalized to arbitrary dimension and (b) can be computed entirely from the phase data itself.

\begin{equation}
\label{eqn:PhaseDerivativeVarianceTotal}
\frac{ \sum_d { \sqrt{ \sigma_d } }  }{ k^2 }
\end{equation}

Phase derivative variance is an inverse measure of phase quality; i.e., a large phase derivative variance is indicative of low phase quality.  In order to be used as a quality metric, phase derivative variance is typically rescaled into the range $[0,1]$ and multiplied by $-1$, such that brighter pixels represent higher phase quality.  This is the default behavior of \code{itk::PhaseDerivativeVarianceImageFilter}.  The un-normalized phase derivative variance can be obtained by setting \code{SetNormalize(false)}.  The rescaled phase derivative variance image for the noise-corrupted phase ramp is shown in Figure~\ref{fig:Phase_Derivative_Variance}.

Note that the patch of noise can be clearly identified in the original wrapped image, the phase residue image, the phase derivative variance image, and the rotational component of the Helmholtz decomposition.

\begin{figure}[h]
\center
\fbox{ \includegraphics[width=0.24\textwidth]{images/3/02d_quality.png} }

\itkcaption[Phase_Derivative_Variance]{Phase Derivative Variance.  Rescaled phase derivative variance for the noise-corrupted phase ramp, computed with \code{itk::PhaseDerivativeVarianceImageFilter}.  The patch of noise can be clearly identified as a region of low image quality.  Note that a thin black border has been added in order to allow visualization of the edge of the image.}
\label{fig:Phase_Derivative_Variance}
\end{figure}


\documentclass[a4paper,12pt,twocolumn]{article}

\setlength{\voffset}{-25.4mm}
\setlength{\hoffset}{-25.4mm}

\setlength{\topmargin}{20mm}
\setlength{\oddsidemargin}{20mm}
\setlength{\evensidemargin}{20mm}
\setlength{\textheight}{25.7cm}
\setlength{\textwidth}{17cm}

\begin{document}
\author{Stuart Midgley}
\title{Introduction to the \LaTeX\ article class}
\maketitle
\begin{abstract}
This is a demonstration article written by Stuart Midgley for the Information Literacy Program (ILP).  This article is broken into three parts.  The first will guide the beginner Latex user through basic equations, layout and other useful information.

The second section will introduce the Latex user to very simple Latex concepts such as font sizes and other rather useful features.  While the third section will pull the whole document together.
\end{abstract}
\section{Introduction}
\LaTeX\ is a powerful document production system used by scientists and writers the world over.  Its beautiful typeset equations are the envy of all other word processors and typesetting programs.

For example, consider the interaction between two charged particles,
\begin{equation}
\vec{F} = \frac{q_{1} q_{2}}{4 \pi \epsilon_{0}{r}^{3}}\vec{r}
\end{equation}
where $\vec{r}$ is the position vector between the two particles.  This equation can be expanded to give the force on $n$ particles quite easily
\begin{equation}
\vec{F}_{j} = \sum_{i=1}^{n} \left( \frac{q_{j} q_{i}}{4 \pi \epsilon_{0} r_{ij}^{3}} \vec{r}_{ij} \right)
\end{equation}

You can even enter a multi line equation
\begin{eqnarray}
\phi_{n+1}(\eta) &  = & 2 x \phi_{n}(\eta)  + \phi_{n-1}(\eta)  \\
\phi_{1}(\eta)  & = & \eta  \\
\phi_{0}(\eta)  & = & 1
\end{eqnarray}
or and inline equation $\phi_{n+1}(\eta) = 2 x \phi_{n}(\eta)  + \phi_{n-1}(\eta)$ which is exciting.

Of course you may want to use specific symbols \dag\ \ddag\ \S\ \P\ \oe\ \`{o} \^{o} \u{o} \t{oo} \b{o} just to make the document look a bit snazzy.

Its also worth noting how well \LaTeX\ jusifies your document.  It always looks good.

\section{Middle}
More importantly, you may want to \textit{highlight} some of your text with nice ``quotes'' and {\Large \textbf{a bigger bolder font}} all of which \LaTeX\ supports.  Of course, \LaTeX\ doesn't help with the grammar or spelling.  Your text editor needs to handle that.

Another important feature of \LaTeX\ is the class file.  The class file specifies everything about a document and ensures a consistent look and feel to your document all the time.  It has often been said, that no document looks as good as a \LaTeX\ document.  The fonts always look good, the document just looks professional.  See if you can make a document looks this good in Word.

\section{Conclusion}
Not much to put here.  Except, that a document written in Latex is very portable.  You should be able to email your text file to anyone in the world and they can recreate your document perfectly, regardless of platform.  Also, since the document is just a text file it is nice and small.
\end{document}


% \documentclass[11pt]{article}
% \input{/Users/jovo/Research/other/latex/latex_document}
%
% \title{Response to Review of TPAMI-2011-11-0845}
% \author{Vogelstein et al.}
%
% \begin{document}
% \maketitle

\onecolumn
\setcounter{page}{1}
\section*{Response to Reviewers} % (fold)
\label{sec:response_to_reviewers}

We would like to thank the two helpful reviewers and the editor for their insightful comments.  We have extensively revised the manuscript in light of the comments we received.  Below are a few general remarks about the current revision, followed by specific responses to reviewer comments (reviewer comments are in \textbf{bold}):

\begin{itemize}
	\item  We have significantly revised the text to clarify that we have devised a fast and \emph{approximate} algorithm for solving quadratic assignment problems (QAP).  That graph matching can be cast as a QAP is useful because it ties our algorithm into a greater literature on QAP.	We were motivated to derive an approximate solution because our application of interest has between hundreds and billions of vertices, so exact algorithms are completely out of the question.  We hope that the text of this version of the manuscript conveys that sentiment clearly.  In fact, although our algorithm seems to be the best cubic-time approximate algorithm for solving graph matching problems, it is not nearly fast enough.  The incoming massive brain-graph data will hopefully motivate others to further improve on our work, which we view merely as a starting point.
	\item Our algorithm now fundamentally has an initial starting position that is fixed at the barycenter of the feasible region.  This is despite that the problem we seek to solve is non-convex, and we do not convexify it.  However, by starting at a sensible initial condition (the barycenter of the feasible region), we found that our algorithm outperforms the previous state of the art on 29 of the 32 standard benchmarks, as well as our brain-graph matching problem of interest.
	\item Submission of this manuscript to TPAMI was motivated by our reading of the very nice paper of Zaslavskiy et al. \cite{Zaslavskiy2009}, which was recently published in TPAMI.  Zaslavskiy developed a novel PATH following algorithm, that was demonstrated to exceed the previous state of the art on 14 of 16 benchmark datasets selected from the QAP library \cite{Burkard1997}.  Although the authors of that manuscript did not explain their choice of 16, it is the exact same 16 used in a previous comparison of various approximate graph matching algorithms \cite{Schellewald2001}. Moreover, in \cite{Schellewald2001}, the authors explain that their choice of those 16 of the $>$100 datasets was based on those being ``particularly difficult''.   Our algorithm outperformed the PATH algorithm on 13 of the 16 datasets (always starting at the barycenter).  If we elect to use only 2 or 3 restarts, which we can only fruitfully do because our objective function is non-convex, we outperform all previous algorithms on all datasets we considered.
	\item Another paper was published in TPAMI while our manuscript was under review \cite{Liu2012}.  This paper extended the PATH paper to deal with directed graphs, developing an algorithm called \texttt{EPATH}.  The authors of this paper consider 16 directed benchmarks from the QAP library, and demonstrated improve performance on 15 of 16 benchmarks.  We applied our \FAQ algorithm to those same benchmarks, again always starting at the barycenter, and did at least as well or better than all previous algorithms on all 16 benchmarks.
	\item Below, we have responded in detail to the reviewers helpful comments. Reviewer comments are in bold, responses are plain text, quotes from the main text are in red.
\end{itemize}

% We respond to each specific comment below.

\newpage
\subsection*{Reviewer 1}

\begin{itemize}
	\item \textbf{1) A principle flaw:
	The suggested approach samples an generally NP-problem at n=1,3 or 100
	random points and search for a local minima in
	the usually highly non-convex function (the relaxed QAP).
	So, for increasing problem size the probability
	to obtain a good solution drops drastically.
	}

	In the original draft of this manuscript, we overemphasized the multiple restart aspect of this work.  In fact, our algorithm outperforms the previous state of the art cubic-time QAP algorithms on 13 of the 16 benchmarks. We have substantially revised the text to reflect this.  Specifically, in the first step of Section \ref{sec:faq}, we now write:

	\tr{\textbf{I: Find a suitable initial position.}  While any doubly stochastic matrix would be a feasible initial point, we choose the
		% two choices seem natural: (i) the
		``flat doubly  stochastic matrix,'' $J=\ve{1} \cdot \ve{1}\T/n$, which is the barycenter of the feasible region.}

		We have added a subsection in the results to merely remark that multiple restarts are a possibility (Section \ref{sub:multiple_restarts}). %merely commenting on the multiple restart option in a subsection. %We regrettably failed to clarify in the results that performance on the brain-graph matching task was repeated 10 times to demonstrate the repeatability of these results.

	\item \textbf{2) Insufficient evaluation:
	A report on statistical properties of the approach
	would make an evaluation of the method possible, i.e, how often
	is the best solution found by the sampling depending on the
	the problem size.
	However such information is not provided in the report.
	}

	\FAQ now always starts with the barycenter of the feasible region. Given this starting position, \FAQ outperforms the previous state-of-the-art algorithm on 29 of the 32 benchmarks considered; the particular benchmarks considered were the exact same ones used by the two most recent publications in TPAMI on graph matching \cite{Zaslavskiy2009,Liu2012}

	\item \textbf{3) Too few experiments:
	The authors have performed 16 experiments on
	the qaplib dataset which has in its standard form
	more than 100 problem instances.
	}

	We have now buttressed our results with another 16 benchmarks, the exact same benchmarks as those used in reference \cite{Liu2012} (see Section \ref{sub:directed}).  Moreover, the 16 that we used were exactly the 16 datasets used in recent TPAMI publication on the PATH algorithm for graph matching reference \cite{Zaslavskiy2009}.  Although the authors of the PATH paper did not explain their choice of 16, they are the same 16 used in reference \cite{Schellewald2001}, in which several different graph matching algorithms were compared.  In reference \cite{Schellewald2001}, it was explained that these 16 are ``particularly difficult''.  Section \ref{sub:undirected} elaborates on this in the text:

	\tr{
Specifically, \texttt{PATH} was compared to the Quadratic Programming Bound approach (\texttt{QGP}) of \cite{Anstreicher2001}, the graduated assignment algorithm (\texttt{GRAD}) of \cite{Gold1996}, and Umeyama's algorithm (\texttt{U}) \cite{Umeyama1988}.  Because either \texttt{PATH} or \texttt{QBP} outperformed \texttt{GRAD} and \texttt{U} on every dataset, Table \ref{tab:1} shows the performance of \FAQ versus \texttt{PATH} and \texttt{QBP}.
	}

	\item \textbf{Developments reported for example in
``N. Brixius and K. Anstreicher. Solving quadratic assignment problems
using convex quadratic programing relaxations. Optimization Methods
and Software.'' are completely ignored.
}

Our algorithm, like PATH, outperformed all the algorithms considered in the above mentioned citation.  We have now clarified this in Section \ref{sub:directed}:

	\tr{
\FAQ outperforms both of the previous state-of-the-art inexact cubic algorithms on 13 out of 16 benchmarks.  Figure \ref{fig:path16} presents the same data graphically. The top panel compares both \FAQ and \texttt{PSOA}---which is the minimum of the previous state-of-the-art (either \texttt{PATH} or \texttt{QBP} here)---to the absolute minimum; \FAQ get closer than \texttt{PSOA} to the minimum on 13 of 16. The bottom panel shows the ratio of \FAQ to \texttt{PSOA}. }

	\item \textbf{On a different level it seems a bit unrealistic to assume that all the
graphs they wish to compare are of equal size, however
it is not discussed or even mentioned why the authors have
reason to assume that the graphs are equally sized.}

Indeed, although for some brain-graphs the number of vertices will be the same, for many, they will not be.  We have modified Section \ref{sec:discussion} accordingly:

 	\tr{
Second, for many brain-graph matching problems, the number of vertices will not be the same across the brains.  Recent work from \cite{Zaslavskiy2009, Zaslavskiy2010} and \cite{Escolano2011} suggest that extensions in this direction would be both relatively straightforward and effective.
	}

	\item \textbf{The authors report a good performance on a quite large graph with
302 nodes, but do not indicate how the graph looks like. That
makes it difficult to speculate about reasons that make this problem
solvable by just sampling at a few positions followed by the Frank-Wolfe
algorithm. It might be that the structure of the problem is
more easy (Can the assignment be found by a spectral analysis of the
adjacency matrix?)
}

We have clarified Section \ref{sub:connectome_classification} to point out extensive analyses of the brain-graphs by reference \cite{Varshney2011}:

	\tr{
The properties of these connectomes are analyzed in \cite{Varshney2011}; a cursory evaluation of the properties of these graphs does not suggest to us why the chemical connectome was so much easier to graph match than the electrical one. 	}

	\item \textbf{minor comments: (The line numbers at the beginning refer to both columns...)
}

Thank you, we have corrected all the minor comments.

\end{itemize}

\newpage
\subsection*{Reviewer 2}
\label{sec:reviewer_2}

\begin{itemize}
	\item \textbf{Though this is a proposal of a QAP inexact solver, it is in principle limited to graphs of the same size.}

We have now mentioned the possibility of extending this work to deal with different numbers of vertices, and have modified the Section \ref{sec:discussion} accordingly:

	 	\tr{
Second, for many brain-graph matching problems, the number of vertices will not be the same across the brains.  Recent work from \cite{Zaslavskiy2009, Zaslavskiy2010} and \cite{Escolano2011} suggest that extensions in this direction would be both relatively straightforward and effective.	}

	\item \textbf{Even in this case, there is no reference neither experimental nor theoretical to the GA (Graduated Assignment) Algorithm (Gold and Rangarajan, PAMI'1996). }

We have now clarified in Section \ref{sub:undirected} that our algorithm outperforms the GRAD algorithm on all benchmarks tested:

	\tr{
Specifically, \texttt{PATH} was compared to the Quadratic Programming Bound approach (\texttt{QGP}) of \cite{Anstreicher2001}, the graduated assignment algorithm (\texttt{GRAD}) of \cite{Gold1996}, and Umeyama's algorithm (\texttt{U}) \cite{Umeyama1988}.  Because either \texttt{PATH} or \texttt{QBP} outperformed \texttt{GRAD} and \texttt{U} on every dataset, Table \ref{tab:1} shows the performance of \FAQ versus \texttt{PATH} and \texttt{QBP}. 	}

% \tr{
% Because either \texttt{PATH} or \texttt{QBP} outperformed \texttt{GRAD} and \texttt{U} on every dataset, Table 1 shows the performance of \FAQ versus \texttt{PATH} and \texttt{QBP}.
% }

	\item \textbf{This is quite simplistic when comparing to the power of deterministic annealing implicit in GA to avoid local minima. A serious comparison between both schemes should be desirable.}

Because our algorithm outperformed a previously published algorithm in TPAMI which outperformed GA (the PATH algorithm), this seemed unnecessary.

	\item \textbf{In [25] it is proposed an algorithm outperforming QBP. The method proposed here is quite similar to that in [25] (which is more focused on labeled graphs but based on FW) so I judge the proposal quite incremental.}

Indeed, our algorithm is similar to the \texttt{PATH} algorithm of reference [25] of the original, \cite{Zaslavskiy2009} in the revision.  Yet, \FAQ outperforms \texttt{PATH} on 13 of 16 benchmarks, which we believe to be significant.  Moreover, our algorithm also addressed directed graphs, while \texttt{PATH} only deals with the undirected case.  Reference  \cite{Liu2012} extends \texttt{PATH} for directed graphs, and our algorithm outperforms their \texttt{EPATH} algorithm (as well as \texttt{GRAD}, \texttt{QCV}, and \texttt{U}) on all 16 benchmarks.

	\item \textbf{See similar algorithm in ``Many-to-Many Graph Matching: a Continuous Relaxation Approach'' in 2010 (arxiv).}

Thank you for pointing out this reference, as we had not yet seen it.  Indeed, this manuscript which is contemporaneous with ours proposes a similar algorithm to \texttt{FAQ}.  However, they only consider many-to-one and many-to-many problems. Moreover, our brain-graph matching problem is unique, and motivates the development of algorithms with significantly better scaling rules.  We have modified Section \ref{sec:discussion} accordingly:

\tr{
Second, for many brain-graph matching problems, the number of vertices will not be the same across the brains.  Recent work from \cite{Zaslavskiy2009, Zaslavskiy2010} and \cite{Escolano2011} suggest that extensions in this direction would be both relatively straightforward and effective.	}

	\item \textbf{Anyway in the experiments only 16 tests where done and the analysis was focuses on the number of restarts. The proposed algorithms require 1-100 starts where in 12 of 16 it only requires 1 start (the van der Waerden matrix).}

As mentioned above, the 16 tests performed are identical to the 16 tests performed by the previous state of the art, reference [25] in the original submission (the \texttt{PATH} algorithm).  And those 16 are the same 16 as used in a previous publication testing various approximate algorithms (reference [30] in the new submission).  Moreover, the authors of [30] state that those 16 were chosen because they were ``particularly difficult''.  We have significantly rewritten the text to emphasize that our algorithm fundamentally is initialized in the barycenter of the feasible region.  A secondary point in the new submission remarks that random restarts can yield superior performance, time permitting. Section elaborates on this in the text:

	\tr{Specifically, \cite{Zaslavskiy2009} created a path following algorithm (\texttt{PATH}) based on a convex and concave relaxation of QAP.  In that manuscript, the authors considered 16 datasets from the QAPLIB benchmark, the same 16 datasets as were used in \cite{Schellewald2001}, which are known to be ``particularly difficult''.	}

	Moreover, we have now added another 16 benchmarks, again, the exact same benchmarks used in a previous TPAMI publication, reference \cite{Liu2012}, to evaluate performance of \FAQ on directed graphs.   \FAQ achieves a superior objective value function for all 16 benchmarks than all previous state-of-the-art algorithms, as described in Section \ref{sub:directed}.

	\item \textbf{No comments on the recently embedding methods for graph matching: see for instance: ``Graph matching through entropic manifold alignment. CVPR 2011''.
Indeed only same-size graphs seem to be explored.}

Thank you for pointing out this omission, we have rectified the text to refer to this work as mentioned above.

\end{itemize}

% section reviewer_2 (end)

% \end{document}


\documentclass{article}

\usepackage[preprint]{./template/neurips_2020}

\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc}    % use 8-bit T1 fonts
\usepackage{hyperref}       % hyperlinks
\usepackage{url}            % simple URL typesetting
\usepackage{booktabs}       % professional-quality tables
\usepackage{amsfonts}       % blackboard math symbols
\usepackage{nicefrac}       % compact symbols for 1/2, etc.
\usepackage{microtype}      % microtypography
\usepackage{amssymb}
\usepackage{physics}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{quantikz}

\title{Chapter 4: Quantum Circuits}

\author{
  John Martinez \\
  \texttt{john.r.martinez14@gmail.com} \\
}

\begin{document}
\maketitle

\section{Quantum Circuits}

%%% Section 4.1
\subsection{Quantum Algorithms}
What is a quantum computer good for? The promise of quantum computers is to
enable new algorithms which render feasible problems requiring exorbiant
resources for their solution on a classical computer.

%%% Section 4.2
\subsection{Single Qubit Operations}
Useful gates not mentioned yet:

  \begin{center}
    $
      T = \begin{bmatrix}
        1 & 0 \\
        0 & e^{i\pi / 4}
      \end{bmatrix};\mspace{10mu}
      S = \begin{bmatrix}
        1 & 0 \\
        0 & i
      \end{bmatrix}
    $
  \end{center}

A few useful algebraic facts to keep in mind are that
$H = \frac{(X + Z)}{\sqrt{2}}$ and $S = T^{2}$

The Pauli matrices give rise to three useful classes of unitary matrices when
they are exponentiated, the \emph{rotation operators} about the $\hat{x}$,
$\hat{y}$, and $\hat{z}$ axes, defined by the equations:

  \begin{center}
    $R_{x}(\theta) \equiv e^{-i\theta X / 2} =
    \begin{bmatrix}
      \cos{\frac{\theta}{2}} & -i\sin{\frac{\theta}{2}} \\
      -i\sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}}
    \end{bmatrix}
    $
  \end{center}

  \begin{center}
    $R_{y}(\theta) \equiv e^{-i\theta Y / 2} =
    \begin{bmatrix}
      \cos{\frac{\theta}{2}} & -\sin{\frac{\theta}{2}} \\
      \sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}}
    \end{bmatrix}
    $
  \end{center}

  \begin{center}
    $R_{z}(\theta) \equiv e^{-i\theta Z / 2} =
    \begin{bmatrix}
      e^{-i\theta / 2} & 0 \\
      0 & e^{i\theta / 2}
    \end{bmatrix}
    $
  \end{center}

\emph{Theorem 4.1}:

Suppose $U$ is a unitary operation on a single qubit.
Then $\exists \alpha, \beta, \gamma, \delta \in \mathbb{R}$
such that
  \begin{center}
    $U = e^{i\alpha}R_{z}(\beta)R_{y}(\gamma)R_{z}(\delta)$.
  \end{center}

\emph{Proof}

Since $U$ is unitary, the rows and columns of $U$ are orthonomal, from which it
follow that $\exists \alpha, \beta, \gamma, \delta \in \mathbb{R}$ such that
\begin{center}
  $U = \begin{bmatrix}
  e^{i(\alpha-\beta/2-\delta/2)}\cos{(\gamma/2)} &
  -e^{i(\alpha-\beta/2+\delta/2)}\sin{(\gamma/2)} \\
  e^{i(\alpha+\beta/2-\delta/2)}\sin{(\gamma/2)} &
  e^{i(\alpha+\beta/2+\delta/2)}\cos{(\gamma/2)}
  \end{bmatrix}$
\end{center}

The above equation follows immediately from the definition of the rotational
matrices.

$\blacksquare$

%%% Section 4.3
\subsection{Controlled Operations}
"If $A$ is true, then do $B$". This is an example of a
\emph{controlled operation}.

A prototypical controlled operation is the controlled-$NOT$. The action of
the $CNOT$ is given by $\ket{c}\ket{t} \rightarrow \ket{c}\ket{t \oplus c}$.
The matrix representation of $CNOT$ is

\begin{center}
  $\begin{bmatrix}
    1 & 0 & 0 & 0 \\
    0 & 1 & 0 & 0 \\
    0 & 0 & 0 & 1 \\
    0 & 0 & 1 & 0
  \end{bmatrix}$
\end{center}

\begin{center}
  \begin{quantikz}
    \ket{c} & \qw & \ctrl{1} & \qw \\
    \ket{t} & \qw & \targ{} & \qw
  \end{quantikz}
\end{center}

Suppose $U$ is an arbitrary single qubit unitary operation. A
\emph{controlled-U} operation is a two qubit operation. That is,
$\ket{c}\ket{t} \rightarrow \ket{c}U^{c}\ket{t}$

\begin{center}
  \begin{quantikz}
    \ket{c} & \qw & \ctrl{1} & \qw \\
    \ket{t} & \qw & \gate{U} & \qw
  \end{quantikz}
\end{center}

Our immediate goal is to understand how to implement the controlled-\emph{U}
operation for arbitrary single qubit $U$, using only single qubit operations
and the \emph{CNOT} gate. Our strategy is a two-part procedure based upon the
decomposition $U = e^{i\alpha}AXBXC$ given in Corollary 4.2 on page 176.

\emph{Corollary 4.2}: Suppose $U$ is a unitary gate on a single qubit. The there
exists unitary operators, $A$, $B$, $C$ on a single qubit such that $ABC = I$
and $U = e^{i\alpha}AXBXC$, where $\alpha$ is some overall phase factor.

\begin{center}
\begin{quantikz}[align equals at=1.5,column sep=0.3cm]
  \qw & \ctrl{1} & \ghost{\begin{bmatrix}
      1 & 0 \\
      0 & e^{i\alpha}
    \end{bmatrix}} \qw \\
  \qw & \gate{U} & \qw
\end{quantikz}{=}\begin{quantikz}[align equals at=1.5,column sep=0.3cm]
  \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \gate{
    \begin{bmatrix}
      1 & 0 \\
      0 & e^{i\alpha}
    \end{bmatrix}
  } & \qw \\
  \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw
\end{quantikz}
\end{center}

Toffoli Gate
\begin{center}
\begin{quantikz}[align equals at=2.0]
  \qw & \ctrl{1} & \ghost{H} \qw \\
  \qw & \ctrl{1} & \ghost{H^{\dagger}} \qw \\
  \qw & \targ{}  & \ghost{H^{\dagger}} \qw
\end{quantikz}{=}
\begin{quantikz}[align equals at=2.0,column sep=0.2cm]  %,row sep=0.2cm]
  \qw & \qw & \qw & \qw & \ctrl{2} & \qw & \qw & \qw & \ctrl{2} & \qw
      & \ctrl{1} & \qw & \ctrl{1} & \gate{T} \\
  \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \ctrl{1} & \qw & \qw
      & \gate{T^{\dagger}} & \targ{} & \gate{T^{\dagger}} & \targ{}
      & \gate{S} \\
  \qw & \gate{H} & \targ{} & \gate{T^{\dagger}} & \targ{} & \gate{T} & \targ{}
      & \gate{T^{\dagger}} & \targ{} & \gate{T} & \gate{H} & \qw & \qw
      & \qw
\end{quantikz}
\end{center}

Network implementation of the $C^{n}(U)$ operation for the case $n = 5$.

\begin{center}
\begin{quantikz}
\lstick[wires=5]{$control$}
  \ket{c_1} & \ctrl{1} & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \qw \\
  \ket{c_2} & \ctrl{4} & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{4} & \qw \\
  \ket{c_3} & \qw & \ctrl{3} & \qw & \qw & \qw & \qw & \qw & \ctrl{3} & \qw & \qw \\
  \ket{c_4} & \qw & \qw & \ctrl{3} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw & \qw \\
  \ket{c_5} & \qw & \qw & \qw & \ctrl{3} & \qw & \ctrl{3} & \qw & \qw & \qw & \qw \\
\lstick[wires=4]{$work$}
  \ket{0} & \targ{} & \ctrl{1} & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \targ{} & \qw \\
  \ket{0} & \qw & \targ{} & \ctrl{1} & \qw & \qw & \qw & \ctrl{1} & \targ{} & \qw & \qw \\
  \ket{0} & \qw & \qw & \targ{} & \ctrl{1} & \qw & \ctrl{1} & \targ{} & \qw & \qw & \qw \\
  \ket{0} & \qw & \qw & \qw & \targ{} & \ctrl{1} & \targ{} & \qw & \qw & \qw & \qw \\
\lstick[wires=1]{$target$}
  \qw & \qw & \qw & \qw & \qw & \gate{U} & \qw & \qw & \qw & \qw & \qw
\end{quantikz}
\end{center}

%%% Section 4.4
\subsection{Measurement}
There are two important principles that it is worth bearing in mind about
quantum circuits.

\textbf{Principle of deferred measurement}: Measurements can always be moved
from an intermediate stage of a quantum circuit to the end of the circuit;
if the measurement results are used at any stage of the circuit, then the
classically controlled operations can be replaced by conditional quantum
operations.

\emph{Quantum Teleportation circuit}
\begin{center}
\begin{quantikz}
  \ket{\psi} & \ctrl{1} & \gate{H} & \qw & \qw & \ctrl{2} & \meter{} \\
\lstick[wires=2]{$\ket{\beta_{00}}$}
  \qw & \targ{} & \qw & \qw & \ctrl{1} & \qw & \meter{} \\
  \qw & \qw & \qw & \qw & \gate{X} & \gate{Z} & \qw\rstick[wires=1]{$\ket{\psi}$}
\end{quantikz}
\end{center}

\textbf{Principle of implicit measurement}: Without loss of generality,
any unterminated quantum wires (quibits which are not measured) at the
end of a quantum circuit may be assumed to be measured.

%%% Section 4.5
\subsection{Universal Quantum Gates}
\subsubsection{Two-Level Unitary Gates are Universal}
Consider a Unitary matrix $U$ which acts on a $d$-dimensional Hilbert space.
$U$ may be decomposed into a product of \emph{two-level unitary matrices}.
Suppose $U$ has the form

\begin{center}
  $
    U = \begin{bmatrix}
      a & d & g \\
      b & e & h \\
      c & f & j
    \end{bmatrix}
  $
\end{center}

We will find two-level unitary matrices $U_1,\dots,U_3$ such that
\begin{center}$U_3 U_2 U_1 U = I.$\end{center}

It follows that
\begin{center}$U = U_{1}^{\dagger}U_{2}^{\dagger}U_{3}^{\dagger}.$\end{center}

$U_1$, $U_2$, and $U_3$ are all two-level unitary matrices, and it is easy to
see that their inverses, $U_{1}^{\dagger}$, $U_{2}^{\dagger}$, and
$U_{3}^{\dagger}$ are also two-level unitary matrices.

\subsubsection{Single Qubit and $CNOT$ Gates are Universal}
\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\title{Open Source Software Lab 3 Part 1}
\author{Colton Zecca}
\date{February 15, 2021}

\begin{document}
\maketitle

\section{Formula A}
$\sqrt{1+2 \sqrt{1+3 \sqrt{1+4 \sqrt{1+\cdots}}}}$

\section{Formula B}
$\sqrt{2}^{\sqrt{2}^{\sqrt{2}^{\sqrt{2}\cdots}}}$

\section{Hadamard Matrix of Size 4}

$$H_4=\begin{bmatrix}
1 & 1 & 1 & 1 \\
-1 & 1 & -1 & 1 \\
-1 & -1 & 1 & 1 \\
1 & -1 & -1 & 1
\end{bmatrix}$$

\end{document}


\subsection{Overflows and unchecked arithmetic}
\label{sect:overflows}
Note: this section can be skipped on first reading.

Consider the C expression \vcc{a+b}, when \vcc{a} and \vcc{b} are,
say, \vcc{unsigned int}s. This might represent one of two programmer
intentions. Most of the time, it is intended to mean ordinary
arithmetic addition on numbers; program correctness is then likely to
depend on this addition not causing an overflow. However, sometimes
the program is designed to cope with overflow, so the programmer means
\vcc{(a + b) % UINT_MAX+1}.
It is always sound to use this second interpretation, but VCC
nevertheless assumes the first by default, for several reasons:
\begin{itemize}
\item The first interpretation is much more common.
\item The second interpretation introduces an implicit \vcc{%}
  operator, turning linear arithmetic into nonlinear arithmetic and
  making subsequent reasoning much more difficult.
\item If the first interpretation is intended but the addition can in
  fact overflow, this potential error will only manifest later in the
  code, making the source of the error harder to track down.
\end{itemize}

Here is an example where the second interpretation is intended, but
VCC complains because it assumes the first:
\vccInput[linerange={begin-}]{c/4.1.hash_fail.c}

\noindent
VCC complains that the hash-computing operation might overflow.
To indicate that this possible overflow behavior is desired we use \vcc{_(unchecked)},
with syntax similar to a regular C type-cast.
This annotation applies to the following expression, and indicates that
you expect that there might be overflows in there.
Thus, replacing the body of the loop with the following
makes the program verify:

\vccInput[linerange={update-endupdate}]{c/4.2.hash.c}

Note that ``unchecked'' does not mean ``unsafe''.
The C standard mandates the second interpretation for unsigned overflows,
and signed overflows are usually implementation-defined to use two-complement.
It just means that VCC will loose information about the operation.
For example consider:
\begin{VCC}
int a, b;
// ...
a = b + 1;
_(assert a < b)
\end{VCC}
This will either complain about possible overflow of \vcc{b + 1} or succeed.
However, the following might complain about \vcc{a < b}, if VCC does not know
that \vcc{b + 1} doesn't overflow.
\begin{VCC}
int a, b;
// ...
a = _(unchecked)(b + 1);
_(assert a < b)
\end{VCC}
Think of \vcc{_(unchecked)E} as computing the expression using mathematical
integers, which never overflow, and then casting the result to the desired range.
VCC knows that
\begin{itemize}
\item if the value of \vcc{E} lies within the range of its type, then
\vcc{E == _(unchecked) E};
\item if \vcc{x} and \vcc{y} are \vcc{unsigned}, then
\vcc{_(unchecked)(x+y) == (x+y <= UINT_MAX ? x+y : x + y - UINT_MAX)},
and similarly for other types;
\item if \vcc{x} and \vcc{y} are \vcc{unsigned}, then
\vcc{_(unchecked)(x-y) == (x >= y ? x - y : UINT_MAX - y + x + 1)}, and
similarly for other types.
\end{itemize}

If these aren't enough, you will need to resort to bit-vector
reasoning (below), or use ghost code to prove what you need \footnote{
In particular, you can use iteration or recursion to prove things by
induction.}.

\subsubsection{Bitvector Reasoning}
\label{sect:bv}

Every now and then, you need to prove some low-level fact that VCC
can't prove using ordinary logical reasoning. If the fact involves
can be expressed over a relatively small number of bits, you can ask
VCC to prove it using boolean reasoning at the level of bits, by
putting \lstinline|{:bv}| after the \vcc{assert} tag. For example:

\vccInputSC[linerange={begin-}]{c/4.3.min5.c}

Assertions proved in this way cannot mention program variables, and
can use only variables of primitive C types.


%
% CMPT 473: Software Quality Assurance - A Course Overview
% Section: Property-Based Testing
%
% Author: Jeffrey Leung
%

\section{Property-Based Testing}
	\label{sec:property-based-testing}
\begin{easylist}

& \textbf{Property-based testing:} Testing which generates tests to evaluate functional properties/requirements
	&& Mathematical representation of an expectation

& Common test strategies:
	&& \textbf{Symmetry:} Test strategy where operations are performed to return to the original value
	&& \textbf{Alternative:} Test strategy where a value is compared with a value generated from alternative solutions
	&& \textbf{Induction:} Test strategy
	&& \textbf{Idempotence:} Test strategy where performing an operation more than once has no effect
	&& \textbf{Invariant:} Test strategy where a property of a preprocessed value must be equal to the property of a processed value

\end{easylist}
\clearpage


% Preamble
\providecommand{\main}{../..}
\documentclass[class=article, crop=false]{standalone}

% Packages
\usepackage{\main/packages}
\usepackage{graphicx}
\usepackage{amsmath}

% Configurations
\graphicspath{{\main/images/}{images/}}
\cellspacetoplimit 4pt
\cellspacebottomlimit 4pt

% Document
\begin{document}
    \section{TOPIC 2 - Radiation Integrals}
    In the analysis of radiation problems, the usual procedure is to specify the sources and
    then require the fields radiated by the sources.
    This is in contrast to the synthesis problem where the radiated fields are specified,
    and we are required to determine the sources.
    It is a very common practice in the analysis procedure to introduce auxiliary functions,
    known as \emph{vector potentials}. \\
    The most common vector potential functions are the $A$ (magnetic vector potential) and $F$
    (electric vector potential), also another pair of potential is the \emph{Hertz potentials} $\Pi_e$ and $\Pi_h$.
    \emph{
        Although the electric and magnetic field intensities ($E$ and $H$) represent physically measurable
        quantities, among most engineers the potentials are strictly mathematical tools.
    }
    The introduction of the potentials often simplifies the solution even though it may require
    determination of additional functions. While it is possible to determine the $E$ and $H$
    fields directly from the source-current densities J and M, as shown in Figure\ref{fig:solutionpaths}
    It is usually much simpler to find the auxiliary potential functions first and then determine the $E$ and $H$.
    This two-step procedure is also shown in Figure\ref{fig:solutionpaths}.
    \footnote{
        The above is quoted directly from the textbook\cite{BALANIS}
    }\\

    \begin{figure}[h!]
        \includegraphics[width=\textwidth]{topics/2/solutionpaths.png}
        \caption{Solution paths for computing fields radiated by sources \cite{BALANIS}}
        \label{fig:solutionpaths}
    \end{figure}

    \newpage

    \subsection{Maxwell's Equations}
    Maxwell's equations constitute the radiation field produced by the sources.\\
    \begin{table}[h]
        \centering
        \begin{tabular}{||c|c|c||}
            \hline
            \textbf{Equation} & \textbf{Differential} & \textbf{Phasor} \\
            \hline\hline
            \emph{Faraday's Law} & $ \curl E = - \pdv{\beta(r, t)}{t} $  & $ E = -j  \omega \beta $  \\
            \emph{Ampere's Law} & $ \curl H = \pdv{D(r,t)}{t} + J(r,t) $ & $ \curl H = j \omega D + J $ \\
            \emph{No Free Magnetic Dipole} &  $ \div B = 0 $ & $ \div B = 0 $ \\
            \emph{Gauss' Law} & $\div D = \rho(r,t) $ & $\div D = \rho $ \\
            \hline
        \end{tabular}
        \caption{Maxwell's equations in differential and phasor forms}
        \label{table:maxwelleqs}
    \end{table}
    \subsection{Procedure to Find Fields for $M$ and $J$ Sources}
    \textbf{Consider the following figure\ref{fig:generalsourceintegral} as a reference to all relevant sections}
    \begin{figure}[h!]
        \includegraphics[width=\textwidth]{topics/2/generalsourceintegral.png}
        \caption{General Source radiating a field \cite{BALANIS}}
        \label{fig:generalsourceintegral}
    \end{figure}

    In summary form, the procedure that can be used to find the fields is as follows: \\

    \emph{Summary}: \\

    \begin{enumerate}
        \item Specify $J$ and $M$. (electric and magnetic current density sources)
        \item \begin{enumerate}
                  \item Find $A$ due to $J$ using: \begin{equation}
                             A(r, \phi, \theta) = \frac{\mu}{4 \pi} \iiint_v J \frac{\mathrm{e}^{-jKR}}{R} \dd V
                  \end{equation}
                  \item Find $F$ due $M$ using:  \begin{equation}
                             F(r, \phi, \theta) = \frac{\epsilon}{4 \pi} \iiint_v M \frac{\mathrm{e}^{-jKR}}{R} \dd V
                  \end{equation}
        \end{enumerate}
        \item \begin{enumerate}
                  \item Find $H_A$ and $E_A$ using:
                  \begin{equation}
                                H_A = \frac{1}{\mu} \curl A
                  \end{equation}
                  \begin{equation}
                                E_A = -j \omega A - j \frac{1}{\omega \mu \epsilon} \grad (\div A)
                  \end{equation}
                  \item Find $E_F$ and $H_F$ using:
                  \begin{equation}
                      E_F = - \frac{1}{\epsilon} \curl F
                  \end{equation}
                  \begin{equation}
                      H_F = - j \omega F - \frac{j}{\omega \mu \epsilon} \grad (\div F)
                  \end{equation}
        \end{enumerate}
        \item Total fields are determined by:
        \begin{equation}
            E = E_A + E_F = - j \omega A - j \frac{1}{\omega \mu \epsilon} \grad (\div A) - \frac{1}{\epsilon} \curl F
        \end{equation} or
        \begin{equation}
            E = E_A + E_F = \frac{1}{j \omega \epsilon} \curl H_A - \frac{1}{\epsilon} \curl F
        \end{equation}
        \begin{equation}
            H = H_A + H_F = \frac{1}{\mu} \curl A - j \omega F - j \frac{1}{\omega \mu \epsilon} \grad (\div F)
        \end{equation} or
        \begin{equation}
            H = H_A + H_F = \frac{1}{\mu} \curl A - \frac{1}{j \omega \mu} \curl E_F
        \end{equation}
    \end{enumerate}
    \footnote{
        The above is quoted directly from the textbook\cite{BALANIS}
    }

    \subsection{Far Field Approximation of Fields}
    \textbf{
        Far Field Approximation of Magnetic Vector Potential
    }: \begin{equation}
           A(r, \theta ,\phi) \approx \frac{\mathrm{e}^{-jkr}}{r} A^{0}(\theta, \phi)
    \end{equation} Where,
    \begin{equation}
        A^{0}(\theta, \phi) = \frac{\mu}{4 \pi} \iiint_v J \mathrm{e}^{j k \dot r^\prime } \dd V^\prime
    \end{equation} \\
    \textbf{
        Far Field Approximation of $\curl A$
    }: \begin{equation}
           \curl A = - j k \hat{a_r} \times A
    \end{equation}
    \textbf{
        Far Field Approximation of Electric Field $E$
    }: \begin{equation}
           E = -j \omega ( A_\theta \hat{a_\theta} + A_\phi \hat{a_\phi} )
    \end{equation}
    \begin{equation}
        E^0(\theta, \phi) = E^0_\theta \hat{a_\theta} + E^0_\phi \hat{a_\phi}
    \end{equation}
    \begin{equation}
        E =  \frac{\mathrm{e}^{-jkr}}{r} E^0(\theta, \phi)
    \end{equation}
    \textbf{
        Far Field Approximation of Magnetic Field $H$
    }: \begin{equation}
           H^0(\theta, \phi) = \frac{1}{\eta} \hat{a_r} \times E^0(\theta, \phi)
    \end{equation}
    \begin{equation}
        H = \frac{\mathrm{e}^{-jkr}}{r} H^0(\theta, \phi)
    \end{equation}
    \begin{equation}
        H = \frac{1}{\eta} \hat{a_r} \times E
    \end{equation}

    \subsection{Vector Potential Integral for Different Sources}
    \textbf{
        Volumetric Source
    }
    \begin{equation}
        A(r, \phi, \theta) = \frac{\mu}{4 \pi} \frac{\mathrm{e}^{-jKr}}{r} \iiint_v J_v(r^\prime, \theta^\prime, \phi^\prime)
        \mathrm{e}^{-jKr^\prime} \dd V^\prime
    \end{equation}
    \begin{equation}
        F(r, \phi, \theta) = \frac{\epsilon}{4 \pi} \frac{\mathrm{e}^{-jKr}}{r} \iiint_v
        M_v(r^\prime, \theta^\prime, \phi^\prime) \mathrm{e}^{-jKr^\prime} \dd V^\prime
    \end{equation}
    \textbf{
        Surface Source
    }
    \begin{equation}
        A(r, \phi, \theta) = \frac{\mu}{4 \pi} \frac{\mathrm{e}^{-jKr}}{r} \iint_s J_s(r^\prime, \theta^\prime, \phi^\prime)
        \mathrm{e}^{-jKr^\prime} \dd S^\prime
    \end{equation}
    \begin{equation}
        F(r, \phi, \theta) = \frac{\epsilon}{4 \pi} \frac{\mathrm{e}^{-jKr}}{r} \iint_s
        M_s(r^\prime, \theta^\prime, \phi^\prime) \mathrm{e}^{-jKr^\prime} \dd S^\prime
    \end{equation}
    \textbf{
        Linear Source
    }
    \begin{equation}
        A(r, \phi, \theta) = \frac{\mu}{4 \pi} \frac{\mathrm{e}^{-jKr}}{r} \int_c J_l(r^\prime, \theta^\prime, \phi^\prime)
        \mathrm{e}^{-jKr^\prime} \dd l^\prime
    \end{equation}
    \begin{equation}
        F(r, \phi, \theta) = \frac{\epsilon}{4 \pi} \frac{\mathrm{e}^{-jKr}}{r} \int_c
        M_l(r^\prime, \theta^\prime, \phi^\prime) \mathrm{e}^{-jKr^\prime} \dd l^\prime
    \end{equation}
\end{document}