Tutorial: Qiskit-Braket provider overview#
Qiskit to Braket mapping#
[1]:
# pip install qiskit_braket_provider
Import all required classes and functions for this overview
[2]:
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.quantum_info import SparsePauliOp
from qiskit import transpile, QuantumCircuit
from qiskit.circuit.random import random_circuit
from qiskit.visualization import plot_histogram
from qiskit.algorithms.optimizers import SLSQP
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import BackendEstimator
from braket.aws import AwsQuantumJob
from qiskit_braket_provider import AWSBraketProvider, BraketLocalBackend
Access Braket devices from Qiskit#
AWSBraketProvider
class gives you a method backends
to access backends that are available through Braket SDK.
Let’s get available devices to use by Qiskit
[3]:
provider = AWSBraketProvider()
backends = provider.backends()
backends
[3]:
[BraketBackend[Aria 1],
BraketBackend[Aspen-10],
BraketBackend[Aspen-11],
BraketBackend[Aspen-8],
BraketBackend[Aspen-9],
BraketBackend[Aspen-M-1],
BraketBackend[Aspen-M-2],
BraketBackend[Aspen-M-3],
BraketBackend[Harmony],
BraketBackend[Lucy],
BraketBackend[SV1],
BraketBackend[TN1],
BraketBackend[dm1]]
For prototyping it is usually a good practice to use simulators to set up workflow of your program and then change it to real device. We can access local simulator by creating instance of class BraketLocalBackend
[4]:
local_simulator = BraketLocalBackend()
local_simulator
[4]:
BraketBackend[default]
We also get get cloud state vector simulator by using method get_backend
for provider.
[5]:
aws_statevector_simulator = provider.get_backend("SV1")
aws_statevector_simulator
[5]:
BraketBackend[SV1]
Of course we can query for specific real devices
[6]:
ionq_device = provider.get_backend("Harmony")
rigetti_device = provider.get_backend("Aspen-M-1")
ionq_device, rigetti_device
[6]:
(BraketBackend[Harmony], BraketBackend[Aspen-M-1])
And if you want to explore what is available by specific contraints, you can specify query arguments to backends
method of provider. Arguments are fully compatible with Braket’s get_device
method. See full doc here.
[7]:
online_simulators_backends = provider.backends(statuses=["ONLINE"], types=["SIMULATOR"])
online_simulators_backends
[7]:
[BraketBackend[SV1], BraketBackend[TN1], BraketBackend[dm1]]
Running circuits on AWS devices#
Let’s create circuit first. We will start with “Hello World” example :)
[8]:
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.draw()
[8]:
┌───┐ q_0: ┤ H ├──■── └───┘┌─┴─┐ q_1: ─────┤ X ├ └───┘
Run job agains selected device
[9]:
job = aws_statevector_simulator.run(qc, shots=10)
job
[9]:
<qiskit_braket_provider.providers.braket_job.AmazonBraketTask at 0x138166410>
Plot histogram
[10]:
plot_histogram(job.result().get_counts())
[10]:

