35

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?

1
  • 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, 2012 at 11:35

6 Answers 6

12

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

3
  • 1
    qasm2circ is also based on xypic, apparently. So it suffers from the same criticisms that the OP gave for qcircuit.
    – Seamus
    Jan 27, 2011 at 15:02
  • 11
    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, 2011 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? Sep 6, 2014 at 12:13
18

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

2
  • 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. Feb 25, 2016 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, 2016 at 8:47
7

Since September 2018 there is the quantikz TiKZ library: https://ctan.org/pkg/quantikz

It has, on purpose, very similar syntax to qcircuit. For your circuit:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz}

\begin{document}

\begin{quantikz}
\gategroup[wires=3,steps=11,style={rounded corners,fill=blue!20}, background]{}
&\lstick{$|{0}\rangle$} & \gate{H}&\ctrl{1} & \ctrl{2} &\qw &\qw 
 \rstick[wires=3]{$\frac{|{000}\rangle + |{111}\rangle}{\sqrt{2}}$} 
\\
&\lstick{$|{0}\rangle$} & \gate{H}& \control{} & \qw& \gate{H} &\qw
\\
&\lstick{$|{0}\rangle$} & \gate{H}& \qw & \control{} & \gate{H} &\qw
&&&&
\end{quantikz}

\end{document}
3
  • Personally I have found quantikz to be rather convenient/natural for drawing circuits in research papers — although alignment around = signs is a bit awkward. qpic is also nice, but it feels like there are more hoops to jump through (it is a tool separate from LaTeX, so not easy to use with Overleaf and similar tools)
    – Marcus P S
    Oct 7, 2020 at 17:03
  • true about overleaf, you have to upload the tikzlibrary file and it cannot be put in a subfolder, but otherwise it works file (even externalization) Dec 9, 2020 at 9:33
  • 1
    There is a Julia library that can be used to programmatically generate the required markup as well: Quantikz.jl krastanov.github.io/Quantikz/stable
    – Krastanov
    Mar 9, 2021 at 21:58
5

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

2

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.

2
  • 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. Jan 28, 2011 at 9:04
  • That's fair enough. Jan 29, 2011 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.