0% found this document useful (0 votes)
244 views7 pages

Q-Circuit: A LaTeX Tutorial

The document provides a tutorial for using the Q-circuit package in LaTeX to easily construct quantum circuit diagrams (QCDs). Q-circuit implements macros that allow drawing QCDs by constructing arrays similar to the array environment. The tutorial explains how to install Q-circuit, provides simple examples of common gates and wires, and describes additional parameters to customize the circuit spacing and layout. More complex examples with multiple controls and arbitrary gates are also presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views7 pages

Q-Circuit: A LaTeX Tutorial

The document provides a tutorial for using the Q-circuit package in LaTeX to easily construct quantum circuit diagrams (QCDs). Q-circuit implements macros that allow drawing QCDs by constructing arrays similar to the array environment. The tutorial explains how to install Q-circuit, provides simple examples of common gates and wires, and describes additional parameters to customize the circuit spacing and layout. More complex examples with multiple controls and arbitrary gates are also presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q-circuit Tutorial

Bryan Eastin, Steven T. Flammia


Department of Physics and Astronomy, University of New Mexico, Albuquerque, New Mexico 87131–1156, USA

Q-circuit is a list of macros that greatly simplifies the construction of quantum circuit diagrams
(QCDs) in LATEX with the help of the XY-pic package. This tutorial should help the reader ac-
quire the skill to render arbitrary QCDs in a matter of minutes. Q-circuit is available for free1 at
[Link]

I. INTRODUCTION II. GETTING STARTED

Ever tried to use LATEX to typeset something like this? To install Q-circuit, place the file [Link] some-
where your TEX distribution can find it and run the ap-
propriate command to update your TEX tree. To use it,

• 
 

• • • • place the command
• = •
\input{Qcircuit}
U V V† V
in the preamble of your document. [Link] loads
Or maybe this? the amsmath and xy packages and implements a set of cir-
cuit commands. If need be, you can obtain the necessary

FE
|ψi • H • packages at [Link]


 

FE
|0i
III. SIMPLE QUANTUM CIRCUITS
|0i H • X Z |ψi
To begin, suppose the reader would like to typeset the
Or how about2 following simple circuit:

_ _ _ _ _Syndrome _ _ _ _ _ _ _ _ _ _ _ _ _  Recovery
Measurement X
 • • 
 • •  R This was typeset using
 
 
  76Ma23
01 54 
• •
  \Qcircuit @C=1em @R=.7em {
 |0i • & \gate{X} & \qw


 
 76M23
01 54
  }
 |0i b  •
 
  76
01M54
23
The command \Qcircuit is simply a disguised
 
 |0i c  • \xymatrix command with a default parameter set. For
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ readers unfamiliar with the xymatrix environment, it
suffices to know that it behaves more or less like the
Typesetting quantum circuit diagrams using standard array environment. That is, new columns are denoted
LATEX graphics packages is a difficult and time consuming by & and new rows by \\, as in the following example:
business. Q-circuit is a high level macro package designed
to change that. With Q-circuit, drawing quantum circuit a i
diagrams is as easy as constructing an array. In a mat- 1 x
ter of minutes you can learn the basic syntax and start
producing circuits of your own. which was typeset using
This tutorial teaches you to use Q-circuit from the \Qcircuit @C=1.4em @R=1.2em {
ground up. Many readers will find that they’ve learned a & i \\
everything they need to know by the end of §III, but 1 & x
plenty of material is included for those that wish to type- }
set more complicated circuits.
The parameters @C=1.4em and @R=1.2em that appear
after \Qcircuit specify the spacing between the columns
and the rows of the circuit, respectively. They may take
1 The Q-circuit package is distributed under the GNU public license. any length as an argument. Additional parameters are
2 Code for these circuits is given in Appendix C. discussed in §V A.
2

