projekter/yquant

Circuit equations

Closed this issue · 2 comments

Circuit equations are probably encountered quite often, but in order to implement them in yquant, I'm not quite satisfied with the current solutions. I want to implement at least some kind of built-in support for them.

Required features:

  • Logic-oriented syntax without code duplication. A circuit equation is different from an ordinary circuit in that there are potentially multiple circuits next to each other which share at least a subset of registers. All registers should be aligned at the same vertical position, and it should not be necessary to re-declare registers in each subcircuit.
  • Most often, this is probably desired without a surrounding box, but maybe sometimes authors wish to enclose their circuit in boxes.
  • Despite having some of the registers in common, ancillas may vary from circuit to circuit.
  • The circuits should be connected by an equals-sign (which should also be customizable).
  • More than a single equation per line should be easily possible.

Desired features:

  • Some way to talk about not only stacking circuits next to, but also below each other. It would be great to offer a flexibility similar to the align environment:
    • Allow for an alignment at the equals sign
    • But also allow for something like \MoveEqLeft, which can be helpful for longer lines
    • Maybe even allow for pagebreaks (and probably more intelligently than align, i.e., re-start the alignment on the new page)

Thoughts:

  • Logically speaking, it does not really make sense to introduce some new gates for this. Gates mean quantum logic and operations, but this is not something that happens when we consider equivalent circuits, which is more of a typography thing. So macros would be better.
  • To allow for most flexibility, the macros should internally use subcircuits, without forcing the user to specify all the annoying attributes and wire type changes.
  • If we talk about multi-line support, then the macros need to issue various yquant environments in the same tikzpicture.
  • If we talk about multi-page support, then the macros need to open and close various tikzpictures. So strictly speaking, they should be responsible for opening and closing the tikzpicture and yquant environments. However, this somehow interfers with the policy of allowing the user to put arbitrary TeX code at arbitrary positions.
  • What about naming gates in the individual circuits? Since they are in subcircuits, the usual naming conventions would apply; however, the subcircuits are implicitly inserted by the macros. Which means the names would not be visible outside at all. Just make them visible without any prefix? Or allow to name the implicit subcircuits?

First idea:

\begin{yquantgroup}% replaces yquant, optional options go to the yquant environment.
                   % as with \yquantimport: can be inside a tikzpicture (then no page
                   % breaking, but easy to pass options to the picture) or outside
                   % (which allows for page breaking, but options must be passed
                   % manually).
                   % yquantgroup* is available, but all _outer_ registers must be
                   % declared explicitly, only inner ones can be declared on-the-fly.
   \registers{
      % put arbitrary shared register declarations in here
      qubit {$\ket0$} a[4];
      qubit {$\ket1$} b;
   }
   \circuit[frameless]{% basically inserts a subcircuit, optional arguments are passed as
                       % subcircuit arguments
      import -; % use the import command to make the outer registers known in the
                % inner circuit. All registers that were not imported manually will be
                % imported automatically as soon as the first non-declaration gate occurs
                % (not necessarily the first gate that uses this register, but any).
                % Manual importing allows to intersperse the registers with internal
                % wires.
      % now just some arbitrary gate commands, as usual
      h -;
      cnot b | a[3];
      cnot b | a[0];
      h -;
   }
   \equals*[$=$] % emit the equality sign.
                 % star = put a horizontal alignment mark at this position
                 % optional parameter: customize the text
   \circuit{
      % next circuit which goes to the right
   }
   \\ % line break = new line in the circuit - end and restart the yquant environment
   \shiftright[1] % the current yquant environment will be shifted to the right either
                  % - by the amount specified in the argument if it is a dimension
                  % - to the position of the xth horizontal alignment mark defined
                  %   before, where x is the optional argument (one based, default 1)
                  % - or not shifted, if the argument is zero
                  % \shiftright can only directly follow a break
                  % \shiftright* additionally puts an alignment at this position
                  % If [aligned] is passed to the yquantgroup, \shiftright is implied
                  % after each line break, unless it is manually specified with a
                  % different argument.
   \equals[$=$] % without the star, the alignment marks of the previous line are copied
                % with the star, the alignment marks are cleared, then overwritten
   \circuit{
      % again a circuit
   }
   \pagebreak % or \newpage or \clearpage or \cleardoublepage: those commands must be
              % inserted manually, then end the yquant and tikzpicture environment and
              % restart them. Only works if yquantgroup is not within a tikzpicture.
              % If commands should be repeated at the beginning of every tikzpicture,
              % they should be specified as the [preamble={}] option to yquantgroup
   % ...
\end{yquantgroup}

Thoughts on this/suggestions? @engeljh or others that frequently use circuit equations?

Noted, frameless by default makes sense. I will probably introduce some style that is applied for subcircuits generated via \circuit, which is by default frameless but allows to switch this globally. I also agree that the page breaking stuff is not a priority in implementation, but I want to at least have a concept that would allow to have some of these capabilities without requiring the user to again type more than necessary. And actually, if it is done like this, the implementation of this part should be pretty straightforward.
I'll leave this open for comments until next weekend and will then start with the implementation.