Difference between revisions of "Quantum computing"
(→Quantum computing in Fōrmulæ) 
(→A full adder) 

(2 intermediate revisions by the same user not shown)  
Line 258:  Line 258:  
[[File:QuantumTutorial49.pngborder]]  [[File:QuantumTutorial49.pngborder]]  
−  ==  +  == Examples of quantum programs == 
−  [[File:QuantumTutorial50.png]]  +  In this section, several (very simple) programs will be written as quantum programs. 
+  
+  Two simple and very known problems of classical computation will be solved in quantum form.  
+  
+  As shown before, the using of quantum computing for solving classical (deterministic) problems offers no advantage. However, we are doing this for educational purposes. after all, we are using a simulated quantum oracle, not a real one, so we are not wasting expensive resources.  
+  
+  === Introduction ===  
+  
+  In electronics and (classical) digital circuits theories, is a very common exercise to design [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].  
+  
+  === A half adder ===  
+  
+  Classically, a half adder is created with the following digital circuit:  
+  
+  [[File:QuantumTutorial50_0.png]]  
+  
+  This circuit has the following tables of inputs and outputs:  
+  
+  { class="wikitable" style="textalign:center"  
+    
+  ! colspan="2" '''Inputs'''  colspan="2" '''Outputs'''  
+   textalign:center;"  
+   '''A'''  '''B'''  '''C'''  '''S'''  
+   style="textalign:center;"  
+   0  0  0  0  
+   style="textalign:center;"  
+   1  0  0  1  
+   style="textalign:center;"  
+   0  1  0  1  
+   style="textalign:center;"  
+   1  1  1  0  
+    
+  }  
+  
+  There are several quantum circuits able to create a half adder. Maybe the simplest one is:  
+  
+  [[File:QuantumTutorial50.png220px]]  
Input bits are A and B, giving the sum S with carry out C.  Input bits are A and B, giving the sum S with carry out C.  
Line 269:  Line 305:  
[[File:QuantumTutorial53.pngborder]]  [[File:QuantumTutorial53.pngborder]]  
+  
+  === A full adder ===  
+  
+  Classically, a full adder is created with the following digital circuit:  
+  
+  [[File:QuantumTutorial50_0b.png300px]]  
+  
+  This circuit has the following tables of inputs and outputs:  
+  
+  { class="wikitable" style="textalign:center"  
+   style="textalign:center;"  
+  !colspan="3" '''Inputs'''  colspan="2" '''Outputs'''  
+   style="textalign:center;"  
+   '''A'''  '''B'''  '''C'''<sub>in</sub>  '''C'''<sub>out</sub>  '''S'''  
+   style="textalign:center;"  
+   0  0  0  0  0  
+   style="textalign:center;"  
+   0  0  1  0  1  
+   style="textalign:center;"  
+   0  1  0  0  1  
+   style="textalign:center;"  
+   0  1  1  1  0  
+   style="textalign:center;"  
+   1  0  0  0  1  
+   style="textalign:center;"  
+   1  0  1  1  0  
+   style="textalign:center;"  
+   1  1  0  1  0  
+   style="textalign:center;"  
+   1  1  1  1  1  
+    
+  }  
+  
+  There are several quantum circuits able to create a full adder. Maybe the simplest one is:  
+  
+  [[File:QuantumTutorial50a.png300px]]  
+  
+  Input bits are A, B and C<sub>in</sub>, giving the sum S with carry out C.  
+  
+  [[File:QuantumTutorial52b.pngborder]]  
+  
+  [[File:QuantumTutorial53b.pngborder]]  
== Programmatic creation of quantum circuits and gates ==  == Programmatic creation of quantum circuits and gates == 
Revision as of 16:49, 23 March 2020
This page is under construction
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.
Contents
Fōrmulæ has a quantum computing package, in this section you will learn how to use it.
Introduction
Deterministic algorithms
Most algorithms are deterministic. It means that they follow a precise and well defined set of steps. If a given input is introduced to a deterministic program multiple times, it will always perform exactly the same set of instructions, and it will always generate the same output.
Probabilistic algorithms
There are several kinds of nondeterministic algorithms. The flavor we are interested in is named probabilistic algorithms.
Suppose we have to calculate the area of the following shape:
Since it is a irregular shape, we cannot use the well known formulae for regular shares, such as circles, polygons, etc.
One form of calculation consists in drawing a square around the shape, and select a number of randomly chosen points inside the square, such as follows:
Several points lie in the blue area, while several others do not. Lets calculate the reason of the points that lie in the blue area respect of the total of point. The area of the blue shape is approximately such that reason multiplied by the area of the enclosing square.
Of course it is an approximation, but this approximation is closed to the real area as we use more points.
It is an example of a probabilistic algorithm. Because it uses random numbers, results can differ even if we use the same input (the same shape, the same number of points, the same enclosing square) several times.
There are several probabilistic algorithms, the Monte Carlo method is a good example.
In XXXX, XXX showed a probabilistic algorithm that runs faster that any known deterministic algorithm.
Quantum mechanics
We do not pretend to explain all the theory about quantum mechanics, we only discuss the concepts that we are going to use, using (hopefully) simple examples.
You have surely known about the Schrödinger's cat paradox: There is a closed box, containing a cat and a mechanism that, after a specific period of time (let us say, an hour), with a 50% probability, will break a poison killing the cat. Quantum mechanics states that after an hour, if we do not open the box to see the result, inside the box there is a ghostly, mixed state of both alive and dead cat. The name of this phenomena is quantum superposition.
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.
A superposition state can be linked, for example, suppose that, before the box experiment we have opened a hole to the box, and attached to it a video camera objective to record the activity inside the box. Then we perform the experiment. after an hour, we take the cassette or memory from the video camera (without detaching it from the box, and without opening the box). According to quantum mechanics the cassette or memory does not contain an alive or dead cat recording, but a superposition of both. The superposition of the box is linked to the videotape superposition. This phenomena is called quantum entanglement.
If we perform an observation on a superposition state, it will not only collapse to a defined state, it will also collapse all their entangles states to a consistent state. In our example, if we decide to watch the cassette or memory, it will collapse and we will see recording of either alive or dead cat. if we later open the box, we will see the same result that the recording.
Quantum computing
In order to differentiate terms, we use the term classic to refer to the traditional theories, so is common to say classic computer to a traditional computer, or quantum algorithm to refer to an algorithm that use quantum elements.
In classic computers, the minimal unit of information is a bit. It is able to store a value of either a 0 (zero) or a 1.
In quantum computers, the minimal unit of information is a qubit (a quantum bit). It is able to store a zero value, usually represented as , a 1 value, usually represented as , 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.
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.
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.
Construction of actual bits and qubits
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.
physical realization of bits are usually done with semiconductors.
Realization of qubits is not easy and it is currently in development stages, several materials are being tested. The main problems are the isolation of element in order to impede the interaction with other materials, and the effect of spontaneous collapse of superposition quantum states. At the time of writing this article, quantum computers are very expensive and they exist in very specialized laboratories.
What can quantum computing be used for ?
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.
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.
Quantum oracles
Because quantum computing has no advantage on deterministic algorithms, the usual form of solve a problem is to separate the deterministic part of it, and run it with a classical computer, cheap and accessible, and let the probabilistic part to be done with a quantum computer. This scheme is called an hybrid architecture. In this architecture, the quantum computer is usually named a quantum oracle.
Since the probabilistic part called multiple times (such as the calculation area example), usually the classical part is for preparation and the loop of the quantum oracle invocation, so the invocation is performed a lot of times.
Current model of quantum computing
How is the task specified to be performed by an quantum oracle ?
It is defined as required by the current model of quantum computation: by a quantum circuit.
The following is an example of a quantum circuit:
We can observe:
 The circuit is like a digital circuit, it consist of one or several wires. In quantum circuits they are not wires, they represent the timeline, from left to right of a qubit. It is also called qubit evolution.
 The initial values of the qubits are at the left
 The circuit contains quantum gates, that alter the nature of a qubit, or they let two or more qubits to interact with each other.
 There can be measurement operations.
 After (at the right of) a measurement operation in a given qubit there cannot be any quantum gate operating on it, because after the measurement, the qubiit will have collapsed to a bit.
