The QPath Blog


Practical Quantum Computing with QuantumPath®: 3-qubit random number generator

Authors

José Luis Hevia
aQuantum CTO

Alonso Martin-Toledano
aQuantum Software Specialist

The article on QuantumPath®qSOA®: technology for dynamic integration of quantum-classical hybrid software systems highlights the importance of quantum-classical hybrid system integration to achieve industry-ready hybrid solution status. In that article, a part of a simple QRNG (Quantum Random Number Generator) algorithm is given as an example.

In this article we are going to present the complete example, a complete proof of concept, in which a QRNG circuit will be integrated into a classic software application. The classic client will be responsible for calling the quantum circuit and collecting and post-processing the data. In QuantumPath® this process will be carried out in an agnostic way, starting from the design of the circuit (which once designed and available in QPath® can be reused whenever the client needs it), its control flow, and the encoding of the result, and the client will be able to launch the execution on any of the quantum computers available on the platform in a totally transparent way.

 

Random and pseudo-random numbers

Random numbers are present in multiple elements that surround us, from the behavior of nature itself to computer programs. In this second case, they take on special relevance as they are necessary for various application fields, such as statistical analysis, security systems, or the choice of an element belonging to a set.

A clear example of this can be found in a draw, such as a lottery, in which users have a series of numbered ballots which, if one of them coincides with a randomly generated value, will be awarded. There is no doubt that, if the number of the winning ticket is obtained by a traditional mechanical method, such as introducing balls into a container and extracting them one by one, a perfectly random number will be obtained. However, despite what you may think at first, this is not the same on a computer.

Although there are functions that appear to generate random numbers, they do not generate truly random numbers. This is due to the deterministic nature of the classical computer itself, which does nothing other than, given some inputs, carry out a series of previously defined operations to create an apparently random number as output. For the generation of numbers, different parameters can be chosen to operate with them and try to obtain a value that is as random as possible; such as the time at which the execution is carried out, the PC’s serial number, or the system language. However, if the specific values of those parameters (seed values) were found and a number was generated again, it would be exactly the same as the previous one. Regardless of the number of times the program was executed. We have assumed this behavior/result as usual because that is how classical computers work.

In this situation, if you want to get a truly random number, you cannot resort to a deterministic generator, so you must opt for the use of characteristic behaviors of nature itself, such as electrons and, therefore, of the probabilistic inherent to quantum computing.

 

Random number generation with quantum computing

While in classical computing the basic unit of information is the bit (with binary states 0 and 1), in quantum computing there is the qubit, which can be found in states 0, 1, or a linear combination of both called superposition.

Knowing this and that when measuring a qubit in superposition it collapses to a base state of 0 or 1, a random number can be generated by applying a Hadamard gate and measuring. The dimension of the number to be obtained will depend on the number of qubits used following the logic of 2n, being the base the two possible states after the measurement (0 or 1), and  the number of qubits used. As shown in the following table:

Practical example with QuantumPath®

To illustrate what has been explained above, although in QPath® the maximum limit of qubits is determined by the capacity of each computer or quantum simulator to be used, to simplify the example the creation of a number of 3 qubits will be shown, that is, a completely random value between 0 and 7.

Once the solution has been created in QuantumPath®, it should contain the circuit shown below. Where three qubits are used to which a Hadamard gate is applied to initialize them in a superposition state and perform the measurement.

QRNG 1

Figure 1 Random quantum circuit

After this, the circuit flow describes the behavior it will have at the execution level. As quantum technologies are based on probability, it is common to launch the circuit numerous times to obtain a representative result. However, in this case, we do not want to execute a complex algorithm in which there may be intermediate failures whose result may be affected, but rather a number is searched for only once.

We could make an analogy with rolling a die. If you want to get one of the six numbers it contains, you could roll it 100 times and keep the number that has been repeated the most or do it only once (a single execution of the circuit), and select that result of a random number as valid.

