|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "IafxybMjKfBO" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "##### Copyright 2020 Google" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": { |
| 16 | + "cellView": "form", |
| 17 | + "id": "pc1aHcGvKmHe" |
| 18 | + }, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 22 | + "# you may not use this file except in compliance with the License.\n", |
| 23 | + "# You may obtain a copy of the License at\n", |
| 24 | + "#\n", |
| 25 | + "# https://www.apache.org/licenses/LICENSE-2.0\n", |
| 26 | + "#\n", |
| 27 | + "# Unless required by applicable law or agreed to in writing, software\n", |
| 28 | + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 29 | + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 30 | + "# See the License for the specific language governing permissions and\n", |
| 31 | + "# limitations under the License." |
| 32 | + ] |
| 33 | + }, |
3 | 34 | {
|
4 | 35 | "cell_type": "markdown",
|
5 | 36 | "metadata": {
|
|
11 | 42 | "This code tutorial shows how to estimate a 1-RDM and perform variational optimization"
|
12 | 43 | ]
|
13 | 44 | },
|
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": { |
| 48 | + "id": "FQEYY3gnK51d" |
| 49 | + }, |
| 50 | + "source": [ |
| 51 | + "<table class=\"tfo-notebook-buttons\" align=\"left\">\n", |
| 52 | + " <td>\n", |
| 53 | + " <a target=\"_blank\" href=\"https://www.example.org/cirq/experiments/hfvqe/quickstart\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on QuantumLib</a>\n", |
| 54 | + " </td>\n", |
| 55 | + " <td>\n", |
| 56 | + " <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/ReCirq/blob/master/docs/hfvqe/quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n", |
| 57 | + " </td>\n", |
| 58 | + " <td>\n", |
| 59 | + " <a target=\"_blank\" href=\"https://github.com/quantumlib/ReCirq/blob/master/docs/hfvqe/quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n", |
| 60 | + " </td>\n", |
| 61 | + " <td>\n", |
| 62 | + " <a href=\"https://storage.googleapis.com/tensorflow_docs/ReCirq/docs/hfvqe/quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n", |
| 63 | + " </td>\n", |
| 64 | + "</table>" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": null, |
| 70 | + "metadata": { |
| 71 | + "id": "8625de3d3c0d" |
| 72 | + }, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "try:\n", |
| 76 | + " import recirq\n", |
| 77 | + "except ImportError:\n", |
| 78 | + " !pip install --quiet git+https://github.com/quantumlib/ReCirq" |
| 79 | + ] |
| 80 | + }, |
14 | 81 | {
|
15 | 82 | "cell_type": "code",
|
16 | 83 | "execution_count": null,
|
|
19 | 86 | },
|
20 | 87 | "outputs": [],
|
21 | 88 | "source": [
|
22 |
| - "# Import library functions and define a helper function\n", |
23 | 89 | "import numpy as np\n",
|
24 | 90 | "import cirq\n",
|
25 | 91 | "\n",
|
26 | 92 | "from recirq.hfvqe.gradient_hf import rhf_func_generator\n",
|
27 | 93 | "from recirq.hfvqe.opdm_functionals import OpdmFunctional\n",
|
28 |
| - "from recirq.hfvqe.analysis import (compute_opdm,\n", |
29 |
| - " mcweeny_purification,\n", |
30 |
| - " resample_opdm,\n", |
31 |
| - " fidelity_witness,\n", |
32 |
| - " fidelity)\n", |
| 94 | + "from recirq.hfvqe.analysis import (\n", |
| 95 | + " compute_opdm, mcweeny_purification,\n", |
| 96 | + " resample_opdm, fidelity_witness,\n", |
| 97 | + " fidelity)\n", |
33 | 98 | "from recirq.hfvqe.third_party.higham import fixed_trace_positive_projection\n",
|
34 |
| - "from recirq.hfvqe.molecular_example import make_h6_1_3\n" |
| 99 | + "from recirq.hfvqe.molecular_example import make_h6_1_3" |
35 | 100 | ]
|
36 | 101 | },
|
37 | 102 | {
|
|
64 | 129 | " constant=molecule.nuclear_repulsion,\n",
|
65 | 130 | " one_body_integrals=obi,\n",
|
66 | 131 | " two_body_integrals=tbi,\n",
|
67 |
| - " num_electrons=molecule.n_electrons // 2, # only simulate spin-up electrons\n", |
| 132 | + " # only simulate spin-up electrons:\n", |
| 133 | + " num_electrons=molecule.n_electrons // 2,\n", |
68 | 134 | " clean_xxyy=True,\n",
|
69 | 135 | " purification=True\n",
|
70 | 136 | " )"
|
|
103 | 169 | "measurement_data = opdm_func.calculate_data(parameters)\n",
|
104 | 170 | "\n",
|
105 | 171 | "# 2.\n",
|
106 |
| - "opdm, var_dict = compute_opdm(measurement_data,\n", |
107 |
| - " return_variance=True)\n", |
| 172 | + "opdm, var_dict = compute_opdm(measurement_data, return_variance=True)\n", |
108 | 173 | "opdm_pure = mcweeny_purification(opdm)\n",
|
109 | 174 | "\n",
|
110 | 175 | "# 3.\n",
|
|
117 | 182 | "nocc = molecule.n_electrons // 2\n",
|
118 | 183 | "nvirt = molecule.n_orbitals - nocc\n",
|
119 | 184 | "initial_fock_state = [1] * nocc + [0] * nvirt\n",
|
120 |
| - "for _ in range(1000): # 1000 repetitions of the measurement\n", |
| 185 | + "\n", |
| 186 | + "# 1000 repetitions of the measurement\n", |
| 187 | + "for _ in range(1000): \n", |
121 | 188 | " new_opdm = resample_opdm(opdm, var_dict)\n",
|
122 | 189 | " raw_energies.append(opdm_func.energy_from_opdm(new_opdm))\n",
|
123 | 190 | " raw_fidelity_witness.append(\n",
|
|
142 | 209 | " fidelity(target_unitary=true_unitary,\n",
|
143 | 210 | " measured_opdm=new_opdm_pure)\n",
|
144 | 211 | " )\n",
|
145 |
| - "print('\\n\\n\\n\\n')\n", |
146 | 212 | "print(\"Canonical Hartree-Fock energy \", molecule.hf_energy)\n",
|
147 | 213 | "print(\"True energy \", energy(parameters))\n",
|
148 | 214 | "print(\"Raw energy \", opdm_func.energy_from_opdm(opdm),\n",
|
|
154 | 220 | "print(\"Purified fidelity witness \", np.mean(purified_fidelity_witness).real,\n",
|
155 | 221 | " \"+- \", np.std(purified_fidelity_witness))\n",
|
156 | 222 | "print(\"Purified fidelity \", np.mean(purified_fidelity).real,\n",
|
157 |
| - " \"+- \", np.std(purified_fidelity))\n" |
| 223 | + " \"+- \", np.std(purified_fidelity))" |
158 | 224 | ]
|
159 | 225 | },
|
160 | 226 | {
|
|
163 | 229 | "id": "669a320cb246"
|
164 | 230 | },
|
165 | 231 | "source": [
|
166 |
| - "This should print out the various energies estimated from the 1-RDM along with error bars. Generated from resampling the 1-RDM based on the estimated covariance." |
| 232 | + "This prints out the various energies estimated from the 1-RDM along with error bars. Generated from resampling the 1-RDM based on the estimated covariance." |
167 | 233 | ]
|
168 | 234 | },
|
169 | 235 | {
|
|
202 | 268 | "source": [
|
203 | 269 | "from recirq.hfvqe.mfopt import moving_frame_augmented_hessian_optimizer\n",
|
204 | 270 | "from recirq.hfvqe.opdm_functionals import RDMGenerator\n",
|
205 |
| - "import matplotlib.pyplot as plt\n", |
| 271 | + "\n", |
206 | 272 | "rdm_generator = RDMGenerator(opdm_func, purification=True)\n",
|
207 | 273 | "opdm_generator = rdm_generator.opdm_generator\n",
|
208 | 274 | "\n",
|
|
213 | 279 | " verbose=True, delta=0.03,\n",
|
214 | 280 | " max_iter=20,\n",
|
215 | 281 | " hessian_update='diagonal',\n",
|
216 |
| - " rtol=0.50E-2)\n" |
| 282 | + " rtol=0.50E-2)" |
217 | 283 | ]
|
218 | 284 | },
|
219 | 285 | {
|
|
235 | 301 | },
|
236 | 302 | "outputs": [],
|
237 | 303 | "source": [
|
| 304 | + "import matplotlib.pyplot as plt\n", |
| 305 | + "\n", |
238 | 306 | "plt.semilogy(range(len(result.func_vals)),\n",
|
239 | 307 | " np.abs(np.array(result.func_vals) - energy(parameters)),\n",
|
240 | 308 | " 'C0o-')\n",
|
241 | 309 | "plt.xlabel(\"Optimization Iterations\", fontsize=18)\n",
|
242 | 310 | "plt.ylabel(r\"$|E - E^{*}|$\", fontsize=18)\n",
|
243 | 311 | "plt.tight_layout()\n",
|
244 |
| - "plt.show()\n" |
| 312 | + "plt.show()" |
245 | 313 | ]
|
246 | 314 | }
|
247 | 315 | ],
|
|
253 | 321 | "kernelspec": {
|
254 | 322 | "display_name": "Python 3",
|
255 | 323 | "name": "python3"
|
256 |
| - }, |
257 |
| - "language_info": { |
258 |
| - "codemirror_mode": { |
259 |
| - "name": "ipython", |
260 |
| - "version": 3 |
261 |
| - }, |
262 |
| - "file_extension": ".py", |
263 |
| - "mimetype": "text/x-python", |
264 |
| - "name": "python", |
265 |
| - "nbconvert_exporter": "python", |
266 |
| - "pygments_lexer": "ipython3", |
267 |
| - "version": "3.6.12" |
268 | 324 | }
|
269 | 325 | },
|
270 | 326 | "nbformat": 4,
|
|
0 commit comments