\long\def\pgf@sh@clippathhorz#1{%
   \csgdef{pgf@sh@cliphorz@\pgf@sm@shape@name}{#1}%
}
\let\pgf@sh@clippath=\pgf@sh@clippathhorz%
\long\def\pgf@sh@clippathvert#1{%
   \csgdef{pgf@sh@clipvert@\pgf@sm@shape@name}{#1}%
}
\def\pgf@sh@inheritclippath[from=#1]{%
   \global\csletcs{pgf@sh@cliphorz@\pgf@sm@shape@name}{pgf@sh@cliphorz@#1}%
   \global\csletcs{pgf@sh@clipvert@\pgf@sm@shape@name}{pgf@sh@clipvert@#1}%
}
\patchcmd%
   \pgfdeclareshape%
   {\let\anchorborder=\pgf@sh@anchorborder}%
   {\let\anchorborder=\pgf@sh@anchorborder%
    \let\clippathhorz=\pgf@sh@clippathhorz%
    \let\clippathvert=\pgf@sh@clippathvert%
    \let\clippath=\pgf@sh@clippath%
    \let\inheritclippath=\pgf@sh@inheritclippath}%
   {}%
   {\PackageError{yquant.sty}%
                 {Failed to patch \string\pgfdeclareshape}%
                 {yquant could not provide a necessary extension to pgf.}}%
% Every shape additionally provides information about how it should clip the wires. The clipping softpath instructions of shape #1 are stored into \pgfshapeclippath[horz|vert]result. The path is drawn with the tikz options #2 in place.
% sync with yquant@config
\protected\def\pgfshapeclippath#1#2{%
   % Ok, check whether #1 is known!
   \ifcsname pgf@sh@ns@#1\endcsname%
      \begin{pgfinterruptpath}%
         \edef\pgfreferencednodename{#1}% for use inside of anchors.
         % install the given style and extract whether we draw a line.
         \let\tikz@mode=\pgfutil@empty%
         \tikzset{every path/.try,#2}%
         \edef\oldpgflinewidth{\the\pgflinewidth}%
         \tikz@mode@drawfalse%
         \tikz@mode%
         \unless\iftikz@mode@draw%
            \pgflinewidth=0pt %
         \fi%
         % MW install special macros
         \csname pgf@sh@ma@#1\endcsname% MW
         % install special coordinates
         \csname pgf@sh@np@#1\endcsname%
         \pgfsettransform{\csname pgf@sh@nt@#1\endcsname}%
         \csname pgf@sh@cliphorz@\csname pgf@sh@ns@#1\endcsname\endcsname%
         \pgfsyssoftpath@getcurrentpath\pgfshapeclippathresult%
         \pgfprocessround{\pgfshapeclippathresult}{\pgfshapeclippathresult}%
         \global\let\pgfshapeclippathhorzresult=\pgfshapeclippathresult%
         \ifcsname pgf@sh@clipvert@\csname pgf@sh@ns@#1\endcsname\endcsname%
            % different clipping in vertical direction
            \pgfsyssoftpath@setcurrentpath\pgfutil@empty%
            \csname pgf@sh@clipvert@\csname pgf@sh@ns@#1\endcsname\endcsname%
            \pgfsyssoftpath@getcurrentpath\pgfshapeclippathresult%
            \pgfprocessround{\pgfshapeclippathresult}{\pgfshapeclippathresult}%
         \fi%
         \global\let\pgfshapeclippathvertresult=\pgfshapeclippathresult%
      \end{pgfinterruptpath}%
   \else%
      \pgferror{No shape named #1 is known}%
      \global\let\pgfshapeclippathhorzresult=\empty%
      \global\let\pgfshapeclippathvertresult=\empty%
   \fi%
}

\pgfdeclareshape{yquant-text}{%
   \inheritsavedanchors[from=rectangle]%
   \foreach \anc in {center, mid, base, north, south, west, mid west, base west, north west, south west, east, mid east, base east, north east, south east} {%
      \inheritanchor[from=rectangle]{\anc}%
   }%
   \inheritanchorborder[from=rectangle]%
   \inheritbackgroundpath[from=rectangle]%
   \clippath{%
      % all pgf temporaries may be overwritten by \pgfpathrectanglecorners
      \begingroup%
         % the outer sep may depend on the line width (though there may not be any line)
         \pgflinewidth=\oldpgflinewidth%
         \global\@tempdima=\pgfkeysvalueof{/pgf/outer xsep} %
         \global\@tempdimb=\pgfkeysvalueof{/pgf/outer ysep} %
      \endgroup%
      \advance\@tempdima by -.5\pgflinewidth%
      \advance\@tempdimb by -.5\pgflinewidth%
      \pgfpathrectanglecorners%
         {\southwest%
          \advance\pgf@x by \@tempdima%
          \advance\pgf@y by \@tempdimb%
         }%
         {\northeast%
          \advance\pgf@x by -\@tempdima%
          \advance\pgf@y by -\@tempdimb%
         }%
   }%
}

\pgfdeclareshape{yquant-rectangle}{%
   % BEGIN_FOLD Saved anchors
   \saveddimen\xradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/x radius}}%
      \ifdim\wd\pgfnodeparttextbox=0pt %
         \pgf@xa=0pt %
      \else%
         \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
      \fi%
      \ifdim\dimexpr.5\wd\pgfnodeparttextbox+\pgf@xa\relax>\pgf@x%
         \pgf@x=\dimexpr.5\wd\pgfnodeparttextbox+\pgf@xa\relax%
      \fi%
   }%
   \saveddimen\yradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/y radius}}%
      \ifdim\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox\relax=0pt %
         \pgf@xa=0pt %
      \else%
         \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}}%
      \fi%
      \@tempdima=\dimexpr.5\ht\pgfnodeparttextbox+.5\dp\pgfnodeparttextbox+\pgf@xa\relax%
      \ifdim\@tempdima>\pgf@x%
         \pgf@x=\@tempdima%
      \fi%
   }%
   \savedanchor\stext{%
      \pgfqpoint%
         {-.5\wd\pgfnodeparttextbox}%
         {-\dimexpr.5\ht\pgfnodeparttextbox-.5\dp\pgfnodeparttextbox\relax}%
   }%
   % END_FOLD
   % BEGIN_FOLD Operator anchors
   \global\cslet{pgf@anchor@yquant-rectangle@center}\pgfpointorigin%
   \anchor{text}{\stext}%
   \anchor{north}%
          {\pgfqpoint{0pt}%
                     {\yradius}}%
   \anchor{north east}%
          {\pgfqpoint{\xradius}%
                     {\yradius}}%
   \anchor{east}%
          {\pgfqpoint{\xradius}%
                     {0pt}}%
   \anchor{south east}%
          {\pgfqpoint{\xradius}%
                     {-\yradius}}%
   \anchor{south}%
          {\pgfqpoint{0pt}%
                     {-\yradius}}%
   \anchor{south west}%
          {\pgfqpoint{-\xradius}%
                     {-\yradius}}%
   \anchor{west}%
          {\pgfqpoint{-\xradius}% \dimexpr not really necessary...
                     {0pt}}%
   \anchor{north west}%
          {\pgfqpoint{-\xradius}%
                     {\yradius}}%
   \anchorborder{%
      \@tempdima=\pgf@x%
      \@tempdimb=\pgf@y%
      \pgfpointborderrectangle{\pgfqpoint{\@tempdima}{\@tempdimb}}%
                              {\pgfqpoint{\xradius}{\yradius}}%
   }%
   % END_FOLD
   % BEGIN_FOLD Path
   \backgroundpath{%
      \pgfpathrectanglecorners%
         {\pgfqpoint{-\xradius}{\yradius}}%
         {\pgfqpoint{\xradius}{-\yradius}}%
   }%
   \clippath{%
      \pgfpathrectanglecorners%
         {\pgfqpoint{-\dimexpr\xradius+.5\pgflinewidth\relax}%
                    {\dimexpr\yradius+.5\pgflinewidth\relax}}%
         {\pgfqpoint{\dimexpr\xradius+.5\pgflinewidth\relax}%
                    {-\dimexpr\yradius+.5\pgflinewidth\relax}}%
   }%
   % END_FOLD
}

