23

I want to typeset things that look like

enter image description here

I'm already aware of

  • Q-circuit, which has nice custom macros for this purpose, but is based on the relatively slow and clunky XyPic
  • TikZ, which I can use to manually set things up (that's how the picture I've hotlinked was drawn)

Are there any other alternatives I should know about?

  • Quantum circuits are written in a similar way to sheet music. It'd be interesting if LilyPond could be adapted/abused to suit this purpose. – qubyte Feb 12 '12 at 11:35
10

Apparently Nielsen and Chuang used qasm2circ to produce the figures in Quantum Computation and Quantum Information.

  • 1
    qasm2circ is also based on xypic, apparently. So it suffers from the same criticisms that the OP gave for qcircuit. – Seamus Jan 27 '11 at 15:02
  • 9
    OP's primary objection to XyPic was speed, which matters if you're using it every time you build your document (which is the standard workflow for Q-circuit). The workflow for qasm2circ is to build the quantum circuit diagrams beforehand and then use \includegraphics, which offsets the slowness of XyPic (and is a better workflow for preparing documents for publication). And regardless of the OP's objections to XyPic, the fact that qasm2circ was used to produce the quantum circuit diagrams for the book on quantum computation makes it an important alternative to be aware of. – las3rjock Jan 27 '11 at 19:41
  • I try to use qasm2circ in Ubuntu with the command qasm2pdf in terminal, but it outputs that qasm2pdf: command not found. What's wrong? – Eden Harder Sep 6 '14 at 12:13
14

The image you link to was a diagram I did in TikZ a few years ago. It's not a particularly efficient way of doing it either. These days I'd typeset that using a TikZ matrix environment. The only real option specific to quantum circuits were qasm2circ and q-circuit. I don't like the output they generate though, and TikZ is really the way to go in my opinion. Sure, it may require a few more key strokes, but I think the effort pays off. One important point to make is that TikZ can be slow, but if you get it to externalise TikZ images then it becomes as fast as a normal \includegraphics after a single slow compilation per diagram.

A revised version of the example you link to which may be more inspirational follows:

\documentclass[10pt]{standalone}
\usepackage{tikz}

% TikZ libraries `calc` needed now to tweak bracket.
\usetikzlibrary{backgrounds,fit,decorations.pathreplacing,calc}
% Dirac Kets
\newcommand{\ket}[1]{\ensuremath{\left|#1\right\rangle}}

\begin{document}
    \begin{tikzpicture}[thick]
    % `operator' will only be used by Hadamard (H) gates here.
    % `phase' is used for controlled phase gates (dots).
    % `surround' is used for the background box.
    \tikzstyle{operator} = [draw,fill=white,minimum size=1.5em] 
    \tikzstyle{phase} = [draw,fill,shape=circle,minimum size=5pt,inner sep=0pt]
    \tikzstyle{surround} = [fill=blue!10,thick,draw=black,rounded corners=2mm]
    %
    \matrix[row sep=0.4cm, column sep=0.8cm] (circuit) {
    % First row.
    \node (q1) {\ket{0}}; &[-0.5cm] 
    \node[operator] (H11) {H}; &
    \node[phase] (P12) {}; &
    \node[phase] (P13) {}; &
    &[-0.3cm]
    \coordinate (end1); \\
    % Second row.
    \node (q2) {\ket{0}}; &
    \node[operator] (H21) {H}; &
    \node[phase] (P22) {}; &
    &
    \node[operator] (H24) {H}; &
    \coordinate (end2);\\
    % Third row.
    \node (q3) {\ket{0}}; &
    \node[operator] (H31) {H}; &
    &
    \node[phase] (P33) {}; &
    \node[operator] (H34) {H}; &
    \coordinate (end3); \\
    };
    % Draw bracket on right with resultant state.
    \draw[decorate,decoration={brace},thick]
        ($(circuit.north east)-(0cm,0.3cm)$)
        to node[midway,right] (bracket) {$\displaystyle\frac{\ket{000}+\ket{111}}{\sqrt{2}}$}
        ($(circuit.south east)+(0cm,0.3cm)$);
    \begin{pgfonlayer}{background}
        % Draw background box.
        \node[surround] (background) [fit = (q1) (H31) (bracket)] {};
        % Draw lines.
        \draw[thick] (q1) -- (end1)  (q2) -- (end2) (q3) -- (end3) (P12) -- (P22) (P13) -- (P33);
    \end{pgfonlayer}
    %
    \end{tikzpicture}
\end{document}

enter image description here

  • Can I use your code as a part of a possible package for drawing quantum circuits using TikZ? I may release it under the GNU public license if I can do it. If you can participate on this package development, that will also be great. Thanks. – Xiaodong Qi Feb 25 '16 at 5:21
  • Hi @XiaodongQi, I'm afraid I'm not using TeX a lot these days, so I wouldn't be able to actively help you. You're very welcome to use my code though! – qubyte Feb 25 '16 at 8:47
4

You might also want to look at the qcircuit package: https://www.ctan.org/pkg/qcircuit

0

I'm using ⟨q|pic⟩ at the moment for my work, which uses TikZ. It's been flexible enough for me so far, and there's lots of examples on the GitHub page linked but I'll include a couple of the examples here:

enter image description here enter image description here

-1

There is circuitikz for drawing circuits with TiKZ.

  • 11
    The circuitikz package is for drawing electrical circuits; quantum circuits are almost completely different. It might be adaptable, but then you probably might as well be using plain TikZ. – Antal Spector-Zabusky Jan 28 '11 at 9:04
  • That's fair enough. – Mikael Vejdemo-Johansson Jan 29 '11 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.