Quantum Random Number Generators (QRNGs) using Streamlit on AWS

Shadab Hussain
9 min readFeb 15, 2021
Img Src- https://physicsworld.com/wp-content/uploads/2014/05/PW-2014-05-16-Johnston-quantum.jpg

Let’s understand first, what are Random Numbers?

Random number generation is a process that, often by means of a random number generator (RNG), generates a sequence of numbers or symbols that cannot be reasonably predicted better than by a random chance.

Random number generators can be truly random:
1. Hardware Random Number Generators (HRNGs)- Generate random numbers as a function of the current value of some physical environment attribute that is constantly changing in a manner that is practically impossible to model
2. Pseudo-Random Number Generators (PRNGs)- Generate numbers that look random, but are actually deterministic, and can be reproduced if the state of the PRNG is known.

Later in this blog, we will demonstrate HRNG through a web app on a real Quantum Computer provided by IBM, and on ANU Qrng & Microsoft Simulator.

Applications of Random Numbers

Randomness has many uses in science, art, statistics, cryptography, gaming, gambling, and other fields. For example, random assignment in randomized controlled trials helps scientists to test hypotheses, and random numbers or pseudorandom numbers help video games such as video poker. Weaker forms of randomness are used in hash algorithms and in creating amortized searching and sorting algorithms.
Few of the applications are-
Cryptography: A ubiquitous use of unpredictable random numbers is in cryptography which underlies most of the schemes which attempt to provide security in modern communications (e.g., confidentiality, authentication, electronic commerce, etc.). For example, if a user wants to use an encryption algorithm, it is best that they select a random number as the key. The selection must have high entropy (i.e., unpredictability) to any attacker, thus increasing attack difficulty. With keys having low entropy (i.e., relatively easily guessable by attackers), security is likely to be compromised.
Games: Unpredictable (by the humans involved) numbers (usually taken to be random numbers) were first investigated in the context of gambling developing, sometimes, pathological forms like apophenia. Many randomizing devices such as dice, shuffling playing cards, and roulette wheels, seem to have been developed for use in games of chance. Modern electronic casino games contain often one or more random number generators which decide the outcome of a trial in the game. Even in modern slot machines, where mechanical reels seem to spin on the screen, the reels are actually spinning for entertainment value only.
Scientific Simulations: In many scientific and engineering fields, computer simulations of real phenomena are commonly used. When the real phenomena are affected by unpredictable processes, such as radio noise or day-to-day weather, these processes can be simulated using random or pseudo-random numbers.

What is Quantum Computing?

Img Src- https://makeagif.com/gif/quantum-computers-explained-limits-of-human-technology-wyOwoK

Every day, we experience the advantages of classical computing. There are problems, however, that the systems of today will never be able to overcome. We don’t have enough computing capacity on Earth to solve problems above a certain scale and complexity. We need a new kind of computation to have a chance at addressing any of these issues. To construct states that scale exponentially with the number of qubits, or quantum bits, universal quantum computers exploit the quantum mechanical phenomenon of superposition and entanglement. All computing systems rely on a simple capacity to store and manipulate data. Individual bits are manipulated by current computers, which store information as binary 0 and 1 states. To manipulate knowledge, quantum computers exploit quantum mechanical phenomena. They rely upon quantum bits, or qubits, to do this.

Why Quantum?

Modern physics has shown us that there are really only two types of events that can happen in the universe: the unitary transformation of a quantum system, and quantum wavefunction collapse (i.e. measurement). The former being a totally deterministic process and the latter being a random one.
Indeed, all randomness in the universe (as far we know) is the result of the collapse of quantum systems upon measurement. In a sense, this is randomness in its purest form and the underlying source of it in any TRNG.

Introduction to Quantum Random Number Generators

Img Src- https://media.springernature.com/lw685/springer-static/image/art%3A10.1038%2Fnpjqi.2016.21/MediaObjects/41534_2016_Article_BFnpjqi201621_Fig1_HTML.jpg?as=webp

Quantum random number generators (QRNGs)
seek to be a new norm of generators of randomness, not only in cryptography but also in many other fields such as AI, Monte Carlo, such as simulations, sampling, etc.

In quantum mechanics, a system can be prepared in a superposition of the (measurement) basis states, as shown in the above figure. According to Born’s rule, the measurement outcome of a quantum state can be intrinsically random, i.e. it can never be predicted better than blindly guessing. Therefore, the nature of inherent randomness in quantum measurements can be exploited for generating truly random numbers. Within a resource framework, coherence can be measured similarly to entanglement. By breaking the coherence or superposition of the measurement basis, it is shown that the obtained intrinsic randomness comes from the consumption of coherence. In turn, quantum coherence can be quantified from intrinsic randomness.

This quantum random number generator, apart from its technical merit, would have fascinated Einstein. He would probably be happy that the theory of relativity led to the outcome; but the approach also suggests something about quantum theory, the random existence of which Einstein denied when he claimed that the world does not play dice with God. Perhaps not, but mankind has figured out how to create its own superior, quantum-based, and completely random dice.