\pgfdeclareshape{yquant-circle}{%
   \inheritsavedanchors[from=yquant-rectangle]%
   \foreach \anc in {center, north, east, south, west, text} {%
      \inheritanchor[from=yquant-rectangle]{\anc}%
   }%
   \anchor{north east}%
          {\pgfqpoint{.707107\dimexpr\xradius\relax}%
                     {.707107\dimexpr\yradius\relax}}%
   \anchor{south east}%
          {\pgfqpoint{.707107\dimexpr\xradius\relax}%
                     {-.707107\dimexpr\yradius\relax}}%
   \anchor{south west}%
          {\pgfqpoint{-.707107\dimexpr\xradius\relax}%
                     {-.707107\dimexpr\yradius\relax}}%
   \anchor{north west}%
          {\pgfqpoint{-.707107\dimexpr\xradius\relax}%
                     {.707107\dimexpr\yradius\relax}}%
   \anchorborder{%
      \@tempdima=\pgf@x%
      \@tempdimb=\pgf@y%
      \pgfpointborderellipse{\pgfqpoint{\@tempdima}{\@tempdimb}}%
                            {\pgfqpoint{\xradius}{\yradius}}%
   }%
   \backgroundpath{%
      \pgfpathellipse{\pgfpointorigin}%
                     {\pgfqpoint{\xradius}{0pt}}%
                     {\pgfqpoint{0pt}{\yradius}}%
   }%
   \clippath{%
      \pgfpathellipse{\pgfpointorigin}%
                     {\pgfqpoint{\dimexpr\xradius+.5\pgflinewidth\relax}{0pt}}%
                     {\pgfqpoint{0pt}{\dimexpr\yradius+.5\pgflinewidth\relax}}%
   }%
}

\pgfdeclareshape{yquant-slash}{%
   \saveddimen\xradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/x radius}}%
   }%
   \saveddimen\yradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/y radius}}%
   }%
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-rectangle]{\anc}%
   }%
   \inheritanchorborder[from=yquant-rectangle]%
   \backgroundpath{%
      \pgfpathmoveto{\pgfqpoint{\xradius}{\yradius}}%
      \pgfpathlineto{\pgfqpoint{-\xradius}{-\yradius}}%
   }%
   \clippath{%
      % we need to clip to the line; but this is not possible, we can only clip to the inner of a path. For this reason, calculate the rectangle that represents the line.
      % TODO: for round line endings, this is not a rectangle. Only a problem if the yradius is so short that the slash ends within a wire.
      \ifcsname yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth\endcsname%
         \letcs\tmp{yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth}%
         \dimen2=\expandafter\@firstoftwo\tmp%
         \dimen4=\expandafter\@secondoftwo\tmp%
      \else%
         % cos(arctan(x)) = 1/sqrt(1+x^2)
         % sin(arctan(x)) = x/sqrt(1+x^2)
         \dimen2=\xradius %
         \dimen4=\yradius %
         % if we divide by a dimension, it is internally converted to sp, so we divide by its pt-value and again by 65536. Same for multiplication. etex fuses muldiv to 64bit, so we don't get overflows.
         \dimen0=\dimexpr\dimen4*\dimen4/\dimen2*65536/\dimen2\relax%
         % calculate the sqrt; but \pgfmathsqrt@ expects a real number without dimension suffix. It internally does \expandafter\pgfmath@x#1pt\relax, so just gobble the additional pt.
         \pgfmathsqrt@{\the\dimexpr1pt+\dimen0\relax\@gobbletwo}%
         \pgfmathreciprocal@\pgfmathresult%
         \dimen2=\dimexpr.5\pgflinewidth*\dimexpr\pgfmathresult pt\relax/65536\relax%
         \dimen4=\dimexpr\dimexpr\yradius\relax*\dimen2/\dimexpr\xradius\relax\relax%
         \csxdef{yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth}%
                {{\the\dimen2}{\the\dimen4}}%
      \fi%
      % now we must be careful (#21) - we emulate the line by rectangles, but rectangles are sensitive to the current scaling, while lines are not. So we must undo the scaling, which may be different for x and y direction. Let's hope there is no rotation, we'd get a mix of different scalings...
      \pgftransformationadjustments%
      \dimen2=\dimexpr\pgfhorizontaltransformationadjustment\dimen2\relax%
      \dimen4=\dimexpr\pgfverticaltransformationadjustment\dimen4\relax%
      \pgfpathmoveto{\pgfqpoint{-\dimexpr\xradius+\dimen4\relax}%
                               {-\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius-\dimen4\relax}%
                               {\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius+\dimen4\relax}%
                               {\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius-\dimen4\relax}%
                               {-\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathclose%
   }%
}

\pgfdeclareshape{yquant-swap}{%
   \inheritsavedanchors[from=yquant-slash]%
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-rectangle]{\anc}%
   }%
   \inheritanchorborder[from=yquant-rectangle]%
   \backgroundpath{%
      \pgfpathmoveto{\pgfqpoint{-\xradius}{\yradius}}%
      \pgfpathlineto{\pgfqpoint{\xradius}{-\yradius}}%
      \pgfpathmoveto{\pgfqpoint{\xradius}{\yradius}}%
      \pgfpathlineto{\pgfqpoint{-\xradius}{-\yradius}}%
   }%
   \clippathhorz{%
      % we need to clip to the line; but this is not possible, we can only clip to the inner of a path. For this reason, calculate the rectangle that represents the line.
      % TODO: for round line endings, this is not a rectangle. Only a problem if the yradius is so short that the slash ends within a wire.
      \ifcsname yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth\endcsname%
         \letcs\tmp{yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth}%
         \dimen2=\expandafter\@firstoftwo\tmp%
         \dimen4=\expandafter\@secondoftwo\tmp%
      \else%
         % cos(arctan(x)) = 1/sqrt(1+x^2)
         % sin(arctan(x)) = x/sqrt(1+x^2)
         \dimen2=\xradius %
         \dimen4=\yradius %
         % if we divide by a dimension, it is internally converted to sp, so we divide by its pt-value and again by 65536. Same for multiplication. etex fuses muldiv to 64bit, so we don't get overflows.
         \dimen0=\dimexpr\dimen4*\dimen4/\dimen2*65536/\dimen2\relax%
         % calculate the sqrt; but \pgfmathsqrt@ expects a real number without dimension suffix. It internally does \expandafter\pgfmath@x#1pt\relax, so just gobble the additional pt.
         \pgfmathsqrt@{\the\dimexpr1pt+\dimen0\relax\@gobbletwo}%
         \pgfmathreciprocal@\pgfmathresult%
         \dimen2=\dimexpr.5\pgflinewidth*\dimexpr\pgfmathresult pt\relax/65536\relax%
         \dimen4=\dimexpr\dimexpr\yradius\relax*\dimen2/\dimexpr\xradius\relax\relax%
         \csxdef{yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth}%
                {{\the\dimen2}{\the\dimen4}}%
      \fi%
      % now we must be careful (#21) - we emulate the line by rectangles, but rectangles are sensitive to the current scaling, while lines are not. So we must undo the scaling, which may be different for x and y direction. Let's hope there is no rotation, we'd get a mix of different scalings...
      \pgftransformationadjustments%
      \dimen2=\dimexpr\pgfhorizontaltransformationadjustment\dimen2\relax%
      \dimen4=\dimexpr\pgfverticaltransformationadjustment\dimen4\relax%
      \dimen6=\dimexpr\dimen2*\dimexpr\xradius\relax/\dimexpr\yradius\relax+%
                      \dimen2*\dimexpr\yradius\relax/\dimexpr\xradius\relax\relax%
      \pgfpathmoveto{\pgfqpoint{-\dimexpr\xradius+\dimen4\relax}%
                               {\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius-\dimen4\relax}%
                               {\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius-\dimen4\relax}%
                               {\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius+\dimen4\relax}%
                               {\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimen6}{0pt}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius+\dimen4\relax}%
                               {-\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius-\dimen4\relax}%
                               {-\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius-\dimen4\relax}%
                               {-\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius+\dimen4\relax}%
                               {-\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{-\dimen6}{0pt}}%
      \pgfpathclose%
   }%
   \clippathvert{%
      % we need to clip to the line; but this is not possible, we can only clip to the inner of a path. For this reason, calculate the rectangle that represents the line.
      % TODO: for round line endings, this is not a rectangle. Only a problem if the yradius is so short that the slash ends within a wire.
      \ifcsname yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth\endcsname%
         \letcs\tmp{yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth}%
         \dimen2=\expandafter\@firstoftwo\tmp%
         \dimen4=\expandafter\@secondoftwo\tmp%
      \else%
         % cos(arctan(x)) = 1/sqrt(1+x^2)
         % sin(arctan(x)) = x/sqrt(1+x^2)
         \dimen2=\xradius %
         \dimen4=\yradius %
         % if we divide by a dimension, it is internally converted to sp, so we divide by its pt-value and again by 65536. Same for multiplication. etex fuses muldiv to 64bit, so we don't get overflows.
         \dimen0=\dimexpr\dimen4*\dimen4/\dimen2*65536/\dimen2\relax%
         % calculate the sqrt; but \pgfmathsqrt@ expects a real number without dimension suffix. It internally does \expandafter\pgfmath@x#1pt\relax, so just gobble the additional pt.
         \pgfmathsqrt@{\the\dimexpr1pt+\dimen0\relax\@gobbletwo}%
         \pgfmathreciprocal@\pgfmathresult%
         \dimen2=\dimexpr.5\pgflinewidth*\dimexpr\pgfmathresult pt\relax/65536\relax%
         \dimen4=\dimexpr\dimexpr\yradius\relax*\dimen2/\dimexpr\xradius\relax\relax%
         \csxdef{yquant@math@cache1@\xradius @\yradius @\the\pgflinewidth}%
                {{\the\dimen2}{\the\dimen4}}%
      \fi%
      % now we must be careful (#21) - we emulate the line by rectangles, but rectangles are sensitive to the current scaling, while lines are not. So we must undo the scaling, which may be different for x and y direction. Let's hope there is no rotation, we'd get a mix of different scalings...
      \pgftransformationadjustments%
      \dimen2=\dimexpr\pgfhorizontaltransformationadjustment\dimen2\relax%
      \dimen4=\dimexpr\pgfverticaltransformationadjustment\dimen4\relax%
      \dimen6=\dimexpr\dimen4*\dimexpr\xradius\relax/\dimexpr\yradius\relax+%
                      \dimen4*\dimexpr\yradius\relax/\dimexpr\xradius\relax\relax%
      \pgfpathmoveto{\pgfqpoint{-\dimexpr\xradius+\dimen4\relax}%
                               {\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius-\dimen4\relax}%
                               {\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{0pt}{\dimen6}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius-\dimen4\relax}%
                               {\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius+\dimen4\relax}%
                               {\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius+\dimen4\relax}%
                               {-\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{\dimexpr\xradius-\dimen4\relax}%
                               {-\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{0pt}{-\dimen6}}%
      \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius-\dimen4\relax}%
                               {-\dimexpr\yradius+\dimen2\relax}}%
      \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius+\dimen4\relax}%
                               {-\dimexpr\yradius-\dimen2\relax}}%
      \pgfpathclose%
   }%
}

