The world’s leading publication for data science, AI, and ML professionals.

Visualizing Quantum Computation

From Zero to Understand What the Hack is Happening!

_Photo by Arteum.ro on Unsplash_
_Photo by Arteum.ro on Unsplash_

Hi there! I’m a Ph.D. student at University of Pisa and my research topic is Quantum Computing!

My First Approach to Quantum Computing

I have to be honest, the very first approach to Quantum Computing is struggling, especially for those who come from a CS degree ( just like me 🙂 ). There is a little to which you can hang on based on your classical computer scientist experience.

One problem in quantum computation is for sure a visualization problem.

Can you understand what the following quantum circuit does?

Figure 1. What is this Quantum Cirtcuit? [Image by Author]
Figure 1. What is this Quantum Cirtcuit? [Image by Author]

If you are a beginner, probably no. I had the same problem too!

I do believe that this way of representing the code of a quantum algorithm in quantum circuits misses "a dimension". This kind of representation tricked me. It was implicitly forcing me to reason in a 2D dimension while the reality was in 3D, so I was unable to visualize it properly.

The missing dimension: "superpositions’s dimension".

The power of Quantum Computing resides in the fact that an object is, at the same time, "Blue and Red", "True and False", "0 and 1"… Unfortunately, quantum circuits are not suitable to represent this "missing dimension", making it difficult to understand what is happening.

But don’t worry! In this article, I will show you my approach to quantum computation. I’ll do my best to let you visualize the under-the-hood-quantum-phenomena!


To do this, I’m going to borrow the "Quantum Distance-based Classifier" presented by Schuld, Fingerhuth, and Petruccione in [1].

At the end of this article, you will find my Github repository implementing this Quantum Distance-based Classifier.


⚠️ DISCLAIMER ⚠️

To avoid making this article unfocused and too long, I’m assuming that you are already comfortable with the following concepts:

  • What a Qubit is and how it is mathematically described.
  • How the Hadamard, Controlled-Not, Toffoli, and Controlled-Rotation gates work.
  • What a Quantum Circuit is.
  • What a Measurement is.

Are you ready?


What does the quantum circuit compute?

The following circuit already shown in Figure 1, is the Quantum Distance-based Classifier.

To prevent inconsistencies, I have to notice you that in [1], the circuit starts by superposing both |a> and |m>. In Figure 2, I decided to postpone the application of H on |m> for an illustrative purpose. However, this difference doesn’t affect in any way the computation.

Figure 2. Quantum Distance based Classifier [Image by Author]
Figure 2. Quantum Distance based Classifier [Image by Author]

What does the circuit compute?

Let’s put it in a super-simple way, given:

  • 2 training vectors belonging to 2 different classes
  • 1 test vector (unknown class)
  • The circuit assigns to the test vector the class of the closest training vector (i.e. the training vector that minimizes the Euclidean distance with the test vector)

Just to fix the point: let’s assign a color (=class) to each training vector:

  • training_0 = Blue
  • training_1 = Red

According to the distance-based classifier and the example in Figure 3, _what would be the color (=class) of the test vector?_

Since the training_1 vector is the closest one to the test vector, the test vector color is RED!

Figure 3. Distance-based classification [Image by Author]
Figure 3. Distance-based classification [Image by Author]

I hope that the problem statement is clear 🙂


GOING QUANTUM: Let’s break down the quantum-circuit!

Circuits are often organized in 3 macro-phases (Figure 4):

  • Quantum Encoding: Encodes classical data into a quantum ones.
  • Computation: Computes the solution.
  • Measurement: Decodes the quantum-representation solution into classical data.
Figure 4. The 3 Phases [Image by Author]
Figure 4. The 3 Phases [Image by Author]

Since we are looking for the "superpositions’s dimension" and since the Quantum Encoding phase is the most interesting from this point of view, I will focus on this one!


Quantum Encoding Phase

We are at a very low abstraction level, there is no built-in data-structure. It is our responsibility to map data in "memory" and this is what the circuit does in this phase.

⚠️ ACHTUNG : there is no memory in quantum computers. They are just processors. However, the word "memory" evokes a good intuition about what we are doing in the circuit (i.e. assigning to a certain combination of qubits a certain encoded value).

In particular, in this phase, we need to:

  1. Chose an encoder.
  2. Map the encoded data into "memory".

