From 893b65e5d4c0b7cb8ebbfca9ee6b090b9a4a182c Mon Sep 17 00:00:00 2001
From: Norbert Preining <norbert@preining.info>
Date: Sun, 14 Jun 2020 03:02:26 +0000
Subject: CTAN sync 202006140302

---
 .../pgf/contrib/yquant/tex/yquant-registers.tex    | 355 ++++++++++++++++++---
 1 file changed, 302 insertions(+), 53 deletions(-)

(limited to 'graphics/pgf/contrib/yquant/tex/yquant-registers.tex')

diff --git a/graphics/pgf/contrib/yquant/tex/yquant-registers.tex b/graphics/pgf/contrib/yquant/tex/yquant-registers.tex
index 18a23662d3..c90a44614c 100644
--- a/graphics/pgf/contrib/yquant/tex/yquant-registers.tex
+++ b/graphics/pgf/contrib/yquant/tex/yquant-registers.tex
@@ -3,6 +3,38 @@
 \def\yquant@register@type@c{2}
 \def\yquant@register@type@qs{3}
 
+\def\yquant@register@type@tostring#1{%
+   \ifcase#1 %
+      nobit%
+   \or%
+      qubit%
+   \or%
+      cbit%
+   \or%
+      qubits%
+   \else%
+      \PackageError{yquant.sty}{Internal inconsistency detected}%
+                   {Unknown wire type: `#1'.}%
+   \fi%
+}
+
+\protected\def\yquant@register@type@fromstring#1#2{%
+   \expandafter\ifstrequal\expandafter{#1}{qubit}%
+      {\let#2=\yquant@register@type@q}%
+      {%
+         \expandafter\ifstrequal\expandafter{#1}{cbit}%
+         {\let#2=\yquant@register@type@c}%
+         {%
+            \expandafter\ifstrequal\expandafter{#1}{qubits}%
+            {\let#2=\yquant@register@type@qs}%
+            {%
+               \PackageError{yquant.sty}{Invalid bit type: `#1'}%
+                  {Use one of `qubit', `cbit', or `qubits'.}%
+            }%
+         }%
+      }%
+}
+
 % Internally, we store the register information in a single macro. Indeed, this is almost always faster than using multiple macros (at least in the way implemented here), apart from changing a single type of information, which is almost as fast.
 % BEGIN_FOLD Constructor: create single register and cleanup environment
 
