Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Latest commit

 

History

History
114 lines (66 loc) · 5.38 KB

File metadata and controls

114 lines (66 loc) · 5.38 KB
title description author ms.author ms.date ms.topic uid
Create a Quantum Random Number Generator
Build a Q# project that demonstrates fundamental quantum concepts like superposition by creating a quantum random number generator.
bromeg
megbrow@microsoft.com
10/25/2019
article
microsoft.quantum.quickstarts.qrng

Quickstart: Implement a Quantum Random Number Generator in Q#

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.

Prerequisites

  • The Microsoft Quantum Development Kit.
  • Create a Q# Project

Write a Q# operation

Q# operation code

  1. Replace the contents of the Program.qs file with the following code:

:::code language="qsharp" source="~/quantum/samples/getting-started/qrng/Qrng.qs" range="3-15,34":::

As mentioned in our What is 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, during execution 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 using 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 de-allocated 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.

Visualizing the code with the Bloch sphere

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.

A qubit state with a high probability of measuring zero

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.

Preparing a qubit in superposition

  • Then we measure the qubit and save the output:

Measuring a qubit and saving 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).

Creating a complete random number generator

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 the Q# command line applications or use a host program.

To create the full Q# command line application, add the following entry point to your Q# program:

:::code language="qsharp" source="~/quantum/samples/getting-started/qrng/Qrng.qs" range="17-33":::

The executable 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.

:::code language="qsharp" source="~/quantum/samples/getting-started/qrng/Qrng.qs" range="3-34":::

In Visual Studio, simply press Ctrl + F5 to execute 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-30":::

You can then run your Python host program from the command line:

$ 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-39":::

You can then run your C# host program from the command line (in Visual Studio you should press F5):

$ dotnet run
The random number generated is 42