A. Wires and gates \Qcircuit @C=1em @R=.7em {


& \ctrl{1} & \targ & \qw \\
& \targ & \ctrl{-1} & \qw
The command \qw draws a wire between two columns
}
of a QCD. The command derives its name from an ab-
breviation of ‘quantum wire’. In this circuit, the command \targ draws the target
gate on the wire, and the \ctrl{#1} puts a bullet down,
H Z H and connects to the target which is #1 array elements
below the control. Hence, to connect the second CNOT
X
gate properly, we used -1.
The diagram above was drawn using A more complicated circuit with multiple controls and



arbitrary gates might look like
\Qcircuit @C=1em @R=.7em {
& \gate{H} & \gate{Z} & \gate{H} & \qw \\ • U



& \qw & \gate{X} & \qw & \qw •
} • •
Note that \qw is used to connect a wire towards the left. •
The \gate command draws the argument of the func- which was drawn using
tion inside a framed box and extends a wire back to the \Qcircuit @C=1em @R=.7em {
previous column. When using the \gate and \qw com- & \ctrl{2} & \targ & \gate{U} & \qw \\
mands, make sure there is another column entry to the & \qw & \ctrl{-1} & \qw & \qw \\
left of the current column entry in your QCD, otherwise & \targ & \ctrl{-1} & \ctrl{-2} & \qw \\
the wire will not connect to anything (and you’ll get an & \qw & \ctrl{-1} & \qw & \qw
error), as in the following example code: }

(**Wrong!**) In the first gate, the control bit connects to the target
\Qcircuit @C=1em @R=.7em { on wire 3. In the second gate, each control connects to
\gate{U} & \qw \\ the object directly above it. Finally, the third gate is an
\gate{U^\dag} & \qw example of how to do controls on arbitrary gates; simply
} place the desired gate where you would normally put a
target.
The proper way to render this circuit would be to in-
clude space for the incoming wires at the beginning by
inserting the & character at the start of each new line: C. Vertical wires
U
Suppose we want to typeset the following circuit:
U†
U1
\[ \Qcircuit @C=1em @R=.7em {
& \gate{U} & \qw \\ •
& \gate{U^\dag} & \qw
} \]
U2

The only difference between these two codes is that the so that the middle control has to connect to more than
one gate. The way to accomplish this is with the \qwx
correct code has an ampersand (&) at the start of each command. The command \qwx[#1] takes an optional
new line. input, #1, and connects from the current position to a
position #1 entries below the current position. The de-
fault argument is -1. Thus, one way to typeset the above
B. CNOT and other controlled single qubit gates diagram is with the following code:
\Qcircuit @C=1em @R=1.2em {
With just these few commands, one can already ren- & \gate{U_1} & \qw \\
der a circuit with an arbitrary number of wires and sin- & \ctrl{-1} \qwx[1] & \qw \\
gle qubit gates. In this section, we’ll learn how to draw & \gate{U_2} & \qw \\
CNOT gates and controlled single qubit gates with an }
arbitrary number of controls.
or, equivalently,

• 

A simple circuit with two CNOT gates in it is
\Qcircuit @C=1em @R=1.2em {

 •

& \gate{U_1} & \qw \\
& \ctrl{1} \qwx & \qw \\
& \gate{U_2} & \qw \\
which was typeset by }
3

which is what the author used. \Qcircuit @C=1em @R=.7em {


Note that wire commands must not precede the gate & \multigate{2}{U^\dag} & \qw \\
command in an entry. Also, remember that commands & \ghost{U^\dag}& \qw \\
taking an optional argument use square braces rather & \ghost{U^\dag} & \qw
}
than curly braces.
First let’s go over the \multigate command.
\multigate{#1}{#2} is a two argument gate that takes
D. Labelling input and output states the depth of the gate for the first argument and the label
of the gate for the second argument. In the above ex-
The last element we need for simple circuits is the abil- ample, #1 equals 2 because the 3-qubit gate extends two
ity to add labels. We’ll look at input and output labels rows below the position of \multigate. On the other
here, other kinds of labels are discussed in §V B. two lines, the \ghost command is used to get the spacing
When labelling input and output qubits, one should and connections right. \ghost behaves like an invisible
use the \lstick and \rstick commands. These com- gate that allows the quantum wires on either side of your
mands ensure that the labels and the wires connecting to multigate to connect correctly.
The generalization to an arbitrarily large gate is now
them line up correctly. The \lstick command is used
obvious. Let’s look at a 6-qubit gate. The code
for input labels (on the left of the diagram), and the
\rstick command is used for output labels (on the right \Qcircuit @C=1em @R=0em {
of the diagram). Placement rules are the same as those & \multigate{5}{\mathcal{F}} & \qw \\
for gates with the exception that \lstick and \rstick & \ghost{\mathcal{F}} & \qw \\
can be inserted in the leftmost column of the array. Here & \ghost{\mathcal{F}} & \qw \\
is an example circuit: & \ghost{\mathcal{F}} & \qw \\



& \ghost{\mathcal{F}} & \qw \\
& \ghost{\mathcal{F}} & \qw
|1i |0i }
|1i • |1i
yields
typeset with

\Qcircuit @C=1em @R=1em {


\lstick{\ket{1}} & \targ & \rstick{\ket{0}} \qw \\ F
\lstick{\ket{1}} & \ctrl{-1} & \rstick{\ket{1}} \qw
}

Thus, for every entry below the top, a \ghost command


IV. MORE COMPLICATED CIRCUITS: with the label for the gate is needed. Strictly speaking,
MULTIPLE QUBIT GATES AND BEYOND the name of the gate is not necessary inside the \ghost
command. Since \ghost is just an invisible place holder,
So far, we have seen how to make arbitrary QCDs in- anything with the same width as the label specified in
volving single qubit gates and controlled gates, including multigate will work as well. In practice, however, it is
CNOT. Since this is known to be universal for compu- usually easiest to use the same argument.
tation, we could just stop here! Of course, many circuit Note that controls to multiple qubit gates work the
diagrams use more complicated structures such as multi- same as for single qubit gates, using \ctrl and \qwx.
qubit gates, measurements, classical wires, and swaps.
We will learn how to use Q-circuit to make all of these
B. Measurements and classical bits
in this section.
Measurement gates are typeset just like ordinary gates,
but they typically have some sort of decoration to in-
A. Multiple qubit gates dicate that measurement has occurred. At present, Q-
circuit supports the following single qubit measurement
Let’s look at an example, and then we’ll explain the gates.
code.
Example Command Example Code

FE
/.Basis*+
() -,
\meter \meter

U \measure \measure{\mbox{Basis}}
44 Mijk
χ"%#$
\measuretab \measuretab{M_{ijk}}

The 3-qubit gate above was typeset with \measureD \measureD{\chi}


4

Often we want to condition some gate on the output \Qcircuit @C=1em @R=.3em {
of a measurement. One convenient way illustrate this is & & \mbox{Defective Circuit}\\
with the classical wire commands, \cw and \cwx. The & \qswap & \qw & \push{\rule{0em}{1em}} \qw \\
classical wire commands work exactly like the quantum & \qswap \qwx & \push{X} \qw & \qw \\
wire commands, but they draw double instead of single & {/} \qw & \gate{H^{\otimes n}} & \qw
}
lines.
Here is an example using measurement gates and clas-
sical wires and the corresponding code.
/.Codebit*+
() -,
D. How to control anything

Controlled-Z gates, wires with bends, and gates that


FE
χ ··· control-on-zero can all be made using the extended family
of control commands. The complete family of control
\Qcircuit @C=1em @R=.7em { commands is \ctrl, \crtlo, \control, and \controlo.
& \qw & \measure{\mbox{Codebit}} \cwx[1] \\ \ctrlo is identical to the \ctrl command (see §III B)
& \qw & \gate{\chi} & \meter & except that it draws an open bullet (indicating control-
\rstick{\cdots} \cw
on-zero). Both commands place a wire to the left and
}
take one argument indicating which wire to connect to.
Q-circuit also includes the commands \multimeasure The commands \control and \controlo are isolated
and \multimeasureD for typesetting measurements on controls; they don’t automatically connect to anything.
multiple qubits. The syntax for these commands exactly Isolated controls allow you to decide exactly what con-
parallels that of the \multigate command (see §IV A). nections are made to your control operator, which makes
An example is shown below. them very useful for working with classical wires and ren-

=<
dering things like the controlled-Z.
Bell:;
• 
Here is an example circuit using various controls.


 

U


\Qcircuit @C=1em @R=.7em { •

FE
& \multimeasureD{1}{\text{Bell}} \\
& \ghost{\text{Bell}} • •

FE
}
• H •
\Qcircuit @C=1em @R=.7em {
C. Non-gate inserts, forcing space, and swap & \ctrl{2} & \ctrlo{1} & \ctrl{1}
& \qw & \multigate{1}{U} & \qw \\
In addition to the gates defined by Q-circuit, standard & \qw & \targ & \ctrlo{2} \qw
LATEX can function as a gate if enclosed in curly brack- & \ctrl{1} & \ghost{U} & \qw \\
ets. By default, inputs are assumed to have zero size, so & \control \qw & \ctrl{1} & \qw
no space will be made for the resulting object and any & \meter & \controlo \cw \cwx \\
wires connecting to it will run straight to the object’s & \qw & \control \qw & \gate{H}
& \meter & \control \cw \cwx
middle. Standard LATEX entries can serve as labels or
}
wire decorations.
To force an object to take up space, you should use Note that we, the authors, have used a pair of controls
the \push command. \push is most useful in conjunc- connected by a wire to denote the controlled-Z gate. This
tion with the LATEX command \rule. Together they can isn’t standard notation, but we feel it is a logically con-
be used to construct various sorts of invisible props and sistent and concise notation, and it illustrates nicely the
struts. symmetry of the controlled-Z gate. We hope to encourage
Q-circuit implements a gate command called \qswap the readers to adopt this notation in their own QCDs.
that is equivalent to the text {\times} \qw. The effect
of \qswap is to insert half of a swap gate (that is a ×)
which can then be connected (using \qwx) to another V. BELLS AND WHISTLES: TWEAKING
instance of \qswap to create a swap gate. YOUR DIAGRAM TO PERFECTION
Here is a circuit that shows how to construct swap,
decorate wires, and use \push to make an invisible prop. By now, the reader should be able to quickly and easily
typeset almost any QCD. Nonetheless, it may occasion-
Defective Circuit ally be desirable to decorate or modify a circuit in ways
×
not yet discussed. This section presents additional tricks,
× X
options, and commands for putting the final polish on
/ H ⊗n your QCDs.
5

A. Spacing Proper usage of \lstick and \rstick was demon-


strated in §III D, so the following example focuses on
The Q-circuit parameters @R and @C were introduced in \ustick and \dstick.

FE
§III; they are examples of a family of spacing parameters a
that can appear between the text \Qcircuit and the
FE
b
opening curly brace. A more complete list of available
parameters is given in the table below.
B A
Parameter Effect
\Qcircuit @C=.7em @R=.3em {
@R=#1 Sets the spacing between rows to #1. & \ustick{a} \qw & \qw & \qw & \qw & \meter \\
@C=#1 Sets the spacing between columns to #1. & \ustick{b} \qw & \qw & \qw & \meter \\
& & & & \dstick{B} \cwx & \dstick{A} \cwx[-2]
@!R Sets all rows to the height of the tallest }
object in the circuit.
@!C Sets all columns to the width of the widest
object in the circuit. C. Grouping
@! Sets all entries to the size of the largest
object in the circuit. It is sometimes useful to box off sections of a circuit to
indicate a subcircuit, as in the following example.
The @R and @C parameters adjust the separation be-
_ _ _ _ _ _ _ 
tween elements, allowing you to dictate the compactness
•  H • H 

•  H   H
of your QCD. @!R, @!C, and @! force the elements of
your circuit to have uniform sizes, this helps to prevent  

 
  Z
bunching that may occur when a particular row or col- _ _ _ _ _ _ _
umn contains many small elements. @!R is particularly

useful for forcing wires to be evenly spaced, as in the
following example. which was typeset using
× × \Qcircuit @C=1em @R=1em {
& \ctrl{2} & \qw & \gate{H} & \ctrl{1} &
× • × \gate{H} & \qw \\
& \qw & \ctrl{1} & \gate{H} & \targ &
T† \gate{H} & \qw \\
& \targ & \targ & \gate{Z} & \qw & \ctrl{-1} &
\Qcircuit @C=.7em @R=.3em @!R { \qw \gategroup{1}{4}{2}{6}{.7em}{--}
& \qswap & \qw & \qswap & \qw\\ }
& \qswap \qwx & \ctrl{1} & \qswap \qwx & \qw \\
& \qw & \gate{T^\dag} & \qw & \qw The command that made the dashed box is in the last
} line of code and is called \gategroup. The \gategroup
command can be placed following any non-empty entry,
but, for clarity, it is perhaps best to put it at the end.
B. Labelling Because it takes six arguments, \gategroup looks
intimidating, but it is actually relatively easy to use.
A label can be placed anywhere that a gate command \gategroup{#1}{#2}{#3}{#4}{#5}{#6} highlights the
might normally appear. Unlike gates, however, Q-circuit entries between rows #1 and #3 and columns #2 and #4 by
treats labels as having zero size when determining the adding a box or a bracket. Argument #6 selects between
layout of a QCD. This prevents large labels from bending various highlights, with the available options being:
your circuit out of whack, but it also means that labels
-- . _\} ^\} \{ \} _) ^) ( )
can overlap with other components.
Normally an element whose size is set to zero is drawn These options produce a dashed box, a dotted box, a
centered on it’s entry. This is what happens when you curly brace on the bottom, top, left, or right, and a nor-
insert text directly using curly brackets (see §IV C). For mal brace on the bottom, top, left, or right. Argument
most labelling, however, it is more useful to have one edge #5 is twice the spacing from the nearest gate to the box.
of the label fixed in the center of an entry. For this rea- \gategroup only checks that the gates at the four cor-
son Q-circuit provides a set of label commands, \lstick, ners of the requested region are properly enclosed. As a
\rstick, \ustick, and \dstick. The stick commands result, gates along the boundary that are bigger than the
each cause their contents to “stick out” from the center corner gates will tend to stick out. This is especially un-
of an entry in a different direction. \lstick, \rstick, sightly when the corner entries are wires, though in that
\ustick, and \dstick produce labels that project out to case the problem can be fixed by inserting an invisible
the left, right, top, and bottom respectively. prop of the desired height (see §IV C).
6

