Running VQE on an Amazon Braket backend

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 the Hamiltonian to compute_minimum_eigenvalue method of VQE to get result.

Full example and explanation of algorithm detail you can find here.

[1]:
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import BackendEstimator
from qiskit.quantum_info import SparsePauliOp
from qiskit_algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms.optimizers import SLSQP
from qiskit_algorithms.utils import algorithm_globals

from qiskit_braket_provider import BraketLocalBackend, BraketProvider

seed = 50
algorithm_globals.random_seed = seed

Get backend to run VQE with

[2]:
provider = BraketProvider()
local_simulator = BraketLocalBackend()
local_simulator
[2]:
BraketBackend[default]

Running VQE

More docs on VQE and algorithms https://qiskit-community.github.io/qiskit-algorithms/tutorials/01_algorithms_introduction.html#A-complete-working-example

[3]:
H2_op = SparsePauliOp(
    ["II", "IZ", "ZI", "ZZ", "XX"],
    coeffs=[
        -1.052373245772859,
        0.39793742484318045,
        -0.39793742484318045,
        -0.01128010425623538,
        0.18093119978423156,
    ],
)

estimator = BackendEstimator(
    local_simulator,
    options={"seed_transpiler": seed, "seed_simulator": seed},
    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.090030662389807,
    'optimal_circuit': <qiskit.circuit.library.n_local.two_local.TwoLocal object at 0x168f412d0>,
    'optimal_parameters': {   ParameterVectorElement(θ[0]): 3.6118600692240777,
                              ParameterVectorElement(θ[1]): 4.19301252102391,
                              ParameterVectorElement(θ[2]): 0.6019852007557841,
                              ParameterVectorElement(θ[4]): -3.3070470445355764,
                              ParameterVectorElement(θ[5]): 1.846293183182938,
                              ParameterVectorElement(θ[3]): 5.949536809130025,
                              ParameterVectorElement(θ[6]): -5.466043598406607,
                              ParameterVectorElement(θ[7]): 0.6984088030463612},
    'optimal_point': array([ 3.61186007,  4.19301252,  0.6019852 ,  5.94953681, -3.30704704,
        1.84629318, -5.4660436 ,  0.6984088 ]),
    'optimal_value': -1.090030662389807,
    'optimizer_evals': None,
    'optimizer_result': <qiskit_algorithms.optimizers.optimizer.OptimizerResult object at 0x10932cb80>,
    'optimizer_time': 0.3481462001800537}