Random number generator in Q#

Random number generator in Q#

Introduction

Random number generation is a process that let's you generate a sequence of numbers that cannot be reasonably predicted. In this article, you will learn how to use Q# to create a true random number generator, which can produce unpredictable outcomes by exploiting the quantum phenomena of superposition and measurement.

Difference Between Pseudo-Random and True Random Numbers

Most programming languages, such as C# or Java, have built-in functions or classes that can generate pseudo-random numbers (for example Random class in C#). These are numbers that appear to be random, but are actually derived from a mathematical formula that takes a seed value as an input. The seed value can be specified by the user, or generated from a source of entropy, such as the current time.

Pseudo-random numbers are sufficient for most purposes, as they satisfy certain statistical tests for randomness. For example, if you generate a random number between 0 and 1, you would expect that about half of the numbers will be less than 0.5. Other tests include the frequency test, the serial test, and the gap test.

However, pseudo-random numbers are not truly random, because they are deterministic and can be predicted if the seed value and the formula are known. This can pose a security risk for some applications that require high levels of randomness and unpredictability.

To generate true random numbers, you need a physical device that can exploit a physical phenomenon that has inherent randomness. For example, some devices use thermal noise, radioactive decay, or atmospheric noise as sources of entropy. These devices are called hardware random number generators (HRNGs).

Another way to generate true random numbers is to use quantum computing, which relies on the quantum property of superposition.

What is Superposition?

Superposition is the ability of a qubit to exist in a combination of two states, usually denoted as \(\ket {0}\) and \(\ket {1}\). This means that a qubit can have some probability of being in either state, until it is measured. When a qubit is measured, it collapses to one of the two states, with the probability determined by the superposition.

What is Measurement?

Measurement is the process of observing a qubit and obtaining its state. Measurement is irreversible, meaning that once a qubit is measured, it loses its superposition and becomes fixed in either \(\ket{0}\) or \(\ket{1}\) . Measurement is also random, meaning that you cannot predict the outcome of a measurement with certainty, unless the qubit is in a pure state (i.e., 100% probability of being in either \(\ket{0}\) or \(\ket{1}\) .

How to Create a Superposition in Q#?

Q# provides several quantum gates, which are operations that can manipulate qubits. One of the most common quantum gates is the Hadamard gate, denoted as H. The Hadamard gate can create a superposition of \(\ket{0}\) and \(\ket{1}\) with equal probabilities, by applying the following transformation:

H \(\ket {0}\) = \(\frac{1}{\sqrt{2}}(\ket{0} + \ket{1})\)

H \(\ket {1}\) = \(\frac{1}{\sqrt{2}}(\ket{0} - \ket{1})\)

To apply the Hadamard gate to a qubit in Q#, you can use the following syntax:

H(qubit); // qubit is the name of the qubit variable

How to Measure a Qubit in Q#?

Q# provides a built-in function, M, which can measure a qubit and return its state as a classical bit. The M function can be used as follows:

let result = M(qubit); // result is a classical bit variable

The result variable will store either Zero or One, depending on the outcome of the measurement.

Random Number Generator code

Now, let’s dive into the code to see how this is implemented:

namespace Quantum.QSharpApplication {
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;

    @EntryPoint()
    operation GenerateRandomNumber () : Int {
        mutable randomNumber = 0; // initialize a classical integer variable
        for i in 0..7 { // loop 8 times
            use qubit = Qubit(); // allocate a qubit
            H(qubit); // apply the Hadamard gate
            let result = M(qubit); // measure the qubit
            if (result == One) { // if the result is One
                set randomNumber += 1 <<< i; // add 2^i to the randomNumber
            }
            Reset(qubit); // reset the qubit to |0>
        }
        return randomNumber; // return the randomNumber
    }
}

The main operation, GenerateRandomNumber, is then defined as the entry point of the program.

In this operation, a loop is set up to iterate eight times, each iteration corresponding to a bit in the 8-bit random number. Within each iteration, a qubit is allocated and manipulated using the Hadamard gate to create a superposition of states. The qubit is then measured, and if the result is One, the corresponding bit in the random number is set to 1. After each measurement, the qubit is reset and reused in next iteration of the loop.

The process is repeated until an 8-bit random number is generated. This number, unpredictable and truly random, is then returned as the result of the operation. This program demonstrates the power of quantum computing in generating true random numbers, leveraging the inherent randomness of quantum phenomena.

Conclusion

In this article, you learned how to use Q# to create a true random number generator, which can produce unpredictable outcomes by exploiting the quantum phenomena of superposition and measurement. You also learned how to use the Hadamard gate and the M function to manipulate and measure qubits.