Retrieve the measured state of each single shot.
[11]:
job.result().get_memory()
[11]:
['11', '11', '00', '11', '11', '11', '00', '00', '00', '00']
Now it’s time to run more complex circuits on devices we got in previous paragraphs.
We will start with generating random circuit and printing it out.
[12]:
circuit = random_circuit(6, 20, seed=42)
circuit.draw(fold=-1)
[12]:
┌────────────────────────┐ ┌─────────────────────────┐┌────┐ ┌───────────────────┐ ┌──────┐ ┌────────────────────┐┌───────────┐┌─────────────────────────┐ ┌──────────────┐ ┌───┐ ┌────────────┐ ┌────────────────────┐ ┌───┐ ┌──────────────────┐ q_0: ┤0 ├────■──────────────────────────────────┤1 ├┤ Sx ├───┤ U2(1.8116,4.2882) ├─────────────────────────────────────────────────────┤1 ├─────────────────────────────────────────X───────────────────────────────────────────────────X─┤ U2(0.56579,4.5387) ├┤ Rx(2.394) ├┤ U3(4.893,4.5044,2.8234) ├───────────────────────────────────┤0 ├───────┤ Y ├────────┤ Ry(3.7128) ├───────────────────────────■───────┤ U2(0.13579,5.1917) ├──┤ H ├──────────────────────────■────────────────────────■───────┤ R(4.1573,2.3478) ├─────────────────── │ │ │ ┌───┐┌────────────┐┌────────────┐│ │└─┬──┘ └───────────────────┘ ┌──────────────┐│ │ │ │ └────────────────────┘└───────────┘└──────┬────────────┬─────┘ ┌───┐ ┌─────┐ │ │ └─┬─┘ └────────────┘ ┌──────┐ │ └────────────────────┘ ├───┴┐ ┌──────────────┐┌────┐ │ ┌───┐ │ └──────────────────┘ q_1: ┤ (XX+YY)(4.7824,4.939) ├─■──┼─┤ X ├┤ Ry(6.0991) ├┤ Rx(1.1905) ├┤ ├──┼────────────────────────────────────────────────────────────────┤0 ├┤ ├─────────────────■───────────■───────────┼──────────────────────────────────────────────■────┼───────────■───────────────────────────────┤ Rx(1.8945) ├──────────────┤ Y ├────────────┤ Sdg ├───┤ ├─────────■─────────────────────────────────────┤1 ├──────┼─────────────■─────────────────┤ Sx ├─┤0 ├┤ Sx ├──┼────────┤ T ├───────────┼────────────────■───────────────────────────── │ │ │ │ └─┬─┘└──┬─────┬───┘└──┬─────┬───┘│ │ │ ┌─────────────────────────┐ │ ││ │┌──────────────┐ │ │ │ ┌────────────────────────────────┐ ┌───┐ │ │ ┌─────┴──────┐ └─────┬──────┘ ┌───────┴───┴────────┐ └─────┘ │ Rzx(5.3823) │ │ │ │ │P(5.8925) └─┬──┘ │ Rxx(4.5678) │└─┬──┘ │ ┌───┴───┴────┐ │ │ ┌─────────────────┐ q_2: ┤1 ├─┼──┼───┼─────┤ Sdg ├───────┤ Tdg ├────┤ (XX-YY)(5.6116,4.8907) ├──┼───┤1 ├────────────────■─────────────────┤ ├┤ Ecr ├┤0 ├─┼───────────┼───────────┼─┤ U(3.5136,1.9098,0.19363,2.744) ├─┤ X ├─────┼────┼─────┤ Ry(1.0491) ├──────────■───────────────────┼─────────────┤ U2(1.7105,0.60564) ├─■───────────┤ ├────■──────────────────────────────■───────────┤ ├──────┼─────────────■───────────────────■────┤1 ├──┼─────┼────┤ U1(1.9926) ├──────X────────────────┼──────────┤ R(1.985,4.8507) ├ └─────────┬────┬─────────┘ │ │ │ └──┬──┘ └─────┘ │ │ │ │ (XX-YY)(2.3277,2.9503) │┌───────────────┴────────────────┐│ Rxx(2.4346) ││ ││ Rxx(2.8835) │ │ │ │ └───────────────┬────────────────┘┌┴───┴─┐ │ │ └──┬──────┬──┘ │ │ └───────┬───┬────────┘ │ │ │ │U1(0.26147) ┌────────────┐ │ │ Ecr │ │ ┌──────┐ ┌───────┐├──────────────┤ │ │ ┌┴────────────┤ │ ┌─┴─┐ └─────────────────┘ q_3: ──────────┤ √X ├───────────┼──┼───■────────┼─────────────■───────┤ ├──┼───┤0 ├┤ U(4.3999,1.9627,5.2292,5.0565) ├┤ ├┤ ├┤1 ├─┼───────────┼───────────X─────────────────┼─────────────────┤ √Xdg ├───┼────┼────────┤1 ├─────────────■───────────────────┼─────────────────────┤ H ├──────────┼───────────┤1 ├────■───────────────┤ Ry(3.6509) ├─┼───────────┤ ├──────┼──────────────┤0 ├───────┤1 ├┤1 ├──■─────┼───┤ Ry(0.28218) ├──────┼──────────────┤ X ├─────────────────────────── └────┘ │ │ │ │ │ │ │ └──────┬────────────┬─────┘└────────────────────────────────┘│ ││ │└──────────────┘ │ │P(3.4782) │ └──────┘┌──┴──┐ │ │ Ecr │ │ │ ├───┤ │ └┬────────────┬┘┌──────────────────┐└────────────┘ │ │ │ │ │ Ecr │ │ ││ Rxx(4.8296) │ ┌─┴──┐└──────┬──────┘ │ └─┬─┘ q_4: ───────────────────────────■──┼────────────■─────────────┼───────┤0 ├──┼──────────┤ Rx(2.0472) ├────────────────────────────────────────┤1 ├┤0 ├─────────────────┼───────────■─────────────────────────────┼─────────────────────────┤ Sdg ├─┼────────┤0 ├─────────────■───────────────────┼─────────────────────┤ S ├──────────┼────────────┤ U1(4.7659) ├─┤ R(3.1038,2.0726) ├───────────────┼───────────┤0 ├──────┼──────────────┤1 ├───────┤0 Rccx ├┤0 ├──────┤ Sx ├───────┼─────────────X────────────────■───────────────────────────── ┌─────────────────┐ │ ┌─┴─┐ └─────────────────────────┘ │ └──┬──────┬──┘ └──────────────┘└──────┘ │P(3.5735) │ └─────┘ │ ┌──┴──────┴──┐ │ ┌──────┴───┴───────┐ │ZZ(1.1107) ├────────────┤ └──────────────────┘ │U1(2.1794) └──────┘┌─────┴──────┐ └──────┘ │ │└────┬───┬─────┘┌───┐ └────┘ │ ┌────────────┐ q_5: ───┤ R(0.59173,6.13) ├────────■────────────────────────┤ Y ├──────────────────────────────────■─────────────┤ √Xdg ├────────────────────────────────────────────────────────────────────────────────────■─────────────────────────────────────────■─────────────────────────────────X─────┤ Ry(0.1427) ├──────────────────────────────■──────────────┤ R(1.6705,6.0895) ├──■────────────┤ Rx(2.6126) ├────────────────────────────────────■───────────────────┤ Rx(3.0832) ├──────────────────────┤2 ├─────┤ H ├──────┤ Y ├──────────────■───────┤ Rx(3.2601) ├─────────────────────────────────────── └─────────────────┘ └───┘ └──────┘ └────────────┘ └──────────────────┘ └────────────┘ └────────────┘ └───────┘ └───┘ └───┘ └────────────┘
Here is a good point to talk about circuit transpilation.
Transpilation is the process of rewriting a given input circuit to match the topology of a specific quantum device, and/or to optimize the circuit for execution on present day noisy quantum systems.
Let’s transpile our generated circuit agains selected device.
[13]:
transpiled_circuit = transpile(circuit, backend=ionq_device, seed_transpiler=42)
transpiled_circuit.draw(idle_wires=False, fold=-1)
[13]:
global phase: 1.0121 ┌────────────┐ ┌───┐ ┌─────────────┐ ┌───┐┌─────────────┐ ┌────────────┐ ┌───┐┌─────────────┐ ┌───┐┌─────────────┐ ┌─────────┐ ┌───┐ ┌──────────┐ ┌───┐┌─────────────┐┌────────────┐┌──────────────┐ ┌───┐ ┌──────────────┐┌────────────┐┌─────────────┐ ┌──────────┐ ┌───┐ ┌──────────┐┌────────────┐ ┌───────────────┐ ┌────────────┐┌────────────┐ ┌────────────┐ ┌─────────┐ ┌────────────┐┌────────────┐┌─────────────┐ q_0 -> 0 ─┤ Rz(0.2266) ├──────────────────┤ X ├─────┤ Ry(-2.3912) ├─────┤ X ├┤ Rz(-0.2266) ├───────────────────────■───┤ Rz(2.9633) ├────────────────────────────────────────────────────────────────┤ X ├┤ Ry(-2.8058) ├───────────────┤ X ├┤ Rz(0.17834) ├──┤ Ry(π/2) ├───────────────────────────────────────────┤ X ├─┤ Rz(-π/4) ├────────────────┤ X ├┤ Rz(-2.2555) ├┤ Ry(1.3517) ├┤ Rz(-0.43523) ├─────────────────────┤ X ├────────────────────X───────────────────────────────────────────────────────────────────────────────────────────────────X─┤ Rz(-0.27086) ├┤ Ry(2.2172) ├┤ Rz(0.43785) ├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────■───────────────────────────────────────────■───────┤ Rz(-π/2) ├─────┤ X ├─────┤ Rz(-π/2) ├┤ Ry(-2.142) ├─────────────────────────────────────────────────────────────────────────────────────────────────────┤0 ├─┤ Rz(3.0787) ├┤ Ry(1.4504) ├─┤ Rz(2.8393) ├───────────────────────────────────────────────────────────────────────────────────────────────■─────────────────────■────────────────────────────────────────────────────────────────────────────────────────────────────────■──────────────────────────────────────────■─────────────────■──┤ Rz(π/4) ├───■──┤ Rz(2.3646) ├┤ Ry(2.1258) ├┤ Rz(-2.3646) ├──────────────────────────────────────────────────────────────────────── └┬──────────┬┘ ┌────────┐ └─┬─┘ └─────────────┘┌───┐└─┬─┘└─┬──────────┬┘┌────────┐ ┌───┐ │ ┌┴────────────┴┐┌────────────┐ └─┬─┘└─────────────┘ └─┬─┘└─────────────┘ └─────────┘ ┌──────────────┐ └─┬─┘ └──────────┘ └─┬─┘└─────────────┘└────────────┘└──────────────┘┌───┐┌─────────────┐ └─┬─┘ │ ┌───┐ ┌────────────┐┌───┐┌─────────────┐ ┌───┐ ┌────────────┐ │ └──────────────┘└┬─────────┬─┘└─────────────┘ ┌─────────────┐ ┌────────────────┐ ┌─────────┐ ┌───────┐ │ │ └──────────┘ └─┬─┘ ├─────────┬┘└───┬───┬────┘ ┌───┐│ │┌┴────────────┤└───┬───┬────┘┌┴────────────┴┐┌─────────┐ ┌───┐┌──────────┐┌───┐ ┌───────────┐ ┌─────────┐ ┌──────────────┐ ┌──────────┐ │ ┌────────┐ ┌───┐ │ ┌──────────┐ ┌───┐ ┌───────────┐ ┌─────────┐┌─────────┐ │ │ │ └─────────┘ │ └────────────┘└────────────┘└─────────────┘ ┌─────────┐ ┌───┐┌──────────┐┌───┐ q_1 -> 1 ──┤ Ry(-π/2) ├───┤ Rz(-π) ├────────┼──────────────────────┤ X ├──┼────┤ Ry(-π/2) ├─┤ Rz(-π) ├───┤ X ├─────┼──┤ Ry(-0.18411) ├┤ Rx(1.1905) ├───────────────────────────────────────────────────┼──────────────────────────────────┼──────────────────────────────────────────────┤0 ├─────────────┼─────────────────────────────────┼───────────────────────────────────────────────┤ X ├┤ Rz(-1.7868) ├───┼──────────────────────┼────┤ X ├─┤ Rz(1.7868) ├┤ X ├┤ Rz(-1.7391) ├───────────────┤ X ├─┤ Rz(1.7391) ├────────────────────┼────────■─────────┤ Rz(π/4) ├───────────────────■────────────────■───────■──────────────────────────■────┤ Rx(0.94723) ├────────────┤1 ├──┤ Rz(π/2) ├────┤ Ry(π) ├──────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────┼──────────────────────────■───────┤ Rx(π/2) ├─────┤ X ├─────────────────────────────────────────────────────────────────────────────────────────────────────┤ X ├┤ ├┤ Rz(-2.9463) ├────┤ X ├─────┤ Rz(-0.19532) ├┤ Ry(π/2) ├─┤ X ├┤ Rz(-π/4) ├┤ X ├─┤ Rz(-3π/4) ├──┤ Ry(π/2) ├─┤0 ├─┤ Ry(-π/2) ├───┼───┤ Rz(-π) ├─┤ X ├──┼───┤ Rz(-π/4) ├───┤ X ├────┤ Rz(-3π/4) ├─┤ Ry(π/2) ├┤ Rz(π/4) ├─────────────────────────────────────────┼────────────────────■─────────────────────┼─────────────────┼────────────────┼──────────────────────────────────────────────────────────────■──┤ Rz(π/4) ├───────────────────┤ X ├┤ Rz(-π/4) ├┤ X ├ ├─────────┬┘ └────────┘ │ ┌─────────────┐└─┬─┘ │ └┬───────┬─┘ └─┬───┬──┘ └─┬─┘ │ └─┬──────────┬─┘└─┬────────┬─┘┌───┐┌──────────┐ ┌───┐┌─────────────┐ │ ┌───┐ ┌─────────────┐ │ ┌───┐ ┌─────────────┐ │ │ │ │ └─┬─┘├─────────────┴┐ │ ┌──────────────┐ │ └─┬─┘ └────────────┘└─┬─┘└────┬───┬────┘┌─────────────┐└─┬─┘┌┴────────────┤┌───┐┌────────────┐ │ │ ┌┴─────────┴─┐┌─────────────┐ │ │ ┌─┴─┐ ┌──────────────┐ ┌─┴─┐ └────┬───┬────┘┌──────────┐│ │ └──┬───┬──┘ ┌┴───────┴┐ ┌───┐ ┌──────────┐┌───┐┌───────────┐┌─────────┐ ┌────────────┐ │ │ └──┬───┬──┘ └─┬─┘ ┌──────────────┐┌───┐┌─────────────┐ ┌───┐┌─────────────┐ ┌───┐┌─────────────┐└─┬─┘│ │└─────────────┘ └─┬─┘ └─┬─────────┬──┘└─────────┘ └─┬─┘└──────────┘└─┬─┘ └───────────┘ └─────────┘ │ Rxx(4.5678) │┌┴──────────┴┐ │ └────────┘ └─┬─┘ │ └──────────┘ └─┬─┘ └───────────┘ └─────────┘├─────────┴┐ ┌────────┐ ┌───┐┌──────────┐ ┌─┴─┐ ┌─────────┐ │ ┌───┐┌──────────┐┌─┴─┐┌───────────┐ │ ┌─────────┐ │ ┌────────────┐ ┌───────────┐ ┌─────────────┐ │ └─────────┘ └─┬─┘└──────────┘└─┬─┘ q_2 -> 2 ──┤ Ry(π/2) ├──────────────────────■───────┤ Ry(-2.3912) ├──┼────■─────┤ Rx(π) ├─────┤ X ├────────┼───────┼────┤ Ry(-π/2) ├────┤ Rz(-π) ├──┤ X ├┤ Rz(-π/4) ├────────────┤ X ├┤ Rz(-1.3795) ├──┼───────┤ X ├─────┤ Ry(-1.1638) ├──┼───────┤ X ├─────┤ Rz(-2.5344) ├──────────────┤ ├─────■───────┼─────────────────────────────────┼─────────■───────────────────────────────────────┼──┤0 ├──┼──┤ Rz(-0.85807) ├────┼──────┼───────────────────┼───────┤ X ├─────┤ Rz(-1.0517) ├──┼──┤ Ry(-1.7568) ├┤ X ├┤ Rz(2.6189) ├─┼────────┼────────┤ Ry(1.2411) ├┤ Rz(-1.7732) ├──┼────────────────┼─────┤ X ├────┤ Ry(-0.52456) ├──┤ X ├───────┤ X ├─────┤ Rz(-π/4) ├┤ ├─────┤ X ├──────┤ Rz(π/4) ├───────┤ X ├──────┤ Rz(-π/4) ├┤ X ├┤ Rz(1.391) ├┤ Ry(π/2) ├─┤ Rz(1.7105) ├───────┼─────────■─────────────────────────────────┼─────────────────────────────────────┤ X ├──────────┼───────┤ Rz(-0.13073) ├┤ X ├┤ Rz(0.13073) ├───────────┤ X ├┤ Rz(-1.0897) ├─────────┤ X ├┤ Rz(-2.2472) ├──■──┤ ├─────────────────────■─────────┤ Rz(π/4) ├─────────────────■────────────────■──────────────────────────────┤1 ├┤ Rz(1.9926) ├──┼────────────────┼────┼────────────────────┼─────────────────────────■─────┤ Ry(-π/2) ├───┤ Rz(-π) ├───┤ X ├┤ Rz(-π/4) ├─────┤ X ├─────┤ Rz(π/4) ├──┼──┤ X ├┤ Rz(-π/4) ├┤ X ├┤ Rz(-3π/4) ├──┼──┤ Ry(π/2) ├───┼────────■───────┤ Rz(3.0033) ├─┤ Ry(1.985) ├─┤ Rz(-3.0033) ├──┼──────────────────────────────────┼────────────────┼── ├─────────┤ └─────────────┘ │ └───────┘ └─┬─┘ │ │ └──────────┘ └────────┘ └─┬─┘└──────────┘┌─────────┐ └─┬─┘└─────────────┘ │ └─┬─┘ └┬────────────┤ │ └─┬─┘ └─┬──────────┬┘┌────────────┐│ Rxx(2.4346) │ ┌─┴─┐ │ ┌────────────┐┌─────────────┐ │ ┌─┴─┐ ┌────────────┐ ┌────────────┐ │ │ Rxx(2.8835) │ │ └──────────────┘ │ │ │ └─┬─┘ └─────────────┘ │ └─────────────┘└─┬─┘└────────────┘ │ │ └────────────┘└─────────────┘ │ │ └───┘ └────┬───┬─────┘ └───┘ └─┬─┘ └──────────┘│ │ └─┬─┘ └─────────┘ └─┬─┘ ├─────────┬┘└─┬─┘└───┬───┬───┘├─────────┴┐└───┬───┬────┘ ┌─┴─┐ │ ┌──────────────┐ ┌─┴─┐ ┌──────────┐┌─────────────┐ └─┬─┘ │ └──────────────┘└─┬─┘├─────────────┤┌─────────┐└─┬─┘└─────────────┘┌───────┐└─┬─┘└─────────────┘ │ Rxx(-1.5416) │ └─────────┘ ┌──────────────┐┌─────────┐ └──────────────┘└────────────┘ │ │ │ │ ┌─────────────┐ │ └──┬───┬───┘┌──┴────────┴──┐└─┬─┘└──┬───┬───┘ ┌──┴───┴───┐ └┬────────┤┌─┴─┐└─┬─┘├──────────┤└───┘└───────────┘ │ └─────────┘ │ │ └───┬───┬────┘ └┬─────────┬┘ └─────────────┘┌─┴─┐┌──────────┐┌───┐┌───────────┐ │ ┌─────────┐ │ q_3 -> 3 ──┤ Rx(π/2) ├───────────────────────────────────────────────┼──────────────────────────┼──────────■───────┼──────────────────────────────────┼───────■──────┤ Ry(π/2) ├───┼───────────────────┼─────────■────────┤ Ry(1.1638) ├──┼─────────■─────────┤ Ry(-π/2) ├─┤ Rz(1.6333) ├┤ ├───┤ X ├─────┼──┤ Rz(2.6872) ├┤ Ry(-2.1999) ├──┼───────┤ X ├─────┤ Ry(2.1999) ├─┤ Rz(1.9627) ├───┼──┤1 ├──┼──────────────────────X──────┼───────────────────┼─────────┼────────────────────────┼───────────────────┼─────────────────┼────────┼───────────────────────────────────────┼────────────────┼───────────────────┤ X ├──────────────────────■───────────────────┤ Rxx(-0.94723) ├───────┼────────────────────────────■────────┤ Rz(π/4) ├───┼──────┤ X ├────┤ Rz(-π/4) ├────┤ X ├──────────┤ X ├───────┼───────────┤ Rz(-0.90092) ├────┤ X ├─────┤ Ry(-π/2) ├┤ Rz(-3.0109) ├─────■────────────┼─────────────────────────■──┤ Ry(-2.6323) ├┤ Rz(π/2) ├──┼─────────■───────┤ Rx(π) ├──┼──────────────────────┤ ├────────────────────────────────────■───────────────────────────────────────■──┤1 ├┤ Rz(π/4) ├─────────────────────────────────┼────────────────■────┼────────────────────■─────┤ Ry(0.14109) ├─────┼────────┤ X ├────┤ Ry(-0.14109) ├──┼─────┤ X ├──────┤ Ry(-π/2) ├──┤ Rz(-π) ├┤ X ├──┼──┤ Rz(-π/4) ├────────────────────┼────────────────┼────────┼───────────┤ X ├───────┤ Rz(π/4) ├─────────────────┤ X ├┤ Rz(-π/4) ├┤ X ├┤ Rz(-3π/4) ├──┼──┤ Ry(π/2) ├───┼── └─────────┘ │ │ ┌─────────┐ │ │ │ └─────────┘ │ ┌─────────┐ │ ┌────────────┐ └────────────┘ │ ┌─────────────┐ ├──────────┤ └────────────┘│ │┌──┴───┴──┐ │ └────────────┘└─────────────┘ │ └───┘ └────────────┘ └────────────┘ │ └──────────────┘ │ ┌────────────┐ ┌───────┐ │ │ │ │ ┌──────────┐ │ ┌────────┐ │ ┌─┴─┐ ┌──────────┐ ┌────────┐ ┌─┴─┐┌──────────┐┌─┴─┐┌──────────┐ └─┬─┘ ┌───────┐ │ │ │ └─────────┘ │ └─┬─┘ ├─────────┬┘ └─┬─┘ ┌────┴───┴─────┐ │ └┬────────────┬┘┌───┴───┴────┐└──────────┘└─────────────┘ │ ┌──────────┐ └─────────────┘└─────────┘ │ ┌─┴─┐ └───────┘ │ │ │ │ │ │ Rxx(4.8296) │├─────────┴┐ ┌────────┐ ┌─┴─┐┌──────────┐ ┌─┴─┐┌───────────┐┌─────────┐└─────────────┘ ┌─┴─┐ └─┬─┘ └──────────────┘ │ └─┬─┘ └──────────┘ └────────┘└───┘ │ ├─────────┬┘ ┌─┴─┐┌──────────┐┌─┴─┐ ┌─┴─┐ └─┬─┘ └─────────┘ └───┘└──────────┘└─┬─┘└───────────┘ │ ├─────────┤ │ q_4 -> 4 ────────────────────────────────────────────────────────────■──────────────────────────■─────┤ Rz(π/4) ├──┼──────────────────────────────────■───────┼────────────────────■────┤ Ry(π/2) ├────■───┤ Ry(2.8058) ├─────────────────■──┤ Rz(-2.0472) ├──┤ Ry(-π/2) ├───────────────┤1 ├┤ Rz(π/2) ├──┼─────────────────────────────────┼─────────────────────────────────────────────────┼────────────────────■───┤ Rz(1.4025) ├─┤ Ry(π) ├──┼───────────────────■─────────┼────────────────────────■────┤ Ry(-π/2) ├───┼────┤ Rz(-π) ├───┼──────┤ X ├───────┤ Ry(-π/2) ├────┤ Rz(-π) ├──┤ X ├┤ Rz(-π/4) ├┤ X ├┤ Rz(3π/4) ├───────■────────┤ Rx(π) ├───────────────────────────┤ ├───────■───────────────────────────────────────────────────■────────■──────┤ Rz(π/4) ├───────■───────┤ Rz(-0.44825) ├─┼────────────┤ Ry(3.1038) ├─┤ Rz(2.0726) ├─────────────────────────────────────────────■─────────┤ Rx(-π/2) ├───────────────────────────────────┼───────┤ X ├────────────────┼──────────────────────┤ ├────────────────────────────────────┼──────────────────────■────────────────┼──┤0 ├┤ Ry(-π/2) ├───┤ Rz(-π) ├─────────────────┤ X ├┤ Rz(-π/4) ├─────┤ X ├┤ Rz(-3π/4) ├┤ Ry(π/2) ├──────────────────┤ X ├────────┼────────────────────────■───────┼───────────────────────────────────────■──┤ Rz(π/4) ├───────────────────┤ X ├┤ Rz(-π/4) ├┤ X ├────┤ X ├───────────■────────────────────────────────────────────────────────■─────────────────■──┤ Rz(π/4) ├───■── ┌─────────────┐┌───────────┐┌─────────────┐ └─────────┘┌─┴─┐ ┌──────────┐ ┌─────────┐ ┌─┴─┐ ┌──────────┐ └─────────┘ └────────────┘ └─────────────┘ └──────────┘ └──────────────┘└─────────┘ │ │ ┌──────────┐ ┌────────────┐ │ └────────────┘ └───────┘ │ ┌─────────────┐ │ └──────────┘ │ └────────┘ │ ┌────┴───┴────┐ └┬────────┬┘ └────────┘ └───┘└──────────┘└───┘└──────────┘ └───────┘ │ │┌─────────────┐┌────────────┐┌──────────────┐ └─────────┘ └──────────────┘ │ZZ(1.1107) ├────────────┤ ├────────────┤ └──────────┘ │ └───┘ │ ┌────────────┐ │ │ ┌──────────┐ ┌───────────┐ ┌─┴─┐ ┌──────────┐┌─┴─┐┌─────────┐ ┌─┴─┐└─┬──────────┬─┘└┬───────┬─┘ └────────┘ └───┘└──────────┘ └───┘└───────────┘└─────────┘ └───┘ │ │ ┌─────────────┐ └─────────┘ └───┘└──────────┘└───┘ └───┘ └─────────┘ q_5 -> 5 ┤ Rz(-3.1159) ├┤ Ry(1.656) ├┤ Rz(0.58629) ├─────────────────────────────────────────────────────────────┤ X ├──┤ Ry(-π/2) ├───┤ Rz(π/2) ├──────────┤ X ├────┤ Rz(3π/4) ├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────■─────────────────────────────────■────┤ Rx(-π/2) ├─┤ Rz(1.7868) ├──────────────────■──────────────────────────────────────────────────■──┤ Rz(-2.4875) ├────────────■────────────────────────────────────────────■─────────────────X─┤ Ry(-1.4281) ├───┤ Rz(-π) ├───────────────────────────────────────────────────────────────────────────────────────────────────────┤0 ├┤ Rz(-1.4692) ├┤ Ry(1.3781) ├┤ Rz(-0.21319) ├─────────────────────────────────────────────────────────────────────────■────────────┤ Rx(2.6126) ├─┤ Rz(1.0897) ├──────────────────────────────────────────────────────────────────────────────────────────────────────■────────────────────────────■───┤ Rx(1.5416) ├─────┤1 ├──┤ Ry(-π/2) ├─┤ Rz(-3π/4) ├──────┤ X ├──────┤ Rz(-π/4) ├┤ X ├┤ Rz(π/4) ├─┤ X ├──┤ Rz(-π/4) ├───┤ Ry(π) ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────■────────────────────────────────■──────┤ Rx(-3.0231) ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── └─────────────┘└───────────┘└─────────────┘ └───┘ └──────────┘ └─────────┘ └───┘ └──────────┘ └──────────┘ └────────────┘ └─────────────┘ └─────────────┘ └────────┘ └────────────────┘└─────────────┘└────────────┘└──────────────┘ └────────────┘ └────────────┘ └────────────┘ └───────────────┘ └──────────┘ └───────────┘ └───┘ └──────────┘└───┘└─────────┘ └───┘ └──────────┘ └───────┘ └─────────────┘
We can run our circuit and get job object back, which will store information about execution: job id, status of job and results when it will be available
[ ]:
ionq_job = ionq_device.run(transpiled_circuit, shots=10)
ionq_job.job_id()
If we do not want to wait for a job completion and return to it later, we can use retrieve_job
method on device to get job object
[15]:
retrieved_job = ionq_device.retrieve_job(task_id="<TASK_ARN>")
retrieved_job
[15]:
<qiskit_braket_provider.providers.braket_job.AmazonBraketTask at 0x13cd4bcd0>
Check status
[16]:
retrieved_job.status()
[16]:
<JobStatus.DONE: 'job has successfully run'>
And plot histogram of results
[17]:
plot_histogram(retrieved_job.result().get_counts())
[17]:

