Quantum task batching - Amazon Braket

Quantum task batching

Quantum task batching is available on every Amazon Braket device, except the local simulator. Batching is especially useful for quantum tasks you run on the on-demand simulators (TN1 or SV1) because they can process multiple quantum tasks in parallel. To help you set up various quantum tasks, Amazon Braket provides example notebooks.

Batching allows you to launch quantum tasks in parallel. For example, if you wish to make a calculation that requires 10 quantum tasks and the circuits in those quantum tasks are independent of each other, it is a good idea to use batching. That way, you don’t have to wait for one quantum task to be complete before another task begins.

The following example shows how to run a batch of quantum tasks:

circuits = [bell for _ in range(5)] batch = device.run_batch(circuits, s3_folder, shots=100) print(batch.results()[0].measurement_counts) # The result of the first quantum task in the batch

For more information, see the Amazon Braket examples on GitHub or Quantum task batching, which has more specific information about batching.

About quantum task batching and costs

A few caveats to keep in mind regarding quantum task batching and billing costs:

  • By default, quantum task batching retries all time out or fail quantum tasks 3 times.

  • A batch of long running quantum tasks, such as 34 qubits for SV1, can incur large costs. Be sure to double check the run_batch assignment values carefully before you start a batch of quantum tasks. We do not recommend using TN1 with run_batch.

  • TN1 can incur costs for failed rehearsal phase tasks (see the TN1 description for more information). Automatic retries can add to the cost and so we recommend setting the number of 'max_retries' on batching to 0 when using TN1 (see Quantum Task Batching, Line 186).

Quantum task batching and PennyLane

Take advantage of batching when you’re using PennyLane on Amazon Braket by setting parallel = True when you instantiate an Amazon Braket device, as shown in the following example.

device = qml.device("braket.aws.qubit",device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1",wires=wires,s3_destination_folder=s3_folder,parallel=True,)

For more information about batching with PennyLane, see Parallelized optimization of quantum circuits.

Task batching and parametrized circuits

When submitting a quantum task batch that contains parametrized circuits, you can either provide an inputs dictionary, which is used for all quantum tasks in the batch, or a list of input dictionaries, in which case the i-th dictionary is paired with the i-th task, as shown in the following example.

from braket.circuits import Circuit, FreeParameter, Observable from braket.aws import AwsQuantumTaskBatch # create the free parameters alpha = FreeParameter('alpha') beta = FreeParameter('beta') # create two circuits circ_a = Circuit().rx(0, alpha).ry(1, alpha).cnot(0,2).xx(0, 2, beta) circ_a.variance(observable=Observable.Z(), target=0) circ_b = Circuit().rx(0, alpha).rz(1, alpha).cnot(0,2).zz(0, 2, beta) circ_b.expectation(observable=Observable.Z(), target=2) # use the same inputs for both circuits in one batch tasks = device.run_batch([circ_a, circ_b], inputs={'alpha': 0.1, 'beta':0.2}) # or provide each task its own set of inputs inputs_list = [{'alpha': 0.3,'beta':0.1}, {'alpha': 0.1,'beta':0.4}] tasks = device.run_batch([circ_a, circ_b], inputs=inputs_list)

You can also prepare a list of input dictionaries for a single parametric circuit and submit them as a quantum task batch. If there is N input dictionaries in the list, the batch contains N quantum task. The i-th quantum task corresponds to the circuit executed with i-th input dictionary.

from braket.circuits import Circuit, FreeParameter # create a parametric circuit circ = Circuit().rx(0, FreeParameter('alpha')) # provide a list of inputs to execute with the circuit inputs_list = [{'alpha': 0.1}, {'alpha': 0.2}, {'alpha': 0.3}] tasks = device.run_batch(circ, inputs=inputs_list)