https://wiki.formulae.org/mediawiki/api.php?action=feedcontributions&user=Admin&feedformat=atomFōrmulæ wiki - User contributions [en]2020-10-27T03:14:56ZUser contributionsMediaWiki 1.24.1https://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4025Quantum computing2020-06-27T20:01:24Z<p>Admin: /* Probabilistic algorithms */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation will be closer to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code> and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
The following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
The following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4024Quantum computing2020-04-26T21:10:17Z<p>Admin: /* A full adder */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code> and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
The following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
The following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4023Quantum computing2020-04-26T21:09:13Z<p>Admin: /* A half adder */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code> and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
The following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4022Quantum computing2020-03-27T16:50:54Z<p>Admin: /* Execution of a quantum circuit */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code> and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4021Quantum computing2020-03-26T01:21:05Z<p>Admin: /* Quantum computing */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code> and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4020Quantum computing2020-03-25T20:28:55Z<p>Admin: /* Inspecting the mathematics behind quantum computing */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code> and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4019Quantum computing2020-03-25T20:26:00Z<p>Admin: /* Creating independent qubits in superposition */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code> and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4018Quantum computing2020-03-25T20:25:34Z<p>Admin: /* Creating entangled qubits */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code>" and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (<code>|0〉</code>). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4017Quantum computing2020-03-25T20:23:27Z<p>Admin: </p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are <code>00</code>" and <code>10</code>, with equal probability each.<br />
<br />
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are <code>00</code>, <code>01</code>, <code>10</code> and <code>11</code> with equal (25%) probability each.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (0). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. In both cases we can only get two and only two results <code>00</code> and <code>11</code> with 50% probability each, as you saw in the previous figure.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4016Quantum computing2020-03-25T20:21:13Z<p>Admin: </p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (0). If the first qubit were measured as the bit <code>1</code>, the controlled gate would flip the content of the second qubit from <code>|0〉</code> to <code>|1〉</code>. 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.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
We will need a function to convert bits to qubits, this is, if the input of this function is the bit <code>0</code> or <code>1</code>, it return the qubit <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4015Quantum computing2020-03-25T20:19:18Z<p>Admin: </p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <code>|0〉</code>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <code>|0〉</code>, or the probability of 100% from a qubit <code>|1〉</code> to '''50%'''. This is now a qubit with a superposition between the states qubit <code>|0〉</code> and qubit <code>|1〉</code>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <code>|1〉</code>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <code>|1〉</code> if it is in a superposition state between <code>|0〉</code> and <code>|1〉</code> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (0). If the first qubit were measured as the bit <code>1</code>, 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.<br />
<br />
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.<br />
<br />
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 <code>|1〉</code>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <code>|0〉</code> or <code>|1〉</code> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4014Quantum computing2020-03-25T19:57:51Z<p>Admin: </p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to <code>0</code> or <code>1</code>, and this qubit has an associated probability to collapse to <code>1</code> of 0%, it will necessarily collapse to the <code>0</code>, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get the bit <code>0</code>, because all the times we have a qubit with 0% of probability to collapse to the bit <code>1</code>.<br />
<br />
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 <code>1</code>). The result, of course, will be always the bit <code>1</code>.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 <code>0</code>, and the other half would collapse to the bit <code>1</code>:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be <code>0</code> or <code>1</code>, with 50% probability each, and the second bit will always be <code>0</code>. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 <code>0</code>, the second qubit would remain unchanged (0). If the first qubit were measured as the bit <code>1</code>, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4013Quantum computing2020-03-25T19:51:09Z<p>Admin: /* Quantum computing */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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 <code>1</code> once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to <code>0</code> when observed, a qubit with a probability of 100% will always collapse to <code>1</code> when observed, a qubit with a probability of 25% will collapse to <code>0</code> at 1/4 of times and to <code>1</code> 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4012Quantum computing2020-03-25T19:49:28Z<p>Admin: /* Quantum computing */</p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit 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 <code>0</code> and <code>1</code> respectively.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <code>|0〉</code> (it is called [https://en.wikipedia.org/wiki/Bra%E2%80%93ket_notation bra-ket notation]), a 1 value, usually represented as <code>|1〉</code>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
Notice that the qubit <code>|0〉</code> is not the same as the bit <code>0</code>. The qubit <code>|0〉</code> will collapse to the bit <code>0</code> when measured, with 100% probability. The same occurs with the qubit <code>|1〉</code> and the bit <code>1</code>.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4011Quantum computing2020-03-25T18:47:56Z<p>Admin: </p>
<hr />
<div>This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4010Quantum computing2020-03-25T03:25:20Z<p>Admin: /* What can quantum computing be used for ? */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4009Quantum computing2020-03-25T03:22:49Z<p>Admin: /* What can quantum computing be used for ? */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
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, instead, they are very different algorithms, created with many different tools than classical algorithms. Development of faster quantum algorithms require a lot of knowledge of mathematics, computation sciences, theories of information, etc.<br />
<br />
At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4008Quantum computing2020-03-25T03:02:08Z<p>Admin: /* What can quantum computing be used for ? */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
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.<br />
<br />
However, some probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4007Quantum computing2020-03-25T02:38:23Z<p>Admin: /* Deterministic algorithms */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
Most algorithms are [https://en.wikipedia.org/wiki/Deterministic_algorithm 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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4006Quantum computing2020-03-25T02:36:27Z<p>Admin: /* Probabilistic algorithms */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms].<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4005Quantum computing2020-03-25T02:32:51Z<p>Admin: /* Physical realization of actual bits and qubits */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
Physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4004Quantum computing2020-03-25T02:31:16Z<p>Admin: /* Construction of actual bits and qubits */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Physical realization of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4003Quantum computing2020-03-25T00:41:30Z<p>Admin: /* Probabilistic algorithms */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In the mid 1970s, [https://en.wikipedia.org/wiki/Solovay%E2%80%93Strassen_primality_test 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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4002Quantum computing2020-03-25T00:38:43Z<p>Admin: /* Probabilistic algorithms */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
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.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4001Quantum computing2020-03-25T00:37:36Z<p>Admin: /* Probabilistic algorithms */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
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.<ref>*{{Cite journal |first=Robert M. |last=Solovay |first2=Volker |last2=Strassen |journal=SIAM Journal on Computing |title=A fast Monte-Carlo test for primality |volume=6 |year=1977 |issue=1 |pages=84–85 |doi=10.1137/0206006 |postscript=<!--None--> }} See also {{Cite journal |first=Robert M. |last=Solovay |first2=Volker |last2=Strassen |journal=SIAM Journal on Computing |title=Erratum: A fast Monte-Carlo test for primality |volume=7 |year=1978 |issue=1 |pages=118 |doi=10.1137/0207009 |postscript=<!--None--> }}</cite><br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=4000Quantum computing2020-03-24T19:26:26Z<p>Admin: /* Current model of quantum computing */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In XXXX, XXX showed a probabilistic algorithm that runs faster that any known deterministic algorithm.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* There are initial values of the qubits. In the figure, they are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=3999Quantum computing2020-03-24T19:25:40Z<p>Admin: /* Quantum computing */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In XXXX, XXX showed a probabilistic algorithm that runs faster that any known deterministic algorithm.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 of times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* The initial values of the qubits are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=3998Quantum computing2020-03-24T19:25:09Z<p>Admin: /* Quantum computing */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In XXXX, XXX showed a probabilistic algorithm that runs faster that any known deterministic algorithm.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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-bit at 1/4 oft times and to 1-bit 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* The initial values of the qubits are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=3997Quantum computing2020-03-24T18:49:01Z<p>Admin: /* Inspecting the mathematics behind quantum computing */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In XXXX, XXX showed a probabilistic algorithm that runs faster that any known deterministic algorithm.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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 oft times and to 1 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* The initial values of the qubits are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- style="text-align:center;"<br />
| '''A''' || '''B''' || '''C'''<sub>in</sub> || '''C'''<sub>out</sub> || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 0 || 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 0 || 1 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 0 || 1 || 0<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 1 || 1<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a full adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50a.png|300px]]<br />
<br />
Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.<br />
<br />
he following is the program for the full adder:<br />
<br />
[[File:QuantumTutorial52b.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53b.png|border]]<br />
<br />
== Programmatic creation of quantum circuits and gates ==<br />
<br />
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:<br />
<br />
[[File:QuantumTutorial60.png|border]]<br />
<br />
The [https://en.wikipedia.org/wiki/Shor%27s_algorithm Shor's algorithm] is a very well known probabilistic quantum algorithm, for [https://en.wikipedia.org/wiki/Integer_factorization integer factorization]. This algorithm requires to apply the called [https://en.wikipedia.org/wiki/Quantum_Fourier_transform 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!<br />
<br />
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:<br />
<br />
[[File:QuantumTutorial61.png|border]]<br />
<br />
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).<br />
<br />
[[File:QuantumTutorial62.png|border]]<br />
<br />
== Inspecting the mathematics behind quantum computing ==<br />
<br />
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.<br />
<br />
The evolution of the qubits on a single gate are defined as a [https://en.wikipedia.org/wiki/Unitary_matrix unitary matrix] of [https://en.wikipedia.org/wiki/Complex_number complex numbers], which can in turn be defined as operations (usually [https://en.wikipedia.org/wiki/Tensor_product tensor products]).<br />
<br />
The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.<br />
<br />
A quantum circuit with ''n'' qubits and ''m'' quantum gates, are defined as a 2<sup>n</sup> x 2<sup>n</sup> matrix of complex numbers, which is also (at least) a multiplication of m 2<sup>n</sup> x 2<sup>n</sup> matrices.<br />
<br />
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.<br />
<br />
For an example, consider the following 3-qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:<br />
<br />
[[File:QuantumTutorial70.png|border]]<br />
<br />
The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:<br />
<br />
[[File:QuantumTutorial72.png|border]]<br />
<br />
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.<br />
<br />
The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):<br />
<br />
[[File:QuantumTutorial71.png|border]]<br />
<br />
Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations.</div>Adminhttps://wiki.formulae.org/mediawiki/index.php?title=Quantum_computing&diff=3996Quantum computing2020-03-24T18:42:30Z<p>Admin: /* Inspecting the mathematics behind quantum computing */</p>
<hr />
<div><pre>This page is under construction</pre><br />
<br />
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.<br />
<br />
__TOC__<br />
<br />
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.<br />
<br />
== Introduction ==<br />
<br />
=== Deterministic algorithms ===<br />
<br />
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.<br />
<br />
=== Probabilistic algorithms ===<br />
<br />
There are several kinds of non-deterministic algorithms. The flavor we are interested in is named ''probabilistic algorithms''.<br />
<br />
Suppose we have to calculate the area of the following shape:<br />
<br />
[[File:QuantumTutorial01.png]]<br />
<br />
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial02.png]]<br />
<br />
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.<br />
<br />
Of course it is an approximation, but this approximation is closed to the real area as we use more points.<br />
<br />
<!-- [[File:QuantumTutorial03.png]]<br />
--><br />
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.<br />
<br />
There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.<br />
<br />
In XXXX, XXX showed a probabilistic algorithm that runs faster that any known deterministic algorithm.<br />
<br />
=== Quantum mechanics ===<br />
<br />
We do not pretend to explain all the theory about [https://en.wikipedia.org/wiki/Quantum_mechanics quantum mechanics], we only discuss the concepts that we are going to use, using (hopefully) simple examples.<br />
<br />
You have surely known about the [https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat 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 [https://en.wikipedia.org/wiki/Quantum_superposition quantum superposition].<br />
<br />
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.<br />
<br />
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 [https://en.wikipedia.org/wiki/Quantum_entanglement quantum entanglement].<br />
<br />
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.<br />
<br />
=== Quantum computing ===<br />
<br />
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.<br />
<br />
In classic computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.<br />
<br />
In quantum computers, the minimal unit of information is a [https://en.wikipedia.org/wiki/Qubit qubit] (a quantum bit). It is able to store a zero value, usually represented as <math>| 0 \rangle</math>, a 1 value, usually represented as <math>| 1 \rangle</math>, 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 oft times and to 1 75% of times when observed.<br />
<br />
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 defines state (an alive cat, or a dead cat) and the superposition disappears forever.<br />
<br />
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.<br />
<br />
=== Construction of actual bits and qubits ===<br />
<br />
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.<br />
<br />
physical realization of bits are usually done with semiconductors.<br />
<br />
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.<br />
<br />
=== What can quantum computing be used for ? ===<br />
<br />
On XXXX, XXX proved that quantum computers can perform any task that classical computers can. Further, it was proved that using a quantum computer to perform a deterministic algorithm will not be faster than with a classical computer.<br />
<br />
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.<br />
<br />
=== Quantum oracles ===<br />
<br />
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'''.<br />
<br />
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.<br />
<br />
=== Current model of quantum computing ===<br />
<br />
How is the task specified to be performed by an quantum oracle ?<br />
<br />
It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].<br />
<br />
The following is an example of a quantum circuit:<br />
<br />
[[File:QuantumTutorial04.png|350px]]<br />
<br />
We can observe:<br />
<br />
* The circuit is like a [https://en.wikipedia.org/wiki/Digital_circuit 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'''.<br />
* The initial values of the qubits are at the left<br />
* The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or they let two or more qubits to interact with each other.<br />
* There can be '''measurement''' operations.<br />
* 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.<br />
<br />
=== Simulation of quantum computers ===<br />
<br />
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.<br />
<br />
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.<br />
<br />
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.<br />
<br />
== Creation of quantum circuits and gates ==<br />
<br />
=== Creation of a quantum circuit ===<br />
<br />
To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:<br />
<br />
[[File:QuantumTutorial05.png]]<br />
<br />
After entering the number, the circuit is like the following:<br />
<br />
[[File:QuantumTutorial06.png]]<br />
<br />
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:<br />
<br />
[[File:QuantumTutorial07.png]]<br />
<br />
=== Addition of quantum gates ===<br />
<br />
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.<br />
<br />
The following quantum gates can be added to a quantum circuit:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Quantum gate || Expression || Parameters || Visual representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate Pauli X (or NOT)] || [[Programming.Quantum.gate.PauliX]] || The index of qubit in the circuit<ref group="note" name="x">Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.</ref> || [[File:QuantumTutorial08.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Y_gate Pauli Y] || [[Programming.Quantum.Gate.PauliY]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial09.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-Z_({{urlencode:}}'%22%60UNIQ--postMath-00000028-QINU%60%22'{{urlencode:}})_gate Pauli Z] || [[Programming.Quantum.Gate.PauliZ]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial10.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard] || [[Programming.Quantum.Gate.Hadamard]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial11.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT] || [[Programming.Quantum.Gate.SqrtNot]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial12.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQ--postMath-00000038-QINU%60%22'{{urlencode:}})_gates Phase shift] || [[Programming.Quantum.Gate.PhaseShift]] || The index of qubit in the circuit<ref group="note" name="x"/><br>The value of the numerator<ref group="note" name="y">The result is a phase shift of <math>\tfrac{n}{d} \pi</math>.</ref><br>The value of the denominator<ref group="note" name="y"/> || [[File:QuantumTutorial13.png]]<br />
|-<br />
| S || [[Programming.Quantum.Gate.S]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial14.png]]<br />
|-<br />
| T || [[Programming.Quantum.Gate.T]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial15.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap] || [[Programming.Quantum.Gate.Swap]] || The indexes of the swapping qubits in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial16.png]]<br />
|-<br />
| Controlling || [[Programming.Quantum.Gate.Controlling]] || The index of the controlling qubit in the circuit<ref group="note" name="x"/><br>The controlled gate<ref group="note">The controlled gate is given as the unique child expression of the controlling expression.</ref> || [[File:QuantumTutorial17.png]]<br />
|-<br />
| Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref> || [[Programming.Quantum.Measurement]] || The index of qubit in the circuit<ref group="note" name="x"/> || [[File:QuantumTutorial18.png]]<br />
|}<br />
<references group="note"/><br />
<br />
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:<br />
<br />
[[File:QuantumTutorial20.png]]<br />
<br />
In expressions is:<br />
<br />
[[File:QuantumTutorial21.png]]<br />
<br />
==== Controlled gates ====<br />
<br />
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'''.<br />
<br />
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.<br />
<br />
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.<br />
<br />
See the following examples:<br />
<br />
{| class="wikitable" style="text-align: center;"<br />
! Gate || Visual representation || Expression representation<br />
|-<br />
| [https://en.wikipedia.org/wiki/Controlled_NOT_gate Controlled NOT (also CNOT)] || [[File:QuantumTutorial30.png]] || [[File:QuantumTutorial31.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Toffoli_gate Toffoli (also CCNOT)] || [[File:QuantumTutorial32.png]] || [[File:QuantumTutorial33.png]]<br />
|-<br />
| [https://en.wikipedia.org/wiki/Fredkin_gate Fredkin (also CSWAP)] || [[File:QuantumTutorial34.png]] || [[File:QuantumTutorial35.png]]<br />
|}<br />
<br />
== Execution of a quantum circuit ==<br />
<br />
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:<br />
<br />
* It takes as parameters, the quantum circuit and an array of the input qubits.<br />
* If the circuit contains 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.<br />
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).<br />
<br />
The simplest quantum circuit is a 1-qubit circuit with no gates. Let us use as input the qubit <math>| 0 \rangle</math>:<br />
<br />
[[File:QuantumTutorial40.png|border]]<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial41.png|border]]<br />
<br />
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0-bit or a 1-bit, and this qubit has an associated probability to collapse to 1-bit of 0%, it will necessarily collapse to the 0-bit, which is the result retrieved.<br />
<br />
It is common to invoke the quantum oracle many times.<br />
<br />
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:<br />
<br />
[[File:QuantumTutorial42.png|border]]<br />
<br />
Let us use our function with the previous example, using 100 calls:<br />
<br />
[[File:QuantumTutorial43.png|border]]<br />
<br />
Even if we call the quantum oracle many times, we always get a 0-bit, because all the times we have a qubit with 0% of probability to collapse to a 1-bit.<br />
<br />
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 a 1-bit). The result, of course, will be always a 1-bit.<br />
<br />
[[File:QuantumTutorial44.png|border]]<br />
<br />
=== Creating a superposition ===<br />
<br />
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit <math>| 0 \rangle</math>, or the probability of 100% from a qubit <math>| 1 \rangle</math> to '''50%'''. This is now a qubit with a superposition between the states qubit <math>| 0 \rangle</math> and qubit <math>| 1 \rangle</math>. 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 a 0-bit, and the other half would collapse to a 1-bit:<br />
<br />
[[File:QuantumTutorial45.png|border]]<br />
<br />
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.<br />
<br />
=== Creating independent qubits in superposition ===<br />
<br />
Let us examine the following excercise, which uses a two-qubit circuit:<br />
<br />
[[File:QuantumTutorial46.png|border]]<br />
<br />
The first bit will be 0-bit or 1-bit, with 50% probability each, and the second bit will always be 0-bit. So, the two possible result are "00" and "10", with equal probability each.<br />
<br />
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.<br />
<br />
[[File:QuantumTutorial47.png|border]]<br />
<br />
=== Creating entangled qubits ===<br />
<br />
The minimal circuit to produce an entanglement of qubits is the following:<br />
<br />
[[File:QuantumTutorial48.png|border]]<br />
<br />
The CNOT ([https://en.wikipedia.org/wiki/Controlled_NOT_gate controlled not]) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is <math>| 1 \rangle</math>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.<br />
<br />
But wait, how the controlled-not gate could know if the first qubit is <math>| 1 \rangle</math> if it is in a superposition state between <math>| 0 \rangle</math> and <math>| 1 \rangle</math> ? 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.<br />
<br />
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 0-bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1-bit, 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.<br />
<br />
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.<br />
<br />
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 <math>| 1 \rangle</math>. Now, the two qubits will always have the opposite value after any measurement:<br />
<br />
[[File:QuantumTutorial49.png|border]]<br />
<br />
== Examples of quantum programs ==<br />
<br />
In this section, several (very simple) programs will be written as quantum programs.<br />
<br />
Two simple and very known problems of classical computation will be solved in quantum form.<br />
<br />
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.<br />
<br />
=== Introduction ===<br />
<br />
In electronics and (classical) digital circuits theories, is a very common exercise to design [https://en.wikipedia.org/wiki/Adder_(electronics) adders]. An adder is a digital circuit used to sum bits. An adder of two bits are named a [https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder half adder]. An adder for three bits is called a [https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder full adder].<br />
<br />
=== A half adder ===<br />
<br />
Classically, a half adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0.png]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- <br />
! colspan="2"| '''Inputs''' || colspan="2"| '''Outputs'''<br />
|- text-align:center;"<br />
| '''A''' || '''B''' || '''C''' || '''S'''<br />
|- style="text-align:center;"<br />
| 0 || 0 || 0 || 0<br />
|- style="text-align:center;"<br />
| 1 || 0 || 0 || 1<br />
|- style="text-align:center;"<br />
| 0 || 1 || 0 || 1<br />
|- style="text-align:center;"<br />
| 1 || 1 || 1 || 0<br />
|-<br />
|}<br />
<br />
There are several quantum circuits able to create a half adder. Maybe the simplest one is:<br />
<br />
[[File:QuantumTutorial50.png|220px]]<br />
<br />
Input bits are A and B, giving the sum S with carry out C.<br />
<br />
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 <math>| 0 \rangle</math> or <math>| 1 \rangle</math> respectively:<br />
<br />
[[File:QuantumTutorial51.png|border]]<br />
<br />
he following is the program for the half adder:<br />
<br />
[[File:QuantumTutorial52.png|border]]<br />
<br />
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.<br />
<br />
[[File:QuantumTutorial53.png|border]]<br />
<br />
=== A full adder ===<br />
<br />
Classically, a full adder is created with the following digital circuit:<br />
<br />
[[File:QuantumTutorial50_0b.png|300px]]<br />
<br />
This circuit has the following tables of inputs and outputs:<br />
<br />
{| class="wikitable" style="text-align:center"<br />
|- style="text-align:center;"<br />
!colspan="3"| '''Inputs''' || colspan="2&am