Running algorithms#
Let’s review an example of running algorithms using Qiskit on Braket devices.
Qiskit provides a number of Algorithms and they are grouped by category according to the task they can perform. For instance Minimum Eigensolvers to find the smallest eigen value of an operator, for example ground state energy of a chemistry Hamiltonian or a solution to an optimization problem when expressed as an Ising Hamiltonian.
Algorithms are configurable and often part of the configuration will be in the form of smaller building blocks, of which different instances of the building block type can be given. For instance with VQE
, the Variational Quantum Eigensolver, it takes a trial wavefunction, in the form of a QuantumCircuit
and a classical optimizer among other things.
Let’s take a look at an example to construct a VQE instance. Here TwoLocal
is the variational form (trial wavefunction), a parameterized circuit which can be varied, and SLSQP a classical optimizer. Then we pass hamiltonian to compute_minimum_eigenvalue
method of VQE
to get result.
Full example and explanation of algorithm detail you can find here.
[18]:
H2_op = SparsePauliOp(
["II", "IZ", "ZI", "ZZ", "XX"],
coeffs=[
-1.052373245772859,
0.39793742484318045,
-0.39793742484318045,
-0.01128010425623538,
0.18093119978423156,
],
)
estimator = BackendEstimator(local_simulator, skip_transpilation=False)
ansatz = TwoLocal(rotation_blocks="ry", entanglement_blocks="cz")
slsqp = SLSQP(maxiter=1)
vqe = VQE(estimator=estimator, ansatz=ansatz, optimizer=slsqp)
result = vqe.compute_minimum_eigenvalue(H2_op)
print(result)
{ 'aux_operators_evaluated': None,
'cost_function_evals': 9,
'eigenvalue': -1.4272080711283344,
'optimal_circuit': <qiskit.circuit.library.n_local.two_local.TwoLocal object at 0x13798f690>,
'optimal_parameters': { ParameterVectorElement(θ[0]): 1.054197452954356,
ParameterVectorElement(θ[1]): 2.759415645946344,
ParameterVectorElement(θ[2]): -3.157245172580292,
ParameterVectorElement(θ[3]): -1.504397646177443,
ParameterVectorElement(θ[4]): -0.8460982537780612,
ParameterVectorElement(θ[5]): 3.3343214671170145,
ParameterVectorElement(θ[6]): 3.4110127659318756,
ParameterVectorElement(θ[7]): -5.015554393329303},
'optimal_point': array([ 1.05419745, 2.75941565, -3.15724517, -1.50439765, -0.84609825,
3.33432147, 3.41101277, -5.01555439]),
'optimal_value': -1.4272080711283344,
'optimizer_evals': None,
'optimizer_result': <qiskit_algorithms.optimizers.optimizer.OptimizerResult object at 0x13c8bc050>,
'optimizer_time': 3.5380678176879883}
Running Hybrid Jobs#
Amazon Braket Hybrid Jobs enables you to easily run hybrid quantum-classical algorithms, such as the Variational Quantum Eigensolver (VQE) and the Quantum Approximate Optimization Algorithm (QAOA), that combine classical compute resources with quantum computing devices to optimize the performance of today’s quantum systems.
In order to run Hybrid Job we need to acomplish couple of step:
Prepare script
[1]:
! cat ./data/2_hybrid_jobs/job_script.py
"""Example of Hybrid Job payload with VQE."""
from braket.jobs import save_job_result
from qiskit.quantum_info import SparsePauliOp
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.algorithms.optimizers import SLSQP
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import BackendEstimator
from qiskit_braket_provider import AWSBraketProvider
def main():
backend = AWSBraketProvider().get_backend("SV1")
h2_op = SparsePauliOp(
["II", "IZ", "ZI", "ZZ", "XX"],
coeffs=[
-1.052373245772859,
0.39793742484318045,
-0.39793742484318045,
-0.01128010425623538,
0.18093119978423156,
],
)
estimator = BackendEstimator(
backend=backend,
options={"seed_simulator": 42, "seed_transpiler": 42, "shots": 10},
skip_transpilation=False,
)
ansatz = TwoLocal(rotation_blocks="ry", entanglement_blocks="cz")
slsqp = SLSQP(maxiter=1)
vqe = VQE(estimator=estimator, ansatz=ansatz, optimizer=slsqp)
vqe_result = vqe.compute_minimum_eigenvalue(h2_op)
save_job_result(
{
"VQE": {
"eigenvalue": vqe_result.eigenvalue.real,
"optimal_parameters": list(vqe_result.optimal_parameters.values()),
"optimal_point": vqe_result.optimal_point.tolist(),
"optimal_value": vqe_result.optimal_value.real,
}
}
)
Prepare image
[20]:
! cat ./data/2_hybrid_jobs/Dockerfile
FROM 292282985366.dkr.ecr.us-west-2.amazonaws.com/amazon-braket-base-jobs:1.0-cpu-py37-ubuntu18.04
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install --no-cache --upgrade git+https://github.com/qiskit-community/qiskit-braket-provider
Run Hybrid Job
[31]:
image_uri = "<ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/amazon-braket-qiskit-provider:latest"
job = AwsQuantumJob.create(
device="arn:aws:braket:::device/quantum-simulator/amazon/sv1",
source_module="data/2_hybrid_jobs/job_script.py",
entry_point="job_script:main",
wait_until_complete=False,
job_name="qiskit-braket-vqe",
image_uri=image_uri,
)
[27]:
# print(job.result())
AwsQuantumJob("<JOB_ARN>").result()
[27]:
{}