# Difference between revisions of "Quantum computing"

This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.

Fōrmulæ has a quantum computing package, in this section you will learn how to use it.

## Introduction

### Deterministic algorithms

Most algorithms are deterministic. It means that they follow a precise and well defined set of steps. If a given input is introduced to a deterministic program multiple times, it will always perform exactly the same set of instructions, and it will always generate the same output.

### Probabilistic algorithms

There are several kinds of non-deterministic algorithms. The flavor we are interested in is named probabilistic algorithms.

Suppose we have to calculate the area of the following shape:

Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.

One form of calculation consists in drawing a square around the shape, and select a number of randomly chosen points inside the square, such as follows:

Several points lie in the blue area, while several others do not. Lets calculate the reason of the points that lie in the blue area respect of the total of point. The area of the blue shape is approximately such that reason multiplied by the area of the enclosing square.

Of course it is an approximation, but this approximation will be closer to the real area as we use more points.

It is an example of a probabilistic algorithm. Because it uses random numbers, results can differ even if we use the same input (the same shape, the same number of points, the same enclosing square) several times.

There are several probabilistic algorithms, the Monte Carlo method is a good example.

In the mid 1970s, Robert Solovay and Volker Strassen found a probabilistic algorithm to test if a number is prime or composite and proved that it runs faster that any known deterministic algorithm.

### Quantum mechanics

We do not pretend to explain all the theory about quantum mechanics, we only discuss the concepts that we are going to use, using (hopefully) simple examples.

You have surely known about the Schrödinger's cat paradox: There is a closed box, containing a cat and a mechanism that, after a specific period of time (let us say, an hour), with a 50% probability, will break a poison killing the cat. Quantum mechanics states that after an hour, if we do not open the box to see the result, inside the box there is a ghostly, mixed state of both alive and dead cat. The name of this phenomena is quantum superposition.

Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.

A superposition state can be linked, for example, suppose that, before the box experiment we have opened a hole to the box, and attached to it a video camera objective to record the activity inside the box. Then we perform the experiment. after an hour, we take the cassette or memory from the video camera (without detaching it from the box, and without opening the box). According to quantum mechanics the cassette or memory does not contain an alive or dead cat recording, but a superposition of both. The superposition of the box is linked to the videotape superposition. This phenomena is called quantum entanglement.

If we perform an observation on a superposition state, it will not only collapse to a defined state, it will also collapse all their entangles states to a consistent state. In our example, if we decide to watch the cassette or memory, it will collapse and we will see recording of either alive or dead cat. if we later open the box, we will see the same result that the recording.

### Quantum computing

In order to differentiate terms, we use the term classic to refer to the traditional theories, so is common to say classic computer to a traditional computer, or quantum algorithm to refer to an algorithm that use quantum elements.

In classic computers, the minimal unit of information is a bit. It is able to store a value of either a 0 (zero) or a 1. For clarity, they will be represented in this document as 0 and 1 respectively.

In quantum computers, the minimal unit of information is a qubit (a quantum bit). It is able to store a zero value, usually represented as |0〉 (it is called bra-ket notation), a 1 value, usually represented as |1〉, or a superposition of both states. More specifically, a qubit has an associated probability. What is this probability referred to ? It is the probability of the qubit to collapse to 1 once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to 0 when observed, a qubit with a probability of 100% will always collapse to 1 when observed, a qubit with a probability of 25% will collapse to 0 at 1/4 of times and to 1 75% of times when observed.

Notice that the qubit |0〉 is not the same as the bit 0. The qubit |0〉 will collapse to the bit 0 when measured, with 100% probability. The same occurs with the qubit |1〉 and the bit 1.

So, when qubits collapse, they become classical bits, in the same way that once we perform an Schrödinger's cat and make an observation, it collapses to a well defined state (an alive cat, or a dead cat) and the superposition disappears forever.

We can also create entangled qubits. Once we measure a qubit it collapse to be a classic bit, and their entangled qubits also collapse to be classical bits, in a consistent state.

### Physical realization of actual bits and qubits

Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.

Physical realization of bits are usually done with semiconductors.

Realization of qubits is not easy and it is currently in development stages, several materials are being tested. The main problems are the isolation of element in order to impede the interaction with other materials, and the effect of spontaneous collapse of superposition quantum states. At the time of writing this article, quantum computers are very expensive and they exist in very specialized laboratories.

### What can quantum computing be used for ?

Quantum computers can perform any task that classical computers can, either deterministic or probabilistic. Further, it was proved that using a quantum computer to simulate or emulate a classic algorithm will not be faster than with a classical computer, so using a quantum computer to perform classical algorithms provides no advantage.

However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.

It does not mean that any given probabilistic algorithm must be faster in quantum form. Faster quantum algorithms are NOT a translation from a classical algorithm to he same algorithm using a quantum computer, They result in very different algorithms, quantum algorithms are created with many different tools than classical algorithms. Development of faster quantum algorithms require a lot creativity, knowledge of mathematics, computation sciences, theories of information, etc.

