Skip to main content
IBM Quantum Platform

The Deutsch-Jozsa algorithm

Deutsch's algorithm outperforms all classical algorithms for a query problem, but the advantage is quite modest: one query versus two. The Deutsch-Jozsa algorithm extends this advantage — and, in fact, it can be used to solve a couple of different query problems.

Here's a quantum circuit description of the Deutsch-Jozsa algorithm. An additional classical post-processing step, not shown in the figure, may also be required depending on the specific problem being solved.

Deutsch-Jozsa algorithm

Of course, we haven't actually discussed what problems this algorithm solves; this is done in the two sections that follow.


The Deutsch-Jozsa problem

We'll begin with the query problem the Deutsch-Jozsa algorithm was originally intended to solve, which is known as the Deutsch-Jozsa problem.

The input function for this problem takes the form f:ΣnΣf:\Sigma^n \rightarrow \Sigma for an arbitrary positive integer n.n. Like Deutsch's problem, the task is to output 00 if ff is constant and 11 if ff is balanced, which again means that the number of input strings on which the function takes the value 00 is equal to the number of input strings on which the function takes the value 11.

Notice that, when nn is larger than 1,1, there are functions of the form f:ΣnΣf:\Sigma^n \rightarrow \Sigma that are neither constant nor balanced. For example, the function f:Σ2Σf:\Sigma^2\rightarrow\Sigma defined as

f(00)=0f(01)=0f(10)=0f(11)=1\begin{aligned} f(00) & = 0 \\ f(01) & = 0 \\ f(10) & = 0 \\ f(11) & = 1 \end{aligned}

falls into neither of these two categories. For the Deutsch-Jozsa problem, we simply don't worry about functions like this — they're considered to be "don't care" inputs. That is, for this problem we have a promise that ff is either constant or balanced.

Deutsch-Jozsa problem
Input: a function f:{0,1}n{0,1}f:\{0,1\}^n\rightarrow\{0,1\}
Promise: ff is either constant or balanced
Output: 00 if ff is constant, 11 if ff is balanced

The Deutsch-Jozsa algorithm, with its single query, solves this problem in the following sense: if every one of the nn measurement outcomes is 0,0, then the function ff is constant; and otherwise, if at least one of the measurement outcomes is 1,1, then the function ff is balanced. Another way to say this is that the circuit described above is followed by a classical post-processing step in which the OR of the measurement outcomes is computed to produce the output.

Algorithm analysis

To analyze the performance of the Deutsch-Jozsa algorithm for the Deutsch-Jozsa problem, it's helpful to begin by thinking about the action of a single layer of Hadamard gates. A Hadamard operation can be expressed as a matrix in the usual way,

