Q-circuit Tutorial
greatly simplies the construction of quantum circuit diagrams
(QCDs) in L
A
TEX 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 free
1
at
http://info.phys.unm.edu/Qcircuit/
.
I.
INTRODUCTION
Ever tried to use L
A
TEX to typeset something like this?
=
10)(
$%&' 10)($%&'
U
V
V
V
Or maybe this?
|
H
pi
|0
10)(
$%&' 10)($%&' pi
|0
H
X
Z
|
Or how about
2
Syndrome Measurement
Recovery
R
|0
10)(
$%&' 10)($%&'
UT SR
HI PQM
a
|0
10)(
$%&' 10)($%&' UTSRHIPQM
b
|0
10)(
$%&' 10)($%&' UTSRHIPQM
c
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Typesetting quantum circuit diagrams using standard
L
A
TEX graphics packages is a dicult and time consuming
business. Q-circuit is a high level macro package designed
to change that. With Q-circuit, drawing quantum circuit
diagrams is as easy as constructing an array. In a mat-
ter of minutes you can learn the basic syntax and start
producing circuits of your own.
This tutorial teaches you to use Q-circuit from the
ground up. Many readers will nd that theyve learned
everything they need to know by the end of §
III
, but
plenty of material is included for those that wish to type-
set more complicated circuits.
1
The Q-circuit package is distributed under the GNU public license.
2
Code for these circuits is given in Appendix
C
.
II.
GETTING STARTED
To install Q-circuit, place the le Qcircuit.tex some-
where your TEX distribution can nd it and run the ap-
propriate command to update your TEX tree. To use it,
place the command
\input{Qcircuit}
in the preamble of your document. Qcircuit.tex loads
the amsmath and xy packages and implements a set of cir-
cuit commands. If need be, you can obtain the necessary
packages at
http://www.ctan.org/
.
III.
SIMPLE QUANTUM CIRCUITS
To begin, suppose the reader would like to typeset the
following simple circuit:
X
This was typeset using
\Qcircuit @C=1em @R=.7em {
& \gate{X} & \qw
}
The command \Qcircuit is simply a disguised
\xymatrix command with a default parameter set. For
readers unfamiliar with the xymatrix environment, it
suces to know that it behaves more or less like the
array environment. That is, new columns are denoted
by & and new rows by \\, as in the following example:
a
i
1 x
which was typeset using
\Qcircuit @C=1.4em @R=1.2em {
a & i \\
1 & x
}
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
any length as an argument. Additional parameters are
discussed in §
V A
.
2
A.
Wires and gates
The command \qw draws a wire between two columns
of a QCD. The command derives its name from an ab-
breviation of quantum wire.
H
Z
H
X
The diagram above was drawn using
\Qcircuit @C=1em @R=.7em {
& \gate{H} & \gate{Z} & \gate{H} & \qw \\
& \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-
tion inside a framed box and extends a wire back to the
previous column. When using the \gate and \qw com-
mands, make sure there is another column entry to the
left of the current column entry in your QCD, otherwise
the wire will not connect to anything (and youll get an
error), as in the following example code:
(**Wrong!**)
\Qcircuit @C=1em @R=.7em {
\gate{U} & \qw \\
\gate{U^\dag} & \qw
}
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:
U
U
\[ \Qcircuit @C=1em @R=.7em {
& \gate{U} & \qw \\
& \gate{U^\dag} & \qw
} \]
The only dierence between these two codes is that the
correct code has an ampersand (&) at the start of each
new line.
B.
CNOT and other controlled single qubit gates
With just these few commands, one can already ren-
der a circuit with an arbitrary number of wires and sin-
gle qubit gates. In this section, well learn how to draw
CNOT gates and controlled single qubit gates with an
arbitrary number of controls.
A simple circuit with two CNOT gates in it is
10)(
$%&'10)($%&'
which was typeset by
\Qcircuit @C=1em @R=.7em {
& \ctrl{1} & \targ & \qw \\
& \targ & \ctrl{-1} & \qw
}
In this circuit, the command \targ draws the target
gate on the wire, and the \ctrl{#1} puts a bullet down,
and connects to the target which is #1 array elements
below the control. Hence, to connect the second CNOT
gate properly, we used -1.
A more complicated circuit with multiple controls and
arbitrary gates might look like
10)(
$%&' U
10)(
$%&'
which was drawn using
\Qcircuit @C=1em @R=.7em {
& \ctrl{2} & \targ & \gate{U} & \qw \\
& \qw & \ctrl{-1} & \qw & \qw \\
& \targ & \ctrl{-1} & \ctrl{-2} & \qw \\
& \qw & \ctrl{-1} & \qw & \qw
}
In the rst gate, the control bit connects to the target
on wire 3. In the second gate, each control connects to
the object directly above it. Finally, the third gate is an
example of how to do controls on arbitrary gates; simply
place the desired gate where you would normally put a
target.
C.
Vertical wires
Suppose we want to typeset the following circuit:
U
1
U
2
so that the middle control has to connect to more than
one gate. The way to accomplish this is with the \qwx
command. The command \qwx[#1] takes an optional
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
diagram is with the following code:
\Qcircuit @C=1em @R=1.2em {
& \gate{U_1} & \qw \\
& \ctrl{-1} \qwx[1] & \qw \\
& \gate{U_2} & \qw \\
}
or, equivalently,
\Qcircuit @C=1em @R=1.2em {
& \gate{U_1} & \qw \\
& \ctrl{1} \qwx & \qw \\
& \gate{U_2} & \qw \\
}
3
which is what the author used.
Note that wire commands must not precede the gate
command in an entry. Also, remember that commands
taking an optional argument use square braces rather
than curly braces.
D.
Labelling input and output states
The last element we need for simple circuits is the abil-
ity to add labels. Well look at input and output labels
here, other kinds of labels are discussed in §
V B
.
When labelling input and output qubits, one should
use the \lstick and \rstick commands. These com-
mands ensure that the labels and the wires connecting to
them line up correctly. The \lstick command is used
for input labels (on the left of the diagram), and the
\rstick command is used for output labels (on the right
of the diagram). Placement rules are the same as those
for gates with the exception that \lstick and \rstick
can be inserted in the leftmost column of the array. Here
is an example circuit:
|1
10)(
$%&' |0
|1
|1
typeset with
\Qcircuit @C=1em @R=1em {
\lstick{\ket{1}} & \targ & \rstick{\ket{0}} \qw \\
\lstick{\ket{1}} & \ctrl{-1} & \rstick{\ket{1}} \qw
}
IV.
MORE COMPLICATED CIRCUITS:
MULTIPLE QUBIT GATES AND BEYOND
So far, we have seen how to make arbitrary QCDs in-
volving single qubit gates and controlled gates, including
CNOT. Since this is known to be universal for compu-
tation, we could just stop here! Of course, many circuit
diagrams use more complicated structures such as multi-
qubit gates, measurements, classical wires, and swaps.
We will learn how to use Q-circuit to make all of these
in this section.
A.
Multiple qubit gates
Lets look at an example, and then well explain the
code.
U
The 3-qubit gate above was typeset with
\Qcircuit @C=1em @R=.7em {
& \multigate{2}{U^\dag} & \qw \\
& \ghost{U^\dag}& \qw \\
& \ghost{U^\dag} & \qw
}
First
lets
go
over
the
\multigate
command.
\multigate{#1}{#2} is a two argument gate that takes
the depth of the gate for the rst argument and the label
of the gate for the second argument. In the above ex-
ample, #1 equals 2 because the 3-qubit gate extends two
rows below the position of \multigate. On the other
two lines, the \ghost command is used to get the spacing
and connections right. \ghost behaves like an invisible
gate that allows the quantum wires on either side of your
multigate to connect correctly.
The generalization to an arbitrarily large gate is now
obvious. Lets look at a 6-qubit gate. The code
\Qcircuit @C=1em @R=0em {
& \multigate{5}{\mathcal{F}} & \qw \\
& \ghost{\mathcal{F}} & \qw \\
& \ghost{\mathcal{F}} & \qw \\
& \ghost{\mathcal{F}} & \qw \\
& \ghost{\mathcal{F}} & \qw \\
& \ghost{\mathcal{F}} & \qw
}
yields
F
Thus, for every entry below the top, a \ghost command
with the label for the gate is needed. Strictly speaking,
the name of the gate is not necessary inside the \ghost
command. Since \ghost is just an invisible place holder,
anything with the same width as the label specied in
multigate will work as well. In practice, however, it is
usually easiest to use the same argument.
Note that controls to multiple qubit gates work the
same as for single qubit gates, using \ctrl and \qwx.
B.
Measurements and classical bits
Measurement gates are typeset just like ordinary gates,
but they typically have some sort of decoration to in-
dicate that measurement has occurred. At present, Q-
circuit supports the following single qubit mea