AWS Quantum Technologies Blog

Speeding up hybrid quantum algorithms with parametric circuits on Amazon Braket

Today, we are excited to announce improvements to the task-processing speed of Amazon Braket and our support for parametric compilation on all gate-based, superconducting quantum processing units (QPUs) from Rigetti Computing.

Today’s launch enables up to 10x faster runtime performance for algorithms that use Amazon Braket Hybrid Jobs with QPUs from Rigetti.

In this post, we’ll explain why that’s important, and show you an example to help you visualize the impact of this new feature.

Background

The quality of the quantum processor (the coherence of qubits and quantum operations on a device) is the most important characteristic of quantum computers today and essential for researching state-of-the-art algorithms. But as devices improve in quality, a second metric becomes increasingly important for customers: throughput performance. Due to device drift, better throughput doesn’t just mean you can get to your results faster: it can translate into improved result quality, too. Today, we’re excited to announce faster task processing speeds on all gate-based QPUs on Braket with no code changes whatsoever.

Throughput performance is even more important for variational quantum algorithms, which  iteratively adjust the weights of a parameterized circuit Ansatz, often needing hundreds of circuit executions to converge. For such algorithms, you can get an additional boost in throughput performance using parametric compilation. This feature is now available to Braket customers with minimal code changes and no additional cost.

How does this work?

Variational algorithms like variational quantum eigensolver (VQE), quantum approximate optimization algorithm (QAOA), and quantum machine learning algorithms are promising candidates to be the first use cases for useful quantum computing, and active areas of research today.

These algorithms typically involve an iterative procedure where you submit one or more circuits to a quantum computer, and run a classical optimization step based on the results of executing the circuits, to produce a revised set of parameters to use in the next iteration. This process is repeated using the same set of circuits, but with updated parameters, until the objective function converges. In 2021, we launched Braket Hybrid Jobs to make running variational quantum algorithms easier and more performant. The Hybrid Jobs feature can help minimize the overall runtime of your algorithm by providing priority access to your chosen QPU, enabling your workloads to avoid wait times in queue. It enables you to run workloads as fire-and-forget: simply submit your hybrid job, and Braket executes your workload when the QPU becomes available.

With today’s launch, we are taking this one step further. One of the major runtime bottlenecks in executing variational algorithms is the computationally-intensive compilation step. Every time you execute a task on a QPU, the circuit is validated, and the compiler maps the input to the native gates and topology of the target QPU, to achieve the highest quality results. However, for circuits with higher qubit counts or more gate operations, this process becomes compute intensive, and can quickly dominate the total runtime of the circuit. This situation is exacerbated for variational algorithms which involve multiple invocations of this compilation step at each iteration of your algorithm, even if the circuit being executed is the same each time.

This is where parametric compilation comes in. Variational algorithms often have a nice property where between iterations, the circuit parameters are updated, but the underlying circuit remains the same. This means that you can compile the circuit just once, cache it, and simply feed fresh parameters to this compiled circuit at runtime. You can thereby avoid the need to recompile the entire circuit, from scratch, every time.

Amazon Braket is now set up to automatically handle the heavy lifting for compiling your input circuit, caching that circuit, and reusing it at runtime with new parameters when you use the Hybrid Jobs feature to run your variational workloads. Note that you can also use this feature to speed up your quantum tasks. We designed it to cache the circuit for the duration of the availability window, helping you avoid expensive re-compilation as you are experimenting and designing your circuits.

Figure 1 shows how this can remove compilation from the critical runtime path of the circuit – enabling you to save valuable time. What’s more, all these changes are applied by default and at no additional cost.

Figure 1: Schematic showing the breakdown of the runtime for a hybrid workload with four tasks. Top: total runtime with four compilation steps in the critical path. Bottom: runtime after parametric compilation. Note: this figure is an illustration only, and only shows the most computationally expensive compilation step: mapping an abstract circuit to the native gates and topology of the QPU. We haven’t shown downstream compilation steps like compilation to pulses here.

Figure 1: Schematic showing the breakdown of the runtime for a hybrid workload with four tasks. Top: total runtime with four compilation steps in the critical path. Bottom: runtime after parametric compilation. Note: this figure is an illustration only, and only shows the most computationally expensive compilation step: mapping an abstract circuit to the native gates and topology of the QPU. We haven’t shown downstream compilation steps like compilation to pulses here.