H=(12121212),H = \begin{pmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\[2mm] \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{pmatrix},

but we can also express this operation in terms of its action on standard basis states:

H0=120+121H1=120121.\begin{aligned} H \vert 0\rangle & = \frac{1}{\sqrt{2}} \vert 0 \rangle + \frac{1}{\sqrt{2}} \vert 1 \rangle\\[3mm] H \vert 1\rangle & = \frac{1}{\sqrt{2}} \vert 0 \rangle - \frac{1}{\sqrt{2}} \vert 1 \rangle. \end{aligned}

These two equations can be combined into a single formula,

Ha=120+12(1)a1=12b{0,1}(1)abb,H \vert a \rangle = \frac{1}{\sqrt{2}} \vert 0 \rangle + \frac{1}{\sqrt{2}} (-1)^a \vert 1 \rangle = \frac{1}{\sqrt{2}} \sum_{b\in\{0,1\}} (-1)^{ab} \vert b\rangle,

which is true for both choices of aΣ.a\in\Sigma.

Now suppose that instead of just a single qubit we have nn qubits, and a Hadamard operation is performed on each. The combined operation on the nn qubits is described by the tensor product HHH\otimes \cdots \otimes H (nn times), which we write as HnH^{\otimes n} for conciseness and clarity. Using the formula from above, followed by expanding and then simplifying, we can express the action of this combined operation on the standard basis states of nn qubits like this:

Hnxn1x1x0=(Hxn1)(Hx0)=(12yn1Σ(1)xn1yn1yn1)(12y0Σ(1)x0y0y0)=12nyn1y0Σn(1)xn1yn1++x0y0yn1y0.\begin{aligned} & H^{\otimes n} \vert x_{n-1} \cdots x_1 x_0 \rangle \\ & \qquad = \bigl(H \vert x_{n-1} \rangle \bigr) \otimes \cdots \otimes \bigl(H \vert x_{0} \rangle \bigr) \\ & \qquad = \Biggl( \frac{1}{\sqrt{2}} \sum_{y_{n-1}\in\Sigma} (-1)^{x_{n-1} y_{n-1}} \vert y_{n-1} \rangle \Biggr) \otimes \cdots \otimes \Biggl( \frac{1}{\sqrt{2}} \sum_{y_{0}\in\Sigma} (-1)^{x_{0} y_{0}} \vert y_{0} \rangle \Biggr) \\ & \qquad = \frac{1}{\sqrt{2^n}} \sum_{y_{n-1}\cdots y_0 \in \Sigma^n} (-1)^{x_{n-1}y_{n-1} + \cdots + x_0 y_0} \vert y_{n-1} \cdots y_0 \rangle. \end{aligned}

Here, by the way, we're writing binary strings of length nn as xn1x0x_{n-1}\cdots x_0 and yn1y0,y_{n-1}\cdots y_0, following Qiskit's indexing convention.

This formula provides us with a useful tool for analyzing the quantum circuit above. After the first layer of Hadamard gates is performed, the state of the n+1n+1 qubits (including the leftmost/bottom qubit, which is treated separately from the rest) is

(H1)(Hn00)=12nxn1x0Σnxn1x0.\bigl( H \vert 1 \rangle \bigr) \bigl( H^{\otimes n} \vert 0 \cdots 0 \rangle \bigr) = \vert - \rangle \otimes \frac{1}{\sqrt{2^n}} \sum_{x_{n-1}\cdots x_0 \in \Sigma^n} \vert x_{n-1} \cdots x_0 \rangle.

When the UfU_f operation is performed, this state is transformed into

12nxn1x0Σn(1)f(xn1x0)xn1x0\vert - \rangle \otimes \frac{1}{\sqrt{2^n}} \sum_{x_{n-1}\cdots x_0 \in \Sigma^n} (-1)^{f(x_{n-1}\cdots x_0)} \vert x_{n-1} \cdots x_0 \rangle

through exactly the same phase kick-back phenomenon that we saw in the analysis of Deutsch's algorithm.

Then the second layer of Hadamard gates is performed, which (by the formula above) transforms this state into

12nxn1x0Σnyn1y0Σn(1)f(xn1x0)+xn1yn1++x0y0yn1y0.\vert - \rangle \otimes \frac{1}{2^n} \sum_{x_{n-1}\cdots x_0 \in \Sigma^n} \sum_{y_{n-1}\cdots y_0 \in \Sigma^n} (-1)^{f(x_{n-1}\cdots x_0) + x_{n-1}y_{n-1} + \cdots + x_0 y_0} \vert y_{n-1} \cdots y_0 \rangle.

This expression looks somewhat complicated, and not too much can be concluded about the probabilities to obtain different measurement outcomes without knowing more about the function f.f.

Fortunately, all we need to know is the probability that every one of the measurement outcomes is 00 — because that's the probability that the algorithm determines that ff is constant. This probability has a simple formula.

12nxn1x0Σn(1)f(xn1x0)2={1if f is constant0if f is balanced\Biggl\vert \frac{1}{2^n} \sum_{x_{n-1}\cdots x_0 \in \Sigma^n} (-1)^{f(x_{n-1}\cdots x_0)} \Biggr\vert^2 = \begin{cases} 1 & \text{if $f$ is constant}\\[1mm] 0 & \text{if $f$ is balanced} \end{cases}

In greater detail, if ff is constant, then either f(xn1x0)=0f(x_{n-1}\cdots x_0) = 0 for every string xn1x0,x_{n-1}\cdots x_0, in which case the value of the sum is 2n,2^n, or f(xn1x0)=1f(x_{n-1}\cdots x_0) = 1 for every string xn1x0,x_{n-1}\cdots x_0, in which case the value of the sum is 2n.-2^n. Dividing by 2n2^n and taking the square of the absolute value yields 1.1.

If, on the other hand, ff is balanced, then ff takes the value 00 on half of the strings xn1x0x_{n-1}\cdots x_0 and the value 11 on the other half, so the +1+1 terms and 1-1 terms in the sum cancel and we're left with the value 0.0.

We conclude that the algorithm operates correctly provided that the promise is fulfilled.

Classical difficulty

The Deutsch-Jozsa algorithm works every time, always giving us the correct answer when the promise is met, and requires a single query. How does this compare with classical query algorithms for the Deutsch-Jozsa problem?

First, any deterministic classical algorithm that correctly solves the Deutsch-Jozsa problem must make exponentially many queries: 2n1+12^{n-1} + 1 queries are required in the worst case. The reasoning is that, if a deterministic algorithm queries ff on 2n12^{n-1} or fewer different strings, and obtains the same function value every time, then both answers are still possible. The function might be constant, or it might be balanced but through bad luck the queries all happen to return the same function value.

The second possibility might seem unlikely — but for deterministic algorithms there's no randomness or uncertainty, so they will fail systematically on certain functions. We therefore have a significant advantage of quantum over classical algorithms in this regard.

There is a catch, however, which is that probabilistic classical algorithms can solve the Deutsch-Jozsa problem with very high probability using just a few queries. In particular, if we simply choose a few different strings of length nn randomly, and query ff on those strings, it's unlikely that we'll get the same function value for all of them when ff is balanced.

To be specific, if we choose kk input strings x1,,xkΣnx^1,\ldots,x^k \in \Sigma^n uniformly at random, evaluate f(x1),,f(xk),f(x^1),\ldots,f(x^k), and answer 00 if the function values are all the same, and 11 if not, then we'll always be correct when ff is constant, and wrong in the case that ff is balanced with probability just 2k+1.2^{-k + 1}. If we take k=11,k = 11, for instance, this algorithm will answer correctly with probability greater than 99.999.9%.

For this reason, we do still have a rather modest advantage of quantum over classical algorithms — but it is nevertheless a quantifiable advantage representing an improvement over Deutsch's algorithm.


The Bernstein-Vazirani problem

Next, we'll discuss a problem known as the Bernstein-Vazirani problem. It's also called the Fourier sampling problem, although there are more general formulations of this problem that also go by that name.

First, let's introduce some notation. For any two binary strings x=xn1x0x = x_{n-1} \cdots x_0 and y=yn1y0y = y_{n-1}\cdots y_0 of length n,n, we define

xy=xn1yn1x0y0.x \cdot y = x_{n-1} y_{n-1} \oplus \cdots \oplus x_0 y_0.

We'll refer to this operation as the binary dot product. An alternative way to define it is like so.

xy={1xn1yn1++x0y0 is odd0xn1yn1++x0y0 is evenx \cdot y = \begin{cases} 1 & x_{{n-1}} y_{n-1} + \cdots + x_0 y_0 \text{ is odd}\\[0.5mm] 0 & x_{{n-1}} y_{n-1} + \cdots + x_0 y_0 \text{ is even} \end{cases}

Notice that this is a symmetric operation, meaning that the result doesn't change if we swap xx and y,y, so we're free to do that whenever it's convenient. Sometimes it's useful to think about the binary dot product xyx \cdot y as being the parity of the bits of xx in positions where the string yy has a 1,1, or equivalently, the parity of the bits of yy in positions where the string xx has a 1.1.

With this notation in hand we can now define the Bernstein-Vazirani problem.

Bernstein-Vazirani problem
Input: a function f:{0,1}n{0,1}f:\{0,1\}^n\rightarrow\{0,1\}
Promise: there exists a binary string s=sn1s0s = s_{n-1} \cdots s_0 for which f(x)=sxf(x) = s\cdot x for all xΣnx\in\Sigma^n
Output: the string ss

We don't actually need a new quantum algorithm for this problem; the Deutsch-Jozsa algorithm solves it. In the interest of clarity, let's refer to the quantum circuit from above, which doesn't include the classical post-processing step of computing the OR, as the Deutsch-Jozsa circuit.

Algorithm analysis

To analyze how the Deutsch-Jozsa circuit works for a function satisfying the promise for the Bernstein-Vazirani problem, we'll begin with a quick observation. Using the binary dot product, we can alternatively describe the action of nn Hadamard gates on the standard basis states of nn qubits as follows.

Hnx=12nyΣn(1)xyyH^{\otimes n} \vert x \rangle = \frac{1}{\sqrt{2^n}} \sum_{y\in\Sigma^n} (-1)^{x\cdot y} \vert y\rangle

Similar to what we saw when analyzing Deutsch's algorithm, this is because the value (1)k(-1)^k for any integer kk depends only on whether kk is even or odd.

Turning to the Deutsch–Jozsa circuit, after the first layer of Hadamard gates is performed, the state of the n+1n+1 qubits is

12nxΣnx.\vert - \rangle \otimes \frac{1}{\sqrt{2^n}} \sum_{x \in \Sigma^n} \vert x \rangle.

The query gate is then performed, which (through the phase kickback phenomenon) transforms the state into

12nxΣn(1)f(x)x.\vert - \rangle \otimes \frac{1}{\sqrt{2^n}} \sum_{x \in \Sigma^n} (-1)^{f(x)} \vert x \rangle.

Using our formula for the action of a layer of Hadamard gates, we see that the second layer of Hadamard gates then transforms this state into

12nxΣnyΣn(1)f(x)+xyy.\vert - \rangle \otimes \frac{1}{2^n} \sum_{x \in \Sigma^n} \sum_{y \in \Sigma^n} (-1)^{f(x) + x \cdot y} \vert y \rangle.

Now we can make some simplifications, in the exponent of 1-1 inside the sum. We're promised that f(x)=sxf(x) = s\cdot x for some string s=sn1s0,s = s_{n-1} \cdots s_0, so we can express the state as

12nxΣnyΣn(1)sx+xyy.\vert - \rangle \otimes \frac{1}{2^n} \sum_{x \in \Sigma^n} \sum_{y \in \Sigma^n} (-1)^{s\cdot x + x \cdot y} \vert y \rangle.

Because sxs\cdot x and xyx\cdot y are binary values, we can replace the addition with the exclusive-OR — again because the only thing that matters for an integer in the exponent of 1-1 is whether it is even or odd. Making use of the symmetry of the binary dot product, we obtain this expression for the state:

12nxΣnyΣn(1)(sx)(yx)y.\vert - \rangle \otimes \frac{1}{2^n} \sum_{x \in \Sigma^n} \sum_{y \in \Sigma^n} (-1)^{(s\cdot x) \oplus (y \cdot x)} \vert y \rangle.

(Parentheses have been added for clarity, though they aren't really necessary because it's conventional to treat the binary dot product as having higher precedence than the exclusive-OR.)

At this point we will make use of the following formula.

(sx)(yx)=(sy)x(s\cdot x) \oplus (y \cdot x) = (s \oplus y) \cdot x

We can obtain the formula through a similar formula for bits,

(ac)(bc)=(ab)c,(a c) \oplus (b c) = (a \oplus b) c,

together with an expansion of the binary dot product and bitwise exclusive-OR:

(sx)(yx)=(sn1xn1)(s0x0)(yn1xn1)(y0x0)=(sn1yn1)xn1(s0y0)x0=(sy)x\begin{aligned} (s\cdot x) \oplus (y \cdot x) & = (s_{n-1} x_{n-1}) \oplus \cdots \oplus (s_{0} x_{0}) \oplus (y_{n-1} x_{n-1}) \oplus \cdots \oplus (y_{0} x_{0}) \\ & = (s_{n-1} \oplus y_{n-1}) x_{n-1} \oplus \cdots \oplus (s_{0} \oplus y_{0}) x_{0} \\ & = (s \oplus y) \cdot x \end{aligned}

This allows us to express the state of the circuit immediately prior to the measurements like this:

12nxΣnyΣn(1)(sy)xy.\vert - \rangle \otimes \frac{1}{2^n} \sum_{x \in \Sigma^n} \sum_{y \in \Sigma^n} (-1)^{(s\oplus y)\cdot x} \vert y \rangle.

The final step is to make use of yet another formula, which works for every binary string z=zn1z0.z = z_{n-1}\cdots z_0.

12nxΣn(1)zx={1if z=0n0if z0n\frac{1}{2^n} \sum_{x \in \Sigma^n} (-1)^{z \cdot x} = \begin{cases} 1 & \text{if $z = 0^n$}\\ 0 & \text{if $z\neq 0^n$} \end{cases}

Here we're using a simple notation for strings that we'll use several more times in the lesson: 0n0^n is the all-zero string of length n.n.

A simple way to argue that this formula works is to consider the two cases separately. If z=0n,z = 0^n, then zx=0z\cdot x = 0 for every string xΣn,x\in\Sigma^n, so the value of each term in the sum is 1,1, and we obtain 11 by summing and dividing by 2n.2^n. On the other hand, if any one of the bits of zz is equal to 1,1, then the binary dot product zxz\cdot x is equal to 00 for exactly half of the possible choices for xΣnx\in\Sigma^n and 11 for the other half — because the value of the binary dot product zxz\cdot x flips (from 00 to 11 or from 11 to 00) if we flip any bit of xx in a position where zz has a 1.1.

If we now apply this formula to simplify the state of the circuit prior to the measurements, we obtain

12nxΣnyΣn(1)(sy)xy=s,\vert - \rangle \otimes \frac{1}{2^n} \sum_{x \in \Sigma^n} \sum_{y \in \Sigma^n} (-1)^{(s\oplus y)\cdot x} \vert y \rangle = \vert - \rangle \otimes \vert s \rangle,

owing to the fact that sy=0ns\oplus y = 0^n if and only if y=s.y = s. Thus, the measurements reveal precisely the string ss we're looking for.

Classical difficulty

While the Deutsch-Jozsa circuit solves the Bernstein-Vazirani problem with a single query, any classical query algorithm must make at least nn queries to solve this problem.

This can be reasoned through a so-called information theoretic argument, which is very simple in this case. Each classical query reveals a single bit of information about the solution, and there are nn bits of information that need to be uncovered — so at least nn queries are needed.

It is, in fact, possible to solve the Bernstein-Vazirani problem classically by querying the function on each of the nn strings having a single 1,1, in each possible position, and 00 for all other bits, which reveals the bits of ss one at a time. So, the advantage of quantum over classical algorithms for this problem is 11 query versus nn queries.

Remark on nomenclature

In the context of the Bernstein-Vazirani problem, it is common that the Deutsch-Jozsa algorithm is referred to as the "Bernstein-Vazirani algorithm." This is slightly misleading, because the algorithm is the Deutsch-Jozsa algorithm, as Bernstein and Vazirani were very clear about in their work.

What Bernstein and Vazirani did after showing that the Deutsch-Jozsa algorithm solves the Bernstein-Vazirani problem (as it is stated above) was to define a much more complicated problem, known as the recursive Fourier sampling problem. This is a highly contrived problem where solutions to different instances of the problem effectively unlock new levels of the problem arranged in a tree-like structure. The Bernstein-Vazirani problem is essentially just the base case of this more complicated problem.

The recursive Fourier sampling problem was the first known example of a query problem where quantum algorithms have a so-called super-polynomial advantage over probabilistic algorithms, thereby surpassing the advantage of quantum over classical offered by the Deutsch-Jozsa algorithm. Intuitively speaking, the recursive version of the problem amplifies the 11 versus nn advantage of quantum algorithms to something much larger.

The most challenging aspect of the mathematical analysis establishing this advantage is showing that classical query algorithms can't solve the problem without making lots of queries. This is quite typical; for many problems it can be very difficult to rule out creative classical approaches that solve them efficiently.

Simon's problem, and the algorithm for it described in the next section, does provide a much simpler example of a super-polynomial (and, in fact, exponential) advantage of quantum over classical algorithms, and for this reason the recursive Fourier sampling problem is less often discussed. It is, nevertheless, an interesting computational problem in its own right.

Was this page helpful?
Report a bug or request content on GitHub.