\pgfdeclareshape{yquant-oplus}{%
   \inheritsavedanchors[from=yquant-slash]%
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-circle]{\anc}%
   }%
   \inheritanchorborder[from=yquant-circle]%
   \backgroundpath{%
      \pgfpathmoveto{\pgfqpoint{0pt}{\yradius}}%
      \pgfpathlineto{\pgfqpoint{0pt}{-\yradius}}%
      \pgfpathmoveto{\pgfqpoint{-\xradius}{0pt}}%
      \pgfpathlineto{\pgfqpoint{\xradius}{0pt}}%
      \pgfpathellipse{\pgfpointorigin}%
                     {\pgfqpoint{\xradius}{0pt}}%
                     {\pgfqpoint{0pt}{\yradius}}%
   }%
   \inheritclippath[from=yquant-circle]%
}

\pgfdeclareshape{yquant-ocross}{%
   \inheritsavedanchors[from=yquant-slash]%
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-circle]{\anc}%
   }%
   \inheritanchorborder[from=yquant-circle]%
   \backgroundpath{%
      \dimen2=.70710678\dimexpr\xradius\relax%
      \dimen4=.70710678\dimexpr\yradius\relax
      \pgfpathmoveto{\pgfqpoint{\dimen2}{\dimen4}}%
      \pgfpathlineto{\pgfqpoint{-\dimen2}{-\dimen4}}%
      \pgfpathmoveto{\pgfqpoint{-\dimen2}{\dimen4}}%
      \pgfpathlineto{\pgfqpoint{\dimen2}{-\dimen4}}%
      \pgfpathellipse{\pgfpointorigin}%
                     {\pgfqpoint{\xradius}{0pt}}%
                     {\pgfqpoint{0pt}{\yradius}}%
   }%
   \inheritclippath[from=yquant-circle]%
}

\pgfdeclareshape{yquant-measure}{%
   \saveddimen\xradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/x radius}}%
      \ifdim.5\wd\pgfnodeparttextbox>\pgf@x%
         \pgf@x=.5\wd\pgfnodeparttextbox%
      \fi%
   }%
   \saveddimen\yradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/y radius}}%
      \ifdim\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox\relax=0pt %
         \ifdim\pgf@x<1.25mm %
            \pgf@x=1.25mm %
         \fi%
      \else%
         \pgf@y=\dimexpr.5\ht\pgfnodeparttextbox+.5\dp\pgfnodeparttextbox+2pt\relax%
         \ifyquant@config@internal@multi@main%
            \advance\pgf@y by 1.25mm %
         \fi%
         \ifdim\pgf@x<\pgf@y%
            \pgf@x=\pgf@y%
         \fi%
      \fi%
   }%
   \savedanchor\stext{%
      \ifyquant@config@internal@multi@main%
         \pgfqpoint%
            {-.5\wd\pgfnodeparttextbox}%
            {\dp\pgfnodeparttextbox}%
      \else%
         \pgfqpoint%
            {-.5\wd\pgfnodeparttextbox}%
            {-\dimexpr.5\ht\pgfnodeparttextbox-.5\dp\pgfnodeparttextbox\relax}%
      \fi%
   }%
   \saveddimen\textheight{%
      \pgf@x=\ht\pgfnodeparttextbox%
   }%
   \savedmacro\main{%
      \ifyquant@config@internal@multi@main%
         \def\main{\noexpand\yquant@config@internal@multi@maintrue}%
      \else%
         \ifdim\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox\relax=0pt%
            \def\main{\noexpand\yquant@config@internal@multi@maintrue}%
         \else%
            \def\main{\noexpand\yquant@config@internal@multi@mainfalse}%
         \fi%
      \fi%
   }%
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-rectangle]{\anc}%
   }%
   \anchor{text}{%
      \stext%
      \main%
      \ifyquant@config@internal@multi@main%
         \pgf@y=\dimexpr-\yradius+1pt+\pgf@y\relax%
      \fi%
   }%
   \inheritanchorborder[from=yquant-rectangle]%
   \inheritbackgroundpath[from=yquant-rectangle]%
   \inheritclippath[from=yquant-rectangle]%
   \beforebackgroundpath{%
      \main%
      % we only draw the meter symbol if this is the main part of a multi-register (or there is no text)
      \ifyquant@config@internal@multi@main%
         % Make sure the meter does not extend beyond the box (we are in a scope here)
         \path [clip]%
            (-\xradius, \yradius) rectangle (\xradius, -\yradius);%
         % The position of the meter symbol depends on the presence of the text. If there is no text, we just vertically center. If there is some text, we shift the symbol upwards from the text until there is no overlap any more.
         \csname pgf@anchor@yquant-measure@text\endcsname%
         \advance\pgf@y by \textheight\relax%
         \ifdim\pgf@y<-1.15mm %
            \@tempdima=-1.15mm %
         \else%
            \@tempdima=\dimexpr\pgf@y+2pt\relax%
         \fi%
         \path [/yquant/operators/every measure meter]%
            (-2.25mm, \@tempdima) arc[start angle=160, end angle=20,%
                                      x radius=2.25mm, y radius=1.4mm]%
            (0, \@tempdima) -- ++(1.6mm, 2.3mm);%
      \fi%
   }%
}