Simulation of quantum computers
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.
Simulation of a quantum computer requires much calculation. The amount of operations grows exponentially of the size of the quantum circuit (number of qubits) and the number of quantum gates on it.
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.
Creation of quantum circuits and gates
Creation of a quantum circuit
To create a quantum circuit, select the expression Programming.Quantum.Circuit. Because the number of qubits is required, it will be asked for:
After entering the number, the circuit is like the following:
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:
Addition of quantum gates
Note. This article does not provide any explanation of how quantum gates work, if you are interested please consult specialized literature.
The following quantum gates can be added to a quantum circuit:
Quantum gate  Expression  Parameters  Visual representation 

Pauli X (or NOT)  Programming.Quantum.gate.PauliX  The index of qubit in the circuit^{[note 1]}  
Pauli Y  Programming.Quantum.Gate.PauliY  The index of qubit in the circuit^{[note 1]}  
Pauli Z  Programming.Quantum.Gate.PauliZ  The index of qubit in the circuit^{[note 1]}  
Hadamard  Programming.Quantum.Gate.Hadamard  The index of qubit in the circuit^{[note 1]}  
Square root of NOT  Programming.Quantum.Gate.SqrtNot  The index of qubit in the circuit^{[note 1]}  
Phase shift  Programming.Quantum.Gate.PhaseShift  The index of qubit in the circuit^{[note 1]} The value of the numerator^{[note 2]} The value of the denominator^{[note 2]} 

