A Quantum Experience

20 Feb 2018 | | theory, quantum, q, informative, ibm

Much more than a post

What is the quantum theory? As said by quantumexperience official site by IBM, it’s an elegant mathematical theory able to explain the counterintuitive behavior of subatomic particles, most notably the phenomenon of entanglement. In the late twentieth century it was discovered that quantum theory applies not only to atoms and molecules, but to bits and logic operations in a computer. This realization has been bringing about a revolution in the science and technology of information processing: I decided to write some notes to better explain, from a physics-agnostic computer scientist’s point of view XD, what I understood - and it is certainly wrong - about Q until now and why I think it’s an amazing field for computer science. For skilled guys, here latex source (and here pdf pre-compiled version) that collect my personal notes about IBM Q platform, in general the quantum-computing world. I was also invited in Verona by the Quantum Research Group of the Department of Computer Science - why? don’t know, maybe the coolest guys were sick 😂 - to talk about the platform and we had a really interesting brain-storming conversation about a quantum version of the Tris game I am working on 😎


I should start by saying that my education background is in Computer Science. While I’ve read a couple of books on quantum mechanics, I don’t have formal training as a physicist: that didn’t deter me from learning the generalities about quantum mechanics and play with quantum computers. In this post, I reported an extract of my notes available here

These notes collected everything that was useful and necessary for me to fully understand the basic concepts related to this world, with particular attention to quantum computation provided by the IBM Q Platform. They represents essentialy a work of enrichment of the material available online, with the intention of making more accessible to anyone who wants to deal with the quantum world. To help me understand more in depth the concepts introduced, I wrote some more recalls of maths using as main source the notes in this book1.

Physics and Computation

A calculation process is essentially a physical process that is performed on a machine whose operation obeys certain physical laws. The classical theory of computation is based on an abstract model of universal machine, the Universal Turing Machine, that works according to a set of rules and principles enunciated in 1936 by Alan Turing and subsequently elaborated by John Von Neumann in the 1940s. These principles have remained essentially unchanged since then, despite the enormous technological advances that today allow to produce far more powerful devices than those that could be achieved in the first half of the twentieth century. The tacit assumption underlying these principles is that a Turing machine idealizes a mechanical computational device - with a potentially infinite memory - that obeys the laws of classical physics.

Usually the concept of difficulty is quite subjective, but for a computer scientist this word has a different meaning: the classical information theory divides the problems that can be solved by a computer according to their complexity, i.e. the time taken by the computer to solve them according to the length of the input. Apparently, there are problems that are unsolvable, even from a computer when the dimensions of the initial parameters become relevant. For instance, it may be impossible to find the solution of a sudoku, solve the enigma of the traveling salesman or break down a number in its prime factors. However, a quantum computer has the ability to perform multiple operations together, i.e. by quantum parallelize tasks. Thus, in the XX century an unlikely alliance between physicists and computer scientist was born with the common goal of developing a quantum machine: computer scientists wanted to amply the class of problem solvable by machines and to overcome the limit of the classic Turing’s computation theory, physicists wanted to understand a little more the mysteries of quantum mechanics. As a result of this cooperation, a series of quantum algorithms have been structured in such a way to use a quantum phenomena such as the principle of superposition or entanglement: only by exploiting these properties properly, it’s possible to tap into all the potential of quantum computing. What makes the quantum computer so interesting?

Quantum computation

Quantum computation is born as an alternative paradigm based on the principles of quantum mechanics. The idea of creating a model of computation as an isolated quantum system began to appear at the beginning of the eighties, when P. Benioff, starting from considerations previously elaborated by C. Bennet, defined the reversible Turing Machine: a computation can always be executed in such a way as to return to the initial state by retracing the various steps of computation backwards.

Subsequently R. Feynman showed that no classical Turing Machine could simulate certain physical phenomena without incurring an exponential slowing of its performances. In contrast, a “universal quantum simulator" could have performed the simulation more efficiently.

In 1985 D. Deutsch formalized these ideas in his Universal Quantum Turing Machine, which in quantum computational theory represents exactly what the Universal Turing Machine represents for classical computability and led to the modern conception of quantum computation.

