|
12 | 12 | "\n",
|
13 | 13 | "Further information may be found for the algorithms in the online [Aqua documentation](https://qiskit.org/documentation/aqua/algorithms.html).\n",
|
14 | 14 | "\n",
|
15 |
| - "Algorithms in Aqua can be created and run as usual in Python by constructing instances and calling methods. There is also a high level `run_algorithm` method that takes a configuration dictionary with data describing which algorithm to use, which components etc along with an InputInstance type to supply data to the algorithm. This latter approach is what we call `declarative` with the former, the regular Python way, `programmatic`. This tutorial will show both approaches.\n", |
| 15 | + "Algorithms in Aqua can be created and run as usual in Python by constructing instances and calling methods.\n", |
16 | 16 | "\n",
|
17 | 17 | "Aqua has many `algorithms` for solving different problems. For some we also have classical algorithms, that take the exact same input data, to solve the problem. This can be useful in the near term as Quantum algorithms are developed since we are still at a stage where we can do classical comparison of the result.\n",
|
18 | 18 | "\n",
|
|
67 | 67 | "\n",
|
68 | 68 | "We can now use the Operator without regard to how it was created. We chose to start this tutorial with a classical algorithm as it involves a little less setting up than the `VQE` quantum algorithm we will use later. Here we will use `ExactEigensolver` to compute the minimum eigenvalue of the Operator (Hamiltonian).\n",
|
69 | 69 | "\n",
|
70 |
| - "#### First let's show the `programmatic` approach.\n", |
71 |
| - "\n", |
72 | 70 | "We construct an `ExactEigensolver` instance, passing in the Operator, and then call `run()` on in order to compute the result. All Aqua algorithms have the run method (it is defined by a base class which all algorithms extend) and while no parameters are need for classical algorithms a quantum algorithm will require a backend (quantum simulator or real device) on which it will be run. The `result` object returned is a dictionary. While the results fields can be different for algorithms solving different problems, and even within a given problem type there may be algorithm specific data returned, for a given problem the fields core to that problem are common across algorithms in order that different algorithms can be chosen to solve the same problem in a consistent fashion."
|
73 | 71 | ]
|
74 | 72 | },
|
|
97 | 95 | "cell_type": "markdown",
|
98 | 96 | "metadata": {},
|
99 | 97 | "source": [
|
100 |
| - "#### Now let's show the `declarative` approach. \n", |
101 |
| - "\n", |
102 |
| - "Here we need to prepare a configuration dictionary of parameters to define the algorithm. Again we we will use the ExactEigensolver and need to create an `algorithm` where it is named by `name`. The name comes from a `CONFIGURATION` dictionary in the algorithm and this name is registered to the Aqua discovery framework so we can load the corresponding class and run it during the exceution of `run_algorithm`. `run_algorithm` requires the configuration dictionary and input data passed via an InputInstance class. For an energy problem the data is supplied via an EnergyInput (extends InputInstance), other problem types have their own specific InputInstance. `run_algorithm` returns the same dictionary as above (internally it calls the run() method of the algorithm and passes back the result)\n", |
| 98 | + "### Lets switch now to using a Quantum algorithm.\n", |
103 | 99 | "\n",
|
104 |
| - "Note: there are other fields such `problem` that could have been added below. This field defaults to `energy`, which is what we want so it has been omitted. Defaults are convenient in the declarative form too as algorithms can define for both their properties as well as defaults for dependent components." |
| 100 | + "We will use the Variational Quantum Eigensolver (VQE) to solve the same problem as above. As its name implies its uses a variational approach. An ansatz (a variational form) is supplied and using a quantum/classical hybrid technique the energy resulting from evaluating the Operator with the variational form on a quantum backend is taken down to a minimum using a classical optimizer that varies the parameters of the variational form." |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "Here we create the variational form and optimizer and then pass them to VQE along with the Operator. The backend is created and passed to the algorithm so it can be run there." |
105 | 108 | ]
|
106 | 109 | },
|
107 | 110 | {
|
|
113 | 116 | "name": "stdout",
|
114 | 117 | "output_type": "stream",
|
115 | 118 | "text": [
|
116 |
| - "-1.8572750302023813\n" |
| 119 | + "-1.8572750302017973\n" |
117 | 120 | ]
|
118 | 121 | }
|
119 | 122 | ],
|
120 |
| - "source": [ |
121 |
| - "from qiskit.aqua import run_algorithm\n", |
122 |
| - "from qiskit.aqua.input import EnergyInput\n", |
123 |
| - "\n", |
124 |
| - "aqua_cfg_dict = {\n", |
125 |
| - " 'algorithm': {\n", |
126 |
| - " 'name': 'ExactEigensolver'\n", |
127 |
| - " }\n", |
128 |
| - "}\n", |
129 |
| - "\n", |
130 |
| - "algo_input = EnergyInput(qubit_op)\n", |
131 |
| - "result = run_algorithm(aqua_cfg_dict, algo_input)\n", |
132 |
| - "print(result['energy'])" |
133 |
| - ] |
134 |
| - }, |
135 |
| - { |
136 |
| - "cell_type": "markdown", |
137 |
| - "metadata": {}, |
138 |
| - "source": [ |
139 |
| - "### Lets switch now to using a Quantum algorithm.\n", |
140 |
| - "\n", |
141 |
| - "We will use the Variational Quantum Eigensolver (VQE) to solve the same problem as above. As its name implies its uses a variational approach. An ansatz (a variational form) is supplied and using a quantum/classical hybrid technique the energy resulting from evaluating the Operator with the variational form on a quantum backend is taken down to a minimum using a classical optimizer that varies the parameters of the variational form.\n", |
142 |
| - "\n", |
143 |
| - "#### Lets do the `declarative` approach first this time\n", |
144 |
| - "\n", |
145 |
| - "In the description above we talked about `VQE` a `variational form` and an `optimizer`. We can now set this up as a dictionary. While we can omit them from the dictionary, such that defaults are used, here we specify them explicitly so we can set their parameters as we desire.\n", |
146 |
| - "\n", |
147 |
| - "As this is a quantum algorithm we need to specify a backend. Here we use the `statevector_simpulator` from the `qiskit.BasicAer` provider from `Qiskit Terra`. As this is a variational algorithm going from quantum to classical and looping until it finds a minimum it takes a few seconds. The result here is very close to our classical result above." |
148 |
| - ] |
149 |
| - }, |
150 |
| - { |
151 |
| - "cell_type": "code", |
152 |
| - "execution_count": null, |
153 |
| - "metadata": { |
154 |
| - "scrolled": true |
155 |
| - }, |
156 |
| - "outputs": [], |
157 |
| - "source": [ |
158 |
| - "aqua_cfg_dict = {\n", |
159 |
| - " 'algorithm': {\n", |
160 |
| - " 'name': 'VQE',\n", |
161 |
| - " 'operator_mode': 'matrix'\n", |
162 |
| - " },\n", |
163 |
| - " 'variational_form': {\n", |
164 |
| - " 'name': 'RYRZ',\n", |
165 |
| - " 'depth': 3,\n", |
166 |
| - " 'entanglement': 'linear'\n", |
167 |
| - " },\n", |
168 |
| - " 'optimizer': {\n", |
169 |
| - " 'name': 'L_BFGS_B',\n", |
170 |
| - " 'maxfun': 1000\n", |
171 |
| - " },\n", |
172 |
| - " 'backend': {\n", |
173 |
| - " 'name': 'statevector_simulator',\n", |
174 |
| - " 'provider': 'qiskit.BasicAer'\n", |
175 |
| - " }\n", |
176 |
| - "}\n", |
177 |
| - "\n", |
178 |
| - "algo_input = EnergyInput(qubit_op)\n", |
179 |
| - "result = run_algorithm(aqua_cfg_dict, algo_input)\n", |
180 |
| - "print(result['energy'])" |
181 |
| - ] |
182 |
| - }, |
183 |
| - { |
184 |
| - "cell_type": "markdown", |
185 |
| - "metadata": {}, |
186 |
| - "source": [ |
187 |
| - "#### And now `programmatic`\n", |
188 |
| - " \n", |
189 |
| - "Here we create the variational form and optimizer and then pass them to VQE along with the Operator. The backend is created and passed to the algorithm so it can be run there." |
190 |
| - ] |
191 |
| - }, |
192 |
| - { |
193 |
| - "cell_type": "code", |
194 |
| - "execution_count": null, |
195 |
| - "metadata": {}, |
196 |
| - "outputs": [], |
197 | 123 | "source": [
|
198 | 124 | "from qiskit import BasicAer\n",
|
199 | 125 | "from qiskit.aqua.algorithms import VQE\n",
|
|
212 | 138 | "cell_type": "markdown",
|
213 | 139 | "metadata": {},
|
214 | 140 | "source": [
|
215 |
| - "While a backend can be passed directly to the quantum algorithm run(), internally it will be detected as such and wrapped as a QuantumInstance. However by doing this explicitly yourself, as below, various parameters governing the execution can be set, including in more advanced cases ability to set noise models, coupling maps etc. The following shows the above but using a QuantumInstance and setting up a default transpiler PassManager for circuit processing." |
| 141 | + "The following shows the above but using a QuantumInstance and setting up a default transpiler PassManager for circuit processing." |
216 | 142 | ]
|
217 | 143 | },
|
218 | 144 | {
|
219 | 145 | "cell_type": "code",
|
220 |
| - "execution_count": null, |
| 146 | + "execution_count": 5, |
221 | 147 | "metadata": {},
|
222 |
| - "outputs": [], |
| 148 | + "outputs": [ |
| 149 | + { |
| 150 | + "name": "stdout", |
| 151 | + "output_type": "stream", |
| 152 | + "text": [ |
| 153 | + "-1.8572750301967933\n" |
| 154 | + ] |
| 155 | + } |
| 156 | + ], |
223 | 157 | "source": [
|
224 | 158 | "from qiskit.aqua import QuantumInstance\n",
|
225 | 159 | "from qiskit.transpiler import PassManager\n",
|
|
239 | 173 | "source": [
|
240 | 174 | "### Concluding\n",
|
241 | 175 | "\n",
|
242 |
| - "This completes an introduction to programming and using Aqua algorithms. There are plenty of other tutorials showing Aqua being used to solve other problems, including AI, Finance, Optimization and Chemistry. We encourage you to explore these further and see that various capabilities and techniques employed." |
| 176 | + "This completes an introduction to programming and using Aqua algorithms. There are plenty of other tutorials showing Aqua being used to solve other problems, including Machine Learning, Finance, Optimization and Chemistry. We encourage you to explore these further and see that various capabilities and techniques employed." |
243 | 177 | ]
|
244 | 178 | },
|
245 | 179 | {
|
|
266 | 200 | "name": "python",
|
267 | 201 | "nbconvert_exporter": "python",
|
268 | 202 | "pygments_lexer": "ipython3",
|
269 |
| - "version": "3.6.8" |
| 203 | + "version": "3.7.4" |
270 | 204 | }
|
271 | 205 | },
|
272 | 206 | "nbformat": 4,
|
|
0 commit comments