LabCanvas

Bloch sphere in TikZ, explained line by line

Quick answer

This figure is a Bloch sphere drawn in plain TikZ: a circle for the sphere, a dashed ellipse for the equator, three axes x1, x2, x3, a vector a with its dashed projection onto the equatorial plane, and the angles θ and φ. It is a flat sketch with hand-picked 2D coordinates rather than a true 3D projection, which keeps it to about twenty lines. The angles are angle pics from the angles library, labelled with the quotes syntax; the variations show how to place the vector from θ and φ with a real 3D view.

Contents

The figure and its code

Bloch sphere: a large circle with a dashed horizontal ellipse for the equator, axes x3 up, x2 right and x1 down to the left from the centre, a vector a from the centre to a dot labelled a, its dashed projection onto the equator, a grey arc marked theta between x3 and a, and a grey arc marked phi between x1 and the projection.
Bloch sphere, typeset with TeX Live 2026 (pdfLaTeX) from the code below, exactly as it is in janosh/diagrams.
Copyright (c) 2021 Janosh Riebesell · MIT licence (full text). The licence asks that this notice stay with the code: keep the first lines when you reuse it.
% Bloch sphere
% Source: https://github.com/janosh/diagrams/blob/7a79b3114e201fad9b6785e936eab859d1bc8c12/assets/bloch-sphere/bloch-sphere.tex
% Copyright (c) 2021 Janosh Riebesell. MIT licence: https://github.com/janosh/diagrams/blob/main/license
% The licence’s copyright and permission notice: https://www.labcanvas.com/tikz-library/credits.html
\documentclass[tikz]{standalone}

\usetikzlibrary{angles,quotes}

\begin{document}
\begin{tikzpicture}

  % Define radius
  \def\r{3}

  % Bloch vector
  \draw (0, 0) node[circle, fill, inner sep=1] (orig) {} -- (\r/3, \r/2) node[circle, fill, inner sep=0.7, label=above:$\vec{a}$] (a) {};
  \draw[dashed] (orig) -- (\r/3, -\r/5) node (phi) {} -- (a);

  % Sphere
  \draw (orig) circle (\r);
  \draw[dashed] (orig) ellipse (\r{} and \r/3);

  % Axes
  \draw[->] (orig) -- ++(-\r/5, -\r/3) node[below] (x1) {$x_1$};
  \draw[->] (orig) -- ++(\r, 0) node[right] (x2) {$x_2$};
  \draw[->] (orig) -- ++(0, \r) node[above] (x3) {$x_3$};

  % Angles
  \pic [draw=gray, text=gray, ->, "$\phi$"] {angle = x1--orig--phi};
  \pic [draw=gray, text=gray, <-, "$\theta$", angle eccentricity=1.4] {angle = a--orig--x3};

\end{tikzpicture}
\end{document}

The document and the radius

Lines 5–13 of the code

\documentclass[tikz]{standalone} loads TikZ and crops the page to the picture. Two libraries are needed: angles for the angle pics and quotes for writing their labels as "$\phi$". \def\r{3} stores the sphere’s radius in a macro so that every coordinate can be written as a fraction of it. Beware that \r is also LaTeX’s ring accent (as in å); redefining it inside the picture is harmless here, but a name such as \R or \radius avoids the clash.

The vector and its projection

Lines 15–17 of the code

Line 16 draws the vector a and places two nodes on the same path: node[circle, fill, inner sep=1] (orig) {} at the start, a small filled dot named orig, and another at the end, at (\r/3, \r/2), = (1, 1.5), with label=above:$\vec{a}$. A node with empty text and inner sep=1 (1pt, since a length without unit is read in points) is simply a dot of that size.

Line 17 is the dashed projection: from the origin to (\r/3, -\r/5), the foot of the vector on the equatorial plane, then up to a. The empty node named phi marks the foot so that the φ angle can refer to it. The foot lies directly below a (the same x, 1), which is what makes the dashed line vertical.

The sphere and the equator

Lines 19–21 of the code

\draw (orig) circle (\r); is the outline, and \draw[dashed] (orig) ellipse (\r{} and \r/3); the equator, a flattened circle as wide as the sphere and a third as tall. Both use the older syntax with the radius in parentheses (the newer one is circle[radius=\r] and ellipse[x radius=\r, y radius=\r/3]). The empty group in \r{} and matters: TeX swallows the space after a command name, so without {} TikZ would see “3and” and miss the keyword and. The whole ellipse is dashed, front and back; a 3D drawing would show the front half solid, as in the first variation.

Three axes in an oblique view

Lines 23–26 of the code

