{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Scipy minimize variational plugin\n", "\n", "In this notebook, we briefly introduce the particularization of the `Optimizer` abstract plugin for `scipy.optimize.minimize`.\n", "\n", "We assume that you are already familiar with the `Optimize` class. If not, you can access a detailed notebook introducting this class [here](../plugins/junctions_and_optimizers.ipynb).\n", "\n", "`ScipyMinimizePlugin` is an `Optimizer` wrapping the `scipy.optimize.minimize` method, thus inheriting from all the underlying minimization algorithms.\n", "\n", "The plugin can be instantiated as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy\n", "from qat.vsolve.optimize import ScipyMinimizePlugin\n", "from scipy.optimize import minimize\n", "\n", "## A cobyla minimizer over any number of variables, random initialization, 20 max steps\n", "cobyla = ScipyMinimizePlugin(tol=1e-2, \n", " method=\"COBYLA\", \n", " options={\"maxiter\": 20})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets try to use this plugin to solve a QAOA instance." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.opt import MaxCut\n", "import networkx as nx\n", "import matplotlib.pyplot as plt\n", "from qat.qpus import get_default_qpu\n", "\n", "qpu = get_default_qpu()\n", "\n", "stack = cobyla | qpu\n", "\n", "graph = nx.cycle_graph(4)\n", "\n", "problem = MaxCut(graph)\n", "job = problem.to_job(\"qaoa\", 2) # '2' is the depth\n", "circuit = job.circuit\n", "result = stack.submit(job)\n", "print(\"The maxcut problem:\")\n", "print(problem)\n", "print(\"Final energy:\", result.value)\n", "print(\"The optimization data:\")\n", "print(result.meta_data[\"optimizer_data\"])\n", "print(\"The best set of parameters:\")\n", "print(result.meta_data[\"parameters\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the 'optimizer_data' entry of the result's meta_data contains the (stringified) output of scipy's minimize function.\n", "\n", "As we can see, 20 iterations are not enough for the optimizer to converge.\n", "Lets try with 200:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cobyla = ScipyMinimizePlugin(method=\"COBYLA\", \n", " tol=1e-2, \n", " options={\"maxiter\": 200})\n", "stack = cobyla | qpu\n", "result = stack.submit(job)\n", "print(\"The maxcut problem:\")\n", "print(problem)\n", "print(\"Final energy:\", result.value)\n", "print(\"The optimization data:\")\n", "print(result.meta_data[\"optimizer_data\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "authors": [ "Simon Martiel", "Thomas Ayral" ], "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.12.11" }, "tags": [ "variational" ] }, "nbformat": 4, "nbformat_minor": 2 }