(Not finished !)
The goal of this post is to create a tutorial and a quick reference for the qcircuit latex package which can draw quantum circuits. This post aims to be an extensive and full reference for anyone who wants to start drawing quantum circuits. Doing everything by myself is very long and any contributions, in the form of an edit or an answer, are welcome.
Introduction
Latex uses the Tex typesetting system to render texts and math in a beautiful way. Qcircuit is a latex package which was developed by Bryan Eastin and Steve Flammia at the University of New Mexico. It works using the xy-pic package to render circuits. We will explore all the different options of the package going from the package basics to advanced options.
This is an example of a circuit, at the end you will understand the inner workings of this.
Basics
\Qcircuit
Don't forget to use \usepackage{Qcircuit}
. There are 2 more parameters you can use :
\usepackage[braket]{Qcircuit}
which will also import the commands\ket{}
and\bra{}
which render as $|A\rangle$ and $\langle A|$ respectively.\usepackage[braket]{Qcircuit}
which will also import the commands\ip{}{}
,\op{}{}
,\melem{}{}{}
and\expvalue{}
which render as $\langle A|B\rangle$, $|A\rangle\langle B|$, $\langle A|B|C\rangle$ and $\langle A\rangle$.
To create a circuit, use the command \Qcircuit
. You have to input the height and the width :
@R=#1
sets the height between the rows to 1, you can also use@!R
to set the height to the height of the highest object.@C=#1
sets the width between the columns to 1, you can also use@!C
to set the width to the width of the widest object.@!
sets the width and height to the height of the highest object ans the width of the widest object.
A circuit is rendered under the form a matrix, where every element has a position and the rendering is done considering the different positions. A matrix is formed with &
to indicate a new element and a \\
to indicate a new line. For example : 1 & 0 \\ 0 & 1
would be the identity matrix.
You then have to give the input for the circuit in curly braces. We now know how to create a basic circuit
Simple Gates
Creating a single-qubit gate is simply done by using \gate{}
command. It only requires one parameter, the name of the gate which can be anything. The gate will have a wire on its left side.
|----|
---| X | (Insert image of X gate)
|----|
Can be reached with this code :
\Qcircuit @C=1em @R=.7em {
& \gate{X}
}
Wires
Wires can connect different elements together or leave the equivalent of a blank space in a circuit. You can draw a wire with the \qw
command. The wire will connect to the element to the left. Therefor, if you use a wire or a gate, you have to have a column to the left of it. If you want a classical wire, use the \cw
command. If you are at the end of the circuit you can use \qwa
which renders similar to $\rightarrow$.
More Gates
Controlled Gates
To add a control wire, use the \ctrl{#1}
function. The function takes in 1 parameter which indicates to which wire up or down to connect to. If the parameter is positive it will connect to wire #1 downwards and if it is negative it will connect to wire #1 upwards.
CNOT
To create a CNOT gate use the control wire above and add the \targ
command at the target wire.
---*---
| (Insert CNOT image)
---0---
Can be reached with :
\Qcircuit @C=1em @R=.7em {
& \ctrl{1} & \qw \\
& \targ & \qw
}
Other simple controlled gates
For other controlled gates, simply exchange the \targ
with the \gate{}
you want.
Vertical wires
Use the \qwx[#1]
command to create vertical wire. Beware the square brackets, because the argument is optional. The argument works like the \ctrl{}
argument, however it has a default value of -1. If you want to add a vertical wire, simply add the \qwx
command after the other element in the matrix, do not add a &
though.
---X---
|
---*--- (Insert image of control X and CNOT with same wire as control)
|
---0---
Can be reached with :
\Qcircuit @C=1em @R=.7em {
& \gate{X} & \qw \\
& \ctrl{1} \qwx & \qw \\
& \targ & \qw
}
Adjancent Multi-Qubit Gates
Multiple-qubit gates, one adjacent wires, use the \multigate{#1}{#2}
command. Parameter #1 gives the depth of the gate. It works like the controlled gates. The second parameter is the name of the gate, it can be anything. To input wires into other parts of the multigate, you have to use the \ghost
placeholder :
\ghost
will add an invisible placeholder with a quantum wire\cghost
will add an invisible placeholder with a classical wire\nghost
will add an invisible placeholder with no wire
---|---|---
===| U |=== (Insert image of multi-gate)
| |
---|---|---
Can be reached with :
\Qcircuit @C=1em @R=.7em {
& \ghost{U} & \qw \\
& \cghost{U} & \cw \\
& \nghost{U} & \\
& \multigate{-3}{U} & \qw
}
Separated Multi-Qubit gates
To do multigates on separated wires can be obtained using the \sgate{#1}{#2}
. First parameter is the name of the gate and the second parameter is to which wire the gate has to connect to. It works like controlled gates. The gate it connects to is simply a normal \gate{}
---|---|---S---
| U | |
---|---|---|---
|
-----------S---
Can be reached with :
\Qcircuit @C=1em @R=.7em {
& \multigate{1}{U} & \sgate{S}{2} & \qw
& \ghost{U} & \qw & \qw
& \qw & \gate{S} & \qw
}
Measurements
Single Qubit Measurements
There are 6 different symbols that can be used to represent measurement.
\meter
will create the basic measurement gate, it is generated like a regular gate.\measure{#1}
creates a measurement gate with text inside it\measuretab{#1}
works like\measure{#1}
\measureD{#1}
works like\measure{#1}
\meterB{#1}
generates like\meter
but you can add a custom base\metersymb
generates the measure symbol alone
Multiple Qubit Measurements
You can use \multimeasure
and \multimeasureD
, which works exactly like \multigate
, for adjacent qubits.
For separated qubits, use \smeterB{#1}
, which works exactly like \sgate
.
Example
(Insert Image)
Can be reached with :
\Qcircuit @C=1em @R=.7em {
& \smeterB{\ket{\xi_{\pm}}}{2} & \measure{\mbox{Codebit}} \cwx[1] \\
& \qw & \gate{\chi} & \meter & \multimeasureD{1}{\text{Bell}} \\
& \gate{\ket{\xi_{\pm}}} & \qw & \qw & \ghost{\text{Bell}} \\
}
Adding other stuff
Non qcircuit gates
References
Here are a couple of links to go further into qcircuit :