Each axis is a relative line from the origin, -- ++(\r, 0) for x2 and -- ++(0, \r) for x3, ending in a label node named after the axis. x1, the axis towards the viewer, is faked with a short line down and to the left, ++(-\r/5, -\r/3): the oblique direction suggests depth without any 3D maths. That is also why the picture is a sketch: the vector a and the ellipse are not computed from a common viewpoint, they are just drawn to look right.

The angles as pics

Lines 28–30 of the code

\pic [draw=gray, text=gray, ->, "$\phi$"] {angle = x1--orig--phi}; draws the angle at orig between the lines towards x1 and towards phi: an arc of the default angle radius (5 mm) with an arrow tip at its end, on the side of the projection. The quotes syntax sets the label, which sits at angle eccentricity times the radius from the vertex (0.6 by default, inside the arc).

The θ angle, angle = a--orig--x3, uses <-, so the tip is at the start of the arc, on the side of a: the arrow runs from the x3 axis to the vector, the direction in which θ is measured. angle eccentricity=1.4 moves its label outside the narrow arc. One subtlety: x1 is the label node below the tip of the axis, not the tip itself, so the φ arc starts a few degrees off the axis line; at 5 mm that is invisible, but a \coordinate at the tip would be exact.

Techniques used

Variations

Our own code, written for this page and typeset with TeX Live 2026 like the figure above. Each one is a complete example: copy it, or open it in the TikZ editor and change it. This code is CC0 1.0, free to use without conditions.

A qubit state placed from θ and φ in a true 3D view

TikZ can project 3D coordinates if you give it the screen directions of the three unit vectors. With x={(-0.5cm,-0.296cm)}, y={(0.866cm,-0.171cm)}, z={(0cm,0.94cm)} (an orthographic view from 30° azimuth and 20° elevation), a point is written as (x,y,z) and the state goes exactly where its angles say: ({\R*sin(\th)*cos(\ph)}, {\R*sin(\th)*sin(\ph)}, {\R*cos(\th)}). The equator is a plot of a circle in the xy-plane, solid in front and dashed behind, and the θ and φ arcs are short plots in their own planes. Change \th and \ph and everything follows.

