Running VQE on a 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 n_local 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.

You can find full examples and explanation of algorithm details here.

[1]:
from qiskit.circuit.library import n_local
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 BraketEstimator, BraketLocalBackend

seed = 50
algorithm_globals.random_seed = seed

Get backend to run VQE with

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

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,
    ],
)

vqe = VQE(
    estimator=BraketEstimator(local_simulator),
    ansatz=n_local(2, rotation_blocks="ry", entanglement_blocks="cz"),
    optimizer=SLSQP(maxiter=1),
)

result = vqe.compute_minimum_eigenvalue(H2_op)
print(result)
{   'aux_operators_evaluated': None,
    'cost_function_evals': 9,
    'eigenvalue': -1.085615411630941,
    'optimal_circuit': <qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x30c0e2620>,
    'optimal_parameters': {   ParameterVectorElement(θ[0]): 3.611860069224077,
                              ParameterVectorElement(θ[1]): 4.19301252102391,
                              ParameterVectorElement(θ[2]): 0.6019852007557844,
                              ParameterVectorElement(θ[3]): 5.949536809130025,
                              ParameterVectorElement(θ[4]): -3.3070470445355764,
                              ParameterVectorElement(θ[5]): 1.8462931831829383,
                              ParameterVectorElement(θ[6]): -5.466043598406607,
                              ParameterVectorElement(θ[7]): 0.6984088030463615},
    'optimal_point': array([ 3.61186007,  4.19301252,  0.6019852 ,  5.94953681, -3.30704704,
        1.84629318, -5.4660436 ,  0.6984088 ]),
    'optimal_value': -1.085615411630941,
    'optimizer_evals': None,
    'optimizer_result': <qiskit_algorithms.optimizers.optimizer.OptimizerResult object at 0x30c0e1390>,
    'optimizer_time': 2.4680850505828857}