At the moment of writing this document, about 65 quantum algorithms have only been found to be faster than their classical counterparts.

### Quantum oracles

Because quantum computing has no advantage on deterministic algorithms, the usual form of solve a problem is to separate the deterministic part of it, and run it with a classical computer, cheap and accessible, and let the probabilistic part to be done with a quantum computer. This scheme is called an hybrid architecture. In this architecture, the quantum computer is usually named a quantum oracle.

Since the probabilistic part called multiple times (such as the calculation area example), usually the classical part is for preparation and the loop of the quantum oracle invocation, so the invocation is performed a lot of times.

### Current model of quantum computing

How is the task specified to be performed by an quantum oracle ?

It is defined as required by the current model of quantum computation: by a quantum circuit.

The following is an example of a quantum circuit:

We can observe:

• The circuit is like a digital circuit, it consist of one or several wires. In quantum circuits they are not wires, they represent the timeline, from left to right of a qubit. It is also called qubit evolution.
• There are initial values of the qubits. In the figure, they are at the left
• The circuit contains quantum gates, that alter the nature of a qubit, or they let two or more qubits to interact with each other.
• There can be measurement operations.
• After (at the right of) a measurement operation in a given qubit there cannot be any quantum gate operating on it, because after the measurement, the qubiit will have collapsed to a bit.

### Simulation of quantum computers

Quantum computers are expensive at the moment of writing this, so simulating them is a good option.

Simulation of a quantum computer requires much calculation. The amount of operations grows exponentially of the size of the quantum circuit (number of qubits) and the number of quantum gates on it.

On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.

## Creation of quantum circuits and gates

### Creation of a quantum circuit

To create a quantum circuit, select the expression Programming.Quantum.Circuit. Because the number of qubits is required, it will be asked for:

After entering the number, the circuit is like the following:

The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:

Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.

The following quantum gates can be added to a quantum circuit:

Quantum gate Expression Parameters Visual representation
Pauli X (or NOT) Programming.Quantum.gate.PauliX The index of qubit in the circuit[note 1]
Pauli Y Programming.Quantum.Gate.PauliY The index of qubit in the circuit[note 1]
Pauli Z Programming.Quantum.Gate.PauliZ The index of qubit in the circuit[note 1]
Square root of NOT Programming.Quantum.Gate.SqrtNot The index of qubit in the circuit[note 1]
Phase shift Programming.Quantum.Gate.PhaseShift The index of qubit in the circuit[note 1]
The value of the numerator[note 2]
The value of the denominator[note 2]
S Programming.Quantum.Gate.S The index of qubit in the circuit[note 1]
T Programming.Quantum.Gate.T The index of qubit in the circuit[note 1]
Swap Programming.Quantum.Gate.Swap The indexes of the swapping qubits in the circuit[note 1]
Controlling Programming.Quantum.Gate.Controlling The index of the controlling qubit in the circuit[note 1]
The controlled gate[note 3]
Measurement[note 4] Programming.Quantum.Measurement The index of qubit in the circuit[note 1]
1. Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.
2. The result is a phase shift of .
3. The controlled gate is given as the unique child expression of the controlling expression.
4. The measurement operation is not a quantum gate, but structurally it is taken as it were.

Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:

In expressions is:

#### Controlled gates

There are several other quantum gates, but they can be created under a special kind of gate, a controlled quantum gate. A controlled gate is when a quantum gate (such like the ones we have discussed right now) called the controlled gate works depending on the state of a specific qubit, called the controlling gate.

As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.

Because a controlling gate can contain any kind of gate (excluding a measurement), it can also contain another controlling gate, so it is possible to create a gate consisting of multiple controlling gates in cascade.

See the following examples:

Gate Visual representation Expression representation
Controlled NOT (also CNOT)
Toffoli (also CCNOT)
Fredkin (also CSWAP)

## Execution of a quantum circuit

To invoke the quantum oracle, we use the Quantum.Programming.ExecuteCircuit expression. It has the following characteristics:

• It takes as parameters, the quantum circuit and an array of the input qubits.
• If the circuit has any qubit wire containing no measurement operator, it is considered as it had an implicit one at the end (right). It means that every qubit will be eventually (explicitely or implicitely) measured.
• It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).

The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit |0〉:

Remeber that if a qubit wire does not contain a measurement operator, it contains an implicit one at the end, so, from the point of view of the ExecuteCircuit expression, the circuit is equivalent to:

So the qubit is immediately measured. Because any qubit, when measured will collapse to 0 or 1, and this qubit has an associated probability to collapse to 1 of 0%, it will necessarily collapse to the 0, which is the result retrieved.

It is common to invoke the quantum oracle many times.

Let us create a function that takes a circuit, the input qubits, and a number of calls. It will return a chart of the results:

Let us use our function with the previous example, using 100 calls:

Even if we call the quantum oracle many times, we always get the bit 0, because all the times we have a qubit with 0% of probability to collapse to the bit 1.

Now, we can use more complex circuits. Let us start making a simple change, We can introduce a NOT gate. This gate will change the probability of the input qubit from 0% to 100% (to collapse to the bit 1). The result, of course, will be always the bit 1.

