LabCanvas

Quantikz: quantum circuit diagrams in LaTeX

Quick answer

Load \usetikzlibrary{quantikz2} after \usepackage{tikz} (or \usepackage{quantikz}, which in the current version 1.0.1 loads the same library) and write the circuit as a grid inside \begin{quantikz} … \end{quantikz}: every row is a qubit wire, & separates the time steps and \\ starts the next wire. The wires are drawn for you. Put one gate command in a cell: \gate{H} for a box, \ctrl{1} for a control dot whose line goes one row down to a \targ{} (together a CNOT), \meter{} for a measurement and \lstick{\ket{0}} for the label at the start of a wire. Gate labels are already in math mode; \lstick text is not.
Quantum circuit with two wires labelled |0⟩: an H gate on the top wire, a CNOT with its control dot on the top wire and ⊕ on the bottom wire, and a measurement meter at the end of each wire.
Bell state: Hadamard, CNOT and measurement of both qubits
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}
  \lstick{\ket{0}} & \gate{H} & \ctrl{1} & \meter{} \\
  \lstick{\ket{0}} &          & \targ{}  & \meter{}
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}
  \lstick{\ket{0}} & \gate{H} & \ctrl{1} & \meter{} \\
  \lstick{\ket{0}} &          & \targ{}  & \meter{}
\end{quantikz}
\end{document}
Contents

Loading quantikz: quantikz2 and the legacy library

Quantikz, by Alastair Kay, is on CTAN and in TeX Live and MiKTeX. The current release, 1.0.1 of 2023-05-24, ships two TikZ libraries, and the README calls quantikz2 “the current release” and the old one “legacy”:

Preamble lineWhat you get
\usetikzlibrary{quantikz2}Version 1.0.1, the form the tutorial recommends (after \usepackage{tikz})
\usepackage{quantikz}The same: in 1.0.1, quantikz.sty is a three-line wrapper that loads TikZ and \usetikzlibrary{quantikz2}
\usetikzlibrary{quantikz}The legacy library, version 0.9.8, kept so that old documents still look as they did

So the answer to “quantikz or quantikz2?” is quantikz2, and with an up-to-date TeX distribution \usepackage{quantikz} already gives it to you. The two differ mainly in the wires. In quantikz2 every cell gets a wire by default, so the \qw and \cw commands of the old syntax are unnecessary (old code that still contains \qw renders the same). The legacy library draws only the wire coming into a gate from the left, so every empty cell needs \qw, as the picture below shows. The tutorial’s conversion notes (§VII.A) list the other changes: labels of \meter are now in math mode, \ctrlbundle is replaced by plain \ctrl, styling goes through keys such as [style={…}], and quantikz2 is less forgiving when \targ and similar commands lack their {}.

Use the quantikz environment, not tikzcd (which also works, as quantikz is built on tikz-cd, but is kept only for compatibility). If you submit to arXiv or a journal whose TeX installation is older than 2023, copy tikzlibraryquantikz2.code.tex from your TeX tree into the folder with your source, as the tutorial advises. If you use quantikz in a paper, the author asks you to cite arXiv:1809.03842.

Two-wire circuit with H and a CNOT. The top wire is complete; the bottom wire has a short segment only between the middle of the circuit and the ⊕, with no wire after the |0⟩ label and none after the ⊕.
The legacy quantikz library (0.9.8): empty cells need \qw, or the wire is missing
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz}
\begin{quantikz}
  % legacy library (0.9.8): a gate draws only the wire coming in from the left,
  % so empty cells and the last cell need \qw
  \lstick{\ket{0}} & \gate{H} & \ctrl{1} & \qw \\
  % the same row without \qw: two wire segments are missing
  \lstick{\ket{0}} &          & \targ{}  &
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz}
\begin{document}
\begin{quantikz}
  % legacy library (0.9.8): a gate draws only the wire coming in from the left,
  % so empty cells and the last cell need \qw
  \lstick{\ket{0}} & \gate{H} & \ctrl{1} & \qw \\
  % the same row without \qw: two wire segments are missing
  \lstick{\ket{0}} &          & \targ{}  &
\end{quantikz}
\end{document}

The grid: wires, cells and single-qubit gates