Naturally, the effects of the introduction of the new calculation model were also felt in the field of computational complexity (as envisaged by Feynman), causing the change of the notion of “treatability". In fact, in 1994 P. Shor shows that the problem of factorization of prime numbers - classically considered intractable - can be solved efficiently, i.e. in polynomial time - with a quantum algorithm. These considerations, combined with the technological ones mentioned above, have led to the emergence of the research field known today as information theory and quantum computation. In particular, the three fundamental, and not very intuitive phenomena of the quantum theory, are the principle of superposition of states, the principle of measurement and the phenomenon of entanglement. To introduce them, it is necessary to introduce some concept related to the quantum world and after that some recall of mathematical algebra.


I don’t want you to provide basic notions to understand quantum mechanics: you can have a look at my repo tex here and some exercises here. If you want to find more without dive into maths details you can refers to guides: a beginner version is available here. The IBM Q team makes available a real quantum computer I found really useful to understand the concepts and exercises proposed in university notes. The last section of the will contain a collection of exercises - with respective answers - exposed in university course, collected from exams draft available online and provided by several universities, proposed by the IBM Q in its tutorial cycle and some other personal circuits I coded to understand better the gates available.

IBM quantum composer

The quantum composer is the official IBM graphical user interface for programming a quantum processor. The composer is a tool to construct quantum circuits using a library of well-defined gates and measurements. You can create your own account in IBM using Github sign up starting from quantum experience site.

When you first click on the “Composer" tab above, you will have a choice between running a real quantum processor or a custom quantum processor. In the custom processor, gates can be placed anywhere, whereas in the real processor, the topology is set by the physical device running in our lab (note that this restricts the usability of some of the two-qubit gates). Once you are in the “Composer" tab, you can start making your very own quantum circuits. The IBM quantum composer is shown in

With the composer, you can create a quantum score, which is analogous to a musical score in several respects. Time progresses from left to right. Each line represents a qubit (as well as what happens to that qubit over time). Each qubit has a different frequency, like a different musical note. The quantum composer’s library (located to the right of the qubit stave) contains many different classes of gates: single-qubit gates, such as the yellow idle operation; the green class of Pauli operators, which represent bit-flips (X, equivalent to a classical NOT); phase-flips (Z); and a combined bit-flip and phase-flip (Y). There are others gates available that haven’t been introduced yet. In general, quantum gates are represented by square boxes that play a frequency for different durations, amplitudes, and phases. Gates on just one line are called single-qubit gates. Before going on with esperiments, let’s introduce these kind of gates.

IBM Q - First Experiment

When you begin an experiment, you’ll be prompted to give it a name, so that you can recognize it later. You will also see two choices: real quantum processor, or custom topology. In both cases, you create your score by dragging gates onto the stave, adding a measurement, and then hitting “run" for the score to execute. If you select “Custom Topology" your only option is to run your score in simulation. This is because the custom processor permits all-to-all connectivity; the real device, in contrast, is limited by physical connectivity. When you select custom topology, a dialogue box will ask you to select the number of qubits and classical bits assigned to different registers. IBM have set the maximum number of qubits to 20.

The operation consists in the measurement of a qubit. If you measure, for instance, you know the result is a classic bit (indicated with a double line) that will be or with probability respectively an .

The execution of your circuit happens immediately (unless the number of qubits is large) and the output can then be viewed in the results. You can try the “single qubit measurement" show in

If you have chosen a real quantum processor, the composer will look like the one shown in

In IBM quantum experience, the results from launching your quantum scores can be visualized in two different ways: a standard histogram/bar graph, and as a quantum sphere, or QSphere - the Block Sphere introduced before. The QSphere represents quantum circuit measurement outcomes in a visually striking and information-dense graphic.

After performing a quantum measurement, a qubit’s information becomes a classical bit, and in our system (as is standard) the measurements are performed in the computational basis. For each qubit the measurement either takes the value 0 if the qubit is measured in state and value if the qubit is measured in state .

In a given run of a quantum circuit with measurements, the result will be one of the possible -bit binary strings. If the experiment is run a second time, even if the measurement is perfect and has no error, the outcome may be different due to the fundamental randomness of quantum physics. The results of a quantum circuit executed many different times can be represented as a distribution over the full possible outcomes. It is not scalable to represent all possible outcomes; therefore, we keep only those outcomes that happen in a given experiment and represent them in two different ways: as bars or as a quantum sphere.