### Creating a superposition

The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit |0〉, or the probability of 100% from a qubit |1〉 to 50%. This is now a qubit with a superposition between the states qubit |0〉 and qubit |1〉. It means that if we had several qubits in such that state, and they all were measured, approximately the half of them would collapse to the bit 0, and the other half would collapse to the bit 1:

This is an example. If the exercise would be run again we may get 50-50, of 51-49, because it is now a probabilistic program.

### Creating independent qubits in superposition

Let us examine the following excercise, which uses a two-qubit circuit:

The first bit will be 0 or 1, with 50% probability each, and the second bit will always be 0. So, the two possible result are 00 and 10, with equal probability each.

In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are 00, 01, 10 and 11 with equal (25%) probability each.

### Creating entangled qubits

The minimal circuit to produce an entanglement of qubits is the following:

The CNOT (controlled not) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is |1〉, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.

But wait, how the controlled-not gate could know if the first qubit is |1〉 if it is in a superposition state between |0〉 and |1〉 ? It does not, the second qubit is left also in a superposition state. Do you remember the Schrödinger's cat paradox at the start of this document ? To make an analogy, the first qubis is the box, and the second qubit is the video camera recorder.

The two qubits are in a superposition state but their states are correlated (entangled). After a measurement of the first qubit, if it were the bit 0, the second qubit would remain unchanged (|0〉). If the first qubit were measured as the bit 1, the controlled gate would flip the content of the second qubit from |0〉 to |1〉. In both cases we can only get two and only two results 00 and 11 with 50% probability each, as you saw in the previous figure.

The same result must be obtained if we measured the second qubit first. after such that measurement, any measurement of the first qubit will always report the same value from the second one.

Notice that entanglement does not mean that entangled qubits will always collapse the the same value after any measure (it is for the previous example). It means that the results will always be consistent. For example, see the following figure. It is the same as the previous one, except that the second qubit is initialized as |1〉. Now, the two qubits will always have the opposite value after any measurement:

## Examples of quantum programs

In this section, several (very simple) programs will be written as quantum programs.

Two simple and very known problems of classical computation will be solved in quantum form.

As shown before, the using of quantum computing for solving classical (deterministic) problems offers no advantage. However, we are doing this for educational purposes. after all, we are using a simulated quantum oracle, not a real one, so we are not wasting expensive resources.

### Introduction

In electronics and (classical) digital circuits theories, is a very common exercise to design adders. An adder is a digital circuit used to sum bits. An adder of two bits are named a half adder. An adder for three bits is called a full adder.

Classically, a half adder is created with the following digital circuit:

This circuit has the following tables of inputs and outputs:

Inputs Outputs
A B C S
0 0 0 0
1 0 0 1
0 1 0 1
1 1 1 0

There are several quantum circuits able to create a half adder. Maybe the simplest one is:

Input bits are A and B, giving the sum S with carry out C.

We will need a function to convert bits to qubits, this is, if the input of this function is the bit 0 or 1, it return the qubit |0〉 or |1〉 respectively:

The following is the program for the half adder:

To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.

Classically, a full adder is created with the following digital circuit:

This circuit has the following tables of inputs and outputs:

Inputs Outputs
A B Cin Cout S
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1

There are several quantum circuits able to create a full adder. Maybe the simplest one is:

Input bits are A, B and Cin, giving the sum S with carry out C.

The following is the program for the full adder:

To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.

## Programmatic creation of quantum circuits and gates

Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:

The Shor's algorithm is a very well known probabilistic quantum algorithm, for integer factorization. This algorithm requires to apply the called quantum Fourier transform to the input. The interesting part is that this transform is a quantum circuit which depends of the input. It means that for every input has its own quantum circuit!

The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:

The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).

## Inspecting the mathematics behind quantum computing

There are mathematical concepts that govern the evolution of qubits through a quantum circuit. In this part we do not pretend to explain it to detail.

The evolution of the qubits on a single gate are defined as a unitary matrix of complex numbers, which can in turn be defined as operations (usually tensor products).

The complete circuit can be represented as a the multiplication of such these matrices.

A quantum circuit with n qubits and m quantum gates, are defined as a 2n x 2n matrix of complex numbers, which is also (at least) a multiplication of m 2n x 2n matrices.

As you can see, the matrices grow exponentially to the number of qubits. This growing makes the simulation of quantum circuit possible for relatively few qubits only.

For an example, consider the following 3-qubit circuit, with associated matrices of size 23 x 23 = 8 x 8:

The expression Programming.Circuit.GetCircuitMatrix retrieves the associated matrix of the circuit:

Because Fōrmulæ is a symbolic system, it is able to provide the result symbolically. Most quantum programming languages are able to manage the calculations numerically only.

The expression Programming.Circuit.GetCircuitOperations retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):

Both the expression Programming.Circuit.GetCircuitMatrix and Programming.Circuit.GetCircuitOperations can only accept quantum circuit containing no measurement operations.