Difference between revisions of "Quantum computing"
(→Controlled gates) 
(→Probabilistic algorithms) 

(70 intermediate revisions by the same user not shown)  
Line 1:  Line 1:  
−  
−  
This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.  This page is a tutorial to perform (simulated) quantum computing in in Fōrmulæ.  
Line 11:  Line 9:  
=== Deterministic algorithms ===  === 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.  +  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. 
=== Probabilistic algorithms ===  === Probabilistic algorithms ===  
−  There are several kinds of nondeterministic algorithms. The flavor we are interested in is named  +  There are several kinds of nondeterministic algorithms. The flavor we are interested in is named [https://en.wikipedia.org/wiki/Randomized_algorithm probabilistic algorithms]. 
Suppose we have to calculate the area of the following shape:  Suppose we have to calculate the area of the following shape:  
Line 29:  Line 27:  
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.  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  +  Of course it is an approximation, but this approximation will be closer to the real area as we use more points. 
−  +  
−  +  
+  <! [[File:QuantumTutorial03.png]]  
+  >  
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.  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 [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.  There are several probabilistic algorithms, the [https://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] is a good example.  
−  In  +  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. 
=== Quantum mechanics ===  === Quantum mechanics ===  
−  +  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.  
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].  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].  
Line 55:  Line 53:  
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 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 [https://en.wikipedia.org/wiki/Bit bit]. It is able to store a value of either a 0 (zero) or a 1.  +  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. 
−  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 <  +  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 braket 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. 
−  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  +  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>. 
+  
+  So, when qubits collapse, they become classical bits, in the same way that once we perform an Schrödinger's cat and make an observation, it collapses to a well defined state (an alive cat, or a dead cat) and the superposition disappears forever.  
We can also create entangled qubits. Once we measure a qubit it collapse to be a classic bit, and their entangled qubits also collapse to be classical bits, in a consistent state.  We can also create entangled qubits. Once we measure a qubit it collapse to be a classic bit, and their entangled qubits also collapse to be classical bits, in a consistent state.  
−  ===  +  === Physical realization of actual bits and qubits === 
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.  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.  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.  
Line 73:  Line 73:  
=== What can quantum computing be used for ? ===  === What can quantum computing be used for ? ===  
−  +  Quantum computers can perform any task that classical computers can, either deterministic or probabilistic. Further, it was proved that using a quantum computer to simulate or emulate a classic algorithm will not be faster than with a classical computer, so using a quantum computer to perform classical algorithms provides no advantage.  
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.  However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.  
+  
+  It does not mean that any given probabilistic algorithm must be faster in quantum form. Faster quantum algorithms are NOT a translation from a classical algorithm to he same algorithm using a quantum computer, They result in very different algorithms, quantum algorithms are created with many different tools than classical algorithms. Development of faster quantum algorithms require a lot creativity, knowledge of mathematics, computation sciences, theories of information, etc.  
+  
+  At the moment of writing this document, about [http://quantumalgorithmzoo.org/ 65 quantum algorithms] have only been found to be faster than their classical counterparts.  
=== Quantum oracles ===  === Quantum oracles ===  
Line 85:  Line 89:  
=== Current model of quantum computing ===  === Current model of quantum computing ===  
−  How is  +  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 [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].  It is defined as required by the current model of quantum computation: by a [https://en.wikipedia.org/wiki/Quantum_circuit quantum circuit].  
Line 95:  Line 99:  
We can observe:  We can observe:  
−  * The circuit is like a  +  * 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'''. 
−  *  +  * There are initial values of the qubits. In the figure, they are at the left 
−  * The circuit contains [https://en.wikipedia.org/wiki/Quantum_logic_gate quantum gates], that alter the nature of a qubit, or let two or more qubits to interact with each other.  +  * 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. 
−  *  +  * 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 ===  === Simulation of quantum computers ===  
Line 109:  Line 113:  
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.  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:  To create a quantum circuit, select the expression [[Programming.Quantum.Circuit]]. Because the number of qubits is required, it will be asked for:  
Line 127:  Line 129:  
[[File:QuantumTutorial07.png]]  [[File:QuantumTutorial07.png]]  
−  +  === 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:  The following quantum gates can be added to a quantum circuit:  
{ class="wikitable" style="textalign: center;"  { class="wikitable" style="textalign: center;"  
−  ! Quantum gate   +  ! Quantum gate  Expression  Parameters  Visual representation 
    
−   [https://en.wikipedia.org/wiki/Quantum_logic_gate#PauliX_gate Pauli X (or NOT)]  1  [[File:QuantumTutorial08.png]]  +   [https://en.wikipedia.org/wiki/Quantum_logic_gate#PauliX_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]] 
    
−   [https://en.wikipedia.org/wiki/Quantum_logic_gate#PauliY_gate Pauli Y]   +   [https://en.wikipedia.org/wiki/Quantum_logic_gate#PauliY_gate Pauli Y]  [[Programming.Quantum.Gate.PauliY]]  The index of qubit in the circuit<ref group="note" name="x"/>  [[File:QuantumTutorial09.png]] 
    
−   [https://en.wikipedia.org/wiki/Quantum_logic_gate#PauliZ_({{urlencode:}}'%22%60UNIQpostMath00000028QINU%60%22'{{urlencode:}})_gate Pauli Z]   +   [https://en.wikipedia.org/wiki/Quantum_logic_gate#PauliZ_({{urlencode:}}'%22%60UNIQpostMath00000028QINU%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]] 
    
−   [https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate Hadamard]   +   [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]] 
    
−   [https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT) Square root of NOT]   +   [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]] 
    
−   [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQpostMath00000038QINU%60%22'{{urlencode:}})_gates Phase shift]   +   [https://en.wikipedia.org/wiki/Quantum_logic_gate#Phase_shift_({{urlencode:}}'%22%60UNIQpostMath00000038QINU%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]] 
    
−   S   +   S  [[Programming.Quantum.Gate.S]]  The index of qubit in the circuit<ref group="note" name="x"/>  [[File:QuantumTutorial14.png]] 
    
−   T   +   T  [[Programming.Quantum.Gate.T]]  The index of qubit in the circuit<ref group="note" name="x"/>  [[File:QuantumTutorial15.png]] 
    
−   [https://en.wikipedia.org/wiki/Quantum_logic_gate#Swap_(SWAP)_gate Swap]   +   [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]] 
    
−   Controlling   +   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]] 
    
−   Measurement<ref group="note">The measurement operation is not a quantum gate, but structurally it is taken as it were.</ref>   +   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]] 
}  }  
<references group="note"/>  <references group="note"/>  
Line 166:  Line 170:  
[[File:QuantumTutorial21.png]]  [[File:QuantumTutorial21.png]]  
−  +  ==== 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'''.  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'''.  
Line 188:  Line 190:  
}  }  
−  +  == Execution of a quantum circuit ==  
To invoke the quantum oracle, we use the [[Quantum.Programming.ExecuteCircuit]] expression. It has the following characteristics:  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.  * It takes as parameters, the quantum circuit and an array of the input qubits.  
−  * If the circuit  +  * If the circuit has any qubit ''wire'' containing no measurement operator, it is considered as it had an implicit one at the end (right). It means that every qubit will be eventually (explicitely or implicitely) measured. 
* It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).  * 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 <  +  The simplest quantum circuit is a 1qubit circuit with no gates. Let us use as input the qubit <code>0〉</code>: 
[[File:QuantumTutorial40.pngborder]]  [[File:QuantumTutorial40.pngborder]]  
−  Remeber that if a qubit ''wire'' does not contain a measurement operator, it contains an implicit one at the end, so  +  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: 
[[File:QuantumTutorial41.pngborder]]  [[File:QuantumTutorial41.pngborder]]  
−  So the qubit is immediately measured. Because any qubit, when measured will collapse to  +  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. 
It is common to invoke the quantum oracle many times.  It is common to invoke the quantum oracle many times.  
Line 216:  Line 218:  
[[File:QuantumTutorial43.pngborder]]  [[File:QuantumTutorial43.pngborder]]  
−  Even if we call the quantum oracle many times, we always get  +  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>. 
−  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  +  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>. 
[[File:QuantumTutorial44.pngborder]]  [[File:QuantumTutorial44.pngborder]]  
−  +  === 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 <  +  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>: 
[[File:QuantumTutorial45.pngborder]]  [[File:QuantumTutorial45.pngborder]]  
Line 230:  Line 232:  
This is an example. If the exercise would be run again we may get 5050, of 5149, because it is now a probabilistic program.  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:  Let us examine the following excercise, which uses a twoqubit circuit:  
[[File:QuantumTutorial46.pngborder]]  [[File:QuantumTutorial46.pngborder]]  
+  
+  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.  
+  
+  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.  
[[File:QuantumTutorial47.pngborder]]  [[File:QuantumTutorial47.pngborder]]  
−  +  === Creating entangled qubits ===  
+  
+  The minimal circuit to produce an entanglement of qubits is the following:  
[[File:QuantumTutorial48.pngborder]]  [[File:QuantumTutorial48.pngborder]]  
+  
+  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.  
+  
+  But wait, how the controllednot 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.  
+  
+  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.  
+  
+  The same result must be obtained if we measured the second qubit first. after such that measurement, any measurement of the first qubit will always report the same value from the second one.  
+  
+  Notice that entanglement does not mean that entangled qubits will always collapse the the same value after any measure (it is for the previous example). It means that the results will always be consistent. For example, see the following figure. It is the same as the previous one, except that the second qubit is initialized as <code>1〉</code>. Now, the two qubits will always have the opposite value after any measurement:  
[[File:QuantumTutorial49.pngborder]]  [[File:QuantumTutorial49.pngborder]]  
−  === Programmatic creation of quantum circuits and  +  == 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 [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.  
+  
+  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:  
+  
+  [[File:QuantumTutorial51.pngborder]]  
+  
+  The following is the program for the half adder:  
+  
+  [[File:QuantumTutorial52.pngborder]]  
+  
+  To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.  
+  
+  [[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.  
+  
+  The following is the program for the full adder:  
+  
+  [[File:QuantumTutorial52b.pngborder]]  
+  
+  To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.  
+  
+  [[File:QuantumTutorial53b.pngborder]]  
+  
+  == Programmatic creation of quantum circuits and gates ==  
+  
+  Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:  
[[File:QuantumTutorial60.pngborder]]  [[File:QuantumTutorial60.pngborder]]  
+  
+  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!  
+  
+  The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:  
[[File:QuantumTutorial61.pngborder]]  [[File:QuantumTutorial61.pngborder]]  
+  
+  The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).  
[[File:QuantumTutorial62.pngborder]]  [[File:QuantumTutorial62.pngborder]]  
−  ==  +  == Inspecting the mathematics behind quantum computing == 
+  
+  There are mathematical concepts that govern the evolution of qubits through a quantum circuit. In this part we do not pretend to explain it to detail.  
+  
+  The evolution of the qubits on a single gate are defined as a [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]).  
+  
+  The complete circuit can be represented as a the [https://en.wikipedia.org/wiki/Matrix_multiplication multiplication] of such these matrices.  
+  
+  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.  
+  
+  As you can see, the matrices grow exponentially to the number of qubits. This growing makes the simulation of quantum circuit possible for relatively few qubits only.  
+  
+  For an example, consider the following 3qubit circuit, with associated matrices of size 2<sup>3</sup> x 2<sup>3</sup> = 8 x 8:  
+  
+  [[File:QuantumTutorial70.pngborder]]  
+  
+  The expression [[Programming.Circuit.GetCircuitMatrix]] retrieves the associated matrix of the circuit:  
+  
+  [[File:QuantumTutorial72.pngborder]]  
+  
+  Because Fōrmulæ is a symbolic system, it is able to provide the result symbolically. Most quantum programming languages are able to manage the calculations numerically only.  
+  
+  The expression [[Programming.Circuit.GetCircuitOperations]] retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):  
+  
+  [[File:QuantumTutorial71.pngborder]]  
+  
+  Both the expression [[Programming.Circuit.GetCircuitMatrix]] and [[Programming.Circuit.GetCircuitOperations]] can only accept quantum circuit containing no measurement operations. 
Latest revision as of 15:01, 27 June 2020
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 will be closer to the real area as we use more points.
It is an example of a probabilistic algorithm. Because it uses random numbers, results can differ even if we use the same input (the same shape, the same number of points, the same enclosing square) several times.
There are several probabilistic algorithms, the Monte Carlo method is a good example.
In the mid 1970s, Robert Solovay and Volker Strassen found a probabilistic algorithm to test if a number is prime or composite and proved that it runs faster that any known deterministic algorithm.
Quantum mechanics
We do not pretend to explain all the theory about quantum mechanics, we only discuss the concepts that we are going to use, using (hopefully) simple examples.
You have surely known about the Schrödinger's cat paradox: There is a closed box, containing a cat and a mechanism that, after a specific period of time (let us say, an hour), with a 50% probability, will break a poison killing the cat. Quantum mechanics states that after an hour, if we do not open the box to see the result, inside the box there is a ghostly, mixed state of both alive and dead cat. The name of this phenomena is quantum superposition.
Once we decide to open the box and observe, the quantum superposition collapses to a state of alive cat or dead cat.
A superposition state can be linked, for example, suppose that, before the box experiment we have opened a hole to the box, and attached to it a video camera objective to record the activity inside the box. Then we perform the experiment. after an hour, we take the cassette or memory from the video camera (without detaching it from the box, and without opening the box). According to quantum mechanics the cassette or memory does not contain an alive or dead cat recording, but a superposition of both. The superposition of the box is linked to the videotape superposition. This phenomena is called quantum entanglement.
If we perform an observation on a superposition state, it will not only collapse to a defined state, it will also collapse all their entangles states to a consistent state. In our example, if we decide to watch the cassette or memory, it will collapse and we will see recording of either alive or dead cat. if we later open the box, we will see the same result that the recording.
Quantum computing
In order to differentiate terms, we use the term classic to refer to the traditional theories, so is common to say classic computer to a traditional computer, or quantum algorithm to refer to an algorithm that use quantum elements.
In classic computers, the minimal unit of information is a bit. It is able to store a value of either a 0 (zero) or a 1. For clarity, they will be represented in this document as 0
and 1
respectively.
In quantum computers, the minimal unit of information is a qubit (a quantum bit). It is able to store a zero value, usually represented as 0〉
(it is called braket notation), a 1 value, usually represented as 1〉
, or a superposition of both states. More specifically, a qubit has an associated probability. What is this probability referred to ? It is the probability of the qubit to collapse to 1
once it is observed (or measured). So, a qubit with a probability of 0% will always collapse to 0
when observed, a qubit with a probability of 100% will always collapse to 1
when observed, a qubit with a probability of 25% will collapse to 0
at 1/4 of times and to 1
75% of times when observed.
Notice that the qubit 0〉
is not the same as the bit 0
. The qubit 0〉
will collapse to the bit 0
when measured, with 100% probability. The same occurs with the qubit 1〉
and the bit 1
.
So, when qubits collapse, they become classical bits, in the same way that once we perform an Schrödinger's cat and make an observation, it collapses to a well defined state (an alive cat, or a dead cat) and the superposition disappears forever.
We can also create entangled qubits. Once we measure a qubit it collapse to be a classic bit, and their entangled qubits also collapse to be classical bits, in a consistent state.
Physical realization of actual bits and qubits
Bits and qubits are abstractions, in order to perform actual classical/quantum computation, physical realization of bits/qubits need to be physically built.
Physical realization of bits are usually done with semiconductors.
Realization of qubits is not easy and it is currently in development stages, several materials are being tested. The main problems are the isolation of element in order to impede the interaction with other materials, and the effect of spontaneous collapse of superposition quantum states. At the time of writing this article, quantum computers are very expensive and they exist in very specialized laboratories.
What can quantum computing be used for ?
Quantum computers can perform any task that classical computers can, either deterministic or probabilistic. Further, it was proved that using a quantum computer to simulate or emulate a classic algorithm will not be faster than with a classical computer, so using a quantum computer to perform classical algorithms provides no advantage.
However, probabilistic algorithms are the land where quantum computing can grow and flourish. After all, qubits work probabilistically, as probabilistic algorithms do.
It does not mean that any given probabilistic algorithm must be faster in quantum form. Faster quantum algorithms are NOT a translation from a classical algorithm to he same algorithm using a quantum computer, They result in very different algorithms, quantum algorithms are created with many different tools than classical algorithms. Development of faster quantum algorithms require a lot creativity, knowledge of mathematics, computation sciences, theories of information, etc.
At the moment of writing this document, about 65 quantum algorithms have only been found to be faster than their classical counterparts.
Quantum oracles
Because quantum computing has no advantage on deterministic algorithms, the usual form of solve a problem is to separate the deterministic part of it, and run it with a classical computer, cheap and accessible, and let the probabilistic part to be done with a quantum computer. This scheme is called an hybrid architecture. In this architecture, the quantum computer is usually named a quantum oracle.
Since the probabilistic part called multiple times (such as the calculation area example), usually the classical part is for preparation and the loop of the quantum oracle invocation, so the invocation is performed a lot of times.
Current model of quantum computing
How is the task specified to be performed by an quantum oracle ?
It is defined as required by the current model of quantum computation: by a quantum circuit.
The following is an example of a quantum circuit:
We can observe:
 The circuit is like a digital circuit, it consist of one or several wires. In quantum circuits they are not wires, they represent the timeline, from left to right of a qubit. It is also called qubit evolution.
 There are initial values of the qubits. In the figure, they are at the left
 The circuit contains quantum gates, that alter the nature of a qubit, or they let two or more qubits to interact with each other.
 There can be measurement operations.
 After (at the right of) a measurement operation in a given qubit there cannot be any quantum gate operating on it, because after the measurement, the qubiit will have collapsed to a bit.
Simulation of quantum computers
Quantum computers are expensive at the moment of writing this, so simulating them is a good option.
Simulation of a quantum computer requires much calculation. The amount of operations grows exponentially of the size of the quantum circuit (number of qubits) and the number of quantum gates on it.
On the other hand, simulating (relatively small) quantum circuits are good for learning, teaching and experimentation.
Creation of quantum circuits and gates
Creation of a quantum circuit
To create a quantum circuit, select the expression Programming.Quantum.Circuit. Because the number of qubits is required, it will be asked for:
After entering the number, the circuit is like the following:
The vertical rectangle is a placeholder for quantum gates. You can add more placeholders as you wish by using the INS key:
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 has any qubit wire containing no measurement operator, it is considered as it had an implicit one at the end (right). It means that every qubit will be eventually (explicitely or implicitely) measured.
 It returns an array of bits (no qubits, because of the last point, all the qubits will be measured).
The simplest quantum circuit is a 1qubit circuit with no gates. Let us use as input the qubit 0〉
:
Remeber that if a qubit wire does not contain a measurement operator, it contains an implicit one at the end, so, from the point of view of the ExecuteCircuit expression, the circuit is equivalent to:
So the qubit is immediately measured. Because any qubit, when measured will collapse to 0
or 1
, and this qubit has an associated probability to collapse to 1
of 0%, it will necessarily collapse to the 0
, which is the result retrieved.
It is common to invoke the quantum oracle many times.
Let us create a function that takes a circuit, the input qubits, and a number of calls. It will return a chart of the results:
Let us use our function with the previous example, using 100 calls:
Even if we call the quantum oracle many times, we always get the bit 0
, because all the times we have a qubit with 0% of probability to collapse to the bit 1
.
Now, we can use more complex circuits. Let us start making a simple change, We can introduce a NOT gate. This gate will change the probability of the input qubit from 0% to 100% (to collapse to the bit 1
). The result, of course, will be always the bit 1
.
Creating a superposition
The Hadamard quantum gate is the most used gate to create a superposition. It will change the probability of 0% of a qubit 0〉
, or the probability of 100% from a qubit 1〉
to 50%. This is now a qubit with a superposition between the states qubit 0〉
and qubit 1〉
. It means that if we had several qubits in such that state, and they all were measured, approximately the half of them would collapse to the bit 0
, and the other half would collapse to the bit 1
:
This is an example. If the exercise would be run again we may get 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 0
or 1
, with 50% probability each, and the second bit will always be 0
. So, the two possible result are 00
and 10
, with equal probability each.
In the following example, the two qubits are in superposition state, but they are independent of each other, so the four possible results are 00
, 01
, 10
and 11
with equal (25%) probability each.
Creating entangled qubits
The minimal circuit to produce an entanglement of qubits is the following:
The CNOT (controlled not) gate is a conditional not gate, it makes the following: If the controlling qubit (in this case, the first one) is 1〉
, then it flips (NOT) the content of the controlled (or target) qubit (in this case, the second one), elsewere it does nothing.
But wait, how the 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 Schrödinger's cat paradox at the start of this document ? To make an analogy, the first qubis is the box, and the second qubit is the video camera recorder.
The two qubits are in a superposition state but their states are correlated (entangled). After a measurement of the first qubit, if it were the bit 0
, the second qubit would remain unchanged (0〉
). If the first qubit were measured as the bit 1
, the controlled gate would flip the content of the second qubit from 0〉
to 1〉
. In both cases we can only get two and only two results 00
and 11
with 50% probability each, as you saw in the previous figure.
The same result must be obtained if we measured the second qubit first. after such that measurement, any measurement of the first qubit will always report the same value from the second one.
Notice that entanglement does not mean that entangled qubits will always collapse the the same value after any measure (it is for the previous example). It means that the results will always be consistent. For example, see the following figure. It is the same as the previous one, except that the second qubit is initialized as 1〉
. Now, the two qubits will always have the opposite value after any measurement:
Examples of quantum programs
In this section, several (very simple) programs will be written as quantum programs.
Two simple and very known problems of classical computation will be solved in quantum form.
As shown before, the using of quantum computing for solving classical (deterministic) problems offers no advantage. However, we are doing this for educational purposes. after all, we are using a simulated quantum oracle, not a real one, so we are not wasting expensive resources.
Introduction
In electronics and (classical) digital circuits theories, is a very common exercise to design adders. An adder is a digital circuit used to sum bits. An adder of two bits are named a half adder. An adder for three bits is called a full adder.
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.
We will need a function to convert bits to qubits, this is, if the input of this function is the bit 0
or 1
, it return the qubit 0〉
or 1〉
respectively:
The following is the program for the half adder:
To test the program, we calculate all the possible combinations of two input bits. Notice that the result is in decimal.
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.
The following is the program for the full adder:
To test the program, we calculate all the possible combinations of three input bits. Notice that the result is in decimal.
Programmatic creation of quantum circuits and gates
Up now, we have created quantum circuits and gates manually. However, it is possible to create them by programming. see the following example:
The Shor's algorithm is a very well known probabilistic quantum algorithm, for integer factorization. This algorithm requires to apply the called quantum Fourier transform to the input. The interesting part is that this transform is a quantum circuit which depends of the input. It means that for every input has its own quantum circuit!
The following program generates the quantum circuit for the discrete Fourier transform, which depends of the input n:
The following are some result, for n 1..5: (Note. Several authors define the circuit slightly different).
Inspecting the mathematics behind quantum computing
There are mathematical concepts that govern the evolution of qubits through a quantum circuit. In this part we do not pretend to explain it to detail.
The evolution of the qubits on a single gate are defined as a unitary matrix of complex numbers, which can in turn be defined as operations (usually tensor products).
The complete circuit can be represented as a the multiplication of such these matrices.
A quantum circuit with n qubits and m quantum gates, are defined as a 2^{n} x 2^{n} matrix of complex numbers, which is also (at least) a multiplication of m 2^{n} x 2^{n} matrices.
As you can see, the matrices grow exponentially to the number of qubits. This growing makes the simulation of quantum circuit possible for relatively few qubits only.
For an example, consider the following 3qubit circuit, with associated matrices of size 2^{3} x 2^{3} = 8 x 8:
The expression Programming.Circuit.GetCircuitMatrix retrieves the associated matrix of the circuit:
Because Fōrmulæ is a symbolic system, it is able to provide the result symbolically. Most quantum programming languages are able to manage the calculations numerically only.
The expression Programming.Circuit.GetCircuitOperations retrieves the associated matrix of the circuit, as a multiplication of matrices, representing each every gate of the circuit):
Both the expression Programming.Circuit.GetCircuitMatrix and Programming.Circuit.GetCircuitOperations can only accept quantum circuit containing no measurement operations.