Explaining The Math Of A Quantum Circuit For The Non-Mathematician
Do you struggle with understanding quantum circuits because of all the math?
Do you want to get started with Quantum Machine Learning? Have a look at Hands-On Quantum Machine Learning With Python.
Yet, if you’re neither a mathematician nor a physicist, studying quantum algorithms can be hard sometimes. It is challenging for it uses symbols extensively. The problem with symbols is that you can’t google them easily. Consider the following circuit, for instance.
It represents a structure you’ll frequently encounter when studying quantum algorithms. But if you have no idea what the terms mean, how do you find out?
Consider |0⟩ and |1⟩, for instance. Just for fun: google them.
How about 𝐻⊗𝑛? Would you even know how to enter this term in Google? (not even the Medium text editor supports writing this term correctly.) I mean, if all you got is an image, not even copying text from it is easy.
These symbols are as good as hieroglyphs if you’re not a mathematician.
Certainly, you don’t need to be a genius to understand a quantum circuit. You’ll be fine as long as you’re a bit smarter than Cletus.
So, let’s shed some light on the quantum circuit. Quantum computing does not use mathematical symbols to describe it by accident. A quantum circuit is a mathematical construct. It is an equation. And, without a doubt, math is a concise and precise language. With math, you can describe algorithms very succinctly.
Unfortunately, for most of us, math is a second language — at best.
Let’s start at the very left. There, we find the input to our circuit. |0⟩ and |1⟩ denote the states the qubits have initially. When we measure a qubit in state |0⟩, it is 0. And, when we measure a qubit in state |1⟩, it is 1. This is similar to a classical bit.
Then, why don’t we just write 0 and 1? Why do we put them in such a weird frame?
The reason is that a qubit is not just 0 or 1. It is in a linear combination of 0 and 1 unless you measure it. Once you measure it, it instantly becomes 0 or 1. And, we describe this linear relation using vectors. So, |0⟩ and |1⟩ denote two vectors. They are the standard basis vectors we use in quantum computing written in the Dirac notation. Vectors are geometric objects with a length and a direction. The Dirac notation (also known as bra-ket) is popular in the field of quantum mechanics. But it is nothing fancy.
We can simply say that
I have to admit. These two equations just translate a vector from one notation into another. So, it still uses math and symbols. But this is a notation most people learn in high school. And, I dropped enough keywords for you to google if you’re unfamiliar with vectors at all.
But before you leave for Google now, why don’t we look at these two vectors graphically?
Each number inside the vector represents a dimension. So, our vectors are two-dimensional.
In this representation, both dimensions reside at the vertical axis but in opposite directions. Thus, the top and the bottom of the system correspond to the standard basis vectors |0⟩ and |1⟩, respectively.
In the figure, there is another vector called “psi” — |𝜓⟩.
|𝜓⟩ stands for an arbitrary qubit state. As we mentioned above, a qubit is in a linear relationship of 0 and 1. Therefore,
𝛼 and 𝛽 are the distances of the vector to the standard basis vectors |0⟩ and |1⟩. If we square them, we get the probability of measuring the qubit as 0 (=𝛼²) or as 1 (=𝛽²).
As an example, if we say 𝛼=1 and 𝛽=0, then |𝜓⟩=1⋅|0⟩+0⋅|1⟩=|0⟩. For 𝛼=1, the probability of measuring the qubit as 0 is 1²=1=100%.
This is a lot of math just to say that we measure one qubit as 0 and another as 1, isn’t it?
But, we’re not yet done with the initialization. You may have noticed the small superscripted 𝑛 next to |0⟩^𝑛. Usually, this indicates an exponent. Here, it says that we don’t have a single qubit in state |0⟩ but 𝑛 qubits in this state. We could also write it as |00⋯0⟩ with 𝑛 0s.
So, our circuit contains n qubits initialized in a state we would measure as 0, and one qubit we would measure as 1.
Next to the initialization of the qubits, we see the terms 𝐻⊗𝑛 and 𝐻. Let’s start with the latter. In quantum computing, capital letters usually represent matrices. The matrix 𝐻 stands for the Hadamard matrix that is defined as
The important thing to know is what happens if you multiply a matrix with a vector (given the number of columns of the matrix equals the number of rows of the vector). Then, the result is another vector.
For instance, if we multiply the Hadamard matrix with the state vector |0⟩ or |1⟩, then we get another vector.
Both resulting vectors have values for 𝛼 and 𝛽 that are equal if you square them. The only difference is the sign of 𝛽 when multiplying 𝐻 with |1⟩. Regularly, the sign is also the short form of this state.
In quantum computing, |+⟩ is short for
and |−⟩ is short for
When you square any of these values to calculate the qubit's probability as 0, or 1 respectively, you’ll get 1/2.
So, the 𝐻 matrix effectively transformed the state of the qubit. We don’t measure the qubit as 0 or 1 with certainty anymore. But when we measure it, it may be 0 or 1—each with the probability of 50%.
Maybe, you’re already suspecting what 𝐻⊗𝑛 means. |0⟩^𝑛 represents 𝑛 qubits in state |0⟩. If we regard these qubits as a single vector, then this vector doesn’t have only two dimensions but 2⋅𝑛 dimensions. It has two dimensions for each qubit.
We can only multiply a matrix with a vector if they have the same number of dimensions. This is what 𝐻⊗𝑛 takes care of.
Let’s look at 𝐻⊗2=𝐻⊗𝐻. This way of multiplying two matrices is called the tensor product. We can write it as
It creates a larger matrix. And, more importantly, it works recursively.
I spare you expanding 𝐻⊗2 here. In general, it is
This formula is known as the Kronecker product. It is the mathematical way of saying:
Apply the same transformation matrix (here 𝐻) to each of the 𝑛 qubits.
The next term of the circuit has the name 𝑈𝑓. It represents a unitary matrix. A unitary matrix is its own conjugate transpose. That sounds complex. But it is, simply, the matrix flipped over its diagonal. A matrix that is its own conjugate transpose has interesting characteristics. For instance, when you multiply such a matrix by itself, the result is the identity matrix. 𝑈 is a matrix, and 𝑈† is its conjugate transpose. Then, 𝑈†𝑈=𝑈𝑈†=𝐼.
We learned that matrices transform the qubit state. But, the identity matrix does not change it. So, we can say 𝐼|𝜓⟩=|𝜓⟩.
So, when you multiply your qubit state by the same unitary twice, it results in the original qubit state again. Or, put differently, the transformation reverts itself.
Another important characteristic of a unitary matrix is that it preserves the measurement probabilities. And, this is the important point in the circuit at hand.
Remember, all qubits are in a state with equal probabilities of 0 and 1 before feeding them into the 𝑈𝑓 transformation. So, if a unitary matrix doesn’t change the probabilities, they are still equal afterward.
Why do we even apply this matrix if it doesn’t change the probabilities?
The difference between |+⟩ and |−⟩ is the answer. As we said above, qubits in both states have the same measurement probabilities.
But the Hadamard gate that we already know has an interesting effect. It does not only turn a qubit from |0⟩ to |+⟩ and |1⟩ into |−⟩, but it also turns |+⟩ back to |0⟩ and |−⟩ back to |1⟩. It reverts itself.
And this is what the final 𝐻⊗𝑛 does. The point is that 𝑈𝑓 is a placeholder for a certain problem we aim to solve.
It only flips some of the qubits from |+⟩ to |−⟩. It flips those that must be 1 in the solution of the problem. And, it leaves those untouched in |+⟩ that need to be 0 in the solution.
In this post, we shed some light on the math underlying a quantum circuit. We learned about the math of a quantum circuit and what it means.
It turns out that there is a lot of meaning in small mathematical symbols. If you know them all, math is a concise way to describe a circuit precisely. But if you don’t know all these things, math alone is insufficient as an explanation. It does not emphasize the important things, and it doesn’t provide clues (like keywords) you could use to do your own research.
The circuit at hand represents the structure of famous quantum algorithms, such as the Deutsch-Jozsa algorithm and the Bernstein-Vazirani algorithm.
The difference between both algorithms lies in the implementation of 𝑈𝑓.
The 𝑈𝑓 matrix flips those qubits from |+⟩ to |−⟩ we want to measure as 1. And these form the solution to the problem the algorithm aims to solve.
In this post, you can learn more about the details of Uf and the Deutsch-Jozsa algorithm.
The more you learn about quantum computing, the more you will understand math, too. But you will also see that quantum computing is not all math. Then, you’ll wonder why nobody bothered to explain things in a non-mathematical manner.
Math should be a complement to the explanation. Then, it is easier for the reader to understand the formulae. If she already understands the concept when seeing a formula, chances are much better all these symbols make any sense.