% The dmeter shape is aware of the circuit orientation
\pgfdeclareshape{yquant-dmeter}{%
   \saveddimen\xradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/x radius}}%
      \ifdim\wd\pgfnodeparttextbox=0pt %
         \pgf@xa=0pt %
      \else%
         \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}\ifyquanthorz{+.5mm}{}}%
      \fi%
      \ifdim\dimexpr.5\wd\pgfnodeparttextbox+\pgf@xa\relax>\pgf@x%
         \pgf@x=\dimexpr.5\wd\pgfnodeparttextbox+\pgf@xa\relax%
      \fi%
   }%
   \saveddimen\yradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/y radius}}%
      \ifdim\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox\relax=0pt %
         \pgf@xa=0pt %
      \else%
         \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}\ifyquanthorz{}{+.325mm}}%
      \fi%
      \@tempdima=\dimexpr.5\ht\pgfnodeparttextbox+.5\dp\pgfnodeparttextbox+\pgf@xa\relax%
      \ifdim\@tempdima>\pgf@x%
         \pgf@x=\@tempdima%
      \fi%
   }%
   \savedanchor\stext{%
      \ifyquanthorz{%
         \pgfqpoint%
            {-\dimexpr.5\wd\pgfnodeparttextbox+1mm\relax}%
            {-\dimexpr.5\ht\pgfnodeparttextbox-.5\dp\pgfnodeparttextbox\relax}%
      }{%
         % \pgf@x is still \yradius
         \@tempdima=\pgf@x%
         \pgfqpoint%
            {-\dimexpr.5\wd\pgfnodeparttextbox\relax}%
            {-\dimexpr.5\ht\pgfnodeparttextbox-.5\dp\pgfnodeparttextbox-.75mm\relax}%
      }%
   }%
   \savedmacro\ifhorz{%
      \ifyquanthorz{%
         \def\ifhorz{\noexpand\@firstoftwo}%
      }{%
         \def\ifhorz{\noexpand\@secondoftwo}%
      }%
   }%
   \foreach \anc in {center, north, south, south west, west, north west, text} {%
      \inheritanchor[from=yquant-rectangle]{\anc}%
   }%
   \foreach \anc in {north east, east, south east} {%
      \inheritanchor[from=yquant-circle]{\anc}%
   }%
   \anchorborder{%
      \@tempdima=\pgf@x%
      \@tempdimb=\pgf@y%
      \pgfpointborderrectangle{\pgfqpoint{\@tempdima}{\@tempdimb}}%
                              {\pgfqpoint{\xradius}{\yradius}}%
      % for very distorted shapes, we lose a lot of accuracy in these calculations
      \ifhorz{%
         \ifdim\pgf@x>\dimexpr\xradius-3mm\relax%
            \pgf@xa=\dimexpr\xradius-3mm\relax% shift of the ellipse
            \pgf@xb=\dimexpr\@tempdimb*65536/\@tempdima\relax%
            \pgf@xb=\dimexpr\pgf@xb*\pgf@xb/65536\relax% slope^2
            \pgf@ya=72.86009pt % xradius^2 of ellipse (72.86009pt = 3mm^2/65536)
            \pgf@yb=\yradius\relax%
            \pgf@yb=\dimexpr\pgf@yb*\pgf@yb/65536\relax% yradius^2
            % sqrt(slope^2 * (xradius^2 - shift^2) + yradius^2)
            \pgfmathsqrt@{\the\dimexpr\pgf@xb*\dimexpr\pgf@ya-\pgf@xa*\pgf@xa/65536\relax/65536+%
                                      \pgf@yb\relax\@gobbletwo}%
            % yradius (shift yradius + xradius sqrt(...)) / (slope^2 xradius^2 + yradius^2)
            \pgf@x=\dimexpr\dimexpr\yradius\relax*%
                           \dimexpr\pgf@xa*\dimexpr\yradius\relax/65536+%
                                   9\dimexpr\pgfmathresult pt\relax% 9 = 3mm/65536
                                   \relax/%
                           \dimexpr\pgf@xb*\pgf@ya/65536+\pgf@yb\relax%
                   \relax%
            % y = slope x
            \pgf@y=\dimexpr\@tempdimb*\pgf@x/\@tempdima\relax%
         \fi%
      }{%
         \ifdim\pgf@y<\dimexpr-\yradius+3mm\relax%
            \pgf@xa=\dimexpr-\yradius+3mm\relax% shift of the ellipse
            \pgf@xb=\dimexpr\@tempdima*65536/\@tempdimb\relax%
            \pgf@xb=\dimexpr\pgf@xb*\pgf@xb/65536\relax% 1/slope^2
            \pgf@ya=\xradius\relax% xradius
            \pgf@ya=\dimexpr\pgf@ya*\pgf@ya/65535\relax% xradius^2
            \pgf@yb=72.86009pt % yradius^2 of ellipse (72.86009pt = 3mm^2/65536)
            % sqrt(1/slope^2 * (yradius^2 - shift^2) + xradius^2)
            \pgfmathsqrt@{\the\dimexpr\pgf@xb*\dimexpr\pgf@yb-\pgf@xa*\pgf@xa/65536\relax/65536+%
                                      \pgf@ya\relax\@gobbletwo}%
            % xradius (shift xradius - yradius sqrt(...)) / (1/slope^2 yradius^2 + xradius^2)
            \pgf@y=\dimexpr\dimexpr\xradius\relax*%
                           \dimexpr\pgf@xa*\dimexpr\xradius\relax/65536-%
                                   9\dimexpr\pgfmathresult pt\relax% 9 = 3mm/65536
                                   \relax/%
                           \dimexpr\pgf@xb*\pgf@yb/65536+\pgf@ya\relax%
                   \relax%
            % x = 1/slope y
            \pgf@x=\dimexpr\@tempdima*\pgf@y/\@tempdimb\relax%
         \fi%
      }%
   }%
   \backgroundpath{%
      \ifhorz{%
         \pgfpathmoveto{\pgfqpoint{-\xradius}{\yradius}}%
         \pgfpathlineto{\pgfqpoint{\dimexpr\xradius-3mm\relax}{\yradius}}%
         \pgfpatharc{90}{-90}{3mm and \yradius}%
         \pgfpathlineto{\pgfqpoint{-\xradius}{-\yradius}}%
      }{%
         \pgfpathmoveto{\pgfqpoint{\xradius}{\yradius}}%
         \pgfpathlineto{\pgfqpoint{\xradius}{\dimexpr-\yradius+3mm\relax}}%
         \pgfpatharc{0}{-180}{\xradius and 3mm}%
         \pgfpathlineto{\pgfqpoint{-\xradius}{\yradius}}%
      }%
      \pgfpathclose%
   }%
   \clippath{%
      \ifhorz{%
         \pgfpathmoveto{\pgfqpoint{-\dimexpr\xradius+.5\pgflinewidth\relax}%
                                  {\dimexpr\yradius+.5\pgflinewidth\relax}}%
         \pgfpathlineto{\pgfqpoint{\dimexpr\xradius-3mm\relax}%
                                  {\dimexpr\yradius+.5\pgflinewidth\relax}}%
         \pgfpatharc{90}{-90}{3mm+.5\pgflinewidth and \yradius+.5\pgflinewidth}%
         \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius+.5\pgflinewidth\relax}%
                                  {-\dimexpr\yradius+.5\pgflinewidth\relax}}%
      }{%
         \pgfpathmoveto{\pgfqpoint{\dimexpr\xradius+.5\pgflinewidth\relax}%
                                  {\dimexpr\yradius+.5\pgflinewidth\relax}}%
         \pgfpathlineto{\pgfqpoint{\dimexpr\xradius+.5\pgflinewidth\relax}%
                                  {\dimexpr-\yradius+3mm\relax}}%
         \pgfpatharc{0}{-180}{\xradius+.5\pgflinewidth and 3mm+.5\pgflinewidth}%
         \pgfpathlineto{\pgfqpoint{-\dimexpr\xradius+.5\pgflinewidth\relax}%
                                  {\dimexpr\yradius+.5\pgflinewidth}}%
      }%
      \pgfpathclose%
   }%
}

