|
16 | 16 | "cell_type": "markdown", |
17 | 17 | "metadata": {}, |
18 | 18 | "source": [ |
19 | | - "# Qiskit Aqua: Tutorial to build a pluggable algorithm/component\n" |
| 19 | + "# _*Qiskit Aqua: Tutorial to build a pluggable algorithm/component*_ \n", |
| 20 | + "\n", |
| 21 | + "The latest version of this notebook is available on https://github.com/Qiskit/qiskit-tutorial.\n", |
| 22 | + "\n", |
| 23 | + "***\n", |
| 24 | + "### Contributors\n", |
| 25 | + "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", |
| 26 | + "### Affiliation\n", |
| 27 | + "- <sup>[1]</sup>IBMQ" |
20 | 28 | ] |
21 | 29 | }, |
22 | 30 | { |
|
28 | 36 | "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." |
29 | 37 | ] |
30 | 38 | }, |
31 | | - { |
32 | | - "cell_type": "markdown", |
33 | | - "metadata": {}, |
34 | | - "source": [ |
35 | | - "### Register a pluggable algorithm/components\n", |
36 | | - "After you complete the algorithm implementation, there are two approaches to register your pluggable algorithm/components.\n", |
37 | | - "\n", |
38 | | - "**Note: you can use the developed algorithm directly in Python without any issue.**\n", |
39 | | - "\n", |
40 | | - "1. Register it permanently. \n", |
41 | | - "2. Register it on-the-fly.\n", |
42 | | - "\n", |
43 | | - "\n", |
44 | | - "#### Register it permanently\n", |
45 | | - "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 prepared a [setup.py](evolutionfidelity/setup.py) example for this tutorial. \n", |
46 | | - "\n", |
47 | | - "Go to the `qiskit/advanced/aqua/evolutionfidelity` folder, and then do `python3 setup.py install` to install the package.\n", |
48 | | - "\n", |
49 | | - "#### Register it on-the-fly\n", |
50 | | - "You can also register it on-the-fly. Aqua provides a function to register your pluggable algorithm/component easily. The following cell shows how to do that." |
51 | | - ] |
52 | | - }, |
53 | | - { |
54 | | - "cell_type": "code", |
55 | | - "execution_count": 1, |
56 | | - "metadata": { |
57 | | - "ExecuteTime": { |
58 | | - "end_time": "2019-12-12T15:16:11.185336Z", |
59 | | - "start_time": "2019-12-12T15:16:08.136942Z" |
60 | | - } |
61 | | - }, |
62 | | - "outputs": [], |
63 | | - "source": [ |
64 | | - "from qiskit.aqua import register_pluggable\n", |
65 | | - "from evolutionfidelity.evolutionfidelity import EvolutionFidelity\n", |
66 | | - "try:\n", |
67 | | - " register_pluggable(EvolutionFidelity)\n", |
68 | | - "except Exception as e:\n", |
69 | | - " print(e)" |
70 | | - ] |
71 | | - }, |
72 | | - { |
73 | | - "cell_type": "code", |
74 | | - "execution_count": 2, |
75 | | - "metadata": { |
76 | | - "ExecuteTime": { |
77 | | - "end_time": "2019-12-12T15:16:11.201460Z", |
78 | | - "start_time": "2019-12-12T15:16:11.198572Z" |
79 | | - } |
80 | | - }, |
81 | | - "outputs": [], |
82 | | - "source": [ |
83 | | - "from qiskit import BasicAer\n", |
84 | | - "import numpy as np\n", |
85 | | - "from qiskit.aqua.operators import MatrixOperator, op_converter\n", |
86 | | - "from qiskit.aqua import local_pluggables, PluggableType" |
87 | | - ] |
88 | | - }, |
89 | | - { |
90 | | - "cell_type": "markdown", |
91 | | - "metadata": {}, |
92 | | - "source": [ |
93 | | - "List all registered algorithms, and we will find `EvolutionFidelity` in the list if you registered it." |
94 | | - ] |
95 | | - }, |
96 | | - { |
97 | | - "cell_type": "code", |
98 | | - "execution_count": 3, |
99 | | - "metadata": { |
100 | | - "ExecuteTime": { |
101 | | - "end_time": "2019-12-12T15:16:11.217871Z", |
102 | | - "start_time": "2019-12-12T15:16:11.214681Z" |
103 | | - } |
104 | | - }, |
105 | | - "outputs": [ |
106 | | - { |
107 | | - "name": "stdout", |
108 | | - "output_type": "stream", |
109 | | - "text": [ |
110 | | - "['QAOA.Variational', 'QGAN', 'VQC', 'VQE', 'ExactEigensolver', 'ExactLSsolver', 'SVM', 'EOH', 'QSVM', 'AmplitudeEstimation', 'MaximumLikelihoodAmplitudeEstimation', 'BernsteinVazirani', 'DeutschJozsa', 'Grover', 'HHL', 'IQPE', 'QPE', 'Shor', 'Simon', 'QEomEE', 'QEomVQE', 'EvolutionFidelity']\n" |
111 | | - ] |
112 | | - } |
113 | | - ], |
114 | | - "source": [ |
115 | | - "print(local_pluggables(PluggableType.ALGORITHM))" |
116 | | - ] |
117 | | - }, |
118 | 39 | { |
119 | 40 | "cell_type": "markdown", |
120 | 41 | "metadata": {}, |
121 | 42 | "source": [ |
122 | 43 | "### Use the algorithm directly\n", |
123 | | - "Without registration, you can still use the designed algorithm/components as is." |
| 44 | + "You can use the designed algorithm/components as is." |
124 | 45 | ] |
125 | 46 | }, |
126 | 47 | { |
127 | 48 | "cell_type": "code", |
128 | | - "execution_count": 4, |
| 49 | + "execution_count": 1, |
129 | 50 | "metadata": { |
130 | 51 | "ExecuteTime": { |
131 | | - "end_time": "2019-12-12T15:16:11.402263Z", |
132 | | - "start_time": "2019-12-12T15:16:11.283486Z" |
| 52 | + "end_time": "2019-08-22T01:38:45.795524Z", |
| 53 | + "start_time": "2019-08-22T01:38:45.524414Z" |
133 | 54 | } |
134 | 55 | }, |
135 | 56 | "outputs": [ |
136 | 57 | { |
137 | 58 | "name": "stdout", |
138 | 59 | "output_type": "stream", |
139 | 60 | "text": [ |
140 | | - "0.9420934576187111\n" |
| 61 | + "0.7182824381334073\n" |
141 | 62 | ] |
142 | 63 | } |
143 | 64 | ], |
144 | 65 | "source": [ |
| 66 | + "from qiskit import BasicAer\n", |
| 67 | + "import numpy as np\n", |
| 68 | + "from qiskit.aqua.operators import MatrixOperator, op_converter\n", |
145 | 69 | "from evolutionfidelity.evolutionfidelity import EvolutionFidelity\n", |
146 | 70 | "from qiskit.aqua.components.initial_states import Zero\n", |
147 | 71 | "from qiskit.aqua import QuantumInstance\n", |
|
160 | 84 | "print(result['score'])" |
161 | 85 | ] |
162 | 86 | }, |
163 | | - { |
164 | | - "cell_type": "markdown", |
165 | | - "metadata": {}, |
166 | | - "source": [ |
167 | | - "### Use the algorithm via a declarative approach\n", |
168 | | - "After you register the package, you will be able to discover the algorithm declaratively." |
169 | | - ] |
170 | | - }, |
171 | | - { |
172 | | - "cell_type": "code", |
173 | | - "execution_count": 5, |
174 | | - "metadata": { |
175 | | - "ExecuteTime": { |
176 | | - "end_time": "2019-12-12T15:16:12.488671Z", |
177 | | - "start_time": "2019-12-12T15:16:12.483037Z" |
178 | | - } |
179 | | - }, |
180 | | - "outputs": [], |
181 | | - "source": [ |
182 | | - "from qiskit.aqua.input import EnergyInput\n", |
183 | | - "from qiskit.aqua import run_algorithm\n", |
184 | | - "\n", |
185 | | - "params = {\n", |
186 | | - " 'problem': {\n", |
187 | | - " 'name': 'eoh'\n", |
188 | | - " },\n", |
189 | | - " 'algorithm': {\n", |
190 | | - " 'name': 'EvolutionFidelity',\n", |
191 | | - " 'expansion_order': 1\n", |
192 | | - " },\n", |
193 | | - " 'initial_state': {\n", |
194 | | - " 'name': 'ZERO'\n", |
195 | | - " }\n", |
196 | | - "}\n", |
197 | | - "\n", |
198 | | - "algo_input = EnergyInput(qubit_op)" |
199 | | - ] |
200 | | - }, |
201 | | - { |
202 | | - "cell_type": "code", |
203 | | - "execution_count": 6, |
204 | | - "metadata": { |
205 | | - "ExecuteTime": { |
206 | | - "end_time": "2019-12-12T15:16:24.808291Z", |
207 | | - "start_time": "2019-12-12T15:16:13.001536Z" |
208 | | - } |
209 | | - }, |
210 | | - "outputs": [ |
211 | | - { |
212 | | - "name": "stderr", |
213 | | - "output_type": "stream", |
214 | | - "text": [ |
215 | | - "/Users/paul/opt/anaconda3/envs/qiskit/lib/python3.7/site-packages/qiskit/providers/models/backendconfiguration.py:337: UserWarning: `dt` and `dtm` now have units of seconds(s) rather than nanoseconds(ns).\n", |
216 | | - " warnings.warn('`dt` and `dtm` now have units of seconds(s) rather '\n" |
217 | | - ] |
218 | | - }, |
219 | | - { |
220 | | - "name": "stdout", |
221 | | - "output_type": "stream", |
222 | | - "text": [ |
223 | | - "0.9420934576187111\n" |
224 | | - ] |
225 | | - } |
226 | | - ], |
227 | | - "source": [ |
228 | | - "result = run_algorithm(params, algo_input, backend=backend)\n", |
229 | | - "print(result['score'])" |
230 | | - ] |
231 | | - }, |
232 | 87 | { |
233 | 88 | "cell_type": "code", |
234 | | - "execution_count": 7, |
| 89 | + "execution_count": 2, |
235 | 90 | "metadata": { |
236 | 91 | "ExecuteTime": { |
237 | | - "end_time": "2019-12-12T15:16:24.898534Z", |
238 | | - "start_time": "2019-12-12T15:16:24.823965Z" |
| 92 | + "end_time": "2019-08-22T01:38:34.386128Z", |
| 93 | + "start_time": "2019-08-22T01:38:34.375938Z" |
239 | 94 | } |
240 | 95 | }, |
241 | 96 | "outputs": [ |
242 | 97 | { |
243 | 98 | "data": { |
244 | 99 | "text/html": [ |
245 | | - "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>0.14.0</td></tr><tr><td>Terra</td><td>0.11.0</td></tr><tr><td>Aer</td><td>0.3.4</td></tr><tr><td>Ignis</td><td>0.2.0</td></tr><tr><td>Aqua</td><td>0.6.1</td></tr><tr><td>IBM Q Provider</td><td>0.4.4</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.7.5 (default, Oct 25 2019, 10:52:18) \n", |
246 | | - "[Clang 4.0.1 (tags/RELEASE_401/final)]</td></tr><tr><td>OS</td><td>Darwin</td></tr><tr><td>CPUs</td><td>4</td></tr><tr><td>Memory (Gb)</td><td>16.0</td></tr><tr><td colspan='2'>Thu Dec 12 10:16:24 2019 EST</td></tr></table>" |
| 100 | + "<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td>Qiskit</td><td>None</td></tr><tr><td>Terra</td><td>0.11.0.dev0+4164568</td></tr><tr><td>Aer</td><td>0.4.0</td></tr><tr><td>Ignis</td><td>0.3.0.dev0+6145a4e</td></tr><tr><td>Aqua</td><td>0.7.0.dev0+2d7a26f</td></tr><tr><td>IBM Q Provider</td><td>0.4.0rc1</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.7.4 (default, Aug 13 2019, 15:17:50) \n", |
| 101 | + "[Clang 4.0.1 (tags/RELEASE_401/final)]</td></tr><tr><td>OS</td><td>Darwin</td></tr><tr><td>CPUs</td><td>2</td></tr><tr><td>Memory (Gb)</td><td>8.0</td></tr><tr><td colspan='2'>Mon Nov 11 17:29:56 2019 EST</td></tr></table>" |
247 | 102 | ], |
248 | 103 | "text/plain": [ |
249 | 104 | "<IPython.core.display.HTML object>" |
|
295 | 150 | "name": "python", |
296 | 151 | "nbconvert_exporter": "python", |
297 | 152 | "pygments_lexer": "ipython3", |
298 | | - "version": "3.7.5" |
| 153 | + "version": "3.7.4" |
299 | 154 | }, |
300 | 155 | "varInspector": { |
301 | 156 | "cols": { |
|
0 commit comments