Hello! Welcome to the first lesson in our module on foundational quantum algorithms.
In the previous module, we established the fundamental language of quantum computing: qubits, gates, superposition, entanglement, and measurement. Now, we'll start using that language to build algorithms that can outperform classical computers on specific tasks.
Our goal for this lesson is to implement the Deutsch-Jozsa algorithm and analyze its quantum advantage over classical algorithms. This algorithm was one of the first to demonstrate that a quantum computer could solve a problem exponentially faster than any deterministic classical computer. While the problem it solves is quite specific, its study reveals core principles like quantum parallelism and interference that are central to more advanced algorithms.
Let's begin by formally defining the problem that the Deutsch-Jozsa algorithm addresses.
1. The Problem: Constant vs. Balanced Functions
The algorithm operates within a computational framework known as the query model. In this model, we are given access to a function f as a "black box" or "oracle." We can provide an input x to the oracle and receive the output f(x), but we cannot see the internal workings of the function. Our goal is to determine a global property of the function f using the minimum number of queries.
The Deutsch-Jozsa problem is defined as follows:
- Given: A function .
- Promise: The function is guaranteed to be either constant or balanced.
- A constant function returns the same output for all inputs (e.g., for all , or for all ).
- A balanced function returns 0 for exactly half of the inputs and 1 for the other half.
- Task: Determine whether is constant or balanced.
Classical Complexity
How many times would a classical computer need to query the function to solve this problem?
-
Deterministic Approach: In the worst-case scenario, you could query the function for half of all possible inputs ( queries) and get the same output every time. At this point, you still can't be certain. The function could be constant, or it could be balanced and you just happened to pick all the inputs that give the same output. You must make one more query, for a total of , to be 100% certain. For large , this is an exponential number of queries.
-
Probabilistic Approach: If we allow for a small probability of error, we can do much better. If we query the function for a few random inputs, say times, and get different outputs, we know immediately that the function is balanced. If we get the same output every time, the function is likely constant. The probability of getting the same output times from a balanced function is . By choosing a reasonably small (e.g., k=10), we can be almost certain of the answer.
This distinction between deterministic and probabilistic classical strategies is crucial for appreciating the nature of the quantum advantage we're about to see.
2. The Quantum Solution
The quantum approach solves this problem deterministically with just a single query to the oracle. To understand how, we first need to formalize the idea of a quantum query.
The following video by Prof. John Watrous provides an excellent introduction to the query model and the formal definition of a quantum oracle. It then uses these concepts to build up to the Deutsch-Jozsa algorithm, starting with the simpler n=1 case (Deutsch's algorithm).
Quantum Query Algorithms | Understanding Quantum Information & Computation | Lesson 05
Please watch the first three sections of this video from the Qiskit YouTube channel. It will introduce the query model, define quantum oracles, and walk through Deutsch's algorithm, which is the n=1 version of the problem we are solving. This will lay the groundwork for the general Deutsch-Jozsa algorithm.
Watch from the beginning until 34:15. Focus on: The concept of the query model and how it differs from standard computation. How a classical function f(x) is implemented as a unitary quantum gate U_f. The 'phase kickback' phenomenon, which is the key mechanism behind the algorithm.
The Deutsch-Jozsa Circuit and its Analysis
As you saw in the video, the key ideas are to use Hadamard gates to prepare a superposition of all possible inputs (quantum parallelism), encode the function's output into the phase of the quantum state (phase kickback), and then use another set of Hadamard gates to cause these phases to interfere, revealing the global property of the function.
Let's generalize this to qubits. The circuit is as follows:
Let's trace the state of the system through the circuit:
-
Initial State : We start with qubits in the state and one ancilla qubit in the state.
-
After First Hadamards : We apply Hadamard gates to all qubits. The first qubits go into an equal superposition of all computational basis states. The ancilla qubit becomes .
-
After Oracle : The oracle acts on a state as . Because our ancilla is in the special state , the "phase kickback" trick applies. The function value is "kicked back" into the phase of the corresponding state.
Applying this to our superposition gives:
At this point, we have queried the function once and simultaneously encoded the information about for all values of into the phases of our state. The ancilla qubit is no longer needed and can be ignored.
-
After Final Hadamards : The final step is to apply Hadamard gates to the first qubits. This operation, , is its own inverse and acts as a kind of Fourier transform over the group . It maps a computational basis state as follows:
where is the bitwise dot product. Applying this to gives the final state before measurement:
-
Measurement: We measure the first qubits. Let's consider the amplitude of the all-zeros state, , which corresponds to . For this state, the dot product is always 0. The amplitude is:
Now, we use our promise about the function :
- If is constant, . The sum becomes . The amplitude is . The probability of measuring is .
- If is balanced, exactly half the values are 0 and half are 1. The sum will have terms of and terms of , so the sum is 0. The amplitude is 0. The probability of measuring is 0.
Therefore, a single run of the circuit and a measurement of the first qubits is sufficient:
- If the result is , the function is constant.
- If the result is anything else, the function is balanced.
For a more condensed mathematical derivation, you can refer to these lecture notes.
Lecture 7: Deutsch-Jozsa algorithm
These lecture notes from the University of Cambridge provide a concise, mathematical summary of the Deutsch-Jozsa algorithm.
Review slides 13 to 16. This provides a compact derivation of the final state and the measurement outcomes for the constant and balanced cases, reinforcing the steps we just walked through.
3. Implementation and Quantum Advantage
Now let's turn to the practical implementation using Qiskit. The following guide from IBM Quantum Learning provides the full code to build the Deutsch-Jozsa circuit, including a function to generate a random oracle that is guaranteed to be either constant or balanced.
The Deutsch-Jozsa Algorithm | IBM Quantum Learning
This IBM Quantum Learning module will guide you through the Qiskit implementation of the Deutsch-Jozsa algorithm.
Read the sections 'The Deutsch-Jozsa algorithm' and the subsequent 'Check your understanding' part. Pay close attention to the Python code that generates the oracle and constructs the main algorithm circuit. Try running the code yourself if you have a Qiskit environment set up.
Analyzing the Quantum Advantage
As we've seen, the Deutsch-Jozsa algorithm provides a striking advantage.
| Algorithm Type | Queries Required | Certainty |
|---|---|---|
| Classical Deterministic | 100% | |
| Classical Probabilistic | High | |
| Quantum (Deutsch-Jozsa) | 1 | 100% |
The Deutsch-Jozsa algorithm demonstrates an exponential speedup over any deterministic classical algorithm for this problem. This was a landmark result, proving that quantum computers could, in principle, solve some problems dramatically faster.
However, it's also important to note that the speedup over a probabilistic classical algorithm is less dramatic. A classical computer can solve the problem with high probability using only a few queries. This nuance is important: the power of quantum algorithms often lies in providing deterministic solutions to problems where classical deterministic solutions are intractable.
Conclusion
In this lesson, we have dissected the Deutsch-Jozsa algorithm, one of the foundational examples of quantum computational advantage.
Key Takeaways:
- The algorithm distinguishes between constant and balanced functions, a problem that is hard for deterministic classical computers.
- It achieves its power by combining quantum parallelism (evaluating the function for all inputs at once in a superposition) with quantum interference (using a final layer of Hadamard gates to make the desired information readable).
- The core physical mechanism enabling this is phase kickback, where an operation on a target qubit (the ancilla) induces a phase shift on a control qubit (the input register).
- The result is an exponential speedup over deterministic classical algorithms, achieved with a single query to the oracle.
The Deutsch-Jozsa problem is often described as being "cooked up" specifically to show off a quantum advantage. In our next lesson, we will explore Grover's search algorithm, which addresses a much more practical and general problem: finding a "marked" item in an unstructured database. While its speedup is not exponential, it is provably better than any possible classical algorithm, highlighting another facet of quantum advantage.