A circuit is a matrix: cells separated by &, rows by \\. A gate in a cell gets a wire coming in from the cell on its left and one going out to the cell on its right, so three rules avoid most surprises:

  • Leave the first cell of a row for the label (\lstick) or empty: a gate there has no incoming wire.
  • End a row with & after the last gate, or no wire leaves it.
  • Put the gate command first in its cell; commands such as \setwiretype, \qwbundle, \slice or \gategroup come after it.

The single-qubit commands, as listed in §III of the tutorial:

CommandDraws
\gate{H}A box; the label is in math mode, so \gate{R_z(\theta)} works as written
\phase{\alpha}A filled dot on the wire with a label
\push{T}Text on the wire without a box
\meter{}, \meter{\ket{\pm}}The measurement symbol, optionally with the basis written above; \metercw is a variant
\measuretab{x}, \measure{x}, \meterD{x}, \inputD{0}Measurement and input shapes that end or start a wire
\trash{}, \ground{}Terminations for a traced-out wire

Commands that look argument-free still need their braces: \meter{}, \targ{}, \control{}. A wire after a measurement stays quantum unless you change it (see classical wires below).

Top wire with boxes H, X, R_z(θ), U†, a filled dot labelled α and plain text T on the wire. Below: a meter followed by a double classical wire, a meter labelled |±⟩ in the middle of a quantum wire, and a pointed tab labelled x ending a wire.
Single-qubit gates, a phase dot, text on a wire, and measurement variants
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}[row sep=0.6cm]
  % single-qubit gates: the label is already in math mode
  & \gate{H} & \gate{X} & \gate{R_z(\theta)} & \gate{U^\dagger} & \phase{\alpha} & \push{T} & \\
  % measurements
  & \meter{} & \setwiretype{c} &    &                   &                &          & \\
  &          &                 &    & \meter{\ket{\pm}} &                &          & \\
  &          &                 &    &                   &                & \measuretab{x}
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}[row sep=0.6cm]
  % single-qubit gates: the label is already in math mode
  & \gate{H} & \gate{X} & \gate{R_z(\theta)} & \gate{U^\dagger} & \phase{\alpha} & \push{T} & \\
  % measurements
  & \meter{} & \setwiretype{c} &    &                   &                &          & \\
  &          &                 &    & \meter{\ket{\pm}} &                &          & \\
  &          &                 &    &                   &                & \measuretab{x}
\end{quantikz}
\end{document}

Controlled gates: CNOT, CZ, Toffoli and SWAP

\ctrl{n} draws a control dot and a vertical line n rows down (negative n goes up). What sits at the other end decides the gate:

  • CNOT: \ctrl{1} on the control wire, \targ{} (⊕) in the same column one row down.
  • CZ: \ctrl{1} and \control{}, a dot with no line of its own.
  • Open (negated) control: \octrl{1}, or \ctrl[open]{1}; \ocontrol{} is the open target dot.
  • Controlled-U: \ctrl{1} above any \gate{U}; the target may also be above, \ctrl{-1}.
  • Toffoli: chain the lines, \ctrl{1} on the first wire, \ctrl{1} on the second, \targ{} on the third.
  • SWAP: \swap{1} and \targX{} (two crosses). \swap[partial swap={\pi/4}]{1} puts a label in a circle on the line.

For one control with several targets, run the line to the farthest target and put \targ{} on each wire it crosses; \wire[u]{q} adds an extra vertical quantum wire from a cell when you need one.

Three wires with six gates labelled underneath: CNOT (dot and ⊕), CZ (two dots), open control (hollow dot and ⊕), C-U (dot above a U box), Toffoli (two dots and ⊕ on the third wire) and SWAP (two crosses joined by a line).
CNOT, CZ, open control, controlled-U, Toffoli and SWAP
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}[wire types={q,q,q,n}, column sep=0.45cm]
  & \ctrl{1}     & & \ctrl{1}   & & \octrl{1}       & & \ctrl{1}  & & \ctrl{1}            & & \swap{1}  & \\
  & \targ{}      & & \control{} & & \targ{}         & & \gate{U}  & & \ctrl{1}            & & \targX{}  & \\
  &              & &            & &                 & &           & & \targ{}             & &           & \\
  & \push{\text{CNOT}} & & \push{\text{CZ}} & & \push{\text{open}} & & \push{\text{C-}U} & & \push{\text{Toffoli}} & & \push{\text{SWAP}} &
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}[wire types={q,q,q,n}, column sep=0.45cm]
  & \ctrl{1}     & & \ctrl{1}   & & \octrl{1}       & & \ctrl{1}  & & \ctrl{1}            & & \swap{1}  & \\
  & \targ{}      & & \control{} & & \targ{}         & & \gate{U}  & & \ctrl{1}            & & \targX{}  & \\
  &              & &            & &                 & &           & & \targ{}             & &           & \\
  & \push{\text{CNOT}} & & \push{\text{CZ}} & & \push{\text{open}} & & \push{\text{C-}U} & & \push{\text{Toffoli}} & & \push{\text{SWAP}} &
