Debian Codegraph @

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

raw
1 % !TeX spellcheck = en_US
2 \documentclass{scrartcl}
3
4 \title{yquant.sty package documentation}
5 \subtitle{Typesetting quantum circuits in a human-readable language}
6 \author{Benjamin Desef}
7
8 % BEGIN_FOLD Packages
9 \RequirePackage{fontspec}
10 \setmainfont{XCharter}[Scale=.98]
11 \RequirePackage[osf, semibold]{sourcesanspro}
12 \usepackage{mathtools}
13 \RequirePackage[math-style=ISO, bold-style=ISO, sans-style=italic]{unicode-math}
14
15 \usepackage{xcolor}
16 \definecolor{darkblue}{RGB}{5, 10, 122}
17 \usepackage[english]{babel}
18 \usepackage[colorlinks, linkcolor=darkblue, bookmarksdepth=paragraph]{hyperref}
19 \usepackage[compat=newest]{yquant}
20 \usepackage{braket,hyphenat,microtype,hologo,minted2,import,ragged2e,booktabs,bookmark}
21 \usetikzlibrary{quotes,fit,shapes.symbols,backgrounds,quantikz2,patterns}
22 \useyquantlanguage{qasm,groups}
23 \usepackage{amsmath,adjustbox,cleveref}
24 \usepackage[framemethod=tikz]{mdframed}
25 % END_FOLD
26
27 \makeatletter
28
29 % if labels are not built correctly, a bugfix to AMS is already in the kernel, but the corresponding fix to cleveref not yet. Then apply (https://tex.stackexchange.com/a/737207/32357)
30 \AtBeginDocument
31 {
32 \def\ltx@label#1{\cref@label{#1}}%add braces
33 \def\label@in@display@noarg#1{\cref@old@label@in@display{#1}}%remove braces
34 \def\label@in@mmeasure@noarg#1{%
35 \begingroup%
36 \measuring@false%
37 \cref@old@label@in@display{#1}%remove braces
38 \endgroup}%
39 } %
40
41 %\long\def\compat#1{\texorpdfstring{\leavevmode\makenote{#1}\penalty10000 }{}}
42 \let\compat=\@gobble
43
44 % BEGIN_FOLD Minted configuration
45 \definecolor{boxGrayBody}{gray}{.94}
46 \definecolor{boxRedHead} {RGB} {178, 34, 34}
47 \definecolor{boxRedBody} {RGB} {255, 255, 192}
48 \definecolor{boxBlueHead}{RGB} {118, 152, 199}
49 \definecolor{boxBlueBody}{RGB} {242, 247, 255}
50 \definecolor{mintgreen} {RGB} {11, 97, 11}
51 \setminted{frame=none, linenos=false, fontsize=\footnotesize, breaklines, bgcolor=boxGrayBody}
52 \setmintedinline{bgcolor={}, fontsize=}
53
54 \newmintinline[yquant]{text}{}
55 \newmintinline[tex]{tex}{}
56 \newmintinline[ebnf]{ebnf}{}
57 % END_FOLD
58
59 % BEGIN_FOLD Block environments (options, code examples, ...)
60 \def\unskipOpt{\vskip-5mm }
61 \def\MintedPygmentize{mamba run -n base pygmentize}
62 \renewcommand{\TestAppExists}[1]{\AppExiststrue}%
63 \ifdefined\minted@inline@iii
64 % < v2.7
65 \let\unpatched@minted@inline@iii=\minted@inline@iii%
66 \def\patched@minted@inline@iii#1{%
67 \unpatched@minted@inline@iii{#1}%
68 \par%
69 \endgroup\egroup\nopagebreak\vskip-\parskip\nopagebreak%
70 \noindent\ignorespaces%
71 }
72 \def\patch@mintinline{%
73 \let\minted@inline@iii=\patched@minted@inline@iii%
74 }
75 \else
76 % v2.7
77 \let\unpatched@RobustMintInlineProcess@ii=\RobustMintInlineProcess@ii
78 \def\patched@RobustMintInlineProcess@ii#1{%
79 \unpatched@RobustMintInlineProcess@ii{#1}%
80 \par%
81 \endgroup\egroup\nopagebreak\vskip-\parskip\nopagebreak%
82 \noindent\ignorespaces%
83 }
84 \def\patch@mintinline{%
85 \let\RobustMintInlineProcess@ii=\patched@RobustMintInlineProcess@ii%
86 }
87 \fi
88 \newenvironment{option*}[3][]{\ifhmode\par\fi\addvspace{5mm}%
89 \vbox\bgroup\begingroup%
90 \patch@mintinline%
91 \raggedleft%
92 \hspace*{-1cm}%
93 \phantomsection\label{yquant:/#2/#3}%
94 \texttt{\ifstrempty{#1}\relax{\compat{#1}}\textcolor{gray}{/#2/}\textcolor{mintgreen}{#3}}\hfill default: %
95 \tex}{\par\addvspace{5mm}}
96 \newenvironment{option}[1][]{\csname option*\endcsname[{#1}]{yquant}}{\csname endoption*\endcsname}
97
98 \newsavebox\codeexamplebox
99 \newenvironment{codeexample}{%
100 \VerbatimEnvironment%
101 \let\FVB@VerbatimOut\minted@FVB@VerbatimOut
102 \let\FVE@VerbatimOut\minted@FVE@VerbatimOut
103 \minted@configlang{tex}%
104 \minted@fvset
105 \begin{VerbatimOut}[codes={\catcode`\^^I=12},firstline,lastline]{\[email protected]}%
106 }{
107 \end{VerbatimOut}%
108 \minted@langlinenoson%
109 \savebox\codeexamplebox{\input \[email protected]}%
110 \ifdim\wd\codeexamplebox>\dimexpr.5\linewidth-3mm\relax%
111 \wd\codeexamplebox=.5\linewidth% squeeze if necessary
112 \else%
113 \wd\codeexamplebox=\dimexpr\wd\codeexamplebox+3mm\relax% else give some space
114 \fi%
115 \noindent\begin{minipage}{\wd\codeexamplebox}%
116 \centering%
117 \usebox\codeexamplebox%
118 \end{minipage}%
119 \begin{minipage}{\dimexpr\linewidth-\wd\codeexamplebox\relax}%
120 \expandafter\minted@pygmentize\expandafter{\minted@lang}%
121 \end{minipage}%
122 \minted@langlinenosoff%
123 \par%
124 }
125
126 \newenvironment{codeexample*}{%
127 \VerbatimEnvironment%
128 \let\FVB@VerbatimOut\minted@FVB@VerbatimOut
129 \let\FVE@VerbatimOut\minted@FVE@VerbatimOut
130 \minted@configlang{tex}%
131 \minted@fvset
132 \begin{VerbatimOut}[codes={\catcode`\^^I=12},firstline,lastline]{\[email protected]}%
133 }{
134 \end{VerbatimOut}%
135 \minted@langlinenoson%
136 \begin{adjustbox}{center}
137 \input \[email protected] %
138 \end{adjustbox}\nopagebreak
139 \expandafter\minted@pygmentize\expandafter{\minted@lang}%
140 \minted@langlinenosoff%
141 \par%
142 }
143
144 \def\bd@boxextra#1{%
145 \node[anchor=north east, overlay] at (current bounding box.north west) {#1\hskip7pt};%
146 }
147
148 \newcommand*\bd@boxintro[5]{%
149 \begingroup%
150 \setlength{\parskip}{0pt}%
151 \setlength{\parindent}{0pt}%
152 \edef\tmp{#5}%
153 \ifx\tmp\empty%
154 \expandafter\@firstoftwo%
155 \else%
156 \expandafter\@secondoftwo%
157 \fi{%
158 \begin{mdframed}[style=bdBox, backgroundcolor=#4, linecolor=#2,
159 firstextra=\bd@boxextra{#1}, singleextra=\bd@boxextra{#1}]%
160 }{%
161 \begin{mdframed}[style=bdBox, backgroundcolor=#4,
162 frametitlefont=\bfseries\sffamily\color{#3},
163 frametitlebackgroundcolor=#2,
164 frametitle={\strut\tmp}, linecolor=#2,
165 firstextra=\bd@boxextra{#1}, singleextra=\bd@boxextra{#1}]%
166 }%
167 \ignorespaces%
168 }
169 \def\bd@boxoutro{%
170 \end{mdframed}%
171 \endgroup%
172 }
173 \iffalse% code folding breaks
174 \end{mdframed}
175 \fi
176 \mdfdefinestyle{bdBox}{
177 skipabove=3pt,
178 skipbelow=0pt,
179 leftmargin=0pt,
180 rightmargin=0pt,
181 innerleftmargin=7pt,
182 innerrightmargin=7pt,
183 innertopmargin=5pt,
184 innerbottommargin=5pt,
185 splittopskip=17pt,
186 frametitlealignment=\raggedright,
187 frametitleaboveskip=3pt,
188 frametitlebelowskip=1.5pt
189 }
190 \newenvironment*{warning}[1][]{%
191 \bd@boxintro{\raisebox{\baselineskip-\height-6.5pt}{\includegraphics[width=1cm]{Warning.pdf}}}%
192 {boxRedHead}{white}{boxRedBody}{\unexpanded{#1}}%
193 \ignorespaces%
194 }{%
195 \bd@boxoutro%
196 }
197 \def\example{\@ifnextchar<\example@i{\example@i<>}}
198 \def\example@i<#1>{\@ifnextchar[{\example@ii<{#1}>}{\example@ii<{#1}>[]}}
199 \long\def\example@ii<#1>[#2]{%
200 \bd@boxintro{\ifstrempty{#1}{}{\ifstrempty{#2}{\compat{#1}}{}}%
201 \raisebox{\baselineskip-\height-6.5pt}{%
202 \includegraphics[width=1cm]{Bulb.png}%
203 }}{boxBlueHead}{white}{boxBlueBody}%
204 {\ifstrempty{#1}{}{\ifstrempty{#2}{}{\unexpanded{\compat{#1}}}}\unexpanded{#2}}%
205 \ignorespaces%
206 }
207 \let\endexample=\bd@boxoutro
208 % END_FOLD
209
210 \def\TikZ{\textcolor{brown}{Ti\textit kZ}}
211 \def\pkg#1{\textcolor{brown}{\texttt{#1}}}
212 \def\langlink#1{\hyperref[sec:foreign:#1]{\pkg{#1}}}
213 \begingroup
214 \catcode`\+=0
215 +catcode`+\=12
216 +global+let+sanitize@link@backslash=\
217 +endgroup
218 \def\sanitize@link#1{yquant:\expandafter\sanitize@link@i\detokenize{#1}\sanitize@link@end}
219 \def\sanitize@link@i#1#2\sanitize@link@end{%
220 \ifx#1\sanitize@link@backslash%
221 MACRO%
222 \else%
223 #1%
224 \fi%
225 #2%
226 }
227 \protected\def\link#1{\@ifnextchar[{\link@i{#1}}{\link@default{#1}}}
228 \def\link@default#1#2{\link@i{#1}[{#2}]{#2}}
229 \def\link@i#1[#2]#3{\expandafter\hyperref\expandafter[\sanitize@link{#2}]{#1{#3}}}
230 \protected\def\linkdef#1{%
231 \phantomsection\expandafter\label\expandafter{\sanitize@link{#1}}%
232 \ignorespaces%
233 }
234 \def\ttlink{\link\texttt}
235 \def\texlink{\link\tex}
236 \def\textlink{\link\@firstofone}
237 \def\yquantlink{\link\yquant}
238 \def\Yquant{\pkg{yquant}}
239 \def\ii{{\symup i}}
240 \def\ee{{\symup e}}
241 \def\ketbra#1#2{\ket{#1}\!\bra{#2}}
242
243 \robustify\bmod % unicode-math makes problems when \bmod is used within a box
244
245 \frenchspacing
246 \RequirePackage{scrlayer-scrpage,scrlayer-notecolumn}
247 \setcounter{tocdepth}{\subsectiontocdepth}
248 \KOMAoptions{DIV=8}
249 \newcommand*{\the@orig@section}{}
250 \let\the@orig@section=\section
251 \renewcommand*{\section}{%
252 \clearpage%
253 \the@orig@section%
254 }
255 \addtokomafont{notecolumn.marginpar}{\footnotesize\RaggedRight}
256 \recalctypearea
257
258 \begin{document}
259 \maketitle
260 \begin{abstract}
261 \noindent This manual introduces \Yquant, a \LaTeX\hyp only package that outputs quantum circuits.
262 They are entered using a human\hyp readable language that, even from the source code, allows for a fluent understanding of the logic that underlies the circuit.
263 \Yquant{} internally builds on \TikZ{} and can be easily combined with arbitrary \LaTeX{} code.
264 Almost one hundred pages of examples complement the formal manual.
265 \end{abstract}
266 \tableofcontents
267
268 \section{Introduction}
269 This document outlines the scope and usage of the \Yquant{} package.
270 It contains both a reference and a huge number of examples.
271 \Yquant{} is a package that makes typesetting quantum circuits easy; the package is available on CTAN.
272 This beta version~0.9 \emph{should} be stable and interfaces are not very likely to change in an incompatible way in the future.
273 \compat{New in 0.4}Sometimes, backwards\hyp incompatible changes are required or advisable, in which case a compatibility setting will allow to revert back to the old behavior (rather, to maximize compatibility, this is an opt\hyp in setting: unless you choose the new behavior, you will get the old one).
274 Please do report all issues and desirable additions on \href{https://github.com/projekter/yquant/issues}{GitHub}.
275
276 \subsection{How to read the manual}
277 The probably fastest way to start using \Yquant{} is by just scanning through the examples in \cref{sec:examples}.
278 A more formal description of the \Yquant{} grammar and its fundamental concepts can be found in \cref{sec:grammar}.
279 If your desire is to change the appearance of \Yquant{} elements, use the configuration reference in \cref{sec:config}.
280 The full list of all available gates is provided in \cref{sec:gates}.
281 Finally, you may find that \Yquant{} \emph{almost} does what you want, but there is some final tweak that you cannot achieve\dots.
282 Then, have a look at \cref{sec:tikz} (or \cref{sec:alt}).
283
284 \subsection{Installation}
285 The recommended way of installation is through CTAN.
286 A direct installation from the Git repository to obtain the latest additions and features is be possible by just cloning it to a path visible to your \TeX{} compiler.
287 For example, you may put the source files in the same directory as your document (if you just want to give a try), or you may extract them to \texttt{tex/latex/yquant} in your local \texttt{texmf} (followed by an update of the file name database).
288 While the repository may contain new additions, they are not thoroughly tested until they end up on CTAN; features that are not documented in this manual are entirely unreliable.
289
290 \compat{New in 0.4}The CTAN repository reflects the most current version tag on Git; the \href{https://github.com/projekter/yquant/releases}{Releases} section on GitHub additionally provides a single\hyp file version of the main package, which can for example conveniently be included in arXiv submissions.
291 Note that the arXiv currently provides \Yquant{}~0.7.4 out\hyp of\hyp the\hyp box.
292
293 \subsection{Purpose of \Yquant, alternatives}\label{sec:alt}
294 \Yquant{} is the acronym for ``yet another quantum circuit package.''
295 This highlights the fact that nothing that this package provides cannot be achieved by other means.
296 In particular, there are at least the following methods to typeset quantum circuits in \TeX.
297 \begin{itemize}
298 \item Use some external program to draw them and include the output via \tex!\includegraphics!.
299 \item Use either \TeX's own drawing capabilities (the \tex!picture! environment) or other drawing packages such as \TikZ{} or \pkg{pstricks}.
300 \item Use a package specifically designed to draw quantum circuits (if you feel some other package should be mentioned here, please file an issue):
301 \begin{itemize}
302 \item \pkg{qasm} is probably the first of them (in terms of age).
303 It was developed to typeset the circuits found in Nielsen and Chuang's famous \emph{Quantum Computation and Quantum Information} book.
304 \pkg{qasm} consists of a Python~2 script (\texttt{qasm2circ}) that reads a quantum circuit written in a very intuitive language: declare names for your qubits, perform gates on them in each line.
305 \texttt{qasm2circ} converts those circuits into \TeX{} files that internally make use of the \pkg{xy} package to display the output.
306 Consequently, the user is restricted to the set of features that \pkg{qasm} directly offers (which is small).
307 Changes to the output, while possible, will be overwritten if \texttt{qasm2circ} is run again.
308 \pkg{qasm} output often looks sub\hyp optimal do to the fact that, e.g., rectangles are made up of four lines that do not properly connect and give a crumbly general feeling.
309
310 Note that since version~0.3, \Yquant{} understands \pkg{qasm} syntax, see \cref{sec:foreign:qasm}.
311
312 Maintenance status: last update of \pkg{qasm} in 2005. Also, \pkg{xy} was last updated in 2013, and the script is not compatible out\hyp of\hyp the\hyp box with Python~3, though an automatic conversion should work.
313 \item \pkg{qcircuit} is probably the most\hyp widely used package.
314 It provides commands that make it much easier to create quantum circuits using the \pkg{xy} package.
315 Its syntax therefore is grid\hyp oriented; inferring what a circuit does or locating a gate in the code can be tough.
316 This is particularly true for multi\hyp qubit gates.
317 Additionally, the \tex!\xymatrix! syntax is also somewhat cryptic.
318 \pkg{qcircuit} provides some flexibility within the limits of \pkg{xy} as to configuring the output.
319
320 Maintenance status: active (\href{https://github.com/CQuIC/qcircuit}{GitHub}); but remember this is \pkg{xy} based, with last update in 2013.
321 \item \pkg{quantikz} is a relatively recent package that, following the same grid\hyp based approach as \pkg{qcircuit}, instead builds on \TikZ{} as a backend.
322 As a consequence, it provides the full flexibility of customization that \TikZ{} offers, where hardly anything cannot be done.
323 It also reduces burdens of the \pkg{xy} syntax.
324 However, the disadvantages of the grid\hyp based syntax still remain.
325
326 Maintenance status: last update in 2023; the underlying \TikZ{} is actively maintained again by now.
327 \item \pkg{qpic} follows the approach of \pkg{qasm}: It makes use of an external Python program that reads the quantum circuits in an own language and converts them into \TikZ{} commands.
328 The language \pkg{qpic} follows is much more powerful than \pkg{qasm}'s.
329 The disadvantage that modifications in the output code will not remain after running the Python script again is mitigated by the possibility to define own \TeX{} macros.
330 Being an external program, \pkg{qpic}'s intrinsic set of features (including, e.g., vertically set circuits) are huge.
331 However, the language \pkg{qpic} uses cannot be understood without a detailed study of the manual, it appears to have been designed with the aim to minimize the length of command names.
332 A disadvantage of external programs is that the amount of space gates need is not accessible by the script; hence, manual intervention may be required.
333
334 Maintenance status: last update in 2023; the underlying \TikZ{} is actively maintained, and the script is compatible with Python~3.
335 \end{itemize}
336 \end{itemize}
337
338 \subsection{License}
339 This work may be distributed and/or modified under the conditions of the \LaTeX{} Project Public License, either version~1.3c of this license or (at your option) any later version.
340 The latest version of this license is in
341 \begin{center}
342 \url{http://www.latex-project.org/lppl.txt}
343 \end{center}
344 and version~1.3c or later is part of all distributions of LaTeX version 2005/12/01 or later.
345
346 \section{Basic elements of \Yquant}\label{sec:grammar}
347 \linkdef{yquant}\Yquant, as some of the aforementioned packages, builds on \TikZ.
348 Its basic syntax is similar to \pkg{pgfplots}: Start a \tex!tikzpicture! environment (perhaps passing some options); inside, start a \tex!yquant! environment.
349
350 Inside the \tex!yquant! environment, \TeX{} will now understand the \Yquant{} language---so \Yquant{} falls into the same category as \pkg{qasm} and \pkg{qpic}, providing a human\hyp readable language for the specification of the circuit that is not fixed to the actual layout.
351
352 However, \Yquant{} is a \TeX\hyp only package (actually, \hologo{LaTeX2e}, but not \hologo{LaTeX3}) that requires no external script to run---so it also falls into the same category as \pkg{qcircuit} and \pkg{quantikz}.
353
354 Since it runs entirely within \TeX, you can at any time interject \Yquant{} code with arbitrary \TeX{} or \TikZ{} code (though if it is ``too arbitrary,'' you may need to restart the \Yquant{} interpreter).
355
356 \subsection{General usage}
357 \begin{minted}[mathescape]{tex}
358 % preamble: $\compat{Changed in 0.4}$\usepackage[compat=<version>]{yquant}
359 \begin{tikzpicture}% tikz options possible
360 % tikz commands go here
361 \begin{yquant}% yquant options possible. Watch the newlines!
362 % yquant and tikz commands go here
363 \end{yquant}
364 % tikz commands go here
365 \end{tikzpicture}
366 \end{minted}
367
368 Note that \Yquant{} depends on \pkg{etoolbox}, \TikZ, \pkg{trimspaces}, and \compat{Changed in 0.4}\pkg{xkeyval}.
369 Additionally, it requires a moderately recent version of \hologo{LaTeX2e}, using either \hologo{LuaLaTeX}; or (untested) \hologo{pdfLaTeX} or \hologo{XeLaTeX}.
370
371 \begin{warning}[Optional arguments]
372 The optional arguments for the \tex!yquant! environment have to appear \emph{on the same line} as the environment itself.
373 If you want to put the arguments into a new line, it is crucial to mask the line break by putting a comment symbol after the environment: \tex!\begin{yquant}%!.
374 Without this comment, \Yquant{} will detect your line break (this is one of the few places in \TeX{} where line breaks and spaces are different) and assume that the expression in square brackets instead provides arguments for the following operation! \\
375 Finally note that in (non\hyp fragile) \pkg{beamer} frames, this discrimination between spaces and new lines does not work; the optional arguments will always be counted for the environment, not for the gate.
376 In this case, you can either declare the frame as fragile or (recommended) introduce a blank line between the environment and the options for the first gate.
377 \end{warning}
378
379 \begin{warning}[\compat{New in 0.4}\linkdef{compat}Compatibility mode]
380 Sometimes, continued development shows that certain choices of interfaces, configuration, or behavior are less ideal than originally thought.
381 In other cases, bugs are detected and fixed.
382 Both may lead to a change in the look of circuits developed with a previous version of \Yquant{} or even---though this should rarely, if ever, happen, and should be filed as a bug---prevent compilation in the new version.
383
384 For this reason, \Yquant{} offers a compatibility key that is highly recommended to be specified as a package option.
385 This allows certain features that are expected to break old layout or functionality to revert to their previous behavior.
386 Every feature affected by the \texttt{compat} key is documented in this manual.
387 Once a compatibility version is selected in a document, it cannot be changed any more.
388 Compatibility versions will include the major and minor, but not the patch level version number (though not every major or minor version will necessarily introduce new compatibility versions).
389 Bugs that clearly violated behavior described in this manual will be fixed without a possibility to revert back.
390 Changes that are not supposed to result in a (more than marginally) different result will not be included in the compatibility layer.
391 If you find this to be wrong in a particular case, please file a bug report.
392
393 When starting a new document, it is recommended to leave out the compatibility key at first compilation.
394 \Yquant{} will then issue a warning from which you can infer the recommended setting, corresponding to the current version.
395 You should then pass the appropriate version to the \tex!\usepackage! command.
396 For example, this manual corresponds to \tex!\usepackage[compat=0.8]{yquant}!.
397
398 Allowed values for \texttt{compat} are \texttt{newest} (discouraged), which equals \texttt{0.8}, \texttt{0.6}, \texttt{0.4}, and \texttt{0.3} (default).
399 \end{warning}
400
401 \subsection{Starred vs. unstarred environment}
402 \linkdef{yquant*}You may choose to use either the \texlink{yquant} or the \tex!yquant*! environment.
403 The former one requires you to define all your registers before you use them (though you may decide to define a register after some operations on \emph{different} registers, but before its first usage).
404
405 The starred form additionally supports the use of undeclared registers: it basically declares a registers upon its first usage.
406 This will always be a \ttlink{qubit} register; but if you use the corresponding attribute and the first usage is an \ttlink{init} command, you may overwrite this.
407
408 \compat{New in 0.2}Subcircuits always use the unstarred form.
409
410 \compat{New in 0.1.1}Additionally, if you refer to the index $i$ of a vector register of length $L < i$, this register will automatically be enlarged to $i \coloneq L$.
411 It is also possible to convert a scalar register into a vector register in this manner.
412 To enlarge a register in the unstarred environment, you must precede the number of registers to be added in the second declaration by a plus sign.
413 Note that in this manner, you may even create discontiguous vectors.
414
415 \emph{This might be a good point to proceed to the examples \cref{sec:examples}.}
416
417 \subsection[Formal syntax]{\compat{Enhanced in 0.1.2, 0.1.1}Formal syntax}
418 Every \Yquant{} command has the same structure (described here in EBNF syntax):
419 \begin{minted}{ebnf}
420 Command = { Arguments }, ?command?, [ Value ], [ RegisterList ], Controls, ";";
421 Arguments = "[", ?pgfkeys?, "]";
422 Value = "{", ?TeX code?, "}";
423 Controls = [ "|", [ RegisterSingleList ] ], [ "~", [ RegisterSingleList ] ];
424
425 RegisterList = (RegisterSingle | RegisterMulti), [ ",", RegisterList ];
426 RegisterSingleList = RegisterSingle, [ ",", RegisterSingleList ];
427
428 RegisterSingle = RegisterSingleNoRange | RegisterRange;
429 RegisterSingleNoRange = ?name?, [ "[", IndexMultiList, "]" ];
430 RegisterMulti = "(", ( RegisterMultiNoRange | [ "*" ], RegisterRange ), ")";
431 RegisterMultiNoRange = [ "*" ], ?name?, [ "[", IndexSingleMainList, "]" ];
432 RegisterRange = [ RegisterUnique ], "-", [ RegisterUnique ];
433 RegisterUnique = ?name?, [ "[", ?number?, "]" ];
434
435 IndexMultiList = IndexMulti, [ ",", IndexMultiList ];
436 IndexSingleList = IndexSingle, [ ",", IndexSingleList ];
437 IndexSingleMainList = [ "*" ], IndexSingle, [ ",", IndexSingleMainList ];
438 IndexMulti = IndexSingle | ( "(", IndexSingle, ")" );
439 IndexSingle = ?number? | ( [ ?number? ], "-", [ ?number? ] );
440 \end{minted}
441 Note that \Yquant{} is quite tolerant with respect to whitespaces.
442 Virtually every comma in the EBNF notation may consist of an arbitrary (including zero) number of whitespaces.
443 Not all combinations that can be constructed by this grammar are actually allowed semantically; but it would make the grammar too verbose to spell this out in detail.
444 Deviations are noted in this manual.
445
446 Valid values for
447 \begingroup%
448 % since v2.6 of minted, \PYG is no longer universally defined, so bring it into scope
449 \minted@defstyle{\minted@get@opt{style}{default}}%
450 \begin{BVerbatim}[commandchars=\\\{\}]
451 \PYG{n+ni}{?command?}
452 \end{BVerbatim}
453 \space (case\hyp insensitive) are documented in a \cref{sec:gates}.
454 We use
455 \begin{BVerbatim}[commandchars=\\\{\}]
456 \PYG{n+ni}{?pgfkeys?}
457 \end{BVerbatim}
458 \space to describe any valid content passed to the \tex!\pgfkeys! macro (rather, \texlink{\yquantset} is invoked with some subtleties); and by
459 \begin{BVerbatim}[commandchars=\\\{\}]
460 \PYG{n+ni}{?name?}
461 \end{BVerbatim}
462 \space we denote any valid register name.
463 Register names must not contain any of the control literals used before (semicolon, comma, parentheses, square brackets, dash, pipe, tilde, beginning star); and you should avoid using special \TeX{} characters.
464 Note that for performance reasons, \Yquant{} does not check whether a register name is valid or not, but expect to either see unintended output or not\hyp so\hyp helpful error messages if you choose an invalid name.
465 \begin{BVerbatim}[commandchars=\\\{\}]
466 \PYG{n+ni}{?number?}
467 \end{BVerbatim}
468 \space is a decimal integer larger or equal to zero (in the context of register creation, strictly larger; in this context, it may also contain a leading
469 \begin{BVerbatim}[commandchars=\\\{\}]
470 \PYG{l+s+s2}{\PYGZdq{}+\PYGZdq{}}
471 \end{BVerbatim}
472 ).\endgroup
473
474 \subsection{Registers}\label{sec:registers}
475 Every quantum circuit is structured by means of \emph{registers}.
476 A register has a \emph{type} that specifies how its wire is drawn, and that may even change during its lifetime.
477 At the moment, \Yquant{} supports four types:
478 \begin{enumerate}
479 \item \ttlink{qubit} is the most common type, used for a quantum register.
480 It corresponds to a single line.
481 \item \ttlink{cbit} is a classical register, which can be either declared from the beginning or arises by using measurements.
482 It corresponds to a double line.
483 \item \ttlink{qubits} is a ``quantum bundle,'' i.e., a bunch of quantum registers that are always addressed in a group as a single register.
484 Operations between bundles of the same length should be interpreted as transversal.
485 It corresponds to a triple line.
486 An alternative (and more common) representation is to use the \ttlink{qubit} type and a \ttlink{slash} gate at its very beginning.
487 \item \ttlink{nobit} is the most obscure type, corresponding to a non\hyp existing wire.
488 Mostly, this register type arises by using the \ttlink{discard} command.
489 However, it can also be directly declared, which on rare occasions might be necessary (its type can then be changed by means of an \ttlink{init} or \ttlink{setwire} pseudo\hyp gate).
490 If you want to declare a register only at a certain horizontal position in the circuit, consider using the \ttlink{[after]} argument instead.
491 \end{enumerate}
492
493 Registers must be declared before they can be used (though in the \texlink{yquant*} environment, this declaration may be implicit, creating a \ttlink{qubit} register).
494
495 Registers can have a vector character, i.e., not only a \emph{name}, but also an \emph{index} (or, in the declaration, a \emph{length}).
496 The index (zero\hyp based) or length is specified in square brackets following the name, which closely mimics the OpenQASM language.
497
498 \compat{New in 0.1.1}Vector registers may be non\hyp contiguous: Whenever you create a bunch of registers, it is put at the bottom of the circuit.
499 If you later on again create registers of the same name---either implicitly in the \texlink{yquant*} environment, or explicitly by preceding the length of the vectors entries to be added by a plus, as in \yquant!qubit a[+3];!---they will be put to what is \emph{now} the bottom of the circuit, even if some other registers are interspersed.
500
501 Registers are referenced---i.e., used in operations---by their name and index.
502 If the latter is omitted, all indices of the register are targeted.
503 Multiple registers can be referenced by joining their names in a comma\hyp separated list, or by means of a range specifier: give the name of the first (topmost), a dash, and the last (bottom\hyp most) register.
504 Both are inclusive.
505 In a range specifier, omitting the start name means that the range begins at the first known register; omitting the end name means that the range ends at the last known (at the moment of its use) register.
506 Omitting both indicates a range over all known registers.
507
508 \compat{New in 0.1.1}It is also possible to use comma\hyp separated lists and ranges within the indices themselves, so that, e.g., \yquant!a[0, 2, 5-], b[-2]! will target the zeroth and second index of \yquant!a!; the remaining indices of \yquant!a! starting from five; and the first three indices of \yquant!b!.
509 However, if you use an \emph{outer} range (i.e., a range between indices of registers with different names), the initial and final register of the range must be unique, i.e., either you omit the index (targeting the first or last register with the given name) or specify a single one.
510
511 \begin{warning}[Ranges and discontiguous registers]
512 Assume a configuration in which the vector register \yquant!a! begins with one qubit, then the single register \yquant!b! follows, and after that \yquant!a! is continued with another qubit.
513
514 The range \yquant!a-b! will target \yquant!a[0]! and \yquant!b[0]!, but not \yquant!a[1]!.
515 As \yquant!a! is used as the initial register in the range without an explicit index specification, \Yquant{} automatically translates this into \yquant!a[0]!, while \yquant!b!, being used as the final register, is automatically translated into the last register of name \yquant!b! (which here happens to be \yquant!b[0]!).
516 Ranges between different register names (outer ranges) are \emph{visual} ranges, i.e., they refer to the top\hyp to\hyp bottom order that is visible.
517 Consequently, the register \yquant!a[1]! is left out since it is visually below the others.
518
519 Likewise, the range \yquant!b-a! will target \yquant!b[0]! and \yquant!a[1]!.
520
521 Ranges within indices are \emph{logical} ranges.
522 Hence, \yquant!a!, \yquant!a[-]!, \yquant!a[0-]!, \yquant!a[-1]!, and \yquant!a[0-1]! are all equivalent: they all refer to the registers \yquant!a[0]! and \yquant!a[1]!, but never to \yquant!b!, regardless of any visual position.
523 \end{warning}
524
525 All that was said so far refers to the operation being carried out on each of the registers \emph{individually}, i.e., producing several copies of the operation.
526 This is different from using the operation multiple times on the individual single registers only with regard to the horizontal positioning: if specified as a register list with one operation, all copies of the operation will be aligned at the same horizontal middle axis (for gates with the same width on each register, this is the same as issuing an \ttlink{align} command before performing the operations individually).
527 \begin{warning}
528 It is forbidden (in the sense of ``not useful and giving unexpected output,'' but \Yquant{} does not check for this) to list the same register multiple times (explicitly or via ranges) in one operation.
529 \end{warning}
530
531 Instead of copies of single\hyp register operations, one might want to carry out a multi\hyp register operation.
532 In this case, the desired list of registers (comma separated, range, or both) must be surrounded by parentheses.
533 It is possible to mix single- and multi\hyp register operations arbitrarily.
534 In an index list, you may also choose to surround only certain indices with parenthesis, provided the whole register is not already a multi\hyp register.
535 \begin{warning}
536 Note that some gates, such as the \ttlink{swap} gate, always require (semantically, not grammatically) multi\hyp register operations.
537 \compat{Changed in 0.1.2}The number of constituents is not stipulated; while a \ttlink{swap} gate with more than two targets is no longer well\hyp defined, other registers such as \ttlink{zz} may still be useful.
538 \Yquant{} will prevent you from using a gate in a multi\hyp qubit setting when it may only be used for single registers.
539 \end{warning}
540 Typically, multi\hyp register operations should only be carried out on adjacent registers---but sometimes, one might want to carry out a multi\hyp qubit operation on a visually discontiguous set of registers (which, due to a particular quantum computer topology, might even be physically feasible).
541 \compat{New in 0.1.2}\Yquant{} supports these discontiguous operations explicitly.
542 It will draw a \emph{main} part of the gate at the first contiguous slice of registers in the target list---you may select another register for this part by preceding the name or index with a star (which, contrary to the simplified grammar, may only occur \emph{once} in a target specification).
543 All other contiguous slices of target registers will be drawn in a \emph{subordinate style} for this gate.
544 Finally, all slices will be connected by a single vertical line with the style \ttlink{/yquant/every multi line}.
545 \compat{New in 0.2}Subcircuits will always span the full region from the first to the last register specified in a multi\hyp qubit gate.
546 This is due to the fact that they may contain arbitrary ancilla registers which may be positioned somewhere in between the parts that actually constitute the subcircuit---surrounding this with a scattered set of connected boxes would look quite unpleasant.
547 \clearpage
548 \begin{warning}[\compat{Improved in 0.1.2}Discontiguous targets and control lines]
549 A control line extends from the very first to the very last affected register in an operation.
550 A sub\hyp gate line that is used for discontiguous registers will only span the range of a multi\hyp register.
551 This distinction becomes crucial if you want to carry out a \emph{controlled} operation on more than one multi\hyp register, where at least one is discontiguous.
552 Without the controls, the separate multi\hyp registers could be identified, since no connecting vertical line extends between them (unless, which you should strictly avoid, they are intertwined).
553 However, with the controls, the control line will make it hard (for some gates, impossible) to visually distinguish the connected parts.
554 \Yquant{} will kindly provide a warning in this case.
555 You may choose to suppress this warning using the boolean key \ttlink{/yquant/operator/multi warning}.
556 \end{warning}
557 \begin{warning}
558 There is no established style for discontiguous gates.
559 Note that at the moment, main and subordinate style coincide for all gates except for the \ttlink{measure} gate with a value.
560 In order to still make it possible to visually distinguish discontiguous multi\hyp register gates operating on slices of a single register from just a bunch of single\hyp register gates that are executed in a parallel manner if controls are present, \Yquant's default vertical line style for the former case is a wavy line instead of a straight one.
561 Still, the meaning of this should probably be explained.
562 Please feel free to submit issues or pull requests with propositions of how default styles or alternative subordinate gate shapes may additionally help to mitigate the problem.
563 \end{warning}
564
565 \subsection{Arguments}
566 Every command may take one or multiple arguments.
567 Those are specified in square brackets that precede the command itself.
568 The content of those square brackets is essentially fed to a \tex!\pgfkeys!-like macro.
569 The default path is set appropriately such that the arguments of the command can be accessed without path specifiers.
570 If the key is not a valid argument for the command or a global argument and it is not given by an absolute path, it is searched for in the \texttt{/yquant} namespace.
571 If it cannot be found there, it is passed to \ttlink{/yquant/operator style}.
572
573 Note that commands may have required arguments.
574 If a required argument is missing, an error will be issued.
575
576 \linkdef{value}The \texttt{value} attribute can alternatively be given inside curly brackets after the command name and before the register specification.
577 This has the advantage that special characters such as a closing square bracket need not be escaped.
578 If both alternatives are present, the value inside curly brackets takes precedence and a warning is issued.
579
580 \subsection{Controls}
581 Lots of gates may have controls, i.e., they are only to be executed if some other gate is set or unset.
582 The former case is called a \emph{positive control}, the latter one a \emph{negative control}.
583 Those are indicated by filled and empty circles on the control registers and a vertical line that joins the registers that belong together.
584
585 The gate specification is followed by the list of target registers.
586 By then writing a pipe (``\yquant!|!''), the list of positive controls is introduced; this mimics the mathematical syntax ``conditioned on'' for probabilities or ``given'' for sets.
587 If there are no positive controls, the list may be empty or, together with the pipe, omitted.
588 Preceded by a tilde (``\yquant!~!''), the list of negative controls then follows; this mimics the syntax of many programming languages that denote logical negation by a tilde.
589 If there are no negative controls, the list may be empty or, together with the tilde, omitted.
590
591 \subsection[Highlighting parts of a circuit]{\compat{New in 0.9}Highlighting parts of a circuit}\label{sec:enclose}
592 Sometimes, there are certain parts of a circuit that deserve special attention.
593 This is often accomplished by framing them in a box or putting some kind of background on them, then adding some text at the boundary of this region.
594 While this can be accomplished in \Yquant{} using various options, starting from version~0.9, the enclose functionality aims to do this most conveniently.
595 Note that this is not part of the \Yquant{} language specification itself, but uses \TeX{} macros instead, for a very simple reason: Sometimes, you might want to highlight only a certain part of a gate, while other parts should stick out of it.
596 This means that you cannot isolate the part that is to be highlighted as you would do it with subcircuits; instead, something with a purely visual meaning, but no intrinsic part of the circuit, is required.
597
598 There are several ways to access the enclose functionality.
599 \subsubsection[\texorpdfstring{\tex{\encloseall}}{\textbackslash encloseall}]{\tex!\encloseall[<options>]{<content>}!}\linkdef\encloseall
600 This should be the choice whenever you want to highlight the gates \texttt{<content>}, while the region should span from the very first to the very last register in the circuit.
601 It is basically a restricted version of the \texlink{\enclose} macro, but at the same time, it is much faster to process, as \TeX{} doesn't have to figure out which registers are involved.
602
603 The \texttt{<options>} allow you to specify any \TikZ{} option which is attached to the enclosing rectangle.
604 None of the advanced options mentioned for \texlink{\enclose} are allowed here---the configuration for \tex!\encloseall! always corresponds to \ttlink[/yquant/enclose/spacing/register]{spacing=register}, \ttlink[/yquant/enclose/shape/rectangle]{shape=rectangle}, so see the details for the configuration options on how this manifests.
605 However, the bracing options may very well be used (and indeed, they are probably best used with \tex!\encloseall!).
606
607 \subsubsection[\texorpdfstring{\tex{\enclose}}{\textbackslash enclose}]{\tex!\enclose[<options>]{<content>}!}\linkdef\enclose
608 This should be used whenever you need more flexibility and the enclosing region is not from the first to the last register in the whole circuit.
609
610 By default, the region will span from the first to the last register that is mentioned within \texttt{<content>}.
611 However, you can overwrite this by using the option keys \linkdef{/yquant/enclose/from}\texttt{from=<register>} and \linkdef{/yquant/enclose/to}\texttt{to=<register>}.
612 If you specify a vector register, this will correspond to the first index for \texttt{from} and to the last for \texttt{to}.
613 Note that it is allowed to specify a register which does not exist yet---however, it has to be created at some point during the execution of \texttt{<content>}.
614 Further note that the register has to be one in the same \emph{circuit} in which \tex!\enclose! is placed---it cannot be in a subcircuit.
615 However, you can achieve this functionality in a different way using named enclosures.
616
617 \begin{option*}{yquant/enclose}{name}!!
618 You may use the \texttt{name=<name>} option to give the enclosing region a name (this is not a \TikZ{} node name, it is only valid in the context of enclosing options).
619 Then, when creating the register in a subcircuit, you may specify the attribute \linkdef{enclose/set from}\texttt{enclose/set from=<name>} or \linkdef{enclose/set to}\texttt{enclose/set to=<name>} to make clear that the region \texttt{<name>} should start at this register.
620 \end{option*}
621
622 It is possible to let an \tex!\enclose! start from multiple different registers---just think of various subcircuits with ancillas that cannot be put in a particular ordering with respect to each other.
623 Specify the attribute for all of the registers that should be included and \Yquant{} will make sure that the region covers all of them.
624
625 \clearpage
626 \begin{example}[Using \tex!\enclose! with explicit range specifications]
627 \begin{codeexample*}
628 \begin{tikzpicture}
629 \begin{yquant}
630 qubit q[4];
631 x -;
632 \enclose[name=region, to={q[2]}, shade] {
633 h q[2];
634 subcircuit {
635 [ancilla, enclose/set from=region] qubit anc;
636 qubit io[2];
637 [ancilla, enclose/set to=region] qubit anc[+2];
638 [ancilla] qubit anc[+1];
639 zz (-);
640 } (q[2, 3]);
641 cnot q[0] | q[3];
642 }
643 x -;
644 \end{yquant}
645 \end{tikzpicture}
646 \end{codeexample*}
647 The example shows nicely how, semantically, \texttt{set from} is no different than specifying \texttt{from}: as soon as one ``from'' (or ``to'') specification is present, the start or end of the range is no longer calculated automatically based on the registers addressed in the region (for then, \texttt{q[0]} from the \ttlink{cnot} would have to be in the region), but defined as the least possible value that still encloses everything that was explicitly requested.
648 \end{example}
649
650 \begin{option*}{yquant/enclose}{shape}!rectangle!
651 Defines how the region that encloses certain gates looks like.
652
653 \begin{itemize}
654 \item \linkdef{/yquant/enclose/shape/rectangle}\texttt{rectangle} \\
655 The enclosing region will by default be a rectangle from the top-/leftmost to the bottom/-rightmost gate.
656 Note that this will implicitly put an \ttlink{align} gate on all the registers that are involved before and after the enclosing---else, it would be possible to have a gate that is not part of the region, but still ends up visually in it.
657 Just the act of creating a register (without a text) will never by itself be part of an enclosing region.
658 If the register creation with value or the first initialization is within an enclose block, the block will also cover the label.
659 Note that the alignment due to the rectangular shape may prevent an initialization from extending to the left of the circuit if some other registers already contain gates.
660
661 \item \linkdef{/yquant/enclose/shape/polygon}\texttt{polygon} (default) \\
662 \Yquant{} will draw a polygon that tries to tightly cover all the gates within the region.
663 No alignment is performed.
664 \begin{example}[Using \tex!\enclose! with polygon shape]
665 \begin{codeexample}
666 \begin{tikzpicture}
667 \begin{yquant}
668 qubit q[5];
669 x -;
670 x q[0, 1, 4];
671 x q[0];
672 \enclose[shape=polygon, fill=yellow]{
673 h q[2, 3];
674 h q[1-];
675 h q[0-2];
676 h q[1];
677 }
678 x q[3, 4];
679 x q[0, 2-4];
680 \end{yquant}
681 \end{tikzpicture}
682 \end{codeexample}
683 Consider the output of the same circuit, using the rectangular shape:
684 \begin{center}
685 \begin{tikzpicture}
686 \begin{yquant}
687 qubit q[5];
688 x -;
689 x q[0, 1, 4];
690 x q[0];
691 \enclose[fill=yellow]{
692 h q[2, 3];
693 h q[1-];
694 h q[0-2];
695 h q[1];
696 }
697 x q[3, 4];
698 x q[0, 2-4];
699 \end{yquant}
700 \end{tikzpicture}
701 \end{center}
702 \end{example}
703 \end{itemize}
704 \end{option*}
705
706 A final question is how large the enclosing region will be in space\hyp like direction (i.e., between the registers).
707
708 \begin{option*}{yquant/enclose}{spacing}!gate or local register!
709 Defines how the extents of the enclosed registers are calculated.
710 Possible values:
711 \begin{itemize}
712 \item \linkdef{/yquant/enclose/spacing/gate}\texttt{gate} \\
713 The enclosing region will be as large as all the gates within this region.
714 Note that you \emph{could} run into trouble with multi\hyp register gates using this option.
715 \begin{example}[Using \tex!\enclose! with \texttt{gate} spacing]
716 \begin{codeexample*}
717 \begin{tikzpicture}
718 \begin{yquant}[enclose/spacing=gate, enclose/style={draw=none}]
719 qubit q;
720 \enclose[fill=red!20!white] { slash q; }
721 \enclose[fill=yellow]{ x q; }
722 \enclose[fill=blue!20!white]{ not q; }
723 \end{yquant}
724 \end{tikzpicture}
725 \end{codeexample*}
726 \end{example}
727 \item \linkdef{/yquant/enclose/spacing/register}\texttt{register} \\
728 The enclosing region will be as large as the \emph{final} register height/depth, which consists of \emph{all} gates that were ever present on the corresponding registers.
729 \begin{example}[Using \tex!\enclose! with \texttt{register} spacing]
730 \begin{codeexample*}
731 \begin{tikzpicture}
732 \begin{yquant}[enclose/spacing=register, enclose/style={draw=none}]
733 qubit q;
734 \enclose[fill=red!20!white] { slash q; }
735 \enclose[fill=yellow]{ x q; }
736 \enclose[fill=blue!20!white]{ not q; }
737 \end{yquant}
738 \end{tikzpicture}
739 \end{codeexample*}
740 \end{example}
741 \vskip-3mm
742 However, this is problematic due to the way subcircuits are handled by \Yquant.
743 If you have a subcircuit which has ancillas, during the calculation of the final positioning, \Yquant{} will accumulate their extents and add it to the closest connected register, so you might suddently end up with a very large enclosing region which you did not expect.
744 \begin{example}[Unexpected behavior of \texttt{register} spacing with subcircuits]
745 \begin{codeexample}
746 \begin{tikzpicture}
747 \begin{yquant}[enclose/style={draw=none, fill=yellow}]
748 qubit before;
749 qubit q;
750 qubit after;
751 x before, after;
752 subcircuit {
753 [ancilla] qubit a;
754 qubit q;
755 swap (-);
756 [ancilla] qubit b;
757 swap (a, b);
758 } q;
759 \enclose[spacing=register]{ x q; }
760 \end{yquant}
761 \end{tikzpicture}
762 \end{codeexample}
763 \end{example}
764 \item \linkdef{/yquant/enclose/spacing/gate or local register}\texttt{spacing=gate or local register} (default) \\
765 Takes into account the minimum of the global register extent \emph{before} subcircuit ancillas are added, but at least the extent of all gates within the region\footnote{%
766 Note that here, the ``at least'' part is almost superfluous---however, it makes sure that the \ttlink{overlay} attribute, which can exclude a gate from taking part in the register extent calculation, is disregarded for enclosings.
767 }.
768 This is not a perfect solution either, a good compromise: Adding the ancillas is the very first step in the calculation of positions; and the very last step is the distribution of multi\hyp register gates, so we could still run into trouble with multi\hyp register gates; but likely, both points of view help each other.
769 \begin{example}[Using \tex!\enclose! with default \texttt{gate or local register} spacing]
770 \begin{codeexample*}
771 \begin{tikzpicture}
772 \begin{yquant}
773 qubit before;
774 qubit q;
775 qubit after;
776 x before, after;
777 subcircuit {
778 [ancilla] qubit a;
779 qubit q;
780 swap (-);
781 [ancilla] qubit b;
782 swap (a, b);
783 } q;
784 \enclose[draw=none, fill=yellow]{
785 box {gate} (q, after);
786 [overlay] box {\vrule height 18mm} q;
787 }
788 \end{yquant}
789 \end{tikzpicture}
790 \end{codeexample*}
791 \end{example}
792 \end{itemize}
793
794 Let's also quickly show the output of this example both with the \texttt{gate} (left) and \texttt{register} (right) spacing:
795 \begin{center}
796 \begin{tikzpicture}
797 \begin{yquant}
798 qubit before;
799 qubit q;
800 qubit after;
801 x before, after;
802 subcircuit {
803 [ancilla] qubit a;
804 qubit q;
805 swap (-);
806 [ancilla] qubit b;
807 swap (a, b);
808 } q;
809 \enclose[draw=none, fill=yellow, spacing=gate]{
810 box {gate} (q, after);
811 [overlay] box {\vrule height 18mm} q;
812 }
813 \end{yquant}
814 \end{tikzpicture}\qquad\begin{tikzpicture}
815 \begin{yquant}
816 qubit before;
817 qubit q;
818 qubit after;
819 x before, after;
820 subcircuit {
821 [ancilla] qubit a;
822 qubit q;
823 swap (-);
824 [ancilla] qubit b;
825 swap (a, b);
826 } q;
827 \enclose[draw=none, fill=yellow, spacing=register]{
828 box {gate} (q, after);
829 [overlay] box {\vrule height 18mm} q;
830 }
831 \end{yquant}
832 \end{tikzpicture}
833 \end{center}
834 \end{option*}
835
836 \subsubsection[\texorpdfstring{\tex{\startenclose} and \tex{\stopenclose}}{\textbackslash startenclose and \textbackslash stopenclose}]{\tex!\startenclose[<options>]{<name>}! and \tex!\stopenclose{<name>}!}\linkdef\startenclose\linkdef\stopenclose
837 While the \texlink{\enclose} macro is already very flexible, it requires the whole content of the enclosing region to be passed as a single \TeX{} group.
838 This makes it impossible to interleave regions; but still, \Yquant{} can do it for you.
839 You must now use the \tex!\startenclose! macro instead, which requires a mandatory name---given explicitly instead of via the options, but it is indeed the same thing and can therefore be used with \ttlink{enclose/set from} and \ttlink{enclose/set to}.
840 The content of the enclose is then simply everything that follows until a corresponding \tex!\stopenclose! with the same name is found.
841 This can be arbitrarily mixed with the group\hyp delimited \texlink{\enclose} macro.
842 \begin{example}[Interleaving enclosing regions]
843 \begin{codeexample}
844 % \usetikzlibrary{patterns}
845 \begin{tikzpicture}
846 \begin{yquant}[enclose/style={draw=none}, enclose/shape=polygon]
847 qubit q[4];
848 h q[0, 2, 3];
849 \startenclose[pattern=north east lines, pattern color=green!60!white]{region1}
850 z q[1];
851 z q[-2];
852 z q[1];
853 \startenclose[red, pattern=north west lines, pattern color=red]{region2}
854 y q[2];
855 \stopenclose{region1}
856 x q[3];
857 x q[3];
858 \stopenclose{region2}
859 h q[0];
860 \end{yquant}
861 \end{tikzpicture}
862 \end{codeexample}
863 \end{example}
864
865 \begin{warning}[Named enclosings and custom gates]
866 The naming scheme of enclosings does not respect any name mangling, therefore names are available globally and must be unique within one \ttlink{yquant} environment.
867 So make sure \emph{never} to use named enclosings within \hyperref[sec:customgate]{custom gate definitions}, as their names might conflict with the ones of the circuits that use them.
868 Unnamed enclosings using \texlink{\enclose} or \texlink{\encloseall} are fine, though.
869 \end{warning}
870
871 \begin{warning}[Transcending subcircuits]
872 Yes, it is indeed possible to start one enclose in a subcircuit and end it after this subcircuit or vice versa.
873 Note that this is much easier for the \ttlink[/yquant/enclose/shape/polygon]{polygon} shape than for rectangles!
874 The rectangular enclosure has to add alignments before and after to make sure that nothing enters its region that is not supposed to be there.
875 Since enclosings can transcend subcircuits, this alignment is nothing that could be reproduced just by using the \ttlink{align} gate---it requires something more sophisticated.
876 The problem is that at the beginning of an enclosing, it is not even known which registers and from which subcircuits will take part.
877 In fact, this is only known when the enclosing is finished, but by then, the devious user might already have exited a subcircuit in which they said \tex!\startenclose!.
878 Now we are in deep trouble, because calculating the \emph{width} of the subcircuit, which is necessary for the box of the subcircuit and proper vertical alignment for example of the control line or multiple subcircuits used in the same expression (luckily, having more than one [multiregister] target is impossible here, as this would lead to a name clash), was already completed---of course without the unknown alignment.
879 So what we do to get a halfway proper display in the end is that during the drawing stage, where we can rather easily perform the alignment, \Yquant{} will compare whether the precalculated width coincides with the actual one, and if not, the width is adjusted post mortem.
880 Yes, indeed, for this is just an attempt not do produce too much garbage.
881 The size of the box itself will be rendered correctly, but for everything that came \emph{before} or a potential control line, the size is as it was calculated without alignment.
882 It would be easily possible for \Yquant{} to place the control line correctly, but gates that came before cannot be re\hyp adjusted, hence the control line could potentially overlap with them -- better not to do this.
883 Let's illustrate this most monstrous failure with a tiny circuit:
884 \begin{codeexample*}
885 \begin{tikzpicture}
886 \begin{yquant}
887 qubit {} a;
888 qubit {} b;
889 box {loooooong gate} b;
890 subcircuit {
891 qubit {} a;
892 x a;
893 \startenclose[fill=blue!20]{enc}
894 y a;
895 } a | b;
896 z b;
897 \stopenclose{enc}
898 h a;
899 box {$S$} b;
900 \end{yquant}
901 \end{tikzpicture}
902 \end{codeexample*}
903 Note how here, the control line is placed where you would expect it if the \ttlink{y} gate directly followed the \ttlink{x} gate without any alignment.
904 In fact, how does the alignment even work?
905 First, the size of the subcircuit (without alignment) is computed.
906 Naturally, the \ttlink{z} gate would come after the full length of the subcircuit, since the control on the lower register blocks the complete space.
907 This is now know as the position of the \ttlink{z} gate, where we then have to align the \ttlink{y} gate at.
908 The pre\hyp calculated box size for the subcircuit would be too small, so it is enlarged, and after both gates, we align once more, so that there is indeed also more space than normal before the $S$~gate.
909 \end{warning}
910
911 \subsubsection{Enclosing drawing pipeline}
912 Drawing the enclosings works in multiple stages: During preparation, \Yquant{} analyzes which gates in the circuit belong to the enclosing region; it also keeps track of the individual register extents (space\hyp like, i.e., from register to register), if the shape requires this.
913 During the drawing stage, \Yquant{} knows in which visual order the registers are placed in the circuit and can therefore sort them properly in order to figure out which is the topmost and which is the bottom\hyp most register.
914 Furthermore, the gate extents (time\hyp like, i.e., from gate to gate) are accumulated, including all separations that are gleaned from the style in \ttlink{/yquant/enclose/every enclose} as well as the one passed explicitly to \texlink{\encloseall}, \texlink{\enclose}, or \texlink{\startenclose}.
915
916 \begin{option*}{yquant/enclose}{every enclose}!draw, dashed, inner spacesep=.5*\pgfkeysvalueof{/yquant/register/separation}, inner timesep=.5*\pgfkeysvalueof{/yquant/operator/separation}!
917 This style is applied to the enclosed region, where by default, the separations increase the region just by the half distance to their next neighboring element.
918 \end{option*}
919
920 \begin{option*}{yquant/enclose}{style}!/yquant/enclose/every enclose/.append style={#1}!
921 This is a shorthand that can be used to modify the appearance of the enclosed region.
922 \end{option*}
923
924 After all the calculations are done, the necessary drawing commands for the enclosing region are \emph{stored}, but not yet executed.
925 Their execution is deferred until the very end of all the drawings---this would ensure that without further measures, they would be drawn \emph{on top} of everything (having them somewhere in between would just result in a weird mix).
926 This is of course not desired; therefore, the enclosing region will be drawn on a background layer, but any \texttt{label} commands will be drawn on the main layer instead, which should put them to the very front.
927 However, this behavior is also configurable.
928
929 \begin{option*}{yquant/enclose}{layer}!background!
930 Defines the layer onto which the enclosing regions are drawn, excluding late options such as labels.
931
932 Note that \Yquant{} automatically loads the \TikZ{} library \texttt{backgrounds}, which makes the \texttt{background} layer available; however, any active layer can be chosen.
933 It is also possible to use \texttt{main}---note that as it is not possible to explicitly draw on the \texttt{main} layer, \Yquant{} will then simply not invoke any change of layers.
934 Hence, if you put the whole \Yquant{} environment on a layer \texttt{X} different from \texttt{main}, saying \texttt{main} here will draw on \texttt{X}, \emph{not} on \texttt{main}.
935 \end{option*}
936
937 \begin{option*}{yquant/enclose}{label layer}!main!
938 Defines the layer onto which all labels of enclosing regions (more precisely, everything that uses the \TikZ{} feature \texttt{append after command} or \texttt{prefix after command}) are drawn.
939
940 The same remarks as for \ttlink{/yquant/enclose/layer} hold.
941 \end{option*}
942
943 \subsubsection{Embracing regions}
944 Sometimes, you want to highlight a part of a circuit in a very unobtrusive way, simply by putting curly braces at some region to the top or bottom (resp. left/right in vertical layout) of the whole circuit.
945 The \tex!\encloseall! feature already sets everything up nicely so that with a bit of \TikZ{} magic on the node that is drawn, we can easily modify the visible shape into such a curly brace.
946 \Yquant{} defines styles that do exactly this.
947
948 \begin{option*}{yquant/enclose}{brace above}!!
949 Style that can be passed to an \tex!\encloseall! region which will put a curly brace above (in vertical layout: to the left of) the whole circuit.
950 The style expects a label as its argument, which will subsequently be drawn above (in vertical layout: to the left of) the brace.
951 Note that everything that applies to ordinary \TikZ{} labels also holds here: you may use a square bracket as the first character of the label in order to specify some styles, and you may use the colon to separate an optional label position from the label text.
952
953 \Yquant{} will also apply the following styles to the decoration of the path:
954 \begin{itemize}
955 \item \ttlink{/yquant/enclose/every brace}
956 \item \ttlink{/yquant/enclose/every brace above or left}
957 \item \ttlink{/yquant/enclose/every brace above} in horizontal layout
958 \item \ttlink{/yquant/enclose/every brace left} in vertical layout
959 \end{itemize}
960
961 \Yquant{} will apply the corresponding \texttt{every brace label} styles to the label.
962
963 Note that only the styles that have been defined \emph{before} \texttt{brace above} is invoked are applied to the brace/label!
964 This allows to apply both a brace above and below and use different styles for them.
965 \end{option*}
966
967 \begin{option*}{yquant/enclose}{brace left}!!
968 This is a synonym for \ttlink{/yquant/enclose/brace above}, as this naming makes more sense for vertical circuits.
969 \end{option*}
970
971 \begin{option*}{yquant/enclose}{brace below}!!
972 Style that can be passed to an \tex!\encloseall! region which will put a curly brace below (in vertical layout: to the right of) the whole circuit.
973 The style expects a label as its argument, which will subsequently be drawn below (in vertical layout: to the right of) the brace.
974 Note that everything that applies to ordinary \TikZ{} labels also holds here: you may use a square bracket as the first character of the label in order to specify some styles, and you may use the colon to separate an optional label position from the label text.
975
976 \Yquant{} will also apply the following styles to the decoration of the path:
977 \begin{itemize}
978 \item \ttlink{/yquant/enclose/every brace}
979 \item \ttlink{/yquant/enclose/every brace below or right}
980 \item \ttlink{/yquant/enclose/every brace below} in horizontal layout
981 \item \ttlink{/yquant/enclose/every brace right} in vertical layout
982 \end{itemize}
983
984 \Yquant{} will apply the corresponding \texttt{every brace label} styles to the label.
985
986 Note that only the styles that have been defined \emph{before} \texttt{brace below} is invoked are applied to the brace/label!
987 This allows to apply both a brace below and above and use different styles for them.
988 \end{option*}
989
990 \begin{option*}{yquant/enclose}{brace right}!!
991 This is a synonym for \ttlink{/yquant/enclose/brace below}, as this naming makes more sense for vertical circuits.
992 \end{option*}
993
994 \clearpage
995 \paragraph{Brace styles}\leavevmode
996 \begin{option*}{yquant/enclose}{every brace}!draw, /pgf/decoration/brace, decorate!
997 This style is applied to the path drawn for every brace put to an enclosing region using any of \ttlink{/yquant/enclose/brace above}, \ttlink{/yquant/enclose/brace left}, \ttlink{/yquant/enclose/brace below}, or \ttlink{/yquant/enclose/brace right}.
998 Note that the option has to be specified \emph{before} the brace style is invoked.
999 \end{option*}
1000
1001 \begin{option*}{yquant/enclose}{brace style}!/yquant/enclose/every brace/.append style={#1}!
1002 This is a shorthand that can be used to modify the appearance of the brace path for enclosing regions.
1003 \end{option*}
1004
1005 \begin{option*}{yquant/enclose}{every brace above or left}!!
1006 This style is applied to the path drawn for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace above} or \ttlink{/yquant/enclose/brace left} styles.
1007 Note that the option has to be specified \emph{before} the brace style is invoked.
1008 \end{option*}
1009
1010 \begin{option*}{yquant/enclose}{brace above or left style}!/yquant/enclose/every brace above or left/.append style={#1}!
1011 This is a shorthand that can be used to modify the appearance of the brace path above or to the left of enclosing regions.
1012 \end{option*}
1013
1014 \begin{option*}{yquant/enclose}{every brace below or right}!!
1015 This style is applied to the path drawn for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace below} or \ttlink{/yquant/enclose/brace right} styles.
1016 Note that the option has to be specified \emph{before} the brace style is invoked.
1017 \end{option*}
1018
1019 \begin{option*}{yquant/enclose}{brace below or right style}!/yquant/enclose/every brace below or right/.append style={#1}!
1020 This is a shorthand that can be used to modify the appearance of the brace path below or to the right of enclosing regions.
1021 \end{option*}
1022
1023 \begin{option*}{yquant/enclose}{every brace above}!/pgf/decoration/raise=1pt!
1024 This style is applied to the path drawn for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace above} or \ttlink{/yquant/enclose/brace left} styles---it will only be active in horizontal layout.
1025 Note that the option has to be specified \emph{before} the brace style is invoked.
1026 \end{option*}
1027
1028 \begin{option*}{yquant/enclose}{brace above style}!/yquant/enclose/every brace above/.append style={#1}!
1029 This is a shorthand that can be used to modify the appearance of the brace path above enclosing regions.
1030 \end{option*}
1031
1032 \begin{option*}{yquant/enclose}{every brace left}!/pgf/decoration/raise=2pt, /pgf/decoration/mirror!
1033 This style is applied to the path drawn for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace above} or \ttlink{/yquant/enclose/brace left} styles---it will only be active in vertical layout.
1034 Note that the option has to be specified \emph{before} the brace style is invoked.
1035 \end{option*}
1036
1037 \begin{option*}{yquant/enclose}{brace left style}!/yquant/enclose/every brace left/.append style={#1}!
1038 This is a shorthand that can be used to modify the appearance of the brace path to the left of enclosing regions.
1039 \end{option*}
1040
1041 \begin{option*}{yquant/enclose}{every brace below}!/pgf/decoration/raise=1pt, /pgf/decoration/mirror!
1042 This style is applied to the path drawn for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace below} or \ttlink{/yquant/enclose/brace right} styles---it will only be active in horizontal layout.
1043 Note that the option has to be specified \emph{before} the brace style is invoked.
1044 \end{option*}
1045
1046 \begin{option*}{yquant/enclose}{brace below style}!/yquant/enclose/every brace below/.append style={#1}!
1047 This is a shorthand that can be used to modify the appearance of the brace path below enclosing regions.
1048 \end{option*}
1049
1050 \begin{option*}{yquant/enclose}{every brace right}!/pgf/decoration/raise=2pt!
1051 This style is applied to the path drawn for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace below} or \ttlink{/yquant/enclose/brace right} styles---it will only be active in vertical layout.
1052 Note that the option has to be specified \emph{before} the brace style is invoked.
1053 \end{option*}
1054
1055 \begin{option*}{yquant/enclose}{brace right style}!/yquant/enclose/every brace right/.append style={#1}!
1056 This is a shorthand that can be used to modify the appearance of the brace path to the right of enclosing regions.
1057 \end{option*}
1058
1059 \paragraph{Brace label styles}\leavevmode
1060 \begin{option*}{yquant/enclose}{every brace label}!label distance=4pt!
1061 This style is applied to the label for every brace put to an enclosing region using any of \ttlink{/yquant/enclose/brace above}, \ttlink{/yquant/enclose/brace left}, \ttlink{/yquant/enclose/brace below}, or \ttlink{/yquant/enclose/brace right}.
1062 Note that the option has to be specified \emph{before} the brace style is invoked.
1063 \end{option*}
1064
1065 \begin{option*}{yquant/enclose}{brace label style}!/yquant/enclose/every brace label/.append style={#1}!
1066 This is a shorthand that can be used to modify the appearance of the labels of braces in enclosing regions.
1067 \end{option*}
1068
1069 \begin{option*}{yquant/enclose}{every brace label above or left}!!
1070 This style is applied to the label for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace above} or \ttlink{/yquant/enclose/brace left} styles.
1071 Note that the option has to be specified \emph{before} the brace style is invoked.
1072 \end{option*}
1073
1074 \begin{option*}{yquant/enclose}{brace label above or left style}!/yquant/enclose/every brace label above or left/.append style={#1}!
1075 This is a shorthand that can be used to modify the appearance of the labels of braces above or to the left of enclosing regions.
1076 \end{option*}
1077
1078 \begin{option*}{yquant/enclose}{every brace label below or right}!!
1079 This style is applied to the label for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace below} or \ttlink{/yquant/enclose/brace right} styles.
1080 Note that the option has to be specified \emph{before} the brace style is invoked.
1081 \end{option*}
1082
1083 \begin{option*}{yquant/enclose}{brace label below or right style}!/yquant/enclose/every brace label below or right/.append style={#1}!
1084 This is a shorthand that can be used to modify the appearance of the labels of braces below or to the right of enclosing regions.
1085 \end{option*}
1086
1087 \begin{option*}{yquant/enclose}{every brace label above}!align=center!
1088 This style is applied to the label for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace above} or \ttlink{/yquant/enclose/brace left} styles---it will only be active in horizontal layout.
1089 Note that the option has to be specified \emph{before} the brace style is invoked.
1090 \end{option*}
1091
1092 \begin{option*}{yquant/enclose}{brace label above style}!/yquant/enclose/every brace label above/.append style={#1}!
1093 This is a shorthand that can be used to modify the appearance of the labels of braces above enclosing regions.
1094 \end{option*}
1095
1096 \begin{option*}{yquant/enclose}{every brace label left}!align=right!
1097 This style is applied to the label for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace above} or \ttlink{/yquant/enclose/brace left} styles---it will only be active in vertical layout.
1098 Note that the option has to be specified \emph{before} the brace style is invoked.
1099 \end{option*}
1100
1101 \begin{option*}{yquant/enclose}{brace label left style}!/yquant/enclose/every brace label left/.append style={#1}!
1102 This is a shorthand that can be used to modify the appearance of the labels of braces to the left of enclosing regions.
1103 \end{option*}
1104
1105 \begin{option*}{yquant/enclose}{every brace label below}!align=center!
1106 This style is applied to the label for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace below} or \ttlink{/yquant/enclose/brace right} styles---it will only be active in horizontal layout.
1107 Note that the option has to be specified \emph{before} the brace style is invoked.
1108 \end{option*}
1109
1110 \begin{option*}{yquant/enclose}{brace label below style}!/yquant/enclose/every brace label below/.append style={#1}!
1111 This is a shorthand that can be used to modify the appearance of the labels of braces below enclosing regions.
1112 \end{option*}
1113
1114 \begin{option*}{yquant/enclose}{every brace label right}!align=left!
1115 This style is applied to the label for every brace put to an enclosing region using \ttlink{/yquant/enclose/brace below} or \ttlink{/yquant/enclose/brace right} styles---it will only be active in vertical layout.
1116 Note that the option has to be specified \emph{before} the brace style is invoked.
1117 \end{option*}
1118
1119 \begin{option*}{yquant/enclose}{brace label right style}!/yquant/enclose/every brace label right/.append style={#1}!
1120 This is a shorthand that can be used to modify the appearance of the labels of braces to the right of enclosing regions.
1121 \end{option*}
1122
1123 \clearpage
1124 \paragraph{Combined shorthands}\leavevmode
1125 \begin{option*}{yquant/enclose}{bracing style}!/yquant/enclose/brace style={#1},/yquant/enclose/brace label style={#1}!
1126 This is a shorthand that can be used to modify the appearance of both the brace path as well as its labels in enclosing regions.
1127 \end{option*}
1128
1129 \begin{option*}{yquant/enclose}{bracing style above or left}!/yquant/enclose/brace above or left style={#1},/yquant/enclose/brace above or left label style={#1}!
1130 This is a shorthand that can be used to modify the appearance of both the brace path as well as its labels above or to the left of enclosing regions.
1131 \end{option*}
1132
1133 \begin{option*}{yquant/enclose}{bracing style below or right}!/yquant/enclose/brace below or right style={#1},/yquant/enclose/brace below or right label style={#1}!
1134 This is a shorthand that can be used to modify the appearance of both the brace path as well as its labels below or to the right of enclosing regions.
1135 \end{option*}
1136
1137 \begin{option*}{yquant/enclose}{bracing style above}!/yquant/enclose/brace above style={#1},/yquant/enclose/brace above label style={#1}!
1138 This is a shorthand that can be used to modify the appearance of both the brace path as well as its labels above enclosing regions.
1139 \end{option*}
1140
1141 \begin{option*}{yquant/enclose}{bracing style left}!/yquant/enclose/brace left style={#1},/yquant/enclose/brace left label style={#1}!
1142 This is a shorthand that can be used to modify the appearance of both the brace path as well as its labels to the left of enclosing regions.
1143 \end{option*}
1144
1145 \begin{option*}{yquant/enclose}{bracing style below}!/yquant/enclose/brace below style={#1},/yquant/enclose/brace below label style={#1}!
1146 This is a shorthand that can be used to modify the appearance of both the brace path as well as its labels below enclosing regions.
1147 \end{option*}
1148
1149 \begin{option*}{yquant/enclose}{bracing style right}!/yquant/enclose/brace right style={#1},/yquant/enclose/brace right label style={#1}!
1150 This is a shorthand that can be used to modify the appearance of both the brace path as well as its labels to the right of enclosing regions.
1151 \end{option*}
1152
1153 \subsection[Importing circuit from files]{\compat{New in 0.2}Importing circuits from files}\label{sec:import}\linkdef\yquantimport
1154 \Yquant{} provides a simple way to import circuits that are stored in external files.
1155 The macro \tex!\yquantimport! can be used in three different contexts:
1156 \begin{itemize}
1157 \item Outside of a \TikZ{} picture environment. \\
1158 Here, \tex!\yquantimport[<options>]{<filename>}! will be equivalent to
1159 \begin{minted}{tex}
1160 \begin{tikzpicture}
1161 \begin{yquant}[<options>]
1162 % the content of <filename> goes here
1163 \end{yquant}
1164 \end{tikzpicture}
1165 \end{minted}
1166 The starred form, \tex!\yquantimport*[<options>]{<filename>}!, instead inserts the starred \Yquant{} environment.
1167 Note that the options are always \Yquant{} options; if you want to pass \TikZ{} options, you will have to create the picture environment by yourself or change the option path to the correct one (\texttt{/tikz/.cd}).
1168 \item Inside a \TikZ{} picture environment, but outside of a \Yquant{} environment. \\
1169 This is the same as before, just that no extra picture environment will be added.
1170 \item Inside both a \TikZ{} picture environment and a \Yquant{} environment. \\
1171 The file will be inserted directly into the environment.
1172 \Yquant's parser is automatically restarted after this.
1173 The content will always be put in a \TeX{} group; if additional options are provided, \Yquant{} also inserts a \TikZ{} \tex!scope! and executes \texlink[\yquantset]{\yquantset{<options>}} directly after the scope.
1174 If \tex!\yquantimport! is used, the content will be read as if the containing environment was an unstarred one; if \tex!\yquantimport*! is used, the content will be read as if the containing environment was a starred one.
1175 \end{itemize}
1176 Note that \Yquant{} internally uses plain \TeX's \tex!\import! command (i.e., \tex!\@@import! in \LaTeX).
1177 \linkdef\yquantimportpath
1178 However, when the \pkg{import} package is loaded, it uses \\ \tex!\subimport{\yquantimportpath}{<filename>}!, where \tex!\yquantimportpath! defaults to \texttt{./}---so by changing this, files from other folders may be imported which by themselves again include other files, and the relative path resolution will work.
1179
1180 Note that you may in particular import the content of a \ttlink{subcircuit}.
1181
1182 \clearpage
1183 \subsection{Defining own gates}\label{sec:customgate}
1184 \begin{warning}[Scope]
1185 All gate declarations are always global.
1186 \end{warning}
1187
1188 \compat{Changed in 0.8\\New in 0.2.1}\linkdef\yquantdefinebox
1189 If you want to define a gate that corresponds to a single \ttlink{box} gate with a certain pre\hyp defined content, you may use the macro\\
1190 \tex!\yquantdefinebox[<attributes>]{<name>}[<style>]{<content>}!, which is far more efficient than the much more general \texlink{\yquantdefinegate} introduced below.
1191 It only allows for single\hyp register usage; use \compat{New in 0.4}\linkdef\yquantdefinemultibox\tex!\yquantdefinemultibox! with the same arguments if you want to allow the gate to be used in a multi\hyp register gate fashion.
1192 The macros work in the following way:
1193 \begin{itemize}
1194 \item They create a new gate with name \texttt{<name>} that can be accessed as all the other build\hyp in gates.
1195 Note that \texttt{<name>} is case\hyp insensitive and may not contain spaces.
1196 Special characters are allowed if \TeX{} can cope with them (i.e., no comment signs, no unbalanced braces, no backslashes...).
1197 \item They create a style \texttt{/yquant/operators/every <name>} and assign the optional \texttt{<style>} to it.
1198 If no style is provided, the default style will inherit from \ttlink{/yquant/operators/every rectangular box}\compat{Changed in 0.6}.
1199 If a \ttlink{compat} version before \texttt{0.6} is chosen, \ttlink{/yquant/operators/every box} will instead be the ancestor.
1200 \item \compat{New in 0.8}They define attributes for the gate as specified in \texttt{<attributes>}, where \texttt{<attributes>} is one of the following:
1201 \begin{itemize}
1202 \item \linkdef{required attrs}\tex!required attrs={<list of attributes>}!
1203 \item \linkdef{optional attrs}\tex!optional attrs={<list of attributes>}!
1204 \end{itemize}
1205 or both, comma separated.
1206 The \texttt{<list of attributes>} is a comma\hyp separated list of attribute names that are known to \Yquant.
1207 \linkdef\yquantdeclareattr
1208 You may declare custom attributes using \tex!\yquantdeclareattr!; note that an attribute is just a \pkg{pgfkeys} style.
1209 Hence, if you want to define an attribute \texttt{myattr} that stores a value in the macro \tex!\myattr!, you may say\\\tex!\yquantdeclareattr{myattr/.store in=\myattr}!.
1210 \item They define \texttt{<content>} to be the value that is written into the box.
1211 You may need to prefix fragile macros by \tex!\protect!.
1212 \compat{Changed in 0.8}\linkdef\yquantedefinebox
1213 By default, the \texttt{<content>} is not expanded; use \tex!\yquantedefinebox! to expand it in a protected manner.
1214 In this case only, if you stored some content in a macro by means of an attribute definition, you should prefix the macro by \tex!\noexpand!; hence, in the previous example, you'd write \tex!\noexpand\myattr! whenever you want to print the content of \tex!\myattr!.
1215
1216 \compat{New in 0.8}Note that if you want to use the \ttlink{value} attribute which holds whatever is passed to the gate in curly brackets, a more convenient way exists.
1217 You do not have to specify \ttlink{value} manually in \texttt{required attrs}, and you don't have to use a special macro to access its value; instead, you can simply say \tex!#1! in the \texttt{<content>}.
1218 If this \TeX{} parameter slot is found, \ttlink{value} will automatically be made into a required attribute, and \tex!#1! will be replaced by the actual value.
1219
1220 Note that before version~0.8, the behavior of \tex!\yquantdefinebox! was to expand during definition; the old behavior is restored with a \ttlink{compat} version smaller than~0.8.
1221 \end{itemize}
1222
1223 \compat{Changed in 0.8\\New in 0.2}\linkdef\yquantdefinegate
1224 Sometimes, you may wish to define gates that are more than just a single box---perhaps a succession of multiple gates or even multi\hyp register gates with individual operations on the input registers.
1225 \Yquant{} provides a simple macro that allows this; \tex!\yquantdefinegate[<attributes>]{<name>}[<style>]{<content>}! works in the following way:
1226 \begin{itemize}
1227 \item It creates a new gate with name \texttt{<name>} that can be accessed as all the other built\hyp in gates.
1228 Note that \texttt{<name>} is case\hyp insensitive and may not contain spaces.
1229 Special characters are allowed if \TeX{} can cope with them (i.e., no comment signs, no unbalanced braces, no backslashes...).
1230 \item It creates a style \texttt{/yquant/operators/every <name>} and assigns the optional \texttt{<style>} to it.
1231 If no style is provided, the default style will inherit from \ttlink{/yquant/operators/every custom gate}.
1232 This will make the gate ``seamless,'' i.e., avoid highlighting the fact that this is a custom gate.
1233 \item It assigns attributes according to \texttt{<attributes>} to the gate as before.
1234 \item It defines a macro that contains \texttt{<content>} and that will be inserted as a subcircuit whenever this gate is invoked.
1235 You may need to prefix fragile macros by \tex!\protect!.
1236 \compat{Changed in 0.8}\linkdef\yquantedefinegate
1237 By default, the \texttt{<content>} is not expanded; use \tex!\yquantedefinegate! to expand it in a protected manner.
1238 All that was said for \texlink{\yquantdefinebox} still holds; however, note one intricacy with attributes: they are assigned when the gate is used.
1239 The attributes defined by \Yquant, except for \ttlink{value} (which is available through \tex!#1!), are applied only to the gate \emph{as a whole}, but they are reset before the content is inserted as a subcircuit.
1240 This makes sure that the attributes are not applied to each gate within the custom gate individually, but only to the box.
1241 However, \Yquant{} will not reset custom attributes (which is basically impossible, as it does not know which effect such an attribute has).
1242 Therefore, a custom attribute will keep its value while the custom gate is applied and may affect gates inside---which could be intended or not.
1243 If it is not, you must make sure to reset the attributes by yourself at an appropriate position.
1244 \end{itemize}
1245 When the gate is later drawn, the styles are invoked in the following order---remember custom gates are implemented by means of subcircuits---:
1246 \begin{enumerate}
1247 \item \ttlink{/yquant/every operator}
1248 \item \texttt{/yquant/operators/every <name>}
1249 \item \ttlink{/yquant/operators/every subcircuit box}
1250 \item \ttlink{/yquant/this operator}
1251 \item \ttlink{/yquant/operators/this subcircuit box}
1252 \end{enumerate}
1253 Gates defined in this way can only make use of the default gates or other custom gates.
1254 They do not accept custom arguments, and it is not possible to declare own, custom shapes in this way (though other predefined shapes may be used).
1255 If they are used in a multi\hyp qubit manner, they will never be split into contiguous slices (but their content will be, so if you use the default style that turns off the box, the only way to notice this is that intermediate unaffected registers will not be allowed to have gates visually within the rectangle that would bound the custom gate).
1256
1257 \begin{warning}[Redefining existing gates\linkdef\yquantredefinebox\linkdef\yquantredefinemultibox\linkdef\yquantredefinegate\linkdef\yquantredefinemultigate\linkdef\yquanteredefinebox\linkdef\yquanteredefinemultibox\linkdef\yquanteredefinegate\linkdef\yquanteredefinemultigate]
1258 The above macros will issue an error if the gate already exists.
1259 You can use \tex!\yquantredefinebox!, \tex!\yquantredefinemultibox! (use the appropriate command for the \emph{new} definition), or \tex!\yquantredefinegate!, or their expanded counterparts to overwrite existing gate definitions.
1260 Note that this will overwrite \emph{any} gate, even the built\hyp in ones.
1261
1262 Generally, it is discouraged to make use of this possibility.
1263 For custom gates, if you redefine a gate as a box which was previously a general subcircuit\hyp based gate, the macro that contains the subcircuit will still be held in memory.
1264 Overwriting built\hyp in gates will not clear the attributes previously associated to this gate (though required attributes will no longer be required afterwards).
1265 Again, this is not a problem but prevents \Yquant{} from issuing potentially helpful error message if such a---now meaningless---attribute is used.
1266
1267 Finally, once a built\hyp in gate is overwritten, it cannot be restored.
1268 In particular, the register creation pseudo\hyp gates \ttlink{qubit}, \ttlink{cbit}, \ttlink{qubits}, and \ttlink{nobit} perform some magic that cannot be mimicked with custom gates.
1269 \end{warning}
1270
1271 More advanced declaration of custom gates requires the use of backend macros.
1272 Refer to \texttt{yquant-lang.tex} for this.
1273 Note that the backend interface changed in version~0.4.
1274 For the declaration of custom shapes, see \texttt{yquant-shapes.tex} for examples.
1275
1276 \subsection[The flow of time: horizontal and vertical layout]{\compat{New in 0.7}The flow of time: horizontal and vertical layout}
1277 By default, quantum circuits are oriented \emph{horizontally}, i.e., time flows from left to right.
1278 Sometimes, this is problematic when printed on a portrait page layout and a \emph{vertical} layout would be better suited.
1279 \Yquant{} supports this by simply passing the configuration option \ttlink{/yquant/vertical} to the circuit (this can even be done globally, so that all circuits are rendered vertically).
1280
1281 The vertical layout is supposed to work exactly as the horizontal layout; however, most testing occurs for horizontal mode only (basically, for every release, the examples in this manual comprise the testsuite).
1282 Hence, if something weird happens in vertical mode, please file a bug report.
1283
1284 Most of the internal and exposed nomenclature in \Yquant{} is designed for the horizontal case (names such as ``height'' or ``width'').
1285 For some of the exposed configuration options, synonyms are provided which are more meaningful in vertical mode or that are more orientation\hyp agnostic.
1286 These synonyms are merely conveniences, they do not provide any new functionality.
1287 As a general rule of thumb, everything that was ``atop'' becomes ``left.''
1288
1289 Some relevant \TikZ{} options refer to a fixed direction, namely \texttt{[x|y] radius} and \texttt{[inner|outer] [x|y]sep}.
1290 If you think that maybe you want to change the orientation of your circuit at some point, you should never use the original \TikZ{} styles---their meaning would depend on the current orientation.
1291 Instead, consider using the \Yquant{} alternatives \texttt{[time|space] radius} and \texttt{[inner|outer] [time|space]sep}.
1292
1293 \section{Configuration}\label{sec:config}
1294 \Yquant{} uses \pkg{pgfkeys} to control its options, which are located in the path \texttt{/yquant}.
1295 The following list contains all options and styles that are recognized, apart from gate arguments.
1296 Those are listed together with their operations.
1297
1298 \subsection{Circuit layout}\unskipOpt
1299 \begin{option}[Changed in 0.7, 0.4]{register/minimum height}!1.5mm!
1300 \Yquant{} automatically determines the height (extent from wire to top boundary; in vertical mode: from wire to left boundary) of a register as the height of the largest operation.
1301 This might be too small for two reasons:
1302 \begin{itemize}
1303 \item if the register is used only with small gates (e.g., only as a control, or as a swap), and it does not have a label (or one containing only x\hyp height letters).
1304 \item if you manually turned off height calculation or multi\hyp extent calculation for a large gate.
1305 \Yquant{} will then not consider the vertical extent of this gate, which might consequently lead to undesirable overlaps.
1306 \end{itemize}
1307 This key provides an easy alleviation of the problem by requiring a minimal height for every register.
1308 As the value of this key is relevant at the time of register declaration, it can also be changed for each register individually.
1309
1310 Note that in vertical mode, the default of this setting is \tex!2.5mm!.
1311
1312 Note that this key is affected by the \ttlink{compat} setting.
1313 Before version \texttt{0.4}, there was no \ttlink{/yquant/register/minimum depth} key.
1314 In this compatibility setting, passing the value $x$ to this key will set both height and depth to $\frac x2$.
1315 The default for $x$ is then \texttt{3mm}.
1316 \end{option}
1317
1318 \begin{option}[Changed in 0.7\\New in 0.4]{register/minimum depth}!1.5mm!
1319 \emph{see} \ttlink{/yquant/register/minimum height}
1320
1321 This key allows to specify a minimum depth (extent from wire to bottom boundary; in vertical mode: from wire to right boundary) for a register.
1322
1323 Note that in vertical mode, the default of this setting is \tex!2.5mm!.
1324
1325 Note that this key is affected by the \ttlink{compat} setting.
1326 Before version \texttt{0.4}, this key will not be available.
1327 \end{option}
1328
1329 \begin{option}[New in 0.7]{register/minimum left}!1.5mm!
1330 This is a synonym for \ttlink{/yquant/register/minimum height}, as this naming makes more sense for vertical circuits.
1331 \end{option}
1332
1333 \begin{option}[New in 0.7]{register/minimum right}!1.5mm!
1334 This is a synonym for \ttlink{/yquant/register/minimum depth}, as this naming makes more sense for vertical circuits.
1335 \end{option}
1336
1337 \begin{option}[New in 0.7]{register/minimum before}!1.5mm!
1338 This is a synonym for \ttlink{/yquant/register/minimum height}, which provides a naming that makes sense in both horizontal and vertical mode.
1339 \end{option}
1340
1341 \begin{option}[New in 0.7]{register/minimum after}!1.5mm!
1342 This is a synonym for \ttlink{/yquant/register/minimum depth}, which provides a naming that makes sense in both horizontal and vertical mode.
1343 \end{option}
1344
1345 \begin{option}{register/separation}!1mm!
1346 This key controls the amount of vertical space that is inserted between two successive registers.
1347 Half of this value is also the length that multi\hyp\ttlink{init} or multi\hyp\ttlink{output} braces extend beyond the mid position of the register.
1348 \end{option}
1349
1350 \begin{option}[Changed in 0.7]{operator/minimum width}!5mm!
1351 \Yquant{} automatically determines the width (in vertical mode: the vertical extent) of an operator according to its content.
1352 However, single\hyp letter boxes are among the most common operators, and giving them slightly different widths would result in a very uneven spacing, as \Yquant{} does not use a grid layout but stacks the operators horizontally one after each other.
1353 Hence, this key provides a minimum width that will be set for every operator.
1354 This does not imply that the \emph{visual} appearance (i.e., the \texttt{x radius} key) is enlarged, but that operators of a smaller actual width will be centered in a virtual box of the minimum width.
1355
1356 Note that in vertical mode, the default of this setting is \tex!3mm!.
1357 \end{option}
1358
1359 \begin{option}[New in 0.7]{operator/minimum extent}!3mm!
1360 This is a synonym for \ttlink{/yquant/operator/minimum width}, which provides a naming that makes sense in both horizontal and vertical mode.
1361 \end{option}
1362
1363 \begin{option}{operator/separation}!1mm!
1364 This key controls the amount of horizontal space that is inserted between two successive operators and at the beginning and end of a circuit.
1365 \end{option}
1366
1367 \begin{option}[New in 0.1.2]{operator/multi warning}!true!
1368 If this key is \texttt{true}, a warning is displayed whenever more than a single multi\hyp register gate, where at least one is discontiguous, is employed together with controls.
1369 Even if a visual distinction between control and multi\hyp qubit line may be possible (depending on the style in use), they will overlap and produce unaesthetic output.
1370 You may disable this warning globally, on a per\hyp circuit, or even on a per\hyp gate basis.
1371 \end{option}
1372
1373 \begin{option}[New in 0.7]{drawing mode}!quality!
1374 This key determines which drawing pipeline is enabled.
1375 For more details, see \cref{sec:pipeline}.
1376 The option should not be changed within a circuit (though only the last value will be relevant).
1377 Allowed values are \texttt{quality} for the default clipping\hyp based pipeline, and \texttt{size} of the filling\hyp based one.
1378 Note that choosing \texttt{quality} will set \ttlink{/yquant/default background} to \texttt{none}; choosing \texttt{size} will set it to \texttt{white} (though this can be changed after setting the option).
1379 \end{option}
1380
1381 \begin{option}[New in 0.7]{default background}!none!
1382 This key contains the default color that is used to fill all gates with a nonempty interior.
1383 \end{option}
1384
1385 \begin{option}[New in 0.7]{default fill}!fill/.expanded=\pgfkeysvalueof{/yquant/default background}!
1386 Use this style if you want to apply the default filling to a user\hyp defined gate.
1387 \end{option}
1388
1389 \subsection{Register creation}\unskipOpt
1390 \begin{option}{register/default name}!\regidx!
1391 The printed name that is used by default if a new register is created explicitly (\ttlink{qubit}, \ttlink{cbit}, \ttlink{qubits}; not used for \ttlink{nobit} or for implicit declarations) and no value is specified.
1392 The following macros are available:
1393 \begin{itemize}
1394 \item \linkdef\reg\tex!\reg! contains your name to identify this register.
1395 \item \linkdef\idx\tex!\idx! contains the index (zero\hyp based) of the current register within a vector register.
1396 \compat{Changed in 0.8}Note that before version~0.8, this was not strictly true; \tex!\idx! contained the index within the \emph{currently added} registers.
1397 Most often, this is the same; however, if you were adding, say, the second part of a discontinuous register (or you were using the \langlink{groups} library), then the indices would have started with zero again.
1398 Starting from \ttlink{compat} version~0.8, \tex!\idx! will always contain the index of the complete vector register.
1399 \item \linkdef\regidx\tex!\regidx! expands to \tex!\reg! if the register is of length one, and to \tex!\reg[\idx]! else.
1400 \compat{Changed in 0.8}Note that before version~0.8, this decision was made during the creation; therefore, if you first created a register of length one which you later added some further registers to, the first label would have expanded \tex!\regidx! to \tex!\reg!.
1401 Starting from \ttlink{compat} version~0.8, this decision is deferred until the drawing stage, when the actual length is known.
1402 As a consequence, to measure the vertical extent of the label consistently, \tex!\regidx! will always be at least as high as the opening square bracket.
1403 This can make registers with no or only very small gates larger than before; therefore, this setting is compatibility\hyp protected.
1404 \item \linkdef\len\tex!\len! contains the length of the current register vector.
1405 Note that \emph{current} means \emph{at the stage of creation}.
1406 As this was always documented in this way (while the behavior of the other macros did not match their documentation), the behavior is the same with all compatibility settings.
1407 \end{itemize}
1408 \end{option}
1409
1410 \begin{option}[Changed in 0.8\\New in 0.6]{register/default lazy name}!!
1411 The printed name that is used by default if a new register is created implicitly (i.e., without using any of \ttlink{qubit}, \ttlink{cbit}, \ttlink{qubits}, or \ttlink{nobit}, but inside a \texlink{yquant*} environment by just using the register).
1412 The same macros as with \ttlink{/yquant/register/default name} are available.
1413 Note that this default setting is not used when the register is created via an \ttlink{init} gate---its value always overwrites the default.
1414 \end{option}
1415
1416 \begin{option}[Changed in 0.7, 0.4]{every label}!shape=yquant-init, anchor=center, align/.expanded=\ifyquanthorz{right}{center}, outer timesep=2pt, /yquant/operator/if multi={draw, decoration={gapped brace, raise=2pt, \ifyquanthorz{mirror}{}}, decorate}!
1417 This style is installed for every single register name label (i.e., upon creation and when used with the \ttlink{init} gate).
1418 The default style allows to use line breaks in the labels.
1419 \linkdef{yquant-init}\linkdef{gapped brace}The node shape, \texttt{yquant-init}, will generate a path at its right side (in vertical mode: at its bottom side), which is replaced by the \texttt{gapped brace} decoration if the gate is used in a multi\hyp register fashion.
1420 The decoration is similar to \TikZ's \texttt{brace} decoration, but additionally allows specify the regions in which a line should be drawn by using the \texttt{/tikz/decoration/from to} key, which expects a comma\hyp separated list of dimension ranges, and which is automatically populated by \Yquant.
1421
1422 Note that if the \ttlink{compat} key is below \texttt{0.3}, the multi options are instead read from \ttlink{/yquant/every multi label}.
1423 \end{option}
1424
1425 \begin{option}[Changed in 0.7]{every initial label}!anchor/.expanded=\ifyquanthorz{east}{south}, /yquant/internal/autorotate init!
1426 This style is installed for every single register name label at the left border of the circuit.
1427 \compat{Changed in 0.4}It is therefore used if a label is specified upon declaration and also for the \ttlink{init} gate if it happens to be the first gate on an unlabelled register (use a zero\hyp width \ttlink{hspace} gate before if you want to suppress this).
1428
1429 \compat{New in 0.7}The automatic rotation will be set up by using the \ttlink{/yquant/vertical} style with an argument; by default, it is empty.
1430 \end{option}
1431
1432 \begin{option}{every qubit label}!!
1433 This style is installed for every single register name label of a register of type \ttlink{qubit}.
1434 \end{option}
1435
1436 \begin{option}{every cbit label}!!
1437 This style is installed for every single register name label of a register of type \ttlink{cbit}.
1438 \end{option}
1439
1440 \begin{option}{every qubits label}!!
1441 This style is installed for every single register name label of a register of type \ttlink{qubits}.
1442 \end{option}
1443
1444 \begin{option}[Removed in 0.4\\Changed in 0.1.2]{every multi label}!draw, decoration={gapped brace, mirror, raise=2pt}, decorate!
1445 This style is installed for every register name label that is attached to a multi\hyp qubit register by means of the \ttlink{init} gate.
1446
1447 Note that this key is only available if the \ttlink{compat} setting is smaller than \texttt{0.4}.
1448 In newer versions, this is incorporated in \ttlink{/yquant/every label}.
1449 \end{option}
1450
1451 \begin{option}[Removed in 0.4\\New in 0.2]{every input label}!!
1452 This style is installed for every register name label in a \ttlink{subcircuit} when the register is an input (or input and output) register.
1453
1454 Note that this key is only available if the \ttlink{compat} setting is smaller than \texttt{0.4}; and in this case, it behaves inconsistently, as it is only applied for labels directly specified during creation, but not for initial \ttlink{init} gates.
1455 \end{option}
1456
1457 \subsection{Register outputs}\unskipOpt
1458 \begin{option}[Changed in 0.7, 0.4]{every output}!shape=yquant-output, anchor/.expanded=\ifyquanthorz{west}{north}, align/.expanded=\ifyquanthorz{left}{center}, outer timesep=2pt, /yquant/operator/if multi={draw, decoration={gapped brace, raise=2pt, \ifyquanthorz{}{mirror}}, decorate}, /yquant/internal/autorotate output!
1459 This style is installed for every \ttlink{output} label at the end of the circuit.
1460 The default style allows to use line breaks in the labels.
1461
1462 \linkdef{yquant-output}The node shape, \texttt{yquant-output}, will generate a path at its left side (in vertical mode: at its top side), which is replaced by the \ttlink{gapped brace} decoration in the case of multi\hyp register usage.
1463 See \ttlink{/yquant/every label} for a more detailed explanation.
1464
1465 \compat{New in 0.7}The automatic rotation will be set up by using the \ttlink{/yquant/vertical} style with an argument; by default, it is empty.
1466 \end{option}
1467
1468 \begin{option}{every qubit output}!!
1469 This style is installed for every \ttlink{output} label of a register of type \ttlink{qubit}.
1470 \end{option}
1471
1472 \begin{option}{every cbit output}!!
1473 This style is installed for every \ttlink{output} label of a register of type \ttlink{cbit}.
1474 \end{option}
1475
1476 \begin{option}{every qubits output}!!
1477 This style is installed for every \ttlink{output} label of a register of type \ttlink{qubits}.
1478 \end{option}
1479
1480 \begin{option}[Removed in 0.4\\Changed in 0.1.2]{every multi output}!draw, decoration={gapped brace, raise=2pt}, decorate!
1481 This style is installed for every \ttlink{output} label that is attached to a multi\hyp qubit register.
1482
1483 Note that this key is only available if the \ttlink{compat} setting is smaller than \texttt{0.4}.
1484 In newer versions, this is incorporated into \ttlink{/yquant/every output}.
1485 \end{option}
1486
1487 \subsection{General styling}\unskipOpt
1488 \begin{option}[Changed in 0.4, 0.1.2]{every circuit}!every node/.prefix style={transform shape}!
1489 Style that is installed for every \texlink{yquant} and \texlink{yquant*} environment, as if it had been given as an option.
1490 The style's default path is, as with all other styles, \texttt{/tikz}.
1491 The style is re\hyp applied for every subcircuit.
1492 The default style will make all nodes (which in particular means, all gates) respect outer canvas transformations.
1493
1494 If your \TikZ{} version is before 3.1.6a, this style will additionally contain \tex!every label/.prefix style={transform shape=false}!, which undoes the effect for \texttt{label}s (see \TikZ{} bug \href{https://github.com/pgf-tikz/pgf/issues/843}{\#843}).
1495 An update is recommended.
1496 \end{option}
1497
1498 \begin{option}{every wire}!draw!
1499 This style is installed whenever a wire is drawn.
1500 \end{option}
1501
1502 \begin{option}{every qubit wire}!!
1503 This style is installed whenever a wire for a register of type \ttlink{qubit} is drawn.
1504 \end{option}
1505
1506 \begin{option}{every cbit wire}!!
1507 This style is installed whenever a wire for a register of type \ttlink{cbit} is drawn.
1508 \end{option}
1509
1510 \begin{option}{every qubits wire}!!
1511 This style is installed whenever a wire for a register of type \ttlink{qubits} is drawn.
1512 \end{option}
1513
1514 \begin{option}{every control line}!draw!
1515 This style is used to draw the vertical control line that connects controlled operations and their controls.
1516 \end{option}
1517
1518 \begin{option}{every control}!shape=yquant-circle, anchor=center, radius=.5mm!
1519 This style is used to draw the node for a control, both positive and negative.
1520 \end{option}
1521
1522 \begin{option}{every positive control}!fill=black!
1523 This style is installed for every positive control (i.e., one that conditions on the register being in state $\ket1$ or $1$).
1524 \end{option}
1525
1526 \begin{option}[Changed in 0.7]{every negative control}!draw, /yquant/default fill!
1527 This style is installed for every negative control (i.e., one that conditions on the register being in state $\ket0$ or $0$).
1528 \end{option}
1529
1530 \begin{option}{every operator}!anchor=center!
1531 This style is installed for every gate (and also pseudo\hyp gates such as the \ttlink{slash} operator) that acts on one or multiple registers.
1532 \end{option}
1533
1534 \begin{option}[New in 0.1.2]{every multi line}!draw, decoration={snake, amplitude=.25mm, segment length=5pt}, decorate!
1535 This style is used to draw the vertical line that connects discontiguous slices of sub\hyp gates.
1536 \end{option}
1537
1538 \begin{option}{this operator}!!
1539 This style is appended to the current style installed for an operator; it should be used only locally to overwrite any global configuration effect. \\
1540 \compat{New in 0.7.3}Consider using \ttlink{/tikz/only at} and \ttlink{/tikz/not at} \emph{within this style} if you want to selectively address certain operators when there are multiple.
1541 \end{option}
1542
1543 \begin{option}{this control}!!
1544 This style is appended to the current style installed for a control; it should be used only locally to overwrite any global configuration effect. \\
1545 \compat{New in 0.7.3}Consider using \ttlink{/tikz/only at} and \ttlink{/tikz/not at} \emph{within this style} if you want to selectively address certain controls when there are multiple.
1546 \end{option}
1547
1548 \begin{option}{operator style}!/yquant/this operator/.append style={#1}!
1549 This is a shorthand that can be used to modify the appearance of the current operator.
1550 \end{option}
1551
1552 \begin{option}{control style}!/yquant/every control line/.append style={#1}, /yquant/this control/.append style={#1}!
1553 This is a shorthand that can be used to modify the appearance of the current control and its associated line.
1554 \end{option}
1555
1556 \begin{option}{style}!/yquant/operator style={#1}, /yquant/control style={#1}!
1557 This is a shorthand that modifies the appearance of both the current operator and any controls or control lines.
1558 \end{option}
1559
1560 \begin{option*}[New in 0.7.3]{tikz}{only at}!!
1561 This is a style which is available within \ttlink{/yquant/this operator} and \ttlink{/yquant/this control}.
1562 It allows to selectively apply its contents only to a certain part of a multi\hyp partite operator.
1563 The style expects an even number of parameters.
1564 The first (third, ...) parameter contains the index of the operator/control where the style should apply.
1565 This index corresponds to the values that \texlink{\idx} can take; i.e., it will be zero for the topmost operator/control (leftmost for vertical circuits) and then increment by one.
1566 The indices of operators, positive, and negative controls are all independent.
1567 You may not only give a single index, but instead also a comma\hyp separated list, and you may also use index ranges (where both the first and second part of the range can be omitted, which then means ``from the first'' or ``to the last'').
1568 The second (fourth, ...) parameter then contains all the styles that should actually be applied.
1569
1570 \begin{codeexample}
1571 \begin{tikzpicture}
1572 \begin{yquant}
1573 qubit q[7];
1574 [operator style={only at={1, 3-}{fill=yellow}
1575 {0}{ultra thick}},
1576 control style={only at={0}{fill=orange}}]
1577 h q[-2, 4, 5] | q[3, 6];
1578 \end{yquant}
1579 \end{tikzpicture}
1580 \end{codeexample}
1581 \end{option*}
1582
1583 \begin{option*}[New in 0.7.3]{tikz}{not at}!!
1584 See \ttlink{/tikz/not at}; this style does the same, but instead only acts if the current register is \emph{not} the one given in the odd arguments.
1585 \end{option*}
1586
1587 \begin{option}[Changed in 0.7.1\\New in 0.1.2]{operator/multi as single}!/yquant/every multi line/.style=/yquant/every control line!
1588 This style is automatically set for certain gates such as the \ttlink{swap} or the \ttlink{zz} gate.
1589 For those gates, neighboring registers will be treated as discontiguous; and this style will enforce their connecting line to have the style used by control lines.
1590
1591 \compat{New in 0.7.1}This style actually checks whether control lines are present in the gate and in this case, it is equal to \tex!draw=none!, i.e., it suppresses the multi\hyp register line.
1592 This is due to the fact that this line would be drawn on top of (a segment of) the control line with an identical style, so that it cannot be seen unless there were some bug in the renderer.
1593 Note that the check for control lines is embedded into the style (the default value shown here is a simplification); so if you change it, you will overwrite the style for \emph{all} cases, not only when there is no control line.
1594
1595 The default \ttlink{/yquant/every multi line} is a wavy line; this allows to distinguish discontiguous multi\hyp qubit gates from multiple single\hyp qubit gates when using controls.
1596 Still, some gates have such an established appearance that---despite being logically misleading---we rather use the same style as for a control line.
1597 \end{option}
1598
1599 \begin{option}[New in 0.4]{operator/if multi}!!
1600 This style can be invoked by other styles with an arguments that contains styles to be executed only if the current gate is used in a multi\hyp register fashion.
1601 See \ttlink{/yquant/every label} for an example.
1602 \end{option}
1603
1604 \begin{option}[New in 0.4]{circuit/seamless}!false!
1605 The value of this setting determines whether circuits drawn in a \Yquant{} environment in the current group will be drawn in a ``seamless'' state (hence, this style must be set \emph{before} the \Yquant{} environment is started).
1606 The key \ttlink{/yquant/operator/separation} will control the amount of padding with which a wire starts or ends before the first or after the last gate.
1607 By turning on the seamless state, this padding is suppressed.
1608 Using outputs or giving an initial value at the register declaration brings the corresponding padding back.
1609 Usually, this setting is intended only for subcircuits.
1610 Direct access is discouraged, as it will persist in subcircuits.
1611 Only access it via \ttlink{/yquant/operators/subcircuit/seamless}.
1612 \end{option}
1613
1614 \begin{option}[New in 0.7]{circuit/orientation}!horizontal!
1615 This setting allows two possible values, \texttt{horizontal} and \texttt{vertical}.
1616 It must only be changed before a circuit or at latest with the option arguments to the \texlink{yquant} environment, but not within a circuit.
1617
1618 In the default \texttt{horizontal} mode, time flows from left to right and registers will be created from top to bottom.
1619 In the alternative \texttt{vertical} mode, time flows from top to bottom and registers will be created from left to right.
1620
1621 Note that this setting influences the behavior of various macros and styles:
1622 \begin{center}
1623 \begin{tabular}{lrr}
1624 \toprule
1625 Macro/Style & \texttt{horizontal} meaning & \texttt{vertical} meaning \\\midrule
1626 \tex!\ifyquanthorz{a}{b}! & \tex!a! & \tex!b! \\
1627 \texttt{/tikz/time radius} & \texttt{/tikz/x radius} & \texttt{/tikz/y radius} \\
1628 \texttt{/tikz/space radius} & \texttt{/tikz/y radius} & \texttt{/tikz/x radius} \\
1629 \texttt{/tikz/inner timesep} & \texttt{/tikz/inner xsep} & \texttt{/tikz/inner ysep} \\
1630 \texttt{/tikz/inner spacesep} & \texttt{/tikz/inner ysep} & \texttt{/tikz/inner xsep} \\
1631 \texttt{/tikz/outer timesep} & \texttt{/tikz/outer xsep} & \texttt{/tikz/outer ysep} \\
1632 \texttt{/tikz/outer spacesep} & \texttt{/tikz/outer ysep} & \texttt{/tikz/outer xsep} \\
1633 \bottomrule
1634 \end{tabular}
1635 \end{center}
1636 Additionally, this setting influences the default values of various \Yquant{} styles---note that if a style was once overwritten, the user\hyp supplied value will be never be changed!
1637 Here, we use the orientation\hyp independent names of the styles, although all their synonyms are equivalent.
1638 \begin{center}
1639 \begin{tabular}{lrr}
1640 \toprule
1641 Style & \texttt{horizontal} def. & \texttt{vertical} def. \\\midrule
1642 \ttlink{/yquant/register/minimum before} & \tex!1.5mm! & \tex!2.5mm! \\
1643 \ttlink{/yquant/register/minimum after} & \tex!1.5mm! & \tex!2.5mm! \\
1644 \ttlink{/yquant/operator/minimum extent} & \tex!5mm! & \tex!3mm! \\
1645 \bottomrule
1646 \end{tabular}
1647 \end{center}
1648 \end{option}
1649
1650 \begin{option}[New in 0.7]{horizontal}!!
1651 This style is a shorthand that sets \ttlink{/yquant/circuit/orientation} to the \texttt{horizontal} value (which is the default).
1652 \end{option}
1653
1654 \begin{option}[New in 0.7]{vertical}!0!
1655 This style is a shorthand that sets \ttlink{/yquant/circuit/orientation} to the \texttt{vertical} value.
1656 Additionally, it accepts a parameter which should be an angle value (in degrees) between \tex!-180! and \tex!180!.
1657 This is a rotation that is automatically applied to the text in every \ttlink{init} gate at the beginning of a circuit; the inverse rotation is automatically applied to the text in every \ttlink{output} gate at the end of a circuit.
1658
1659 Note that the gates themselves are \emph{not} rotated.
1660 This would be counterproductive for multi\hyp qubit gates that contain braces---those should still be orthogonal to the wire lines.
1661 In order to achieve this, the macro will use internal \TikZ{} details and exploit that the \texttt{execute at begin node} key is directly followed by the braced content of the node.
1662 Hence, it will add a corresponding \tex!\adjustbox{rotate=#1}! to this style.
1663 Therefore, \Yquant{} will raise a warning if the argument is used and the \pkg{adjustbox} package is not loaded, in which case the rotation is just ignored.
1664 \end{option}
1665
1666 \begin{option}[New in 0.4]{every post measurement control}!indirect!
1667 This style determines the default arrangement of measurements that are followed by positive controls.
1668
1669 \linkdef{indirect}The default option \texttt{indirect} will draw the measurement at the position where it is specified.
1670 Any later use of a control will be at the position of the controlled gate.
1671
1672 \linkdef{direct}The option \texttt{direct} will defer the measurement.
1673 If later on, a controlled operation is used where the positive controls contain all of the targets of this measurement \emph{and} no other gate was executed meanwhile on any of the targets of this measurement, then the measurement gate will replace the corresponding positive control knobs (and might inherit \TikZ{} options of the embedding gate); otherwise, it will behave as if the \texttt{indirect} option had been specified.
1674
1675 Some care must be taken when gates are named that are affected by this option.
1676 If the embedding gate is named, the positive controls that will be replaced by measurements are no longer available with the ``\texttt{p}''~suffix (but other positive controls will still be numbered as if all were).
1677 Attach the name to the measurement in order to access it as if it were an ordinary gate; however, note that the name only becomes available after the later embedding gate was called.
1678
1679 Note that this setting affects all measurements that have a compatible shape; currently, this is only \ttlink{measure}.
1680 While there is no technical difficulty in implementing the same behavior for \ttlink{dmeter}, its particular shape does not really suggest this use.
1681 However, if you desire to do so, please file a feature request.
1682 \end{option}
1683
1684 \subsection{Styles for operators}\unskipOpt
1685 \begin{option}[Changed in 0.4]{operators/every barrier}!shape=yquant-line, dashed, draw, shorten <= -1mm, shorten >= -1mm!
1686 This style is installed for every \ttlink{barrier} pseudo\hyp gate, i.e., the one that is used to explicitly denote a separation between ``before'' and ``after'' within the circuit.
1687
1688 Note that the \texttt{shorten} keys are only present in the default style if you specify at least the compatibility version \texttt{0.4}.
1689 \end{option}
1690
1691 \begin{option}[Changed in 0.6]{operators/every box}!/yquant/operators/every rectangular box!
1692 This style is installed for every \ttlink{box} operator.
1693 Note that with a \ttlink{compat} setting strictly smaller than~\texttt{0.6}, the definition of this style was the one that is now \ttlink{/yquant/operators/every rectangular box}, and this style was also the base style from which all box\hyp like gates inherited.
1694 With a \ttlink{compat} setting of at least~\texttt{0.6}, no other gates apart from \ttlink{box} will use this style directly or indirectly.
1695 \end{option}
1696
1697 \begin{option}[Changed in 0.4\\New in 0.2]{operators/every custom gate}!/yquant/operators/subcircuit/seamless!
1698 This style is by default installed for every user\hyp defined gate.
1699 User\hyp defined gates are implemented via subcircuits; this style suppresses the box that surrounds the subcircuit and by default suppresses all register names.
1700 This allows a seamless integration of the gate/subcircuit into the main circuit, without putting particular emphasis to the fact that what was defined as the custom gate indeed belongs together.
1701 Note that with the \ttlink{compat} key set before \texttt{0.4}, this style instead defaults to \tex!/yquant/operators/subcircuit/frameless, /yquant/register/default name=!.
1702 \end{option}
1703
1704 \begin{option}[Changed in 0.7]{operators/every dmeter}!shape=yquant-dmeter, time radius=2mm, space radius=2mm, draw, /yquant/default fill!
1705 This style is installed for every \ttlink{dmeter} gate.
1706 \linkdef{yquant-dmeter}The \texttt{yquant-dmeter} shape consists of a rectangle whose right side is replaced by a circle, resembling the letter ``D.''
1707 \end{option}
1708
1709 \begin{option}{operators/every h}!/yquant/operators/every rectangular box!
1710 This style is installed for every \ttlink{h} (Hadamard) operator.
1711 \end{option}
1712
1713 \begin{option}[Changed in 0.7]{operators/every inspect}!shape=yquant-output, align/.expanded=\ifyquanthorz{left}{center}, outer timesep=.3333em, space radius=2.47mm, /yquant/default fill, /yquant/operator/if multi={draw, decoration={gapped brace, raise=2pt, \ifyquanthorz{}{mirror}}, decorate}!
1714 This style is installed for every \ttlink{inspect} gate.
1715 It does not have any shape on its own, apart from multi\hyp register uses, in which it will contain a brace on its left (in vertical mode: on its top).
1716 \end{option}
1717
1718 \begin{option}[New in 0.7.2]{operators/every iswap}!shape=yquant-ocross, radius=.75mm, draw!
1719 This style is installed for every \ttlink{iswap} gate that interchanges two qubits and conditionally adds phases.
1720 \linkdef{yquant-iswap}The \texttt{yquant-iswap} shape is a cross enclosed in a circle.
1721 \end{option}
1722
1723 \begin{option}[Changed in 0.7]{operators/every measure}!shape=yquant-measure, x radius=4mm, y radius=2.5mm, draw, /yquant/default fill!
1724 This style is installed for every \ttlink{measure} gate.
1725 \linkdef{yquant-measure}The \texttt{yquant-measure} shape is a rectangle that contains a ``meter'' symbol.
1726 It allows for a text to be put inside (e.g., a basis), which then shifts the meter symbol accordingly.
1727 \end{option}
1728
1729 \begin{option}{operators/every measure meter}!draw, -{Latex[length=2.5pt]}!
1730 This style is applied to the path that resembles the ``meter'' symbol that is drawn by the \ttlink{yquant-measure} shape.
1731 Due to the default style, the \TikZ{} library \pkg{arrows.meta} is automatically loaded with \Yquant.
1732 \end{option}
1733
1734 \begin{option}{operators/every not}!shape=yquant-oplus, radius=1.3mm, draw!
1735 This style is installed for every \ttlink{not} or \ttlink{cnot} gate (which are synonyms, and actually do the same as the Pauli~$\sigma_x$ gate).
1736 \linkdef{yquant-oplus}The \texttt{yquant-oplus} shape resembles the addition\hyp modulo\hyp two symbol $\oplus$.
1737 \end{option}
1738
1739 \begin{option}{operators/every pauli}!/yquant/operators/every rectangular box!
1740 This style is installed for every Pauli operator, i.e., \ttlink{x}, \ttlink{y}, and \ttlink{z}.
1741 \end{option}
1742
1743 \begin{option}{operators/every phase}!shape=yquant-circle, radius=.5mm, fill!
1744 This style is installed for every \ttlink{phase} gate $\ketbra00 + \ee^{\ii\phi} \ketbra11$.
1745 \end{option}
1746
1747 \begin{option}[Changed in 0.7\\New in 0.6]{operators/every rectangular box}!shape=yquant-rectangle, draw, align=center, inner timesep=1mm, time radius=2mm, space radius=2.47mm, /yquant/default fill!
1748 This style is not associated to any particular gate, but will be inherited by a lot of gates that have a rectangular box frame with some text.
1749 This style should not be used with a \ttlink{compat} setting strictly smaller than~\texttt{0.6}.
1750 \end{option}
1751
1752 \begin{option}{operators/every slash}!shape=yquant-slash, x radius=.5mm, y radius=.7mm, draw!
1753 This style is installed for every \ttlink{slash} pseudo\hyp gate, i.e., the one that is used to indicate that a single register line actually denotes multiple registers.
1754 \end{option}
1755
1756 \begin{option}[New in 0.2]{operators/every subcircuit}!!
1757 This style is installed for every \ttlink{subcircuit}.
1758 Note that all styles given here will also apply to every element in the subcircuit; in a way, this is an addition to \ttlink{/yquant/every circuit} (which is also again put into effect at the beginning of a subcircuit).
1759 \end{option}
1760
1761 \begin{option}[New in 0.2]{operators/every subcircuit box}!/yquant/operators/every rectangular box, fill=none!
1762 This style is installed for every \ttlink{subcircuit}.
1763 Note that in contrast to all other styles such as \ttlink{/yquant/operators/every subcircuit} or \ttlink{/yquant/this operator}, this style is only applied to the ``container'' node of the subcircuit, but not to the elements in the subcircuit themselves.
1764 Also note that the box style by default contains an \texttt{inner xsep} that will be added as an inside padding.
1765 This makes sense if your wires have labels so that these labels don't move too closely to the border of the box.
1766 However, if you do not labelled wires but still want to have a box around the subcircuit, you should consider removing the separation---as it will be added to the initial wire padding given by \ttlink{/yquant/operator/separation}.
1767 \end{option}
1768
1769 \begin{option}[New in 0.2]{subcircuit box style}!/yquant/operators/every subcircuit box/.append style={#1}!
1770 This is a shorthand to append styles to the subcircuit box only.
1771 \end{option}
1772
1773 \begin{option}[New in 0.2]{operators/this subcircuit box}!!
1774 This style is appended to the current style installed for the \ttlink{subcircuit}, but will not apply to its contents.
1775 Additionally, this style will be reset to an empty style at the beginning of each subcircuit, so that it really only applies to exactly the subcircuit box it is explicitly specified on, not to nested subcircuit boxes.
1776 \end{option}
1777
1778 \begin{option}[New in 0.2]{this subcircuit box style}!/yquant/operators/this subcircuit box/.append style={#1}!
1779 This is a shorthand to append styles to the current subcircuit box only.
1780 \end{option}
1781
1782 \begin{option}[New in 0.4]{operators/subcircuit/frameless}!/yquant/operators/this subcircuit box/.append style={draw=none, inner sep=0pt}!
1783 This is a shorthand style that removes the frame and additional inner separation for the current subcircuit.
1784 Note that still, the wire padding given by \ttlink{/yquant/operator/separation} is present within the---now invisible---outer box that contains the subcircuit (use \ttlink{/yquant/operators/subcircuit/seamless} to suppress it).
1785 Hence, the most prominent application of this key is if the wires before and after the subcircuit are \ttlink{nobit}s, which provides a clean way to build up circuit equations with perfectly aligned wires (examples can be found in \cref{sec:examples}).
1786 \end{option}
1787
1788 \begin{option}[New in 0.5]{operators/subcircuit/name mangling}!prefix or discard!
1789 This option defines how named nodes within subcircuits are made available to the outer circuit:
1790 \begin{itemize}
1791 \item \linkdef{mangling/prefix or discard}\texttt{prefix or discard} \\
1792 If the subcircuit itself has a name $s$ and the inner gate has a name $g$, the outer circuit can refer to the inner gate via the name \texttt{$s$-$g$}.
1793 Note that $s$ itself may not only consist of the user\hyp specified name, but may instead already be suffixed by \texttt{-0}, \texttt{-1}, \dots, if the subcircuits were assigned to multiple targets.
1794
1795 If the subcircuit itself has no name, works as \ttlink[mangling/discard]{discard}.
1796 \item \linkdef{mangling/prefix or transparent}\texttt{prefix or transparent} \\
1797 If the subcircuit itself has a name, as works as \ttlink[mangling/prefix or discard]{prefix or discard}; else, works as \ttlink[mangling/transparent]{transparent}.
1798 \item \linkdef{mangling/transparent}\texttt{transparent} \\
1799 The inner gates are always available in the outer circuit by their original names.
1800 Note that this may potentially lead to naming conflicts, which are always resolved by the latest name overwriting all previous declarations without notice.
1801 \item \linkdef{mangling/discard}\texttt{discard} \\
1802 The inner gates will not be available in the outer circuit.
1803 \end{itemize}
1804 Note that if a new gate is defined via \texlink\yquantdefinegate---which internally uses subcircuits---the value of this option at the time of declaration is the relevant one, not the one at the time of usage.
1805 This can be influenced via \ttlink{/yquant/operators/subcircuit/name mangling reset}.
1806 \end{option}
1807
1808 \begin{option}[New in 0.5]{operators/subcircuit/name mangling reset}!true!
1809 The current value of \ttlink{/yquant/operators/subcircuit/name mangling} will be reset at the beginning of a subcircuit to the value it had upon declaration of the subcircuit only if this option is \texttt{true} upon \emph{using} the subcircuit (which typically will only make a difference if the subcircuit was defined via \texlink{\yquantdefinegate} at some earlier stage).
1810 \begin{warning}
1811 Setting this value to \tex!false! has the potential of breaking the corresponding subcircuit/custom gate, as it may internally reference gates by names that are no longer the correct ones.
1812 Do not use this property without a very good reason and thorough understanding of what is happening.
1813 \end{warning}
1814 \end{option}
1815
1816 \begin{option}[New in 0.4]{operators/subcircuit/seamless}!/yquant/operators/subcircuit/frameless, /yquant/register/default name=, /yquant/circuit/seamless!
1817 This option carries out multiple actions that are responsible to let the current subcircuit appear in a ``seamless'' state:
1818 \begin{itemize}
1819 \item It calls \ttlink{/yquant/operators/subcircuit/frameless}.
1820 \item It sets \ttlink{/yquant/circuit/seamless} to \texttt{true}.
1821 \item It ensures that \ttlink{/yquant/circuit/seamless} is reset within the subcircuit, so that it does not propagate to nested subcircuits.
1822 \end{itemize}
1823 \end{option}
1824
1825 \begin{option}{operators/every swap}!shape=yquant-swap, radius=.75mm, draw!
1826 This style is installed for every \ttlink{swap} gate that interchanges two qubits.
1827 \linkdef{yquant-swap}The \texttt{yquant-swap} shape consists of a single cross.
1828 \end{option}
1829
1830 \begin{option}[Changed in 0.7\\New in 0.6]{operators/every text}!shape=yquant-rectangle, align=center, inner timesep=1mm, time radius=2mm, space radius=2.47mm, /yquant/default fill!
1831 This style is installed for every \ttlink{text} gate.
1832 \end{option}
1833
1834 \begin{option}[New in 0.1.2]{operators/every wave}!shape=yquant-circle, radius=.5mm, fill!
1835 This style is installed for every \ttlink{correlate} gate.
1836 \end{option}
1837
1838 \begin{option}{operators/every x}!/yquant/operators/every pauli!
1839 This style is installed for every Pauli operator $\sigma_x$, i.e., \ttlink{x}.
1840 \end{option}
1841
1842 \begin{option}[Changed in 0.7]{operators/every xx}!shape=yquant-rectangle, radius=.75mm, draw, /yquant/default fill!
1843 This style is installed for every \ttlink{xx} gate in symmetrized notation ($\ketbra{++}{++} + \ketbra{+-}{+-} + \ketbra{-+}{-+} - \ketbra{--}{--}$).
1844 \end{option}
1845
1846 \begin{option}{operators/every y}!/yquant/operators/every pauli!
1847 This style is installed for every Pauli operator $\sigma_y$, i.e., \ttlink{y}.
1848 \end{option}
1849
1850 \begin{option}{operators/every z}!/yquant/operators/every pauli!
1851 This style is installed for every Pauli operator $\sigma_z$, i.e., \ttlink{z}.
1852 \end{option}
1853
1854 \begin{option}{operators/every zz}!shape=yquant-circle, radius=.5mm, fill!
1855 This style is installed for every \ttlink{zz} gate (aka \textsc{CPhase}) in symmetrized notation ($\ketbra{00}{00} + \ketbra{01}{01} + \ketbra{10}{10} - \ketbra{11}{11}$).
1856 \end{option}
1857
1858 \section{Doing the impossible}\label{sec:tikz}
1859 \Yquant{} will almost certainly never be able to do everything an author has in mind.
1860 Sometimes, there is the need to draw something non\hyp standard, and this cannot be implemented in the \Yquant{} language.
1861 However, since \Yquant{} is a layer on top of \TikZ, it should be very hard to find something (meaningful) that cannot be done by combining the power of both packages.
1862
1863 \subsection{Mixing \texorpdfstring{\Yquant}{yquant} and \texorpdfstring{\TikZ}{TikZ} code}
1864 \linkdef\yquant
1865 Before or after any gate, you may interrupt the \Yquant{} instructions to perform arbitrary \TikZ{} path operations.
1866 After every such operation, \Yquant{} will automatically restart its parser so that you can fluently jump between \Yquant{} and \TikZ{} code.
1867 You can even interject arbitrary \TeX{} code (or, say, low\hyp level \pkg{pgf} commands); however, then, \Yquant{} is not able to restart its parser.
1868 For this reason, after the last command in a block of \TeX{} commands, you must issue \tex!\yquant!, which then re\hyp enables the \Yquant{} language.
1869
1870 \subsection{Accessing gates in \texorpdfstring{\TikZ}{TikZ}}
1871 \linkdef{name}
1872 The feature to perform arbitrary \TikZ{} operations is powerful in itself, but would be of limited use were there no way to access the elements in the quantum circuit.
1873 \Yquant{} provides a global attribute \texttt{name} that can be assigned to every gate.
1874 All quantum operations are in fact \TikZ{} nodes, and the name you give to them then becomes a \TikZ{} name, which you can easily reference to get the coordinates of a particular operator.
1875 Note that the name you specify is only available if a single register is targeted.
1876 The name is suffixed by \tex!-\idx!, where \tex!\idx! refers to the (zero\hyp based) index of the operation ordered from top to bottom (i.e., if an operator acts on two qubits and should be named \texttt{op}, the topmost operator will be available as \texttt{op-0} and the second as \texttt{op-1}).
1877 Multiple slices in a discontiguous multi\hyp register are additionally suffixed by \texttt{-s\textit{<slice index>}}.
1878 All controls are also named, suffixed by \tex!-p\idx! or \tex!-n\idx! for positive and negative controls (i.e., the topmost positive control of the previous operator will be available as \texttt{op-p0}).
1879 Counters for target registers, positive, and negative controls are all independent.
1880 Finally, you can even access names within a subcircuit, provided you give a name to the subcircuit.
1881 All nodes in the subcircuit will then have the name \texttt{\textit{<subcircuit name>}-\textit{<name specified in the subcircuits>}}.
1882 Note that here, \texttt{\textit{<subcircuit name>}} is the \emph{full} name of the subcircuit, which includes the \tex!-\idx! suffix, \compat{New in 0.4.1}unless there is only a single target register.
1883 For nested subcircuits, you will get multiple prefixes.
1884 \compat{New in 0.5}The prefixing behavior can be influenced by \ttlink{/yquant/operators/subcircuit/name mangling}.
1885
1886 \subsection{Shapes and the drawing pipeline}\label{sec:pipeline}
1887 \linkdef\protect
1888 All \Yquant{} shapes have the anchors available you would typically expect from a \TikZ{} shape of the given outline.
1889 The \texttt{center} anchor will be aligned to the wire.
1890 \compat{Changed in 0.1.2}In addition to the normal paths implemented by \TikZ{} shapes, the width and height of those fit for \Yquant{} at least twice as large as given by the \texttt{/tikz/x~radius} and \texttt{/tikz/y~radius}; and they must implement clipping paths, a \Yquant{} addition to \TikZ{} shapes.
1891 Such a path has to provide the ``clipping outline,'' i.e., anything that should not contain register or control lines.
1892 There may be a difference between horizontal and vertical clipping outlines.
1893 To understand clipping paths, \Yquant's drawing pipeline needs to be explained.
1894 \begin{itemize}
1895 \item In a first run---this is what happens directly at the position where you type the gate command---\Yquant{} will ``virtually'' draw the gates in order to determine their dimensions and calculate register heights.
1896 The actual drawing commands are written to a macro (this is the cause that some macros must be preceded by \tex!\protect! if used in a gate \texttt{value}---in fact, if multiple registers are targeted in one gate, the style and values required for this gate are only stored once, so that for example \texlink{\idx} is a \tex!\protected! macro until the very end).
1897 \item \compat{New in 0.4}Deferred gates (measurements that may replace future control knobs) are stored temporarily and queried when the next gate is executed or at the end of the circuit.
1898 The corresponding commands---either re\hyp inserting if they must appear at their original position or substituting the controls---are inserted appropriately.
1899 \item When \tex!\end{yquant}! is encountered, the vertical positions are determined and the actual drawing commands are executed.
1900 \item Unless the operation changes the wire type or style, do the following (first two items for every register at which an operator node has to be created).
1901 \begin{itemize}
1902 \item Create the operator node at the appropriate position.
1903 \item Call \tex!\pgfshapeclippath! on the newly created node.
1904 This will first determine whether the node was stroked; if not, \tex!\pgflinewidth! is set to zero.
1905 Then, it will call the horizontal clipping path, which is supposed to create some soft path commands.
1906 Those soft path commands are collected in a macro on a per\hyp register basis and the soft path is cleared.
1907 The same happens for the vertical clipping path, which is collected in a macro on a per\hyp operation basis.
1908 \item If control lines or multi lines are to be drawn, the vertical clipping path commands are now executed and installed as an inverted\footnote{Inverting the clipping means that instead of drawing only \emph{within} the clipping path (which corresponds to the gates), we only draw \emph{outside}. However, as there is no direct support for this, we invert by exploiting the even\hyp odd rule. If you specify a register multiple times, whether as target, control, or mixed, funny effects can be expected, as the clipping region is inverted multiple times. Note that using a register more than once is always an error, but \Yquant{} does not check for it due to the high overhead.} clipping.
1909 \item Control lines and multi lines are drawn (in this order) from one to the next \texttt{center} anchor.
1910 Due to the clipping commands, this will create a perfect connection with the shape of the gate, but even transparent gates are possible without the lines being visible.
1911 \end{itemize}
1912 \item If the operation changes the wire type or style, or if there is no operation left on this register, the following is done.
1913 \begin{itemize}
1914 \item Load the clipping paths accumulated for all the gates acting on this register and install the inverted clipping.
1915 \item Draw the wire as one continuous line from where the last wire ended (or the beginning of the circuit) to the center of the last gate, or to the common end position for all wires of the circuit.
1916 \item Remove the clipping paths stored so far on this register, apart from the clipping on the last gate (which will be needed again if this was not the end of the circuit).
1917 \end{itemize}
1918 \end{itemize}
1919
1920 \compat{New in 0.7}Note that \Yquant{} also supports a simplified drawing pipeline which does not involve clipping paths.
1921 It can be enabled by setting the \ttlink{/yquant/drawing mode} option to \texttt{size}.
1922 The simplified pipeline has the following benefits:
1923 \begin{itemize}
1924 \item \TeX{} has to compute a bit less, so the compilation process can be sped up.
1925 \item PDF readers may render the circuit faster.
1926 \item The size of the output files is decreased.
1927 \item Compatibility with very simplistic clients that don't support clipping well is improved (this may be an issue for some PDF to image converters, for example).
1928 \item Theoretically, you may use any kind of predefined shape in this mode (from \TikZ{} or other packages), since the clipping paths are no longer required.
1929 \end{itemize}
1930 However, with it come certain drawbacks:
1931 \begin{itemize}
1932 \item Gates are no longer transparent, but filled with some fixed background color, so if transparency is important for you, this just does not work.
1933 \item \Yquant{} relies on \TikZ's layering capabilities; if you use layers by yourself, you have to pay some attention to do it correctly.
1934 \item \linkdef{layers}The z\hyp order may not be as you expect it; in particular:
1935 All the gates will be drawn on the \texttt{main} layer; all the wires on the \texttt{wires} layer, which is behind \texttt{main}; so if you don't use layers by yourself, everything that you draw will overshadow the wires, but not necessarily the gates.
1936 \item If you were to fill a subcircuit, this would then erase all the internal wires.
1937 Hence, the box of a subcircuit is drawn on the \texttt{behindwires} layer; but this implies that also the \emph{frame} of the box is drawn behind the wires and that this box is also overshadowed by any of your drawing.
1938 \item Wires that visually cross gates which are not part of the gate may be displayed differently from the standard pipeline.
1939 \end{itemize}
1940 Unless you use \tex!\pgfsetlayers! to add the layers \texttt{wires} and \texttt{behindwires} manually, \Yquant{} will automatically place the \texttt{wires} layer directly before \texttt{main}; then it will place \texttt{behindwires} directly before \texttt{wires}.
1941 This in particular ensures full compatibility with the \texttt{backgrounds} library: the background layer is still the very first in the layer list.
1942 In contrast to \pkg{pgfplots}, this automatism should work even if the \Yquant{} environment is placed within groups.
1943
1944 \subsection{Overwriting the height and depth calculation}
1945 \Yquant{} automatically takes care of calculating the height and depth of all registers, so that their final vertical positions are chosen without overlap.
1946 This is almost always advisable, but it has some weaknesses:
1947 \begin{itemize}
1948 \item If you specify a multi\hyp register gate, say, extending for three registers and this requires a certain height and depth, where should this be accounted for?
1949 \compat{New in 0.4}\Yquant{} is able to handle these situations by first determining all heights and depths that can safely be attributed to individual registers.
1950 After that, it checks for all multi\hyp register gates: Is the space from the top of the first to the bottom of the last register enough to hold the multi\hyp qubit gate?
1951 If not, it evenly distributes the additional required space to all registers that are visually within the range of this multi\hyp register.
1952
1953 This will fail to produce good results (hopefully) only in two cases:
1954 \begin{itemize}
1955 \item If you place \texttt{label}s on the gate, those are outside of the gate---and typically, either below or above.
1956 Hence, the additional extent stemming from them should \emph{not} be equally distributed among all registers, but either to the height of the first or the depth of the last one.
1957 Currently, \Yquant{} is unable to detect this (and, considering the fact that you can place \texttt{label}s at any angle, this is not an easy problem to solve except for special cases).
1958 \item If you make use of a discontiguous \ttlink{init} gate with a large vertical extent, \Yquant{} will correctly allocate space as if the gate's content were placed in the vertical center.
1959 However, if there is no way to put the arch of the brace at the middle, as the register at this position is excluded from the gate, the content will be shifted---but only \emph{after} calculating the extent.
1960 Hence, the automatically calculated vertical positions will be unsuitable.
1961 \end{itemize}
1962 \item Sometimes, there is more space available than \Yquant{} thinks because you already discarded some wire.
1963 \Yquant{} does not keep track of whether the wires below or above a gate are actually visible at this position---which is not even be known at the time the gate command is issued, as horizontal positions are determined only in the drawing stage.
1964 Hence, you may choose to draw ``within'' the other, invisible wire.
1965 \end{itemize}
1966
1967 \compat{New in 0.4}In these certain special cases, you may want to turn off the automatic calculation for one particular gate.
1968 Note that you may then, depending on the situation, obtain results with overlapping gates.
1969 You can use the keys \ttlink{/yquant/register/minimum height} and \ttlink{/yquant/register/minimum depth} when declaring the relevant register to manually specify a larger desired value, but you have to experiment with regard to what this value is.
1970
1971 \linkdef{overlay}
1972 The global attribute \texttt{overlay} (conveniently overshadowing \TikZ's \texttt{overlay} key, which should not be used for gates) can take the values
1973 \begin{itemize}
1974 \item \texttt{true} (default if no value given, combines \texttt{multi}, \texttt{height}, and \texttt{depth}),
1975 \item \texttt{multi} (short \texttt{m}),
1976 \item \texttt{height} (short \texttt{ht}, \texttt{h}), equivalent to \texttt{left} (short \texttt{l}) and \texttt{before} (short \texttt{bef}, \texttt{b}), which sound more meaningful for vertical and generic circuit orientations,
1977 \item \texttt{depth} (short \texttt{dp}, \texttt{d}), equivalent to \texttt{right} (short \texttt{r}) and \texttt{after} (short \texttt{aft}, \texttt{a}), which sound more meaningful for vertical and generic circuit orientations,
1978 \item \texttt{single} (short \texttt{s}, combines \texttt{height} and \texttt{depth}), and
1979 \item \texttt{false} (useless, default if attribute not given).
1980 \end{itemize}
1981 It disables the calculation of the selected vertical extent for this particular gate.
1982 (In fact, \texttt{multi}, \texttt{height}, \texttt{depth}, and \texttt{single} are subkeys that accept boolean values.)
1983
1984 \section{Reference: Gates and operations}\label{sec:gates}
1985 This section lists all operations \Yquant{} currently understands.
1986 It also details all arguments that can be given to customize the operation, apart from \ttlink{name} and \compat{New in 0.4}\ttlink{overlay}, which are always available.
1987 Note that the \texlink[value]{[value=<value>]} attribute can (and should) alternatively be given as a braced expression that follows the name of the register.
1988 Within \tex!<value>!, unless specified differently, the macro \texlink{\idx} is always available and corresponds to the index of the current register in the list of targets.
1989
1990 \subsection[\texorpdfstring{\yquant{addstyle}}{addstyle}]{\compat{New in 0.1.2}\yquant!addstyle!}\linkdef{addstyle}
1991 Syntax: \yquant!setstyle <target>;! \\
1992 This is an invisible pseudo\hyp gate that immediately changes the \TikZ{} style with which the register lines of all target registers are drawn.
1993 It adds to the styles that are already installed.
1994 Use \ttlink{setstyle} to replace styles.
1995 It may not span multiple registers and does not allow for controls.
1996
1997 \emph{Possible attributes:}
1998 \begin{itemize}
1999 \item \yquant![value=<styles>]! (required) \\
2000 Denotes the new styles; this should be a string that could be passed to \tex!\tikzset!.
2001 \end{itemize}
2002
2003 \subsection{\texorpdfstring{\yquant{align}}{align}}\linkdef{align}
2004 Syntax: \yquant!align <target>;! \\
2005 This is an invisible pseudo\hyp gate that enforces all affected registers to share a common horizontal position for their next gate, which is determined by the largest position of all gates involved.
2006 It may not span multiple registers and does not allow for controls.
2007 \compat{Changed in 0.4}The gate now always aligns the wires, i.e., if they are discarded directly after this gate, they will still discarded all at the same position.
2008
2009 \emph{Possible attributes:} none
2010
2011 \subsection{\texorpdfstring{\yquant{barrier}}{barrier}}\linkdef{barrier}
2012 Syntax: \yquant!barrier <target>;! \\
2013 This is a pseudo\hyp gate that denotes some physical barrier that ensures execution with a specific timing; it is basically a visible version of the \yquantlink{align} gate, denoted by a vertical line.
2014 It may span multiple registers, but does not allow for controls.
2015 The style \ttlink{/yquant/operators/every barrier} is installed.
2016
2017 \emph{Possible attributes:} none
2018
2019 \subsection{\texorpdfstring{\yquant{box}}{box}}\linkdef{box}
2020 Syntax: \yquant!box <target> | <pcontrol> ~ <ncontrol>;! \\
2021 This is a generic register of a rectangular shape that can be filled with arbitrary content.
2022 It may span multiple registers and allows for controls.
2023 The style \ttlink{/yquant/operators/every box} is installed.
2024
2025 \emph{Possible attributes:}
2026 \begin{itemize}
2027 \item \yquant![value=<value>]! \\
2028 Denotes the content of the box.
2029 \end{itemize}
2030
2031 \subsection{\texorpdfstring{\yquant{cbit}}{cbit}}\linkdef{cbit}
2032 Syntax: \yquant!cbit <name>[<len>];! \\
2033 Declares a register of type \texttt{cbit}.
2034
2035 \emph{see \yquantlink{qubit}}
2036
2037 \subsection[\texorpdfstring{\yquant{correlate}}{correlate}]{\compat{New in 0.1.2}\yquant!correlate!}\linkdef{correlate}
2038 Syntax: \yquant!correlate <target>;! \\
2039 This is a pseudo\hyp gate that indicates a correlation (usually a Bell\hyp state) present between the multi\hyp registers involved.
2040 This gate should span multiple registers and does not allow for controls.
2041 The style \ttlink{/yquant/operators/every wave} is installed.
2042
2043 \emph{Possible attributes:} none
2044
2045 \subsection{\texorpdfstring{\yquant{cnot}}{cnot}}\linkdef{cnot}
2046 Syntax: \yquant!cnot <target> | <pcontrol> ~ <ncontrol>;! \\
2047 This is a synonym for the \ttlink{not} gate.
2048 Note that despite its name, controls are not mandatory and also here, the style \ttlink{/yquant/operators/every not} is installed.
2049
2050 \subsection[\texorpdfstring{\yquant{discard}}{discard}]{\compat{Changed in 0.8}\yquant!discard!}\linkdef{discard}
2051 Syntax: \yquant!discard <target> | <pcontrol> ~ <ncontrol>;! \\
2052 This is an invisible pseudo\hyp gate that changes the type of all target registers to \ttlink{nobit}, i.e., no line will be drawn for them.
2053 This has effect already for the outgoing line of the last gate on the target registers.
2054 The gate may not span multiple registers.
2055 To change a register type on\hyp the\hyp fly into something different from \ttlink{nobit}, use the \compat{Changed in 0.1.2}\ttlink{settype} pseudo\hyp gate.
2056
2057 \compat{New in 0.8}Since version~0.8, the gate allows for controls, which will draw as the ``corner'' gate:
2058 \begin{center}
2059 \begin{yquantgroup}
2060 \registers{
2061 qubit {} a[2];
2062 qubit {} b;
2063 }
2064 \circuit{
2065 discard b | a;
2066 }
2067 \equals
2068 \circuit{
2069 h b;
2070 measure b;
2071 z a[1] | a[0], b;
2072 discard b;
2073 }
2074 \end{yquantgroup}
2075 \end{center}
2076
2077 \emph{Possible attributes:} none
2078
2079 \subsection{\texorpdfstring{\yquant{dmeter}}{dmeter}}\linkdef{dmeter}
2080 Syntax: \yquant!dmeter <target>;! \\
2081 This is a measurement gate, denoted by a ``D'' shape.
2082 It changes the type of all targets involved.
2083 It may span multiple registers, but does not allow for controls.
2084 The style \ttlink{/yquant/operators/every dmeter} is installed.
2085
2086 \emph{Possible attributes:}
2087 \begin{itemize}
2088 \item \yquant![value=<value>]! \\
2089 Allows to specify a text that will be included inside the gate, possible enlarging its width.
2090 For outside texts, use \TikZ{} \texttt{label}s instead.
2091 \item \yquant![type=<qubit|cbit|qubits>]! \\
2092 Allows to specify the type into which the affected targets are converted.
2093 Default is \ttlink{cbit}.
2094 \end{itemize}
2095
2096 \subsection{\texorpdfstring{\yquant{h}}{h}}\linkdef{h}
2097 Syntax: \yquant!h <target> | <pcontrol> ~ <ncontrol>;! \\
2098 This is a Hadamard gate, $\bigl( \ketbra00 + \ketbra01 + \ketbra10 - \ketbra11 \bigr) \fracslash \sqrt2$, denoted by a rectangle that contains the letter $H$.
2099 It may not span multiple registers, but allows for controls. \\
2100 The style \ttlink{/yquant/operators/every h} is installed.
2101
2102 \emph{Possible attributes:} none
2103
2104 \subsection{\texorpdfstring{\yquant{hspace}}{hspace}}\linkdef{hspace}
2105 Syntax: \yquant!hspace <target>;! \\
2106 This is an invisible pseudo\hyp gate that inserts a certain amount of white space into all target registers.
2107 It may not span multiple registers and does not allow for controls.
2108 \compat{Changed in 0.4}The gate now always has an effect, e.g., if the wire is discarded after this gate, it will still be extended by the given amount first.
2109
2110 \emph{Possible attributes}:
2111 \begin{itemize}
2112 \item \yquant![value=<dim>]! (required) \\
2113 Gives the amount of white space that is to be inserted. Must be a valid (nonnegative) \TeX{} dimension.
2114 \end{itemize}
2115
2116 \subsection[\texorpdfstring{\yquant{init}}{init}]{\compat{Changed in 0.8, 0.4}\yquant!init!}\linkdef{init}
2117 Syntax: \yquant!init <target> | <pcontrol> ~ <ncontrol>;! \\
2118 This is a pseudo\hyp gate that (re)initializes a registers to a given state.
2119 It may span multiple registers, or it may allow for controls (though the exclusiveness is not checked).
2120 The style \ttlink{/yquant/every label} is installed.
2121 Note that this pseudo\hyp gate, unlike all others, behaves differently if it is the first operation acting on a register and does not have controls: in this case, it does not increment the horizontal position, but uses the space available to the left; and the style \ttlink{/yquant/every initial label} is installed additionally.
2122 Internally, creating a new register with some printed name is translated into the creation of an unnamed register, followed by application of this gate with the desired text.
2123
2124 \compat{New in 0.8}Since version~0.8, the gate allows for controls, which will draw as the ``corner'' gate:
2125 \begin{center}
2126 \begin{yquantgroup}
2127 \registers{
2128 qubit {} a[2];
2129 nobit b;
2130 }
2131 \circuit{
2132 init b | a;
2133 }
2134 \equals
2135 \circuit{
2136 init {$\ket0$} b;
2137 cnot b | a;
2138 }
2139 \end{yquantgroup}
2140 \end{center}
2141
2142 \emph{Possible attributes:}
2143 \begin{itemize}
2144 \item \yquant![type=<qubit|cbit|qubits>]! \\
2145 Allows to specify the type into which the affected target registers are converted.
2146 Default is the type of the first target register that is different from \ttlink{nobit}, or \ttlink{qubit} if they all are \ttlink{nobit}.
2147 The style \texttt{/yquant/every <type> label} is installed additionally.
2148 \item \compat{Changed in 0.8}\yquant![value=<value>]! \\
2149 Denotes the label that is printed to the left of the wire.
2150 Since version~0.8, the value is optional; if it is not given, it is assumed to be empty.
2151 \end{itemize}
2152
2153 \subsection[\texorpdfstring{\yquant{inspect}}{inspect}]{\compat{New in 0.4}\yquant!inspect!}\linkdef{inspect}
2154 Syntax: \yquant!inspect <target>;! \\
2155 This is a pseudo\hyp gate that allows to print the current state of one or multiple registers within a circuit.
2156 It may span multiple registers, but does not allow for controls.
2157 The style \ttlink{/yquant/operators/every inspect} is installed.
2158 Essentially, it is the same as an \ttlink{output} gate that will be drawn immediately at the current position and not deferred until the end; hence, it also draws braces when used in a multi\hyp register context.
2159 \compat{New in 0.6}If this is not desired, use the \ttlink{text} gate instead.
2160
2161 \emph{Possible attributes:}
2162 \begin{itemize}
2163 \item \yquant![value=<value>]! (required) \\
2164 Denotes the text that is to be printed.
2165 \end{itemize}
2166
2167 \subsection[\texorpdfstring{\yquant{iswap}}{iswap}]{\compat{New in 0.7.2}\yquant!iswap!}\linkdef{iswap}
2168 Syntax: \yquant!iswap <targets> | <pcontrol> ~ <ncontrol>;! \\
2169 This is the two\hyp qubit i\textsc{swap} gate $\ketbra{00}{00} + \ii\ketbra{01}{10} + \ii\ketbra{10}{01} + \ketbra{11}{11}$ that exchanges two qubits and conditionally adds phases.
2170 It is denoted by crosses within circles at the affected registers which are connected by a control line.
2171 It may span multiple registers (in fact, it should always span exactly two registers, though \Yquant{} does not enforce this), and it allows for controls.
2172 However, refrain from combining \emph{multiple} two\hyp qubit targets \emph{together} with controls.
2173 The control line will extend from the first to the last of all registers involved in the operation, so that it is impossible to discern visually which registers should actually be swapped.
2174 Using multiple swaps without controls in one operation is fine, as well as a single controlled swap.
2175 The style \ttlink{/yquant/operators/every iswap} is installed.
2176
2177 \emph{Possible attributes:} none
2178
2179 \subsection[\texorpdfstring{\yquant{measure}}{measure}]{\compat{Changed in 0.4}\yquant!measure!}\linkdef{measure}
2180 Syntax: \yquant!measure <target>;! \\
2181 This is a measurement gate, denoted by a rectangle with a meter symbol.
2182 It changes the type of all targets involved.
2183 It may span multiple registers, but does not allow for controls.
2184 The style \ttlink{/yquant/operators/every measure} is installed.
2185 It may be deferred to be used instead of a control knob for a later gate using the \ttlink{/yquant/every post measurement control} style or the suitable attributes; see the documentation for this style.
2186
2187 \emph{Possible attributes:}
2188 \begin{itemize}
2189 \item \yquant![type=<qubit|cbit|qubits>]! \\
2190 Allows to specify the type into which the affected targets are converted.
2191 Default is \ttlink{cbit}.
2192 \item \yquant![value=<value>]! \\
2193 Allows to specify a text that will be included at the bottom of the rectangle (which will shift the meter symbol upwards accordingly).
2194 For outside texts, use \TikZ{} \texttt{label}s instead.
2195 \item \linkdef{[direct control]}\yquant![direct control]! \\
2196 Temporarily sets \texlink[/yquant/every post measurement control]{/yquant/every post measurement control=direct} for this particular gate.
2197 \item \linkdef{[indirect control]}\yquant![indirect control]! \\
2198 Temporarily sets \texlink[/yquant/every post measurement control]{/yquant/every post measurement control=indirect} for this particular gate.
2199 \end{itemize}
2200
2201 \subsection{\texorpdfstring{\yquant{nobit}}{nobit}}\linkdef{nobit}
2202 Syntax: \yquant!nobit <name>[<len>];! \\
2203 Declares a register of type \ttlink{nobit}.
2204 The \texttt{<name>} must be a self\hyp chosen name for the register which was not previously used as a register name in this circuit (but names can be re\hyp used in subcircuits).
2205 Names are case\hyp insensitive.
2206 The register can be made into a vector register by specifying \texttt{<len>} (default \tex!1!).
2207
2208 \emph{Possible attributes:}
2209 \begin{itemize}
2210 \item \compat{New in 0.2}\yquant![out]! or \yquant![ancilla]! (required in subcircuits) \\
2211 \emph{see \ttlink{qubit}}
2212 \end{itemize}
2213
2214 \subsection{\texorpdfstring{\yquant{not}}{not}}\linkdef{not}
2215 Syntax: \yquant!not <target> | <pcontrol> ~ <ncontrol>;! \\
2216 This is a \textsc{not} gate, $\ketbra01 + \ketbra10$, denoted by the $\oplus$ symbol.
2217 It may not span multiple registers, but allows for controls.
2218 Due to its common usage, the synonymous gate \yquantlink{cnot} is provided.
2219 The style \ttlink{/yquant/operators/every not} is installed.
2220
2221 \emph{Possible attributes:} none
2222
2223 \subsection[\texorpdfstring{\yquant{output}}{output}]{\compat{Changed in 0.4}\yquant!output!}\linkdef{output}
2224 Syntax: \yquant!output <target>;! \\
2225 This is a pseudo\hyp gate that allows to write some text at the very end of the register line.
2226 It may only be specified once per register.
2227 It may span multiple registers, but does not allow for controls.
2228 The style \ttlink{/yquant/every output} is installed, and also the style \texttt{/yquant/every <type> output}, where \texttt{<type>} is the type of the affected register (at the time of printout).
2229 \compat{New in 0.6}Note that while outputs can be named, the name will only become available after the \texlink{yquant} environment itself was ended (but of course before the outer \texttt{tikzpicture} was closed).
2230
2231 \emph{Possible attributes:}
2232 \begin{itemize}
2233 \item \yquant![value=<value>]! (required) \\
2234 Denotes the text that is to be printed.
2235 \end{itemize}
2236
2237 \subsection{\texorpdfstring{\yquant{phase}}{phase}}\linkdef{phase}
2238 Syntax: \yquant!phase <name> | <pcontrol> ~ <ncontrol>;! \\
2239 This is a phase gate, $\ketbra00 + \ee^{\ii\phi} \ketbra11$, denoted by a filled circle.
2240 It may not span multiple registers, but allows for controls.
2241 The style \ttlink{/yquant/operators/every phase} is installed.
2242
2243 \emph{Possible attributes:}
2244 \begin{itemize}
2245 \item \yquant![value=<value>]! (required) \\
2246 Denotes the angle $\phi$ that is to be printed together with the gate.
2247 Position and appearance can be influenced by setting the position of \TikZ{} labels, as this is internally used.
2248 Note that at the moment, it is not possible to change any label options on a gate\hyp type basis, only locally or fully globally (\TikZ{} feature request \href{https://github.com/pgf-tikz/pgf/issues/811}{\#811}).
2249 \end{itemize}
2250
2251 \subsection{\texorpdfstring{\yquant{qubit}}{qubit}}\linkdef{qubit}
2252 Syntax: \yquant!qubit <name>[<len>];! \\
2253 Declares a register of type \texttt{qubit}.
2254 The \texttt{<name>} must be a self\hyp chosen name for the register which was not previously used as a register name in this circuit (but names can be re\hyp used in subcircuits).
2255 Names are case\hyp insensitive.
2256 The register can be made into a vector register by specifying \texttt{<len>} (default \tex!1!).
2257
2258 \emph{Possible attributes:}
2259 \begin{itemize}
2260 \item \linkdef{[after]}\yquant![after=<regname>]! \\
2261 If given, the register will start not at the left of the circuit but instead at the position at which the last gate in the register \texttt{<regname>} ended.
2262
2263 This attribute may not be given in combination with \yquant![in]! or \yquant![inout]!.
2264 \item \linkdef{[in]}\linkdef{[out]}\linkdef{[inout]}\linkdef{[ancilla]}\compat{New in 0.2}\yquant![in]!, \yquant![out]!, \yquant![inout]!, or \yquant![ancilla]! \\
2265 Default: \yquant![ancilla]! for top\hyp level circuits (do not change there); \yquant![inout]! for subcircuits.
2266
2267 Determines how a subcircuit interacts with its parent circuit.
2268
2269 Registers declared with the \yquant![ancilla]! attribute are available only to the subcircuit; they cannot be connected to an outside wire.
2270
2271 Registers declared with the \yquant![in]! or \yquant![inout]! attribute will expect an outer wire of the same type to be present and will then be identical with this outer wire.
2272 Any changes applied to the wire within the subcircuit automatically also happen on the associated outer wire.
2273 If the attribute is \yquant![in]!, the wire will automatically be discarded at the end of the subcircuit (and hence also in the outer circuit, where it may be re\hyp initialized).
2274 This is different from applying the \ttlink{discard} gate in that the wire will still extend until the end of the subcircuit and may thus receive proper \ttlink{output}s.
2275
2276 Registers declared with the \yquant![out]! attribute will expect a discarded outer wire to be present, which will be initialized to a \texttt{qubit} at the beginning of the subcircuit, and from then on be identical with the outer wire.
2277 \item \yquant![value=<value>]! \\
2278 Denotes the label that is printed to the left of the wire.
2279 If the value is omitted, the default is used (\ttlink{/yquant/register/default name}, preinitialized to \texlink\regidx).
2280
2281 Inside the value, \texlink{\reg} expands to \texttt{<name>}, \texlink{\len} expands to \texttt{<len>}, \texlink{\idx} expands to the current index within the vector register (for new registers, $0 \leq \text{\tex!\idx!} < \text{\texttt{<len>}}$), and \texlink{\regidx} expands to \texlink{\reg} if \texttt{<len>} is one, or to \tex!\reg[\idx]! else.
2282 For details about these macros and changes in version~0.8, see their documentation.
2283 \item \yquant![enclose/set from=<enclose name>]! \\
2284 Tells \Yquant{} that the named enclosing \texttt{<name>} should at least start from this register (if multiple registers are created, from the first one), if not at a preceding one. \\
2285 See the documentation for \ttlink{/yquant/enclose/from}.
2286 \item \yquant![enclose/set to=<enclose name>]! \\
2287 Tells \Yquant{} that the named enclosing \texttt{<name>} should at least end at this register (if multiple registers are created to the last one), if not at a succeeding one. \\
2288 See the documentation for \ttlink{/yquant/enclose/to}.
2289 \end{itemize}
2290
2291 \subsection{\texorpdfstring{\yquant{qubits}}{qubits}}\linkdef{qubits}
2292 Syntax: \yquant!qubits <name>[<len>];! \\
2293 Declares a register of type \texttt{qubits}.
2294
2295 \emph{see \yquantlink{qubit}}
2296
2297 \subsection[\texorpdfstring{\yquant{setstyle}}{setstyle}]{\compat{New in 0.1.2}\yquant!setstyle!}\linkdef{setstyle}
2298 Syntax: \yquant!setstyle <target>;! \\
2299 This is an invisible pseudo\hyp gate that immediately changes the \TikZ{} style with which the register lines of all target registers are drawn.
2300 It replaces all previous styles.
2301 Use \ttlink{addstyle} to accumulate styles.
2302 It may not span multiple registers and does not allow for controls.
2303
2304 \emph{Possible attributes:}
2305 \begin{itemize}
2306 \item \yquant![value=<styles>]! (required) \\
2307 Denotes the new styles; this should be a string that could be passed to \tex!\tikzset!.
2308 \end{itemize}
2309
2310 \subsection[\texorpdfstring{\yquant{settype}}{settype}]{\compat{New in 0.1.2}\yquant!settype!}\linkdef{settype}
2311 Syntax: \yquant!settype <target>;! \\
2312 This is an invisible pseudo\hyp gate that immediately changes the type of the targets registers, taking effect with the output line extending from the last drawn gate.
2313 It may not span multiple registers and does not allow for controls.
2314
2315 \emph{Possible attributes:}
2316 \begin{itemize}
2317 \item \yquant![value=<qubit|cbit|qubits>]! (required) \\
2318 Denotes the new type that is assigned to all registers.
2319 To change the type to \ttlink{nobit}, use the \yquantlink{discard} pseudo\hyp gate instead.
2320 \end{itemize}
2321
2322 \subsection[\texorpdfstring{\yquant{setwire}}{setwire}]{\compat{Removed in 0.4\\Deprecated in 0.1.2}\yquant!setwire!}\linkdef{setwire}
2323 \emph{Use \ttlink{settype} instead.}
2324
2325 This gate is only available if a compatibility version before \texttt{0.4} is chosen.
2326
2327 \subsection[\texorpdfstring{\yquant{slash}}{slash}]{\compat{Changed in 0.4}\yquant!slash!}\linkdef{slash}
2328 Syntax: \yquant!slash <target>;! \\
2329 This is a pseudo\hyp gate used to denote that a single line actually represents multiple registers.
2330 It is drawn as a short slash through the line of the register.
2331 The style \ttlink{/yquant/operators/every slash} is installed.
2332 Note that this gate will ignore the \ttlink{/yquant/operator/minimum width} key.
2333 With a \ttlink{compat} key of~0.3 or earlier, the gate was special in that it did not advance the horizontal position on the wire, which allows to use it on only some of the wires without leading to a ragged start of subsequent gates.
2334 However, as \Yquant's default separation is not large enough to give a pleasant layout when the slash is squeezed in the initial separation, this was dropped as of version~0.4.
2335 Use the \ttlink{align} gate after all \ttlink{slash}es to get a better layout.
2336
2337 \emph{Possible attributes:} none
2338
2339 \subsection[\texorpdfstring{\yquant{subcircuit}}{subcircuit}]{\compat{New in 0.2}\yquant!subcircuit!}\linkdef{subcircuit}
2340 Syntax: \yquant!subcircuit <target>;! \\
2341 This is a subcircuit gate which inserts independent quantum circuits at the current position within the circuit.
2342 It may span multiple registers, but is never split into contiguous slices.
2343 It allows for controls and may change the type of any target involved, depending on the particular subcircuit.
2344 The style \ttlink{/yquant/operators/every subcircuit} is installed.
2345
2346 \emph{Possible attributes:}
2347 \begin{itemize}
2348 \item \compat{New in 0.4}\yquant!frameless! \\
2349 This \ttlink{/yquant/operators/subcircuit/frameless} style is activated with this shorthand.
2350 \item \compat{New in 0.5}\yquant!name mangling! \\
2351 This shorthand will pass the value of the attribute directly to the configuration key \ttlink{/yquant/operators/subcircuit/name mangling}.
2352 \item \compat{New in 0.4}\yquant!seamless! \\
2353 The \ttlink{/yquant/operators/subcircuit/seamless} style is activated with this shorthand (implies \yquant!frameless!).
2354 \item \yquant!value=<subcircuit>! (required) \\
2355 Denotes the content of the subcircuit.
2356 It is specified in the usual syntax of \Yquant.
2357 Note that, regardless of the outer environment, a subcircuit always implicitly uses the unstarred form, i.e., you must declare every register explicitly before its first usage.
2358 This is to make sure that the interface of the circuit, i.e., which registers are taken as input and/or output parameters and in which order, is not accidentally mistaken.
2359
2360 The mapping between input and output registers is trivial for single\hyp qubit uses.
2361 For multi\hyp qubit uses, it works in the following way---in short, it matches in visual order.
2362 You declare input and output registers by using the appropriate attributes on the \ttlink{qubit}, \ttlink{cbit}, \ttlink{qubits} (or even \ttlink{nobit}) gates.
2363 The list of all non\hyp ancillas, from the topmost to the bottom\hyp most, forms the list of parameter registers of the subcircuit.
2364 This is exactly the number of registers that must be supplied within one multi\hyp qubit target.
2365 Also within the multi\hyp qubit target, we sort all registers from the topmost to the bottom\hyp most (in the order as they visually appear, not the order in which they are entered).
2366 Those two lists of equal length are then mapped $1:1$ to each other.
2367 Intermixing with ancillas is possible at every position and will lead to a vertical shift of the wires, until all registers, inner and outer, can be displayed flawlessly.
2368
2369 As subcircuits follow the same rules as ordinary circuits, it is possible to mix them with arbitrary \TeX{} code, and also to access named gates within the subcircuit---but note that named gates in the outer circuit cannot be accessed (at least unless you play with the \texttt{name prefix} key in \TikZ).
2370 In order to access inner nodes from the outer circuit, the subcircuit itself must be named; the inner nodes are then prefixed by the name of the subcircuit and a dash.
2371
2372 It is possible to nest subcircuits arbitrarily.
2373 \end{itemize}
2374
2375 \subsection{\texorpdfstring{\yquant{swap}}{swap}}\linkdef{swap}
2376 Syntax: \yquant!swap <targets> | <pcontrol> ~ <ncontrol>;! \\
2377 This is the two\hyp qubit \textsc{swap} gate $\ketbra{00}{00} + \ketbra{01}{10} + \ketbra{10}{01} + \ketbra{11}{11}$ that exchanges two qubits.
2378 It is denoted by crosses at the affected registers which are connected by a control line.
2379 It may span multiple registers (in fact, it should always span exactly two registers, though \Yquant{} does not enforce this), and it allows for controls.
2380 However, refrain from combining \emph{multiple} two\hyp qubit targets \emph{together} with controls.
2381 The control line will extend from the first to the last of all registers involved in the operation, so that it is impossible to discern visually which registers should actually be swapped.
2382 Using multiple swaps without controls in one operation is fine, as well as a single controlled swap.
2383 The style \ttlink{/yquant/operators/every swap} is installed.
2384
2385 \emph{Possible attributes:} none
2386
2387 \subsection[\texorpdfstring{\yquant{text}}{text}]{\compat{New in 0.6}\yquant!text!}\linkdef{text}
2388 Syntax: \yquant!text <targets> | <pcontrol> ~ <ncontrol>;! \\
2389 This is a pseudo\hyp gate that allows to write some text within the circuit.
2390 It may span multiple registers and allows for controls (though the situations in which controls make sense are pretty scarce).
2391 The style \ttlink{/yquant/operators/every text} is installed.
2392 Contrary to the \ttlink{inspect} gate, this gate will not draw curly braces in multi\hyp register use.
2393 It basically corresponds to a \ttlink{box} gate with suppressed drawing.
2394
2395 \emph{Possible attributes:}
2396 \begin{itemize}
2397 \item \yquant![value=<value>]! (required) \\
2398 Denotes the text that is to be printed.
2399 \end{itemize}
2400
2401 \subsection{\texorpdfstring{\yquant{x}}{x}}\linkdef{x}
2402 Syntax: \yquant!x <target> | <pcontrol> ~ <ncontrol>;! \\
2403 This is a Pauli $\sigma_{\symup x}$ gate $\ketbra01 + \ketbra10$, denoted by a rectangle that contains the letter $X$.
2404 It may not span multiple registers, but allows for controls. \\
2405 The style \ttlink{/yquant/operators/every x} is installed.
2406
2407 \emph{Possible attributes:} none
2408
2409 \subsection{\texorpdfstring{\yquant{xx}}{xx}}\linkdef{xx}
2410 Syntax: \yquant!xx <targets>;! \\
2411 This is a symmetric flip gate, denoted by joined open squares.
2412 It should span multiple registers and it allows for controls.
2413 The same warnings as for the \ttlink{swap} gate apply.
2414 The style \ttlink{/yquant/operators/every xx} is installed.
2415
2416 \emph{Possible attributes:} none
2417
2418 \subsection{\texorpdfstring{\yquant{y}}{y}}\linkdef{y}
2419 Syntax: \yquant!y <target> | <pcontrol> ~ <ncontrol>;! \\
2420 This is a Pauli $\sigma_{\symup y}$ gate $-\ii\ketbra01 + \ii\ketbra10$, denoted by a rectangle that contains the letter $Y$.
2421 It may not span multiple registers, but allows for controls. \\
2422 The style \ttlink{/yquant/operators/every y} is installed.
2423
2424 \emph{Possible attributes:} none
2425
2426 \subsection{\texorpdfstring{\yquant{z}}{z}}\linkdef{z}
2427 Syntax: \yquant!z <target> | <pcontrol> ~ <ncontrol>;! \\
2428 This is a Pauli $\sigma_{\symup z}$ gate $\ketbra00 - \ketbra11$, denoted by a rectangle that contains the letter $Z$.
2429 It may not span multiple registers, but allows for controls. \\
2430 The style \ttlink{/yquant/operators/every z} is installed.
2431
2432 \emph{Possible attributes:} none
2433
2434 \subsection{\texorpdfstring{\yquant{zz}}{zz}}\linkdef{zz}
2435 Syntax: \yquant!zz <targets>;! \\
2436 This is a symmetric phase gate, denoted by joined filled circles.
2437 It should span multiple registers, but does not allow for controls.
2438 The same warnings as for the \ttlink{swap} gate apply.
2439 The style \ttlink{/yquant/operators/every zz} is installed.
2440
2441 \emph{Possible attributes:} none
2442
2443
2444 \section{Examples}\label{sec:examples}
2445 This section will contain lots of examples.
2446 On the left\hyp hand side, the output is given, while the code to construct the example is on the right.
2447 All examples that are provided originate from the examples supplied with \pkg{qasm}, \pkg{qcircuit}, \pkg{quantikz}, and \pkg{qpic}.
2448 We will essentially follow their manuals example\hyp by\hyp example, which gives a nice comparison in how to achieve the given feature using these packages and \Yquant{} instead.
2449 All examples of course require inclusion of the \Yquant{} package with newest compatibility in the preamble, and some also require \pkg{braket}.
2450
2451 \subsection{\pkg{qasm} documentation}
2452 \begingroup%
2453 \yquantset{register/default name=$\ket{\reg_{\idx}}$}%
2454 The \pkg{qasm} documentation most often names the registers in the way $\ket{\mathrm{register}_{\mathrm{index}}}$.
2455 This can be achieved by writing
2456 \begin{minted}{tex}
2457 qubit {$\ket{<name>_{\idx}}$} <name>[<len>];
2458 \end{minted}
2459 but if you want to realize this naming scheme for all circuits in your document, it is more convenient to say
2460 \begin{minted}{tex}
2461 \yquantset{register/default name=$\ket{\reg_{\idx}}$}
2462 \end{minted}
2463 in the preamble, as is done here.
2464
2465 \compat{New in 0.3}Note that \Yquant{} also directly supports the \langlink{qasm} syntax, see \cref{sec:foreign:qasm}.
2466
2467 \begin{example}[test1 (create an EPR pair)]
2468 \begin{codeexample}
2469 \begin{tikzpicture}
2470 \begin{yquant}
2471 qubit q[2];
2472
2473 h q[0];
2474 cnot q[1] | q[0];
2475 \end{yquant}
2476 \end{tikzpicture}
2477 \end{codeexample}
2478 \end{example}
2479
2480 \clearpage
2481 \begin{example}<Updated in 0.1.1>[test2 (simple teleportation circuit)]
2482 \begin{codeexample}
2483 \begin{tikzpicture}
2484 \begin{yquant}
2485 qubit q[3];
2486
2487 h q[1];
2488 cnot q[2] | q[1];
2489 cnot q[1] | q[0];
2490 h q[0];
2491 measure q[0-1];
2492
2493 z q[2] | q[1];
2494 x q[2] | q[0];
2495 \end{yquant}
2496 \end{tikzpicture}
2497 \end{codeexample}
2498 \end{example}
2499
2500 \begin{example}[test3 (swap circuit)]
2501 \begin{codeexample}
2502 \begin{tikzpicture}
2503 \begin{yquant}
2504 qubit q[2];
2505
2506 cnot q[1] | q[0];
2507 cnot q[0] | q[1];
2508 cnot q[1] | q[0];
2509 \end{yquant}
2510 \end{tikzpicture}
2511 \end{codeexample}
2512 \end{example}
2513
2514 \clearpage
2515 \begin{example}<Updated in 0.1.1>[test4 (quantum fourier transform on three qubits)]
2516 \begin{codeexample}
2517 \begin{tikzpicture}
2518 \begin{yquant}
2519 qubit j[3];
2520
2521 h j[0];
2522 box {$S$} j[0] | j[1];
2523 box {$T$} j[0] | j[2];
2524 h j[1];
2525 box {$S$} j[1] | j[2];
2526 h j[2];
2527 swap (j[0, 2]);
2528 \end{yquant}
2529 \end{tikzpicture}
2530 \end{codeexample}
2531 \end{example}
2532
2533 \begin{example}[test5 (demonstrate arbitrary qubit matrix ops)]
2534 \begin{codeexample}
2535 % \usepackage{amsmath}
2536 \begin{tikzpicture}
2537 \begin{yquant}
2538 qubit j[2];
2539
2540 box {$\begin{bmatrix}
2541 e^{i \alpha} & 0 \\
2542 0 & e^{-i \alpha}
2543 \end{bmatrix}$} j[1] | j[0];
2544 box {$\begin{bmatrix}
2545 \cos\theta & -\sin\theta \\
2546 \sin\theta & \cos\theta
2547 \end{bmatrix}$} j[0];
2548 \end{yquant}
2549 \end{tikzpicture}
2550 \end{codeexample}
2551 \end{example}
2552
2553 \clearpage
2554 \begin{example}<Updated in 0.1.1>[test6 (demonstrate multiple-qubit controlled single-q-gates)]
2555 \begin{codeexample}
2556 \begin{tikzpicture}
2557 \begin{yquant}
2558 qubit j[4];
2559
2560 cnot j[2] | j[0, 1];
2561 x j[0];
2562 box {$U$} j[1] | j[0, 2-3];
2563 h j[2];
2564 measure j[3];
2565 \end{yquant}
2566 \end{tikzpicture}
2567 \end{codeexample}
2568 \end{example}
2569
2570 \begin{example}<Updated in 0.1.1>[test7 (measurement of operator with correction)]
2571 \begin{codeexample}
2572 \begin{tikzpicture}
2573 \begin{yquant}
2574 qubit q[2];
2575
2576 h q[0];
2577 box {$U$} q[1] | q[0];
2578 h q[0];
2579 measure q[0];
2580 box {$V$} q[1] | q[0];
2581 \end{yquant}
2582 \end{tikzpicture}
2583 \end{codeexample}
2584 \end{example}
2585
2586 \clearpage
2587 \begin{example}<Updated in 0.4>[test8 (stage in simplification of quantum teleportation)]
2588 \begin{codeexample}
2589 \begin{tikzpicture}
2590 \begin{yquant}
2591 qubit {$\ket{q_0} = \ket\psi$} q[1];
2592 qubit {$\ket{q_{\idx}} = \ket0$} q[+2];
2593
2594 h q[1];
2595 cnot q[1] | q[0];
2596 cnot q[2] | q[1];
2597 cnot q[1] | q[0];
2598 h q[0];
2599 cnot q[2] | q[1];
2600 z q[0] | q[2];
2601 \end{yquant}
2602 \end{tikzpicture}
2603 \end{codeexample}
2604 Note that we left out two Hadamards at the end.
2605
2606 Another way to provide various initial values in a single command is by performing case discrimination on \texlink\idx, for example in the following manner:
2607
2608 \begin{minted}{tex}
2609 qubit {$\ket{q_{\idx}} = \Ifcase\idx\relax \ket\psi \Else \ket0 \Fi$} q[3];
2610 \end{minted}
2611 In principle, all \TeX{} conditionals that check against \texlink{\idx} need to be prefixed by \texlink\protect.
2612 \linkdef\Ifnum\linkdef\Ifcase\linkdef\Or\linkdef\Else\linkdef\Fi\linkdef\Unless\linkdef\The
2613 If the \ttlink{compat} key is at least \texttt{0.4}, \Yquant{} will make the commands \tex!\Ifnum!, \tex!\Ifcase!, \tex!\Or!, \tex!\Else!, \tex!\Fi!, \tex!\Unless! and \tex!\The! available for use within gates; they correspond in a certain way to auto\hyp\texlink\protect ed versions of the corresponding \TeX{} primitives.
2614 Most likely, you will never need them inside values if not in the exact combination with \texlink\idx.
2615 \end{example}
2616
2617 \clearpage
2618 \begin{example}[test9 (two-qubit gate circuit implementation of Toffoli)]
2619 \begin{codeexample}
2620 \begin{tikzpicture}
2621 \begin{yquant}
2622 qubit q[3];
2623
2624 box {$\sqrt X$} q[2] | q[1];
2625 cnot q[1] | q[0];
2626 box {$\sqrt X^\dagger$} q[2] | q[1];
2627 cnot q[1] | q[0];
2628 box {$\sqrt X$} q[2] | q[0];
2629 \end{yquant}
2630 \end{tikzpicture}
2631 \end{codeexample}
2632 \end{example}
2633
2634 \begin{example}<Alternative in 0.1.2\\Updated in 0.1.1>[test10 (multi-qubit gates also demonstrates use of classical bits)]
2635 \begin{codeexample}
2636 \begin{tikzpicture}
2637 \begin{yquant}
2638 qubit {$\ket{q_0}$} q;
2639 cbit {$c_1$} c;
2640 qubit {$\ket{q_2}$} q[+1];
2641
2642 h q[0];
2643 box {$U$} (q[0], c);
2644 box {$S$} q[1];
2645 box {$U$} (c, q[1]);
2646 \end{yquant}
2647 \end{tikzpicture}
2648 \end{codeexample}
2649 Instead of a discontiguous vector register, we could also have used three scalar registers.
2650 The labels chosen for \pkg{qasm} do not fit well to the indices \Yquant{} assigns.
2651 We might also have used a three\hyp register vector and used the \ttlink{settype} pseudo\hyp gate to immediately change the second register into a classical one, which would give indices matching the labels---but still, the registers would have a common name, which would make this a very unnatural approach.
2652 \end{example}
2653
2654 \clearpage
2655 \begin{example}<Updated in 0.1.1>[test11 (user-defined multi-qubit ops)]
2656 \begin{codeexample}
2657 \begin{tikzpicture}
2658 \begin{yquant}
2659 qubit q[3];
2660
2661 h q[0];
2662 box {$U_{f(x)}$} (q[0, 1]);
2663 h q[1];
2664 box {$U_{f(x, y)}$} (q);
2665 \end{yquant}
2666 \end{tikzpicture}
2667 \end{codeexample}
2668 Here we used the fact that a vector register can also be addressed as a whole.
2669 Instead of \tex!(q)!, we could have also written, e.g., \tex!(q[0]-q[2])! or \tex!(q[0-2])!, or enumerated all sub\hyp registers in a comma\hyp separated list.
2670 \end{example}
2671
2672 \begin{example}<Updated in 0.1.1>[test12 (multi-qubit controlled multi-qubit operations)]
2673 \begin{codeexample}
2674 \begin{tikzpicture}
2675 \begin{yquant}
2676 qubit q[3];
2677
2678 h q[0];
2679 box {$U$} (q[1-2]) | q[0];
2680 h q[0];
2681 box {$V$} (q[0-1]) | q[2];
2682 \end{yquant}
2683 \end{tikzpicture}
2684 \end{codeexample}
2685 \end{example}
2686
2687 \clearpage
2688 \begin{example}[test13 (three-qubit phase estimation circuit with QFT and controlled-U)]
2689 \begin{codeexample*}
2690 \begin{tikzpicture}
2691 \begin{yquant}
2692 qubit {$\ket{j_{\idx}} = \ket0$} j[3];
2693 qubit s[2];
2694
2695 h j;
2696 box {$U^4$} (s) | j[0];
2697 box {$U^2$} (s) | j[1];
2698 box {$U$} (s) | j[2];
2699 h j[0];
2700 box {$S$} j[1] | j[0];
2701 h j[1];
2702 box {$T$} j[2] | j[0];
2703 box {$S$} j[2] | j[1];
2704 h j[2];
2705 measure j;
2706 \end{yquant}
2707 \end{tikzpicture}
2708 \end{codeexample*}
2709 \end{example}
2710
2711 \begingroup%
2712 \mdfapptodefinestyle{bdBox}{%
2713 leftmargin=-.12\linewidth,
2714 rightmargin=-.12\linewidth
2715 }%
2716 \begin{example}[test14 (three-qubit FT QEC circuit with syndrome measurement)]
2717 \begin{codeexample*}
2718 \begin{tikzpicture}
2719 \begin{yquant}
2720 qubit q[3];
2721 qubit {$\ket{s_{\idx}} = \ket0$} s[2];
2722 cbit {$c_{\idx} = 0$} c[2];
2723
2724 h s[0];
2725 cnot s[1] | s[0];
2726 cnot s[0] | q[0];
2727 cnot s[1] | q[1];
2728 cnot s[1] | s[0];
2729 h s[0];
2730 measure s;
2731 cnot c[0] | s[0];
2732 cnot c[1] | s[1];
2733 discard s; % to suppress wires extending until re-initialization
2734
2735 init {$\ket0$} s;
2736 h s[0];
2737 cnot s[1] | s[0];
2738 cnot s[0] | q[1];
2739 cnot s[1] | q[2];
2740 cnot s[1] | s[0];
2741 h s[0];
2742 measure s;
2743
2744 box {Process\\Syndrome} (s, c);
2745 box {$\symcal R$} (q) | s, c;
2746 \end{yquant}
2747 \end{tikzpicture}
2748 \end{codeexample*}
2749 \end{example}
2750 \endgroup%
2751
2752 \clearpage
2753 \begin{example}<Updated in 0.1.1>[test15 (``D-type'' measurement)]
2754 \begin{codeexample}
2755 \begin{tikzpicture}
2756 \begin{yquant}
2757 qubit {$\ket{q_0} = \ket\psi$} q;
2758 qubit {$\ket{q_1} = \ket+$} q[+1];
2759
2760 zz (q);
2761 dmeter {$H Z_\theta$} q[0];
2762 \end{yquant}
2763 \end{tikzpicture}
2764 \end{codeexample}
2765 \end{example}
2766
2767 \begin{example}<Updated in 0.4, 0.1.2, 0.1.1>[test16 (example from Nielsen paper on cluster states)]
2768 \begin{codeexample}
2769 \begin{tikzpicture}
2770 \begin{yquant}
2771 qubit {$\ket{q_{\idx}} = \ket\psi$} q[2];
2772 qubit {$\ket{q_2} = \ket\phi$} q[+1];
2773 qubit {$\ket{q_3} = \ket0$} q[+1];
2774
2775 zz (q[1], q[2]);
2776 align q;
2777 cnot q[3] | q[2];
2778 slash q[0];
2779 dmeter q[3];
2780 align q;
2781 [solid]
2782 barrier q[2];
2783 discard q[2];
2784 \end{yquant}
2785 \end{tikzpicture}
2786 \end{codeexample}
2787 We needed to include an \ttlink{align} pseudo\hyp gate to put the slash at the desired position.
2788 Usually, this would be sufficient to put the \ttlink{cnot} and the \ttlink{slash} gate directly under each other, as it is in the \pkg{qasm} example.
2789 However, the \ttlink{slash} gate is special in that it does not need horizontal space and is put with only half of the usual operator separation into the circuit (for this reason, it can be put at the beginning of a wire without creating weird shifts with respect to the ``unslashed'' registers---it is put in the initial line that every wire even without an operation has).
2790 Hence, you should normally only use the \ttlink{slash} gate as the very first gate in a circuit.
2791 To get the vertical stopper mark, we abuse a \ttlink{barrier} on just a single wire and turn it from dashed to solid before \ttlink{discard}ing.
2792 \end{example}
2793
2794 \begin{example}<Updated in 0.1.1>[test17 (example from Nielsen paper on cluster states)]
2795 \begin{codeexample}
2796 \begin{tikzpicture}
2797 \begin{yquant}
2798 qubit {$\ket{q_0} = \ket\psi$} q;
2799 qubit {$\ket{q_{\idx}} = \ket+$} q[+2];
2800 qubit {$\ket{q_3} = \ket\phi$} q[+1];
2801
2802 zz q[(0-1), (2-3)];
2803 zz (q[1-2]);
2804 dmeter {$H$} q[1-2];
2805 \end{yquant}
2806 \end{tikzpicture}
2807 \end{codeexample}
2808 This example shows how the multi\hyp qubit delimiter (the parenthesis) can even be used within indices.
2809 \end{example}
2810
2811 \begin{example}<Updated in 0.1.2>[test18 (multiple-control bullet op)]
2812 \begin{codeexample}
2813 \begin{tikzpicture}
2814 \begin{yquant}
2815 qubit {$\ket{q_{\idx}} = \ket{\Ifcase\idx\relax \psi \Or + \Or + \Or \phi \Fi}$} q[4];
2816
2817 zz (q);
2818 dmeter {$H$} q[1, 2];
2819 \end{yquant}
2820 \end{tikzpicture}
2821 \end{codeexample}
2822 This gate is probably a generalization of \ttlink{zz}, $\symbb1 - 2 \ketbra{1\dotsm1}{1\dotsm1}$, and indeed since version~0.1.2, we can use \ttlink{zz} for this purpose.
2823 This time, we used the case distinction method in the initialization, as already alluded to before.
2824 \end{example}
2825 \endgroup
2826
2827 \clearpage
2828 \subsection{\pkg{qcircuit} documentation}
2829 \begingroup%
2830 \yquantset{operator/separation=2mm}
2831 For a better orientation, we use the same section headings as the \pkg{qcircuit} manual.
2832 The manual uses unnamed registers a lot; often, we will use the \texlink{yquant*} environment to make things more concise.
2833 As the \pkg{qcircuit} manual uses a bit larger separation between the operators than \Yquant's default, we globally say\\*\tex!\yquantset{operator/separation=2mm}!.
2834
2835 \subsubsection{I. Introduction}
2836 \begin{example}<Updated in 0.5, 0.4, 0.1.1>%
2837 \begin{codeexample*}
2838 % \useyquantlanguage{groups}
2839 \begin{yquantgroup}
2840 \registers{
2841 qubit {} q[3];
2842 }
2843 \circuit{
2844 box {$U$} q[2] | q[0, 1];
2845 }
2846 \equals
2847 \circuit{
2848 box {$V$} q[2] | q[1];
2849 cnot q[1] | q[0];
2850 box {$V^\dagger$} q[2] | q[1];
2851 cnot q[1] | q[0];
2852 box {$V$} q[2] | q[0];
2853 }
2854 \end{yquantgroup}
2855 \end{codeexample*}
2856 The best way to realize circuit equalities is with the help of \langlink{groups} language extension, which is documented in \cref{sec:foreign:groups}.
2857 \end{example}
2858
2859 \begin{example}<Updated in 0.4>
2860 \begin{codeexample*}
2861 \begin{tikzpicture}
2862 \begin{yquant}
2863 qubit {$\ket\psi$} a;
2864 qubit {$\ket0$} b[2];
2865
2866 h b[1];
2867 cnot b[0] | b[1];
2868 cnot b[0] | a;
2869 h a;
2870 align a, b;
2871 measure a;
2872 [direct control]
2873 measure b[0];
2874
2875 x b[1] | b[0];
2876 z b[1] | a;
2877
2878 discard a;
2879 discard b[0];
2880 output {$\ket\psi$} b[1];
2881 \end{yquant}
2882 \end{tikzpicture}
2883 \end{codeexample*}
2884 Here, we see how to use a measurement as a direct output for the next controlled operation.
2885 \end{example}
2886
2887 \begin{example}<Updated in 0.9, 0.4>
2888 \begin{codeexample*}
2889 % \usetikzlibrary{quotes}
2890 \begin{tikzpicture}
2891 \begin{yquant}
2892 qubit {} msg[3];
2893 nobit syndrome[3];
2894
2895 [this subcircuit box style={dashed, "Syndrome Measurement"}]
2896 subcircuit {
2897 qubit {} msg[3];
2898 [out]
2899 qubit {$\ket0$} syndrome[3];
2900
2901 cnot syndrome[0] | msg[0];
2902 cnot syndrome[0] | msg[1];
2903 cnot syndrome[1] | msg[1];
2904 cnot syndrome[1] | msg[2];
2905 cnot syndrome[2] | msg[0];
2906 cnot syndrome[2] | msg[2];
2907
2908 dmeter {$M_{\symbol{\numexpr`a+\idx}}$} syndrome;
2909 } (msg[-2], syndrome[-2]);
2910
2911 ["Recovery"]
2912 box {$\mathcal R$} (msg) | syndrome;
2913 discard syndrome;
2914 \end{yquant}
2915 \end{tikzpicture}
2916 \end{codeexample*}
2917 The example demonstrates how to put a description next to a gate.
2918 In general, those descriptions should be realized using the \TikZ{} feature \texttt{label}.
2919 Using the \TikZ{} library \texttt{quotes}, the label is most easily specified.
2920 Since the label is not part of the valid arguments and also cannot be found in the \texttt{/yquant} path, it is automatically passed to \ttlink{/yquant/operator style}.
2921
2922 To enclose a part of the circuit by a rectangle, we use a subcircuit.
2923 This appears to be logically preferrable to using the \texlink{\enclose} macro, as the syndrome measurement is a self-contained operation which introduces its own registers; in principle, it could be turned into a custom gate and reused.
2924
2925 We define the incoming \ttlink{qubit}s in the outer circuit, they will have the default attribute \ttlink{[inout]}; the syndrome registers, which are created only in the subcircuit enter as \ttlink{nobit}s and consequently have the \ttlink{[out]} attribute.
2926 It is important to note that both the \texttt{dashed} style as well as the \texttt{label} (here with quoted syntax) are specified only inside \ttlink{/yquant/this subcircuit box style}.
2927 This ensures that they are not also attached to every single gate in the subcircuit.
2928
2929 Then we see how to apply an operation to multiple registers in parallel while using the \texlink{\idx} macro to still give them a different text.
2930 Since \texlink{\idx} gives a numerical index (zero\hyp based), we exploit the ASCII code (actually, this document is compiled in Unicode mode...) to turn this into a letter.
2931
2932 Note that it could have become necessary to pass the \ttlink{overlay} attribute to the recovery gate, as it is a multi\hyp register gate with a \texttt{label}, meaning that \Yquant{} cannot reliably distribute its total vertical extent over its constituent registers.
2933 However, as the $\symcal R$ together with the label in total were not higher than the three\hyp qubit gate would have been anyway, this was not necessary here.
2934 In generally, don't use \ttlink{overlay} unless necessary; maybe a future version will even be able to handle the more difficult cases better.
2935
2936 Finally, we will give a similar circuit by using the \texlink{\enclose} interface instead of subcircuits, this time also showing how we can change the shape of the measurement gate to one as in the \pkg{qcircuit} manual:
2937
2938 \begin{codeexample*}
2939 % \usetikzlibrary{quotes}
2940 \begin{tikzpicture}
2941 \begin{yquant}
2942 qubit {} msg[3];
2943 \encloseall["Syndrome Measurement"]{
2944 qubit {$\ket0$} syndrome[3];
2945
2946 cnot syndrome[0] | msg[0];
2947 cnot syndrome[0] | msg[1];
2948 cnot syndrome[1] | msg[1];
2949 cnot syndrome[1] | msg[2];
2950 cnot syndrome[2] | msg[0];
2951 cnot syndrome[2] | msg[2];
2952 [shape=yquant-rectangle, rounded corners=.45em]
2953 dmeter {$M_{\symbol{\numexpr`a+\idx}}$} syndrome;
2954 }
2955 ["Recovery"]
2956 box {$\symcal R$} (msg) | syndrome;
2957 discard syndrome;
2958 \end{yquant}
2959 \end{tikzpicture}
2960 \end{codeexample*}
2961 We name several elements that visually form the enclosing rectangle; then, we use the \TikZ{} library \texttt{fit} to put a node around them all.
2962 \linkdef{yquant-rectangle}Any gate can be given a custom shape; here, we use a \texttt{yquant-rectangle}, which is the analogue to \TikZ's \texttt{rectangle} and thus supports the standard \texttt{rounded corners} style.
2963 \end{example}
2964
2965 \subsubsection{IV. Simple Quantum Circuits}
2966 \begin{example}
2967 \begin{codeexample}
2968 \begin{tikzpicture}
2969 \begin{yquant*}
2970 x q;
2971 \end{yquant*}
2972 \end{tikzpicture}
2973 \end{codeexample}
2974 \end{example}
2975
2976 \clearpage
2977 \paragraph{A. Wires and gates}\leavevmode
2978 \begin{example}
2979 \begin{codeexample}
2980 \begin{tikzpicture}
2981 \begin{yquant*}
2982 h a;
2983 align a, b;
2984 z a;
2985 x b;
2986 h a;
2987 \end{yquant*}
2988 \end{tikzpicture}
2989 \end{codeexample}
2990 \end{example}
2991
2992 \begin{example}<Updated in 0.1.2>
2993 \begin{codeexample}
2994 \begin{tikzpicture}
2995 \begin{yquant*}
2996 box {$U$} a;
2997 box {$U^\dagger$} b;
2998 setstyle {->} -;
2999 \end{yquant*}
3000 \end{tikzpicture}
3001 \end{codeexample}
3002 \Yquant{} allows to change wire styles by means of the \ttlink{setstyle} and \ttlink{addstyle} pseudo\hyp gates.
3003 Here, we use the gate on all wires in order to set an arrow style.
3004 Note that arrowheads are actually very special in two respects:
3005 \begin{itemize}
3006 \item \Yquant{} draws continuous wires for as long as possible.
3007 In this example, the wire path extends from the very left to the end of the circuit; \compat{Changed in 0.1.2}\Yquant{} does not draw a wire \emph{to} the gate and then a separate one \emph{from} the gate to the next or the end.
3008 The only way to force \Yquant{} to draw multiple wires is to change the wire style or type mid\hyp circuit.
3009 For example, by saying \yquant!addstyle {} -;!, all wire paths will be separated at the current position, which \emph{in theory} allows to draw arrowsheads on intermediate wires.
3010 \item \emph{In practice}, this will not work due to the clipping commands that \Yquant{} installs.
3011 Every wire extends from the center of the left to the center of the right gate, and the gate's shape acts as a clipping path.
3012 Consequently, though the arrowhead is drawn, it is actually drawn at the center of the gate instead of the \texttt{west} anchor and then clipped away (unless the gate is small, in which case you might still see some fragments of the arrowhead).
3013 \end{itemize}
3014 Thus, it is currently not possible to use arrowheads on intermediate wires.
3015 If you really need to do this (say, for only a single gate), you may experiment with the \TikZ{} \texttt{shorten} keys, which allow you to manually reduce the length of the wire, but the amount of reduction must be hand\hyp computed for every gate.
3016 If you need this more often, consider filing a feature request.
3017 \end{example}
3018
3019 \paragraph{B. CNOT and other controlled single qubits gates}\leavevmode
3020 \begin{example}
3021 \begin{codeexample}
3022 \begin{tikzpicture}
3023 \begin{yquant*}
3024 cnot a[1] | a[0];
3025 cnot a[0] | a[1];
3026 \end{yquant*}
3027 \end{tikzpicture}
3028 \end{codeexample}
3029 \end{example}
3030
3031 \begin{example}<Updated in 0.1.1>
3032 \begin{codeexample}
3033 \begin{tikzpicture}
3034 \begin{yquant*}
3035 cnot q[2] | q[0];
3036 cnot q[0] | q[1-3];
3037 box {$U$} q[0] | q[2];
3038 \end{yquant*}
3039 \end{tikzpicture}
3040 \end{codeexample}
3041 \end{example}
3042
3043 \paragraph{C. Vertical wires}\leavevmode
3044 \begin{example}<Updated in 0.4, 0.1.1>
3045 \begin{codeexample}
3046 \begin{tikzpicture}
3047 \begin{yquant*}
3048 box {$U_{\The\numexpr\idx+1}$} q[0, 2] | q[1];
3049 \end{yquant*}
3050 \end{tikzpicture}
3051 \end{codeexample}
3052 There is no direct support for this construction, but as with the initialization of a vector registers, \Yquant{} allows to access the macro \texlink{\idx} within an operator \ttlink{value}.
3053 This macro follows the same rules as the name suffix, i.e., it assigns indices (zero\hyp based) to the target registers in top\hyp to\hyp bottom order, regardless of which order was specified in the target list.
3054 Since we instead want a one\hyp based subscript, we need to add one.
3055 Note that if you want to output \texlink{\idx} directly or within an unexpandable expression, you don't need to take any action.
3056 However, here, \tex!\the! is expandable; and since \Yquant{} needs to process all its output twice (first in order to determine the vertical spacing, second to actually typeset), you must manually take care that the command is \emph{not} expanded prematurely by saying \texlink[\protect]{\protect\the} instead, for which \Yquant{}, with a \ttlink{compat} setting of at least \texttt{0.4}, provides the shorthand \texlink\The.
3057 Had we used the plain \TeX{} \tex!\the! instead, the subscript would have been ``$1$'' for both operators.
3058 \end{example}
3059
3060 \paragraph{D. Labeling input and output states}\leavevmode
3061 \begin{example}
3062 \begin{codeexample}
3063 \begin{tikzpicture}
3064 \begin{yquant*}
3065 qubit {$\ket1$} q[2];
3066 cnot q[0] | q[1];
3067 output {$\ket\idx$} q;
3068 \end{yquant*}
3069 \end{tikzpicture}
3070 \end{codeexample}
3071 \end{example}
3072
3073 \begin{example}<Updated in 0.4, 0.1.1>
3074 \begin{codeexample}
3075 % \usetikzlibrary{calc}
3076 \begin{tikzpicture}
3077 \begin{yquant*}
3078 {
3079 \yquantset{every multi label/.style={every node/.style={anchor=east, midway}}}
3080 init {$\ket{0^k}$} (a[-1]);
3081 }
3082 init {$\ket\psi$} (b[-1]);
3083 [name=cinit]
3084 qubit {\Ifcase\idx\relax$A$\Or$B$\Fi} c[2];
3085 \node[anchor=east] at ($(cinit-0.west)!.5!(cinit-1.west)$) {$\ket\psi$};
3086 \end{yquant*}
3087 \end{tikzpicture}
3088 \end{codeexample}
3089 Here, three different styles for the initialization of multi\hyp qubit labels are used.
3090 The second one (using a curly brace) corresponds to the default.
3091 It is overwritten for the first qubit, and to make this modification local, this is done in a group.
3092 The third qubit pair uses an overall label and additionally individual labels on the lines.
3093 The recommended way to do this starting from version~0.4 is to add the ``special'' label by means of a \TikZ{} command.
3094 \end{example}
3095
3096 \subsubsection{V. More Complicated Circuits: Multiple Qubit gates and Beyond}
3097 \paragraph{A. Multiple qubit gates}\leavevmode
3098 \begin{example}<Updated in 0.1.1>
3099 \begin{codeexample}
3100 \begin{tikzpicture}
3101 \begin{yquant*}
3102 box {$U^\dagger$} (a[-2]);
3103 \end{yquant*}
3104 \end{tikzpicture}
3105 \end{codeexample}
3106 \end{example}
3107
3108 \begin{example}<Updated in 0.4, 0.1.1>
3109 \begin{codeexample}
3110 \yquantdefinebox{dots}[inner sep=0pt]{$\dots$}
3111 \begin{tikzpicture}
3112 \begin{yquant}
3113 qubit {} a;
3114 cbit {} b;
3115 [register/minimum height=0pt, register/minimum depth=0pt]
3116 nobit ellipsis;
3117 qubit {} c;
3118
3119 dots ellipsis;
3120 box {$U$} (a, b, ellipsis, c);
3121 dots ellipsis;
3122 \end{yquant}
3123 \end{tikzpicture}
3124 \end{codeexample}
3125 This demonstrates how a register of type \ttlink{nobit} might even be useful if the register is never used and no subcircuits are involved.
3126 Note how we overwrite the default minimum height and depth setting for this register only.
3127 Additionally, we for the first time define our own gate, which we call \texttt{dots}.
3128 As we define our own style, it does not inherit from \ttlink{/yquant/operators/every box}; hence, we only need to overwrite the \texttt{inner sep} coming from \TikZ's defaults.
3129 \end{example}
3130
3131 \clearpage
3132 \begin{example}<Updated in 0.1.2, 0.1.1>
3133 \begin{codeexample}
3134 \begin{tikzpicture}
3135 \begin{yquant*}
3136 box {$\symcal F$} (a[-1]);
3137 box {$\symcal G$} (a[0, 2]);
3138 \end{yquant*}
3139 \end{tikzpicture}
3140 \end{codeexample}
3141 This demonstrates \Yquant's capabilities of discontiguous multi\hyp qubit gates.
3142 \Yquant{} automatically splits multi\hyp qubit gates into slices of directly adjacent wires (which, for the $\symcal G$ case, are the single wires \yquant!a[0]! and as second slice \yquant!a[2]!).
3143 \end{example}
3144
3145 \paragraph{B. Measurements and classical bits}\leavevmode
3146 \begin{example}
3147 \begin{codeexample}
3148 \begin{tikzpicture}
3149 \begin{yquant*}
3150 measure a;
3151 dmeter {$\chi$} b;
3152 measure {$\ket{\xi_\pm}$} c;
3153 \end{yquant*}
3154 \end{tikzpicture}
3155 \end{codeexample}
3156 The ``tab'' and ``measure'' type are not supported yet.
3157 Extracting a meter symbol on its own will not be supported.
3158 If you are interested in the code, have a look at \texttt{yquant-shapes.tex} and search for the \ttlink{yquant-measure} shape.
3159 \end{example}
3160
3161 \begin{example}<Updated in 0.4, 0.1.2>
3162 \begin{codeexample}
3163 \begin{tikzpicture}
3164 \begin{yquant*}
3165 [shape=yquant-rectangle, rounded corners=.45em, direct control]
3166 measure {Codebit} a;
3167 box {$\chi$} b | a;
3168 discard a;
3169 measure b;
3170 \end{yquant*}
3171 \end{tikzpicture}
3172 \end{codeexample}
3173 Rectangles with rounded corners are not a specific style, but since the \ttlink{yquant-rectangle} shape internally uses \tex!\pgfpathrectangle!, the ordinary \TikZ{} option can be used (also, an inset specification can control how much the corner is rounded).
3174 \end{example}
3175
3176 \clearpage
3177 \begin{example}
3178 \begin{codeexample}
3179 \begin{tikzpicture}
3180 \begin{yquant}
3181 qubit {$\ket\psi$} q;
3182
3183 measure {$\ket{\xi_\pm}$} q;
3184 \end{yquant}
3185 \end{tikzpicture}
3186 \end{codeexample}
3187 \end{example}
3188
3189 \begin{example}<Updated in 0.1.1>
3190 \begin{codeexample}
3191 \begin{tikzpicture}
3192 \begin{yquant*}
3193 dmeter {Bell} (a[0, 1]);
3194 discard a;
3195 \end{yquant*}
3196 \end{tikzpicture}
3197 \end{codeexample}
3198 \end{example}
3199
3200 \begin{example}<Updated in 0.1.2, 0.1.1>
3201 \begin{codeexample}
3202 \begin{tikzpicture}
3203 \begin{yquant*}
3204 measure {$\ket{\xi_\mp}$} (a[-1, 3]);
3205 \end{yquant*}
3206 \end{tikzpicture}
3207 \end{codeexample}
3208 Multi\hyp qubit gates (including measurements) on non\hyp adjacent registers are properly supported.
3209 As explained in \cref{sec:registers}, there is one main and multiple subordinate gate in such a discontiguous multi\hyp qubit operation (though at the moment, the \ttlink{measure} gates with text is the only gate that makes this distinction).
3210 In our case, the main part contains the measurement symbol and the text, while the subordinate gates only contain the text.
3211 By default, \Yquant{} uses the first slice as main part, but you may influence this by preceding what you want to be ``main'' by a star:
3212
3213 \begin{codeexample}
3214 \begin{tikzpicture}
3215 \begin{yquant*}
3216 measure {$\ket{\xi_\mp}$} (a[-1, *3]);
3217 \end{yquant*}
3218 \end{tikzpicture}
3219 \end{codeexample}
3220 \end{example}
3221
3222 \clearpage
3223 \paragraph{C. Non-gate inserts, forcing space, and swap}\leavevmode
3224 \begin{example}<Updated in 0.6, 0.1.1>
3225 \begin{codeexample}
3226 \begin{tikzpicture}
3227 \begin{yquant*}
3228 [name=sw]
3229 swap (a[0-1]);
3230 text {$X$} a[1];
3231 slash b;
3232 box {$H^{\otimes n}$} b;
3233 \node[anchor=199] at (sw-0.north) {Defective Circuit};
3234 \end{yquant*}
3235 \end{tikzpicture}
3236 \end{codeexample}
3237 Here, the intermediate text was inserted by using a \ttlink{text} gate; before version~0.6, this would have to be done by a \ttlink{box} gate with \yquant![draw=none]! attribute.
3238 Another way would be to use an \ttlink{init} command, although this is semantically wrong (probably).
3239 \end{example}
3240
3241 \begin{example}
3242 \begin{codeexample}
3243 \begin{tikzpicture}
3244 \begin{yquant*}
3245 qubit {} a;
3246 [name=ypos]
3247 qubit {} b[3];
3248
3249 cnot a | b[0];
3250 [name=left]
3251 cnot a | b[1];
3252 hspace {7mm} -;
3253 [name=right]
3254 cnot a | b[2];
3255 \end{yquant*}
3256 \path (left |- ypos-0) -- (right |- ypos-1) node[midway] {$\dots$};
3257 \end{tikzpicture}
3258 \end{codeexample}
3259 Note how the register range \texttt{-} was used to denote all registers.
3260 We positioned the dots by first naming the relevant registers, so that the vertical position is at the coordinates \texttt{ypos-0} and \texttt{ypos-1}; and then, we also named the \ttlink{cnot} gates, so that we are able to discern the horizontal position.
3261 \end{example}
3262
3263 \clearpage
3264 \paragraph{D. Barriers}\leavevmode
3265 \begin{example}<Updated in 0.1.1>
3266 \begin{codeexample}
3267 \begin{tikzpicture}
3268 \begin{yquant*}
3269 x a[0, 1];
3270 barrier (a);
3271 \end{yquant*}
3272 \end{tikzpicture}
3273 \end{codeexample}
3274 \end{example}
3275 Now the \pkg{qcircuit} manual lists three circuits with barriers at different positions.
3276 They cannot be drawn with \Yquant; however, since neither of them is a valid circuit (no indication whether the control is positive or negative), this is of no concern.
3277
3278 \paragraph{E. How to control anything}\leavevmode
3279 \begin{example}<Updated in 0.1.1>
3280 \begin{codeexample}
3281 \begin{tikzpicture}
3282 \begin{yquant*}
3283 zz (a[0, 2]);
3284 cnot a[1] ~ a[0];
3285 zz (a[2, 3]);
3286 h a[3] | a[0] ~ a[1];
3287 measure a[2, 3];
3288 box {$U$} (a[0, 1]) | a[3] ~ a[2];
3289 discard a[2, 3];
3290 \end{yquant*}
3291 \end{tikzpicture}
3292 \end{codeexample}
3293 Note that it is not possible to draw a control to measurement (the measurement operations are explicitly defined not to accept controls): Either the measurement is performed or not (which transforms the register type), but a measurement conditioned on a quantum state is not possible.
3294 In principle, one could think of a measurement conditioned on a classical register (in which case the register type cannot change, as maybe the state stays quantum; the measurement operation then is similar to a complete dephasing).
3295 If there is need for this, please file a feature request.
3296 But note the relatively common (though unsupported by \Yquant{} at the moment) use of control lines that directly go from the measurement operator to the controlled operation; in this case, the opposite direction would be meant by using the same notation.
3297 \end{example}
3298
3299 \subsubsection{VI. Bells and Whistles: Tweaking Your Diagram to Perfection}
3300 For options how to configure the circuits, refer to \cref{sec:config}.
3301
3302 \paragraph{A. Spacing}\leavevmode
3303 \begin{example}<Updated in 0.1.1>
3304 \begin{codeexample}
3305 \begin{tikzpicture}
3306 \begin{yquant*}
3307 swap (a[0, 1]);
3308 box {$T^\dagger$} a[2] | a[1];
3309 swap (a[0, 1]);
3310 \end{yquant*}
3311 \end{tikzpicture}
3312 \end{codeexample}
3313 \end{example}
3314
3315 \paragraph{B. Labeling}\leavevmode
3316 \begin{example}<Updated in 0.6, 0.4>
3317 \begin{codeexample}
3318 \begin{tikzpicture}
3319 \begin{yquant}[every initial label/.style={anchor=south east, yshift=1mm}, every post measurement control=direct]
3320 qubit {\rlap{\hskip2mm $a$}} a;
3321 qubit {\rlap{\hskip2mm $b$}} b;
3322 nobit out;
3323 hspace {5mm} -;
3324
3325 measure b;
3326 text {$B$} out | b;
3327 measure a;
3328 text {$A$} out | a;
3329 discard -;
3330 \end{yquant}
3331 \end{tikzpicture}
3332 \end{codeexample}
3333 We support measurements with vertical outputs, but only if they replace the positive control of some action.
3334 Here, we fake this behavior by introducing an invisible register at the bottom, which will contain the outputs.
3335 Note that if we were to give style options to the \ttlink{text}s, they would also affect the measurements.
3336 The reason for this is that internally, the measurement will be nested within the same scope that draws the \ttlink{text}---so the options given to the \ttlink{text} will be inherited by the \ttlink{measure}ment.
3337 As an operator style overwrites default styles, this will also apply to the measurements.
3338 Hence, to circumvent this, we would need to \emph{revert} the options as attributes to the \ttlink{measure}ments, even if the reverted option was already included in their native style.
3339
3340 Repositioning the initial labels needs some care and manual fine\hyp tuning.
3341 \end{example}
3342
3343 \paragraph{C. Grouping}\leavevmode
3344 \begin{example}<Updated in 0.9, 0.4, 0.1.1>
3345 \begin{codeexample*}
3346 \begin{tikzpicture}
3347 \begin{yquant*}[register/separation=3mm]
3348 cnot a[2] | a[0];
3349 cnot a[2] | a[1];
3350 \enclose[to={a[1]}]{
3351 h a[0, 1];
3352 z a[2];
3353 cnot a[1] | a[0];
3354 h a[0];
3355 h a[1] | a[2];
3356 }
3357
3358 hspace {2mm} -;
3359 \end{yquant*}
3360 \end{tikzpicture}
3361 \end{codeexample*}
3362 Note that \tex!\begin{yquant*}! must not be followed by a line break (unless masked by \tex!%!) if options follow.
3363 Here, we use the \texlink{\enclose} macro to specify a part of the circuit that should be highlighted.
3364 The time\hyp like extent is given by the earliest and latest gates in the circuit; however, note that the last register is \emph{included} in the enclosed section, but explicitly \emph{excluded} using the \ttlink[/yquant/enclose/to]{to} key, so that now both the \ttlink z\hyp gate as well as the control of the \ttlink[h]{Hadamard} are not part of the enclosed region.
3365 \iffalse\end{yquant*}\fi% code folding
3366 \end{example}
3367 \endgroup
3368
3369 \clearpage
3370 \subsection{\pkg{quantikz} documentation}
3371 \begingroup
3372 \yquantset{operator/separation=4mm}%
3373 Again, our section headings will be the same as in the \pkg{quantikz} manual.
3374 And since \pkg{quantikz} also has even more space between the gates, we globally say \tex!\yquantset{operator/separation=4mm}!.
3375
3376 \subsubsection{II. A single wire}
3377 \begin{example}
3378 \begin{codeexample*}
3379 \begin{tikzpicture}[label position=north east, every label/.style={inner sep=1pt}]
3380 \begin{yquant}
3381 qubit {$\ket0$} a;
3382
3383 phase {$\alpha$} a;
3384 h a;
3385 phase {$\beta$} a;
3386 h a;
3387 phase {$\gamma$} a;
3388
3389 [every output/.append style={align=center}]
3390 output {Arbitrary\\pure state} a;
3391 \end{yquant}
3392 \end{tikzpicture}
3393 \end{codeexample*}
3394 The captions of \ttlink{phase} commands are internally implemented using \TikZ{} \texttt{label}s.
3395 At the moment, it is not possible to change any label options on a gate-type basis, only locally or fully globally (\TikZ{} feature request \href{https://github.com/pgf-tikz/pgf/issues/811}{\#811}).
3396 \end{example}
3397
3398 \clearpage
3399 \paragraph{A. Measurements}\leavevmode
3400 \begin{example}
3401 \begin{codeexample}
3402 % \usetikzlibrary{quotes}
3403 \begin{tikzpicture}
3404 \begin{yquant*}
3405 ["$0$"]
3406 measure a;
3407 discard a;
3408
3409 init {} a;
3410 dmeter {$1$} a;
3411 discard a;
3412 \end{yquant*}
3413 \end{tikzpicture}
3414 \end{codeexample}
3415 Other measurement shapes are not supported at the moment.
3416 \end{example}
3417
3418 \paragraph{B. Wires and arrows}\leavevmode
3419 \begin{example}<Updated in 0.6, 0.1.2>
3420 \begin{codeexample*}
3421 % \usetikzlibrary{quotes}
3422 \begin{tikzpicture}
3423 \begin{yquant}[operator/separation=1cm, every label/.append style={align=center}]
3424 qubit {$\ket0$\\initial state} a;
3425
3426 text {$X$} a;
3427
3428 ["$0$/$1$", type=qubit]
3429 measure a;
3430
3431 addstyle {->} a;
3432 output {$\ket1$} a;
3433 \yquantset{operator/separation=5mm}
3434 \end{yquant}
3435 \end{tikzpicture}
3436 \end{codeexample*}
3437 This example demonstrates how to instruct the \ttlink{measure} gate to use a different output type than the standard \ttlink{cbit}.
3438
3439 In general, any macros that are used within a \TikZ{} path or a \Yquant{} operation must not be fragile, or must be preceded with \texlink\protect.
3440 In this example, \tex!\\! is a robust command (at least in newer kernels), so protection is not required.
3441 Since it may occur quite frequently that \Yquant{} is used within a \tex!center! environment or in \tex!\centering! mode (in which \tex!\\! is still fragile), \Yquant{} takes care of this (it actually robustifies \tex!\@centercr!, which is the meaning of \tex!\\! in these surroundings---and which is now incorporated into the \LaTeX{} kernel as of June~2021).
3442
3443 In order to change the style of an individual wire, we use \ttlink{addstyle}.
3444 To make the final line shorter, we change the operator separation by issuing \texlink{\yquantset} at the end.
3445 \end{example}
3446
3447 \begin{example}<New in 0.4>
3448 \begin{codeexample}
3449 \begin{tikzpicture}
3450 \begin{yquant}
3451 qubit {$\ket0$} q;
3452 [name=h]
3453 h q;
3454 discard q;
3455 \path[/yquant/every wire, /yquant/every qubit wire, -Stealth] (h) -| ++(1cm, -.5cm) node[below] {trash};
3456 \end{yquant}
3457 \end{tikzpicture}
3458 \end{codeexample}
3459 Here, we use an ordinary \tex!\path! command to reproduce the ``trash'' line.
3460 This time, we chose to use the appropriate styles as \Yquant{} itself would do it instead of just saying \tex!\draw! without the options, which would also have worked.
3461 \end{example}
3462
3463 \begin{example}<Updated in 0.4>
3464 \begin{codeexample}
3465 % \usetikzlibrary{quotes}
3466 \begin{tikzpicture}
3467 \begin{yquant*}
3468 qubit {$\ket0^{\otimes n}$} a;
3469 ["north east:3" {font=\protect\footnotesize, inner sep=0pt}]
3470 slash a;
3471 h a;
3472 \end{yquant*}
3473 \end{tikzpicture}
3474 \end{codeexample}
3475 Again, you see an example of how some commands need to be \texlink\protect ed when used in \Yquant{} options, and that you can indeed exploit all features of the \texttt{quotes} library.
3476 \end{example}
3477
3478 \begin{example}
3479 \begin{codeexample}
3480 \begin{tikzpicture}
3481 \begin{yquant}
3482 qubits {$\ket0^{\otimes n}$} a;
3483 h a;
3484 \end{yquant}
3485 \end{tikzpicture}
3486 \end{codeexample}
3487 \end{example}
3488
3489 \subsubsection{III. Multiple Qubits}
3490 \begin{example}
3491 \begin{codeexample*}
3492 \begin{tikzpicture}
3493 \begin{yquant}
3494 qubit {$\ket0$} a;
3495 qubit {$\ket0$} b;
3496
3497 h a, b;
3498 cnot b | a;
3499 box {$U$} a ~ b;
3500 zz (a, b);
3501
3502 [after=a]
3503 qubit {} c;
3504
3505 swap (a, c);
3506 box {$U$} c | a ~ b;
3507 \end{yquant}
3508 \end{tikzpicture}
3509 \end{codeexample*}
3510 This example demonstrates the use of the \ttlink[{[after]}]{after} argument that instructs the register creation to begin the register only after the current position of another register that already exists.
3511 Note that this argument will always make the wire begin \emph{at the right end} of the last gate of the referenced register; however, if---as is the case here---this gate is shorter than \ttlink{/yquant/operator/minimum width}, this might not coincide with the visual right end.
3512 \end{example}
3513
3514 \clearpage
3515 \begin{example}<Updated in 0.4, 0.1.2, 0.1.1>
3516 \begin{codeexample*}
3517 \begin{tikzpicture}
3518 \begin{yquant*}
3519 [name=c]
3520 cnot a[0, 2] | a[1];
3521 [name=m, direct control]
3522 measure a[1];
3523 discard a[2];
3524 box {$U$} a[0] | a[1];
3525 \path[/yquant/every wire, /yquant/every qubit wire] (c-1) -- (m.center |- c-1);
3526 discard a[1];
3527 \end{yquant*}
3528 \end{tikzpicture}
3529 \end{codeexample*}
3530 Here, we manually extended the wire on the last register.
3531 We could instead have performed an \ttlink{align} gate before the discarding process, then, the wire line would have been extended by \Yquant{}; but since \ttlink{align} aligns at the \emph{right end} as opposed to the center of the gate, the wire line would have been a bit longer.
3532 Still, this \TikZ{} wire is inferior to a wire drawn by \Yquant, as it does not use clippings: the connection with the \ttlink{cnot} gate may not be accurate; in particular, if the wire is of a different color or if you need to draw classical or bundle wires, the connection will become unpleasant.
3533 \end{example}
3534
3535 \begin{example}<Updated in 0.1.1>
3536 \begin{codeexample}
3537 \begin{tikzpicture}
3538 \begin{yquant*}
3539 cnot a[1, 2] | a[0];
3540 measure a[1], a[2];
3541 box {$U$} a[0] | a[1];
3542 box {$V$} a[0] | a[2];
3543 discard a[1]-;
3544 \end{yquant*}
3545 \end{tikzpicture}
3546 \end{codeexample}
3547 \end{example}
3548
3549 \Yquant{} doesn't offer anything comparable to the new \tex!\ctrlbundle! command; and as the bundle lines are spaced much more tightly in \Yquant, this would not really make sense.
3550
3551 \clearpage
3552 \subsubsection{IV. Operating on many Qubits}
3553 \begin{example}<Updated in 0.1.1>
3554 \begin{codeexample}
3555 \begin{tikzpicture}
3556 \begin{yquant*}
3557 init {$\ket0^{\otimes3}$} (a[-2]);
3558
3559 box {$U$} (a[1-2]) | a[0];
3560 measure a[0];
3561 discard a[0];
3562 output {$\ket{11}$} (a[1-2]);
3563 \end{yquant*}
3564 \end{tikzpicture}
3565 \end{codeexample}
3566 Multi\hyp qubits inputs are possible using the \ttlink{init} command.
3567 The text assigned to a register declaration is always for an individual register.
3568 \end{example}
3569
3570 \begin{example}<Updated in 0.4>
3571 \begin{codeexample}
3572 \begin{tikzpicture}
3573 \begin{yquant*}[register/minimum height=6mm, register/minimum depth=6mm]
3574 hspace {7.5mm} a;
3575 h a;
3576 hspace {7.5mm} a;
3577 [x radius=1cm]
3578 box {$U$} (b, c);
3579 cnot b | a;
3580 measure a;
3581 discard a;
3582 \end{yquant*}
3583 \end{tikzpicture}
3584 \end{codeexample}
3585 \Yquant{} does not use a grid layout: operators are stacked next to each other.
3586 Therefore, there is no automatic centering of a column, though it could be emulated using hand-crafted \ttlink{hspace} commands, as was done here (the Hadamard gate uses the \ttlink{/yquant/operator/minimum width}, which is \tex!5mm!, while the large box has a width of \tex!2cm!, so that we need two \tex!7.5mm! spacings at the end, as the \ttlink{hspace} pseudo\hyp gate only inserts exactly the space you give, but not additional [twice] \ttlink{/yquant/operator/separation}, as would be the case for a hypothetical zero\hyp width gate).
3587 In fact, we don't even need the second \ttlink{hspace}, since the two\hyp qubit \ttlink{cnot} will automatically enforce correct alignment.
3588 \end{example}
3589
3590 \begin{example}<Updated in 0.4>
3591 \begin{codeexample*}
3592 \begin{tikzpicture}
3593 \begin{yquant}[register/separation=3mm, every nobit output/.style={}]
3594 qubit {} a[4];
3595 [every inspect/.append style={outer xsep=0pt}, operator/minimum width=0pt, font=\footnotesize, name=sub]
3596 subcircuit {
3597 \yquantset{operator/separation=0pt}
3598 qubit {} x[2];
3599 qubit {} y;
3600 discard -;
3601
3602 inspect {$x$} (x);
3603 [inner xsep=0pt]
3604 inspect {$y\vphantom f$} y;
3605
3606 [shape=yquant-init, decoration={mirror}]
3607 inspect {\hskip4mm $x$} (x);
3608 [shape=yquant-init, inner xsep=0pt]
3609 inspect {$y \oplus f(x)$} y;
3610 } (a[1-3]) | a[0];
3611 \node at (sub) {$U$};
3612 settype {qubit} a;
3613 \end{yquant}
3614 \end{tikzpicture}
3615 \end{codeexample*}
3616 We use quite some tricks to achieve such a layout.
3617 We use a subcircuit as a container and \ttlink{inspect} gates to indicate the inputs and output states accurately.
3618 As those are ordinary gates, we reset \ttlink{/yquant/operator/minimum width}, so that the ``$y$'' indication is not too long.
3619 Additionally, they are usually meant to be used within a circuit, i.e., they have an additional margin denoted by the \texttt{outer xsep}, which we also remove.
3620 Then, within the subcircuit, we reset the \ttlink{/yquant/operator/separation}, which would insert additional whitespace at the beginning.
3621 It is important to do this within the subcircuit and not as an attribute; else, we would also remove the outer lines going into the subcircuit.
3622
3623 Initial or final \ttlink{inspect} gates without a brace do not really need the separation between brace tip and text (\texttt{inner xsep}), so we also remove it.
3624 The output gates should have their braces and separations at the other side, which corresponds to changing their shape from \ttlink{yquant-output} to \ttlink{yquant-init} and mirroring the decoration (as in \ttlink{/yquant/every label}).
3625 To get the desired right\hyp alignment, we hand\hyp tailor an \tex!\hskip! that enlarges the upper output label---automatic alignments would not work here: putting the two \ttlink{inspect}s together with a case distinction on \texlink{\idx} would center them; using \ttlink{output}s would left\hyp align them.
3626
3627 Finally, we have to deal with the caption of the gate, which should be absolutely centered with respect to the subcircuit and not have any influence on the spacing---so we just insert it retrospectively as an ordinary \TikZ{} node.
3628
3629 Also note the use of \ttlink{discard} and \ttlink{settype} since we needed wires before and after the subcircuit, which must match the inner wires in type, but we actually do not want to have inner wires.
3630 \end{example}
3631
3632 \begin{example}
3633 \begin{codeexample}
3634 \begin{tikzpicture}
3635 \begin{yquant*}
3636 box {$\sqrt Z$} a;
3637 box {$X$} b;
3638 box {$\sqrt{\mathrm{SWAP}}$} (a, b);
3639 measure a;
3640 \end{yquant*}
3641 \end{tikzpicture}
3642 \end{codeexample}
3643 This time, we did not artificially discard the lines.
3644 \end{example}
3645
3646 \begin{example}<Updated in 0.4, 0.1.1>
3647 \begin{codeexample}
3648 \begin{tikzpicture}
3649 \begin{yquant}
3650 qubit {$c_{\idx}$} c[4];
3651 box {M\\A\\J} (c[-2]);
3652 box {M\\A\\J} (c[1-]);
3653 \end{yquant}
3654 \end{tikzpicture}
3655 \end{codeexample}
3656 Notice here that the vertical spacing is uneven.
3657 \Yquant{} realizes that the minimal vertical spacing will not be enough to account for the multi\hyp qubit boxes.
3658 However, when it tries to adjust positions accordingly so that the last gate fits, this will of course not change anything for the first wire, which is not contained in the gate.
3659 After having increased the spacing, \Yquant{} realizes that this already was enough to accomodate for the first gate, so no further action is taken.
3660 In order to get a more even spacing, just increase \ttlink{/yquant/register/minimum height} and \ttlink{/yquant/register/minimum depth}.
3661 \end{example}
3662
3663 \paragraph{A. Different connections}\leavevmode
3664 \begin{example}
3665 \begin{codeexample}
3666 \begin{tikzpicture}
3667 \begin{yquant}[register/default name=]
3668 qubit a;
3669 cbit b;
3670 nobit c;
3671 cbit d;
3672 qubit e;
3673 box {$U$} (-);
3674 settype {qubit} c;
3675 \end{yquant}
3676 \end{tikzpicture}
3677 \end{codeexample}
3678 This example demonstrates the declaration of a non\hyp existing register and the \ttlink{settype} pseudo\hyp gate that acts as a zero\hyp width, no\hyp content \ttlink{init} gate.
3679 \end{example}
3680
3681 \subsubsection{V. Slicing}
3682 \begin{example}
3683 \begin{codeexample}
3684 \begin{tikzpicture}
3685 \begin{yquant}
3686 qubit {} a[3];
3687 h a[0];
3688 [red, thick, label=step]
3689 barrier (a);
3690 cnot a[1] | a[0];
3691 measure a[0];
3692 discard a[0];
3693 cnot a[2] | a[1];
3694 h a[1];
3695 \end{yquant}
3696 \end{tikzpicture}
3697 \end{codeexample}
3698 There is nothing like a \texttt{slice all} keyword, as \Yquant's underlying layout is not grid\hyp based.
3699 Changing the style of slice captions simply means providing label options.
3700 This time, we used the \texttt{label} key instead of the shorter syntax provided by the \texttt{quotes} library, which is of course also possible.
3701 \end{example}
3702
3703 \begin{example}
3704 \begin{codeexample*}
3705 % \usetikzlibrary{quotes}
3706 \begin{tikzpicture}[every label/.style={rotate=40, anchor=south west}]
3707 \begin{yquant}[operators/every barrier/.append style={blue, thick}]
3708 qubit {} a[3];
3709 h a[0];
3710 ["slice 1"]
3711 barrier (-);
3712 cnot a[1] | a[0];
3713 ["slice 2"]
3714 barrier (-);
3715 measure a[0];
3716 discard a[0];
3717 cnot a[2] | a[1];
3718 ["slice 3"]
3719 barrier (-);
3720 h a[1];
3721 \end{yquant}
3722 \end{tikzpicture}
3723 \end{codeexample*}
3724 \end{example}
3725
3726 \begin{example}<Updated in 0.4>
3727 \begin{codeexample*}
3728 % \usetikzlibrary{quotes}
3729 \begin{tikzpicture}[label distance=4mm]
3730 \begin{yquant}[operators/every barrier/.append style={red, thick, shorten <= -4mm, shorten >= -4mm}]
3731 qubit {$\ket0$} a;
3732 h a;
3733 ["1"]
3734 barrier a;
3735 phase {[label distance=0pt]$\varphi$} a;
3736 ["2"]
3737 barrier a;
3738 h a;
3739 ["3"]
3740 barrier a;
3741 output {$\cos\frac\varphi2 \ket0 - i\sin\frac\varphi2 \ket1$} a;
3742 \end{yquant}
3743 \end{tikzpicture}
3744 \end{codeexample*}
3745 Usually, the \texttt{shorten} keys do not have any effect on \Yquant{} operations, since the latter are all made up of nodes.
3746 \linkdef{yquant-line}However, the \texttt{yquant-line} shape explicitly takes care of correctly handling them.
3747 It is the only one that does so.
3748 Since barriers usually end quite closely to the wires---and the default \texttt{dashed} style may make this worse---the \texttt{shorten}ing may often prove useful.
3749 Note that if the barriers are enlarged by means of negative \texttt{shorten}ings, this will not affect the bounding box and you must take care of appropriately shifting labels.
3750 The internal register height calculations might be inconsistent for multi\hyp register barriers with \texttt{shorten}ing: While \Yquant{} takes care of enlarging the registers so that there is enough space for placing the \ttlink{barrier} with its \emph{original} (single\hyp register) height, it actual height of the registers is only known at the second stage in calculation; but applying \texttt{shorten}ings after this stage would require another iteration of height calculation.
3751 Hence, multi\hyp register barriers that are enlarged by a lot will probably look bad unless you add manual spacing to the appropriate registers.
3752 Also note that we used much larger magnitudes in order to achieve a similar appearance as in \pkg{quantikz}.
3753 To avoid that the large distance also affects the \ttlink{phase} gate badly, we locally reset the distance; for this, there are two ways.
3754 The easiest one is to make use of the fact that the value of the \ttlink{phase} gate is passed directly as \texttt{label} argument, so that we can locally reset the distance.
3755 The other possibility would be to write
3756 \begin{minted}[linenos=false, fontsize=\footnotesize, bgcolor=boxGrayBody]{tex}
3757 {
3758 \yquantset{/tikz/label distance=0pt}
3759 phase {$\varphi$} a;
3760 }
3761 \end{minted}
3762 since due to the aforementioned lack of support for a style that sets the options in \TikZ, we must manually use a (grouped) \tex!\yquantset! instruction for this.
3763 Note that whenever you change a \TikZ{} style in a \Yquant{} environment, use the \tex!\yquantset! macro, \emph{not} \tex!\tikzset! or \tex!\pgfkeys!.
3764 Not only will the latter two not automatically restart the parser (so that you would have to issue \texlink{\yquant} after their use), but \Yquant{} has to process all its content twice in order to properly determine the register height.
3765 Only \texlink{\yquantset} will be properly captured and re\hyp issued at the correct position when the content is actually typeset.
3766 Had we written \tex!\tikzset{label distance=0pt} \yquant!, no effect at all would have been visible, since this command would only have taken effect in the first (invisible) round when \Yquant{} determines heights.
3767 \end{example}
3768
3769 \Yquant{} does not provide a mechanism for vertical labels, but you may of course just insert line breaks at appropriate positions (and set the \texttt{align} property of the labels).
3770
3771 \subsubsection{VI. Spacing}
3772 \paragraph{A. Local adjustment}\leavevmode
3773 \begin{example}<Updated in 0.4>
3774 \begin{codeexample}
3775 \begin{tikzpicture}
3776 \begin{yquant}[register/default name=]
3777 [register/minimum depth=2cm]
3778 qubit a;
3779 qubit b;
3780 qubit c;
3781
3782 h a;
3783 x b-;
3784 hspace {2cm} -;
3785 x a;
3786 z b-;
3787 h a;
3788 z b-;
3789 \end{yquant}
3790 \end{tikzpicture}
3791 \end{codeexample}
3792 The vertical distance between registers is calculated by \Yquant{} automatically based on the height and depth that \Yquant{} find for this particular register---i.e., how much space is required above and below the wire line for all the gates.
3793 In order to enlarge these values, reset \ttlink{/yquant/register/minimum height} or \ttlink{/yquant/register/minimum depth} to a different value.
3794 It is not possible to artificially \emph{reduce} the calculated heights and depths, as this would result in overlapping gates.
3795 However, sometimes it might be required to exclude a certain gate from the calculation; then, use the \ttlink{overlay} attribute.
3796 \end{example}
3797
3798 \begin{example}
3799 \begin{codeexample*}
3800 \begin{tikzpicture}
3801 \begin{yquant*}
3802 [x radius=1cm]
3803 x a;
3804 box {\hbox to 1cm{\hfil$X$\hfil}} a;
3805 hspace {1cm} a;
3806 x a;
3807 discard a;
3808 \end{yquant*}
3809 \end{tikzpicture}
3810 \end{codeexample*}
3811 Here, we demonstrate two possibilities to enlarge a box: The first is by specifying its size in terms of the \texttt{x radius} or \texttt{y radius} keys beforehand.
3812 Those values serve as minimum sizes and would be extended if the text extended beyond the box.
3813 The second option is to just enlarge the text artificially by explicitly putting it into a fixed\hyp width box.
3814 Note that in the first case, the \emph{radius} is specified, i.e., the half\hyp width, while in the second case, it is the \emph{total} width (both times modulo the inner separation).
3815 Also note that the \ttlink{/yquant/operator/minimum width} style is unsuitable for the given task: it would not change the visual width, only what \Yquant{} assumes its width to be.
3816 \end{example}
3817
3818 \clearpage
3819 \paragraph{B. Global Adjustment}\leavevmode
3820 \begin{example}
3821 \begin{codeexample*}
3822 \begin{tikzpicture}
3823 \begin{yquant*}[operator/separation=1cm]
3824 h a;
3825 phase {$\beta$} a;
3826 h a;
3827 \end{yquant*}
3828 \end{tikzpicture}
3829 \end{codeexample*}
3830 \end{example}
3831
3832 \begin{example}<Updated in 0.4, 0.1.1>
3833 \begin{codeexample}
3834 \begin{tikzpicture}
3835 \begin{yquant*}[register/minimum height=0pt, register/minimum depth=0pt]
3836 x a[0, 2];
3837 zz (a[0, 1]);
3838 x a[0];
3839 h b;
3840 \end{yquant*}
3841 \end{tikzpicture}
3842 \end{codeexample}
3843 By default, \Yquant{} will use the height and depth that is required by the individual gates, but at least \ttlink{/yquant/register/minimum height} or, respectively, \ttlink{/yquant/register/minimum depth} (which default to \tex!1.5mm!).
3844 Only manually reducing the default height will produce the cramped spacing displayed here.
3845 \end{example}
3846
3847 \clearpage
3848 \paragraph{C. Alignment}\leavevmode
3849 \begin{example}<Updated in 0.5, 0.4>
3850 \begin{codeexample}
3851 \begin{tikzpicture}
3852 \begin{yquant*}
3853 x a[0];
3854 cnot a[1] | a[0];
3855 \end{yquant*}
3856 \end{tikzpicture}
3857 $=$
3858 \begin{tikzpicture}
3859 \begin{yquant*}
3860 cnot a[1] | a[0];
3861 x a;
3862 \end{yquant*}
3863 \end{tikzpicture}
3864 \end{codeexample}
3865 Not specifying anything for the vertical alignment will lead to the common \TikZ{} problem: the baseline will be at the bottom, which is particularly bad in this case due to the missing $X$ gate.
3866 The keys for minimal register sizes do not help here, since they only affect \Yquant's internal handling, but not the bounding box calculated by \TikZ.
3867 The recommended way to draw circuit equations is always with the \langlink{groups} language extension.
3868
3869 \begin{codeexample}
3870 % \useyquantlanguage{groups}
3871 \begin{yquantgroup}
3872 \registers{
3873 qubit {} q[2];
3874 }
3875 \circuit{
3876 x q[0];
3877 cnot q[1] | q[0];
3878 }
3879 \equals
3880 \circuit{
3881 cnot q[1] | q[0];
3882 x q;
3883 }
3884 \end{yquantgroup}
3885 \end{codeexample}
3886 \end{example}
3887
3888 \clearpage
3889 \begin{example}<Updated in 0.5\\New in 0.4>
3890 \begin{codeexample*}
3891 % \useyquantlanguage{groups}
3892 \begin{yquantgroup}
3893 \registers{
3894 qubit {} q[2];
3895 }
3896 \circuit{
3897 init {$\ket x$} q[0];
3898 init {$\ket y$} q[1];
3899
3900 h q[0];
3901 cnot q[1] | q[0];
3902 output {} (-);
3903 }
3904 \equals[$\mapsto\quad\ket{\psi_{x, y}}\quad\mapsto$]
3905 \circuit{
3906 init {} (q);
3907 cnot q[1] | q[0];
3908 h q[0];
3909
3910 output {$\ket x$} q[0];
3911 output {$\ket y$} q[1];
3912 }
3913 \end{yquantgroup}
3914 \end{codeexample*}
3915 Here, we do not have a circuit equation (i.e., logical statements involving multiple rather independent circuits), but a circuit progression.
3916 Since only in one circuit we have a description of the registers, we declare them without an initial text and put their initialization into \ttlink{init} gates.
3917 The mapping in between is done by using the optional argument of the \texlink[groups/equals]{\equals} macro.
3918 In order to obtain the braces at the ends, we use empty \ttlink{output} and \ttlink{init} gates.
3919 \end{example}
3920
3921 \clearpage
3922 \subparagraph{1. Perfecting Vertical Alignment}\leavevmode
3923 \begin{example}<Updated in 0.5, 0.4>
3924 \begin{codeexample*}
3925 % \useyquantlanguage{groups}
3926 \begin{yquantgroup}
3927 \registers{
3928 qubit {} q;
3929 qubit {$\ket0$} q[+1];
3930 }
3931 \circuit{
3932 cnot q[0] | q[1];
3933 cnot q[1] | q[0];
3934 cnot q[0] | q[1];
3935 }
3936 \equals
3937 \circuit{
3938 cnot q[1] | q[0];
3939 cnot q[0] | q[1];
3940 }
3941 \end{yquantgroup}
3942 \end{codeexample*}
3943 \end{example}
3944
3945 \paragraph{D. Scaling}\leavevmode
3946 \begin{example}
3947 \begin{codeexample*}
3948 \begin{tikzpicture}[scale=1.5]
3949 \begin{yquant*}
3950 h a;
3951 phase {$\beta$} a;
3952 h a;
3953 \end{yquant*}
3954 \end{tikzpicture}
3955 \end{codeexample*}
3956 Here, we first scaled the circuit itself.
3957 The default style for \ttlink{/yquant/every circuit} sets the \texttt{transform shape} key for every node (which means any gate), so that those are also scaled.
3958 If your \TikZ{} version is at least 3.1.6a, this is all that needs to be done.
3959 In earlier versions, there was a bug that required \Yquant{} to reset the \texttt{transform shape} key for \texttt{label}s, which would then require you to scale those manually.
3960 \end{example}
3961
3962 \clearpage
3963 \subsubsection{VII. Typesetting}
3964 \paragraph{A. Global Styling}\leavevmode
3965 \begin{example}
3966 \begin{codeexample*}
3967 % \usetikzlibrary{quotes}
3968 \begin{tikzpicture}
3969 \begin{yquant*}[operators/every h/.append style={fill=red!20}]
3970 h a;
3971 phase {$\beta$} a;
3972 h a;
3973 ["$\ket\pm$" above right]
3974 measure a;
3975 \end{yquant*}
3976 \end{tikzpicture}
3977 \end{codeexample*}
3978 Instead of setting \ttlink{/yquant/operators/every h}, we could also have changed \ttlink{/yquant/operators/every box}.
3979 Had we used \ttlink{/yquant/every operator}, then the measurement would also have changed.
3980 Again, due to a \TikZ{} limitation, it is not possible to change the position of labels on a per\hyp style basis, only by using \texttt{label} options or a global setting.
3981 \end{example}
3982
3983 \begin{example}
3984 \begin{codeexample*}
3985 \begin{tikzpicture}[thick]
3986 \begin{yquant*}[every operator/.prefix style={fill=white}]
3987 cnot a[1] | a[0];
3988 box {$R_z(-\theta\fracslash2)$} a[1];
3989 cnot a[1] | a[0];
3990 box {$R_z(\theta\fracslash2)$} a[1];
3991 measure a[1];
3992 \end{yquant*}
3993 \end{tikzpicture}
3994 \end{codeexample*}
3995 As the ``thin'' style is the default, we present the opposite.
3996 By default, all operators are transparent; we changed this by giving all of them a white background color (but as a style \emph{prefix}, so that, e.g., black fillings overwrite this).
3997 Contrary to \pkg{quantikz}, this also fills the \ttlink{cnot}s.
3998 If you only want to fill certain operators, you have to selectively target them using their styles.
3999 \end{example}
4000
4001 \begin{example}<Updated in 0.1.2, 0.1.1>
4002 \begin{codeexample*}
4003 \begin{tikzpicture}
4004 \begin{yquant}[operators/every box/.append style={fill=white}]
4005 qubit {} j[3];
4006 box {$J_{12}$} (-j[1]);
4007 box {$J_{13}$} (j[0, 2]);
4008 box {$J_{23}$} (j[1]-);
4009 \end{yquant}
4010 \end{tikzpicture}
4011 \end{codeexample*}
4012 \Yquant{} properly splits discontiguous multi\hyp qubit operations.
4013 \end{example}
4014
4015 \paragraph{B. Per-Gate Styling}\leavevmode
4016 \begin{example}<Updated in 0.4>
4017 \begin{codeexample}
4018 % \usetikzlibrary{quotes}
4019 \begin{tikzpicture}
4020 \begin{yquant*}
4021 [fill=red!20, font=\color{cyan}]
4022 h a;
4023 [green]
4024 phase {[green]$\beta$} a;
4025 ["$\ket\pm$", blue]
4026 measure a;
4027 discard a;
4028 \end{yquant*}
4029 \end{tikzpicture}
4030 \end{codeexample}
4031 Note that assigning styles in this way will forward them to \ttlink{/yquant/operator style}, i.e., if you have controls, the style will not apply to them.
4032 \ttlink{/yquant/style} is suitable to style both, e.g., \tex|[style={fill=red!20}]|.
4033 \end{example}
4034
4035 \begin{example}<Updated in 0.9, 0.6, 0.4>
4036 \begin{codeexample*}
4037 % \usetikzlibrary{shapes.symbols, fit}
4038 \begin{tikzpicture}
4039 \begin{yquant}
4040 qubit {} data;
4041 qubit {$\ket0$} anc1[2];
4042
4043 h data;
4044 cnot anc1 | data;
4045 [after=data]
4046 qubit {$\ket0$} anc2[2];
4047 [name=noise]
4048 text {\phantom{noise}} (data, anc1);
4049 cnot anc2[0] | data;
4050 cnot anc2 | anc1[0];
4051 cnot anc2[1] | anc1[1];
4052 measure anc2[0];
4053 [blue] measure anc2[1];
4054 \end{yquant}
4055 \node[starburst, cyan, fill=yellow, draw=red, line width=2pt,
4056 inner xsep=-4pt, inner ysep=-5pt, fit=(noise)] {noise};
4057 \end{tikzpicture}
4058 \end{codeexample*}
4059 \TikZ{} shapes cannot simply be used with \Yquant.
4060 Any \Yquant{} shape must be aware of the keys \texttt{x radius} and \texttt{y radius} that control its width and height.
4061 Additionally, \Yquant{} shapes must implement clipping paths.
4062 Those objects, which are a \Yquant{} addition to \TikZ{} allow \Yquant{} to properly clip wires and vertical lines to the shape of the gate.
4063 \Yquant{} draws its elements sequentially; hence, a wire that comes into an operator will be hidden by anything the operator draws on top of it; but outgoing wires will in turn draw on the operator (modulo clipping).
4064 To avoid the issues, we construct an invisible box operator and name it; \emph{outside} of the \texlink{yquant} environment, we \texttt{fit} the special \TikZ{} shape on top of it.
4065 Alternatively, we could also use the \texlink{\enclose} macro to cover the \ttlink{text}; however, this would then be drawn \emph{under} the wires (which we could fix by setting \ttlink{/yquant/enclose/layer} to \texttt{main}).
4066 \end{example}
4067
4068 \paragraph{C. Boxing/Highlighting Parts of a Circuit}\leavevmode
4069 \begin{example}<Updated in 0.9, 0.2>
4070 \begin{codeexample*}
4071 % \usetikzlibrary{quotes}
4072 \begin{tikzpicture}
4073 \begin{yquant*}
4074 h a;
4075 cnot b | a;
4076 \enclose["reversed c-\textsc{not}", solid, inner ysep=3pt] {
4077 h -;
4078 cnot b | a;
4079 h -;
4080 }
4081 cnot b | a;
4082 h b;
4083 \end{yquant*}
4084 \end{tikzpicture}
4085 \end{codeexample*}
4086 Here, we could use subcircuits instead of \texlink{\enclose}, which would double the separation:
4087
4088 \begin{codeexample*}
4089 % \usetikzlibrary{quotes}
4090 \begin{tikzpicture}
4091 \begin{yquant*}
4092 h a;
4093 cnot b | a;
4094 [this subcircuit box style={inner ysep=6pt, "reversed c-\textsc{not}"}]
4095 subcircuit {
4096 qubit {} x;
4097 qubit {} y;
4098 h -;
4099 cnot y | x;
4100 h -;
4101 } (-);
4102 cnot b | a;
4103 h b;
4104 \end{yquant*}
4105 \end{tikzpicture}
4106 \end{codeexample*}
4107 Here, we used the key \ttlink{/yquant/this subcircuit box style} to influence only the style of the subcircuit box itself instead of providing global options that apply to every object in the subcircuit (you wouldn't want the label be assigned to every single gate).
4108 \end{example}
4109
4110 \begin{example}<Updated in 0.9, 0.4>
4111 \begin{codeexample*}
4112 % \usetikzlibrary{quotes}
4113 \begin{tikzpicture}
4114 \begin{yquant*}
4115 h a;
4116 \encloseall[rounded corners, fill=blue!20, "\textsc{swap}" below] {
4117 cnot b | a;
4118 cnot a | b;
4119 cnot b | a;
4120 }
4121 h b;
4122 \end{yquant*}
4123 \end{tikzpicture}
4124 \end{codeexample*}
4125 Since version~0.9, the \texlink{\encloseall} macro is available to comfortably wrap things in boxes.
4126 The default style already draws a dashed box, so here we simply add some complements.
4127 Note that by using \texlink{\encloseall}! instead of \texlink{\enclose}, we make sure that the vertical extent of the box not only encloses all the gates that are in between, but in fact covers the whole height and depth of the involved registers (which here are larger due to the size of the \ttlink[h]{Hadamard} gates).
4128 Further note that the \emph{creation} of registers (without putting an initialization label) is not part of the region: in principle, the register \texttt{b} is lazily created within the \texlink{\encloseall}, but without a label, this is probably not intended to be taken into account, hence \Yquant{} decides to ignore this fact.
4129 \end{example}
4130
4131 \Yquant{} does not support the fancy nearest\hyp neighbor swap gate that \pkg{quantikz} has.
4132 It would however not be very difficult to implement this particular shape and make it available.
4133 Maybe even a multi\hyp swap gate using the \texttt{knots} library would be possible.
4134
4135 \clearpage
4136 \subsubsection{VIII. Otherwise undocumented features}
4137 \begin{example}<Updated in 0.6, 0.4>
4138 \begin{codeexample*}
4139 % \usetikzlibrary{quantikz,fit}
4140 \begin{tikzpicture}
4141 \begin{yquant}[register/default name=]
4142 qubit a;
4143 [name=wave, register/minimum height=5mm, register/minimum depth=5mm]
4144 nobit wave;
4145 qubit b;
4146 qubit c;
4147
4148 h a, b;
4149 box {$U$} c | a;
4150 text {$\dots$} a, b-;
4151 box {$U^k$} c | b;
4152 h a, b;
4153 \end{yquant}
4154 \node[wave, fit=(wave) (current bounding box.east |- wave), inner ysep=.5pt, inner xsep=0pt] {};
4155 \end{tikzpicture}
4156 \end{codeexample*}
4157 Here, we included \pkg{quantikz}, which provides the \texttt{wave} shape, then introduced a register that will contain this wave (and enlarged it sufficiently).
4158 After the circuit is drawn, we \texttt{fit} the wave along.
4159 Since the name assigned to a register without any text actually is of a \texttt{coordinate} shape, we need to enlarge the height of the wave by providing a slightly increased \texttt{inner ysep}.
4160 Additionally, \pkg{quantikz} sets a negative \texttt{inner xsep}, which is probably required for its grid layout; but \Yquant{} positions exactly, so we also need to reset this.
4161 \end{example}
4162
4163 \Yquant{} does not provide a shape corresponding to the ``creating an ebit'' gate.
4164
4165 \clearpage
4166 \subsubsection{X. Troubleshooting}
4167 \begin{example}<Updated in 0.4>
4168 \begin{codeexample*}
4169 \begin{tikzpicture}
4170 \begin{yquant}
4171 qubit {$\ket0$} a[2];
4172 box {$\begin{pmatrix}
4173 \alpha & \beta \\
4174 \beta & -\alpha
4175 \end{pmatrix}$} a[0];
4176 cnot a[1] | a[0];
4177 box {$U_{\The\numexpr\idx+1}$} a;
4178 \end{yquant}
4179 \end{tikzpicture}
4180 \end{codeexample*}
4181 \end{example}
4182 \endgroup
4183
4184 \subsection[\texorpdfstring{\pkg{qpic}}{qpic} documentation]{\compat{Added in 0.6}\pkg{qpic} documentation}
4185 \begingroup
4186 \yquantset{operator/separation=3mm, register/default name=$\reg$, register/default lazy name=$\reg$}%
4187 Again, our section headings will be the same as in the \pkg{qpic} manual.
4188 As the \pkg{qpic} manual uses a bit larger separation between the operators than \Yquant's default, we globally say \tex!\yquantset{operator/separation=3mm, register/default name=$\reg$, register/default lazy name=$\reg$}!, which allows to easily generate all the registers on\hyp the\hyp fly.
4189
4190 \subsubsection{1 Introduction}
4191 \begingroup%
4192 \mdfapptodefinestyle{bdBox}{%
4193 leftmargin=-.12\linewidth,
4194 rightmargin=-.12\linewidth
4195 }%
4196 \begin{example}\label{ex:qupicfirst}
4197 \begin{codeexample*}
4198 \def\reversecircuit#1{%
4199 \begin{tikzpicture}
4200 \let\high=\empty
4201 \listeadd\high{#1}
4202 \def\cnot##1|##2;{%
4203 \ifinlist{##2}\high{
4204 \yquant [style=red] cnot a[##1] | a[##2];
4205 \ifinlist{##1}\high{
4206 \listremove\high{##1}
4207 \yquant addstyle {black} a[##1];
4208 }{
4209 \listadd\high{##1}
4210 \yquant addstyle {red} a[##1];
4211 }
4212 }{
4213 \yquant cnot a[##1] | a[##2];
4214 }
4215 }
4216 \def\cnotA{\cnot 0|1; \cnot 2|3; \cnot 4|5; \cnot 6|7;}
4217 \def\cnotB{\cnot 2|1; \cnot 4|3; \cnot 6|5; \cnot 8|7;}
4218 \def\cnotC{\cnot 1|0; \cnot 3|2; \cnot 5|4; \cnot 7|6;}
4219 \def\cnotD{\cnot 1|2; \cnot 3|4; \cnot 5|6; \cnot 7|8;}
4220 \def\cnotBlock{\cnotA \cnotB barrier (-); \cnotC \cnotD}
4221 \begin{yquant}[operator/minimum width=0pt, register/minimum height=2mm,
4222 register/minimum depth=2mm]
4223 qubit {\Ifnum\idx=#1\color{red}\Fi$\reg_{\The\numexpr\idx+1}$} a[9];
4224 addstyle {very thick, red} a[#1];
4225
4226 \cnotBlock barrier (-);
4227 \cnotBlock barrier (-);
4228 \cnotBlock barrier (-);
4229 \cnotBlock barrier (-);
4230 \cnotBlock
4231 output {\protect\xifinlist{\idx}{\high}{\color{red}}\relax
4232 $a_{\The\numexpr9-\idx}$} -;
4233 \end{yquant}
4234 \end{tikzpicture}%
4235 }
4236 \reversecircuit2
4237 \end{codeexample*}
4238 This is an extremely interesting example, which could have been implemented in a lot of different manners.
4239 We chose an approach where we deferred the logic of coloring the gates entirely to \TeX.
4240 Note that we put everything, including the whole \texttt{tikzpicture} itself, in a macro \tex!\reversecircuit!, which we call directly after its definition by saying \tex!\reversecircuit2!.
4241 This is of course an overkill in this situation---there is no need for the macro definition.
4242 However, note that the macro expects the wire that is to be colored in red as its argument.
4243 So by slightly changing the invocation to
4244 \begin{minted}{tex}
4245 \foreach \ici in {0, ..., 8} {
4246 \reversecircuit\ici
4247 \par\vspace{1cm}
4248 }
4249 \end{minted}
4250 we are able to render the circuit with all different initial wires one after the other very easily.
4251 We do not show the output in the manual to keep it succinct, but just try it out by yourself.
4252
4253 We now explain what is done in the macro.
4254
4255 We first define an \pkg{etoolbox} list that is stored in \tex!\high!.
4256 The idea is that this list holds at any point in time the indices of all the registers that are currently colored in red.
4257 Initially, we add the index that was given as a parameter to the macro---in our case, this was \tex!2!.
4258 Note we use \tex!\listeadd! instead of \tex!\listadd!, which is important for the invocation via \tex!\foreach!---we want to have the number in the list, not the macro \tex!\ici! that holds the index of the initially colored wire).
4259
4260 Next, we do not want to manually do the bookkeeping of this list.
4261 All we want to do is to issue the command to put a \ttlink{cnot} gate on the appropriate registers and \TeX{} should keep track of the correct coloring and register state.
4262 For this, we first define a macro \tex!\cnot! that expects the index of the target and the index of the control register.
4263 Within this macro, we check whether the register of the control is currently highlighted (\tex!\ifinlist{#2}\high!).
4264 If this is not the case, we draw the \ttlink{cnot} gate without any additional styles (note that since we interrupted the \Yquant{} parser due to the lots of intervening macros, we first have to restart it saying \texlink\yquant).
4265 However, if it is the case that the control register is currently highlighted, we draw the \ttlink{cnot} gate with the attribute \texttt{[style=red]}---we do not only want to draw the gate itself in red (for which \texttt{[red]} would be sufficient), but also its control line and the control blob, so we use the \ttlink{/yquant/style} shorthand.
4266 Note that in the \pkg{qpic} manual, some of the control lines are thicker than others.
4267 This could be implemented by adding the argument \tex!every control line/.append style={very thick}!; however, as it is unclear what the thick line should indicate, we did not add this to the example.
4268
4269 Then, we have to change the state of the target register appropriately, since the highlighting state will propagate from control to target.
4270 If the target register was already highlighted, we have to remove it from the list and we change its line color back to \texttt{black}; if it was not highlighted, we add it to the list and change its line color to \texttt{red}.
4271 Note that the use of \ttlink{addstyle} will keep adding styles, so in the end, the register line style will be a long string \texttt{red,black,red,black,...}; we could do better by saying \ttlink{setstyle}, which would overwrite the line style.
4272 However, since we will initially set the line width of \tex!a[#1]! to \texttt{very thick}---which should be kept throughout the circuit---we would have to take additional care not to lose this setting.
4273 Here, we chose the simpler version.
4274
4275 After setting the coloring preliminaries, we note that if we slice the circuit at any time, it will have four possible gate configurations (or a \ttlink{barrier}).
4276 We define abbreviations for these in the macros \tex!\cnotA! to \tex!\cnotD!.
4277 Since they will always follow in this order, we also define a \tex!\cnotBlock! abbreviation that executes these configurations together with their intermediate \ttlink{barrier}.
4278 We do not include the final barrier, since it is not present in the last block.
4279
4280 Now, we start the \texlink{yquant} environment.
4281 We give some options for a nicer spacing and initialize the registers.
4282 In the \pkg{qpic} example, the registers are $1$\hyp indexed, so we use \tex!\The\numexpr\idx+1! to give back the value of the current register index (\texlink\idx) plus one; we also conditionally color the register name in red if the index coincides with the parameter.
4283 Note that here we use \Yquant's shorthand for \tex!\protect\the!, \tex!\protect\ifnum!, and \tex!\protect\fi!, which are \texlink\The, \texlink\Ifnum, and \texlink\Fi, to get the correct expansion behavior.
4284
4285 Then, we add the initial style for the \tex!a[#1]! wire.
4286 Inserting the gates together with the correct coloring is now extremely simple: we just need to call our \tex!\cnotBlock! command and intersperse it with \ttlink{barrier}s.
4287 At the end, we output all the gates in reverse order, which works similarly to the initialization of the gates, and also conditionally color the reversed register.
4288 This coloring could in principle be done similarly to the coloring of the initial label, saying something like \tex!\Ifnum\numexpr8-\idx=#1 \color{red}\Fi!.
4289 Here, we chose the ``more honest'' approach to color all the registers that are still present in the coloring list---note the need to \texlink{\protect} the \pkg{etoolbox} macro \tex!\xifinlist!.
4290 In this way, we could, e.g., terminate the circuit earlier and still get the correct output coloring at this particular point.
4291
4292 Note that if there were more that just five blocks, we could also have made use of \TikZ's \tex!\foreach! loop to output all the \tex!\cnotBlock! commands.
4293 However, be aware of the fact that \tex!\foreach! puts its content in a group, so the \tex!\high! list assignments would have been local and forgotten in the next iteration.
4294 Either they would need to be made globally or some non\hyp grouping loop construct would have to be used (e.g., \tex!\pgfplotsforeachungrouped!).
4295 \end{example}
4296 \endgroup
4297
4298 \clearpage
4299 \subsubsection{2 Simple Examples}
4300 \paragraph{2.1 Example 1: Majority}\leavevmode
4301 \begin{example}
4302 \begin{codeexample*}
4303 \begin{tikzpicture}
4304 \begin{yquant*}
4305 box {\rotatebox{90}{$\operatorname{MAJ}$}} (a, b, c);
4306
4307 text {$=$} (-);
4308
4309 cnot b | c;
4310 cnot a | c;
4311 cnot c | a, b;
4312
4313 output {$a \oplus c$} a;
4314 output {$b \oplus c$} b;
4315 output {$\operatorname{MAJ}(a, b, c)$} c;
4316 \end{yquant*}
4317 \end{tikzpicture}
4318 \end{codeexample*}
4319 \end{example}
4320
4321 \clearpage
4322 \paragraph{2.2 Example 2: Quantum Fourier Transform}\leavevmode
4323 \begin{example}
4324 \begin{codeexample*}
4325 \begin{tikzpicture}
4326 \begin{yquant}[operators/every box/.append style={shape=yquant-circle, radius=2.5mm}]
4327 qubit {$x_2$} x2;
4328 qubit {$x_1$} x1;
4329 qubit {$x_0$} x0;
4330
4331 h x2;
4332 box {$2$} x2 | x1;
4333 box {$3$} x2 | x0;
4334 h x1;
4335 box {$2$} x1 | x0;
4336 h x0;
4337
4338 output {$\frac{1}{\sqrt2} (\ket0 + e^{2\pi i \cdot 0.x_2 x_1 x_0} \ket1)$} x2;
4339 output {$\frac{1}{\sqrt2} (\ket0 + e^{2\pi i \cdot 0.x_1 x_0} \ket1)$} x1;
4340 output {$\frac{1}{\sqrt2} (\ket0 + e^{2\pi i \cdot 0.x_0} \ket1)$} x0;
4341 \end{yquant}
4342 \end{tikzpicture}
4343 \end{codeexample*}
4344 In this example, we opted to use three distinct registers instead of one vector register, since the reversed indexing would probably have led to more confusion.
4345 We globally overwrite the \ttlink{/yquant/operators/every box} style to use a circular shape instead.
4346 Note that this would usually be an ellipse, so we explicitly set the radius to a value that exceeds the minimum (half) width.
4347 As of version~0.6, the style \ttlink{/yquant/operators/every box} is no longer the base style for other rectangular boxes such as \ttlink{h} (note this requires a compatibility version of at least \texttt{0.6} or higher).
4348 For earlier versions, the change in shape would also affect the Hadamard gate and therefore would have to be reverted using the \ttlink{/yquant/operators/every h} style.
4349 \end{example}
4350
4351 \clearpage
4352 \paragraph{2.3 Example 3: Shor's Algorithm}\leavevmode
4353 \begingroup%
4354 \mdfapptodefinestyle{bdBox}{%
4355 leftmargin=-.04\linewidth,
4356 rightmargin=-.02\linewidth
4357 }%
4358 \begin{example}<Updated in 0.9, 0.7>
4359 \begin{codeexample*}
4360 \long\def\explain#1#2#3{%
4361 \encloseall[brace left={#1}, brace right={$\displaystyle#2$}] { #3 }
4362 }
4363 \begin{tikzpicture}
4364 \begin{yquant}[vertical,
4365 every control/.append style={radius=1.5mm},
4366 operators/every slash/.append style={x radius=3mm, y radius=1.5mm},
4367 operators/every box/.append style={y radius=4mm},
4368 operators/every measure/.append style={y radius=4mm},
4369 operator/minimum extent=1.32cm,
4370 enclose/every brace/.style={}]
4371 qubit {$\ket{0\dotsm0}$} x; slash x;
4372 qubit {$\ket{0\dotsm0}$} y; slash y;
4373
4374 \explain{Quantum Hadamard Transform}
4375 {\sum_{x = 0}^{2^n -1} \ket x \ket 0}{
4376 box {$H^{\otimes n}$} x;
4377 }
4378
4379 \explain{Exponentiation}
4380 {\sum_{x = 0}^{2^n -1} \ket x \ket{a^x \bmod N}}{
4381 box {$a^x \bmod N$} y | x;
4382 }
4383
4384 \explain{Measure $y = a^b$}
4385 {\sum_{j = 0}^{\lfloor\frac{2^n -1}{r}\rfloor} \ket{b + j r} [y]}{
4386 measure y;
4387 output {$y$} y;
4388 }
4389
4390 \explain{Quantum Fourier Transform}
4391 {\sum_{x = 0}^{2^n -1} \left( \sum_{j = 0}^{\lfloor\frac{2^n -1}{r}\rfloor}
4392 \omega^{x * (b + j r)} \right) \ket x}
4393 {
4394 box {QFT} x;
4395 }
4396
4397 output {$?$} x;
4398 \end{yquant}
4399 \end{tikzpicture}
4400 \end{codeexample*}
4401 Once again, this is a very interesting example.
4402 The circuit itself is very standard (though it is the first one in vertical mode).
4403 In order to output the explanations, we basically just use the \texlink{\encloseall} macro with the \ttlink{/yquant/enclose/brace left} and \ttlink{/yquant/enclose/brace right} options, and since we need it so often, we define a macro shorthand for this.
4404 However, here, the explanations should be drawn without and kind of brace---so we simply empty the \ttlink{/yquant/enclose/every brace}, effectively removing the brace completely.
4405
4406 Note that the code example above would work exactly in this way in traditional \LaTeX{} documents; however, this document is set with \pkg{unicode-math}.
4407 This leads to a problem when using \tex!\bmod!, which has to be \texlink\protect ed---or, as we did here, just say \tex!\robustify\bmod! somewhere before its usage and after loading all the packages.
4408 \end{example}
4409 \endgroup
4410
4411 \paragraph{2.4 Example 4: Teleportation}
4412 \begin{example}<Updated in 0.9, 0.7>
4413 \begin{codeexample*}
4414 \begin{tikzpicture}
4415 \begin{yquant}[vertical]
4416 qubit {\color{red}$\ket\phi$} q;
4417 qubit {$\ket0$} q[+2];
4418 setstyle {red} q[0];
4419
4420 \encloseall[brace right=Alice and Bob form an\\entangled pair of qubits] {
4421 h q[1];
4422 cnot q[2] | q[1];
4423 }
4424 setstyle {red} q[1];
4425 setstyle {blue} q[2];
4426 barrier (q);
4427
4428 \encloseall[bracing style=red, brace left=Alice entangles\\and measures] {
4429 [style=red, operator/separation=0pt] cnot q[1] | q[0];
4430 [red] h q[0];
4431 [red] measure q[0, 1];
4432 }
4433
4434 \encloseall[bracing style=blue, brace right=Bob applies gates based on\\Alice's measurements] {
4435 [style=blue] x q[2] | q[1];
4436 [style=blue] z q[2] | q[0];
4437 discard q[0, 1];
4438 }
4439
4440 output {\color{blue}$\ket\phi$} q[2];
4441 \end{yquant}
4442 \end{tikzpicture}
4443 \end{codeexample*}
4444 This example shows a different way of providing explanations along the circuit, which this time also works for hints encompassing more than a single gate.
4445 Since version~0.9, \Yquant{} provides the \texlink{\encloseall} macro, which is used for highlighting a certain part of the circuit.
4446 The \ttlink{/yquant/enclose/brace left} and \ttlink{/yquant/enclose/brace right} styles modify the enclosing action to cover the full register range and put the description at the outside.
4447 \end{example}
4448
4449 \clearpage
4450 \subsubsection{3.1 Wires}
4451 \paragraph{3.1.1 Wire Declarations}
4452 \begin{example}
4453 \begin{codeexample}
4454 \begin{tikzpicture}
4455 \begin{yquant}
4456 qubit a;
4457 qubit b;
4458 qubit {} c;
4459
4460 hspace {1cm} -;
4461
4462 output {$\phi$} a;
4463 \end{yquant}
4464 \end{tikzpicture}
4465 \end{codeexample}
4466 \end{example}
4467
4468 \begin{example}
4469 \begin{codeexample}
4470 \makeatletter
4471 % https://tex.stackexchange.com/a/112212/32357
4472 \DeclareRobustCommand\rvdots{%
4473 \vbox{%
4474 \baselineskip4\p@\lineskiplimit\z@%
4475 \kern-\p@%
4476 \hbox{.}\hbox{.}\hbox{.}%
4477 }%
4478 }
4479 \begin{tikzpicture}
4480 \begin{yquant}[every nobit output/.style={}, register/separation=3mm]
4481 qubit {$x_1$} x;
4482 qubit {$\rvdots$} x[+1]; discard x[1];
4483 qubit {$x_n$} x[+1];
4484
4485 hspace {1cm} -;
4486
4487 output {$y_1$} x[0];
4488 output {$\rvdots$} x[1];
4489 output {$y_n$} x[2];
4490 \end{yquant}
4491 \end{tikzpicture}
4492 \end{codeexample}
4493 This is one of at least four possible implementations (note we \href{https://tex.stackexchange.com/a/112212/32357}{defined \tex!\rvdots!}, since the native \tex!\vdots! does not appear to be very well\hyp centered).
4494 It declares the ``invisible'' register as part of the vector register \yquant!x!.
4495 As a consequence, whenever the whole vector register is addressed in operations, the operation is also drawn on the invisible register.
4496 For multi\hyp register gates, this may be desired (they just span the whole region), for single\hyp register gates, this is most certainly undesired.
4497 Note that \Yquant{} does not allow to declare an \ttlink{output} gate for invisible registers---usually, this does not make sense.
4498 However, this is not enforced as a hard constraint, but rather due to the fact that the style \texttt{/yquant/every nobit output} does not exist.
4499 To prevent an error message, we just define this as an empty style.
4500 Also note that, to get a proper vertical spacing, we decided to use the \ttlink{/yquant/register/separation} key---which works well, as there are no other registers.
4501 If there were others, it would be better to increase the height and depth of the invisible register.
4502
4503 Alternatively, we might declare the invisible register with a completely different name.
4504 This would create a discontiguous vector register \yquant!x!, which is probably the better thing to do for single\hyp register gates.
4505 However, \Yquant{} may now try to split multi\hyp register gates into contiguous slices---there could be arbitrary registers between discontiguous parts of a vector register, and they should of course not be targeted if they are not in the list of targets of a gate---but here, we would actually want to have this.
4506
4507 A third approach mixes \Yquant{} and \TikZ{} code.
4508 We declare a vector register with size two, manually increase, say, the depth of the first register, and put a \tex!\node! at the appropriate position by naming the initial labels.
4509 In this way, vector usage will never target the ``invisible'' line---since it does not exist; both single\hyp{} and multi\hyp register gates will work appropriately.
4510 As a drawback, we need to decide whether we want to enlarge the depth of the first or the height of the second register (or both, splitting in half)---but what if some gates will actually be so large that they would provide enough of height or depth had we just chosen a different way of distribution the space?
4511
4512 In order to remediate this, a fourth way using only two registers would be to defer the drawing of the dots to a multi\hyp register \ttlink{text} gate which receives a \texttt{y~radius} that enforces an appropriate separation.
4513 \Yquant{} will then automatically perfectly distribute the vertical extents among height and depth of the involved registers.
4514 The drawback with this approach is of course that the dots will be drawn \emph{within} the circuit, not to the left.
4515 There is an undocumented option that we can use to shift the gate to the left; but since this only works for initializers, we still need to draw the dots for the outputs manually.
4516 A possible implementation could look as follows.
4517
4518 \begingroup%
4519 \makeatletter%
4520 \DeclareRobustCommand\rvdots{%
4521 \vbox{%
4522 \baselineskip4\p@\lineskiplimit\z@%
4523 \kern-\p@%
4524 \hbox{.}\hbox{.}\hbox{.}%
4525 }%
4526 }%
4527 \begin{codeexample}
4528 % \rvdots definition from above
4529 \begin{tikzpicture}
4530 \begin{yquant}
4531 qubit {$x_1$} x;
4532 qubit {$x_n$} x[+1];
4533 [internal/move label, anchor=east, y radius=8mm]
4534 text {$\rvdots$} (x);
4535
4536 hspace {1cm} -;
4537
4538 [name=o1]
4539 output {$y_1$} x[0];
4540 [name=o2]
4541 output {$y_n$} x[1];
4542 \end{yquant}
4543 \path (o1.south west) -- (o2.north west) node[midway, /yquant/every output] {$\rvdots$};
4544 \end{tikzpicture}
4545 \end{codeexample}
4546 \endgroup%
4547 \end{example}
4548
4549 \begin{example}
4550 \begin{codeexample}
4551 \begin{tikzpicture}
4552 \begin{yquant*}
4553 [decorate=false, draw=none]
4554 init {$A$} (q[0, 1]);
4555 init {$\ket0^{\otimes3}$} (q[2-4]);
4556
4557 hspace {1cm} -;
4558
4559 output {$B$} (q[1, 2]);
4560 output {$\ket0^{\otimes2}$} (q[3, 4]);
4561 \end{yquant*}
4562 \end{tikzpicture}
4563 \end{codeexample}
4564 Note that here the $A$ is drawn without the curly braces.
4565 There are two simple ways to achieve this: by setting \texttt{draw} to \texttt{none}, the curly brace is suppressed, but still the~$A$ would be drawn at the same position as if the brace were there.
4566 We additionally set \texttt{decorate} to \texttt{false} to fully remove any reminiscence of the brace, so that the text is closest to the wires.
4567 (Note that just removing the decoration without also removing the drawing would lead to a vertical line that connects all the affected wires---this is how the \texlink{yquant-init} shape looks like in an undecorated fashion.)
4568 \end{example}
4569
4570 \clearpage
4571 \subsubsection{3.2 Gates}
4572 \paragraph{3.2.1 Controlled NOT and controlled \texorpdfstring{$Z$}{Z}}
4573 \begin{example}
4574 \begin{codeexample}
4575 \begin{tikzpicture}
4576 \begin{yquant*}
4577 not a;
4578 \end{yquant*}
4579 \end{tikzpicture}
4580 \end{codeexample}
4581 \end{example}
4582
4583 \begin{example}
4584 \begin{codeexample}
4585 \begin{tikzpicture}
4586 \begin{yquant*}[register/default lazy name=$\symbol{\numexpr`a+\idx}$]
4587 cnot q[1] | q[0];
4588 \end{yquant*}
4589 \end{tikzpicture}
4590 \end{codeexample}
4591 Since in \Yquant's notation, \texttt{b} is mentioned before \texttt{a}, it would also be created as the first wire.
4592 If we instead resort to vector registers, we can directly specify which position our registers should have.
4593 Of course, for longer circuits, an explicit declaration is probably favorable.
4594 \end{example}
4595
4596 \begin{example}
4597 \begin{codeexample}
4598 \begin{tikzpicture}
4599 \begin{yquant*}[register/default lazy name=$\symbol{\numexpr`a+\idx}$]
4600 cnot q[2] | q[-1];
4601 \end{yquant*}
4602 \end{tikzpicture}
4603 \end{codeexample}
4604 \end{example}
4605
4606 \begin{example}
4607 \begin{codeexample}
4608 \begin{tikzpicture}
4609 \begin{yquant*}
4610 not a;
4611 zz (b, c);
4612 cnot c | a ~ b;
4613 \end{yquant*}
4614 \end{tikzpicture}
4615 \end{codeexample}
4616 \end{example}
4617
4618 \clearpage
4619 \paragraph{3.2.2 General Gates}
4620 \begin{example}
4621 \begin{codeexample}
4622 \begin{tikzpicture}
4623 \begin{yquant*}
4624 box {\symbol{\numexpr`A+\idx}} a, b;
4625 box {$\tau$} (-);
4626 box {GATE} a | b;
4627 \end{yquant*}
4628 \end{tikzpicture}
4629 \end{codeexample}
4630 Note that the macro \texlink{\idx} is available in any gate, and it gives the index of the current register within the target list.
4631 \end{example}
4632
4633 \begin{example}
4634 \begin{codeexample}
4635 \begin{tikzpicture}
4636 \begin{yquant*}[operators/every box/.append style={shape=yquant-circle}]
4637 box {$\rho$} a;
4638 box {$\rho$} b | a;
4639 box {$\rho$} (-);
4640 \end{yquant*}
4641 \end{tikzpicture}
4642 \end{codeexample}
4643 Note that the macro \texlink{\idx} is available in any gate, and it gives the index of the current register within the target list.
4644 \end{example}
4645
4646 \begin{example}
4647 \begin{codeexample}
4648 \begin{tikzpicture}
4649 \begin{yquant}[vertical=-90]
4650 qubit a; qubit b; qubit c;
4651 box {$M$} (c, b) | a;
4652 box {GATE} (-);
4653 box {$M^{-1}$} (c, b) | a;
4654 \end{yquant}
4655 \end{tikzpicture}
4656 \end{codeexample}
4657 Here, we use the automatic rotation feature that the \ttlink{/yquant/vertical} style provides.
4658 \end{example}
4659
4660 \begin{example}
4661 \begin{codeexample}
4662 \begin{tikzpicture}
4663 \begin{yquant*}
4664 [name=fs] box {\Ifnum\idx<1 $f^{-1}$\Else $f$\Fi} (a, b), (c, d);
4665 [name=g] box {$g$} (b, c) | a;
4666 [shape=yquant-circle, name=theta] box {$\theta$} d;
4667 \end{yquant*}
4668 \draw (fs-0) -- (fs-1) (g) -- (theta);
4669 \end{tikzpicture}
4670 \end{codeexample}
4671 By putting the two $f$\hyp boxes into a single gate, we ensured that \Yquant{} will center them with respect to each other.
4672 \end{example}
4673
4674 \paragraph{3.2.3 Other predefined Gates}
4675 \begin{example}
4676 \begin{codeexample}
4677 \begin{tikzpicture}
4678 \begin{yquant*}
4679 h a;
4680 h x;
4681 \end{yquant*}
4682 \end{tikzpicture}
4683 \end{codeexample}
4684 \end{example}
4685
4686 \begin{example}
4687 \begin{codeexample}
4688 \begin{tikzpicture}
4689 \begin{yquant*}
4690 z a, b;
4691 zz (-);
4692 z a | b;
4693 \end{yquant*}
4694 \end{tikzpicture}
4695 \end{codeexample}
4696 \end{example}
4697
4698 \begin{example}
4699 \begin{codeexample}
4700 \begin{tikzpicture}
4701 \begin{yquant*}
4702 zz (a, b), (c, d);
4703 zz (b, c);
4704 zz (-b), (c-);
4705 zz (b, c);
4706 \end{yquant*}
4707 \end{tikzpicture}
4708 \end{codeexample}
4709 \end{example}
4710
4711 \begin{example}
4712 \begin{codeexample}
4713 \begin{tikzpicture}
4714 \begin{yquant*}
4715 swap (a, b);
4716 \end{yquant*}
4717 \end{tikzpicture}
4718 \end{codeexample}
4719 \end{example}
4720
4721 \begin{example}
4722 \begin{codeexample*}
4723 \begin{tikzpicture}
4724 \begin{yquant*}[plusctrl/.style={/yquant/every control/.style={/yquant/operators/every not}, /yquant/every positive control/.style={}}]
4725 [plusctrl] box {$f$} (a, b) | c;
4726 [plusctrl, shape=yquant-circle] box {$\zeta$} c | d;
4727 [plusctrl] box {$f$} (a, b) | c;
4728 cnot b | a;
4729 cnot c | d;
4730 \end{yquant*}
4731 \end{tikzpicture}
4732 \end{codeexample*}
4733 This very unorthodox\hyp looking style can be achieved by altering the control styles in such a way that it basically looks like a \ttlink{not} gate.
4734 \end{example}
4735
4736 \subsubsection{3.3 Attributes}
4737 \paragraph{Size Attributes}
4738 \begin{example}<Changed in 0.7>
4739 \begin{codeexample*}
4740 \begin{tikzpicture}
4741 \begin{yquant*}
4742 [y radius=20pt] box {HIGH} (a, b);
4743 [x radius=20pt] box {WIDE} (-);
4744 [time radius=20pt] box {LONG} (-);
4745 [space radius=20pt] box {BROAD} (-);
4746 \end{yquant*}
4747 \end{tikzpicture}
4748 \end{codeexample*}
4749 In horizontal mode, \texttt{time radius} is a synonym for \texttt{x radius}, while \texttt{space radius} is a synonym for \texttt{y radius}.
4750 \end{example}
4751
4752 \begin{example}<Changed in 0.7>
4753 \begin{codeexample}
4754 \begin{tikzpicture}
4755 \begin{yquant*}[vertical=45]
4756 [y radius=20pt] box {HIGH} (a, b);
4757 [x radius=20pt] box {WIDE} (-);
4758 [time radius=20pt] box {LONG} (-);
4759 [space radius=20pt] box {BROAD} (-);
4760 \end{yquant*}
4761 \end{tikzpicture}
4762 \end{codeexample}
4763 \end{example}
4764
4765 \begin{example}
4766 \begin{codeexample}
4767 \begin{tikzpicture}
4768 \begin{yquant}
4769 [register/minimum depth=10pt]
4770 qubit a;
4771 qubit b;
4772 [register/minimum height=1pt]
4773 qubit c;
4774
4775 cnot c | a, b;
4776 \end{yquant}
4777 \end{tikzpicture}
4778 \end{codeexample}
4779 Upon creation, the minimum register sizes can be passed on to \Yquant; note that the \ttlink{/yquant/register/minimum height} extends from the wire line to the top of the space that is allocated for the wire, whereas the corresponding key \ttlink{/yquant/register/minimum depth} extends from the wire line to the bottom.
4780 Hence, the values given here are half of \pkg{qpic}'s.
4781 \end{example}
4782
4783 \begin{example}
4784 \begin{codeexample}
4785 \begin{tikzpicture}
4786 \begin{yquant*}
4787 [inner sep=0pt, radius=2.5pt]
4788 box {} a | b;
4789 [shape=yquant-circle, radius=10pt]
4790 box {$g$} b | a;
4791 cnot a | b;
4792 [operator style={radius=7.5pt}, control style={radius=4pt}]
4793 cnot a | b;
4794 \end{yquant*}
4795 \end{tikzpicture}
4796 \end{codeexample}
4797 To mimick closely \pkg{qpic}'s manual, we used an empty \ttlink{box} instead of the \ttlink{xx} gate, which also is a rectangle.
4798 Note that the shapes that accept text also have an inner separation, which would interfere with the \texttt{radius} setting.
4799 \end{example}
4800
4801 \begin{example}
4802 \begin{codeexample}
4803 \begin{tikzpicture}
4804 \begin{yquant*}
4805 [x radius=12.5pt]
4806 box {$f$} a;
4807 [radius=12.pt, shape=yquant-circle]
4808 box {$f$} a;
4809 \end{yquant*}
4810 \end{tikzpicture}
4811 \end{codeexample}
4812 Note that when changing to the \texttt{yquant-circle} shape, this will become an ellipse if only one of the radii is modified.
4813 \end{example}
4814
4815 \begin{example}
4816 \begin{codeexample}
4817 \begin{tikzpicture}
4818 \begin{yquant}
4819 qubit {\color{purple}$a$} a;
4820 setstyle {purple} a;
4821 qubit b;
4822
4823 [style=green!50!black]
4824 cnot a | b;
4825 setstyle {red} b;
4826 [orange, control style=blue]
4827 h a | b;
4828 \end{yquant}
4829 \end{tikzpicture}
4830 \end{codeexample}
4831 \end{example}
4832
4833 \begin{example}
4834 \begin{codeexample}
4835 \yquantdefinebox{circle}[shape=yquant-circle, draw, inner sep=0pt, radius=2mm]{}
4836 \begin{tikzpicture}
4837 \begin{yquant*}
4838 [fill=red!50!white] box {$f$} a | b;
4839 [fill=blue, name=b] circle a;
4840 [fill=green, name=g] circle b;
4841 not a;
4842 [fill=yellow] not b;
4843 \end{yquant*}
4844 \draw (b) -- (g);
4845 \end{tikzpicture}
4846 \end{codeexample}
4847 \end{example}
4848
4849 \begin{example}
4850 \begin{codeexample}
4851 \begin{tikzpicture}
4852 \begin{yquant*}
4853 setstyle {dotted} a;
4854 setstyle {very thick} b;
4855 [dashed, fill=yellow] box {$G$} (-);
4856 cnot a | b;
4857 setstyle {densely dotted} a;
4858 \end{yquant*}
4859 \end{tikzpicture}
4860 \end{codeexample}
4861 \end{example}
4862
4863 \begin{example}
4864 \begin{codeexample}
4865 \begin{tikzpicture}[on/.style={red, very thick}]
4866 \begin{yquant*}[on/.style={style=red, control style={very thick}}]
4867 cnot a | b;
4868 qubit {\color{red}$c$} c;
4869 setstyle {on} c;
4870 [on] cnot b | c;
4871 setstyle {on} b;
4872 [on] cnot a | b;
4873 setstyle {on} a;
4874 \end{yquant*}
4875 \end{tikzpicture}
4876 \end{codeexample}
4877 This example demonstrates for the first time that new registers can also be declared at any later time in the circuit.
4878 Note that we defined two very different styles:
4879 \begin{itemize}
4880 \item \texttt{/tikz/on} for the \texttt{tikzpicture} \\
4881 This is an ordinary \TikZ{} style and hence will be applied whenever it is used in a styling context---for example, when added to the wire styles.
4882 \item \texttt{/yquant/on} for the \texttt{yquant*} environment \\
4883 This is a style that does not directly apply any styling, but it instead passes options to \ttlink{/yquant/style} (we want to have the gates as well as their controls and control lines in red) as well as to \ttlink{/yquant/control style} (we want to draw the control lines thicker [in principle, this would also affect the controls, but they are filled, not drawn], but we don't want to draw the lines of the \ttlink{cnot} gates themselves be drawn thicker.).
4884 Since attributes for gates will first look in the \texttt{/yquant} namespace, this style is applied when used as an attribute for a gate (but beware that \yquant![style=on]! would call the other style).
4885 \end{itemize}
4886 \end{example}
4887
4888 \begin{example}
4889 \begin{codeexample}
4890 \begin{tikzpicture}
4891 \begin{yquant}[register/default name=$\reg$]
4892 cbit a;
4893 qubit b;
4894 h b | a;
4895 discard a;
4896 \end{yquant}
4897 \end{tikzpicture}
4898 \end{codeexample}
4899 \end{example}
4900
4901 \Yquant{} does not offer the variety of shapes that \pkg{qpic} does; please file a feature request if there is a need.
4902
4903 \begin{example}
4904 \begin{codeexample}
4905 \begin{tikzpicture}
4906 \begin{yquant}
4907 qubit a;
4908 qubit b;
4909 [shape=yquant-circle, radius=1.2mm, inner sep=0pt]
4910 box {$\cdot$} b | a;
4911 box {$A$} (-);
4912 \end{yquant}
4913 \end{tikzpicture}
4914 \end{codeexample}
4915 \end{example}
4916
4917 \clearpage
4918 \begin{example}
4919 \begin{codeexample*}
4920 % \usepackage[hidelinks]{hyperref}
4921 % \usetikzlibrary{calc}
4922 \makeatletter
4923 \def\tikzHyperNode#1{%
4924 \ifdefined\tikz@alias%
4925 \unless\ifyquantmeasuring%
4926 \pgfqkeysalso{/tikz}{%
4927 % https://tex.stackexchange.com/a/36111/32357
4928 alias=sourcenode,
4929 append after command={
4930 % we don't need to worry about outer sep, yquant shapes ignore this value
4931 let \p1=(sourcenode.north west),
4932 \p2=(sourcenode.south east),
4933 \n1={\x2-\x1},
4934 \n2={\y1-\y2} in
4935 node [inner sep=0pt, outer sep=0pt, anchor=north west, at=(\p1)]
4936 {#1{\XeTeXLinkBox{\phantom{\rule{\n1}{\n2}}}}}
4937 }%
4938 }%
4939 \fi%
4940 \fi%
4941 }
4942 \tikzset{
4943 hyperlink/.code={\tikzHyperNode{\hyperlink{#1}}},
4944 hyperref/.code={\tikzHyperNode{\hyperref[{#1}]}}
4945 }
4946
4947 \begin{tikzpicture}
4948 \begin{yquant}
4949 qubit a;
4950 qubit b;
4951 [hyperref=sec:grammar]
4952 box {SUB} (a-b);
4953 \end{yquant}
4954 \end{tikzpicture}
4955 \end{codeexample*}
4956 In principle, adding a hyperlink to any gate works as adding a hyperlink to any \TikZ{} node.
4957 We use a standard implementation; because of the way \Yquant{} internally handles styles applied to a gate, we must protect this in the \tex!\ifdefined\tikz@alias! (\Yquant{} executes the options outside of nodes first to set, e.g., line styles appropriately, which leads to an error, as the \texttt{alias} option only works within nodes).
4958 We also want to refrain from unnecessarily adding hyperlinks during the initial measurement phase.
4959 We then provide two \TikZ{} styles to do the job, depending on whether the link should be created via \tex!\hyperlink! or \tex!\hyperref! and apply it.
4960 Note that here, we explicitly created the registers first.
4961 Had we used an implicit creation, we would also have applied to hyperref to the register labels!
4962
4963 Be aware of the fact that hyperlinks in PDFs will always be rectangular; if your gate shape is different from this, do not expect the shapes to match.
4964 \end{example}
4965
4966 \subsubsection{3.4 Measurement and Other Wire Type Changes}
4967 \begin{example}
4968 \begin{codeexample}
4969 \begin{tikzpicture}
4970 \begin{yquant*}
4971 measure a;
4972 cnot b | a;
4973 dmeter {$Z$} b;
4974 \end{yquant*}
4975 \end{tikzpicture}
4976 \end{codeexample}
4977 \end{example}
4978
4979 \Yquant{} does not support the \texttt{tag} shape.
4980
4981 \begin{example}
4982 \begin{codeexample}
4983 \begin{tikzpicture}
4984 \begin{yquant*}
4985 [direct control] measure a;
4986 cnot b | a;
4987 dmeter {$Z$} b;
4988 \end{yquant*}
4989 \end{tikzpicture}
4990 \end{codeexample}
4991 \end{example}
4992
4993 \begin{example}
4994 \begin{codeexample}
4995 \begin{tikzpicture}
4996 \begin{yquant*}[operator/separation=2mm]
4997 setstyle {-|, shorten >= 3mm} a;
4998 inspect {$0$} a;
4999 discard a;
5000
5001 hspace {1cm} -;
5002
5003 setstyle {|-, shorten <= 2mm} a;
5004 init {$1$} a;
5005 \end{yquant*}
5006 \end{tikzpicture}
5007 \end{codeexample}
5008 There are no gates in \Yquant{} that resemble the visual \ttlink{discard} or reinitialization marker; however, this can be achieved by placing appropriate arrowheads at the wires.
5009 Still, this is a problematic solution: Every wire in \Yquant{} will extend from the center of one gate to the center of the next gate; protruding parts will be clipped away.
5010 Hence, the arrowhead will not be visible, as it is below the \ttlink{inspect} or \ttlink{init} gate---so we must \texttt{shorten} the wire by an ``appropriate'' amount.
5011 Additionally, if the circuit were longer, we would want to quickly get rid of this arrowhead style.
5012 \Yquant{} will try to make the wire lines as long as possible---i.e., in a normal circuit without any changes, the wire will in fact be one continuous line from the left to the right.
5013 However, whenever something changes at the wire---say, the style or type is changed---\Yquant{} needs to start a new path.
5014 We don't want the arrowheads to still be installed on this new path, hence we would quickly need to remove them.
5015 \end{example}
5016
5017 \begin{example}
5018 \begin{codeexample}
5019 \begin{tikzpicture}
5020 \begin{yquant}
5021 qubit a;
5022 hspace {5mm} a;
5023 [after=a] qubit b;
5024 box {$f$} (a, b);
5025 inspect {$A$} a;
5026 [after=a] qubit c;
5027 discard a;
5028 box {$g$} (b, c);
5029 align -;
5030 init {$a'$} a;
5031 inspect {$B$} b;
5032 discard b;
5033 box {$h$} (-);
5034 output {$A'$} a;
5035 output {$C'$} c;
5036 \end{yquant}
5037 \end{tikzpicture}
5038 \end{codeexample}
5039 Here, we create registers with the \ttlink[{[after]}]{after} attribute at some later point in the circuit.
5040 Note that logically speaking, the $h$~box should have had the targets \yquant!(a, c)!; however, as \Yquant{} does not know that the middle register was already discarded, it would have drawn two boxes joined by a wiggly line to indicate the discontiguous multi\hyp qubit register.
5041 \end{example}
5042
5043 \subsubsection{3.5 Managing Slices}
5044 \begin{example}
5045 \begin{codeexample}
5046 \begin{tikzpicture}
5047 \begin{yquant}
5048 qubit a; qubit b; qubit c; qubit d;
5049 zz (a, c);
5050 h b;
5051 measure d;
5052 \end{yquant}
5053 \end{tikzpicture}
5054 \end{codeexample}
5055 \end{example}
5056
5057 \begin{example}
5058 \begin{codeexample}
5059 \begin{tikzpicture}
5060 \begin{yquant}
5061 qubit a; qubit b; qubit c; qubit d;
5062 zz (a, c);
5063 h b;
5064 align -;
5065 measure d;
5066 \end{yquant}
5067 \end{tikzpicture}
5068 \end{codeexample}
5069 \end{example}
5070
5071 \begin{example}
5072 \begin{codeexample}
5073 \begin{tikzpicture}
5074 \begin{yquant}
5075 qubit a; qubit b; qubit c; qubit d;
5076 zz (a, c);
5077 align -;
5078 h b;
5079 measure d;
5080 \end{yquant}
5081 \end{tikzpicture}
5082 \end{codeexample}
5083 \end{example}
5084
5085 \begin{example}
5086 \begin{codeexample*}
5087 \begin{tikzpicture}
5088 \begin{yquant}[operators/every barrier/.style={shape=yquant-line, draw, shorten <= -2mm, shorten >= -2mm, decoration={zigzag, segment length=4, amplitude=1pt}, decorate}]
5089 qubit a; qubit b; qubit c; qubit d;
5090 zz (a, c);
5091 h b;
5092 align -;
5093 barrier d;
5094 measure d;
5095 [red] barrier a, d;
5096 \end{yquant}
5097 \end{tikzpicture}
5098 \end{codeexample*}
5099 Note that a \Yquant{} \ttlink{barrier} works a bit differently from \pkg{qpic}'s.
5100 It is basically just an ordinary gate with a dashed line style; by the fact that all registers that are listed in the target list are aligned automatically, the \ttlink{barrier} usually does its job.
5101 Hence, we need to \ttlink{align} before the first \ttlink{barrier}, as it does not perform an alignment by itself on registers that were not mentioned as targets.
5102
5103 Here, we also change the default style (which is a dashed line) to the zigzag line that uses \pkg{qpic}'s style.
5104 Note that \Yquant{} automatically loads the library \texttt{decorations.pathmorphing}, so we don't need to do this.
5105 \end{example}
5106
5107 \begin{example}
5108 \begin{codeexample}
5109 \begin{tikzpicture}
5110 \begin{yquant*}[operator/minimum width=0pt, operator/separation=2mm]
5111 [shape=yquant-circle, radius=1.5ex]
5112 box {$2$} a | b;
5113 h a, b, c;
5114 [style=green!50!black]
5115 cnot c | a, b;
5116 [operator/separation=0pt, green!50!black]
5117 not c;
5118 \end{yquant*}
5119 \end{tikzpicture}
5120 \end{codeexample}
5121 In order to stick two operators directly next to each other, we must set the \ttlink{/yquant/operator/separation} to zero; this is the whitespace that is inserted before an operator.
5122 However, if the total with of an operator is smaller than \ttlink{/yquant/operator/minimum width}, it is centered in a box of this width (giving a more uniform layout with lots of small gates), which would add additional whitespace both to the right of the Hadamards and to the left of the \ttlink{cnot}s.
5123 We just globally suppress this minimum width, which is unproblematic for this particular circuit (we could also locally change it).
5124 \end{example}
5125
5126 \begin{example}
5127 \begin{codeexample}
5128 \begin{tikzpicture}
5129 \begin{yquant*}[operator/minimum width=0pt, operator/separation=2mm]
5130 [shape=yquant-circle, radius=1.5ex]
5131 box {$2$} a | b;
5132 h a, b, c;
5133 [style=red]
5134 cnot c | a, b;
5135 [operator/separation=0pt]
5136 not c;
5137 cnot c | a, b;
5138 [operator/separation=0pt, red]
5139 not c;
5140 \end{yquant*}
5141 \end{tikzpicture}
5142 \end{codeexample}
5143 \end{example}
5144
5145 \begin{example}
5146 \begin{codeexample}
5147 \begin{tikzpicture}
5148 \begin{yquant*}[operators/every box/.append style={shape=yquant-circle, radius=1.5ex}]
5149 box {$1$} a;
5150 not b;
5151 box {$2$} a;
5152 align -;
5153 not b;
5154 \end{yquant*}
5155 \end{tikzpicture}
5156 \end{codeexample}
5157 An instruction like \texttt{MIXGATES} does not exist in \Yquant, as it does not use a grid\hyp based layout; but of course, its behavior can be faked by \ttlink{align} gates.
5158 \end{example}
5159
5160 \clearpage
5161 \subsubsection{3.6 Reversing and Repeating}
5162 \begin{example}
5163 \begin{codeexample*}
5164 \begin{tikzpicture}
5165 \yquantdefinegate{cnots}{
5166 qubit a; qubit b; qubit c;
5167 cnot b | a;
5168 cnot b | c;
5169 cnot a | b;
5170 cnot c | b;
5171 }
5172 \begin{yquant*}
5173 cnots (a, b, c);
5174 cnots (-);
5175 \end{yquant*}
5176 \end{tikzpicture}
5177 \end{codeexample*}
5178 \Yquant{} does not have a concept of slices and hence can also not automatically repeat gates within a certain slice.
5179 However, there are multiple ways to achieve the circuits in this section without repeating parts manually.
5180 Here, we defined a custom gate that contained the content and inserted it two times.
5181 Another alternative would be to do this using macros, as was illustrated in the \hyperref[ex:qupicfirst]{very first example} of the \pkg{qpic} section.
5182 \end{example}
5183
5184 \begin{example}
5185 \begin{codeexample*}
5186 \begin{tikzpicture}
5187 \yquantdefinegate{gates}{
5188 qubit a; qubit b; qubit c;
5189 [plusctrl] box {$f$} a | b;
5190 addstyle {dotted} b;
5191 [plusctrl] box {$g$} (a, b) | c;
5192 [plusctrl] box {$f$} a | b;
5193 addstyle {solid} b;
5194 }
5195 \begin{yquant*}[plusctrl/.style={/yquant/every control/.style={/yquant/operators/every not}, /yquant/every positive control/.style={}}]
5196 gates (a, b, c);
5197 cnot b | c;
5198 gates (-);
5199 \end{yquant*}
5200 \end{tikzpicture}
5201 \end{codeexample*}
5202 Note that for this sequence, we did basically the same thing as before, as we identified a symmetric slice---so reversing the order does not do anything.
5203
5204 We will also expand this example and give a very simple (and a bit shortsighted) implementation of a macro that reverses the order of gates.
5205 Note that this macro basically just splits its content at semicolons and when it is done inputs all the parts in reverse order.
5206 Hence, it will fail if semicolons appear, e.g., in attributes without enclosing them in braces.
5207 It also does not correspond exactly to the \texttt{R} instruction from \pkg{qpic}, as it does not \emph{reverse} wire styles (basically \ttlink{addstyle} would become a hypothetical \texttt{subtractstyle} macro), but just inserts them in reverse order.
5208 Hence, the following example will give a different circuit with respect to the wire style!
5209
5210 \begin{codeexample*}
5211 \makeatletter
5212 \long\def\reversegates#1{%
5213 \begingroup%
5214 \let\reversegates@list=\empty%
5215 \count0=0 %
5216 \expandafter\reversegates@i#1;\reversegates@stop%
5217 }
5218 \long\def\reversegates@i#1;#2\reversegates@stop{%
5219 \ifstrempty{#2}{%
5220 \yquant@fordown \reversegates@idx := \count0 downto 1 {%
5221 \expandafter\expandafter\expandafter\yquant%
5222 \csname reversegates@list@\reversegates@idx\endcsname%
5223 }%
5224 \endgroup%
5225 }{%
5226 \ifstrequal{#2}{;}{%
5227 \reversegates@i;\reversegates@stop%
5228 }{%
5229 \advance\count0 by 1 %
5230 \csdef{reversegates@list@\the\count0}{#1;}%
5231 \reversegates@i#2\reversegates@stop%
5232 }%
5233 }
5234 }
5235 \begin{tikzpicture}
5236 \def\gates{%
5237 [plusctrl] box {$f$} a | b;
5238 addstyle {dotted} b;
5239 [plusctrl] box {$g$} (a, b) | c;
5240 [plusctrl] box {$f$} a | b;
5241 addstyle {solid} b;
5242 }
5243 \begin{yquant*}[plusctrl/.style={/yquant/every control/.style={/yquant/operators/every not}, /yquant/every positive control/.style={}}]
5244 \expandafter\yquant\gates
5245 cnot b | c;
5246 \reversegates\gates
5247 \end{yquant*}
5248 \end{tikzpicture}
5249 \end{codeexample*}
5250 \end{example}
5251
5252 \begin{example}
5253 \begin{codeexample}
5254 \begin{tikzpicture}
5255 \begin{yquant}
5256 qubit a; qubit b; qubit c;
5257 cnot b | a;
5258 cnot b | c;
5259 \foreach \i in {1, 2} { \yquant
5260 cnot a | b;
5261 cnot c | b;
5262 }
5263 \end{yquant}
5264 \end{tikzpicture}
5265 \end{codeexample}
5266 Another way to repeat things is to just use appropriate repetition macros (and remember to restart the parser); here, we used \tex!\foreach! from \TikZ, but any other will also do the job.
5267 \end{example}
5268
5269 \subsubsection{3.7 Other Circuit Elements}
5270 \begin{example}
5271 \begin{codeexample}
5272 \begin{tikzpicture}
5273 \begin{yquant*}[operators/every slash/.append style={radius=2mm}]
5274 slash a;
5275 [label=10:n] slash a;
5276 \end{yquant*}
5277 \end{tikzpicture}
5278 \end{codeexample}
5279 \end{example}
5280
5281 \begin{example}
5282 \begin{codeexample}
5283 \begin{tikzpicture}
5284 \begin{yquant}
5285 qubit a; qubit b;
5286 cnot b | a;
5287 inspect {\Ifcase\idx$a$\Else$a \oplus b$\Fi} -;
5288 cnot b | a;
5289 inspect {$b$} b;
5290 \end{yquant}
5291 \end{tikzpicture}
5292 \end{codeexample}
5293 Here, we achieved the centering of the two \ttlink{inspect}ed registers by putting them in a single gate instruction with case discrimination.
5294 \end{example}
5295
5296 \begin{example}
5297 \begin{codeexample}
5298 \begin{tikzpicture}
5299 \begin{yquant*}
5300 swap (a, b);
5301 text {$=$} (-);
5302 cnot b | a;
5303 cnot a | b;
5304 cnot b | a;
5305 \end{yquant*}
5306 \end{tikzpicture}
5307 \end{codeexample}
5308 This is a very simple equality; for more complicated ones, the \langlink{groups} library is recommended.
5309 \end{example}
5310
5311 \begin{example}
5312 \begin{codeexample}
5313 \begin{tikzpicture}
5314 \begin{yquant}
5315 qubit a; qubit b; qubit c; qubit d;
5316 box {$f$} (a, b);
5317 inspect {$G$} (a, b);
5318 init {$F$} (c, d);
5319 box {$g$} (c, d);
5320 \end{yquant}
5321 \end{tikzpicture}
5322 \end{codeexample}
5323 Note that we first defined all the registers explicitly, and they all use an initializing text.
5324 Had we directly used the \ttlink{init} gate on the registers \yquant!(c, d)! as the \emph{first} gate when \emph{neither} of both registers had an initializing text, then the~$F$ would have been placed to the left of the wires.
5325 Basically, a \ttlink{qubit} declaration with a value is the same as declaring the register without a value plus another \ttlink{init} gate that puts the value in place.
5326 A zero\hyp length \ttlink{hspace} gate or an \ttlink{align}ment directly at the beginning would be a way to prevent this shift to the left from happening.
5327 \end{example}
5328
5329 \Yquant{} does not support the permutation gate that \pkg{qpic} has.
5330 It would however not be very difficult to implement this particular shape and make it available.
5331 Maybe even a multi\hyp swap gate using the \texttt{knots} library would be possible.
5332
5333 \clearpage
5334 \subsubsection{3.8 Comments}
5335 \begin{example}
5336 \begin{codeexample*}
5337 \begin{tikzpicture}
5338 \begin{yquant}
5339 qubit a; qubit b;
5340 \foreach \i in {0, 1, 2} { \yquant
5341 cnot b | a;
5342 cnot a | b;
5343 \ifnum\i<2 \yquant
5344 [operator/separation=2pt, operator/minimum width=0pt]
5345 barrier (-);
5346 \fi
5347 }
5348 \end{yquant}
5349 \end{tikzpicture}
5350 \end{codeexample*}
5351 \end{example}
5352
5353 \begin{example}
5354 \begin{codeexample}
5355 % \usetikzlibrary{quotes}
5356 \begin{tikzpicture}
5357 \begin{yquant}[operators/every box/.append style={shape=yquant-circle, radius=1.5ex}]
5358 qubit a;
5359 [blue, "above" above] box {$1$} a;
5360 [red, "below" below] box {$2$} a;
5361 ["both" above, "sides" below] box {$3$} a;
5362 \end{yquant}
5363 \end{tikzpicture}
5364 \end{codeexample}
5365 \end{example}
5366
5367 \begin{example}
5368 \begin{codeexample*}
5369 \begin{tikzpicture}
5370 \begin{yquant}[operators/every box/.append style={shape=yquant-circle, radius=1.5ex}]
5371 qubit a;
5372 \startenclose[brace above=above]{first}
5373 box {$1$} a;
5374 \startenclose[bracing style=blue, brace below=below]{second}
5375 box {$2$} a;
5376 \stopenclose{first}
5377 box {$3$} a;
5378 \stopenclose{second}
5379 \end{yquant}
5380 \end{tikzpicture}
5381 \end{codeexample*}
5382 This shows a complicated use of the \texlink{\enclose}\hyp like macros: here, we have overlapping enclosing regions.
5383 Therefore, it is not possible to use the \texlink{\enclose} macro for both descriptions (though for one would be fine).
5384 Instead, we create named enclosures, which can be interleaved arbitrarily.
5385 \end{example}
5386
5387 \begin{example}<Updated in 0.9>
5388 \begin{codeexample*}
5389 \begin{tikzpicture}
5390 \begin{yquant}
5391 qubit a;
5392 qubit b;
5393 qubit c;
5394 \enclose[draw=none, fill=red!40!white] {
5395 cnot b | a;
5396 cnot a | b;
5397 }
5398 hspace {2mm} -;
5399 \enclose[rounded corners=10pt] {
5400 cnot c | b;
5401 cnot b | c;
5402 }
5403 \end{yquant}
5404 \end{tikzpicture}
5405 \end{codeexample*}
5406 \end{example}
5407
5408 \clearpage
5409 \subsubsection{3.9 Macros and \LaTeX{} Code}
5410 \begin{example}
5411 \begin{codeexample}
5412 \begin{tikzpicture}[loud/.style={red, very thick}]
5413 \yquantdefinebox{phase2}[loud, draw, shape=yquant-circle, radius=1.5ex]{$2$}
5414 \begin{yquant*}
5415 [style=loud]
5416 phase2 a | b;
5417 phase2 b;
5418 \end{yquant*}
5419 \end{tikzpicture}
5420 \end{codeexample}
5421 Note that here, we choose an alternative gate name, as there already is the built\hyp in gate \ttlink{phase}.
5422 While we could overwrite it, this is generally a very bad idea.
5423 Keep in mind that gate declarations are global and also that gates are case insensitive, so changing the capitalization would not help.
5424 Finally note that when we define a style for a new gate, it only pertains to the \emph{gate} itself.
5425 It is not possible to change styles external to the gate---such as control lines---within the gate definition itself.
5426 \end{example}
5427
5428 \begin{example}
5429 The following example in the \pkg{qpic} manual requires some additional thoughts.
5430 It defines a custom gate with a variable number of target registers.
5431 The \texlink{\yquantdefinegate} interface does not officially allow for this, although some low\hyp level hacking can of course be done (a \emph{sorted} \pkg{etoolbox} list is provided in the macro \tex!\yquant@circuit@subcircuit@param!, which holds the internal indices of all currently involved target registers).
5432
5433 It is of course always possible to write some macros that output the required gate commands.
5434 Looking at the particular example, it is actually not really necessary to define a gate that has a variable number of targets.
5435 Rather, in \Yquant, one would define a new gate that just contains the two $\oplus$ symbols next to each other; the control line is drawn separately from the gate anyway.
5436 This very straightforward description will unfortunately fail, for the following reason:
5437 When a control line is drawn, \Yquant{} currently always draws it from the \texttt{center} anchor of the current shape upwards or downwards.
5438 However, for the $\mathord\oplus\!\mathord\oplus$ shape, the control line point should actually be in the middle of the right $\oplus$.
5439 This is an off\hyp center point, so we need some hacking to convince \Yquant{} to do this.
5440 The following code is pretty long; we will therefore give parts of the code, followed by an explanation.
5441
5442 First of all, we define a shape (similar to what is done in \texttt{yquant-shapes.tex}) that holds the two $\oplus$es.
5443 \begin{codeexample*}
5444 \makeatletter
5445 \pgfdeclareshape{yquant-doubleoplus}{%
5446 \inheritsavedanchors[from=yquant-slash]%
5447 \anchor{center}{\pgfqpoint{.5\dimexpr\xradius\relax}{0pt}}%
5448 \foreach \anc in {north, east, south, west} {
5449 \inheritanchor[from=yquant-rectangle]{\anc}
5450 }
5451 \anchor{north east}{\pgfqpoint{.853553\dimexpr\xradius\relax}
5452 {.707107\dimexpr\yradius\relax}}%
5453 \anchor{south east}{\pgfqpoint{.853553\dimexpr\xradius\relax}
5454 {-.707107\dimexpr\yradius\relax}}%
5455 \anchor{south west}{\pgfqpoint{-.853553\dimexpr\xradius\relax}
5456 {-.707107\dimexpr\yradius\relax}}%
5457 \anchor{north west}{\pgfqpoint{-.853553\dimexpr\xradius\relax}
5458 {.707107\dimexpr\yradius\relax}}%
5459 % The border anchor is a bit more tricky, we leave it out here
5460 \backgroundpath{%
5461 \pgf@relevantforpicturesizefalse%
5462 \pgfpathmoveto{\pgfqpoint{-.5\dimexpr\xradius\relax}{\yradius}}%
5463 \pgfpathlineto{\pgfqpoint{-.5\dimexpr\xradius\relax}{-\yradius}}%
5464 \pgfpathmoveto{\pgfqpoint{.5\dimexpr\xradius\relax}{\yradius}}%
5465 \pgfpathlineto{\pgfqpoint{.5\dimexpr\xradius\relax}{-\yradius}}%
5466 \pgfpathmoveto{\pgfqpoint{-\xradius}{0pt}}%
5467 \pgfpathlineto{\pgfqpoint{\xradius}{0pt}}%
5468 \pgfpathellipse{\pgfqpoint{-.5\dimexpr\xradius\relax}{0pt}}%
5469 {\pgfqpoint{.5\dimexpr\xradius\relax}{0pt}}%
5470 {\pgfqpoint{0pt}{\yradius}}%
5471 \pgf@relevantforpicturesizetrue%
5472 \pgfpathellipse{\pgfqpoint{.5\dimexpr\xradius\relax}{0pt}}%
5473 {\pgfqpoint{.5\dimexpr\xradius\relax}{0pt}}%
5474 {\pgfqpoint{0pt}{\yradius}}%
5475 }%
5476 \clippath{%
5477 \pgfpathellipse
5478 {\pgfqpoint{-.5\dimexpr\xradius}{0pt}}
5479 {\pgfqpoint{.5\dimexpr\xradius+\pgflinewidth}{0pt}}
5480 {\pgfqpoint{0pt}{\dimexpr\yradius+.5\pgflinewidth\relax}}%
5481 \pgfpathellipse
5482 {\pgfqpoint{.5\dimexpr\xradius\relax}{0pt}}
5483 {\pgfqpoint{.5\dimexpr\xradius+\pgflinewidth\relax}{0pt}}
5484 {\pgfqpoint{0pt}{\dimexpr\yradius+.5\pgflinewidth\relax}}%
5485 }%
5486 }
5487 \end{codeexample*}
5488 \input \[email protected] % we need to execute all this again without the group
5489 Nothing special happens in the first few lines (although the \texttt{center} anchor is now off\hyp center): We declare a new shape \texttt{yquant-doubleoplus}, inherit some saved anchors and anchors (for details, see the \TikZ{} manual, section~106.5.3, ``Command for Declaring New Shapes'').
5490 The \texttt{x~radius} now corresponds to the \emph{diameter} of one of the circles, since we have two circles next to each other.
5491 We do not define border anchors at the moment; they would require some additional computation, but in lots of scenarios, they are not necessary (as we would also not need most of the anchors, but it is always good to have them).
5492 The clip path is also not very special, it just contains the shape that is to be clipped away; basically, both circles.
5493 The background path deserves more attention.
5494 We draw the two circles and the vertical and horizontal lines; but note that we disable the \pkg{pgf}'s size protocol for all but the right circle.
5495 Hence, when this shape is used, \TeX{} and \Yquant{} will actually think that it only occupies space for the right circle; the left one will protrude in the margin.
5496 (Actually, we could wrap this in a test such as \tex!\ifdefined\yquant@prefix! to only discard protocoling within a \texlink{yquant} environment, so that the shape is properly usable outside.)
5497
5498 Next, we must take care of re\hyp inserting this ``lost'' margin whenever the gate is used; and we also define a style that appropriately uses the shape:
5499 \begin{codeexample*}
5500 \yquantset{
5501 operators/every noffoli/.style={
5502 shape=yquant-doubleoplus, x radius=2.6mm, y radius=1.3mm, draw
5503 },
5504 internal/noffoli shift/.code={%
5505 \begingroup%
5506 \expandafter\tikzset\expandafter{\yquant@draw@@style}%
5507 \tikzset{/yquant/every operator, /yquant/operators/every noffoli, /yquant/this operator}%
5508 \edef\cmd{%
5509 \endgroup
5510 \dimdef\noexpand\yquant@config@operator@sep{%
5511 \yquant@config@operator@sep+
5512 \pgfkeysvalueof{/tikz/x radius}%
5513 }%
5514 }%
5515 \cmd
5516 }
5517 }
5518 \end{codeexample*}
5519 \input \[email protected] % we need to execute all this again without the group
5520 The first style is almost a copy of the \ttlink{/yquant/operators/every not} style, only with the new shape and a doubled \texttt{x~radius}.
5521 The second style is more complicated: Protected in a group, it first sets all the custom style overwrites that a user may pass to the gate (e.g., the user may wish to overwrite the radii)---those are stored in the internal macro \tex!\yquant@draw@@style!.
5522 Then, it applies the styles in the order as the gate would do it; note that an attribute such as \yquant![x radius=1cm]! would only add the directive to the \ttlink{/yquant/this operator} style, but not apply it yet, therefore we now execute all the options that were stored previously.
5523 As the final action that survives the group, we add the current value for the \texttt{x~radius} to the current value of the operator separation---this effectively enacts the proper placement of our gate.
5524
5525 Having defined those styles, we finally need to declare the gate itself, so that it can be used in a circuit:
5526 \begin{codeexample*}
5527 \yquant@langhelper@declare@command
5528 {noffoli}
5529 {}
5530 {%
5531 \appto\yquant@attrs@remaining{,/yquant/internal/noffoli shift}%
5532 \yquant@prepare
5533 {}%
5534 {/yquant/operators/every noffoli}%
5535 }%
5536 \yquant@langhelper@setup@attrs{noffoli}{}{}
5537 \end{codeexample*}
5538 % those were global commands anyway
5539 We call \tex!\yquant@langhelper@declare@command!, as for every standard gate declaration, with the desired name of the gate (\tex!{noffoli}!), the actions that are to be carried out \emph{before} the targets and controls are parsed (\tex!{}!), and the actions that are to be carried out once the targets and controls are known.
5540 We append the style that we just defined to the list of attributes, and execute the gate preparation.
5541 Finally, we also declare the attributes that this gate takes---no required and no optional attributes.
5542
5543 After all this work, which can be saved in some shared document and used whenever necessary, we can come to the application, which is now very straightforward (however, note that our gate declaration was too simplistic for vertical mode; but an adaptation is not difficult).
5544
5545 \begin{codeexample}
5546 \begin{tikzpicture}
5547 \begin{yquant*}
5548 noffoli a | b, c;
5549 [style=red] noffoli c | a, b;
5550 noffoli b | a;
5551 \end{yquant*}
5552 \end{tikzpicture}
5553 \end{codeexample}
5554 \end{example}
5555
5556 \begin{example}
5557 \begin{codeexample}
5558 % \usetikzlibrary{backgrounds}
5559 \begin{tikzpicture}
5560 \begin{yquant}
5561 qubit a; qubit b;
5562 [name=cn]
5563 cnot b | a;
5564 \draw[fill=blue] (cn) circle[radius=5pt];
5565 box {FONT} (-);
5566 [name=cn, fill=boxBlueBody]
5567 cnot b | a;
5568 \scoped[on background layer] \draw[fill=red] (cn) circle[radius=5pt];
5569 \end{yquant}
5570 \end{tikzpicture}
5571 \end{codeexample}
5572 There are no special options to mix \TikZ{} code with \Yquant{} code, as this can be done natively at any time.
5573 To draw at the position of another gate, just name the gate.
5574 Note that---as was illustrated here---also using the same name multiple times is possible, in this case, the latter use overwrites the former.
5575 There is no direct equivalent to the \texttt{PRETIKZ} option---the gate has to be drawn first in order to get its position.
5576 However, \TikZ{} supports layers, so it is easy to draw something behind a gate: just put it on a background layer, e.g., the one provided by the \texttt{backgrounds} library.
5577 Also note that here, we filled the second \ttlink{cnot} gate with our background color in order to give the same image as in the \pkg{qpic} manual.
5578 The circle is not filled by default, hence the red ``outer'' circle would be visible also inside the \ttlink{cnot} circle.
5579 \end{example}
5580
5581 The \texttt{HYPERTARGET} instruction can be directly reproduced in \TeX{} by just putting a \tex!\hypertarget! before the \texttt{tikzpicture}.
5582 \endgroup
5583
5584 \section{Foreign language support and extensions}\label{sec:foreign}
5585 \Yquant{} is built in various modules, so that it is not hard to use the quantum circuit rendering backend, but expose a different language frontend.
5586 \compat{New in 0.3}\Yquant{} not only understands its own language, but also others.
5587 \compat{New in 0.5}Although we refer to ``foreign languages,'' additional extension packages of the \Yquant{} language itself are also covered in this section and can be loaded by the same syntax.
5588
5589 \subsection[groups]{\compat{Updated in 0.8, 0.7\\New in 0.5}groups}\label{sec:foreign:groups}
5590 By saying \tex!\useyquantlanguage{groups}! in the preamble after loading \Yquant{} itself, additional support for groups of \Yquant{} circuits is loaded.
5591 Various circuits in a group share a common set of registers, are appropriately aligned horizontally if on the same line and can also be aligned vertically among multiple lines.
5592 The main intended use is for circuit equations.
5593
5594 \linkdef{yquantgroup}
5595 This extension provides the environment \tex!yquantgroup!.
5596 You may use this environment within a \tex!tikzpicture!; in this case, all page\hyp break related features are not available.
5597 You may alternatively use the environment outside of a \tex!tikzpicture!; in this case, it will start and end the pictures appropriately.
5598
5599 As with \texlink{yquant}, the environment accepts optional options that are passed to \texlink\yquantset; a starred form is available that allows for the lazy creation of registers---but note that \emph{shared} registers must always be created explicitly; only if some circuits in the group have additional registers, the lazy creation applies.
5600
5601 \subsubsection{General usage}\vskip-2mm
5602 \begin{minted}{tex}
5603 % preamble:
5604 % \usepackage[compat=<version>]{yquant}
5605 % \useyquantlanguage{groups}
5606 \begin{tikzpicture}% tikz options possible. This environment may be omitted.
5607 % tikz commands go here
5608 \begin{yquantgroup}% yquant(group) options possible.
5609 \registers{
5610 % arbitrary shared register declaration go here
5611 }
5612 % any of \circuit, \equals, \\, \shiftright
5613 % if the tikzpicture environment was omitted: also allows
5614 % \pagebreak, \newpage, \clearpage, \cleardoublepage, \intertext, \shortintertext
5615 % in most cases, TikZ commands are also allowed
5616 \end{yquantgroup}
5617 % tikz commands go here
5618 \end{tikzpicture}
5619 \end{minted}
5620
5621 \subsubsection{Special macros}
5622 \paragraph*{\tex!\registers!}
5623 \linkdef{groups/registers}
5624 The \tex!\registers! macro can and must only be used once in a \texlink{yquantgroup} environment.
5625 It contains the declaration of all the registers that are shared among the various circuits within a group.
5626 Basically, if you follow the convention in a usual \Yquant{} circuit to first declare all the registers, then use the gates, then you would put the declaration part in the \tex!\registers! macro.
5627 However, note that it is in principle also possible to mix register declarations with other gates and \TikZ{} commands.
5628
5629 In case you do not use the \ttlink{import} gate in any of the circuits within the group and you do not declare own registers, the behavior is very straightforward: basically, the content of \tex!\registers! is copied verbatim at the beginning of each circuit\footnote{%
5630 This is not strictly true.
5631 Vector registers will always be created one\hyp by\hyp one, as \Yquant{} cannot know (without undertaking some effort) whether they are complete or interspersed with others.
5632 This implies that for \ttlink{compat} versions prior to~0.8, indexing will look very strange.
5633 }.
5634 Otherwise, the general rule is: importing a register will ensure that all non\hyp declaration commands that preceded this register declaration are executed; and importing the last register will additionally execute all succeeding commands within \tex!\registers!.
5635
5636 \paragraph*{\tex!\circuit[<style>]{<content>}!}
5637 \linkdef{groups/circuit}
5638 The \tex!\circuit! macro can be thought of as starting a \tex!yquant! (or \texlink{yquant*}) environment and using its mandatory argument \tex!<content>! as the content of the circuit; the optional \tex!<style>! is used to apply additional styling options to the circuit.
5639
5640 This is not entirely accurate: In reality, the content is put into a \ttlink{subcircuit} and \tex!<style>! is passed as arguments to the subcircuit.
5641
5642 The default style \ttlink{/yquant/operators/every group circuit} is applied to the circuit.
5643 This style is configured such that the illusion of working in a top\hyp level \texlink{yquant} environment is very convincing: The circuit is frameless by default and uses the transparent name mangling scheme.
5644
5645 All the registers that were previously defined via \texlink[groups/registers]{\registers} are automatically available within the circuit, as if their declaration had been copied.
5646 In fact, \Yquant{} will make a register available the first time it is referenced in some gate; if at the end of a circuit some of the shared registers were not used, they will be imported before exiting the circuit.
5647 Consequently, if you define own registers just for a single circuit, these will always be at the very top.
5648 This can be influenced by means of the \ttlink{import} gate, which is only available in group \tex!\circuit!s.
5649 This gate allows to import a declared register at an arbitrary position.
5650
5651 \paragraph*{\tex!\equals*[<content>]!}\label{sec:foreign:groups:equals}
5652 \linkdef{groups/equals}
5653 The \tex!\equals! macro inserts a blank text---internally, a \ttlink{box}\hyp like gate with the style \ttlink{/yquant/operators/every group equals}---that contains \tex!<content>!.
5654 If omitted, \tex!<content>! is given by \tex!$=$!.
5655
5656 The optional star will put a horizontal alignment mark at the position where the box is inserted.
5657 Similar to the \tex!&! operation in \pkg{amsmath}'s \tex!align! environment or the \tex!\>! in \TeX's native \tex!tabbing!, \Yquant{} will now remember the horizontal position of the box internally and will allow you to directly jump to this position in the next line.
5658 Note that you may well have multiple alignment marks in a single line, which \Yquant{} internally numbers \texttt{1}, \texttt{2}, \dots.
5659
5660 \paragraph*{\tex!\\[<separation>]!}
5661 \linkdef{groups/linebreak}
5662 The \tex!\\! macro inserts a line break (never a page break), so that the next \texlink[groups/circuit]{\circuit} or \texlink[groups/equals]{\equals} will be put below all circuits that were output before, and it will again start at the same left position as the first circuit.
5663 The default vertical distance is given by \ttlink{/yquant/group/line separation}, but it may be overwritten by the optional \tex!<separation>! argument, which must be a \TeX{} dimension.
5664
5665 Note that if you \emph{set} new alignment marks in a new line, this will delete the alignment marks that were previously set.
5666
5667 If the option \ttlink{/yquant/group/aligned} is passed to the \texlink{yquantgroup} environment, the command \texlink[groups/shiftright]{\shiftright} is implied after each linebreak.
5668
5669 \paragraph*{\tex!\shiftright*[<where>]!}
5670 \linkdef{groups/shiftright}
5671 The \tex!\shiftright! command will put the ``cursor,'' i.e., the horizontal position at which the next \texlink[groups/circuit]{\circuit} or \texlink[groups/equals]{\equals} will start, at the position specified by \tex!<where>!.
5672 By default, \tex!<where>! is \tex!1!.
5673 If the optional star is present, \Yquant{} will additionally put an alignment mark at this position (see the documentation for \texlink[groups/equals]{\equals}).
5674 If the option \ttlink{/yquant/group/aligned} is passed to the \texlink{yquantgroup} environment, the command \tex!\shiftright! is implied after each linebreak or starred page break.
5675
5676 The option \tex!<where>! can take various forms:
5677 \begin{itemize}
5678 \item It may be a natural number \texttt{1}, \texttt{2}, \dots, denoting the number of an alignment mark specified in a previous line.
5679 \item It may be the number \texttt{0}, denoting the very beginning of the line; this is useful if the \ttlink{/yquant/group/aligned} option is given, but for a specific line, no alignment should be performed.
5680 \item It may be a \TeX{} dimension, in which case this dimension is directly added to the cursor (so it is a relative value).
5681 This is where passing the optional star makes most sense.
5682 If you want to position absolutely, you may first issue \tex!\shiftright[0]! followed by a shift by the dimension that you want.
5683 \end{itemize}
5684
5685 \begin{warning}
5686 The macro is named \tex!\shiftright!; however, \Yquant{} does not enforce that the actual position is to the right of the current position.
5687 You may indeed be able to create overlapping circuits if you shift back to a previous position.
5688 \end{warning}
5689
5690 \paragraph*{\tex!\pagebreak*!, \tex!\newpage*!, \tex!\clearpage*!, \tex!\cleardoublepage*!}
5691 \linkdef{groups/pagebreak}\linkdef{groups/newpage}\linkdef{groups/clearpage}\linkdef{groups/cleardoublepage}
5692 The page breaking commands are available only if the \texlink{yquantgroup} was not enclosed in a \tex!tikzpicture!.
5693 They will end the current picture environment, issue the original page breaking command, and start a new picture.
5694 Hence, if you want to pass options globally to the picture, you should use the \ttlink{/yquant/preamble} option for the \texlink{yquantgroup}; the content of this key will be passed as options for every implicitly started \tex!tikzpicture!.
5695
5696 Usually, remembering the horizontal alignment marks on a new page does not make much sense.
5697 For this reason, the commands will delete all alignment; use their starred versions to retain them.
5698 If the option \ttlink{/yquant/group/aligned} is passed to the \texlink{yquantgroup} environment, the command \texlink[groups/shiftright]{\shiftright} is implied after the starred version of the page break.
5699
5700 Typically, you will not want to refer to named gates in a circuit on a different page; remember that if you need this feature, you must pass the \texttt{remember picture} key in the \ttlink{/yquant/preamble} option, as this is a reference to another \tex!tikzpicture!.
5701 Also don't forget to use the (\TikZ) \texttt{overlay} key on the corresponding path that references the node in order not to mess up with the bounding box (see the \TikZ{} documentation for those two keys).
5702
5703 \paragraph*{\tex!\intertext!, \tex!\shortintertext!}
5704 \compat{New in 0.8}\linkdef{groups/intertext}\linkdef{groups/shortintertext}
5705 The text intermission commands are available only if the \texlink{yquantgroup} was not enclosed in a \tex!tikzpicture!.
5706 They will end the current picture environment, output the text (or really just anything) that was given as required argument as a new paragraph, and start a new picture with the same alignment.
5707 Note that the commands do not detect whether a page break happens, they will \emph{always} preserve the alignment.
5708
5709 While the commands resemble those from \pkg{amsmath} and \pkg{mathtools}, they take two optional arguments, which must be valid \TeX{} skips and which define the vertical space inserted before and after the text.
5710 By default, \tex!\intertext! is the same as \tex!\intertext[\belowdisplayskip][\abovedisplayskip]!, while\\\tex!\shortintertext! is the same as\\\tex!\shortintertext[\belowdisplayshortskip][\abovedisplayshortskip]!.
5711
5712 \subsubsection{Configuration}
5713 Loading the \pkg{groups} language extension will define several new configuration keys.
5714
5715 \begin{option}{group/every group}!!
5716 Style that is installed for every \texlink{yquantgroup} and \texlink[yquantgroup]{yquantgroup*} environment, as if it had been given as an option.
5717 The style's default path is \texttt{/tikz}.
5718 \end{option}
5719
5720 \begin{option}{group/line separation}!5mm!
5721 This is the default vertical line separation that is inserted whenever a new line is issued in a \texlink{yquantgroup}.
5722 \end{option}
5723
5724 \begin{option}{group/aligned}!false!
5725 This boolean flag defines whether \texlink[groups/shiftright]{\shiftright} is automatically issued after \texlink[groups/linebreak]{\\} and the starred page breaking commands.
5726 \end{option}
5727
5728 \begin{option}{preamble}!!
5729 This style may only be passed to the \texlink{yquantgroup} alignment directly as an option; it is not available via \texlink{\yquantset} and the like.
5730 It is only relevant if the \texlink{yquantgroup} is not contained in a \tex!tikzpicture!.
5731 The content of this style will be given as an optional argument to the \tex!tikzpicture!; this is the recommended way to specify \TikZ{} options, as they are automatically preserved among page breaks.
5732 \end{option}
5733
5734 \begin{option}{operators/every group circuit}!/yquant/operators/every subcircuit, /yquant/operators/subcircuit/frameless, /yquant/operators/subcircuit/name mangling=transparent!
5735 This style is installed for the \ttlink{subcircuit} that implicitly wraps each \texlink[groups/circuit]{\circuit}.
5736 Note that some magic is carried out to ensure that the name mangling setting only applies to the \emph{direct} content of the \texlink[groups/circuit]{\circuit}; any \ttlink{subcircuit}s within the \texlink[groups/circuit]{\circuit} will use the default name mangling scheme.
5737 \end{option}
5738
5739 \begin{option}[Changed in 0.7]{operators/every group equals}!shape=yquant-rectangle, align=center, anchor/.expanded=\ifyquanthorz{center}{north west}, inner xsep=1mm, x radius=2mm, y radius=2.47mm!
5740 This style is installed for every \texlink[groups/equals]{\equals}, which is internally realized similarly to a \ttlink{box} gate.
5741 \end{option}
5742
5743 \subsubsection{Gates and operations}
5744 No gates or operations may be used directly within the \texlink{yquantgroup} environment, but all the usual \Yquant{} gates and operations are available within \texlink[groups/registers]{\registers} and \texlink[groups/circuit]{\circuit}.
5745 Additionally, within \texlink[groups/circuit]{\circuit}, the \ttlink{import} gate is available.
5746
5747 \paragraph*{\yquant!import!}\linkdef{import}\leavevmode\\
5748 Syntax: \yquant!import <target>;! \\
5749 This is a pseudo\hyp gate that makes all the outer registers given in \tex!<target>! available in the current circuit.
5750 Consequently, the register names that are specified in \tex!<target>!, also ranges, do not refer to the registers in the \emph{current} \texlink[groups/circuit]{\circuit}, but instead to those defined via \texlink[groups/registers]{\registers}.
5751 Therefore, it is for example possible to import all outer registers at once using \yquant!import -;!.
5752 Vector registers can also be imported partially.
5753
5754 If additional content (\TeX{} commands such as \TikZ{} paths, non\hyp creation gates) is used within \texlink[groups/registers]{\registers}, everything that comes \emph{before} the declaration of a register will be copied into the \texlink[groups/circuit]{\circuit} when the register is imported; for a vector, this refers to the index zero.
5755 Additionally, any additional content that comes \emph{after} the declaration of the last register will be copied directly after the last register was imported.
5756
5757 \begin{warning}[Out-of-order importing]
5758 Note that it is principle possible to import registers out\hyp of\hyp order.
5759 Since matching outer and inner wires in subcircuits is done in the order in which they appear, this will lead to inner registers with names that do not match their outer registers and is probably highly undesirable.
5760 \end{warning}
5761
5762 Usually, this gate will not be needed as \Yquant{} will automatically import an outer register upon its first use.
5763
5764 \emph{Possible attributes:} none
5765
5766 \subsubsection[Vertical layout]{\compat{New in 0.7}Vertical layout}
5767 This library is aware of vertical circuits.
5768 Note that the layout \emph{between} the circuits will always be the same, irrespective of the actual circuit orientation: Circuit will be set from left to right, and line breaks will always lead to a vertical shift and a reset of the horizontal position.
5769
5770 That said, the circuits may \emph{internally} be set in vertical mode.
5771 Every invocation of \texlink[groups/circuit]{\circuit} will then always restart a new \texlink{yquant} environment.
5772 While the content will still be put in a lonely \ttlink{subcircuit}---to keep consistency in the styling options---no inter\hyp circuit wire alignment will be carried out.
5773
5774 There is an additional complication regarding the vertical alignment of circuit and equality signs within one ``line.''
5775 Now, the circuits can very well have a varying height, so vertically centering the circuits with respect to each other would not lead to a satisfying layout.
5776 We might desire to vertically center the equality sign between its two enclosing circuits.
5777 However, what if the line contains more than a two circuits and they all have different heights?
5778 Then, the equality signs would be at different positions.
5779 For this reason, all \texlink[groups/circuit]{\circuit}s and \texlink[groups/equals]{\equals} will be aligned at their top.
5780
5781 \subsection{qasm}\label{sec:foreign:qasm}
5782 By saying \tex!\useyquantlanguage{qasm}! in the preamble after loading \Yquant{} itself, the parser for \langlink{qasm} (not OpenQASM) is loaded.
5783 \linkdef{qasm}\linkdef\qasmimport
5784 It provides the environment \tex!qasm! as well as the macro \tex!\qasmimport!, which works similarly to \texlink{\yquantimport} (but does not accept additional options).
5785
5786 \subsubsection{Language specification}
5787 The \pkg{qasm} language is not formally defined, but an overview is provided at \href{https://web.archive.org/web/20050410022847/https://www.media.mit.edu/quanta/qasm2circ/#spec}{the archived website of \texttt{qasm2circ}}.
5788 The \Yquant{} implementation is designed to be compatible with the original parser, with the following exceptions:
5789 \begin{itemize}
5790 \item In \pkg{qasm}, lines could begin in an arbitrary manner; the first whitespace followed by the first valid command were then the instruction.
5791 Contrary to this, \Yquant's parser always expects a line to start with a valid gate (preceded by arbitrary whitespaces), a comment, or to be empty.
5792 \item \linkdef{qasm/dmeter}\linkdef{qasm/meter}\linkdef{qasm/dmeterwide}
5793 In \pkg{qasm}, user\hyp defined gates will be drawn in a box unless they contain the text \texttt{\textbackslash dmeter}, and they will be recognized as measurement gates if they contain \texttt{\textbackslash meter} or \texttt{\textbackslash dmeter}.
5794 Contrary to this, \Yquant's parser expect the gates to \emph{start} with one of the \emph{macros} \tex!\meter!, \tex!\dmeter!, or \tex!\dmeterwide!.
5795 Using these macros \emph{within} the content of a gate does not make sense from the point of view that in \Yquant, gates are nodes with shapes, so either the full gate has a particular shape or it does not, but not only parts of it.
5796 \item \linkdef{qasm/space}\linkdef{qasm/nop}
5797 The \texttt{space} gate is supposed to produce a horizontal whitespace without a gate.
5798 In \Yquant's implementation, you have to discard the wire if you want to reproduce this behavior; \texttt{space} and \texttt{nop} are equivalent.
5799 \end{itemize}
5800
5801 \linkdef{qasm/m}\linkdef{qasm/txt}
5802 The default \pkg{qasm} style defines several macros that can be used in gates.
5803 \Yquant{} makes \tex!\m! (matrix; requires \pkg{amsmath}) and \tex!\txt! (switch to text mode) available within the \pkg{qasm} environment.
5804
5805 Do not expect \Yquant's output to match the one of \pkg{qasm} exactly.
5806 \Yquant{} is not grid based, so that commands such as \ttlink[qasm/nop]{nop} don't even make sense.
5807 They are implemented for compatibility reasons and will produce a fixed horizontal space of the operator minimum width plus one separation, which might or might not be accurate.
5808
5809 Note that whatever you write between \tex!\begin{qasm}! and \tex!\end{qasm}! is essentially treated as verbatim; only where the specification says so (in the definition of a new gate and in the optional third command to the register definition), it is interpreted as \TeX{} markup.
5810 Consequently, in \pkg{beamer}, any frame containing these environments must be given the \texttt{fragile} option.
5811
5812 \subsubsection{Configuration}
5813 Loading the \pkg{qasm} language interpreter will define several new configuration keys.
5814 For all the gates, it will use the keys defined in \cref{sec:config}, and it additionally provides the following:
5815
5816 \begin{option}{operators/every s}!/yquant/operators/every box!
5817 This style is installed for every \texttt{s} operator.
5818 \end{option}
5819
5820 \begin{option}{operators/every t}!/yquant/operators/every box!
5821 This style is installed for every \texttt{t} operator.
5822 \end{option}
5823
5824 \begin{option}{operators/every utwo}!/yquant/operators/every box!
5825 This style is installed for every \texttt{Utwo} operator.
5826 \end{option}
5827
5828 \begin{option*}{qasm}{zero}!\qasm@ket0!
5829 The content of this macro is used as the initialization content whenever the \texttt{zero} gate is invoked.
5830 \end{option*}
5831
5832 \begin{option*}{qasm}{register/default qubit name}!\qasm@ket{#1}!
5833 This macro is invoked with a single parameter (the name of a qubit register) and gives back what is printed as the name of the register (will be in math mode automatically).
5834 \end{option*}
5835
5836 \begin{option*}{qasm}{register/default qubit name value}!\qasm@ket{#1} = \qasm@ket{#2}!
5837 This macro is invoked with two parameters (the name of a qubit register and its initial value) and gives back what is printed as the name of the register (will be in math mode automatically).
5838 \end{option*}
5839
5840 \subsubsection{Examples}
5841 The unaltered \texttt{.qasm} files provided from \href{https://www.media.mit.edu/quanta/qasm2circ/}{the \texttt{qasm2circ} page} were stored in the subfolder \texttt{qasm} relative to this manual's \TeX{} file.
5842 The following command is then used to print all of them:
5843 \begin{minted}{tex}
5844 % preamble:
5845 % \usepackage[compat=<version>]{yquant}
5846 % \usepackage{import}
5847 % \useyquantlanguage{qasm}
5848 \def\yquantimportpath{qasm/}
5849 \foreach \circuitno in {1, ..., 18} {
5850 \paragraph{Circuit \#\circuitno}
5851 \begin{center}
5852 \qasmimport{test\circuitno.qasm}
5853 \end{center}
5854 }
5855 \end{minted}
5856
5857 \def\yquantimportpath{qasm/}
5858 \foreach \circuitno in {1, ..., 18} {
5859 \paragraph{Circuit \#\circuitno}\leavevmode\nopagebreak\par\nopagebreak
5860 \begin{adjustbox}{center}
5861 \qasmimport{test\circuitno.qasm}
5862 \end{adjustbox}
5863 }
5864
5865 \section{Integration with other packages}
5866 In general, \Yquant{} should not introduce incompatibilities with other packages.
5867 However, the possibility to mix \Yquant{} code with arbitrary \TeX{} code may lead to certain expectations on how things should work, which may not always be met.
5868 This is mainly due to the fact that \Yquant{} requires two passes of its content (see \cref{sec:pipeline}), as it has to measure the heights and depths of the individual gates.
5869 Similar issues can for example also arise in \pkg{amsmath}'s \texttt{align} environment, which also has a measuring and a shipout stage.
5870 If you run into an incompatibility using a macro from another package (or even plain \TeX), try the following:
5871 \begin{enumerate}
5872 \item Using a \TeX{} macro within \Yquant{} code will stop the \Yquant{} parser.
5873 Hence, all gates following this macro will be ignored.
5874 Did you remember to issue \texlink{\yquant} in order to restart the parser after your macro? \\
5875 \emph{Symptom:} No errors, but gates are missing
5876 \item Is the macro robust?
5877 Modern packages could automatically take care of this by a \tex!\protected! definition, but older ones may not.
5878 Try to prefix the macro by \texlink\protect. \\
5879 \emph{Symptom:} Unexpected error messages
5880 \item Does the macro depend on other macros defined \emph{within} the circuit?
5881 The double pass may lead to problems.
5882 If possible, define your macros outside of the \texlink{yquant} environment.
5883 If this is not possible, make sure the definitions are expandable, then at measurement stage, \Yquant{} will do the expansion, so that you get the correct results. \\
5884 \emph{Symptom:} Only the latest assignment will show up whenever the macro is used; the vertical spacing (for subcircuits, possibly also the horizontal spacing) may even be screwed.
5885 \item Does the macro create output, using its own font?
5886 If the output depends on the current position, this position will be completely wrong.
5887 This is due to the fact that the macro is executed at the first pass only, where \Yquant{} does not know about any positions at all.
5888 \compat{New in 0.6}\linkdef\yquantsecondpass\linkdef\yquantesecondpass
5889 The macro \tex!\yquantsecondpass! will defer its content so that it is executed only at the second pass, where positions are known.
5890 It will also automatically restart the parser.
5891 Note that \tex!\yquantsecondpass! will not expand its content.
5892 If you need expansion, you may use \tex!\yquantesecondpass!, which uses \tex!\protected@edef!. \\
5893 \emph{Symptom:} Content occurring in the wrong place or missing
5894 \item Does the macro need to be executed at both passes?
5895 There may be reasons for this, in particular if you use commands from \pkg{pgf}'s basic or system layer.
5896 However, all custom macros will only be executed once, at the first pass.
5897 \compat{New in 0.6}\linkdef\yquantescape\linkdef\yquanteescape
5898 Wrap the macros in \tex!\yquantescape! in order to execute them both times.
5899 The parser will automatically be restarted afterwards.
5900 Note that \tex!\yquantescape! will not expand its content.
5901 You may use \tex!\yquanteescape!, which will first expand its content using \tex!\protected@edef!.
5902 Note that the content will first be executed, then stored for the second pass. \\
5903 \emph{Symptom:} Content missing
5904 \item If all of this did not work and the use case is interesting enough, please file a bug report.
5905 \end{enumerate}
5906
5907 \subsection{\texorpdfstring{\TikZ}{TikZ}}
5908 \Yquant{} is built on top of \TikZ{} an hence integrates well with \TikZ.
5909 You can use all \tex!\path!-like comands as well as scopes and \Yquant{} will automatically take care of restarting the parser appropriately.
5910 If it does not, this is very likely a bug, please file a report.
5911
5912 \linkdef\yquantset
5913 You should typically not use \tex!\tikzset!, as all changes made by this macro will only be executed in the first pass.
5914 Instead, use \texlink{\yquantset} and change the path appropriately: this macro will first store its argument (using \tex!\protected@edef!) for the second pass and then set the appropriate options also in the first pass.
5915
5916 Note that low\hyp level \pkg{pgf} functions are not altered by \Yquant; this would be highly inefficient.
5917 \compat{New in 0.6}If you need to use them, wrap them in \texlink{\yquantescape} or \texlink\yquanteescape.
5918
5919 \subsection[\texorpdfstring{\pkg{beamer}}{beamer}]{\compat{New in 0.6}\pkg{beamer}}
5920 \Yquant{} integrates with \pkg{beamer} overlays.
5921 This means that you can use the overlay commands \tex!\only!, \tex!\alt!, \tex!\temporal!, \tex!\uncover!, \tex!\visible!, and \tex!\invisible! directly in your \Yquant{} code; the parser will automatically be restarted whenever necessary.
5922 Note that the \texttt{*env} environments (\texttt{onlyenv}, \texttt{altenv}, \texttt{uncoverenv}, \texttt{visibleenv}, and \texttt{invisibleenv}) should \emph{not} be used within \Yquant{} code.
5923
5924 The macros \tex!\pause! and \tex!\uncover! are also supported to some degree (\tex!\uncover! with braces is fully supported).
5925 They should work well in simple circuits, but unexpected results can be expected in more complex scenarios.
5926 If you don't get appropriate results, use the aforementioned macros.
5927
5928 You may also use \tex!\note! inside \Yquant{} circuits.
5929
5930 Note that \Yquant{} does not overwrite the definitions of the \pkg{beamer} macros, which implies that you can also use them \emph{within} gates (e.g., for the \ttlink{value} of a \ttlink{box}).
5931 However, this means that the \Yquant{} parser must be running to detect these macros and take appropriate action.
5932 Hence, if you interrupted the code via some special macros, make sure to restart the parser even if your next macro is, e.g., \tex!\only!.
5933
5934 Finally note that \tex!\only!, \tex!\alt!, and \tex!\temporal! are more special than the usual \Yquant\hyp code\hyp interrupting macros.
5935 They will \emph{not} terminate the group that was opened for the current gate.
5936 As a consequence, you can also use these macros for arguments.
5937 Note that whenever you pass arguments to a gate using the \texttt{[<arguments>] <gate> <registers>;} syntax, the value of \texttt{<arguments>} is directly fed to \tex!\pgfkeys!, which \emph{does not} understand \pkg{beamer} macros.
5938 Hence, you \emph{cannot} use, e.g., \tex!\only! \emph{within} the brackets.
5939 However, you can wrap the arguments including the brackets as a whole in \tex!\only!---this happens before \Yquant{} relinquishes control to \tex!\pgfkeys! and therefore is executed as expected.
5940 Since you can also pass multiple arguments to a gate by repeating \texttt{[<arguments>]}, this easily allows to combine arguments with and arguments without overlays.
5941 \begin{example}[Using overlays for arguments]
5942 \begin{minted}{tex}
5943 % \documentclass{beamer}
5944 \begin{frame}
5945 \begin{tikzpicture}
5946 \begin{yquant}
5947 qubit a;
5948 [fill=yellow]
5949 \alt<2>{[draw=blue]}{[draw=green]}
5950 \only<3>{[ultra thick]}
5951 h a;
5952 \end{yquant}
5953 \end{tikzpicture}
5954 \end{frame}
5955 \end{minted}
5956 The Hadamard gate will always be filled yellow; its line color will be blue on the second frame and green on all other frames.
5957 On the third frame, its line width is dramatically increased.
5958 \end{example}
5959
5960 \compat{New in 0.9}You can also use overlay specifications for enclosing regions: this is as simple as saying \texlink[\enclose]{\enclose<...>}, \texlink[\encloseall]{\encloseall<...>}, or \texlink[\startenclose]{\startenclose<...>}.
5961 Note that you cannot put an overlay specification to \texlink{\stopenclose}---it will automatically use the one from the corresponding \texlink{\startenclose}.
5962 By default, this overlay is enforced using \tex!\uncover!.
5963 You may also change this behavior; but be aware of the fact that if you use the \ttlink[/yquant/enclose/shape/rectangle]{rectangle} shape, an \ttlink{align} gate will be enforced after the enclosed region.
5964 Hence, using \tex!\only! for the overlay may visually alter the circuit by shifting parts.
5965
5966 \begin{option}{enclose/overlay command}!\uncover!
5967 This \pkg{beamer} overlay command is used whenever \texlink{\enclose}, \texlink{\encloseall} or \texlink{\startenclose} is invoked with an overlay specification.
5968 Note that this key cannot be changed using the implicit options given in square brackets after the macros; it has to be changed before the macro invocation to have any effect.
5969 \end{option}
5970
5971 \section{Changelog}
5972 \subsection*{2020-03-15: Version 0.1}
5973 Initial release
5974
5975 \subsection*{2020-03-22: Version 0.1.1}
5976 Complete rewrite of the register name parser.
5977 \Yquant{} now understands comma\hyp separated lists and ranges in indices, and also is far more tolerant with respect to whitespaces. \\
5978 \Yquant{} now also supports non\hyp contiguous vector registers and allows to add new registers into an already existing vector that is not the last register, and also in the unstarred mode.
5979
5980 \subsection*{2020-04-11: Version 0.1.2}
5981 Introduce \ttlink{setstyle} and \ttlink{addstyle} pseudo\hyp gates that allow to style individual wires; rename \ttlink{setwire} to \ttlink{settype} (the old name is still available and shows a deprecation warning). \\
5982 Complete rewrite of the way \Yquant{} draws wires; projection anchors are removed in favor of clipping paths.
5983 This allows perfect connections between gates and wires, even if the (rather rectangular) wire lines meets with nonplanar shapes, while still preserving the possibility of transparent wires. \\
5984 \Yquant{} now also properly draws non\hyp contiguous multi\hyp qubit operations. \\
5985 New gate: \ttlink{correlate}.
5986 Various bug fixes.
5987
5988 \subsection*{2020-06-02: Version 0.2}
5989 Introduce \ttlink{subcircuit}; required rewriting how \Yquant{} internally positions vertically.
5990 Provide simple macros to load circuits (or parts) from a file and to declare own custom gates.
5991
5992 \subsection*{2020-06-07: Version 0.2.1}
5993 Introduce a macro to declare a lightweight custom gate, which is only a single box with custom content.
5994
5995 \subsection*{2020-06-13: Version 0.3}
5996 Introduce support for the \langlink{qasm} language.
5997
5998 \subsection*{2020-07-11: Version 0.3.1}
5999 Add legacy support for very old \TikZ{} versions such as the one used on the arXiv.
6000
6001 \subsection*{2020-08-24: Version 0.3.2}
6002 Fix \href{https://github.com/projekter/yquant/issues/5}{\#5}: Can't draw circuits with more than 9 qubits.
6003
6004 \subsection*{2020-10-27: Version 0.3.3}
6005 Fix \href{https://github.com/projekter/yquant/issues/6}{\#6}: shorten doesn't work for 2-qubit barriers. This fixes a bug in how the \texttt{shorten} keyword worked on \ttlink{barrier}s, which may require re\hyp assessing your chosen values.
6006
6007 \subsection*{2021-02-21: Version 0.4-alpha}
6008 Lots of internal fixes, most notably vertical alignment with subcircuits. \\
6009 Introduce capability to perform vertical alignment with multi\hyp register gates. \\
6010 Dramatic changes under the hood regarding horizontal positioning, which is now only determined in the drawing stage; this paves the way for delayed gates, which are planned for 0.4.
6011 Also changes in the gate declaration interface. \\
6012 Introduce \textlink[compat]{compatibility layer}, so that layout\hyp breaking changes will not become effective unless explicitly requested. \\
6013 Separate register height into a height and depth key. \\
6014 Introduce \ttlink{overlay} key to disable height calculation selectively. \\
6015 Change register style declaration, so that this is now always equivalent to creating an unnamed register followed by an \ttlink{init} gate with the given text.
6016 Note: This may be a \textbf{breaking change} that cannot be compatibility\hyp protected---if you used \TeX{} conditionals involving \texlink{\idx} for creation labels of registers, you will now need to either \texlink{\protect} them all or just capitalize their first letter (which corresponds to auto\hyp\texlink\protect ed versions for \ttlink{compat} at least \texttt{0.4}). \\
6017 Now use nodes for \ttlink{init} and \ttlink{output} gates. \\
6018 Change behavior of \ttlink{hspace} and \ttlink{align}: Now also extend if the wire is discarded afterwards. \\
6019 Introduce the commands \texlink\Ifnum, \texlink\Ifcase, \texlink\Or, \texlink\Else, \texlink\Fi, \texlink\Unless, and \texlink{\The} available for use within gates that behave like auto\hyp\texlink\protect ed versions of their plain \TeX{} equivalents.
6020
6021 \subsection*{2021-03-27: Version 0.4}
6022 New gate: \ttlink{inspect}. Various bug fixes. \\
6023 Introduce the \ttlink[{[direct control]}]{direct control} feature: \ttlink{measure} gates can now substitute positive controls of future gates.
6024
6025 \subsection*{2021-07-03: Version 0.4.1}
6026 Fix \href{https://github.com/projekter/yquant/issues/9}{\#9}: Output bracket misaligned. \\
6027 Fix \href{https://github.com/projekter/yquant/issues/10}{\#10}: Unable to access node in subcircuit. As of this version, named nodes in subcircuits will also be properly aliased if there is only a single target subcircuit (so that you don't need to use the \texttt{-0} suffix for the subcircuit's name).
6028
6029 \subsection*{2021-08-17: Version 0.5}
6030 Improvement: Active outer canvas transformations (\TikZ{} shifts, scalings, rotations) should be supported more nicely (no guarantees!). \\
6031 Improvement: Custom gates (\texlink\yquantdefinegate) can now contain \TikZ{} \tex!\path!\hyp like commands without the \tex!\noexpand! prefix. \\
6032 Improvement: Automatically discard wires \emph{inside} a subcircuit (even if they had the \ttlink{[out]} or \ttlink{[inout]} attribute) if they are discarded directly after the subcircuit \emph{and} they have output gates within the subcircuit (else, the wire would be re\hyp drawn from the output to the border of the subcircuit). \\
6033 Bugfix: Referencing named gates in nested subcircuits now works without producing an error (worked before, but gave errors). \\
6034 Bugfix: Properly handle the \ttlink[{[direct control]}]{direct control} feature if it was specified, but not used until the end of the (sub)circuit. \\
6035 Introduce name mangling options for subcircuits. \\
6036 Implement \href{https://github.com/projekter/yquant/issues/11}{\#11}: Circuit equations. As of this version, the \langlink{groups} language is available that allows to easily implement circuit equations.
6037
6038 \subsection*{2021-09-04: Version 0.5.1}
6039 Bugfix: The \ttlink{xx} gate style was not made available since version~0.4-alpha.
6040
6041 \subsection*{2021-12-28: Version 0.6}
6042 Bugfix: Support to set the \ttlink{/yquant/operators/subcircuit/seamless} property outside of \Yquant{} environments. \\
6043 New: Support for \pkg{beamer}. \\
6044 New macros: \texlink\yquantsecondpass, \texlink\yquantesecondpass, \texlink\yquantescape, and \texlink{\yquanteescape} for more fine\hyp grained control of when to execute macros. \\
6045 Bugfix: the auto\hyp\texlink\protect ed versions \texlink{\Ifnum} etc. now also work in \ttlink{output} gates. \\
6046 New gate: \ttlink{text}. \\
6047 Introduce the \ttlink{/yquant/operators/every rectangular box} style as a common ancestor of gates such as \ttlink{box}, \ttlink{h}, ... instead of using the \ttlink{/yquant/operators/every box} style for this.
6048 As a consequence, boxes can now be styled globally without affecting the other gates.
6049 This new behavior is \textlink[compat]{compatibility\hyp protected}. \\
6050 Bugfix: \ttlink{output}s can now be named. \\
6051 Bugfix: Border angles of \texttt{yquant-circle} now work properly. \\
6052 New configuration: \ttlink{/yquant/register/default lazy name}. \\
6053 Documentation: Replace the wishlist by a section on integration with other packages. \\
6054 Documentation: Include the examples of \pkg{qpic} in this manual.
6055
6056 \subsection*{2022-02-05: Version 0.7}
6057 New: Support for vertical layout. \\
6058 Various bugfixes. \\
6059 Internal change of the loading order of the package files. \\
6060 Fix \href{https://github.com/projekter/yquant/issues/18}{\#18}: Subcircuit boxes don't render in \texlink{yquantgroup} environment.
6061 Now, styles can properly modify the \ttlink{/yquant/operators/this subcircuit box} style without affecting the content of the subcircuit, only the box. \\
6062 New: Support for a simplified drawing pipeline without clipping paths.
6063
6064 \subsection*{2022-05-07: Version 0.7.1}
6065 Fix issues with using handlers for attributes. \\
6066 Fix \href{https://github.com/projekter/yquant/issues/21}{\#21}: CSWAP scaling problems. Now the clipping of the \ttlink{swap} and \ttlink{slash} gate scale appropriately when scaling canvas transformations are in effect (no guarantee with rotations!). Multi-register lines in the same style as control lines (\ttlink{/yquant/operator/multi as single}) will be automatically hidden if control lines are present---they would be drawn on top of each other in the exact same style.
6067
6068 \subsection*{2022-12-24: Version 0.7.2}
6069 Fix \href{https://github.com/projekter/yquant/issues/23}{\#23}: Add the \ttlink{iswap} gate.
6070
6071 \subsection*{2023-01-21: Version 0.7.3}
6072 Fix \href{https://github.com/projekter/yquant/issues/24}{\#24}: Clipping in subcircuits can under very special circumstances fail. Change all clipping paths to a most conservative huge rectangle---before, we tried to be economical.
6073 Also use a more meticulous way of subcircuit position alignment (which will probably consume more resources during compilation, but multiple subcircuits may cancel each other in height and depth calculation if we don't do it). \\
6074 Address \href{https://github.com/projekter/yquant/issues/25}{\#25}: We now provide the styles \ttlink{/tikz/only at} and \ttlink{/tikz/not at} that allow to easily use styles on an multi\hyp register operator that just affect some parts of their register representation. \\
6075 Now the size of controls is taken into account when calculating the size of an operation---so extremely large controls will appropriately enlarge the height/depth or horizontal increment.
6076 This is more useful when putting labels to controls.
6077
6078 \subsection*{2023-03-05: Version 0.7.4}
6079 Fix \href{https://github.com/projekter/yquant/issues/26}{\#26}: Reset all the special groups\hyp related commands that are only valid \emph{directly} in a \texlink{yquantgroup} environment when parsing the arguments of \texlink[groups/registers]{\registers}, \texlink[groups/circuit]{\circuit}, or \texlink[groups/equals]{\equals}.
6080 Most notably, this affects \texlink[groups/linebreak]{\\}, which can now be used properly within a grouped circuit.
6081
6082 \subsection*{2023-11-26: Version 0.7.5}
6083 Fix \href{https://github.com/projekter/yquant/issues/29}{\#29}: Scaling the whole picture would lead to compilation failures and wrong output with subcircuits.
6084
6085 \subsection*{2025-06-15: Version 0.8}
6086 Bugfix: Linebreaks in a \texlink{yquantgroup} environment no longer corrupt future commands. \\
6087 New (\href{https://github.com/projekter/yquant/issues/32}{\#32}): add a possibility to define attributes---and hence, pass parameters---to custom gates. \\
6088 Changed: \texlink{\yquantdefinebox} and \texlink{\yquantdefinegate} now do no longer expand their content at the time of definition.
6089 The expanding behavior can be obtained with the new macros \texlink{\yquantedefinebox} and \texlink{\yquantedefinegate}.
6090 This is \textlink[compat]{compatibility\hyp protected}. \\
6091 Changed: \texlink{\idx} and \texlink{\regidx} will now evaluate only at the time of drawing, giving a correct index when used with discontiguous registers, and in particular, with the \langlink{groups} language.
6092 As a consequence, \texlink{\regidx} is now always contains an implicit \tex!\vphantom[! to have the same vertical extent during preparation as in drawing.
6093 This can lead to larger spacings between very small registers and is therefore compatibility\hyp protected.
6094 To use the newer version with smaller spacing, give the label at creation explicitly (or using \texlink{\reg}). \\
6095 Bugfix: Rarely, calculation of the beginning of a register line did not succeed (leading to a missing line) due to fixed\hyp point inaccuracies; introduce a tolerance to fix this. \\
6096 Bugfix: The documentation PDF was broken in the Git repository, as it was apparently not recognized as binary. \\
6097 New: Introduce ``corner'' gates by allowing \ttlink{init} and \ttlink{discard} to be conditioned on other registers; no longer require a \ttlink{value} attribute for \ttlink{init}. \\
6098 Bugfix: The definition of \texlink{\yquantescape} vs. \texlink{\yquanteescape} was just the opposite of what was documented. \\
6099 Bugfix: Changing global \TikZ{} variables such as the line width might have leaked into other registers and gates. \\
6100 New: add the \texlink[groups/intertext]{\intertext} and \texlink[groups/shortintertext]{\shortintertext} commands in a \texlink{yquantgroup} environment. \\
6101 Bugfix: Always recognize changes in register types in subcircuits (and thus \langlink{groups}). \\
6102 Documentation: The documentation now contains much more links between individual commands.
6103 Maybe in the future, this can even be expanded to code examples?
6104
6105 \subsection*{2025-08-29: Version 0.8.1}
6106 Bugfix: Subcircuits, where a wire with multi\hyp digit internal index had the \textlink{[in]} attribute broke the layout. \\
6107 Bugfix: The \texlink{\idx} and \texlink{\regidx} changes in version~0.8 are now applied to the lazy register creation (\ttlink{/yquant/register/default lazy name}) as well. \\
6108 Bugfix: Allow \tex!\par! in gate definitions once more.
6109
6110 \subsection*{2026-01-03: Version 0.9}
6111 Bugfix: Remove some legacy code that modified \TikZ{} decorations, which has not been used since version~0.1.2 and might cause some issues with other packages (problems with \pkg{pgfplots} and axis discontinuities were encountered).
6112 Fix \href{https://github.com/projekter/yquant/issues/35}{\#35}: Make sure to always advance all involved registers in a subcircuit, including controls. \\
6113 Bugfix: Controlled subcircuits will no longer potentially trigger an internal error. \\
6114 New: \texlink{\enclose}\hyp like macros for conveniently highlighting parts of a circuit. \\
6115 Bugfix: Subcircuits will now work properly even in the presence of global rotation transformations. \\
6116 Bugfix: Subcircuits will in vertical mode now properly account for their box space when calculating register positions.
6117 %END_FOLD
6118 \end{document}