From 32f9a397a899dfac82a4f4d80670f48b675da246 Mon Sep 17 00:00:00 2001
From: Norbert Preining <preining@logic.at>
Date: Sat, 3 Apr 2021 23:54:49 +0000
Subject: yquant (4apr21)

git-svn-id: svn://tug.org/texlive/trunk@58712 c570f23f-e606-0410-a88d-b1316a301751
---
 .../tex/latex/yquant/yquant-registers.tex          | 356 ++++++++++++++-------
 1 file changed, 241 insertions(+), 115 deletions(-)

(limited to 'Master/texmf-dist/tex/latex/yquant/yquant-registers.tex')

diff --git a/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex b/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex
index c90a44614ca..2c44fd039bb 100644
--- a/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex
+++ b/Master/texmf-dist/tex/latex/yquant/yquant-registers.tex
@@ -39,65 +39,70 @@
 % BEGIN_FOLD Constructor: create single register and cleanup environment
 
 % #1: type
-% #2: x position
-% #3: name
-% #4: index
-\protected\def\yquant@register@define#1#2#3#4{%
+% #2: name
+% #3: index
+\protected\def\yquant@register@define#1#2#3{%
    \csnumgdef{\yquant@prefix registers}%
              {\csname\yquant@prefix registers\endcsname+1}%
    \csxdef{\yquant@prefix register@\csname\yquant@prefix registers\endcsname}{%
       {#1}% type
-      {#2}% x pos
-      {{\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
+      {0pt}% x pos
+      {{\yquant@config@register@minimum@height}%
+       {\yquant@config@register@minimum@depth}%
+       {}}% height, depth, and multi-space parts; at the end, the y position
+      {{0pt}{0pt}{}{}}% wire start positions and clipping
       {}% wire style
+      {\yquant@register@flag@clean}% information about the last gate
    }%
-   \global\csletcs{\yquant@prefix registername@#3[#4]}{\yquant@prefix registers}%
-   \csxdef{\yquant@prefix registerhigh@#3}{#4}%
+   \global\csletcs{\yquant@prefix registername@#2[#3]}{\yquant@prefix registers}%
+   \csxdef{\yquant@prefix registerhigh@#2}{#3}%
    \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@#3}%
+   \yquant@cleanup@csadd{\yquant@prefix registername@#2[#3]}%
+   \ifnum0=#3\relax%
+      \yquant@cleanup@csadd{\yquant@prefix registerhigh@#2}%
    \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{%
+% #4: id in the outer circuit
+\protected\def\yquant@register@alias#1#2#3#4{%
    \csnumgdef{\yquant@prefix registers}%
              {\csname\yquant@prefix registers\endcsname+1}%
-   \ifcsname\yquant@parent registermap@#5\endcsname%
+   \ifcsname\yquant@parent registermap@#4\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}%
+         {\yquant@parent registermap@#4}%
    \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}%
+             {\yquant@parent register@#4}%
    \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}%
+   % we still store height, depth, and multi-space parts. To keep the same macros, we create a full register structure, but "abuse" the type component to contain the direct parent
+   \csxdef{\yquant@prefix register@\csname\yquant@prefix registers\endcsname}{%
+      {{\expandafter\yquant@register@alias@extract\yquant@parent}{#4}}% direct parent (circuit id, register id)
+      {}% unused
+      {{\yquant@config@register@minimum@height}%
+       {\yquant@config@register@minimum@depth}%
+       {}}% height, depth, and multi-space parts
+      {}% unused
+      {}% unused
+      {}% unused
+   }%
+   \global\csletcs{\yquant@prefix registername@#2[#3]}{\yquant@prefix registers}%
+   \csxdef{\yquant@prefix registerhigh@#2}{#3}%
+   \yquant@cleanup@csadd{\yquant@prefix register@\csname\yquant@prefix registers\endcsname}%
+   \yquant@cleanup@csadd{\yquant@prefix registermap@\csname\yquant@prefix registers\endcsname}%
+   \yquant@cleanup@csadd{\yquant@prefix registername@#2[#3]}%
+   \ifnum0=#3\relax%
+      \yquant@cleanup@csadd{\yquant@prefix registerhigh@#2}%
    \fi%
    \expandafter\yquant@register@set@@aux%
       \csname\csname\yquant@prefix registermap@\csname\yquant@prefix registers\endcsname\endcsname\endcsname%
-      \yquant@register@set@x@aux%
-      {#2}%
+      \yquant@register@set@typelastgate@aux%
+      {{#1}{\yquant@register@flag@clean}}%
 }
+
+\def\yquant@register@alias@extract yquant@env#1@{#1}%
 % END_FOLD
 
 % BEGIN_FOLD Converter: convert a register name to its id
@@ -162,19 +167,7 @@
       \fi%
       % pre-set y position
       \yquant@for \idx := \idx to \numexpr \len -1\relax {%
-         \yquant@register@define%
-            \yquant@register@type@q%
-            {\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%
-                                    {}%
-                                    {}%
-         }%
+         \yquant@prepare@create\reg\idx\yquant@register@type@q%
       }%
    \endgroup%
 }
@@ -680,7 +673,17 @@
    \fi%
 }
 
-\def\yquant@register@get@type@aux#1#2#3#4#5{#1}
+\def\yquant@register@get@parent#1{%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter\expandafter\expandafter%
+      \yquant@register@get@type@aux\csname\yquant@prefix register@#1\endcsname%
+   \else%
+      \PackageError{yquant.sty}{Internal inconsistency detected}%
+                   {Tried to get parent of a non-connected wire.}%
+   \fi%
+}
+
+\def\yquant@register@get@type@aux#1#2#3#4#5#6{#1}
 
 \def\yquant@register@get@x#1{%
    \ifcsname\yquant@prefix registermap@#1\endcsname%
@@ -692,31 +695,38 @@
    \fi%
 }
 
-\def\yquant@register@get@x@aux#1#2#3#4#5{#2}
+\def\yquant@register@get@x@aux#1#2#3#4#5#6{#2}
 
 \def\yquant@register@get@height#1{%
-   \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%
+   \expandafter\expandafter\expandafter%
+   \yquant@register@get@height@aux\csname\yquant@prefix register@#1\endcsname%
+}
+
+\def\yquant@register@peek@height#1#2{%
+   \expandafter\expandafter\expandafter%
+   \yquant@register@get@height@aux\csname yquant@env#1@register@#2\endcsname%
 }
 
-\def\yquant@register@get@height@aux#1#2#3#4#5{\@firstoftwo#3}
+\def\yquant@register@get@height@aux#1#2#3#4#5#6{\@firstofthree#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%
+   \expandafter\expandafter\expandafter%
+   \yquant@register@get@depth@aux\csname\yquant@prefix register@#1\endcsname%
+}
+
+\def\yquant@register@peek@depth#1#2{%
+   \expandafter\expandafter\expandafter%
+   \yquant@register@get@depth@aux\csname yquant@env#1@register@#2\endcsname%
 }
 
-\def\yquant@register@get@depth@aux#1#2#3#4#5{\@secondoftwo#3}
+\def\yquant@register@get@depth@aux#1#2#3#4#5#6{\@secondofthree#3}
+
+\def\yquant@register@get@multispace#1{%
+   \expandafter\expandafter\expandafter%
+   \yquant@register@get@multispace@aux\csname\yquant@prefix register@#1\endcsname%
+}
+
+\def\yquant@register@get@multispace@aux#1#2#3#4#5#6{\@thirdofthree#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@y{%
@@ -739,7 +749,7 @@
    \fi%
 }
 
-\def\yquant@register@get@y@aux#1#2#3#4#5{#3}
+\def\yquant@register@get@y@aux#1#2#3#4#5#6{#3}
 
 % y distance between two registers.
 \def\yquant@register@get@ydist#1#2{%
@@ -766,8 +776,8 @@
    }%
 }
 
-\protected\def\yquant@register@get@lastwire@aux#1#2#3#4#5#6{%
-   \def#6{#4}%
+\protected\def\yquant@register@get@lastwire@aux#1#2#3#4#5#6#7{%
+   \def#7{#4}%
 }
 
 \def\yquant@register@get@style#1{%
@@ -780,7 +790,28 @@
    \fi%
 }
 
-\def\yquant@register@get@style@aux#1#2#3#4#5{#5}
+\def\yquant@register@get@style@aux#1#2#3#4#5#6{#5}
+
+\protected\long\def\yquant@register@execclear@lastgate#1#2{%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \expandafter%
+      \yquant@register@execclear@lastgate@aux@i\csname\csname\yquant@prefix registermap@#1\endcsname\endcsname{#2}%
+   \else%
+      \expandafter%
+      \yquant@register@execclear@lastgate@aux@i\csname\yquant@prefix register@#1\endcsname{#2}%
+   \fi%
+}
+
+\def\yquant@register@execclear@lastgate@aux@i#1{%
+   \expandafter\yquant@register@execclear@lastgate@aux@ii#1#1%
+}
+
+\long\def\yquant@register@execclear@lastgate@aux@ii#1#2#3#4#5#6#7#8{%
+   \ifstrempty{#6}\relax{%
+      #6{#8}%
+      \xdef#7{\unexpanded{{#1}{#2}{#3}{#4}{#5}{}}}%
+   }%
+}
 
 \def\yquant@register@get@typeywire#1{%
    \ifcsname\yquant@prefix registermap@#1\endcsname%
@@ -797,10 +828,10 @@
    }%
 }
 