\end{quantikz}
\end{document}

Multi-qubit gates

The first optional argument of \gate is the number of wires: \gate[2]{U} in the top row of the gate covers that wire and the one below. Leave the cells it covers empty. \meter spans wires the same way.

  • \gate[2,swap]{} draws a two-qubit box with crossing wires inside.
  • \gateinput{$x$} and \gateoutput{$x$} write labels inside the left and right edge of a gate, on the current row; \gateinput[2]{…} groups two wires with a brace. These labels are in text mode, and the gate does not widen for them: give a minimum width as the second optional argument, \gate[3][1.8cm]{U_f}.
  • \gate[3, disable auto height]{\verticaltext{MAJ}} stops a tall label from stretching every wire.
Three wires: a U box over the top two, a tall QFT box over all three, a box over the top two with crossing wires inside, and a U_f box over all three with x and a brace on the inputs and outputs of the top two wires and y, y ⊕ f(x) on the third.
Gates spanning wires: \gate[2], \gate[3], a swap box and an oracle with input and output labels
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}
  & \gate[2]{U} & \gate[3]{\mathrm{QFT}} & \gate[2,swap]{} & \gate[3][1.8cm]{U_f}\gateinput[2]{$x$}\gateoutput[2]{$x$} & \\
  &             &                        &                 &                                                          & \\
  &             &                        &                 & \gateinput{$y$}\gateoutput{$y\oplus f(x)$}               &
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}
  & \gate[2]{U} & \gate[3]{\mathrm{QFT}} & \gate[2,swap]{} & \gate[3][1.8cm]{U_f}\gateinput[2]{$x$}\gateoutput[2]{$x$} & \\
  &             &                        &                 &                                                          & \\
  &             &                        &                 & \gateinput{$y$}\gateoutput{$y\oplus f(x)$}               &
\end{quantikz}
\end{document}

Labelling wires: \lstick, \rstick and \midstick

\lstick{…} labels the start of a wire and goes in the first cell; \rstick{…} labels the end and goes in the last cell; \midstick{…} interrupts the wires in the middle (useful for “⋯” or for an equals sign between two circuits). Their text is in text mode, so maths needs dollars: \lstick{$q_0$}. With a number they span several wires and draw a brace: \lstick[2]{ancillas}; brackets=none removes it and braces={…} styles it.

Quantikz defines \ket, \bra, \proj and \braket, which work without math mode. If another package (physics, braket) already defines them, quantikz keeps the other definition; load quantikz after it and put \forceredefine at the end of the preamble if you want quantikz’s versions.

Four wires. The top two are labelled q0: |0⟩ and q1: |0⟩, have H and a CNOT, a gap with ⋯, a U box over both and a brace labelled output. The bottom two are grouped by a brace labelled ancillas; one has an X gate and ends in |1⟩, the other ends in |0⟩.
\lstick, \rstick and \midstick, alone and spanning wires with braces
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}
  \lstick{$q_0$: \ket{0}} & \gate{H} & \ctrl{1} & \midstick[2,brackets=none]{$\cdots$} & \gate[2]{U} & \rstick[2]{output} \\
  \lstick{$q_1$: \ket{0}} &          & \targ{}  &                                      &             & \\
  \lstick[2]{ancillas}    &          &          &                                      & \gate{X}    & \rstick{\ket{1}} \\
                          &          &          &                                      &             & \rstick{\ket{0}}
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}
  \lstick{$q_0$: \ket{0}} & \gate{H} & \ctrl{1} & \midstick[2,brackets=none]{$\cdots$} & \gate[2]{U} & \rstick[2]{output} \\
  \lstick{$q_1$: \ket{0}} &          & \targ{}  &                                      &             & \\
  \lstick[2]{ancillas}    &          &          &                                      & \gate{X}    & \rstick{\ket{1}} \\
                          &          &          &                                      &             & \rstick{\ket{0}}
