The QPath Blog


q_rEngineer_QP_APP_b

Using QuantumPath® Q Provider Factory with quantum programs deployed with the Amazon Braket SDK

Q Provider Factory APP, as a QuantumPath® technology, allows refactoring the quantum provider of a program developed in a given manufacturer’s SDK. This opens up an interesting range of possibilities for exploring alternatives while minimizing risks and time.

One of the technologies supported by Q Provider Factory is Amazon Braket SDK, Python development software provided by Amazon, an open source library that allows the development of quantum circuits and interact with different devices and simulators within the AWS quantum ecosystem.

Next, we will show how to use, step by step, Q Provider Factory to run, through QuantumPath®, a quantum program implemented with Amazon Braket SDK. The example chosen for the article is an implementation of the Simon’s algorithmwhose source code can be found in Amazon Braket’s GitHub repository.

Illustration 1: Simon’s algorithm in the Amazon Braket GitHub repository

 

In the first Q Provider Factory article published in August 2023, available on The QPath® Blog, the steps to follow to refactor a quantum application were already introduced. Let’s see that following the same steps, it is really easy to introduce new providers and exploit CORE functionalities to investigate new contexts.

It is important to remember that the QuantumPath® solution is the one that determines the link between quantum assets and quantum providers. Its role is to act as a provider of “connection chains” to execute circuits in the different linked providers. As a previous step, if we already have a solution linked to AWS, we can reuse it or generate a new one by preparing its connections to the available simulators or QPUs.

Illustration 2: QuantumPath solution with some quantum execution devices enabled

 

Uso de Q Provider Factory

·       Installl QrEngineer SDK.

To work with Q Provider Factory, it is necessary to install QrEngineer SDK if we did not already have it, for which it is necessary to execute the following instruction in a command console:

 pip install QrEngineerSDK

 Note: since these are Beta products, it is advisable to update the libraries if you already have them installed.

pip install –-upgrade QrEngineerSDK

 

·       Identify the main file of the quantum program to be integrated with QuantumPath®, in this case simon_algorithm.py.

 

·       Identify the line where the creation of the Circuit object with the definition of the quantum circuit ends and delete the code used to launch the experiment against the local simulator.

n = len(s)

circ = Circuit()

# Apply Hadamard gates to first n qubits

circ.h(range(n))

# Now apply the Oracle for f

circ.simons_oracle(s)

# Apply Hadamard gates to the first n qubits

circ.h(range(n))

print(circ)    # get number of qubits

                                                                     |                                                                    

task = device.run(circ, shots=4*n)

result = task.result()

counts = result.measurement_counts

plt.bar(counts.keys(), counts.values());

plt.xlabel(‘bit strings’);

plt.ylabel(‘counts’);

plt.xticks(rotation=90)

plt.show()

 

·       Add after that line, the Q Provider Factory code needed to transpile the circuit and run it through QuantumPath®: 

o   Add an import statement to the QProviderFactory class of the SDK.

 from QrEngineerSDK import QProviderFactory

 

o   Instantiate the QProviderFactory object, specifying the QuantumPath® credentials.

qprovider = QProviderFactory(user = ‘***qpath user***’, passwordSHA256=’***sha-256 encrypted password***’)

 

o   Transpile the original circuit to a QuantumPath® gate circuit, using the transpileAmazonCircuit instruction.

 

qpathCircuit = qProvider.transpileAmazonCircuit(circuit)

 

o   Launch the execution of the circuit through QuantumPath®, providing the solution identifier, the quantum device, the namespace, the name of the circuit and the number of executions or samples to be carried out.

 

qpathSolutionID = 10223
qpathDeviceID = 14 #AMAZON BRAKET 25qbits Local Simulator
qpathCircuitName = ‘SIMONS_ALGORITHM’
qpathNamespace = ‘TEST’
qpathNumRepeats = 100
result =
qprovider.executeQPathCircuit(qpathSolutionID,qpathDeviceID,qpathCircuitName,qpathNamespace,qpathNumRepeats,qpathCircuit)

Note: As mentioned above, both the solution id and the quantum device id can be obtained from the information page of the solution itself in QuantumPath®. 

Once the circuit has been executed through QuantumPath®, it is necessary to recover the histogram resulting from the execution, to perform the aggregation of the results ignoring the measurement in the last qubits, beyond the n digits of the string, as in the original secret (n) example:

 

new_results = {}

histogramValues = list(qpathResult.histogram.values())[0]

for bitstring, count in histogramValues.items():        

# Only keep the outcomes on first n qubits

   trunc_bitstring = bitstring[:n]

   # Add the count to that of the of truncated bit string

new_results[trunc_bitstring] =
new_results.get(trunc_bitstring, 0) + count

The rest of the classical post-processing performed in the original example remains unchanged. 

Once the refactored code has been executed, the user can access the QuantumPath® platform and visualize the circuit and flow automatically generated by the SDK in the indicated solution and check the result of the execution on the platform.