In the Quantum Distance-based Classifier we are observing, the chosen encoder is the amplitude encoding. Let’s understand it!

Amplitude Encoding

It is an interesting type of encoding that can encode continuous values in the amplitudes.

How?

I recall you how a qubit is defined:

Qubit definition [Image by Author]
Qubit definition [Image by Author]

Because the absolute squares of the amplitudes equate to probabilities, it follows that:

[Image by Author]
[Image by Author]

Have you noticed? This condition looks like… the circumference equation!

[Image by Author]
[Image by Author]
[Image by Author]
[Image by Author]

So?

You can also define a qubit as follows:

[Image by Author]
[Image by Author]

Given a certain angle α, you can map a qubit in a circumference!

Let’s put it all together.

Given the entry [0.789, 0.615] ** of a standardized and normalized dataset, we have to find an angle** α such that:

[Image by Author]
[Image by Author]
[Image by Author]
[Image by Author]

How can I figure out this angle α to perform the correct rotation?

Now we have to plug this angle α in our controlled-rotation gate and it will rotate the qubit as previously described!

_A keen reader would notice that the arctan(sinα/_cosα) _has been multiplied by a factor of 2. The "why" is beyond the scope of this article, however, it deals with the fact that a qubit is naturally plotted over a sphere (_Bloch sphere), not on a circumference. Anyway, in my opinion, it is easier to grasp the idea of amplitude encoding by means of a circumference than the Bloch sphere 🙂

That’s amplitude encoding 🙂

BACK TO THE CIRCUIT

Back to the circuit in Figure 5, we are going to amplitude-encode the following vectors:

  • 2 training vectors (_Training_0, Training1)
  • 1 test vector.
Figure 5. Quantum Encoding Phase [Image by Author]
Figure 5. Quantum Encoding Phase [Image by Author]

For simplicity, we will use the same entries used in [1].

These values belong to the Iris dataset; they are already standardized and normalized. In case you want to reproduce the experiment, each entry is annotated along with the sample number ( hope it helps 🙂 ).

  • Test: [-0.549, 0.836] → α = arctan2(0.836,-0.549)*2 = 4.304 (Iris sample 28)
  • Training_0: [0,1] Just rotate to |1> through a Toffoli gate. (Iris sample 33)
  • Training_1: [0.789, 0.615] → α = arctan2(0.615,0.789)*2 = 1.325 (Iris sample 85)

Given the previous alphas, we can plot the 3 vectors (data) over a circumference (Figure 6).

Figure 6. Plotted data over a circumference [Image by Author]
Figure 6. Plotted data over a circumference [Image by Author]

THE MISSING DIMENSION: Superpositions

This is the part that I want to stress more since you will be able to understand what the gates highlighted in green do (Figure 7) and thus visualizing the missing dimension.

Figure 7. Quantum Encoding Phase [Image by Author]
Figure 7. Quantum Encoding Phase [Image by Author]

One question you are maybe wondering, which is strictly related to superpositions, is:

How can 3 different vectors be loaded on the same qubit |i> of the circuit?

To answer this question, and thus finding our "missing dimension", let’s analyze the Quantum Encoding phase gate-by-gate.


Quantum Circuit GATE-by-GATE

HADAMARD on |a>

Figure 8. Circuit Step 1 [Image by Author]
Figure 8. Circuit Step 1 [Image by Author]

The first Hadamard gate splits the computation into two branches which can be represented through a tree (Figure 9).

Figure 9. Tree Step 1 [Image by Author]
Figure 9. Tree Step 1 [Image by Author]

How to interpret this tree? It’s simple!

In the middle of the tree we indicate:

  • The gate applied (here the Hadamard gate)
  • The qubit affected by the gate

The two branches indicate how the qubit ( |a>) has been affected by the gate (H). Here, the qubit has been put in equal superposition.

I know… I know… In the tree there aren’t amplitudes but it’s not important for our visual experiment. What is important is not to "overload" the picture.

The very first take away message is that, by now, two branches exist simultaneously and evolve "independently" one to another!


UPLOADING TEST VECTOR [-0.549, 0.836] on |i>

Figure 10. Circuit Step 2 [Image by Author]
Figure 10. Circuit Step 2 [Image by Author]

As we already pointed out, this rotation maps classical data into a quantum representation through amplitude encoding.

