summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/yquant/yquantlanguage-qasm.sty
blob: eaa848e03f242f60543232a00b6942c916b6ef05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
% yquantlanguage-qasm.sty
%  Extend yquant to understand qasm syntax.
%
% Copyright 2020 Benjamin Desef
%
% This work may be distributed and/or modified under the conditions of the LaTeX Project
% Public License, either version 1.3c of this license or (at your option) any later
% version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX version 2005/12/01 or
% later.
%
% This work has the LPPL maintenance status `author-maintained'.
%
% The Current Maintainer of this work is Benjamin Desef.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{yquantlanguage-qasm}[2020/06/13 v0.3 yquant-qasm]

\RequirePackage{yquant}[2020/06/13]

% BEGIN_FOLD Environment definitions
\begingroup
   \catcode`\{=12 \global\let\qasm@env@charbgroup={
   \catcode`\#=12 \global\let\qasm@env@charhashtag=#
\endgroup
{\catcode`\^^M=12 \protected\gdef\qasm@defwithnewline#1#{\def#1^^M}}
{\catcode`\|=0 \catcode`\\=12 |global|let|qasm@env@charbackslash=\ |gdef|qasm@env@charend{\endqasm}}
\protected\def\qasm@env@begin{%
   \begingroup%
      \qasm@env@setcatcodes%
      \@ifnextchar[\qasm@env@begin@arg{\qasm@env@begin@arg[]}%
}

\protected\def\qasm@env@setcatcodes{%
   % make most TeX specials ordinary - not \dospecials, we want to keep spaces
   \catcode`\\=12 %
   \catcode`\{=12 %
   \catcode`\}=12 %
   \catcode`\$=12 %
   \catcode`\&=12 %
   \catcode`\#=12 %
   \catcode`\^=12 \catcode`\^^K=12 %
   \catcode`\_=12 \catcode`\^^A=12 %
   \catcode`\%=12 %
   \catcode`\~=12 %
   \catcode`\^^M=12 %
}

\def\qasm@env@begin@arg{%
   \yquant@env@lazyfalse%
   \let\yquant@env@check=\qasm@env@check%
   \let\yquant@register@get@ids=\qasm@register@get@ids%
   \let\yquant@attrs@remaining=\empty% we don't have attributes, but the backend requires this to be set up
   \let\tikz@ensure@dollar@catcode=\relax% this check will fail, but it does not make sense in our case. Every tikz command will be read with the appropriate dollar catcode.
   % provide some commands that might be used during gate construction
   \let\m=\qasm@cmd@m%
   \let\txt=\qasm@cmd@txt%
   \let\meter=\qasm@cmd@meter%
   \let\dmeter=\qasm@cmd@dmeter%
   \let\dmeterwide=\qasm@cmd@dmeterwide%
   \yquant@env@begin@generic\qasm%
}

\def\qasm@env@end{%
      \yquant@env@end%
   \endgroup%
}

% since the backslash is just an `other' character, we cannot leave our environment. Hence, we manually check for \endqasm and \end{qasm}.
\def\qasm@env@checkend#1#2#3#4#5{%
   \ifstrequal{#1#2#3#4 }{\end}{% for us, \end is a macro so that TeX will insert another space
      \ifx q#5%
         \expandafter\qasm@env@checkend@plain%
      \else%
         \ifx\qasm@env@charbgroup#5%
            \expandafter\expandafter\expandafter\qasm@env@checkend@latex%
         \else%
            \PackageError{yquant.sty}%
                         {Invalid qasm syntax: `\meaning#1'}%
                         {Adhere to the specs!}%
         \fi%
      \fi%
   }{%
      \PackageError{yquant.sty}%
                   {Invalid qasm syntax: `\meaning#1'}%
                   {Adhere to the specs!}%
   }%
}

\def\qasm@env@checkend@plain#1#2#3{%
   \ifstrequal{#1#2#3}{asm}{%
         \endgroup% we initially thought this was a normal command, so exit the group
      \endqasm%
      \futurelet\qasm@env@checkend@plain@delimiter\qasm@env@checkend@plain@validate%
   }{%
      \PackageError{yquant.sty}%
                   {Invalid qasm syntax: `\meaning\qasm@env@charbackslash'}%
                   {Adhere to the specs!}%
   }%
}

\def\qasm@env@checkend@plain@validate{%
   % we already ended the qasm environment, based on the assumption that what followed was not a letter
   \ifcat\qasm@env@checkend@plain@delimiter A%
      % we were wrong and should never have left the environment. Now it's too late
      \PackageError{yquant.sty}%
                   {Invalid qasm syntax: `\meaning\qasm@env@charbackslash'}%
                   {Adhere to the specs!}%
   \fi%
}

\begingroup
   \catcode`\[=1 \catcode`\]=2 \catcode`\}=12 % we set [ just for editors that scan for matching parenthesis, though we don't need it
   \gdef\qasm@env@checkend@latex#1#2#3#4#5[%
      \ifstrequal[#1#2#3#4#5][qasm}][%
            \endgroup% we initially thought this was a normal command, so exit the group
         \end[qasm]%
      ][%
         \PackageError[yquant.sty]%
                      [Invalid qasm syntax: `\meaning\qasm@env@charbackslash']%
                      [Adhere to the specs!]%
      ]%
   ]%
\endgroup

\protected\def\qasm@env@restorecatcodes{%
   % restore standard catcodes (hopefully they were standard before entering the environment)
   \catcode`\\=0 % backslash is TeX escape character
   \catcode`\{=1 % left brace is begin-group character
   \catcode`\}=2 % right brace is end-group character
   \catcode`\$=3 % dollar sign is math shift
   \catcode`\&=4 % ampersand is alignment tab
   \catcode`\#=6 % hash mark is macro parameter character
   \catcode`\^=7 \catcode`\^^K=7 % circumflex and uparrow are for superscripts
   \catcode`\_=8 \catcode`\^^A=8 % underline and downarrow are for subscripts
   \catcode`\%=14 % percent sign is comment character
   \catcode`\~=\active % tilde is active
%   \catcode`\^^M=5 % ascii return is end-line - not needed, is done manually
}

\tikzaddtikzonlycommandshortcutlet\qasm\qasm@env@begin
\tikzaddtikzonlycommandshortcutlet\endqasm\qasm@env@end

\protected\def\qasm@env@check{%
   \let\next=\relax%
   \ifx\yquant@env@nextchar\qasm@env@charhashtag% comment
      \let\next=\qasm@env@gobblecomment%
   \else%
      % Here we assume standard catcodes for A
      \ifcat\noexpand\yquant@env@nextchar A% letter
         \let\next=\qasm@langhelper@check@name%
      \else%
         \ifx\yquant@env@nextchar\yquant@env@linebreak% linebreaks are no longer end-of-line characters, but have catcode 12 (other).
            \let\next=\yquant@env@gobblepar%
         \else%
            \ifx\yquant@env@nextchar\par%
               \let\next=\yquant@env@gobblepar%
            \else%
               \ifx\yquant@env@nextchar\qasm@env@charbackslash% backslash might end the environment
                  \let\next=\qasm@env@checkend%
               \else%
                  \PackageError{yquant.sty}%
                     {Invalid qasm syntax: `\meaning\yquant@env@nextchar'}%
                     {Adhere to the specs!}%
               \fi%
            \fi%
         \fi%
      \fi%
   \fi%
   \next%
}

\qasm@defwithnewline\qasm@env@gobblecomment#1{%
   \yquant@env@contscan%
}

\def\qasmimport#1{%
   \ifpgfpicture%
      \ifnum\yquant@env>0 %
         \PackageError{yquant.sty}{You cannot use qasm circuits as subcircuits}{}%
      \else%
         \begingroup%
            \let\yquantimport@now=\qasmimport@now%
            \everyeof{\noexpand}%
            \qasm@env@setcatcodes%
            \yquantimportcommand{#1}%
            \expandafter\expandafter\expandafter\qasm%
               \expandafter\qasmimport@file%
            \qasm@env@charend%
         \endgroup%
      \fi%
   \else%
      \begin{tikzpicture}%
         \begingroup%
            \let\yquantimport@now=\qasmimport@now%
            \everyeof{\noexpand}%
            \qasm@env@setcatcodes%
            \yquantimportcommand{#1}%
            \expandafter\expandafter\expandafter\qasm%
               \expandafter\qasmimport@file%
            \qasm@env@charend%
         \endgroup%
      \end{tikzpicture}
   \fi%
}

\def\qasmimport@now#1{%
   \edef\qasmimport@file{\@@input #1 }%
}
% END_FOLD

% BEGIN_FOLD Langhelper definitions
% Parsing command itself
\protected\qasm@defwithnewline\qasm@langhelper@check@name#1 #2{%
   \lowercase{\edef\cmd{#1}}%
   \unless\ifcsname qasm@lang@\cmd\endcsname%
      \PackageError{yquant.sty}{Unsupported qasm command: `#1'}%
         {You used a command that is unknown to qasm.}%
   \fi%
   \catcode`\^^M=5 % this catcode is required for scantokens, which we almost always need (because we use yquant's extensive register name facilities)
   \csname qasm@lang@\cmd\endcsname#2 \qasm@end%
}

% Defining commands that accept registers and take an optional value
% #1: name of the argument
% #2: command that is to be called
% #3: number of controls
% #4: number of targets
\protected\def\qasm@langhelper@declare@command#1#2#3#4{%
   \begingroup%
      \lowercase{\edef\cmd{#1}}%
      % everything after the next space is just gobbled
      \protected\long\csgdef{qasm@lang@\cmd}##1 ##2\qasm@end{%
         \qasm@langhelper@declare@command@@fetchargs{#3}{#4}{##1}{#2}%
         \yquant@env@rescan%
      }%
   \endgroup%
}

\protected\def\qasm@langhelper@declare@command@@fetchargs#1#2#3#4{%
   \let\targets=\empty%
   \let\controls=\empty%
   \count0=0 %
   \count2=#1 %
   \forcsvlist%
      \qasm@langhelper@declare@command@@fetchargs@%
      {#3}%
   \unless\ifnum\count0=\numexpr\count2+#2\relax %
      \PackageError{yquant.sty}{Invalid command use}%
         {This gate requires #2 target\unless\ifnum#2=1 s\fi\space and #1 control\unless\ifnum#1=1 s\fi.}%
   \fi%
   \edef\cmd{%
      \unexpanded{#4}%
         {\expandafter\@gobble\controls\empty}{}%
         {\expandafter\@gobble\targets\empty}%
   }%
   \cmd%
}

\protected\def\qasm@langhelper@declare@command@@fetchargs@#1{%
   \advance\count0 by 1 %
   \unless\ifnum\count0>\count2 %
      % we are at the beginning
      \appto\controls{,#1}%
   \else%
      \appto\targets{,#1}%
   \fi%
}

% converts names to indices
\protected\def\qasm@register@get@ids#1{%
   \begingroup%
      \let\yquant@register@get@ids@list=\empty%
      \count0=2147483647 % minimal id
      \count2=0 % maximal id
      \count4=0 % number of total registers
      \forcsvlist\qasm@register@get@ids@outerlist{#1}%
      \global\let\yquant@register@get@ids@list=\yquant@register@get@ids@list%
      \xdef\yquant@register@get@ids@min{\the\count0}%
      \xdef\yquant@register@get@ids@max{\the\count2}%
      \xdef\yquant@register@get@ids@count{\the\count4}%
      % this is very simple: if multi-registers are allowed and we have more than one, then we are in a multi-register; else not.
      \ifyquant@register@get@allowmulti%
         \ifnum\count4> 1%
            \count6=\count0 % no way to chain the main register
            \yquant@register@multi@splitparts%
            \xdef\yquant@register@get@ids@list{%
               \noexpand\yquant@register@multi%
                  {\the\count0}{\the\count2}{\the\count4}{\yquant@register@get@ids@list}%
               \yquant@list@delim%
            }%
            \count4=1 %
         \fi%
      \fi%
   \endgroup%
}

\protected\def\qasm@register@get@ids@outerlist#1{%
   \yquant@register@get@id\idx{#1[0]}%
   \listeadd\yquant@register@get@ids@list{\idx}%
   \ifnum\count0>\idx %
      \count0=\idx %
   \fi%
   \ifnum\count2<\idx %
      \count2=\idx %
   \fi%
   \advance\count4 by 1 %
}
% END_FOLD

% BEGIN_FOLD Configuration
\pgfkeys{%
   /yquant/operators/every s/.style={%
      /yquant/operators/every box%
   },%
   /yquant/operators/every t/.style={%
      /yquant/operators/every box%
   },%
   /yquant/operators/every utwo/.style={%
      /yquant/operators/every box%
   },
   /qasm/zero/.store in=%
      \qasm@config@zero,%
   /qasm/register/default qubit name/.store in=%
      \qasm@config@qubit@nameonly,%
   /qasm/register/default qubit name value/.store in=%
      \qasm@config@qubit@namevalue%
}
\def\qasm@ket#1{\mathopen|\mathinner{#1}\rangle}
\def\qasm@config@zero{$\qasm@ket0$}
\let\qasm@config@qubit@nameonly=\qasm@ket
\def\qasm@config@qubit@namevalue#1#2{\qasm@ket{#1} = \qasm@ket{#2}}
% END_FOLD

% BEGIN_FOLD Automagically convert names
% if ([A-z]+)(\d+) matches, replace by $1_{$2}, else let unchanged, fully expandable
\def\qasmname#1{%
   \expandafter\qasmname@entry\expandafter{#1}%
}

\def\qasmname@entry#1{%
   \ifstrempty{#1}{}{%
      \qasmname@checkletter#1\relax\qasmname@end{}%
   }%
}

\def\qasmname@checkletter#1#2\qasmname@end#3{%
   \ifstrempty{#2}{%
      % done, and we didn't get a number, so regex doesn't match (#1 is \relax)
      #3%
   }{%
      \ifnum`#1>64 %
         \ifnum`#1<123 %
            \expandafter\expandafter\expandafter\@firstoftwo%
         \else%
            \expandafter\expandafter\expandafter\@secondoftwo%
         \fi%
      \else%
         \expandafter\@secondoftwo%
      \fi%
      {%
         \qasmname@checkletter#2\qasmname@end{#3#1}%
      }{%
         % not in A-z, move to the number part - but only if there was something matching before!
         \ifstrempty{#3}{%
            \qasmname@droprelax#1#2\qasmname@end%
         }{%
            \qasmname@checknumber#1#2\qasmname@end{#3}{}%
         }%
      }%
   }%
}

\def\qasmname@checknumber#1#2\qasmname@end#3#4{%
   \ifstrempty{#2}{%
      % done - was there a number?
      \ifstrempty{#4}{%
         #3%
      }{%
         #3_{#4}%
      }%
   }{%
      \ifnum`#1>47 %
         \ifnum`#1<58 %
            \expandafter\expandafter\expandafter\@firstoftwo%
         \else%
            \expandafter\expandafter\expandafter\@secondoftwo%
         \fi%
      \else%
         \expandafter\@secondoftwo%
      \fi%
      {%
         \qasmname@checknumber#2\qasmname@end{#3}{#4#1}%
      }{%
         \qasmname@droprelax#3#4#1#2\qasmname@end%
      }%
   }%
}

\def\qasmname@droprelax#1\relax#2\qasmname@end{%
   \ifstrempty{#2}{%
      #1%
   }{%
      \qasmname@droprelax{#1\relax}#2\qasmname@end%
   }%
}
% END_FOLD

% BEGIN_FOLD Language definitions
\protected\def\qasm@lang@qubit#1 #2\qasm@end{%
   \let\yquant@lang@create@type=\yquant@register@type@q%
   \def\yquant@lang@create@style{qubit}%
   \qasm@lang@create#1,,,,\qasm@end%
}

\protected\def\qasm@lang@cbit#1 #2\qasm@end{%
   \let\yquant@lang@create@type=\yquant@register@type@c%
   \def\yquant@lang@create@style{cbit}%
   \qasm@lang@create#1,,,,\qasm@end%
}

\protected\def\qasm@lang@create#1,#2,#3,#4,#5\qasm@end{%
   \ifstrempty{#4}\relax{%
      \PackageError{yquant.sty}{Invalid number of arguments}%
                   {Register creation in qasm expects at most three arguments.}%
   }%
   \ifstrequal{#5}{,,}{%
      % we have a third parameter which defines the TeX name - retokenize it
      \begingroup%
         \qasm@env@restorecatcodes%
         \everyeof{\noexpand}%
         \endlinechar=-1 %
         \edef\process{%
            \def\noexpand\yquant@lang@attr@value{%
               \scantokens{\unexpanded{#3}}%
            }%
         }
         \expandafter%
      \endgroup%
      \process%
   }{%
      % make num subscript if name = alpha+numbers
      \edef\yquant@lang@attr@value{\qasmname{#1}}%
   }%
   \ifx\yquant@lang@create@type\yquant@register@type@c%
      \ifstrempty{#2}{% cbit has initial value?
         \edef\yquant@lang@attr@value{%
            $\unexpanded\expandafter{\yquant@lang@attr@value}$%
         }%
      }{%
         \begingroup%
            \qasm@env@restorecatcodes%
            \everyeof{\noexpand}%
            \endlinechar=-1 %
            \edef\process{%
               \def\noexpand\yquant@lang@attr@value{%
                  $\unexpanded\expandafter{\yquant@lang@attr@value} = %
                   \scantokens{\unexpanded{#2}}$%
               }%
            }
            \expandafter%
         \endgroup%
         \process%
      }%
   \else%
      \ifstrempty{#2}{% qubit has initial value?
         \edef\yquant@lang@attr@value{%
            \unexpanded\expandafter{\expandafter$%
                \expandafter\qasm@config@qubit@nameonly%
                \expandafter{\yquant@lang@attr@value}%
             $}%
         }%
      }{%
         \begingroup%
            \qasm@env@restorecatcodes%
            \everyeof{\noexpand}%
            \endlinechar=-1 %
            \edef\process{%
               \def\noexpand\yquant@lang@attr@value{%
                  $\noexpand\qasm@config@qubit@namevalue%
                   {\unexpanded\expandafter{\yquant@lang@attr@value}}%
                   {\scantokens{\unexpanded{#2}}}$%
               }%
            }
            \expandafter%
         \endgroup%
         \process%
      }%
   \fi%
   \yquant@lang@create@do{#1}[1][;%
   \yquant@env@rescan%
}

\protected\def\qasm@lang@def#1,#2,'#3' #4\qasm@end{%
      \lowercase{\def\cmd{#1}}%
      % we need to do a couple of case distictions (it might seem crazy to look for the existence of a particular macro in #3, but this is what qasm does)
      \ifstrequal{#3}{bullet}{%
         \edef\process{%
            \def\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname{%
               \let\noexpand\yquant@lang@attr@value=\noexpand\empty%
               \noexpand\yquant@lang@@phase%
            }%
         }%
      }{%
         % re-tokenize the code
         \begingroup%
            \qasm@env@restorecatcodes%
            \everyeof{\noexpand}%
            \endlinechar=-1 %
            \edef\process{%
               \def\noexpand\texcode{%
                  \scantokens{\unexpanded{#3}}%
               }%
            }%
            \expandafter%
         \endgroup%
         \process%
         % now check whether the text begins with \meter, \dmeter or \dmeterwide. This is not 100% equivalent to qasm, which only checks whether these macros are present, not whether they are at the beginning. But due to our node approach, anything different from the whole content being this macro does not make sense.
         \expandafter\ifyquant@firsttoken\expandafter\meter\expandafter{\texcode}{%
            \edef\process{%
               \def\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname####1####2{%
                  \noexpand\yquant@lang@@measure%
               }%
            }%
         }{%
            \expandafter\ifyquant@firsttoken\expandafter\dmeter\expandafter{\texcode}{%
               \protected@edef\process{%
                  \def\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname####1####2{%
                     \def\noexpand\yquant@lang@attr@value{\texcode}%
                     \noexpand\yquant@lang@@dmeter%
                  }%
               }%
            }{%
               \expandafter\ifyquant@firsttoken\expandafter\dmeterwide\expandafter{\texcode}{%
                  \protected@edef\process{%
                     \def\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname####1####2{%
                        \def\noexpand\yquant@lang@attr@value{\texcode}%
                        \noexpand\yquant@lang@@dmeter%
                     }%
                  }%
               }{%
                  \edef\process{%
                     \def\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname{%
                        \yquant@draw%
                           {$\unexpanded\expandafter{\texcode}$}%
                           {/yquant/operators/every box}%
                     }%
                  }%
               }%
            }%
         }%
      }%
      \eappto\process{%
         % we don't use \qasm@langhelper@declare@command here, since it works globally
         \protected\long\def\expandafter\noexpand\csname qasm@lang@\cmd\endcsname####1 ####2\noexpand\qasm@end{%
            \qasm@langhelper@declare@command@@fetchargs{#2}1{####1}\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname%
            \noexpand\yquant@env@rescan%
         }%
      }%
      \expandafter%
   \endgroup%
   \process%
   \yquant@env@scan%
}

\protected\def\qasm@lang@defbox#1,#2,#3,'#4' #5\qasm@end{%
      \ifnum#2<#3 %
         \PackageError{yquant.sty}{Invalid box definition}%
                      {You need at least as many total qubits as controls.}%
      \fi%
      \lowercase{\def\cmd{#1}}%
      % no special cases here
      \begingroup%
         \qasm@env@restorecatcodes%
         \everyeof{\noexpand}%
         \endlinechar=-1 %
         \edef\process{%
            \def\noexpand\texcode{%
               \scantokens{\unexpanded{#4}}%
            }%
         }%
         \expandafter%
      \endgroup%
      \process%
      \edef\process{%
         \def\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname{%
            \noexpand\yquant@register@get@allowmultitrue%
            \yquant@draw%
               {$\unexpanded\expandafter{\texcode}$}%
               {/yquant/operators/every box}%
         }%
         % we don't use \qasm@langhelper@declare@command here, since it works globally
         \protected\long\def\expandafter\noexpand\csname qasm@lang@\cmd\endcsname####1 ####2\noexpand\qasm@end{%
            \qasm@langhelper@declare@command@@fetchargs{#3}{\the\numexpr#2-#3\relax}{####1}\expandafter\noexpand\csname qasm@lang@@def@\cmd\endcsname%
            \noexpand\yquant@env@rescan%
         }%
      }%
      \expandafter%
   \endgroup%
   \process%
   \yquant@env@scan%
}

\qasm@langhelper@declare@command{measure}{\expandafter\yquant@lang@@measure\@gobbletwo}01

\qasm@langhelper@declare@command{H}\yquant@lang@@h01

\qasm@langhelper@declare@command{X}\yquant@lang@@x01

\qasm@langhelper@declare@command{Y}\yquant@lang@@y01

\qasm@langhelper@declare@command{Z}\yquant@lang@@z01

\qasm@langhelper@declare@command{S}\qasm@lang@@s01
\def\qasm@lang@@s{%
   \yquant@draw%
      {$S$}%
      {/yquant/operators/every s}%
}

\qasm@langhelper@declare@command{T}\qasm@lang@@t01
\def\qasm@lang@@t{%
   \yquant@draw%
      {$T$}%
      {/yquant/operators/every t}%
}

\qasm@langhelper@declare@command{nop}\qasm@lang@@nop01
\def\qasm@lang@@nop#1#2#3{%
   \dimdef\yquant@lang@attr@value{\yquant@config@operator@minimum@width+\yquant@config@operator@sep}%
   \yquant@lang@@hspace{#3}%
   % we don't use a grid-based layout, so a nop really does nothing
}

\qasm@langhelper@declare@command{zero}\qasm@lang@@zero01
\def\qasm@lang@@zero#1#2#3{%
   \let\yquant@lang@attr@value=\qasm@config@zero%
   \yquant@lang@@init{#3}%
}

\qasm@langhelper@declare@command{discard}{\expandafter\yquant@lang@@discard\@gobbletwo}01

\qasm@langhelper@declare@command{slash}{\expandafter\yquant@lang@@slash\@gobbletwo}01

\qasm@langhelper@declare@command{dmeter}{\expandafter\yquant@lang@@dmeter\@gobbletwo}01

\qasm@langhelper@declare@command{cnot}\yquant@lang@@not11

\qasm@langhelper@declare@command{c-z}\yquant@lang@@z11

\qasm@langhelper@declare@command{c-x}\yquant@lang@@x11

\qasm@langhelper@declare@command{toffoli}\yquant@lang@@not21

\qasm@langhelper@declare@command{ZZ}{\expandafter\yquant@lang@@zz\@gobbletwo}02

\qasm@langhelper@declare@command{XX}\yquant@lang@@xx02

\qasm@langhelper@declare@command{swap}\yquant@lang@@swap02

\qasm@langhelper@declare@command{Utwo}\qasm@lang@@Utwo02
\def\qasm@lang@@Utwo{%
   \yquant@register@get@allowmultitrue%
   \yquant@draw%
      {$U$}%
      {/yquant/operators/every utwo}%
}

% space is a hspace _without_ wire - in priciple, we would have to discard and then re-initialize
\qasm@langhelper@declare@command{space}\qasm@lang@@nop01
% END_FOLD

% some commands that might be used in boxes
\def\qasm@cmd@m#1{\begin{pmatrix}#1\end{pmatrix}}
\def\qasm@cmd@txt#1{$#1$}% do not use \text{...}, but exit math mode - in this way, TikZ can still handle linebreak
\def\qasm@cmd@meter{}%
\def\qasm@cmd@dmeterwide#1#2{$#1$}%
\def\qasm@cmd@dmeter#1{$#1$}%

\endinput