Categories: Algorithms, Quantum information.

Deutsch-Jozsa algorithm

The Deutsch algorithm and its extension, the Deutsch-Jozsa algorithm, were first to prove that quantum computers can solve certain problems more efficiently than any classical system.

Given an unknown “black box” binary function f(x)f(x) of one or more bits xx, the goal is determine whether ff is constant (i.e. f(x)f(x) is the same for all xx) or balanced (i.e. exactly 50% of all xx-values yield f(x)=0f(x) = 0, and the other 50% yield f(x)=1f(x) = 1). We can query ff as many times as we want with inputs of our choice, but we want to solve the problem using as few queries as possible.

The problem is extremely artificial and of no practical use, but quantum computers can solve it with a single query, while classical computers need up to 2N1+12^{N - 1} + 1 queries for an NN-bit xx.

Deutsch algorithm

The Deutsch algorithm handles the simplest case, where xx is only a single bit. Only four ff exist:

In other words, we only need to determine if f(0)=f(1)f(0) = f(1) or f(0)f(1)f(0) \neq f(1). To do this, we use the following quantum circuit, where UfU_f is the oracle we query:

Deutsch circuit

Due to unitarity constraints, the action of UfU_f is defined to be as follows, with \oplus meaning XOR:

xyUfxyf(x)\begin{aligned} \Ket{x} \Ket{y} \quad \to \boxed{U_f} \to \quad \Ket{x} \Ket{y \oplus f(x)} \end{aligned}

Starting on the left from two qubits 0\Ket{0} and 1\Ket{1}, we apply the Hadamard gate HH to both:

01H2+=12(0+1)(01)\begin{aligned} \Ket{0} \Ket{1} \quad \to \boxed{H^{\otimes 2}} \to \quad \Ket{+} \Ket{-} = \frac{1}{2} \Big( \Ket{0} + \Ket{1} \Big) \Big( \Ket{0} - \Ket{1} \Big) \end{aligned}

Feeding this result into the oracle UfU_f then leads us to:

Uf120(0f(0)1f(0))+121(0f(1)1f(1))\begin{aligned} \to \boxed{U_f} \to \quad \frac{1}{2} \Ket{0} \Big( \Ket{0 \oplus f(0)} - \Ket{1 \oplus f(0)} \Big) + \frac{1}{2} \Ket{1} \Big( \Ket{0 \oplus f(1)} - \Ket{1 \oplus f(1)} \Big) \end{aligned}

The parenthesized superpositions can be reduced. Assuming that f(b)=0f(b) = 0, we notice:

0f(b)1f(b)=0010=01\begin{aligned} \Ket{0 \oplus f(b)} - \Ket{1 \oplus f(b)} = \Ket{0 \oplus 0} - \Ket{1 \oplus 0} = \Ket{0} - \Ket{1} \end{aligned}

On the other hand, if we assume that f(b)=1f(b) = 1, we get the opposite result:

0f(b)1f(b)=0111=(01)\begin{aligned} \Ket{0 \oplus f(b)} - \Ket{1 \oplus f(b)} = \Ket{0 \oplus 1} - \Ket{1 \oplus 1} = - \big(\Ket{0} - \Ket{1}\big) \end{aligned}

We can thus combine both cases, f(b)=0f(b) = 0 or f(b)=1f(b) = 1, into the following single expression:

0f(b)1f(b)=(1)f(b)(01)\begin{aligned} \Ket{0 \oplus f(b)} - \Ket{1 \oplus f(b)} = (-1)^{f(b)} \big(\Ket{0} - \Ket{1}\big) \end{aligned}

Using this, we rewrite the intermediate state of the quantum circuit like so:

01H2Uf12((1)f(0)0+(1)f(1)1)(01)\begin{aligned} \Ket{0} \Ket{1} \quad \to \boxed{H^{\otimes 2}} \to \boxed{U_f} \to \quad \frac{1}{2} \Big( (-1)^{f(0)} \Ket{0} + (-1)^{f(1)} \Ket{1} \Big) \Big( \Ket{0} - \Ket{1} \Big) \end{aligned}

The second qubit in state \Ket{-} is garbage; it is no longer of interest. The first qubit is given by:

12((1)f(0)0+(1)f(1)1)=(1)f(0)2(0+(1)f(0)f(1)1)\begin{aligned} \frac{1}{\sqrt{2}} \Big( (-1)^{f(0)} \Ket{0} + (-1)^{f(1)} \Ket{1} \Big) = \frac{(-1)^{f(0)}}{\sqrt{2}} \Big( \Ket{0} + (-1)^{f(0) \oplus f(1)} \Ket{1} \Big) \end{aligned}

If ff is constant, then f(0)f(1)=0f(0) \oplus f(1) = 0, meaning this state is (1)f(0)+(-1)^{f(0)} \Ket{+}. On the other hand, if ff is balanced, then f(0)f(1)=1f(0) \oplus f(1) = 1, meaning this state is (1)f(0)(-1)^{f(0)} \Ket{-}. Taking the Hadamard transform of this qubit therefore yields:

H(1)f(0)f(0)f(1)\begin{aligned} \to \boxed{H} \to \quad (-1)^{f(0)} \Ket{f(0) \oplus f(1)} \end{aligned}

