projekter / yquant

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

Ability to define a custom box(gate) with attributes #32

Open DerryAlex opened 4 months ago

DerryAlex commented 4 months ago

A motivation is to support $R{x}, R{y}, R_{z}$. It would be nice if the following syntax is possible.

\definebox[required_attrs={value}]{ry}{$R_t \left( \yquant@lang@attr@value \right)$}
\definebox[required_attrs={value}]{rz}{$R_t \left( \yquant@lang@attr@value \right)$}

\begin{yquant*}
    ry {$\theta_1$} q;
    rz {$\theta_2$} q;
\end{yquant*}

图片

A more complicated usage might be

\definegate[required_attrs={ctrl1, ctrl2, op}]{control3}{
    qubit a; qubit b; qubit c;
    controlbox { \yquant@lang@attr@ctrl1 } a; % See issue 31 for controlbox
    controlbox { \yquant@lang@attr@ctrl2 } b;
    box { \yquant@lang@attr@op } c | a, b;
}

\begin{yquant*}
    [ctrl1=$2$, ctrl2=$1$, op=$V$] control3 (a, b, c);
    [ctrl1=$2$, ctrl2=$2$, op=$V^2$] control3 (a, b, c);
\end{yquant*}

图片

projekter commented 4 months ago

While I agree that custom attributes for own gates would be useful, and I will add them when I have time to do so, the same applies as in the controlbox-issue: If you want to define more complicated gates, have a look at how this is done in yquant-lang.tex and just copy the behavior. In this way, you can define for example your rx box:

\documentclass[tikz]{standalone}

\usepackage[compat=0.6]{yquant}

\def\rxcontent#1{$R_x\left(#1\right)$}
\makeatletter
\yquant@langhelper@declare@command%
   {rx}%
   \yquant@register@get@allowmultitrue%
   {%
      \expandafter\yquant@prepare%
         \expandafter{\expandafter\rxcontent\expandafter{\yquant@lang@attr@value}}%
         {/yquant/operators/every box}%
   }
\yquant@langhelper@setup@attrs{rx}{value}{}
\makeatother

\begin{document}
   \begin{tikzpicture}
      \begin{yquant*}
         rx {test} a;
      \end{yquant*}
   \end{tikzpicture}
\end{document}

You can also define custom attributes if you wish, you just have to declare the attribute once (see the top of yquant-lang.tex for examples) and then in setup@attrs say which required or optional attributes you want to have. Then you can combine them as you wish. Just make sure that the attributes are expanded before \yquant@prepare is called.

Things are in some way even simpler for defined gates instead of boxes, as they allow to do much more. I would simply define the three attributes that you want, then define the gate to use these attributes (note that the content of the gate is expanded at creation, so you'll have to wrap everything in \unexpanded), and finally associate the attributes with the gate. Then, your given code works as you wrote it.

\yquant@langhelper@declare@attr{
   ctrl1/.store in=\attrctrlone,
   ctrl2/.store in=\attrctrltwo,
   op/.store in=\attrop
}

\yquantdefinegate{control3}{\unexpanded{
   qubit a; qubit b; qubit c;
   controlbox {\attrctrlone} a; % See issue 30
   controlbox {\attrctrltwo} b;
   box {\attrop} c | a, b;
}}

\yquant@langhelper@setup@attrs{control3}{ctrl1,ctrl2,op}{}

Be aware of the fact that numbers and letters by default have a different category code, so you cannot define (easily) a macro that contains a number in its name. This will always end the name and be interpreted as the token that follows the macro, unless you change the catcode of the char or wrap everything in \csname...\endcsname.

Let's leave this issue open until I have implemented a better attribute support (I guess April or May).

DerryAlex commented 4 months ago

Many thanks