In the case of a single iteration, we can see how each time this operation is performed, the number generated is different, obtaining in the first the number in binary 000, equivalent in decimal to 0, while in the second the 101, or in decimal the 5.

Telemetria1

Figure 2 RNG Histogram result from single shot

On the other hand, in the case that you want to obtain a set of random numbers, and the order in which they are received does not matter, you can execute the same circuit with a certain number of executions, as in the following example where 100 are sought random numbers between 0 and 7. Observing this time all the possibilities of numbers from 000 to 111, together with the number of times that it has come out as a result of random generation.

Figure 3 RNG Histogram result from multiple shots

 

Practical example with qSOA®

Thanks to the functionalities of QuantumPath® and the flexibility of qSOA®, there will be multiple contexts in which to develop hybrid systems that can use truly random numbers. In this article, we will focus on two possible ways to exploit the problem assets:

1)    Using an assisted graphical design of the circuit and its flow. Run it and validate the results graphically. And then launch the execution from the rich client developed in Python and exploit the results.

2)    From the rich client itself, dynamically generate the assets needed to compose the assets and then run them.

Let’s implement the proposed example.

The solution can be integrated as a hybrid classical-quantum system thanks to qSOA® and, as will be shown below, through the QuantumPath® SDK in Python. All this within an environment such as a Jupyter Notebook. As a prerequisite, we must have the QuantumPathQSOAPySDK package installed and have access to the QPath® platform. It is important to note that, to work with QPath®, you will only need that package and a Python work environment. Nothing more. Since all potential vendors of quantum technology are the responsibility of QuantumPath®, the developer doesn’t have to worry about it thanks to its agnostic capability.

To access QuantumPath®, if you do not have a subscription, we recommend that you sign up for the free developer subscription. For more information on how to prepare the environment, access the product documentation.

 

Prepare Everything: The QuantumPath® solution

Before starting the process of creating the circuit itself, the solution that will house the circuit and its flow must be prepared. The different providers of quantum machines and simulators available in QuantumPath® will also be selected. To subsequently run the applications created in any of them without having to worry about the different natures of each architecture. Carrying out this whole process in a completely agnostic way.

QRNG 4

Figure 4 Quantumpath® solution details

 

Context 1: visual development of assets. Integration with qSOA®

In this context, the creation of the circuit and the flow can be done from the QuantumPath® interface in an explicit and assisted way with the designers, as shown below:

QRNG 5

Figure 5 QuantumPath® Circuit design

Continuing with the flow associated with the circuit, checking the box to publish in qSOA®:

QRNG 6

Figure 6 QuantumPath® Flow Design

This context provides the advantage that the entire design is assisted by QuantumPath® and can be tried and tested with its tools. In such a way that the final product only has to be exploited as a use case by the classic system with which we will integrate it.

Finally, the integration with qSOA® arrives, where we will prepare the execution and obtain the results. Instead of writing a very bare code, let’s write a “rich client” that will be prepared for other circuits and flows, in such a way that the data that may vary can be requested on the screen or collected from any other type of process…

# PLATFORM CONNECTION

from QuantumPathQSOAPySDK import QSOAPlatform

qsoa QSOAPlatform(input(‘Username’), input(‘Password(SHA-256)’))

# REQUEST THE SOLUTION

solutionList = qsoa.getQuantumSolutionList()

solutionList = {y: x for x, y in solutionList.items()}

print(‘Solution List:’, list(solutionList.keys()))

solution solutionList[input(‘SolutionName: ‘)]

# SHOW THE DIFFERENT QUANTUM DEVICES OF THE
SOLUTION

deviceList = qsoa.getQuantumDeviceList(solution)

deviceList = {y: x for x, y in deviceList.items()}

print(‘Device List:’, list(deviceList.keys()))

device =
deviceList
[input(‘Device Name: ‘)]

# REQUEST THE FLOW

flowList = qsoa.getQuantumFlowList(solution)