Depending on whether ff is constant or balanced, the mearurement outcome of this state will be 0\Ket{0} or 1\Ket{1} with 100\% probability. We have solved the problem!

Note that we only consulted the oracle (i.e. applied UfU_f) once. A classical computer would need to query it twice, once with input x=0x = 0, and again with x=1x = 1.

Deutsch-Jozsa algorithm

The Deutsch-Jozsa algorithm generalizes the above to NN-bit inputs xx. We are promised that f(x)f(x) is either constant or balanced; other possibilities are assumed to be impossible. This algorithm is then implemented by the following quantum circuit:

Deutsch-Jozsa circuit

There are NN qubits in initial state 0\Ket{0}, and one in 1\Ket{1}. For clarity, the oracle UfU_f works like so:

x1x2xNyUfx1xNyf(x1,...,xN)\begin{aligned} \Ket{x_1} \Ket{x_2} \cdots \Ket{x_N} \Ket{y} \quad \to \boxed{U_f} \to \quad \Ket{x_1} \cdots \Ket{x_N} \Ket{y \oplus f(x_1, ..., x_N)} \end{aligned}

Applying the N+1N + 1 Hadamard gates to the initial state yields the following superposition:

0N1HN+1+N=12Nx=02N1x\begin{aligned} \Ket{0}^{\otimes N} \Ket{1} \quad \to \boxed{H^{\otimes N + 1}} \to \quad \Ket{+}^{\otimes N} \Ket{-} = \frac{1}{\sqrt{2^N}} \sum_{x = 0}^{2^N - 1} \Ket{x} \Ket{-} \end{aligned}

Where x=x1xN\Ket{x} = \Ket{x_1} \cdots \Ket{x_N} denotes a classical binary state. For example, if x=5=20+22x = 5 = 2^0 + 2^2 in the summation, then x=1010N3\Ket{x} = \Ket{1} \Ket{0} \Ket{1} \Ket{0}^{\otimes N-3} (from least to most significant).

We give this state to the oracle, and, by the same logic as for the Deutsch algorithm, get back:

Uf12Nx=02N1(1)f(x)x\begin{aligned} \to \boxed{U_f} \to \quad \frac{1}{\sqrt{2^N}} \sum_{x = 0}^{2^N - 1} (-1)^{f(x)} \Ket{x} \Ket{-} \end{aligned}

The last qubit \Ket{-} is garbage. Next, applying the Hadamard transform to the other NN gives:

HN12Nx=02N1(1)f(x)(12Ny=02N1(1)xyy)\begin{aligned} \to \boxed{H^{\otimes N}} \to \quad \frac{1}{\sqrt{2^N}} \sum_{x = 0}^{2^N - 1} (-1)^{f(x)} \bigg( \frac{1}{\sqrt{2^N}} \sum_{y = 0}^{2^N - 1} (-1)^{x \cdot y} \Ket{y} \bigg) \end{aligned}

Where xyx \cdot y is the bitwise dot product of the binary representations of xx and yy, so, for example, if N=2N = 2, then xy=x1y1+x2y2x \cdot y = x_1 y_1 + x_2 y_2. Note that the above expression has not been reduced at all; it follows from the definition of the Hadamard transform. We can rewrite it like so:

12Nx=02N1y=02N1(1)f(x)+xyy=y=02N1(12Nx=02N1(1)f(x)+xy)y=y=02N1cyy\begin{aligned} \frac{1}{2^N} \sum_{x = 0}^{2^N - 1} \sum_{y = 0}^{2^N - 1} (-1)^{f(x) + x \cdot y} \Ket{y} = \sum_{y = 0}^{2^N - 1} \bigg( \frac{1}{2^N} \sum_{x = 0}^{2^N - 1} (-1)^{f(x) + x \cdot y} \bigg) \Ket{y} = \sum_{y = 0}^{2^N - 1} c_y \Ket{y} \end{aligned}

The parenthesized expression can be interpreted as the coefficients of a superposition of several yy-values. Therefore, the probability that a measurement yields y=0y = 0, i.e. y=0N\Ket{y} = \Ket{0}^{\otimes N}, is:

c02=12Nx=02N1(1)f(x)2\begin{aligned} |c_0|^2 = \bigg| \frac{1}{2^N} \sum_{x = 0}^{2^N - 1} (-1)^{f(x)} \bigg|^2 \end{aligned}

The summation always contains an even number of terms, for all values of NN. Consequently, if ff is constant, then c02= ⁣± ⁣2N/2N2=1|c_0|^2 = |\!\pm\! 2^N / 2^N|^2 = 1. Otherwise, if ff is balanced, all the terms cancel out, so we are left with c02=0|c_0|^2 = 0. In other words, we reach the same result as the Deutsch algorithm: we only need to measure the NN qubits once; ff is constant if and only if all are zero.

The Deutsch-Jozsa algorithm needs only one oracle query to give an error-free result, whereas a classical computer needs 2N1+12^{N-1} + 1 queries in the worst case; a revolutionary discovery.


  1. J.S. Neergaard-Nielsen, Quantum information: lectures notes, 2021, unpublished.
  2. S. Aaronson, Introduction to quantum information science: lecture notes, 2018, unpublished.