author | description | ms.author | ms.date | ms.service | ms.subservice | ms.topic | no-loc | title | uid | ||
---|---|---|---|---|---|---|---|---|---|---|---|
bromeg |
Build a Q# project that demonstrates fundamental quantum concepts like superposition by creating a quantum random number generator. |
megbrow |
02/01/2021 |
azure-quantum |
qdk |
tutorial |
|
Create a Quantum Random Number Generator |
microsoft.quantum.tutorial-qdk.random-number |
A simple example of a quantum algorithm written in Q# is a quantum random number generator. This algorithm leverages the nature of quantum mechanics to produce a random number.
- The Microsoft Quantum Development Kit.
- Create a Q# project for either a Q# application, with a Python host program, or a C# host program.
- Replace the contents of the Program.qs file with the following code:
namespace Qrng {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
operation SampleQuantumRandomNumberGenerator() : Result {
use q = Qubit(); // Allocate a qubit.
H(q); // Put the qubit to superposition. It now has a 50% chance of being 0 or 1.
return MResetZ(q); // Measure the qubit value.
}
}
As mentioned in our Understanding quantum computing article, a qubit is a unit of quantum information that can be in superposition. When measured, a qubit can only be either 0 or 1. However, before measurement, the state of the qubit represents the probability of reading either a 0 or a 1 with a measurement. This probabilistic state is known as superposition. We can use this probability to generate random numbers.
In our Q# operation, we introduce the Qubit
datatype, native to Q#. We can only allocate a Qubit
with a use
statement. When it gets allocated, a qubit is always in the Zero
state.
Using the H
operation, we are able to put our Qubit
in superposition. To measure a qubit and read its value, you use the M
intrinsic operation.
By putting our Qubit
in superposition and measuring it, our result will be a different value each time the code is invoked.
When a Qubit
is deallocated it must be explicitly set back to the Zero
state, otherwise the simulator will report a runtime error. An easy way to achieve this is invoking Reset
.
In the Bloch sphere, the north pole represents the classical value 0 and the south pole represents the classical value 1. Any superposition can be represented by a point on the sphere (represented by an arrow). The closer the end of the arrow to a pole the higher the probability the qubit collapses into the classical value assigned to that pole when measured. For example, the qubit state represented by the red arrow below has a higher probability of giving the value 0 if we measure it.
We can use this representation to visualize what the code is doing:
- First we start with a qubit initialized in the state 0 and apply
H
to create a superposition in which the probabilities for 0 and 1 are the same.
- Then we measure the qubit and save the output:
Since the outcome of the measurement is completely random, we have obtained a random bit. We can call this operation several times to create integers. For example, if we call the operation three times to obtain three random bits, we can build random 3-bit numbers (that is, a random number between 0 and 7).
Now that we have a Q# operation that generates random bits, we can use it to build a complete quantum random number generator. We can use a Q# application or use a host program.
To create the full Q# application, add the following entry point to your Q# program:
namespace Qrng {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
operation SampleQuantumRandomNumberGenerator() : Result {
use q = Qubit(); // Allocate a qubit.
H(q); // Put the qubit to superposition. It now has a 50% chance of being 0 or 1.
return MResetZ(q); // Measure the qubit value.
}
operation SampleRandomNumberInRange(max : Int) : Int {
mutable bits = new Result[0];
for idxBit in 1..BitSizeI(max) {
set bits += [SampleQuantumRandomNumberGenerator()];
}
let sample = ResultArrayAsInt(bits);
return sample > max
? SampleRandomNumberInRange(max)
| sample;
}
@EntryPoint()
operation SampleRandomNumber() : Int {
let max = 50;
Message($"Sampling a random number between 0 and {max}: ");
return SampleRandomNumberInRange(max);
}
}
The program will run the operation or function marked with the @EntryPoint()
attribute on a simulator or resource estimator, depending on the project configuration and command-line options.
In Visual Studio, simply press Ctrl + F5 to run the script.
In VS Code, build the Program.qs the first time by typing the below in the terminal:
dotnet build
For subsequent runs, there is no need to build it again. To run it, type the following command and press enter:
dotnet run --no-build
To run your new Q# program from Python, save the following code as host.py
:
:::code language="python" source="~/quantum/samples/interoperability/qrng/host.py" range="11-":::
You can then run your Python host program from the command prompt:
$ python host.py
Preparing Q# environment...
..The random number generated is 42
To run your new Q# program from C#, modify Driver.cs
to include the following C# code:
:::code language="csharp" source="~/quantum/samples/interoperability/qrng/Host.cs" range="4-":::
You can then run your C# host program from the command prompt (in Visual Studio you should press F5):
$ dotnet run
The random number generated is 42