S  Programming.Quantum.Gate.S  The index of qubit in the circuit^{[note 1]}  
T  Programming.Quantum.Gate.T  The index of qubit in the circuit^{[note 1]}  
Swap  Programming.Quantum.Gate.Swap  The indexes of the swapping qubits in the circuit^{[note 1]}  
Controlling  Programming.Quantum.Gate.Controlling  The index of the controlling qubit in the circuit^{[note 1]} The controlled gate^{[note 3]} 

Measurement^{[note 4]}  Programming.Quantum.Measurement  The index of qubit in the circuit^{[note 1]} 
 ↑ ^{1.00} ^{1.01} ^{1.02} ^{1.03} ^{1.04} ^{1.05} ^{1.06} ^{1.07} ^{1.08} ^{1.09} ^{1.10} Qubits are numbered downwards, so the first qubit is the topmost, and its index is 1, not 0.
 ↑ ^{2.0} ^{2.1} The result is a phase shift of .
 ↑ The controlled gate is given as the unique child expression of the controlling expression.
 ↑ The measurement operation is not a quantum gate, but structurally it is taken as it were.
Speaking in expressions, a quantum circuit is an expression containing quantum gates as its subexpressions. For example, the following quantum circuit:
In expressions is:
Controlled gates
There are several other quantum gates, but they can be created under a special kind of gate, a controlled quantum gate. A controlled gate is when a quantum gate (such like the ones we have discussed right now) called the controlled gate works depending on the state of a specific qubit, called the controlling gate.
As expressions, a a quantum controlling gate is an expression that contains one subexpression: the controlled gate.
Because a controlling gate can contain any kind of gate (excluding a measurement), it can also contain another controlling gate, so it is possible to create a gate consisting of multiple controlling gates in cascade.
See the following examples:
Gate  Visual representation  Expression representation 

Controlled NOT (also CNOT)  
Toffoli (also CCNOT)  
Fredkin (also CSWAP) 
Execution of a quantum circuit
To invoke the quantum oracle, we use the Quantum.Programming.ExecuteCircuit expression. It has the following characteristics:
 It takes as parameters, the quantum circuit and an array of the input qubits.
 If the circuit 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.
 It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).
