1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "metadata": {
6    "id": "cedf868076a2"
7   },
8   "source": [
9    "##### Copyright 2020 The Cirq Developers"
10   ]
11  },
12  {
13   "cell_type": "code",
14   "execution_count": 1,
15   "metadata": {
16    "cellView": "form",
17    "id": "906e07f6e562"
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  },
34  {
35   "cell_type": "markdown",
36   "metadata": {
37    "id": "bf6694967996"
38   },
39   "source": [
40    "# Quantum simulation of electronic structure"
41   ]
42  },
43  {
44   "cell_type": "markdown",
45   "metadata": {
46    "id": "d96dfab4145b"
47   },
48   "source": [
49    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
50    "  <td>\n",
51    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/tutorials/educators/chemistry\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
52    "  </td>\n",
53    "  <td>\n",
54    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/master/docs/tutorials/educators/chemistry.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
55    "  </td>\n",
56    "  <td>\n",
57    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/master/docs/tutorials/educators/chemistry.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
58    "  </td>\n",
59    "  <td>\n",
60    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/tutorials/educators/chemistry.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
61    "  </td>\n",
62    "</table>"
63   ]
64  },
65  {
66   "cell_type": "markdown",
67   "metadata": {
68    "id": "c3dd9da6211f"
69   },
70   "source": [
71    "The quantum simulation of electronic structure is one of the most promising applications of quantum computers. It has potential applications to materials and drug design. This tutorial provides an introduction to OpenFermion, a library for obtaining and manipulating representations of fermionic and qubit Hamiltonians as well as compiling quantum simulation circuits in Cirq."
72   ]
73  },
74  {
75   "cell_type": "code",
76   "execution_count": 2,
77   "metadata": {
78    "id": "zeDOA0lc6YyI"
79   },
80   "outputs": [],
81   "source": [
82    "try:\n",
83    "    import openfermion as of\n",
84    "    import openfermionpyscf as ofpyscf\n",
85    "except ImportError:\n",
86    "    print(\"Installing OpenFermion and OpenFermion-PySCF...\")\n",
87    "    !pip install openfermion openfermionpyscf --quiet"
88   ]
89  },
90  {
91   "cell_type": "code",
92   "execution_count": 3,
93   "metadata": {
94    "id": "d1ed5395e9f5"
95   },
96   "outputs": [],
97   "source": [
98    "import numpy as np\n",
99    "from scipy.sparse import linalg\n",
100    "\n",
101    "import cirq\n",
102    "import openfermion as of\n",
103    "import openfermionpyscf as ofpyscf"
104   ]
105  },
106  {
107   "cell_type": "markdown",
108   "metadata": {
109    "id": "GzfsOoPI5sHP"
110   },
111   "source": [
112    "## Background\n",
113    "\n",
114    "A system of $N$ fermionic modes is\n",
115    "described by a set of fermionic *annihilation operators*\n",
116    "$\\{a_p\\}_{p=0}^{N-1}$ satisfying the *canonical anticommutation relations*\n",
117    "$$\\begin{aligned}\n",
118    "    \\{a_p, a_q\\} &= 0, \\\\\n",
119    "    \\{a_p, a^\\dagger_q\\} &= \\delta_{pq},\n",
120    "  \\end{aligned}$$ where $\\{A, B\\} := AB + BA$. The adjoint\n",
121    "$a^\\dagger_p$ of an annihilation operator $a_p$ is called a *creation\n",
122    "operator*, and we refer to creation and annihilation operators as\n",
123    "fermionic *ladder operators*.\n",
124    "    \n",
125    "The canonical anticommutation relations impose a number of consequences on the structure of the vector space on which the ladder operators act; see [Michael Nielsen's notes](http://michaelnielsen.org/blog/archive/notes/fermions_and_jordan_wigner.pdf) for a good discussion.\n",
126    "\n",
127    "The electronic structure Hamiltonian is commonly written in the form\n",
128    "$$\n",
129    "\\sum_{pq} T_{pq} a_p^\\dagger a_q + \\sum_{pqrs} V_{pqrs} a_p^\\dagger a_q^\\dagger a_r a_s\n",
130    "$$\n",
131    "where the $T_{pq}$ and $V_{pqrs}$ are coefficients which depend on the physical system being described. We are interested in calculating the lowest eigenvalue of the Hamiltonian. This eigenvalue is also called the ground state energy.\n",
132    "\n",
133    "\n",
134    "## FermionOperator and QubitOperator\n",
135    "\n",
136    "### `openfermion.FermionOperator`\n",
137    "\n",
138    "- Stores a weighted sum (linear combination) of fermionic terms\n",
139    "- A fermionic term is a product of ladder operators\n",
140    "- Examples of things that can be represented by `FermionOperator`:\n",
141    "$$\n",
142    "\\begin{align}\n",
143    "& a_1 \\nonumber \\\\\n",
144    "& 1.7 a^\\dagger_3 \\nonumber \\\\\n",
145    "&-1.7 \\, a^\\dagger_3 a_1 \\nonumber \\\\\n",
146    "&(1 + 2i) \\, a^\\dagger_4 a^\\dagger_3 a_9 a_1 \\nonumber \\\\\n",
147    "&(1 + 2i) \\, a^\\dagger_4 a^\\dagger_3 a_9 a_1 - 1.7 \\, a^\\dagger_3 a_1 \\nonumber\n",
148    "\\end{align}\n",
149    "$$\n",
150    "\n",
151    "- A fermionic term is internally represented as a tuple of tuples\n",
152    "- Each inner tuple represents a single ladder operator as (index, action)\n",
153    "- Examples of fermionic terms:\n",
154    "$$\n",
155    "\\begin{align}\n",
156    "I & \\mapsto () \\nonumber \\\\\n",
157    "a_1 & \\mapsto ((1, 0),) \\nonumber \\\\\n",
158    "a^\\dagger_3 & \\mapsto ((3, 1),) \\nonumber \\\\\n",
159    "a^\\dagger_3 a_1 & \\mapsto ((3, 1), (1, 0)) \\nonumber \\\\\n",
160    "a^\\dagger_4 a^\\dagger_3 a_9 a_1 & \\mapsto ((4, 1), (3, 1), (9, 0), (1, 0)) \\nonumber\n",
161    "\\end{align}\n",
162    "$$\n",
163    "\n",
164    "- `FermionOperator` is a sum of terms, represented as a dictionary from term to coefficient"
165   ]
166  },
167  {
168   "cell_type": "code",
169   "execution_count": 4,
170   "metadata": {
171    "id": "u1J3mO4r5sHR"
172   },
173   "outputs": [
174    {
175     "name": "stdout",
176     "output_type": "stream",
177     "text": [
178      "{((4, 1), (3, 1), (9, 0), (1, 0)): (1+2j), ((3, 1), (1, 0)): -1.7}\n"
179     ]
180    }
181   ],
182   "source": [
183    "op = of.FermionOperator(((4, 1), (3, 1), (9, 0), (1, 0)), 1+2j) + of.FermionOperator(((3, 1), (1, 0)), -1.7)\n",
184    "\n",
185    "print(op.terms)"
186   ]
187  },
188  {
189   "cell_type": "markdown",
190   "metadata": {
191    "id": "6gMYqNFi5sHV"
192   },
193   "source": [
194    "Alternative notation, useful when playing around:\n",
195    "\n",
196    "$$\n",
197    "\\begin{align}\n",
198    "I & \\mapsto \\textrm{\"\"} \\nonumber \\\\\n",
199    "a_1 & \\mapsto \\textrm{\"1\"} \\nonumber \\\\\n",
200    "a^\\dagger_3 & \\mapsto \\textrm{\"3^\"} \\nonumber \\\\\n",
201    "a^\\dagger_3 a_1 & \\mapsto \\textrm{\"3^}\\;\\textrm{1\"} \\nonumber \\\\\n",
202    "a^\\dagger_4 a^\\dagger_3 a_9 a_1 & \\mapsto \\textrm{\"4^}\\;\\textrm{3^}\\;\\textrm{9}\\;\\textrm{1\"} \\nonumber\n",
203    "\\end{align}\n",
204    "$$"
205   ]
206  },
207  {
208   "cell_type": "code",
209   "execution_count": 5,
210   "metadata": {
211    "id": "w55QckU25sHW"
212   },
213   "outputs": [
214    {
215     "name": "stdout",
216     "output_type": "stream",
217     "text": [
218      "{((4, 1), (3, 1), (9, 0), (1, 0)): (1+2j), ((3, 1), (1, 0)): -1.7}\n"
219     ]
220    }
221   ],
222   "source": [
223    "op = of.FermionOperator('4^ 3^ 9 1', 1+2j) + of.FermionOperator('3^ 1', -1.7)\n",
224    "\n",
225    "print(op.terms)"
226   ]
227  },
228  {
229   "cell_type": "markdown",
230   "metadata": {
231    "id": "hRrwfjR55sHZ"
232   },
233   "source": [
234    "Just print the operator for a nice readable representation:"
235   ]
236  },
237  {
238   "cell_type": "code",
239   "execution_count": 6,
240   "metadata": {
241    "id": "VYnPilSU5sHa"
242   },
243   "outputs": [
244    {
245     "name": "stdout",
246     "output_type": "stream",
247     "text": [
248      "-1.7 [3^ 1] +\n",
249      "(1+2j) [4^ 3^ 9 1]\n"
250     ]
251    }
252   ],
253   "source": [
254    "print(op)"
255   ]
256  },
257  {
258   "cell_type": "markdown",
259   "metadata": {
260    "id": "vvZxYedI5sHw"
261   },
262   "source": [
263    "### `openfermion.QubitOperator`\n",
264    "\n",
265    "Same as `FermionOperator`, but the possible actions are 'X', 'Y', and 'Z' instead of 1 and 0."
266   ]
267  },
268  {
269   "cell_type": "code",
270   "execution_count": 7,
271   "metadata": {
272    "id": "79UFeqFz5sHw"
273   },
274   "outputs": [
275    {
276     "name": "stdout",
277     "output_type": "stream",
278     "text": [
279      "1.0 [X1 Y2 Z3] +\n",
280      "3.0 [X3 Z4]\n"
281     ]
282    }
283   ],
284   "source": [
285    "op = of.QubitOperator(((1, 'X'), (2, 'Y'), (3, 'Z')))\n",
286    "op += of.QubitOperator('X3 Z4', 3.0)\n",
287    "\n",
288    "print(op)"
289   ]
290  },
291  {
292   "cell_type": "markdown",
293   "metadata": {
294    "id": "v_PQSbeB5sH1"
295   },
296   "source": [
297    "`FermionOperator` and `QubitOperator` actually inherit from the same parent class: `openfermion.SymbolicOperator`."
298   ]
299  },
300  {
301   "cell_type": "markdown",
302   "metadata": {
303    "id": "AE28NBCu5sH5"
304   },
305   "source": [
306    "## The Jordan-Wigner and Bravyi-Kitaev transforms\n",
307    "\n",
308    "A fermionic transform maps `FermionOperator`s to `QubitOperator`s in a way that preserves the canonical anticommutation relations. The most basic transforms are the Jordan-Wigner transform (JWT) and Bravyi-Kitaev transform (BKT). Note that the BKT requires the total number of qubits to be predetermined. Whenever a fermionic transform is being applied implicitly, it is the JWT."
309   ]
310  },
311  {
312   "cell_type": "code",
313   "execution_count": 8,
314   "metadata": {
315    "id": "Cum2qpwA5sH6"
316   },
317   "outputs": [
318    {
319     "name": "stdout",
320     "output_type": "stream",
321     "text": [
322      "(0.25+0j) [X2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 X15] +\n",
323      "0.25j [X2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Y15] +\n",
324      "-0.25j [Y2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 X15] +\n",
325      "(0.25+0j) [Y2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Y15]\n",
326      "\n",
327      "(-0.25+0j) [Z1 X2 X3 X7 Z15] +\n",
328      "-0.25j [Z1 X2 X3 Y7 Z11 Z13 Z14] +\n",
329      "0.25j [Z1 Y2 X3 X7 Z15] +\n",
330      "(-0.25+0j) [Z1 Y2 X3 Y7 Z11 Z13 Z14]\n"
331     ]
332    }
333   ],
334   "source": [
335    "op = of.FermionOperator('2^ 15')\n",
336    "\n",
337    "print(of.jordan_wigner(op))\n",
338    "print()\n",
339    "print(of.bravyi_kitaev(op, n_qubits=16))"
340   ]
341  },
342  {
343   "cell_type": "markdown",
344   "metadata": {
345    "id": "9T6yt_Nu5sH_"
346   },
347   "source": [
348    "### Exercise\n",
349    "\n",
350    "Below are some examples of how `FermionOperator`s are mapped to `QubitOperator`s by the Jordan-Wigner transform (the notation 'h.c.' stands for 'hermitian conjugate'):\n",
351    "$$\n",
352    "\\begin{align*}\n",
353    "    a_p^\\dagger &\\mapsto \\frac12 (X_p - i Y_p) Z_0 \\cdots Z_{p-1}\\\\\n",
354    "    a_p^\\dagger a_p &\\mapsto \\frac12 (I - Z_p)\\\\\n",
355    "    (\\beta a_p^\\dagger a_q + \\text{h.c.}) &\\mapsto \\frac12 [\\text{Re}(\\beta) (X_p ZZ \\cdots ZZ X_q + Y_p ZZ \\cdots ZZ Y_q) + \\text{Im}(\\beta) (Y_p ZZ \\cdots ZZ X_q - X_p ZZ \\cdots ZZ Y_q)]\n",
356    "\\end{align*}\n",
357    "$$\n",
358    "Verify these mappings for $p=2$ and $q=7$. The `openfermion.hermitian_conjugated` function may be useful here."
359   ]
360  },
361  {
362   "cell_type": "code",
363   "execution_count": 9,
364   "metadata": {
365    "id": "o1bOGrkvUHVq"
366   },
367   "outputs": [
368    {
369     "name": "stdout",
370     "output_type": "stream",
371     "text": [
372      "0.5 [Z0 Z1 X2] +\n",
373      "0.5j [Z0 Z1 Y2]\n",
374      "\n",
375      "(0.5+0j) [] +\n",
376      "(-0.5+0j) [Z2]\n",
377      "\n",
378      "(0.5+0j) [X2 Z3 Z4 Z5 Z6 X7] +\n",
379      "(-1+0j) [X2 Z3 Z4 Z5 Z6 Y7] +\n",
380      "(1+0j) [Y2 Z3 Z4 Z5 Z6 X7] +\n",
381      "(0.5+0j) [Y2 Z3 Z4 Z5 Z6 Y7]\n"
382     ]
383    }
384   ],
385   "source": [
386    "a2 = of.FermionOperator('2')\n",
387    "print(of.jordan_wigner(a2))\n",
388    "print()\n",
389    "\n",
390    "a2dag = of.FermionOperator('2^')\n",
391    "print(of.jordan_wigner(a2dag*a2))\n",
392    "print()\n",
393    "\n",
394    "a7 = of.FermionOperator('7')\n",
395    "a7dag = of.FermionOperator('7^')\n",
396    "print(of.jordan_wigner((1+2j)*(a2dag*a7) + (1-2j)*(a7dag*a2)))"
397   ]
398  },
399  {
400   "cell_type": "markdown",
401   "metadata": {
402    "id": "gSwBGl9UUy1k"
403   },
404   "source": [
405    "### Solution"
406   ]
407  },
408  {
409   "cell_type": "code",
410   "execution_count": 10,
411   "metadata": {
412    "id": "xwDvR6Ol5sIA"
413   },
414   "outputs": [
415    {
416     "name": "stdout",
417     "output_type": "stream",
418     "text": [
419      "0.5 [Z0 Z1 X2] +\n",
420      "-0.5j [Z0 Z1 Y2]\n",
421      "\n",
422      "(0.5+0j) [] +\n",
423      "(-0.5+0j) [Z2]\n",
424      "\n",
425      "(1+0j) [X2 Z3 Z4 Z5 Z6 X7] +\n",
426      "(-1.5+0j) [X2 Z3 Z4 Z5 Z6 Y7] +\n",
427      "(1.5+0j) [Y2 Z3 Z4 Z5 Z6 X7] +\n",
428      "(1+0j) [Y2 Z3 Z4 Z5 Z6 Y7]\n"
429     ]
430    }
431   ],
432   "source": [
433    "a2 = of.FermionOperator('2')\n",
434    "a2dag = of.FermionOperator('2^')\n",
435    "a7 = of.FermionOperator('7')\n",
436    "a7dag = of.FermionOperator('7^')\n",
437    "\n",
438    "print(of.jordan_wigner(a2dag))\n",
439    "print()\n",
440    "print(of.jordan_wigner(a2dag*a2))\n",
441    "print()\n",
442    "\n",
443    "op = (2+3j)*a2dag*a7\n",
444    "op += of.hermitian_conjugated(op)\n",
445    "print(of.jordan_wigner(op))"
446   ]
447  },
448  {
449   "cell_type": "markdown",
450   "metadata": {
451    "id": "j2BmxWOu5sIC"
452   },
453   "source": [
454    "### Exercise\n",
455    "\n",
456    "Use the `+` and `*` operators to verify that after applying the JWT to ladder operators, the resulting `QubitOperator`s satisfy\n",
457    "$$\n",
458    "\\begin{align}\n",
459    "    a_2 a_7 + a_7 a_2 &= 0 \\\\\n",
460    "    a_2 a_7^\\dagger + a_7^\\dagger a_2 &= 0\\\\\n",
461    "    a_2 a_2^\\dagger + a_2^\\dagger a_2 &= 1\n",
462    "\\end{align}\n",
463    "$$"
464   ]
465  },
466  {
467   "cell_type": "markdown",
468   "metadata": {
469    "id": "VsJlMb_GVU1Y"
470   },
471   "source": [
472    "### Solution"
473   ]
474  },
475  {
476   "cell_type": "code",
477   "execution_count": 11,
478   "metadata": {
479    "id": "yzwsrS1O5sID"
480   },
481   "outputs": [
482    {
483     "name": "stdout",
484     "output_type": "stream",
485     "text": [
486      "0\n",
487      "0\n",
488      "(1+0j) []\n"
489     ]
490    }
491   ],
492   "source": [
493    "a2_jw = of.jordan_wigner(a2)\n",
494    "a2dag_jw = of.jordan_wigner(a2dag)\n",
495    "a7_jw = of.jordan_wigner(a7)\n",
496    "a7dag_jw = of.jordan_wigner(a7dag)\n",
497    "\n",
498    "print(a2_jw * a7_jw + a7_jw * a2_jw)\n",
499    "print(a2_jw * a7dag_jw + a7dag_jw * a2_jw)\n",
500    "print(a2_jw * a2dag_jw + a2dag_jw * a2_jw)"
501   ]
502  },
503  {
504   "cell_type": "markdown",
505   "metadata": {
506    "id": "p1fQ3-DK5sIS"
507   },
508   "source": [
509    "## Array data structures\n",
510    "\n",
511    "- When `FermionOperator`s have specialized structure we can store coefficients in numpy arrays, enabling fast numerical manipulation.\n",
512    "- Array data structures can always be converted to `FermionOperator` using `openfermion.get_fermion_operator`.\n",
513    "\n",
514    "### InteractionOperator\n",
515    "\n",
516    "- Stores the one- and two-body tensors $T_{pq}$ and $V_{pqrs}$ of the molecular Hamiltonian\n",
517    "\n",
518    "$$\n",
519    "\\sum_{pq} T_{pq} a_p^\\dagger a_q + \\sum_{pqrs} V_{pqrs} a_p^\\dagger a_q^\\dagger a_r a_s\n",
520    "$$\n",
521    "\n",
522    "- Default data structure for molecular Hamiltonians\n",
523    "- Convert from `FermionOperator` using `openfermion.get_interaction_operator`\n",
524    "\n",
525    "### DiagonalCoulombHamiltonian\n",
526    "\n",
527    "- Stores the one- and two-body coefficient matrices $T_{pq}$ and $V_{pq}$ of a Hamiltonian with a diagonal Coulomb term:\n",
528    "\n",
529    "$$\n",
530    "\\sum_{pq} T_{pq} a_p^\\dagger a_q + \\sum_{pq} V_{pq} a_p^\\dagger a_p a_q^\\dagger a_q\n",
531    "$$\n",
532    "\n",
533    "- Leads to especially efficient algorithms for quantum simulation\n",
534    "- Convert from `FermionOperator` using `openfermion.get_diagonal_coulomb_hamiltonian`\n",
535    "\n",
536    "### QuadraticHamiltonian\n",
537    "\n",
538    "- Stores the Hermitian matrix $M_{pq}$ and antisymmetric matrix $\\Delta_{pq}$ describing a general quadratic Hamiltonian\n",
539    "\n",
540    "$$\n",
541    "\\sum_{p, q} M_{pq} a^\\dagger_p a_q\n",
542    "+ \\frac12 \\sum_{p, q}\n",
543    "    (\\Delta_{pq} a^\\dagger_p a^\\dagger_q + \\text{h.c.})\n",
544    "$$\n",
545    "\n",
546    "- Routines included for efficient diagonalization (can handle thousands of fermionic modes)\n",
547    "- Convert from `FermionOperator` using `openfermion.get_quadratic_hamiltonian`"
548   ]
549  },
550  {
551   "cell_type": "markdown",
552   "metadata": {
553    "id": "fN6Bq0gbdK5G"
554   },
555   "source": [
556    "## Generating the Hamiltonian for a molecule"
557   ]
558  },
559  {
560   "cell_type": "markdown",
561   "metadata": {
562    "id": "l_UXxxqw5sIW"
563   },
564   "source": [
565    "The cell below demonstrates using one of our electronic structure package plugins, OpenFermion-PySCF, to generate a molecular Hamiltonian for a hydrogen molecule. Note that the Hamiltonian is returned as an `InteractionOperator`. We'll convert it to a `FermionOperator` and print the result."
566   ]
567  },
568  {
569   "cell_type": "code",
570   "execution_count": 12,
571   "metadata": {
572    "id": "r8mmL0aj5sIW"
573   },
574   "outputs": [
575    {
576     "name": "stdout",
577     "output_type": "stream",
578     "text": [
579      "0.66147151365 [] +\n",
580      "-1.2178260299951058 [0^ 0] +\n",
581      "0.3316650744318082 [0^ 0^ 0 0] +\n",
582      "0.09231339177803066 [0^ 0^ 2 2] +\n",
583      "0.3316650744318082 [0^ 1^ 1 0] +\n",
584      "0.09231339177803066 [0^ 1^ 3 2] +\n",
585      "0.09231339177803066 [0^ 2^ 0 2] +\n",
586      "0.3267206861819477 [0^ 2^ 2 0] +\n",
587      "0.09231339177803066 [0^ 3^ 1 2] +\n",
588      "0.3267206861819477 [0^ 3^ 3 0] +\n",
589      "0.3316650744318082 [1^ 0^ 0 1] +\n",
590      "0.09231339177803066 [1^ 0^ 2 3] +\n",
591      "-1.2178260299951058 [1^ 1] +\n",
592      "0.3316650744318082 [1^ 1^ 1 1] +\n",
593      "0.09231339177803066 [1^ 1^ 3 3] +\n",
594      "0.09231339177803066 [1^ 2^ 0 3] +\n",
595      "0.3267206861819477 [1^ 2^ 2 1] +\n",
596      "0.09231339177803066 [1^ 3^ 1 3] +\n",
597      "0.3267206861819477 [1^ 3^ 3 1] +\n",
598      "0.32672068618194783 [2^ 0^ 0 2] +\n",
599      "0.09231339177803066 [2^ 0^ 2 0] +\n",
600      "0.32672068618194783 [2^ 1^ 1 2] +\n",
601      "0.09231339177803066 [2^ 1^ 3 0] +\n",
602      "-0.5096378744364826 [2^ 2] +\n",
603      "0.09231339177803066 [2^ 2^ 0 0] +\n",
604      "0.34339576784573445 [2^ 2^ 2 2] +\n",
605      "0.09231339177803066 [2^ 3^ 1 0] +\n",
606      "0.34339576784573445 [2^ 3^ 3 2] +\n",
607      "0.32672068618194783 [3^ 0^ 0 3] +\n",
608      "0.09231339177803066 [3^ 0^ 2 1] +\n",
609      "0.32672068618194783 [3^ 1^ 1 3] +\n",
610      "0.09231339177803066 [3^ 1^ 3 1] +\n",
611      "0.09231339177803066 [3^ 2^ 0 1] +\n",
612      "0.34339576784573445 [3^ 2^ 2 3] +\n",
613      "-0.5096378744364826 [3^ 3] +\n",
614      "0.09231339177803066 [3^ 3^ 1 1] +\n",
615      "0.34339576784573445 [3^ 3^ 3 3]\n"
616     ]
617    }
618   ],
619   "source": [
620    "# Set molecule parameters\n",
621    "geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.8))]\n",
622    "basis = 'sto-3g'\n",
623    "multiplicity = 1\n",
624    "charge = 0\n",
625    "\n",
626    "# Perform electronic structure calculations and\n",
627    "# obtain Hamiltonian as an InteractionOperator\n",
628    "hamiltonian = ofpyscf.generate_molecular_hamiltonian(\n",
629    "    geometry, basis, multiplicity, charge)\n",
630    "\n",
631    "# Convert to a FermionOperator\n",
632    "hamiltonian_ferm_op = of.get_fermion_operator(hamiltonian)\n",
633    "\n",
634    "print(hamiltonian_ferm_op)"
635   ]
636  },
637  {
638   "cell_type": "markdown",
639   "metadata": {
640    "id": "jWxU4bG1oYfZ"
641   },
642   "source": [
643    "Let's calculate the ground energy (lowest eigenvalue) of the Hamiltonian. First, we'll map the `FermionOperator` to a `QubitOperator` using the JWT. Then, we'll convert the `QubitOperator` to a SciPy sparse matrix and get its lowest eigenvalue."
644   ]
645  },
646  {
647   "cell_type": "code",
648   "execution_count": 13,
649   "metadata": {
650    "id": "rIEH_5eLaxvG"
651   },
652   "outputs": [
653    {
654     "name": "stdout",
655     "output_type": "stream",
656     "text": [
657      "Ground_energy: -1.134147666677095\n",
658      "JWT transformed Hamiltonian:\n",
659      "(-0.16733398905695201+0j) [] +\n",
660      "(-0.04615669588901533+0j) [X0 X1 Y2 Y3] +\n",
661      "(0.04615669588901533+0j) [X0 Y1 Y2 X3] +\n",
662      "(0.04615669588901533+0j) [Y0 X1 X2 Y3] +\n",
663      "(-0.04615669588901533+0j) [Y0 Y1 X2 X3] +\n",
664      "(0.16251648748871642+0j) [Z0] +\n",
665      "(0.1658325372159041+0j) [Z0 Z1] +\n",
666      "(0.11720364720195856+0j) [Z0 Z2] +\n",
667      "(0.1633603430909739+0j) [Z0 Z3] +\n",
668      "(0.16251648748871636+0j) [Z1] +\n",
669      "(0.1633603430909739+0j) [Z1 Z2] +\n",
670      "(0.11720364720195856+0j) [Z1 Z3] +\n",
671      "(-0.1974429369975584+0j) [Z2] +\n",
672      "(0.17169788392286722+0j) [Z2 Z3] +\n",
673      "(-0.19744293699755838+0j) [Z3]\n"
674     ]
675    }
676   ],
677   "source": [
678    "# Map to QubitOperator using the JWT\n",
679    "hamiltonian_jw = of.jordan_wigner(hamiltonian_ferm_op)\n",
680    "\n",
681    "# Convert to Scipy sparse matrix\n",
682    "hamiltonian_jw_sparse = of.get_sparse_operator(hamiltonian_jw)\n",
683    "\n",
684    "# Compute ground energy\n",
685    "eigs, _ = linalg.eigsh(hamiltonian_jw_sparse, k=1, which='SA')\n",
686    "ground_energy = eigs[0]\n",
687    "\n",
688    "print('Ground_energy: {}'.format(ground_energy))\n",
689    "print('JWT transformed Hamiltonian:')\n",
690    "print(hamiltonian_jw)"
691   ]
692  },
693  {
694   "cell_type": "markdown",
695   "metadata": {
696    "id": "gjUBgFx-axmm"
697   },
698   "source": [
699    "### Exercise\n",
700    "Compute the ground energy of the same Hamiltonian, but via the Bravyi-Kitaev transform. Verify that you get the same value."
701   ]
702  },
703  {
704   "cell_type": "code",
705   "execution_count": 14,
706   "metadata": {
707    "id": "ID2vls21XKj-"
708   },
709   "outputs": [
710    {
711     "name": "stdout",
712     "output_type": "stream",
713     "text": [
714      "Ground_energy: -1.1341476666770918\n",
715      "BK transformed Hamiltonian:\n",
716      "(-0.16733398905695201+0j) [] +\n",
717      "(0.04615669588901533+0j) [X0 Z1 X2] +\n",
718      "(0.04615669588901533+0j) [X0 Z1 X2 Z3] +\n",
719      "(0.04615669588901533+0j) [Y0 Z1 Y2] +\n",
720      "(0.04615669588901533+0j) [Y0 Z1 Y2 Z3] +\n",
721      "(0.16251648748871642+0j) [Z0] +\n",
722      "(0.16251648748871636+0j) [Z0 Z1] +\n",
723      "(0.1633603430909739+0j) [Z0 Z1 Z2] +\n",
724      "(0.1633603430909739+0j) [Z0 Z1 Z2 Z3] +\n",
725      "(0.11720364720195856+0j) [Z0 Z2] +\n",
726      "(0.11720364720195856+0j) [Z0 Z2 Z3] +\n",
727      "(0.1658325372159041+0j) [Z1] +\n",
728      "(-0.19744293699755838+0j) [Z1 Z2 Z3] +\n",
729      "(0.17169788392286722+0j) [Z1 Z3] +\n",
730      "(-0.1974429369975584+0j) [Z2]\n"
731     ]
732    }
733   ],
734   "source": [
735    "# Map to QubitOperator using the JWT\n",
736    "hamiltonian_bk = of.bravyi_kitaev(hamiltonian_ferm_op)\n",
737    "\n",
738    "# Convert to Scipy sparse matrix\n",
739    "hamiltonian_bk_sparse = of.get_sparse_operator(hamiltonian_bk)\n",
740    "\n",
741    "# Compute ground energy\n",
742    "eigs, _ = linalg.eigsh(hamiltonian_bk_sparse, k=1, which='SA')\n",
743    "ground_energy = eigs[0]\n",
744    "\n",
745    "print('Ground_energy: {}'.format(ground_energy))\n",
746    "print('BK transformed Hamiltonian:')\n",
747    "print(hamiltonian_bk)"
748   ]
749  },
750  {
751   "cell_type": "markdown",
752   "metadata": {
753    "id": "dEqS3XV0eXx1"
754   },
755   "source": [
756    "### Solution"
757   ]
758  },
759  {
760   "cell_type": "code",
761   "execution_count": 15,
762   "metadata": {
763    "id": "9Ss_OhXo5sIP"
764   },
765   "outputs": [
766    {
767     "name": "stdout",
768     "output_type": "stream",
769     "text": [
770      "Ground_energy: -1.134147666677097\n",
771      "BKT transformed Hamiltonian:\n",
772      "(-0.16733398905695201+0j) [] +\n",
773      "(0.04615669588901533+0j) [X0 Z1 X2] +\n",
774      "(0.04615669588901533+0j) [X0 Z1 X2 Z3] +\n",
775      "(0.04615669588901533+0j) [Y0 Z1 Y2] +\n",
776      "(0.04615669588901533+0j) [Y0 Z1 Y2 Z3] +\n",
777      "(0.16251648748871642+0j) [Z0] +\n",
778      "(0.16251648748871636+0j) [Z0 Z1] +\n",
779      "(0.1633603430909739+0j) [Z0 Z1 Z2] +\n",
780      "(0.1633603430909739+0j) [Z0 Z1 Z2 Z3] +\n",
781      "(0.11720364720195856+0j) [Z0 Z2] +\n",
782      "(0.11720364720195856+0j) [Z0 Z2 Z3] +\n",
783      "(0.1658325372159041+0j) [Z1] +\n",
784      "(-0.19744293699755838+0j) [Z1 Z2 Z3] +\n",
785      "(0.17169788392286722+0j) [Z1 Z3] +\n",
786      "(-0.1974429369975584+0j) [Z2]\n"
787     ]
788    }
789   ],
790   "source": [
791    "# Map to QubitOperator using the BKT\n",
792    "hamiltonian_bk = of.bravyi_kitaev(hamiltonian_ferm_op)\n",
793    "\n",
794    "# Convert to Scipy sparse matrix\n",
795    "hamiltonian_bk_sparse = of.get_sparse_operator(hamiltonian_bk)\n",
796    "\n",
797    "# Compute ground state energy\n",
798    "eigs, _ = linalg.eigsh(hamiltonian_bk_sparse, k=1, which='SA')\n",
799    "ground_energy = eigs[0]\n",
800    "\n",
801    "print('Ground_energy: {}'.format(ground_energy))\n",
802    "print('BKT transformed Hamiltonian:')\n",
803    "print(hamiltonian_bk)"
804   ]
805  },
806  {
807   "cell_type": "markdown",
808   "metadata": {
809    "id": "H0DLpARr5sIb"
810   },
811   "source": [
812    "### Exercise\n",
813    "\n",
814    "- The BCS mean-field d-wave model of superconductivity has the Hamiltonian\n",
815    "\n",
816    "$$\n",
817    "H = - t \\sum_{\\langle i,j \\rangle} \\sum_\\sigma\n",
818    "        (a^\\dagger_{i, \\sigma} a_{j, \\sigma} +\n",
819    "         a^\\dagger_{j, \\sigma} a_{i, \\sigma})\n",
820    "    - \\sum_{\\langle i,j \\rangle} \\Delta_{ij}\n",
821    "      (a^\\dagger_{i, \\uparrow} a^\\dagger_{j, \\downarrow} -\n",
822    "       a^\\dagger_{i, \\downarrow} a^\\dagger_{j, \\uparrow} +\n",
823    "       a_{j, \\downarrow} a_{i, \\uparrow} -\n",
824    "       a_{j, \\uparrow} a_{i, \\downarrow})\n",
825    "$$\n",
826    "\n",
827    "Use the `mean_field_dwave` function to generate an instance of this model with dimensions 10x10.\n",
828    " - Convert the Hamiltonian to a `QubitOperator` with the JWT. What is the length of the longest Pauli string that appears?\n",
829    " - Convert the Hamiltonian to a `QubitOperator` with the BKT. What is the length of the longest Pauli string that appears?\n",
830    " - Convert the Hamiltonian to a `QuadraticHamiltonian`. Get its ground energy using the `ground_energy` method of `QuadraticHamiltonian`. What would happen if you tried to compute the ground energy by converting to a sparse matrix?"
831   ]
832  },
833  {
834   "cell_type": "markdown",
835   "metadata": {
836    "id": "Zrnv4DLRuFJM"
837   },
838   "source": [
839    "## Hamiltonian simulation with Trotter formulas\n",
840    "\n",
841    "- Goal: apply $\\exp(-i H t)$ where $H = \\sum_j H_j$\n",
842    "- Use an approximation such as $\\exp(-i H t) \\approx (\\prod_{j=1} \\exp(-i H_j t/r))^r$\n",
843    "- Exposed via the `openfermion.simulate_trotter` function\n",
844    "- Currently implemented algorithms are from [arXiv:1706.00023](https://arxiv.org/pdf/1706.00023.pdf), [arXiv:1711.04789](https://arxiv.org/pdf/1711.04789.pdf), and [arXiv:1808.02625](https://arxiv.org/pdf/1808.02625.pdf), and are based on the JWT\n",
845    "- Currently supported Hamiltonian types: `DiagonalCoulombHamiltonian` and `InteractionOperator`\n",
846    "\n",
847    "As a demonstration, we'll simulate time evolution under the hydrogen molecule Hamiltonian we generated earlier.\n",
848    "\n",
849    "First, let's create a random initial state and apply the exact time evolution by matrix exponentiation:\n",
850    "\n",
851    "$$\n",
852    "\\lvert \\psi \\rangle \\mapsto \\exp(-i H t) \\lvert \\psi \\rangle\n",
853    "$$"
854   ]
855  },
856  {
857   "cell_type": "code",
858   "execution_count": 16,
859   "metadata": {
860    "id": "_e5mbQI61fdT"
861   },
862   "outputs": [],
863   "source": [
864    "# Create a random initial state\n",
865    "n_qubits = of.count_qubits(hamiltonian)\n",
866    "initial_state = of.haar_random_vector(2**n_qubits, seed=7)\n",
867    "\n",
868    "# Set evolution time\n",
869    "time = 1.0\n",
870    "\n",
871    "# Apply exp(-i H t) to the state\n",
872    "exact_state = linalg.expm_multiply(-1j*hamiltonian_jw_sparse*time, initial_state)"
873   ]
874  },
875  {
876   "cell_type": "markdown",
877   "metadata": {
878    "id": "_OZPjkN513zJ"
879   },
880   "source": [
881    "Now, let's create a circuit to perform the evolution and compare the fidelity of the resulting state with the one from exact evolution. The fidelity can be increased by increasing the number of Trotter steps. Note that the Hamiltonian input to `openfermion.simulate_trotter` should be an `InteractionOperator`, not a `FermionOperator`."
882   ]
883  },
884  {
885   "cell_type": "code",
886   "execution_count": 17,
887   "metadata": {
888    "id": "1DXSZ54u12Tt"
889   },
890   "outputs": [
891    {
892     "name": "stdout",
893     "output_type": "stream",
894     "text": [
895      "0.9999820449924582\n"
896     ]
897    }
898   ],
899   "source": [
900    "# Initialize qubits\n",
901    "qubits = cirq.LineQubit.range(n_qubits)\n",
902    "\n",
903    "# Create circuit\n",
904    "circuit = cirq.Circuit(\n",
905    "    of.simulate_trotter(\n",
906    "       qubits, hamiltonian, time,\n",
907    "       n_steps=10,\n",
908    "       order=0,\n",
909    "       algorithm=of.LOW_RANK)\n",
910    ")\n",
911    "\n",
912    "# Apply the circuit to the initial state\n",
913    "result = circuit.final_state_vector(initial_state)\n",
914    "\n",
915    "# Compute the fidelity with the final state from exact evolution\n",
916    "fidelity = abs(np.dot(exact_state, result.conj()))**2\n",
917    "\n",
918    "print(fidelity)"
919   ]
920  },
921  {
922   "cell_type": "code",
923   "execution_count": 18,
924   "metadata": {
925    "id": "tG4PCh4faFTL"
926   },
927   "outputs": [
928    {
929     "name": "stdout",
930     "output_type": "stream",
931     "text": [
932      "0             1                  2                3\n",
933      "│             │                  │                │\n",
934      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
935      "│             │                  │                │\n",
936      "Rz(0.052π)    Rz(0.052π)         Rz(0.03π)        Rz(0.03π)\n",
937      "│             │                  │                │\n",
938      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
939      "│             │                  │                │\n",
940      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
941      "│             │                  │                │\n",
942      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
943      "│             │                  │                │\n",
944      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
945      "│             │                  │                │\n",
946      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
947      "│             │                  │                │\n",
948      "@─────────────@^-0.022           Z^0              │\n",
949      "│             │                  │                │\n",
950      "×─────────────×                  @────────────────@^-0.021\n",
951      "│             │                  │                │\n",
952      "│             │                  ×────────────────×\n",
953      "│             │                  │                │\n",
954      "│             @──────────────────@^-0.021         │\n",
955      "│             │                  │                │\n",
956      "│             ×──────────────────×                │\n",
957      "│             │                  │                │\n",
958      "@─────────────@^-0.021           @────────────────@^-0.021\n",
959      "│             │                  │                │\n",
960      "×─────────────×                  ×────────────────×\n",
961      "│             │                  │                │\n",
962      "Rz(-0.01π)    @──────────────────@^-0.021         Rz(-0.011π)\n",
963      "│             │                  │                │\n",
964      "Rz(π)         ×──────────────────×                Rz(π)\n",
965      "│             │                  │                │\n",
966      "│             Rz(-0.01π)         Rz(-0.011π)      │\n",
967      "│             │                  │                │\n",
968      "│             Rz(0)              Rz(0)            │\n",
969      "│             │                  │                │\n",
970      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
971      "│             │                  │                │\n",
972      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
973      "│             │                  │                │\n",
974      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
975      "│             │                  │                │\n",
976      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
977      "│             │                  │                │\n",
978      "│             Z^0                @────────────────@^-0.006\n",
979      "│             │                  │                │\n",
980      "@─────────────@^-0.006           ×────────────────×\n",
981      "│             │                  │                │\n",
982      "×─────────────×                  │                │\n",
983      "│             │                  │                │\n",
984      "│             @──────────────────@^0.006          │\n",
985      "│             │                  │                │\n",
986      "│             ×──────────────────×                │\n",
987      "│             │                  │                │\n",
988      "@─────────────@^0.006            @────────────────@^0.006\n",
989      "│             │                  │                │\n",
990      "×─────────────×                  ×────────────────×\n",
991      "│             │                  │                │\n",
992      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
993      "│             │                  │                │\n",
994      "Rz(π)         ×──────────────────×                Rz(0)\n",
995      "│             │                  │                │\n",
996      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
997      "│             │                  │                │\n",
998      "│             Rz(0)              Rz(π)            │\n",
999      "│             │                  │                │\n",
1000      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1001      "│             │                  │                │\n",
1002      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1003      "│             │                  │                │\n",
1004      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1005      "│             │                  │                │\n",
1006      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1007      "│             │                  │                │\n",
1008      "@─────────────@^0                Z^0              │\n",
1009      "│             │                  │                │\n",
1010      "×─────────────×                  @────────────────@^0\n",
1011      "│             │                  │                │\n",
1012      "│             │                  ×────────────────×\n",
1013      "│             │                  │                │\n",
1014      "│             @──────────────────@^0              │\n",
1015      "│             │                  │                │\n",
1016      "│             ×──────────────────×                │\n",
1017      "│             │                  │                │\n",
1018      "@─────────────@^0                @────────────────@^0\n",
1019      "│             │                  │                │\n",
1020      "×─────────────×                  ×────────────────×\n",
1021      "│             │                  │                │\n",
1022      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1023      "│             │                  │                │\n",
1024      "Rz(0)         ×──────────────────×                Rz(0)\n",
1025      "│             │                  │                │\n",
1026      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1027      "│             │                  │                │\n",
1028      "Rz(0.03π)     Rz(0)              Rz(0)            Rz(0.052π)\n",
1029      "│             │                  │                │\n",
1030      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1031      "│             │                  │                │\n",
1032      "│             Rz(0.03π)          Rz(0.052π)       │\n",
1033      "│             │                  │                │\n",
1034      "│             Rz(0)              Rz(0)            │\n",
1035      "│             │                  │                │\n",
1036      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1037      "│             │                  │                │\n",
1038      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1039      "│             │                  │                │\n",
1040      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1041      "│             │                  │                │\n",
1042      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1043      "│             │                  │                │\n",
1044      "│             Z^0                @────────────────@^-0.022\n",
1045      "│             │                  │                │\n",
1046      "@─────────────@^-0.021           ×────────────────×\n",
1047      "│             │                  │                │\n",
1048      "×─────────────×                  │                │\n",
1049      "│             │                  │                │\n",
1050      "│             @──────────────────@^-0.021         │\n",
1051      "│             │                  │                │\n",
1052      "│             ×──────────────────×                │\n",
1053      "│             │                  │                │\n",
1054      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1055      "│             │                  │                │\n",
1056      "×─────────────×                  ×────────────────×\n",
1057      "│             │                  │                │\n",
1058      "Rz(-0.011π)   @──────────────────@^-0.021         Rz(-0.01π)\n",
1059      "│             │                  │                │\n",
1060      "Rz(π)         ×──────────────────×                Rz(π)\n",
1061      "│             │                  │                │\n",
1062      "│             Rz(-0.011π)        Rz(-0.01π)       │\n",
1063      "│             │                  │                │\n",
1064      "│             Rz(0)              Rz(0)            │\n",
1065      "│             │                  │                │\n",
1066      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1067      "│             │                  │                │\n",
1068      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1069      "│             │                  │                │\n",
1070      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1071      "│             │                  │                │\n",
1072      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1073      "│             │                  │                │\n",
1074      "@─────────────@^-0.006           Z^0              │\n",
1075      "│             │                  │                │\n",
1076      "×─────────────×                  @────────────────@^-0.006\n",
1077      "│             │                  │                │\n",
1078      "│             │                  ×────────────────×\n",
1079      "│             │                  │                │\n",
1080      "│             @──────────────────@^0.006          │\n",
1081      "│             │                  │                │\n",
1082      "│             ×──────────────────×                │\n",
1083      "│             │                  │                │\n",
1084      "@─────────────@^0.006            @────────────────@^0.006\n",
1085      "│             │                  │                │\n",
1086      "×─────────────×                  ×────────────────×\n",
1087      "│             │                  │                │\n",
1088      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1089      "│             │                  │                │\n",
1090      "Rz(0)         ×──────────────────×                Rz(π)\n",
1091      "│             │                  │                │\n",
1092      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1093      "│             │                  │                │\n",
1094      "│             Rz(π)              Rz(0)            │\n",
1095      "│             │                  │                │\n",
1096      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1097      "│             │                  │                │\n",
1098      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1099      "│             │                  │                │\n",
1100      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1101      "│             │                  │                │\n",
1102      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1103      "│             │                  │                │\n",
1104      "│             Z^0                @────────────────@^0\n",
1105      "│             │                  │                │\n",
1106      "@─────────────@^0                ×────────────────×\n",
1107      "│             │                  │                │\n",
1108      "×─────────────×                  │                │\n",
1109      "│             │                  │                │\n",
1110      "│             @──────────────────@^0              │\n",
1111      "│             │                  │                │\n",
1112      "│             ×──────────────────×                │\n",
1113      "│             │                  │                │\n",
1114      "@─────────────@^0                @────────────────@^0\n",
1115      "│             │                  │                │\n",
1116      "×─────────────×                  ×────────────────×\n",
1117      "│             │                  │                │\n",
1118      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1119      "│             │                  │                │\n",
1120      "Rz(0)         ×──────────────────×                Rz(0)\n",
1121      "│             │                  │                │\n",
1122      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1123      "│             │                  │                │\n",
1124      "Rz(0.052π)    Rz(0)              Rz(0)            Rz(0.03π)\n",
1125      "│             │                  │                │\n",
1126      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1127      "│             │                  │                │\n",
1128      "│             Rz(0.052π)         Rz(0.03π)        │\n",
1129      "│             │                  │                │\n",
1130      "│             Rz(0)              Rz(0)            │\n",
1131      "│             │                  │                │\n",
1132      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1133      "│             │                  │                │\n",
1134      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1135      "│             │                  │                │\n",
1136      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1137      "│             │                  │                │\n",
1138      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1139      "│             │                  │                │\n",
1140      "@─────────────@^-0.022           Z^0              │\n",
1141      "│             │                  │                │\n",
1142      "×─────────────×                  @────────────────@^-0.021\n",
1143      "│             │                  │                │\n",
1144      "│             │                  ×────────────────×\n",
1145      "│             │                  │                │\n",
1146      "│             @──────────────────@^-0.021         │\n",
1147      "│             │                  │                │\n",
1148      "│             ×──────────────────×                │\n",
1149      "│             │                  │                │\n",
1150      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1151      "│             │                  │                │\n",
1152      "×─────────────×                  ×────────────────×\n",
1153      "│             │                  │                │\n",
1154      "Rz(-0.01π)    @──────────────────@^-0.021         Rz(-0.011π)\n",
1155      "│             │                  │                │\n",
1156      "Rz(π)         ×──────────────────×                Rz(π)\n",
1157      "│             │                  │                │\n",
1158      "│             Rz(-0.01π)         Rz(-0.011π)      │\n",
1159      "│             │                  │                │\n",
1160      "│             Rz(0)              Rz(0)            │\n",
1161      "│             │                  │                │\n",
1162      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1163      "│             │                  │                │\n",
1164      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1165      "│             │                  │                │\n",
1166      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1167      "│             │                  │                │\n",
1168      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1169      "│             │                  │                │\n",
1170      "│             Z^0                @────────────────@^-0.006\n",
1171      "│             │                  │                │\n",
1172      "@─────────────@^-0.006           ×────────────────×\n",
1173      "│             │                  │                │\n",
1174      "×─────────────×                  │                │\n",
1175      "│             │                  │                │\n",
1176      "│             @──────────────────@^0.006          │\n",
1177      "│             │                  │                │\n",
1178      "│             ×──────────────────×                │\n",
1179      "│             │                  │                │\n",
1180      "@─────────────@^0.006            @────────────────@^0.006\n",
1181      "│             │                  │                │\n",
1182      "×─────────────×                  ×────────────────×\n",
1183      "│             │                  │                │\n",
1184      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1185      "│             │                  │                │\n",
1186      "Rz(π)         ×──────────────────×                Rz(0)\n",
1187      "│             │                  │                │\n",
1188      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1189      "│             │                  │                │\n",
1190      "│             Rz(0)              Rz(π)            │\n",
1191      "│             │                  │                │\n",
1192      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1193      "│             │                  │                │\n",
1194      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1195      "│             │                  │                │\n",
1196      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1197      "│             │                  │                │\n",
1198      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1199      "│             │                  │                │\n",
1200      "@─────────────@^0                Z^0              │\n",
1201      "│             │                  │                │\n",
1202      "×─────────────×                  @────────────────@^0\n",
1203      "│             │                  │                │\n",
1204      "│             │                  ×────────────────×\n",
1205      "│             │                  │                │\n",
1206      "│             @──────────────────@^0              │\n",
1207      "│             │                  │                │\n",
1208      "│             ×──────────────────×                │\n",
1209      "│             │                  │                │\n",
1210      "@─────────────@^0                @────────────────@^0\n",
1211      "│             │                  │                │\n",
1212      "×─────────────×                  ×────────────────×\n",
1213      "│             │                  │                │\n",
1214      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1215      "│             │                  │                │\n",
1216      "Rz(0)         ×──────────────────×                Rz(0)\n",
1217      "│             │                  │                │\n",
1218      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1219      "│             │                  │                │\n",
1220      "Rz(0.03π)     Rz(0)              Rz(0)            Rz(0.052π)\n",
1221      "│             │                  │                │\n",
1222      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1223      "│             │                  │                │\n",
1224      "│             Rz(0.03π)          Rz(0.052π)       │\n",
1225      "│             │                  │                │\n",
1226      "│             Rz(0)              Rz(0)            │\n",
1227      "│             │                  │                │\n",
1228      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1229      "│             │                  │                │\n",
1230      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1231      "│             │                  │                │\n",
1232      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1233      "│             │                  │                │\n",
1234      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1235      "│             │                  │                │\n",
1236      "│             Z^0                @────────────────@^-0.022\n",
1237      "│             │                  │                │\n",
1238      "@─────────────@^-0.021           ×────────────────×\n",
1239      "│             │                  │                │\n",
1240      "×─────────────×                  │                │\n",
1241      "│             │                  │                │\n",
1242      "│             @──────────────────@^-0.021         │\n",
1243      "│             │                  │                │\n",
1244      "│             ×──────────────────×                │\n",
1245      "│             │                  │                │\n",
1246      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1247      "│             │                  │                │\n",
1248      "×─────────────×                  ×────────────────×\n",
1249      "│             │                  │                │\n",
1250      "Rz(-0.011π)   @──────────────────@^-0.021         Rz(-0.01π)\n",
1251      "│             │                  │                │\n",
1252      "Rz(π)         ×──────────────────×                Rz(π)\n",
1253      "│             │                  │                │\n",
1254      "│             Rz(-0.011π)        Rz(-0.01π)       │\n",
1255      "│             │                  │                │\n",
1256      "│             Rz(0)              Rz(0)            │\n",
1257      "│             │                  │                │\n",
1258      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1259      "│             │                  │                │\n",
1260      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1261      "│             │                  │                │\n",
1262      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1263      "│             │                  │                │\n",
1264      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1265      "│             │                  │                │\n",
1266      "@─────────────@^-0.006           Z^0              │\n",
1267      "│             │                  │                │\n",
1268      "×─────────────×                  @────────────────@^-0.006\n",
1269      "│             │                  │                │\n",
1270      "│             │                  ×────────────────×\n",
1271      "│             │                  │                │\n",
1272      "│             @──────────────────@^0.006          │\n",
1273      "│             │                  │                │\n",
1274      "│             ×──────────────────×                │\n",
1275      "│             │                  │                │\n",
1276      "@─────────────@^0.006            @────────────────@^0.006\n",
1277      "│             │                  │                │\n",
1278      "×─────────────×                  ×────────────────×\n",
1279      "│             │                  │                │\n",
1280      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1281      "│             │                  │                │\n",
1282      "Rz(0)         ×──────────────────×                Rz(π)\n",
1283      "│             │                  │                │\n",
1284      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1285      "│             │                  │                │\n",
1286      "│             Rz(π)              Rz(0)            │\n",
1287      "│             │                  │                │\n",
1288      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1289      "│             │                  │                │\n",
1290      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1291      "│             │                  │                │\n",
1292      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1293      "│             │                  │                │\n",
1294      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1295      "│             │                  │                │\n",
1296      "│             Z^0                @────────────────@^0\n",
1297      "│             │                  │                │\n",
1298      "@─────────────@^0                ×────────────────×\n",
1299      "│             │                  │                │\n",
1300      "×─────────────×                  │                │\n",
1301      "│             │                  │                │\n",
1302      "│             @──────────────────@^0              │\n",
1303      "│             │                  │                │\n",
1304      "│             ×──────────────────×                │\n",
1305      "│             │                  │                │\n",
1306      "@─────────────@^0                @────────────────@^0\n",
1307      "│             │                  │                │\n",
1308      "×─────────────×                  ×────────────────×\n",
1309      "│             │                  │                │\n",
1310      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1311      "│             │                  │                │\n",
1312      "Rz(0)         ×──────────────────×                Rz(0)\n",
1313      "│             │                  │                │\n",
1314      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1315      "│             │                  │                │\n",
1316      "Rz(0.052π)    Rz(0)              Rz(0)            Rz(0.03π)\n",
1317      "│             │                  │                │\n",
1318      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1319      "│             │                  │                │\n",
1320      "│             Rz(0.052π)         Rz(0.03π)        │\n",
1321      "│             │                  │                │\n",
1322      "│             Rz(0)              Rz(0)            │\n",
1323      "│             │                  │                │\n",
1324      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1325      "│             │                  │                │\n",
1326      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1327      "│             │                  │                │\n",
1328      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1329      "│             │                  │                │\n",
1330      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1331      "│             │                  │                │\n",
1332      "@─────────────@^-0.022           Z^0              │\n",
1333      "│             │                  │                │\n",
1334      "×─────────────×                  @────────────────@^-0.021\n",
1335      "│             │                  │                │\n",
1336      "│             │                  ×────────────────×\n",
1337      "│             │                  │                │\n",
1338      "│             @──────────────────@^-0.021         │\n",
1339      "│             │                  │                │\n",
1340      "│             ×──────────────────×                │\n",
1341      "│             │                  │                │\n",
1342      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1343      "│             │                  │                │\n",
1344      "×─────────────×                  ×────────────────×\n",
1345      "│             │                  │                │\n",
1346      "Rz(-0.01π)    @──────────────────@^-0.021         Rz(-0.011π)\n",
1347      "│             │                  │                │\n",
1348      "Rz(π)         ×──────────────────×                Rz(π)\n",
1349      "│             │                  │                │\n",
1350      "│             Rz(-0.01π)         Rz(-0.011π)      │\n",
1351      "│             │                  │                │\n",
1352      "│             Rz(0)              Rz(0)            │\n",
1353      "│             │                  │                │\n",
1354      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1355      "│             │                  │                │\n",
1356      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1357      "│             │                  │                │\n",
1358      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1359      "│             │                  │                │\n",
1360      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1361      "│             │                  │                │\n",
1362      "│             Z^0                @────────────────@^-0.006\n",
1363      "│             │                  │                │\n",
1364      "@─────────────@^-0.006           ×────────────────×\n",
1365      "│             │                  │                │\n",
1366      "×─────────────×                  │                │\n",
1367      "│             │                  │                │\n",
1368      "│             @──────────────────@^0.006          │\n",
1369      "│             │                  │                │\n",
1370      "│             ×──────────────────×                │\n",
1371      "│             │                  │                │\n",
1372      "@─────────────@^0.006            @────────────────@^0.006\n",
1373      "│             │                  │                │\n",
1374      "×─────────────×                  ×────────────────×\n",
1375      "│             │                  │                │\n",
1376      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1377      "│             │                  │                │\n",
1378      "Rz(π)         ×──────────────────×                Rz(0)\n",
1379      "│             │                  │                │\n",
1380      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1381      "│             │                  │                │\n",
1382      "│             Rz(0)              Rz(π)            │\n",
1383      "│             │                  │                │\n",
1384      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1385      "│             │                  │                │\n",
1386      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1387      "│             │                  │                │\n",
1388      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1389      "│             │                  │                │\n",
1390      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1391      "│             │                  │                │\n",
1392      "@─────────────@^0                Z^0              │\n",
1393      "│             │                  │                │\n",
1394      "×─────────────×                  @────────────────@^0\n",
1395      "│             │                  │                │\n",
1396      "│             │                  ×────────────────×\n",
1397      "│             │                  │                │\n",
1398      "│             @──────────────────@^0              │\n",
1399      "│             │                  │                │\n",
1400      "│             ×──────────────────×                │\n",
1401      "│             │                  │                │\n",
1402      "@─────────────@^0                @────────────────@^0\n",
1403      "│             │                  │                │\n",
1404      "×─────────────×                  ×────────────────×\n",
1405      "│             │                  │                │\n",
1406      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1407      "│             │                  │                │\n",
1408      "Rz(0)         ×──────────────────×                Rz(0)\n",
1409      "│             │                  │                │\n",
1410      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1411      "│             │                  │                │\n",
1412      "Rz(0.03π)     Rz(0)              Rz(0)            Rz(0.052π)\n",
1413      "│             │                  │                │\n",
1414      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1415      "│             │                  │                │\n",
1416      "│             Rz(0.03π)          Rz(0.052π)       │\n",
1417      "│             │                  │                │\n",
1418      "│             Rz(0)              Rz(0)            │\n",
1419      "│             │                  │                │\n",
1420      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1421      "│             │                  │                │\n",
1422      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1423      "│             │                  │                │\n",
1424      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1425      "│             │                  │                │\n",
1426      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1427      "│             │                  │                │\n",
1428      "│             Z^0                @────────────────@^-0.022\n",
1429      "│             │                  │                │\n",
1430      "@─────────────@^-0.021           ×────────────────×\n",
1431      "│             │                  │                │\n",
1432      "×─────────────×                  │                │\n",
1433      "│             │                  │                │\n",
1434      "│             @──────────────────@^-0.021         │\n",
1435      "│             │                  │                │\n",
1436      "│             ×──────────────────×                │\n",
1437      "│             │                  │                │\n",
1438      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1439      "│             │                  │                │\n",
1440      "×─────────────×                  ×────────────────×\n",
1441      "│             │                  │                │\n",
1442      "Rz(-0.011π)   @──────────────────@^-0.021         Rz(-0.01π)\n",
1443      "│             │                  │                │\n",
1444      "Rz(π)         ×──────────────────×                Rz(π)\n",
1445      "│             │                  │                │\n",
1446      "│             Rz(-0.011π)        Rz(-0.01π)       │\n",
1447      "│             │                  │                │\n",
1448      "│             Rz(0)              Rz(0)            │\n",
1449      "│             │                  │                │\n",
1450      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1451      "│             │                  │                │\n",
1452      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1453      "│             │                  │                │\n",
1454      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1455      "│             │                  │                │\n",
1456      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1457      "│             │                  │                │\n",
1458      "@─────────────@^-0.006           Z^0              │\n",
1459      "│             │                  │                │\n",
1460      "×─────────────×                  @────────────────@^-0.006\n",
1461      "│             │                  │                │\n",
1462      "│             │                  ×────────────────×\n",
1463      "│             │                  │                │\n",
1464      "│             @──────────────────@^0.006          │\n",
1465      "│             │                  │                │\n",
1466      "│             ×──────────────────×                │\n",
1467      "│             │                  │                │\n",
1468      "@─────────────@^0.006            @────────────────@^0.006\n",
1469      "│             │                  │                │\n",
1470      "×─────────────×                  ×────────────────×\n",
1471      "│             │                  │                │\n",
1472      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1473      "│             │                  │                │\n",
1474      "Rz(0)         ×──────────────────×                Rz(π)\n",
1475      "│             │                  │                │\n",
1476      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1477      "│             │                  │                │\n",
1478      "│             Rz(π)              Rz(0)            │\n",
1479      "│             │                  │                │\n",
1480      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1481      "│             │                  │                │\n",
1482      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1483      "│             │                  │                │\n",
1484      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1485      "│             │                  │                │\n",
1486      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1487      "│             │                  │                │\n",
1488      "│             Z^0                @────────────────@^0\n",
1489      "│             │                  │                │\n",
1490      "@─────────────@^0                ×────────────────×\n",
1491      "│             │                  │                │\n",
1492      "×─────────────×                  │                │\n",
1493      "│             │                  │                │\n",
1494      "│             @──────────────────@^0              │\n",
1495      "│             │                  │                │\n",
1496      "│             ×──────────────────×                │\n",
1497      "│             │                  │                │\n",
1498      "@─────────────@^0                @────────────────@^0\n",
1499      "│             │                  │                │\n",
1500      "×─────────────×                  ×────────────────×\n",
1501      "│             │                  │                │\n",
1502      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1503      "│             │                  │                │\n",
1504      "Rz(0)         ×──────────────────×                Rz(0)\n",
1505      "│             │                  │                │\n",
1506      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1507      "│             │                  │                │\n",
1508      "Rz(0.052π)    Rz(0)              Rz(0)            Rz(0.03π)\n",
1509      "│             │                  │                │\n",
1510      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1511      "│             │                  │                │\n",
1512      "│             Rz(0.052π)         Rz(0.03π)        │\n",
1513      "│             │                  │                │\n",
1514      "│             Rz(0)              Rz(0)            │\n",
1515      "│             │                  │                │\n",
1516      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1517      "│             │                  │                │\n",
1518      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1519      "│             │                  │                │\n",
1520      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1521      "│             │                  │                │\n",
1522      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1523      "│             │                  │                │\n",
1524      "@─────────────@^-0.022           Z^0              │\n",
1525      "│             │                  │                │\n",
1526      "×─────────────×                  @────────────────@^-0.021\n",
1527      "│             │                  │                │\n",
1528      "│             │                  ×────────────────×\n",
1529      "│             │                  │                │\n",
1530      "│             @──────────────────@^-0.021         │\n",
1531      "│             │                  │                │\n",
1532      "│             ×──────────────────×                │\n",
1533      "│             │                  │                │\n",
1534      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1535      "│             │                  │                │\n",
1536      "×─────────────×                  ×────────────────×\n",
1537      "│             │                  │                │\n",
1538      "Rz(-0.01π)    @──────────────────@^-0.021         Rz(-0.011π)\n",
1539      "│             │                  │                │\n",
1540      "Rz(π)         ×──────────────────×                Rz(π)\n",
1541      "│             │                  │                │\n",
1542      "│             Rz(-0.01π)         Rz(-0.011π)      │\n",
1543      "│             │                  │                │\n",
1544      "│             Rz(0)              Rz(0)            │\n",
1545      "│             │                  │                │\n",
1546      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1547      "│             │                  │                │\n",
1548      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1549      "│             │                  │                │\n",
1550      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1551      "│             │                  │                │\n",
1552      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1553      "│             │                  │                │\n",
1554      "│             Z^0                @────────────────@^-0.006\n",
1555      "│             │                  │                │\n",
1556      "@─────────────@^-0.006           ×────────────────×\n",
1557      "│             │                  │                │\n",
1558      "×─────────────×                  │                │\n",
1559      "│             │                  │                │\n",
1560      "│             @──────────────────@^0.006          │\n",
1561      "│             │                  │                │\n",
1562      "│             ×──────────────────×                │\n",
1563      "│             │                  │                │\n",
1564      "@─────────────@^0.006            @────────────────@^0.006\n",
1565      "│             │                  │                │\n",
1566      "×─────────────×                  ×────────────────×\n",
1567      "│             │                  │                │\n",
1568      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1569      "│             │                  │                │\n",
1570      "Rz(π)         ×──────────────────×                Rz(0)\n",
1571      "│             │                  │                │\n",
1572      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1573      "│             │                  │                │\n",
1574      "│             Rz(0)              Rz(π)            │\n",
1575      "│             │                  │                │\n",
1576      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1577      "│             │                  │                │\n",
1578      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1579      "│             │                  │                │\n",
1580      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1581      "│             │                  │                │\n",
1582      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1583      "│             │                  │                │\n",
1584      "@─────────────@^0                Z^0              │\n",
1585      "│             │                  │                │\n",
1586      "×─────────────×                  @────────────────@^0\n",
1587      "│             │                  │                │\n",
1588      "│             │                  ×────────────────×\n",
1589      "│             │                  │                │\n",
1590      "│             @──────────────────@^0              │\n",
1591      "│             │                  │                │\n",
1592      "│             ×──────────────────×                │\n",
1593      "│             │                  │                │\n",
1594      "@─────────────@^0                @────────────────@^0\n",
1595      "│             │                  │                │\n",
1596      "×─────────────×                  ×────────────────×\n",
1597      "│             │                  │                │\n",
1598      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1599      "│             │                  │                │\n",
1600      "Rz(0)         ×──────────────────×                Rz(0)\n",
1601      "│             │                  │                │\n",
1602      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1603      "│             │                  │                │\n",
1604      "Rz(0.03π)     Rz(0)              Rz(0)            Rz(0.052π)\n",
1605      "│             │                  │                │\n",
1606      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1607      "│             │                  │                │\n",
1608      "│             Rz(0.03π)          Rz(0.052π)       │\n",
1609      "│             │                  │                │\n",
1610      "│             Rz(0)              Rz(0)            │\n",
1611      "│             │                  │                │\n",
1612      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1613      "│             │                  │                │\n",
1614      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1615      "│             │                  │                │\n",
1616      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1617      "│             │                  │                │\n",
1618      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1619      "│             │                  │                │\n",
1620      "│             Z^0                @────────────────@^-0.022\n",
1621      "│             │                  │                │\n",
1622      "@─────────────@^-0.021           ×────────────────×\n",
1623      "│             │                  │                │\n",
1624      "×─────────────×                  │                │\n",
1625      "│             │                  │                │\n",
1626      "│             @──────────────────@^-0.021         │\n",
1627      "│             │                  │                │\n",
1628      "│             ×──────────────────×                │\n",
1629      "│             │                  │                │\n",
1630      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1631      "│             │                  │                │\n",
1632      "×─────────────×                  ×────────────────×\n",
1633      "│             │                  │                │\n",
1634      "Rz(-0.011π)   @──────────────────@^-0.021         Rz(-0.01π)\n",
1635      "│             │                  │                │\n",
1636      "Rz(π)         ×──────────────────×                Rz(π)\n",
1637      "│             │                  │                │\n",
1638      "│             Rz(-0.011π)        Rz(-0.01π)       │\n",
1639      "│             │                  │                │\n",
1640      "│             Rz(0)              Rz(0)            │\n",
1641      "│             │                  │                │\n",
1642      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1643      "│             │                  │                │\n",
1644      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1645      "│             │                  │                │\n",
1646      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1647      "│             │                  │                │\n",
1648      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1649      "│             │                  │                │\n",
1650      "@─────────────@^-0.006           Z^0              │\n",
1651      "│             │                  │                │\n",
1652      "×─────────────×                  @────────────────@^-0.006\n",
1653      "│             │                  │                │\n",
1654      "│             │                  ×────────────────×\n",
1655      "│             │                  │                │\n",
1656      "│             @──────────────────@^0.006          │\n",
1657      "│             │                  │                │\n",
1658      "│             ×──────────────────×                │\n",
1659      "│             │                  │                │\n",
1660      "@─────────────@^0.006            @────────────────@^0.006\n",
1661      "│             │                  │                │\n",
1662      "×─────────────×                  ×────────────────×\n",
1663      "│             │                  │                │\n",
1664      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1665      "│             │                  │                │\n",
1666      "Rz(0)         ×──────────────────×                Rz(π)\n",
1667      "│             │                  │                │\n",
1668      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1669      "│             │                  │                │\n",
1670      "│             Rz(π)              Rz(0)            │\n",
1671      "│             │                  │                │\n",
1672      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1673      "│             │                  │                │\n",
1674      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1675      "│             │                  │                │\n",
1676      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1677      "│             │                  │                │\n",
1678      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1679      "│             │                  │                │\n",
1680      "│             Z^0                @────────────────@^0\n",
1681      "│             │                  │                │\n",
1682      "@─────────────@^0                ×────────────────×\n",
1683      "│             │                  │                │\n",
1684      "×─────────────×                  │                │\n",
1685      "│             │                  │                │\n",
1686      "│             @──────────────────@^0              │\n",
1687      "│             │                  │                │\n",
1688      "│             ×──────────────────×                │\n",
1689      "│             │                  │                │\n",
1690      "@─────────────@^0                @────────────────@^0\n",
1691      "│             │                  │                │\n",
1692      "×─────────────×                  ×────────────────×\n",
1693      "│             │                  │                │\n",
1694      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1695      "│             │                  │                │\n",
1696      "Rz(0)         ×──────────────────×                Rz(0)\n",
1697      "│             │                  │                │\n",
1698      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1699      "│             │                  │                │\n",
1700      "Rz(0.052π)    Rz(0)              Rz(0)            Rz(0.03π)\n",
1701      "│             │                  │                │\n",
1702      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1703      "│             │                  │                │\n",
1704      "│             Rz(0.052π)         Rz(0.03π)        │\n",
1705      "│             │                  │                │\n",
1706      "│             Rz(0)              Rz(0)            │\n",
1707      "│             │                  │                │\n",
1708      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1709      "│             │                  │                │\n",
1710      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1711      "│             │                  │                │\n",
1712      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1713      "│             │                  │                │\n",
1714      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1715      "│             │                  │                │\n",
1716      "@─────────────@^-0.022           Z^0              │\n",
1717      "│             │                  │                │\n",
1718      "×─────────────×                  @────────────────@^-0.021\n",
1719      "│             │                  │                │\n",
1720      "│             │                  ×────────────────×\n",
1721      "│             │                  │                │\n",
1722      "│             @──────────────────@^-0.021         │\n",
1723      "│             │                  │                │\n",
1724      "│             ×──────────────────×                │\n",
1725      "│             │                  │                │\n",
1726      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1727      "│             │                  │                │\n",
1728      "×─────────────×                  ×────────────────×\n",
1729      "│             │                  │                │\n",
1730      "Rz(-0.01π)    @──────────────────@^-0.021         Rz(-0.011π)\n",
1731      "│             │                  │                │\n",
1732      "Rz(π)         ×──────────────────×                Rz(π)\n",
1733      "│             │                  │                │\n",
1734      "│             Rz(-0.01π)         Rz(-0.011π)      │\n",
1735      "│             │                  │                │\n",
1736      "│             Rz(0)              Rz(0)            │\n",
1737      "│             │                  │                │\n",
1738      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1739      "│             │                  │                │\n",
1740      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1741      "│             │                  │                │\n",
1742      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1743      "│             │                  │                │\n",
1744      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1745      "│             │                  │                │\n",
1746      "│             Z^0                @────────────────@^-0.006\n",
1747      "│             │                  │                │\n",
1748      "@─────────────@^-0.006           ×────────────────×\n",
1749      "│             │                  │                │\n",
1750      "×─────────────×                  │                │\n",
1751      "│             │                  │                │\n",
1752      "│             @──────────────────@^0.006          │\n",
1753      "│             │                  │                │\n",
1754      "│             ×──────────────────×                │\n",
1755      "│             │                  │                │\n",
1756      "@─────────────@^0.006            @────────────────@^0.006\n",
1757      "│             │                  │                │\n",
1758      "×─────────────×                  ×────────────────×\n",
1759      "│             │                  │                │\n",
1760      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1761      "│             │                  │                │\n",
1762      "Rz(π)         ×──────────────────×                Rz(0)\n",
1763      "│             │                  │                │\n",
1764      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1765      "│             │                  │                │\n",
1766      "│             Rz(0)              Rz(π)            │\n",
1767      "│             │                  │                │\n",
1768      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1769      "│             │                  │                │\n",
1770      "PhISwap(0.25)─PhISwap(0.25)^-0.5 Z^0              │\n",
1771      "│             │                  │                │\n",
1772      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^0.5\n",
1773      "│             │                  │                │\n",
1774      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1775      "│             │                  │                │\n",
1776      "@─────────────@^0                Z^0              │\n",
1777      "│             │                  │                │\n",
1778      "×─────────────×                  @────────────────@^0\n",
1779      "│             │                  │                │\n",
1780      "│             │                  ×────────────────×\n",
1781      "│             │                  │                │\n",
1782      "│             @──────────────────@^0              │\n",
1783      "│             │                  │                │\n",
1784      "│             ×──────────────────×                │\n",
1785      "│             │                  │                │\n",
1786      "@─────────────@^0                @────────────────@^0\n",
1787      "│             │                  │                │\n",
1788      "×─────────────×                  ×────────────────×\n",
1789      "│             │                  │                │\n",
1790      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1791      "│             │                  │                │\n",
1792      "Rz(0)         ×──────────────────×                Rz(0)\n",
1793      "│             │                  │                │\n",
1794      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1795      "│             │                  │                │\n",
1796      "Rz(0.03π)     Rz(0)              Rz(0)            Rz(0.052π)\n",
1797      "│             │                  │                │\n",
1798      "Rz(0)         Rz(0)              Rz(0)            Rz(0)\n",
1799      "│             │                  │                │\n",
1800      "│             Rz(0.03π)          Rz(0.052π)       │\n",
1801      "│             │                  │                │\n",
1802      "│             Rz(0)              Rz(0)            │\n",
1803      "│             │                  │                │\n",
1804      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1805      "│             │                  │                │\n",
1806      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-1\n",
1807      "│             │                  │                │\n",
1808      "PhISwap(0.25)─PhISwap(0.25)^-1   Z^0              │\n",
1809      "│             │                  │                │\n",
1810      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1811      "│             │                  │                │\n",
1812      "│             Z^0                @────────────────@^-0.022\n",
1813      "│             │                  │                │\n",
1814      "@─────────────@^-0.021           ×────────────────×\n",
1815      "│             │                  │                │\n",
1816      "×─────────────×                  │                │\n",
1817      "│             │                  │                │\n",
1818      "│             @──────────────────@^-0.021         │\n",
1819      "│             │                  │                │\n",
1820      "│             ×──────────────────×                │\n",
1821      "│             │                  │                │\n",
1822      "@─────────────@^-0.021           @────────────────@^-0.021\n",
1823      "│             │                  │                │\n",
1824      "×─────────────×                  ×────────────────×\n",
1825      "│             │                  │                │\n",
1826      "Rz(-0.011π)   @──────────────────@^-0.021         Rz(-0.01π)\n",
1827      "│             │                  │                │\n",
1828      "Rz(π)         ×──────────────────×                Rz(π)\n",
1829      "│             │                  │                │\n",
1830      "│             Rz(-0.011π)        Rz(-0.01π)       │\n",
1831      "│             │                  │                │\n",
1832      "│             Rz(0)              Rz(0)            │\n",
1833      "│             │                  │                │\n",
1834      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1835      "│             │                  │                │\n",
1836      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1837      "│             │                  │                │\n",
1838      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1839      "│             │                  │                │\n",
1840      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 Z^0\n",
1841      "│             │                  │                │\n",
1842      "@─────────────@^-0.006           Z^0              │\n",
1843      "│             │                  │                │\n",
1844      "×─────────────×                  @────────────────@^-0.006\n",
1845      "│             │                  │                │\n",
1846      "│             │                  ×────────────────×\n",
1847      "│             │                  │                │\n",
1848      "│             @──────────────────@^0.006          │\n",
1849      "│             │                  │                │\n",
1850      "│             ×──────────────────×                │\n",
1851      "│             │                  │                │\n",
1852      "@─────────────@^0.006            @────────────────@^0.006\n",
1853      "│             │                  │                │\n",
1854      "×─────────────×                  ×────────────────×\n",
1855      "│             │                  │                │\n",
1856      "Rz(-0.003π)   @──────────────────@^0.006          Rz(-0.003π)\n",
1857      "│             │                  │                │\n",
1858      "Rz(0)         ×──────────────────×                Rz(π)\n",
1859      "│             │                  │                │\n",
1860      "│             Rz(-0.003π)        Rz(-0.003π)      │\n",
1861      "│             │                  │                │\n",
1862      "│             Rz(π)              Rz(0)            │\n",
1863      "│             │                  │                │\n",
1864      "│             PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1865      "│             │                  │                │\n",
1866      "│             Z^0                PhISwap(0.25)────PhISwap(0.25)^-0.5\n",
1867      "│             │                  │                │\n",
1868      "PhISwap(0.25)─PhISwap(0.25)^0.5  Z^0              │\n",
1869      "│             │                  │                │\n",
1870      "Z^0           PhISwap(0.25)──────PhISwap(0.25)^-1 │\n",
1871      "│             │                  │                │\n",
1872      "│             Z^0                @────────────────@^0\n",
1873      "│             │                  │                │\n",
1874      "@─────────────@^0                ×────────────────×\n",
1875      "│             │                  │                │\n",
1876      "×─────────────×                  │                │\n",
1877      "│             │                  │                │\n",
1878      "│             @──────────────────@^0              │\n",
1879      "│             │                  │                │\n",
1880      "│             ×──────────────────×                │\n",
1881      "│             │                  │                │\n",
1882      "@─────────────@^0                @────────────────@^0\n",
1883      "│             │                  │                │\n",
1884      "×─────────────×                  ×────────────────×\n",
1885      "│             │                  │                │\n",
1886      "Rz(0)         @──────────────────@^0              Rz(0)\n",
1887      "│             │                  │                │\n",
1888      "Rz(0)         ×──────────────────×                Rz(0)\n",
1889      "│             │                  │                │\n",
1890      "│             Rz(0)              Rz(0)            │\n",
1891      "│             │                  │                │\n",
1892      "│             Rz(0)              Rz(0)            │\n",
1893      "│             │                  │                │\n"
1894     ]
1895    }
1896   ],
1897   "source": [
1898    "print(circuit.to_text_diagram(transpose=True))"
1899   ]
1900  },
1901  {
1902   "cell_type": "markdown",
1903   "metadata": {
1904    "id": "vim-c2VhuGZO"
1905   },
1906   "source": [
1907    "## Bogoliubov transformation\n",
1908    "\n",
1909    "- Single-particle orbital basis change\n",
1910    "- In the particle-conserving case, takes the form\n",
1911    "$$\n",
1912    "U a_p^\\dagger U^\\dagger = b_p^\\dagger, \\quad b_p^\\dagger = \\sum_{q} u_{pq} a_q^\\dagger\n",
1913    "$$\n",
1914    "and $u$ is unitary.\n",
1915    "- Can be used to diagonalize any quadratic Hamiltonian:\n",
1916    "$$\n",
1917    "\\sum_{p, q} T_{pq} a_p^\\dagger a_q \\mapsto \\sum_{j} \\varepsilon_j b_j^\\dagger b_j + \\text{constant}\n",
1918    "$$\n",
1919    "- Implementation from [arXiv:1711.05395](https://arxiv.org/pdf/1711.05395.pdf); uses linear depth and linear connectivity\n",
1920    "\n",
1921    "As an example, we'll prepare the ground state of a random particle-conserving quadratic Hamiltonian."
1922   ]
1923  },
1924  {
1925   "cell_type": "code",
1926   "execution_count": 19,
1927   "metadata": {
1928    "id": "sOhY4QQtuGZP"
1929   },
1930   "outputs": [
1931    {
1932     "name": "stdout",
1933     "output_type": "stream",
1934     "text": [
1935      "1.690525703800356 [] +\n",
1936      "(0.5315776978980016+0j) [0^ 0] +\n",
1937      "(-1.347208023348913+2.7004721387490935j) [0^ 1] +\n",
1938      "(-0.28362365442898696-1.8784499457335426j) [0^ 2] +\n",
1939      "(0.12594647819298657-1.3106154125325498j) [0^ 3] +\n",
1940      "(-0.3880303291443195-1.1751249212322041j) [0^ 4] +\n",
1941      "(-1.347208023348913-2.7004721387490935j) [1^ 0] +\n",
1942      "(2.5012533818678193+0j) [1^ 1] +\n",
1943      "(0.3391421007279024-3.8305756810505094j) [1^ 2] +\n",
1944      "(-0.3509690502067961+0.090677856754656j) [1^ 3] +\n",
1945      "(1.8575239595653907-1.4736314761076197j) [1^ 4] +\n",
1946      "(-0.28362365442898696+1.8784499457335426j) [2^ 0] +\n",
1947      "(0.3391421007279024+3.8305756810505094j) [2^ 1] +\n",
1948      "(-0.019560786804260433+0j) [2^ 2] +\n",
1949      "(-2.979765944360631+2.5490724453105917j) [2^ 3] +\n",
1950      "(0.5091942820312417+0.344618218148502j) [2^ 4] +\n",
1951      "(0.12594647819298657+1.3106154125325498j) [3^ 0] +\n",
1952      "(-0.3509690502067961-0.090677856754656j) [3^ 1] +\n",
1953      "(-2.979765944360631-2.5490724453105917j) [3^ 2] +\n",
1954      "(3.767336752913784+0j) [3^ 3] +\n",
1955      "(-1.2963431636902167-1.5288970105744286j) [3^ 4] +\n",
1956      "(-0.3880303291443195+1.1751249212322041j) [4^ 0] +\n",
1957      "(1.8575239595653907+1.4736314761076197j) [4^ 1] +\n",
1958      "(0.5091942820312417-0.344618218148502j) [4^ 2] +\n",
1959      "(-1.2963431636902167+1.5288970105744286j) [4^ 3] +\n",
1960      "(-0.3445183403145406+0j) [4^ 4]\n"
1961     ]
1962    }
1963   ],
1964   "source": [
1965    "n_qubits = 5\n",
1966    "quad_ham = of.random_quadratic_hamiltonian(\n",
1967    "    n_qubits, conserves_particle_number=True, seed=7)\n",
1968    "\n",
1969    "print(of.get_fermion_operator(quad_ham))"
1970   ]
1971  },
1972  {
1973   "cell_type": "markdown",
1974   "metadata": {
1975    "id": "HwObrpO5uGZS"
1976   },
1977   "source": [
1978    "Now we construct a circuit which maps computational basis states to eigenstates of the Hamiltonian."
1979   ]
1980  },
1981  {
1982   "cell_type": "code",
1983   "execution_count": 20,
1984   "metadata": {
1985    "id": "P1w7kKokuGZT"
1986   },
1987   "outputs": [
1988    {
1989     "name": "stdout",
1990     "output_type": "stream",
1991     "text": [
1992      "0             1                   2                    3                     4\n",
1993      "│             │                   │                    │                     │\n",
1994      "Rz(0)         Rz(0)               Rz(π)                Rz(-π)                Rz(0)\n",
1995      "│             │                   │                    │                     │\n",
1996      "│             │                   │                    PhISwap(0.25)─────────PhISwap(0.25)^0.073\n",
1997      "│             │                   │                    │                     │\n",
1998      "│             │                   PhISwap(0.25)────────PhISwap(0.25)^(5/11)  Z^(-7/9)\n",
1999      "│             │                   │                    │                     │\n",
2000      "│             PhISwap(0.25)───────PhISwap(0.25)^0.814  Z^-0.508              │\n",
2001      "│             │                   │                    │                     │\n",
2002      "PhISwap(0.25)─PhISwap(0.25)^0.703 Z^-0.304             PhISwap(0.25)─────────PhISwap(0.25)^0.381\n",
2003      "│             │                   │                    │                     │\n",
2004      "│             Z^-0.767            PhISwap(0.25)────────PhISwap(0.25)^(13/16) Z^0.401\n",
2005      "│             │                   │                    │                     │\n",
2006      "│             PhISwap(0.25)───────PhISwap(0.25)^(7/11) Z^(-13/14)            │\n",
2007      "│             │                   │                    │                     │\n",
2008      "│             │                   Z^0.393              PhISwap(0.25)─────────PhISwap(0.25)^0.481\n",
2009      "│             │                   │                    │                     │\n",
2010      "│             │                   PhISwap(0.25)────────PhISwap(0.25)^0.422   Z^-0.738\n",
2011      "│             │                   │                    │                     │\n",
2012      "│             │                   │                    Z^-0.824              │\n",
2013      "│             │                   │                    │                     │\n",
2014      "│             │                   │                    PhISwap(0.25)─────────PhISwap(0.25)^0.506\n",
2015      "│             │                   │                    │                     │\n",
2016      "│             │                   │                    │                     Z^(5/7)\n",
2017      "│             │                   │                    │                     │\n"
2018     ]
2019    }
2020   ],
2021   "source": [
2022    "_, basis_change_matrix, _ = quad_ham.diagonalizing_bogoliubov_transform()\n",
2023    "\n",
2024    "qubits = cirq.LineQubit.range(n_qubits)\n",
2025    "circuit = cirq.Circuit(\n",
2026    "    of.bogoliubov_transform(\n",
2027    "       qubits,\n",
2028    "       basis_change_matrix))\n",
2029    "\n",
2030    "print(circuit.to_text_diagram(transpose=True))"
2031   ]
2032  },
2033  {
2034   "cell_type": "markdown",
2035   "metadata": {
2036    "id": "-jDHy5izuGZV"
2037   },
2038   "source": [
2039    "In the rotated basis, the quadratic Hamiltonian takes the form\n",
2040    "$$\n",
2041    "H = \\sum_j \\varepsilon_j b_j^\\dagger b_j + \\text{constant}\n",
2042    "$$\n",
2043    "We can get the $\\varepsilon_j$ and the constant using the `orbital_energies` method of `QuadraticHamiltonian`."
2044   ]
2045  },
2046  {
2047   "cell_type": "code",
2048   "execution_count": 21,
2049   "metadata": {
2050    "id": "Q0aZhjc0uGZW"
2051   },
2052   "outputs": [
2053    {
2054     "name": "stdout",
2055     "output_type": "stream",
2056     "text": [
2057      "[-6.25377614 -1.2291963   0.71202361  5.0062515   8.20078604]\n",
2058      "1.690525703800356\n"
2059     ]
2060    }
2061   ],
2062   "source": [
2063    "orbital_energies, constant = quad_ham.orbital_energies()\n",
2064    "\n",
2065    "print(orbital_energies)\n",
2066    "print(constant)"
2067   ]
2068  },
2069  {
2070   "cell_type": "markdown",
2071   "metadata": {
2072    "id": "YCsxdHRpuGZZ"
2073   },
2074   "source": [
2075    "The ground state of the Hamiltonian is prepared by filling in the orbitals with negative energy."
2076   ]
2077  },
2078  {
2079   "cell_type": "code",
2080   "execution_count": 22,
2081   "metadata": {
2082    "id": "ggOZq0DXuGZZ"
2083   },
2084   "outputs": [
2085    {
2086     "name": "stdout",
2087     "output_type": "stream",
2088     "text": [
2089      "(-5.792446738060052+1.1102230246251565e-16j)\n",
2090      "-5.792446738060049\n"
2091     ]
2092    }
2093   ],
2094   "source": [
2095    "# Apply the circuit with initial state having the first two modes occupied.\n",
2096    "result = circuit.final_state_vector(initial_state=0b11000)\n",
2097    "\n",
2098    "# Compute the expectation value of the final state with the Hamiltonian\n",
2099    "quad_ham_sparse = of.get_sparse_operator(quad_ham)\n",
2100    "print(of.expectation(quad_ham_sparse, result))\n",
2101    "\n",
2102    "# Print out the ground state energy; it should match\n",
2103    "print(quad_ham.ground_energy())"
2104   ]
2105  },
2106  {
2107   "cell_type": "markdown",
2108   "metadata": {
2109    "id": "6WXdbSBmuGZc"
2110   },
2111   "source": [
2112    "Recall that the Jordan-Wigner transform of $b_j^\\dagger b_j$ is $\\frac12(I-Z)$. Therefore, $\\exp(-i \\varepsilon_j b_j^\\dagger b_j)$ is equivalent to a single-qubit Z rotation under the JWT. Since the operators $b_j^\\dagger b_j$ commute, we have\n",
2113    "$$\n",
2114    "\\exp(-i H t) = \\exp(-i \\sum_j \\varepsilon_j b_j^\\dagger b_j t)\n",
2115    "= \\prod_j \\exp(-i \\varepsilon_j b_j^\\dagger b_j t)\n",
2116    "$$\n",
2117    "This gives a method for simulating time evolution under a quadratic Hamiltonian:\n",
2118    "- Use a Bogoliubov transformation to change to the basis in which the Hamiltonian is diagonal (Note: this transformation might be the inverse of what you expect. In that case, use `cirq.inverse`)\n",
2119    "- Apply single-qubit Z-rotations with angles proportional to the orbital energies\n",
2120    "- Undo the basis change\n",
2121    "\n",
2122    "The code cell below creates a random initial state and applies time evolution by direct matrix exponentiation."
2123   ]
2124  },
2125  {
2126   "cell_type": "code",
2127   "execution_count": 23,
2128   "metadata": {
2129    "id": "pqELuMY8uGZd"
2130   },
2131   "outputs": [],
2132   "source": [
2133    "# Create a random initial state\n",
2134    "initial_state = of.haar_random_vector(2**n_qubits)\n",
2135    "\n",
2136    "# Set evolution time\n",
2137    "time = 1.0\n",
2138    "\n",
2139    "# Apply exp(-i H t) to the state\n",
2140    "final_state = linalg.expm_multiply(-1j*quad_ham_sparse*time, initial_state)"
2141   ]
2142  },
2143  {
2144   "cell_type": "markdown",
2145   "metadata": {
2146    "id": "dFzXiKp0uGZf"
2147   },
2148   "source": [
2149    "### Exercise\n",
2150    "\n",
2151    "Fill in the code cell below to construct a circuit which applies $\\exp(-i H t)$ using the method described above"
2152   ]
2153  },
2154  {
2155   "cell_type": "code",
2156   "execution_count": 24,
2157   "metadata": {
2158    "id": "f0y43UhbuGZh"
2159   },
2160   "outputs": [
2161    {
2162     "name": "stdout",
2163     "output_type": "stream",
2164     "text": [
2165      "0.08926042490120051\n"
2166     ]
2167    }
2168   ],
2169   "source": [
2170    "# Initialize qubits\n",
2171    "qubits = cirq.LineQubit.range(n_qubits)\n",
2172    "\n",
2173    "# Write code below to create the circuit\n",
2174    "# You should define the `circuit` variable here\n",
2175    "# ---------------------------------------------\n",
2176    "\n",
2177    "\n",
2178    "# ---------------------------------------------\n",
2179    "\n",
2180    "# Apply the circuit to the initial state\n",
2181    "result = circuit.final_state_vector(initial_state)\n",
2182    "\n",
2183    "# Compute the fidelity with the correct final state\n",
2184    "fidelity = abs(np.dot(final_state, result.conj()))**2\n",
2185    "\n",
2186    "# Print fidelity; it should be 1\n",
2187    "print(fidelity)"
2188   ]
2189  },
2190  {
2191   "cell_type": "markdown",
2192   "metadata": {
2193    "id": "Wp1a_Zf8EUen"
2194   },
2195   "source": [
2196    "### Solution"
2197   ]
2198  },
2199  {
2200   "cell_type": "code",
2201   "execution_count": 25,
2202   "metadata": {
2203    "id": "QHQvLlf1uGZt"
2204   },
2205   "outputs": [
2206    {
2207     "name": "stdout",
2208     "output_type": "stream",
2209     "text": [
2210      "0.999999999999994\n"
2211     ]
2212    }
2213   ],
2214   "source": [
2215    "# Initialize qubits\n",
2216    "qubits = cirq.LineQubit.range(n_qubits)\n",
2217    "\n",
2218    "# Write code below to create the circuit\n",
2219    "# You should define the `circuit` variable here\n",
2220    "# ---------------------------------------------\n",
2221    "def exponentiate_quad_ham(qubits, quad_ham):\n",
2222    "    _, basis_change_matrix, _ = quad_ham.diagonalizing_bogoliubov_transform()\n",
2223    "    orbital_energies, _ = quad_ham.orbital_energies()\n",
2224    "    \n",
2225    "    yield cirq.inverse(\n",
2226    "        of.bogoliubov_transform(qubits, basis_change_matrix))\n",
2227    "    for i in range(len(qubits)):\n",
2228    "        yield cirq.rz(rads=-orbital_energies[i]).on(qubits[i])\n",
2229    "    yield of.bogoliubov_transform(qubits, basis_change_matrix)\n",
2230    "\n",
2231    "circuit = cirq.Circuit(exponentiate_quad_ham(qubits, quad_ham))\n",
2232    "# ---------------------------------------------\n",
2233    "\n",
2234    "# Apply the circuit to the initial state\n",
2235    "result = circuit.final_state_vector(initial_state)\n",
2236    "\n",
2237    "# Compute the fidelity with the correct final state\n",
2238    "fidelity = abs(np.dot(final_state, result.conj()))**2\n",
2239    "\n",
2240    "# Print fidelity; it should be 1\n",
2241    "print(fidelity)"
2242   ]
2243  }
2244 ],
2245 "metadata": {
2246  "colab": {
2247   "collapsed_sections": [
2248    "Wp1a_Zf8EUen"
2249   ],
2250   "name": "chemistry.ipynb",
2251   "toc_visible": true
2252  },
2253  "kernelspec": {
2254   "display_name": "Python 3",
2255   "name": "python3"
2256  }
2257 },
2258 "nbformat": 4,
2259 "nbformat_minor": 0
2260}
2261