-\protected\def\yquant@register@get@typeywire@aux#1#2#3#4#5#6#7#8{%
-   \def#6{#1}%
-   \def#7{#3}%
-   \def#8{#4}%
+\protected\def\yquant@register@get@typeywire@aux#1#2#3#4#5#6#7#8#9{%
+   \def#7{#1}%
+   \def#8{#3}%
+   \def#9{#4}%
 }%
 
 % Set #1 to the maximum x value found between #2 and #3
@@ -833,6 +864,12 @@
    \endgroup%
    \expandafter\def\expandafter#1\expandafter{\the\dimen0}%
 }
+
+\protected\def\yquant@register@flag@clean#1{%
+   \ifstrequal{#1}{init}{%
+      \advance\count8 by 1 %
+   }\relax%
+}
 % END_FOLD
 
 % BEGIN_FOLD Setters: change register information
@@ -856,8 +893,8 @@
    \yquant@register@set@type@aux%
 }
 
-\long\def\yquant@register@set@type@aux#1#2#3#4#5#6{%
-   {#6}\unexpanded{{#2}{#3}{#4}{#5}}%
+\long\def\yquant@register@set@type@aux#1#2#3#4#5#6#7{%
+   {#7}\unexpanded{{#2}{#3}{#4}{#5}{#6}}%
 }
 
 \protected\def\yquant@register@set@x#1{%
@@ -876,23 +913,12 @@
    \yquant@register@set@x@aux%
 }
 