flowList = {y: x for x, y in flowList.items()}

print(‘Flow List:’, list(flowList.keys()))

flow =
flowList
[input(‘Flow Name: ‘)]

# EXECUTION

applicationName = input(‘Application Name: ‘)

application = qsoa.runQuantumApplicationSync(applicationName, solution, flow, device)

# PROCESS RESULTS

execution = qsoa.getQuantumExecutionResponse(application)

qsoa.representResults(execution)

 

Context 2: dynamic development of assets, full integration with qSOA®

Similar to context 1, but in this case, we will define the assets dynamically. The dynamic option has the advantage that both its initial design and subsequent refactorings can be carried out by modifying the circuit and/or the flow based on business requirements, if necessary.

Regarding the previous program, the connection to the platform is made similarly…

 

 

# PLATFORM CONNECTION

from QuantumPathQSOAPySDK import QSOAPlatform

qsoa = QSOAPlatform(input(‘Username’), input(‘Password(SHA-256)’))

# REQUEST THE SOLUTION

solutionList = qsoa.getQuantumSolutionList()

solutionList = {y: x for x, y in solutionList.items()}

print(‘Solution List:’, list(solutionList.keys()))

solution = solutionList[input(‘Solution Name: ‘)]

 

 

and from here comes the changes: the body of the circuit is developed through the API functions assigned to it, building the visual language in the following way:

 

# CREATE ASSET: circuit and main flow

# DEVELOPMENT OF THE CIRCUIT VL

circuit = qsoa.CircuitGates()

circuit.h([0, 1, 2])

circuit.measure()

 

circuitName = input(‘Circuit Name: ‘)

circuitNamespace = input(‘Circuit Namespace: ‘)

circuitDescription = input(‘Circuit Description: ‘)

circuitBody = circuit

circuitType = ‘GATES’

circuitLevel = ‘VL’

 

qsoa.createAssetSync(solution, circuitName, circuitNamespace, circuitDescription, circuitBody, circuitType, circuitLevel)

 

# CREATE FLOW ASSET

flowName = input(‘Flow Name: ‘)

flowNamespace = circuitNamespace

flowDescription = input(‘Flow Description: ‘)

flowBody = ‘ABSTRACT(START,);CIRCUIT(‘ + flowNamespace + ‘.’ + circuitName + ‘);ABSTRACT(END,);’

flowLevel = ‘IL’

 

assetFlow = qsoa.createAssetFlowSync(solution, flowName, flowNamespace, flowDescription, flowBody, flowLevel, publish=True)

 

flow = assetFlow.getIdAsset()

 

Finally, we proceed to the execution in the quantum computer, the collection of results, and their graphic representation. As can be seen, again it is a part common to all contexts. This time it will run twice: each time indicating the quantum device and showing the number obtained.

# QUANTUM DEVICES

deviceList = qsoa.getQuantumDeviceList(solution)

deviceList = {y: x for x, y in deviceList.items()}

print(‘Device List:’, list(deviceList.keys()))

 

for i in range(2):

    # EXECUTION

    applicationName = input(‘Application Name: ‘)

    device = deviceList[input(‘Device Name: ‘)]

    application = qsoa.runQuantumApplicationSync(applicationName, solution, flow, device)

 

    # RESULTS

    execution = qsoa.getQuantumExecutionResponse(application)

 

    print(‘Random Numer:’, int(list(list(execution.getHistogram().values())[0].keys())[0], 2))

    qsoa.representResults(execution)

 

As you can see in the image above, completely random numbers are obtained again. Thus, showing how the quantum world can be integrated with the classical one and offer real solutions to problems that classical computing cannot solve satisfactorily.

It should be noted that all the assets generated with qSOA® are stored in the solution and can be managed from the visual environment that it provides to the Q Assets Compositor® platform. This allows you to continue managing the life cycle graphically as well. The asset dictionary can be exploited with all the known use cases of classical computing, and its management will be defined by the business.