Use PennyLane with Amazon Braket
Hybrid algorithms are algorithms that contain both classical and quantum instructions. The classical instructions are ran on classical hardware (an EC2 instance or your laptop), and the quantum instructions are ran either on a simulator or on a quantum computer. We recommend that you run hybrid algorithms using the Hybrid Jobs feature. For more information, see When to use Amazon Braket Jobs.
Amazon Braket enables you to set up and run hybrid quantum algorithms with the assistance of the Amazon Braket PennyLane plugin, or with the Amazon Braket Python SDK and example notebook repositories. Amazon Braket example notebooks, based on the SDK, enable you to set up and run certain hybrid algorithms without the PennyLane plugin. However, we recommend PennyLane because it provides a richer experience.
About hybrid quantum algorithms
Hybrid quantum algorithms are important to the industry today because contemporary quantum computing devices generally produce noise, and therefore, errors. Every quantum gate added to a computation increases the chance of adding noise; therefore, long-running algorithms can be overwhelmed by noise, which results in faulty computation.
Pure quantum algorithms such as Shor’s (Quantum Phase Estimation example)
In hybrid quantum algorithms, quantum processing units (QPUs) work as co-processors for classic CPUs, specifically to speed up certain calculations in a classical algorithm. Circuit executions become much shorter, within reach of the capabilities of today’s devices.
In this section:
- Amazon Braket with PennyLane
- Hybrid algorithms in Amazon Braket example notebooks
- Hybrid algorithms with embedded PennyLane simulators
- Adjoint gradient on PennyLane with Amazon Braket simulators
- Using Hybrid Jobs and PennyLane to run a QAOA algorithm
- Run hybrid workloads with PennyLane embedded simulators
Amazon Braket with PennyLane
Amazon Braket provides support for PennyLane
The PennyLane library provides interfaces to familiar machine learning tools, including PyTorch and TensorFlow, to make training quantum circuits quick and intuitive.
-
The PennyLane Library -– PennyLane is pre-installed in Amazon Braket notebooks. For access to Amazon Braket devices from PennyLane, open a notebook and import the PennyLane library with the following command.
import pennylane as qml
Tutorial notebooks help you get started quickly. Alternatively, you can use PennyLane on Amazon Braket from an IDE of your choice.
-
The Amazon Braket PennyLane plugin — To use your own IDE, you can install the Amazon Braket PennyLane plugin manually. The plugin connects PennyLane with the Amazon Braket Python SDK
, so you can run circuits in PennyLane on Amazon Braket devices. To install the the PennyLane plugin, use the following command.
pip install amazon-braket-pennylane-plugin
The following example demonstrates how to set up access to Amazon Braket devices in PennyLane:
# to use SV1 import pennylane as qml sv1 = qml.device("braket.aws.qubit", device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1", wires=2) # to run a circuit: @qml.qnode(sv1) def circuit(x): qml.RZ(x, wires=0) qml.CNOT(wires=[0,1]) qml.RY(x, wires=1) return qml.expval(qml.PauliZ(1)) result = circuit(0.543) #To use the local sim: local = qml.device("braket.local.qubit", wires=2)
For tutorial examples and more information about PennyLane, see the Amazon Braket examples repository
The Amazon Braket PennyLane plugin enables you to switch between Amazon Braket QPU and embedded simulator devices in PennyLane with a single line of code. It offers two Amazon Braket quantum devices to work with PennyLane:
-
braket.aws.qubit
for running with the Amazon Braket service’s quantum devices, including QPUs and simulators -
braket.local.qubit
for running with the Amazon Braket SDK’s local simulator
The Amazon
Braket PennyLane plugin is open source. You can install it from the PennyLane Plugin GitHub repository
For more information about PennyLane, see the documentation on the PennyLane website
Hybrid algorithms in Amazon Braket example notebooks
Amazon Braket does provide a variety of example notebooks that
do not rely on the PennyLane plugin for running hybrid algorithms.
You can get started with any of these
Amazon Braket
hybrid example notebooks
The Amazon Braket example notebooks rely on the Amazon Braket Python SDK
You can explore Amazon
Braket further with our example notebooks
Hybrid algorithms with embedded PennyLane simulators
Amazon Braket Hybrid Jobs now comes with high performance
CPU- and GPU-based embedded simulators from
PennyLanelightning.qubit
simulator, the
lightning.gpu
simulator accelerated using NVIDIA’s
cuQuantum library
With Hybrid Jobs, you can now run your variational algorithm code using a combination of a classical co-processor and a QPU, an Amazon Braket on-demand simulator such as SV1, or directly using the embedded simulator from PennyLane.
The embedded simulator is already available with the Hybrid Jobs container, you simply need to decorate your main Python
function with the @hybrid_job
decorator. To use the PennyLane lightning.gpu
simulator, you also
need to specify a GPU instance in the InstanceConfig
as shown in the following code snippet:
import pennylane as qml from braket.jobs import hybird_job from braket.jobs.config import InstanceConfig @hybrid_job(device="local:pennylane/lightning.gpu", instance_config=InstanceConfig(instanceType="ml.p3.8xlarge")) def function(wires): dev = qml.device("lightning.gpu", wires=wires) ...
Refer to the example notebook
Adjoint gradient on PennyLane with Amazon Braket simulators
With the PennyLane plugin for Amazon Braket, you can compute gradients using the adjoint differentiation method when running on the local state vector simulator or SV1.
Note: To use the adjoint differentiation method, you must specify diff_method='device'
in your qnode
, and not
diff_method='adjoint'
. See the following example.
device_arn = "arn:aws:braket:::device/quantum-simulator/amazon/sv1" dev = qml.device("braket.aws.qubit", wires=wires, shots=0, device_arn=device_arn) @qml.qnode(dev, diff_method="device") def cost_function(params): circuit(params) return qml.expval(cost_h) gradient = qml.grad(circuit) initial_gradient = gradient(params0)
Note
Currently, PennyLane will compute grouping indices for QAOA Hamiltonians and use them to split the Hamiltonian into multiple expectation values. If you want to use SV1’s adjoint differentiation capability when running QAOA from PennyLane, you will need reconstruct the cost Hamiltonian by removing the grouping indices, like so: cost_h, mixer_h = qml.qaoa.max_clique(g, constrained=False)
cost_h = qml.Hamiltonian(cost_h.coeffs, cost_h.ops)