There are Optical and Non-Optical Quantum Random Number Generators.

Optical Quantum Random Number Generators (OQRNGs): Most of the existing QRNGs are based on quantum optics. The inherent randomness in many parameters of the quantum states of light allows for a rich choice of implementations. Light from lasers, light-emitting diodes, or single-photon sources is a convenient and affordable substitute for radioactive material as a source of quantum randomness and there are many available detectors. Classification of OQRNGs based on the generation mechanism:

  • Branching path generators
  • Time of arrival generators
  • Photon counting generators
  • Attenuated pulse generators
  • Generators based on quantum vacuum fluctuations
  • Generators based on the phase noise of lasers
  • Generators based on amplified spontaneous emission
  • Generators based on Raman scattering
  • Generators based on optical parametric oscillators

Non-Optical Quantum Random Number Generators (NQRNGs): While quantum light offers a simple source of quantum randomness, there have also been proposals for quantum random number generators based on other physical systems. QRNG based on radioactive alpha decay and a random number generator that amplifies the electrons coming from nano-size emitters under an electric field are based on tunneling. Other quantum random number generators measure the state of atomic quantum systems, like trapped ions. QRNGs based on measurements on trapped ions, while slower than their optical counterparts, have an interesting application to device-independent quantum random number generation.
And there are many more proposed methods for NQRNGs.

Some of the Open-Source Quantum Development

Qiskit is one of the best and should not be missed out on. Qiskit is an open-source SDK for operating at the level of pulses, circuits, and device modules for quantum computers. It supports Random Number Generation using IBM Quantum Experience backends. The resulting raw numbers can then be passed to Cambridge Quantum Computing (CQC) randomness extractors to get higher-quality random numbers.

Also, Microsoft’s Q# (pronounced Q sharp) is a domain-specific programming language that supports an interface that enables delegation to pluggable backend hardware or to a quantum simulator and can be used for QRNGs.

ANU QRNG also offers true random numbers to anyone on the internet. The random numbers are generated in real-time in their lab by measuring the quantum fluctuations of the vacuum. The vacuum is described very differently in quantum physics and classical physics. In classical physics, a vacuum is considered as a space that is empty of matter or photons. Quantum physics however says that that same space resembles a sea of virtual particles appearing and disappearing all the time. This is because the vacuum still possesses zero-point energy. Consequently, the electromagnetic field of the vacuum exhibits random fluctuations in phase and amplitude at all frequencies. By carefully measuring these fluctuations, they are able to generate ultra-high bandwidth random numbers.

Steps to Build and Deploy a QRNG app using streamlit on AWS:

Here we will deploy the QRNG app using streamlit on AWS which will have different backend sources like IBMQ, Q#, ANU QRNG.

Step 1. Set up Amazon Free EC2 Instance

  • Before starting with the EC2 instance, we need to sign up with our email ID and set up the payment information on the AWS website. Works just like a simple sign-on.
  • Then go to AWS Management Console using https://us-west-2.console.aws.amazon.com/console and select “Launch a Virtual Machine” where we will deploy our Streamlit app.
  • In the first step, we will choose the AMI template for the machine. Here, I select the 20.04 Ubuntu Server since it is applicable for the Free Tier.
  • Now we will select the t2.micro instance which is eligible for the free tier and it has a single CPU instance with 512 MB RAM.
  • Do Next till we reach “6. Configure Security Group” tab. Here we need to add “Custom TCP Rule”, with Port Range:8501 (custom port used by Streamlit), and Source: Anywhere.
  • Next click on the “Review and Launch” and then on the “Launch” button to launch the instance. After clicking on Launch we need to create a new key pair to log in to this particular machine. Name the key pair as streamlit-ec2 and download it using the “Download Key Pair” button and then click on “Launch Instance”.
  • We can now see our instance is up and running
  • Select your instance, and copy the Public IPv4 DNS Address from the Networking tab.
  • After that run the following commands to ssh to the instance:

Step 2. Environment Setup for QRNG App

  • Now we have an ubuntu prompt for the virtual machine. Next, we need to set up this machine to run QRNG app.
  • We will first install miniconda and add its path to the environment variable.
  • Install the qhasrp package and IQ# (a kernel used by Jupyter and Python that provides the core functionality for compiling and running Q# operations)

Step 3. Run Streamlit on EC2

  • Clone QRNG app Git Repository to get the code and Install Dependencies
  • Below is the app code inqrng_final.py file:
  • Run streamlit on EC2

Now we can go to a browser and type the external URL to access your app. In my case the address is http://18.237.145.107:8501. QRNG app is up and running for playing around it.

This is just a basic idea combining the existing quantum tech stack for app development using Streamlit without worrying much about the frontend. Contributions or suggestions are more than welcome on Github.

--

--

Shadab Hussain

MLOps @themathcompany, #AWS Community Builder for #data, #DataScience, #Quantum, #MachineLearning