Debian Codegraph @

texmf-dist/doc/latex/quantikz/quantikz.tex

raw
1 \documentclass[aps,pra,10pt,nofootinbib]{revtex4-2}
2 %\documentclass{article}
3 %\pdfoutput=1
4 % Package(s) to include
5 %\listfiles
6 \usepackage{amsfonts,amssymb,amsmath} % for math symbols.
7 \usepackage{lmodern,adjustbox}
8 \usepackage[skins,breakable]{tcolorbox}
9 \usepackage{tikz}
10 \usepackage{enumitem}
11 \usetikzlibrary{quantikz2}
12 \tcbuselibrary{listings}
13 \tcbuselibrary{breakable}
14 \usetikzlibrary{shapes.geometric}
15 \lstset{
16 basicstyle=\small\ttfamily,
17 columns=flexible,
18 breaklines=true
19 }
20 \usepackage[pdftex,hyperfigures,pdfpagelabels]{hyperref}
21 %\lstloadlanguages{Latex}
22
23 \usetikzlibrary{external}
24 %\tikzexternalize
25 %\tikzset{/tikz/external/optimize=false}
26
27 % \NewDocumentEnvironment{Code}{O{}+b}{
28 % \begin{cod}
29 % \begin{center}
30 % #2
31 % \end{center}
32 % \tcblower
33 % % \begin{lstlisting}
34 % #2
35 % % \end{lstlisting}
36 % \end{cod}
37 % }{}
38
39 \newtcblisting{Code}{enhanced,fonttitle=\sffamily\bfseries\large,valign=center,drop fuzzy shadow,lefthand ratio=0.4,lower separated=false,text side listing,before upper=\centering
40 }
41 \newtcolorbox{FullCode}{enhanced,fonttitle=\sffamily\bfseries\large,valign=center,drop fuzzy shadow,breakable}
42 \tcbset{shield externalize}
43
44 %define trimeter gate
45
46 \makeatletter
47
48 %add new anchors
49 \pgfaddtoshape{isosceles triangle}{
50 %define the anchor lstartone.
51 \anchor{lstartone}{%
52 \trianglepoints %built-in function for this shape. Defines certain macros with position info
53 %get the position of the lower left corner. Stored in macros \pgf@x and \pgf@y
54 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}
55 \pgf@ya=.5\pgf@y
56 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}
57 \pgf@y=.5\pgf@y
58 \advance\pgf@y by \pgf@ya%
59 \advance\pgf@y by -\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}%
60 %by end of function, position of anchor stored in \pgf@x and \pgf@y
61 }
62 \anchor{lstarttwo}{%
63 \trianglepoints
64 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}
65 \pgf@ya=.5\pgf@y
66 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}
67 \pgf@y=.5\pgf@y
68 \advance\pgf@y by \pgf@ya%
69 \advance\pgf@y by \pgfkeysvalueof{/tikz/commutative diagrams/classical gap}%
70 }
71 \anchor{lendone}{%
72 \trianglepoints
73 \pgf@process{\pgfpointintersectionoflines%
74 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
75 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}%
76 {\pgfpointadd{\centerpoint}{\pgfpoint{0cm}{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
77 {\pgfpointadd{\centerpoint}{\pgfpoint{0.5cm}{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
78 }%
79 }
80 \anchor{lendtwo}{%
81 \trianglepoints
82 \pgf@process{\pgfpointintersectionoflines%
83 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
84 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}%
85 {\pgfpointadd{\centerpoint}{\pgfpoint{0cm}{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
86 {\pgfpointadd{\centerpoint}{\pgfpoint{0.5cm}{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
87 }%
88 }
89 %now do the anchors for a down wire
90 \anchor{dstarttwo}{%
91 \trianglepoints
92 \pgf@process{\pgfpointintersectionoflines%
93 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
94 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}%
95 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
96 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
97 }%
98 }
99 \anchor{dstartone}{%
100 \trianglepoints
101 \pgf@process{\pgfpointintersectionoflines%
102 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
103 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}%
104 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
105 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
106 }%
107 }
108 \anchor{dendtwo}{%
109 \trianglepoints
110 \pgf@process{\pgfpointintersectionoflines%
111 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
112 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}%
113 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
114 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
115 }%
116 }
117 \anchor{dendone}{%
118 \trianglepoints
119 \pgf@process{\pgfpointintersectionoflines%
120 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
121 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}%
122 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
123 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
124 }%
125 }
126 }
127
128 %aliases for r and u wires
129 \pgfdeclareanchoralias{isosceles triangle}{dendtwo}{ustartwo}
130 \pgfdeclareanchoralias{isosceles triangle}{dendone}{ustartone}
131 \pgfdeclareanchoralias{isosceles triangle}{dstarttwo}{uendtwo}
132 \pgfdeclareanchoralias{isosceles triangle}{dstartone}{uendone}
133 \pgfdeclareanchoralias{isosceles triangle}{lendtwo}{rstartwo}
134 \pgfdeclareanchoralias{isosceles triangle}{lendone}{rstartone}
135 \pgfdeclareanchoralias{isosceles triangle}{lstarttwo}{rendtwo}
136 \pgfdeclareanchoralias{isosceles triangle}{lstartone}{rendone}
137 \makeatother
138
139
140 \tikzset{
141 trimeter/.style={
142 thickness, % gets the line width correct
143 filling,
144 shape=isosceles triangle, %this is your shape name
145 draw=black, %don't forget this, or you won't see much!
146 inner sep=2pt
147 }}
148 \DeclareExpandableDocumentCommand{\mygate}{O{}{m}}{|[trimeter,#1]| {#2}}
149
150
151
152 \begin{document}
153
154 \title{Tutorial on the Quantikz Package}
155 \date{\today}
156 \author{Alastair \surname{Kay}}
157 \affiliation{Royal Holloway University of London, Egham, Surrey, TW20 0EX, UK}
158 \email{[email protected]}
159 \begin{abstract}
160 \end{abstract}
161 \maketitle
162 I've always used \href{https://arxiv.org/abs/quant-ph/0406003}{QCircuit} for typesetting quantum circuit diagrams within \LaTeX, but found the Xy-pic based notation rather impenetrable and I struggled to adapt it for my needs (this is probably my failing rather than the package's). Thus, I wanted a tikz package that could do the same. That package is Quantikz. Those familiar with QCircuit will recognise much of the notation, although it has evolved a bit (hopefully simplified!).
163
164 The latest release (denoted by version numbers 1.x) is a major step forward in terms of the behind-the-scenes code. Unfortunately, this has necessitated breaking some compatibility with previous versions. Your old circuits should still work, but they might not look exactly as expected! Primarily the concept of the wires in a circuit has been modified as classical wires were only ever an after-thought, but have now been elevated to an equivalent status to the quantum wire.
165
166 \tableofcontents
167
168 \section{Usage}
169
170 The quantikz package is available on CTAN, and will therefore be available through most (current) TeX distributions. Once installed, simply write
171 \begin{verbatim}
172 \usepackage{tikz}
173 \usetikzlibrary{quantikz2}
174 \end{verbatim}
175 in the preamble of your document
176 Now, each time that you want to include a quantum circuit, you just enclose it in a \verb!tikzcd! or \verb!quantikz! environment. (Theoretically, there is an advantange of \verb!quantikz! over \verb!tikzcd!, but \verb!tikzcd! is retained for backwards compatibility. Use \verb!quantikz! for preference.)
177
178 The current version of TeX on the arXiv is not up to date enough to provide the quantikz package. When uploading your source to the arXiv, you need to obtain the file tikzlibraryquantikz2.code.tex (you will always be able to locate it on your computer in the main tex directory if you have installed the package, but it should also accompany the source code of this file, and the most recent version is available \href{http://dx.doi.org/10.17637/rh.7000520}{here}). Just include the file in the main directory of your source code.
179
180 \section{Basic Usage}
181
182 Quantum circuits are laid out with a matrix notation, with cells separated by \& (just like all matrices, tables etc.\ in \LaTeX). Here, we typeset a single quantum wire.
183 \begin{Code}
184 \begin{quantikz}
185 &&&&&
186 \end{quantikz}
187 \end{Code}
188
189 New wires are created by the new line command, \textbackslash\textbackslash. By default, all wires are quantum wires, i.e.\ a single solid line.
190 \begin{Code}
191 \begin{quantikz}
192 &&&&& \\
193 &&&&&
194 \end{quantikz}
195 \end{Code}
196 Wire types can be changed at a global level. Options are \texttt{q} (quantum), \texttt{c} (classical), \texttt{b} (bundle) and \texttt{n} (none). Here we specify that the first wire is quantum, and the second is classical. We also show how to change the classical wire to a quantum one mid-circuit (without separating gates, it looks ugly).
197 \begin{Code}
198 \begin{quantikz}[wire types={q,c}]
199 &&&&& \\
200 &&&&\setwiretype{q}&
201 \end{quantikz}
202 \end{Code}
203 The wire change command affects the wire drawn from that cell, which is the one running back to the previous cell. This may mean changing the wire type in the column \emph{after} the one you're expecting!
204
205 \subsection{Gates \& Measurement}
206
207 Inside a cell (between two \&), you can insert a gate command. Often, this will just be \verb|\gate|, a plain box that contains a label corresponding to the parameter. The first optional parameter can be used to specify the number of qubits that the gate spans. You always put the command in the first cell in which that gate should appear. The label of the gate command is already in math mode, so you can enter arbitrary mathematical functions.
208 \begin{Code}
209 \begin{quantikz}
210 &\gate{H}&\gate[2]{U}&\gate{R_Z(\theta)}& \meter{} \\
211 &&&\phase{\alpha} &
212 \end{quantikz}
213 \end{Code}
214 You can also see in the above example how to add a measurement gate (of which there are a number of variants) and a phase gate.
215
216 It is important that gate commands come before any other commands in a cell, such as those changing wire type. You do not typically put a gate command in the first cell (the one before the first \& in a row) as there won't be an entering wire in that case.
217
218 Make sure you include the trailing \& after the gate, or you won't have a wire coming out of the last gate!
219
220 \subsection{Controlled Gates}
221
222 Controlled gates typically consist of two elements -- a control and a target. The control creates a vertical wire of a specified length (may be negative). Note that the target commands, although they don't accept a parameter, need the \{\} after their call. Standard gates can also be targets.
223 \begin{Code}
224 \begin{quantikz}
225 & \ctrl{1} & \targ{} & \swap{1} & \ctrl[vertical wire=c]{2} &&\\
226 & \control{} & \ctrl[open]{-1} & \targX{} && \gate{X} &\\
227 &&&& \gate{U} & \meter{} \wire[u][1]{c}
228 \end{quantikz}
229 \end{Code}
230 Other controls can be created simply by the addition of a vertical wire.
231 If you want a gate that is one control and several targets, it is often good enough to just create the vertical wire so that it goes to the most distant target. You may need the command \verb!\wire[d]{q}! (vertical quantum wire) to create vertical connections.
232
233 \begin{Code}
234 \begin{quantikz}
235 & \ctrl{2} & \gate{U} & \\
236 & \targ{} & \ctrl{1}\wire[u]{q} & \\
237 & \targ{} & \gate{V} &
238 \end{quantikz}
239 \end{Code}
240
241
242
243 % Classical control following a measurement is more naturally specified directly using the \verb!wire! command,
244 % \begin{Code}
245 % \begin{center}
246 % \begin{quantikz}
247 % & \meter{}\wire[d][2]{c} \\
248 % & &\meter{} & \ctrl[c]{1}\setwiretype{c}\\
249 % & \gate{U} && \gate{V}&
250 % \end{quantikz}
251 % \end{center}
252 % \tcblower
253 % \begin{lstlisting}
254 % \begin{quantikz}
255 % & \meter{}\wire[d][2]{c} \\
256 % & &\meter{} & \ctrl[c]{1}\setwiretype{c}\\
257 % & \gate{U} && \gate{V}&
258 % \end{quantikz}
259 % \end{lstlisting}
260 % \end{Code}
261 % Note how a wire can end early just by leaving out the \& at the end of a row, without have to \verb!\setwiretype{n}!.
262
263
264 \subsection{Labelling Circuits}
265
266 There are many ways to label the different parts of your circuit. The basic commands for labelling wires at the start/end of a circuit are \verb!\lstick! and \verb!\rstick! respectively. These can apply to multiple qubits; just apply the command to the first, and say how many wires it should cover. The arguments are typset in text mode, but you can of course convert to math mode.
267 \begin{Code}
268 \begin{quantikz}
269 \lstick{\ket{0}} & \gate{U} & \gate[2]{\sqrt{\textsc{swap}}} &\rstick[2]{out} \\
270 \lstick[2]{input} & \gate[2]{V} && \\
271 & & & \rstick{$\frac{1}{\sqrt{2}}$}
272 \end{quantikz}
273 \end{Code}
274 You can use standard \LaTeX\ maths expressions for your labels. Usually, spacing can be automatically adjusted just fine.
275
276
277
278 Sometimes, it might be that you want a multi-line label, and it should not be that each wire takes the height of those multiple lines. At this point, use the key \verb!disable auto height!. By default, each row will be assigned the height that a gate with label $U$ would be. This can be overridden by the third optional parameter of the gate command, if desired.
279
280 \begin{Code}
281 \begin{quantikz}
282 \lstick{$c_0$} & \gate[3,disable auto height]{\verticaltext{MAJ}} & & \\
283 \lstick{$c_1$} & & \gate[3,disable auto height]{\verticaltext{MAJ}} & \\
284 \lstick{$c_2$} & & & \\
285 \lstick{$c_3$} & & &
286 \end{quantikz}
287 \end{Code}
288 Note that we used the command \verb!\verticaltext! for typesetting the text vertically.
289
290
291 Individual wires can also often be labelled. However, as this is typically an optional argument that appears late in the sequence, make sure you specify all proceeding parameters! For example,
292 \begin{Code}
293 \begin{quantikz}
294 & \ctrl[wire style={"s"}]{1} & \wire[l][1]["p"{above,pos=0.2}]{a} \\
295 & \targ{} &
296 \end{quantikz}
297 \end{Code}
298 You will observe that stylings can also be passed. The \texttt{pos} is a fraction of the distance along the line. In this case, it's being drawn right to left, hence being closer to the right-hand edge\footnote{In the previous incarnation of quantikz, there was an accidental, undocumented method to label control wires. Internal changes have necessitated breaking that functionality.}.
299
300 \subsection{Boxing/Highlighting Parts of a Circuit}\label{sec:boxing}
301
302 It is often useful to highlight parts of a circuit. We do this with the \verb!\gategroup! command. The optional parameters \verb!wires! (the default) and \verb!steps! specify the number of rows and columns that the group spans respectively. The mandatory argument is the label for the box (although this can be empty). The top-left corner of the box coincides with the cell in which the command is placed.
303 \begin{Code}
304 \begin{quantikz}
305 & \gate{H} & \ctrl{1} & \gate{H}\gategroup[2,steps=3,style={inner sep=6pt}]{reversed c-{\sc not}} & \ctrl{1} & \gate{H} & \ctrl{1} & & \\
306 & & \targ{} & \gate{H} & \targ{} & \gate{H} & \targ{} & \gate{H} &
307 \end{quantikz}
308 \end{Code}
309 This is probably where you want to start to tune some of the optional parameters to style the box the way you want. For more details beyond some basic examples that follow, see Sec.\ \ref{sec:style}. By default, this box is drawn on top of the circuit itself. If you want it to be behind (for example, should you want it to have a background colour), then use the \verb!background! option.
310 \begin{Code}
311 \begin{quantikz}
312 & \gate{H} & \ctrl{1}\gategroup[2,steps=3,style={dashed,rounded corners,fill=blue!20, inner xsep=2pt},background,label style={label position=below,anchor=north,yshift=-0.2cm}]{{\sc swap}} & \targ{} & \ctrl{1} & & \\
313 & & \targ{} & \ctrl{-1} & \targ{} & \gate{H} &
314 \end{quantikz}
315 \end{Code}
316 The \verb!label style! key can be used to tune the label's properties, including positioning. Note that it is often good to use \verb!anchor=mid! for label anchors because if you have multiple labels, this will help get them horizontally aligned. It just means you have to use some \verb!yshift! to move the label off the border around the gategroup.
317
318 \subsection{Slicing}
319
320 It is often helpful to `slice' up a circuit for the sake of explaining it step by step. To do this, we provide the \verb!\slice{title}! command, which inserts a dashed vertical line after the column in which the command is added.
321 \begin{Code}
322 \begin{quantikz}
323 & \gate{H}\slice{step} & \ctrl{1} & \meter{} \\
324 & & \targ{} & \ctrl{1} & \gate{H} & \\
325 & & & \targ{} & &
326 \end{quantikz}
327 \end{Code}
328 You can also slice every step by using option \verb!slice all!, and the labels will be automatically numbered. This is likely to behave strangely unless you explicitly ensure that all rows have the same number of entries (i.e.\ short rows should have extra \& characters added).
329 \begin{Code}
330 \begin{quantikz}[slice all]
331 & \gate{H} & \ctrl{1} & \meter{} &\setwiretype{n}& \\
332 & & \targ{} & \ctrl{1} & \gate{H} & \\
333 & & & \targ{} & &
334 \end{quantikz}
335 \end{Code}
336 If you need to adjust where the last slice is, use the optional parameter \verb!remove end slices!, which counts the number of columns fewer to add slices to. You can also change the title of each of the slices, by setting \verb!slice titles!. Include the macro \verb!\col! in your specification if you want to use the step number. Note, however, that the columns won't automatically space themselves out to accommodate a very wide label. You can style the slicing lines with the \verb!slice style! key, and the labels with \verb!slice label style!. These can be used to rotate the labels and create a bit more space!
337 \begin{Code}
338 \begin{quantikz}[slice all,remove end slices=1,slice titles=slice \col,slice style=blue,slice label style={inner sep=1pt,anchor=south west,rotate=40}]
339 & \gate{H} & \ctrl{1} & \meter{} &\setwiretype{n}& \\
340 & & \targ{} & \ctrl{1} & \gate{H} & \\
341 & & & \targ{} & &
342 \end{quantikz}
343 \end{Code}
344 \noindent If you get compile errors when trying to slice, check the last line of your matrix, and make sure it doesn't end in \textbackslash\textbackslash.
345
346 % The \verb!vertical slice labels! key reorients the labels as below.
347 % \begin{Code}
348 % \begin{quantikz}[slice all,remove end slices=1,slice titles=slice \col,vertical slice labels]
349 % & \gate{H} & \ctrl{1} & \meter{} && \\
350 % & & \targ{} & \ctrl{1} & \gate{H} & \\
351 % & & & \targ{} & &
352 % \end{quantikz}
353 % \end{Code}
354
355 \newpage
356 \section{Commands \& Options}
357
358 \begin{description}[style=nextline]
359 \item [\textbackslash begin\{quantikz\}{[opts]}\ldots\textbackslash end\{quantikz\}]
360 The main environment in which you create quantum circuit diagrams. The main body of the environment is a table, with cells separated by \&, and new rows started by \textbackslash\textbackslash.
361
362 \texttt{opts} is a comma separated list of the following options:
363
364 \begin{tabular}{p{4cm}p{10cm}}
365 \texttt{wire types = \{list\}} & \texttt{list} is a comma separated list defining the wire type for each wire in the circuit, choosing from \texttt{q} (quantum), \texttt{c} (classical), \texttt{b} (wire bundle), \texttt{n} (none) If not specified, all wires are assumed to be quantum.\\
366 \texttt{thin lines} & Option for circuit aesthetic with thinner lines. \\
367 \texttt{transparent} & Sets entire circuit to have transparent background. \\
368 \texttt{classical gap=} & Classical wires are created by placing two wires, at centre $\pm$ classical gap. Default: 0.03cm. \\
369 \texttt{align equals at = n} & place the vertical centre of the circuit at wire number n (can be non-integer). If \verb!wire types! is specified, this is automatically set to $(N+1)/2$ where $N$ is the length of the list (i.e.\ number of wires).\\
370 \texttt{slice all} & add slices on all columns\\
371 \texttt{remove end slices = n} & does not show the last n slices\\
372 \texttt{slice titles =} & use the assigned text to label each slice. Use \verb!\col! to convey column number. \\
373 \texttt{slice style =}& Standard tikz style commands for the lines of the slice. Enclose in \{\} if giving multiple commands\\
374 \texttt{slice label style =} & Standard tikz style commands for the label of the slice. Enclose in \{\} if giving multiple commands\\
375 \texttt{vertical slice labels} & write the text of the slices vertically instead of horizontally. \\
376 %\texttt{wire style =}&Set a default style for wires (e.g.\ specify a colour).
377 \end{tabular}
378
379 The environment also receives any standard tikzcd (and tikz) options. See \href{http://mirrors.ctan.org/graphics/pgf/contrib/tikz-cd/tikz-cd-doc.pdf}{that manual} for more information. Particularly useful cases include:
380
381 \begin{tabular}{p{4cm}p{10cm}}
382 \texttt{column sep = n} & put spacing between each column of length n (e.g.\ 1cm). This is the padding between columns, \emph{not} the centres of gates, unless you add the between origins key.\\
383 \texttt{row sep = n} & put spacing between each row of length n (e.g.\ 1cm). This is the padding between rows, \emph{not} the centres of gates, unless you add the between origins key.\\
384 \texttt{between origins} & makes the row/column sep measurement be between the centres of gates.
385 \end{tabular}
386
387 but you can also supply a colour, and that specifies the border colour of gates etc.
388
389 \item [\textbackslash setwiretype{[n]}\{t\}]
390 Sets wire $n$ (default: current row, from that column onwards) to being of type $t$, which must be one of: \texttt{q} (quantum), \texttt{c} (classical) or \texttt{b} (bundle). Note that this will only affect the rendering from that point on, letting you change the wire type mid circuit. Should always come after any gate drawing command in a cell.
391 \begin{Code}
392 \begin{quantikz}
393 &&&\setwiretype{c}&&&
394 \end{quantikz}
395 \end{Code}
396 \item [\textbackslash wire{[d][n][s]}\{t\}]
397 Draw a wire starting from the current cell of type \texttt{t} which can be one of \texttt{a} (automatic), \texttt{q}, \texttt{c}, \texttt{b}, going in direction \texttt{d} (\texttt{u,d,l,r} for up/down/left/right) for a number of cells $n$. Automatic uses the current wire type of that wire. Note that the standard horizontal wire for that cell will still be drawn. Some styling can be specified using \texttt{s}. Largely unnecessary. Should always come after any gate drawing command in a cell.
398 \begin{Code}
399 \begin{quantikz}
400 &&&\wire[d][1]{c}&&&\\
401 &&&&&&
402 \end{quantikz}
403 \end{Code}
404 \item [\textbackslash wireoverride\{t\}]
405 Sets the wire type for the current cell (i.e.\ the wire starting in this cell and going backwards) to \texttt{t} without changing the wire type for the rest of the row. Should always come after any gate drawing command in a cell.
406 \begin{Code}
407 \begin{quantikz}
408 %of the 6 wire sections (count the &), knock out the third.
409 &&&\wireoverride{n}&&&
410 \end{quantikz}
411 \end{Code}
412 \item [\textbackslash qwbundle{[s]}\{n\}]
413 The standard typesetting of a wire bundle is 3 horizontal wires. Alternatively, you can use a single quantum wire, adding \textbackslash qwbundle to put a slash through the wire, labelled by \texttt{n}, typically the number of qubits that single wire represents. Should always come after any gate drawing command in a cell. The size of the strike can be altered by altering the parameter \texttt{s}:
414
415 \begin{tabular}{p{4cm}p{10cm}}
416 \texttt{style=} & Set the style of the line through a comma-separated list of tikz style commands. \\
417 \texttt{Strike Height=} & Set the height of the strike through\\
418 \texttt{Strike Width=} & Set the width of the strike through.
419 \end{tabular}
420
421 \begin{Code}
422 \begin{quantikz}[wire types={b,q},classical gap=0.07cm]
423 &&&& \\
424 & \qwbundle{n} &&&
425 \end{quantikz}
426 \end{Code}
427 \item [\textbackslash permute\{list\}]
428 Permutation of wires. Takes the comma separated \texttt{list} of positive integers and connects wire $n$ to the wire specified in the $n^{th}$ element of the list. Counting starts such that wire 1 is the current wire. All wires are assumed to be quantum wires.
429
430 For example, \verb!\permute{3,1,2}! connects the first qubit to the third (relative to the location of the command, not absolute row number), the second to the first and the third to the second.
431 \begin{Code}
432 \begin{quantikz}[background color=black!5!white]
433 &\permute{3,1,2} & \\
434 & & \\
435 & &
436 \end{quantikz}
437 \end{Code}
438 Note that the wiring code is not sophisticated -- it will probably look quite ugly for anything complicated! Also, it assumes that the wires are quantum (not classical or bundled). Overlapping wires are given a gap by using a thicker line of the colour \texttt{background color} (default: white), so if your circuit sits on top of something non-white (as here), you'll need to change that.
439
440 \item [\textbackslash linethrough]
441 Objects (e.g.\ gates) in cells take up their own space that, by default, wires do not cross. This command puts a quantum wire across the current cell. Note that this will appear underneath multi-qubit gates, and therefore invisible unless those gates are transparent.
442
443 One place where this might be useful is as a ``pass-through'' on a gate, such as
444 \begin{Code}
445 \begin{quantikz}[transparent]
446 & \gate[2]{J_{12}} & \gate[3,label style={yshift=0.2cm}]{J_{13}} & & \\
447 & & \linethrough &\gate[2]{J_{23}} & \\
448 &&&&
449 \end{quantikz}
450 \end{Code}
451
452 \item [\textbackslash push\{t\}]
453 Places text \texttt{t} on the quantum wire, with no spacing or gate command around it.
454 \begin{Code}
455 \begin{quantikz}
456 &&\push{X}&&
457 \end{quantikz}
458 \end{Code}
459
460 \item [\textbackslash phantomgate\{s\}, \textbackslash hphantomgate\{s\}]
461 Places a quantum wire that occupies that same space as a single-qubit gate with label \texttt{s} would. The h version only occupies horizontal space, not vertical.
462 \begin{Code}
463 \begin{quantikz}
464 & \gate{H} & \phantomgate{really wide gate} & \gate{H} &
465 \end{quantikz}
466 \end{Code}
467
468 \item [\textbackslash ghost{[w][h]}\{l\}]
469 Creates an invisible quantum gate that has the same height as \verb!\gate[][w][h]{l}!. Just like any other gate command, this should come before any other commands in a cell, and cannot be in the same cell as another gate command.
470 \begin{Code}
471 \begin{quantikz}
472 %give the wires the same vertical space as if they had H gates on them
473 &&\ghost{H} \\
474 && \ghost{H}
475 \end{quantikz}=\begin{quantikz}
476 & \gate{H} & \\
477 & \gate{H} &
478 \end{quantikz}
479 \end{Code}
480
481 \item [\textbackslash phase{[s]}\{l\}]
482 Creates a phase gate (black circle) with label \texttt{l}. The optional parameter \texttt{s} controls the styling via the parameters
483
484 \begin{tabular}{p{4cm}p{10cm}}
485 \texttt{style=} & Set the style of the line through a comma-separated list of tikz style commands. \\
486 \texttt{label style=} & Set the style of the label through a comma-separated list of tikz style commands.
487 \end{tabular}
488
489 \begin{Code}
490 \begin{quantikz}
491 & \phase{\alpha} &
492 \end{quantikz}
493 \end{Code}
494
495 \item [\textbackslash lstick{[s]}\{t\}, \textbackslash midstick{[s]}\{t\}, \textbackslash rstick{[s]}\{t\}]
496 Text placement for labelling a wire to left/middle/right using text \texttt{t} (in text mode, not math mode) and styled using comma separated list of commands \texttt{s} from
497
498 \begin{tabular}{p{4cm}p{10cm}}
499 \texttt{n} & If $n$ is a positive integer value, this is interpreted as the number of wires to use (default is 1). By default, introduces curly braces to span multiple wires. \\
500 \texttt{wires=n} & long-hand form of the above.\\
501 \texttt{label style=} & Styles the text using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}.\\
502 \texttt{brackets=}& Choose from \texttt{none/left/right/both} to switch on/off appropriate bracketing. (left won't affect \textbackslash lstick, and right won't affect \textbackslash rstick). \\
503 \texttt{braces=} & Styles the braces using standard tikz commands.
504 \end{tabular}
505
506 \begin{Code}
507 \begin{quantikz}
508 \lstick{\ket{0}} & \gate{H} & \midstick[2]{middle} & \gate{H}& \rstick[2,brackets=none]{out} \\
509 \lstick{\ket{0}} & \gate{H} &&\gate{H}&
510 \end{quantikz}
511 \end{Code}
512
513 \item [\textbackslash gate{[opts][w][h]}\{l\}]
514 The standard quantum gate command. Creates a box containing the label \texttt{l}. \texttt{w} and \texttt{h} are optional minimum width and minimum height parameters to override the automatic sizing. Gate is styled using the optional parameter \texttt{opts}, which is a comma separated list of commands:
515
516 \begin{tabular}{p{4cm}p{10cm}}
517 \texttt{n} & If $n$ is a positive integer value, this is interpreted as the number of wires to use (default is 1).\\
518 \texttt{wires=n} & long-hand form of the above.\\
519 \texttt{style=} & Styles the gate (box) using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}.\\
520 \texttt{label style=} & Styles the gate text using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}.\\
521 \texttt{disable auto height} & pretty self-explanatory\\
522 \texttt{swap} & Fixes the gate to be a 2-qubit gate, depicting a swap between the two wires.
523 \end{tabular}
524
525 \begin{Code}
526 \begin{quantikz}
527 & \gate{H} & \gate[2]{U} & \gate[2,swap]{} & \\
528 &&&&
529 \end{quantikz}
530 \end{Code}
531
532 \item [\textbackslash gateinput{[s]}\{l\}, \textbackslash gateoutput{[s]}\{l\}]
533 Put a label \texttt{l} inside the current gate command, starting on the current row, on either the input or output. If spanning multiple rows, will group the wires using curly braces by default. The width of the containing gate does not automatically adjust to the contents of these extra labels, so you will have to add it with the second optional parameter of \verb!\gate!. Style with parameter \texttt{s} using comma separated list of commands from:
534
535 \begin{tabular}{p{4cm}p{10cm}}
536 \texttt{n} & If $n$ is a positive integer value, this is interpreted as the number of wires to span (default is 1).\\
537 \texttt{wires=n} & long-hand form of the above.\\
538 \texttt{label style=} & Styles the text using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}.\\
539 \texttt{braces=} & Styles the braces using standard tikz commands.
540 \end{tabular}
541
542 \begin{Code}
543 \begin{quantikz}
544 &\ctrl{1} & \\
545 &\gate[3][1.7cm]{U}\gateinput[2]{$x$}\gateoutput[2]{$x$} & \\
546 & & \\
547 &\gateinput{$y$}\gateoutput{$y\oplus f(x)$}
548 &
549 \end{quantikz}
550 \end{Code}
551
552 \item [\textbackslash meter{[opts][w][h]}\{l\}, \textbackslash metercw{[opts][w][h]}\{l\}]
553 Measurement gates of different styles. Measurement is labelled by label \texttt{l}. Styling specified by optional parameter \texttt{opts}, a comma separated list of tikz commands. (Note the similarity to the gate command.) Can span multiple wires.
554
555 \begin{Code}
556 \begin{quantikz}
557 & \metercw[label style={inner sep=1pt}]{x} & \meter[2]{y} \\
558 &&
559 \end{quantikz}
560 \end{Code}
561
562 \begin{tabular}{p{4cm}p{10cm}}
563 \texttt{n} & If $n$ is a positive integer value, this is interpreted as the number of wires to use (default is 1).\\
564 \texttt{wires=n} & long-hand form of the above.\\
565 \texttt{style=} & Styles the gate (box) using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}.\\
566 \texttt{label style=} & Styles the gate text using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}.\\
567 \texttt{disable auto height} & pretty self-explanatory\\
568 \end{tabular}
569
570 \item [\textbackslash measure{[s]}\{l\}, \textbackslash measuretab{[s]}\{l\}, \textbackslash meterD{[s]}\{l\}, \textbackslash inputD{[s]}\{l\}]
571 Single-qubit measurement gates of different styles. Measurement is labelled by label \texttt{l}. Styling specified by optional parameter \texttt{s}, a comma separated list of tikz style commands. Note that this is provided as \verb![s]! where most other gates would expect \verb![style=s]!. \verb!\inputD! is the equivalent for inputs of \verb!\meterD!. For \verb!\inputD!, incoming wire is disabled, overriding global style.
572 \begin{Code}
573 \begin{quantikz}
574 & \measure{x} \\
575 & \measuretab{x} \\
576 \inputD{0} & \meterD{x\vphantom{0}}
577 %vphantom makes the input and output shapes the same height, because x and 0 are different heights
578 \end{quantikz}
579 \end{Code}
580
581 \item [\textbackslash swap{[s]}\{n\}]
582 Create an X shape on the current wire and a single vertical wire going downwards $n$ rows (may be negative). Used as a swap gate, typically paired with \verb!\targX!. Styled using parameter \texttt{s} as comma separated list of commands, some of which provide access to a partial swap:
583
584 \begin{tabular}{p{4cm}p{10cm}}
585 \texttt{partial swap=}&The text to place inside a circle \\
586 \texttt{partial position=}&Fractional position of the circle along the vertical line. Default: 0.5 (i.e.\ middle). \\
587 \texttt{style=}&Tikz styling parameters for the gate. \\
588 \texttt{label style=}&Tikz styling parameters for the label, overriding those of the whole gate. \\
589 \texttt{vertical wire=}& Specifies the type of vertical wire: \texttt{q} (quantum, default), \texttt{c} (classical) or \texttt{b} (bundle)
590 \end{tabular}
591
592 \begin{Code}
593 \begin{quantikz}[row sep=0.8cm]
594 & \swap[partial swap={\pi/4},partial position=0.3]{2} & \swap{1} & \\
595 & & \targX{} & \\
596 & \targX{} &&
597 \end{quantikz}
598 \end{Code}
599
600 \item [\textbackslash ctrl{[s]}\{n\}, \textbackslash octrl{[s]}\{n\}]
601 Starting point of a controlled gate, using filled circle (\textbackslash ctrl) or open circle (\textbackslash octrl). By default, single vertical line is added to go down $n$ rows (may be negative). Settings are controlled via the optional parameter \texttt{s}
602
603 \begin{tabular}{p{4cm}p{10cm}}
604 \texttt{style=}&Tikz styling parameters for the gate (both the circle and the vertical wire).\\
605 \texttt{wire style=}& Styling parameters for the wire only, overriding anything set by \texttt{style}. \\
606 \texttt{vertical wire=}& Specifies the type of vertical wire: \texttt{q} (quantum, default), \texttt{c} (classical) or \texttt{b} (bundle) \\
607 \texttt{open}& Makes the circle an open circle, i.e.\ \verb!\ctrl[open]{1}! is a synonym for \verb!\octrl{1}!.
608 \end{tabular}
609
610 \begin{Code}
611 \begin{quantikz}
612 & \ctrl{2} & \octrl{1} & \\
613 & \control{} & \targ{} & \\
614 & \ocontrol{} &&
615 \end{quantikz}
616 \end{Code}
617
618 \item [\textbackslash control{[s]}\{\}, \textbackslash ocontrol{[s]}\{\}]
619 Target equivalents of the above commands, but with no vertical wire. See previous two entries for usage examples.
620
621 \begin{tabular}{p{4cm}p{10cm}}
622 \texttt{style=}&Tikz styling parameters for the gate (both the circle and the vertical wire).\\
623 \texttt{open}& Makes the circle an open circle, i.e.\ \verb!\control[open]{}! is a synonym for \verb!\ocontrol{}!.
624 \end{tabular}
625
626
627
628 \item [\textbackslash targ{[s]}\{\}, \textbackslash targX{[s]}\{\}]
629 Target elements of controlled-not and controlled-swap (no vertical wire). \texttt{s} provides styling parameters. See previous entries for usage examples.
630
631 \begin{tabular}{p{4cm}p{10cm}}
632 \texttt{style=}&Tikz styling parameters for the gate.
633 \end{tabular}
634
635 \item [\textbackslash gategroup{[opts]}\{l\}]
636 Create a large box with top-left-hand corner positioned (roughly) at the top-left of the current cell. The box has label \texttt{l} and is styled with the options \texttt{opts}:
637
638 \begin{tabular}{p{4cm}p{10cm}}
639 \texttt{n} & If $n$ is a positive integer value, this is interpreted as the number of rows (wires) to cover (default is 1).\\
640 \texttt{wires=n} & long-hand form of the above.\\
641 \texttt{steps=m} & This is the number of columns (time steps) that the box covers. Default is 1. \\
642 \texttt{style=} & Styles the box using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}.\\
643 \texttt{label style=} & Styles the label text using standard tikz commands. If using multiple commands, or anything involving a space, enclose in \{\}. \\
644 \texttt{background} & Draw the gategroup behind the circuit (useful if the box has a background colour).
645 \end{tabular}
646
647 \begin{Code}
648 \begin{quantikz}
649 & \ctrl{1} & \gate{H}\gategroup[2,steps=3,style={inner sep=6pt}]{reversed c-{\sc not}} & \ctrl{1} & \gate{H} & \ctrl{1} & \\
650 & \targ{} & \gate{H} & \targ{} & \gate{H} & \targ{} &
651 \end{quantikz}
652 \end{Code}
653
654 \item [\textbackslash slice{[s]}\{l\}]
655 Insert a slice between the current column and the next, with a title of \texttt{l}. Styles can be applied via the optional parameter \texttt{s}:
656
657 \begin{tabular}{p{4cm}p{10cm}}
658 \texttt{style=}&Tikz styling parameters for the slice. \\
659 \texttt{label style=}&Tikz styling parameters for the title/label.
660 \end{tabular}
661
662 \begin{Code}
663 \begin{quantikz}
664 & \gate{H}\slice{step} & \ctrl{1} & \meter{} \\
665 & & \targ{} & \ctrl{1} & \gate{H} & \\
666 & & & \targ{} & &
667 \end{quantikz}
668 \end{Code}
669
670 \item [\textbackslash makeebit{[s]}\{l\}]
671 Create an e-bit. This places the label \texttt{l} halfway between the current row and the next row. Two lines come off this label, leading to the current wire and the one below.
672
673 \begin{tabular}{p{4cm}p{10cm}}
674 \texttt{style=}&Tikz styling parameters for the wires. \\
675 \texttt{label style=}&Tikz styling parameters for the label. \\
676 \texttt{angle=}&The angle of the two lines drawn, in degrees. Default: -45
677 \end{tabular}
678
679 \begin{Code}
680 \begin{quantikz}
681 \makeebit[angle=-60,label style=blue]{generate} & & \\
682 & &
683 \end{quantikz}
684 \end{Code}
685
686 \item [\textbackslash trash{[s]}\{l\}, \textbackslash ground{[s]}\{l\}]
687 Two different ways of denoting the termination of a wire (e.g.\ tracing out). Label text \texttt{l}, styling options directly supplied via \texttt{s}.
688 \begin{Code}
689 \begin{quantikz}
690 &\gate[3]{U} & \\
691 && \ground{} \\
692 && \trash{\text{trace}}
693 \end{quantikz}
694 \end{Code}
695
696 \item [\textbackslash wave{[s]}\{\}]
697 Draw a wave along an entire row. \texttt{s} is standard tikz formatting commands for additional control over the style. By default, there is no wire drawn on this row. To have one, you should run \textbackslash setwiretype\{q\} immediately after the \textbackslash wave command.
698
699 \begin{Code}
700 \begin{quantikz}
701 & \gate{H} & \ctrl{3} & \ \ldots\ & & \gate{H} & \\
702 \wave&&&&&&\\
703 & \gate{H} & & \ \ldots\ & \ctrl{1} & \gate{H} & \\
704 & & \gate{U} & \ \ldots\ & \gate{U^k} & &
705 \end{quantikz}
706 \end{Code}
707
708 \item [\textbackslash verticaltext\{l\}]
709 Present the text in label \texttt{l} as vertically stacked. Can be helpful for slices.
710
711 \item [\textbackslash ket\{l\}, \textbackslash bra\{l\}, \textbackslash proj\{l\}, \textbackslash braket\{l\}\{m\}]
712 Typeset Dirac notation $\ket{l}$, $\bra{l}$, $\proj{l}$ and $\braket{l}{m}$ respectively. These commands do not require math mode, and the braces will automatically resize to the argument. They are defined to behave well with other packages (e.g.\ physics) that may define the same commands. You may need to be careful of the order in which you load those packages: load quantikz \emph{after} the other package --- if quantikz sees that those commands are already defined, it does not redefine them. If you wish to ensure that you are using the version that this package defines, run \textbackslash forceredefine at the end of your preamble (after all packages have loaded).
713
714 % % \item [\textbackslash alias\{n\}]
715 % % Used in an otherwise empty cell, that gives the current row a name \texttt{n}. This is useful for referring to it for the purposes of vertical positioning.
716 \end{description}
717
718
719
720
721
722
723
724 \section{Spacing}\label{sec:spacing}
725
726 \subsection{Local Adjustment}
727
728 There are several different ways in which we can manipulate the spacing of a diagram. Adding space to an individual row or column can be done in the standard way of tables in LaTeX. Here we add 2cm of space to the column between the $H$ and $X$ gates, and 1cm of space between the top two rows.
729 \begin{Code}
730 \begin{quantikz}
731 & \gate{H} &[2cm] \gate{X} & \gate{H} & \\[1cm]
732 & \gate{X} & \gate{Z} & \gate{Z} &\\
733 & \gate{X} & \gate{Z} & \gate{Z} &
734 \end{quantikz}
735 \end{Code}
736 If you don't know how much space you need, but it should be determined by the size of some text, you can use \verb!\hphantom{}! (widens the gate, in a similar way to \verb!\gate[1cm]{}!) or \verb!\hphantomgate{}! (increases the length of a wire) for horizontal spacing, and \verb!\ghost{}! for vertical spacing.
737 \begin{Code}
738 \begin{quantikz}
739 & \gate{X} \hphantom{very wide} & \gate{X} & \hphantomgate{wide} & \gate{X} &
740 \end{quantikz}
741 \end{Code}
742
743 \subsection{Global Adjustment}
744
745 Standard tikz commands facilitate a global adjustment of row and column spacing. For example, a ridiculous horizontal spacing:
746 \begin{Code}
747 \begin{quantikz}[column sep=1cm]
748 & \gate{H} & \phase{\beta} & \gate{H} &
749 \end{quantikz}
750 \end{Code}
751 This specifically adjusts the \emph{gap} between the rows and columns, not the distance between the centres of the rows and columns. Depending on what gates you have on each wire, the spacing may not be the same between each wire. Sometimes this is desirable, particularly if a gate in one particular row is much larger than anything in the other rows. At other times, it just makes your diagram look a little odd. For example, look at the gap between the top two wires and the bottom two wires:
752 \begin{Code}
753 \begin{quantikz}[row sep=0.1cm]
754 & \gate{X} & \ctrl{1} & \gate{X} & \\
755 & & \control{} & & \\
756 & \gate{X} & & & \\
757 & \gate{H} & & &
758 \end{quantikz}
759 \end{Code}
760 If you want to make sure that every quantum wire is equally spaced, do the following to \verb!row sep!:
761 \begin{Code}
762 \begin{quantikz}[row sep={0.6cm,between origins}]
763 & \gate{X} & \ctrl{1} & \gate{X} & \\
764 & & \control{} & & \\
765 & \gate{X} & & & \\
766 & \gate{H} & & &
767 \end{quantikz}
768 \end{Code}
769 This is particularly useful to achieve alignment of several circuits, as in \ref{sec:align}.
770
771
772 \subsection{Alignment}\label{sec:align}
773
774 How do we centre a circuit diagram? Simply surround it with \verb!\begin{center}! and \verb!\end{center}! commands, or within any standard equation environment.
775
776 Vertical alignment between different circuits can be more fiddly, depending on how much of a perfectionist you are. Sometimes, they work immediately, but the wires don't always align perfectly with each other. Generally the problem is that the highest gate in each row is different (here, the LHS is missing an $X$ gate on the second row)
777 \begin{Code}
778 \begin{quantikz}
779 & \gate{X} & \ctrl{1} & \\
780 & & \targ{} &
781 \end{quantikz}
782 =\begin{quantikz}
783 & \ctrl{1} & \gate{X} & \\
784 & \targ{} & \gate{X} &
785 \end{quantikz}
786 \end{Code}
787 \noindent Ensuring an even spacing between rows, as described in Sec.\ \ref{sec:spacing}, can help (but is not always appropriate). Often the easiest is to fudge it using the \verb!\ghost! command which will add a 0-width gate of the height corresponding to its argument. So, having identified the problem with the above circuit, we can replace it with:
788 \begin{Code}
789 \begin{quantikz}
790 & \gate{X} & \ctrl{1} & \\
791 & \ghost{X} & \targ{} &
792 \end{quantikz}
793 =\begin{quantikz}
794 & \ctrl{1} & \gate{X} & \\
795 & \targ{} & \gate{X} &
796 \end{quantikz}
797 \end{Code}
798
799 If you cannot identify the offending gate, and particularly if the operation is not a standard \verb!\gate! command, you might be better off combining the two circuits in a single circuit with no wires joining the two parts. You can use a \texttt{midstick} command here. By default, it places braces both before and after, but these can be replaced using the optional argument \texttt{brackets=none|left|right|both}. Thus,
800 \begin{Code}
801 \begin{quantikz}
802 & & \ctrl{1} & \midstick[2,brackets=none]{=}& \ctrl{1} & \gate{Z} & \\
803 & \gate{Z} & \targ{} & & \targ{} & \gate{Z} &
804 \end{quantikz}
805 \end{Code}
806
807 \subsubsection{Perfecting Vertical Alignment}
808
809 Sometimes when you're typesetting circuit identities as multiple separate circuits, the vertical alignment of the equals sign doesn't appear quite right (and can really niggle). Here, for example, the equals seems a bit low (because the baseline is the middle of the diagram by default, and here, the rows are not equal heights):
810 \begin{Code}
811 \begin{quantikz}
812 && \\
813 & \gate{Z} &
814 \end{quantikz}=\begin{quantikz}
815 &&&& \\
816 & \gate{H} & \gate{X} & \gate{H} &
817 \end{quantikz}
818 \end{Code}
819 To that end, we have added the key \texttt{align equals at=} option for the quantikz environment. This specifies which wire should be aligned with the equals sign. You can even use a non-integer. For instance, 1.5 will set it half way between wires 1 and 2.
820 \begin{Code}
821 \begin{quantikz}[align equals at=1.5]
822 && \\
823 & \gate{Z} &
824 \end{quantikz}=\begin{quantikz}[align equals at=1.5]
825 &&&& \\
826 & \gate{H} & \gate{X} & \gate{H} &
827 \end{quantikz}
828 \end{Code}
829 If you use the \texttt{wire types} global option, this happens automatically. You can still override it provided the \texttt{align equals at} comes \emph{after}.
830
831 \section{Styling \& Customising}\label{sec:style}
832
833 \subsection{Global Styling}
834
835 If you want to change the properties of an entire circuit such that all the typically black elements are a different colour, and the backgrounds of cells are another, you can supply the quantikz command with two keys: \verb!color! and \verb!background color!. The second of these works well alongside the \verb!\pagecolor! command for making the rest of the page a particular colour.
836 \begin{Code}
837 \begin{quantikz}[color=blue,background color=yellow]
838 \lstick{\ket{\psi}} & \gate{H} & \gate{X} & \meter{}
839 \end{quantikz}
840 \end{Code}
841
842 There are two further keys that change styles globally: `thin lines' to make the lines thin, more in keeping with what QCircuit produced, and `transparent', should you want the background of all the gates to be transparent:
843 \begin{Code}
844 \begin{quantikz}[thin lines,transparent]
845 & \ctrl{1} & & \ctrl{1} & & \\
846 & \targ{} & \gate{R_z(-\theta/2)} & \targ{} & \gate{R_z(\theta/2)} & \meter{}
847 \end{quantikz}
848 \end{Code}
849
850 Global properties that affect all circuit elements of a given type can be affected through \verb!tikzset!.
851 \begin{Code}
852 \tikzset{
853 operator/.append style={fill=red!20},
854 my label/.append style={above right,xshift=0.3cm},
855 phase label/.append style={label position=above}
856 }
857 \begin{quantikz}
858 & \gate{H} & \phase{\beta} & \gate{H} & \meter{\ket{\pm}}
859 \end{quantikz}
860 \end{Code}
861 The global styles are:
862 \begin{center}
863 \begin{tabular}{c|c}
864 Style Name & Affected Command(s) \\
865 \hline
866 operator & \verb!\gate! \\
867 meter & \verb!\meter! \\
868 slice & \verb!\slice! \\
869 wave & \verb!\wave! \\
870 leftinternal & \verb!\gateinput! \\
871 rightinternal & \verb!\gateoutput! \\
872 dm & left braces (\verb!\gateoutput!,\verb!\lstick!) \\
873 dd & right braces (\verb!\gateinput!,\verb!\rstick!) \\
874 phase & \verb!\phase!, \verb!\control!, \verb!\ophase!, \verb!\ocontrol! \\
875 circlewc & \verb!\targ! \\
876 crossx2 & \verb!\swap!,\verb!\targX! \\
877 my label & measurement bases in \verb!\meter! \\
878 phase label & phases in \verb!\phase! \\
879 gg label & main gate label in \verb!\gate! \\
880 group label & label in \verb!\gategroup!
881 \end{tabular}
882 \end{center}
883
884 \subsection{Per-Gate Styling}
885
886 Individual gates can be modified using optional arguments of the calling function.
887 \begin{Code}
888 \begin{quantikz}
889 & \gate[style={fill=red!20},label style=cyan]{H} & \phase[style={green},label style={label position=above}]{\beta} & \gate{H} & & \meter[style={draw=blue}]{\ket{\pm}}
890 \end{quantikz}
891 \end{Code}
892 The specific syntax varies a little depending on the type of gate. See the commands list for the specific cases.
893 % \begin{center}
894 % \begin{tabular}{p{0.4\textwidth}|p{0.4\textwidth}}
895 % gates that don't accept formatting parameters & \verb!\qw!,\verb!\vqw!,\verb!\qwbundle!,\verb!\cw!,\verb!\vcw!,\verb!\cwbend!,\verb!\push! \\
896 % \hline
897 % gates that accept tikz node formatting keys directly in optional argument & \verb!\phase!, \verb!\control!, \verb!\ocontrol!, \verb!\targ!, \verb!\targX!, \verb!\trash!, \verb!\meter!, \verb!\metercw!, \verb!\meterD!, \verb!\measure!, \verb!\measuretab!, \verb!\wave! \\
898 % \hline
899 % gates that accept node formatting keys as \verb!label style! key in optional argument & \verb!\gate!, \verb!\slice!, \verb!\lstick!, \verb!\rstick!, \verb!\gategroup!, \verb!\gateinput!, \verb!\gateoutput!
900 % \end{tabular}
901 % \end{center}
902 % Those that accept the \verb!label style! key also accept a second key for formatting other elements of the display
903 % \begin{center}
904 % \begin{tabular}{c|cc}
905 % \verb!\gate!,\verb!\gategroup! & \verb!style! & styles the box \\
906 % \verb!\slice! & \verb!style! & styles the line\\
907 % \verb!\lstick!,\verb!\rstick!,\verb!\gateinput!,\verb!\gateoutput! & \verb!braces! & styles the brace
908 % \end{tabular}
909 % \end{center}
910 If you want to input several styling parameters with one of the keys, just group them together in a set of curly braces, \{\}. Typical styling parameters include \verb!draw=! specifying line colour, \verb!fill=!, specifying fill colour, \verb!inner xsep=! and \verb!inner ysep=! specifying horizontal and vertical margins respectively, \verb!xshift=! and \verb!yshift=! for adjusting horizontal and vertical positioning. Beyond that, the \href{http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf}{tikz manual} is your friend!
911
912 This styling is really quite flexible, as we can override the default shapes with anything that we want:
913 \begin{Code}
914 \tikzset{
915 noisy/.style={starburst,fill=yellow,draw=red,line width=2pt,inner xsep=-4pt,inner ysep=-5pt}
916 }
917 \begin{quantikz}[row sep=0.3cm,column sep=0.3cm,wire types={q,q,q,n,n}]%
918 & \gate{H} & \ctrl{2}& & \gate[3,style={noisy},label style=cyan]{\text{noise}} & \ctrl{3} & & & \\
919 \lstick{\ket{0}} & & \targ{} & & & & \ctrl{3} & & \\
920 \lstick{\ket{0}} & & \targ{}& & & & & \ctrl{2} & \\
921 &&&&\lstick{\ket{0}} & \targ{}\setwiretype{q} & \targ{} & & \meter{} \\
922 &&&&\lstick{\ket{0}} & \setwiretype{q} & \targ{} & \targ{} & \meter{}
923 \end{quantikz}
924 \end{Code}
925 % We can also supplement with standard tikz commands by using the `execute at end picture' key. The cells of the matrix contain nodes called \verb!\tikzcdmatrixname-i-j!, where i is the row number and j is the column number. Thus,
926 % \begin{Code}
927 % \begin{center}
928 % \begin{quantikz}[row sep=0.3cm,column sep=0.3cm,execute at end picture={
929 % \node [fit=(\tikzcdmatrixname-1-6)(\tikzcdmatrixname-4-6)(\tikzcdmatrixname-5-9),draw,dashed,inner sep=4pt,label={[yshift=0.3cm]above:syndrome extraction}] {};
930 % }]
931 % & \gate{H} & \ctrl{2}& \qw & \gate[3,style={starburst,fill=yellow,draw=red,line width=2pt,inner xsep=-4pt,inner ysep=-5pt}]{\text{noise}} & \ctrl{3} & \qw & \qw& \qw\\
932 % \lstick{$\ket{0}$} & \qw & \targ{} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw \\
933 % \lstick{$\ket{0}$} & \qw & \targ{}& \qw & \qw & \qw & \qw & \ctrl{2} & \qw\\
934 % &&&&\lstick{$\ket{0}$} & \targ{} & \targ{} & \qw & \meter{} \\
935 % &&&&\lstick{$\ket{0}$} & \qw & \targ{} & \targ{} & \meter{}
936 % \end{quantikz}
937 % \end{center}
938 % \tcblower
939 % \begin{lstlisting}
940 % \begin{quantikz}[row sep=0.3cm,column sep=0.3cm,execute at end picture={
941 % \node [fit=(\tikzcdmatrixname-1-6)(\tikzcdmatrixname-4-6)(\tikzcdmatrixname-5-9),draw,dashed,inner sep=4pt,label={[yshift=0.3cm]above:syndrome extraction}] {};
942 % }]
943 % & \gate{H} & \ctrl{2}& \qw & \gate[3,style={starburst,fill=yellow,draw=red,line width=2pt,inner xsep=-4pt,inner ysep=-5pt}]{\text{noise}} & \ctrl{3} & \qw & \qw& \qw\\
944 % \lstick{$\ket{0}$} & \qw & \targ{} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw \\
945 % \lstick{$\ket{0}$} & \qw & \targ{}& \qw & \qw & \qw & \qw & \ctrl{2} & \qw\\
946 % &&&&\lstick{$\ket{0}$} & \targ{} & \targ{} & \qw & \meter{} \\
947 % &&&&\lstick{$\ket{0}$} & \qw & \targ{} & \targ{} & \meter{}
948 % \end{quantikz}
949 % \end{lstlisting}
950 % \end{Code}
951
952 \subsection{Scaling}
953
954 If you want to override the standard size of a circuit (gate elements, text and spacing), you can make it a node inside a \verb!tikzpicture!:
955 \begin{Code}
956 \begin{tikzpicture}
957 \node[scale=1.5] {
958 \begin{quantikz}
959 & \gate{H} & \phase{\beta} & \gate{H} &
960 \end{quantikz}
961 };
962 \end{tikzpicture}
963 \end{Code}
964 It's also possible to rescale to a fixed width, so long as you declare the \verb!adjustbox! package in the document preamble.
965 \begin{Code}
966 \begin{adjustbox}{width=0.8\textwidth}
967 \begin{quantikz}
968 & \gate{H} & \phase{\beta} & \gate{H} &
969 \end{quantikz}
970 \end{adjustbox}
971 \end{Code}
972
973 \section{Bells and Whistles}
974
975 Since we have built quantikz on top of tikzcd, any of the standard arrow commands will work (don't forget to turn off the default wire!). For example, after a measurement, you might want to use an arrow to report a particular measurement outcome using \verb!\arrow[r]!. The \verb!r! conveys that the arrow should head one cell to the right. You can use combinations of up (u), down (d), left (l) and right as you wish. For more styling options, see the \href{http://mirrors.ctan.org/graphics/pgf/contrib/tikz-cd/tikz-cd-doc.pdf}{tikzcd manual}.
976 \begin{Code}
977 \begin{quantikz}
978 \lstick{\ket{0}\\initial state} & & \push{X} & & \meter{0/1} \arrow[r] & \rstick{\ket{1}}\setwiretype{n}
979 \end{quantikz}
980 \end{Code}
981
982 It's perhaps worth mentioning that gate commands can include matrices.
983 \begin{Code}
984 \begin{quantikz}
985 \lstick{\ket{0}} & \gate{\frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}} & \rstick{\ket{+}}
986 \end{quantikz}
987 \end{Code}
988
989
990 \subsection{Externalization}
991
992 A large document with many quantum circuits in it can be very slow to compile. This is where externalization comes in, producing images for each circuit. (It's also helpful if you have to produce accessible versions of lecture notes!) Quantikz \emph{should} work with the externalization routines of tikz. Turn it on just as you normally would,
993 \begin{FullCode}
994 \begin{lstlisting}
995 \usetikzlibrary{external}
996 \tikzexternalize % activate!
997 \end{lstlisting}
998 \end{FullCode}
999 Note that for any circuits within an amsmath environment such as align, since these are run twice, you may end up with two copies of a given image.
1000
1001
1002 \subsection{New Gate Types}
1003
1004 One of the most common types of email I get about quantikz goes along the lines of ``Thank you for this brilliant package. I really want to use it, but I need \emph{this} specific gate shape that nobody else has ever heard of before for my own personal reasons and I can't believe you haven't implemented it already. Fix it now!''. This is impractical. However, with the latest version of quantikz, you can now use standard tikz commands in order to define your own shapes for single-qubit gates. Be warned: it can be quite fiddly.
1005
1006 The key to the process is defining your own shape (or appropriating an existing shape from the \href{https://tikz.dev/library-shapes}{shapes library}). As an example, we will use the \texttt{isosceles triangle} shape.
1007
1008 Then we need to ensure that wires will join on to your shape at the correct places. There are two different mechanisms for this. Quantum wires use the standard shape border, i.e., if you draw a straight line between the centre of two cells, the point of the join should be where this line intersects the edge of your shape. For standard shapes, this will typically just work If you're specifying your own shape, you'll need to ensure that the function \verb!\anchorborder! function works correctly, at least at the north/south/east/west points. Classical wires (and bundles) attach to different anchors on the edge of the shape. We need 16 of them (but really, 8 are just aliases of the first 8). For example, a left-going classical wire will involve drawing two horizontal lines. One starts at \texttt{lstartone} and ends at the \texttt{lendone} of the target node. The other starts at \texttt{lstarttwo} and ends at \texttt{lendtwo}. The \texttt{lstartone} anchor should be on the left-most edge of your shape, at a height \pgfkeysvalueof{/tikz/commutative diagrams/classical gap} above the center, but then you will have to calculate the correct horizontal position. Similarly, \texttt{lstarttwo} should be on the left-most edge of your shape, at a height \pgfkeysvalueof{/tikz/commutative diagrams/classical gap} below the center. The end points are the equivalent points on the right-hand edge, and will also correspond to \texttt{rstartone} and \texttt{rstarttwo}.
1009 \begin{FullCode}
1010 \begin{lstlisting}
1011 \usetikzlibrary{shapes.geometric}
1012 \makeatletter
1013
1014 %add new anchors
1015 \pgfaddtoshape{isosceles triangle}{
1016 %define the anchor lstartone.
1017 \anchor{lstartone}{%
1018 \trianglepoints %built-in function for this shape. Defines certain macros with position info
1019 %get the position of the lower left corner. Stored in macros \pgf@x and \pgf@y
1020 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}
1021 \pgf@ya=.5\pgf@y
1022 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}
1023 \pgf@y=.5\pgf@y
1024 \advance\pgf@y by \pgf@ya%
1025 \advance\pgf@y by -\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}%
1026 %by end of function, position of anchor stored in \pgf@x and \pgf@y
1027 }
1028 \anchor{lstarttwo}{%
1029 \trianglepoints
1030 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}
1031 \pgf@ya=.5\pgf@y
1032 \pgf@process{\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}
1033 \pgf@y=.5\pgf@y
1034 \advance\pgf@y by \pgf@ya%
1035 \advance\pgf@y by \pgfkeysvalueof{/tikz/commutative diagrams/classical gap}%
1036 }
1037 \anchor{lendone}{%
1038 \trianglepoints
1039 \pgf@process{\pgfpointintersectionoflines%
1040 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
1041 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}%
1042 {\pgfpointadd{\centerpoint}{\pgfpoint{0cm}{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
1043 {\pgfpointadd{\centerpoint}{\pgfpoint{0.5cm}{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
1044 }%
1045 }
1046 \anchor{lendtwo}{%
1047 \trianglepoints
1048 \pgf@process{\pgfpointintersectionoflines%
1049 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
1050 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}%
1051 {\pgfpointadd{\centerpoint}{\pgfpoint{0cm}{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
1052 {\pgfpointadd{\centerpoint}{\pgfpoint{0.5cm}{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}}}%
1053 }%
1054 }
1055 \end{lstlisting}
1056 \end{FullCode}
1057 Similarly, we need 4 anchors for the down wire.
1058 \begin{FullCode}
1059 \begin{lstlisting}
1060 %now do the anchors for a down wire
1061 \anchor{dstarttwo}{%
1062 \trianglepoints
1063 \pgf@process{\pgfpointintersectionoflines%
1064 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
1065 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}%
1066 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
1067 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
1068 }%
1069 }
1070 \anchor{dstartone}{%
1071 \trianglepoints
1072 \pgf@process{\pgfpointintersectionoflines%
1073 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
1074 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerrightanchor}{\pgfpointorigin}{\rotate}}}%
1075 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
1076 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
1077 }%
1078 }
1079 \anchor{dendtwo}{%
1080 \trianglepoints
1081 \pgf@process{\pgfpointintersectionoflines%
1082 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
1083 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}%
1084 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
1085 {\pgfpointadd{\centerpoint}{\pgfpoint{\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
1086 }%
1087 }
1088 \anchor{dendone}{%
1089 \trianglepoints
1090 \pgf@process{\pgfpointintersectionoflines%
1091 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\apexanchor}{\pgfpointorigin}{\rotate}}}%
1092 {\pgfpointadd{\centerpoint}{\pgfmathrotatepointaround{\lowerleftanchor}{\pgfpointorigin}{\rotate}}}%
1093 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0cm}}}%
1094 {\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfkeysvalueof{/tikz/commutative diagrams/classical gap}}{0.5cm}}}%
1095 }%
1096 }
1097 }
1098 \end{lstlisting}
1099 \end{FullCode}
1100 The \texttt{r} and \texttt{u} wires just go in the opposite direction to the \texttt{l} and \texttt{d} wires. So we don't need to calculate anything else, just equate them.
1101 \begin{FullCode}
1102 \begin{lstlisting}
1103 %aliases for r and u wires
1104 \pgfdeclareanchoralias{isosceles triangle}{dendtwo}{ustartwo}
1105 \pgfdeclareanchoralias{isosceles triangle}{dendone}{ustartone}
1106 \pgfdeclareanchoralias{isosceles triangle}{dstarttwo}{uendtwo}
1107 \pgfdeclareanchoralias{isosceles triangle}{dstartone}{uendone}
1108 \pgfdeclareanchoralias{isosceles triangle}{lendtwo}{rstartwo}
1109 \pgfdeclareanchoralias{isosceles triangle}{lendone}{rstartone}
1110 \pgfdeclareanchoralias{isosceles triangle}{lstarttwo}{rendtwo}
1111 \pgfdeclareanchoralias{isosceles triangle}{lstartone}{rendone}
1112 \makeatother
1113 \end{lstlisting}
1114 \end{FullCode}
1115
1116 We now have a shape that we can use as the base of a gate. It probably isn't quite styled how you might want it, so it's worth setting up a style.
1117
1118 \begin{FullCode}
1119 \begin{lstlisting}
1120 \tikzset{
1121 trimeter/.style={
1122 thickness, % gets the line width correct
1123 filling, %inherits filling. Typically background colour (default: white), but could be transparent.
1124 shape=isosceles triangle, %this is your shape name
1125 draw=black, %don't forget this, or you won't see much!
1126 inner sep=2pt
1127 }}
1128 \end{lstlisting}
1129 \end{FullCode}
1130
1131 Finally, define the code that will let you run the gate.
1132 \begin{Code}
1133 \DeclareExpandableDocumentCommand{\mygate}{O{}{m}}{%
1134 |[trimeter,#1]| {#2}}
1135
1136 \begin{quantikz}[classical gap=0.05cm]
1137 \lstick{\ket{0}} & \mygate{1} & \setwiretype{c} \\
1138 &\gate{U}\wire[u]{c} &
1139 \end{quantikz}
1140 \end{Code}
1141 Your function will work just like any of the single-qubit measurement commands: per-gate styling can be supplied via the optional argument. If your gate does not require the text label, you can remove the \texttt{\#2} from definition, but you must not remove the \texttt{\{m\}} from the argument specification.
1142
1143 It's possible to define a multi-qubit version in a very similar way, although they often end up looking a bit ugly, and non-straight edges are unlikely to fit correctly with incoming wires (which will be most noticeable if you activate transparency). It's why we don't officially have a multi-qubit version of \verb!\meterD!, although you may be able to get a good enough version working for your purposes.
1144
1145 \begin{FullCode}
1146 \begin{lstlisting}
1147 \DeclareExpandableDocumentCommand{\mygate}{O{}O{2em}O{1.5em}m}{%
1148 \gate[#1,ps=trimeter,disable auto height][#2][#3]{#4}
1149 }
1150 \end{lstlisting}
1151 \end{FullCode}
1152
1153
1154 \section{Converting from QCircuit}
1155
1156 I've updated all of my existing teaching materials from QCircuit to the original Quantikz with very little trouble. There are a few standard replacements:
1157 \begin{center}
1158 \begin{tabular}{c|c}
1159 QCircuit notation & Quantikz notation \\
1160 \hline
1161 \verb!\QCircuit @C=n @R=m {#}! & \verb!\begin{quantikz}[row sep=m,col sep=n]#\end{quantikz}! \\
1162 \verb!\multigate{n}! & \verb!\gate[n+1]! \\
1163 \verb!\targ! & \verb!\targ{}! \\
1164 \verb!\control! & \verb!\control{}! \\
1165 \verb!\meter! & \verb!\meter{}! \\
1166 \verb!\measureD! & \verb!\meterD{}! \\
1167 \end{tabular}
1168 \end{center}
1169 Updating the \verb!\gategroup! command requires a little more care since the first two arguments have to be removed, and the command placed in the correct cell, at which point \verb!\gategroup{i}{j}{k}{l}{m}! becomes
1170 \begin{center}
1171 \verb!\gategroup[k+1-i,steps=l+1-j]!.
1172 \end{center}
1173 My primary use of gategroup was to achieve the effect now achieved with \verb!\lstick[k+1-i]!.
1174
1175 It should not be necessary to use \verb!\ghost! commands in the way they were used in QCircuit.
1176
1177 \subsection{Converting from Quantikz to Quantikz2}
1178
1179 Quantikz was originally written aiming to maintain compatibility with QCircuit. However, over time, it has proved desirable to add a number of new features. Some of these have necessitated breaking this backwards compatibility. This makes upgrading a little more fiddly in (probably) rare cases.
1180
1181 \begin{itemize}
1182 \item You should always use that quantikz environment in preference to the tikzcd environment.
1183 \item Individual qw/cw commands are unnecessary. However, if there were places where you deliberately left a part of the circuit without a wire, that will need to be updated as everything has a qw by default.
1184 \item Labels on e.g.\ \verb!\meter! are now in math mode where before they were in text mode. You'll get an error if your label now starts with \$.
1185 \item \verb!\ctrlbundle! is obsolete. It will be automatically selected if appropriate. Just use \verb!\ctrl!.
1186 \item \texttt{ampersand replacement} is unlikely to work (at least in the quantikz environment), but shouldn't be necessary.
1187 \item Many of the styling options have been changed -- rather than inputting many of them directly like \verb![s]!, they can be accessed with a key \verb![style=s]!. This allows for much greater capacity for variation in the future. If you didn't use the options, then no changes are needed.
1188 \item There are some instances where the original quantikz let you get away with a call such as \verb!\targ! instead of \verb!\targ{}!. Quantikz2 tends to b less forgiving.
1189 \end{itemize}
1190
1191 % \section{Compatibility}\label{sec:compat}
1192
1193 % Quantikz defines its own versions of bra, ket, proj and braket. If you want to use your own definitions, just make sure they appear before you load the quantikz package. Other packages may also define these functions (for example, the physics package). Again, just make sure you load that package first, and your document will use those definitions. Note that there may be differences in the definition that affect the examples in this document. For example, the physics package requires the ket command to be in math mode, so you cannot just write \verb!\lstick{\ket{}}! but must instead use \verb!\lstick{$\ket{}$}!. If you want to use the quantikz definitions rather than the other definition, keep the load order the same (quantikz last) and just call \verb!\forceredefine! in the preamble.
1194
1195
1196
1197 \section{Troubleshooting}\label{sec:trouble}
1198 \begin{itemize}
1199 \item Have you checked that all commands that need them are followed by an empty argument, \{\}? Things like \verb!meter!, \verb!\control! (basically, those that can accept an optional styling parameter) look like they don't take any parameters, but they have to be followed by the pair of braces or you'll get very odd effects.
1200 \item If you get a whole bunch of unexpected text in one of your cells instead of a gate, make sure that the gate command is the first command in the cell, and that other commands (such as \verb!\qwbundle!) appear after.
1201 \item If you're getting errors about cells not being found (and especially if you're doing any slicing, or gategroups), check that your last row doesn't end with \textbackslash\textbackslash, and make sure that your last row contains as many cells (even if they're empty) as there are columns in the circuit.
1202 \item I am not aware of any specific compatibility problems with beamer, tabular, align etc.\ in the current version. If you do have any problems you might try using the tikzcd environment instead of quantikz, and applying the \texttt{ampersand replacement=\textbackslash\&} option, followed by separating all cells using \textbackslash\& instead of \&.
1203 % \item I half-expect problems if you try to put a array/matrix environment inside a gate command. But it seems to work!
1204 % \begin{Code}
1205 % \begin{center}
1206 % \begin{quantikz}
1207 % \lstick{\ket{0}} & \gate{\left(\begin{array}{cc} \alpha & \beta \\ \beta & -\alpha \end{array}\right)} & \ctrl{1} & \gate{U_1} & \\
1208 % \lstick{\ket{0}} & & \targ{} & \gate{U_2} &
1209 % \end{quantikz}
1210 % \end{center}
1211 % \tcblower
1212 % \begin{lstlisting}
1213 % \begin{quantikz}
1214 % \lstick{\ket{0}} & \gate{\left(\begin{array}{cc} \alpha & \beta \\ \beta & -\alpha \end{array}\right)} & \ctrl{1} & \gate{U_1} & \\
1215 % \lstick{\ket{0}} & & \targ{} & \gate{U_2} &
1216 % \end{quantikz}
1217 % \end{lstlisting}
1218 % \end{Code}
1219 % If you have problems
1220 % \item If you are trying to typeset your circuit inside some sort of tabular or align environment and are getting an error ``single ampersand used with wrong catcode'', trying adding the \verb![ampersand replacement=\&]! option to tikzcd, and instead of separating every cell by \&, use \textbackslash\&. This can also be useful if you want to insert a normal matrix as a gate. Incompatible with externalisation.
1221 % \begin{Code}
1222 % \begin{align*}
1223 % \begin{tikzcd}[ampersand replacement=\&,align equals at=1]
1224 % \& \gate{H} \& \gate{X} \& \gate{H} \&
1225 % \end{tikzcd}&=\begin{tikzcd}[ampersand replacement=\&,align equals at=1]
1226 % \& \gate{Z} \&
1227 % \end{tikzcd}
1228 % \end{align*}
1229 % \tcblower
1230 % \begin{lstlisting}
1231 % \begin{align*}
1232 % \begin{tikzcd}[ampersand replacement=\&,align equals at=1]
1233 % \& \gate{H} \& \gate{X} \& \gate{H} \&
1234 % \end{tikzcd}
1235 % &=
1236 % \begin{tikzcd}[ampersand replacement=\&,align equals at=1]
1237 % \& \gate{Z} \&
1238 % \end{tikzcd}
1239 % \end{align*}
1240 % \end{lstlisting}
1241 % \end{Code}
1242
1243 % \begin{Code}
1244 % \begin{center}
1245 % \begin{tikzcd}[ampersand replacement=\&]
1246 % \lstick{\ket{0}} \& \gate{\left(\begin{array}{cc} \alpha & \beta \\ \beta & -\alpha \end{array}\right)} \& \ctrl{1} \& \gate{U_1} \& \\
1247 % \lstick{\ket{0}} \& \& \targ{} \& \gate{U_2} \&
1248 % \end{tikzcd}
1249 % \end{center}
1250 % \tcblower
1251 % \begin{lstlisting}
1252 % \begin{tikzcd}[ampersand replacement=\&]
1253 % \lstick{\ket{0}} \& \gate{\left(\begin{array}{cc} \alpha & \beta \\ \beta & -\alpha \end{array}\right)} \& \ctrl{1} \& \gate{U_1} \& \\
1254 % \lstick{\ket{0}} \& \& \targ{} \& \gate{U_2} \&
1255 % \end{tikzcd}
1256 % \end{lstlisting}
1257 % \end{Code}
1258 % Alternatively, define the matrix/array as a command outside the quantikz environment.
1259 % \begin{Code}
1260 % \begin{center}
1261 % \def\tempmatrix{\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}}
1262 % \begin{quantikz}
1263 % & \gate{\tempmatrix} &
1264 % \end{quantikz}
1265 % \end{center}
1266 % \tcblower
1267 % \begin{lstlisting}
1268 % \def\tempmatrix{\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}}
1269 % \begin{quantikz}
1270 % & \gate{\tempmatrix} &
1271 % \end{quantikz}
1272 % \end{lstlisting}
1273 % \end{Code}
1274 % Beamer is particularly annoying with doing this. It probably helps to issue a global command
1275 % \begin{lstlisting}
1276 % \tikzcdset{
1277 % every matrix/.style={ampersand replacement=\&}
1278 % }
1279 % \end{lstlisting}
1280 % somewhere in your document preamble. The tikzcd manual suggests another possible strategy, but I haven't had much success with it.
1281 % \item If you're using a circuit diagram inside an \texttt{align} environment, the \texttt{tikzcd} environment works better than \texttt{quantikz}. You may also need to use the ampersand replacement strategy.
1282 % \item If you have a \verb!\cwbend! in the bottom right cell of the matrix, sometimes you get an error (I have no understanding of why). Add an extra \& after, and all seems to be well.
1283 \item If you're using transparency, and the width of gates seems to be greater than you expected, it may be worthwhile removing the .aux file and recompiling. If your tex editor isn't good at resetting the .aux file, the system may be remembering older widths.
1284 \item Package load order: I've had reports that if you load certain packages in the wrong order it can create weird errors. For example, if you load the package cleveref after quantikz, and then use a split environment, it can lead to the error ``Only one \# is allowed per tab.''. Change the load order and it goes away. I have no idea why this happens.
1285 % \item If you try to make a controlled-swap gate, you'll find a small gap in the control wire:
1286 % \begin{Code}
1287 % \begin{center}
1288 % \begin{quantikz}
1289 % & \ctrl{1} & \qw \\
1290 % & \swap{1} & \qw \\
1291 % & \targX{} & \qw
1292 % \end{quantikz}
1293 % \end{center}
1294 % \tcblower
1295 % \begin{lstlisting}
1296 % \begin{quantikz}
1297 % & \ctrl{1} & \qw \\
1298 % & \swap{1} & \qw \\
1299 % & \targX{} & \qw
1300 % \end{quantikz}
1301 % \end{lstlisting}
1302 % \end{Code}
1303 % This is easy to fudge by making the control wire longer, for example
1304 % \begin{Code}
1305 % \begin{center}
1306 % \begin{quantikz}
1307 % & \ctrl{2} & \qw \\
1308 % & \swap{1} & \qw \\
1309 % & \targX{} & \qw
1310 % \end{quantikz}
1311 % \end{center}
1312 % \tcblower
1313 % \begin{lstlisting}
1314 % \begin{quantikz}
1315 % & \ctrl{2} & \qw \\
1316 % & \swap{1} & \qw \\
1317 % & \targX{} & \qw
1318 % \end{quantikz}
1319 % \end{lstlisting}
1320 % \end{Code}
1321 \item If you are trying to submit to a Springer journal, they seem to actively prevent you from using tikz. You have to produce a separate image for each circuit. The externalisation options in tikz may be very helpful here to take your existing code and produce a pdf output of each circuit.
1322 \end{itemize}
1323
1324 For any bug reports (please make sure you've checked the above list first!) or feature requests, please contact [email protected].
1325
1326 \section{Citation}
1327
1328 If you found this package useful, please consider citing the arXiv version of this document, arXiv:1809.03842.
1329
1330
1331 \end{document}