% The line shape is aware of the circuit orientation (it always extends in space dimension with constant time)
\pgfdeclareshape{yquant-line}{%
   \saveddimen\xradius{%
      % we only need this for the border anchor; the value is automatically correct in the paths.
      \ifyquanthorz{%
         \pgf@x=.5\pgflinewidth%
      }{%
         \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/x radius}+.5*\yquant@config@register@sep}%
      }%
   }%
   \saveddimen\yradius{%
      \ifyquanthorz{%
         \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/y radius}+.5*\yquant@config@register@sep}%
      }{%
         \pgf@x=.5\pgflinewidth%
      }%
   }%
   \savedmacro\ifhorz{%
      \ifyquanthorz{%
         \def\ifhorz{\noexpand\@firstoftwo}%
      }{%
         \def\ifhorz{\noexpand\@secondoftwo}%
      }%
   }%
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-rectangle]{\anc}%
   }%
   \inheritanchorborder[from=yquant-rectangle]%
   \backgroundpath{%
      % manually shorten for the bounding box
      \ifhorz{%
         \pgf@xa=\dimexpr\yradius-\pgf@shorten@end@additional\relax%
         \pgf@ya=\dimexpr\yradius-\pgf@shorten@start@additional\relax%
         \pgf@shorten@end@additional=0pt %
         \pgf@shorten@start@additional=0pt %
         \pgfpathmoveto{\pgfqpoint{0pt}{\pgf@xa}}%
         \pgfpathlineto{\pgfqpoint{0pt}{-\pgf@ya}}%
      }{%
         \pgf@xa=\dimexpr\xradius-\pgf@shorten@end@additional\relax%
         \pgf@ya=\dimexpr\xradius-\pgf@shorten@start@additional\relax%
         \pgf@shorten@end@additional=0pt %
         \pgf@shorten@start@additional=0pt %
         \pgfpathmoveto{\pgfqpoint{\pgf@xa}{0pt}}%
         \pgfpathlineto{\pgfqpoint{-\pgf@ya}{0pt}}%
      }%
   }%
   \clippath{%
      \ifhorz{%
         \@tempdima=\dimexpr\yradius-\pgf@shorten@end@additional\relax%
         \@tempdimb=\dimexpr\yradius-\pgf@shorten@start@additional\relax%
         \pgfpathrectanglecorners%
            {\pgfqpoint{-.5\pgflinewidth}{\@tempdima}}%
            {\pgfqpoint{.5\pgflinewidth}{-\@tempdimb}}%
      }{%
         \@tempdima=\dimexpr\xradius-\pgf@shorten@end@additional\relax%
         \@tempdimb=\dimexpr\xradius-\pgf@shorten@start@additional\relax%
         \pgfpathrectanglecorners%
            {\pgfqpoint{\@tempdima}{-.5\pgflinewidth}}%
            {\pgfqpoint{-\@tempdimb}{.5\pgflinewidth}}%
      }%
   }%
}

% BEGIN_FOLD Gapped brace decoration
\def\pgfdecorationsegmentfromto{0-1}%

\protected\def\yquant@gappedbrace@extract#1-#2\yquant@sep{%
   \dimdef\from{#1\pgfdecoratedremainingdistance}%
   \dimdef\to{#2\pgfdecoratedremainingdistance}%
}

\def\yquant@gappedbrace@shiftloop#1{%
   \yquant@gappedbrace@extract#1\yquant@sep%
   \unless\ifdim\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance<\from %
      \unless\ifdim\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance>\to %
         % be careful about arch positions at the border
         \ifdim\dimexpr\to-\from\relax<2\pgfdecorationsegmentamplitude %
            % The arch is larger than the segment. We do not draw a line to it or an end line and place it in the mid of the segment, even if it is too short (this issues one extra \pgfpathmoveto command, but catching this rare case is not worth the effort).
            \edef\pgfdecorationsegmentaspect{\pgfmath@tonumber{\dimexpr%
               .5\dimexpr\from+\to\relax*65536/\pgfdecoratedremainingdistance%
            \relax}}%
         \else%
            % The segment is large enough to cover the whole arch. But maybe we are too close at the border?
            \ifdim\dimexpr\pgf@xc-\pgfdecorationsegmentamplitude\relax<\from %
               \pgf@xc=\dimexpr\from+\pgfdecorationsegmentamplitude\relax%
            \fi%
            \ifdim\dimexpr\pgf@xc+\pgfdecorationsegmentamplitude\relax>\to %
               \pgf@xc=\dimexpr\to-\pgfdecorationsegmentamplitude\relax%
            \fi%
            \edef\pgfdecorationsegmentaspect{\pgfmath@tonumber{\dimexpr%
               \dimexpr\pgf@xc*65536/\pgfdecoratedremainingdistance%
            \relax}}%
         \fi%
         \expandafter\expandafter\expandafter\listbreak%
      \fi%
   \fi%
}

% Assumes all decoration macros and lengths are filled. Checks whether the arch is too close at a corner and shifts it appropriately. Upon exit, \pgfdecorationsegmentaspect is modified.
\protected\def\yquant@gappedbrace@calcshift{%
   \pgf@xc=\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance%
   \expandafter\forcsvlist\expandafter\yquant@gappedbrace@shiftloop%
      \expandafter{\pgfdecorationsegmentfromto}%
}

\def\yquant@gappedbrace@loop#1{%
   \yquant@gappedbrace@extract#1\yquant@sep%
   \unless\ifdim\from=0pt %
      \pgfpathmoveto{%
         \pgfqpoint{\from}{.5\pgfdecorationsegmentamplitude}%
      }%
   \fi%
   \unless\ifdim\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance<\from %
      \unless\ifdim\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance>\to %
         % be careful about arch positions at the border
         \ifdim\dimexpr\to-\from\relax<2\pgfdecorationsegmentamplitude %
            % The arch is larger than the segment. We do not draw a line to it or an end line and place it in the mid of the segment, even if it is too short (this issues one extra \pgfpathmoveto command, but catching this rare case is not worth the effort).
            \pgfpathmoveto{%
               \pgfqpoint{\dimexpr.5\dimexpr\from+\to\relax-\pgfdecorationsegmentamplitude\relax}%
                         {.5\pgfdecorationsegmentamplitude}%
            }%
            \edef\to{\the\pgfdecoratedremainingdistance}% to prevent the final line, we do not need "to" any more
         \else%
            % The segment is large enough to cover the whole arch. But maybe we are too close at the border?
            \ifdim\dimexpr\pgf@xc-\pgfdecorationsegmentamplitude\relax<\from %
               \pgf@xc=\dimexpr\from+\pgfdecorationsegmentamplitude\relax%
            \fi%
            \ifdim\dimexpr\pgf@xc+\pgfdecorationsegmentamplitude\relax>\to %
               \pgf@xc=\dimexpr\to-\pgfdecorationsegmentamplitude\relax%
            \fi%
            % Both cases can't occur at the same time in this \else clause.
            \pgfpathlineto{%
               \pgfqpoint{\dimexpr\pgf@xc-\pgfdecorationsegmentamplitude\relax}%
                         {.5\pgfdecorationsegmentamplitude}%
            }%
         \fi%
         \pgfpathcurveto{%
            \pgfqpoint{\dimexpr\pgf@xc-.5\pgfdecorationsegmentamplitude\relax}%
                      {.5\pgfdecorationsegmentamplitude}%
         }{%
            \pgfqpoint{\dimexpr\pgf@xc-.15\pgfdecorationsegmentamplitude\relax}%
                      {.7\pgfdecorationsegmentamplitude}%
         }{%
            \pgfqpoint{\pgf@xc}{\pgfdecorationsegmentamplitude}%
         }%
         \pgfpathcurveto{%
            \pgfqpoint{\dimexpr\pgf@xc+.15\pgfdecorationsegmentamplitude\relax}%
                      {.7\pgfdecorationsegmentamplitude}%
         }{%
            \pgfqpoint{\dimexpr\pgf@xc+.5\pgfdecorationsegmentamplitude\relax}%
                      {.5\pgfdecorationsegmentamplitude}%
         }{%
            \pgfqpoint{\dimexpr\pgf@xc+\pgfdecorationsegmentamplitude\relax}%
                      {.5\pgfdecorationsegmentamplitude}%
         }%
      \fi%
   \fi%
   \unless\ifdim\to=\pgfdecoratedremainingdistance %
      \pgfpathlineto{%
         \pgfqpoint{\to}{.5\pgfdecorationsegmentamplitude}%
      }%
   \fi%
}

