{ "cells": [ { "cell_type": "markdown", "id": "7f19aecc", "metadata": {}, "source": [ "# Running VQE on an Amazon Braket backend" ] }, { "cell_type": "markdown", "id": "55c313b7", "metadata": {}, "source": [ "Let's review an example of running algorithms using Qiskit on Braket devices.\n", "\n", "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.\n", "\n", "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.\n", "\n", "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.\n", "\n", "Full example and explanation of algorithm detail you can find [here](https://qiskit-community.github.io/qiskit-algorithms/tutorials/01_algorithms_introduction.html).\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "ebac1b5c", "metadata": { "ExecuteTime": { "end_time": "2024-03-06T05:50:24.799348Z", "start_time": "2024-03-06T05:50:23.124683Z" } }, "outputs": [], "source": [ "from qiskit.circuit.library import TwoLocal\n", "from qiskit.primitives import BackendEstimator\n", "from qiskit.quantum_info import SparsePauliOp\n", "from qiskit_algorithms.minimum_eigensolvers import VQE\n", "from qiskit_algorithms.optimizers import SLSQP\n", "from qiskit_algorithms.utils import algorithm_globals\n", "\n", "from qiskit_braket_provider import BraketLocalBackend, BraketProvider\n", "\n", "seed = 50\n", "algorithm_globals.random_seed = seed" ] }, { "cell_type": "markdown", "id": "ec9353f3", "metadata": {}, "source": [ "Get backend to run VQE with" ] }, { "cell_type": "code", "execution_count": 2, "id": "e94c63b1", "metadata": { "ExecuteTime": { "end_time": "2024-03-06T05:50:24.889207Z", "start_time": "2024-03-06T05:50:24.800625Z" } }, "outputs": [ { "data": { "text/plain": "BraketBackend[default]" }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "provider = BraketProvider()\n", "local_simulator = BraketLocalBackend()\n", "local_simulator" ] }, { "cell_type": "markdown", "id": "140737fe", "metadata": {}, "source": [ "## Running VQE\n", "\n", "\n", "\n", "More docs on VQE and algorithms https://qiskit-community.github.io/qiskit-algorithms/tutorials/01_algorithms_introduction.html#A-complete-working-example" ] }, { "cell_type": "code", "execution_count": 3, "id": "9081f3b3", "metadata": { "ExecuteTime": { "end_time": "2024-03-06T05:50:25.245606Z", "start_time": "2024-03-06T05:50:24.889913Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{ 'aux_operators_evaluated': None,\n", " 'cost_function_evals': 9,\n", " 'eigenvalue': -1.090030662389807,\n", " 'optimal_circuit': ,\n", " 'optimal_parameters': { ParameterVectorElement(θ[0]): 3.6118600692240777,\n", " ParameterVectorElement(θ[1]): 4.19301252102391,\n", " ParameterVectorElement(θ[2]): 0.6019852007557841,\n", " ParameterVectorElement(θ[4]): -3.3070470445355764,\n", " ParameterVectorElement(θ[5]): 1.846293183182938,\n", " ParameterVectorElement(θ[3]): 5.949536809130025,\n", " ParameterVectorElement(θ[6]): -5.466043598406607,\n", " ParameterVectorElement(θ[7]): 0.6984088030463612},\n", " 'optimal_point': array([ 3.61186007, 4.19301252, 0.6019852 , 5.94953681, -3.30704704,\n", " 1.84629318, -5.4660436 , 0.6984088 ]),\n", " 'optimal_value': -1.090030662389807,\n", " 'optimizer_evals': None,\n", " 'optimizer_result': ,\n", " 'optimizer_time': 0.3481462001800537}\n" ] } ], "source": [ "H2_op = SparsePauliOp(\n", " [\"II\", \"IZ\", \"ZI\", \"ZZ\", \"XX\"],\n", " coeffs=[\n", " -1.052373245772859,\n", " 0.39793742484318045,\n", " -0.39793742484318045,\n", " -0.01128010425623538,\n", " 0.18093119978423156,\n", " ],\n", ")\n", "\n", "estimator = BackendEstimator(\n", " local_simulator,\n", " options={\"seed_transpiler\": seed, \"seed_simulator\": seed},\n", " skip_transpilation=False,\n", ")\n", "ansatz = TwoLocal(rotation_blocks=\"ry\", entanglement_blocks=\"cz\")\n", "slsqp = SLSQP(maxiter=1)\n", "\n", "vqe = VQE(estimator=estimator, ansatz=ansatz, optimizer=slsqp)\n", "\n", "result = vqe.compute_minimum_eigenvalue(H2_op)\n", "print(result)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 5 }