A light blue Bloch sphere in perspective with axes x, y and z, the poles marked |0⟩ at the top and |1⟩ at the bottom, the equator solid in front and dashed behind, a red vector |ψ⟩ with its dashed projection onto the equator, blue arcs marking theta from the z axis and phi from the x axis, and the formula |ψ⟩ = cos(θ/2)|0⟩ + e^{iφ} sin(θ/2)|1⟩.
a qubit state |ψ⟩ at θ = 50°, φ = 60° on a Bloch sphere drawn with 3D coordinates
Preamble: \usepackage{tikz}
\begin{tikzpicture}[
    % an orthographic view: azimuth 30 degrees, elevation 20 degrees
    x={(-0.5cm,-0.296cm)}, y={(0.866cm,-0.171cm)}, z={(0cm,0.94cm)},
    >=stealth, line cap=round,
  ]
  \def\R{2.6}   % radius
  \def\th{50}   % polar angle theta of the state
  \def\ph{60}   % azimuth phi of the state
  % the sphere's outline, and the equator: front half solid, back half dashed
  \fill[blue!6] (0,0,0) circle[radius=\R cm];
  \draw (0,0,0) circle[radius=\R cm];
  \draw[gray, dashed] plot[domain=120:300, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  \draw[gray] plot[domain=-60:120, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  % axes and poles
  \draw[->] (0,0,0) -- (1.35*\R,0,0) node[below left] {$x$};
  \draw[->] (0,0,0) -- (0,1.3*\R,0) node[right] {$y$};
  \draw[->] (0,0,0) -- (0,0,1.25*\R) node[above] {$z$};
  \fill (0,0,\R) circle[radius=1.3pt] node[above right] {$|0\rangle$};
  \fill (0,0,-\R) circle[radius=1.3pt] node[below right] {$|1\rangle$};
  % the state from its two angles, and its projection on the equator
  \coordinate (psi) at ({\R*sin(\th)*cos(\ph)}, {\R*sin(\th)*sin(\ph)}, {\R*cos(\th)});
  \coordinate (proj) at ({\R*sin(\th)*cos(\ph)}, {\R*sin(\th)*sin(\ph)}, 0);
  \draw[dashed] (0,0,0) -- (proj) -- (psi);
  \draw[->, very thick, red!70!black] (0,0,0) -- (psi) node[above right] {$|\psi\rangle$};
  % the angles as short arcs in 3D
  \draw[->, blue!70!black] plot[domain=0:\ph, samples=20, smooth, variable=\t] ({0.7*cos(\t)}, {0.7*sin(\t)}, 0);
  \node[blue!70!black] at ({1.05*cos(\ph/2)}, {1.05*sin(\ph/2)}, 0) {$\varphi$};
  \draw[->, blue!70!black] plot[domain=0:\th, samples=20, smooth, variable=\t]
    ({0.9*sin(\t)*cos(\ph)}, {0.9*sin(\t)*sin(\ph)}, {0.9*cos(\t)});
  \node[blue!70!black] at ({1.25*sin(\th/2)*cos(\ph)}, {1.25*sin(\th/2)*sin(\ph)}, {1.25*cos(\th/2)}) {$\theta$};
  \node[anchor=west] at (0, 1.05*\R, -0.9*\R)
    {$|\psi\rangle = \cos\frac{\theta}{2}|0\rangle + e^{i\varphi}\sin\frac{\theta}{2}|1\rangle$};
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    % an orthographic view: azimuth 30 degrees, elevation 20 degrees
    x={(-0.5cm,-0.296cm)}, y={(0.866cm,-0.171cm)}, z={(0cm,0.94cm)},
    >=stealth, line cap=round,
  ]
  \def\R{2.6}   % radius
  \def\th{50}   % polar angle theta of the state
  \def\ph{60}   % azimuth phi of the state
  % the sphere's outline, and the equator: front half solid, back half dashed
  \fill[blue!6] (0,0,0) circle[radius=\R cm];
  \draw (0,0,0) circle[radius=\R cm];
  \draw[gray, dashed] plot[domain=120:300, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  \draw[gray] plot[domain=-60:120, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  % axes and poles
  \draw[->] (0,0,0) -- (1.35*\R,0,0) node[below left] {$x$};
  \draw[->] (0,0,0) -- (0,1.3*\R,0) node[right] {$y$};
  \draw[->] (0,0,0) -- (0,0,1.25*\R) node[above] {$z$};
  \fill (0,0,\R) circle[radius=1.3pt] node[above right] {$|0\rangle$};
  \fill (0,0,-\R) circle[radius=1.3pt] node[below right] {$|1\rangle$};
  % the state from its two angles, and its projection on the equator
  \coordinate (psi) at ({\R*sin(\th)*cos(\ph)}, {\R*sin(\th)*sin(\ph)}, {\R*cos(\th)});
  \coordinate (proj) at ({\R*sin(\th)*cos(\ph)}, {\R*sin(\th)*sin(\ph)}, 0);
  \draw[dashed] (0,0,0) -- (proj) -- (psi);
  \draw[->, very thick, red!70!black] (0,0,0) -- (psi) node[above right] {$|\psi\rangle$};
  % the angles as short arcs in 3D
  \draw[->, blue!70!black] plot[domain=0:\ph, samples=20, smooth, variable=\t] ({0.7*cos(\t)}, {0.7*sin(\t)}, 0);
  \node[blue!70!black] at ({1.05*cos(\ph/2)}, {1.05*sin(\ph/2)}, 0) {$\varphi$};
  \draw[->, blue!70!black] plot[domain=0:\th, samples=20, smooth, variable=\t]
    ({0.9*sin(\t)*cos(\ph)}, {0.9*sin(\t)*sin(\ph)}, {0.9*cos(\t)});
  \node[blue!70!black] at ({1.25*sin(\th/2)*cos(\ph)}, {1.25*sin(\th/2)*sin(\ph)}, {1.25*cos(\th/2)}) {$\theta$};
  \node[anchor=west] at (0, 1.05*\R, -0.9*\R)
    {$|\psi\rangle = \cos\frac{\theta}{2}|0\rangle + e^{i\varphi}\sin\frac{\theta}{2}|1\rangle$};
\end{tikzpicture}
\end{document}

The six cardinal states from one \foreach

The same view with the six states on the axes, |0⟩ and |1⟩ at the poles and |±⟩ and |±i⟩ on the equator. Each state is one entry of a \foreach list with five fields, θ, φ, label, label position and line style, so the vectors on the far side of the sphere can be dashed. The sphere is ball color shading made lighter with opacity=0.4.

A softly shaded sphere with six red arrows from the centre: |0⟩ up, |1⟩ down (dashed), |+⟩ towards the viewer at lower left, |−⟩ away from the viewer (dashed), |+i⟩ to the right and |−i⟩ to the left (dashed), over a solid front and dashed back equator.
the six cardinal qubit states drawn from a list of angles; states behind the sphere are dashed
Preamble: \usepackage{tikz}
\begin{tikzpicture}[
    x={(-0.5cm,-0.296cm)}, y={(0.866cm,-0.171cm)}, z={(0cm,0.94cm)},
    >=stealth, font=\small,
  ]
  \def\R{2.2}
  \shade[ball color=blue!25, opacity=0.4] (0,0,0) circle[radius=\R cm];
  \draw (0,0,0) circle[radius=\R cm];
  \draw[gray, dashed] plot[domain=120:300, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  \draw[gray] plot[domain=-60:120, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  % the six states on the axes: theta, phi, label, label position, line style
  % (states on the far side of the sphere are dashed)
  \foreach \th/\ph/\lab/\pos/\sty in {
      0/0/{|0\rangle}/above/solid, 180/0/{|1\rangle}/below/dashed,
      90/0/{|{+}\rangle}/below left/solid, 90/180/{|{-}\rangle}/above right/dashed,
      90/90/{|{+i}\rangle}/right/solid, 90/270/{|{-i}\rangle}/left/dashed} {
    \draw[->, thick, red!70!black, \sty]
      (0,0,0) -- ({\R*sin(\th)*cos(\ph)}, {\R*sin(\th)*sin(\ph)}, {\R*cos(\th)}) node[\pos] {$\lab$};
  }
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    x={(-0.5cm,-0.296cm)}, y={(0.866cm,-0.171cm)}, z={(0cm,0.94cm)},
    >=stealth, font=\small,
  ]
  \def\R{2.2}
  \shade[ball color=blue!25, opacity=0.4] (0,0,0) circle[radius=\R cm];
  \draw (0,0,0) circle[radius=\R cm];
  \draw[gray, dashed] plot[domain=120:300, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  \draw[gray] plot[domain=-60:120, samples=60, smooth, variable=\t] ({\R*cos(\t)}, {\R*sin(\t)}, 0);
  % the six states on the axes: theta, phi, label, label position, line style
  % (states on the far side of the sphere are dashed)
  \foreach \th/\ph/\lab/\pos/\sty in {
      0/0/{|0\rangle}/above/solid, 180/0/{|1\rangle}/below/dashed,
      90/0/{|{+}\rangle}/below left/solid, 90/180/{|{-}\rangle}/above right/dashed,
      90/90/{|{+i}\rangle}/right/solid, 90/270/{|{-i}\rangle}/left/dashed} {
    \draw[->, thick, red!70!black, \sty]
      (0,0,0) -- ({\R*sin(\th)*cos(\ph)}, {\R*sin(\th)*sin(\ph)}, {\R*cos(\th)}) node[\pos] {$\lab$};
  }
\end{tikzpicture}
\end{document}

Frequently asked questions

How do I draw a Bloch sphere in TikZ?

Draw a circle for the sphere, an ellipse for the equator (dashed behind), three axes from the centre and the state vector, then mark θ and φ with \pic {angle=…} from the angles library. For a vector placed exactly from θ and φ, set the x, y and z unit vectors of the picture and use 3D coordinates, as in the first variation.

How do I draw an angle with an arrow and a label in TikZ?

Load \usetikzlibrary{angles, quotes} and write \pic [draw, ->, "$\theta$", angle radius=6mm] {angle = A--O--B}; where A, O and B are named coordinates or nodes. angle eccentricity moves the label towards or away from the vertex.

Why does ellipse (\r{} and \r/3) need the {}?

TeX drops the space after a command name, so \r and would reach TikZ as 3and and the keyword and would not be found. The empty group {} ends the command name and keeps the space.

References

  1. janosh/diagrams at commit 7a79b31: assets/bloch-sphere/bloch-sphere.tex (MIT licence)github.com
  2. PGF/TikZ manual, §13.2.1 Canvas, XYZ, and Polar Coordinate Systemstikz.dev
  3. PGF/TikZ manual, §13.4.1 Specifying Relative Coordinates (++)tikz.dev
  4. PGF/TikZ manual, §14.6 The Circle and Ellipse Operationstikz.dev
  5. PGF/TikZ manual, §14.12 The Plot Operationtikz.dev
  6. PGF/TikZ manual, §15.7 Shading a Path (ball color)tikz.dev
  7. PGF/TikZ manual, §17.10.2 The Label Optiontikz.dev
  8. PGF/TikZ manual, §41 Angle Librarytikz.dev
  9. PGF/TikZ manual, §17.10.4 The Quotes Syntaxtikz.dev

The walkthrough was checked against these sources and the pictures are the code typeset with TeX Live 2026. The original figure is © Janosh Riebesell and contributors, under the MIT licence; the variations on this page are ours, CC0 1.0.

TikZ guides for these techniques

More figures explained

Change this figure in your browserOpen the figure or any variation in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.

Open the figure in the TikZ editor