Getting Started with Parametric Compilation on Braket

It’s time to see this in action. You can use parametric compilation on supported Braket QPUs by submitting parametrized circuits through Amazon Braket Hybrid Jobs.

The code example below is a simplified example of the algorithm script of a hybrid job, and can be found in our example notebooks Github repository. The circuit has a RX gate with a single parameter theta. Instead of bounding the value of the parameter theta when the circuit’s defined, you can simply declare it as a free parameter. You can then supply the value of parameter theta together with the parametrized circuit at submission.

Amazon Braket is designed to automatically compile and cache the circuit for you when you run the first task as part of a Hybrid Job. For subsequent tasks that use the same circuit, as the hybrid job iterates, Braket is set up to automatically use the compiled circuit with updated parameters without the need to recompile the same circuit.

import os
from braket.aws import AwsDevice
from braket.circuits import Circuit, FreeParameter

def start_here():

    print("Initiating parametric job!")

    # Use the device declared in the job script
    device = AwsDevice(os.environ["AMZN_BRAKET_DEVICE_ARN"])

    num_iterations = 5
    
    theta = FreeParameter("theta")
    theta_value = 0.1 # current value of the parameter

    circuit = Circuit().rx(0, theta)
    
    for _ in range(num_iterations):
        result = device.run(circuit, shots=1000, inputs={"theta":   theta_value})
        theta_value = update_parameter(result, theta_value) # some optimization function

    print("Parametric job completed!")

Now that you know how to use parametric compilation with Amazon Braket Hybrid Jobs, let’s look at a more complex example: a Max Cut QAOA using PennyLane, a python library for differentiable programming of quantum computers. PennyLane comes built in with Amazon Braket, so no additional set up steps are needed.

You can learn more about this algorithm by referring to our example notebook. To use parametric circuits with PennyLane, all you need to do is specify the flag: parametrize_differentiable=True when instantiating the Braket device through PennyLane in your algorithm script, and Amazon Braket will handle the rest:

import pennylane as qml

qml.device(
       "braket.aws.qubit",
       device_arn=device_arn,
       wires=num_ wires,
       shots=shots,
       parallel=True,
       max_parallel=max_parallel,
       parametrize_differentiable=True,
)
 Figure 2: Speedup of each optimization step of a Max Cut QAOA algorithm running with parametric compilation with a Braket Hybrid Job, compared to running without using parametric compilation.


Figure 2: Speedup of each optimization step of a Max Cut QAOA algorithm running with parametric compilation with a Braket Hybrid Job, compared to running without using parametric compilation.

Figure 2 shows the performance gains we achieved from using parametric circuits with Hybrid Jobs, compared to simply submitting the circuit with the parameter values pre-specified. In the former case, Amazon Braket compiled and cached the circuit for reuse, whereas in the latter, the circuit was compiled at each iteration, resulting in a more than 9x speedup for the 10-qubit Max-Cut problem. Furthermore, the speedup depends on the complexity of the circuit: bigger circuits require more compilation time, which means that parametric compilation can unlock even greater savings by avoiding expensive recompilation steps.

Conclusion

In today’s era of noisy quantum hardware, getting the most out of quantum devices means putting performance first. Faster runtimes doesn’t just mean higher throughput: they can also minimize the impact of device drift to  yield higher quality results.

Our vision with Braket Hybrid Jobs was to give customers best-in-class performance for running the most complex hybrid workloads by providing priority access to the QPU, and minimizing wait times in queues for circuits executed within a job. But as quantum hardware improves, customers want even greater runtime performance for hybrid algorithms to push quantum computers to their limits.

In this blog, we announced two improvements to speed up runtimes for variational algorithms: first, quantum tasks submitted to Rigetti processors can now run faster with no code changes whatsoever. Second, we launched parametric compilation, or the ability to compile a parametrized circuit once, and re-run it many times and avoid subsequent compilation steps. We described how you can use it to speed up your variational algorithms at no additional cost on all superconducting gate-based quantum processors from Rigetti Computing in the US West (N. California) AWS region. To get started, check out our example notebooks[Create Your First Hybrid Job, and QAOA using PennyLane], and our service documentation for Amazon Braket.

If you’re an academic researcher,  interested in exploring variational quantum algorithms, apply for research credits through the AWS Cloud Credits for Research program.