@@ -16,17 +48,55 @@
    \csxdef{\yquant@prefix register@\csname\yquant@prefix registers\endcsname}{%
       {#1}% type
       {#2}% x pos
-      {\yquant@config@register@minimum@height}% height; at the end, the y position
-      {{0pt}{0pt}{}{}}% wire start positions and clipping
+      {{\the\dimexpr.5\dimexpr\yquant@config@register@minimum@height\relax\relax}%
+       {\the\dimexpr.5\dimexpr\yquant@config@register@minimum@height\relax\relax}}% height and depth; at the end, the y position
+      {{\yquant@env@create@x}{\yquant@env@create@x}{}{}}% wire start positions and clipping
       {}% wire style
    }%
    \global\csletcs{\yquant@prefix registername@#3[#4]}{\yquant@prefix registers}%
-   \csxdef{\yquant@prefix registerhigh@\reg}{#4}%
+   \csxdef{\yquant@prefix registerhigh@#3}{#4}%
    \yquant@cleanup@csadd{\yquant@prefix register@\csname\yquant@prefix registers\endcsname}%
    \yquant@cleanup@csadd{\yquant@prefix registername@#3[#4]}%
    \ifnum0=#4\relax%
-      \yquant@cleanup@csadd{\yquant@prefix registerhigh@\reg}%
+      \yquant@cleanup@csadd{\yquant@prefix registerhigh@#3}%
+   \fi%
+}
+
+% in a subcircuit, create a new register that is only an alias for an existing register in the outer circuit.
+% #5: id in the outer circuit
+\protected\def\yquant@register@alias#1#2#3#4#5{%
+   \csnumgdef{\yquant@prefix registers}%
+             {\csname\yquant@prefix registers\endcsname+1}%
+   \ifcsname\yquant@parent registermap@#5\endcsname%
+      % the parent is already an alias itself, pass this on transparently
+      \global\csletcs%
+         {\yquant@prefix registermap@\csname\yquant@prefix registers\endcsname}%
+         {\yquant@parent registermap@#5}%
+   \else%
+      \csxdef{\yquant@prefix registermap@\csname\yquant@prefix registers\endcsname}%
+             {\yquant@parent register@#5}%
+   \fi%
+   % even though this is an alias, we still need to keep track of its internal dimensions
+   \csgdef{\yquant@prefix registerdim@\csname\yquant@prefix registers\endcsname}%
+          {{0pt}{0pt}}%
+   \yquant@cleanup@csadd{\yquant@prefix registerdim@\csname\yquant@prefix registers\endcsname}%
+   \global\csletcs{\yquant@prefix registername@#3[#4]}{\yquant@prefix registers}%
+   \csxdef{\yquant@prefix registerhigh@#3}{#4}%
+   \yquant@cleanup@csadd{\yquant@prefix registermap@\csname\yquant@prefix registers\endcsname}%
+   \yquant@cleanup@csadd{\yquant@prefix registername@#3[#4]}%
+   \ifnum0=#4\relax%
+      \yquant@cleanup@csadd{\yquant@prefix registerhigh@#3}%
+   \fi%
+   \csxdef{\yquant@prefix lastinput}{{\csname\yquant@prefix registers\endcsname}{#5}}%
+   \unless\ifcsname\yquant@prefix firstinput\endcsname%
+      \csxdef{\yquant@prefix firstinput}{{\csname\yquant@prefix registers\endcsname}{#5}}%
+      \yquant@cleanup@csadd{\yquant@prefix firstinput}%
+      \yquant@cleanup@csadd{\yquant@prefix lastinput}%
    \fi%
+   \expandafter\yquant@register@set@@aux%
+      \csname\csname\yquant@prefix registermap@\csname\yquant@prefix registers\endcsname\endcsname\endcsname%
+      \yquant@register@set@x@aux%
+      {#2}%
 }
 % END_FOLD
 
@@ -43,19 +113,17 @@
 
 % same as before, but resolves to the _index_ of the last register
 \protected\def\yquant@register@get@id@high#1#2{%
-   \ifcsname\yquant@prefix registerhigh@#2\endcsname%
-      \letcs#1{\yquant@prefix registerhigh@#2}%
-   \else%
+   \unless\ifcsname\yquant@prefix registerhigh@#2\endcsname%
       \yquant@register@get@id@lazycreate{#2}%
-      \letcs#1{\yquant@prefix registers}%
    \fi%
+   \letcs#1{\yquant@prefix registerhigh@#2}%
 }
 
 % In the lazy mode, we create all unknown registers on-the-fly.
 \def\yquant@register@get@id@lazycreate#1{%
    \ifyquant@env@lazy%
       % In principle, we could just call \yquant@lang@@qubit. However, if access to a vector register is desired and a _part_ of this does already exist (though not the requested index), we only create the missing registers.
-      \yquant@register@get@id@lazycreate@parse#1[;
+      \yquant@register@get@id@lazycreate@parse#1[;%
    \else%
       \PackageError{yquant.sty}{Register `#1' unknown}%
          {You referred to an unknown register.
@@ -96,13 +164,14 @@
       \yquant@for \idx := \idx to \numexpr \len -1\relax {%
          \yquant@register@define%
             \yquant@register@type@q%
-            {0pt}%
-            \reg\idx
+            {\yquant@env@create@x}%
+            \reg\idx%
          % Prepare to shipout
          \csxappto{\yquant@prefix draw}{%
             \yquant@lang@create@draw{\csname\yquant@prefix registers\endcsname}%
                                     {0pt}%
                                     {qubit}%
+                                    0%
                                     {}%
                                     {}%
          }%
@@ -137,7 +206,10 @@
             \catcode`(=\active%
             \def[##1]{\yquant@register@get@ids@@index{##1}}%
             \def(##1){\yquant@register@get@ids@@multi{##1}}%
-            \edef\yquant@register@get@ids@retokenized{\scantokens{#1\noexpand}}%
+            % this is also invoked by other languages which might have completely redefined all catcodes (qasm redefines the backslash), so don't just add \noexpand at the end of the \scantokens...
+            \everyeof{\noexpand}%
+            \endlinechar=-1 %
+            \edef\yquant@register@get@ids@retokenized{\scantokens{#1}}%
             \expandafter%
          \endgroup%
          \expandafter\docsvlist\expandafter{\yquant@register@get@ids@retokenized}%
@@ -239,7 +311,7 @@
          }%
          \count0=2147483647 %
          \csletcs{ifallowmain}{iffalse}%
-      }{
+      }{%
          \let\yquant@register@get@ids@norange@checkindex@setmain=\relax%
       }%
    \else%
@@ -346,7 +418,7 @@
          \expandafter\yquant@register@get@id\expandafter\idx\expandafter{\idx}%
          \count6=\idx%
          \csletcs{ifallowmain}{iffalse}%
-      }{
+      }{%
          \yquant@register@get@id\idx{\current[#1]}%
       }%
    \else%
@@ -575,50 +647,99 @@
    }%
 }
 
+\protected\def\yquant@register@multi@splitparts@sort{%
+   \yquant@sort@list\yquant@register@get@ids@list\yquant@sort@ascending%
+}
+
 \protected\def\yquant@register@get@multiassingle{%
    \yquant@register@get@allowmultitrue%
    \let\yquant@register@multi@splitparts=\yquant@register@multi@splitparts@sepall%
    \preto\yquant@attrs@remaining{/yquant/operator/multi as single,}%
 }
+
+\protected\def\yquant@register@get@multiaslist{%
+   \yquant@register@get@allowmultitrue%
+   \let\yquant@register@multi@splitparts=\yquant@register@multi@splitparts@sort%
+}
+
+\let\yquant@register@multi@reset@original@splitparts=\yquant@register@multi@splitparts%
+\protected\def\yquant@register@reset@multi{%
+%   \yquant@register@get@allowmultifalse% not needed, as \yquant@circuit@operator automatically resets it
+   \let\yquant@register@multi@splitparts=\yquant@register@multi@reset@original@splitparts%
+}
 % END_FOLD
 
 % BEGIN_FOLD Getters: extract the requested information from the register with given id
 \def\yquant@register@get@type#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant@register@get@type@aux\csname\yquant@prefix register@#1\endcsname%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@type@aux\csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@type@aux\csname\yquant@prefix register@#1\endcsname%
+   \fi%
 }
 
 \def\yquant@register@get@type@aux#1#2#3#4#5{#1}
 
 \def\yquant@register@get@x#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant@register@get@x@aux\csname\yquant@prefix register@#1\endcsname%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@x@aux\csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@x@aux\csname\yquant@prefix register@#1\endcsname%
+   \fi%
 }
 
 \def\yquant@register@get@x@aux#1#2#3#4#5{#2}
 
 \def\yquant@register@get@height#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant@register@get@height@aux\csname\yquant@prefix register@#1\endcsname%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \@firstoftwo\csname\yquant@prefix registerdim@#1\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@height@aux\csname\yquant@prefix register@#1\endcsname%
+   \fi%
 }
 
-\def\yquant@register@get@height@aux#1#2#3#4#5{#3}
+\def\yquant@register@get@height@aux#1#2#3#4#5{\@firstoftwo#3}
+
+\def\yquant@register@get@depth#1{%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \@secondoftwo\csname\yquant@prefix registerdim@#1\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@depth@aux\csname\yquant@prefix register@#1\endcsname%
+   \fi%
+}
+
+\def\yquant@register@get@depth@aux#1#2#3#4#5{\@secondoftwo#3}
 
 % The y parameter get macros exist in two forms: The protected one is used during the storage to the draw macro; it should never be executed. The env environment then maps them to their proper expandable forms.
-\protected\def\yquant@register@get@@protected{%
+\protected\def\yquant@register@get@y{%
    \PackageError%
       {yquant.sty}%
       {Execution of a get-y macro in an illegal context}%
       {get-y macros may not be called unless the yquant environment ends.}%
 }
 
-\let\yquant@register@get@y=\yquant@register@get@@protected
-\let\yquant@register@y=\yquant@register@get@@protected
 % This plays a similar role: It must never be expanded before the tikz command is invoked (because it does not exist).
-\let\nodepart=\yquant@register@get@@protected
+\protected\def\nodepart{}
 
-% getting the y position is the same as getting the height parameter, since we will reuse it at the end.
-\let\yquant@register@get@y@unprotected=\yquant@register@get@height
+\def\yquant@register@get@y@unprotected#1{%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@y@aux\csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@y@aux\csname\yquant@prefix register@#1\endcsname%
+   \fi%
+}
+
+\def\yquant@register@get@y@aux#1#2#3#4#5{#3}
 
 % y distance between two registers.
 \def\yquant@register@get@ydist#1#2{%
@@ -626,17 +747,23 @@
       {\the\dimexpr\yquant@register@get@y{#2}-\yquant@register@get@y{#1}\relax}%
 }
 
-% y position of the current register
-\def\yquant@register@y@unprotected{0pt}%
-
 \protected\def\yquant@register@get@y@@expandable{%
    \let\yquant@register@get@y=\yquant@register@get@y@unprotected%
-   \let\yquant@register@y=\yquant@register@y@unprotected%
 }
 
 \def\yquant@register@get@lastwire#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant@register@get@lastwire@aux\csname\yquant@prefix register@#1\endcsname%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@lastwire@aux\csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@lastwire@aux\csname\yquant@prefix register@#1\endcsname%
+   }%
 }
 
 \protected\def\yquant@register@get@lastwire@aux#1#2#3#4#5#6{%
@@ -644,15 +771,30 @@
 }
 
 \def\yquant@register@get@style#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant@register@get@style@aux\csname\yquant@prefix register@#1\endcsname%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@style@aux\csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   \else%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@style@aux\csname\yquant@prefix register@#1\endcsname%
+   \fi%
 }
 
 \def\yquant@register@get@style@aux#1#2#3#4#5{#5}
 
 \def\yquant@register@get@typeywire#1{%
-   \expandafter\expandafter\expandafter%
-   \yquant@register@get@typeywire@aux\csname\yquant@prefix register@#1\endcsname%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@typeywire@aux\csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@typeywire@aux\csname\yquant@prefix register@#1\endcsname%
+   }%
 }
 
 \protected\def\yquant@register@get@typeywire@aux#1#2#3#4#5#6#7#8{%
@@ -695,12 +837,23 @@
 
 % BEGIN_FOLD Setters: change register information
 \protected\long\def\yquant@register@set@@aux#1#2#3{%
-   \long\xdef#1{\expandafter#2#1{#3}}%
+   \xdef#1{\expandafter#2#1{#3}}%
 }
 
 \protected\def\yquant@register@set@type#1{%
-   \expandafter\yquant@register@set@@aux%
-   \csname\yquant@prefix register@#1\endcsname\yquant@register@set@type@aux%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant@register@set@@aux%
+         \csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant@register@set@@aux%
+      \csname\yquant@prefix register@#1\endcsname%
+   }%
+   \yquant@register@set@type@aux%
 }
 
 \long\def\yquant@register@set@type@aux#1#2#3#4#5#6{%
@@ -708,8 +861,19 @@
 }
 
 \protected\def\yquant@register@set@x#1{%
-   \expandafter\yquant@register@set@@aux%
-   \csname\yquant@prefix register@#1\endcsname\yquant@register@set@x@aux%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant@register@set@@aux%
+         \csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant@register@set@@aux%
+      \csname\yquant@prefix register@#1\endcsname%
+   }%
+   \yquant@register@set@x@aux%
 }
 
 \long\def\yquant@register@set@x@aux#1#2#3#4#5#6{%
@@ -717,25 +881,99 @@
 }
 
 \protected\def\yquant@register@update@height#1#2{%
-   \ifdim#2>\yquant@register@get@height{#1} %
-      \expandafter\yquant@register@set@@aux%
-      \csname\yquant@prefix register@#1\endcsname\yquant@register@set@height@aux{#2}%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \@firstoftwo\csname\yquant@prefix registerdim@#1\endcsname%
+         \relax%
+         \csxdef{\yquant@prefix registerdim@#1}{%
+            {#2}%
+            {\expandafter\expandafter\expandafter%
+                \@secondoftwo\csname\yquant@prefix registerdim@#1\endcsname}%
+         }%
+      \fi%
+   \else%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \yquant@register@get@height@aux\csname\yquant@prefix register@#1\endcsname%
+         \relax%
+         \expandafter\yquant@register@set@@aux%
+            \csname\yquant@prefix register@#1\endcsname%
+            \yquant@register@set@height@aux{#2}%
+      \fi%
+   \fi%
+}
+
+\long\def\yquant@register@set@height@aux#1#2#3#4#5#6{%
+   \unexpanded{{#1}{#2}}%
+   {{#6}{\unexpanded\expandafter{\@secondoftwo#3}}}%
+   \unexpanded{{#4}{#5}}%
+}
+
+\protected\def\yquant@register@update@depth#1#2{%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \@secondoftwo\csname\yquant@prefix registerdim@#1\endcsname%
+         \relax%
+         \csxdef{\yquant@prefix registerdim@#1}{%
+            {\expandafter\expandafter\expandafter%
+                \@firstoftwo\csname\yquant@prefix registerdim@#1\endcsname}%
+            {#2}%
+         }%
+      \fi%
+   \else%
+      \ifdim#2>%
+         \expandafter\expandafter\expandafter%
+            \yquant@register@get@depth@aux\csname\yquant@prefix register@#1\endcsname%
+         \relax%
+         \expandafter\yquant@register@set@@aux%
+            \csname\yquant@prefix register@#1\endcsname%
+            \yquant@register@set@depth@aux{#2}%
+      \fi%
    \fi%
 }
 
+\long\def\yquant@register@set@depth@aux#1#2#3#4#5#6{%
+   \unexpanded{{#1}{#2}}%
+   {{\unexpanded\expandafter{\@firstoftwo#3}}{#6}}%
+   \unexpanded{{#4}{#5}}%
+}
+
 \protected\def\yquant@register@set@y#1{%
-   \expandafter\yquant@register@set@@aux%
-   \csname\yquant@prefix register@#1\endcsname\yquant@register@set@height@aux%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant@register@set@@aux%
+         \csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant@register@set@@aux%
+      \csname\yquant@prefix register@#1\endcsname%
+   }%
+   \yquant@register@set@y@aux%
 }
 
-% Set the currently used register
-\long\def\yquant@register@set@height@aux#1#2#3#4#5#6{%
+\long\def\yquant@register@set@y@aux#1#2#3#4#5#6{%
    \unexpanded{{#1}{#2}}{#6}\unexpanded{{#4}{#5}}%
 }
 
 \protected\def\yquant@register@set@lastwire#1{%
-   \expandafter\yquant@register@set@@aux%
-   \csname\yquant@prefix register@#1\endcsname\yquant@register@set@lastwire@aux%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant@register@set@@aux%
+         \csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant@register@set@@aux%
+      \csname\yquant@prefix register@#1\endcsname%
+   }%
+   \yquant@register@set@lastwire@aux%
 }
 
 \long\def\yquant@register@set@lastwire@aux#1#2#3#4#5#6{%
@@ -743,8 +981,19 @@
 }
 
 \protected\def\yquant@register@set@style#1{%
-   \expandafter\yquant@register@set@@aux%
-   \csname\yquant@prefix register@#1\endcsname\yquant@register@set@style@aux%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\@firstoftwo%
+   \else%
+      \expandafter\@secondoftwo%
+   \fi%
+   {%
+      \expandafter\yquant@register@set@@aux%
+         \csname\csname\yquant@prefix registermap@#1\endcsname\endcsname%
+   }{%
+      \expandafter\yquant@register@set@@aux%
+      \csname\yquant@prefix register@#1\endcsname%
+   }%
+   \yquant@register@set@style@aux%
 }
 
 \long\def\yquant@register@set@style@aux#1#2#3#4#5#6{%
-- 
cgit v1.2.3