For a single qubit there are two outcomes, and the sphere has only two levels; for two qubits, it has three sections with the middle section separated into two parts; for three qubits, it has four sections with the middle two being broken into three sections, and so on, following Pascal’s triangle. The usefulness of the Block Sphere representation is for distinguishing classical states from entangled states. A computational basis state will have a single line pointing in one direction. Under the assumption the state is pure, a superposition of two basis states will have two lines pointing in two directions of half weight. If these directions are on opposite sides of the QSphere we have a state that is maximally entangled (for ) in the computation bases. Finally if there are faint lines in every direction we have made a uniform superposition state.

IBM Q - Testing the gates

The configuration to test the effect of X gate is really simple: first, drag and drop an X gate on the first qubit (first line) - time is discrete, divided in several dots. The initial state of each qubit is

In general, an operation on a single qubit can be specified by a matrix. However, not all arrays define “legitimate" operations on qubits. We recall that the normalization condition requires that in any quantum state The same condition must also apply to the state that is obtained after carrying out the operation. The property of matrices that guarantees the transformation of a unit vector into a vector that is still unitary is unity.

You can try also the other Pauli operators using Y and Z gates. In the next few paragraphs, something more will be said about these two gates.

IBM Q - Create a superposition

On the contrary to the classic case in which we can define a single non-trivial operation on a single bit, in the quantum case there are many non-trivial operations on a single qubit. Besides the NOT two important operations that we will use later are the port: which only affects the component by changing the sign, and the Hadamard gate: The latter operation is one of the most useful and is very often used in the definition of quantum circuits. Its effect is that of transforming a base state into an overlap that results, after a measurement in the computational basis, to be or with equal probability. For example, by applying to you get: that is the state ![Display of Hadamard port applied to input : the output is

The effect of can therefore be seen as an half-executed NOT, so that the resulting state is neither nor , but a coherent overlap of the two base states. For this reason is often called the square root of NOT. Note that this expression has only a physical meaning! From an algebraic point of view, is not the matrix. With a simple calculation one can in fact verify that is the identity and therefore applying twice to a state leaves it unaltered. In the Bloch sphere, the operation corresponds to a rotation of of the sphere around the axis followed by a reflection through the plane . Another way to see the rotation is to imagine it as a rotation over the bisector between and axis: a rotation around swaps points on the axis to the axis (and vice versa), and negates points on the axis. The shows the effect of applying to qubit .


You can try to visualize the effect of on the qubit For effect of the rotation and subsequent reflection through the plane you will obtain again The logic gates to a qubit , and are represented graphically as in


Multiple qubits quantum logic gates

Operations on quantum registers of two or more qubits are necessary to describe the transformations of compound states and in particular of the so-called entangled states. We have seen that a two-qubit register can not always be decomposed into the tensor product of the individual qubits components. Therefore we can not in such cases simulate an operation on the two qubits through operations on each qubit component. Also operations on qubit registers correspond to unit operations as in the case of a single qubit. The most important logic gates that implement operations on two classic bits are the AND, OR, XOR, NAND and NOR ports. The NOT and AND ports form a universal set, i.e. any boolean function can be accomplished by a combination of these two operations. For the same reason, the NAND constitutes a universal whole. Note that XOR alone or even together with NOT is not universal: since it preserves the total parity of the bits, only a subset of the boolean functions can be represented by this operation. The quantum analog of XOR is the CNOT gate (controlled-NOT) which operates on two qubits: the first is called the control qubit and the second is the qubit target. The CNOT gate is graphically represented by the circuit in


If the control is zero then the target is left unchanged; if the control is one, then the target is denied, that is Equivalently, CNOT can be seen as the transformation where is the control qubit, is the target and is the sum module that is the classical XOR operation. The representation as a unitary matrix is: where the first column describes the transformation of the vector of the computational base , the second that of the vector , the third of and the fourth of .

It is important to note that the CNOT, like all unit transformations, is invertible: input can always be obtained from the output. This is not true for the XOR and NAND logic gates: in general, classic operations are irreversible. The CNOT gate and one-qubit ports represent the prototypes of all quantum logic gates. In fact, is is possible to demonstrate the universality of these operations (later on this).

IBM Q - Testing the CNOT gate

[from Matteo: Add experiment over this.]

The gates made with vertical lines connecting two qubits together are a physical implementation of the CNOT gates just introduced. These two-qubit gates function like an exclusive OR gate in conventional digital logic. The qubit at the solid-dot end of the CNOT gate controls the whether or not the target qubit at the -end of the gate is inverted (hence controlled NOT, or CNOT). Some gates, like the CNOT, have hardware constraints; the set of allowed connections is defined by the schematic of the device located below the quantum Composer, along with recently calibrated device parameters.

### Quantum circuits

SWAP operation

A simple example of a quantum circuit is given in Fig. [circuit]{reference-type=”ref” reference=”circuit”}.


The circuit realizes the exchange of two qubits states. Given in input the register of two qubits , a CNOT is carried out with qubit of control a. As a result, is replaced by . The latter is taken as a control of a second CNOT with target . The effect is that a is replaced by . Finally, a last CNOT with control and target realizes the exchange by replacing with . The line with the black dot indicates the control qubit, while the qubits target are the inputs of . According to this convention the controlled-NOT is nothing more than a controlled- with .


Testing the swapping of the qubit is really simple. Let’s prepare a simulated register with two qubit in the initial state , like the one shown in

NOTE: in IBM platform the histogram will provide the result in the opposite order. For instance, in the figure, the unique bar on histogram is labelled , where refer to the second (q[1]) qubit in the register and to the first (q[0]). Thus, as show in the histogram, the result will be the swapping between the two qubit. Mathematically, the proof is simple. Let’s start by saying that Thus,

The initial state is ready (with value ). Then, we apply a CNOT. Our first qubit is in , thus the second qubit will be negated as well: the status become . Then, a second CNOT is applied using the second qubit as a control and the first as a target qubit. The first qubit change to the , bringing the entire register in the . The last CNOT doesn’t anything: the swap is completed. Ok but what if the initial status was set tup or any other possible permutation? Let’s see the effect of the circuit over the four possible initial state (the third is the one we already described).

Quantum teleportation

Continue here

Thank you everybody for reading!

  1. Available on Amazon 

comments powered by Disqus

Older · View Archive (33)

AWS Lambda, GoLang and Grafana to perform sentiment analysis for your company / business


In this article I will talk about my experience with AWS Lambda + API Gateway, GoLang (of course) and Grafana to build a sentiment analysis tool over customizable topics. Who should you read this post? Don’t know, maybe a CIO, a CTO, a CEO, a generic Chief or a MasterChef, for sure an AWS and GoLang fan like me. First of all: to better understand how to use Elasticsearch, read my previous post Elasticsearch over My home Network Attached Storage: it’s not so exciting as it seems, but you will have a general idea about what is Elasticsearch and how can you use it. Second: if you don’t know about AWS Lambda, study it. I personally believe that it represents one of the most interesting services currently offered by AWS: as they state, AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume and there is no charge when your code is not running. The amazing thing is that with a Free Tier trial you have 1 milions requests for free - O.O - to run code of any type of application or backend service - all with zero administration: you just upload your code - unfortunately the online editor for GoLang is not supported yet - and AWS Lambda1 takes care of everything required to run and scale your code with high availability. You can even set up your code to automatically trigger from other AWS services - as I have done with API Gateway - or call it directly from any web or mobile app. And…last but definetly not the least, why I’m writing this post!? Because starting from 15 January 2018, AWS Lambda support GoLang!!!

  1. You can find more information here 


Node.js, DynamoDB, and AWS Step Functions to collect sentimented movie reviews


Recently I worked with AWS Lambda and API Gateway to extend my set of personal APIs and collect information from several sources. I wrote an article on that (if you want to have a look). In this article I will talk about the AWS Step Functions service that enable create finite states machines to easy coordinate the components of distributed applications and microservices using visual workflows. Why AWS Step Functions? Because they let me create a tool to gather movie titles in teather, search for reviews about each of them and make a basic sentiment analysis over the review to help me decide what’s worth watching at teather and what’s worth waiting for on Netflix :D More in general, with AWS Step Functions, you can build applications made of individual components that each perform a discrete function: this lets you scale and change applications quickly. Step Functions is a reliable way to coordinate components and step through the functions of your application. They provides a graphical console to arrange and visualize the components of your application as a series of steps. This makes it simple to build and run multistep applications. Step Functions automatically triggers and tracks each step, and retries when there are errors, so your application executes in order and as expected. Step Functions logs the state of each step, so when things do go wrong, you can diagnose and debug problems quickly.