|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<img src=\"../../images/qiskit-heading.gif\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "# _*Qiskit Aqua: Tutorial to build a pluggable algorithm/component*_ \n", |
| 15 | + "\n", |
| 16 | + "The latest version of this notebook is available on https://github.com/Qiskit/qiskit-tutorial.\n", |
| 17 | + "\n", |
| 18 | + "***\n", |
| 19 | + "### Contributors\n", |
| 20 | + "Richard Chen<sup>[1]</sup>, Shaohan Hu<sup>[1]</sup>, Antonio Mezzacapo<sup>[1]</sup>, Peng Liu<sup>[1]</sup>, Marco Pistoia<sup>[1]</sup>, Stephen Wood<sup>[1]</sup>\n", |
| 21 | + "### Affiliation\n", |
| 22 | + "- <sup>[1]</sup>IBMQ" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "metadata": {}, |
| 28 | + "source": [ |
| 29 | + "### Introduction\n", |
| 30 | + "\n", |
| 31 | + "This notebook is part of the simple example of an algorithm for `Qiskit Aqua`. This sample, called `Evolution Fidelity`, illustrates how to implement an algorithm and what steps to take to configure and run it. The implementation of the algorithm itself is located in the `evolutionfidelity` directory." |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "### Register a pluggable algorithm/components\n", |
| 39 | + "After you complete the algorithm implementation, there are two approaches to register your pluggable algorithm/components.\n", |
| 40 | + "\n", |
| 41 | + "**Note: you can use the developed algorithm directly in Python without any issue.**\n", |
| 42 | + "\n", |
| 43 | + "1. Register it permentally. \n", |
| 44 | + "2. Register it on-the-fly.\n", |
| 45 | + "\n", |
| 46 | + "\n", |
| 47 | + "#### Register it permentally\n", |
| 48 | + "If you complete the pluggable algorithm/components as a Python package, you can refer to this [instruction](https://qiskit.org/documentation/aqua/extending.html#extending-aqua) to prepare the `setup.py` file to register the pluggable algorithm/component, which will be discovered in Qiskit-Aqua. We prepare a [setup.py](evolutionfidelity/setup.py) example for this tutorial. \n", |
| 49 | + "\n", |
| 50 | + "Go to the `qiskit/aqua/general/evolutionfidelity` folder, and then do `python3 setup.py install` to install the package.\n", |
| 51 | + "\n", |
| 52 | + "#### Register it on-the-fly\n", |
| 53 | + "You can also register it on-the-fly, Aqua provides a function to register your pluggable algorithm/compomenet easily. The following cell shows how to do that." |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 1, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "from qiskit.aqua import register_pluggable\n", |
| 63 | + "from evolutionfidelity.evolutionfidelity import EvolutionFidelity\n", |
| 64 | + "try:\n", |
| 65 | + " register_pluggable(EvolutionFidelity)\n", |
| 66 | + "except Exception as e:\n", |
| 67 | + " print(e)" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 2, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "from qiskit import BasicAer\n", |
| 77 | + "import numpy as np\n", |
| 78 | + "from qiskit.aqua.operator import Operator\n", |
| 79 | + "from qiskit.aqua import local_pluggables, PluggableType" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "List all registered algorithms, and we will find `EvolutionFidelity` in the list if you registered." |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 3, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [ |
| 94 | + { |
| 95 | + "name": "stdout", |
| 96 | + "output_type": "stream", |
| 97 | + "text": [ |
| 98 | + "['QAOA.Variational', 'VQC', 'VQE', 'ExactEigensolver', 'ExactLSsolver', 'SVM', 'EOH', 'QSVM', 'AmplitudeEstimation', 'BernsteinVazirani', 'DeutschJozsa', 'Grover', 'HHL', 'IQPE', 'QPE', 'Shor', 'Simon', 'EvolutionFidelity']\n" |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "print(local_pluggables(PluggableType.ALGORITHM))" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "### Use the algorithm directly\n", |
| 111 | + "Without registration, you can still use the designed algorithm/components as is." |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 4, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [ |
| 119 | + { |
| 120 | + "name": "stdout", |
| 121 | + "output_type": "stream", |
| 122 | + "text": [ |
| 123 | + "0.934847761060059\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "from evolutionfidelity.evolutionfidelity import EvolutionFidelity\n", |
| 129 | + "from qiskit.aqua.components.initial_states import Zero\n", |
| 130 | + "from qiskit.aqua import QuantumInstance\n", |
| 131 | + "num_qubits = 2\n", |
| 132 | + "temp = np.random.random((2 ** num_qubits, 2 ** num_qubits))\n", |
| 133 | + "qubit_op = Operator(matrix=temp + temp.T)\n", |
| 134 | + "\n", |
| 135 | + "initial_state = Zero(qubit_op.num_qubits)\n", |
| 136 | + "\n", |
| 137 | + "algo = EvolutionFidelity(qubit_op, initial_state, expansion_order=1)\n", |
| 138 | + "\n", |
| 139 | + "backend = BasicAer.get_backend('statevector_simulator')\n", |
| 140 | + "quantum_instance = QuantumInstance(backend=backend)\n", |
| 141 | + "\n", |
| 142 | + "result = algo.run(quantum_instance)\n", |
| 143 | + "print(result['score'])" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "### Use the algorithm via a declarative approach\n", |
| 151 | + "After you register the package, you will be able to discover the algorithm declaratively." |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 5, |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [], |
| 159 | + "source": [ |
| 160 | + "from qiskit.aqua.input import EnergyInput\n", |
| 161 | + "from qiskit.aqua import run_algorithm\n", |
| 162 | + "\n", |
| 163 | + "params = {\n", |
| 164 | + " 'problem': {\n", |
| 165 | + " 'name': 'eoh'\n", |
| 166 | + " },\n", |
| 167 | + " 'algorithm': {\n", |
| 168 | + " 'name': 'EvolutionFidelity',\n", |
| 169 | + " 'expansion_order': 1\n", |
| 170 | + " },\n", |
| 171 | + " 'initial_state': {\n", |
| 172 | + " 'name': 'ZERO'\n", |
| 173 | + " }\n", |
| 174 | + "}\n", |
| 175 | + "\n", |
| 176 | + "algo_input = EnergyInput(qubit_op)" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": 6, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [ |
| 184 | + { |
| 185 | + "name": "stdout", |
| 186 | + "output_type": "stream", |
| 187 | + "text": [ |
| 188 | + "0.9348477610600592\n" |
| 189 | + ] |
| 190 | + } |
| 191 | + ], |
| 192 | + "source": [ |
| 193 | + "result = run_algorithm(params, algo_input, backend=backend)\n", |
| 194 | + "print(result['score'])" |
| 195 | + ] |
| 196 | + } |
| 197 | + ], |
| 198 | + "metadata": { |
| 199 | + "kernelspec": { |
| 200 | + "display_name": "Python 3", |
| 201 | + "language": "python", |
| 202 | + "name": "python3" |
| 203 | + }, |
| 204 | + "language_info": { |
| 205 | + "codemirror_mode": { |
| 206 | + "name": "ipython", |
| 207 | + "version": 3 |
| 208 | + }, |
| 209 | + "file_extension": ".py", |
| 210 | + "mimetype": "text/x-python", |
| 211 | + "name": "python", |
| 212 | + "nbconvert_exporter": "python", |
| 213 | + "pygments_lexer": "ipython3", |
| 214 | + "version": "3.6.1" |
| 215 | + } |
| 216 | + }, |
| 217 | + "nbformat": 4, |
| 218 | + "nbformat_minor": 2 |
| 219 | +} |
0 commit comments