Skip to content Inglorious Coderz IT

← All posts

Quantum Computing

Let's Program a Quantum Computer in Python

An introduction to quantum computing, starting from the code.

· by Matteo Antony Mistretta

This article was originally written in Italian. Read the original →

Warning: we’ll be talking about logic circuits, vectors, matrices and quantum physics. For the bravest only!

If you recognize the following diagram as a logic circuit, you’re on the right track:

Half Adder

This article is an attempt to explain the basics of quantum computing in a simple, linear and experimental way, starting from the code rather than from maths or physics: subjects that, however fascinating, are not for everyone, and are perhaps more useful to explain how things work than how to use them. In short, this is a post for engineers more than for scientists.

To try the following code on your own computer you need to install Qiskit, and possibly the Jupyter extension for Visual Studio Code.

Special thanks go to Federico Mattei from IBM who, with a workshop held for Powercoders, got me hooked on the subject:

📸 See the post on Instagram

and to Gabriele Agliardi, also from IBM, who read and validated my articles:

📸 See the post on Instagram

OK, let’s start. First of all, let’s import the libraries we need.

from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_bloch_multivector, plot_histogram
import math

Then a few handy functions to draw or better format the output. It’s not worth trying to understand what they do right now; we’ll come back to them later.

def draw(circuit):
    return circuit.draw(output='mpl')


def unitary(circuit):
    backend = Aer.get_backend('unitary_simulator')
    result = execute(circuit, backend).result()
    prettify_matrix(result.get_unitary())


def statevector(circuit):
    backend = Aer.get_backend('statevector_simulator')
    result = execute(circuit, backend).result()
    prettify_vector(result.get_statevector())
    return plot_bloch_multivector(result.get_statevector())


def counts(circuit):
    backend = Aer.get_backend('qasm_simulator')
    result = execute(circuit, backend, shots=100).result()
    counts = result.get_counts()
    print(counts)
    return plot_histogram(counts)


def prettify_matrix(matrix):
    pretty_matrix = ''
    for row in range(len(matrix)):
        pretty_matrix += '|'
        for col in range(len(matrix[row])):
            pretty_matrix += prettify_value(matrix[row][col])
            if col < len(matrix[row]) - 1:
                pretty_matrix += ' '
        pretty_matrix += '|\n'
    print(pretty_matrix)


def prettify_vector(vector):
    pretty_vector = ''
    for row in range(len(vector)):
        pretty_vector += '|' + prettify_value(vector[row]) + '|\n'
    print(pretty_vector)


def prettify_value(value):
    if math.isclose(value.imag, 0, abs_tol=0.01):
        return '{num.real:>-.2f}'.format(num=value)
    elif math.isclose(value.real, 0, abs_tol=0.01):
        return '{num.imag:>-.2f}i'.format(num=value)
    else:
        return '{num.real:>-.2f}+{num.imag:>-.2f}i'.format(num=value)

Logic gates

The simplest computer we can build is a circuit with a single quantum wire and no logic gates.

circuit = QuantumCircuit(1)
draw(circuit)

svg

Quantum circuits can be represented mathematically as unitary matrices: mathematically, this means that these matrices multiplied by their conjugate transpose give the identity matrix (A⋅A†=IA \cdot A^\dagger = I), but in plain words they describe operations that are always reversible, that is, operations that can also be performed backwards in time, or that let you trace the output back to the initial input. If you want to dig deeper into what that means and why, there’s an excellent answer on StackExchange:

Quantum gates have to be reversible because quantum mechanics is reversible (and even more specifically it is unitary). It’s just an observed fact about the universe. (Even measurement can be modeled as a reversible unitary operation, inconvenient though that may be.)

Actually, classical computers also have to be reversible. We just happen to be able to sidestep the problem by throwing out accumulated garbage information as we go. Throwing out garbage information during quantum computations would also be possible, but because discarded garbage information counts as a measurement and measurement tends to break quantum algorithms… not so viable.

Circuits can run on different backends, simulated or real, and to inspect the corresponding unitary matrix we’ll use a dedicated simulator.

backend = Aer.get_backend('unitary_simulator')
result = execute(circuit, backend).result()
result.get_unitary()
array([[1.+0.j, 0.+0.j],
       [0.+0.j, 1.+0.j]])

Since the unitary matrix is hard to read like this, let’s use one of the functions defined at the beginning of the article to format it better.

unitary(circuit)
|1.00 0.00|
|0.00 1.00|

It’s the identity matrix! Basically, this circuit doesn’t change the initial input at all. In fact, we can use the I gate, which corresponds to the identity matrix.

circuit = QuantumCircuit(1)
circuit.i(0)
draw(circuit)

svg

unitary(circuit)
|1.00 0.00|
|0.00 1.00|

