summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/quantikz/manual.tex
blob: d89420f3bbbd18eba3e34dd40b9fc3d247665bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
\documentclass[aps,pra,10pt,nofootinbib]{revtex4}
%\pdfoutput=1
% Package(s) to include
%\listfiles
\usepackage{amsfonts,amssymb,amsmath}            % for math symbols.
\usepackage{lmodern,adjustbox}
\usepackage[skins,breakable]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{quantikz}
\tcbuselibrary{listings}
\tcbuselibrary{breakable}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true
}
\usepackage[pdftex,hyperfigures,pdfpagelabels]{hyperref} 
%\lstloadlanguages{Latex}

\newtcolorbox{Code}{enhanced,fonttitle=\sffamily\bfseries\large,valign=center
,drop fuzzy shadow,sidebyside,lefthand ratio=0.4,lower separated=false}

\begin{document}

\title{Tutorial on the Quantikz Package}
\date{\today}
\author{Alastair \surname{Kay}}
\affiliation{Royal Holloway University of London, Egham, Surrey, TW20 0EX, UK}
\email{alastair.kay@rhul.ac.uk}
\begin{abstract}
\end{abstract}
\maketitle
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!).

\section{Usage}

There is not currently a package that lets you install Quantikz automatically. Instead, you need to obtain the file tikzlibraryquantikz.code.tex (it should accompany the source code of this file, and the most recent version will always be available \href{http://dx.doi.org/10.17637/rh.7000520}{here}.) and put it in the same folder as your main tex document. Then, in the preamble of your document, simply write
\begin{verbatim}
\usepackage{tikz}
\usetikzlibrary{quantikz}
\end{verbatim}
Now, each time that you want to include a quantum circuit, you just enclose it in a \verb!tikzcd! environment.

Once you start using this for multiple projects, it gets annoying to have to create a copy every time (but remember that you'll need to include a copy for arXiv/journal submission). It might be easier to create a local texmf branch that contains the relevant file in a sub-folder \verb!tex/latex/quantikz/!. If you use MikTeX, see here for \href{https://miktex.org/howto/miktex-console}{Registering a user-managed TEXMF root directory}. A range of variants can be found on \href{https://tex.stackexchange.com/questions/1137/where-do-i-place-my-own-sty-or-cls-files-to-make-them-available-to-all-my-te}{Stack Exchange}.
\section{A Single Wire}
%\expandafter\def\expandafter\mname\expandafter{\getrandomletter}
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.
\begin{Code}
\begin{center}
\begin{tikzcd}
 & \phase{\alpha} & \gate{H} & \phase{\beta} & \gate{H} & \phase{\gamma} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \phase{\alpha} & \gate{H} & \phase{\beta} & \gate{H} & \phase{\gamma} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
Single-qubit gates take the form \verb!\gate{H}!, with any sequence of maths allowed in the argument, or, for phase gates, \verb!\phase{phase}!. The first gate should be in the second column rather than the first (so that the preceding quantum wire has somewhere to go). You {\em can} put it in the first column, it just won't have an incoming wire. 

Text can be added at the start of a quantum wire using \verb!\lstick{}!, and at the end using \verb!\rstick{}!. These are not maths environments.
\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick{$\ket{0}$} & \phase{\alpha} & \gate{H} & \phase{\beta} & \gate{H} & \phase{\gamma} & \rstick{Arbitrary\\pure state}\qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick{\ket{0}} & \phase{\alpha} & \gate{H} & \phase{\beta} & \gate{H} & \phase{\gamma} & \rstick{Arbitrary\\pure state}\qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
Notice that a multi-line \verb!lstick! or \verb!rstick! is trivial\footnote{The only issue is that the height of the label will not be taken into account in the vertical spacing of quantum wires. You may have to adjust this manually. See Section \ref{sec:align} for an example of how to achieve this.}.

\subsection{Measurements}

There are several measurement devices to choose from, and the measurement basis can be specified as the parameter:
\begin{Code}
\begin{center}
\begin{tikzcd}
& \meter{0} && \meterD{1} & & \measuretab{\text{out}}& & \measure{\text{basis}}
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \meter{0} && \meterD{1} && \measuretab{\text{out}} && \measure{\text{basis}}
\end{tikzcd}
\end{lstlisting}
\end{Code}
Thus, in our previous circuit:
\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick{$\ket{0}$} & \phase{\alpha} & \gate{H} & \phase{\beta} & \gate{H} & \phase{\gamma} & \meter{\ket{\pm}}
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick{\ket{0}} & \phase{\alpha} & \gate{H} & \phase{\beta} & \gate{H} & \phase{\gamma} & \meter{\ket{\pm}}
\end{tikzcd}
\end{lstlisting}
\end{Code}

\subsection{Wires and Arrows}

There are a few bells and whistles: \verb!\qw! connects a quantum wire from the current cell back to the previous one. Most gates have this built in (\verb!\rstick! is the notable exception). Equally, sometimes you want a `do nothing' operation, and \verb!\qw! is perfect for that. Also, you can just insert text into a cell without boxing it (as you would for a gate). This can be useful for errors, and is achieved with \verb!\push{}!. 
\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick{$\ket{0}$\\initial state} & \qw & \push{X} & \qw & \rstick{$\ket{1}$} \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick{$\ket{0}$\\initial state} & \qw & \push{X} & \qw & \rstick{$\ket{1}$} \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
Since we have built quantikz on top of tikzcd, any of the standard arrow commands will work. For example, after a measurement, you might what 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}.
\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick{$\ket{0}$\\initial state} & \qw & \push{X} & \qw & \meter{0/1} \arrow[r] & \rstick{\ket{1}}
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick{$\ket{0}$\\initial state} & \qw & \push{X} & \qw & \meter{0/1} \arrow[r] & \rstick{\ket{1}}
\end{tikzcd}
\end{lstlisting}
\end{Code}

Sometimes, you may want to group a whole bunch of wires together. Use \verb!\qwbundle! instead of \verb!\qw!.
\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick{$\ket{0}^{\otimes n}$} &[2mm] \gate{H}\qwbundle &  \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick{$\ket{0}^{\otimes n}$} &[2mm] \gate{H} \qwbundle& \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
The size of the strike can be altered by doing \verb!\pgfkeyssetvalue{/quantikz/Strike Height}{new height}! and \verb!\pgfkeyssetvalue{/quantikz/Strike Width}{new width}!. To adjust the spacing, you'll need to alter the column spacing. This can be done either globally (see later for usage of \verb!row sep! which is similar to the \verb!column sep! you'd need), or on a column by column basis, as done in the previous example with the \verb![2mm]! statement, adding 2mm of horizontal space between two columns.

You can access an alternative form for the multi-qubit wire using the \verb!alternate! key
\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick{$\ket{0}^{\otimes n}$} & \gate{H} \qwbundle[alternate]&  \qwbundle[alternate] 
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick{$\ket{0}^{\otimes n}$} & \gate{H} \qwbundle[alternate]& \control\qwbundle[alternate] 
\end{tikzcd}
\end{lstlisting}
\end{Code}

\section{Multiple Qubits}

New rows are started with \textbackslash\textbackslash. If the wires don't interact, then you proceed exactly as before. Naturally, there are several gates that induce interaction between wires: \verb!\ctrl{3}! makes a control-qubit (for controlled-not, controlled-phase, controlled-$U$, etc), where the target is 3 wires below. \verb!\octrl! is near-identical, except the control circle is open, as usually used to convey being controlled off $\ket{0}$ instead of $\ket{1}$. These will need to be matched with target commands. These could be normal gates or \verb!\targ{}! (controlled-not), \verb!\control{}! (controlled-phase), \verb!\ocontrol{}! (controlled-phase, controlled off $\ket{0}$). There is also a swap gate, \verb!\swap{2}!, with target \verb!\targX{}!. These can be combined to qive multi-controlled gates.
\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick{$\ket{0}$} & \gate{H} & \ctrl{1} & \gate{U} & \ctrl{1} & \swap{2} & \ctrl{1} & \qw  \\
\lstick{$\ket{0}$} & \gate{H} & \targ{} & \octrl{-1} & \control{} & \qw & \octrl{1} & \qw \\
&&&&&\targX{} & \gate{U} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick{$\ket{0}$} & \gate{H} & \ctrl{1} & \gate{U} & \ctrl{1} & \swap{2} & \ctrl{1} & \qw  \\
\lstick{$\ket{0}$} & \gate{H} & \targ{} & \octrl{-1} & \control{} & \qw & \octrl{1} & \qw \\
&&&&&\targX{} & \gate{U} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}

If you want a gate that is one control and several targets, you need the command \verb!\vqw{1}! (vertical quantum wire) to create vertical connections, or possibly \verb!\vcw{1}! to create the classical version.

\begin{Code}
\begin{center}
\begin{tikzcd}
& \targ{} & \gate{U} & \qw \\
& \ctrl{1} \vqw{-1} & \meter{} \vcw{-1} \\
& \targ{} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \targ{} & \gate{U} & \qw \\
& \ctrl{1} \vqw{-1} & \meter{} \vcw{-1} \\
& \targ{} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
If you want to use classical controls with both horizontal and vertical components, you can do the following.
\begin{Code}
\begin{center}
\begin{tikzcd}
& \ctrl{1} & \qw & \gate{U} & \gate{V} \\
& \targ{} \vqw{1} & \meter{} & \cwbend{-1}  \\
& \targ{} & \meter{} & \cw & \cwbend{-2} &
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \ctrl{1} & \qw & \gate{U} & \gate{V} \\
& \targ{} \vqw{1} & \meter{} & \cwbend{-1} \\
& \targ{} & \meter{} & \cw & \cwbend{-2} &
\end{tikzcd}
\end{lstlisting}
\end{Code}

\section{Operating on Many Qubits}

We have already met the \verb!\lstick!, \verb!\rstick!, and \verb!\gate! commands. By default, these all act on a single quantum wire. However, they also take the optional parameter \verb!wires=n! to specify that they should extend over $n$ wires. The syntax is otherwise unchanged.

\begin{Code}
\begin{center}
\begin{tikzcd}
\lstick[wires=3]{$\ket{0}^{\otimes 3}$} & \ctrl{1} & \meter{} \\
 & \gate[wires=2]{U} & \qw\rstick[wires=2]{\ket{11}} \\ %
 &  & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
\lstick[wires=3]{$\ket{0}^{\otimes 3}$} & \ctrl{1} & \meter{} \\
 & \gate[wires=2]{U} & \qw\rstick[wires=2]{\ket{11}} \\ %
 &  & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}

If you want to add extra width on the \verb!\gate!, you can either alter the text by adding spaces either side, or you could use an \verb!\hphantom! command in one of the underlying cells.
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate{H} & \ctrl{1} & \meter{} \\
& \gate[wires=2]{U}  & \targ{} & \qw \\
& \hphantom{wide label} & \qw & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate{H} & \ctrl{1} & \meter{} \\
& \gate[wires=2]{U}  & \targ{} & \qw \\
& \hphantom{wide label} & \qw & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
Often, it is nice to be able to label the inputs and outputs on the box, which we do with the \verb!\gateinput! and \verb!\gateoutput! commands. These commands take 1 compulsory argument -- the label text. By default, this applies to a single wire, but you can again use the optional \verb!wires! parameter to extend it. The starting point is determined by where the command is placed.
\begin{Code}
\begin{center}
\begin{tikzcd}
&\ctrl{1} & \qw \\
&\gate[wires=3]{U}
	\gateinput[wires=2]{$x$}
	\gateoutput[wires=2]{$x$}
	&\qw \\
&\hphantom{U very long} & \qw \\
&\gateinput{$y$}
\gateoutput{$y\oplus f(x)$}
&\qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
&\ctrl{1} & \qw \\
&\gate[wires=3]{U}
	\gateinput[2]{$x$}
	\gateoutput[wires=2]{$x$}&\qw \\
&\hphantom{U very long} \qw & \qw \\
&\qw\gateinput{$y$}\gateoutput{$y\oplus f(x)$}&\qw
\end{tikzcd}
\end{lstlisting}
\end{Code}

\section{Slicing}

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.
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate{H}\slice{step}  & \ctrl{1} & \meter{} \\
& \qw       & \targ{} & \ctrl{1} & \gate{H} & \qw \\
& \qw & \qw & \targ{} & \qw & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate{H}\slice{step}  & \ctrl{1} & \meter{} \\
& \qw       & \targ{} & \ctrl{1} & \gate{H} & \qw \\
& \qw & \qw & \targ{} & \qw & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
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).
\begin{Code}
\begin{center}
\begin{tikzcd}[slice all]
& \gate{H} & \ctrl{1} & \meter{} && \\
& \qw       & \targ{} & \ctrl{1} & \gate{H} & \qw \\
& \qw & \qw & \targ{} & \qw & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[slice all]
& \gate{H}  & \ctrl{1} & \meter{} && \\
& \qw       & \targ{} & \ctrl{1} & \gate{H} & \qw \\
& \qw & \qw & \targ{} & \qw & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
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 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!
\begin{Code}
\begin{center}
\begin{tikzcd}[slice all,remove end slices=1,slice titles=slice \col,slice style=blue,slice label style={inner sep=1pt,anchor=south west,rotate=40}]
& \gate{H} & \ctrl{1} & \meter{} && \\
& \qw       & \targ{} & \ctrl{1} & \gate{H} & \qw \\
& \qw & \qw & \targ{} & \qw & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[slice all,remove end slices=1,slice titles=slice \col,slice style=blue,slice label style={inner sep=1pt,anchor=south west,rotate=40}]
& \gate{H}  & \ctrl{1} & \meter{} && \\
& \qw       & \targ{} & \ctrl{1} & \gate{H} & \qw \\
& \qw & \qw & \targ{} & \qw & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
\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.

\section{Typesetting}

\subsection{Alignment}\label{sec:align}

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.

Vertical alignment between different circuits can be more fiddly, depending on how much of a perfectionist you are. Sometimes, they work immediately:
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate{X} & \ctrl{1} & \qw \\
& \qw & \targ{} & \qw
\end{tikzcd}
=\begin{tikzcd}
& \ctrl{1} & \gate{X} & \qw \\
& \targ{}  & \gate{X} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate{X} & \ctrl{1} & \qw \\
& \qw & \targ{} & \qw
\end{tikzcd}
=\begin{tikzcd}
& \ctrl{1} & \gate{X} & \qw \\
& \targ{}  & \gate{X} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
\noindent but the wires don't always align perfectly with each other. Ensuring an even spacing between rows can help (but is not always appropriate). See Sec.\ \ref{sec:space}.

You might be better off writing it as a single quantum circuit with no wires joining the two parts. The spacing works better if your circuit has an odd number of wires, otherwise you have to add an extra row in the middle, and then change the spacing of that row with its neighbours.
\begin{Code}
\begin{center}
\begin{tikzcd}
& \qw & \ctrl{2} & \qw&&& \ctrl{2} & \gate{Z} & \qw \\[-0.3cm]
&&&&=&\\[-0.3cm]
& \gate{Z} & \targ{} & \qw&&& \targ{}  & \gate{Z} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \qw & \ctrl{2} & \qw&&& \ctrl{2} & \gate{Z} & \qw \\[-0.3cm]
&&&&=&\\[-0.3cm]
& \gate{Z} & \targ{} & \qw&&& \targ{}  & \gate{Z} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}



\subsection{Scaling}
If you want to override the standard size of a circuit, you can make it a node inside a \verb!tikzpicture!:
\begin{Code}
\begin{center}
\begin{tikzpicture}
\node[scale=2] {
\begin{tikzcd}
& \gate{H} & \phase{\beta} & \gate{H} & \qw
\end{tikzcd}
};
\end{tikzpicture}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzpicture}
\node[scale=2] {
\begin{tikzcd}
& \gate{H} & \phase{\beta} & \gate{H} & \qw
\end{tikzcd}
};
\end{tikzpicture}
\end{lstlisting}
\end{Code}
It's also possible to rescale to a fixed width, so long as you declare the \verb!adjustbox! package in the document preamble.
\begin{Code}
\begin{center}
\begin{adjustbox}{width=0.8\textwidth}
\begin{tikzcd}
& \gate{H} & \phase{\beta} & \gate{H} & \qw
\end{tikzcd}
\end{adjustbox}
\end{center}
\tcblower
\begin{lstlisting}
\begin{adjustbox}{width=0.8\textwidth}
\begin{tikzcd}
& \gate{H} & \phase{\beta} & \gate{H} & \qw
\end{tikzcd}
\end{adjustbox}
\end{lstlisting}
\end{Code}

\subsection{Spacing}\label{sec:space}
If you wish, it is possible to adjust spacing in the circuits using standard tikz commands. For example, a ridiculous horizontal spacing:
\begin{Code}
\begin{center}
\begin{tikzcd}[column sep=1cm]
& \gate{H} & \phase{\beta} & \gate{H} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[row sep=0.5cm,column sep=1cm]
& \gate{H} & \phase{\beta} & \gate{H} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
For horizontal spacing of an individual column, use \verb!\hphantom{}! if it's a place where there's already a gate, or there shouldn't be a quantum wire. If there's only a quantum wire, use \verb!\hphantomgate{}!. For spacing of an individual row, see the example on typesetting equation identities in Sec.\ \ref{sec:align}.

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. If you want to make sure that every quantum wire is equally spaced, do the following to \verb!row sep!:
\begin{Code}
\begin{center}
\begin{tikzcd}[row sep={1cm,between origins}]
\lstick[wires=3]{$\ket{0}^{\otimes n}$} & \gate{H} & \gate[wires=4]{O}\gateinput[wires=3]{$x$}
\gateoutput[wires=3]{$x$} & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
\lstick{$\ket{1}$} & \gate{H} & \phantom{really wide}\gateinput{$y$}\gateoutput{$y\oplus f(x)$} & \gate{H} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[row sep={1cm,between origins}]
\lstick[wires=3]{$\ket{0}^{\otimes n}$} & \gate{H} & \gate[wires=4]{O}\gateinput[wires=3]{$x$}
\gateoutput[wires=3]{$x$} & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
\lstick{$\ket{1}$} & \gate{H} & \phantom{really wide}\gateinput{$y$}\gateoutput{$y\oplus f(x)$} & \gate{H} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
Strictly, the \verb!wires=! statement should not be necessary, as this is presumed to be the default key. Thus, we can get the same results with
\begin{Code}
\begin{center}
\begin{tikzcd}[row sep={1cm,between origins}]
\lstick[3]{$\ket{0}^{\otimes n}$} & \gate{H} & \gate[4]{O}\gateinput[3]{$x$}
\gateoutput[3]{$x$} & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
\lstick{$\ket{1}$} & \gate{H} & \phantom{really wide}\gateinput{$y$}\gateoutput{$y\oplus f(x)$} & \gate{H} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[row sep={1cm,between origins}]
\lstick[3]{$\ket{0}^{\otimes n}$} & \gate{H} & \gate[4]{O}\gateinput[3]{$x$}
\gateoutput[3]{$x$} & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
 & \gate{H} &  & \gate{H} & \meter{}	\\
\lstick{$\ket{1}$} & \gate{H} & \phantom{really wide}\gateinput{$y$}\gateoutput{$y\oplus f(x)$} & \gate{H} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}

\subsection{Global Styling}

Global properties that affect all circuit elements of a given type can be affected through \verb!tikzset!.
\begin{Code}
\begin{center}
\tikzset{
	operator/.append style={fill=red!20},
	my label/.append style={above right,xshift=0.3cm}
}
\begin{tikzcd}
& \gate{H} & \phase{\beta} & \gate{H} & \qw & \meter{\ket{\pm}}
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\tikzset{
	operator/.append style={fill=red!20},
	my label/.append style={above right,xshift=0.3cm}
}
\begin{tikzcd}
& \gate{H} & \phase{\beta} & \gate{H} & \qw & \meter{\ket{\pm}}
\end{tikzcd}
\end{lstlisting}
\end{Code}
The global styles are:
\begin{center}
\begin{tabular}{c|c}
Style Name & Affected Command(s)	\\
\hline
operator & \verb!\gate!	\\
meter & \verb!\meter! \\
slice & \verb!\slice! \\
wave & \verb!\wave! \\
leftinternal	& \verb!\gateinput! \\
rightinternal	& \verb!\gateoutput! \\
dm & left braces (\verb!\gateoutput!,\verb!\lstick!) \\
dd & right braces (\verb!\gateinput!,\verb!\rstick!) \\
phase & \verb!\phase!, \verb!\control! \\
ophase & \verb!\ophase!, \verb!\ocontrol! \\
circlewc & \verb!\targ! \\
crossx2 & \verb!\swap!,\verb!\targX! \\
my label & measurement bases in \verb!\meter! \\
phase label & phases in \verb!\phase! \\
gg label & main gate label in \verb!\gate! \\
group label & label in \verb!\gategroup!
\end{tabular}
\end{center}

\subsection{Per-Gate Styling}

Individual gates can be modified using optional arguments of the calling function. 
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate[style={fill=red!20},label style=cyan]{H} & \phase[green]{\beta} & \gate{H} & \qw & \meter{\ket{\pm}}
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate[style={fill=red!20},label style=cyan]{H} & \phase[green]{\beta} & \gate{H} & \qw & \meter{\ket{\pm}}
\end{tikzcd}
\end{lstlisting}
\end{Code}
The specific syntax varies a little depending on the type of gate
\begin{center}
\begin{tabular}{p{0.4\textwidth}|p{0.4\textwidth}}
gates that don't accept formatting parameters & \verb!\qw!,\verb!\vqw!,\verb!\qwbundle!,\verb!\cw!,\verb!\vcw!,\verb!\cwbend!,\verb!\push!	\\
\hline
gates that accept tikz node formatting keys directly in optional argument & \verb!\phase!, \verb!\control!, \verb!\ocontrol!, \verb!\targ!, \verb!\targX!, \verb!\meter!, \verb!\meterD!, \verb!\measure!, \verb!\measuretab!, \verb!\wave! \\
\hline
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!
\end{tabular}
\end{center}
Those that accept the \verb!label style! key also accept a second key for formatting other elements of the display
\begin{center}
\begin{tabular}{c|cc}
\verb!\gate!,\verb!\gategroup! & \verb!style! & styles the box	\\
\verb!\slice! & \verb!style! & styles the line\\
\verb!\lstick!,\verb!\rstick!,\verb!\gateinput!,\verb!\gateoutput! & \verb!braces! & styles the brace
\end{tabular}
\end{center}
If you want to input several styling parameters with one of these 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!

This styling is really quite flexible, as we can override the default shapes with anything that we want:
\begin{Code}
\begin{center}
\tikzset{
	group-1-5/.append style={starburst,fill=yellow,draw=red,line width=2pt,inner ysep=-15pt}
}
\begin{tikzcd}[row sep=0.3cm,column sep=0.4cm]%
& \gate{H} & \ctrl{2}& \qw & \gate[3,style={starburst,fill=yellow,draw=red,line width=2pt,inner xsep=-2pt,inner ysep=-5pt},label style=cyan]{\text{noise}} & \ctrl{3} & \qw  & \qw & \qw\\
\lstick{$\ket{0}$} & \qw & \targ{} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw \\
\lstick{$\ket{0}$} & \qw & \targ{}& \qw & \qw & \qw & \qw & \ctrl{2} & \qw\\
&&&&\lstick{$\ket{0}$} & \targ{} & \targ{} & \qw & \meter{} \\
&&&&\lstick{$\ket{0}$} & \qw & \targ{} & \targ{} & \meter{} 
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[row sep=0.3cm,column sep=0.4cm]%
& \gate{H} & \ctrl{2}& \qw & \gate[3,style={starburst,fill=yellow,draw=red,line width=2pt,inner xsep=-2pt,inner ysep=-5pt},label style=cyan]{\text{noise}} & \ctrl{3} & \qw  & \qw& \qw\\
\lstick{$\ket{0}$} & \qw & \targ{} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw \\
\lstick{$\ket{0}$} & \qw & \targ{}& \qw & \qw & \qw & \qw & \ctrl{2} & \qw\\
&&&&\lstick{$\ket{0}$} & \targ{} & \targ{} & \qw & \meter{} \\
&&&&\lstick{$\ket{0}$} & \qw & \targ{} & \targ{} & \meter{} 
\end{tikzcd}
\end{lstlisting}
\end{Code}
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,
\begin{Code}
\begin{center}
\begin{tikzcd}[row sep=0.3cm,column sep=0.4cm,execute at end picture={
	\node [fit=(\tikzcdmatrixname-1-6)(\tikzcdmatrixname-4-6)(\tikzcdmatrixname-5-9),draw,dashed,inner sep=4pt,label={[yshift=0.3cm]above:syndrome extraction}] {};
}]
& \gate{H} & \ctrl{2}& \qw & \gate[3,style={starburst,fill=yellow,draw=red,line width=2pt,inner xsep=-2pt,inner ysep=-5pt}]{\text{noise}} & \ctrl{3} & \qw  & \qw& \qw\\
\lstick{$\ket{0}$} & \qw & \targ{} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw \\
\lstick{$\ket{0}$} & \qw & \targ{}& \qw & \qw & \qw & \qw & \ctrl{2} & \qw\\
&&&&\lstick{$\ket{0}$} & \targ{} & \targ{} & \qw & \meter{} \\
&&&&\lstick{$\ket{0}$} & \qw & \targ{} & \targ{} & \meter{} 
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[row sep=0.3cm,column sep=0.4cm,execute at end picture={
	\node [fit=(\tikzcdmatrixname-1-6)(\tikzcdmatrixname-4-6)(\tikzcdmatrixname-5-9),draw,dashed,inner sep=4pt,label={[yshift=0.3cm]above:syndrome extraction}] {};
}]
& \gate{H} & \ctrl{2}& \qw & \gate[3,style={starburst,fill=yellow,draw=red,line width=2pt,inner xsep=-2pt,inner ysep=-5pt}]{\text{noise}} & \ctrl{3} & \qw  & \qw& \qw\\
\lstick{$\ket{0}$} & \qw & \targ{} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw \\
\lstick{$\ket{0}$} & \qw & \targ{}& \qw & \qw & \qw & \qw & \ctrl{2} & \qw\\
&&&&\lstick{$\ket{0}$} & \targ{} & \targ{} & \qw & \meter{} \\
&&&&\lstick{$\ket{0}$} & \qw & \targ{} & \targ{} & \meter{} 
\end{tikzcd}
\end{lstlisting}
\end{Code}

\subsection{Boxing/Highlighting Parts of a Circuit}

In fact, this boxing command is sufficiently useful that we implement it as \verb!\gategroup!. The optional parameters \verb!wires! (again, 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.
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate{H} & \ctrl{1} & \gate{H}\gategroup[wires=2,steps=3,style={inner sep=6pt}]{reversed c-{\sc not}} & \ctrl{1} & \gate{H} & \ctrl{1} & \qw & \qw \\
& \qw & \targ{} & \gate{H} & \targ{} & \gate{H} & \targ{} & \gate{H} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate{H} & \ctrl{1} & \gate{H}\gategroup[wires=2,steps=3,style={inner sep=6pt}]{reversed c-{\sc not}} & \ctrl{1} & \gate{H} & \ctrl{1} & \qw & \qw \\
& \qw & \targ{} & \gate{H} & \targ{} & \gate{H} & \targ{} & \gate{H} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
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.
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate{H} & \ctrl{1}\gategroup[2,steps=3,style={dashed,rounded corners,fill=blue!20, inner xsep=2pt},background]{{\sc swap}} & \targ{} & \ctrl{1} & \qw & \qw \\
& \qw & \targ{} & \ctrl{-1} & \targ{} & \gate{H} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate{H} & \ctrl{1}\gategroup[2,steps=3,style={dashed,rounded corners,fill=blue!20, inner xsep=2pt},background]{{\sc swap}} & \targ{} & \ctrl{1} & \qw & \qw \\
& \qw & \targ{} & \ctrl{-1} & \targ{} & \gate{H} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
The \verb!label style! key can be used to tune the label's properties, including positioning. For example,
\begin{Code}
\begin{center}
\begin{tikzcd}
& \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} & \qw & \qw \\
& \qw & \targ{} & \ctrl{-1} & \targ{} & \gate{H} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \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} & \qw & \qw \\
& \qw & \targ{} & \ctrl{-1} & \targ{} & \gate{H} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
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.


At this point, we mention an extra option for the gate command. The \verb!swap! keyword turns the gate into a variant of a {\sc swap} gate which explicitly shows the wires (the \verb!wires! setting is fixed to 2, overriding any value you might supply).
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate{H} & \gate[swap]{} & \qw & \qw \\
& \qw &  & \gate{H} & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate{H} & \gate[swap]{} & \qw & \qw \\
& \qw &  & \gate{H} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
\section{Otherwise Undocumented Features}
A wave for separating rows. Do we really need this? Perhaps not, but it's fun!
\begin{Code}
\begin{center}
\begin{tikzcd}
& \gate{H} & \ctrl{3} & \ \ldots\ \qw & \qw & \gate{H} & \qw \\
\wave&&&&&&\\
& \gate{H} & \qw & \ \ldots\ \qw & \ctrl{1} & \gate{H} & \qw \\
& \qw & \gate{U} & \ \ldots\ \qw & \gate{U^k} & \qw & \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}
& \gate{H} & \ctrl{3} & \ \ldots\ \qw & \qw & \gate{H} & \qw \\
\wave&&&&&&\\
& \gate{H} & \qw & \ \ldots\ \qw & \ctrl{1} & \gate{H} & \qw \\
& \qw & \gate{U} & \ \ldots\ \qw & \gate{U^k} & \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}

\section{Converting from QCircuit}

I've updated all of my existing teaching materials from QCircuit to Quantikz with very little trouble. There are a few standard replacements:
\begin{center}
\begin{tabular}{c|c}
QCircuit notation & Quantikz notation \\
\hline
\verb!\QCircuit @C=n @R=m {#}! & \verb!\begin{tikzcd}[row sep=m,col sep=n]#\end{tikzcd}!	\\
\verb!\multigate{n}! & \verb!\gate[n+1]!	\\
\verb!\targ! & \verb!\targ{}! \\
\verb!\control! & \verb!\control{}! \\
\verb!\meter! & \verb!\meter{}! \\
\verb!\measureD! & \verb!\meterD{}! \\
\end{tabular}
\end{center}
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 \verb!\gategroup[wires=k+1-i,steps=l+1-j]!. My primary use of gategroup was to achieve the effect now achieved with \verb!\lstick[k+1-i]!.

\section{Troubleshooting}
\begin{itemize}
\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.
\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.
\item If you're getting errors about cells not being found (and especially if you're doing any slicing), 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 matrix.
\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
\begin{Code}
\begin{center}
\begin{tikzcd}[ampersand replacement=\&]
\lstick{\ket{0}} \& \gate{\left(\begin{array}{cc} \alpha & \beta \\ \beta & -\alpha \end{array}\right)} \& \ctrl{1} \& \gate{U_1} \& \qw \\
\lstick{\ket{0}} \& \qw \& \targ{} \& \gate{U_2} \& \qw
\end{tikzcd}
\end{center}
\tcblower
\begin{lstlisting}
\begin{tikzcd}[ampersand replacement=\&]
\lstick{\ket{0}} \& \gate{\left(\begin{array}{cc} \alpha & \beta \\ \beta & -\alpha \end{array}\right)} \& \ctrl{1} \& \gate{U_1} \& \qw \\
\lstick{\ket{0}} \& \qw \& \targ{} \& \gate{U_2} \& \qw
\end{tikzcd}
\end{lstlisting}
\end{Code}
Beamer is particularly annoying with doing this. It probably helps to issue a global command
\begin{lstlisting}
\tikzcdset{
	every matrix/.style={ampersand replacement=\&}
}
\end{lstlisting}
somewhere in your document preamble. The tikzcd manual suggests another possible strategy, but I haven't had much success with it.
\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.
\end{itemize}
\end{document}