% This is a variant of the brace in pathreplacing that allows for holes
\pgfdeclaredecoration{gapped brace}{final}{%
   \state{final}{%
      \pgf@xc=\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance%
      \pgfpathmoveto\pgfpointorigin% required for "raise" key
      \pgfpathcurveto{%
         \pgfqpoint{.15\pgfdecorationsegmentamplitude}{.3\pgfdecorationsegmentamplitude}%
      }{%
         \pgfqpoint{.5\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}%
      }{%
         \pgfqpoint{\pgfdecorationsegmentamplitude}{.5\pgfdecorationsegmentamplitude}%
      }%
      \expandafter\forcsvlist\expandafter\yquant@gappedbrace@loop%
         \expandafter{\pgfdecorationsegmentfromto}%
      \pgfpathlineto{%
         \pgfqpoint{\dimexpr\pgfdecoratedremainingdistance-\pgfdecorationsegmentamplitude\relax}%
                   {.5\pgfdecorationsegmentamplitude}%
      }%
      \pgfpathcurveto{%
         \pgfqpoint{\dimexpr\pgfdecoratedremainingdistance-.5\pgfdecorationsegmentamplitude\relax}%
                   {.5\pgfdecorationsegmentamplitude}%
      }{%
         \pgfqpoint{\dimexpr\pgfdecoratedremainingdistance-.15\pgfdecorationsegmentamplitude\relax}%
                   {.3\pgfdecorationsegmentamplitude}%
      }{%
         \pgfqpoint{\pgfdecoratedremainingdistance}{0pt}%
      }%
   }%
}%

\protected\def\yquant@gappedbrace@timer{%
   \ifdim\tikz@time pt=-1pt %
      % first set \pgfdecoratedremainingdistance appropriately
      \pgfpointdiff\tikz@timer@start\tikz@timer@end%
      \pgfmathsqrt@{\dimexpr\pgf@x*\pgf@x/65536+\pgf@y*\pgf@y/65536\relax\@gobbletwo}%
      \pgfdecoratedremainingdistance=\pgfmathresult pt %
      % now perform the transformation
      \yquant@gappedbrace@calcshift%
      % and finally let us return the desired position, just ignoring \tikz@time...
      \pgftransformlineattime{\pgfdecorationsegmentaspect}{\tikz@timer@start}{\tikz@timer@end}%
   \else%
      \pgftransformlineattime{\tikz@time}{\tikz@timer@start}{\tikz@timer@end}%
   \fi%
}

\pgfkeys{%
   /pgf/decoration/from to/.store in=\pgfdecorationsegmentfromto%
}
% END_FOLD

% The init and output shapes are tightly connected to quantum circuits, so they will depend on the current circuit orientation!
\pgfdeclareshape{yquant-init}{%
   \saveddimen\xradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/x radius}}%
      \ifyquanthorz{%
         % inner xsep is between the brace tip and the right end of the text. Check if the decoration is raised (will define \def\tikz@dec@shift{\pgftransformyshift{#1}})
         \iftikz@decoratepath%
            \ifx\tikz@dec@shift\relax%
               \pgf@xa=\pgfdecorationsegmentamplitude %
            \else%
               \pgfmathsetlength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift+\pgfdecorationsegmentamplitude}%
            \fi%
            \pgfmathaddtolength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
         \fi%
         % outer xsep is (a lower bound) from the left end of the border to the left end of the text
         \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/outer xsep}}%
         \@tempdima=.5\dimexpr\wd\pgfnodeparttextbox+\pgf@xa+\pgf@xb\relax%
         \ifdim\@tempdima>\pgf@x%
            \pgf@x=\@tempdima%
         \fi%
      }{%
         \ifdim.5\wd\pgfnodeparttextbox>\pgf@x%
            \pgf@x=.5\wd\pgfnodeparttextbox%
         \fi%
         \iftikz@decoratepath%
            \ifdim2\pgfdecorationsegmentamplitude>\pgf@x%
               \pgf@x=2\pgfdecorationsegmentamplitude%
            \fi%
         \fi%
      }%
      \global\pgf@y=\pgf@x%
   }%
   \saveddimen\yradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/y radius}}%
      \ifyquanthorz{%
         \@tempdima=.5\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox\relax%
         \ifdim\@tempdima>\pgf@x%
            \pgf@x=\@tempdima%
         \fi%
         \iftikz@decoratepath%
            \ifdim2\pgfdecorationsegmentamplitude>\pgf@x%
               \pgf@x=2\pgfdecorationsegmentamplitude%
            \fi%
         \fi%
      }{%
         % inner ysep is between the brace tip and the bottom end of the text. Check if the decoration is raised (will define \def\tikz@dec@shift{\pgftransformyshift{#1}})
         \iftikz@decoratepath%
            \ifx\tikz@dec@shift\relax%
               \pgf@xa=\pgfdecorationsegmentamplitude %
            \else%
               \pgfmathsetlength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift+\pgfdecorationsegmentamplitude}%
            \fi%
            \pgfmathaddtolength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}}%
         \fi%
         % outer ysep is (a lower bound) from the top end of the border to the top end of the text
         \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/outer ysep}}%
         \@tempdima=.5\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox+\pgf@xa+\pgf@xb\relax%
         \ifdim\@tempdima>\pgf@x%
            \pgf@x=\@tempdima%
         \fi%
      }%
   }%
   \savedanchor\stext{%
      % \pgf@x is still \yradius, \pgf@y is still \xradius
      \@tempdima=\pgf@y%
      \@tempdimb=\pgf@x%
      \ifyquanthorz{%
         \iftikz@decoratepath%
            % first set \pgfdecoratedremainingdistance appropriately
            \pgfdecoratedremainingdistance=2\@tempdimb%
            % now perform the transformation
            \yquant@gappedbrace@calcshift%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}+\pgfdecorationsegmentamplitude}%
            \unless\ifx\tikz@dec@shift\relax%
               \pgfmathaddtolength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift}%
            \fi%
            \pgfqpoint%
               {\dimexpr\@tempdima-\wd\pgfnodeparttextbox-\pgf@xa\relax}%
               {\dimexpr\@tempdimb% = \yradius
                        -\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance% - aspect
                        -.5\ht\pgfnodeparttextbox+.5\dp\pgfnodeparttextbox% center there
                \relax}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
            \pgfqpoint%
               {\dimexpr\@tempdima-\wd\pgfnodeparttextbox-\pgf@xa\relax}%
               {-.5\dimexpr\ht\pgfnodeparttextbox-\dp\pgfnodeparttextbox\relax}%
         \fi%
      }{%
         \iftikz@decoratepath%
            % first set \pgfdecoratedremainingdistance appropriately
            \pgfdecoratedremainingdistance=2\@tempdima%
            % now perform the transformation
            \yquant@gappedbrace@calcshift%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}+\pgfdecorationsegmentamplitude}%
            \unless\ifx\tikz@dec@shift\relax%
               \pgfmathaddtolength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift}%
            \fi%
            \pgfqpoint%
               {\dimexpr-\@tempdima+%
                        \pgfdecorationsegmentaspect\pgfdecoratedremainingdistance% - aspect
                        -.5\wd\pgfnodeparttextbox% center there
                \relax}%
               {\dimexpr-\@tempdimb+\dp\pgfnodeparttextbox+\pgf@xa\relax}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}}%
            \pgfqpoint%
               {-.5\wd\pgfnodeparttextbox}%
               {\dimexpr-\@tempdimb+\dp\pgfnodeparttextbox+\pgf@xa\relax}%
         \fi%
      }%
   }%
   \savedmacro\ifhorz{%
      \ifyquanthorz{%
         \def\ifhorz{\noexpand\@firstoftwo}%
      }{%
         \def\ifhorz{\noexpand\@secondoftwo}%
      }%
   }%
   % BEGIN_FOLD Operator anchors
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-rectangle]{\anc}%
   }%
   \anchor{text}{\stext}%
   \inheritanchorborder[from=yquant-rectangle]%
   % END_FOLD
   % BEGIN_FOLD Path
   \backgroundpath{%
      \ifhorz{%
         \pgfpathmoveto{\pgfqpoint{\xradius}{\yradius}}%
         \pgfpathlineto{\pgfqpoint{\xradius}{-\yradius}}%
      }{%
         \pgfpathmoveto{\pgfqpoint{-\xradius}{-\yradius}}%
         \pgfpathlineto{\pgfqpoint{\xradius}{-\yradius}}%
      }%
      \pgfpointtransformed{\pgfqpoint{-\xradius}{\yradius}}%
      \pgf@protocolsizes{\pgf@x}{\pgf@y}%
      \pgfpointtransformed{\pgfqpoint{-\xradius}{-\yradius}}% for rotations
      \pgf@protocolsizes{\pgf@x}{\pgf@y}%
   }%
   \clippath{%
      \pgfpathrectanglecorners%
         {\pgfqpoint{-\dimexpr\xradius\relax}%
                    {\dimexpr\yradius\relax}}%
         {\pgfqpoint{\dimexpr\xradius\ifhorz{+.5\pgflinewidth}{}\relax}%
                    {-\dimexpr\yradius\ifhorz{}{+.5\pgflinewidth}\relax}}%
   }%
   % END_FOLD
}