\end{quantikz}
\end{document}

Classical wires, bundles and wire types

Wire types are q (quantum, the default), c (classical, a double line), b (bundle, three lines) and n (none). Set them for the whole circuit with \begin{quantikz}[wire types={q,q,c,b,q}], one letter per row, or change one wire from a cell onwards with \setwiretype{c}. The change applies to the wire that ends in that cell, the segment running back to the previous cell, which is why the \setwiretype{c} goes in the cell after the \meter{}.

  • A classically controlled gate: \ctrl[vertical wire=c]{1} on the classical wire above \gate{X}.
  • \setwiretype{n} ends a wire; \wireoverride{n} blanks a single segment without changing the rest of the row.
  • \qwbundle{n} puts a slash labelled n through a quantum wire, the usual notation for a register of n qubits.
  • classical gap=0.05cm changes the distance between the two lines of classical wires (default 0.03 cm).
Five wires: q (measured) with H and a meter, then a double line with a control dot whose double vertical line goes to an X gate on the second wire; a classical double wire labelled c; a triple-line bundle labelled b; and a quantum wire labelled q with a slash marked n.
Wire types: quantum, classical after a measurement, a classically controlled X, a bundle and \qwbundle
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}[wire types={q,q,c,b,q}]
  \lstick{q (measured)} & \gate{H} & \meter{} & \setwiretype{c} & \ctrl[vertical wire=c]{1} & \\
  \lstick{q}            &          &          &                 & \gate{X}                  & \\
  \lstick{c}            &          &          &                 &                           & \\
  \lstick{b}            &          &          &                 &                           & \\
  \lstick{q}            & \qwbundle{n} &      &                 &                           &
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}[wire types={q,q,c,b,q}]
  \lstick{q (measured)} & \gate{H} & \meter{} & \setwiretype{c} & \ctrl[vertical wire=c]{1} & \\
  \lstick{q}            &          &          &                 & \gate{X}                  & \\
  \lstick{c}            &          &          &                 &                           & \\
  \lstick{b}            &          &          &                 &                           & \\
  \lstick{q}            & \qwbundle{n} &      &                 &                           &
\end{quantikz}
\end{document}

Slices (barriers) and gate groups

\slice{label} draws a dashed vertical line after the column it is written in, labelled at the top: the usual way to mark the steps of a circuit, and the quantikz equivalent of a Qiskit-style barrier (\slice{} for no label). \begin{quantikz}[slice all] slices every column and numbers the slices; remove end slices=1, slice titles=step \col, slice style=blue and slice label style={…} adjust them. Slicing every column needs every row to have the same number of cells.

\gategroup[2,steps=3]{label} draws a box whose top-left corner is the cell it is written in, covering 2 wires and 3 columns, with the label above. style={dashed, rounded corners, fill=blue!15} styles the box, background puts it behind the gates (needed with a fill), and label style={label position=below, anchor=north, yshift=-0.2cm} moves the label under it.

Two wires: an H gate, a red dashed line labelled 1, a shaded dashed rounded box containing three alternating CNOTs with the caption SWAP from three CNOTs below it, a red dashed line labelled 2, and meters on both wires.
Numbered slices (dashed red lines) and a shaded \gategroup with its label below
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}
  & \gate{H}\slice{1} & \ctrl{1}\gategroup[2,steps=3,style={dashed,rounded corners,fill=blue!15,inner xsep=2pt},background,label style={label position=below,anchor=north,yshift=-0.2cm}]{SWAP from three CNOTs} & \targ{} & \ctrl{1}\slice{2} & \meter{} \\
  &                   & \targ{}  & \ctrl{-1} & \targ{} & \meter{}
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}
  & \gate{H}\slice{1} & \ctrl{1}\gategroup[2,steps=3,style={dashed,rounded corners,fill=blue!15,inner xsep=2pt},background,label style={label position=below,anchor=north,yshift=-0.2cm}]{SWAP from three CNOTs} & \targ{} & \ctrl{1}\slice{2} & \meter{} \\
  &                   & \targ{}  & \ctrl{-1} & \targ{} & \meter{}
