5.2.2 Hadamard transform and interference
While Deutsch’s algorithm is very surprising, it achieves only a very small improvement over the best classical algorithm, namely 1 evaluation of instead of 2. This could be useful if evaluating takes a very long time, say a year. But if it takes only a millisecond, then most people would not mind to wait for two milliseconds to get the answer (and pay much less for it because they would not need to use a quantum computer)! To demonstrate the usefulness of quantum computers, we would like to speed up computations by more than just a factor of 2.
There is no hope of getting a larger speedup if we only look at functions of a single bit, since those can be fully determined by two evaluations: and . We will therefore look more generally at functions with input bits, since there are many more such functions (indeed, there are of them). Recall that our goal is not to evaluate a function (indeed, we can do that with a single query to the oracle) but rather to learn some interesting property of the function that relates the values it takes on different inputs. Are there properties of -bit functions that we can learn very efficiently by using clever quantum algorithms?
To generalize Deutsch’s algorithm, observe that its key ingredient was to introduce a Hadamard gate before and after the sign oracle. We can do something very similar if we have an -bit function. In this case, the sign oracle is a quantum operation of qubits, so we could simply apply Hadamard gates on all qubits before and after the oracle. The quantum operation that applies Hadamards on each of the qubits in parallel is called the Hadamard transform. Recall from Section 3.2.3 that we can write it as follows:
Let us first see what happens if we apply the Hadamard transform to a basis state. For , the result is simply the uniform superposition over all basis states:
This state is a superposition of basis states. There is a more compact way of writing this:
The notation means that you compute the sum of for all possible choices of the bits .
More generally, the output on an arbitrary basis state is always a superposition of all basis states with all amplitudes equal except for signs. Figuring out what exactly those signs are is a bit tricky. As a warm-up, try it first for the and cases.
Exercise 5.4 (Two Hadamards).
Recall from Eq. 2.34 that , for any .
-
1.
Write the state , for arbitrary , as follows:
where ??? is some expression involving . Determine this expression.
-
2.
Write the state , for arbitrary , in the form
where ??? stands for some expression involving .
Can you determine what this expression is?
Solution.
-
1.
Here ??? stands for .
-
2.
For ,
so ??? stands for .
Did you figure out the solution? Good, then you are allowed to read on!
In general, you can derive the following formula that describes the sign pattern of the amplitudes you get when applying the Hadamard transform to any -qubit basis state: For any ,
We can now generalize the Deutsch algorithm in a straightforward way. Starting with the -qubit basis state , we first apply the Hadamard transform, next the sign oracle for the function of interest, and finally another Hadamard transform. For example, for , this corresponds to the following Quirky circuit:
For general , the mathematical formula for the final -qubit state is:
Can we write down this state more explicitly? As before, we can compute this step by step. First, we apply the Hadamard transform to the all-zeros basis state. By Eq. 5.10, we obtain the uniform superposition over all basis states:
Next we apply the sign oracle :
What does the final Hadamard transform achieve? By linearity, we can apply Eq. 5.11 to each basis vector, so we obtain the following expression for the state in Eq. 5.12:
where we exchanged the two sums to obtain the last equality. For this is exactly the same as Eq. 5.7. In general, however, this expression looks quite unwieldy and hard to interpret – it seems like the circuit computes some superposition of basis states, where the amplitude is some strange sum of plus and minus signs!
Let us try to get some intuition by looking at the amplitude of in Eq. 5.13. The square of this number is the probability that, when we measure the qubits, all outcomes are zero. Since this amplitude corresponds to , it is given simply by
What does this mean? Suppose that there are input bitstrings for which evaluates to zero and bitstrings for which evaluates to one. Then,
There are two interesting extreme cases:1414 14 For , every function is either constant or balanced. For this is not true (e.g., the AND function is neither constant nor balanced).
-
•
If is a constant function then either (for the all-zeros function) or (for the all-ones function). Either way, the amplitude is . Since the squares of all amplitudes should sum to 1, we conclude that all other amplitudes in Eq. 5.13 must be 0. In other words, whenever is constant, the state in Eq. 5.13 is simply . If we measure all qubits of this state then all outcomes will be zero.
-
•
If is a balanced function, which means that there are as many zeros as ones, then and so the amplitude is 0. This means that if we measure the state in Eq. 5.13 then we can never get all outcomes equal to zero. Hence, for a balanced function at least one of the measurement outcomes will always be one.
Note how these two cases correspond to very different patterns of interference (see Section 2.6.1). In the first case, the amplitude of gets amplified to due to a highly focused constructive interference, while at the same time all other amplitudes simultaneously vanish due to a massively widespread destructive interference. In the second case, experiences destructive interference, causing non-zero amplitudes to pop up elsewhere. Hadamard transform illustrates the central importance of interference in quantum computing. In the next two sections, we will make crucial use of it to design quantum algorithms on a large number of qubits.