\newcount\yquant@draw@@currentcontroltype%

\protected\def\yquant@draw@init#1#2{%
   \ifyquant@register@isaliased{#1}{%
      % for connected subcircuit wires, check if this actually changes the type [happens for formerly discarded output wires] and flush the wire if necessary
      \edef\yquant@draw@init@type{\yquant@register@get@type{#1}}%
      \unless\if\yquant@draw@init@type#2\relax%
         \yquant@circuit@flushwire{#1}%
         \ifx\yquant@draw@init@type\yquant@register@types@nobit%
            % in case the wire was discarded before, forget about the lastx position, it should always start with the others
            \yquant@register@get@lastwire{#1}\wirelast%
            \yquant@register@set@lastwire{#1}{%
               {\yquant@draw@subcircuit@wirestart}{\yquant@draw@subcircuit@wirestart}{}%
               {\unexpanded\expandafter\expandafter\expandafter{%
                   \expandafter\@fourthoffour\wirelast%
                }}%
            }%
         \fi%
         \yquant@register@set@type{#1}{#2}%
      \fi%
   }{%
      % for new wires, reset the type, and if this a subcircuit, put the appropriate x position in place. It might have changed from its initial value during preparation stage
      \yquant@register@set@type{#1}{#2}%
      \ifdefined\yquant@draw@subcircuit@wirestart%
         \yquant@register@set@x{#1}{\yquant@draw@subcircuit@wirestart}%
         \yquant@register@set@lastwire{#1}{%
            {\yquant@draw@subcircuit@wirestart}{\yquant@draw@subcircuit@wirestart}{}{}%
         }%
      \fi%
   }%
}

\def\yquant@draw@subcircuit@leftpos{0pt}% outermost circuit: 0pt
\protected\long\def\yquant@draw@group#1#2#3#4#5#6{%
   \yquant@begingroup%
      \yquant@register@get@maxxlist\yquant@draw@@x{#6}%
      \ifx F#2%
         \yquant@draw@@currentcontroltype=0 %
      \else%
         \yquant@draw@@currentcontroltype=#2 %
      \fi%
      \let\yquant@circuit@extendcontrolline@cmd=\empty%
      \let\yquant@circuit@extendcontrolline@prev=\relax%
      \let\yquant@circuit@extendcontrolline@clip=\empty%
      \let\yquant@circuit@extendmultiline@total=\empty%
      \def\yquant@draw@@width{#1}%
      \def\yquant@draw@@style{#4}%
      \def\yquant@draw@@content{#5}%
      \def\yquant@draw@@idx@content{0}%
      \def\yquant@draw@@idx@pcontrol{0}%
      \def\yquant@draw@@idx@ncontrol{0}%
      % If the quotes library is loaded, activate it. (else, this is by default \relax)
      \tikz@enable@node@quotes%
      \yquant@config@operator@position@rightalignfalse%
      \yquant@config@operator@position@advancetrue%
      \pgfkeys{/yquant/internal/before styles}%
      \yquant@set{#3}%
      % if the operator is right-aligned, we will not advance, as we must assume that the x position is already chosen appropriately
      \ifyquant@config@operator@position@rightalign%
         % for rightalign, we instead overwrite the lastx configurations, so that the outgoing wire starts after the operator.
         \forlistloop\yquant@draw@group@rightadvance{#6}%
      \else%
         \ifyquant@env@seamless{%
            \unless\ifdim\yquant@draw@@x=\yquant@draw@subcircuit@leftpos%
               \dimdef\yquant@draw@@x{%
                  \yquant@draw@@x\yquant@orientation@plus\yquant@config@operator@sep%
               }%
            \fi%
         }{%
            \dimdef\yquant@draw@@x{%
               \yquant@draw@@x\yquant@orientation@plus\yquant@config@operator@sep%
            }%
         }%
         \ifyquant@config@operator@position@advance%
            \dimdef\newx{\yquant@draw@@x\yquant@orientation@plus#1}%
            \forlistloop\yquant@draw@group@advance{#6}%
         \fi%
         \dimdef\yquant@draw@@x{\yquant@draw@@x\yquant@orientation@plus.5\dimexpr#1\relax}%
      \fi%
      \unless\ifyquant@config@internal@noenclose%
         \forlistloop{\yquant@draw@enclose@measure{#6}}\yquant@circuit@enclose@active%
      \fi%
      \yquant@config@internal@noenclosefalse%
}

\protected\def\yquant@draw@group@advance#1{%
   \yquant@register@set@x{#1}\newx%
}

\protected\def\yquant@draw@group@rightadvance#1{%
   \ifyquant@register@isaliased{#1}{%
      % for connected subcircuit wires, make sure to flush the wire until the beginning.
      \yquant@circuit@flushwire{#1}%
   }\relax%
}

\protected\def\yquant@draw@endgroup#1#2#3{%
      \unless\ifx F#1%
         \yquant@draw@cline%
      \fi%
      \ifcase#3\relax%
      \or%
         \yquant@draw@alias@ctrl{#2}n%
      \or%
         \yquant@draw@alias@ctrl{#2}p%
      \or%
         \yquant@draw@alias@ctrl{#2}p%
         \yquant@draw@alias@ctrl{#2}n%
      \or%
         \yquant@draw@alias{#2}%
      \or%
         \yquant@draw@alias{#2}%
         \yquant@draw@alias@ctrl{#2}n%
      \or%
         \yquant@draw@alias{#2}%
         \yquant@draw@alias@ctrl{#2}p%
      \or%
         \yquant@draw@alias{#2}%
         \yquant@draw@alias@ctrl{#2}p%
         \yquant@draw@alias@ctrl{#2}n%
      \fi%
      \yquant@circuit@extendmultiline@total%
   \yquant@endgroup%
}

\protected\def\yquant@draw@injectiongroup#1#2\yquant@draw@endinjectiongroup#3#4#5{%
   \csdef{yquant@draw@@injection@#1}{%
      \yquant@draw@begininjection#2\yquant@draw@stopinjection{#3}{#4}{#5}%
   }%
}

\protected\long\def\yquant@draw@begininjection#1#2#3#4#5#6{%
   % this is called within another draw group, so lots of things are already set up
   \yquant@begingroup%
      \def\yquant@draw@@style{#4}%
      \def\yquant@draw@@content{#5}%
      \let\yquant@draw@@idx@content=\yquant@draw@@idx@pcontrol%
      \yquant@set{#3}% this will accumulate with outer styles from the enclosing draw group, but there is nothing we can do about it
}

\protected\def\yquant@draw@stopinjection#1#2#3{%
   \ifcase#3\relax%
   \or%
      \yquant@draw@alias@ctrl{#2}n%
   \or%
      \yquant@draw@alias@ctrl{#2}p%
   \or%
      \yquant@draw@alias@ctrl{#2}p%
      \yquant@draw@alias@ctrl{#2}n%
   \or%
      \yquant@draw@alias{#2}%
   \or%
      \yquant@draw@alias{#2}%
      \yquant@draw@alias@ctrl{#2}n%
   \or%
      \yquant@draw@alias{#2}%
      \yquant@draw@alias@ctrl{#2}p%
   \or%
      \yquant@draw@alias{#2}%
      \yquant@draw@alias@ctrl{#2}p%
      \yquant@draw@alias@ctrl{#2}n%
   \fi%
   \edef\process{%
         \yquant@endgroup%
         \ifyquant@config@draw@quality%
            \def\noexpand\yquant@circuit@extendcontrolline@clip{%
               \unexpanded\expandafter{\yquant@circuit@extendcontrolline@clip}%
            }%
         \fi%
         \def\noexpand\yquant@circuit@extendcontrolline@prev{%
            \unexpanded\expandafter{\yquant@circuit@extendcontrolline@prev}%
         }%
         \def\noexpand\yquant@circuit@extendcontrolline@cmd{%
            \unexpanded\expandafter{\yquant@circuit@extendcontrolline@cmd}%
         }%
         \def\noexpand\yquant@circuit@extendmultiline@total{%
            \unexpanded\expandafter{\yquant@circuit@extendmultiline@total}%
         }%
         \def\noexpand\yquant@draw@@idx@pcontrol{\yquant@draw@@idx@content}%
      }%
   \process%
}

\protected\def\yquant@draw@inject#1{%
   \csname yquant@draw@@injection@#1\endcsname%
   \csgundef{yquant@draw@@injection@#1}%
}

\protected\def\yquant@draw@inject@outer#1{%
   % we thought we wanted to inject #1, however, this was not valid; so now insert it directly.
   \expandafter\expandafter\expandafter\yquant@draw@inject@outer@helper%
      \csname yquant@draw@@injection@#1\endcsname%
   \csgundef{yquant@draw@@injection@#1}%
}

\def\yquant@draw@inject@outer@helper\yquant@draw@begininjection#1\yquant@draw@stopinjection{%
   \yquant@draw@group%
      #1%
   \yquant@draw@endgroup%
}

\protected\def\yquant@draw@single#1#2{%
   \let\idx=\yquant@draw@@idx@content%
   \yquant@config@operator@multifalse%
   \edef\cmd{%
      \noexpand\path \yquant@coords(\yquant@draw@@x, \yquant@register@get@y{#1})%
         node[/yquant/every operator, \yquant@draw@@style, /yquant/this operator,%
              name prefix=, name suffix=, name=yquantbox]%
         {\unexpanded\expandafter{\yquant@draw@@content}};%
      \ifyquant@config@draw@quality%
         \pgfshapeclippath{yquantbox}%
                          {/yquant/every operator, \yquant@draw@@style,%
                           /yquant/this operator}%
      \fi%
   }%
   \cmd%
   \yquant@circuit@extendwire{#1}{center}%
   \expandafter\yquant@circuit@extendcontrolline\expandafter%
      {\the\yquant@draw@@currentcontroltype}\yquant@draw@@x{0pt}%
   % check for empty name parameter
   \ifstrempty{#2}\relax{%
      \pgfnodealias{\tikz@pp@name{#2}}{yquantbox}%
   }%
   \numdef\yquant@draw@@idx@content{\yquant@draw@@idx@content+1}%
}

\protected\def\yquant@draw@multi#1#2#3#4#5{%
   \yquant@config@operator@multitrue%
   \let\idx=\yquant@draw@@idx@content%
   \edef\yquant@draw@multi@@name{#5}%
   \def\yquant@draw@@idx@multipart{0}%
   \let\yquant@circuit@extendmultiline@cmd=\empty%
   \let\yquant@circuit@extendmultiline@prev=\relax%
   \let\yquant@circuit@extendmultiline@clip=\empty%
   \let\yquant@register@multi@contiguous=\yquant@draw@multi@contiguous%
   #4%
   \ifnum\yquant@draw@@idx@multipart>1 %
      % make sure also the first split part is available via the "-s0" suffix
      \unless\ifx\yquant@draw@multi@@name\empty%
         \pgfnodealias{\tikz@pp@name{\yquant@draw@multi@@name-s0}}%
                      {\tikz@pp@name{\yquant@draw@multi@@name}}%
      \fi%
      \yquant@draw@mline@prep%
   \fi%
   \numdef\yquant@draw@@idx@content{\yquant@draw@@idx@content+1}%
}

\protected\def\yquant@draw@multi@contiguous#1#2#3{%
   % We need to somehow extract the y radius
   \edef\cmd{%
      \noexpand\path \yquant@coords(\yquant@draw@@x, \the\dimexpr.5\dimexpr%
                         \yquant@register@get@y{#1}+\yquant@register@get@y{#2}\relax%
                      \relax)%
         node[/yquant/every operator, \yquant@draw@@style, /yquant/this operator,%
              /yquant/internal/multi main=\ifnum#3=1 true\else false\fi\unless\ifnum#1=#2 ,%
              space radius/.expanded=\the\dimexpr.5\dimexpr\yquant@register@get@ydist{#1}{#2}\relax\relax+%
                     .5*\noexpand\pgfkeysvalueof{/tikz/\yquant@orientation@y\space radius}\fi,%
              name prefix=, name suffix=, name=yquantbox]%
            {\unexpanded\expandafter{\yquant@draw@@content}};%
      \ifyquant@config@draw@quality%
         \pgfshapeclippath{yquantbox}%
                          {/yquant/every operator, \yquant@draw@@style,%
                           /yquant/this operator}%
      \fi%
   }%
   \cmd%
   \yquant@for \i := #1 to #2 {%
      \yquant@circuit@extendwire\i{center}%
   }%
   \yquant@circuit@extendmultiline\yquant@draw@@x%
   \expandafter\yquant@circuit@extendcontrolline\expandafter%
      {\the\yquant@draw@@currentcontroltype}\yquant@draw@@x{0pt}%
   \unless\ifx\yquant@draw@multi@@name\empty%
      \ifnum\yquant@draw@@idx@multipart=0 %
         \pgfnodealias{\tikz@pp@name{\yquant@draw@multi@@name}}{yquantbox}%
      \else%
         \pgfnodealias{\tikz@pp@name{\yquant@draw@multi@@name-s\yquant@draw@@idx@multipart}}{yquantbox}%
      \fi%
   \fi%
   \numdef\yquant@draw@@idx@multipart{\yquant@draw@@idx@multipart+1}%
}

\protected\def\yquant@draw@multiinit#1#2#3#4#5{%
   \let\idx=\yquant@draw@@idx@content%
   \@tempdima=\yquant@orientation@minus.5\dimexpr\yquant@config@register@sep\relax%
   \dimdef\yquant@draw@multiinit@@min{\yquant@register@get@y{#1}-\@tempdima}%
   \dimdef\yquant@draw@multiinit@@max{\yquant@register@get@y{#2}+\@tempdima}%
   \dimdef\yquant@draw@multiinit@@total{%
      \yquant@draw@multiinit@@max-\yquant@draw@multiinit@@min%
   }%
   \def\pgfdecorationsegmentaspect{0}%
   \let\yquant@register@multi@contiguous=\yquant@draw@multiinit@contiguous%
   \let\pgfdecorationsegmentfromto=\empty%
   #4%
   \edef\pgfdecorationsegmentfromto{\expandafter\@gobble\pgfdecorationsegmentfromto}%
   \yquant@config@operator@multitrue%
   % We need to somehow extract the y radius
   \edef\cmd{%
      \noexpand\path \yquant@coords(\yquant@draw@@x, \the\dimexpr.5\dimexpr%
                         \yquant@draw@multiinit@@min+\yquant@draw@multiinit@@max\relax%
                      \relax)%
         node[/yquant/every operator, \yquant@draw@@style,
              \ifnum\yquant@compat<2 /yquant/every multi label,\fi%
              /yquant/this operator,%
              space radius=\yquant@abs{\the\dimexpr.5\dimexpr\yquant@draw@multiinit@@total\relax\relax},%
              name prefix=, name suffix=, name=yquantbox]%
            {\unexpanded\expandafter{\yquant@draw@@content}};%
      \ifyquant@config@draw@quality%
         \pgfshapeclippath{yquantbox}%
                          {/yquant/every operator, \yquant@draw@@style,%
                           \ifnum\yquant@compat<2 /yquant/every multi label,\fi%
                           /yquant/this operator}%
      \fi%
   }%
   \cmd%
   \yquant@for \i := #1 to #2 {%
      \yquant@circuit@extendwire\i{center}%
   }%
   % no control line (init doesn't allow for those, so just save the no-op), no multi line
   % check for empty name parameter
   \ifstrempty{#5}\relax{%
      \pgfnodealias{\tikz@pp@name{#5}}{yquantbox}%
   }%
   \numdef\yquant@draw@@idx@content{\yquant@draw@@idx@content+1}%
}

\protected\def\yquant@draw@multiinit@contiguous#1#2#3{%
   \edef\yquant@draw@multiinit@@from{%
      \pgfmath@tonumber{\dimexpr%
         \dimexpr\yquant@register@get@y{#1}-\@tempdima-\yquant@draw@multiinit@@min\relax*65536/%
         \dimexpr\yquant@draw@multiinit@@total\relax%
      \relax}%
   }%
   \edef\yquant@draw@multiinit@@to{%
      \pgfmath@tonumber{\dimexpr%
         \dimexpr\yquant@register@get@y{#2}+\@tempdima-\yquant@draw@multiinit@@min\relax*65536/%
         \dimexpr\yquant@draw@multiinit@@total\relax%
      \relax}%
   }%
   \eappto\pgfdecorationsegmentfromto{,%
      \yquant@draw@multiinit@@from-\yquant@draw@multiinit@@to%
   }%
   % We need to decide where to put the brace arch.
   \ifdim\yquant@draw@multiinit@@from pt<.5pt %
      \ifdim\yquant@draw@multiinit@@to pt>.5 pt%
         % This segment covers the true 1/2 position, take it
         \def\pgfdecorationsegmentaspect{.5}%
      \else%
         % We are not there yet, so the end of this segment is the closest we can get to the mid so far
         \edef\pgfdecorationsegmentaspect{\yquant@draw@multiinit@@to}%
      \fi%
   \else%
      % We are already beyond the mid...
      \ifdim\pgfdecorationsegmentaspect pt<.5pt %
         % ...but we did not find an ideal position yet
         \ifdim\dimexpr\yquant@draw@multiinit@@from pt-.5pt\relax<%
            \dimexpr.5pt-\pgfdecorationsegmentaspect pt\relax%
            % this one is closer to the mid than anything found before
            \edef\pgfdecorationsegmentaspect{\yquant@draw@multiinit@from}%
         \fi%
      \fi%
   \fi%
}

\protected\long\def\yquant@draw@output@single#1#2{%
   \let\idx=\yquant@draw@@idx@content%
   \yquant@config@operator@multifalse%
   \edef\cmd{%
      \noexpand\path \yquant@coords(\yquant@circuit@endwires@x, \yquant@register@get@y{#1})%
         node[\ifnum\yquant@compat>1 /yquant/every operator,\fi%
              /yquant/every output,%
              /yquant/every \yquant@register@type@tostring{\yquant@register@get@type{#1}} output,%
              \yquant@draw@@style, /yquant/this operator,%
              name prefix=, name suffix=, name=yquantbox]%
         {\unexpanded\expandafter{\yquant@draw@@content}};%
      \ifyquant@config@draw@quality%
         \ifdefined\yquant@parent%
            \pgfshapeclippath{yquantbox}%
                             {\ifnum\yquant@compat>1 /yquant/every operator,\fi%
                              /yquant/every output,%
                              /yquant/every \yquant@register@type@tostring{\yquant@register@get@type{#1}} output,%
                              \yquant@draw@@style,%
                              /yquant/this operator}%
         \fi%
      \fi%
   }%
   \cmd%
   % only extend for subcircuits
   \ifdefined\yquant@parent%
      \yquant@circuit@extendwire{#1}{center}%
   \fi%
   % check for empty name parameter
   \ifstrempty{#2}\relax{%
      \pgfnodealias{\tikz@pp@name{#2}}{yquantbox}%
   }%
   \numdef\yquant@draw@@idx@content{\yquant@draw@@idx@content+1}%
}

\protected\long\def\yquant@draw@output@multi#1#2#3#4#5{%
   \let\idx=\yquant@draw@@idx@content%
   \@tempdima=\yquant@orientation@minus.5\dimexpr\yquant@config@register@sep\relax%
   \dimdef\yquant@draw@multiinit@@min{\yquant@register@get@y{#1}-\@tempdima}%
   \dimdef\yquant@draw@multiinit@@max{\yquant@register@get@y{#2}+\@tempdima}%
   \dimdef\yquant@draw@multiinit@@total{%
      \yquant@draw@multiinit@@max-\yquant@draw@multiinit@@min%
   }%
   \def\pgfdecorationsegmentaspect{0}%
   \let\yquant@register@multi@contiguous=\yquant@draw@multiinit@contiguous%
   \let\pgfdecorationsegmentfromto=\empty%
   #4%
   \edef\pgfdecorationsegmentfromto{\expandafter\@gobble\pgfdecorationsegmentfromto}%
   \yquant@config@operator@multitrue%
   % We need to somehow extract the y radius
   \edef\cmd{%
      \noexpand\path \yquant@coords(\yquant@circuit@endwires@x, \the\dimexpr.5\dimexpr%
                         \yquant@draw@multiinit@@min+\yquant@draw@multiinit@@max\relax%
                      \relax)%
         node[\ifnum\yquant@compat>1 /yquant/every operator,\fi /yquant/every output,%
              \yquant@draw@@style,%
              \ifnum\yquant@compat<2 /yquant/every multi output,\fi%
              /yquant/this operator,%
              space radius=\yquant@abs{\the\dimexpr.5\dimexpr\yquant@draw@multiinit@@total\relax\relax},%
              name prefix=, name suffix=, name=yquantbox]%
            {\unexpanded\expandafter{\yquant@draw@@content}};%
      \ifyquant@config@draw@quality%
         \ifdefined\yquant@parent%
            \pgfshapeclippath{yquantbox}%
                             {\ifnum\yquant@compat>1 /yquant/every operator,\fi /yquant/every output,%
                              \yquant@draw@@style,
                              \ifnum\yquant@compat<2 /yquant/every multi output,\fi%
                              /yquant/this operator}%
         \fi%
      \fi%
   }%
   \cmd%
   % only extend for subcircuits
   \ifdefined\yquant@parent%
      \yquant@for \i := #1 to #2 {%
         \yquant@circuit@extendwire\i{center}%
      }%
   \fi%
   % check for empty name parameter
   \ifstrempty{#5}\relax{%
      \pgfnodealias{\tikz@pp@name{#5}}{yquantbox}%
   }%
   \numdef\yquant@draw@@idx@content{\yquant@draw@@idx@content+1}%
}

\protected\def\yquant@draw@subcircuit@nodecallback#1{%
   \ifstrequal{#1}{yquantbox}\relax{%
      \listcsxadd{\yquant@prefix draw@subcircuit@nodelist}{#1}%
   }%
}

% #1: id of subcircuit
% #2: name
% #3: 1 if aliasing of the name should be done (remove -0), 0 else
% #4: {width of subcircuit (excluding outer box)}{including outer box}{left margin}
% #5: affected wires
\protected\long\def\yquant@draw@subcircuit@prepare#1#2#3#4#5{%
   \let\idx=\yquant@draw@@idx@content%
   % In order to wrap the inner circuit in a proper box operator and clip outer paths appropriately (which was not possible yet, as we didn't know the exact vertical positions), we first place it within a box.
   % First, we anticipate the macro that is used by our subcircuit to store the node
   % names.
   \edef\yquant@draw@subcircuit@nodelist{yquant@env#1@draw@subcircuit@nodelist}%
   \global\cslet\yquant@draw@subcircuit@nodelist\empty%
   % \pgfinterruptboundingbox, but just for y
   \yquant@begingroup%
      \edef\pgf@interrupt@savemaxy{\the\yquant@pgf@picmaxy}%
      \edef\pgf@interrupt@saveminy{\the\yquant@pgf@picminy}%
      \edef\pgf@interrupt@savemaxx{\the\yquant@pgf@picmaxx}%
      \edef\pgf@interrupt@saveminx{\the\yquant@pgf@picminx}%
      \yquant@pgf@picmaxy=-16000pt %
      \yquant@pgf@picminy=16000pt %
      \yquant@pgf@picmaxx=-16000pt %
      \yquant@pgf@picminx=16000pt %
      \pgf@size@hookedfalse%
      \let\pgf@path@size@hook=\pgfutil@empty%
      % now we must take care of extending all the wires appropriately until the beginning of the box. #4 contains width of this box, #5 contains registers affected by this box. Note that for subcircuits, x extension by the group is disabled.
      \dimdef\newx{%
         \yquant@draw@@x\yquant@orientation@minus%
         .5\dimexpr\@secondofthree#4\relax%
      }%
      \dimen0=\newx%
      \let\yquant@draw@subcircuit@leftpos=\newx% seamless subcircuits: do not add another separation if we are there
      \let\pgfshapeclippathhorzresult=\empty%
      \forlistloop\yquant@draw@move@loop{#5}%
      % but the new positions are not at the beginning of the box, but inside (with possible margin)
      \dimdef\newx{%
         \yquant@draw@@x\yquant@orientation@minus%
         .5\dimexpr\@firstofthree#4\relax\yquant@orientation@minus%
         \@thirdofthree#4%
      }%
      \forlistloop\yquant@draw@group@advance{#5}%
      \setbox\yquant@prepare@subcircuit@box=\hbox{{%
         \let\yquant@draw@subcircuit@wirestart=\newx%
         \let\yquant@parent=\yquant@prefix%
         \def\yquant@prefix{yquant@env#1@}%
         \pgfkeysgetvalue{/tikz/name prefix}\yquant@draw@subcircuit@prevprefix%
         % name mangling
         \ifcase\yquant@config@operator@subcircuit@mangling\relax%
            % prefix or discard: if we have an outer name, use it as the prefix; if not, discard all names
            \ifstrempty{#2}{%
               % we make sure there are no conflicts by prefixing any named nodes in any case.
               \pgfkeys{/tikz/name prefix/.expanded={sub\yquant@prefix-}}%
               \let\pgf@nodecallback=\yquant@draw@subcircuit@nodecallback%
            }{%
               \pgfkeys{/tikz/name prefix/.expanded={\yquant@draw@subcircuit@prevprefix#2-}}%
               \ifnum#3=1 %
                  \let\pgf@nodecallback=\yquant@draw@subcircuit@nodecallback%
               \fi%
            }%
         \or%
            % prefix or transparent: if we have an outer name, use it as a prefix; if not, directly use the outer namespace
            \ifstrempty{#2}\relax{%
               \pgfkeys{/tikz/name prefix/.expanded={\yquant@draw@subcircuit@prevprefix#2-}}%
               \ifnum#3=1 %
                  \let\pgf@nodecallback=\yquant@draw@subcircuit@nodecallback%
               \fi%
            }%
         \or%
            % transparent: all names go directly in the outer namespace
         \or%
            % discard: no name will be visible in the outer namespace whatsoever
            \pgfkeys{/tikz/name prefix/.expanded={sub\yquant@prefix-}}%
            \let\pgf@nodecallback=\yquant@draw@subcircuit@nodecallback%
         \else%
            \PackageError{yquant.sty}{Assertion failure}{Unknown value for name mangling.}%
         \fi%
         \pgfkeys{/yquant/operators/this subcircuit box/.style={}}%
         \edef\yquant@draw@subcircuit@style{%
            /yquant/every operator, \yquant@draw@@style,%
            /yquant/this operator, /yquant/internal/multi main=true,%
         }%
         \expandafter\tikzset\expandafter{\yquant@draw@subcircuit@style}%
         % #18: The styles may have set this subcircuit box again, but we disallow this. The "this" style is only supposed to effect the actual box and nothing else, so it should not permeat in the inner of the circuit. This is what every subcircuit box is for!
         \pgfkeys{/yquant/operators/this subcircuit box/.style={}}%
         \yquant@begingroup%
            \csname\yquant@prefix draw\endcsname%
         \yquant@endgroup%
         % BEGIN_FOLD bounding box
         % Completely empty wires did not yet affect the bounding box
         % Problem: we compare a user-mode TikZ position (register y position) with a system-level pgf position (picture boundary box). If shifts or scalings are in effect, we must first transform the point appropriately. Note that negative scalings may upset our assumption of which is the minimum and which is the maximum! If additionally rotations are in effect, we must even consider those transformations for the leftmost and the rightmost point, which makes all of this far more complicated than it should be. For this reason, we have a shortcut in action in case no nontranslation transformations are present.
         \ifpgf@pt@identity%
            \dimen0=\dimexpr\yquant@register@get@y1+\yquant@pgf@pt@y\relax%
            \ifdim\dimen0>\yquant@pgf@picmaxy %
               \global\yquant@pgf@picmaxy=\dimen0 %
            \fi%
            \dimen0=\dimexpr\yquant@register@get@y{\csname\yquant@prefix registers\endcsname}+\yquant@pgf@pt@y\relax%
            \ifdim\dimen0<\yquant@pgf@picminy %
               \global\yquant@pgf@picminy=\dimen0 %
            \fi%
         \else%
            % first wire, left end
            \pgfpointtransformed{\yquant@pgfqpoint{\dimexpr\yquant@draw@@x-.5\dimexpr\@firstofthree#4\relax\relax}{\yquant@register@get@y1\relax}}%
            \ifdim\yquant@pgf@y>\yquant@pgf@picmaxy %
               \global\yquant@pgf@picmaxy=\yquant@pgf@y %
            \fi%
            \ifdim\yquant@pgf@y<\yquant@pgf@picminy % negative scaling
               \global\yquant@pgf@picminy=\yquant@pgf@y %
            \fi%
            % first wire, right end
            \pgfpointtransformed{\yquant@pgfqpoint{\dimexpr\yquant@draw@@x+.5\dimexpr\@firstofthree#4\relax\relax}{\yquant@register@get@y1\relax}}%
            \ifdim\yquant@pgf@y>\yquant@pgf@picmaxy %
               \global\yquant@pgf@picmaxy=\yquant@pgf@y %
            \fi%
            \ifdim\yquant@pgf@y<\yquant@pgf@picminy % negative scaling
               \global\yquant@pgf@picminy=\yquant@pgf@y %
            \fi%
            % last wire, left end
            \pgfpointtransformed{\yquant@pgfqpoint{\dimexpr\yquant@draw@@x-.5\dimexpr\@firstofthree#4\relax\relax}{\yquant@register@get@y{\csname\yquant@prefix registers\endcsname}\relax}}%
            \ifdim\yquant@pgf@y>\yquant@pgf@picmaxy %
               \global\yquant@pgf@picmaxy=\yquant@pgf@y %
            \fi%
            \ifdim\yquant@pgf@y<\yquant@pgf@picminy % negative scaling
               \global\yquant@pgf@picminy=\yquant@pgf@y %
            \fi%
            % last wire, right end
            \pgfpointtransformed{\yquant@pgfqpoint{\dimexpr\yquant@draw@@x+.5\dimexpr\@firstofthree#4\relax\relax}{\yquant@register@get@y{\csname\yquant@prefix registers\endcsname}\relax}}%
            \ifdim\yquant@pgf@y>\yquant@pgf@picmaxy %
               \global\yquant@pgf@picmaxy=\yquant@pgf@y %
            \fi%
            \ifdim\yquant@pgf@y<\yquant@pgf@picminy % negative scaling
               \global\yquant@pgf@picminy=\yquant@pgf@y %
            \fi%
         \fi%
         % END_FOLD
         \ifnum\yquant@config@operator@subcircuit@mangling<2 %
            % only bother with aliasing if the prefix is present at all
            \ifnum#3=1 %
               \ifstrempty{#2}\relax{%
                  % we must apply the aliasing to all the included nodes. We do this in this group, which still has the callback available, so that the aliased nodes will also be added to the list and can propagate (note that the number of node names is then exponential in the number of nested subcircuits)
                  \pgfkeysgetvalue{/tikz/name prefix}\yquant@draw@subcircuit@alias@prefix%
                  \expandafter\protected\expandafter\edef\expandafter%
                     \yquant@draw@subcircuit@alias@do\expandafter##\expandafter1\yquant@draw@subcircuit@alias@prefix##2\relax##3\relax{%
                     \noexpand\ifstrempty{##1}{%
                        \noexpand\pgfnodealias{\yquant@draw@subcircuit@prevprefix\yquant@draw@subcircuit@alias@removezero#2-##2}{\yquant@draw@subcircuit@alias@prefix##2}%
                        \noexpand\@gobble% there is an additional \relax
                     }\relax%
                  }%
                  \expandafter\forlistcsloop\expandafter%
                     {\expandafter\yquant@draw@subcircuit@alias\expandafter{%
                        \yquant@draw@subcircuit@alias@prefix}%
                     }\yquant@draw@subcircuit@nodelist%
               }%
            \fi%
         \fi%
      }}%
      \ifpgf@pt@identity%
         \dimen0=\dimexpr.5\yquant@pgf@picminy+.5\yquant@pgf@picmaxy-\yquant@pgf@pt@y\relax%
         \dimen2=\dimexpr\yquant@pgf@picmaxy-\yquant@pgf@picminy\relax%
      \else%
         \begingroup%
            \ifdim\pgf@pt@ab pt=0pt% may also be reflection
               % no rotations, not too hard
               \pgftransforminvert%
               \pgfpointtransformed{\pgfqpoint{\pgf@picminx}{\pgf@picminy}}%
               \@tempdima=\yquant@pgf@y%
               \pgfpointtransformed{\pgfqpoint{\pgf@picmaxx}{\pgf@picmaxy}}%
               \dimen2=\dimexpr\yquant@pgf@y-\@tempdima\relax%
               \ifdim\dimen2<0pt % reflections
                  \dimen2=-\dimen2 %
               \fi%
               \dimen0=\dimexpr.5\dimexpr\@tempdima+\yquant@pgf@y\relax\relax%
            \else%
               % We must figure out the size manually. pgf@picminy/pgf@picmaxy are not trustworthy, as they describe the height over the outer unrotated rectangle that completely covers the rotated one.
               % Let a be the true width of the rotated rectangle and b its height. We are looking for b.
               % If alpha is the rotation angle, the outer rectangle has height Y
               %    Y = a sin alpha + b cos alpha
               % and width
               %    X = a cos alpha + b sin alpha
               % so that b = (Y cos alpha - X sin alpha) / (cos^2 alpha - sin^2 alpha)
               % In the transformation matrix, we already have - if there's no scaling -
               %    sin alpha = \pgf@pt@ab, cos alpha = \pgf@pt@aa.
               % And if there's scaling, these entries are scaled, so in total we divide by the scaling exactly once, which is the inversion, as it should be.
               \@tempdima=\dimexpr\pgf@picmaxx-\pgf@picminx\relax% \@tempdima is X
               \@tempdimb=\dimexpr\pgf@picmaxy-\pgf@picminy\relax% \@tempdimb is Y
               \@tempdimc=\dimexpr\pgf@pt@aa\dimexpr\pgf@pt@aa pt\relax-%
                                  \pgf@pt@ab\dimexpr\pgf@pt@ab pt\relax\relax% denominator
               \dimen\ifyquanthorz24=\dimexpr\dimexpr\pgf@pt@aa\@tempdimb-\pgf@pt@ab\@tempdima\relax*65536/%
                                             \@tempdimc\relax%
               \dimen\ifyquanthorz42=\dimexpr\dimexpr\pgf@pt@aa\@tempdima-\pgf@pt@ab\@tempdimb\relax*65536/%
                                             \@tempdimc\relax%
               % The y position of the middle point is simply .5\pgf@picminy+.5\pgf@picmaxy in the unrotated system. Hence, we have to calculate the full point (x, y) and rotate it backwards:
               \ifyquanthorz{%
                  % y cos alpha - x sin alpha
                  \dimen0=.5\dimexpr\pgf@pt@aa\dimexpr\pgf@picminy+\pgf@picmaxy-2\pgf@pt@y\relax-\pgf@pt@ab\dimexpr\pgf@picminx+\pgf@picmaxx-2\pgf@pt@x\relax\relax%
               }{%
               % x cos alpha + y sin alpha
                  \dimen0=.5\dimexpr\pgf@pt@aa\dimexpr\pgf@picminx+\pgf@picmaxx-2\pgf@pt@x\relax+\pgf@pt@ab\dimexpr\pgf@picminy+\pgf@picmaxy-2\pgf@pt@y\relax\relax%
               }%
               \ifdim\dimen2<0pt % reflections
                  \dimen2=-\dimen2 %
               \fi%
            \fi%
            \edef\cmd{%
               \endgroup%
               \dimen0=\the\dimen0 %
               \dimen2=\the\dimen2 %
            }%
         \cmd%
      \fi%
      % now, due to cross-subcircuit alignments of enclosing rectangles, it might be that our computed width is far larger than the one we would expect
      \dimen4=\yquant@orientation@minus\maxdimen %
      \forlistloop\yquant@draw@subcircuit@remeasure{#5}%
      \advance \dimen4 by \yquant@orientation@plus\yquant@circuit@endwires@operatorsep\space%
      \gundef\yquant@circuit@endwires@operatorsep%
      \dimen6=\@firstofthree#4 %
      \dimdef\delta{%
         \dimen4%
         -\yquant@draw@@x%
         \yquant@orientation@minus.5\dimen6%
      }%
      \ifdim\dimexpr\delta\yquant@orientation@minus100sp\relax\ifyquanthorz><0pt %
         % we simply increase our size - but this cannot help for the centering if there are multiple subcircuits or a conditional line
         \dimdef\yquant@draw@@x{\yquant@draw@@x+.5\dimexpr\delta\relax}%
         \dimdef\yquant@draw@@width{\yquant@draw@@width\yquant@orientation@plus\delta}%
         \advance \dimen6 by \yquant@orientation@plus\delta\space%
      \fi%
      \edef\cmd{%
         \noexpand\path \yquant@coords(\yquant@draw@@x, \the\dimen0)%
            node[/yquant/every operator, \yquant@draw@@style,%
                 /yquant/operators/every subcircuit box, /yquant/this operator,%
                 /yquant/operators/this subcircuit box,%
                 /yquant/internal/multi main=true,%
                 name prefix=, name suffix=, name=yquantbox]%
            {\ifyquanthorz{\vbox to \the\dimen2 {\hbox to \the\dimen6 {}}}%
                          {\hbox to \the\dimen2 {\vbox to \the\dimen6 {}}}};%
      }%
      \ifyquant@config@draw@quality%
         \cmd%
      \else%
         % we draw the box on the behindwires layer. In this way, the box can even be filled without overshadowing the wires. However, this implies that the frame of the box will also be drawn behind the wires. If the color of the frame is different from the color of the wires, this may not be desired. In this case, there's no alternative to the quality layout.
         \yquant@begingroup%
            \let\pgfonlayer@assert@is@active=\relax%
            \pgfonlayer{behindwires}%
               \cmd%
            \endpgfonlayer%
         \yquant@endgroup%
      \fi%
      \unhbox\yquant@prepare@subcircuit@box%
   % \endpgfinterruptboundingbox + increase
      \ifdim\pgf@interrupt@savemaxy>\yquant@pgf@picmaxy%
         \global\yquant@pgf@picmaxy=\pgf@interrupt@savemaxy%
      \fi%
      \ifdim\pgf@interrupt@saveminy<\yquant@pgf@picminy%
         \global\yquant@pgf@picminy=\pgf@interrupt@saveminy%
      \fi%
      \ifdim\pgf@interrupt@savemaxx>\yquant@pgf@picmaxx%
         \global\yquant@pgf@picmaxx=\pgf@interrupt@savemaxx%
      \fi%
      \ifdim\pgf@interrupt@saveminx<\yquant@pgf@picminx%
         \global\yquant@pgf@picminx=\pgf@interrupt@saveminx%
      \fi%
      % Now that the subcircuit is finished, we need advance all the wires
      \dimdef\newx{\yquant@draw@@x\yquant@orientation@plus.5\dimexpr\yquant@draw@@width\relax}%
      \forlistloop\yquant@draw@group@advance{#5}%
   \yquant@endgroup%
   % name mangling
   \ifcase\yquant@config@operator@subcircuit@mangling\relax%
      % prefix or discard
      \ifstrempty{#2}{%
         % However, if the outer node was not named, no access to the inner nodes is desired, so we delete all nodes again.
         \def\do##1{%
            \csgundef{pgf@sh@ns@##1}%
            \csgundef{pgf@sh@np@##1}%
            \csgundef{pgf@sh@nt@##1}%
            \csgundef{pgf@sh@pi@##1}%
            \csgundef{pgf@sh@ma@##1}%
         }%
         \dolistcsloop{\yquant@draw@subcircuit@nodelist}%
         \csgundef\yquant@draw@subcircuit@nodelist%
      }{%
         \ifcsname\yquant@prefix draw@subcircuit@nodelist\endcsname%
            \csxappto{\yquant@prefix draw@subcircuit@nodelist}%
                     {\csname\yquant@draw@subcircuit@nodelist\endcsname}%
         \fi%
      }%
   \or%
      % prefix or transparent: in any case, make everything available for mangling in the outer circuit
      \ifcsname\yquant@prefix draw@subcircuit@nodelist\endcsname%
         \csxappto{\yquant@prefix draw@subcircuit@nodelist}%
                  {\csname\yquant@draw@subcircuit@nodelist\endcsname}%
      \fi%
   \or%
      % transparent: same
      \ifcsname\yquant@prefix draw@subcircuit@nodelist\endcsname%
         \csxappto{\yquant@prefix draw@subcircuit@nodelist}%
                  {\csname\yquant@draw@subcircuit@nodelist\endcsname}%
      \fi%
   \or%
      % discard: remove them all
      \def\do##1{%
         \csgundef{pgf@sh@ns@##1}%
         \csgundef{pgf@sh@np@##1}%
         \csgundef{pgf@sh@nt@##1}%
         \csgundef{pgf@sh@pi@##1}%
         \csgundef{pgf@sh@ma@##1}%
      }%
      \dolistcsloop{\yquant@draw@subcircuit@nodelist}%
      \csgundef\yquant@draw@subcircuit@nodelist%
   \else%
      \PackageError{yquant.sty}{Assertion failure}{Unknown value for name mangling (2).}%
   \fi%
}

\protected\def\yquant@draw@subcircuit@remeasure#1{%
   \ifdim\dimen4\ifyquanthorz<>\yquant@register@get@x{#1} %
      \dimen4=\yquant@register@get@x{#1} %
   \fi%
}

\def\yquant@draw@subcircuit@alias@removezero#1-0{#1}

\def\yquant@draw@subcircuit@alias#1#2{%
   \yquant@draw@subcircuit@alias@do#2\relax#10-\relax\relax%
}

\protected\long\def\yquant@draw@subcircuit@single#1#2#3#4#5{%
   \yquant@config@operator@multifalse%
   \yquant@draw@subcircuit@prepare{#2}{#3}{#4}{#5}{#1}%
   \ifyquant@config@draw@quality%
      \edef\cmd{%
         \pgfshapeclippath{yquantbox}{%
             /yquant/every operator, \yquant@draw@@style,%
             /yquant/operators/every subcircuit box, /yquant/this operator,%
             /yquant/operators/this subcircuit box,%
         }%
      }%
      \cmd%
      % see comment in draw@subcircuit@multi
      \yquant@softpath@extractmaxxat\pgfshapeclippathhorzresult{\yquant@register@get@y{#1}}%
      \let\pgfshapeclippathhorzresult=\empty%
      \yquant@circuit@extendwire{#1}*%
   \else%
      \ifyquanthorz{%
         \pgfpointanchor{yquantbox}{east}%
         \dimen0=\pgf@x%
      }{%
         \pgfpointanchor{yquantbox}{south}%
         \dimen0=\pgf@y%
      }%
      \yquant@circuit@extendwire{#1}*%
   \fi%
   \expandafter\yquant@circuit@extendcontrolline\expandafter%
      {\the\yquant@draw@@currentcontroltype}\yquant@draw@@x{0pt}%
   % check for empty name parameter
   \ifstrempty{#3}\relax{%
      \pgfnodealias{\tikz@pp@name{#3}}{yquantbox}%
   }%
   \numdef\yquant@draw@@idx@content{\yquant@draw@@idx@content+1}%
}

\protected\long\def\yquant@draw@subcircuit@multi#1#2#3#4#5#6#7#8{%
   \yquant@config@operator@multitrue%
   % there is no contiguous slicing for subcircuits, as they may have all kinds of wire operations that can extend beyond the individual slices, let alone ancillas
   \yquant@draw@subcircuit@prepare{#5}{#6}{#7}{#8}{#4}%
   \ifyquant@config@draw@quality%
      \edef\cmd{%
         \pgfshapeclippath{yquantbox}{%
             /yquant/every operator, \yquant@draw@@style,%
             /yquant/operators/every subcircuit box, /yquant/this operator,%
             /yquant/operators/this subcircuit box,%
             /yquant/internal/multi main=true,%
         }%
      }%
      \cmd%
      % install the clippings - but only on wires that are visually between the first and list while not being part of the circuit.
      \let\nonaffectedpgfshapeclippathhorzresult=\pgfshapeclippathhorzresult%
      \yquant@for \i := #1 to #2 {%
         \xifinlist{\i}{#4}{%
            % Usually, we always begin with a wire from the center of the operator shape and clip the inner parts away. This can't be done here, as the wire needs to be drawn _inside_ of the outer box operator here. Instead of clipping against the clip path, we extract its maximum x position at the position of the wire (which is an overkill for simple shapes, but allows to specify even more complicated ones) and place the wire at this position without clipping.
            % Note: this works very well for lines joining at perpendicular angles; but if the shape of the box is more fancy, while the position will be calculated correctly, the wire has a rectangular (or rounded, depending on the line cap) shape that is drawn on top of the operator. While \yquant@softpath@extractmaxxat could without much effort determine exactly the segment of the path that corresponds to the rightmost line, we would then have to convert this single line into a closed path that fills the linewidth and clip against it to get proper joiners. Since most likely, no-one will ever need this, we don't do it. But file a feature request if desired.
            \yquant@softpath@extractmaxxat\nonaffectedpgfshapeclippathhorzresult%
                                          {\yquant@register@get@y\i}%
            \let\pgfshapeclippathhorzresult=\empty%
            \yquant@circuit@extendwire\i*%
         }{%
            \let\pgfshapeclippathhorzresult=\nonaffectedpgfshapeclippathhorzresult%
            \yquant@circuit@extendwire\i{center}%
         }%
      }%
   \else%
      % we don't fill subcircuits, so the nonaffected wires will be drawn through (we deliberatly fill the subcircuit on the behindwires layer, so that the wires within a subcircuit will be visible despite the filling; but we cannot differentiate between "good" internal and "bad" nonaffected wires). If such a constellation arises, better use the quality layout
      \ifyquanthorz{%
         \pgfpointanchor{yquantbox}{east}%
         \dimen0=\pgf@x%
      }{%
         \pgfpointanchor{yquantbox}{south}%
         \dimen0=\pgf@y%
      }%
      \yquant@for \i := #1 to #2 {%
         \xifinlist{\i}{#4}{%
            \yquant@circuit@extendwire\i*%
         }{%
            \yquant@circuit@extendwire\i{center}%
         }%
      }%
   \fi%
   \expandafter\yquant@circuit@extendcontrolline\expandafter%
      {\the\yquant@draw@@currentcontroltype}\yquant@draw@@x{0pt}%
   \ifstrempty{#6}\relax{%
      \pgfnodealias{\tikz@pp@name{#6}}{yquantbox}%
   }%
   \numdef\yquant@draw@@idx@content{\yquant@draw@@idx@content+1}%
}

\protected\def\yquant@draw@control#1#2#3{%
   \edef\cmd{%
      \noexpand\path \yquant@coords(\yquant@draw@@x, \yquant@register@get@y{#2})%
         node[/yquant/every control, /yquant/every #1 control, /yquant/this control,%
              name prefix=, name suffix=, name=yquantbox]%
         {};%
      \ifyquant@config@draw@quality%
         \pgfshapeclippath{yquantbox}%
                          {/yquant/every control, /yquant/every #1 control,%
                           /yquant/this control}%
      \fi%
   }%
   \cmd%
   \yquant@circuit@extendwire{#2}{center}%
   \yquant@draw@@currentcontroltype=\yquant@register@get@type{#2}\relax%
   \expandafter\yquant@circuit@extendcontrolline\expandafter%
      {\the\yquant@draw@@currentcontroltype}\yquant@draw@@x{0pt}%
   % check for empty name parameter
   \ifstrempty{#3}\relax{%
      \pgfnodealias{\tikz@pp@name{#3}}{yquantbox}%
   }%
}

\protected\def\yquant@draw@pcontrol#1#2{%
   \let\idx=\yquant@draw@@idx@pcontrol%
   \yquant@draw@control{positive}{#1}{#2}%
   \numdef\yquant@draw@@idx@pcontrol{\yquant@draw@@idx@pcontrol+1}%
}

\protected\def\yquant@draw@ncontrol#1#2{%
   \let\idx=\yquant@draw@@idx@ncontrol%
   \yquant@draw@control{negative}{#1}{#2}%
   \numdef\yquant@draw@@idx@ncontrol{\yquant@draw@@idx@ncontrol+1}%
}

\protected\long\def\yquant@draw@onwires#1{%
   \yquant@begingroup%
      \let\pgfonlayer@assert@is@active=\relax%
      \pgfonlayer{wires}%
         #1%
      \endpgfonlayer%
   \yquant@endgroup%
}

\protected\def\yquant@draw@cline{%
   \ifyquant@config@draw@quality%
      \pgfscope%
         % install the clipping
         \pgfsyssoftpath@setcurrentpath\yquant@circuit@extendcontrolline@clip%
         % and invert it. It is sufficient to cover the current bounding box, as the wire will be drawn between existing operators.
         \ifyquantdebug%
            \pgfsetfillcolor{teal}%
            \pgfsetfillopacity{.3}%
            \pgfusepathqfill%
         \else%
            \begingroup%
               \pgf@relevantforpicturesizefalse%
               \pgftransformreset%
               \pgfpathrectangle%
                  {\yquant@pgfqpoint{-.5\maxdimen}{-.5\maxdimen}}%
                  {\yquant@pgfqpoint{\maxdimen}{\maxdimen}}%
               \pgfseteorule% even-odd to properly invert the clipping
               \pgfusepathqclip%
            \endgroup%
         \fi%
         \edef\cmd{%
            \noexpand\path[/yquant/every control line]%
               \yquant@circuit@extendcontrolline@cmd;%
         }%
         \cmd%
      \endpgfscope%
   \else%
      \yquant@draw@onwires{%
         \edef\cmd{%
            \noexpand\path[/yquant/every control line]%
               \yquant@circuit@extendcontrolline@cmd;%
         }%
         \cmd%
      }%
   \fi%
}

\protected\def\yquant@draw@mline@prep{%
   \eappto\yquant@circuit@extendmultiline@total{%
      \yquant@draw@mline%
         {\unexpanded\expandafter{\yquant@circuit@extendmultiline@clip}}%
         {\yquant@circuit@extendmultiline@cmd}%
   }%
}

\protected\def\yquant@draw@mline#1#2{%
   \ifyquant@config@draw@quality%
      \pgfscope%
         % install the clipping
         \def\pgfsyssoftpath@thepath{#1}%
         \pgfsyssoftpath@setcurrentpath\pgfsyssoftpath@thepath%
         % and invert it. It is sufficient to cover the current bounding box, as the wire will be drawn between existing operators.
         \ifyquantdebug%
            \pgfsetfillcolor{teal}%
            \pgfsetfillopacity{.3}%
            \pgfusepathqfill%
         \else%
            \begingroup%
               \pgf@relevantforpicturesizefalse%
               \pgftransformreset%
               \pgfpathrectangle%
                  {\yquant@pgfqpoint{-.5\maxdimen}{-.5\maxdimen}}%
                  {\yquant@pgfqpoint{\maxdimen}{\maxdimen}}%
               \pgfseteorule% even-odd to properly invert the clipping
               \pgfusepathqclip%
            \endgroup%
         \fi%
         \path[/yquant/every multi line] #2;%
      \endpgfscope%
   \else%
      \yquant@draw@onwires{%
         \path[/yquant/every multi line] #2;%
      }%
   \fi%
}

\protected\def\yquant@draw@alias#1{%
   \pgfnodealias{\tikz@pp@name{#1}}{\tikz@pp@name{#1-0}}%
}

\protected\def\yquant@draw@alias@ctrl#1#2{%
   \pgfnodealias{\tikz@pp@name{#1-#2}}{\tikz@pp@name{#1-#20}}%
}

\protected\def\yquant@draw@wire#1#2{%
   \yquant@begingroup%
      \yquant@register@get@typeywire{#1}\wiretype\wireypos\wirelast%
      \unless\ifnum\wiretype=\yquant@register@types@nobit%
         \edef\wirexprevpos{\expandafter\@firstoffour\wirelast}%
         \ifx0#2%
            \edef\wirexpos{\expandafter\@secondoffour\wirelast}%
         \else%
            \let\wirexpos=\yquant@circuit@endwires@x%
         \fi%
         \ifdim\yquant@orientation@plus\wirexpos>\yquant@orientation@plus\wirexprevpos %
            \edef\wirestyle{\noexpand\tikzset{%
               /yquant/this wire/.style={%
                  /yquant/every wire,%
                  /yquant/every \yquant@register@type@tostring\wiretype\space wire,%
                     \yquant@register@get@style{#1}%
               }, /yquant/this wire%
            }}%
            \wirestyle%
            \ifyquant@config@draw@quality%
               % load all clippings
               \edef\wireclipping{%
                  \unexpanded\expandafter\expandafter\expandafter{%
                     \expandafter\@thirdandfourthoffour\wirelast%
                  }%
               }%
               \pgfscope%
                  \unless\ifx\wireclipping\empty% may happen if the style is altered to be invisible, though the type is not none
                     % install the clipping
                     \pgfsyssoftpath@setcurrentpath\wireclipping%
                     % invert the clipping
                     \ifyquantdebug%
                        \pgfsetfillcolor{orange}%
                        \pgfsetfillopacity{.3}%
                        \pgfusepathqfill%
                     \else%
                        \begingroup%
                           \pgf@relevantforpicturesizefalse%
                           \pgftransformreset%
                           % everything serves as clipping inversion, which smells like a rectangle from (-\maxdimen, -\maxdimen) to (\maxdimen, \maxdimen). However, this is translated into a base point plus a direction vector, and since neither is allowed to exceed \maxdimen, we must just hope that the circuit is never drawn outside of .5\maxdimen - which is a fair assumption.
                           \pgfpathrectangle%
                              {\yquant@pgfqpoint{-.5\maxdimen}{-.5\maxdimen}}%
                              {\yquant@pgfqpoint{\maxdimen}{\maxdimen}}%
                        \endgroup%
                        \pgfseteorule% even-odd to properly invert the clipping
                        \pgfusepathqclip%
                     \fi%
                  \fi%
                  % the clip inversion is left to the drawing commands (clip two \pgflinewidth more to avoid renderer artifacts)
                  \csname yquant@draw@wire@\wiretype\endcsname{#1}%
               \endpgfscope%
            \else%
               \yquant@draw@onwires{%
                  \csname yquant@draw@wire@\wiretype\endcsname{#1}%
               }%
            \fi%
         \fi%
      \fi%
   \yquant@endgroup%
}

% quantum wire
\protected\csdef{yquant@draw@wire@\yquant@register@types@qubit}#1{%
   \edef\cmd{%
      \noexpand\path [/yquant/this wire]%
         \yquant@coords(\wirexprevpos,\wireypos) -- \yquant@coords(\wirexpos,\wireypos);%
   }%
   \cmd%
}

% classical wire
\protected\csdef{yquant@draw@wire@\yquant@register@types@cbit}#1{%
   \edef\cmd{%
      \noexpand\path [/yquant/this wire]%
         \yquant@coords(\wirexprevpos,\wireypos+2\pgflinewidth) --
         \yquant@coords(\wirexpos,\wireypos+2\pgflinewidth)%
         \yquant@coords(\wirexprevpos,\wireypos-2\pgflinewidth) --
         \yquant@coords(\wirexpos,\wireypos-2\pgflinewidth);%
   }%
   \cmd%
}

% quantum-bundle
\protected\csdef{yquant@draw@wire@\yquant@register@types@qubits}#1{%
   \edef\cmd{%
      \noexpand\path [/yquant/this wire]%
         \yquant@coords(\wirexprevpos,\wireypos+2\pgflinewidth) --
         \yquant@coords(\wirexpos,\wireypos+2\pgflinewidth)%
         \yquant@coords(\wirexprevpos,\wireypos) --
         \yquant@coords(\wirexpos,\wireypos)%
         \yquant@coords(\wirexprevpos,\wireypos-2\pgflinewidth) --
         \yquant@coords(\wirexpos,\wireypos-2\pgflinewidth);%
   }%
   \cmd%
}

\protected\def\yquant@draw@hspace#1#2{%
   \begingroup%
      \yquant@register@get@maxxlist\newx{#1}%
      \dimdef\newx{\newx\yquant@orientation@plus#2}%
      \dimen0=\newx%
      \let\pgfshapeclippathhorzresult=\empty%
      \forlistloop\yquant@draw@move@loop{#1}%
   \endgroup%
}

\protected\def\yquant@draw@move@loop#1{%
   \yquant@register@set@x{#1}\newx%
   \yquant@circuit@extendwire{#1}*%
}

% enclose
\def\yquant@draw@startenclose@initpolygon#1#2{%
   \csxdef{yquant@draw@enclose@#1@left@#2}{\maxdimen}%
   \csxdef{yquant@draw@enclose@#1@right@#2}{-\maxdimen}%
}

\protected\def\yquant@draw@startenclose#1{%
   \listgadd\yquant@circuit@enclose@active{#1}%
   \ifcase\csname yquant@circuit@enclose@#1@shape\endcsname\space%
      % rectangle: we have to keep track of the extremal left and right positions globally
      \csxdef{yquant@draw@enclose@#1@left}{\maxdimen}%
      \csxdef{yquant@draw@enclose@#1@right}{-\maxdimen}%
      % we have to align all the involved registers. However, subcircuits that are not yet there should be ignored.
      \let\yquant@circuit@enclose@align@list=\empty%
      \expandafter\ifx\csname yquant@circuit@enclose@#1@involved\endcsname\yquant@circuit@enclose@allinvolved%
         \forlistcsloop\yquant@circuit@enclose@alignall{yquant@circuit@enclose@#1@hierarchy}%
         \csgundef{yquant@circuit@enclose@#1@hierarchy}%
      \else%
         \forlistcsloop\yquant@circuit@enclose@align{yquant@circuit@enclose@#1@involved}%
      \fi%
      \expandafter\yquant@draw@enclose@align\expandafter{\yquant@circuit@enclose@align@list}%
      \undef\yquant@circuit@enclose@align@list%
   \or%
      % polygon: we have to keep track of the extremal left and right positions per register
      \forlistcsloop{\yquant@draw@startenclose@initpolygon{#1}}{yquant@circuit@enclose@#1@involved}%
   \else%
      \PackageError{yquant.sty}{Internal error}{Unknown enclose shape type.}%
   \fi%
}

\protected\def\yquant@draw@enclose@measure@anyrelevant#1#2{%
   \ifyquant\ifcsname yquant@circuit@enclose@#1@extentT@\yquant@register@resolvetocolon{#2}\endcsname\fi{%
      \let\next=\@firstofone%
      \listbreak%
   }\relax%
}

\protected\def\yquant@draw@enclose@measure@register#1#2{%
   \edef\resolution{\yquant@register@resolvetocolon{#2}}%
   \ifcsname yquant@circuit@enclose@#1@extentT@\resolution\endcsname%
      \ifcsname yquant@draw@enclose@#1@left@\resolution\endcsname%
         \ifdim\csname yquant@draw@enclose@#1@left@\resolution\endcsname>\leftpos\space%
            \global\cslet{yquant@draw@enclose@#1@left@\resolution}\leftpos%
         \fi%
         \ifdim\csname yquant@draw@enclose@#1@right@\resolution\endcsname<\rightpos\space%
            \global\cslet{yquant@draw@enclose@#1@right@\resolution}\rightpos%
         \fi%
      \else%
         \global\cslet{yquant@draw@enclose@#1@left@\resolution}\leftpos%
         \global\cslet{yquant@draw@enclose@#1@right@\resolution}\rightpos%
      \fi%
   \fi%
}

\protected\def\yquant@draw@enclose@measure#1#2{%
   \begingroup%
      \ifcase\csname yquant@circuit@enclose@#2@shape\endcsname\space%
         % is any of the registers in the target list relevant for our enclose?
         \expandafter\ifx\csname yquant@circuit@enclose@#2@involved\endcsname\yquant@circuit@enclose@allinvolved%
            \let\next=\@firstofone%
         \else%
            \let\next=\@gobble%
            \forlistloop{\yquant@draw@enclose@measure@anyrelevant{#2}}{#1}%
         \fi%
         \next{%
            \edef\width{\unless\ifyquant@config@operator@position@rightalign.5\fi\dimexpr\yquant@draw@@width\relax}%
            \ifdim\csname yquant@draw@enclose@#2@left\endcsname>\dimexpr\yquant@draw@@x-\width\relax%
               \csdimgdef{yquant@draw@enclose@#2@left}{\yquant@draw@@x-\width}%
            \fi%
            \ifdim\csname yquant@draw@enclose@#2@right\endcsname<\dimexpr\yquant@draw@@x+\width\relax%
               \csdimgdef{yquant@draw@enclose@#2@right}{\yquant@draw@@x+\width}%
            \fi%
         }%
      \or%
         \edef\width{\unless\ifyquant@config@operator@position@rightalign.5\fi\dimexpr\yquant@draw@@width\relax}%
         \dimdef\leftpos{\yquant@draw@@x-\width}%
         \dimdef\rightpos{\yquant@draw@@x+\width}%
         \forlistloop{\yquant@draw@enclose@measure@register{#2}}{#1}%
      \else%
         \PackageError{yquant.sty}{Internal error}{Unknown enclose shape type (2).}%
      \fi%
   \endgroup%
}

\def\yquant@draw@enclose@sort#1#2{%
   \ifyquant\ifdim\yquant@orientation@minus\yquant@register@colonsplit\yquant@register@peek@y{#2}>%
                  \yquant@orientation@minus\yquant@register@colonsplit\yquant@register@peek@y{#1}\space\fi%
}

\protected\def\yquant@draw@enclose@calctop#1#2{%
   \dimdef\enclosetop{%
      \yquant@register@colonsplitcs\yquant@register@peek@y{yquant@sort@item#2}%
      \yquant@orientation@plus%
      \csname yquant@circuit@enclose@#1@extentT@\csname yquant@sort@item#2\endcsname\endcsname%
      \yquant@orientation@plus%
      \dimen6%
   }%
}

\protected\def\yquant@draw@enclose@calcbottom#1#2{%
   \dimdef\enclosebottom{%
      \yquant@register@colonsplitcs\yquant@register@peek@y{yquant@sort@item#2}%
      \yquant@orientation@minus%
      \csname yquant@circuit@enclose@#1@extentB@\csname yquant@sort@item#2\endcsname\endcsname%
      \yquant@orientation@minus%
      \dimen6%
   }%
}

\protected\def\yquant@draw@enclose@calcleft#1#2{%
   \ifdim\csname yquant@draw@enclose@#1@left@\csname yquant@sort@item#2\endcsname\endcsname=\maxdimen%
      \PackageError{yquant.sty}{Partially empty enclose}{Polygon \string\enclose\space contained a register with no gates in the region.}%
   \fi%
   \dimdef\encloseleft{%
      \csname yquant@draw@enclose@#1@left@\csname yquant@sort@item#2\endcsname\endcsname%
      -\dimen4%
   }%
}

\protected\def\yquant@draw@enclose@calcright#1#2{%
   \dimdef\encloseright{%
      \csname yquant@draw@enclose@#1@right@\csname yquant@sort@item#2\endcsname\endcsname%
      +\dimen4%
   }%
}

\protected\def\yquant@draw@stopenclose#1{%
   % finally, we have to draw something.
   \begingroup%
      \begingroup%
         % figure out the separation
         % If the quotes library is loaded, activate it. (else, this is by default \relax)
         \let\yquant@config@enclose@brace=\@gobbletwo%
         \tikz@enable@node@quotes%
         \tikzset{/yquant/enclose/every enclose}%
         \expandafter\tikzset\expandafter{\unexpanded\expandafter\expandafter\expandafter{\csname yquant@circuit@enclose@#1@style\endcsname}}%
         \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/pgf/inner xsep}}%
         \pgfmathsetlength\pgf@y{\pgfkeysvalueof{/pgf/inner ysep}}%
         \edef\setsep{%
            \endgroup%
            \noexpand\dimen\ifyquanthorz46=\the\pgf@x\space%
            \noexpand\dimen\ifyquanthorz64=\the\pgf@y\space%
         }%
         \setsep%
      \expandafter\ifx\csname yquant@circuit@enclose@#1@involved\endcsname\yquant@circuit@enclose@allinvolved%
         \def\i{1}%
         \csdef{yquant@sort@item0}{1:1}%
         \csedef{yquant@sort@item1}{1:\csname yquant@env1@registers\endcsname}%
         \def\yquant@sort@count{2}%
      \else%
         % First, we'll have to sort the involved registers according to their y position.
         \yquant@sort@clear%
         \expandafter\yquant@sort@addlist\csname yquant@circuit@enclose@#1@involved\endcsname%
         \yquant@sort\yquant@draw@enclose@sort%
         % Now prepare all the points in our rectangle/polygon (we always use the polygon shape for consistency)
         \numdef\i{\yquant@sort@count-1}%
      \fi%
      \yquant@draw@enclose@calcbottom{#1}\i%
      \dimen0=\enclosebottom\space% store the bottom-most position
      \ifcase\csname yquant@circuit@enclose@#1@shape\endcsname\space%
         % If we just need a rectangle, the remaining extracting steps are simple, just look at the extremal parts.
         \ifdim\csname yquant@draw@enclose@#1@left\endcsname=\maxdimen%
            \PackageError{yquant.sty}{Empty enclose}{No gates contained in \string\enclose\space region.}%
         \fi%
         \dimdef\encloseleft{\csname yquant@draw@enclose@#1@left\endcsname-\dimen4}%
         \dimen2=\encloseleft\space%
         \dimdef\encloseright{\csname yquant@draw@enclose@#1@right\endcsname+\dimen4}%
         \yquant@draw@enclose@calctop{#1}0%
         \edef\points{%
            \yquant@coords(\encloseleft,\enclosebottom)%
            \yquant@coords(\encloseright,\enclosebottom)%
            \yquant@coords(\encloseright,\enclosetop)%
            \yquant@coords(\encloseleft,\enclosetop)%
         }%
         \expandafter\ifx\csname yquant@circuit@enclose@#1@involved\endcsname\yquant@circuit@enclose@allinvolved%
            \csgundef{yquant@circuit@enclose@#1@extentT@1:1}%
            \csgundef{yquant@circuit@enclose@#1@extentB@1:\csname yquant@env1@registers\endcsname}%
         \else%
            \forlistcsloop{\yquant@draw@enclose@cleanup@rectangle{#1}}{yquant@circuit@enclose@#1@involved}%
         \fi%
         \csgundef{yquant@draw@enclose@#1@left}%
         \csgundef{yquant@draw@enclose@#1@right}%
      \or%
         % If we need a polygon, we have to collect all the necessary parts. Let's start with the bottom line:
         \yquant@draw@enclose@calcleft{#1}\i%
         \yquant@draw@enclose@calcright{#1}\i%
         \edef\points{%
            \yquant@coords(\encloseleft,\enclosebottom)%
            \yquant@coords(\encloseright,\enclosebottom)%
         }%
         % Then we go up at the right
         \yquant@fordown \i := \numexpr\i-1\relax downto 0 {%
            % first we calculate the next right position
            \let\encloseoldright=\encloseright%
            \yquant@draw@enclose@calcright{#1}\i%
            \unless\ifdim\encloseoldright=\encloseright\space% only if the right position changes, a new path segment follows
               \yquant@draw@enclose@calctop{#1}{\the\numexpr\i+1\relax}%
               \yquant@draw@enclose@calcbottom{#1}\i%
               \dimdef\midpoint{.5\dimexpr\enclosetop\relax+.5\dimexpr\enclosebottom\relax}% to prevent overflow
               \eappto\points{%
                  \yquant@coords(\encloseoldright,\midpoint)%
                  \yquant@coords(\encloseright,\midpoint)%
               }%
            \fi%
         }%
         % Then there's the topmost register, some part of the right line might still be missing
         \yquant@draw@enclose@calctop{#1}0%
         \yquant@draw@enclose@calcleft{#1}0%
         \eappto\points{%
            \yquant@coords(\encloseright,\enclosetop)%
            \yquant@coords(\encloseleft,\enclosetop)%
         }%
         \dimen2=\encloseleft\space% figure out the leftmost position
         % Let's go down at the left
         \yquant@for \i := 1 to \numexpr\yquant@sort@count-1\relax {%
            \let\encloseoldleft=\encloseleft%
            \yquant@draw@enclose@calcleft{#1}\i%
            \unless\ifdim\encloseoldleft=\encloseleft\space%
               \yquant@draw@enclose@calcbottom{#1}{\the\numexpr\i-1\relax}%
               \yquant@draw@enclose@calctop{#1}\i%
               \dimdef\midpoint{.5\dimexpr\enclosetop\relax+.5\dimexpr\enclosebottom\relax}%
               \eappto\points{%
                  \yquant@coords(\encloseoldleft,\midpoint)%
                  \yquant@coords(\encloseleft,\midpoint)%
               }%
               \ifdim\encloseleft<\dimen2 %
                  \dimen2=\encloseleft\space%
               \fi%
            \fi%
         }%
         \forlistcsloop{\yquant@draw@enclose@cleanup@polygon{#1}}{yquant@circuit@enclose@#1@involved}%
      \else%
         \PackageError{yquant.sty}{Internal error}{Unknown enclose shape type (3).}%
      \fi%
      \xappto\yquant@draw@enclose@drawcommands{%
         \yquant@draw@enclose@handlelayers{#1}{\pgfkeysvalueof{/yquant/enclose/layer}}{\pgfkeysvalueof{/yquant/enclose/label layer}}{%
            \noexpand\node[{shape=yquant-enclose-polygon, /yquant/enclose/shape/polygon/points={\points},
                            /yquant/enclose/every enclose,
                            \unexpanded\expandafter\expandafter\expandafter{\csname yquant@circuit@enclose@#1@style\endcsname},
                            anchor=south \ifyquanthorz{west}{east}, transform shape,
                            /yquant/internal/name unaliased=yquantbox}] at %
               \yquant@coords(\the\dimen2, \the\dimen0) {};
         }%
      }%
   \endgroup%
   \listgremove\yquant@circuit@enclose@active{#1}%
   \csgundef{yquant@circuit@enclose@#1@allowmovelabel}%
   \csgundef{yquant@circuit@enclose@#1@shape}%
   \csgundef{yquant@circuit@enclose@#1@involved}%
}

\protected\def\yquant@draw@enclose@cleanup@rectangle#1#2{%
   \csgundef{yquant@circuit@enclose@#1@extentT@#2}%
   \csgundef{yquant@circuit@enclose@#1@extentB@#2}%
}

\protected\def\yquant@draw@enclose@cleanup@polygon#1#2{%
   \csgundef{yquant@circuit@enclose@#1@extentT@#2}%
   \csgundef{yquant@circuit@enclose@#1@extentB@#2}%
   \csgundef{yquant@circuit@enclose@#1@left@#2}%
   \csgundef{yquant@circuit@enclose@#1@right@#2}%
}

\protected\def\yquant@draw@enclose@align#1{%
   % this is an align that surpasses subcircuits - #1 is a list containing prefixes and ids
   \begingroup%
      \yquant@register@peek@maxxlist\newx{#1}%
      \dimen0=\newx%
      \let\pgfshapeclippathhorzresult=\empty%
      \forlistloop{\yquant@register@colonsplit\yquant@draw@enclose@align@loop}{#1}%
   \endgroup%
}

\protected\def\yquant@draw@enclose@align@loop#1#2{%
   \begingroup%
      \edef\yquant@prefix{yquant@env#1@}%
      \yquant@register@set@x{#2}\newx%
      \yquant@circuit@extendwire{#2}*%
   \endgroup%
}

\protected\long\def\yquant@draw@enclose@handlelayers#1#2#3#4{%
   \def\yquant@config@enclose@braces{0}%
   \yquant@begingroup%
      \ifcsname yquant@circuit@enclose@#1@beamercommand\endcsname%
         \letcs\yquant@draw@enclose@beamercommand{yquant@circuit@enclose@#1@beamercommand}%
      \else%
         \let\yquant@draw@enclose@beamercommand\@firstofone%
      \fi%
      \ifstrequal{#2}{#3}{%
         \ifstrequal{#2}{main}{%
            \yquant@draw@enclose@beamercommand{#4}%
         }{%
            \pgfonlayer{#2}%
               \yquant@draw@enclose@beamercommand{#4}%
            \endpgfonlayer%
         }%
      }{%
         \yquant@begingroup%
            \tikzset{append after command/.code=\relax, prefix after command/.code=\relax}%
            \let\yquant@config@enclose@brace=\@gobbletwo%
            \ifstrequal{#2}{main}{%
               \yquant@draw@enclose@beamercommand{#4}%
            }{%
               \pgfonlayer{#2}%
                  \yquant@draw@enclose@beamercommand{#4}%
               \endpgfonlayer%
            }%
         \yquant@endgroup%
         \edef\drawrest{%
            \noexpand\path[late options={/yquant/internal/name unaliased=yquantbox, /yquant/internal/dename, /yquant/enclose/every enclose,
                                         \unexpanded\expandafter\expandafter\expandafter{\csname yquant@circuit@enclose@#1@style\endcsname}}];%
         }%
         \ifstrequal{#3}{main}{%
            \yquant@draw@enclose@beamercommand{\drawrest}%
         }{%
            \pgfonlayer{#3}%
               \yquant@draw@enclose@beamercommand{\drawrest}%
            \endpgfonlayer%
         }%
      }%
   \yquant@endgroup%
   \csgundef{yquant@circuit@enclose@#1@style}%
}