\end{quantikz}
\end{document}

Example: quantum teleportation

The Bell state circuit at the top of this page (H, then CNOT) is the first half of teleportation. Here it prepares the shared pair on the lower two wires; the sender then entangles |ψ⟩ with her half (CNOT, H), measures both qubits, and the two classical results control X and Z corrections on the receiver’s qubit, which ends in |ψ⟩. The classical wires start with \setwiretype{c} after each \meter{}, the corrections use \ctrl[vertical wire=c], and \setwiretype{n} stops each classical wire after its control.

Three-wire circuit: |ψ⟩ on top and two |0⟩ wires. A dashed box labelled Bell pair holds H and a CNOT on the lower two wires; then a CNOT from the top wire to the middle, H on the top, meters on the top two wires, double classical wires from the meters controlling X and Z gates on the bottom wire, which ends in |ψ⟩.
Quantum teleportation with classical wires controlling the X and Z corrections
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}
  \lstick{\ket{\psi}} &          &          & \ctrl{1} & \gate{H} & \meter{} & \setwiretype{c} & \ctrl[vertical wire=c]{2} & \setwiretype{n} \\
  \lstick{\ket{0}}    & \gate{H}\gategroup[2,steps=2,style={dashed,rounded corners,inner xsep=2pt},label style={label position=below,anchor=north,yshift=-0.2cm}]{Bell pair} & \ctrl{1} & \targ{} & & \meter{} & \ctrl[vertical wire=c]{1}\setwiretype{c} & \setwiretype{n} & \\
  \lstick{\ket{0}}    &          & \targ{}  &          &          &          & \gate{X}        & \gate{Z}                  & \rstick{\ket{\psi}}
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}
  \lstick{\ket{\psi}} &          &          & \ctrl{1} & \gate{H} & \meter{} & \setwiretype{c} & \ctrl[vertical wire=c]{2} & \setwiretype{n} \\
  \lstick{\ket{0}}    & \gate{H}\gategroup[2,steps=2,style={dashed,rounded corners,inner xsep=2pt},label style={label position=below,anchor=north,yshift=-0.2cm}]{Bell pair} & \ctrl{1} & \targ{} & & \meter{} & \ctrl[vertical wire=c]{1}\setwiretype{c} & \setwiretype{n} & \\
  \lstick{\ket{0}}    &          & \targ{}  &          &          &          & \gate{X}        & \gate{Z}                  & \rstick{\ket{\psi}}
\end{quantikz}
\end{document}

Example: three-qubit quantum Fourier transform

The QFT is a staircase of Hadamards and controlled phase rotations Rk, followed by a SWAP that reverses the qubit order. Each rotation is a \gate{R_k} on the target with \ctrl{-1} or \ctrl{-2} pointing up from the control.

Three wires x1, x2, x3: H, R2 controlled from x2 and R3 controlled from x3 on x1; H and R2 controlled from x3 on x2; H on x3; then a SWAP between x1 and x3.
Three-qubit quantum Fourier transform with controlled R2 and R3 rotations and the final SWAP
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}
  \lstick{$x_1$} & \gate{H} & \gate{R_2} & \gate{R_3} &          &            &          & \swap{2} & \\
  \lstick{$x_2$} &          & \ctrl{-1}  &            & \gate{H} & \gate{R_2} &          &          & \\
  \lstick{$x_3$} &          &            & \ctrl{-2}  &          & \ctrl{-1}  & \gate{H} & \targX{} &
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}
  \lstick{$x_1$} & \gate{H} & \gate{R_2} & \gate{R_3} &          &            &          & \swap{2} & \\
  \lstick{$x_2$} &          & \ctrl{-1}  &            & \gate{H} & \gate{R_2} &          &          & \\
  \lstick{$x_3$} &          &            & \ctrl{-2}  &          & \ctrl{-1}  & \gate{H} & \targX{} &
\end{quantikz}
\end{document}

