projekter / yquant

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

Setstyle/Addstyle delete the wire #24

Closed tigerjack closed 1 year ago

tigerjack commented 1 year ago

Describe the bug Setting the style of a wire using either setstyle or addstyle delete the previous wire.

To Reproduce This is a MWE reproducing the behavior

\documentclass[tikz]{standalone}
\usepackage[compat=0.6]{yquant}
\begin{document}
\begin{tikzpicture}
  \begin{yquant}
    nobit above;
    qubit {$a0,\idx$} a0[3]; qubit {$a1,\idx$} a1[3]; qubit {$a2,\idx$} a2[3];
    nobit below;
    \makeatletter

    \foreach \bef in {0, ..., 2} {
      \yquant subcircuit {
        nobit above;
        qubit {} a0[3]; qubit {} a1[3]; qubit {} a2[3];
        nobit below;
        \foreach \i in {0, ..., 2} {
          \unless\ifnum\i=\bef\space
            \yquant
            setstyle {color=blue} a\i[\bef];
            \foreach \aft in {0, ..., 2} {
              \unless\ifnum\aft=\bef
                \yquant
                z a\i[\aft] | a\bef[\aft], a\i[\bef];
              \fi
            }
            \yquant
            setstyle {color=orange} a\i[\bef]; % should be black
          \fi
        }
      } (-);
    }
  \end{yquant}
\end{tikzpicture}
\end{document}

Expected output Change the color of a wire without affecting the previous one.

Screenshots image

Environment

projekter commented 1 year ago

Can you please give a more precise description what you expect? Where is a wire deleted? And what should be black?

projekter commented 1 year ago

Ah, now I see what you mean (probably). As a workaround, you can set [drawing mode=size] as option for yquant (requires version 0.7). But this is just a workaround because really there's a much larger problem here: The heights/depths are completed incorrectly. I'll have to look into this, but this will probably take some time.

tigerjack commented 1 year ago

@projekter I confirm that the workaround correctly draws the missing line on the wire a2,1, with no overlap with the Z gate. Those were the two issues in the original figure.

Thanks, as always, for your help.

tigerjack commented 1 year ago

@projekter On a closer inspection, the workaround correctly fixes the problem with the missing wire. However, the wire color is set to the one specified after. For example, in the image, I would like to have the blue color to be applied exactly when I called it. Or, in other words, the initial part of wire a2,0 should be black. Or at least a reasonable portion of it.

image

projekter commented 1 year ago

Well, the initial part (the one before the subcircuit) is black. If I then just look at the first subcircuit, i.e., \bef is zero, then all the loops expand to

nobit above;
qubit {} a0[3]; qubit {} a1[3]; qubit {} a2[3];
nobit below;
% i = 0: since \i=\bef, the \if part is skipped.
% i = 1:
setstyle {color=blue} a1[0];
   % aft = 0: since \aft=\bef, the if part is skipped.
   % aft = 1
   z a1[1] | a0[1], a1[0];
   % aft = 2
   z a1[2] | a0[2], a1[0];
setstyle {color=orange} a1[0];
% i = 2
setstyle {color=blue} a2[0];
   % aft = 0: since \aft=\bef, the if part is skipped
   % aft = 1
   z a2[1] | a0[1], a2[0];
   % aft = 2
   z a2[2] | a0[2], a2[0];
setstyle {color=orange} a2[0];

And looking at this, yquant correctly colors your wires as you instructed it to do. Note that when you change a wire style, it will change the style starting from the last gate on this wire and continuing until further notice. This is because there are no true time slices in yquant. The current "time" of a wire is always the position of the last gate on the wire. So since you apply setstyle as the very first things before any other wire, it will simply color the wire from the start (in this case, from the start of the subcircuit). In order to change this, you'll have to place a gate before the setstyle command. Any gate will do, also if it is invisible - you'll probably want to put hspace in there, so that you can exactly state how long the black line segment should be. (In fact, you can simply put the hspace to all gates at the very beginning).

tigerjack commented 1 year ago

Thanks again @projekter , indeed using hspace {} -; right before the addstyle command did the trick.

Out of curiosity, what if I want to extend instead the color a bit above the gate? Like, for example, on the wire a1,0, what if I want for the blue color to extend a bit after the control qubit? Is there an easy way for it?

EDIT: well, I found the answer myself. Just use another hspace after :)

projekter commented 1 year ago

And hspace is not called this way for nothing. You can pass any valid length as argument to extend the line: hspace {1cm} -;. Or use the align pseudo-gate, if you want to match with the position of another register.

projekter commented 1 year ago

The styling issue is now fixed and works in the default mode. However, I didn't deal the wrong vertical placement yet.

projekter commented 1 year ago

And now the positioning problem is also fixed.