The simplest quantum circuit is a 1qubit circuit with no gates. Let us use as input the qubit :
Remeber that if a qubit wire does not contain a measurement operator, it contains an implicit one at the end, so, from the point of view of the ExecuteCircuit expression, the circuit is equivalent to:
So the qubit is immediately measured. Because any qubit, when measured will collapse to a 0bit or a 1bit, and this qubit has an associated probability to collapse to 1bit of 0%, it will necessarily collapse to the 0bit, which is the result retrieved.
It is common to invoke the quantum oracle many times.
Let us create a function that takes a circuit, the input qubits, and a number of calls. It will return a chart of the results:
Let us use our function with the previous example, using 100 calls:
Even if we call the quantum oracle many times, we always get a 0bit, because all the times we have a qubit with 0% of probability to collapse to a 1bit.
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 1bit). The result, of course, will be always a 1bit.
Creating a superposition
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit , or the probability of 100% from a qubit to 50%. This is now a qubit with a superposition between the states qubit and qubit . 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 0bit, and the other half would collapse to a 1bit:
This is an example. If the exercise would be run again we may get 5050, of 5149, because it is now a probabilistic program.
Creating independent qubits in superposition
Let us examine the following excercise, which uses a twoqubit circuit:
The first bit will be 0bit or 1bit, with 50% probability each, and the second bit will always be 0bit. So, the two possible result are "00" and "10", with equal probability each.
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are "00", "01", "10" and "11" with equal (25%) probability each.
Creating entangled qubits
The minimal circuit to produce an entanglement of qubits is the following:
The CNOT (controlled not) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is 1>, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.
But wait, how the controllednot gate could know if the first qubit is 1> if it is in a superposition state between 0> and 1>? It does not, the second qubit is left also in a superposition state. Do you remember the Shoringer's cat paradox at the start of this document ? To make an analogy, the first qubis is the box, and the second qubit is the video camera recorder.
The two qubits are in a superposition state but their states are related (entangled). After a measurement of the first qubit, if it were the 0bit, the second qubit would remain unchanged (0). If the first qubit were measured as the 1bit, the controlled gate would flip the content of the second qubit from 0 to 1. In both cases we can only get two and only two results "00" and "11" with 50% probability each, as you saw in the previous figure.
The same result must be obtained if we measured the second qubit first. after such that measurement, any measurement of the first qubit will always report the same value from the second one.
Notice that entanglement does not mean that entangled qubits will always collapse the the same value after any measure (it is for the previous example). It means that the results will always be consistent. Foe example, see the following figure. It is the same as the previous one, except that the second qubit is initialized as 1>. Now, the two qubits will always have the opposite value after any measurement:
Examples of quantum programs
In this section, several (very simple) programs will be written as quantum programs.
Two simple and very known problems of classical computation will be solved in quantum form.
As shown before, the using of quantum computing for solving classical (deterministic) problems offers no advantage. However, we are doing this for educational purposes. after all, we are using a simulated quantum oracle, not a real one, so we are not wasting expensive resources.
Introduction
In electronics and (classical) digital circuits theories, is a very common exercise to design adders. An adder is a digital circuit used to sum bits. An adder of two bits are named a half adder. An adder for three bits is called a full adder.
A half adder
Classically, a half adder is created with the following digital circuit:
This circuit has the following tables of inputs and outputs:
Inputs  Outputs  

A  B  C  S 
0  0  0  0 
1  0  0  1 
0  1  0  1 
1  1  1  0 
There are several quantum circuits able to create a half adder. Maybe the simplest one is:
Input bits are A and B, giving the sum S with carry out C.
A full adder
Classically, a full adder is created with the following digital circuit:
This circuit has the following tables of inputs and outputs:
Inputs  Outputs  

A  B  C_{in}  C_{out}  S 
0  0  0  0  0 
0  0  1  0  1 
0  1  0  0  1 
0  1  1  1  0 
1  0  0  0  1 
1  0  1  1  0 
1  1  0  1  0 
1  1  1  1  1 
There are several quantum circuits able to create a full adder. Maybe the simplest one is:
Input bits are A, B and C_{in}, giving the sum S with carry out C.