The mapped entry is the test vector: [-0.549, 0.836] → alpha = 4.304

We can observe that, since it is a controlled-rotation whose control qubit is |a> (Figure 11), the operator affects only the right branch in which |a> is |1>.

Figure 11. Tree Step 2 [Image by Author]
Figure 11. Tree Step 2 [Image by Author]

Attention: the syntax "R(4.304)|0>" is a compact way to express that |0> has been rotated by the controlled-rotation.


PAULI X-GATE on |a>

Figure 12. Circuit Step 3 [Image by Author]
Figure 12. Circuit Step 3 [Image by Author]

What is the purpose of this X-gate on |a>?

The point here is that you don’t want to "overwrite" the test vector just uploaded in the right branch (R(4.303)|0>). Thus, we have to move the next computation on the left branch, leaving unaltered the right branch.

To leave unaltered the right branch, since _Training0 and _Training1 are doubly controlled and since the first control qubit is |a> and the X-gate have just flipped |a> to |0>, then, by now, any doubly controlled rotation that involves |a> as control qubit and |i> as target qubit on the right branch, will not affect |i> (Figure 13).

Figure 13. Tree Step 3 [Image by Author]
Figure 13. Tree Step 3 [Image by Author]

This pattern avoids the overwriting of the test vector! Since this is a key passage, let me know whether it is clear or not 🙂


HADAMARD on |m>

Figure 14. Circuit Step 4 [Image by Author]
Figure 14. Circuit Step 4 [Image by Author]

Again, we are splitting the computation by applying a Hadamard. This time the qubit affected by the Hadamard is |m>.

Figure 15. Tree Step 4 [Image by Author]
Figure 15. Tree Step 4 [Image by Author]

By now, we have four branches that exist simultaneously and evolve independently from one to another! (Figure 15)


UPLOADING TRAINING_0 [0,1] on |i>

Figure 16. Circuit Step 5 [Image by Author]
Figure 16. Circuit Step 5 [Image by Author]

Toffoli gate is affecting only the second branch from the left. Also, as we can see in Figure 17, the value of |i> in the right branch has not been affected, indeed, it still has the Test vector (R(4.304)|0>) correctly encoded in it.

Figure 17. Tree Step 5 [Image by Author]
Figure 17. Tree Step 5 [Image by Author]

Question: Why?

Answer: Because the Toffoli is doubled-controlled and, since |a> on the right branch is |0>, then the Toffoli is not applied. Moreover, the Toffoli doesn’t affect either the first branch because even if |a> is |1>, the second control qubit|m> is |0>


PAULI X-GATE on |m>

Figure 18. Circuit Step 6 [Image by Author]
Figure 18. Circuit Step 6 [Image by Author]

The application of the X-gate on |m> follows the same pattern logic we applied on |a> in the third step. Since we do not want to overwrite the [0,1] entry uploaded in the previous step, we flip |m> so that in the second branch it becomes|0> and in the first one it is |1>. In this way, only the first branch will be affected by the next doubly-controlled operator (Figure 19).

Figure 19. Tree Step 6 [Image by Author]
Figure 19. Tree Step 6 [Image by Author]

UPLOADING TRAINING VECTOR [0.789, 0.615] on |i>

Figure 20. Circuit Step 7 [Image by Author]
Figure 20. Circuit Step 7 [Image by Author]

For the reasons discussed before, the 2nd, 3rd, 4th branches are not affected by the doubly controlled rotation, while the only branch affected is the 1st one since its control qubits are both |1> in this specific subbranch (Figure 21).

Figure 21. Tree Step 7 [Image by Author]
Figure 21. Tree Step 7 [Image by Author]

MAP THE CLASS TO EACH TRAINING VECTOR

Figure 22. Circuit Step 8 [Image by Author]
Figure 22. Circuit Step 8 [Image by Author]

Until now, we did not map explicitly the class to each Training vectors. Let’s do it!

In particular, we assign classes as follows:

  • Training_0 belongs to class 0
  • Training _1 belongs to class 1

The qubit that encodes the class of a Training vector is |c>

In the case of Training_0, we do not need to apply any gate to |c> because in that branch (the second one), |c> is already set to |0>.

Figure 23. Tree Step 8 [Image by Author]
Figure 23. Tree Step 8 [Image by Author]