Illustration 3: Circuit generated with QrEngineer Python SDK, seen in QuantumPath®

 

In addition, as can be seen in the following illustrations, the development and execution tasks can be carried out as deemed appropriate with the circuit adapted to QPath®.

Illustration 4: Editing the “SIMON_ALGORITHM” circuit with Q Assets Composer® 

Illustration 5: QuantumPath® Runtime Dashboard

 

Once Braket is adapted and operational within the QuantumPath® ecosystem, a new path will open in the exploration of the quantum program under construction, with all the advantages that are added as added value thanks to this ecosystem. The developer will be able to continue research with Braket at the same time as being able to use QuantumPath®, which can help him to simplify the path to achieve his/her goal: the applied quantum advantage.

 

Using Q Provider Factory Web UI

Q Provider Factory Web UI allows to accelerate the quantum source adaptation process mentioned in the previous point, assisting the user, in a guided way, in the refactoring of the original program code.

To do this, the user must follow the following steps:

·       Access the tool and authenticate with their QuantumPath® credentials.

·       Create a new project or access an existing one.

Illustration 6: Q Provider Factory Project Catalog.

 

·       Attach the source code of the circuit or quantum program.

For this example, the part of the circuit code that is implemented in a Jupyter Notebook file has been exported to the Python file simons_algorithm.py. Both files, simons_algorithm.py and simons_util.py have been compressed into a .zip file for uploading to the tool.

Illustration 7: Form for adding a quantum circuit.

Illustration 8: Quantum circuit code added to a project.

 

·       From the “Code migrations” tab, generate new integration code with QuantumPath®, indicating:

o   Namespace and name of the circuit to be created in QPath®.

o   QPath® solution where to place the circuit.

o   Quantum device where to launch the execution of the circuit.

Illustration 9: Form to generate the code for a new integration with QPath®.

 

·       From the source code, select the main program file and the line where the Q Provider Factory code will be inserted.

Illustration 10: Selecting the location where the Q Provider Factory code is to be inserted.

 

·       Save the generated code and download it for local execution.

Illustration 11: Code generated by the tool

Illustration 12: List of generated migrations, with the option to download the code.

 

·       To complete the example, it is necessary to edit the main simons_algorithm.py file and make the following changes: 

o   Locate the line where the Q ProviderFactory object is initialized and add the password:

§  Base64 encoded:

qprovider = QProviderFactory(user = ‘***quantum path user***’, passwordB64=’***Base64-encoded password***’)

§  SHA256 encrypted:

provider = QProviderFactory(user = ‘***quantum path user***’, passwordSHA256=’***sha-256 encrypted password***’)

 

o   Locate the part where the results are aggregated, and replace it with the following code: 

new_results = {}

histogramValues = list(qpathResult.histogram.values())[0]

for bitstring, count in histogramValues.items():        

# Only keep the outcomes on first n qubits

   trunc_bitstring = bitstring[:n]

   # Add the count to that of the of truncated bit string

new_results[trunc_bitstring] =       
                new_results.get(trunc_bitstring, 0) + count

By launching the execution of the program from a local command console, you can see the result of the integration and execution against QPath®:

Illustration 13: Command console executing the modified “SIMONS_ALGORITHM” program.

 

Having done this, as shown in the Q Provider Factory section, the developer will be able to access, visualize and work without limitations with the implementation of the “Simon’s algorithm” from the QuantumPath® platform.

 

Summary

QuantumPath® is a Full-Stack platform for quantum software development and lifecycle control of hybrid quantum/classical systems: from the creation of the quantum algorithm, through its development, testing and implementation, integration, deployment, and reuse. A platform designed to enable the engineering and lifecycle of hybrid quantum software.

In this article, as we have already introduced in previous articles published in The QPath® Blog, the path opened by Q Provider Factory -and the product’s guided services-, make it possible for the user to establish a turning point in his quest for quantum advantage. Today, advances in quantum computing open up new avenues of exploration due to the growing number of technologies and providers.  Often, this makes it unfeasible to select the best provider for a given problem to solve or to justify determining which one is best suited for the solution, due to the cost of researching and learning from all these avenues. 

Thanks to QPath®’s Q Provider Factory adapters, the few-step task of adapting the Simon’s algorithm to QuantumPath® without altering the user’s Braket experience offers a new added value by making available not only new tools but also a new platform that makes it possible to evaluate the quantum advantage by opening up new possibilities. Continuing along the same line, combining forces or opening up new ones, will be just one more option on the road ahead. But the option exists and is viable.

As we have seen, once the Simon’s algorithm has been adapted to the platform, it will enter the life cycle, as a native element, of the rest of the QPath® capabilities (QAgnostic, QHybrid, QQuality, QManagement, QBusiness and QWorkforce) and all its managed services. This offers other possibilities of exploitation and evolution of the development adapted to QPath®, by being able to use in its life cycle the wide set of tools, services, methods, and processes of the platform designed from the perspective of Quantum Software Engineering.