The input is not a bit, that is a scalar that is either 0 or 1, but a qubit: a vector in a three-dimensional space. Let’s see what it looks like.

statevector(circuit)
|1.00|
|0.00|

svg

This is the vector called ∣0⟩|0\rangle (zero ket) in Dirac notation (aka bra-ket notation).

Fun fact: if the qubit lives in a three-dimensional world, why does the vector only have two dimensions? For convenience, on the y axis we use the number i (or j). So, to be precise, a qubit is represented by a two-dimensional vector of complex numbers! Here’s an example.

circuit = QuantumCircuit(1)
circuit.initialize([1/math.sqrt(2), 1j/math.sqrt(2)], 0)
statevector(circuit)
|0.71|
|0.71i|

svg

And here’s the unitary matrix.

unitary(circuit)
|0.71 -0.71|
|0.71i 0.71i|

In practice, none of the algorithms I’ve seen so far really needs to bother with the third dimension, so we can treat the vector as purely two-dimensional.

Let’s look at the first simple logic gate that actually does something: the X gate (equivalent to the NOT gate in classical circuits).

circuit = QuantumCircuit(1)
circuit.x(0)
draw(circuit)

svg

The associated matrix flips the input vector.

unitary(circuit)
|0.00 1.00|
|1.00 0.00|

And indeed the resulting vector is flipped. This is the vector called ∣1⟩|1\rangle (one ket).

statevector(circuit)
|0.00|
|1.00|

svg

This matrix is unitary too: if we try a double negation, we get the identity matrix again.

circuit = QuantumCircuit(1)
circuit.x(0)
circuit.x(0)
draw(circuit)

svg

unitary(circuit)
|1.00 0.00|
|0.00 1.00|
statevector(circuit)
|1.00|
|0.00|

svg

Just as there’s an X gate, there are also Y and Z gates. All three rotate the state vector by 180° around their respective axis, and together they make up the Pauli gates. So in this case NOT is not just a negation: it’s a half turn around an axis. What happens if we rotate ∣0⟩|0\rangle around the y axis?

circuit = QuantumCircuit(1)
circuit.y(0)
draw(circuit)

svg

statevector(circuit)
|0.00|
|1.00i|

svg

The Y gate leads to the same result as the X gate, just rotating around the y axis! Here’s its matrix, for the curious.

unitary(circuit)
|0.00 -1.00i|
|1.00i 0.00|

As expected, it includes complex numbers, since we’re involving the y axis.

circuit = QuantumCircuit(1)
circuit.z(0)
draw(circuit)

svg

statevector(circuit)
|1.00|
|-0.00|

svg

The Z gate rotated the vector onto itself! Not very useful when the vector is already parallel to the z axis. Here’s the corresponding unitary matrix.

unitary(circuit)
|1.00 0.00|
|-0.00 -1.00|

A more interesting gate is called the Hadamard gate, denoted by the symbol H. Let’s see what it does.

circuit = QuantumCircuit(1)
circuit.h(0)
draw(circuit)

svg

statevector(circuit)
|0.71|
|0.71|

svg

Finally, an interesting result! It might look like the vector was simply rotated by 90° around the y axis, but if that were the case the operation wouldn’t be reversible: applying the same gate again wouldn’t bring the vector back to its starting point, but would push it even further, all the way to ∣1⟩|1\rangle. The Hadamard gate actually takes a stranger path: it can be decomposed into a 180° rotation around the x axis followed by a -90° rotation around the y axis, or we can see it as a circular rotation around a pivot placed at 45°, as described on StackExchange.

Hadamard on the Bloch sphere

Here’s the unitary matrix: the values are a rounding of 121 \over \sqrt 2, which is why it’s usually written as

12[111−1]\frac{1}{\sqrt 2} \begin{bmatrix}1 & 1\\ 1 & -1\end{bmatrix}

unitary(circuit)
|0.71 0.71|
|0.71 -0.71|

The vector is in a superposition of states: it’s not ∣0⟩|0\rangle, it’s not ∣1⟩|1\rangle, it’s a bit of both. In the literature it’s called ∣+⟩|+\rangle (plus ket) and its opposite ∣−⟩|-\rangle (minus ket). For completeness, the vectors lying on the y axis are called ∣↑⟩|\uparrow\rangle (up ket) and ∣↓⟩|\downarrow\rangle (down ket) respectively.

Fun fact: what is that 121 \over \sqrt 2 quantity? The length of a vector is computed with the Pythagorean theorem: x2+y2\sqrt{x^2 + y^2}. If the vector must always have length 1 (it’s a unit vector), then the vector [11]\begin{bmatrix} 1\\ 1\end{bmatrix} must be divided by its magnitude 12+12=2\sqrt{1^2 + 1^2} = \sqrt 2.