Styling, spacing and scaling

  • Whole circuit: thin lines (closer to the old QCircuit look), transparent (no white fill in gates), color=blue and background color=….
  • One gate: \gate[style={fill=teal!20}, label style=cyan]{H}, \meter[style={draw=blue}]{}, \phase[style=green]{\beta}.
  • Every gate of a kind: \tikzset{operator/.append style={fill=red!20}}; the style names are operator (\gate), meter, slice, phase, circlewc (\targ), crossx2 (\swap) and others (§V.A).
  • Spacing: column sep=0.4cm and row sep=0.6cm set the gaps between cells; row sep={0.9cm,between origins} spaces the wires evenly whatever the gate heights. &[2cm] widens one column gap and \\[1cm] one row gap.
  • Scaling: put the circuit in a node, \begin{tikzpicture}\node[scale=0.8]{\begin{quantikz} … \end{quantikz}};\end{tikzpicture}, or use \begin{adjustbox}{width=\linewidth} from the adjustbox package to fit a width.

For a document with many circuits, TikZ externalization (\usetikzlibrary{external}, \tikzexternalize) works with quantikz and turns each circuit into a separate PDF; that is also the tutorial’s advice for publishers that do not accept TikZ code.

Three-wire circuit in thin lines: teal-filled H boxes on the top two wires, two CNOTs onto the bottom wire, orange rounded T and T† boxes, and blue-outlined meters at the end of every wire.
thin lines, even wire spacing (between origins) and per-gate fill and draw styles
Preamble: \usepackage{tikz} \usetikzlibrary{quantikz2}
\begin{quantikz}[thin lines, row sep={0.9cm,between origins}, column sep=0.4cm]
  & \gate[style={fill=teal!20}]{H} & \ctrl{2} & & \gate[style={fill=orange!25,rounded corners}]{T} & \meter[style={draw=blue}]{} \\
  & \gate[style={fill=teal!20}]{H} &          & \ctrl{1} & & \meter[style={draw=blue}]{} \\
  &                                & \targ{}  & \targ{}  & \gate[style={fill=orange!25,rounded corners}]{T^\dagger} & \meter[style={draw=blue}]{}
\end{quantikz}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{quantikz2}
\begin{document}
\begin{quantikz}[thin lines, row sep={0.9cm,between origins}, column sep=0.4cm]
  & \gate[style={fill=teal!20}]{H} & \ctrl{2} & & \gate[style={fill=orange!25,rounded corners}]{T} & \meter[style={draw=blue}]{} \\
  & \gate[style={fill=teal!20}]{H} &          & \ctrl{1} & & \meter[style={draw=blue}]{} \\
  &                                & \targ{}  & \targ{}  & \gate[style={fill=orange!25,rounded corners}]{T^\dagger} & \meter[style={draw=blue}]{}
\end{quantikz}
\end{document}

Alternatives: qcircuit and yquant

  • Qcircuit (2.6.0) is the older Xy-pic package that quantikz was modelled on, and the format Qiskit’s LaTeX circuit drawer writes. The notation converts almost one to one: \multigate{n} becomes \gate[n+1], \targ becomes \targ{}, \meter becomes \meter{} (tutorial §VII).
  • yquant (0.9.1, 2026-07-19) describes a circuit as a list of operations on named registers instead of a grid and places the gates itself, which is convenient for long algorithms.

To edit quantikz code and see the result as you type, paste it into the LabCanvas TikZ editor, which compiles with TeX Live 2026, the same renderer as the pictures on this page.

Common errors and how to fix them

