projekter / yquant

Typesetting quantum circuits in a human-readable language
LaTeX Project Public License v1.3c
56 stars 5 forks source link

Styling controls individually #25

Closed tigerjack closed 1 year ago

tigerjack commented 1 year ago

Is your feature request related to a problem? Please describe. At the moment, I can only style the whole operator or all the controls at once, but not the single controls.

Describe the solution you'd like I would like to style each control (i.e., the dots) of a multi-controlled gate individually.

Describe alternatives you've considered I tried to overcome the limitations using the usual begin/end{scope}, but it gets complicated and difficult to generalize.

Additional context Using the code below

\documentclass[tikz]{standalone}
\usepackage{./yquant}
\usetikzlibrary{backgrounds,fit}
\begin{document}
\begin{tikzpicture}
  \begin{yquant}
    qubit {$q_\idx$} q[4];
    [this control/.append style={fill=orange}] 
    cnot q[3] | q[0], q[1], q[2];
    %
    [global attrs/name/.expanded={mcnot}]
    cnot q[3]| q[0], q[1], q[2];
  \end{yquant}
  \begin{scope}[on background layer, maximum width=.5pt,  every node/.style={draw,dotted}]
    \node[circle, red, fit=(mcnot-p1)] {};
    \node[circle, blue, fit=(mcnot-p2)] {};
  \end{scope}
\end{tikzpicture}
\end{document}

I obtained something like this, which is far from optimal image

projekter commented 1 year ago

There is no "intrinsic" support for this, and there are only styling keys that address all gates, positive, or negative controls. However, it is not too hard to fake this: As always, yquant defines the \idx macro before applying every style to the control. This macro holds the current visual index within the group (so, it iterates from 0 to the number of gates -1; from 0 to the number of positive control -1; and also negative controls). So you could simply define a conditional style like this:

\documentclass[tikz]{standalone}
\usepackage[compat=0.6]{yquant}
\begin{document}
   \begin{tikzpicture}
     \begin{yquant}
       qubit {$q_\idx$} q[4];
       [this control/.append style={fill=orange}]
       cnot q[3] | q[0], q[1], q[2];
       %
       [this control/.code={\unexpanded{%
         \ifcase\idx\relax%
            \pgfkeysalso{fill=red}%
         \or%
            \pgfkeysalso{fill=blue}%
         \else%
            \pgfkeysalso{fill=green}%
         \fi%
       }}]
       cnot q[3]| q[0], q[1], q[2];
     \end{yquant}
   \end{tikzpicture}
\end{document}

Of course, you can use any kind of style that you like. If you use this more often, it might be worth defining a custom style that does this job for you.

tigerjack commented 1 year ago

Excellent! Thank you @projekter