Table of contents
Introduction
In my previous article, I showed how to implement a simple Hello World program in Q#. In this article, we will take things one step further by introducing the usage of quantum gates, superposition and collapse of probability wave for a qubit. Quantum superposition is the phenomenon where a quantum system, such as a qubit, can exist in a combination of two or more states until it is measured. This allows quantum computers to perform parallel computations and achieve exponential speedup over classical computers. Seems complex but actually it is really straightforward.
In the following example, a qubit variable will be declared. Then it will be put into superposition and then measured. Afterward, the qubit will be reused, put into superposition and measured again. The program is therefore not deterministic and can produce different results each time it is executed.
Code
namespace Quantum.QSharpApplication2 {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
@EntryPoint()
operation SuperpositionAndCollapse () : Unit {
// Allocate a qubit from the system
use q1 = Qubit();
// Apply the Hadamard gate to create superposition
H(q1);
// Measure the qubit and collapse the probability wave
let collapsedValue = M(q1);
// Print the result of the measurement
if ((collapsedValue == Zero)) {
Message("0");
} else {
Message("1");
}
// Apply the Hadamard gate again to create superposition
H(q1);
// Measure the qubit and collapse the probability wave
let collapsedValue2 = M(q1);
// Print the result of the measurement
if ((collapsedValue2 == Zero)) {
Message("0");
} else {
Message("1");
}
}
}
Code Analysis
Let's start code analysis with the line:
use q1 = Qubit();
The classical computer offers a segment of memory where you can define variables of different types. The real quantum computers offer a pool of qubits instead. These might not be created or destroyed by a developer. You might think that the use statement acts like var in C#. But it actually takes one unused qubit from the system and assigns it to a variable named q1
.
Then the q1 qubit is put into superposition using the Hadamard gate:
H(q1);
Eventually, the fact of observation is applied to this qubit effectively collapsing the probability wave to a discrete value and assigning it to the newly created collapsedValue
variable:
let collapsedValue = M(q1);
Subsequently, this value is tested and a result is printed into the console:
if ((collapsedValue == Zero)) { Message("0"); } else { Message("1"); }
Afterward, the scenario is repeated to illustrate that previous operations do not 'deplete' a qubit. It might be reused and put into superposition again. Any other quantum gate might still be applied to it. Even if you deallocate this variable and then create a new one with a 'fresh' qubit, there is a chance that the previously used physical qubit will be assigned to it.
The result you will get might have different values:
By the way, you just implemented a true random number generator. It will not generate truly random numbers in a simulated environment on a classical computer though. For that, you need a real quantum computer.