5.2.1 Deutsch’s algorithm

It is late on Sunday evening. Alice and Bob have just finished their homework assignments for the quantum computing class and are about to watch a 3D movie. When they turn on their holographic television set, they discover that the movie screening has been postponed due to unexpected dramatic news coverage coming from the International Transgalactic Station. There has been a terrible accident and a module containing two crew members, Hila and Iman, has separated from the main mothership. The last message received from the module was that Iman has been injured and is heavily bleeding – he is in need of an urgent blood transfer. What makes the situation complicated is that Hila and Iman are somewhat in shock and forgot their own blood type – they can only remember that they each had blood type A or B. The news presenter is appealing to the general public for help with suggesting a way for Hila and Iman to determine if they have the same blood type, because if that was the case Hila could transfer her blood to Iman to save his life. This is because the medical kit in Hila and Iman’s module includes a lympho-transcoder that is able to convert either of the two blood types to the opposite one. Hence, even if it turns out that their blood types are mismatching, Hila can convert her’s to the opposite one using the lympho-transcoder.

Upon hearing these news, Alice and Bob immediately abandon their plan to watch a movie and start to ponder what could be done to help Hila and Iman. The news coverage continues with some additional information. Luckily, it turns out that the module involved in the accident contains a database chip that stores Hila and Iman’s blood type. We can model this by a function f:{0,1}{0,1}f\colon\{0,1\}\to\{0,1\}, where

