- Published on
Step by step guide to Deutsch's Algorithm
- Authors
- Name
- Jonas Vetterle
- @jvetterle
We are still in the early days of quantum computing, and we are yet to see real-world applications of it with real impact. But we are already seeing the first signs of quantum advantage, where quantum computers can outperform classical computers on certain tasks.
In this post, we will demonstrate quantum advantage by walking through Deutsch's algorithm step by step.
Note that this is a simple, contrived example without any real-word applications. However, it is a good starting point to understand the potential of quantum computing.
What is Deutsch's Algorithm?
Deutsch's algorithm (invented by David Deutsch in 1985) is an example of a quantum algorithm that can solve a problem faster, by some measure, than any classical algorithm.
Faster in this context means that the algorithm solves a very specific problem in one go (if we had a noise-free quantum computer). This is in contrast to a classical algorithm that we would have to run twice to solve the same problem.
The Problem Deutsch's Algorithm Solves
Deutsch's algorithm solves a simple problem: given a function that takes a single bit (i.e. 0 or 1) as input and returns a single bit (i.e. 0 or 1) as output, determine if the function is constant or balanced.
- A constant function is one where the output is always 0 or always 1, regardless of the input.
- A balanced function is one where the output is 0 for one input and 1 for the other input.
In other words, we are given some function , which is unknown to us, but which we can call and observe the output, and the problem is to determine if is constant or balanced.
As the function takes a single bit as input, there are only four possible functions.
- The first 2 possible functions are constant:
- and is constant (always 0)
- and is constant (always 1)
- The other 2 possible functions are balanced:
- and is balanced
- and is balanced
The problem is to determine if the function we're given is constant or balanced.
The Classical Solution involves 2 queries
It's easy to see that if we approach this problem classically, we would have to call the function twice to determine if it's constant or balanced.
For example, we can call the function with input 0 and learn that the output is , we can tell that we are either in case 1.2 (constant) or case 2.2 (balanced).
But we would still have to call the function a second time with input 1 to distinguish between the two cases.
The Quantum Solution involves just 1 query
The Quantum solution (Deutsch's algorithm) can solve this problem in one go. As you might expect, the solution is a lot more involved than the classical one. But in a nutshell we will rely on the following principles:
- Superposition: We're putting a qubit in superposition, and then apply on it. This allows us to evaluate for both inputs at the same time.
- Phase Kickback: The effect where the phase of a qubit is "kicked back" onto another qubit during a controlled quantum operation like the CNOT gate.
High level overview of Deutsch's Algorithm
On a high level the algorithm works as follows:
- Prepare the qubits and in the state . We call the joint state .
- Apply a Hadamard gate to both qubits to put them in superposition. We call this state
- Apply the gate (there are 4 choices as we know): . More on the gate below.
- Apply a Hadamard gate to qubit . We call this state
- Measure qubit .
The result will depend on the function we chose: If the function is constant, the measurement will always be 0. If the function is balanced, the measurement will be 1. Crucially, we only have to call the function once.
Some Math Preliminaries
The XOR operator/ addition modulo 2
Before we continue, let's introduce a little bit of notation. The XOR operator, also known as exclusive OR, or addition modulo 2, is a binary operation that takes two bits and returns 1 if the bits are different, and 0 if they are the same. We sometimes use the symbol to denote the XOR operator.
For example: if we write , we mean the XOR of the binary numbers and , which is 110 (6 in decimal). So .
Given this, the classical solution to the problem is to call the function twice and apply the XOR operator to the results.
In other words:
- if we know that the function is constant
- if we know that the function is balanced
Introducing a controlled quantum operation
The algorithm involves 2 qubits: one which will we'll call , the qubit that we'll evaluate on, and another one which we will call that will be used for the phase kickback mentioned above.
First, there is one important quantum gate that we have to define: the gate . This is a unitary, i.e. reversible, 2-qubit gate that applies our function to the qubits and .
What does is that it maps the state to . Remember that is the XOR (or addition modulo 2) operator and is one of the 4 cases defined above.
As you can see, is a controlled operation in the sense that it applies alters the state of based on the value of . That is, the state of controls the state of .
Let's see how acts in, for example, the case of a constant function :
Nothing happens.
Now let's try the balanced function :
You get the idea.
Walking through Deutsch's Algorithm step by step
Ok let's get into the nitty gritty. We'll use to keep track of qubit and for qubit . We start with
After applying to the Hadamard gates, the state becomes:
Now we apply the gate , which as we know applies this transformation:
We can simplify this, since , where is either or , so it's 0 or 1. So, rewriting we get:
since .
We just observed Phase Kickback in action: Notice how the application of left in state , but kicked back the phase factor in front of .
Now we can pull out
where we used the fact that .
This is because and are either 0 or 1, so the difference between them is either 0, 1 or -1. If the difference is 0, then we get . And if the difference is 1 or -1 we get .
We do this because now we can see more clearly the effect of phase kickback: it introduces a relative phase of in qubit .
Let's look at the term for a moment. Since can be either 0 or 1, this is either if or if .
In other words we have the following 2 cases:
Importantly, notice that the state of qubit is now either or , depending on whether the function is constant or balanced!
This is all due to the phase kickback we observed earlier. To reiterate: the application of kicked back the phase factor in front of . This introduced a relative phase of in qubit .
It also introduced a global phase of in front of the whole state. But we don't care about this because we can't measure it.
It's worth noting that there are 2 necessary conditions for this to work:
- we had to apply the Hadamard gate to qubit at the beginning. Otherwise the gate wouldn't have any effect on qubit . As you remember from the formula , has no effect on qubit in the computational basis.
- we had to start with the state for qubit , otherwise the phase kickback on qubit wouldn't have occurred.
The final step is to apply a Hadamard gate to qubit :
Now we can measure qubit and we will get 0 if the function is constant and 1 if the function is balanced.
Conclusion
Deutsch's algorithm is a simple example of a quantum algorithm that can solve a problem faster than any classical algorithm. It's a significant milestone in the history of quantum computing, as it shows that quantum computers can outperform classical computers on certain tasks.
The speedup in this case modest, it's a linear speed up, but it's a proof of concept and laid the groundwork for more advanced quantum algorithms, which we'll explore in future posts.
It also provides a hint as to how to structure quantum algorithms in general: modulating the phase of qubits to create interference patterns that make certain measurements more likely than others. In the case of Deutsch's algorithm, making the correct answer 100% likely.
If you want to put theory into practice, check out Running Deutsch's Algorithm on a Real Quantum Computer.