LaTeX says (or you see)Cause and fix
LaTeX Error: Environment quantikz undefined.Add \usetikzlibrary{quantikz2} after \usepackage{tikz}, or \usepackage{quantikz}.
Missing $ inserted. (reported at \end{quantikz}, from \lstick{q_0})\lstick, \rstick, \midstick, \gateinput and \gateoutput are in text mode: write \lstick{$q_0$}. (\ket{0} works without dollars.)
Missing } inserted. (reported at \end{quantikz}, from \meter without braces)Commands that take an optional argument and look argument-free still need an empty pair of braces: \meter{}, \targ{}, \control{}, \targX{}.
Undefined control sequence. \errmessage #1->\tikzcd@errmessage … I think the culprit is a tikzcd arrow in cell …With \slice or \gategroup, the last row ended with \\ or is shorter than the others. Delete the final \\ and give the last row as many cells (&) as the longest row (tutorial §VIII).
\meter{$x$} or \gate{$H$} gives an upright x or HGate and meter labels are already in math mode, so the dollars switch back to text. Write \meter{x}, \gate{H}.
No wire after the last gate of a rowEnd the row with & after the gate, so there is a cell for the outgoing wire.
Wire segments missing, or a gate drawn as plain text in its cellWith the legacy \usetikzlibrary{quantikz}, empty cells need \qw; switch to quantikz2. With quantikz2, make the gate command the first thing in its cell and put \setwiretype, \qwbundle or \slice after it.
The classical (double) wire starts one cell late\setwiretype{c} changes the segment that ends in its cell, the one coming from the left. Put it in the cell right after the \meter{}, or set wire types={…} for the whole row.

Frequently asked questions

How do I draw a quantum circuit in LaTeX?

Load \usepackage{tikz} and \usetikzlibrary{quantikz2}, then write \begin{quantikz} \lstick{\ket{0}} & \gate{H} & \ctrl{1} & \meter{} \\ \lstick{\ket{0}} & & \targ{} & \meter{} \end{quantikz}. Rows are wires, & separates time steps, and wires are drawn automatically.

Should I use quantikz or quantikz2?

quantikz2. It is the current release (1.0.1); \usetikzlibrary{quantikz} loads the legacy 0.9.8 code, which needs \qw for every empty cell. \usepackage{quantikz} loads quantikz2 in version 1.0.1, so on a current TeX distribution it gives you the new version too.

How do I draw a CNOT gate in quantikz?

Put \ctrl{1} on the control wire and \targ{} in the same column of the wire below. For a target above the control use \ctrl{-1}; for CZ replace \targ{} by \control{}.

How do I draw a barrier or a dashed line between steps?

Use \slice{} (or \slice{label}) in a cell: it draws a dashed vertical line after that column across all wires. [slice all] on the environment slices every column; slice style={black, dotted} changes the look.

How do I draw a measurement followed by a classical wire?

Write \meter{} and put \setwiretype{c} in the next cell; the wire from the meter on becomes a double line. A gate controlled by the result is \ctrl[vertical wire=c]{n} above (or below) a \gate{…}.

How do I make a quantikz circuit fit the page?

Reduce column sep and row sep, or scale it: wrap it in \begin{adjustbox}{width=\linewidth} … \end{adjustbox} (adjustbox package) or in a scaled node, \node[scale=0.8]{\begin{quantikz}…\end{quantikz}}; inside a tikzpicture.

References

  1. quantikz package on CTAN (v1.0.1, 2023-05-24)ctan.org
  2. Tutorial on the Quantikz Package (PDF on CTAN)mirrors.ctan.org
  3. Quantikz tutorial, §II Basic usage: gates and measurementmirrors.ctan.org
  4. Quantikz tutorial, §II.B Controlled gates and §II.C Labelling circuitsmirrors.ctan.org
  5. Quantikz tutorial, §II.D Boxing/highlighting and §II.E Slicingmirrors.ctan.org
  6. Quantikz tutorial, §III Commands & optionsmirrors.ctan.org
  7. Quantikz tutorial, §IV Spacingmirrors.ctan.org
  8. Quantikz tutorial, §V Styling & customisingmirrors.ctan.org
  9. Quantikz tutorial, §VII.A Converting from Quantikz to Quantikz2 and §VIII Troubleshootingmirrors.ctan.org
  10. A. Kay, Tutorial on the Quantikz Package, arXiv:1809.03842arxiv.org
  11. tikz-cd package on CTAN (v1.0), which quantikz builds onctan.org
  12. PGF/TikZ manual, §20 Matrices and Alignmenttikz.dev
  13. qcircuit package on CTAN (v2.6.0)ctan.org
  14. yquant package on CTAN (v0.9.1, 2026-07-19)ctan.org

The facts on this page were checked against these sources, and every picture is the code with it typeset with TeX Live 2026. Code on this page: CC0 1.0, copy it freely.

More TikZ guides

Tools

Try the code in your browserEvery snippet on this page opens in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.

Open the TikZ editor