Measurement

Every self-respecting piece of software is split into three phases:

  • Input preparation
  • Computation
  • Output measurement

Quantum computers are no exception, except that if the qubit is in a superposition of states, measuring it will make it collapse onto one of the two possible states with 50% probability. The qubits we want to measure are mapped onto classical wires, so the output is made of plain bits.

circuit = QuantumCircuit(1, 1)
circuit.measure(0, 0)
draw(circuit)

svg

counts(circuit)
{'0': 100}

svg

100 times out of 100 the qubit collapsed to bit 0. Let’s do the same with the ∣1⟩|1\rangle state.

circuit = QuantumCircuit(1, 1)
circuit.x(0)
circuit.measure(0, 0)
draw(circuit)

svg

counts(circuit)
{'1': 100}

svg

Here too, the qubit collapsed to bit 1 all 100 times. What happens if we use the Hadamard gate instead?

circuit = QuantumCircuit(1, 1)
circuit.h(0)
circuit.measure(0, 0)
draw(circuit)

svg

counts(circuit)
{'0': 50, '1': 50}

svg

In this case the qubit collapsed to bit 0 exactly half of the time and to bit 1 the other half. Repeating the experiment we might get different results (that’s why multiple shots are run), but always with 50% probability.

Two-wire circuits

OK, let’s move on to two quantum wires. As long as we use the gates introduced so far, the two wires will run like parallel tracks, without affecting each other.

circuit = QuantumCircuit(2, 2)
circuit.x(1)
draw(circuit)

svg

The corresponding unitary matrix is a combination of the matrices on the single wires: to be precise, it’s their tensor product A⊗BA \otimes B.

unitary(circuit)
|0.00 0.00 1.00 0.00|
|0.00 0.00 0.00 1.00|
|1.00 0.00 0.00 0.00|
|0.00 1.00 0.00 0.00|

Now let’s measure the output: we shouldn’t expect any surprises, the circuit should behave like a classical one. We can also add a “barrier” to visually separate the computation phase from the measurement phase.

circuit.barrier()
circuit.measure([0,1], [0,1])
draw(circuit)

svg

counts(circuit)
{'10': 100}

svg

The result is, 100 times out of 100, the number 10! Of course we’re talking binary, so it’s a 1 (the most significant bit) and a 0 (the least significant one), which we can read as the number 2.

At this point you might expect AND, OR, XOR gates and all their combinations to exist. Not quite. Those gates wouldn’t be reversible, so we need very special gates that let us trace the output back to the input, or that can also be used going “backwards in time”.

The first interesting two-wire gate is CNOT, which negates one qubit (called target) if the other one (called control) is 1.

circuit = QuantumCircuit(2, 2)
circuit.cnot(0, 1)
draw(circuit)

svg

The corresponding unitary matrix is not very intuitive, unless you read the post on my blog where I try to make sense of these matrices.

unitary(circuit)
|1.00 0.00 0.00 0.00|
|0.00 0.00 0.00 1.00|
|0.00 0.00 1.00 0.00|
|0.00 1.00 0.00 0.00|

Let’s see the circuit in action.

circuit.barrier()
circuit.measure([0,1], [0,1])
draw(circuit)

svg

counts(circuit)
{'00': 100}

svg

Nothing happens when both qubits are initialized to zero. So let’s try with the control qubit set to 1.

circuit = QuantumCircuit(2, 2)
circuit.x(0)
circuit.cnot(0, 1)
circuit.barrier()
circuit.measure([0,1], [0,1])
draw(circuit)

svg

counts(circuit)
{'11': 100}

svg

All very intuitive — except that when we use this gate on qubits in superposition, the effect is completely different.

circuit = QuantumCircuit(2, 2)
circuit.h([0, 1])
circuit.x(0)
circuit.cnot(0, 1)
circuit.h([0, 1])
circuit.barrier()
circuit.measure([0,1], [0,1])
draw(circuit)

svg

counts(circuit)
{'00': 100}

svg

The negated qubit turns out to be the control one! This behavior is called “phase kickback”, a trick that stems from quantum entanglement, and it’s at the heart of most of the major quantum algorithms.

There would be so much more to say, but this was meant to be just a short introduction. To leave you with a little cliffhanger, here’s a three-wire gate called the “Toffoli gate”, or CCNOT, which behaves like a NAND gate when the most significant bit is initialized to ∣1⟩|1\rangle. And once you have a gate like NAND, you can build any other kind of logic circuit.

circuit = QuantumCircuit(3, 3)
circuit.x(2)
circuit.toffoli(0, 1, 2)
draw(circuit)

svg

Some useful links to start learning quantum computing:

— IceOnFire