Debian Codegraph @

texmf-dist/doc/latex/zx-calculus/zx-calculus.tex

raw
1 \documentclass[a4paper,doc2]{ltxdoc} % doc2 is needed to force the old version, or links get colored in a weird red way even with hidelinks. https://github.com/latex3/latex2e/issues/822
2
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 %%% Packages
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 %% Warning: if you compile and get:
7 %% ERROR: Argument of \tikz@lib@matrix@with@options has an extra }.
8 %% make sure to fix catcodes around it as | is given a different meaning in ltxdoc.
9
10 \usepackage{amsmath}
11 \usepackage[margin=3cm]{geometry}
12 \usepackage{calc}
13 \usepackage{braket}
14 \usepackage{tikz}
15 \usetikzlibrary{shadows,fit}
16 \usetikzlibrary{snakes}
17 \usetikzlibrary{nfold} % needs very recent CTAN > 2023, or manually copy the .tex files
18 % \usetikzlibrary fails because file is not in current directory, lazy to setup TEXINPUTS
19 \makeatletter
20 \usetikzlibrary{zx-calculus}
21 \makeatother
22 % Loads the great package that produces tikz-like manual (see also tikzcd for examples)
23 \input{pgfmanual-en-macros.tex} % Is supposed to be included in recent TeX distributions, but I get errors...
24 \usepackage{makeidx} % Produces an index of commands.
25 \makeindex % Useful or not index will be created
26 \usepackage{alertmessage} % For warning, info...
27 \usepackage[hidelinks]{hyperref}
28 \newcommand{\mylink}[2]{\href{#1}{#2}\footnote{\url{#1}}}
29 \usepackage{verbatim}
30 \usepackage{cleveref}
31 \usepackage{mathtools}
32 \usepackage{listings}
33 \usepackage [
34 n,
35 lambda,
36 advantage,
37 operators,
38 sets,
39 adversary,
40 landau,
41 probability,
42 notions,
43 logic,
44 ff,
45 mm,
46 primitives,
47 events,
48 complexity,
49 asymptotics,
50 keys
51 ] {cryptocode}
52
53 %%%% Bibliography
54 % I want to have separate references for appendix and main body
55 % https://tex.stackexchange.com/questions/98660/
56 \usepackage[style=trad-alpha,
57 sortcites=true,
58 doi=false,
59 url=false,
60 giveninits=true, % Bob Foo --> B. Foo
61 isbn=false,
62 url=false,
63 eprint=false,
64 sortcites=false, % \cite{B,A,C}: [A,B,C] --> [B,A,C]
65 ]{biblatex}
66 \renewcommand{\multicitedelim}{, } % [ABC96; DEF12] -> [ABC96, DEF12]
67 % [unknown_ref] => [??]
68 % https://tex.stackexchange.com/a/352573
69 \makeatletter
70 \protected\def\abx@missing#1{\textbf{??}}
71 \makeatother
72 \renewcommand*{\bibfont}{\normalfont\small} % BibLatex font seems bigger than Bibtex?
73 \addbibresource{biblio.bib}
74
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %%% Documentation
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78
79 \begin{document}
80 %%% Title: thanks tikzcd for the styling
81 \begin{center}
82 \vspace*{1em} % Thanks tikzcd
83 \tikz\node[scale=1.2]{%
84 \color{gray}\Huge\ttfamily \char`\{\raisebox{.09em}{\textcolor{red!75!black}{zx\raisebox{-0.1em}{-}calculus}}\char`\}};
85
86 \vspace{0.5em}
87 {\Large\bfseries ZX-calculus with \tikzname}
88
89 \vspace{1em}
90 {Léo Colisson \quad Version 2.3}\\[3mm]
91 {\href{https://github.com/leo-colisson/zx-calculus}{\texttt{github.com/leo-colisson/zx-calculus}}}
92 \end{center}
93
94 \tableofcontents
95
96 \section{Introduction}
97
98 This library (based on the great \tikzname{} and \tikzname-cd packages) allows you to typeset ZX-calculus and diagrams for diagrammatic reasoning~\cite{CK17_PicturingQuantumProcesses,van20_ZXcalculusWorkingQuantum} directly in \LaTeX{}. It comes with a default---but highly customizable---style:
99 \begin{codeexample}[]
100 \begin{ZX}
101 \zxZ{\alpha} \arrow[r] & \zxFracX-{\pi}{4}
102 \end{ZX}
103 \end{codeexample}
104 Even if this has not yet been tested a lot, you can also use a ``phase in label'' style, without really changing the code:
105 \begin{codeexample}[]
106 \begin{ZX}[phase in label right]
107 \zxZ{\alpha} \arrow[d] \\
108 \zxFracX-{\pi}{4}
109 \end{ZX}
110 \end{codeexample}
111
112 Since 24/02/2023, we also provide ways to easily create new, highly customizable shapes, with text, anchors, sub-nodes, rotations, and more:
113 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
114 \begin{codeexample}[width=0pt]
115 % Define a reusable node
116 \zxNewNodeFromPic{MyDivider}[][zx create anchors={\zxVirtualCenterWest, \zxVirtualCenterEast},
117 every node/.append style={transform shape}
118 ]{
119 \node[regular polygon, regular polygon sides=3,shape border rotate=90,%shape border rotate=90,
120 draw=black,fill=gray!50, inner sep=1.6pt, rounded corners=0.8mm,zx main node] {};
121 \coordinate(\zxVirtualCenterEast) at (.2mm,0); % Used to start lines on the side of the shape
122 \coordinate(\zxVirtualCenterWest) at (-1mm,0);
123 }
124 % Use the node
125 \begin{ZX}
126 & [2mm] & [3mm] \zxN{} \\[\zxZeroRow]
127 \zxZ[B]{} \rar[Bn'=n+m, wc] & \zxMyDivider{}
128 \rar[<',ru,Bn'Args={n}{pos=.7}]
129 \rar[Bn.Args={m}{pos=.7},<.,rd] &\\[\zxZeroRow]
130 & & \zxN{}
131 \end{ZX}
132 \end{codeexample}
133 }
134
135 Since 2023-09-27, you can also do advanced operations on multi-gate elements or use our default style:
136 \begin{codeexample}[]
137 \begin{ZX}[circuit, thick lines]
138 \rar & \zxGate{H} \rar & \zxGateMulti{2}{3}{H} & & \rar & \\
139 \rar & \zxGate{H} \rar & & & \rar &
140 \end{ZX}
141 \end{codeexample}
142
143 and create more traditional circuits:
144
145 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
146 \begin{codeexample}[width=0pt]
147 \begin{ZX}[circuit, thick lines]
148 \zxInput{\ket{\psi}} \rar & \zxBox{H} \rar & \zxCtrl{} \dar \rar & \zxCross{} \dar \rar
149 & \zxBox[add label={Measure}]{\zxMeter{}} \ar[dr,classical,--|] \\
150 \ar[r] & \zxOCtrl{} \rar \ar[u] & \zxNot{} \rar & \zxCross{} \ar[rr]
151 & & \zxBox{H} \rar & \zxOutput{\ket{\phi}}
152 \end{ZX}
153 \end{codeexample}
154
155 \begin{codeexample}[width=0pt]
156 \begin{ZX}[circuit]
157 \ar[rr] & & [\zxZeroCol] \zxCtrl{} \dar \ar[rr] & [\zxZeroCol] & \\
158 \rar \zxInputMulti{2}{\ket{\psi}} & \zxGateMulti{3}{3}{U} \zxGateInputMulti{2}{x}
159 & & \rar \zxGateOutputMulti{2}{x \oplus y} & \zxOutputMulti{2}{\text{Output}} \\
160 \rar &
161 & & \rar & \\
162 \rar \zxInput{\ket{0}} & \zxGateInput{y}
163 & & \rar \zxGateOutput{y \oplus 1} & \zxOutput{\ket{1}}
164 \end{ZX}
165 \end{codeexample}
166
167 \begin{codeexample}[width=0cm]
168 \begin{ZX}[circuit]
169 \ar[rr] && \zxCtrl{} \ar[dl, end subnode=wrapper] \ar[rr] && \\
170 \rar & \zxCtrl{} \dar \rar \zxGroupGates{2}{3}{below:\textsc{swap}} & \zxNot{} \dar \rar & \zxCtrl{} \dar \rar & \\
171 \rar & \zxNot{} \rar & \zxCtrl{} \rar & \zxNot{} \rar &
172 \end{ZX}
173 \end{codeexample}
174 }
175
176 But this library can be used to create much more complex diagrams, while keeping the matrix easy to read and modify by defining nodes beforehand:
177 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
178 \begin{codeexample}[width=0pt]
179 \begin{ZX}[circuit, thick lines,zx column sep=6mm,mbr=2,
180 /utils/exec={%
181 \def\FwIfFx{
182 \zxGate[a=fna]{\parbox{1.5cm}{\centering\tiny
183 Forward if $c_i$'s $\neq$ abort and $f(x) = \top$, else $\ket{\bot}^{H}$%
184 }%
185 }
186 \rar[latex-,Bn'Args={H}{pos=.60},thick]
187 }%
188 \def\XiSigma{%
189 \zxGate{\xi_\sigma}
190 \zxContName[orange]{(cR)}{below:$\sigma_M$}
191 \rar[latex-,Bn'Args={H}{pos=.6}, thick]%
192 }%
193 \def\InterfaceParties{%
194 \zxInputMulti{1}{\parbox{1.8cm}{\centering\tiny Interface of honests parties}}
195 \rar[latex-,Bn'Args={H}{pos=.50},thick]%
196 }%
197 \def\Verif{%
198 \zxContName{(fna)(G)}{below:$\mathcal{V}_{\ket{G}}$}%
199 }%
200 \def\G{%
201 \zxGate[a=G]{\ket{G}}
202 \ar[rd,|--,-latex,start anchor={[xshift=1mm]south}, Bn'Args={M}{pos=.80},thick]
203 \ar[ld,|--,-latex,start anchor={[xshift=-1mm]south},Bn'Args={H}{pos=.75},thick]
204 \ar[rrd,'>,start anchor=east, end anchor=north,latex-, "c_i = \bot\text{\tiny\ if abort}"]
205 }%
206 \def\FwIfNoAbort{%
207 \zxGate{\parbox{1.2cm}{\centering\tiny Forward if \\no abort}}
208 \rar[latex-,Bn'Args={H}{pos=.6}, thick]%
209 }%
210 \def\PiH{%
211 \zxGate{\pi_H}
212 \rar[latex-latex]%
213 }%
214 \def\Cr{%
215 \zxGate[a=cR]{\mathcal{R}}
216 \rar[latex-latex]%
217 }
218 \def\MultipleParties{%
219 \zxOutputMulti{1}{\parbox{1.5cm}{\centering\tiny Interface of corrupted parties}}%
220 }%
221 \def\XiH{%
222 \zxGate[a=xi]{\xi_H}
223 \ar[rr,latex-,cl,shift left=-2mm,"{x}" {below}]%
224 }%
225 \def\Filter{%
226 \zxGate{\bot_H}%
227 \ar[to=G,"c_i = 0",-latex]
228 }%
229 },
230 ]
231 \Filter & & \Verif & \G & & & & & \\[\zxZeroRow-1mm]
232 \InterfaceParties & \FwIfFx & \XiH & & \XiSigma & \FwIfNoAbort & \PiH & \Cr & \MultipleParties
233 \end{ZX}
234 \end{codeexample}
235 }
236
237 The goal is to provide an alternative to the great |tikzit| package: we wanted a solution that does not require the creation of an additional file, the use of an external software, and which automatically adapts the width of columns and rows depending on the content of the nodes (in |tikzit| one needs to manually tune the position of each node, especially when dealing with large nodes). Our library also provides a default style and tries to separate the content from the style: that way it should be easy to globally change the styling of a given project without redesigning all diagrams. However, it should be fairly easy to combine tikzit and this library: when some diagrams are easier to design in tikzit, then it should be possible to directly load the style of this library inside tikzit.
238
239 This library is quite young, so feel free to propose improvements or report issues on \href{https://github.com/leo-colisson/zx-calculus/issues}{\texttt{github.com/leo-colisson/zx-calculus/issues}}. We will of course try to maintain backward compatibility as much as possible, but we can't guarantee at 100\% that small changes (spacing, wire looks\dots{}) won't be made later. In case you want a completely unalterable style, just copy the two files of this library in your project forever (see installation)!
240
241 \section{Installation}
242
243 \textbf{WARNING:} the versions distributed in the released TexLive 2024 are either too old (the interface has been importantly improved recently), or cannot be loaded due to a bug (you will get an error |ERROR: Missing = inserted for \ifnum| due to a misunderstanding of how to handle \mylink{https://tex.stackexchange.com/questions/713222/providespackage-with-semantic-versionning}{semantic versioning in \LaTeX{}}). CTAN contains now the good version, so you should be able to upgrade your package with |tlmgr|, but it might still be safer to manually install the package (for instance if you plan to submit your article to the arXiv) before TexLive 2025 is out and widely deployed. To manually install this library, simply download (you have a button ``Download raw file'') and copy these two files at the root of your project:
244 \begin{itemize}
245 \item \mylink{https://github.com/leo-colisson/zx-calculus/blob/main/tikzlibraryzx-calculus.code.tex}{\texttt{tikzlibraryzx-calculus.code.tex}}
246 \item \mylink{https://github.com/leo-colisson/zx-calculus/blob/main/zx-calculus.sty}{\texttt{zx-calculus.sty}}
247 \end{itemize}
248
249 Then, you can insert in your file:
250 % verse indents stuff, index adds to the index of command at the end of the file, || is a shortcut of \verb||
251 \begin{verse}
252 \index{zx@\protect\texttt{zx-calculus} package}%
253 \index{Packages and files!zx-calculus@\protect\texttt{zx-calculus}}%
254 |\usepackage{zx-calculus}|%
255 \end{verse}
256 or load \tikzname{} and then use:
257 \begin{verse}%
258 \index{cd@\protect\texttt{zx-calculus} library}%
259 \index{Libraries!cd@\protect\texttt{zx-calculus}}%
260 |\usetikzlibrary{zx-calculus}|%
261 \end{verse}
262
263 \section{Quickstart}
264
265 You can create a diagram either with |\zx[options]{matrix}|, |\zxAmp[options]{matrix}| or with:
266 \begin{verse}
267 |\begin{ZX}[options]|\\
268 | matrix|\\
269 |\end{ZX}|
270 \end{verse}
271 The matrix is composed of rows separated by |\\| and columns separated by |&| (except in |\zxAmp| where columns are separated with |\&|).
272
273 % \alertwarning{ % Can't use |...| or \verb|...| in this environment :'(
274 % Due to \LaTeX{} restrictions, \texttt{\&} can sometimes cause some troubles. \texttt{\textbackslash{}zxAmp} is always guaranteed to work (just make sure to use \texttt{\textbackslash{}\&} instead of \texttt{\&}). See \cref{subsec:addDiagram} for more details.
275 % }
276
277 This matrix is basically a \tikzname{} matrix of nodes (even better, a |tikz-cd| matrix, so you can use all the machinary of |tikz-cd|), so cells can be created using \verb#|[tikz style]| content#. However, the users does not usually need to use this syntax since many nodes like |\zxZ{spider phase}| have been created for them (including |\zxN{}| which is an empty node):
278
279 \begin{codeexample}[width=0pt]
280 \begin{ZX}
281 \zxZ{} & \zxZ{\alpha} & \zxZ-{\alpha} & \zxZ{\alpha+\beta} & \zxFracZ{\pi}{2} & \zxFracZ-{\pi}{2}\\
282 \zxX{} & \zxX{\alpha} & \zxX-{\alpha} & \zxX{\alpha+\beta} & \zxFracX{\pi}{2} & \zxFracX-{\pi}{2}\\
283 \zxN{} & \zxH{}
284 \end{ZX}
285 \end{codeexample}
286
287 Note that if a node has no argument like |\zxN|, you should still end it like |\zxN{}| to make sure you code will be backward compatible and will behave correctly.
288
289 To link the nodes, you should use |\arrow[options]| (|\ar[options]| for short) at the end of a cell (you can put many arrows). The options can contain a direction, made of a string of |r| (for ``right''), |l| (for ``left''), |d| (for `down''), |u| (for ``up'') letters. That way, |\ar[rrd]| would be an arrow going right, right, and down:
290 \begin{codeexample}[]
291 \begin{ZX}
292 \zxZ{} \ar[rrd] & \zxX{}\\
293 & & \zxX-{\alpha}
294 \end{ZX}
295 \end{codeexample}
296 See how the alignment of your matrix helps reading it: in emacs |M-x align| is your friend (or even better, if you are tired of selecting the lines to align, bind \mylink{https://tex.stackexchange.com/a/64566/116348}{this \texttt{align-environment} function} to some shortcuts, like |C-<tab>| and you will just have to do a single key-press to align your matrix).
297
298 You may also encounter some shortcuts, like |\rar| instead of |\ar[r]|. Since straight lines are boring, we created many styles that you can just add in the options. For instance, a measured Bell-pair can be created using the |C| style (note also how the |*| argument forces the node to be tighter), as the name of the style tries to mimic the shape of the wire:
299 \begin{codeexample}[]
300 \begin{ZX}
301 \zxZ*{a \pi} \ar[d,C]\\
302 \zxZ*{b \pi}
303 \end{ZX}
304 \end{codeexample}
305
306 \alertinfo{
307 \textbf{Tips (how to format big diagrams)}: on small diagrams, describing the arrow position using relative position like \texttt{rrb} works really nicely, but on big diagrams that you need to rewrite, it can quickly be hard to manage, as moving a node will break all links to it. While it is possible to specify both |from| and |to| in an absolute way, the experience shows that it is easier to work with a ``semi-relative'' addressing, where only the |to| is specified in an absolute setting, while the |from| is automatically derived from the current position of the arrow. You can use \texttt{a=somename} to give a name for a node (\texttt{a} being the short name of alias).
308 Big diagrams also tend to produce big matrices that do not fit on screen, and are harder to align (for instance emacs will struggle to align automatically the matrix if the nodes also contains |\texttt{\&}| like in matrices), rewrite etc. For this reason, we recommend on big diagrams to define shorter alias before the matrix (just make sure to create them inside the \texttt{ZX} environment via \texttt{/utils/exec} if you plan to be able to easily externalize them):
309 }
310
311 This way, on non-trivial diagrams, we recommend to format graphs like:
312 \begin{codeexample}[]
313 \begin{ZX}[
314 /utils/exec={
315 \def\Zalpha{
316 \zxZ{\alpha} \ar[to=beta,N] \ar[to=gamma,N]
317 }
318 \def\Zbeta{
319 \zxZ[a=beta]{\beta} \ar[to=gamma,C-]
320 }
321 \def\Zgamma{
322 \zxZ[a=gamma]{\gamma}
323 }
324 },
325 ]
326 & \Zbeta \\
327 \Zalpha & \\
328 & \Zgamma
329 \end{ZX}
330 \end{codeexample}
331 This makes it way easier to rewrite as you mostly only need to change the small matrix (and possibly shape of wires):
332 \begin{codeexample}[]
333 \begin{ZX}[
334 /utils/exec={
335 \def\Zalpha{
336 \zxZ{\alpha} \ar[to=beta,N] \ar[to=gamma,N]
337 }
338 \def\Zbeta{
339 \zxZ[a=beta]{\beta} \ar[to=gamma,C-]
340 }
341 \def\Zgamma{
342 \zxZ[a=gamma]{\gamma}
343 }
344 },
345 ]
346 \Zalpha & & \Zgamma \\
347 & \Zbeta &
348 \end{ZX}
349 \end{codeexample}
350 You can of course mix all styles if you find it easier, |\def| and regular nodes and/or arrows, |\ar[rr]|, |\ar[to=foo]|\dots{} Since v2.1, you can also add arguments to def like:
351 \begin{verbatim}
352 \def\myNode#1#2{\zxX[fill=#1]{#2}}
353 \end{verbatim}
354 directly in the |/utils/exec| argument, and use later |\myNode{green}{\alpha}|. If you prefer the xparse-style to define functions, make sure to use the exandable variants like |\NewExpandableDocumentCommand{\foo}{m}{\zxX{#1}}|.
355
356 Note that you can also set |debug mode| to display the name of the nodes for an easier addressing:
357 \begin{codeexample}[]
358 \begin{ZX}[debug mode]
359 % See how the code stayed unchanged despite changing the position of the nodes
360 \zxN{} & \zxZ[a=beta]{\beta} \\
361 \zxZ{\alpha} \ar[to=beta,N] \ar[to=gamma,N] & \\
362 & \\
363 & & \zxZ[a=gamma]{\gamma}
364 \end{ZX}
365 \end{codeexample}
366
367 We also introduce many other styles, like |N| for wires that arrive and leave at wide angle (yeah, the |N| is the best letter I could find to fit that shape):
368 \begin{codeexample}[]
369 \begin{ZX}
370 \zxN{} & \zxZ{\beta}\\
371 \zxZ{\alpha} \ar[ru,N] \ar[rd,N] &\\
372 & \zxZ{\gamma}
373 \end{ZX}
374 \end{codeexample}
375 Or |s| for wires that arrive and leave at sharp angles\footnote{Note: on older versions, a |\textbackslash zxN\{\}| might be needed on the first cell as it seems that the first cell of a matrix can't be empty… this should be fixed in latest versions.}:
376 \begin{codeexample}[]
377 \begin{ZX}
378 & \zxZ{\beta} \\
379 \zxZ{\alpha} \ar[ru,s] \ar[rd,s] & \\
380 & \zxZ{\gamma}
381 \end{ZX}
382 \end{codeexample}
383 You have then different variations of a style depending on the shape and/or direction of it. For instance, if we want the arrival of the |N| wire to be flat, use |N-|:
384 \begin{codeexample}[]
385 \begin{ZX}
386 \zxZ{\alpha} \ar[rd,N-] \\
387 & \zxZ{\beta}
388 \end{ZX}
389 \end{codeexample}
390 Similarly |o'| is a style for wires that have the shape of the top part of the circle, and comes with variations depending on the part of the circle that must be kept:
391 \begin{codeexample}[width=0pt]
392 \begin{ZX}
393 \zxZ{\alpha} \ar[r,o',green] \ar[r,o.,red] \ar[d,o-,blue] \ar[d,-o,purple] & \zxZ{\beta}\\
394 \zxZ{\beta}
395 \end{ZX}
396 \end{codeexample}
397 Note that the position of the embellishments (|'|, |-|, |.|\dots{}) tries to graphically represent the shape of the node. That way |-o| means ``take the left part (position of |-|) of the circle |o|''. Applied to |C|, this gives:
398 \begin{codeexample}[]
399 \begin{ZX}
400 \zxX{} \ar[d,C] \ar[r,C'] & \zxZ{} \ar[d,C-]\\
401 \zxZ{} \ar[r,C.] & \zxX{}
402 \end{ZX}
403 \end{codeexample}
404
405 You also have styles which automatically add another node in between, for instance |H| adds a Hadamard node in the middle of the node:
406 \begin{codeexample}[]
407 \begin{ZX}
408 \zxZ{\alpha} \ar[r,o',H] \ar[r,o.,H] &[\zxHCol] \zxZ{\beta}
409 \end{ZX}
410 \end{codeexample}
411 Note that we used also |&[\zxHCol]| instead of |&| (on the first line). This is useful to add an extra space between the columns to have a nicer look. The same applies for rows (see the |*Row| instead of |*Col|):
412 \begin{codeexample}[]
413 \begin{ZX}
414 \zxZ{\alpha} \ar[d,-o,Z] \ar[d,o-,X] \\[\zxSRow]
415 \zxX{\beta}
416 \end{ZX}
417 \end{codeexample}
418 The reason for this is that it is hard to always get exactly the good spacing by default (for instance here \tikzname{} has no idea that a |H| node will be inserted when it starts to build the diagram), and sometimes the spacing needs some adjustments. However, while you could manually tweak this space using something like |&[1mm]| (it adds |1mm| to the column space), it is better to use some pre-configured spaced that can be (re)-configured document-wise to keep a uniform spacing. You could define your own spacing, but we already provide a list for the most important spacings. They all start with |zx|, then you find the type of space: |H| for Hadamard, |S| for spiders, |W| when you connect only |\zxNone| nodes (otherwise the diagram will be too shrinked), |w| when one side of the row contains only |\zxNone|\dots{} and then you find |Col| (for columns spacing) or |Row| (for rows spacing). For instance we can use the |\zxNone| style (|\zxN| for short) style and the above spacing to obtain this:
419 \begin{codeexample}[]
420 \begin{ZX}
421 \zxN{} \rar &[\zxwCol] \zxH{} \rar &[\zxwCol] \zxN{}
422 \end{ZX}
423 \end{codeexample}
424 \noindent or that:
425 \begin{codeexample}[]
426 \begin{ZX}
427 \zxN{} \ar[d,C] \ar[dr,s] &[\zxWCol] \zxN{} \\[\zxWRow]
428 \zxN{} \ar[ru,s] & \zxN{} \\
429 \end{ZX}
430 \end{codeexample}
431
432
433 When writing equations, you may also want to change the baseline to align properly your diagrams on a given line like that (since march 2023):
434 \begin{codeexample}[]
435 $\zx[mbr=2]{ % mbr is a shortcut for "math baseline row"
436 \zxX{}\\
437 \zxZ[a=myZ]{}
438 }
439 = \zx{\zxX{} & \zxZ{}}$
440 \end{codeexample}
441 You can also specify (this works on older versions) a specific node like that (|a=blabla| gives the alias name |blabla| to the node, and configure tools useful for debugging):
442 \begin{codeexample}[]
443 $\zx[math baseline=myZ]{
444 \zxX{}\\
445 \zxZ[a=myZ]{}
446 }
447 = \zx{\zxX{} & \zxZ{}}$
448 \end{codeexample}
449
450 We also provide easy methods like |phase in label right| to change the labelling of a note (per-node, per-picture or document wise) to move the phase in a label automatically:
451 \begin{codeexample}[]
452 \begin{ZX}[phase in label right]
453 \zxZ{\alpha} \arrow[d] \\
454 \zxFracX-{\pi}{4}
455 \end{ZX}
456 \end{codeexample}
457
458 Now you should know enough to start your first diagrams. The rest of the documentation will go through all the styles, customizations and features, including the one needed to obtain:
459 \begin{codeexample}[width=3cm]
460 \begin{ZX}
461 \leftManyDots{n} \zxX{\alpha} \zxLoopAboveDots{} \middleManyDots{} \ar[r,o'={a=75}]
462 & \zxX{\beta} \zxLoopAboveDots{} \rightManyDots{m}
463 \end{ZX}
464 \end{codeexample}
465 \noindent You will also see some tricks (notably based on alias) to create clear bigger diagrams, like this debug mode which turns
466 {
467 \begin{ZX}[zx row sep=1pt,
468 execute at begin picture={%
469 %%% Definition of long items (the goal is to have a small and readable matrix
470 % (warning: macro can't have numbers in TeX. Also, make sure not to use existing names)
471 \def\Zpifour{\zxFracZ[a=Zpi4]-{\pi}{4}}%
472 \def\mypitwo{\zxFracX[a=mypi2]{\pi}{2}}%
473 }
474 ]
475 %%% Matrix: in emacs "M-x align" is practical to automatically format it. a is for 'alias'
476 & \zxN[a=n]{} & \zxZ[a=xmiddle]{} & & \zxN[a=out1]{} \\
477 \zxN[a=in1]{} & \Zpifour{} & \zxX[a=Xdown]{} & \mypitwo{} & \\
478 & & & & \zxN[a=out2]{} \\
479 \zxN[a=in2]{} & \zxX[a=X1]{} & \zxZ[a=toprightpi]{\pi} & & \zxN[a=out3]{}
480 %%% Arrows
481 % Column 1
482 \ar[from=in1,to=X1,s]
483 \ar[from=in2,to=Zpi4,.>]
484 % Column 2
485 \ar[from=X1,to=xmiddle,N']
486 \ar[from=X1,to=toprightpi,H]
487 \ar[from=Zpi4,to=n,C] \ar[from=n,to=xmiddle,wc]
488 \ar[from=Zpi4,to=Xdown]
489 % Column 3
490 \ar[from=xmiddle,to=Xdown,C-]
491 \ar[from=xmiddle,to=mypi2,)]
492 % Column 4
493 \ar[from=mypi2,to=toprightpi,(']
494 \ar[from=mypi2,to=out1,<']
495 \ar[from=mypi2,to=out2,<.]
496 \ar[from=Xdown,to=out3,<.]
497 \end{ZX} into %
498 {%
499 \def\zxDebugMode{}%%%%
500 \begin{ZX}[zx row sep=1pt,
501 execute at begin picture={%
502 %%% Definition of long items (the goal is to have a small and readable matrix
503 % (warning: macro can't have numbers in TeX. Also, make sure not to use existing names)
504 \def\Zpifour{\zxFracZ[a=Zpi4]-{\pi}{4}}%
505 \def\mypitwo{\zxFracX[a=mypi2]{\pi}{2}}%
506 }
507 ]
508 %%% Matrix: in emacs "M-x align" is practical to automatically format it. a is for 'alias'
509 & \zxN[a=n]{} & \zxZ[a=xmiddle]{} & & \zxN[a=out1]{} \\
510 \zxN[a=in1]{} & \Zpifour{} & \zxX[a=Xdown]{} & \mypitwo{} & \\
511 & & & & \zxN[a=out2]{} \\
512 \zxN[a=in2]{} & \zxX[a=X1]{} & \zxZ[a=toprightpi]{\pi} & & \zxN[a=out3]{}
513 %%% Arrows
514 % Column 1
515 \ar[from=in1,to=X1,s]
516 \ar[from=in2,to=Zpi4,.>]
517 % Column 2
518 \ar[from=X1,to=xmiddle,N']
519 \ar[from=X1,to=toprightpi,H]
520 \ar[from=Zpi4,to=n,C] \ar[from=n,to=xmiddle,wc]
521 \ar[from=Zpi4,to=Xdown]
522 % Column 3
523 \ar[from=xmiddle,to=Xdown,C-]
524 \ar[from=xmiddle,to=mypi2,)]
525 % Column 4
526 \ar[from=mypi2,to=toprightpi,(']
527 \ar[from=mypi2,to=out1,<']
528 \ar[from=mypi2,to=out2,<.]
529 \ar[from=Xdown,to=out3,<.]
530 \end{ZX}
531 } \ (of course it only helps during the construction).\\
532
533 You will also see how you can customize the styles, and how you can easily extend this library to get any custom diagram:
534 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
535 \begin{codeexample}[width=0pt]
536 { % \usetikzlibrary{shadows}
537 \tikzset{
538 my bloc/.style={
539 anchor=center,
540 inner sep=2pt,
541 inner xsep=.7em,
542 minimum height=3em,
543 draw,
544 thick,
545 fill=blue!10!white,
546 double copy shadow={opacity=.5},tape,
547 }
548 }
549 \zx{|[my bloc]| f \rar &[1mm] |[my bloc]| g \rar &[1mm] \zxZ{\alpha} \rar & \zxNone{}}
550 }
551 \end{codeexample}
552 }
553
554 If you have some questions, suggestions, or bugs, please report them on \texttt{\url{https://github.com/leo-colisson/zx-calculus/issues}}.
555
556 \textbf{Tips}: if you are unsure of the definition of a style in an example, just click on it, a link will point to its definition. Also, if your pdf viewer does not copy/paste these examples correctly, you can copy them from the source code of this documentation available \mylink{https://github.com/leo-colisson/zx-calculus/blob/main/doc/zx-calculus.tex}{here} (to find the example, just use the ``search'' function of your web browser).
557
558 \section{Usage}
559
560 \subsection{Add a diagram}\label{subsec:addDiagram}
561 \begin{pgfmanualentry}
562 \extractcommand\zx\opt{\oarg{options}}\marg{your diagram}\@@
563 \extractenvironement{ZX}\opt{\oarg{options}}\@@
564 \extractcommand\zxAmp\opt{\oarg{options}}\marg{your diagram}\@@
565 \pgfmanualbody
566 You can create a new ZX-diagram either with a macro (quicker for inline diagrams) or with an environment. All these commands are mostly equivalent, except that in |\zxAmp| columns are separated with |\&| instead of |&| (this was useful before as |&| was not usable in |align| or inside macros. Now it should be fixed.). The \meta{options} can be used to locally change the style of the diagram, using the same options as the |{tikz-cd}| environment (from the \mylink{https://www.ctan.org/pkg/tikz-cd}{\texttt{tikz-cd} package}). The \meta{your diagram} argument, or the content of |{ZX}| environment is a \tikzname{} matrix of nodes, exactly like in the |tikz-cd| package: rows are separated using |\\|, columns using |&| (except for |\zxAmp| where columns are separated using |\&|), and nodes are created using \verb#|[tikz style]| node content# or with shortcut commands presented later in this document (recommended). Wires can be added like in |tikz-cd| (see more below) using |\arrow| or |\ar|: we provide later recommended styles to quickly create different kinds of wires which can change with the configured style. Content is typeset in math mode by default, and diagrams can be included in any equation.
567 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
568 % Do not indent not to put space in final code
569 \begin{codeexample}[]
570 Spider \zx{\zxZ{\alpha}}, equation $\zx{\zxZ{}} = \zx{\zxX{}}$ %
571 and custom diagram: %
572 \begin{ZX}[red]
573 \zxZ{\beta} \arrow[r] & \zxZ{\alpha} \\
574 |[fill=pink,draw]| \gamma \arrow[ru,bend right]
575 \end{ZX}
576 \end{codeexample}
577 }
578 \end{pgfmanualentry}
579
580 \begin{stylekey}{/zx/defaultEnv/amp}
581 In a previous version (before 2022/02/09), it was not possible to use |&| inside macros and |align| due to \LaTeX{} limitations. However, we found a solution by re-scanning the tokens, so now no special care should be taken in align or macros. But in case you need to deal with an environment having troubles with |&|, either use the |ampersand replacement=\&| option (whose shortcut is |amp|) or |\zxAmp| (in any case, replace |&| with |\&|).
582 \begin{codeexample}[vbox]
583 An aligned equation:
584 \begin{align}
585 \zxAmp{\zxZ{} \arrow[r] \& \zxX{}} &= \begin{ZX}[amp] \zxX{} \arrow[r] \& \zxZ{} \end{ZX}
586 \end{align}
587 This limitation does not apply anymore:
588 \begin{align}
589 \zx{\zxZ{} \arrow[r] & \zxX{}} &= \begin{ZX} \zxX{} \arrow[r] & \zxZ{} \end{ZX}
590 \end{align}
591 even in macros: {\setlength{\fboxsep}{0pt} \fbox{\zx{\zxZ{} \rar & \zxX{}}}}
592 \end{codeexample}
593 \end{stylekey}
594
595 \subsection{Nodes}
596 \subsubsection{Spiders}
597 The following commands are useful to create different kinds of nodes. Always add empty arguments like |\example{}| if none are already present, otherwise if you type |\example| we don't guarantee backward compatibility.
598
599 \begin{command}{\zxEmptyDiagram{}}
600 Create an empty diagram.
601 \begin{codeexample}[width=3cm]
602 \begin{ZX}
603 \zxEmptyDiagram{}
604 \end{ZX}
605 \end{codeexample}
606 \end{command}
607
608
609 \begin{pgfmanualentry}
610 \extractcommand\zxNone\opt{-\textbar+}\marg{text}\@@
611 \extractcommand\zxN\opt{-\textbar+}\marg{text}\@@
612 \extractcommand\zxNL\@@
613 \extractcommand\zxNR\@@
614 \pgfmanualbody
615 Adds an empty node with |\zxNone{}| (alias |\zxN{}|). The \verb#-|+# decorations are used to add a bit of horizontal (\verb#\zxNone-{}#), vertical (\verb#\zxNone|{}#) and both (\verb#\zxNone+{}#) spacing.
616
617 |\zxNone| is just a coordinate (and therefore can't have any text inside, but when possible this node should be preferred over the other versions since it has really zero width), but |\zxNone-{}| and \verb#\zxNone|{}# are actually nodes with |inner sep=0| along one direction. For that reason, they still have a tiny height or width (impossible to remove as far as I know). If you don't want to get holes when connecting multiple wires to them, it is therefore necessary to use |\zxNone{}| or the |wire centered| style (alias |wc|) (if you are using the |IO| mode, see also the |between none| style). But anyway you should mostly use |\zxNone|.
618
619 Moreover, you should also add column and row spacing |&[\zxWCol]| and |\\[\zxWRow]| to avoid too shrinked diagrams when only wires are involved.
620 \begin{codeexample}[width=3cm]
621 \begin{ZX}
622 \zxNone{} \ar[C,d] \ar[rd,s] &[\zxWCol] \zxNone{}\\[\zxWRow]
623 \zxNone{} \ar[ru,s] & \zxNone{}
624 \end{ZX}
625 \end{codeexample}
626 Use |&[\zxwCol]| (on the first line) and/or |\\[\zxwRow]| when a single None node is connected to the wire to add appropriate spacing (this spacing can of course be redefined to your preferences):
627 \begin{codeexample}[]
628 Compare \begin{ZX}
629 \zxN{} \rar & \zxZ{} \rar & \zxN{}
630 \end{ZX} with \begin{ZX}
631 \zxN{} \rar &[\zxwCol] \zxZ{} \rar &[\zxwCol] \zxN{}
632 \end{ZX}
633 \end{codeexample}
634 This kind of code is so common that there is an alias for it: |\zxNL| and |\zxNR| automatically add a |\zxN{}| node, configure the column space (for this reason don't add an additional |&|, and be aware that emacs won't align them properly. Note also that the space will only be taken into account if it is on the first line) and add a straight arrow. The |L/R| part of the name is just to specify if the node is on the right or left of the diagram to put the column and arrow on the right side:
635 \begin{codeexample}[]
636 \zx{\zxNL \zxX{} \zxNR}
637 \end{codeexample}
638 Note that these two alias can be used without |{}|. But they are the only ones.
639
640 The \verb!\zxN|{text}! and \verb!\zxN-{text}! may be useful to display some texts:
641 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
642 \begin{codeexample}[]
643 \begin{ZX}[content fixed baseline]
644 & \zxN|{\dots} \dar \\
645 \zxZ{\theta_i} \rar & \zxZ{} \dar \rar & \zxZ{-(\delta_i+\theta_i+r\pi)}\rar & \zxH{} \rar & \zxX{a\pi} \\
646 & \zxN|{\dots} \\
647 \end{ZX}
648 \end{codeexample}
649 }
650
651 When the top left cell is empty, you may get an error at the compilation |Single ampersand used with wrong catcode| (this error should be fixed in latest releases) or |<symbol> allowed only in math mode| (not sure why). To solve this issue, you can add an empty node on the very first cell:
652 \begin{codeexample}[]
653 \begin{ZX}
654 \zxN{} &[\zxwCol] \zxN{} \ar[d]\\[\zxwRow]
655 \zxNone{} \rar & \zxZ{}
656 \end{ZX}
657 \end{codeexample}
658 \end{pgfmanualentry}
659
660 You may also get the error |Single ampersand used with wrong catcode| when |&| has already a different meaning, for instance in |align|, in that case you may change the |&| character into |\&| using |[ampersand replacement=\&]|. Note however that in recent versions ($\geq$ 2022/02/09) this should not happen anymore.
661 \begin{codeexample}[vbox]
662 \begin{align}
663 \begin{ZX}[ampersand replacement=\&]
664 \zxN{} \rar \&[\zxWCol] \zxN{}
665 \end{ZX}
666 &= \begin{ZX}[ampersand replacement=\&]
667 \zxN{} \rar \&[\zxwCol] \zxZ{} \rar \&[\zxwCol] \zxN{}
668 \end{ZX}\\
669 &= \begin{ZX}[ampersand replacement=\&]
670 \zxN{} \rar \&[\zxwCol] \zxX{} \rar \&[\zxwCol] \zxN{}
671 \end{ZX}
672 \end{align}
673 \end{codeexample}
674
675 \begin{command}{\zxNoneDouble\opt{-\textbar+}\marg{text}}
676 Like |\zxNone|, but the spacing for \verb#-|+# is large enough to fake two lines in only one. Not extremely useful (or one needs to play with |start anchor=south,end anchor=north|).
677 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
678 \begin{codeexample}[width=3cm]
679 \begin{ZX}
680 \zxNoneDouble|{} \ar[r,s,start anchor=north,end anchor=south] \ar[r,s,start anchor=south,end anchor=north] &[\zxWCol] \zxNoneDouble|{}
681 \end{ZX}
682 \end{codeexample}
683 }
684 \end{command}
685
686 \begin{command}{\zxFracZ\opt{-}\marg{numerator}\opt{\oarg{numerator with parens}\oarg{denominator with parens}}\marg{denominator}}
687 Adds a Z node with a fraction, use the minus decorator to add a small minus in front (a normal minus would be too big, but you can configure the symbol).
688 \begin{codeexample}[width=3cm]
689 \begin{ZX}
690 \zxFracZ{\pi}{2} & \zxFracZ-{\pi}{2}
691 \end{ZX}
692 \end{codeexample}
693 The optional arguments are useful when the numerator or the denominator need parens when they are written inline (in that case optional arguments must be specified): it will prove useful when using a style that writes the fraction inline, for instance the default style for labels:
694 \begin{codeexample}[]
695 Compare %
696 \begin{ZX}
697 \zxFracZ{a+b}[(a+b)][(c+d)]{c+d}
698 \end{ZX} with %
699 \begin{ZX}[phase in label right]
700 \zxFracZ{a+b}[(a+b)][(c+d)]{c+d}
701 \end{ZX}
702 \end{codeexample}
703 \end{command}
704
705 \begin{command}{\zxFracX\opt{-}\marg{numerator}\marg{denominator}}
706 Adds an X node with a fraction.
707 \begin{codeexample}[width=3cm]
708 \begin{ZX}
709 \zxFracX{\pi}{2} & \zxFracX-{\pi}{2}
710 \end{ZX}
711 \end{codeexample}
712 \end{command}
713
714 \begin{command}{\zxZ\opt{\oarg{other styles}*-}\marg{text}}
715 Adds a Z node. \meta{other styles} are optional \tikzname{} arguments (the same as the one provided to |tikz-cd|) They should be use with care, and if possible moved to the style directly to keep a consistent look across the paper.
716 \begin{codeexample}[width=3cm]
717 \begin{ZX}
718 \zxZ{} & \zxZ{\alpha} & \zxZ{\alpha + \beta} & \zxZ[fill=blue!50!white,text=red]{(a \oplus b)\pi}
719 \end{ZX}
720 \end{codeexample}
721 The optional |-| optional argument is to add a minus sign (customizable, see |\zxMinusInShort|) in front of a very short expression and try to keep a circular shape. This is recommended notably for single letter expressions.
722 \begin{codeexample}[width=3cm]
723 Compare \zx{\zxZ{-\alpha}} with \zx{\zxZ-{\alpha}}. Labels:
724 \zx[pila]{\zxZ{-\alpha}} vs \zx[pila]{\zxZ-{\alpha}}.
725 \end{codeexample}
726 The |*| optional argument is to force a condensed style, no matter what is the text inside. This can be practical \emph{sometimes}:
727 \begin{codeexample}[width=3cm]
728 Compare \zx{\zxN{} \rar &[\zxwCol] \zxZ{a\pi}} with \zx{\zxN{} \rar &[\zxwCol] \zxZ*{a\pi}}.
729 \end{codeexample}
730 \noindent but you should use it as rarely as possible (otherwise, change the style directly). See that it does not always give nice results:
731 \begin{codeexample}[width=3cm]
732 Compare \zx{\zxZ{-\alpha} \rar & \zxZ{\alpha+\beta}}
733 with \zx{\zxZ*{-\alpha} \rar & \zxZ*{\alpha+\beta}}.
734 Labels:
735 \zx[pila]{\zxZ{-\alpha} \rar & \zxZ{\alpha+\beta}}
736 vs \zx[pila]{\zxZ*{-\alpha} \rar & \zxZ*{\alpha+\beta}}.
737 \end{codeexample}
738 \end{command}
739
740
741 \begin{command}{\zxX\opt{\oarg{other styles}*-}\marg{text}}
742 Adds an X node, like for the Z node.
743 \begin{codeexample}[width=3cm]
744 \begin{ZX}
745 \zxX{} & \zxX{\alpha} & \zxX-{\alpha} & \zxX{\alpha + \beta}
746 & \zxX[text=green]{(a \oplus b)\pi}
747 \end{ZX}
748 \end{codeexample}
749 \end{command}
750
751 \begin{command}{\zxH\opt{\oarg{other styles}}}
752 Adds an Hadamard node. See also |H| wire style.
753 \begin{codeexample}[width=3cm]
754 \begin{ZX}
755 \zxNone{} \rar & \zxH{} \rar & \zxNone{}
756 \end{ZX}
757 \end{codeexample}
758 \end{command}
759
760
761
762 \begin{command}{\leftManyDots\opt{\oarg{text scale}\oarg{dots scale}}\marg{text}}
763 Shortcut to add a dots and a text next to it. It automatically adds the new column, see more examples below. Internally, it uses |3 dots| to place the dots, and can be reproduced using the other nodes around. Note that this node automatically adds a new cell, so you should \emph{not} use |&|.
764 \begin{codeexample}[]
765 \begin{ZX}
766 \leftManyDots{n} \zxX{\alpha}
767 \end{ZX}
768 \end{codeexample}
769 \end{command}
770
771 \begin{command}{\leftManyDots\opt{\oarg{text scale}\oarg{dots scale}}\marg{text}}
772 Shortcut to add a dots and a text next to it. It automatically adds the new column, see more examples below.
773 \begin{codeexample}[width=3cm]
774 \begin{ZX}
775 \zxX{\alpha} \rightManyDots{m}
776 \end{ZX}
777 \end{codeexample}
778 \end{command}
779
780 \begin{command}{\middleManyDots{}}
781 Shortcut to add a dots and a text next to it, see more examples below.
782 \begin{codeexample}[width=3cm]
783 \begin{ZX}
784 \zxX{\alpha} \middleManyDots{} & \zxX{\beta}
785 \end{ZX}
786 \end{codeexample}
787 \end{command}
788
789 \begin{command}{\zxLoop\opt{\oarg{direction angle}\oarg{opening angle}\oarg{other styles}}}
790 Adds a loop in \meta{direction angle} (defaults to $90$), with opening angle \meta{opening angle} (defaults to $20$).
791 \begin{codeexample}[width=3cm]
792 \begin{ZX}
793 \zxX{\alpha} \zxLoop{} & \zxX{} \zxLoop[45]{} & \zxX{} \zxLoop[0][30][red]{}
794 \end{ZX}
795 \end{codeexample}
796 \end{command}
797
798 \begin{command}{\zxLoopAboveDots\opt{\oarg{opening angle}\oarg{other styles}}}
799 Adds a loop above the node with some dots.
800 \begin{codeexample}[width=3cm]
801 \begin{ZX}
802 \zxX{\alpha} \zxLoopAboveDots{}
803 \end{ZX}
804 \end{codeexample}
805 \end{command}
806
807 \noindent The previous commands can be useful to create this figure:
808 \begin{codeexample}[width=0pt]% Forces code/example on two lines.
809 \begin{ZX}
810 \leftManyDots{n} \zxX{\alpha} \zxLoopAboveDots{} \middleManyDots{} \ar[r,o'={a=75}]
811 & \zxX{\beta} \zxLoopAboveDots{} \rightManyDots{m}
812 \end{ZX}
813 \end{codeexample}
814
815
816 \begin{pgfmanualentry}
817 \makeatletter
818 \def\extrakeytext{style, }
819 \extractkey/zx/styles/rounded style/content vertically centered\@nil%
820 \extractkey/zx/styles/rounded style/content fixed baseline\@nil%
821 \extractkey/zx/styles/rounded style preload/content vertically centered\@nil%
822 \extractkey/zx/styles/rounded style preload/content fixed baseline\@nil%
823 \extractkey/zx/styles/rounded style preload/content fixed also frac\@nil%
824 \makeatother
825 \pgfmanualbody
826 By default the content of the nodes are vertically centered. This can be nice to have as much space as possible around the text, but when using several nodes with letters having different height or depth, the baseline of each node won't be aligned (this is particularly visible on nodes with very high text, like |b'|):
827 \begin{codeexample}[width=0pt]
828 \begin{ZX}
829 \zxX[a=start]{\alpha} & \zxX{\beta} & \zxX{a} & \zxX{b} & \zxX*{a\pi} & \zxX*{b\pi}
830 & \zxX*{b'\pi} & \zxZ*{'b\pi} & \zxZ{(a \oplus b )\pi} & \zxFracX-{\pi}{4}
831 & \zxFracZ{\pi}{4} & \zxZ-{\delta} & \zxZ[a=end]-{\gamma}
832 \ar[from=start,to=end,on layer=background]
833 \end{ZX}
834 \end{codeexample}
835 Using |content fixed baseline|, it is however possible to fix the height and depth of the text to make sure the baselines are aligned (|content vertically centered| is use to come back to the default behavior). When used as a ZX option, it avoids setting this on fractions since it renders poorly. Use |content fixed baseline also frac| if you also want to fix the baseline of all fractions as well (this last style is useful only as a ZX option, since |content fixed baseline| works on all nodes).
836 \begin{codeexample}[width=0pt]
837 \begin{ZX}[content fixed baseline]
838 \zxX[a=start]{\alpha} & \zxX{\beta} & \zxX{a} & \zxX{b} & \zxX*{a\pi} & \zxX*{b\pi}
839 & \zxX*{b'\pi} & \zxZ*{'b\pi} & \zxZ{(a \oplus b )\pi} & \zxFracX-{\pi}{4}
840 & \zxFracZ{\pi}{4} & \zxZ-{\delta} & \zxZ[a=end]-{\gamma}
841 \ar[from=start,to=end,on layer=background]
842 \end{ZX}
843 \end{codeexample}
844 Note however that the height is really hardcoded (not sure how to avoid that) and is quite small (otherwise nodes quickly become too large), so too large content may overlay on top of the node (this is visible on the |'b\pi| node). You can use this style either on a per-picture basis (it's what we just did), on a per-node basis (just use it in the options of the node), or globally:
845 \begin{codeexample}[width=0pt]
846 \tikzset{
847 /zx/user overlay/.style={
848 content fixed baseline,
849 },
850 }
851 \begin{ZX}
852 \zxX[a=start]{\alpha} & \zxX{\beta} & \zxX{a} & \zxX{b} & \zxX*{a\pi} & \zxX*{b\pi}
853 & \zxX*{b'\pi} & \zxZ*{'b\pi} & \zxZ{(a \oplus b )\pi} & \zxFracX-{\pi}{4}
854 & \zxFracZ{\pi}{4} & \zxZ-{\delta} & \zxZ[a=end]-{\gamma}
855 \ar[from=start,to=end,on layer=background]
856 \end{ZX}
857 \end{codeexample}
858 It can also be practical to combine it with |small minus|:
859 \begin{codeexample}[]
860 \begin{ZX}
861 \zxZ-{\delta_j} & \zxZ[content fixed baseline]-{\delta_j} &
862 \zxZ[small minus]-{\delta_j} & \zxZ[content fixed baseline,small minus]-{\delta_j}
863 \end{ZX}
864 \end{codeexample}
865 \end{pgfmanualentry}
866
867 \subsubsection{Phase in label style}
868
869 We also provide styles to place the phase on a label next to an empty node (not yet very well tested):
870
871 \begin{pgfmanualentry}
872 \makeatletter
873 \def\extrakeytext{style, }
874 \extractkey/zx/styles/rounded style/phase in content\@nil%
875 \extractkey/zx/styles/rounded style/phase in label=style (default {})\@nil%
876 \extractkey/zx/styles/rounded style/pil=style (default {})\@nil%
877 \extractkey/zx/styles/rounded style/phase in label above=style (default {})\@nil%
878 \extractkey/zx/styles/rounded style/pila=style (default {})\@nil%
879 \extractkey/zx/styles/rounded style/phase in label below=style (default {})\@nil%
880 \extractkey/zx/styles/rounded style/pilb=style (default {})\@nil%
881 \extractkey/zx/styles/rounded style/phase in label right=style (default {})\@nil%
882 \extractkey/zx/styles/rounded style/pilr=style (default {})\@nil%
883 \extractkey/zx/styles/rounded style/phase in label left=style (default {})\@nil%
884 \extractkey/zx/styles/rounded style/pill=style (default {})\@nil%
885 \makeatother
886 \pgfmanualbody
887 The above styles are useful to place a spider phase in a label outside the node. They can either be put on the style of a node to modify a single node at a time:
888 \begin{codeexample}[]
889 \zx{\zxX[phase in label]{\alpha} \rar & \zxX{\alpha}}
890 \end{codeexample}
891 \noindent It can also be configured on a per-figure basis:
892 \begin{codeexample}[]
893 \zx[phase in label right]{
894 \zxZ{\alpha} \dar \\
895 \zxX{\alpha} \dar \\
896 \zxZ{}}
897 \end{codeexample}
898 \noindent or globally:
899 \begin{codeexample}[]
900 \tikzset{
901 /zx/user overlay/.style={
902 phase in label={label position=-45, text=purple,fill=none}
903 }
904 }
905 \zx{
906 \zxFracX-{\pi}{2}
907 }
908 \end{codeexample}
909 Note that we must use |user post preparation labels| and not |/zx/user overlay nodes| because this will be run after all the machinery for labels has been setup.
910
911 While |phase in content| forces the content of the node to be inside the node instead of inside a label (which is the default behavior), all other styles are special cases of |phase in label|. The \meta{style} parameter can be any style made for a tikz label:
912 \begin{codeexample}[width=3cm]
913 \zx{
914 \zxX[phase in label={label position=45, text=purple}]{\alpha}
915 }
916 \end{codeexample}
917
918 For ease of use, the special cases of label position |above|, |below|, |right| and |left| have their respective shortcut style. The |pil*| versions are shortcuts of the longer style written above. For instance, |pilb| stands for |phase in label below|. Note also that by default labels will take some space, but it's possible to make them overlay without taking space using the |overlay| label style\dots{} however do it at your own risks as it can overlay the content around (also the text before and after):
919 \begin{codeexample}[width=0pt]
920 \zx{
921 \zxZ[pilb]{\alpha+\beta} \rar & \zxX[pilb]{\gamma} \rar & \zxZ[pilb=overlay]{\gamma+\eta}
922 }
923 \end{codeexample}
924 The above also works for fractions:
925 \begin{codeexample}[]
926 \zx{\zxFracX[pilr]-{\pi}{2}}
927 \end{codeexample}
928 For fractions, you can configure how you want the label text to be displayed, either in a single line (default) or on two lines, like in nodes. The function |\zxConvertToFracInLabel| is in charge of that conversion, and can be changed to your needs to change this option document-wise. To use the same notation in both content and labels, you can do:
929 \begin{codeexample}[width=0pt]
930 Compare
931 \begin{ZX}[phase in label right]
932 \zxFracZ{\pi}{2} \dar \\
933 \zxFracZ{a+b}[(a+b)][(c+d)]{c+d}
934 \end{ZX} with
935 {\RenewExpandableDocumentCommand{\zxConvertToFracInLabel}{mmmmm}{
936 \zxConvertToFracInContent{#1}{#2}{#3}{#4}{#5}%
937 }
938 \begin{ZX}[phase in label right]
939 \zxFracZ{\pi}{2} \dar \\
940 \zxFracZ{a+b}[(a+b)][(c+d)]{c+d}
941 \end{ZX} (exact same code!)
942 }
943 \end{codeexample}
944 Note that in |\zxFracZ{a+b}[(a+b)][(c+d)]{c+d}| the optional arguments are useful to put parens appropriately when the fraction is written inline.
945 \end{pgfmanualentry}
946
947 \begin{stylekey}{/zx/defaultEnvdebug mode}
948 If this macro is defined, debug mode is active. See below how it can be useful (here is a quick example).
949 \begin{codeexample}[width=3cm]
950 \begin{ZX}[debug mode]
951 \rar[B] & [\zxwCol] \zxDivider[a=divTop]{} & \\
952 & & \zxZ[a=Ztopleft]{}
953 \ar[from=divTop, to=Ztopleft]
954 \end{ZX}
955 \end{codeexample}
956 \end{stylekey}
957
958 \begin{command}{\zxDebugMode{}}
959 If this macro is defined, debug mode is active. See below how it can be useful.
960 \end{command}
961 \begin{stylekey}{/tikz/every node/a=alias}
962 Shortcut to add an |alias| to a wire, and in debug mode it also displays the alias of the nodes next to it (very practical to quickly add wires as we will see later). To enable debug mode, just type |\def\zxDebugMode{}| before your drawing, potentially in a group like |{\def\zxDebugMode{} your diagram...}| if you want to apply it to a single diagram.
963
964 This will be very practical later when using names instead of directions to connect wires (this can improve readability and maintainability). This is added automatically in |/tikz/every node| style. Note that debug mode is effective only for |a| and not |alias|.
965 \begin{codeexample}[width=3cm]
966 \begin{ZX}
967 \zxX[a=A]{} & \zxZ[a=B]{\beta}
968 \ar[from=A,to=B]
969 \end{ZX}
970 {\def\zxDebugMode{} %% Enable debug mode for next diagram%
971 \begin{ZX}
972 \zxX[a=A]{} & \zxZ[a=B]{\beta}
973 \ar[from=A,to=B]
974 \end{ZX}
975 }
976 \end{codeexample}
977 \end{stylekey}
978
979 \begin{stylekey}{/zx/defaultEnv/math baseline=node alias}
980 You can easily change the default baseline which defaults to:
981 \begin{verse}
982 |baseline={([yshift=-axis_height]current bounding box.center)}|
983 \end{verse}
984 (|axis_height| is the distance to use to center equations on the ``mathematical axis'') by using this in the \meta{options} field of |\zx[options]{...}|. However, this can be a bit long to write, so |math baseline=yourAlias| is a shorcut to |baseline={([yshift=-axis_height]yourAlias)}|:
985 \begin{codeexample}[width=0pt]
986 Compare $\begin{ZX}
987 \leftManyDots{n} \zxX{\alpha} \zxLoopAboveDots{} \middleManyDots{} \ar[r,o'={a=75}]
988 & \zxX{\beta} \zxLoopAboveDots{} \rightManyDots{m}
989 \end{ZX}
990 = {\def\zxDefaultSoftAngleS{20} % useful to make the angle in \leftManyDots{} nicer.
991 \begin{ZX}
992 \leftManyDots{n} \zxX{\alpha+\beta} \rightManyDots{m}
993 \end{ZX}}$ with $\begin{ZX}[math baseline=wantedBaseline]
994 \leftManyDots{n} \zxX{\alpha} \zxLoopAboveDots{} \middleManyDots{} \ar[r,o'={a=75}]
995 %% See here --v the node chosen as the baseline
996 & \zxX[a=wantedBaseline]{\beta} \zxLoopAboveDots{} \rightManyDots{m}
997 \end{ZX}
998 = {\def\zxDefaultSoftAngleS{20} % useful to make the angle in \leftManyDots{} nicer.
999 \begin{ZX}
1000 \leftManyDots{n} \zxX{\alpha+\beta} \rightManyDots{m}
1001 \end{ZX}}$
1002 \end{codeexample}
1003 Also, if you find your diagram a bit ``too high'', check that you did not forget to remove a trailing |\\| at the end of the last line:
1004 \begin{codeexample}[width=3cm]
1005 Compare $\begin{ZX}
1006 \zxZ{} \rar[o'] \rar[o.] & \zxX{}\\
1007 \zxZ{} \rar[o'] \rar[o.] \rar & \zxX{}\\ %% <--- remove last \\
1008 \end{ZX} = \zx{\zxEmptyDiagram}$ with $\begin{ZX}
1009 \zxZ{} \rar[o'] \rar[o.] & \zxX{}\\
1010 \zxZ{} \rar[o'] \rar[o.] \rar & \zxX{}
1011 \end{ZX} = \zx{\zxEmptyDiagram}$
1012 \end{codeexample}
1013 \end{stylekey}
1014
1015 \begin{pgfmanualentry}
1016 \def\extrakeytext{style, }
1017 \makeatletter% should not be letter for \@@... strange
1018 \extractkey/zx/defaultEnv/math baseline row=row to center (default {1})\@nil%
1019 \extractkey/zx/defaultEnv/mbr=row to center (default {1})\@nil%
1020 \makeatother
1021 You can also choose directly a line to center on: for instance to center on the first line, use: |math baseline row=1|, or, equivalently |mbr=1| or directly |mbr|:
1022 \begin{codeexample}[width=0pt]
1023 $\begin{ZX}[mbr]
1024 \zxN{} \rar[B] & [\zxwCol] \zxMatrix{A} \rar[B] &[\zxwCol] \zxN{}
1025 \end{ZX} = A$
1026 \end{codeexample}
1027 Since 2023/10/03 you can also use a float value to specify that you want to set the center between two lines:
1028 \begin{codeexample}[width=0pt]
1029 Compare $G = \begin{ZX}[circuit]
1030 \rar & \zxGate{G} \rar & \zxGate{H} \rar & \\
1031 \ar[rrr] & & &
1032 \end{ZX}$ with $G = \begin{ZX}[circuit,mbr=1.5]
1033 \rar & \zxGate{G} \rar & \zxGate{H} \rar & \\
1034 \ar[rrr] & & &
1035 \end{ZX}$.
1036 \end{codeexample}
1037 \end{pgfmanualentry}
1038
1039 \subsubsection{Ground}
1040
1041 \textbf{NB}: this functionality, based on custom nodes (\cref{subsec:customNodes}) was added on 13/03/2023.
1042
1043 \begin{pgfmanualentry}
1044 \def\extrakeytext{style, }
1045 \extractcommand\zxGround\oarg{picture style}\opt{.}\opt{-}\opt{'}\marg{}\@@
1046 \extractcommand\zxGroundScale\@@
1047 \pgfmanualbody
1048 The ground symbol can be used to denote a discarding operation… but is also useful to compute the norm of a state or denote a measurement by first copying the state and discarding one copy. This way, a measurement in the computational basis can be represented as:
1049 \begin{codeexample}[width=0pt]
1050 \begin{ZX}
1051 & [\zxwCol] & [\zxwCol] \zxN{} \rar & \zxN{} \\
1052 \zxN{} \rar & \zxZ{} \ar[ur,<'] \ar[dr,<'] & \\[\zxZeroRow]
1053 & & \zxGround{}
1054 \end{ZX}
1055 \end{codeexample}
1056 You can also change the direction using the alternative names:
1057 \begin{codeexample}[]
1058 \begin{ZX}
1059 \zxGround-{}\rar & \zxGround{} \\
1060 \zxGround'{}\dar\\
1061 \zxGround.{}
1062 \end{ZX}
1063 \end{codeexample}
1064 Internally, the ground symbol is drawn using a |pic|, so you can customize it as any other pic:
1065 \begin{codeexample}[]
1066 \begin{ZX}
1067 \zxN{} \rar &[\zxwCol] \zxGround[scale=1.5,red,rotate=45]{}
1068 \end{ZX}
1069 \end{codeexample}
1070 Note that you can also redefine |\def\zxGroundScale{1.8}| to change the default scale on a whole document.
1071 Moreover, by default the |pic| takes some space (this way it will not overlap with the next symbol, or the text below/after), but you want sometimes to make it |overlay|, for instance to preserve the symmetry with an empty wire (then, you might need to add some column space |&[yourspace]| or row space |\\[yourspace]| to avoid overlap with text around it):
1072 \begin{codeexample}[width=0pt]
1073 Compare
1074 \begin{ZX}
1075 & [\zxwCol] & [\zxwCol] \zxN{} \rar & \zxN{} \\
1076 \zxN{} \rar & \zxZ{} \ar[ur,<'] \ar[dr,<'] & \\
1077 & & \zxGround[overlay]{}
1078 \end{ZX}
1079 with
1080 \begin{ZX}
1081 & [\zxwCol] & [\zxwCol] \zxN{} \rar & \zxN{} \\
1082 \zxN{} \rar & \zxZ{} \ar[ur,<'] \ar[dr,<'] & \\
1083 & & \zxGround{}
1084 \end{ZX}
1085 \end{codeexample}
1086 \end{pgfmanualentry}
1087
1088 \subsubsection{Scalable ZX}
1089
1090 We provide some notations coming from the scalable ZX calculus~\cite{CHP19_SZXcalculusScalableGraphical}.
1091
1092 \begin{pgfmanualentry}
1093 \def\extrakeytext{style, }
1094 \extractcommand\zxDivider\oarg{picture style}\oarg{picture style}\opt{.}\opt{-}\opt{'}\marg{}\@@
1095 \pgfmanualbody
1096 Dividers can be used to split (or gather) groups of wires. The |.|,|-|,|'| modifiers are used, respectively, to denote the bottom/right/top versions:
1097 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1098 \begin{codeexample}[width=0pt]
1099 We provide some pre-defined symbols in both horizontal: %
1100 \begin{ZX}
1101 \zxZ[B]{} \rar[Bn'=n+m, wc] &[\zxwCol] \zxDivider{}
1102 \rar[o',Bn'Args={n}{}]
1103 \rar[o.,Bn.Args={m}{}] &[\zxWCol] \zxDivider-{} \rar[B,wc] & \zxZ[B]{}
1104 \end{ZX}
1105 and vertical mode:
1106 \begin{ZX}
1107 \zxZ[B]{} \dar[Bn=n+m, wc] \\[\zxwRow]
1108 \zxDivider'{} \dar[-o,BnArgs={n}{}] \dar[o-,Bn-Args={m}{}] \\[\zxWRow]
1109 \zxDivider.{} \dar[B,wc]\\
1110 \zxZ[B]{}
1111 \end{ZX}
1112 \end{codeexample}
1113 }
1114 \end{pgfmanualentry}
1115
1116
1117 \begin{pgfmanualentry}
1118 \def\extrakeytext{style, }
1119 \extractcommand\zxMatrix\oarg{pic style}\oarg{node style}\opt{.}\opt{-}\opt{'}\opt{/}\opt{*}\opt{\_\{text in pmatrix\}}\marg{matrix name}\@@
1120 \pgfmanualbody
1121 Matrices are represented using arrows: |matrix name| is the content of the label of the node:
1122 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1123 \begin{codeexample}[]
1124 \begin{ZX}
1125 \zxN{} \rar & \zxMatrix{A} \rar & \zxN{}
1126 \end{ZX}
1127 \end{codeexample}
1128 }
1129 The |*| option is used to reverse the direction of the arrow, typically for the transpose (note how we can give an alias (with |a| in |node style|) to combine with |math baseline|, or its quicker variants |mbr=nb line to center on| to properly vertically align the node):
1130 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1131 \begin{codeexample}[width=0pt]
1132 We define %
1133 $\begin{ZX}[mbr]
1134 \zxN{} \rar[B] & \zxMatrix*{A} \rar[B] & \zxN{}
1135 \end{ZX} \coloneqq
1136 \begin{ZX}[mbr=2]
1137 & [\zxWCol] \zxN{} \ar[B,dr,s] & [\zxWCol] \zxN{} \\[\zxZeroCol+.3mm]
1138 \ar[B,dr,s] & \zxMatrix{A} \ar[B,u,C] \ar[B,d,C-] & \\
1139 & & \zxN{} \\
1140 \end{ZX}$.
1141 \end{codeexample}
1142 }
1143 The position of the label can be changed with |-| is in horizontal wires, and in vertical wires we use |'| and |.| (putting the label respectively on right and left).
1144 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1145 \begin{codeexample}[width=0pt]
1146 \begin{ZX}
1147 \zxN{} \rar & \zxMatrix{} \rar & \zxMatrix{A} \rar & \zxMatrix*{A} \rar
1148 & \zxMatrix-{A} \rar & \zxMatrix-*{A} \rar & \zxN{}
1149 \end{ZX}
1150 \end{codeexample}
1151 }
1152 Similarly in horizontal wires:
1153 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1154 \begin{codeexample}[]
1155 \begin{ZX}
1156 \zxN{} \dar \\
1157 \zxMatrix'{} \dar \\
1158 \zxMatrix.{A} \dar \\
1159 \zxMatrix'*{A} \dar \\
1160 \zxMatrix'{A} \dar \\
1161 \zxMatrix.*{A} \dar \\
1162 \zxN{}
1163 \end{ZX}
1164 \end{codeexample}
1165 }
1166 If you want to change the position of the label to a more advanced position (e.g.\ with an angle), the simpler solution is to add |yourAngle:| in front of the label (see tikz labels for more details):
1167 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1168 \begin{codeexample}[]
1169 \begin{ZX}
1170 \zxZ{} \rar & \zxMatrix{45:A} \rar & \zxN{}
1171 \end{ZX}
1172 \end{codeexample}
1173 }
1174 Note that it might be useful to put the label as an overlay using |/| (i.e. it is not counted in the bounding box of the cell, and might overlap with content around) in order to reduce space if we know there is nothing on the nearby cell: (we can also manually change the row/colum sep with negative values… but it might be better to avoid this kind of manual tweaks):
1175 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1176 \begin{codeexample}[]
1177 Compare %
1178 \begin{ZX}
1179 \zxZ{} \rar \dar & \zxMatrix-{A} \rar & \zxN{} \\
1180 \zxX{}
1181 \end{ZX} with %
1182 \begin{ZX}
1183 \zxZ{} \rar \dar & \zxMatrix-/{A} \rar & \zxN{} \\
1184 \zxX{}
1185 \end{ZX}
1186 \end{codeexample}
1187 }
1188 It is also useful to put a |pmatrix| inside. While it is possible to write the full |pmatrix|, we can use the |_{}| embelishment to automatically wrap the text with |\begin{bmatrix} \end{bmatrix}|:
1189 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1190 \begin{codeexample}[width=0pt]
1191 This %
1192 \begin{ZX}[mbr]
1193 \zxN{} \rar & \zxMatrix_{A & B \\ C & D}{} \rar & \zxN{}
1194 \end{ZX} %
1195 is a shortcut for %
1196 \begin{ZX}[math baseline row=1]
1197 \zxN{} \rar & \zxMatrix{\begin{bmatrix} A & B \\ C & D \end{bmatrix}} \rar & \zxN{}
1198 \end{ZX}
1199 \end{codeexample}
1200 }
1201 Note that it seems that some environments do not play well with the way we handle |&| (our changes were needed to make them compatible with |align|, and to provide an easy interface with the |external| library… but it seems to not fit well with all environments, e.g.\ arrays). In that case you should use |\begin{ZXNoExt}| together with |[ampersand replacement=\&]| (of course, use |\&| instead of |&| in the rest of the matrix):
1202 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1203 \begin{codeexample}[width=0pt]
1204 $\begin{ZXNoExt}[ampersand replacement=\&]
1205 \zxN{} \rar \& \zxMatrix{
1206 \begin{bmatrix}
1207 \begin{array}{c|c}
1208 A & B \\
1209 \hline
1210 C & D
1211 \end{array}
1212 \end{bmatrix}} \rar \& \zxN{}
1213 \end{ZXNoExt}$
1214 \end{codeexample}
1215 }
1216 Here is a demo to check if the true north etc anchors are placed correctly:
1217 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1218 \begin{codeexample}[width=0pt]
1219 \begin{ZX}
1220 \zxH{} \ar[to=Gll,C'] \ar[to=Gll,C.] & \zxMatrix[a=Gll]'{G}
1221 \end{ZX} %
1222 \begin{ZX}
1223 \zxH{} \ar[to=Gll,C'] \ar[to=Gll,C.] & \zxMatrix[a=Gll]'*{G}
1224 \end{ZX} %
1225 \begin{ZX}
1226 \zxH{} \ar[to=Gll,C'] \ar[to=Gll,C.] & \zxMatrix[a=Gll].{G}
1227 \end{ZX} %
1228 \begin{ZX}
1229 \zxH{} \ar[to=Gll,C'] \ar[to=Gll,C.] & \zxMatrix[a=Gll].*{G}
1230 \end{ZX} %
1231 \begin{ZX}
1232 \zxH{} \ar[to=Gll,C] \ar[to=Gll,C-] \\
1233 \zxMatrix[a=Gll]{G}
1234 \end{ZX} %
1235 \begin{ZX} %
1236 \zxH{} \ar[to=Gll,C] \ar[to=Gll,C-] \\
1237 \zxMatrix[a=Gll]*{G}
1238 \end{ZX} %
1239 \begin{ZX}
1240 \zxH{} \ar[to=Gll,C-] \ar[to=Gll,C] \\
1241 \zxMatrix[a=Gll]-{G}
1242 \end{ZX} %
1243 \begin{ZX}
1244 \zxH{} \ar[to=Gll,C] \ar[to=Gll,C-] \\
1245 \zxMatrix[a=Gll]-*{G}
1246 \end{ZX}
1247 \end{codeexample}
1248 }
1249
1250
1251 \end{pgfmanualentry}
1252
1253 \begin{pgfmanualentry}
1254 \def\extrakeytext{style, }
1255 \makeatletter% should not be letter for \@@... strange
1256 \extractkey/zx/picCustomStyleMatrixMainNode\@nil%
1257 \extractkey/zx/picCustomStyleMatrixLabel\@nil%
1258 \extractkey/zx/picCustomStyleBeforeUserMatrix\@nil%
1259 \extractkey/zx/picCustomStyleAterUserMatrix\@nil%
1260 \extractkey/zx/picCustomStyleLastPicMatrix\@nil%
1261 \makeatother
1262 \pgfmanualbody
1263 If you would like to override some settings, note that you can use all customization options provided by our custom node system (\cref{subsec:customNodes}). In particular, you can use |pic style| to change the options of the pic used to draw the node, including scale, rotation…, |node style| to style the parent node of the pic (less used, mostly to give alias names to the shape). We additionally provide special styles to configure the nodes more precisely: |/zx/picCustomStyleMatrixMainNode| and |/zx/picCustomStyleMatrixLabel| to configure more specifically the :
1264 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1265 \begin{codeexample}[width=0pt]
1266 \begin{ZX}
1267 \zxN{} \rar & \zxMatrix{A} \rar &
1268 \zxMatrix[
1269 scale=2,
1270 /zx/picCustomStyleMatrixMainNode/.style={fill=blue!50},
1271 /zx/picCustomStyleMatrixLabel/.style={red}]{A}
1272 \rar & \zxN{}
1273 \end{ZX}
1274 \end{codeexample}
1275 }
1276 You can also set globally the styles like |/zx/picCustomStyleBeforeUserMatrix| (automatically provided, see details in \cref{subsec:customNodes}) to automatically add a style to your picture :
1277 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1278 \begin{codeexample}[width=0pt]
1279 \tikzset{
1280 /zx/picCustomStyleBeforeUserMatrix/.style={
1281 scale=2,
1282 /zx/picCustomStyleMatrixMainNode/.style={fill=blue!50},
1283 /zx/picCustomStyleMatrixLabel/.style={red,circle,draw,inner sep=1pt},
1284 % \zxCustomPicAdditionalPic can be any tikz code to run after the creation of the pic:
1285 /utils/exec={\def\zxCustomPicAdditionalPic{%
1286 % the main node has empty name, so .center is the center of the main node
1287 \node[draw,circle,inner sep=2pt,fill=pink] at (.center) {};%
1288 }%
1289 },
1290 },
1291 }
1292 \begin{ZX}
1293 \zxZ{} \rar & \zxMatrix{A} \rar & \zxMatrix*{B} \rar & \zxN{}
1294 \end{ZX}
1295 \end{codeexample}
1296 }
1297 \end{pgfmanualentry}
1298
1299 \subsubsection{Circuit-related}
1300
1301 Since 2023-09-17, we provide a number of options to typeset circuits.
1302
1303 \begin{pgfmanualentry}
1304 \def\extrakeytext{style, }
1305 \extractcommand\zxBox\opt{\oarg{style}}\marg{box text}\@@
1306 \extractcommand\zxGate\opt{\oarg{style}}\marg{box text}\@@
1307 \makeatletter
1308 \def\extrakeytext{style, }
1309 \extractkey/zx/every box=style\@nil%
1310 \extractkey/zx/boxes=\marg{style to add}\@nil%
1311 \makeatother
1312 \pgfmanualbody
1313 (both commands are alias) You can add simple gates using |\zxGate{X}| (the content is typeset in math mode by default), possibly adding additional styling to the main box using the optional argument or |/zx/every box/.style={your replacement style}| or |/zx/boxes={your style to add}|:
1314 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1315 \begin{codeexample}[]
1316 \begin{ZX}
1317 \zxBox{G} \rar[B] & [\zxwCol] \zxN{} \\
1318 \zxBox[fill=green]{G_A} \rar[B] & [\zxwCol] \zxN{}
1319 \end{ZX}
1320 \end{codeexample}
1321 }
1322 \begin{codeexample}[width=0pt]
1323 \begin{ZX}[circuit, thick lines]
1324 \rar & \zxGate[add label={below:\text{Hadamard}}]{H} \rar &
1325 \end{ZX}
1326 \end{codeexample}
1327 Note that you can also use matrices inside:
1328 \begin{codeexample}[width=0pt]
1329 \begin{ZX}[circuit]
1330 \rar & \zxGate{
1331 \begin{pmatrix}
1332 a & b\\
1333 c & d
1334 \end{pmatrix}
1335 } \rar & \zxGate{f} \rar &
1336 \end{ZX}
1337 \end{codeexample}
1338 \end{pgfmanualentry}
1339
1340 \begin{pgfmanualentry}
1341 \extractcommand\zxDefaultColumnSepCircuit\@@
1342 \extractcommand\zxDefaultRowSepCircuit\@@
1343 \makeatletter
1344 \def\extrakeytext{style, }
1345 \extractkey/zx/defaultEnv/circuit\@nil%
1346 \makeatother
1347 \pgfmanualbody
1348 This style enables the circuit mode, which is simply changing the default spacing between lines and columns. Compare:
1349 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1350 \begin{codeexample}[]
1351 \begin{ZX}
1352 \rar & \zxGate{H} \rar & \zxGateMulti{2}{3}{H} & & \rar & \\
1353 \rar & \zxGate{H} \rar & & & \rar &
1354 \end{ZX}
1355 \end{codeexample}
1356 }
1357 with
1358 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1359 \begin{codeexample}[]
1360 \begin{ZX}[circuit]
1361 \rar & \zxGate{H} \rar & \zxGateMulti{2}{3}{H} & & \rar & \\
1362 \rar & \zxGate{H} \rar & & & \rar &
1363 \end{ZX}
1364 \end{codeexample}
1365 }
1366 You can customize the default spacing with |\def\zxDefaultColumnSepCircuit{4mm}| and |\def\zxDefaultRowSepCircuit{4mm}|.
1367 \end{pgfmanualentry}
1368
1369 \begin{pgfmanualentry}
1370 \def\extrakeytext{style, }
1371 \extractcommand\zxGateMulti\opt{\oarg{style fit node}}\opt{\oarg{advanced styling}}\marg{number rows}\marg{number columns}\marg{box text}\@@
1372 \makeatletter
1373 \def\extrakeytext{style, }
1374 \extractkey/zx/generic multi/add label=\marg{label}\@nil%
1375 \extractkey/zx/generic multi/add label advanced=\marg{style}\marg{label}\@nil%
1376 \extractkey/zx/generic multi/content inner nodes=\marg{content inner nodes}\@nil%
1377 \extractkey/zx/generic multi/main=\marg{style}\@nil%
1378 \extractkey/zx/generic multi/main text=\marg{style}\@nil%
1379 \extractkey/zx/generic multi/additional code=\marg{code to add new nodes}\@nil%
1380 \extractkey/zx/generic multi/fit content=\opt{\marg{additional row margin}\marg{additional column margin}\marg{minimum height inner nodes}\marg{minimum width inner nodes}}\@nil%
1381 \extractkey/zx/generic multi/safe fit\@nil%
1382 \makeatother
1383 \pgfmanualbody
1384 Create a multi-line/row gate:
1385 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1386 \begin{codeexample}[]
1387 \begin{ZX}[circuit]
1388 \rar & \zxGate{H} \rar & \zxGateMulti{2}{3}{H} & & \rar & \\
1389 \rar & \zxGate{H} \rar & & & \rar &
1390 \end{ZX}
1391 \end{codeexample}
1392 }
1393 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1394 \begin{codeexample}[]
1395 \begin{ZX}
1396 \rar & \zxBox{H} \rar & \zxGateMulti{1}{3}{H} \dar & & \rar & \zxBox{H} & & \\
1397 & & \zxBox{H} \rar & \zxBox{H} & \zxBox{H} & & &
1398 \end{ZX}
1399 \end{codeexample}
1400 }
1401 Note that the column will adapt to fit the content if it is too long:
1402 \begin{codeexample}[]
1403 \begin{ZX}[circuit]
1404 \rar & \zxGateMulti{1}{2}{HHHHHHHHHH} & \rar & \\
1405 \zxGate{H} & \zxGate{H} & \zxGate{H} & \zxGate{H} &
1406 \end{ZX}
1407 \end{codeexample}
1408 You can also customize the style of a gate in a simple way using the first argument (the style will only be applied to the fitting box and to the box when computing its dimensions, but not to the content itself that is added in a separate node, see below for more fine-tuned configuration):
1409 \begin{codeexample}[]
1410 \begin{ZX}[circuit]
1411 A & B & C & D & E & & \\
1412 \zxBox{H} \rar & \zxGateMulti[a=secondGate]{2}{3}{H} & & \rar & \zxBox{X} & & \\
1413 \zxBox{H} \rar & & & & \zxBox{X} \lar & & \\
1414 \zxBox{H} \rar \ar[dr,bend right] & \zxBox{H} \rar[to=secondGate] & & & & & \\
1415 & \zxGateMulti[,fill=orange!50!white]{1}{2}{H} & &
1416 \end{ZX}
1417 \end{codeexample}
1418 You can also customize its behavior more precisely using the second optional argument using the above listed styles. |main| and |main text| allows you to change the style of the main node and the main text:
1419 \begin{codeexample}[]
1420 \begin{ZX}[circuit]
1421 A & B & C & D & E & & \\
1422 \zxBox{H} \rar & \zxGateMulti[a=secondGate]{2}{3}{H} & & \rar & \zxBox{X} & & \\
1423 \zxBox{H} \rar & & & & \zxBox{X} \lar & & \\
1424 \zxBox{H} \rar \ar[dr,bend right] & \zxBox{H} \rar[to=secondGate] & & & & & \\
1425 & \zxGateMulti[][main={fill=orange!50!white}, main text={red}]{1}{2}{H} & &
1426 \end{ZX}
1427 \end{codeexample}
1428 while |additional code| allows you to write arbitrary code after the creation of the main node (|zxMainNode| and |zxMainNodeText| are the alias of the main node and text node):
1429 \begin{codeexample}[]
1430 \begin{ZX}[circuit]
1431 \zxBox{H} \rar & \zxGateMulti[][
1432 main={fill=orange!50!white},
1433 main text={red},
1434 additional code={
1435 \node[
1436 circle,
1437 inner sep=2pt,
1438 fill=red,
1439 at={($(zxMainNode.east)+(0mm,1.5mm)$)},
1440 zx subnode={redCircle}
1441 ]
1442 {};
1443 }%
1444 ]{1}{2}{H} & \rar[start subnode={redCircle}, to=lastH, C-] & \\
1445 \zxBox[a=lastH]{H} &
1446 \end{ZX}
1447 \end{codeexample}
1448
1449 The |add label| and |add label advanced| allows you to add labels:
1450 \begin{codeexample}[width=0pt]
1451 \begin{ZX}[circuit]
1452 \rar & \zxGateMulti[add label={Bell \\measurement}]{2}{1}{\zxMeter{}} \rar[classical] & \\
1453 \rar & \rar[cl] &
1454 \end{ZX}
1455 \end{codeexample}
1456 Internally, in order to place the matrix, we place a number of inner nodes in the region of the matrix. You can customize them using |content inner nodes| and |style inner nodes|. This can be practical if you want to force the node to take less space for instance (but the content might go outside of the node):
1457 \begin{codeexample}[width=0pt]
1458 \begin{ZX}[circuit]
1459 & & & & & & & \\
1460 & \zxGate{H} \rar & \zxGateMulti[][
1461 content inner nodes={},
1462 main={opacity=.5},
1463 style inner nodes={
1464 red,
1465 minimum width=2pt,
1466 minimum height=2pt,
1467 test/.style={fill=#1,opacity=.7}, test=red
1468 }]{2}{3}{HHHH} & & & & & \\
1469 & & & & & & &
1470 \end{ZX}
1471 \end{codeexample}
1472
1473 By default, we run |fit content/.default={0mm}{0mm}{\zxBoxMinimumHeight}{\zxBoxMinimumWidth}| which works by evaluating the width of the content, and by automatically setting the size of the inner nodes so that the content fits nicely at the end (it takes the default column sep of the matrix, so it will not see custom adjustment of the column size with |&[1cm]|), with a minimum size. You can also call instead |safe fit| in the second argument, that will just set all inner nodes to be equal to the size of the content (which might not be optimal in term of space). While it is faster and works well with nodes of small size, it gives weird results on large nodes:
1474 Compare
1475 \begin{codeexample}[width=0pt]
1476 \begin{ZX}[circuit]
1477 & A & B & C & D & E & & \\
1478 & \zxBox{H} \rar & \zxGateMulti{1}{3}{HHHH} & & \rar & \zxBox{H} & & \\
1479 & & \zxBox{H} & \zxBox{H} & \zxBox{H} & & & \\
1480 & & & & & & & \\
1481 & & & & & & & \\
1482 \end{ZX}
1483 \end{codeexample}
1484 with
1485 \begin{codeexample}[width=0pt]
1486 \begin{ZX}[circuit]
1487 & A & B & C & D & E & & \\
1488 & \zxBox{H} \rar & \zxGateMulti[][safe fit]{1}{3}{HHHH} & & \rar & \zxBox{H} & & \\
1489 & & \zxBox{H} & \zxBox{H} & \zxBox{H} & & & \\
1490 & & & & & & & \\
1491 & & & & & & & \\
1492 \end{ZX}
1493 \end{codeexample}
1494 Note that if time is an issue for you, see the section on externalization \cref{sec:externalization}.
1495 \end{pgfmanualentry}
1496
1497 %{\catcode`\|=12 % Ensures | is not anymore \verb|...|
1498 \begin{pgfmanualentry}
1499 \def\extrakeytext{style, }
1500 \extractcommand\zxElt\opt{\oarg{style}}\marg{box text}\@@
1501 \extractcommand\zxCtrl\opt{\oarg{style}}\marg{}\@@
1502 \extractcommand\zxOCtrl\opt{\oarg{style}}\marg{}\@@
1503 \extractcommand\zxNot\opt{\oarg{style}}\marg{}\@@
1504 \extractcommand\zxCross\opt{\oarg{style}}\marg{}\@@
1505 \extractcommand\zxMeter\opt{\oarg{scale}}\marg{}\@@
1506 \makeatletter
1507 \def\extrakeytext{style, }
1508 \extractkey/zx/wires definition/classical\@nil%
1509 \extractkey/zx/wires definition/cl\@nil%
1510 \extractkey/zx/wires definition/=--\textbar\@nil%
1511 \extractkey/zx/wires definition/=\textbar--\@nil%
1512 \makeatother
1513 \pgfmanualbody
1514 |\zxElt| is like |\zxGate| but without any border (useful to add spacing around elements like |\ket{\psi}|, but you might prefer |\zxInput| that does not reserve any space in the matrix for the content and allows an easier align of wires), and the other commands just create the corresponding symbols that you can link together as you want as usual using |\ar| (note that |\zxMeter{}| is node a node but just an icon, so place it yourself inside |\zxGate| or |\zxGateMulti|). The style are wire styles, and are useful to print classical wires (|cl| is an alias for |classical|) and wires bent with a 90 degrees angle. Note that I could not write the right name in the doc, you should remove the equal sign like in \texttt{--\textbar}.
1515 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1516 \begin{codeexample}[width=0pt]
1517 \begin{ZX}[circuit]
1518 \zxElt{\ket{\psi}} \rar & \zxBox{H} \rar & \zxCtrl{} \dar \rar & \zxCross{} \dar \rar
1519 & \zxBox[add label={Measure}]{\zxMeter{}} \ar[dr,classical,--|] \\
1520 \ar[r] & \zxOCtrl{} \rar \ar[u] & \zxNot{} \rar & \zxCross{} \ar[rr]
1521 & & \zxBox{H} \rar &
1522 \end{ZX}
1523 \end{codeexample}
1524 }
1525 \end{pgfmanualentry}
1526
1527 \begin{pgfmanualentry}
1528 \def\extrakeytext{style, }
1529 \extractcommand\zxInput\opt{\oarg{style}}\marg{text}\@@
1530 \extractcommand\zxInputMulti\opt{\oarg{style}}\opt{\oarg{label style}}\marg{nb rows}\marg{text}\@@
1531 \extractcommand\zxOutput\opt{\oarg{style}}\marg{text}\@@
1532 \extractcommand\zxOutputMulti\opt{\oarg{style}}\opt{\oarg{label style}}\marg{nb rows}\marg{text}\@@
1533 \extractcommand\zxGateInput\opt{\oarg{style}}\marg{text}\@@
1534 \extractcommand\zxGateInputMulti\opt{\oarg{style}}\opt{\oarg{label style}}\marg{nb rows}\marg{text}\@@
1535 \extractcommand\zxGateOutput\opt{\oarg{style}}\marg{text}\@@
1536 \extractcommand\zxGateOutputMulti\opt{\oarg{style}}\opt{\oarg{label style}}\marg{nb rows}\marg{text}\@@
1537 \makeatletter
1538 \def\extrakeytext{style, }
1539 \extractkey/zx/enlarge brace=\marg{+ or - distance}\@nil%
1540 \extractkey/zx/input style=\marg{style}\@nil%
1541 \extractkey/zx/output style=\marg{style}\@nil%
1542 \extractkey/zx/input multi style=\marg{style}\@nil%
1543 \extractkey/zx/output multi style=\marg{style}\@nil%
1544 \extractkey/zx/input multi label style=\marg{style}\@nil%
1545 \extractkey/zx/output multi label style=\marg{style}\@nil%
1546 \extractkey/zx/gate input style=\marg{style}\@nil%
1547 \extractkey/zx/gate output style=\marg{style}\@nil%
1548 \extractkey/zx/gate input multi style=\marg{style}\@nil%
1549 \extractkey/zx/gate output multi style=\marg{style}\@nil%
1550 \extractkey/zx/gate input multi label style=\marg{style}\@nil%
1551 \extractkey/zx/gate output multi label style=\marg{style}\@nil%
1552 \makeatother
1553 \pgfmanualbody
1554
1555 Use these functions in order to label the input or outputs of circuits (you can also label any diagram made with this library) or gates (the variant you want to use should be clear from the name. You can customize the arrow and label using the optional arguments or globally using the corresponding style (note that the style for the gate inputs was inspired by the great Quantikz package). The command should be located on the row where the brace should start (either in the cell containing the wire or inside the gate directly):
1556 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1557 \begin{codeexample}[]
1558 \begin{ZX}[circuit]
1559 \rar \zxInputMulti[blue]{2}{\ket{\psi}} & \zxGateMulti{3}{2}{U} \zxGateInputMulti{2}{x}
1560 & \rar \zxGateOutputMulti{2}{x \oplus y} & \zxOutputMulti[red][green]{2}{\text{Output}} \\
1561 \rar &
1562 & \rar & \\
1563 \rar \zxInput{\ket{0}} & \zxGateInput{y}
1564 & \rar \zxGateOutput{y \oplus 1} & \zxOutput{\ket{1}}
1565 \end{ZX}
1566 \end{codeexample}
1567 }
1568
1569 You can enlarge or reduce the distance of the braces (set by default to |\zxDefaultEnlargeBraceOne| for braces on single wires, and |\zxDefaultEnlargeBraceMulti| for braces on multiple wires) using |enlarge brace|:
1570 \begin{codeexample}[width=0pt]
1571 \begin{ZX}
1572 \zxBox[a=A]{A} \rar & \zxOutputMulti{1}{\parbox{2.2cm}{\raggedright\tiny
1573 This is the default spacing for one multi-output}} \\
1574 \zxBox{B} \rar & \zxOutputMulti{2}{\parbox{2.2cm}{\raggedright\tiny
1575 This is the default spacing for multiple outputs}} \\
1576 \zxBox[a=B]{B} \rar & \\
1577 \zxBox[a=A]{A} \rar & \zxOutputMulti[enlarge brace=1mm]{1}{\parbox{2.2cm}{\raggedright\tiny
1578 You can increase or decrease the spacing}} \\
1579 \zxBox{B} \rar & \zxOutputMulti[enlarge brace=-\zxDefaultEnlargeBraceMulti]{2}{\parbox{2.2cm}{
1580 \raggedright\tiny You can increase or decrease the spacing}} \\
1581 \zxBox[a=B]{B} \rar & \\
1582 \end{ZX}
1583 \end{codeexample}
1584
1585 You can prefix the above commands with |V| (for vertical) when reading from bottom to top (the associated style starts with |vertical|):
1586
1587 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1588 \begin{codeexample}[width=0pt]
1589 \begin{ZX}[circuit]
1590 \dar \zxVOutputMulti{2}{\text{Output}} & \dar & \dar \zxVOutput{\ket{1}} \\
1591 \zxGateMulti{2}{3}{U} \zxVGateOutputMulti{2}{x \oplus y} & & \zxVGateOutput{y \oplus 1} \\
1592 \dar \zxVGateInputMulti{2}{x} & \dar & \dar \zxVGateInput{y} \\
1593 \zxVInputMulti{2}{\ket{\psi}} & & \zxVInput{\ket{0}}
1594 \end{ZX}
1595 \end{codeexample}
1596 }
1597
1598 You can also use |\zxT| (for Top) instead of |\zxV| if you prefer the convention where inputs are located at the top of the diagram):
1599 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1600 \begin{codeexample}[width=0pt]
1601 \begin{ZX}[circuit]
1602 \dar \zxTInputMulti{2}{\ket{\psi}} & \dar & \dar \zxTInput{\ket{0}} \\
1603 \zxGateMulti{2}{3}{U} \zxTGateInputMulti{2}{x} & & \zxTGateInput{y} \\
1604 \dar \zxTGateOutputMulti{2}{x\oplus y} & \dar & \dar \zxTGateOutput{y \oplus 1} \\
1605 \zxTOutputMulti{2}{\text{Output}} & & \zxTOutput{\ket{1}}
1606 \end{ZX}
1607 \end{codeexample}
1608 }
1609 Note that these labels are not taken into account when computing the width of the gate box, so you might need to manually adapt their width of the column, for instance by setting the content of an individual cell like:
1610 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1611 \begin{codeexample}[width=0pt]
1612 \begin{ZX}[circuit]
1613 \dar \zxTInputMulti{2}{\ket{\psi}} & \dar & \dar \zxTInput{\ket{0}} \\
1614 \zxGateMulti{2}{3}{U} \zxTGateInputMulti{2}{x} & & \zxTGateInput{y} \\
1615 \dar \zxTGateOutputMulti{2}{x\oplus y} & \dar
1616 %% We replace the cell with a larger cell:
1617 & \zxGate[zxTinyFontAndSpacing]{\phantom{y \oplus 1 \oplus x}};
1618 %% This is useful or the cell will be replaced by another cell during the fit:
1619 \zxMainNodeAlreadySet{} \dar \zxTGateOutput{y \oplus 1 \oplus x} \\
1620 \zxTOutputMulti{2}{\text{Output}} & & \zxTOutput{\ket{1}}
1621 \end{ZX}
1622 \end{codeexample}
1623 }
1624 \end{pgfmanualentry}
1625
1626 \begin{pgfmanualentry}
1627 \def\extrakeytext{style, }
1628 \extractcommand\zxSlice\opt{\oarg{style label}}\opt{\oarg{style line}}\opt{position percent}\marg{text}\@@
1629 \extractcommand\zxVSlice\opt{\oarg{style label}}\opt{\oarg{style line}}\opt{position percent}\marg{text}\@@
1630 \makeatletter
1631 \def\extrakeytext{style, }
1632 \extractkey/zx/slice label style=\marg{style}\@nil%
1633 \extractkey/zx/slice line style=\marg{style}\@nil%
1634 \extractkey/zx/vertical slice label style=\marg{style}\@nil%
1635 \extractkey/zx/vertical slice line style=\marg{style}\@nil%
1636 \makeatother
1637 \pgfmanualbody
1638
1639 You can add a slice in your diagram or circuit using |\zxSlice{text}| at the corresponding column:
1640 \begin{codeexample}[width=0pt]
1641 \begin{ZX}[circuit]
1642 \rar & \zxGate{X} \zxSlice{1} \rar & \zxGate{Y} \zxSlice{Part\\2} \rar & \zxGate{\zxMeter{}} \\
1643 \rar & \zxGate{H} \rar & \zxGate{Y} \rar &
1644 \end{ZX}
1645 \end{codeexample}
1646 We also provide a style |rotated=angle| to rotate the label (the default angle is 30 degrees):
1647 \begin{codeexample}[width=0pt]
1648 \begin{ZX}[circuit]
1649 \rar & \zxGate{X} \zxSlice[rotated]{First part} \rar
1650 & \zxGate{Y} \zxSlice[rotated]{Second part} \rar & \zxGate{\zxMeter{}} \\
1651 \rar & \zxGate{H} \rar & \zxGate{Y} \rar &
1652 \end{ZX}
1653 \end{codeexample}
1654 You can change the position using the 3rd optional argument (default to .5):
1655 \begin{codeexample}[width=0pt]
1656 \begin{ZX}[circuit]
1657 \rar & \zxGate{X} \zxSlice[][][.2]{1} \rar & \zxGate{Y} \zxSlice[][][.8]{Part\\2} \rar & \zxGate{\zxMeter{}} \\
1658 \rar & \zxGate{H} \rar & \zxGate{Y} \rar &
1659 \end{ZX}
1660 \end{codeexample}
1661 You can also customize it as you want using the two optional arguments (for the label first and for the line after):
1662 \begin{codeexample}[width=0pt]
1663 \begin{ZX}[circuit]
1664 \rar
1665 & \zxGate{Y} \rar
1666 \zxSlice[rotate=90,anchor=east,pos=1,blue][solid,blue,decorate,decoration={snake}]{Why not \\below?}
1667 & \zxGate{\zxMeter{}} \\
1668 \rar & \zxGate{H} \rar & \zxGate{Y} \rar &
1669 \end{ZX}
1670 \end{codeexample}
1671 Finally, the |\zxVSlice| version works similarly, but for vertical slices (make sure to insert it \textbf{before} the newline):
1672 \begin{codeexample}[width=0pt]
1673 \begin{ZX}[circuit]
1674 \zxElt{\text{Alice}} &[\zxZeroCol] \rar & \zxGate{X} \rar & \zxGate{Y} \rar & \zxGate{\zxMeter{}}
1675 \zxVSlice{No communication}\\
1676 \zxElt{\text{Bob}} & \rar & \zxGate{H} \rar & \zxGate{Y} \rar &
1677 \end{ZX}
1678 \end{codeexample}
1679 You can configure it as before, for instance to obtain this fancy effect:
1680 \begin{codeexample}[width=0pt]
1681 \begin{ZX}[circuit]
1682 \rar & \zxCtrl{} \rar \dar & \zxGate{Y} \rar & \zxGate{\zxMeter{}}
1683 \zxVSlice[][
1684 black,
1685 double distance=4pt,
1686 double,
1687 solid,
1688 decorate,
1689 decoration={snake,segment length=2cm},
1690 % Also funny:
1691 % decoration={random steps,segment length=.2cm},
1692 on layer=foreground
1693 ]{Repeat $n$ times}\\[3mm]
1694 \rar & \zxCtrl{} \dar \rar & \zxGate{Y} \rar & \zxGate{\zxMeter{}}\\
1695 \rar & \zxGate{X} \rar & \zxGate{Y} \rar & \zxGate{\zxMeter{}}
1696 \end{ZX}
1697 \end{codeexample}
1698 \end{pgfmanualentry}
1699
1700 \begin{pgfmanualentry}
1701 \makeatletter
1702 \def\extrakeytext{style, }
1703 \extractkey/tikz/zx filling style=\marg{your style} (default {fill=white})\@nil%
1704 \makeatother
1705 \pgfmanualbody
1706 We can also disable or set any custom filling using this style:
1707 \begin{codeexample}[width=0pt]
1708 \begin{ZX}[circuit,/tikz/zx filling style/.style={}]
1709 \zxInput{\ket{\psi}} \rar & \zxBox{H} \rar & \zxCtrl{} \dar \rar & \zxCross{} \dar \rar
1710 & \zxBox[add label={Measure}]{\zxMeter{}} \ar[dr,classical,--|] \\
1711 \ar[r] & \zxOCtrl{} \rar \ar[u] & \zxNot{} \rar & \zxCross{} \ar[rr]
1712 & & \zxBox{H} \rar & \zxOutput{\ket{\phi}}
1713 \end{ZX}
1714 \end{codeexample}
1715 \end{pgfmanualentry}
1716
1717
1718 \begin{pgfmanualentry}
1719 \makeatletter
1720 \def\extrakeytext{style, }
1721 \extractkey/tikz/every node/block\@nil%
1722 \makeatother
1723 This is useful if you wish to typeset a block that must be wrapped inside a minipage-like environment (actually we use varwidth):
1724 \begin{codeexample}[width=0pt]
1725 \begin{ZX}[circuit]
1726 \rar & \zxGate[block]{
1727 \procedureblock[linenumbering]{$\indcpa_\enc^\adv(\secpar)$}{
1728 b \sample \bin\\
1729 (\pk,\sk)\sample \kgen(\secparam)
1730 }
1731 } \rar & \zxGate{f} \rar &
1732 \end{ZX}
1733 \end{codeexample}
1734 \begin{codeexample}[width=0pt]
1735 \begin{ZX}[circuit, zx column sep=6mm]
1736 \rar["k"] & \zxGateMulti[block]{2}{3}{
1737 \procedureblock[linenumbering]{$\mathsf{Enc}(k,m)$}{
1738 \pclinecomment{This is the standard one-time pad:} \\
1739 \pcreturn k \oplus m
1740 }
1741 } & & \rar["c"] & \zxGate{\mathcal{A}} \rar["\tilde{m}"] & \\
1742 \rar["m"]&& & & &
1743 \end{ZX}
1744 \end{codeexample}
1745 \end{pgfmanualentry}
1746
1747
1748 \subsection{Wires}
1749
1750 \subsubsection{Creating wires and debug mode}
1751
1752 \begin{pgfmanualentry}
1753 \extractcommand\arrow\opt{\oarg{options}}\@@
1754 \extractcommand\ar\opt{\oarg{options}}\@@
1755 \extractcommand\zxWire\marg{options}\@@
1756 \pgfmanualbody
1757 These synonym commands (actually coming from |tikz-cd|, except for the last |\zxWire| that is introduced in this library in 2023-10-03 for readability, note that |\zxWire| additionally adds by default a |r| to go right) are used to draw wires between nodes. We refer to |tikz-cd| for an in-depth documentation, but what is important for our purpose is that the direction of the wires can be specified in the \meta{options} using a string of letters |r| (right), |l| (left), |u| (up), |d| (down). It's also possible to specify a node alias as a source or destination as shown below.
1758 \begin{codeexample}[]
1759 \zx{\zxZ{} \ar[r] & \zxX{}} = \zx{\zxX{} \arrow[rd] \\ & \zxZ{}}
1760 \end{codeexample}
1761 \meta{options} can also be used to add any additional style, either custom ones, or the ones defined in this library (this is recommended since it can be easily changed document-wise by simply changing the style). Multiple wires can be added in the same cell. Other shortcuts provided in |tikz-cd| like |\rar|\dots{} can be used.
1762 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1763 \begin{codeexample}[width=0pt]
1764 \begin{ZX}
1765 \zxZ{\alpha} \arrow[d, C] % C = Bell-like wire
1766 \ar[r,H,o'] % o' = top part of circle
1767 % H adds Hadamard, combine with \zxHCol
1768 \ar[r,H,o.] &[\zxHCol] \zxZ{\gamma}\\
1769 \zxZ{\beta} \rar & \zxX{} \ar[ld,red,"\circ" {marking,blue}] \ar[rd,s] \\
1770 \zxFracX-{\pi}{4} & &\zxZ{}
1771 \end{ZX}
1772 \end{codeexample}
1773 }
1774 The |\zxWire| can be used as follows:
1775 \begin{codeexample}[width=0pt]
1776 \begin{ZX}[circuit, row sep={.6cm,between origins}]
1777 \zxWire{} & \zxCross{} \zxWire{} \zxWire{d} & \\
1778 \zxWire{} & \zxCross{} \zxWire{} &
1779 \end{ZX}
1780 \end{codeexample}
1781 \end{pgfmanualentry}
1782
1783 As explained in |tikz-cd|, there are further shortened forms:
1784 \begin{pgfmanualentry}
1785 \extractcommand\rar\opt{\oarg{options}}\@@
1786 \extractcommand\lar\opt{\oarg{options}}\@@
1787 \extractcommand\dar\opt{\oarg{options}}\@@
1788 \extractcommand\uar\opt{\oarg{options}}\@@
1789 \extractcommand\drar\opt{\oarg{options}}\@@
1790 \extractcommand\urar\opt{\oarg{options}}\@@
1791 \extractcommand\dlar\opt{\oarg{options}}\@@
1792 \extractcommand\ular\opt{\oarg{options}}\@@
1793 \pgfmanualbody
1794 \end{pgfmanualentry}
1795 The first one is equivalent to
1796 \begin{verse}
1797 |\arrow|{\oarg{options}}|{r}|
1798 \end{verse}
1799 and the other ones work analogously.
1800
1801 Note that sometimes, it may be practical to properly organize big diagrams to increase readability. To that end, one surely wants to have a small and well indented matrix (emacs |M-x align-current| or |M-x align| (for selected lines) commands are very practical to indent matrices automatically). Unfortunately, adding wires inside the matrix can make the line really long and hard to read. Similarly, some nodes involving fractions or long expressions can also be quite long. It is however easy to increase readability (and maintainability) by moving the wires at the very end of the diagram, using |a| (like |alias|, but with a debug mode) to connect nodes and |\def| to create shortcuts. Putting inside a macro with |\def| long node definitions can also be useful to keep small items in the matrix:
1802 \begin{codeexample}[width=0pt]
1803 \begin{ZX}[zx row sep=1pt,
1804 execute at begin picture={%
1805 %%% Definition of long items (the goal is to have a small and readable matrix
1806 % (warning: macro can't have numbers in TeX. Also, make sure not to use existing names)
1807 \def\Zpifour{\zxFracZ[a=Zpi4]-{\pi}{4}}%
1808 \def\mypitwo{\zxFracX[a=mypi2]{\pi}{2}}%
1809 }
1810 ]
1811 %%% Matrix: in emacs "M-x align-current" is practical to automatically format it.
1812 %%% a is for 'alias'... but also provides a debug mode, see below.
1813 & & & & \zxZ[a=toprightpi]{\pi} \\
1814 \zxN[a=in1]{} & \zxX[a=X1]{} & & & & \zxN[a=out1]{} \\
1815 & & \zxZ[a=xmiddle]{} & \mypitwo{} & & \zxN[a=out2]{} \\
1816 \zxN[a=in2]{} & \Zpifour{} & & \zxX[a=Xdown]{} & & \zxN[a=out3]{}
1817 %%% Arrows
1818 % Column 1
1819 \ar[from=in1,to=X1]
1820 \ar[from=in2,to=Zpi4]
1821 % Column 2
1822 \ar[from=X1,to=xmiddle,(.]
1823 \ar[from=X1,to=toprightpi,<',H]
1824 \ar[from=Zpi4,to=xmiddle,(']
1825 \ar[from=Zpi4,to=Xdown,o.]
1826 % Column 3
1827 \ar[from=xmiddle,to=Xdown,s.]
1828 \ar[from=xmiddle,to=mypi2]
1829 % Column 4
1830 \ar[from=mypi2,to=toprightpi,(']
1831 \ar[from=mypi2,to=out1,<']
1832 \ar[from=mypi2,to=out2]
1833 \ar[from=Xdown,to=out3]
1834 \end{ZX}
1835 \end{codeexample}
1836 In that setting, it is often useful to enable the debug mode via |\def\zxDebugMode{}| as explained above to quickly visualize the alias given to each node (note that debug mode works with |a=| but not with |alias=|). For instance, it was easy to rewrite the above diagram by moving nodes in the matrix and arrows after checking their name on the produced pdf (NB: you can increase |column sep| and |row sep| temporarily to make the debug information more visible):
1837 \begin{codeexample}[width=0pt]
1838 {
1839 \def\zxDebugMode{}%%%%
1840 \begin{ZX}[zx row sep=1pt,
1841 execute at begin picture={%
1842 %%% Definition of long items (the goal is to have a small and readable matrix
1843 % (warning: macro can't have numbers in TeX. Also, make sure not to use existing names)
1844 \def\Zpifour{\zxFracZ[a=Zpi4]-{\pi}{4}}%
1845 \def\mypitwo{\zxFracX[a=mypi2]{\pi}{2}}%
1846 }
1847 ]
1848 %%% Matrix: in emacs "M-x align" is practical to automatically format it. a is for 'alias'
1849 & \zxN[a=n]{} & \zxZ[a=xmiddle]{} & & \zxN[a=out1]{} \\
1850 \zxN[a=in1]{} & \Zpifour{} & \zxX[a=Xdown]{} & \mypitwo{} & \\
1851 & & & & \zxN[a=out2]{} \\
1852 \zxN[a=in2]{} & \zxX[a=X1]{} & \zxZ[a=toprightpi]{\pi} & & \zxN[a=out3]{}
1853 %%% Arrows
1854 % Column 1
1855 \ar[from=in1,to=X1,s]
1856 \ar[from=in2,to=Zpi4,.>]
1857 % Column 2
1858 \ar[from=X1,to=xmiddle,N']
1859 \ar[from=X1,to=toprightpi,H]
1860 \ar[from=Zpi4,to=n,C] \ar[from=n,to=xmiddle,wc]
1861 \ar[from=Zpi4,to=Xdown]
1862 % Column 3
1863 \ar[from=xmiddle,to=Xdown,C-]
1864 \ar[from=xmiddle,to=mypi2,)]
1865 % Column 4
1866 \ar[from=mypi2,to=toprightpi,(']
1867 \ar[from=mypi2,to=out1,<']
1868 \ar[from=mypi2,to=out2,<.]
1869 \ar[from=Xdown,to=out3,<.]
1870 \end{ZX}
1871 }
1872 \end{codeexample}
1873
1874 \begin{pgfmanualentry}
1875 \def\extrakeytext{style, }
1876 \makeatletter% should not be letter for \@@... strange
1877 \extractkey/zx/wires definition/3d above\@nil%
1878 \makeatother
1879 \pgfmanualbody
1880 This style is useful to fake a 3D effect to put a wire above another one like \begin{ZX}[]
1881 \zxN{} \ar[d,C] \ar[rd,s] & [\zxWCol] \zxN{} \dar[C-] \\[\zxWRow]
1882 \zxN{} \ar[ru,s,3d above] & \zxN{}
1883 \end{ZX} using:
1884 \begin{codeexample}[width=0pt]
1885 \begin{ZX}[]
1886 \zxN{} \ar[d,C] \ar[rd,s] & [\zxWCol] \zxN{} \dar[C-] \\[\zxWRow]
1887 \zxN{} \ar[ru,s,3d above,very thick] & \zxN{}
1888 \end{ZX}
1889 \end{codeexample}
1890 \end{pgfmanualentry}
1891
1892 \begin{pgfmanualentry}
1893 \def\extrakeytext{style, }
1894 \makeatletter% should not be letter for \@@... strange
1895 \extractkey/zx/defaultEnv/thick lines\@nil%
1896 \extractkey/tikz/zx thickness wires style=your style\@nil%
1897 \makeatother
1898 When designing circuits, you might prefer a thicker line style, following the style of Quantikz. This is not enabled by default since for ZX diagrams we want to maintain an important difference between this version and the bold version, but you can enable it with |thick lines|, that internally sets the style |zx thickness wires style| to |thick, line cap=round|.
1899 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1900 \begin{codeexample}[width=0pt]
1901 Compare \begin{ZX}[circuit, thick lines]
1902 \zxInput{\ket{\psi}} \rar & \zxBox{H} \rar & \zxCtrl{} \dar \rar & \zxCross{} \dar \rar
1903 & \zxBox[add label={Measure}]{\zxMeter{}} \ar[dr,classical,--|] \\
1904 \ar[r] & \zxOCtrl{} \rar \ar[u] & \zxNot{} \rar & \zxCross{} \ar[rr]
1905 & & \zxBox{H} \rar & \zxOutput{\ket{\phi}}
1906 \end{ZX} with \begin{ZX}[circuit]
1907 \zxInput{\ket{\psi}} \rar & \zxBox{H} \rar & \zxCtrl{} \dar \rar & \zxCross{} \dar \rar
1908 & \zxBox[add label={Measure}]{\zxMeter{}} \ar[dr,classical,--|] \\
1909 \ar[r] & \zxOCtrl{} \rar \ar[u] & \zxNot{} \rar & \zxCross{} \ar[rr]
1910 & & \zxBox{H} \rar & \zxOutput{\ket{\phi}}
1911 \end{ZX}
1912 \end{codeexample}
1913 }
1914 \end{pgfmanualentry}
1915
1916 \subsubsection{Wire styles (new generation)}
1917
1918 We give now a list of wire styles provided in this library (|/zx/wires definition/| is an automatically loaded style). We recommend using them instead of manual styling to ensure they are the same document-wise, but they can of course be customized to your need. Note that the name of the styles are supposed (ahah, I do my best with what ASCII provides) to graphically represent the action of the style, and some characters are added to precise the shape: typically |'| means top, |.| bottom, |X-| is right to X (or should arrive with angle 0), |-X| is left to X (or should leave with angle zero). These shapes are usually designed to work when the starting node is left most (or above of both nodes have the same column). But they may work both way for some of them.
1919
1920 Note that the first version of that library (which appeared one week before this new version\dots{} hopefully backward compatibility won't be too much of a problem) was using |in=| and |out=| to create these styles. However, it turns out to be not very reliable since the shape of the wire was changing (sometimes importantly) depending on the position of the nodes. This new version should be more reliable, but the older styles are still available by using |IO, nameOfWirestyle| (read more in \cref{subsub:IOwires}).
1921
1922 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1923 \begin{pgfmanualentry}
1924 \makeatletter
1925 \def\extrakeytext{style, }
1926 % not sure how to do it in a cleaner way without manually defining everything
1927 \pgfmanualentryheadline{{\ttfamily\declare{/zx/wires definition/|--}}\hfill(style, no default)}%
1928 \pgfmanualpdflabel[\catcode`\|=12 ]{|--}{}
1929 % the space after \textt is important or we get two different sections
1930 % the 2 is just a random char since | does not work (the syntax is "stuff to group@actual text to print" as
1931 % I understand)
1932 \index{zx@\texttt {/zx/}!wires definition@\texttt {wires definition/}!2--@\texttt {\protect\pgfmanualbar--}}
1933 \pgfmanualentryheadline{{\ttfamily\declare{/zx/wires definition/--|}}\hfill(style, no default)}%
1934 \pgfmanualpdflabel[\catcode`\|=12 ]{--|}{}
1935 % the space after \textt is important or we get two different sections
1936 % the 2 is just a random char since | does not work (the syntax is "stuff to group@actual text to print" as
1937 % I understand)
1938 \index{zx@\texttt {/zx/}!wires definition@\texttt {wires definition/}!--2@\texttt {--\protect\pgfmanualbar}}
1939 \makeatother
1940 \pgfmanualbody
1941 This gives a wire bent with a 90 degrees angle (respectively starting horizontally then vertically, or first vertically then horizontally).
1942 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1943 \begin{codeexample}[width=0pt]
1944 \begin{ZX}[circuit]
1945 \zxElt{\ket{\psi}} \rar & \zxBox{H} \rar & \zxCtrl{} \dar \rar & \zxCross{} \dar \rar
1946 & \zxBox[add label={Measure}]{\zxMeter{}} \ar[dr,classical,--|] \\
1947 \ar[r] & \zxOCtrl{} \rar \ar[u] & \zxNot{} \rar & \zxCross{} \ar[rr]
1948 & & \zxBox{H} \rar &
1949 \end{ZX}
1950 \end{codeexample}
1951 }
1952 \end{pgfmanualentry}
1953 }
1954
1955 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1956 % I can't find a cleaner solution than manually calling pdflabel, pgfmanualentryheadline etc due to the |
1957 % character
1958 \begin{pgfmanualentry}
1959 \makeatletter
1960 \def\extrakeytext{style, }\catcode`\|=12
1961 \pgfmanualentryheadline{{\ttfamily\declare{/zx/wires definition/horizontal |--}}\hfill(style, no default)}%
1962 \pgfmanualpdflabel[\catcode`\|=12 ]{horizontal |--}{}
1963 % the space after \textt is important or we get two different sections
1964 % the 2 is just a random char since | does not work (the syntax is "stuff to group@actual text to print" as
1965 % I understand)
1966 \index{zx@\texttt {/zx/}!wires definition@\texttt {wires definition/}!horizontal 2--@\texttt {horizontal \protect\pgfmanualbar--}}
1967 \pgfmanualentryheadline{{\ttfamily\declare{/zx/wires definition/horizontal --|}}\hfill(style, no default)}%
1968 \pgfmanualpdflabel[\catcode`\|=12 ]{horizontal --|}{}
1969 \index{zx@\texttt {/zx/}!wires definition@\texttt {wires definition/}!horizontal --2@\texttt {horizontal --\protect\pgfmanualbar}}
1970 \pgfmanualentryheadline{{\ttfamily\declare{/zx/wires definition/vertical |--}}\hfill(style, no default)}%
1971 \pgfmanualpdflabel[\catcode`\|=12 ]{vertical |--}{}
1972 % the space after \textt is important or we get two different sections
1973 % the 2 is just a random char since | does not work (the syntax is "stuff to group@actual text to print" as
1974 % I understand)
1975 \index{zx@\texttt {/zx/}!wires definition@\texttt {wires definition/}!vertical 2--@\texttt {vertical \protect\pgfmanualbar--}}
1976 \pgfmanualentryheadline{{\ttfamily\declare{/zx/wires definition/vertical --|}}\hfill(style, no default)}%
1977 \pgfmanualpdflabel[\catcode`\|=12 ]{vertical --|}{}
1978 \index{zx@\texttt {/zx/}!wires definition@\texttt {wires definition/}!vertical --2@\texttt {vertical --\protect\pgfmanualbar}}
1979 \makeatother
1980 \pgfmanualbody
1981 Like \texttt{--\textbar} \texttt{\textbar--} except that it only keeps the horizontal/vertical part (we also provide shorter alias like \texttt{\textbar--}). This is mostly useful when you want an horizontal/vertical wire starting/ending at a point without having to bother about shifting the other point to maintain horizontality:
1982 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
1983 \begin{codeexample}[width=0pt]
1984 \begin{ZX}
1985 \rar[horizontal |--, end subnode=A] \rar[horizontal |--, end subnode=B] &
1986 \node[circle,fill=blue,zx subnode=A,yshift=2mm]{a}; \node[circle,fill=green,zx subnode=B,yshift=-2mm]{b};
1987 \rar[horizontal --|, start subnode=A] \rar[horizontal --|, start subnode=B] &
1988 \end{ZX} and %
1989 \begin{ZX}
1990 \dar[vertical --|, end subnode=A]
1991 \dar[vertical --|, end subnode=B] \\
1992 \node[circle,fill=blue,zx subnode=A,xshift=-2mm]{a};
1993 \node[circle,fill=green,zx subnode=B,xshift=2mm]{b};
1994 \dar[vertical |--, start subnode=A] \dar[vertical |--, start subnode=B] \\
1995 \end{ZX}
1996 \end{codeexample}
1997 }
1998 \end{pgfmanualentry}
1999 }
2000
2001 \begin{pgfmanualentry}
2002 \makeatletter
2003 \def\extrakeytext{style, }
2004 \extractkey/zx/wires definition/C=radius ratio (default 1)\@nil%
2005 \extractkey/zx/wires definition/C.=radius ratio (default 1)\@nil%
2006 \extractkey/zx/wires definition/C'=radius ratio (default 1)\@nil%
2007 \extractkey/zx/wires definition/C-=radius ratio (default 1)\@nil%
2008 \makeatother
2009 \pgfmanualbody
2010 Bell-like wires with an arrival at ``right angle'', |C| represents the shape of the wire, while |.| (bottom), |'| (top) and |-| (side) represent (visually) its position. Combine with |wire centered| (|wc|) to avoid holes when connecting multiple wires (not required with |\zxNone{}|, alias |\zxN{}|).
2011 \begin{codeexample}[]
2012 A Bell pair \zx{\zxNone{} \ar[d,C] \\[\zxWRow]
2013 \zxNone{}}
2014 , a swapped Bell pair
2015 \begin{ZX}
2016 \zxN{} \ar[d,C] \ar[rd,s] &[\zxWCol] \zxN{} \\[\zxWRow]
2017 \zxN{} \ar[ru,s] & \zxN{}
2018 \end{ZX}
2019 and a funny graph
2020 \begin{ZX}
2021 \zxX{} \ar[d,C] \ar[r,C'] & \zxZ{} \ar[d,C-]\\
2022 \zxZ{} \ar[r,C.] & \zxX{}
2023 \end{ZX}.
2024 \end{codeexample}
2025 Note that this style is actually connecting the nodes using a perfect circle (it is \emph{not} based on |curve to|), and therefore should \emph{not} be used together with |in|, |out|, |looseness|\dots{} (this is the case also for most other styles except the ones in |IO|). It has the advantage of connecting nicely nodes which are not aligned or with different shapes:
2026 \begin{codeexample}[]
2027 \begin{ZX}
2028 \zxX{\alpha} \ar[dr,C]\\
2029 & \zxNone{}
2030 \end{ZX}
2031 \end{codeexample}
2032 The \meta{radius ratio} parameter can be used to turn the circle into an ellipse using this ratio between both axis:
2033 \begin{codeexample}[]
2034 \begin{ZX}
2035 \zxX{\alpha}
2036 \ar[dr,C=0.5,red]
2037 \ar[dr,C,green]
2038 \ar[dr,C=2,blue]
2039 \ar[dr,C=3,purple]\\
2040 & \zxNone{}
2041 \end{ZX}
2042 \begin{ZX}
2043 \zxX{} \ar[d,C=2] \ar[r,C'=2] & \zxZ{} \ar[d,C-=2,H]\\
2044 \zxZ{} \ar[r,C.=2] & \zxX{}
2045 \end{ZX}.
2046 \end{codeexample}
2047
2048 Note that you can customize like for other nodes the direction of the wire using something like |\ar[d, C, end anchor=south]| or |\ar[d, C, end subnode=mySubnode]|. For more advanced users, you can create your own gates and specify directly inside the gate where the |C|-like shapes should connect to. The algorithm followed is:
2049 \begin{itemize}
2050 \item First, it tries to connect to the center of a |true west|, |true east|, |true north|, |true south| subnode (depending on the variation of |C|).
2051 \item Then, it tries to connect to a |true west|, |true east|, |true north|, |true south| \textbf{anchor}
2052 \item Otherwise, it falls back to the |west|, |east|, |north|, |south| anchors.
2053 \end{itemize}
2054
2055 \begin{codeexample}[width=0pt]
2056 \NewExpandableDocumentCommand{\myWeirdGate}{}{
2057 \zxBox[a=foo]{D}
2058 \node[circle,fill=green,zx subnode=true west,inner sep=2pt,at=(foo.south west)]{};
2059 }
2060
2061 \begin{ZX}
2062 A \dar[C] \\
2063 \myWeirdGate
2064 \end{ZX}
2065 \end{codeexample}
2066
2067 \end{pgfmanualentry}
2068
2069
2070 \begin{pgfmanualentry}
2071 \makeatletter
2072 \def\extrakeytext{style, }
2073 \extractkey/zx/wires definition/o'=angle (default 40)\@nil%
2074 \extractkey/zx/wires definition/o.=angle (default 40)\@nil%
2075 \extractkey/zx/wires definition/o-=angle (default 40)\@nil%
2076 \extractkey/zx/wires definition/-o=angle (default 40)\@nil%
2077 \makeatother
2078 \pgfmanualbody
2079 Curved wire, similar to |C| but with a soften angle (optionally specified via \meta{angle}, and globally editable with |\zxDefaultLineWidth|). Again, the symbols specify which part of the circle (represented with |o|) must be kept.
2080 \begin{codeexample}[width=3cm]
2081 \begin{ZX}
2082 \zxX{} \ar[d,-o] \ar[d,o-]\\
2083 \zxZ{} \ar[r,o'] \ar[r,o.] & \zxX{}
2084 \end{ZX}.
2085 \end{codeexample}
2086 Note that these wires can be combined with |H|, |X| or |Z|, in that case one should use appropriate column and row spacing as explained in their documentation:
2087 \begin{codeexample}[width=3cm]
2088 \begin{ZX}
2089 \zxX{\alpha} \ar[d,-o,H] \ar[d,o-,H]\\[\zxHRow]
2090 \zxZ{\beta} \rar & \zxZ{} \ar[r,o',X] \ar[r,o.,Z] &[\zxSCol] \zxX{}
2091 \end{ZX}.
2092 \end{codeexample}
2093 \end{pgfmanualentry}
2094
2095 \begin{pgfmanualentry}
2096 \makeatletter
2097 \def\extrakeytext{style, }
2098 \extractkey/zx/wires definition/(=angle (default 30)\@nil%
2099 \extractkey/zx/wires definition/)=angle (default 30)\@nil%
2100 \extractkey/zx/wires definition/('=angle (default 30)\@nil%
2101 \extractkey/zx/wires definition/('=angle (default 30)\@nil%
2102 \makeatother
2103 \pgfmanualbody
2104 Curved wire, similar to |o| but can be used for diagonal items. The angle is, like in |bend right|, the opening angle from the line which links the two nodes. For the first two commands, the |(| and |)| symbols must be imagined as if the starting point was on top of the parens, and the ending point at the bottom.
2105 \begin{codeexample}[width=3cm]
2106 \begin{ZX}
2107 \zxX{} \ar[rd,(] \ar[rd,),red]\\
2108 & \zxZ{}
2109 \end{ZX}.
2110 \end{codeexample}
2111 Then, |('|=|(| and |(.|=|)|; this notation is, I think, more intuitive when linking nodes from left to right. |('| is used when going to top right and |(.| when going to bottom right.
2112 \begin{codeexample}[width=3cm]
2113 \begin{ZX}
2114 \zxN{} & \zxX{}\\
2115 \zxZ{} \ar[ru,('] \ar[rd,(.] & \\
2116 & \zxX{}
2117 \end{ZX}
2118 \end{codeexample}
2119 When the nodes are too far appart, the default angle of |30| may produce strange results as it will go above (for |('|) the vertical line. Either choose a shorter angle, or see |<'| instead. Note that for now this node is based on |in| and |out|, but it may change later. So if you want to change looseness, or really rely on the precise specified angle, prefer to use |IO,(| instead (which takes the |IO| version, guaranteed to stay untouched).
2120 \end{pgfmanualentry}
2121
2122 \begin{pgfmanualentry}
2123 \makeatletter
2124 \def\extrakeytext{style, }
2125 \extractkey/zx/wires definition/start fake center north\@nil%
2126 \extractkey/zx/wires definition/start fake center south\@nil%
2127 \extractkey/zx/wires definition/start fake center east\@nil%
2128 \extractkey/zx/wires definition/start fake center west\@nil%
2129 \extractkey/zx/wires definition/start real center\@nil
2130 \extractkey/zx/wires definition/end fake center north\@nil%
2131 \extractkey/zx/wires definition/end fake center south\@nil%
2132 \extractkey/zx/wires definition/end fake center east\@nil%
2133 \extractkey/zx/wires definition/end fake center west\@nil%
2134 \extractkey/zx/wires definition/end real center\@nil
2135 \extractkey/zx/wires definition/left to right\@nil%
2136 \extractkey/zx/wires definition/right to left\@nil%
2137 \extractkey/zx/wires definition/up to down\@nil%
2138 \extractkey/zx/wires definition/down to up\@nil%
2139 \extractkey/zx/wires definition/force left to right\@nil%
2140 \extractkey/zx/wires definition/force right to left\@nil%
2141 \extractkey/zx/wires definition/force up to down\@nil%
2142 \extractkey/zx/wires definition/force down to up\@nil%
2143 \extractkey/zx/wires definition/no fake center\@nil%
2144 \makeatother
2145 \pgfmanualbody
2146 Usually each wire should properly use these functions, so the end user should not need that too often (during a first reading, you can skip this paragraph). We added 4 anchors to nodes: |fake center north|, |fake center south|, |fake center east| and |fake center west|. These anchors are used to determine the starting point of the wires depending on the direction of the wire. Because some nodes may not have these anchors, we can't directly set |start anchor=fake center north, on layer=edgelayer| (but the user can do that if they are using only nodes with these anchors) or the code may fail on some nodes. For that reason, we check that these anchors exist while drawing our wires (which, at the best of my knowledge, can only be done while drawing the path). The |start/end fake center *| code is responsible to configure that properly (|start real center| will use the real center), and |left to right| (and similar) just configure both the |start| and |end| point to ensure the node starts at the appropriate anchor. However this won't work for style not defined in this library: in case you are sure that these anchors exists and want to use your own wire styles, you can then set the anchors manually and use |on layer=edgelayer|, or use |force left to right| (and similar) which will automatically do that for the |start| and |end| points.
2147 \begin{codeexample}[]
2148 \begin{ZX}
2149 \zxX{\alpha+\beta} \ar[r,o',no fake center] & \zxZ{\alpha+\beta}\\
2150 \zxX{\alpha+\beta} \ar[r,o'] & \zxZ{\alpha+\beta}
2151 \end{ZX}
2152 \end{codeexample}
2153 \end{pgfmanualentry}
2154
2155 \begin{pgfmanualentry}
2156 \makeatletter
2157 \def\extrakeytext{style, }
2158 \extractkey/zx/args/-andL/-=x\@nil%
2159 \extractkey/zx/args/-andL/1-=x\@nil%
2160 \extractkey/zx/args/-andL/2-=x\@nil%
2161 \extractkey/zx/args/-andL/L=y\@nil%
2162 \extractkey/zx/args/-andL/1L=y\@nil%
2163 \extractkey/zx/args/-andL/2L=y\@nil%
2164 \extractkey/zx/args/-andL/1 angle and length=\marg{angle}\marg{length}\@nil%
2165 \extractkey/zx/args/-andL/1al=\marg{angle}\marg{length}\@nil%
2166 \extractkey/zx/args/-andL/2 angle and length=\marg{angle}\marg{length}\@nil%
2167 \extractkey/zx/args/-andL/2al=\marg{angle}\marg{length}\@nil%
2168 \extractkey/zx/args/-andL/angle and length=\marg{angle}\marg{length}\@nil%
2169 \extractkey/zx/args/-andL/al=\marg{angle}\marg{length}\@nil%
2170 \extractkey/zx/args/-andL/1 angle=\marg{angle}\@nil%
2171 \extractkey/zx/args/-andL/1a=\marg{angle}\@nil%
2172 \extractkey/zx/args/-andL/2 angle=\marg{angle}\@nil%
2173 \extractkey/zx/args/-andL/1a=\marg{angle}\marg{length}\@nil%
2174 \extractkey/zx/args/-andL/angle=\marg{angle}\@nil%
2175 \extractkey/zx/args/-andL/a=\marg{angle}\@nil%
2176 \extractkey/zx/args/-andL/symmetry-L\@nil%
2177 \extractkey/zx/args/-andL/symmetry\@nil%
2178 \extractkey/zx/args/-andL/negate1L\@nil%
2179 \extractkey/zx/args/-andL/negate2L\@nil%
2180 \extractkey/zx/args/-andL/negateL\@nil%
2181 \extractkey/zx/args/-andL/negate1-\@nil%
2182 \extractkey/zx/args/-andL/negate2-\@nil%
2183 \extractkey/zx/args/-andL/negate-\@nil%
2184 \extractkey/zx/args/-andL/oneMinus1-\@nil%
2185 \extractkey/zx/args/-andL/oneMinus2-\@nil%
2186 \extractkey/zx/args/-andL/oneMinus1L\@nil%
2187 \extractkey/zx/args/-andL/oneMinus2L\@nil%
2188 \makeatother
2189 \pgfmanualbody
2190 The next wires can take multiple options. They are all based on the same set of options for now, namely |/zx/args/-andL/|. The |1*| options are used to configure the starting point, the |2*| to configure the ending point, if no number is given both points are updated. |-| and |L| are used to place two anchors of a Bezier curve. They are expressed in relative distance (so they are typically between $0$ and $1$, but can be pushed above $1$ or below $0$ for stronger effects), |-| is typically on the |x| axis and |L| on the |y| axis (the name represents ``graphically'' the direction). They are however not named |x| and |y| because some wires use them slighlty differently, notably |o| which uses |-| for the direction of the arrow and |L| for the direction perpendicular to the arrow (again the shape of |L| represents a perpendicular line). Each wire interprets |-| and |L| to ensure that $0$ should lead to a straight line, and that a correct shape is obtained when |1-| equals |2-|, |1L| equals |2L| (except for non-symmetric shapes of course), and both |-| and |L| are positive.
2191
2192 The other expressions involving |angle| (or the shortcut |a|) allow you to define |1-|,|1L|\dots{} using a maybe more intuitive ``polar'' notation, i.e.\ an ``angle'' and a relative length (if not specified, like in |1 angle|, the length defaults to $0.6$). Note that the angle is not really an angle (it is an angle only when the nodes are placed at $45$ degrees, or for the |bezier x/y| variations), but a ``squeezed angle'' (when nodes are not at $45$ degrees, the shape is squeezed horizontally or vertically not to change the wire) and similarly for the length. In the above list, the meaning of each expression should be clear from the name: for instance |1angle and length={45}{.8}| will setup a squeezed angle of $45$ and a relative length of $.8$ for the first point, i.e.\ this is equivalent to $1-=.8\cos(45)$ and $1L=.8\sin(45)$, and |angle=45| will change the angle of both points to $45$, with a relative length of $.6$. In the above list, each long expression has below it a shorter version, for intance |a=45| is equivalent to |angle=45|.
2193
2194 The last expressions (|symmetry-L|, |symmetry|\dots) are used internally to do some math. Of course if you need to do symmetries at some point you can use these keys (|symmetry-L| exchange |-| and |L|, and |symmetry| exchanges |1| and |2|), |negateX| just negates |X| and |oneMinusX| replaces |X| with |1-X|. Each of the following nodes have default values which can be configured as explained in \cref{subsec:wirecustom}.
2195 \end{pgfmanualentry}
2196
2197 \begin{pgfmanualentry}
2198 \makeatletter
2199 \def\extrakeytext{style, }
2200 \extractkey/zx/wires definition/s=-andL config (default defaultS)\@nil%
2201 \extractkey/zx/wires definition/s'=-andL config (default defaultS')\@nil%
2202 \extractkey/zx/wires definition/s.=-andL config (default defaultS')\@nil%
2203 \extractkey/zx/wires definition/-s=-andL config (default default-S)\@nil%
2204 \extractkey/zx/wires definition/-s'=-andL config (default \{defaultS',default-S\})\@nil%
2205 \extractkey/zx/wires definition/-s.=-andL config (default \{defaultS',default-S\})\@nil%
2206 \extractkey/zx/wires definition/s-=-andL config (default \{defaultS',default-S,symmetry\})\@nil%
2207 \extractkey/zx/wires definition/s'-=-andL config (default \{defaultS',default-S,symmetry\})\@nil%
2208 \extractkey/zx/wires definition/s.-=-andL config (default \{defaultS',default-S,symmetry\})\@nil%
2209 \extractkey/zx/wires definition/-S=-andL config (default \{defaultS',default-S\})\@nil%
2210 \extractkey/zx/wires definition/-S'=-andL config (default \{defaultS',default-S\})\@nil%
2211 \extractkey/zx/wires definition/-S.=-andL config (default \{defaultS',default-S\})\@nil%
2212 \extractkey/zx/wires definition/S-=-andL config (default \{defaultS',default-S,symmetry\})\@nil%
2213 \extractkey/zx/wires definition/S'-=-andL config (default \{defaultS',default-S,symmetry\})\@nil%
2214 \extractkey/zx/wires definition/S.-=-andL config (default \{defaultS',default-S,symmetry\})\@nil%
2215 \makeatother
2216 \pgfmanualbody
2217 |s| and |S| are used to create a s-like wire (|s| is smoother than |S| that arrives and leave horizontally), to have nicer diagonal lines between nodes. Other versions are soften versions (the input and output angles are not as sharp. Adding |'| or |.| specifies if the wire is going up-right or down-right, however as of today if it mostly used for backward compatibility since, for instance, |-s'| is the same as |-s| (but some styles may want to do a difference later). The only exception is for |s|/|s'|/|s.|: |s| has a sharper output angle than |s'| and |s.| (which are both equals).
2218 \begin{codeexample}[width=3cm]
2219 \begin{ZX}
2220 \zxX{\alpha} \ar[s,rd] \\
2221 & \zxZ{\beta}\\
2222 \zxX{\alpha} \ar[s.,rd] \\
2223 & \zxZ{\beta}\\
2224 & \zxZ{\alpha}\\
2225 \zxX{\beta} \ar[S,ru] \\
2226 & \zxZ{\alpha}\\
2227 \zxX{\beta} \ar[s',ru] \\
2228 \end{ZX}
2229 \end{codeexample}
2230 |-| forces the angle on the side of |-| to be horizontal. Because for now the wires start inside the node, this is not very visible. For that reason, versions with a capital |S| have an anchor on the side of |-| lying on the surface of the node (|S| has two such anchors since both inputs and outputs arrives horizontally) instead of on the |fake center *| anchor (see explanation on |fake center| anchors above).
2231 \begin{codeexample}[width=3cm]
2232 \begin{ZX}
2233 \zxX{\alpha} \ar[s.,rd] \\
2234 & \zxZ{\beta}\\
2235 \zxX{\alpha} \ar[-s.,rd] \\
2236 & \zxZ{\beta}\\
2237 \zxX{\alpha} \ar[s.-,rd] \\
2238 & \zxZ{\beta}\\
2239 \zxX{\alpha} \ar[S,rd] \\
2240 & \zxZ{\beta}\\
2241 \end{ZX}
2242 \end{codeexample}
2243 It is possible to configure it using the options in |-andL config| as explained above (default values are given in \cref{subsec:wirecustom}), where |-| is the (relative) position of the horizontal Bezier anchor and |L| its relative vertical position (to keep a |s|-shape, you should have |-|$>$|L|).
2244 \begin{codeexample}[width=3cm]
2245 \begin{ZX}
2246 \zxX{\alpha} \ar[rd,s.] \\
2247 & \zxZ{\beta}\\
2248 % same as s., configure globally using defaultS'\\
2249 \zxX{\alpha} \ar[rd,s.={-=.8,L=.2}]\\
2250 & \zxZ{\beta}\\
2251 \zxX{\alpha} \ar[rd,s.={L=.4}] \\
2252 & \zxZ{\beta}\\
2253 \zxX{\alpha} \ar[rd,s.={L=0.1,-=1}] \\
2254 & \zxZ{\beta}\\
2255 \zxX{\alpha} \ar[rd,edge above, control points visible,s.={-=2}] \\
2256 & \zxZ{\beta}
2257 \end{ZX}
2258 \end{codeexample}
2259 For the non-symmetric versions (involving a vertical arrival), you can configure each point separately using |1-| and |1L| (first point) and |2-| and |2L| (second points). You can also specify the ``squeezed angle'' and ``length'' of each point, for instance using the |1 angle and length={10}{.8}| option (short version is |1al={10}{.8}|) or both at the same time using |al={10}{.6}| (this last command being itself equivalent to |a=10|). As explained later |edge above| and |control points visible| can help you to visualize the control points of the underlying Bezier curve.
2260 \begin{codeexample}[width=3cm]
2261 \begin{ZX}
2262 \zxZ{} \ar[dr,s.={al={10}{.8}}]\\ &\zxZ{}\\
2263 \zxZ{} \ar[edge above,control points visible,dr,s.={a=10}]\\ &\zxZ{}
2264 \end{ZX}
2265 \end{codeexample}
2266 \end{pgfmanualentry}
2267
2268 \begin{pgfmanualentry}
2269 \makeatletter
2270 \def\extrakeytext{style, }
2271 \extractkey/zx/wires definition/ss=-andL config (default \{defaultS,symmetry-L\})\@nil%
2272 \extractkey/zx/wires definition/SS=-andL config (default \{defaultS,symmetry-L\})\@nil%
2273 \extractkey/zx/wires definition/ss.=-andL config (default \{defaultS',symmetry-L\})\@nil%
2274 \extractkey/zx/wires definition/.ss=-andL config (default \{defaultS',symmetry-L\}30)\@nil%
2275 \extractkey/zx/wires definition/sIs.=-andL config (default defaultSIS)\@nil%
2276 \extractkey/zx/wires definition/.sIs=-andL config (default \{defaultS',defaultSIS\})\@nil%
2277 \extractkey/zx/wires definition/ss.I-=-andL config (default \{defaultS',defaultSIS,symmetry\})\@nil%
2278 \extractkey/zx/wires definition/I.ss-=-andL config (default \{defaultS',defaultSIS,symmetry\})\@nil%
2279 \extractkey/zx/wires definition/SIS=-andL config (default \{defaultS',defaultSIS\})\@nil%
2280 \extractkey/zx/wires definition/.SIS=-andL config (default \{defaultS',defaultSIS\})\@nil%
2281 \extractkey/zx/wires definition/ISS=-andL config (default \{defaultS',defaultSIS,symmetry\})\@nil%
2282 \extractkey/zx/wires definition/SS.I=-andL config (default \{defaultS',defaultSIS,symmetry\})\@nil%
2283 \extractkey/zx/wires definition/I.SS=-andL config (default \{defaultS',defaultSIS,symmetry\})\@nil%
2284 \extractkey/zx/wires definition/SSI=-andL config (default \{defaultS',defaultSIS,symmetry\})\@nil%
2285 \makeatother
2286 \pgfmanualbody
2287 |ss| is similar to |s| except that we go from top to bottom instead of from left to right. The position of |.| says if the node is wire is going bottom right (|ss.|) or bottom left (|.ss|).
2288 \begin{codeexample}[width=3cm]
2289 \begin{ZX}
2290 \zxX{\alpha} \ar[ss,rd] \\
2291 & \zxZ{\beta}\\
2292 \zxX{\alpha} \ar[ss.,rd] \\
2293 & \zxZ{\beta}\\
2294 & \zxX{\beta} \ar[.ss,dl] \\
2295 \zxZ{\alpha}\\
2296 & \zxX{\beta} \ar[.ss={},dl] \\
2297 \zxZ{\alpha}\\
2298 \end{ZX}
2299 \end{codeexample}
2300 |I| forces the angle above (if in between the two |s|) or below (if on the same side as |.|) to be vertical.
2301 \begin{codeexample}[width=3cm]
2302 \begin{ZX}
2303 \zxX{\alpha} \ar[ss,rd] \\
2304 & \zxZ{\beta}\\
2305 \zxX{\alpha} \ar[sIs.,rd] \\
2306 & \zxZ{\beta}\\
2307 \zxX{\alpha} \ar[ss.I,rd] \\
2308 & \zxZ{\beta}\\
2309 & \zxX{\beta} \ar[.sIs,dl] \\
2310 \zxZ{\alpha}\\
2311 & \zxX{\beta} \ar[I.ss,dl] \\
2312 \zxZ{\alpha}\\
2313 \end{ZX}
2314 \end{codeexample}
2315 The |S| version forces the anchor on the vertical line to be on the boundary.
2316 \begin{codeexample}[width=3cm]
2317 \begin{ZX}
2318 \zxX{\alpha} \ar[SS,rd] \\
2319 & \zxZ{\beta}\\
2320 \zxX{\alpha} \ar[SIS,rd] \\
2321 & \zxZ{\beta}\\
2322 \zxX{\alpha} \ar[SSI,rd] \\
2323 & \zxZ{\beta}\\
2324 & \zxX{\beta} \ar[.sIs,dl] \\
2325 \zxZ{\alpha}\\
2326 & \zxX{\beta} \ar[I.ss,dl] \\
2327 \zxZ{\alpha}\\
2328 \end{ZX}
2329 \end{codeexample}
2330 As for |s| it can be configured:
2331 \begin{codeexample}[width=3cm]
2332 \begin{ZX}
2333 \zxX{\alpha} \ar[rd,SIS] \\
2334 & \zxZ{\beta}\\
2335 \zxX{\alpha} \ar[rd,SIS={1L=.4}] \\
2336 & \zxZ{\beta}\\
2337 \zxX{\alpha} \ar[rd,SIS={1L=.8}] \\
2338 & \zxZ{\beta}\\
2339 \zxX{\alpha} \ar[rd,SIS={1L=1,2L=1}] \\
2340 & \zxZ{\beta}\\
2341 \end{ZX}
2342 \end{codeexample}
2343 \end{pgfmanualentry}
2344
2345 \begin{pgfmanualentry}
2346 \makeatletter
2347 \def\extrakeytext{style, }
2348 \extractkey/zx/wires definition/N=-andL config (default defaultN)\@nil%
2349 \extractkey/zx/wires definition/N'=-andL config (default defaultN)\@nil%
2350 \extractkey/zx/wires definition/N.=-andL config (default defaultN)\@nil%
2351 \extractkey/zx/wires definition/-N=-andL config (default \{defaultN,defaultN-\})\@nil%
2352 \extractkey/zx/wires definition/-N'=-andL config (default \{defaultN,defaultN-\})\@nil%
2353 \extractkey/zx/wires definition/-N.=-andL config (default \{defaultN,defaultN-\})\@nil%
2354 \extractkey/zx/wires definition/N-=-andL config (default \{defaultN,defaultN-,symmetry\})\@nil%
2355 \extractkey/zx/wires definition/N'-=-andL config (default \{defaultN,defaultN-,symmetry\})\@nil%
2356 \extractkey/zx/wires definition/N.-=-andL config (default \{defaultN,defaultN-,symmetry\})\@nil%
2357 \extractkey/zx/wires definition/Nbase=-andL config (default defaultN)\@nil%
2358 \makeatother
2359 \pgfmanualbody
2360 |N| is used to create a left-to-right wire leaving at wide angle and arriving at wide angle (it's named |N| because it roughly have the shape of a capital |N|). In older versions, |'| and |.| was required to specify if the wire should go up-right or down-right, but it is not useful anymore (we keep it for compatibilty with |IO| styles and in case some styles want to do a distinction later).
2361 \begin{codeexample}[width=3cm]
2362 \begin{ZX}
2363 \zxX{\alpha} \ar[N,rd] \\
2364 & \zxZ{\beta}\\
2365 & \zxZ{\alpha}\\
2366 \zxX{\beta} \ar[N,ru]
2367 \end{ZX}
2368 \end{codeexample}
2369 |-| forces the angle on the side of |-| to be horizontal.
2370 \begin{codeexample}[width=3cm]
2371 \begin{ZX}
2372 \zxX{\alpha} \ar[-N,rd] \\
2373 & \zxZ{\beta}\\
2374 & \zxZ{\alpha}\\
2375 \zxX{\beta} \ar[N-,ru]
2376 \end{ZX}
2377 \end{codeexample}
2378 Like other wires, it can be configured using |-| (horizontal relative position of anchor points) and |L| (vertical relative position of anchor points, make sure to have |-|$<$|L| to have a |N|-looking shape), |al={angle}{relative length}|\dots{}
2379 \begin{codeexample}[width=3cm]
2380 \begin{ZX}
2381 \zxX{\alpha} \ar[N,rd] \\
2382 & \zxZ{\beta}\\
2383 \zxX{\alpha} \ar[N={L=1.2},rd] \\
2384 & \zxZ{\beta}
2385 \end{ZX}
2386 \end{codeexample}
2387 All these styles are based on Nbase (which should not be used directly), including the styles like |<|. If you wish to overwrite later |N|-like commands, but not |<|-like, then change |N|. If you wish to also update |<| commands, use |Nbase|.
2388 \end{pgfmanualentry}
2389
2390
2391
2392 \begin{pgfmanualentry}
2393 \makeatletter
2394 \def\extrakeytext{style, }
2395 \extractkey/zx/wires definition/NN=-andL config (default \{defaultN,symmetry-L,defaultNN\})\@nil%
2396 \extractkey/zx/wires definition/NN.=-andL config (default \{defaultN,symmetry-L,defaultNN\})\@nil%
2397 \extractkey/zx/wires definition/.NN=-andL config (default \{defaultN,symmetry-L,defaultNN\})\@nil%
2398 \extractkey/zx/wires definition/NIN=-andL config (default \{defaultN,symmetry-L,defaultNN,defaultNIN\})\@nil%
2399 \extractkey/zx/wires definition/INN=-andL config (default \{defaultN,symmetry-L,defaultNN,defaultNIN,symmetry\})\@nil%
2400 \extractkey/zx/wires definition/NNI=-andL config (default \{defaultN,symmetry-L,defaultNN,defaultNIN,symmetry\})\@nil%
2401 \makeatother
2402 \pgfmanualbody
2403 Like |N| but for diagrams read up-to-down or down-to-up. The |.| are maintly used for backward compatibility with |IO| style.
2404 % \begin{codeexample}[width=3cm]
2405 % \begin{ZX}
2406 % \zxX{\alpha} \ar[NN,rd] \\
2407 % & \zxZ{\beta}\\
2408 % & \zxZ{\alpha}\\
2409 % \zxX{\beta} \ar[NN,ru]
2410 % \end{ZX}
2411 % \end{codeexample}
2412 % |I| forces the angle on the side of |I| to be vertical.
2413 % \begin{codeexample}[width=3cm]
2414 % \begin{ZX}
2415 % \zxX{\alpha} \ar[NIN,rd] \\
2416 % & \zxZ{\beta}\\
2417 % & \zxZ{\alpha}\\
2418 % \zxX{\beta} \ar[NNI,ru]
2419 % \end{ZX}
2420 % \end{codeexample}
2421 % It can be configured like |N| using |-|, |L|\dots{}
2422 \end{pgfmanualentry}
2423
2424 \begin{pgfmanualentry}
2425 \makeatletter
2426 \def\extrakeytext{style, }
2427 \extractkey/zx/wires definition/<'=-andL config (default like N-)\@nil%
2428 \extractkey/zx/wires definition/<.=-andL config (default like N-)\@nil%
2429 \extractkey/zx/wires definition/'>=-andL config (default like -N)\@nil%
2430 \extractkey/zx/wires definition/.>=-andL config (default like -N)\@nil%
2431 %\extractkey/zx/wires definition/^.=-andL config (default 60)\@nil%
2432 %\extractkey/zx/wires definition/.^=-andL config (default 60)\@nil%
2433 \extractkey/zx/wires definition/'v=-andL config (default like INN)\@nil%
2434 \extractkey/zx/wires definition/v'=-andL config (default like NNI)\@nil%
2435 \makeatother
2436 \pgfmanualbody
2437 |<'| and |<.| are similar to |N-|, except that the anchor of the vertical line is put on the boundary (similarly for |*>| and |-N|, |*v*| and |INN|, and |*^*| and |NIN|: |.^| and |^.| were not possible to put in this documentation since the documentation package does not like the |^| character). The position of |'| and |.| does not really matters anymore in new versions, but for backward compatibility with |IO| styles, and maybe forward compatibility (another style may need this information), it's cleaner to put |.| or |'| on the direction of the wire. It also helps the reader of your diagrams to see the shape of the wire.
2438 \begin{codeexample}[width=0cm]
2439 \begin{ZX}
2440 \zxN{} & \zxZ{}\\
2441 \zxX{} \ar[ru,<'] \ar[rd,<.] \\
2442 \zxN{} & \zxZ{}\\
2443 \end{ZX}
2444 \end{codeexample}
2445 \begin{codeexample}[width=0cm]
2446 \begin{ZX}
2447 \zxN{} & \zxZ{}\\
2448 \zxX{} \ar[ru,.>] \ar[rd,'>] \\
2449 \zxN{} & \zxZ{}\\
2450 \end{ZX}
2451 \end{codeexample}
2452 \begin{codeexample}[width=0cm]
2453 \begin{ZX}
2454 \zxN{} & \zxFracX{\pi}{2} \ar[dl,.^] \ar[dr,^.] & \\
2455 \zxZ{} & & \zxX{}
2456 \end{ZX}
2457 \end{codeexample}
2458 \begin{codeexample}[width=0cm]
2459 \begin{ZX}
2460 \zxZ{} & & \zxX{}\\
2461 \zxN{} & \zxX{} \ar[ul,'v] \ar[ur,v'] &
2462 \end{ZX}
2463 \end{codeexample}
2464 \end{pgfmanualentry}
2465
2466
2467 \begin{pgfmanualentry}
2468 \makeatletter
2469 \def\extrakeytext{style, }
2470 \extractkey/zx/wires definition/3 dots=text (default =)\@nil%
2471 \extractkey/zx/wires definition/3 vdots=text (default =)\@nil%
2472 \makeatother
2473 \pgfmanualbody
2474 The styles put in the middle of the wire (without drawing the wire) $\dots$ (for |3 dots|) or $\vdots$ (for |3 vdots|). The dots are scaled according to |\zxScaleDots| and the text \meta{text} is written on the left. Use |&[\zxDotsRow]| and |\\[\zxDotsRow]| to properly adapt the spacing of columns and rows.
2475 \begin{codeexample}[width=3cm]
2476 \begin{ZX}
2477 \zxZ{\alpha} \ar[r,o'] \ar[r,o.]
2478 \ar[r,3 dots]
2479 \ar[d,3 vdots={$n$\,}] &[\zxDotsCol] \zxFracX{\pi}{2}\\[\zxDotsRow]
2480 \zxZ{\alpha} \rar & \zxFracX{\pi}{2}
2481 \end{ZX}
2482 \end{codeexample}
2483 \end{pgfmanualentry}
2484
2485 \begin{pgfmanualentry}
2486 \makeatletter
2487 \def\extrakeytext{style, }
2488 \extractkey/zx/wires definition/H=style (default {})\@nil%
2489 \extractkey/zx/wires definition/Z=style (default {})\@nil%
2490 \extractkey/zx/wires definition/X=style (default {})\@nil%
2491 \makeatother
2492 \pgfmanualbody
2493 Adds a |H| (Hadamard), |Z| or |X| node (without phase) in the middle of the wire. Width of column or rows should be adapted accordingly using |\zxNameRowcolFlatnot| where |Name| is replaced by |H|, |S| (for ``spiders'', i.e.\ |X| or |Z|), |HS| (for both |H| and |S|) or |W|, |Rowcol| is either |Row| (for changing row sep) or |Col| (for changing column sep) and |Flatnot| is empty or |Flat| (if the wire is supposed to be a straight line as it requires more space). For instance:
2494 \begin{codeexample}[width=3cm]
2495 \begin{ZX}
2496 \zxZ{\alpha} \ar[d] \ar[r,o',H] \ar[r,o.,H] &[\zxHCol] \zxX{\beta}\\
2497 \zxZ{\alpha} \ar[d,-o,X] \ar[d,o-,Z] \\[\zxHSRow]
2498 \zxX{\gamma}
2499 \end{ZX}
2500 \end{codeexample}
2501 The \meta{style} parameter can be used to add additional \tikzname{} style to the nodes, notably a position using |\ar[rd,-N.,H={pos=.35}]|. The reason for using that is that the wires start inside the nodes, therefore the ``middle'' of the wire is closer to the node when the other side is on an empty node.
2502 \begin{codeexample}[width=0pt]
2503 \begin{ZX}[zx row sep=0pt]
2504 \zxN{} \ar[rd,-N.,H={pos=.35}] &[\zxwCol,\zxHCol] &[\zxwCol,\zxHCol] \zxN{} \\[\zxNRow]%%
2505 & \zxX{\alpha}
2506 \ar[ru,N'-,H={pos=1-.35}]
2507 \ar[rd,N.-,H={pos=1-.35}] & \\[\zxNRow]
2508 \zxN{} \ar[ru,-N',H={pos=.35}] & & \zxN{}
2509 \end{ZX}
2510 \end{codeexample}
2511 Note that it's possible to automatically start wires on the border of the node, but it is slower and create other issues, see \cref{subsec:wiresInsideOutside} for more details. The second option (also presented in this section) is to manually define the |start anchor| and |end anchor|, but it can change the shape of the wire).
2512 \end{pgfmanualentry}
2513
2514 \begin{pgfmanualentry}
2515 \makeatletter
2516 \def\extrakeytext{style, }
2517 \extractkey/zx/wires definition/wire centered\@nil%
2518 \extractkey/zx/wires definition/wc\@nil%
2519 \extractkey/zx/wires definition/wire centered start\@nil%
2520 \extractkey/zx/wires definition/wcs\@nil%
2521 \extractkey/zx/wires definition/wire centered end\@nil%
2522 \extractkey/zx/wires definition/wce\@nil%
2523 \makeatother
2524 \pgfmanualbody
2525 Forces the wires to start at the center of the node (|wire centered start|, alias |wcs|), to end at the center of the node (|wire centered end|, alias |wce|) or both (|wire centered|, alias |wc|). This may be useful, for instance in the old |IO| mode (see below) when nodes have different sizes (the result looks strange otherwise), or with some wires (like |C|) connected to |\ZxNone+| (if possible, use |\zxNone| (without any embellishment) since it does not suffer from this issue as it is a coordinate).
2526
2527 See also |between none| to also increase looseness when connecting only wires (use |between none| \emph{only} in |IO| mode).
2528 \begin{codeexample}[width=3cm]
2529 \begin{ZX}
2530 \zxZ{} \ar[IO,o',r] \ar[IO,o.,r] & \zxX{\alpha}\\
2531 \zxZ{} \ar[IO,o',r,wc] \ar[IO,o.,r,wc] & \zxX{\alpha}
2532 \end{ZX}
2533 \end{codeexample}
2534 % Without |wc| (note that because there is no node, we need to use |&[\zxWCol]| (for columns) and |\\[\zxWRow]| (for rows) to get nicer spacing):
2535 % \begin{codeexample}[width=3cm]
2536 % \zx{\zxNone{} \rar &[\zxWCol] \zxNone{} \rar &[\zxWCol] \zxNone{} }
2537 % \end{codeexample}
2538 % With |wc|:
2539 % \begin{codeexample}[width=3cm]
2540 % \zx{\zxNone{} \rar[wc] &[\zxWCol] \zxNone{} \rar[wc] &[\zxWCol] \zxNone{}}
2541 % \end{codeexample}
2542 \end{pgfmanualentry}
2543
2544
2545 \begin{pgfmanualentry}
2546 \makeatletter
2547 \def\extrakeytext{style, }
2548 \extractkey/zx/wires definition/bezier=-andL config \@nil%
2549 \extractkey/zx/wires definition/bezier x=-andL config \@nil%
2550 \extractkey/zx/wires definition/bezier y=-andL config \@nil%
2551 \extractkey/zx/wires definition/bezier 4=\{x1\}\{y1\}\{x2\}\{y2\} \@nil%
2552 \extractkey/zx/wires definition/bezier 4 x=\{x1\}\{y1\}\{x2\}\{y2\} \@nil%
2553 \extractkey/zx/wires definition/bezier 4 y=\{x1\}\{y1\}\{x2\}\{y2\} \@nil%
2554 \makeatother
2555 \pgfmanualbody
2556 To create a bezier wire. These styles are not really meant to be used for the final user because they are long to type and would not be changed document-wise when the style is changed, but most styles are based on these styles. For the |bezier 4 *| versions, the two first arguments are the relative position of the first anchor (|x| and |y| position), the next two of the second anchor. In the |bezier *| versions, the value of |1-| will be the relative |x| position of the first point, |1L| the relative position of the second, and |2-| and |2L| will be for the second point (the advantage of this is that it is also possible to specify angles using |1al={angle}{length}|\dots{} as explained in the |-andL| syle). They are said to be relative in the sense that |{0}{0}| is the coordinate of the first point, and |{1}{1}| the second point. The |bezier x| and |bezier 4 y| are useful when the node are supposed to be horizontally or vertically aligned: the distance are now expressed as a fraction of the horizontal (respectively vertical) distance between the two nodes. Using relative coordinates has the advantage that if the nodes positions are moved, the aspect of the wire does not change (it is just squeezed), while this is not true with |in|/|out| wires which preserves angles but not shapes.
2557 Note that all arrows that are based on the |bezier| style (most of the keys: |o|-like, |N|-like, |<|-like, |s|-like) can try to connect to subnodes or anchors called |fake center {east/north/west/south}| in order to start the wire in between the real center and an |east/...| coordinate (note that some variations can use another anchor, for instance |<'| will set the end anchor to |west| by default). Since adding manually an anchor to a node can be quite tedious, it is often easier to create a subnode, using either |zx subnode=name of subnode| or |\zxCoordinateSubnode{name of subnode}|. For instance, we show how to create a node such that bezier arriving from right will be connected to the south east part of the node while bezier leaving from the left will start at the north east anchor:
2558 \begin{codeexample}[width=0pt]
2559 \NewExpandableDocumentCommand{\myWeirdNode}{m}{%
2560 \node[zx main node,alias=tmp,fill=pink,rounded corners,inner sep=2pt]{A};
2561 % will be used by bezier curves to know how to connect:
2562 \zxCoordinateSubnode[at={(tmp.north east)}]{fake center east}
2563 \zxCoordinateSubnode[at={(tmp.south west)}]{fake center west}
2564 }
2565
2566 \begin{ZX}
2567 \myWeirdNode{A} \ar[rd, s] \ar[C,dr] & & \myWeirdNode{B} \\
2568 & \zxZ{a \oplus b \oplus c \oplus d} \ar[ru, s] &
2569 \end{ZX}
2570 \end{codeexample}
2571 \end{pgfmanualentry}
2572
2573
2574 \subsubsection{IO wires, the old styles}\label{subsub:IOwires}
2575
2576 \begin{stylekey}{/zx/wires definition/IO}
2577 As explained above, wires were first defined using |in|, |out| and |looseness|, but this turned out to be sometimes hard to use since the shape of the wire was changing depending on the position. For example consider the differences between the older version:
2578 \begin{codeexample}[]
2579 \begin{ZX}
2580 \zxN{} & \zxZ{} \\
2581 \zxZ{} \ar[ru,IO,N'] \ar[rd,IO,N.] &\\
2582 & \zxZ{} \\
2583 \end{ZX}
2584 \begin{ZX}
2585 \zxN{} & \zxZ{} \\[-3pt]
2586 \zxZ{} \ar[ru,IO,N'] \ar[rd,IO,N.] &\\[-3pt]
2587 & \zxZ{} \\
2588 \end{ZX}
2589 \begin{ZX}
2590 \zxN{} & \zxZ{} \\[-5pt]
2591 \zxZ{} \ar[ru,IO,N'] \ar[rd,IO,N.] &\\[-5pt]
2592 & \zxZ{} \\
2593 \end{ZX}
2594 \end{codeexample}
2595 \begin{codeexample}[]
2596 \begin{ZX}
2597 \zxN{} & \zxZ{} \\
2598 \zxZ{} \ar[ru,N'] \ar[rd,N.] &\\
2599 & \zxZ{} \\
2600 \end{ZX}
2601 and the newer:
2602 \begin{ZX}
2603 \zxN{} & \zxZ{} \\[-3pt]
2604 \zxZ{} \ar[ru,N'] \ar[rd,N.] &\\[-3pt]
2605 & \zxZ{} \\
2606 \end{ZX}
2607 \begin{ZX}
2608 \zxN{} & \zxZ{} \\[-5pt]
2609 \zxZ{} \ar[ru,N'] \ar[rd,N.] &\\[-5pt]
2610 & \zxZ{} \\
2611 \end{ZX}
2612 \end{codeexample}
2613 Here is another example:
2614 \begin{codeexample}[]
2615 Before \begin{ZX}
2616 \zxNone{} \ar[IO,C,d,wc] \ar[rd,IO,s] &[\zxWCol] \zxNone{} \\[\zxWRow]
2617 \zxNone{} \ar[ru,IO,s] & \zxNone{}
2618 \end{ZX} after \begin{ZX}
2619 \zxNone{} \ar[C,d] \ar[rd,s] &[\zxWCol] \zxNone{} \\[\zxWRow]
2620 \zxNone{} \ar[ru,s] & \zxNone{}
2621 \end{ZX}
2622 \end{codeexample}
2623 This example led to the creation of the |bn| style, in order to try to find appropriate looseness values depending on the case\dots{} but it is harder to use and results are less predictable.
2624
2625 The new method also allowed us to use |N| for both |N.| and |N'| styles (however we kept both versions for backward compatibility and in case later we want to make a distinction between nodes going doing or up).
2626
2627 However, if you prefer the old style, you can just use them by adding |IO,| in front of the style name (styles are nested inside |IO|). Note however that the customization options are of course different.
2628 \end{stylekey}
2629
2630 We list now the older |IO| styles:
2631
2632 \begin{pgfmanualentry}
2633 \makeatletter
2634 \def\extrakeytext{style, }
2635 \extractkey/zx/wires definition/IO/C\@nil%
2636 \extractkey/zx/wires definition/IO/C\@nil%
2637 \extractkey/zx/wires definition/IO/C'\@nil%
2638 \extractkey/zx/wires definition/IO/C-\@nil%
2639 \makeatother
2640 \pgfmanualbody
2641 |IO| mode for the |C| wire (used for Bell-like shapes).
2642 \begin{codeexample}[]
2643 Bell pair \zx{\zxNone{} \ar[d,IO,C] \\[\zxWRow]
2644 \zxNone{}}
2645 and funny graph
2646 \begin{ZX}
2647 \zxX{} \ar[d,IO,C] \ar[r,C'] & \zxZ{} \ar[d,IO,C-]\\
2648 \zxZ{} \ar[r,IO,C.] & \zxX{}
2649 \end{ZX}.
2650 \end{codeexample}
2651 Note that the |IO| version cannot really be used when nodes are not aligned (using |wc| can sometimes help with the alignment):
2652 \begin{codeexample}[]
2653 Normal \begin{ZX}
2654 \zxX{\alpha} \ar[dr,C]\\
2655 & \zxNone{}
2656 \end{ZX}, and IO \begin{ZX}
2657 \zxX{\alpha} \ar[dr,IO,C]\\
2658 & \zxNone{}
2659 \end{ZX}
2660 \end{codeexample}
2661 \end{pgfmanualentry}
2662
2663 \begin{pgfmanualentry}
2664 \makeatletter
2665 \def\extrakeytext{style, }
2666 \extractkey/zx/wires definition/IO/o'=angle (default 40)\@nil%
2667 \extractkey/zx/wires definition/IO/o.=angle (default 40)\@nil%
2668 \extractkey/zx/wires definition/IO/o-=angle (default 40)\@nil%
2669 \extractkey/zx/wires definition/IO/-o=angle (default 40)\@nil%
2670 \makeatother
2671 \pgfmanualbody
2672 |IO| version of |o|. Curved wire, similar to |C| but with a soften angle (optionally specified via \meta{angle}, and globally editable with |\zxDefaultLineWidth|). Again, the symbols specify which part of the circle (represented with |o|) must be kept.
2673 \begin{codeexample}[width=3cm]
2674 \begin{ZX}
2675 \zxX{} \ar[d,IO,-o] \ar[d,IO,o-]\\
2676 \zxZ{} \ar[r,IO,o'] \ar[r,IO,o.] & \zxX{}
2677 \end{ZX}.
2678 \end{codeexample}
2679 Note that these wires can be combined with |H|, |X| or |Z|, in that case one should use appropriate column and row spacing as explained in their documentation:
2680 \begin{codeexample}[width=3cm]
2681 \begin{ZX}
2682 \zxX{\alpha} \ar[d,IO,-o,H] \ar[d,IO,o-,H]\\[\zxHRow]
2683 \zxZ{\beta} \rar & \zxZ{} \ar[r,IO,o',X] \ar[r,IO,o.,Z] &[\zxSCol] \zxX{}
2684 \end{ZX}.
2685 \end{codeexample}
2686 \end{pgfmanualentry}
2687
2688 \begin{pgfmanualentry}
2689 \makeatletter
2690 \def\extrakeytext{style, }
2691 \extractkey/zx/wires definition/IO/(=angle (default 30)\@nil%
2692 \extractkey/zx/wires definition/IO/)=angle (default 30)\@nil%
2693 \extractkey/zx/wires definition/IO/('=angle (default 30)\@nil%
2694 \extractkey/zx/wires definition/IO/('=angle (default 30)\@nil%
2695 \makeatother
2696 \pgfmanualbody
2697 |IO| version of |(| (so far they are the same, but it may change later, use this version if you want to play with |looseness|). Curved wire, similar to |o| but can be used for diagonal items. The angle is, like in |bend right|, the opening angle from the line which links the two nodes. For the first two commands, the |(| and |)| symbols must be imagined as if the starting point was on top of the parens, and the ending point at the bottom.
2698 \begin{codeexample}[width=3cm]
2699 \begin{ZX}
2700 \zxX{} \ar[rd,IO,(] \ar[rd,IO,),red]\\
2701 & \zxZ{}
2702 \end{ZX}.
2703 \end{codeexample}
2704 Then, |('|=|(| and |(.|=|)|; this notation is, I think, more intuitive when linking nodes from left to right. |('| is used when going to top right and |(.| when going to bottom right.
2705 \begin{codeexample}[width=3cm]
2706 \begin{ZX}
2707 \zxN{} & \zxX{}\\
2708 \zxZ{} \ar[ru,IO,('] \ar[IO,rd,(.] & \\
2709 & \zxX{}
2710 \end{ZX}
2711 \end{codeexample}
2712 When the nodes are too far appart, the default angle of |30| may produce strange results as it will go above (for |('|) the vertical line. Either choose a shorter angle, or see |<'| instead.
2713 \end{pgfmanualentry}
2714
2715 \begin{pgfmanualentry}
2716 \makeatletter
2717 \def\extrakeytext{style, }
2718 \extractkey/zx/wires definition/IO/s\@nil%
2719 \extractkey/zx/wires definition/IO/s'=angle (default 30)\@nil%
2720 \extractkey/zx/wires definition/IO/s.=angle (default 30)\@nil%
2721 \extractkey/zx/wires definition/IO/-s'=angle (default 30)\@nil%
2722 \extractkey/zx/wires definition/IO/-s.=angle (default 30)\@nil%
2723 \extractkey/zx/wires definition/IO/s'-=angle (default 30)\@nil%
2724 \extractkey/zx/wires definition/IO/s.-=angle (default 30)\@nil%
2725 \makeatother
2726 \pgfmanualbody
2727 |IO| version of |s|. |s| is used to create a s-like wire, to have nicer soften diagonal lines between nodes. Other versions are soften versions (the input and output angles are not as sharp, and the difference angle can be configured as an argument or globally using |\zxDefaultSoftAngleS|). Adding |'| or |.| specifies if the wire is going up-right or down-right.
2728 \begin{codeexample}[width=3cm]
2729 \begin{ZX}
2730 \zxX{\alpha} \ar[IO,s,rd] \\
2731 & \zxZ{\beta}\\
2732 \zxX{\alpha} \ar[IO,s.,rd] \\
2733 & \zxZ{\beta}\\
2734 & \zxZ{\alpha}\\
2735 \zxX{\beta} \ar[IO,s,ru] \\
2736 & \zxZ{\alpha}\\
2737 \zxX{\beta} \ar[IO,s',ru] \\
2738 \end{ZX}
2739 \end{codeexample}
2740 |-| forces the angle on the side of |-| to be horizontal.
2741 \begin{codeexample}[width=3cm]
2742 \begin{ZX}
2743 \zxX{\alpha} \ar[IO,s.,rd] \\
2744 & \zxZ{\beta}\\
2745 \zxX{\alpha} \ar[IO,-s.,rd] \\
2746 & \zxZ{\beta}\\
2747 \zxX{\alpha} \ar[IO,s.-,rd] \\
2748 & \zxZ{\beta}\\
2749 \end{ZX}
2750 \end{codeexample}
2751 \end{pgfmanualentry}
2752
2753 \begin{pgfmanualentry}
2754 \makeatletter
2755 \def\extrakeytext{style, }
2756 \extractkey/zx/wires definition/IO/ss\@nil%
2757 \extractkey/zx/wires definition/IO/ss.=angle (default 30)\@nil%
2758 \extractkey/zx/wires definition/IO/.ss=angle (default 30)\@nil%
2759 \extractkey/zx/wires definition/IO/sIs.=angle (default 30)\@nil%
2760 \extractkey/zx/wires definition/IO/.sIs=angle (default 30)\@nil%
2761 \extractkey/zx/wires definition/IO/ss.I-=angle (default 30)\@nil%
2762 \extractkey/zx/wires definition/IO/I.ss-=angle (default 30)\@nil%
2763 \makeatother
2764 \pgfmanualbody
2765 |IO| version of |ss|. |ss| is similar to |s| except that we go from top to bottom instead of from left to right. The position of |.| says if the node is wire is going bottom right (|ss.|) or bottom left (|.ss|).
2766 \begin{codeexample}[width=3cm]
2767 \begin{ZX}
2768 \zxX{\alpha} \ar[IO,ss,rd] \\
2769 & \zxZ{\beta}\\
2770 \zxX{\alpha} \ar[IO,ss.,rd] \\
2771 & \zxZ{\beta}\\
2772 & \zxX{\beta} \ar[IO,.ss,dl] \\
2773 \zxZ{\alpha}\\
2774 & \zxX{\beta} \ar[IO,.ss,dl] \\
2775 \zxZ{\alpha}\\
2776 \end{ZX}
2777 \end{codeexample}
2778 |I| forces the angle above (if in between the two |s|) or below (if on the same side as |.|) to be vertical.
2779 \begin{codeexample}[width=3cm]
2780 \begin{ZX}
2781 \zxX{\alpha} \ar[IO,ss,rd] \\
2782 & \zxZ{\beta}\\
2783 \zxX{\alpha} \ar[IO,sIs.,rd] \\
2784 & \zxZ{\beta}\\
2785 \zxX{\alpha} \ar[IO,ss.I,rd] \\
2786 & \zxZ{\beta}\\
2787 & \zxX{\beta} \ar[IO,.sIs,dl] \\
2788 \zxZ{\alpha}\\
2789 & \zxX{\beta} \ar[IO,I.ss,dl] \\
2790 \zxZ{\alpha}\\
2791 \end{ZX}
2792 \end{codeexample}
2793 \end{pgfmanualentry}
2794
2795 \begin{pgfmanualentry}
2796 \makeatletter
2797 \def\extrakeytext{style, }
2798 \extractkey/zx/wires definition/IO/<'=angle (default 60)\@nil%
2799 \extractkey/zx/wires definition/IO/<.=angle (default 60)\@nil%
2800 \extractkey/zx/wires definition/IO/'>=angle (default 60)\@nil%
2801 \extractkey/zx/wires definition/IO/.>=angle (default 60)\@nil%
2802 %\extractkey/zx/wires definition/IO/^.=angle (default 60)\@nil%
2803 %\extractkey/zx/wires definition/IO/.^=angle (default 60)\@nil%
2804 \extractkey/zx/wires definition/IO/'v=angle (default 60)\@nil%
2805 \extractkey/zx/wires definition/IO/v'=angle (default 60)\@nil%
2806 \makeatother
2807 \pgfmanualbody
2808 These keys are a bit like |('| or |(.| but the arrival angle is vertical (or horizontal for the |^| (up-down) and |v| (down-up) versions). As before, the position of the decorator |.|,|'| denote the direction of the wire.
2809 \begin{codeexample}[width=0cm]
2810 \begin{ZX}
2811 \zxN{} & \zxZ{}\\
2812 \zxX{} \ar[IO,ru,<'] \ar[IO,rd,<.] \\
2813 \zxN{} & \zxZ{}\\
2814 \end{ZX}
2815 \end{codeexample}
2816 \begin{codeexample}[width=0cm]
2817 \begin{ZX}
2818 \zxN{} & \zxFracX{\pi}{2} \ar[IO,dl,.^] \ar[IO,dr,^.] & \\
2819 \zxZ{} & & \zxX{}
2820 \end{ZX}
2821 \end{codeexample}
2822 \begin{codeexample}[width=0cm]
2823 \begin{ZX}
2824 \zxZ{} & & \zxX{}\\
2825 \zxN{} & \zxX{} \ar[IO,ul,'v] \ar[IO,ur,v'] &
2826 \end{ZX}
2827 \end{codeexample}
2828 \end{pgfmanualentry}
2829
2830 \begin{pgfmanualentry}
2831 \makeatletter
2832 \def\extrakeytext{style, }
2833 \extractkey/zx/wires definition/IO/N'=angle (default 60)\@nil%
2834 \extractkey/zx/wires definition/IO/N.=angle (default 60)\@nil%
2835 \extractkey/zx/wires definition/IO/-N'=angle (default 60)\@nil%
2836 \extractkey/zx/wires definition/IO/-N.=angle (default 60)\@nil%
2837 \extractkey/zx/wires definition/IO/N'-=angle (default 60)\@nil%
2838 \extractkey/zx/wires definition/IO/N.-=angle (default 60)\@nil%
2839 \makeatother
2840 \pgfmanualbody
2841 |IO| version of |N|. |N| is used to create a wire leaving at wide angle and arriving at wide angle. Adding |'| or |.| specifies if the wire is going up-right or down-right.
2842 \begin{codeexample}[width=3cm]
2843 \begin{ZX}
2844 \zxX{\alpha} \ar[IO,N.,rd] \\
2845 & \zxZ{\beta}\\
2846 & \zxZ{\alpha}\\
2847 \zxX{\beta} \ar[IO,N',ru] \\
2848 \end{ZX}
2849 \end{codeexample}
2850 |-| forces the angle on the side of |-| to be horizontal.
2851 \begin{codeexample}[width=3cm]
2852 \begin{ZX}
2853 \zxX{\alpha} \ar[IO,-N.,rd] \\
2854 & \zxZ{\beta}\\
2855 & \zxZ{\alpha}\\
2856 \zxX{\beta} \ar[IO,N'-,ru] \\
2857 \end{ZX}
2858 \end{codeexample}
2859 \end{pgfmanualentry}
2860
2861 \begin{stylekey}{/zx/wires definition/ls=looseness}
2862 Shortcut for |looseness|, allows to quickly redefine looseness. Use with care (or redefine style directly), and \emph{do not use on styles that are not in |IO|}, since they don't use the |in|/|out| mechanism (only |(|-like styles use |in|/|out|\dots{} for now. In case you want to change looseness of |(|, prefer to use |IO,(| as it is guaranteed to be backward compatible). We may try later to give a key |looseness| for these styles, but it's not the case for now. For |IO| styles, you can also change yourself other values, like |in|, |out|\dots
2863 \begin{codeexample}[]
2864 \begin{ZX}
2865 \zxZ{} \ar[rd,s] \\
2866 & \zxX{}\\
2867 \zxZ{} \ar[rd,IO,s] \\
2868 & \zxX{}\\
2869 \zxZ{} \ar[rd,IO,s,ls=3] \\
2870 & \zxX{}
2871 \end{ZX}
2872 \end{codeexample}
2873 \end{stylekey}
2874
2875 \begin{pgfmanualentry}
2876 \makeatletter
2877 \def\extrakeytext{style, }
2878 \extractkey/zx/wires definition/between none\@nil%
2879 \extractkey/zx/wires definition/bn\@nil%
2880 \makeatother
2881 \pgfmanualbody
2882 When drawing only |IO| wires (normal wires would suffer from this parameter), the default looseness may not be good looking and holes may appear in the line. This style (whose alias is |bn|) should therefore be used when curved wires \emph{from the |IO| path} are connected together. In that case, also make sure to separate columns using |&[\zxWCol]| and rows using |\\[\zxWRow]|.
2883 \begin{codeexample}[width=3cm]
2884 A swapped Bell pair is %
2885 \begin{ZX}
2886 \zxNone{} \ar[IO,C,d,wc] \ar[rd,IO,s,bn] &[\zxWCol] \zxNone{} \\[\zxWRow]
2887 \zxNone{} \ar[ru,IO,s,bn] & \zxNone{}
2888 \end{ZX}
2889 \end{codeexample}
2890 \end{pgfmanualentry}
2891
2892 \begin{pgfmanualentry}
2893 \makeatletter
2894 \def\extrakeytext{style, }
2895 \extractkey/zx/wires definition/bold\@nil%
2896 \extractkey/zx/wires definition/B\@nil%
2897 \extractkey/zx/wires definition/non bold\@nil%
2898 \extractkey/zx/wires definition/NB\@nil%
2899 \extractkey/zx/wires definition/boldn\@nil%
2900 \extractkey/zx/wires definition/boldn-\@nil%
2901 \extractkey/zx/wires definition/boldn'\@nil%
2902 \extractkey/zx/wires definition/boldn.\@nil%
2903 \extractkey/zx/wires definition/Bn=message (default n)\@nil%
2904 \extractkey/zx/wires definition/Bn-=message (default n)\@nil%
2905 \extractkey/zx/wires definition/Bn'=message (default n)\@nil%
2906 \extractkey/zx/wires definition/Bn.=message (default n)\@nil%
2907 \extractkey/zx/wires definition/BnArgs=\{message\}\{style\} (default \{n\}\{\})\@nil%
2908 \extractkey/zx/wires definition/Bn-Args=\{message\}\{style\} (default \{n\}\{\})\@nil%
2909 \extractkey/zx/wires definition/Bn'Args=\{message\}\{style\} (default \{n\}\{\})\@nil%
2910 \extractkey/zx/wires definition/Bn.Args=\{message\}\{style\} (default \{n\}\{\})\@nil%
2911 \makeatother
2912 \pgfmanualbody
2913 Creates a bold (or non-bold) wire (|B| and |NB| being short aliases). The versions with a |n| at the end adds a $n$ on the right/left/above/below (for the respective symbols empty, |-|, |'|, |.|), that you can overwrite by providing an option:
2914 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
2915 \begin{codeexample}[width=0pt]
2916 \begin{ZX}
2917 \zxX[bold]{} \rar[Bn',o'] \rar[Bn.=m,o.] &[\zxwCol] \zxZ{} \rar &[\zxwCol] \zxN{}
2918 \end{ZX}
2919 \end{codeexample}
2920 }
2921 Finally, the last variations with args like |BnArgs| allows you to provide an additional label style, notably arguments like |pos=.7| to change the position of the line in the path (when the lines are drawn from the middle of the nodes, which is the case for many styles by default for efficiency reasons, the middle of the path may appear poorly centered: using |pos| is one method to center it back, see also \cref{subsec:wiresInsideOutside}).
2922 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
2923 \begin{codeexample}[width=0pt]
2924 \begin{ZX}
2925 \zxX[bold]{} \rar[<.,Bn.Args={n}{pos=.6},dr] &[\zxwCol] \\
2926 & \zxN{}
2927 \end{ZX}
2928 \end{codeexample}
2929 }
2930 Note that |bold| and its alias |B| can also be used as an argument to the |ZX| environment to turn all spiders and wires in bold:
2931 \begin{codeexample}[width=0pt]
2932 \begin{ZX}[bold]
2933 \zxX{} \rar[o'] \rar[o.] & \zxZ{} \rar &[\zxwCol] \zxN{}
2934 \end{ZX}
2935 \end{codeexample}
2936 You might also want to combine it with the |non bold| option to temporarily set a wire non-bold:
2937 \begin{codeexample}[width=0pt]
2938 \begin{ZX}[bold]
2939 \zxX{} \rar[o'] \rar[o.] & \zxZ{} \rar[non bold] &[\zxwCol] \zxN{}
2940 \end{ZX}
2941 \end{codeexample}
2942 \end{pgfmanualentry}
2943
2944 \begin{pgfmanualentry}
2945 \makeatletter
2946 \def\extrakeytext{style, }
2947 \extractkey/zx/styles/rounded style/wires bold\@nil%
2948 \extractkey/zx/styles/rounded style/Bw\@nil%
2949 \extractkey/zx/styles/rounded style/BBw\@nil%
2950 \makeatother
2951 \pgfmanualbody
2952 If you set |wires bold| (alias |Bw|) to a node, all wired connected to that node will be bold. |BBw| additionally turn the current node bold.
2953 \begin{codeexample}[width=0pt]
2954 \begin{ZX}
2955 \zxX{} \rar[o'] \rar[o.] & \zxZ[BBw]{} \rar &[\zxwCol] \zxN{}
2956 \end{ZX}
2957 \end{codeexample}
2958 \end{pgfmanualentry}
2959
2960
2961 \subsection{Custom nodes}\label{subsec:customNodes}
2962
2963 \textbf{NB}: a number of functionalities needed in this section were added on 24/02/2023 and 03/10/2023, so be sure to have the latest version.
2964
2965 \subsubsection{Creating your own node: method 1}
2966
2967 Technically, you can create any tikz style to create custom nodes like:
2968 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
2969 \begin{codeexample}[width=0pt]
2970 \begin{ZX}
2971 \zxN{} \rar & |[draw,fill=blue!50,draw=blue,inner sep=3mm]| \text{my custom node}
2972 \end{ZX}
2973 \end{codeexample}
2974 }
2975 possibly using |\tikzset| and/or |\NewExpandableDocumentCommand| to avoid repetitions:
2976 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
2977 \begin{codeexample}[width=0pt]
2978 \tikzset{
2979 my custom node/.style={
2980 draw,fill=blue!50,draw=blue,inner sep=3mm
2981 },
2982 }
2983 \NewExpandableDocumentCommand{\myCustomNode}{m}{|[my custom node]| \text{#1}}
2984 \begin{ZX}
2985 \zxN{} \rar & \myCustomNode{Hey}
2986 \end{ZX}
2987 \end{codeexample}
2988 }
2989
2990 \begin{pgfmanualentry}
2991 \def\extrakeytext{style, }
2992 \extractcommand\zxCoordinateSubnode\opt{\oarg{style}}\marg{name of subnode}\@@
2993 \extractcommand\zxCoordinateMain\opt{\oarg{style}}\marg{}\@@
2994 \makeatletter% should not be letter for \@@... strange
2995 \extractkey/tikz/zx main node\@nil%
2996 \extractkey/tikz/zx subnode=\marg{name of subnode}\@nil%
2997 \extractkey/tikz/zx at origin\@nil%
2998 \extractkey/tikz/start subnode=\marg{name of subnode}\@nil%
2999 \extractkey/tikz/end subnode=\marg{name of subnode}\@nil%
3000 \makeatother
3001 \pgfmanualbody%
3002
3003 However, we recommend users to use the |\node[zx main node,zx at origin, ...]{...};| syntax rather than:
3004 \begin{verbatim}
3005 |[...]| ...
3006 \end{verbatim}
3007 for multiple reasons: first, it will be more robust (for instance you need the |\node| syntax if you plan to insert this in |zxExecuteAtEndPicture| or in a regular tikz picture which is not contained in a matrix), but you also have more freedom since you can create nodes having multiples subnodes, anchors etc.
3008 You should note the |zx main node| that is needed to give a name to the current node so that |\arrow| can find it, otherwise you will get an error saying that the node does not exists when you try to link to it, and |zx at origin| that will just do |at={(0,0)}| (which is the default position anyway) but that might be useful later in order to easily place a node outside of a matrix. We also provide |\zxCoordinateMain| as the equivalent of |zx main node| for coordinates since they would not accept a name via a style.
3009
3010 Subnodes are a kind of generalization of anchors: you can define an existing node as a subnode with |zx subnode=your subnode| and link from/to this node using |start subnode=your subnode| and |end subnode=your subnode|:
3011
3012 \begin{codeexample}[width=0pt]
3013 \NewExpandableDocumentCommand{\zxMyDoubleNode}{O{}m}{
3014 \node[fill=blue!20, zx main node, inner sep=3mm,alias=zxtmp,#1] {\text{#2}};
3015 \node[circle,fill=red!70, inner sep=.8mm,zx subnode=redNode] at ([shift={(0mm,1.5mm)}]zxtmp.west) {};
3016 \node[circle,fill=orange!70, inner sep=.8mm,zx subnode=orangeNode] at ([shift={(0mm,-1.5mm)}]zxtmp.west) {};
3017 \node[circle,fill=green!70, inner sep=.8mm,zx subnode=greenNode] at ([shift={(0mm,-1.5mm)}]zxtmp.east) {};
3018 }
3019 \begin{ZX}
3020 A \rar[end subnode=redNode] \rar[end subnode=orangeNode]
3021 & [5mm] \zxMyDoubleNode{Hey} \dar \rar[start subnode=greenNode,end subnode=orangeNode]
3022 & \zxMyDoubleNode[fill=pink]{Hey}\\
3023 B \ar[ru, end subnode=orangeNode] &
3024 \end{ZX}
3025 \end{codeexample}
3026 Node that you can also set some special subnodes called |fake center {north,south,east,west}| in order to help bezier-based wires to find where to start, and |true {north,south,east,west}| to say to |C| where to start (see the corresponding section for more details). Since we often want to specify coordinates here to be precise, you can use |\zxCoordinateSubnode| to place the coordinate at the right place.
3027 \end{pgfmanualentry}
3028
3029
3030 \subsubsection{Method 2: using pic}
3031
3032 We also provide a second approach to create custom nodes (the syntax is actually very close) based on |pic|. This has pro and cons compared to the above approach: it is a bit more complicated and involve more dark magic under the hood, but only creates a single node, provides reusable styles that might be easier to integrate in a tikz picture, and we also provide some automatic way to deal with rotations, scaling etc... Historically, this was created before we realized that cells could contain multiple nodes and that it was possible to use subnodes to anchors, but now that subnodes have been introduced both approaches are relatively close. Nonetheless, all tricks introduced here can be used with the first method, except that anchors should be replaced with subnodes.
3033
3034 \begin{pgfmanualentry}
3035 \def\extrakeytext{style, }
3036 \extractcommand\zxNewNodeFromPic\marg{name new node}\oarg{style before user}\oarg{style after user}\marg{code}\@@
3037 \makeatletter% should not be letter for \@@... strange
3038 \extractkey/tikz/zx create anchors=\marg{list, of, coordinates, to, turn, in, anchor}\@nil%
3039 \extractkey/tikz/invert top bottom\@nil%
3040 \extractkey/tikz/start subnode=\marg{name of subnode}\@nil%
3041 \extractkey/tikz/stop subnode=\marg{name of subnode}\@nil%
3042 \makeatother
3043 \pgfmanualbody
3044 |\zxNewNodeFromPic{Name}[style before][style after]{code}| is used to create a new command with name |\zxName|. |code| is the code to draw the node (that you would use in a |\pic| environment, and that will be centered on |(0,0)|):
3045 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3046 \begin{codeexample}[width=0pt]
3047 \zxNewNodeFromPic{MyNode}{
3048 \node[draw,fill=blue!50,draw=blue,inner sep=3mm, zx main node, alias=mynode] at (0,0) {};
3049 \node[draw,fill=red!50,draw=blue,inner sep=1mm] at (mynode.north east) {};
3050 }
3051 \begin{ZX}
3052 \zxN{} \rar & \zxMyNode{} \rar & \zxMyNode{}
3053 \end{ZX}
3054 \end{codeexample}
3055 }
3056 Note how giving |zx main node| to a node makes it the target of arrows (note that you should make sure that your picture has at least one main node if you want features like |debug mode| to work). Without this, the lines would go to the coordinate |(0,0)|, and go through all the nodes:
3057 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3058 \begin{codeexample}[width=0pt]
3059 \zxNewNodeFromPic{MyNode}{
3060 \node[draw,fill=blue!50,draw=blue,inner sep=3mm, alias=mynode] at (0,0) {};
3061 \node[draw,fill=red!50,draw=blue,inner sep=1mm] at (mynode.north east) {};
3062 }
3063 \begin{ZX}
3064 \zxN{} \rar & \zxMyNode{}
3065 \end{ZX}
3066 \end{codeexample}
3067 }
3068 Note that sometimes, one might want to draw lines to a fixed point, while leaving the line below the object (for instance if you use a |rounded corners| option and try to enter by the corner, tikz will actually stop before which is not beautiful). A simple solution is to use |wc| (short for |wire centered|) on the arrow, and it will automatically move the path on a layer behind (see also |wcs| and |wce| to configure only the starting or ending points, and some other nodes also use this, notably most styles based on |bezier|, like |s|):
3069 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3070 \begin{codeexample}[width=0pt]
3071 \zxNewNodeFromPic{MyDivider}{
3072 \node[regular polygon, regular polygon sides=3, shape border rotate=90,
3073 draw=black,fill=gray!50, inner sep=1.6pt, rounded corners=0.8mm,zx main node] {};
3074 }
3075 Compare %
3076 \begin{ZX}
3077 \zxZ{} \rar[very thick] &\zxMyDivider{} \rar[very thick] & \zxN{}
3078 \end{ZX} %
3079 and %
3080 \begin{ZX}
3081 \zxZ{} \rar[very thick,wce] &\zxMyDivider{} \rar[very thick] & \zxN{}
3082 \end{ZX}
3083 \end{codeexample}
3084 }
3085 Note that I'm working on a better solutions to move nodes on different layers, or automatically see which anchor to choose depending on the custom node… but it's not that \mylink{https://tex.stackexchange.com/questions/618823/node-on-layer-style-in-tikz-matrix-tikzcd}{easy}.
3086
3087 We can similarly put the |zx main node| on the other node to point to the other node by default:
3088 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3089 \begin{codeexample}[width=0pt]
3090 \zxNewNodeFromPic{MyNode}{
3091 \node[draw,fill=blue!50,draw=blue,inner sep=3mm, alias=mynode] at (0,0) {};
3092 \node[draw,fill=red!50,draw=blue,inner sep=1mm, zx main node] at (mynode.north east) {};
3093 }
3094 \begin{ZX}
3095 A \rar[end anchor=center,on layer=main] & \zxMyNode{} \rar & B & \zxMyNode{} & \zxMyNode{}
3096 \end{ZX}
3097 \end{codeexample}
3098 }
3099
3100 \paragraph{Add margin around nodes.} You might want to add empty paths to increase the ``margin'' around your picture, or |overlay| some nodes if you don't want them to be counted when computing the bounding box. For simplicity, we provide a command |\zxExtendBoundingBox| that extends the current bounding box. This command takes as argument a list of arguments like |left|, |right|, |top|, |bottom|, |horizontal|, |vertical|, |extend|, where each argument takes a distance and extends to the corresponding direction (|extend| extends in all directions). For instance, below we increase the box by 1cm horizontally and by 3mm on each side on the y axis:
3101 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3102 \begin{codeexample}[width=0pt]
3103 \zxNewNodeFromPic{MyNode}{
3104 \node[draw,fill=blue!50,draw=blue,inner sep=3mm, zx main node] at (0,0) {};
3105 \zxExtendBoundingBox{horizontal=1cm, vertical=3mm}
3106 }
3107 \begin{ZX}
3108 A \rar & \zxMyNode{} \rar & B\\
3109 \end{ZX}
3110 \end{codeexample}
3111 }
3112
3113 \paragraph{Give text as argument.} It is also possible to pass text as an argument by simply adding |\tikzpictext| where you want the text to appear, and calling then |\zxMyNode{your text}|:
3114 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3115 \begin{codeexample}[width=0pt]
3116 \zxNewNodeFromPic{MyNode}{
3117 \node[draw,fill=blue!50,draw=blue,inner sep=3mm, zx main node, alias=mynode] at (0,0) {\tikzpictext};
3118 \node[draw,fill=red!50,draw=blue,inner sep=1mm] at (mynode.north east) {};
3119 }
3120 \begin{ZX}
3121 \zxMyNode{Hello} & \zxMyNode{do you see} & \zxMyNode{the text} & ?
3122 \end{ZX}
3123 \end{codeexample}
3124 }
3125
3126 \paragraph{Pass arbitrary arguments.} You can actually pass virtually any argument(s) by using the optional argument of the created |\zxMyNode| (that is just an additional argument to the pic options, inserted before \emph{style after user}, but after \emph{style before user} that we can use to create additional arguments with default values using the tikz/pgf power):
3127 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3128 \begin{codeexample}[width=0pt]
3129 % Requires \usetikzlibrary {shapes.geometric} for the star shape
3130 \zxNewNodeFromPic{MyStar}[
3131 nb spikes/.store in=\myNbSides, % When users type nb spikes=42, this puts 42 into \myNbSides
3132 nb spikes=5, % <- Default value
3133 ]{
3134 \node[star, star points=\myNbSides, minimum size=6mm, draw, fill=red] at (0,0) {};
3135 }
3136 \begin{ZX}
3137 \text{Default:} & \zxMyStar{} & \text{More !} & \zxMyStar[nb spikes=8]{} & \zxMyStar[nb spikes=12]{}
3138 \end{ZX}
3139 \end{codeexample}
3140 }
3141
3142 \paragraph{Overriding existing parameters.}
3143
3144 You might want to override a custom node, for instance because this custom node was provided by an external library (e.g.\ the ground and scalable ZX symbols are made using custom-nodes, and you might want to customize them). We provide for that a series of styles: if |XXXX| is the name on the node (e.g.\ |Ground|), the |/zx/picCustomStyleBeforeUserXXXX| style is sent to the picture style before loading the options of the user, |/zx/picCustomStyleAfterUserXXXX| is loaded after the input of the user, |/zx/picCustomStyleLastPicXXXX| is loaded as the last argument to the picture (after the default style). You can also set the command |\zxCustomPicAdditionalPic| to any code to draw at the end of the picture:
3145 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3146 \begin{codeexample}[width=0pt]
3147 \tikzset{
3148 /zx/picCustomStyleBeforeUserMatrix/.style={
3149 scale=2,
3150 % \zxCustomPicAdditionalPic can be any tikz code to run after the creation of the pic:
3151 /utils/exec={\def\zxCustomPicAdditionalPic{%
3152 % the main node has empty name, so .center is the center of the main node
3153 \node[draw,circle,inner sep=2pt,fill=pink] at (.center) {};%
3154 }%
3155 },
3156 },
3157 }
3158 \begin{ZX}
3159 \zxZ{} \rar & \zxMatrix{A} \rar & \zxMatrix*{B} \rar & \zxN{}
3160 \end{ZX}
3161 \end{codeexample}
3162 }
3163
3164 When designing your own custom nodes, you can also put:\\
3165 |NameOfYourStyle/.append style={}, NameOfYourStyle,|\\
3166 where you want to allow users to load |NameOfYourStyle| (|NameOfYourStyle/.append style={}| is used to create the style if it does not exist).
3167
3168 If you want to change a create a new node style without overriding a style, you can of course create your own command that sets the parameters for you:
3169 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3170 \begin{codeexample}[width=0pt]
3171 \tikzset{
3172 /zx/picCustomStyleBeforeUserMatrix/.style={
3173 scale=2,
3174 % \zxCustomPicAdditionalPic can be any tikz code to run after the creation of the pic:
3175 /utils/exec={\def\zxCustomPicAdditionalPic{%
3176 % the main node has empty name, so .center is the center of the main node
3177 \node[draw,circle,inner sep=2pt,fill=pink] at (.center) {};%
3178 }%
3179 },
3180 },
3181 }
3182 \begin{ZX}
3183 \zxZ{} \rar & \zxMatrix{A} \rar & \zxMatrix*{B} \rar & \zxN{}
3184 \end{ZX}
3185 \end{codeexample}
3186 }
3187
3188 \paragraph{Local style.}
3189
3190 Sometimes, you might want to define styles that are available only locally: for instance, you might prefer to type |main={fill=green}| instead of:\\
3191 |/zx/picCustomStyleBoxMainNode/.append style={fill=green}|\\
3192 Nothing prevents you from doing so directly in the default style. Just, make sure to use, e.g.\ |####1|, if you want to call an argument (internally we use nested styles and functions):
3193
3194 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3195 \begin{codeexample}[width=0pt]
3196 \zxNewNodeFromPic{MyBox}[
3197 main/.style={
3198 /zx/picCustomStyleMyBoxMainNode/.append style={####1},
3199 },
3200 ]{%
3201 \node[draw, inner sep=1.3mm, rectangle, zx main node, execute at begin node=$, execute at end node=$,
3202 /zx/picCustomStyleMyBoxMainNode/.append style={}, % Make sure it exists to avoid errors
3203 /zx/picCustomStyleMyBoxMainNode,
3204 ]{\tikzpictext};%
3205 }
3206
3207 \begin{ZX}
3208 \zxMyBox{G} \rar[B] & [\zxwCol] \zxN{}\\
3209 \zxMyBox[main={fill=green}]{G} \rar[B] & [\zxwCol] \zxN{}
3210 \end{ZX}
3211 \end{codeexample}
3212 }
3213
3214 \paragraph{Rotations.} Note that often, we want to rotate the nodes. The automatically generated macro to add new nodes comes with multiple flavors by adding the usual symbols |-|,|'|,|.|: by default, |\myNode-| will ``rotate'' by 180 degrees the pic, |\myNode'| will rotate it by 270 degrees to fake a node put ``above'', and |\myNode.| will rotate it by 90 degrees to fake a node put below. Importantly, by default tikz rotates/scales the coordinates, but \textbf{not the nodes or text}, so you might want to specify \textbf{transform shape} on each node you want to rotate and scale according to the parent transform, or using the |every node/.append style={transform shape}| (\textbf{WARNING}: make sure to use |append| or otherwise |zx main node| you not work and you will not be able to give a name to a node) option to apply it on all nodes:
3215 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3216 \begin{codeexample}[width=0pt]
3217 \zxNewNodeFromPic{MyTriangle}[
3218 % To also rotate/scale the node, otherwise only it's coordinate is rotated
3219 every node/.append style={transform shape},
3220 % Just to show how to quickly scale a node by default, to quickly update the size of a whole picture:
3221 scale=1.3
3222 ]{
3223 \node[regular polygon, regular polygon sides=3, shape border rotate=90, draw=black,fill=blue!50, inner sep=1mm,rounded corners, zx main node] {};
3224 }
3225 \begin{ZX}
3226 \zxMyTriangle{} \rar & \zxMyTriangle-{} & \zxMyTriangle'{} \dar \\
3227 & & \zxMyTriangle.{}
3228 \end{ZX}
3229 \end{codeexample}
3230 }
3231 Note however that you can customize how rotations is applied. By default, the |'-.| variants updates |\zxCurrentRotationMode| to the rotation angle (from 0 to 90,180,270), and do |rotate=\zxCurrentRotationMode|. You can however easily change that behavior, by first undoing this rotation (in the \emph{style before user}) with |rotate=-\zxCurrentRotationMode| and do any action you like depending on the value of |\zxCurrentRotationMode|. Here is a nice use-case: if you rotate a node with |transform shape|, it will also rotate the text inside:
3232 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3233 \begin{codeexample}[width=0pt]
3234 \zxNewNodeFromPic{MyTriangle}[
3235 every node/.append style={transform shape},
3236 ]{
3237 \node[regular polygon, regular polygon sides=3, shape border rotate=90, draw=black,fill=blue!50, font=\footnotesize,inner sep=1pt,rounded corners, zx main node] {\tikzpictext};
3238 }
3239 \begin{ZX}
3240 \zxMyTriangle{A} \rar & \zxMyTriangle-{A} & \zxMyTriangle'{A} \dar \\
3241 & & \zxMyTriangle.{A} \\
3242 \end{ZX}
3243 \end{codeexample}
3244 }
3245 To avoid that issue, we will undo the default rotation, and apply on the node of interest a rotation with |shape border rotate| (that rotates the border but not the text inside):
3246 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3247 \begin{codeexample}[width=0pt]
3248 \zxNewNodeFromPic{MyTriangle}[
3249 % We undo the default rotation:
3250 rotate=-\zxCurrentRotationMode,
3251 ]{
3252 \node[
3253 regular polygon, regular polygon sides=3, draw=black,fill=blue!50, font=\footnotesize,
3254 inner sep=1pt,rounded corners, zx main node,
3255 shape border rotate=90+\zxCurrentRotationMode, % Rotates the shape but not the text
3256 ] {\tikzpictext};
3257 }
3258 \begin{ZX}
3259 \zxMyTriangle{A} \rar & \zxMyTriangle-{A} & \zxMyTriangle'{A} \dar \\
3260 & & \zxMyTriangle.{A} \\
3261 \end{ZX}
3262 \end{codeexample}
3263 }
3264 Note that one might want to exchange |\zxMyNode|/|\zxMyNode-| or |\zxMyNode'|/|\zxMyNode.| (for instance, it might be more natural to denote |'| in place of |.| and |.| in place |'|, since these symbols visually represent the top/bottom place of the node). In that case, you just need to apply the style |invert top bottom| or |invert right left| in your \emph{style before user} and it will invert them automatically.
3265
3266 \paragraph{Add anchors to node.} We can automatically (and easily) add multiple anchors to our nodes (the |fake center east/…| anchors are used for instance by shapes like |\ar[<']| to see where the node should start from, while the |true north/…| anchors are used to notify where is the true north after the rotation (after rotating a node, the east might actually be on the north etc), which is for instance used by shapes like |C'|): just add coordinates with the name of the desired anchor in your graph (say |anchorA| and |anchorB|), and in \emph{style \textbf{after} user} add this list of anchor like: |zx create anchors={anchorA,anchorB}| (actually this list is expanded using the |\foreach| syntax so you can use more advanced syntax like |anchor1,anchor...,anchor10| to avoid repeating the 10 names of the anchor). For instance, we can do this way:
3267 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3268 \begin{codeexample}[width=0pt]
3269 \zxNewNodeFromPic{MyStar}[][
3270 % Note that this is the SECOND optional argument!
3271 zx create anchors={anchorA,anchorB},
3272 ]{
3273 \node[star, minimum size=6mm, draw, alias=mystar, fill=red, zx main node] at (0,0) {};
3274 \coordinate (anchorA) at (mystar.outer point 1);
3275 \coordinate (anchorB) at (mystar.outer point 2);
3276 }
3277 \begin{ZX}
3278 A \rar \rar[->,end anchor=anchorA,bend left] \rar[->,end anchor=anchorB,bend left] &[5mm] \zxMyStar{}
3279 \end{ZX}
3280 \end{codeexample}
3281 }
3282 The ZX library typically uses some special anchors like |fake center east/…| and |true east/…| to determine where some wires should start (not all kinds of wires follow this convention, for instance |<'| uses |fake center| since the wire is supposed to leave close to the center, while |C| follows |true west| since the wire is supposed to leave from the west). Because the north anchor is not anymore in the north after applying a rotation, we provide also |\zxVirtualCenterWest| (same for |East|, |North|, |South|) that will ``counter balance'' the rotation, to ensure the real north anchor is always in the north, by renaming the anchors appropriately. This way, by using these ``virtual anchors'', you only need to place your anchor when the shape is not rotated, and it should automatically rotate the anchors appropriately when needed (see remarks regarding |every node/.append style={transform shape}|):
3283 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3284 \begin{codeexample}[width=0pt]
3285 % Define a reusable node
3286 \zxNewNodeFromPic{MyDivider}[][zx create anchors={\zxVirtualCenterWest, \zxVirtualCenterEast},
3287 every node/.append style={transform shape}
3288 ]{
3289 \node[regular polygon, regular polygon sides=3,shape border rotate=90,%shape border rotate=90,
3290 draw=black,fill=gray!50, inner sep=1.6pt, rounded corners=0.8mm,zx main node] {};
3291 \coordinate(\zxVirtualCenterEast) at (.2mm,0); % Used to start lines on the side of the shape
3292 \coordinate(\zxVirtualCenterWest) at (-1mm,0);
3293 }
3294 % Use the node horizontally
3295 \begin{ZX}
3296 \zxZ[B]{} \rar[Bn'=n+m, wc] & \zxMyDivider{}
3297 \rar[o',Bn'Args={n}{}]
3298 \rar[o.,Bn.Args={m}{}] &[\zxWCol] \zxMyDivider-{} \rar[B,wc] & \zxZ[B]{}
3299 \end{ZX}
3300 % Use the node vertically
3301 \begin{ZX}
3302 \zxZ[B]{} \dar[Bn=n+m, wc] \\
3303 \zxMyDivider'{} \dar[-o,BnArgs={n}{}] \dar[o-,Bn-Args={m}{}] \\[\zxWRow]
3304 \zxMyDivider.{} \dar[B,wc]\\
3305 \zxZ[B]{}
3306 \end{ZX}
3307 \end{codeexample}
3308 }
3309
3310 You can even dynamically add anchors depending on the input of the user. For instance, if the user can choose the number of spikes of the star, you might want to create as many anchors as there are spikes:
3311 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3312 \begin{codeexample}[width=0pt]
3313 \zxNewNodeFromPic{MyStar}[
3314 nb spikes/.store in=\myNbSides,
3315 nb spikes=5,
3316 every node/.append style={transform shape},
3317 ][
3318 % zx create anchors is parsed ultimately by \foreach so we can use lists like
3319 % this to create anchors spike-1, spike-2...
3320 zx create anchors/.expanded={spike-1,spike-...,spike-\myNbSides},
3321 ]{%
3322 \node[star, fill=red, star points=\myNbSides, minimum size=1cm, draw, alias=mystar]
3323 at (0,0) {\tikzpictext};
3324 % We need to place coordinates where we want to final anchors to be.
3325 \foreach \i in {1,...,\myNbSides}{
3326 \coordinate (spike-\i) at (mystar.outer point \i);
3327 }%
3328 }
3329 \begin{ZX}
3330 B \foreach \i in {1,...,8}{\expanded{\noexpand\rar[->,end anchor=spike-\i,bend left]}} &[5cm] \zxMyStar[nb spikes=8]{?!?}
3331 \end{ZX}
3332 \end{codeexample}
3333 }
3334
3335 Note that this library uses some anchors called |fake center {east,west,north,south}|, to indicate for some curve style where the line should start and stop (|\ar| alone does not use any anchor, and not all curves use them: |S| for instance does not use it, while |s| does: in any case, you can force it for any curve with |force left to right| and alike, see the corresponding documentation Note also that in this mode the curve is drawn by default below the shapes, but this can be changed, see the corresponding section for details).
3336 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3337 \begin{codeexample}[width=0pt]
3338 \zxNewNodeFromPic{MyNode}[][zx create anchors/.expanded={fake center west}]{
3339 \node[draw,fill=blue!50,draw=blue,inner sep=3mm, zx main node, alias=mynode] at (0,0) {};
3340 \node[draw,fill=red!50,draw=blue,inner sep=1mm] at (mynode.north east) {};
3341 \coordinate(fake center west) at (mynode.north east);
3342 }
3343 \begin{ZX}
3344 \zxZ{} \ar[rd,s] & \\
3345 A \rar & \zxMyNode{}
3346 \end{ZX}
3347 \end{codeexample}
3348 }
3349
3350 \paragraph{Link to a sub-node.} Sometimes, we might prefer to draw a link to a node instead of an anchor (an anchor can only be a single coordinate). To that end, just name your nodes as usual, and use |start subnode| or |end subnode| to do the link
3351 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3352 \begin{codeexample}[width=0pt]
3353 \zxNewNodeFromPic{MyDoubleNode}{
3354 \node[fill=blue!20, zx main node, inner sep=3mm] at (0,0) {};
3355 \node[circle,fill=red!70, inner sep=.8mm,name=redNode] at (0,1.5mm) {};
3356 \node[circle,fill=orange!70, inner sep=.8mm, name=orangeNode] at (0,-1.5mm) {};
3357 }
3358 \begin{ZX}
3359 A \rar[end subnode=redNode] \rar[end subnode=orangeNode] &[5mm] \zxMyDoubleNode{}\\
3360 B \ar[ru, end subnode=orangeNode]
3361 \end{ZX}
3362 \end{codeexample}
3363 }
3364
3365 Note that you can also combine it with |to| and |alias|:
3366 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
3367 \zxNewNodeFromPic{MyDoubleNode}{
3368 \node[fill=blue!20, zx main node, inner sep=3mm] at (0,0) {};
3369 \node[circle,fill=red!70, inner sep=.8mm,name=redNode] at (0,1.5mm) {};
3370 \node[circle,fill=orange!70, inner sep=.8mm, name=orangeNode] at (0,-1.5mm) {};
3371 }
3372 \begin{ZX}
3373 \zxZ{} \ar[to=buttons, end subnode=orangeNode] \\
3374 \zxX{} \rar & \zxMyDoubleNode[a=buttons]{} \rar &
3375 \end{ZX}
3376 }
3377
3378 \end{pgfmanualentry}
3379
3380
3381
3382
3383 \subsubsection{Create your own multi-column/row gate}
3384
3385 We provide since 2023/09/23 a number of functions in order to deal with gates spawning multiple rows or columns, including to create your own multi-columns/rows gates. We present first the individual functions, and show after how to combine them to create more advanced styles. Note that the following define commands, but you can easily insert them in a style using:
3386 \begin{verbatim}
3387 /utils/exec={\yourCommands}
3388 \end{verbatim}
3389 or by creating a new style with |my style/.code={\yourCommand}|.
3390
3391 \begin{pgfmanualentry}
3392 \extractcommand\zxGetNameAbsoluteNode\marg{row}\marg{column}\@@
3393 \pgfmanualbody%
3394 Function to get the name of a cell using its absolute coordinate in the matrix (start at 1):
3395 \begin{codeexample}[]
3396 \begin{ZX}[
3397 execute at end picture={
3398 \node[draw, rounded corners, fill=orange,
3399 node on layer=background,
3400 fit=(\zxGetNameAbsoluteNode{1}{1})(\zxGetNameAbsoluteNode{1}{2})
3401 ]{};
3402 }]
3403 A & B
3404 \end{ZX}
3405 \end{codeexample}
3406 \end{pgfmanualentry}
3407
3408 \begin{pgfmanualentry}
3409 \extractcommand\zxExecuteAtEndPicture\marg{code}\@@
3410 \extractcommand\zxOriginalRow\@@
3411 \extractcommand\zxOriginalCol\@@
3412 \extractcommand\zxGetNameRelativeNode\marg{relative row}\marg{relative column}\@@
3413 \extractcommand\zxGetNameRelativeNodeStartOne\marg{relative row}\marg{relative column}\@@
3414 \pgfmanualbody%
3415 |execute at end picture| is first annoying to use, but it also runs after the definition of the wires, meaning that it is not possible to point to nodes defined in that portion of the code. To solve these issues, we define |\zxExecuteAtEndPicture{your code}| that you can insert inside a node. Note that in |your code| you can use |\zxOriginalRow| and |\zxOriginalCol| that will contain the position of the column and row where you inserted that command, and |\zxGetNameRelativeNode| will allow you to get the name of the neighbour nodes by specifying the difference of columns and the difference of lines. Note that these variables and commands are usable in |\zxExecuteAtCellRelative| and |\zxExecuteAtCellAbsolute| as well.
3416 \begin{codeexample}[]
3417 \NewExpandableDocumentCommand{\myFitWithBelowNeighbour}{}{
3418 \zxExecuteAtEndPicture{%
3419 \node[draw, rounded corners, fill=orange,
3420 node on layer=background,
3421 fit=(\zxGetNameRelativeNode{0}{0})(\zxGetNameRelativeNode{1}{0})
3422 ]{};
3423 }%
3424 }
3425 \begin{ZX}
3426 A & B & C & D \\
3427 A & B \myFitWithBelowNeighbour & C & D \\
3428 A & B & C & D \\
3429 \end{ZX}
3430 \end{codeexample}
3431 The |zxGetNameRelativeNodeStartOne| variant index elements from $1$ instead of $0$.
3432 \end{pgfmanualentry}
3433
3434 \begin{pgfmanualentry}
3435 \def\extrakeytext{style, }
3436 \extractcommand\zxExecuteAtCellAbsolute\marg{row}\marg{column}\marg{code}\@@
3437 \extractcommand\zxExecuteAtCellRelative\marg{relative row}\marg{relative column}\marg{code}\@@
3438 \extractcommand\zxExecuteAtRegionRelative\marg{relative row}\marg{relative column}\marg{code}\@@
3439 \pgfmanualbody%
3440 Often, just adding some nodes at the end of the picture is not enough: you might want to add other nodes automatically in the matrix. For instance, if you want to implement yourself a gate spawning multiple rows, a simple way to achieve that would be to add invisible boxes inside the matrix and use them to fit the final component. You can place nodes where you want using something like this (note that you should use |zx main node| that is basically a shortcut for:
3441 \begin{verbatim}
3442 \tikzset{name=\zxCurrentDiagram-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}
3443 \end{verbatim}
3444 otherwise |\rar| will not be able to know which node to join: note that it is automatically added in nodes like |\zxX{}|):
3445 \begin{codeexample}[]
3446 \NewExpandableDocumentCommand{\mySimpleMultigate}{m}{
3447 \zxBox{#1}
3448 \zxExecuteAtCellRelative{1}{-1}{\node[zx main node, green]{\text{I am relative}};}
3449 \zxExecuteAtCellRelative{0}{1}{\node[zx main node, green]{\text{I am also relative}};}
3450 \zxExecuteAtCellAbsolute{2}{4}{\node[zx main node, red]{\text{I am absolute}};}
3451 }
3452 \begin{ZX}
3453 A & B & C \\
3454 & \mySimpleMultigate{X} & & \\
3455 & & \\
3456 \end{ZX}
3457 \end{codeexample}
3458 (note that the relative version adds it to cells relative to the current cell while the absolute version executes it at the absolute cell coordinate)
3459 \textbf{Important}: for all these manipulation to work, be sure to only specify coordinates after the main node, either later on the same line or on next rows since previous cells have already been parsed.
3460
3461 You can also do it for a whole region of size $n \times m$ using |\zxExecuteAtRegionRelative{n}{m}{code}| like this (just make sure that the cells exist):
3462 \begin{codeexample}[]
3463 \NewExpandableDocumentCommand{\myRegion}{}{
3464 \zxExecuteAtRegionRelative{2}{3}{%
3465 \zxBox{\text{\tiny Original row: \zxOriginalRow{}, column:\zxOriginalCol{}
3466 % We compute the relative position to print it:
3467 (rel: \the\numexpr\the\pgfmatrixcurrentrow -\zxOriginalRow\relax
3468 x\the\numexpr\the\pgfmatrixcurrentcolumn-\zxOriginalCol\relax).}}}
3469 }
3470 \begin{ZX}
3471 A & B & C & D & E & F & G \\
3472 & & \myRegion & & & & \\
3473 & & & & & & \\
3474 & & & & & &
3475 \end{ZX}
3476 \end{codeexample}
3477 (note that if you use |\zxExecuteAtRegionRelative| and want to define one alias for the top-left node, you should use |a if origin=alias for the node| instead of |a=alias name for the node| since it would give the alias to the last node instead of the first node). Note that if you execute it multiple times on the same cell, both codes will be executed.
3478 \end{pgfmanualentry}
3479
3480 \begin{pgfmanualentry}
3481 \def\extrakeytext{style, }
3482 \extractcommand\zxSetVariable\marg{name variable}\marg{content variable}\@@
3483 \extractcommand\zxSetVariableExpandOnce\marg{name variable}\marg{content variable}\@@
3484 \extractcommand\zxSetVariableExpand\marg{name variable}\marg{content variable}\@@
3485 \extractcommand\zxGetVariable\marg{name variable}\@@
3486 \makeatletter% should not be letter for \@@... strange
3487 \extractkey/tikz/zx style from variable=\marg{name variable}\@nil%
3488 \makeatother
3489 \pgfmanualbody%
3490 You might want at some point to pass data to the node you placed elsewhere, for instance the style it should take etc. For simple setups, you can directly pass the argument like:
3491 \begin{codeexample}[]
3492 \NewExpandableDocumentCommand{\mySimpleMultigate}{mmmm}{
3493 \zxBox{#1}
3494 \zxExecuteAtCellRelative{1}{-1}{\node[zx main node, #2]{\text{I am relative}};}
3495 \zxExecuteAtCellRelative{0}{1}{\zxBox[zx main node]{\text{I am also relative}}}
3496 \zxExecuteAtCellAbsolute{2}{4}{\node[zx main node, #4]{\text{I am absolute}};}
3497 }
3498 \begin{ZX}
3499 & & \\
3500 & \mySimpleMultigate{X}{red}{pink}{orange} & & \\
3501 & & \\
3502 \end{ZX}
3503 \end{codeexample}
3504 but for more complicated setups, you might want to use some variables. This can be done using the above commands (set it in the main node, and get it in other nodes) (or you can also directly give it the argument): this should just create a new macro and evaluate it.
3505 \begin{codeexample}[]
3506 \NewExpandableDocumentCommand{\mySimpleMultigate}{mmm}{
3507 \zxX{#1}
3508 \zxSetVariable{my variable}{Hey #2}
3509 \zxSetVariable{my style}{#3}
3510 \zxExecuteAtCellRelative{1}{-1}{\node[zx style from variable={my style}]{\text{\zxGetVariable{my variable}}};}
3511 }
3512 \begin{ZX}
3513 A & B & C \\
3514 & \mySimpleMultigate{\alpha}{Alice}{purple,opacity={.3}} & & \\
3515 & & \\
3516 \end{ZX}
3517 \end{codeexample}
3518 The difference between |\zxSetVariable|, |\zxSetVariableExpandOnce| and |\zxSetVariableExpand| is that they respectively don't expand their input like |\gdef|, expand it once, or expand it completely like |\gxdef|. This can be useful in more complex setups, for instance if you store the style/variable in a macro first using pgfkeys:
3519 \begin{codeexample}[width=0pt]
3520 \NewExpandableDocumentCommand{\MyGateMulti}{O{}mmm}{
3521 % The path; here is useful to help tikzcd to see that it is a node as it tries to search for path and \pgfkeys does not look like a path
3522 % If you forget it, you will get something like \node is not defined
3523 \path;\pgfkeys{
3524 /zx/gateMulti/.cd,
3525 content inner nodes/.store in=\myContent,
3526 content inner nodes=#4,
3527 a/.store in=\zxGateMultiAlias,
3528 a=,
3529 % .style store in is a custom handler we introduce, which
3530 % is like .store in but allows the content to have hashes
3531 % needed if the style contains something like
3532 % style inner nodes={foo/.style={fill=#1,draw=#1!50!black},foo=red}
3533 style inner nodes/.style store in=\zxGateMultiStyle,
3534 style inner nodes={},
3535 #1,
3536 }%
3537 % Store the content for letter use
3538 \zxSetVariableExpandOnce{content inner nodes}{\myContent}
3539 \zxSetVariableExpandOnce{style inner nodes}{\zxGateMultiStyle}
3540 \zxExecuteAtRegionRelativeAndOrigin{#2}{#3}{%
3541 \zxBox[zx main node,
3542 % a if origin=\zxGateMultiAlias,
3543 draw,
3544 zx style from variable={style inner nodes},
3545 %/zx/gateMulti/style inner nodes aux
3546 ]{\zxGetVariable{content inner nodes}};
3547 }%
3548 }
3549
3550 \begin{ZX}
3551 & & & & & & & \\
3552 & & \MyGateMulti[style inner nodes={red,test/.style={fill=#1,opacity=.3},test=red}]{2}{3}{H} & & & & & \\
3553 & & & & & & & \\
3554 & & & & & & & \\
3555 & & & & & & & \\
3556 \end{ZX}
3557 \end{codeexample}
3558 \end{pgfmanualentry}
3559
3560 With all of this, we can now create a simple multi-columns/rows gate mechanism (but you certainly want to use the one provided by this library if you actually care about multi-columns/rows gates:
3561
3562
3563 \begin{codeexample}[width=0pt]
3564 % \mySimpleMultigate{rows}{cols}{content}
3565 \NewExpandableDocumentCommand{\mySimpleMultigate}{mmm}{
3566 \node[zx main node, inner sep=1mm]{#3};
3567 \zxExecuteAtCellRelative{#1}{#2}{%
3568 \node[zx main node, inner sep=1mm]{#3};
3569 }
3570 \zxExecuteAtEndPicture{
3571 \node[node on layer=foreground,
3572 fill=white, inner sep=0pt, draw,
3573 fit=(\zxGetNameRelativeNode{0}{0})(\zxGetNameRelativeNode{#1}{#2}),
3574 label={[node on layer=foreground]center:{#3}}]{};
3575 }
3576 }
3577 \begin{ZX}
3578 A & B & C & D & E & F \\
3579 \rar & \mySimpleMultigate{2}{3}{X} & & & & \lar \\
3580 \rar & & & & & \lar \\
3581 \zxN{} \dar[C] \rar & & & & & \zxN[a=bottomright]{} \lar \\
3582 \zxN{} \ar[to=bottomright,C-] &
3583 \end{ZX}
3584 \end{codeexample}
3585
3586 Note that you can also define subnodes in the final node and point to them. You can either use its alias, or point to any node added via region relative or cell relative, it will automatically find the parent node:
3587
3588 \begin{codeexample}[width=0pt]
3589 % \mySimpleMultigate{rows}{cols}{content}
3590 \NewExpandableDocumentCommand{\myFancyGate}{O{}mmm}{
3591 % a=#1 make sure to add the alias on the first element
3592 \node[zx main node, inner sep=1mm, a=#1]{#4};
3593 \zxExecuteAtRegionRelative{#2}{#3}{%
3594 \node[zx main node, inner sep=1mm]{#4};
3595 }%
3596 \zxExecuteAtEndPicture{
3597 \node[node on layer=foreground,
3598 alias=tmp,
3599 opacity=.5,
3600 fill=white, inner sep=0pt, draw,
3601 fit=(\zxGetNameRelativeNode{0}{0})(\zxGetNameRelativeNode{\the\numexpr#2-1\relax}{\the\numexpr#3-1\relax}),
3602 label={[node on layer=foreground]center:{#4}}]
3603 {};
3604 \node[node on layer=foreground, fill=red, inner sep=.9mm, circle, zx subnode=redCircle] at (tmp.south east) {};
3605 }
3606 }
3607 \begin{ZX}
3608 A & B & C & D & E & F \\
3609 \rar[end subnode=redCircle, blue, bend right] & \myFancyGate[aliasMyGate]{3}{4}{X} & & & \rar & \\
3610 \rar[purple] & & & & & \lar \\
3611 \rar[end subnode=redCircle, pink, bend right] & & & & & \lar \\
3612 \ar[to=aliasMyGate,red] & \\
3613 \ar[to=aliasMyGate,end subnode=redCircle,green,bend right] &
3614 \end{ZX}
3615 \end{codeexample}
3616
3617 You can also force all nodes pointing to a region-relative or cell-relative to point to a specific anchor:
3618
3619 \begin{pgfmanualentry}
3620 \def\extrakeytext{style, }
3621 \makeatletter% should not be letter for \@@... strange
3622 \extractkey/every node/pre arrow style if start node=\marg{style, to, apply}\@nil%
3623 \extractkey/every node/pre arrow style if end node=\marg{style, to, apply}\@nil%
3624 \extractkey/every node/post arrow style if start node=\marg{style, to, apply}\@nil%
3625 \extractkey/every node/post arrow style if end node=\marg{style, to, apply}\@nil%
3626 \makeatother
3627 \pgfmanualbody%
3628 This will apply the corresponding style to any wire leaving from the current cell or arriving to the current cell depending on the variation (node that you can use these functions in any node, there is no special role played by |zxExecuteAtRegionRelativeAndOrigin| here), |pre| variants being run before the user input (\textbf{WARNING}: at the step, the target of the node is not known!) while |post| variants are run after the user input:
3629
3630 \begin{codeexample}[width=0pt]
3631 % \myFancyGate[alias]{rows}{cols}{content}
3632 \NewExpandableDocumentCommand{\myFancyGate}{O{}mmm}{
3633 \zxExecuteAtRegionRelativeAndOrigin{#2}{#3}{%
3634 \node[zx main node, inner sep=1mm, a if origin=#1,
3635 %% These two lines says to connect any line coming/leaving from here to the redCircle subnode
3636 post arrow style if start node={start subnode=redCircle},
3637 post arrow style if end node={end subnode=redCircle},
3638 ]{#4};
3639 }%
3640 \zxExecuteAtEndPicture{
3641 \node[node on layer=foreground,
3642 alias=tmp,
3643 opacity=.5,
3644 fill=white, inner sep=0pt, draw,
3645 % The expression "\the\numexpr#3-1\relax" removes 1 to the current column or it will draw too many
3646 % columns (size n x m means that we stop at element (n-1) x (m-1) since we start from zero
3647 fit=(\zxGetNameRelativeNode{0}{0})(\zxGetNameRelativeNode{\the\numexpr#2-1\relax}{\the\numexpr#3-1\relax}),
3648 label={[node on layer=foreground]center:{#4}}]
3649 {};
3650 \node[node on layer=foreground, fill=red, inner sep=.9mm, circle, zx subnode=redCircle] at (tmp.south east) {};
3651 }
3652 }
3653 \begin{ZX}
3654 A & B & C & D & E & F \\
3655 \rar & \myFancyGate[aliasMyGate]{3}{4}{X} & & & \rar & \\
3656 \rar[purple] & & & & & \lar \\
3657 \rar[end subnode=redCircle, pink, bend right] & & & & & \lar \\
3658 \ar[to=aliasMyGate,red] & \\
3659 \ar[to=aliasMyGate,end subnode=redCircle,green,bend right]
3660 \end{ZX}
3661 \end{codeexample}
3662 \end{pgfmanualentry}
3663
3664
3665 \subsection{Caching pictures via an externalization library}\label{sec:externalization}
3666
3667 The pictures built with this package can easily take maybe 0.5 seconds per picture to be built, which can easily add up to a long compilation time. Caching externalization can save you a lot of time: I went from a tiny draft taking 30 seconds to be built to a built time of 2.5 seconds.
3668
3669 \subsubsection{robust-externalize: recommended}
3670
3671 While \tikzname{} provides an external library, it has many drawbacks that make it unusable in practice. To solve that issue, I created my own library called \mylink{https://github.com/leo-colisson/robust-externalize}{\texttt{robust-externalize}}. It's still very young, but has been working really reliably in my tests (note that a major update occurred in september 2023, so we updated the instruction accordingly).
3672
3673 To use it, first install |robust-externalize| by copy/pasting robust-externalize.sty in your project (unless your \LaTeX{} distribution already has it), make sure you have version 1.1, and load it using:
3674 \begin{codeexample}[code only]
3675 \usepackage{robust-externalize}
3676 \robExtConfigure{
3677 % We create a new preset for zx pictures
3678 new preset={ZX}{
3679 latex, % we inherit from the latex preset
3680 % make sure to use the same font as the document, or the equations will not be properly aligned
3681 add to latex options={11pt},
3682 add to preamble={ % we make sure the zx library is loaded when building cached images
3683 \usepackage{amsmath}
3684 \usepackage{zx-calculus}
3685 },
3686 dependencies={}, % Add here any file that must induce a recompilation of your file if it changes.
3687 },
3688 }
3689
3690 % say to cache by default all ZX environments (can be changed later) and \zx{} commands
3691 \cacheEnvironment{ZX}{ZX}
3692 \cacheCommand{zx}{ZX}
3693
3694 % You can pass options to robust-externalize via <>
3695 \begin{ZX}<add to preamble={\def\name{Alice}}>
3696 \zxX{\text{\name}}
3697 \end{ZX}
3698 \end{codeexample}
3699
3700 Now, just recompile your project: it should cache the pictures. See \mylink{https://github.com/leo-colisson/robust-externalize}{\texttt{robust-externalize}} for more details.
3701
3702 \subsubsection{Tikz external: not recommended}
3703
3704 \textbf{WARNING}: I added some options to use tikz |external| library to save compilation time... And then I realized that tikz |external| was quite close to be unusable in practice as it has many caveats. I ended up coding my own replacement to externalize any operation, and it works much better! See comment in the previous section.
3705
3706 Since 2022/02/08, it is possible to use the tikz |external| library to save compilation time. To load it, you need to add the following tikz libraries:
3707 \begin{verse}
3708 |\usetikzlibrary{external}|\\
3709 |\usetikzlibrary{zx-calculus}|\\
3710 |\tikzexternalize|\\
3711 |\zxConfigureExternalSystemCallAuto|\\
3712 \end{verse}
3713 Then, compile with shell-escape, for instance using |pdflatex -shell-escape yourfile.tex|.
3714
3715 \textbf{WARNING}: if |external| is loaded before |zx-calculus|, you don't need to run |\zxConfigureExternalSystemCallAuto|. This command is only useful to ensure the system call used by |external| displays errors appropriately by configuring the interaction mode to match the one used by the parent compilation command. If you prefer to disable this feature to use |external|'s default, define `\def\zxDoNotPatchSystemCall{}` before loading the |zx-calculus| library.
3716
3717 \alertwarning{Note however that this has not yet being extensively tested, and the \texttt{external} library has a few caveats presented below}
3718 \begin{itemize}
3719 \item If you change the order of the diagrams, or add a diagram in the middle of the document, all subsequent diagrams will be recompiled. This issue has been reported \mylink{https://github.com/pgf-tikz/pgf/issues/758}{here} and is caused by the fact that the figures are called \texttt{figure0},\dots,\texttt{figureN}. To limit this issue, you can regularly insert \texttt{\textbackslash{}tikzsetfigurename\{nameprefix\}} in your document with different names to avoid a full recompilation of the file (or using groups to change it for a single newly added equation).
3720 \item The \texttt{external} library uses the main file to recompile each picture, so if your file is large or loads a lot of libraries, it may \mylink{https://tex.stackexchange.com/questions/633175/tikz-externalize-is-much-slower-than-tikz-on-first-run}{take a while to compile a single diagram} (to give an example, this library takes 41 seconds to compile without the externalize library, with the externalize library it takes 14mn for the first run and 3 seconds for the next runs). For this reason, you may want to use \texttt{\textbackslash{}tikzset\{external/export=false\}} or |\tikzexternaldisable| (the latter won't fail if your elements are not parsable by tikz external) in a group to disable temporarily the external library while you are writing your diagram. You may like the |list and make| option of tikz external that produces a Makefile that one can compile separately to build in parallel all pictures.
3721 \item If you compile once a diagram without any error, and recompile it after inserting an error, you will see an error while compiling. But if you recompile again, the error will disappear and the diagram that lastly succeeded to build will be inserted instead of the newly buggy diagram. This has been reported \mylink{https://github.com/pgf-tikz/pgf/issues/1137}{here}.
3722 \item Sometimes, |external| cuts some parts of the picture, namely when parts are drawn outside of the bounding box. I've not experienced that with zx diagrams directly (we don't go beyond the bounding box), but the example at the end of this document with the |double copy shadow| has such issues because the shadow is drawn outside of the bounding box. One should therefore disable the externalization (or increase the bounding box) in these cases.
3723 \item It seems that sometimes the inner sep of some labels in |external| mode defaults to zero (see the CNOT example below), I'm not sure why. Adding explicitly the value of the label fixes this.
3724 \end{itemize}
3725
3726 \begin{pgfmanualentry}
3727 \extractcommand\zxExternalAuto\@@
3728 \extractcommand\zxExternalWrap\@@
3729 \extractcommand\zxExternalNoWrap\@@
3730 \extractcommand\zxExternalNoWrapNoExt\@@
3731 \extractcommand\zxExternalWrapForceExt\@@
3732 \pgfmanualbody
3733 Also, the library |external| forces us to wrap our diagrams into a basically empty tikz-pictures to make it work. The current library will automatically wrap the diagrams when the |external| library is enabled, but you can customize how diagrams are wrapped manually: |\zxExternalAuto| (default) will wrap it automatically if |external| is enabled, |\zxExternalWrap| will always wrap it, |\zxExternalNoWrap| will never wrap it (you will get errors if you use |external|), |\zxExternalNoWrapNoExt| will not wrap the figure, but will disable temporary the externalization for diagrams to avoid errors and |\zxExternalWrapForceExt| will wrap the figures and enable tikz |external| locally only for zx-diagrams (using |\tikzexternalenable|). This last option is particularly useful when using |external| while most other environments are not compatible with externalization (like tikzcd, quantikz, blochsphere, maybe cryptocode\dots{}): the idea is to disable tikzexternalize everywhere, except for zx diagrams:
3734 \begin{verse}
3735 |\tikzexternalize|\\
3736 |\tikzexternaldisable|\\
3737 |\zxExternalWrapForceExt|
3738 \end{verse}
3739 \end{pgfmanualentry}
3740
3741 \begin{pgfmanualentry}
3742 \extractcommand\zxExternalSuffix\marg{suffix}\@@
3743 \pgfmanualbody
3744 By default, the library adds a suffix |zx| to figures corresponding to zx diagrams (it avoids to recompile diagrams when a normal figure is added in between two zx diagrams). You can change (or remove) this suffix using |\zxExternalSuffix{yoursuffix}|, where the suffix can also be empty.
3745 \end{pgfmanualentry}
3746
3747 Note that if you get an error:\\
3748 |Argument of \tikzexternal@laTeX@collect@until@end@tikzpicture has an extra }|\\
3749 it is likely that you have an element that is not handled by tikz external, like a raw |tikzcd| environment, a |blochsphere| environment\dots{} Either disable temporary tikz external around it:\\
3750 |{\tikzexternaldisable your code not compatible with external}|\\
3751 or wrap your element around |\begin{tikzpicture}\node{your figure};\end{tikzpicture}| (people usually don't like nested tikz pictures\dots{} but it often works nicely). In case you also care about the baseline, you may prefer to wrap it using:\\
3752 |\begin{tikzpicture}[baseline=(mynode.base)]\node(mynode){your figure};\end{tikzpicture}|
3753
3754 \subsection{How to visually group multiple nodes}\label{subsec:decoration}
3755
3756
3757 \begin{pgfmanualentry}
3758 \def\extrakeytext{style, }
3759 \extractcommand\zxCont\oarg{style}\oarg{color}\marg{nb rows}\marg{nb columns}\oarg{label style}\oarg{(other)(nodes)(to)(fit)}\marg{Text of label}\@@
3760 \extractcommand\zxGroupGates\oarg{style}\marg{nb rows}\marg{nb columns}\oarg{label style}\oarg{(other)(nodes)(to)(fit)}\marg{Text of label}\@@
3761 \extractcommand\zxContName\oarg{style}\oarg{color}\marg{(nodes)(to)(fit)}\oarg{label style}\marg{Text of label}\@@
3762 \extractcommand\zxNamedBox\oarg{style}\oarg{color}\marg{(nodes)(to)(fit)}\oarg{label style}\marg{Text of label}\@@
3763 \pgfmanualbody
3764 These commands are used to highlight a part of a diagram (|Cont| is for ``Container''), or, for |\zxGroupGates|, to group gates together (it is like |Cont| but with a different style), the main difference is that the |Cont| version can be used inside the matrix and will automatically contain the current node (you must then either by specifying the size of the box or the name of the other nodes to fit), while |\zxNamedBox| must be inserted later (so you might not use it much), like:
3765 \begin{codeexample}[width=0cm]
3766 \begin{ZX}
3767 \zxN{} \rar & [\zxwCol] \zxZ{} \zxCont{2}{1}{\textsc{cnot}} \dar \rar & [\zxwCol] \zxN{} \\
3768 \zxN{} \rar & \zxX{} \rar & [\zxwCol] \zxN{} \\
3769 \end{ZX}
3770 \end{codeexample}
3771 \begin{codeexample}[width=0cm]
3772 \begin{ZX}
3773 \zxN{} \rar & [\zxwCol] \zxZ{} \zxContName{(endCnot)}{\textsc{cnot}} \dar \rar & [\zxwCol] \zxN{} \\
3774 \zxN{} \rar & \zxX[a=endCnot]{} \rar & [\zxwCol] \zxN{} \\
3775 \end{ZX}
3776 \end{codeexample}
3777 \begin{codeexample}[width=0cm]
3778 \begin{ZX}[
3779 % \zxCont offers a more practical interface
3780 execute at end picture={
3781 \zxNamedBox{(cnot1)(cnot2)}{
3782 below:\textsc{cnot}
3783 }
3784 }
3785 ]
3786 \zxN{} \rar &[\zxwCol] \zxZ[alias=cnot1]{} \dar \rar &[\zxwCol] \zxN{}\\
3787 \zxN{} \rar & \zxX[alias=cnot2]{} \rar &[\zxwCol] \zxN{}\\
3788 \end{ZX}
3789 \end{codeexample}
3790
3791 Note that the |fit| library cannot fit a wire. So if you want to include the wires around a node, the simpler thing might be to manually increase the size of the box in the style. We provide for that an helper command |fit margins={top=1mm,bottom=2mm,right=2.2mm,left=2.2mm}| (the |inner sep| and |shift| commands of tikz are not really practical to move a single border), you can also use |horizontal|, |vertical|, or |all|, and |l| is a shorcut to add larger margins instead of |fit margins={all=1mm}|.
3792 \begin{codeexample}[width=0cm]
3793 \begin{ZX}[
3794 execute at end picture={
3795 \zxNamedBox[fit margins={right=2.2mm}]{(measX)(measZ)}{
3796 below:\footnotesize Bell measurement
3797 }
3798 \zxNamedBox[fit margins={bottom=2pt,top=2pt,left=2.0mm}][green!80!black]{(bellA)(bellB)}{
3799 left:\footnotesize Bell pair:
3800 }
3801 }
3802 ]
3803 \zxN[a=bellA]{} \rar \dar[C] & [\zxwCol] \zxX*{x\pi} \rar & \zxZ*{z\pi} \rar & \zxN{} \\[\zxwCol]
3804 \zxN[a=bellB]{} \rar & \zxX[a=measX]{x\pi} \dar[C-] & \\
3805 \zxN{} \rar & \zxZ[a=measZ]{z\pi}
3806 \end{ZX}
3807 \end{codeexample}
3808 This will automatically create a subnode |wrapper| pointing to the wrapping node. You can use it to link to that node (note that you should be sure that the wire points to the top left element of the group):
3809 \begin{codeexample}[width=0cm]
3810 \begin{ZX}[circuit]
3811 \ar[rr] && \zxCtrl{} \ar[dl, end subnode=wrapper] \ar[rr] && \\
3812 \rar & \zxCtrl{} \dar \rar \zxGroupGates{2}{3}{below:\textsc{swap}} & \zxNot{} \dar \rar & \zxCtrl{} \dar \rar & \\
3813 \rar & \zxNot{} \rar & \zxCtrl{} \rar & \zxNot{} \rar &
3814 \end{ZX}
3815 \end{codeexample}
3816 \end{pgfmanualentry}
3817
3818 \section{Advanced styling}
3819
3820 \subsection{Overlaying or creating styles}
3821
3822 It is possible to arbitrarily customize the styling, create or update ZX or tikz styles\dots{} First, any option that can be given to a |tikz-cd| matrix can also be given to a |ZX| environment (we refer to the manual of |tikz-cd| for more details). We also provide overlays to quickly modify the ZX style.
3823
3824 \begin{stylekey}{/zx/default style nodes}
3825 This is where the default style must be loaded. By default, it simply loads the (nested) style packed with this library, |/zx/styles/rounded style|. You can change the style here if you would like to globally change a style.
3826
3827 Note that a style must typically define at least |zxZ4|, |zxX4|, |zxFracZ6|, |zxFracX6|, |\zxH|, |zxHSmall|, |zxNoPhaseSmallZ|, |zxNoPhaseSmallX|, |zxNone{,+,-,I}|, |zxNoneDouble{,+,-,I}| and all the |phase in label*|, |pil*| styles (see code on how to define them). Because the above styles (notably |zxZ*| and |zxFrac*|) are slightly complex to define (this is needed in order to implement |phase in label|, |-| versions\dots{}), it may be quite long to implement them all properly by yourself.
3828
3829 For that reason, it may be easier to load our default style and overlay only some of the styles we use (see example in |/zx/user overlay nodes| right after). You can check our code in |/zx/styles/rounded style| to see what you can redefine (intuitively, the styles like |my style name| should be callable by the end user, |myStyleName| may be redefined by users or used in tikzit, and |my@style@name| are styles that should not be touched by the user). The styles that have most interests are |zxNoPhase| (for Z and X nodes without any phase inside), |zxShort| (for Z and X nodes for fractions typically), |zxLong| (for other Z and X nodes) and |stylePhaseInLabel| (for labels when using |phase in label|). These basic styles are extended to add colors (just add |Z|/|X| after the name) like |zxNoPhaseZ|\dots{} You can change them, but if you just want to change the color, prefer to redefine |colorZxZ|/|colorZxZ| instead (note that this color does not change |stylePhaseInLabelZ/X|, so you are free to redefine these styles as well). All the above styles can however be called from inside a tikzit style, if you want to use tikzit internally (make sure to load this library then in |*.tikzdefs|).
3830
3831 Note however that you should avoid to call these styles from inside |\zx{...}| since |\zx*| and |\zxFrac*| are supposed to choose automatically the good style for you depending on the mode (fractions, labels in phase\dots{}). For more details, we encourage the advanced users too look at the code of the library, and examples for simple changes will be presented now.
3832 \end{stylekey}
3833
3834 \begin{stylekey}{/zx/user overlay nodes}
3835 If a user just wants to overlay some parts of the node styles, add your changes here.
3836 \begin{codeexample}[]
3837 {\tikzset{
3838 /zx/user overlay nodes/.style={
3839 zxH/.append style={dashed,inner sep=2mm}
3840 }}
3841 \zx{\zxNone{} \rar & \zxH{} \rar & \zxNone{}}
3842 }
3843 \end{codeexample}
3844 You can also change it on a per-diagram basis:
3845 \begin{codeexample}[]
3846 \zx[text=yellow,/zx/user overlay nodes/.style={
3847 zxSpiders/.append style={thick,draw=purple}}
3848 ]{\zxX{} \rar & \zxX{\alpha} \rar & \zxFracZ-{\pi}{2}}
3849 \end{codeexample}
3850 The list of keys that can be changed will be given below in |/zx/styles/rounded style/*|.
3851 \end{stylekey}
3852
3853 \begin{stylekey}{/zx/user overlay}
3854 This key will be loaded like it if were inside the options of the picture |\zx[options]{...}|. To avoid always typing |\zx[content vertically centered]{...}|, you can therefore use:
3855 \begin{codeexample}[width=0pt]
3856 \tikzset{
3857 /zx/user overlay/.style={
3858 content vertically centered,
3859 },
3860 }
3861 \begin{ZX}
3862 \zxX{\alpha} & \zxX{\beta} & \zxX{a} & \zxX{b} & \zxX*{a\pi} & \zxX*{b\pi} & \zxX*{b'\pi} & \zxX*{'b\pi} & \zxZ{(a \oplus b )\pi}
3863 \end{ZX}
3864 \end{codeexample}
3865 \end{stylekey}
3866
3867 \begin{stylekey}{/zx/default style wires}
3868 Default style for wires. Note that |/zx/wires definition/| is always loaded by default, and we don't add any other style for wires by default. But additional styles may use this functionality.
3869 \end{stylekey}
3870
3871 \begin{stylekey}{/zx/user overlay wires}
3872 The user can add here additional styles for wires.
3873 \begin{codeexample}[]
3874 \begin{ZX}[/zx/user overlay wires/.style={thick,->,C/.append style={dashed}}]
3875 \zxNone{} \ar[d,C] \rar[] &[\zxWCol] \zxNone{}\\[\zxWRow]
3876 \zxNone{} \rar[] & \zxNone{}
3877 \end{ZX}
3878 \end{codeexample}
3879 \end{stylekey}
3880
3881 \begin{stylekey}{/zx/styles/rounded style}
3882 This is the style loaded by default. It contains internally other (nested) styles that must be defined for any custom style.
3883 \end{stylekey}
3884
3885 We present now all the properties that a new node style must have (and that can overlayed as explained above).
3886 \begin{stylekey}{/zx/styles/rounded style/zxAllNodes}
3887 Style applied to all nodes.
3888 \end{stylekey}
3889
3890 \begin{stylekey}{/zx/styles/rounded style/zxEmptyDiagram}
3891 Style to draw an empty diagram.
3892 \end{stylekey}
3893
3894 \begin{pgfmanualentry}
3895 \makeatletter
3896 \def\extrakeytext{style, }
3897 \extractkey/zx/styles/rounded style/zxNone\@nil%
3898 \extractkey/zx/styles/rounded style/zxNone+\@nil%
3899 \extractkey/zx/styles/rounded style/zxNone-\@nil%
3900 \extractkey/zx/styles/rounded style/zxNoneI\@nil%
3901 \makeatother
3902 \pgfmanualbody
3903 Styles for None wires (no inner sep, useful to connect to wires). The |-|,|I|,|+| have additional horizontal, vertical, both spaces.
3904 \end{pgfmanualentry}
3905
3906 \begin{pgfmanualentry}
3907 \makeatletter
3908 \def\extrakeytext{style, }
3909 \extractkey/zx/styles/rounded style/zxNoneDouble\@nil%
3910 \extractkey/zx/styles/rounded style/zxNoneDouble+\@nil%
3911 \extractkey/zx/styles/rounded style/zxNoneDouble-\@nil%
3912 \extractkey/zx/styles/rounded style/zxNoneDoubleI\@nil%
3913 \makeatother
3914 \pgfmanualbody
3915 Like |zxNone|, but with more space to fake two nodes on a single line (not very used).
3916 \end{pgfmanualentry}
3917
3918 \begin{stylekey}{/zx/styles/rounded style/zxSpiders}
3919 Style that apply to all circle spiders.
3920 \end{stylekey}
3921
3922 \begin{stylekey}{/zx/styles/rounded style/zxNoPhase}
3923 Style that apply to spiders without any angle inside. Used by |\zxX{}| when the argument is empty.
3924 \end{stylekey}
3925
3926 \begin{stylekey}{/zx/styles/rounded style/zxNoPhaseSmall}
3927 Like |zxNoPhase| but for spiders drawn in between wires.
3928 \end{stylekey}
3929
3930 \begin{stylekey}{/zx/styles/rounded style/zxShort}
3931 Spider with text but no inner space. Used notably to obtain nice fractions.
3932 \end{stylekey}
3933
3934 \begin{stylekey}{/zx/styles/rounded style/zxLong}
3935 Spider with potentially large text. Used by |\zxX{\alpha}| when the argument is not empty.
3936 \end{stylekey}
3937
3938 \begin{pgfmanualentry}
3939 \makeatletter
3940 \def\extrakeytext{style, }
3941 \extractkey/zx/styles/rounded style/zxNoPhaseZ\@nil%
3942 \extractkey/zx/styles/rounded style/zxNoPhaseX\@nil%
3943 \extractkey/zx/styles/rounded style/zxNoPhaseSmallZ\@nil%
3944 \extractkey/zx/styles/rounded style/zxNoPhaseSmallX\@nil%
3945 \extractkey/zx/styles/rounded style/zxShortZ\@nil%
3946 \extractkey/zx/styles/rounded style/zxShortX\@nil%
3947 \extractkey/zx/styles/rounded style/zxLongZ\@nil%
3948 \extractkey/zx/styles/rounded style/zxLongX\@nil%
3949 \makeatother
3950 \pgfmanualbody
3951 Like above styles, but with colors of |X| and |Z| spider added. The color can be changed globally by updating the |colorZxX| color. By default we use:
3952 \begin{verse}
3953 |\definecolor{colorZxZ}{RGB}{204,255,204}|\\
3954 |\definecolor{colorZxX}{RGB}{255,136,136}|\\
3955 |\definecolor{colorZxH}{RGB}{255,255,0}|
3956 \end{verse}
3957 as the second recommendation in \href{https://zxcalculus.com/accessibility.html}{\texttt{zxcalculus.com/accessibility.html}}.
3958 \end{pgfmanualentry}
3959
3960 \begin{stylekey}{/zx/styles/rounded style/zxH}
3961 Style for Hadamard spiders, used by |\zxH{}| and uses the color |colorZxH|.
3962 \end{stylekey}
3963
3964 \begin{stylekey}{/zx/styles/rounded style/zxHSmall}
3965 Like |zxH| but for Hadamard on wires, (see |H| style).
3966 \end{stylekey}
3967
3968 \begin{pgfmanualentry}
3969 \extractcommand\zxConvertToFracInContent\marg{sign}\marg{num no parens}\marg{denom no parens}\marg{nom parens}\marg{denom parens}\@@
3970 \extractcommand\zxConvertToFracInLabel\@@
3971 \pgfmanualbody
3972 These functions are not meant to be used, but redefined using something like (we use |\zxMinus| as a shorter minus compared to $-$):
3973 \begin{verse}
3974 |\RenewExpandableDocumentCommand{\zxConvertToFracInLabel}{mmmmm}{%|\\
3975 | \ifthenelse{\equal{#1}{-}}{\zxMinus}{#1}\frac{#2}{#3}%|\\
3976 |}|
3977 \end{verse}
3978 This is used to change how the library does the conversion between |\zxFrac| and the actual written text (either in the node content or in the label depending on the function). The first argument is the sign (string |-| for minus, anything else must be written in place of the minus), the second and third argument are the numerator and denominator of the fraction when used in |\frac{}{}| while the last two arguments are the same except that they include the parens which should be added when using an inline version. For instance, one could get a call |\zxConvertToFracInLabel{-}{a+b}{c+d}{(a+b)}{(c+d)}|. See part on labels to see an example of use.
3979 \end{pgfmanualentry}
3980
3981 \begin{pgfmanualentry}
3982 \def\extrakeytext{style, }
3983 \extractcommand\zxMinusUnchanged\@@
3984 \extractcommand\zxMinus\@@
3985 \extractcommand\zxMinusInShort\@@
3986 \makeatletter% should not be letter for \@@... strange
3987 \extractkey/zx/defaultEnv/zx column sep=length\@nil%
3988 \extractkey/zx/styles/rounded style preload/small minus\@nil%
3989 \extractkey/zx/styles/rounded style preload/big minus\@nil%
3990 \extractkey/zx/styles/rounded style/small minus\@nil%
3991 \extractkey/zx/styles/rounded style/big minus\@nil%
3992 \makeatother
3993 \pgfmanualbody
3994 |\zxMinus| is the minus sign used in fractions, |\zxMinusInShort| is used in |\zxZ-{\alpha}| and |\zxMinusUnchanged| is a minus sign shorter than $-$. You can redefine them, for instance:
3995 \begin{codeexample}[]
3996 Compare {\def\zxMinusInShort{-}
3997 \zx{\zxZ-{\alpha}}
3998 } and
3999 {\def\zxMinusInShort{\zxMinus}
4000 \zx{\zxZ-{\alpha}}
4001 }
4002 \end{codeexample}
4003 You can also choose to always use a big or a small minus, either on a per-node, per-figure, or document-wise.
4004 \begin{codeexample}[]
4005 \begin{ZX}
4006 \zxFracZ-{\pi}{4} & \zxZ-{\alpha} & \zxZ-{\delta_i} & \zxZ[small minus]-{\delta_i}
4007 \end{ZX} Picture-wise %
4008 \begin{ZX}[small minus]
4009 \zxZ-{\alpha} & \zxZ-{\delta_i} & \zxZ-{\delta_i} & \zxFracZ-{\pi}{4}
4010 \end{ZX} Document-wise %
4011 \tikzset{/zx/user overlay/.style={small minus}}%
4012 \begin{ZX}
4013 \zxZ-{\alpha} & \zxZ-{\delta_i} & \zxZ[big minus]-{\delta_i} & \zxFracZ[big minus]-{\pi}{4}
4014 \end{ZX}
4015 \end{codeexample}
4016 \end{pgfmanualentry}
4017
4018 \noindent We also define several spacing commands that can be redefined to your needs:
4019 \begin{pgfmanualentry}
4020 \extractcommand\zxHCol{}\@@
4021 \extractcommand\zxHRow{}\@@
4022 \extractcommand\zxHColFlat{}\@@
4023 \extractcommand\zxHRowFlat{}\@@
4024 \extractcommand\zxSCol{}\@@
4025 \extractcommand\zxSRow{}\@@
4026 \extractcommand\zxSColFlat{}\@@
4027 \extractcommand\zxSRowFlat{}\@@
4028 \extractcommand\zxHSCol{}\@@
4029 \extractcommand\zxHSRow{}\@@
4030 \extractcommand\zxHSColFlat{}\@@
4031 \extractcommand\zxHSRowFlat{}\@@
4032 \extractcommand\zxWCol{}\@@
4033 \extractcommand\zxWRow{}\@@
4034 \extractcommand\zxwCol{}\@@
4035 \extractcommand\zxwRow{}\@@
4036 \extractcommand\zxDotsCol{}\@@
4037 \extractcommand\zxDotsRow{}\@@
4038 \extractcommand\zxZeroCol{}\@@
4039 \extractcommand\zxZeroRow{}\@@
4040 \extractcommand\zxNCol{}\@@
4041 \extractcommand\zxNRow{}\@@
4042 \pgfmanualbody
4043 These are spaces, to use like |&[\zxHCol]| or |\\[\zxHRow]| in order to increase the default spacing of rows and columns depending on the style of the wire. |H| stands for Hadamard, |S| for Spiders, |W| for Wires only, |w| is you link a |zxNone| to a spider (goal is to increase the space), |N| is when you have a |\zxN| and want to reduce the space between columns, |HS| for both Spiders and Hadamard, |Dots| for the 3 dots styles, |Zero| completely resets the default column sep. And of course |Col| for columns, |Row| for rows.
4044 \begin{codeexample}[width=3cm]
4045 \begin{ZX}
4046 \zxN{} \ar[rd,-N.] &[\zxwCol] &[\zxwCol] \zxN{} \\[\zxNRow]%%
4047 & \zxX{\alpha}
4048 \ar[ru,N'-]
4049 \ar[rd,N.-] & \\[\zxNRow]
4050 \zxN{} \ar[ru,-N'] & & \zxN{}
4051 \end{ZX}
4052 \end{codeexample}
4053 Note that you can add multiple of them by separating with commas (see |\pgfmatrixnextcell|'s documentation for more details). For instance to have a column separation of exactly |2mm|, do |&[\zxZeroCol,2mm]| (if you just do |&[2mm]| the column will be |2mm| larger). This can also be useful to avoid having a huge space when nodes have multiple empty outputs:
4054 \begin{codeexample}[]
4055 Compare:
4056 \begin{ZX}
4057 \zxN{} & & \zxN{} \\
4058 \zxN{} \rar & \zxZ{} \ar[ur,<'] \ar[dr,<'] & \\
4059 & & \zxN{} \\
4060 \end{ZX}
4061 with
4062 \begin{ZX}
4063 \zxN{} & & \zxN{} \\[\zxZeroRow]
4064 \zxN{} \rar & \zxZ{} \ar[ur,<'] \ar[dr,<'] & \\[\zxZeroRow]
4065 & & \zxN{}
4066 \end{ZX}
4067 \end{codeexample}
4068 \end{pgfmanualentry}
4069
4070 \begin{pgfmanualentry}
4071 \def\extrakeytext{style, }
4072 \extractcommand\zxDefaultColumnSep{}\@@
4073 \extractcommand\zxDefaultRowSep{}\@@
4074 \makeatletter% should not be letter for \@@... strange
4075 \extractkey/zx/defaultEnv/zx column sep=length\@nil%
4076 \extractkey/zx/defaultEnv/zx row sep=length\@nil%
4077 \makeatother
4078 \pgfmanualbody
4079 |\zxDefaultColumn/RowSep| are the column and row space, and the corresponding styles are to change a single matrix. Prefer to change these parameters compared to changing the |row sep| and |column sep| (without |zx|) of the matrix directly since other spacing styles like |\zxZeroCol| or |\zxNCol| depend on |\zxDefaultColumn|.
4080 \end{pgfmanualentry}
4081
4082
4083 \begin{pgfmanualentry}
4084 \extractcommand\zxDefaultSoftAngleS{}\@@
4085 \extractcommand\zxDefaultSoftAngleO{}\@@
4086 \extractcommand\zxDefaultSoftAngleChevron{}\@@
4087 \pgfmanualbody
4088 Default opening angles of |S|, |o| and |v|/|<| wires. Defaults to respectively $30$, $40$ and $45$.
4089 \end{pgfmanualentry}
4090
4091 \subsection{Wire customization}\label{subsec:wirecustom}
4092
4093 \begin{pgfmanualentry}
4094 \makeatletter
4095 \def\extrakeytext{style, }
4096 \extractkey/zx/args/-andL/\@nil%
4097 \extractkey/zx/args/-andL/defaultO (default {-=.2,L=.4})\@nil%
4098 \extractkey/zx/args/-andL/defaultN (default {-=.2,L=.8})\@nil%
4099 \extractkey/zx/args/-andL/defaultN- (default {1-=.4,1L=0})\@nil%
4100 \extractkey/zx/args/-andL/defaultNN (default {})\@nil%
4101 \extractkey/zx/args/-andL/defaultNIN (default {1-=0,1L=.6})\@nil%
4102 \extractkey/zx/args/-andL/defaultS (default {-=.8,L=0})\@nil%
4103 \extractkey/zx/args/-andL/defaultS' (default {-=.8,L=.2})\@nil%
4104 \extractkey/zx/args/-andL/default-S (default {1-=.8,1L=0})\@nil%
4105 \extractkey/zx/args/-andL/defaultSIS (default {1-=0,1L=.8})\@nil%
4106 \makeatother
4107 \pgfmanualbody
4108 Default values used by wires (). You can customize them globally using something like:
4109 \begin{verse}
4110 |\tikzset{|\\
4111 | /zx/args/-andL/.cd,|\\
4112 | defaultO/.style={-=.2,L=.4}|\\
4113 |}|
4114 \end{verse}
4115 Basically |defaultO| will configure all the |o| familly, |defaultS'| will configure all the ``soft'' versions of |s|, |default-S| will configure the anchor on the side of the vertical arrival\dots{} For more details or which wire uses which configuration, check the default value given in each style definition.
4116 \end{pgfmanualentry}
4117
4118 \subsection{Wires starting inside or on the boundary of the node}\label{subsec:wiresInsideOutside}
4119
4120 This library provides multiple methods to draw the wires between the nodes (for all curves depending on |bezier|, which is basically everything but |C| and straight lines).
4121
4122 \paragraph{Default drawing method.}
4123 By default the lines will be drawn behind the node and the starting and ending points will be defined to be a |fake center *| anchor (if it exists, the exact chosen anchors (north, south\dots{}) depending on the direction). Because this anchor lies behind the node, we put them on the |edgelayer| layer. For debugging purpose, it can be practical to display them:
4124 \begin{pgfmanualentry}
4125 \def\extrakeytext{style, }
4126 \extractcommand\zxEdgesAbove\@@
4127 \makeatletter% should not be letter for \@@... strange
4128 \extractkey/zx/wires definition/edge above\@nil%
4129 \extractkey/zx/wires definition/edge not above\@nil%
4130 \makeatother
4131 \pgfmanualbody
4132 If the macro |\zxEdgesAbove| is undefined (using |\let\zxEdgesAbove\undefined|) edges will be drawn above the nodes. To change it on a per-edge basis, use |edge above| (or its contrary |edge not above|) \emph{before the name of the wire}. This is mostly useful to understand how lines are drawn and for debugging purpose.
4133 \begin{codeexample}[]
4134 What you see:
4135 \zx{\zxZ{\alpha+\beta} \ar[dr,s] \\
4136 & \zxZ{\alpha+\beta}}
4137 What is drawn:
4138 \zx{\zxZ{\alpha+\beta} \ar[dr,edge above,s] \\
4139 & \zxZ{\alpha+\beta}}
4140 \end{codeexample}
4141 (you can note the fact that the wire does not start at the center but at a |fake center *| anchor to provide a nicer look)
4142 \end{pgfmanualentry}
4143
4144 \begin{pgfmanualentry}
4145 \def\extrakeytext{style, }
4146 \extractcommand\zxControlPointsVisible\@@
4147 \makeatletter% should not be letter for \@@... strange
4148 \extractkey/zx/wires definition/control points visible\@nil%
4149 \extractkey/zx/wires definition/control points not visible\@nil%
4150 \makeatother
4151 \pgfmanualbody
4152 Similarly, it can be useful for debugging to see the control points of the curves (note that |C|, straight lines and |(| wires are not based on our curve system, so it won't do anything for them). If the macro |\zxControlPointsVisible| is defined (using |\def\zxEdgesAbove{}|) control points will be drawn. To change it on a per-edge basis, use |control points visible| (or its contrary |control points not visible|). This is mostly useful to understand how lines are drawn and for debugging purpose.
4153 \begin{codeexample}[]
4154 Controls not visible:
4155 \zx{\zxZ{\alpha+\beta} \ar[dr,s] \\
4156 & \zxZ{\alpha+\beta}}
4157 Control visible:
4158 \zx{\zxZ{\alpha+\beta} \ar[dr,control points visible,s] \\
4159 & \zxZ{\alpha+\beta}}
4160 Control visible + edge above:
4161 \zx{\zxZ{\alpha+\beta} \ar[dr,edge above,control points visible,s] \\
4162 & \zxZ{\alpha+\beta}}
4163 \end{codeexample}
4164 \textbf{WARNING}: this command adds some points in the wire path, and in particular if you have a |H| wire (Hadamard in the middle of the wire), this option will not place it correctly. But it's not really a problem since it's just to do a quick debugging.
4165 \end{pgfmanualentry}
4166
4167 Unfortunately, the default drawing method also has drawbacks. For instance, when using the |H| edge between a spider and an empty node, the ``middle'' of the edge will appear too close to the center by default (we draw the first edge above to illustrate the reason of this visual artifact):
4168 \begin{codeexample}[width=0cm]
4169 \begin{ZX}
4170 \zxN{} \ar[rd,edge above,-N.,H] &[\zxwCol,\zxHCol] &[\zxwCol,\zxHCol] \zxN{} \\[\zxNRow]%%
4171 & \zxX{\alpha}
4172 \ar[ru,N'-,H]
4173 \ar[rd,N.-,H] & \\[\zxNRow]
4174 \zxN{} \ar[ru,-N',H] & & \zxN{}
4175 \end{ZX}
4176 \end{codeexample}
4177 To solve that issue, you need to manually position the |H| node as shown before:
4178 \begin{codeexample}[width=0cm]
4179 \begin{ZX}
4180 \zxN{} \ar[rd,edge above,-N.,H={pos=.35}] &[\zxwCol,\zxHCol] &[\zxwCol,\zxHCol] \zxN{} \\[\zxNRow]%%
4181 & \zxX{\alpha}
4182 \ar[ru,N'-,H={pos=1-.35}]
4183 \ar[rd,N.-,H={pos=1-.35}] & \\[\zxNRow]
4184 \zxN{} \ar[ru,-N',H={pos=.35}] & & \zxN{}
4185 \end{ZX}
4186 \end{codeexample}
4187 Or manually position the anchor outside the node (you can use angles, centered on the real center on the shape), but be aware that it can change the shape of the node (see below):
4188 \begin{codeexample}[width=0cm]
4189 \begin{ZX}
4190 \zxN{} \ar[rd,edge above,-N.,H,end anchor=180-45] &[\zxwCol,\zxHCol] &[\zxwCol,\zxHCol] \zxN{} \\[\zxNRow]%%
4191 & \zxX{\alpha}
4192 \ar[ru,N'-,H,start anchor=45]
4193 \ar[rd,N.-,H,start anchor=-45] & \\[\zxNRow]
4194 \zxN{} \ar[ru,-N',H,end anchor=180+45] & & \zxN{}
4195 \end{ZX}
4196 \end{codeexample}
4197
4198 A second drawback is that it is not possible to add arrows on the curved wires (except |C| which uses a different approach), since they will be hidden behind the node:
4199 \begin{codeexample}[]
4200 What you see:
4201 \zx{\zxZ{\alpha+\beta} \ar[dr,s,<->] \\
4202 & \zxZ{\alpha+\beta}}
4203 What is drawn:
4204 \zx{\zxZ{\alpha+\beta} \ar[dr,edge above,s,<->] \\
4205 & \zxZ{\alpha+\beta}}
4206 \end{codeexample}
4207 Here, the only solution (without changing the drawing mode) is to manually position the anchor as before\dots{} but note that on nodes with a large content |45| degrees is actually nearly on the top since the angle is not taken from a fake center but from the real center of the node.
4208 \begin{codeexample}[]
4209 \zx{\zxZ{\alpha+\beta} \ar[dr,s,<->,start anchor=-45,end anchor=180-45] \\
4210 & \zxZ{\alpha+\beta}}
4211 \zx{\zxZ{\alpha+\beta} \ar[dr,s,<->,start anchor=-15,end anchor=180-15] \\
4212 & \zxZ{\alpha+\beta}}
4213 \end{codeexample}
4214 Note that the shape of the wire may be a bit different since the ending and leaving parts was hidden before, and the current styles are not designed to look nicely when starting on the border of a node. For that reason, you may need to tweak the style of the wire yourself using |-|, |L| options.
4215
4216 \paragraph{The ``intersection'' drawing methods}
4217
4218 We also define other modes to draw wires (they are very new and not yet tested a lot). In the first mode, appropriate |fake center *| is taken, then depending on the bezier control points, a point is taken on the border of the shape (starting from the fake center and using the direction of the bezier control point). Then the node is drawn. Here is how to enable this mode:
4219
4220 \begin{pgfmanualentry}
4221 \def\extrakeytext{style, }
4222 \extractcommand\zxEnableIntersections\marg{}\@@
4223 \extractcommand\zxDisableIntersections\marg{}\@@
4224 \extractcommand\zxEnableIntersectionsNodes\@@
4225 \extractcommand\zxEnableIntersectionsWires\@@
4226 \makeatletter% should not be letter for \@@... strange
4227 \extractkey/zx/wires definition/use intersections\@nil%
4228 \extractkey/zx/wires definition/dont use intersections\@nil%
4229 \makeatother
4230 \pgfmanualbody
4231 The simpler method to enables or disable intersections is to call |\zxEnableIntersections{}| or |\zxDisableIntersections{}| (potentially in a group to have a local action only). Note that \emph{this does not automatically adapt the styles}, see |ui| to adapt the styles automatically.
4232 \begin{codeexample}[width=0cm]
4233 {% Enable intersections (but does not load our custom "intersections" style, see ui).
4234 \zxEnableIntersections{}% Small space left = artifact of the documentation
4235 \begin{ZX}
4236 \zxN{} \ar[rd,edge above,-N.,H] &[\zxwCol,\zxHCol] &[\zxwCol,\zxHCol] \zxN{} \\[\zxNRow]%%
4237 & \zxX{\alpha}
4238 \ar[ru,N'-,H]
4239 \ar[rd,N.-,H] & \\[\zxNRow]
4240 \zxN{} \ar[ru,-N',H] & & \zxN{}
4241 \end{ZX}
4242 }
4243 \end{codeexample}
4244 (The |edge above| is just to show that the wire does not go inside.) However, this method enable intersections for the whole drawing. You can disable it for a single arrow using the |dont use intersections| style. But it is possible instead to enable it for a single wire. To do that, first define |\def\zxEnableIntersectionsNodes{}| (it will automatically add a |name path| on each node. If you don't care about optimizations, you can just define it once at the beginning of your project), and then use |use intersections| on the wires which should use intersections:
4245 \begin{codeexample}[width=0cm]
4246 {% Create the machinary needed to compute intersections, but does not enable it.
4247 \def\zxEnableIntersectionsNodes{}% Small space left = artifact of the documentation
4248 \begin{ZX}
4249 \zxN{} \ar[rd,edge above,-N.,H, %% "use intersections" does not load any style, cf ui.
4250 use intersections] &[\zxwCol,\zxHCol] &[\zxwCol,\zxHCol] \zxN{} \\[\zxNRow]%%
4251 & \zxX{\alpha}
4252 \ar[ru,edge above,N'-,H,use intersections]
4253 \ar[rd,edge above,N.-,H] & \\[\zxNRow]
4254 \zxN{} \ar[ru,edge above,-N',H] & & \zxN{}
4255 \end{ZX}
4256 }
4257 \end{codeexample}
4258 \end{pgfmanualentry}
4259
4260 \begin{pgfmanualentry}
4261 \def\extrakeytext{style, }
4262 \makeatletter% should not be letter for \@@... strange
4263 \extractkey/zx/wires definition/ui\@nil%
4264 \makeatother
4265 \pgfmanualbody
4266 This method has however a few drawbacks. One of the first reason that explains why we don't use it by default is that it is quite long to compute (it involves the |intersections| library to obtain the bezier point to start at and my code may also be not very well optimized as I'm a beginner with \LaTeX{} and \tikzname{} programming\dots{} and what a strange language!). Secondly, it has not yet been tested a lot. Note also that the default wire styles have not been optimized for this setup and the results may vary compared to the default drawing mode (sometimes they are ``better'', sometimes they are not). We have however tried to define a second style |/zx/args/-andL/ui/| that have nicer results. To load it, just type |ui| \emph{before the wire style name}, it will automatically load |use intersections| together with our custom styles (see below how to use |user overlay wires| to load it by default):
4267 %%% I'm not sure why, but inside codeexample if I write zxEnableIntersections{}%<go to line>\begin{ZX}... then an additional space is added... Not sure why.
4268 \begin{codeexample}[]
4269 {%
4270 \def\zxEnableIntersectionsNodes{}
4271 Before \begin{ZX}
4272 \zxX{\beta} \ar[r,o'] & \zxX{}
4273 \end{ZX} after \begin{ZX}
4274 \zxX{\beta} \ar[r,o',use intersections] & \zxX{}
4275 \end{ZX} corrected manually \begin{ZX}
4276 \zxX{\beta} \ar[r,edge above, use intersections, o'={-=.2,L=.15}] & \zxX{}
4277 \end{ZX} or with our custom style \begin{ZX}
4278 \zxX{\beta} \ar[r,edge above, ui, o'] & \zxX{}
4279 \end{ZX}.
4280 }
4281 \end{codeexample}
4282 Here are further comparisons:
4283 \begin{codeexample}[]
4284 {%
4285 \def\zxEnableIntersectionsNodes{}
4286 Before \begin{ZX}
4287 \zxX{} \ar[r,o'] & \zxX{}
4288 \end{ZX} ui \begin{ZX}
4289 \zxX{} \ar[r, ui, o'] & \zxX{}
4290 \end{ZX}. Before \begin{ZX}
4291 \zxX{\alpha} \ar[r,o'] & \zxZ{\beta}
4292 \end{ZX} ui \begin{ZX}
4293 \zxX{\alpha} \ar[r, ui, o'] & \zxZ{\beta}
4294 \end{ZX}. Before \begin{ZX}
4295 \zxX{\alpha+\beta} \ar[r,o'] & \zxZ{\alpha+\beta}
4296 \end{ZX} ui \begin{ZX}
4297 \zxX{\alpha+\beta} \ar[r, ui, o'] & \zxZ{\alpha+\beta}
4298 \end{ZX}.
4299 }
4300 \end{codeexample}
4301 With |N|:
4302 \begin{codeexample}[]
4303 {%
4304 \def\zxEnableIntersectionsNodes{}
4305 \begin{ZX}
4306 \zxX{} \ar[rd,N]\\ & \zxX{}
4307 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4308 \zxX{} \ar[rd,ui,N]\\ & \zxX{}
4309 \end{ZX}.
4310 \begin{ZX}
4311 \zxX{\alpha} \ar[rd,N]\\ & \zxX{}
4312 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4313 \zxX{\alpha} \ar[rd,ui,N]\\ & \zxX{}
4314 \end{ZX}.
4315 \begin{ZX}
4316 \zxX{\alpha} \ar[rd,N]\\ & \zxX{\beta}
4317 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4318 \zxX{\alpha} \ar[rd,ui,N]\\ & \zxX{\beta}
4319 \end{ZX}.
4320 \begin{ZX}
4321 \zxX{\alpha+\beta} \ar[rd,N]\\ & \zxX{\alpha+\beta}
4322 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4323 \zxX{\alpha+\beta} \ar[rd,ui,N]\\ & \zxX{\alpha+\beta}
4324 \end{ZX}.
4325 }
4326 \end{codeexample}
4327
4328 With |N-|:
4329 \begin{codeexample}[]
4330 {%
4331 \def\zxEnableIntersectionsNodes{}
4332 \begin{ZX}
4333 \zxX{} \ar[rd,N-]\\ & \zxX{}
4334 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4335 \zxX{} \ar[rd,ui,N-]\\ & \zxX{}
4336 \end{ZX}.
4337 \begin{ZX}
4338 \zxX{\alpha} \ar[rd,N-]\\ & \zxX{}
4339 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4340 \zxX{\alpha} \ar[rd,ui,N-]\\ & \zxX{}
4341 \end{ZX}.
4342 \begin{ZX}
4343 \zxX{\alpha} \ar[rd,N-]\\ & \zxX{\beta}
4344 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4345 \zxX{\alpha} \ar[rd,ui,N-]\\ & \zxX{\beta}
4346 \end{ZX}.
4347 \begin{ZX}
4348 \zxX{\alpha+\beta} \ar[rd,N-]\\ & \zxX{\alpha+\beta}
4349 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4350 \zxX{\alpha+\beta} \ar[rd,ui,N-]\\ & \zxX{\alpha+\beta}
4351 \end{ZX}.
4352 }
4353 \end{codeexample}
4354
4355 With |<.|:
4356 \begin{codeexample}[]
4357 {%
4358 \def\zxEnableIntersectionsNodes{}
4359 \begin{ZX}
4360 \zxX{} \ar[rd,<.]\\ & \zxX{}
4361 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4362 \zxX{} \ar[rd,ui,<.]\\ & \zxX{}
4363 \end{ZX}.
4364 \begin{ZX}
4365 \zxX{\alpha} \ar[rd,<.]\\ & \zxX{}
4366 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4367 \zxX{\alpha} \ar[rd,ui,<.]\\ & \zxX{}
4368 \end{ZX}.
4369 \begin{ZX}
4370 \zxX{\alpha} \ar[rd,<.]\\ & \zxX{\beta}
4371 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4372 \zxX{\alpha} \ar[rd,ui,<.]\\ & \zxX{\beta}
4373 \end{ZX}.
4374 \begin{ZX}
4375 \zxX{\alpha+\beta} \ar[rd,<.]\\ & \zxX{\alpha+\beta}
4376 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4377 \zxX{\alpha+\beta} \ar[rd,ui,<.]\\ & \zxX{\alpha+\beta}
4378 \end{ZX}.
4379 }
4380 \end{codeexample}
4381
4382 With |NIN|:
4383 \begin{codeexample}[]
4384 {%
4385 \def\zxEnableIntersectionsNodes{}
4386 \begin{ZX}
4387 \zxX{} \ar[rd,NIN]\\ & \zxX{}
4388 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4389 \zxX{} \ar[rd,ui,NIN]\\ & \zxX{}
4390 \end{ZX}.
4391 \begin{ZX}
4392 \zxX{\alpha} \ar[rd,NIN]\\ & \zxX{}
4393 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4394 \zxX{\alpha} \ar[rd,ui,NIN]\\ & \zxX{}
4395 \end{ZX}.
4396 \begin{ZX}
4397 \zxX{\alpha} \ar[rd,NIN]\\ & \zxX{\beta}
4398 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4399 \zxX{\alpha} \ar[rd,ui,NIN]\\ & \zxX{\beta}
4400 \end{ZX}.
4401 \begin{ZX}
4402 \zxX{\alpha+\beta} \ar[rd,NIN]\\ & \zxX{\alpha+\beta}
4403 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4404 \zxX{\alpha+\beta} \ar[rd,ui,NIN]\\ & \zxX{\alpha+\beta}
4405 \end{ZX}.
4406 }
4407 \end{codeexample}
4408
4409 With this mode |s| behaves basically like |S| since the only difference is the anchor:
4410 \begin{codeexample}[]
4411 {%
4412 \def\zxEnableIntersectionsNodes{}
4413 \begin{ZX}
4414 \zxX{} \ar[rd,s]\\ & \zxX{}
4415 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4416 \zxX{} \ar[rd,ui,s]\\ & \zxX{}
4417 \end{ZX}.
4418 \begin{ZX}
4419 \zxX{\alpha} \ar[rd,s]\\ & \zxX{}
4420 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4421 \zxX{\alpha} \ar[rd,ui,s]\\ & \zxX{}
4422 \end{ZX}.
4423 \begin{ZX}
4424 \zxX{\alpha} \ar[rd,s]\\ & \zxX{\beta}
4425 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4426 \zxX{\alpha} \ar[rd,ui,s]\\ & \zxX{\beta}
4427 \end{ZX}.
4428 \begin{ZX}
4429 \zxX{\alpha+\beta} \ar[rd,s]\\ & \zxX{\alpha+\beta}
4430 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4431 \zxX{\alpha+\beta} \ar[rd,ui,s]\\ & \zxX{\alpha+\beta}
4432 \end{ZX}.
4433 }
4434 \end{codeexample}
4435
4436 With |s'|:
4437 \begin{codeexample}[]
4438 {%
4439 \def\zxEnableIntersectionsNodes{}
4440 \begin{ZX}
4441 \zxX{} \ar[rd,s']\\ & \zxX{}
4442 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4443 \zxX{} \ar[rd,ui,s']\\ & \zxX{}
4444 \end{ZX}.
4445 \begin{ZX}
4446 \zxX{\alpha} \ar[rd,s']\\ & \zxX{}
4447 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4448 \zxX{\alpha} \ar[rd,ui,s']\\ & \zxX{}
4449 \end{ZX}.
4450 \begin{ZX}
4451 \zxX{\alpha} \ar[rd,s']\\ & \zxX{\beta}
4452 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4453 \zxX{\alpha} \ar[rd,ui,s']\\ & \zxX{\beta}
4454 \end{ZX}.
4455 \begin{ZX}
4456 \zxX{\alpha+\beta} \ar[rd,s']\\ & \zxX{\alpha+\beta}
4457 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4458 \zxX{\alpha+\beta} \ar[rd,ui,s']\\ & \zxX{\alpha+\beta}
4459 \end{ZX}.
4460 }
4461 \end{codeexample}
4462
4463 With |-s|:
4464 \begin{codeexample}[]
4465 {%
4466 \def\zxEnableIntersectionsNodes{}
4467 \begin{ZX}
4468 \zxX{} \ar[rd,-s]\\ & \zxX{}
4469 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4470 \zxX{} \ar[rd,ui,-s]\\ & \zxX{}
4471 \end{ZX}.
4472 \begin{ZX}
4473 \zxX{\alpha} \ar[rd,-s]\\ & \zxX{}
4474 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4475 \zxX{\alpha} \ar[rd,ui,-s]\\ & \zxX{}
4476 \end{ZX}.
4477 \begin{ZX}
4478 \zxX{\alpha} \ar[rd,-s]\\ & \zxX{\beta}
4479 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4480 \zxX{\alpha} \ar[rd,ui,-s]\\ & \zxX{\beta}
4481 \end{ZX}.
4482 \begin{ZX}
4483 \zxX{\alpha+\beta} \ar[rd,-s]\\ & \zxX{\alpha+\beta}
4484 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4485 \zxX{\alpha+\beta} \ar[rd,ui,-s]\\ & \zxX{\alpha+\beta}
4486 \end{ZX}.
4487 }
4488 \end{codeexample}
4489
4490 With |SIS|:
4491 \begin{codeexample}[]
4492 {%
4493 \def\zxEnableIntersectionsNodes{}
4494 \begin{ZX}
4495 \zxX{} \ar[rd,SIS]\\ & \zxX{}
4496 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4497 \zxX{} \ar[rd,ui,SIS]\\ & \zxX{}
4498 \end{ZX}.
4499 \begin{ZX}
4500 \zxX{\alpha} \ar[rd,SIS]\\ & \zxX{}
4501 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4502 \zxX{\alpha} \ar[rd,ui,SIS]\\ & \zxX{}
4503 \end{ZX}.
4504 \begin{ZX}
4505 \zxX{\alpha} \ar[rd,SIS]\\ & \zxX{\beta}
4506 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4507 \zxX{\alpha} \ar[rd,ui,SIS]\\ & \zxX{\beta}
4508 \end{ZX}.
4509 \begin{ZX}
4510 \zxX{\alpha+\beta} \ar[rd,SIS]\\ & \zxX{\alpha+\beta}
4511 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4512 \zxX{\alpha+\beta} \ar[rd,ui,SIS]\\ & \zxX{\alpha+\beta}
4513 \end{ZX}.
4514 }
4515 \end{codeexample}
4516
4517 With |^.|:
4518 \begin{codeexample}[]
4519 {%
4520 \def\zxEnableIntersectionsNodes{}
4521 \begin{ZX}
4522 \zxX{} \ar[rd,^.]\\ & \zxX{}
4523 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4524 \zxX{} \ar[rd,ui,^.]\\ & \zxX{}
4525 \end{ZX}.
4526 \begin{ZX}
4527 \zxX{\alpha} \ar[rd,^.]\\ & \zxX{}
4528 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4529 \zxX{\alpha} \ar[rd,ui,^.]\\ & \zxX{}
4530 \end{ZX}.
4531 \begin{ZX}
4532 \zxX{\alpha} \ar[rd,^.]\\ & \zxX{\beta}
4533 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4534 \zxX{\alpha} \ar[rd,ui,^.]\\ & \zxX{\beta}
4535 \end{ZX}.
4536 \begin{ZX}
4537 \zxX{\alpha+\beta} \ar[rd,^.]\\ & \zxX{\alpha+\beta}
4538 \end{ZX} $\Rightarrow $ ui \begin{ZX}
4539 \zxX{\alpha+\beta} \ar[rd,ui,^.]\\ & \zxX{\alpha+\beta}
4540 \end{ZX}.
4541 }
4542 \end{codeexample}
4543
4544 Now using our favorite drawing. Here we illustrate how we apply our custom style to all arrows.
4545 \begin{codeexample}[width=0pt]
4546 \def\zxEnableIntersectionsNodes{}%
4547 \tikzset{
4548 /zx/user overlay wires/.style={
4549 ui, % Other method
4550 }
4551 }
4552 \begin{ZX}[
4553 execute at begin picture={%
4554 %%% Definition of long items (the goal is to have a small and readable matrix
4555 % (warning: macro can't have numbers in TeX. Also, make sure not to use existing names)
4556 \def\Zpifour{\zxFracZ[a=Zpi4]-{\pi}{4}}%
4557 \def\mypitwo{\zxFracX[a=mypi2]{\pi}{2}}%
4558 }
4559 ]
4560 %%% Matrix: in emacs "M-x align" is practical to automatically format it. a is for 'alias'
4561 & \zxN[a=n]{} & \zxZ[a=xmiddle]{} & & \zxN[a=out1]{} \\
4562 \zxN[a=in1]{} & \Zpifour{} & \zxX[a=Xdown]{} & \mypitwo{} & \\
4563 & & & & \zxN[a=out2]{} \\
4564 \zxN[a=in2]{} & \zxX[a=X1]{} & \zxZ[a=toprightpi]{\pi} & & \zxN[a=out3]{}
4565 %%% Arrows
4566 % Column 1
4567 \ar[from=in1,to=X1,s]
4568 \ar[from=in2,to=Zpi4,.>]
4569 % Column 2
4570 \ar[from=X1,to=xmiddle,N']
4571 \ar[from=X1,to=toprightpi,H]
4572 \ar[from=Zpi4,to=n,C] \ar[from=n,to=xmiddle,wc]
4573 \ar[from=Zpi4,to=Xdown]
4574 % Column 3
4575 \ar[from=xmiddle,to=Xdown,C-]
4576 \ar[from=xmiddle,to=mypi2,'>]
4577 % Column 4
4578 \ar[from=toprightpi,to=mypi2,-N]
4579 \ar[from=mypi2,to=out1,<']
4580 \ar[from=mypi2,to=out2,<.]
4581 \ar[edge above,use intersections,from=Xdown,to=out3,<.]
4582 \end{ZX}
4583 \end{codeexample}
4584
4585 The same using |control points visible| to check if the good styles are applied:
4586 \begin{codeexample}[width=0pt]
4587 \def\zxEnableIntersectionsNodes{}%
4588 \tikzset{
4589 /zx/user overlay wires/.style={
4590 ui, % Enable our style on all
4591 edge above, % For debugging
4592 control points visible % For debugging
4593 }
4594 }
4595 \def\zxDebugMode{}
4596 \def\zxControlPointsVisible{}
4597 \begin{ZX}[
4598 execute at begin picture={%
4599 %%% Definition of long items (the goal is to have a small and readable matrix
4600 % (warning: macro can't have numbers in TeX. Also, make sure not to use existing names)
4601 \def\Zpifour{\zxFracZ[a=Zpi4]-{\pi}{4}}%
4602 \def\mypitwo{\zxFracX[a=mypi2]{\pi}{2}}%
4603 }
4604 ]
4605 %%% Matrix: in emacs "M-x align" is practical to automatically format it. a is for 'alias'
4606 & \zxN[a=n]{} & \zxZ[a=xmiddle]{} & & \zxN[a=out1]{} \\
4607 \zxN[a=in1]{} & \Zpifour{} & \zxX[a=Xdown]{} & \mypitwo{} & \\
4608 & & & & \zxN[a=out2]{} \\
4609 \zxN[a=in2]{} & \zxX[a=X1]{} & \zxZ[a=toprightpi]{\pi} & & \zxN[a=out3]{}
4610 %%% Arrows
4611 % Column 1
4612 \ar[from=in1,to=X1,s]
4613 \ar[from=in2,to=Zpi4,.>]
4614 % Column 2
4615 \ar[from=X1,to=xmiddle,N']
4616 \ar[from=X1,to=toprightpi,H]
4617 \ar[from=Zpi4,to=n,C] \ar[from=n,to=xmiddle,wc]
4618 \ar[from=Zpi4,to=Xdown]
4619 % Column 3
4620 \ar[from=xmiddle,to=Xdown,C-]
4621 \ar[from=xmiddle,to=mypi2,'>]
4622 % Column 4
4623 \ar[from=toprightpi,to=mypi2,-N]
4624 \ar[from=mypi2,to=out1,<']
4625 \ar[from=mypi2,to=out2,<.]
4626 \ar[edge above,use intersections,from=Xdown,to=out3,<.]
4627 \end{ZX}
4628 \end{codeexample}
4629
4630 Now, we can also globally enable the |ui| style and the intersection only for some kinds of arrows. For instance (here we enable it for all styles based on |N|, i.e.\ |*N*| and |>|-like wires). See that the |s| node is note using the intersections mode:
4631 \begin{codeexample}[width=0pt]
4632 \def\zxEnableIntersectionsNodes{}%
4633 \tikzset{
4634 /zx/user overlay wires/.style={
4635 %% Nbase changes both N-like and >-like styles.
4636 %% Use N/.append to change only N-like.
4637 Nbase/.append style={%
4638 ui, % intersection only for arrows based on N (N and <)
4639 },
4640 edge above, % For debugging
4641 control points visible % For debugging
4642 }
4643 }
4644 \def\zxDebugMode{}
4645 \def\zxControlPointsVisible{}
4646 \begin{ZX}[
4647 execute at begin picture={%
4648 %%% Definition of long items (the goal is to have a small and readable matrix
4649 % (warning: macro can't have numbers in TeX. Also, make sure not to use existing names)
4650 \def\Zpifour{\zxFracZ[a=Zpi4]-{\pi}{4}}%
4651 \def\mypitwo{\zxFracX[a=mypi2]{\pi}{2}}%
4652 }
4653 ]
4654 %%% Matrix: in emacs "M-x align" is practical to automatically format it. a is for 'alias'
4655 & \zxN[a=n]{} & \zxZ[a=xmiddle]{} & & \zxN[a=out1]{} \\
4656 \zxN[a=in1]{} & \Zpifour{} & \zxX[a=Xdown]{} & \mypitwo{} & \\
4657 & & & & \zxN[a=out2]{} \\
4658 \zxN[a=in2]{} & \zxX[a=X1]{} & \zxZ[a=toprightpi]{\pi} & & \zxN[a=out3]{}
4659 %%% Arrows
4660 % Column 1
4661 \ar[from=in1,to=X1,s]
4662 \ar[from=in2,to=Zpi4,.>]
4663 % Column 2
4664 \ar[from=X1,to=xmiddle,N']
4665 \ar[from=X1,to=toprightpi,H]
4666 \ar[from=Zpi4,to=n,C] \ar[from=n,to=xmiddle,wc]
4667 \ar[from=Zpi4,to=Xdown]
4668 % Column 3
4669 \ar[from=xmiddle,to=Xdown,C-]
4670 \ar[from=xmiddle,to=mypi2,'>]
4671 % Column 4
4672 \ar[from=toprightpi,to=mypi2,-N]
4673 \ar[from=mypi2,to=out1,<']
4674 \ar[from=mypi2,to=out2,<.]
4675 \ar[edge above,use intersections,from=Xdown,to=out3,<.]
4676 \end{ZX}
4677 \end{codeexample}
4678
4679 \end{pgfmanualentry}
4680
4681 \begin{pgfmanualentry}
4682 \def\extrakeytext{style, }
4683 \extractcommand\zxIntersectionLineBetweenStartEnd\@@
4684 \makeatletter% should not be letter for \@@... strange
4685 \extractkey/zx/wires definition/intersections mode between start end\@nil%
4686 \extractkey/zx/wires definition/intersections mode bezier controls\@nil%
4687 \makeatother
4688 \pgfmanualbody
4689
4690 Node that we also defined another intersection mechanism, in which the intersection with the node boundary is computed using the line that links the two fake centers of the starting and ending point. To use it, either define |\def\zxIntersectionLineBetweenStartEnd{}| or use the style |intersections between start end| (or to come back to the normal intersection mode |intersections bezier controls|). Note that this just changes the mode of computing intersections, but does not enable intersections, you still need to enable intersections as explained above (for instance using |use intersection|, or |ui| if you also want to load our style). Note however that we don't spent too much effort in this mode as the result is often not really appealing, in particular the |o| shapes, and therefore we designed no special style for it and made only a few tests.
4691 \begin{codeexample}[]
4692 {%
4693 \def\zxEnableIntersectionsNodes{}
4694 \begin{ZX}
4695 \zxX{\alpha} \ar[rd,N]\\ & \zxX{\beta}
4696 \end{ZX} $\Rightarrow $ between start end \begin{ZX}
4697 \zxX{\alpha} \ar[edge above,rd,ui,intersections mode between start end,N]\\ & \zxX{\beta}
4698 \end{ZX}.
4699 \begin{ZX}
4700 \zxX{\alpha} \ar[r,o'] \ar[r,o.] & \zxX{\beta}
4701 \end{ZX} $\Rightarrow $ between start end \begin{ZX}
4702 \zxX{\alpha} \ar[r,ui,intersections mode between start end,o']
4703 \ar[r,ui,intersections mode between start end,o.] & \zxX{\beta}
4704 \end{ZX}.
4705 }
4706 \end{codeexample}
4707 \end{pgfmanualentry}
4708
4709 \subsection{Nested diagrams}
4710
4711
4712 If you consider this example:
4713
4714 \begin{codeexample}[]
4715 Alone \begin{ZX}
4716 \zxX{} \rar \ar[r,o'] \ar[r,o.] & \zxZ{}
4717 \end{ZX} %
4718 in a diagram %
4719 \begin{ZX}[math baseline=myb]
4720 &[\zxwCol] \zxFracX-{\pi}{2} & \zxX{}
4721 \rar
4722 \ar[r,o']
4723 \ar[r,o.] & \zxZ{} &[\zxwCol]\\
4724 \zxN[a=myb]{} \rar & \zxFracZ{\pi}{2} \rar & \zxFracX{\pi}{2} \rar & \zxFracZ{\pi}{2} \rar & \zxN{}
4725 \end{ZX}
4726 \end{codeexample}
4727
4728 You can see that the constant looks much wider in the second picture, due to the fact that the nodes below increase the column size. One solution I found for this problem is to use |savebox| to create your drawing \emph{before} the diagram, and then use the |fit| library to include it where you want in the matrix (see below for a command that does that automatically):
4729
4730 \begin{codeexample}[width=0pt]
4731 %% Create a new box
4732 \newsavebox{\myZXbox}
4733 %% Save our small diagram.
4734 %% Warning: on older versions, you needed to use \& instead of &
4735 %% (the char '&' cause troubles in functions), but I fixed it 2022/02/09
4736 \sbox{\myZXbox}{%
4737 % add \tikzset{external/optimize=false} if you use tikz "external" library %
4738 \zx{ % you may need
4739 \zxX{} \rar \ar[r,o'] \ar[r,o.] & \zxZ{}
4740 }%
4741 }
4742
4743 $x = \begin{ZX}[
4744 execute at end picture={
4745 %% Add our initial drawing at the end:
4746 \node[fit=(start)(end),yshift=-axis_height] {\usebox{\myZXbox}};
4747 },
4748 math baseline=myb]
4749 &[\zxwCol] \zxFracX-{\pi}{2} & \zxN[a=start]{} & \zxN[a=end]{}\\
4750 \zxN[a=myb]{} \rar & \zxFracZ{\pi}{2} \rar & \zxFracX{\pi}{2} \rar & \zxFracZ{\pi}{2} \rar & \zxN{}
4751 \end{ZX}$
4752 \end{codeexample}
4753
4754 Note that this has the advantage of preserving the baseline of the big drawing. However, it is a bit cumbersome to use, so we provide here a wrapper that automatically does the following code (this has not been tested extensively, and may be subject to changes):
4755
4756 \begin{pgfmanualentry}
4757 \def\extrakeytext{style, }
4758 \extractcommand\zxSaveDiagram\marg{name with backslash}\opt{\oarg{zx options}}\marg{diagram with ampersand \textbackslash\&}\@@
4759 \makeatletter% should not be letter for \@@... strange
4760 \extractkey/zx/wires definition/use diagram=\marg{name with \textbackslash}\marg{(nodes)(to)(fit)}\@nil%
4761 \makeatother
4762 \pgfmanualbody
4763 Use |\zxSaveDiagram| to save and name a diagram (must be done before the diagram you want to insert this diagram into, also \textbf{do not forget the backslash before the name} (note that before 2022/09/02, you needed to use |\&| instead of |&|, but this has been fixed after 2022/02/09. In case you really care about backward compatibility, either download the sty file in your folder, or add |ampersand replacement=\&| in the options of the |\zxSaveDiagram| function and use |\&| as before). Then, |use diagram| to insert it inside a diagram (the second argument is a list of nodes given to the |fit| library):
4764 \begin{codeexample}[width=0pt]
4765 %% v---- note the backslash
4766 \zxSaveDiagram{\myZXconstant}{\zxX{} \rar \ar[r,o'] \ar[r,o.] & \zxZ{}}
4767 $x = \begin{ZX}[use diagram={\myZXconstant}{(start)(end)}, math baseline=myb]
4768 &[\zxwCol] \zxFracX-{\pi}{2} & \zxN[a=start]{} & \zxN[a=end]{}\\
4769 \zxN[a=myb]{} \rar & \zxFracZ{\pi}{2} \rar & \zxFracX{\pi}{2} \rar & \zxFracZ{\pi}{2} \rar & \zxN{}
4770 \end{ZX}$
4771 \end{codeexample}
4772 Note that if you need more space to insert the drawing, you can use |\zxN+[a=start,minimum width=2cm]{}| instead of |\zxN|.
4773 \end{pgfmanualentry}
4774
4775 Sometimes, you may also find useful to stack diagram (for instance because the matrix of the first diagram is not related to the matrix of the second diagram), or position multiple diagrams relative to each others. You can use arrays to do that, but it will not preserve the baseline. Another solution is to put the |ZX| environment inside nodes contained in |tikzpicture| (I know that tikz does not like nesting\dots{} but it works nice for what I tried. I will also try to provide an helper function to do that later). For example here we use it to add the constants below the second diagram:
4776 \begin{codeexample}[vbox]
4777 \begin{equation*}
4778 \begin{ZX}
4779 &[\zxwCol] & &[\zxwCol] \zxN{} \ar[dd,3 vdots] \\[\zxNCol]
4780 \zxN{} \rar & \zxZ{\pi} \rar & \zxX{\alpha} \ar[ru,N'-] \ar[rd,N.-] & \\[\zxNCol]
4781 & & & \zxN{}
4782 \end{ZX} =
4783 \begin{tikzpicture}[
4784 baseline=(A.base)
4785 ]
4786 \node[inner sep=0pt](A){%
4787 \begin{ZX}
4788 &[\zxwCol] & \zxZ{\pi} \ar[dd,3 vdots] \rar &[\zxwCol] \zxN{} \\[\zxNCol]
4789 \zxN{} \rar & \zxX-{\alpha} \ar[ru,N'-] \ar[rd,N.-] & \\[\zxNCol]
4790 & & \zxZ{\pi} \rar & \zxN{}
4791 \end{ZX}
4792 };
4793 \node[inner sep=0pt,below=\zxDefaultColumnSep of A](B){
4794 \begin{ZX}
4795 \zxOneOverSqrtTwo{} & \zxX{\alpha} \rar & \zxZ{\pi}
4796 \end{ZX}
4797 };
4798 \end{tikzpicture}
4799 \end{equation*}
4800 \end{codeexample}
4801
4802 \subsection{Further customization}
4803
4804 You can further customize your drawings using any functionality from \tikzname{} and |tikz-cd| (but it is of course at your own risk). For instance, we can change the separation between rows and/or columns for a whole picture (but prefer to use |zx row sep| as it also updates pre-configured column spaces):
4805 \begin{codeexample}[width=0pt]
4806 \begin{ZX}[row sep=1mm]
4807 & & & & \zxZ{\pi} \\
4808 \zxN{} \rar & \zxX{} \ar[rd,(.] \ar[urrr,(',H] & & & & \zxN{} \\
4809 & & \zxZ{} \ar[rd,s.] \rar &
4810 \zxFracX{\pi}{2} \ar[uur,('] \ar[rru,<'] \ar[rr] & & \zxN{} \\
4811 \zxN{} \rar & \zxFracZ-{\pi}{4} \ar[ru,('] \ar[rr,o.] & & \zxX{} \ar[rr] & & \zxN{}
4812 \end{ZX}
4813 \end{codeexample}
4814 You can also use the tikz |between origins| option in order to specify the distance between the center of the rows and not between their borders. This is particularly practical when gates have different heights, or if you want to compare two diagram with different content (you might also want to use something like |mbr=1.5| to set the math baseline between two lines):
4815 \begin{codeexample}[width=0pt]
4816 $\begin{ZX}[circuit, row sep={.6cm,between origins}]
4817 \rar & \zxCross{} \rar \dar & \\
4818 \rar & \zxCross{} \rar &
4819 \end{ZX} =
4820 \begin{ZX}[circuit, row sep={.6cm,between origins}]
4821 \rar & \zxN{} \ar[rd,s] & \zxN{} \rar & \\
4822 \rar & \zxN{} \ar[ru,s] & \zxN{} \rar &
4823 \end{ZX}$
4824 \end{codeexample}
4825
4826 Or we can define our own style to create blocks:
4827 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
4828 \begin{codeexample}[width=0pt]
4829 { % \usetikzlibrary{shadows}
4830 \tikzset{
4831 my bloc/.style={
4832 anchor=center,
4833 inner sep=2pt,
4834 inner xsep=.7em,
4835 minimum height=3em,
4836 draw,
4837 thick,
4838 fill=blue!10!white,
4839 double copy shadow={opacity=.5},tape,
4840 }
4841 }
4842 \zx{|[my bloc]| f \rar &[1mm] |[my bloc]| g \rar &[1mm] \zxZ{\alpha} \rar & \zxNone{}}
4843 }
4844 \end{codeexample}
4845 }
4846 We can also use for instance |fit|, |alias|, |execute at end picture| and layers (the user can use |background| for things behind the drawing, |box| for special nodes above the drawings (like multi-column nodes, see below), and |foreground| which is even higher) to do something like that:
4847 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
4848 \begin{codeexample}[width=3cm]
4849 % \usetikzlibrary{fit}
4850 \begin{ZX}[
4851 execute at end picture={
4852 \node[
4853 inner sep=2pt,
4854 node on layer=background, %% Ensure the node is behind the drawing
4855 rounded corners,
4856 draw=blue,
4857 dashed,
4858 fill=blue!50!white,
4859 opacity=.5,
4860 fit=(cnot1)(cnot2), %% position of the node, thanks fit library
4861 "\textsc{cnot}" {above, inner sep=1mm} %% Adds label, thanks quote library. Not sure why, inner sep is set to 0 when using tikz "external" library.
4862 ] {};
4863 }
4864 ]
4865 \zxNone{} \rar & \zxZ[alias=cnot1]{} \dar \rar & \zxNone{}\\
4866 \zxNone{} \rar & \zxX[alias=cnot2]{} \rar & \zxNone{}\\
4867 \end{ZX}
4868 \end{codeexample}
4869 Because this code is quite lengthy and useful, we provide in \cref{subsec:decoration} a shorter syntax.
4870 }
4871
4872 You can also use other libraries, in particular |nfold| can be practical to insert many wires at once (note that you need a recent version since it was introduced in CTAN 2023, or you need to manually install the library):
4873 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
4874 \begin{codeexample}[width=0pt]
4875 % \usetikzlibrary{nfold} % needs very recent CTAN > 2023, or manually copy the .tex files
4876 \begin{ZX}[circuit]
4877 \rar[scaling nfold=5]
4878 & \zxGate{X} \rar[rd, line join=round, double distance=2pt, scaling nfold=3,--|] & \\
4879 & & \zxGate{Y}
4880 \end{ZX}
4881 \end{codeexample}
4882 }
4883
4884
4885 \section{Future works}
4886
4887 There is surely many things to improve in this library, and given how young it is there is surely many undiscovered bugs. So feel free to propose ideas or report bugs \mylink{https://github.com/leo-colisson/zx-calculus/issues}{one the github page}. The intersections code is also quite slow, so I would be curious to check if I can optimize it (the first goal was to make it work). I should also work on the compatibility with tikzit (basically just write tikz configuration files that you can just use and document how to use tikzit with it), or even write a dedicated graphical tool (why not based on tikzit itself, or \mylink{https://tikzcd.yichuanshen.de/}{this tool}). I also want to find a nicer way to merge cells (for now I propose to use the |fit| library but it's not very robust against overlays) and to nest ZX diagrams. And of course fix typos in the documentation and write other styles, including notations not specific to ZX-calculus. Feel free to submit Pull Requests to solve that, or to submit bug reports to explain uncovered use-cases!
4888
4889 \section{Acknowledgement}
4890
4891 I'm very grateful of course to everybody that worked on these amazing field which made diagramatic quantum computing possible, and to the many StackExchange users that helped me to understand a bit more \LaTeX{} and \tikzname{} (sorry, I don't want to risk an incomplete list, but many thanks to egreg, David Carlisle, cfr, percusse, Andrew Stacey, Henri Menke, SebGlav, Qrrbrbirlbel\dots{}). I also thank Robert Booth for making me realize how my old style was ugly, and for giving advices on how to improve it. Thanks to John van de Wetering and Alastair Kay (author of Quantikz), whose styles have also been a source of inspiration~\cite{van20_ZXcalculusWorkingQuantum}, and to Augusto Stoffel for the great tikzcd package on which this library is built.
4892
4893 \section{Changelog}
4894
4895 \begin{itemize}
4896 \item v2.3 (2024/03/15):
4897 \begin{itemize}
4898 \item Fix bug that forbids package to load with error |ERROR: Missing = inserted for \ifnum|: \url{https://tex.stackexchange.com/questions/713222/providespackage-with-semantic-versionning}
4899 \end{itemize}
4900 \item v2.2 (11/03/2024):
4901 \begin{itemize}
4902 \item Added slice with percentage position
4903 \end{itemize}
4904 \item v2.1 (09/03/2024):
4905 \begin{itemize}
4906 \item Fix bug with arguments impossible:
4907 {\catcode`\|=12 % Ensures | is not anymore \verb|...|
4908 \begin{codeexample}[vbox]
4909 \begin{ZX}[
4910 /utils/exec={
4911 \def\foo#1{
4912 \node[fill=#1,inner sep=2pt,rounded corners,alias=foo]{\text{Hey #1!}};
4913 \node[fill=#1!50!black,circle,inner sep=1pt] at (foo.north west) {};
4914 }
4915 }
4916 ]
4917 \foo{green} & \foo{red} & |[foo/.style={color=#1}, foo=red]| Hey
4918 \end{ZX}
4919 \end{codeexample}}
4920 \end{itemize}
4921 \item v2.0 (22/12/2023, now using semantic versioning):
4922 \begin{itemize}
4923 \item Fix bugs with baseline like |mbr=1.5|
4924 \item Fix bugs related to bracemulti (now can use multiple wires)
4925 \item Other bug fixes
4926 \item Add new line connection \texttt{--\textbar}, \texttt{h--\textbar} etc.
4927 \end{itemize}
4928 \item 2023/09/26:
4929 Many updates in the past months:
4930 \begin{itemize}
4931 \item Custom nodes are made easy
4932 \item Multi-gates nodes and many circuit-related functions
4933 \item Added |post arrow style if start node| and other similar commands
4934 \item updated the doc to use the latest version of robust-externalize that is much simpler to use now\dots
4935 \item Some nodes can also now be usable outside tikzcd, but I've not tried (I should provide more options for this).
4936 \item Group gates
4937 \end{itemize}
4938 \item 2022/02/09:
4939 \begin{itemize}
4940 \item Added compatibility with external tikz library
4941 \item More robust handling of |&|: align and macros does not need |\&| anymore.
4942 \item |\&| in |\zxSaveDiagram| is replaced with |&|. This introduces a small backward incompatibility, but hey, I said it was still subject to changes :-)
4943 \end{itemize}
4944 \end{itemize}
4945
4946 \section{TODO}
4947 \begin{itemize}
4948 \item I think that some anchors are not properly configured in stuff like |\zxBox| etc, so a nice sanity check should be done
4949 \item Clean the code, eventually
4950 \item Consider subnodes as anchors (fake center etc)
4951 \item more circuit related stuff
4952 \item multigate invisible element
4953 \item curly braces made easy
4954 \item lstick
4955 \end{itemize}
4956
4957 \printindex
4958
4959 \printbibliography[heading=bibintoc]
4960
4961 \end{document}