\pgfdeclareshape{yquant-output}{%
   % Almost the same as in yquant-input, only a minimal change in \stext's x position
   \saveddimen\xradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/x radius}}%
      \ifyquanthorz{%
         % inner xsep is between the brace tip and the left end of the text. Check if the decoration is raised (will define \def\tikz@dec@shift{\pgftransformyshift{#1}})
         \iftikz@decoratepath%
            \ifx\tikz@dec@shift\relax%
               \pgf@xa=\pgfdecorationsegmentamplitude %
            \else%
               \pgfmathsetlength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift+\pgfdecorationsegmentamplitude}%
            \fi%
            \pgfmathaddtolength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
         \fi%
         % outer xsep is (a lower bound) from the right end of the border to the right end of the text
         \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/outer xsep}}%
         \@tempdima=.5\dimexpr\wd\pgfnodeparttextbox+\pgf@xa+\pgf@xb\relax%
         \ifdim\@tempdima>\pgf@x%
            \pgf@x=\@tempdima%
         \fi%
      }{%
         \ifdim.5\wd\pgfnodeparttextbox>\pgf@x%
            \pgf@x=.5\wd\pgfnodeparttextbox%
         \fi%
         \iftikz@decoratepath%
            \ifdim2\pgfdecorationsegmentamplitude>\pgf@x%
               \pgf@x=2\pgfdecorationsegmentamplitude%
            \fi%
         \fi%
      }%
      \global\pgf@y=\pgf@x%
   }%
   \saveddimen\yradius{%
      \pgfmathsetlength\pgf@x{\pgfkeysvalueof{/tikz/y radius}}%
      \ifyquanthorz{%
         \@tempdima=.5\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox\relax%
         \ifdim\@tempdima>\pgf@x%
            \pgf@x=\@tempdima%
         \fi%
         \iftikz@decoratepath%
            \ifdim2\pgfdecorationsegmentamplitude>\pgf@x%
               \pgf@x=2\pgfdecorationsegmentamplitude%
            \fi%
         \fi%
      }{%
         % inner ysep is between the brace tip and the top end of the text. Check if the decoration is raised (will define \def\tikz@dec@shift{\pgftransformyshift{#1}})
         \iftikz@decoratepath%
            \ifx\tikz@dec@shift\relax%
               \pgf@xa=\pgfdecorationsegmentamplitude %
            \else%
               \pgfmathsetlength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift+\pgfdecorationsegmentamplitude}%
            \fi%
            \pgfmathaddtolength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}}%
         \fi%
         % outer ysep is (a lower bound) from the bottom end of the border to the bottom end of the text
         \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/outer ysep}}%
         \@tempdima=.5\dimexpr\ht\pgfnodeparttextbox+\dp\pgfnodeparttextbox+\pgf@xa+\pgf@xb\relax%
         \ifdim\@tempdima>\pgf@x%
            \pgf@x=\@tempdima%
         \fi%
      }%
   }%
   \savedanchor\stext{%
      % \pgf@x is still \yradius, \pgf@y is still \xradius
      \@tempdima=\pgf@y%
      \@tempdimb=\pgf@x%
      \ifyquanthorz{%
         \iftikz@decoratepath%
            % first set \pgfdecoratedremainingdistance appropriately
            \pgfdecoratedremainingdistance=2\@tempdimb%
            % now perform the transformation
            \yquant@gappedbrace@calcshift%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}+\pgfdecorationsegmentamplitude}%
            \unless\ifx\tikz@dec@shift\relax%
               \pgfmathaddtolength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift}%
            \fi%
            \pgfqpoint%
               {\dimexpr-\@tempdima+\pgf@xa\relax}%
               {\dimexpr\@tempdimb% = \yradius
                        -\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance% - aspect
                        -.5\ht\pgfnodeparttextbox+.5\dp\pgfnodeparttextbox% center there
                \relax}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
            \pgfqpoint%
               {\dimexpr-\@tempdima+\pgf@xa\relax}%
               {-.5\dimexpr\ht\pgfnodeparttextbox-\dp\pgfnodeparttextbox\relax}%
         \fi%
      }{%
         \iftikz@decoratepath%
            % first set \pgfdecoratedremainingdistance appropriately
            \pgfdecoratedremainingdistance=2\@tempdima%
            % now perform the transformation
            \yquant@gappedbrace@calcshift%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}+\pgfdecorationsegmentamplitude}%
            \unless\ifx\tikz@dec@shift\relax%
               \pgfmathaddtolength\pgf@xa{\expandafter\@secondoftwo\tikz@dec@shift}%
            \fi%
            \pgfqpoint%
               {\dimexpr-\@tempdima% = \xradius
                        +\pgfdecorationsegmentaspect\pgfdecoratedremainingdistance% - aspect
                        -.5\wd\pgfnodeparttextbox% center there
                \relax}%
               {\dimexpr\@tempdimb-\ht\pgfnodeparttextbox-\pgf@xa\relax}%
         \else%
            \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner ysep}}%
            \pgfqpoint%
               {-.5\wd\pgfnodeparttextbox}%
               {\dimexpr\@tempdimb-\ht\pgfnodeparttextbox-\pgf@xa\relax}%
         \fi%
      }%
   }%
   \savedmacro\ifhorz{%
      \ifyquanthorz{%
         \def\ifhorz{\noexpand\@firstoftwo}%
      }{%
         \def\ifhorz{\noexpand\@secondoftwo}%
      }%
   }%
   % BEGIN_FOLD Operator anchors
   \foreach \anc in {center, north, north east, east, south east, south, south west, west, north west} {%
      \inheritanchor[from=yquant-init]{\anc}%
   }%
   \anchor{text}{\stext}%
   \inheritanchorborder[from=yquant-init]%
   % END_FOLD
   % BEGIN_FOLD Path
   \backgroundpath{%
      \ifhorz{%
         \pgfpathmoveto{\pgfqpoint{-\xradius}{\yradius}}%
         \pgfpathlineto{\pgfqpoint{-\xradius}{-\yradius}}%
      }{%
         \pgfpathmoveto{\pgfqpoint{-\xradius}{\yradius}}%
         \pgfpathlineto{\pgfqpoint{\xradius}{\yradius}}%
      }%
      \pgfpointtransformed{\pgfqpoint{\xradius}{\yradius}}%
      \pgf@protocolsizes{\pgf@x}{\pgf@y}%
      \pgfpointtransformed{\pgfqpoint{\xradius}{-\yradius}}% for rotations
      \pgf@protocolsizes{\pgf@x}{\pgf@y}%
   }%
   \clippath{%
      \pgfpathrectanglecorners%
         {\pgfqpoint{-\dimexpr\xradius\ifhorz{+.5\pgflinewidth}{}\relax}%
                    {\dimexpr\yradius\ifhorz{}{+.5\pgflinewidth}\relax}}%
         {\pgfqpoint{\dimexpr\xradius\relax}%
                    {-\dimexpr\yradius\relax}}%
   }%
   % END_FOLD
}