The situations about Training_1 is different. The branch where Training_1 is loaded is the first one. Since we want to flip to |1> only the class qubit |c> of this branch, we should apply a Toffoli Gate whose control qubits are |a> and |m> and whose target qubit is |c>. That would work because in this branch, |a> = |1> and |m> = |1>.

But probably you noticed that, in this last step of the Quantum Encoding Phase, there is a CNOT gate (Figure 23) and not a Toffoli! So why?

Since the CNOT has as control qubit |m> and as target qubit |c>, it means that the CNOT will affect ALL the branches where|m> = |1> (i.e. the CNOT will affect the 1st branch and the 3rd one). The final qubits setup is shown in Figure 24.

Figure 24. Final Qubits Setup [Image by Author]
Figure 24. Final Qubits Setup [Image by Author]

To get the intuition about why the circuit employed a CNOT instead of a Toffoli, notice that we have 2 copies of the same Test vector (Branch 3 and Branch 4). The difference is that one copy has been assigned to class |1> (Branch 3) while the other one to class |0> (Branch 4).


"MEMORY"

But wait! Did you remember that earlier I use the improper term "memory"?

Look at the combinations of qubits |a> and |m> in each column of Figure 24.

In some sense, |a> and |m> act as indexes where you store a specific vector whose value is encoded in qubit |i> and its class in qubit |c>

In particular:

  • |00>: Branch 4 → Test vector assigned to class |0>
  • |01>: Branch 3 → Test vector assigned to class |1>
  • |10>: Branch 2 → Training_1 vector assigned to class |0>
  • |11>: Branch 1 →Training_0 vector assigned to class |1>

Now we can back to the reasons why there are two copies of the test vector 🙂


Quantum Computation – the H gate

Given this setup, you are able to "clash"(=interfere) Branch 3 on Branch 1 and Branch 4 on Branch 2. In other words, we are able to compare in one shot the same test vector (labeled with a different class) against two different Training vectors.

To make them "clash", the circuit applies the Hadamard of the Computation Phase on |a> (Figure 25).

Figure 25. Circuit Step 9 [Image by Author]
Figure 25. Circuit Step 9 [Image by Author]

A visual intuition about what happens in the Computation Phase by applying the Hadamard Gate, follows (Figure 26).

Figure 26. Hadamard Effect in Computing Phase [Gif by Author]
Figure 26. Hadamard Effect in Computing Phase [Gif by Author]

In essence, thanks to the Hadamard gate, the circuit computes the distance between the two training vectors and the test vector, which is the aim of this circuit. After the "clash", the circuit measures the results and outputs the class to which the test vector belongs.

The measurement phase is out of the scope of this visual experiment, however, you can find on my Github, the implementation of the Quantum Distance-based Classifier and also some handwritten PDF notes where the quantum encoding phase is evolved mathematically.

Brotherhood94/quantum_distance_based_classifier

We finally found the missing dimension!

We are at the end of this journey in the Quantum World from a CS perspective!

Comparing the Circuit Visualization (Figure 27) and the Tree Visualization (Figure 28), you can understand what I intend for "missing dimension". In fact, the Circuit Visualization doesn’t explicitly show how a qubit is affected by a gate in terms of "branches". Here is where the Tree Visualization comes to the rescue! The branches of the Tree-Visualization represents, in a graphical way, the superpositions and what the circuit is "storing".

Figure 27. The Circuit Visualization [Image by Author]
Figure 27. The Circuit Visualization [Image by Author]
Figure 28. The Tree Visualization [Image by Author]
Figure 28. The Tree Visualization [Image by Author]

This Tree-Visualization works well for circuits like the one presented, while for others is less effective. However, I hope that this visualization helped you to understand better the concept of "superposition" at least as much as it helped me!

For the sake of clarity, I omitted some details in this article that were not important to find the "missing dimension". However, If you need clarification, just leave a comment 🙂


🚀 Did you find useful this kind of visual representation?

If yes, feel free to leave a clap and let’s keep in ** touch on LinkedIn and Twitte**r!

I will super-duper appreciate it! 🙂


See yaaaaa! ✌🏼

References

[1] Schuld, M., Fingerhuth, M., & Petruccione, F. (2017). Implementing a distance-based classifier with a quantum interference circuit. EPL (Europhysics Letters), 119(6), 60002.


Related Articles