f(0)\displaystyle f(0) ={0if Hila’s blood type is A,1if Hila’s blood type is B.\displaystyle=\begin{cases}0&\text{if Hila's blood type is A},\\ 1&\text{if Hila's blood type is B}.\end{cases}
and
f(1)\displaystyle f(1) ={0if Iman’s blood type is A,1if Iman’s blood type is B.\displaystyle=\begin{cases}0&\text{if Iman's blood type is A},\\ 1&\text{if Iman's blood type is B}.\end{cases}

What needs to be determined is whether f(0)=f(1)f(0)=f(1) or not!

The solution seems at hand: Hila and Iman simply need to query the database twice to read out their respective blood types, f(0)f(0) and f(1)f(1), and then compare the two values to see if they are the same. Unfortunately, the accident has partly fried the control logic of the database chip and the news presenter reports that after the first query the database chip will likely burn out completely.

Our two protagonists are at an impasse. Clearly, any classical algorithm needs to evaluate ff twice in order to determine whether f(0)=f(1)f(0)=f(1). Indeed, if you know only the value of f(0)f(0) then whether f(0)=f(1)f(0)=f(1) still depends on the value of f(1)f(1) and, unless you also compute f(1)f(1), you would not be able to tell whether f(0)=f(1)f(0)=f(1). Similarly, if you know only f(1)f(1) then you cannot compare it with f(0)f(0) unless you also know what the value of f(0)f(0) is. No matter what strategy you use, you need to know both f(0)f(0) and f(1)f(1) in order to determine whether f(0)=f(1)f(0)=f(1). Is there really no way around this?

After flipping through some manuals, Bob discovers that the database chip can be switched into quantum mode. When enabled, the database chip no longer evaluates the function classically but instead implements the sign oracle OfO_{f}. Could this somehow be used to solve the problem? Alice thinks about it for a second and suddenly realizes that this is precisely what Deutsch’s algorithm is for! Alice and Bob quickly go over some calculations to confirm that it works and sit down to write an intergalactic e-mail with instructions to Hila and Iman on how they can solve the problem. Their instructions are as follows:

  1. 1.

    Prepare a qubit in state |+=(|0+|1)/2\left|+\right\rangle=(\left|0\right\rangle+\left|1\right\rangle)/\sqrt{2}.

  2. 2.

    Use the database chip in quantum mode to apply the operation OfO_{f}.

  3. 3.

    Apply the Hadamard gate HH to the output qubit and measure it.

  4. 4.

    If the outcome is 0 then Hila and Iman have the same blood type and otherwise they have different blood types.

Note that in this procedure Hila and Iman only query the database chip once to determine if they have the same blood type. Here is an implementation of the algorithm in Quirky:

[Uncaptioned image]

The picture shows that the outcome is 11, so Hila and Iman have different blood types.

But why does Deutsch’s algorithm work? Let us analyze it step by step. The first Hadamard gate creates the state |+=H|0\left|+\right\rangle=H\left|0\right\rangle. Next, we apply the sign oracle OfO_{f}, which leads to the state

Of|+\displaystyle O_{f}\left|+\right\rangle =12Of|0+12Of|1\displaystyle=\frac{1}{\sqrt{2}}O_{f}\left|0\right\rangle+\frac{1}{\sqrt{2}}O_% {f}\left|1\right\rangle
=12(1)f(0)|0+12(1)f(1)|1.\displaystyle=\frac{1}{\sqrt{2}}(-1)^{f(0)}\left|0\right\rangle+\frac{1}{\sqrt% {2}}(-1)^{f(1)}\left|1\right\rangle.

After applying the second Hadamard, we obtain the following state:

HOf|+\displaystyle HO_{f}\left|+\right\rangle =12(1)f(0)H|0+12(1)f(1)H|1\displaystyle=\frac{1}{\sqrt{2}}(-1)^{f(0)}H\left|0\right\rangle+\frac{1}{% \sqrt{2}}(-1)^{f(1)}H\left|1\right\rangle
=12(1)f(0)|++12(1)f(1)|\displaystyle=\frac{1}{\sqrt{2}}(-1)^{f(0)}\left|+\right\rangle+\frac{1}{\sqrt% {2}}(-1)^{f(1)}\left|-\right\rangle
=12(1)f(0)(|0+|1)+12(1)f(1)(|0|1)\displaystyle=\frac{1}{2}(-1)^{f(0)}\left(\left|0\right\rangle+\left|1\right% \rangle\right)+\frac{1}{2}(-1)^{f(1)}\left(\left|0\right\rangle-\left|1\right% \rangle\right)
=(1)f(0)+(1)f(1)2|0+(1)f(0)(1)f(1)2|1.\displaystyle=\frac{(-1)^{f(0)}+(-1)^{f(1)}}{2}\left|0\right\rangle+\frac{(-1)% ^{f(0)}-(-1)^{f(1)}}{2}\left|1\right\rangle. (5.7)

Note that the two signs (1)f(0)(-1)^{f(0)} and (1)f(1)(-1)^{f(1)} are added in the first amplitude, but subtracted in the second amplitude. Depending on the values of f(0)f(0) and f(1)f(1), for each amplitude we will observe either constructive or destructive interference (see Section 2.6.1). In fact, which of the two amplitudes will remain is determined only by whether f(0)f(0) and f(1)f(1) are equal or not:

f(0)=f(1):\displaystyle f(0)=f(1):\qquad HOf|+=±|0,\displaystyle HO_{f}\left|+\right\rangle=\pm\left|0\right\rangle, (5.8)
f(0)f(1):\displaystyle f(0)\neq f(1):\qquad HOf|+=±|1.\displaystyle HO_{f}\left|+\right\rangle=\pm\left|1\right\rangle.

It is a good exercise to verify this explicitly:

Exercise 5.3 (Verifying Deutsch’s algorithm).

Recall from 5.1 that there are four functions f:{0,1}{0,1}f:\{0,1\}\to\{0,1\}. For each function, compute the state HOf|+HO_{f}\left|+\right\rangle using Eq. 5.7.

Solution. We evaluate Eq. 5.7 for all four functions.
  • For f(x)=xf(x)=x:

    HOf|+=1+(1)2|0+1(1)2|1=|1.\displaystyle HO_{f}\left|+\right\rangle=\frac{1+(-1)}{2}\left|0\right\rangle+% \frac{1-(-1)}{2}\left|1\right\rangle=\left|1\right\rangle.
  • For f(x)=NOT(x)f(x)=\mathrm{NOT}(x):

    HOf|+=1+12|0+112|1=|1.\displaystyle HO_{f}\left|+\right\rangle=\frac{-1+1}{2}\left|0\right\rangle+% \frac{-1-1}{2}\left|1\right\rangle=-\left|1\right\rangle.
  • For the all-zeros function:

    HOf|+=1+12|0+112|1=|0.\displaystyle HO_{f}\left|+\right\rangle=\frac{1+1}{2}\left|0\right\rangle+% \frac{1-1}{2}\left|1\right\rangle=\left|0\right\rangle.
  • For the all-ones function:

    HOf|+=(1)+(1)2|0+(1)(1)2|1=|0.\displaystyle HO_{f}\left|+\right\rangle=\frac{(-1)+(-1)}{2}\left|0\right% \rangle+\frac{(-1)-(-1)}{2}\left|1\right\rangle=-\left|0\right\rangle.

Eq. 5.8 shows that the final measurement yields outcome 0 if and only if f(0)=f(1)f(0)=f(1). Thus, Deutsch’s algorithm correctly determines whether f(0)=f(1)f(0)=f(1). Importantly, it evaluates the function f:{0,1}{0,1}f:\{0,1\}\to\{0,1\} only once by using the sign oracle. In contrast, we discussed above that any classical algorithm necessarily needs to evaluate both function values f(0)f(0) and f(1)f(1) separately.

Another way to interpret Deutsch’s algorithm is that it computes the sum of the two bits f(0)f(0) and f(1)f(1) modulo two. This is because f(0)f(1)=0f(0)\oplus f(1)=0 if and only if f(0)=f(1)f(0)=f(1). Indeed, recall from Eq. 3.21 that addition modulo two works as follows:

x1x_{1} x2x_{2} x1x2x_{1}\oplus x_{2}
0 0 0
0 1 1
1 0 1
1 1 0
(5.9)

This is also why the sum modulo two is known as the XOR (short for ‘exclusive OR’) of the two bits, since it is one if exactly one of the two bits is set.