\pgfdeclareshape{yquant-nothing}{%
   % this is an empty shape
   % BEGIN_FOLD Operator anchors
   \foreach \anc in {center, text, north, north east, east, south east, south, south west, west, north west, border} {%
      \global\cslet{pgf@anchor@yquant-nothing@\anc}\pgfpointorigin%
   }%
   % END_FOLD
}

\protected\def\yquant@pgflistloop@end{\yquant@pgflistloop@end}
\def\yquant@pgflistloop#1#2{%
   % loop through a coordinate list #2, executing #1
   \yquant@pgflistloop@i{#1}#2(\yquant@pgflistloop@end,)%
}

\def\yquant@pgflistloop@i#1(#2,#3){%
   \ifyquant\ifx\yquant@pgflistloop@end#2 \fi\relax{%
      #1{#2}{#3}%
      \yquant@pgflistloop@i{#1}%
   }%
}

\def\yquant@pgflistloop@skipfirst#1#2{%
   % loop through a coordinate list #2, executing #1, skipping the first element
   \yquant@pgflistloop@skipfirst@i{#1}#2(\yquant@pgflistloop@end,)%
}

\def\yquant@pgflistloop@skipfirst@i#1(#2,#3){%
   \ifyquant\ifx\yquant@pgflistloop@end#2 \fi\relax{%
      \yquant@pgflistloop@i{#1}%
   }%
}

\def\yquant@pgflistloop@first#1#2{%
   % expand #1 to the first item in the list, assuming the list is non-empty
   \yquant@pgflistloop@first@i{#1}#2\yquant@pgflistloop@end%
}

\def\yquant@pgflistloop@first@i#1(#2,#3)#4\yquant@pgflistloop@end{%
   #1{#2}{#3}%
}

\def\yquant@shape@enclosepolygon@line#1#2{%
   \pgfpathlineto{\pgfqpoint{#1}{#2}}%
}

\def\yquant@shape@enclosepolygon@findextrema#1#2{%
   \ifdim#1<\pgf@x %
      \pgf@x=#1 %
   \fi%
   \ifdim#1>\pgf@xa %
      \pgf@xa=#1 %
   \fi%
   \ifdim#2<\pgf@y %
      \pgf@y=#2 %
   \fi%
   \ifdim#2>\pgf@ya %
      \pgf@ya=#2 %
   \fi%
}

\pgfdeclareshape{yquant-enclose-polygon}{%
   % This is not an ordinary yquant shape, it does not define clip paths.
   % This is a polygon of arbitrary points, where the anchors will be at the enclosing rectangle.
   % The last point will be connected to the first.
   % BEGIN_FOLD Saved anchors
   \savedmacro\points{%
      \edef\points{\pgfkeysvalueof{/yquant/enclose/shape/polygon/points}}%
   }
   \savedmacro\extrema{%
      \edef\points{\pgfkeysvalueof{/yquant/enclose/shape/polygon/points}}%
      \pgf@x=\maxdimen%
      \pgf@xa=-\maxdimen%
      \pgf@y=\maxdimen%
      \pgf@ya=-\maxdimen%
      \expandafter\yquant@pgflistloop\expandafter\yquant@shape@enclosepolygon@findextrema\expandafter{\points}%
      \edef\extrema{{\the\pgf@x}{\the\pgf@y}{\the\pgf@xa}{\the\pgf@ya}}%
   }%
   % END_FOLD
   % BEGIN_FOLD Operator anchors
   \anchor{center}%
          {\pgf@x=\dimexpr.5\dimexpr\expandafter\@firstoffour\extrema\relax+.5\dimexpr\expandafter\@thirdoffour\extrema\relax\relax%
           \pgf@y=\dimexpr.5\dimexpr\expandafter\@secondoffour\extrema\relax+.5\dimexpr\expandafter\@fourthoffour\extrema\relax\relax}%
   \global\csletcs{pgf@anchor@yquant-enclose-polygon@text}{pgf@anchor@yquant-enclose-polygon@center}%
   \anchor{north}%
          {\pgf@x=\dimexpr.5\dimexpr\expandafter\@firstoffour\extrema\relax+.5\dimexpr\expandafter\@thirdoffour\extrema\relax\relax%
           \pgf@y=\expandafter\@fourthoffour\extrema\space}%
   \anchor{north east}%
          {\pgf@x=\expandafter\@thirdoffour\extrema\space%
           \pgf@y=\expandafter\@fourthoffour\extrema\space}%
   \anchor{east}%
          {\pgf@x=\expandafter\@thirdoffour\extrema\space%
           \pgf@y=\dimexpr.5\dimexpr\expandafter\@secondoffour\extrema\relax+.5\dimexpr\expandafter\@fourthoffour\extrema\relax\relax}%
   \anchor{south east}
          {\pgf@x=\expandafter\@thirdoffour\extrema\space%
           \pgf@y=\expandafter\@secondoffour\extrema\space}%
   \anchor{south}%
          {\pgf@x=\dimexpr.5\dimexpr\expandafter\@firstoffour\extrema\relax+.5\dimexpr\expandafter\@thirdoffour\extrema\relax\relax%
           \pgf@y=\expandafter\@secondoffour\extrema\space}%
   \anchor{south west}%
          {\pgf@x=\expandafter\@firstoffour\extrema\space%
           \pgf@y=\expandafter\@secondoffour\extrema\space}%
   \anchor{west}%
          {\pgf@x=\expandafter\@firstoffour\extrema\space%
           \pgf@y=\dimexpr.5\dimexpr\expandafter\@secondoffour\extrema\relax+.5\dimexpr\expandafter\@fourthoffour\extrema\relax\relax}%
   \anchor{north west}
          {\pgf@x=\expandafter\@firstoffour\extrema\space%
           \pgf@y=\expandafter\@fourthoffour\extrema\space}%
   \anchorborder{%
      \@tempdima=\pgf@x%
      \@tempdimb=\pgf@y%
      \pgfpointborderrectangle{\pgfqpoint{\@tempdima}{\@tempdimb}}%
                              {\pgfqpoint{.5\dimexpr\expandafter\@thirdoffour\extrema-\expandafter\@firstoffour\extrema}%
                                         {.5\dimexpr\expandafter\@fourthoffour\extrema-\expandafter\@secondoffour\extrema}}%
      \advance\pgf@x by \dimexpr.5\dimexpr\expandafter\@firstoffour\extrema\relax+.5\dimexpr\expandafter\@thirdoffour\extrema\relax\relax%
      \advance\pgf@y by \dimexpr.5\dimexpr\expandafter\@secondoffour\extrema\relax+.5\dimexpr\expandafter\@fourthoffour\extrema\relax\relax%
   }%
   % END_FOLD
   % BEGIN_FOLD Path
   \backgroundpath{%
      \pgfpathmoveto{%
         \expandafter\yquant@pgflistloop@first\expandafter\pgfqpoint\expandafter{\points}%
      }%
      \expandafter\yquant@pgflistloop@skipfirst\expandafter\yquant@shape@enclosepolygon@line\expandafter{\points}%
      \pgfpathclose%
   }%
   % END_FOLD
}