This entry is for people who use the LaTeX document preparation system, as I did in the book. It’s not a tutorial on LaTeX in general, but shows some techniques for drawing quantum circuits. To be direct, it’s pretty geeky for LaTeX people.
An early decision I had to make was how to draw quantum circuit diagrams in the book. Here’s an example of one:
This includes three Hadamard H gates, two S gates, a T gate, and a swap gate. Would I need to write my own drawing routines?
I really didn’t want to do that because of my time constraints but I also hoped that I could find something better. It didn’t take me long to do so: Alastair Kay’s excellent quantikx package on the CTAN Comprehensive TeX Archive Network. The documentation there is very good, but in this blog entry I’m going to show you how to evolve a simple circuit to have stylistic customizations that you might want to modify and use.
Below are five displayed versions of the same circuit. They are numbered on the left side.
The first is the default formatting from quantikz. It is perfectly fine and you can see similarly formatted circuits in research articles about quantum computing.
\begin{center}
\begin{tikzpicture}
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \gate{H} & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \gate{X} & \gate{H} & \targ{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
The markup \ket{0}
produces the |0>
at the beginning of each wire, which is a horizontal line. \qw
creates a segment of a quantum wire. \gate
is the basic command for drawing a labeled gate with a rectangle. \meter
is the quantum measurement operator. \ctrl{1}
and \targ{}
are the two parts of a CNOT two-qubit gate. \ctrl{1}
is on the wire for the control qubit and extends a line down one wire. There the line meets the \targ{}
(target) qubit and is drawn as a circle around a “+” sign.
In the second example, I’ve changed the font in the H and X gates.
\newcommand*{\gateStyle}[1]{{\textsf{\bfseries #1}}}
\newcommand*{\hGate}{\gateStyle{H}}
\newcommand*{\xGate}{\gateStyle{X}}
\begin{center}
\begin{tikzpicture}
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \gate{\hGate} & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \gate{\xGate} & \gate{\hGate} & \targ{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
I added three LaTeX macros to encapsulate the new function and make it easier to reuse.
\gatestyle
puts its text in a bold sans serif font.
\hGate
draws the Hadamard H using \gatestyle
.
\xGate
draws the X using \gatestyle
.
While it is now easier to use \hGate
and \xGate
for text, it’s still wordy to use them as gates in a circuit. The third example defines two more macros, \circuitH
and \circuitX
, and shows how to set the background and font colors. For a printed book, you might want to have gates with backgrounds in different shades of gray. Alternatively, you could use the same background color for all the Clifford gates.
\newcommand*{\circuitH}{\gate[style={fill=black},label style=white]{\textnormal{\hGate{}}}}
\newcommand*{\circuitX}{\gate[style={fill=teal},label style=white]{\textnormal{\xGate}}}
\begin{center}
\begin{tikzpicture}
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \circuitH & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \circuitX & \circuitH & \targ{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
Now let’s set the color for the circle in \targ
.
\newcommand*{\circuitTarget}[1]{\targ[style={fill=yellow}]{#1}}
\begin{center}
\begin{tikzpicture}
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \circuitH & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \circuitX & \circuitH & \circuitTarget{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
I think you get the idea. You can also set the background color for \meter
, which I leave to you as an exercise. Note that in the April, 2019, version of quantikx, you could not change the color of the line inside the \meter
graphic. You need to copy and redefine the macro (or create a new macro) to do that.
Finally, let me explain what that [scale=1.0]
is doing after the \node
. This allows you to scale the entire drawing and make it larger or smaller. However, it does not change the text size. The fifth example shows the fourth example drawn 20% larger.
\begin{center}
\begin{tikzpicture}
\node[scale=1.2] {
\begin{quantikz}
\ket{0} & \qw & \circuitH & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \circuitX & \circuitH & \circuitTarget{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
Here is the complete LaTeX file I used to generate the examples:
\usetikzlibrary{quantikz}
\mainmatter
\begin{center}
\begin{tikzpicture}
\node at (-5,0) {(1)};
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \gate{H} & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \gate{X} & \gate{H} & \targ{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
\newcommand*{\gateStyle}[1]{{\textsf{\bfseries #1}}}
\newcommand*{\hGate}{\gateStyle{H}}
\newcommand*{\xGate}{\gateStyle{X}}
\begin{center}
\begin{tikzpicture}
\node at (-5,0) {(2)};
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \gate{\hGate} & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \gate{\xGate} & \gate{\hGate} & \targ{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
\newcommand*{\circuitH}{\gate[style={fill=black},label style=white]{\textnormal{\hGate{}}}}
\newcommand*{\circuitX}{\gate[style={fill=teal},label style=white]{\textnormal{\xGate}}}
\begin{center}
\begin{tikzpicture}
\node at (-5,0) {(3)};
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \circuitH & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \circuitX & \circuitH & \targ{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
\newcommand*{\circuitTarget}[1]{\targ[style={fill=yellow}]{#1}}
\begin{center}
\begin{tikzpicture}
\node at (-5,0) {(4)};
\node[scale=1.0] {
\begin{quantikz}
\ket{0} & \qw & \circuitH & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \circuitX & \circuitH & \circuitTarget{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\node at (-4.4,0) {(5)};
\node[scale=1.2] {
\begin{quantikz}
\ket{0} & \qw & \circuitH & \ctrl{1} & \meter{} & \qw \\
\ket{0} & \circuitX & \circuitH & \circuitTarget{} & \meter{} & \qw
\end{quantikz}
};
\end{tikzpicture}
\end{center}
Previous: My five rules for making revisions from editorial comments
Next: What’s in the book
Share this:
Before I discuss what and I how I wrote, let me talk about the markup of the book. By “markup” I mean the underlying format of the content that determines its structure such as the title page, table of contents, parts, chapters, sections, paragraphs, bibliography, and the index, along with font styles and sizes.
In my experience, most publishers, both traditional and online, prefer you to use Microsoft Word to create the book, and it has its own underlying markup language that you typically never see. In a more-or-less what-you-see-is-what-you-get way, you can write and style the book.The publishing workflow is often based on this choice.
My requirements for the book creation process included:
- beautiful math rendering, both in sentences and displayed multi-part formulas,
- built-in support for generating diagrams,
- easy methods to change formatting throughout the book quickly, and
- good support for working quickly on a large text.
Regarding the size of the book, in early 2019 I thought the book would come in around 300 pages and I would have a complete draft on September 1. I ended up writing a book with slightly more than 500 pages with the first full draft delivered on October 9. I had full drafts of various chapters before then, but that was the first time there were no sections with TODO markers.
Word has come a long way on many of these requirements, especially the math, though it can be very laborious to create a book with hundreds or thousands of formulas. Here’s the real problem though: eBooks with math in them often look terrible if you put them in a reflowable format. That is, if you let, say, your Amazon Kindle change the fonts and the line widths, the math just doesn’t look right.
People argue about this forever, but there is an excellent chance that you will end up with fuzzy, misaligned expressions that are the wrong size compared to the surrounding text. So, I early on made the decision that the eBook would not be reflowable. Since that was the case, there was no reason for me to stick with Word. I decided to markup the book in LaTeX. Luckily, Andrew Waldron at Packt Publishing agreed. [Though see this later development regarding the eBook.]
With LaTeX, you have complete and arbitrary control over all parts of the formatting. There are thousands of packages that make your life easier by providing significant functionality that you would not want to write yourself.
LaTeX has
- the best math formatting facilities of any system,
- packages like pgf/tikz for creating diagrams,
- a full macro programming language for formatting control and calculations, and
- easy ways to break a document into sections so you can work on one part at a time.
If you get into macro programming, things can get complicated. I’ve been doing it for 30 years, so it doesn’t faze me. Here are two good books on LaTeX to get you started:
Previous: Last minute tweaks to my quantum computing book cover
Next: My five rules for making revisions from editorial comments
Share this:
I’m writing something that requires me to draw many plots in the Cartesian plane R2 and so I wrote this macro to simplify the process. The arguments are
- The optional tikzpicture scaling factor
- The x-coordinate of the lower left corner
- The y-coordinate of the lower left corner
- The x-coordinate of the upper right corner
- The y-coordinate of the upper right corner
- The label for the horizontal axis
- The label for the vertical axis
It behaves nicely if any of the coordinates are 0 but does no checking for the values or placement of the corners.
I have a separate macro \tikzGridColor
that defines the grid color. At the moment it is set to a shade of blue so the graph looks like it is done on graph paper.
Scroll right to see all the code.
[sourcecode language=”tex”]
\usetikzlibrary{angles,quotes,arrows,positioning}
\usetikzlibrary{arrows.meta,calc,patterns}
\usetikzlibrary{decorations.pathreplacing,backgrounds}
\def\tikzGridColor{blue!75!white}
\newcommand{\graphGridTwoD}[7][0.5]{
\draw[step=#1,\tikzGridColor,very thin] (#2-0.9,#3-0.9) grid (#4+0.9,#5+0.9);
\draw[thick,<->] (#2-0.75,0) — (#4+0.75,0) node[below] {\small #6};
\draw[thick,<->] (0,#3-0.75) — (0,#5+0.75) node[left] {\small #7};
\ifthenelse{#2<0}
{\foreach \x in {#2,…,-1}
\draw[thick] (\x,.1) — (\x,-.1) node[below] {\tiny$\x$};
}{}
\ifthenelse{#4>0}
{\foreach \x in {1,…,#4}
\draw[thick] (\x,.1) — (\x,-.1) node[below] {\tiny$\x$};
}{}
\ifthenelse{#3<0}
{\foreach \y in {#3,…,-1}
\draw[thick] (.1,\y) — (-.1,\y) node[left] {\tiny$\y$};
}{}
\ifthenelse{#5>0}
{\foreach \y in {1,…,#5}
\draw[thick] (.1,\y) — (-.1,\y) node[left] {\tiny$\y$};
}{}
\node[below left] at (0,0) {\tiny$0$};
}
[/sourcecode]