-\long\def\yquant@register@set@x@aux#1#2#3#4#5#6{%
-   \unexpanded{{#1}}{#6}\unexpanded{{#3}{#4}{#5}}%
+\long\def\yquant@register@set@x@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}}{#7}\unexpanded{{#3}{#4}{#5}{#6}}%
 }
 
 \protected\def\yquant@register@update@height#1#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%
+   \unless\ifyquant@lang@attr@overlay@height%
       \ifdim#2>%
          \expandafter\expandafter\expandafter%
             \yquant@register@get@height@aux\csname\yquant@prefix register@#1\endcsname%
@@ -904,25 +930,22 @@
    \fi%
 }
 
-\long\def\yquant@register@set@height@aux#1#2#3#4#5#6{%
+\protected\def\yquant@register@set@height@remote#1#2#3{%
+   \expandafter\yquant@register@set@@aux%
+      \csname yquant@env#1@register@#2\endcsname%
+      \yquant@register@set@height@aux{#3}%
+}
+
+\long\def\yquant@register@set@height@aux#1#2#3#4#5#6#7{%
    \unexpanded{{#1}{#2}}%
-   {{#6}{\unexpanded\expandafter{\@secondoftwo#3}}}%
-   \unexpanded{{#4}{#5}}%
+   {{#7}%
+    {\unexpanded\expandafter{\@secondofthree#3}}%
+    {\unexpanded\expandafter{\@thirdofthree#3}}}%
+   \unexpanded{{#4}{#5}{#6}}%
 }
 
 \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%
+   \unless\ifyquant@lang@attr@overlay@depth%
       \ifdim#2>%
          \expandafter\expandafter\expandafter%
             \yquant@register@get@depth@aux\csname\yquant@prefix register@#1\endcsname%
@@ -934,10 +957,75 @@
    \fi%
 }
 
-\long\def\yquant@register@set@depth@aux#1#2#3#4#5#6{%
+\protected\def\yquant@register@set@depth@remote#1#2#3{%
+   \expandafter\yquant@register@set@@aux%
+      \csname yquant@env#1@register@#2\endcsname%
+      \yquant@register@set@depth@aux{#3}%
+}
+
+\long\def\yquant@register@set@depth@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}{#2}}%
+   {{\unexpanded\expandafter{\@firstofthree#3}}%
+    {#7}%
+    {\unexpanded\expandafter{\@thirdofthree#3}}}%
+   \unexpanded{{#4}{#5}{#6}}%
+}
+
+\protected\def\yquant@register@update@multispace#1#2#3{%
+   \unless\ifnum#1<#2 %
+      \PackageError{yquant.sty}{Internal inconsistency detected}%
+                   {Setting multispace part in wrong order.}%
+   \fi%
+   \unless\ifyquant@lang@attr@overlay@multi%
+      \begingroup%
+         \edef\tmp{%
+            \expandafter\expandafter\expandafter%
+            \yquant@register@get@multispace@aux\csname\yquant@prefix register@#1\endcsname%
+         }%
+         \let\old=\tmp%
+         \yquant@list@eupdateorinsert\tmp{#2}{#3}%
+         \unless\ifx\old\tmp%
+            \expandafter\yquant@register@set@@aux%
+               \csname\yquant@prefix register@#1\endcsname%
+               \yquant@register@set@multispace@aux\tmp%
+         \fi%
+      \endgroup%
+   \fi%
+}
+
+\long\def\yquant@register@set@multispace@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}{#2}}%
+   {{\unexpanded\expandafter{\@firstofthree#3}}%
+    {\unexpanded\expandafter{\@secondofthree#3}}%
+    {#7}}%
+   \unexpanded{{#4}{#5}{#6}}%
+}
+
+\protected\def\yquant@register@inject@extents#1#2{%
+   % enlarge the last subcircuit that was created previously by height #1 and depth #2.
+   % We enforce subcircuits to have registers, so no check needed.
+   \expandafter\yquant@register@set@@aux%
+      \csname yquant@env\the\yquant@env @register@1\endcsname%
+      \yquant@register@inject@height@aux{#1}%
+   \expandafter\yquant@register@set@@aux%
+      \csname yquant@env\the\yquant@env @register@\csname yquant@env\the\yquant@env @registers\endcsname\endcsname%
+      \yquant@register@inject@depth@aux{#2}%
+}
+
+\long\def\yquant@register@inject@height@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}{#2}}%
+   {{\the\dimexpr\@firstofthree#3+#7\relax}%
+    {\unexpanded\expandafter{\@secondofthree#3}}%
+    {\unexpanded\expandafter{\@thirdofthree#3}}}%
+   \unexpanded{{#4}{#5}{#6}}%
+}
+
+\long\def\yquant@register@inject@depth@aux#1#2#3#4#5#6#7{%
    \unexpanded{{#1}{#2}}%
-   {{\unexpanded\expandafter{\@firstoftwo#3}}{#6}}%
-   \unexpanded{{#4}{#5}}%
+   {{\unexpanded\expandafter{\@firstofthree#3}}%
+    {\the\dimexpr\@secondofthree#3+#7\relax}%
+    {\unexpanded\expandafter{\@thirdofthree#3}}}%
+   \unexpanded{{#4}{#5}{#6}}%
 }
 
 \protected\def\yquant@register@set@y#1{%
@@ -956,8 +1044,8 @@
    \yquant@register@set@y@aux%
 }
 
-\long\def\yquant@register@set@y@aux#1#2#3#4#5#6{%
-   \unexpanded{{#1}{#2}}{#6}\unexpanded{{#4}{#5}}%
+\long\def\yquant@register@set@y@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}{#2}}{#7}\unexpanded{{#4}{#5}{#6}}%
 }
 
 \protected\def\yquant@register@set@lastwire#1{%
@@ -976,8 +1064,22 @@
    \yquant@register@set@lastwire@aux%
 }
 
-\long\def\yquant@register@set@lastwire@aux#1#2#3#4#5#6{%
-   \unexpanded{{#1}{#2}{#3}}{#6}\unexpanded{{#5}}%
+\long\def\yquant@register@set@lastwire@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}{#2}{#3}}{#7}\unexpanded{{#5}{#6}}%
+}
+
+\protected\def\yquant@register@set@typelastwire#1{%
+   \ifcsname\yquant@prefix registermap@#1\endcsname%
+      \PackageError{yquant.sty}{Internal inconsistency detected}%
+                   {Setting type and lastwire was requested for a subcircuit.}%
+   \fi%
+   \expandafter\yquant@register@set@@aux%
+      \csname\yquant@prefix register@#1\endcsname%
+      \yquant@register@set@typelastwire@aux%
+}
+
+\long\def\yquant@register@set@typelastwire@aux#1#2#3#4#5#6#7{%
+   {\@firstoftwo#7}\unexpanded{{#2}{#3}}{\@secondoftwo#7}\unexpanded{{#5}{#6}}%
 }
 
 \protected\def\yquant@register@set@style#1{%
@@ -996,7 +1098,31 @@
    \yquant@register@set@style@aux%
 }
 
-\long\def\yquant@register@set@style@aux#1#2#3#4#5#6{%
-   \unexpanded{{#1}{#2}{#3}{#4}}{#6}%
+\long\def\yquant@register@set@style@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}{#2}{#3}{#4}}{#7}\unexpanded{{#6}}%
+}
+
+\protected\def\yquant@register@set@lastgate#1{%
+   \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@lastgate@aux%
+}
+
+\long\def\yquant@register@set@lastgate@aux#1#2#3#4#5#6#7{%
+   \unexpanded{{#1}{#2}{#3}{#4}{#5}}{#7}%
+}
+
+\long\def\yquant@register@set@typelastgate@aux#1#2#3#4#5#6#7{%
+   {\@firstoftwo#7}\unexpanded{{#2}{#3}{#4}{#5}}{\@secondoftwo#7}%
 }
 % END_FOLD
\ No newline at end of file
-- 
cgit v1.2.3