VI. ACKNOWLEDGMENTS APPENDIX C: CODE FOR THE


INTRODUCTION
The authors would like to thank Aaron Smith, Joe
Renes, and Andrew Silberfarb for useful discussions, The first QCD depicts a way of decomposing doubly
ideas, and debugging. Thanks to Carl Caves and Michael controlled unitaries. It was typeset with
Nielsen for encouragement on this project. An extra
thanks to Michael Nielsen for suggesting some useful
LATEX resources. \Qcircuit @C=.5em @R=0em @!R {
The development of Q-circuit was supported in part & \ctrl{1} & \qw & & & \qw & \ctrl{1} & \qw &
by the National Security Agency (NSA) and the Ad- \ctrl{1} & \ctrl{2} & \qw\\
& \ctrl{1} & \qw &
vanced Research and Developement Activity (ARDA)
\push{\rule{.3em}{0em}=\rule{.3em}{0em}} & &
under the Army Research Office (ARO) contract num- \ctrl{1} & \targ & \ctrl{1} & \targ & \qw &
bers DAAD19-01-1-0648 and W911NF-04-1-0242. \qw\\
& \gate{U} & \qw & & & \gate{V} & \qw &
\gate{V^\dag} & \qw & \gate{V} & \qw
}
APPENDIX A: POSITIONING Q-CIRCUIT
DIAGRAMS IN LATEX
The second QCD depicts quantum teleportation and
Q-circuit produces TEX graphics objects. In theory was typeset with
these objects should act like any symbol or character.
Thus, they can be placed in equation environments, ar-
rays, and figures. In practice there are a few, largely \Qcircuit @C=.7em @R=.4em @! {
unexplained, complications. \lstick{\ket{\psi}} & \qw & \qw & \ctrl{1} &
\gate{H} & \meter & \control \cw\\
One of these is vertical centering in a line of text. To \lstick{\ket{0}} & \qw & \targ & \targ & \qw &
center the top line of a circuit, it is sufficient to invoke it \meter & \cwx\\
in inline math mode using $. To center the entire circuit, \lstick{\ket{0}} & \gate{H} & \ctrl{-1} & \qw &
place it inside an array. \qw & \gate{X} \cwx & \gate{Z} \cwx &
Horizontal centering within figures is also problem- \rstick{\ket{\psi}} \qw
atic. Typically this can be corrected by placing the }
\Qcircuit command inside a \centerline command,
an \mbox command, or an equation environment. For
The third QCD depicts quantum error correction on
some LATEX distributions the commands \leavevmode the bit flip code. It was typeset with
and \centering must be added to center a figure.
Finally, circuits using large labels often appear a bit
off center. This is because labels are not included when \Qcircuit @C=1.3em @R=.6em {
calculating the size of a circuit. The best solution is & & & & & & \mbox{Syndrome Measurement} & & & &
probably to add white space (see §IV C) until the labels \mbox{Recovery}\\
all fit within the boundaries of the circuit. & \qw & \qw & \ctrl{3} & \qw & \qw & \qw &
\ctrl{5} & \qw & \qw &
\multigate{2}{\ \mathcal{R}\ } & \qw\\
& \qw & \qw & \qw & \ctrl{2} & \ctrl{3} & \qw &
APPENDIX B: BUGS AND FUTURE WORK \qw & \qw & \qw & \ghost{\ \mathcal{R}\ } \qw &
\qw\\
& \qw & \qw & \qw & \qw & \qw & \ctrl{2} & \qw &
1. Wires often end just short of curved surfaces.
\ctrl{3} & \qw & \ghost{\ \mathcal{R}\ } \qw &
\qw\\
2. \gategroup needs to check all the boundary gates & & \lstick{\ket{0}} & \targ \qw & \targ \qw &
when determining the highlighted area. \qw & \qw & \qw & \qw & \measure{M_a} &
\control \cw \cwx\\
& & \lstick{\ket{0}} & \qw & \qw & \targ \qw &
3. Targets look poor when the font size is set to small.
\targ \qw & \qw & \qw & \measure{M_b} &
\control \cw \cwx\\
4. It would be nice if the \ghost command could read & & \lstick{\ket{0}} & \qw & \qw & \qw & \qw &
the argument of the \multigate command auto- \targ \qw & \targ \qw & \measure{M_c}
matically. \gategroup{2}{2}{7}{10}{.8em}{--} &
\control \cw \cwx
}
5. Larger issues of centering within LATEX need to be
addressed.
7

APPENDIX D: TABLE OF COMMANDS

The following table is grouped according to the effect


of each command.

Subject Command
Loading Q-circuit \input{Qcircuit}
Making Circuits \Qcircuit
Spacing @C=#1
@R=#1
@!R
@!C
@!
\push{#1}
Wires \qw[#1]
\qwx[#1]
\cw[#1]
\cwx[#1]
Gates \gate{#1}
\targ
\qswap
\multigate{#1}{#2}
\ghost{#1}
Controls \ctrl{#1}
\ctrlo{#1}
\control
\controlo
Measurements \meter
\measure{#1}
\measureD{#1}
\measuretab{#1}
\multimeasure{#1}{#2}
\multimeasureD{#1}{#2}
Labels \lstick{#1}
\rstick{#1}
\ustick{#1}
\dstick{#1}
\bra{#1}
\ket{#1}
\gategroup{#1}{#2}{#3}{#4}{#5}{#6}

You might also like