{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "SzKwuqYESWwm" }, "source": [ "##### Copyright 2020 The Cirq Developers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2024-08-16T10:50:07.234755Z", "iopub.status.busy": "2024-08-16T10:50:07.234187Z", "iopub.status.idle": "2024-08-16T10:50:07.238344Z", "shell.execute_reply": "2024-08-16T10:50:07.237660Z" }, "id": "4yPUsdJxSXFq" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "dVkNQc0WSIwk" }, "source": [ "# Quantum variational algorithm" ] }, { "cell_type": "markdown", "metadata": { "id": "zC1qlUJoSXhm" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " View on QuantumAI\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", " \n", " Download notebook\n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "y0TUn0KcBcSO" }, "source": [ "In this tutorial, we use the [variational quantum eigensolver](https://arxiv.org/abs/1304.3061) (VQE) in Cirq to optimize a simple Ising model." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:07.241873Z", "iopub.status.busy": "2024-08-16T10:50:07.241324Z", "iopub.status.idle": "2024-08-16T10:50:24.571173Z", "shell.execute_reply": "2024-08-16T10:50:24.570345Z" }, "id": "bd9529db1c0b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "installing cirq...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\r\n", "tensorflow-metadata 1.15.0 requires protobuf<4.21,>=3.20.3; python_version < \"3.11\", but you have protobuf 4.25.4 which is incompatible.\u001b[0m\u001b[31m\r\n", "\u001b[0m" ] }, { "name": "stdout", "output_type": "stream", "text": [ "installed cirq.\n" ] } ], "source": [ "try:\n", " import cirq\n", "except ImportError:\n", " print(\"installing cirq...\")\n", " !pip install --quiet cirq\n", " print(\"installed cirq.\")\n", " import cirq" ] }, { "cell_type": "markdown", "metadata": { "id": "xcn4Ncad_5pT" }, "source": [ "## Background: Variational Quantum Algorithm\n", "\n", "The [variational method](https://en.wikipedia.org/wiki/Variational_method_(quantum_mechanics)) in quantum theory is a classical method for finding low energy states of a quantum system. The rough idea of this method is that one defines a trial wave function (sometimes called an *ansatz*) as a function of some parameters, and then one finds the values of these parameters that minimize the expectation value of the energy with respect to these parameters. This minimized ansatz is then an approximation to the lowest energy eigenstate, and the expectation value serves as an upper bound on the energy of the ground state.\n", "\n", "In the last few years (see [arXiv:1304.3061](https://arxiv.org/abs/1304.3061) and [arXiv:1507.08969](https://arxiv.org/abs/1507.08969), for example), it has been realized that quantum computers can mimic the classical technique and that a quantum computer does so with certain advantages. In particular, when one applies the classical variational method to a system of $n$ qubits, an exponential number (in $n$) of complex numbers is necessary to generically represent the wave function of the system. However, with a quantum computer, one can directly produce this state using a parameterized quantum circuit with less than exponential parameters, and then use repeated measurements to estimate the expectation value of the energy.\n", "\n", "This idea has led to a class of algorithms known as variational quantum algorithms. Indeed this approach is not just limited to finding low energy eigenstates, but minimizing any objective function that can be expressed as a quantum observable. It is an open question to identify under what conditions these quantum variational algorithms will succeed, and exploring this class of algorithms is a key part of the research for [noisy intermediate scale quantum computers](https://arxiv.org/abs/1801.00862).\n", "\n", "The classical problem we will focus on is the 2D +/- Ising model with transverse field ([ISING](http://iopscience.iop.org/article/10.1088/0305-4470/15/10/028/meta)). This problem is NP-complete. It is highly unlikely that quantum computers will be able to efficiently solve it across all instances because it is generally believed that quantum computers cannot solve all NP-complete problems in polynomial time. Yet this type of problem is illustrative of the general class of problems that Cirq is designed to tackle.\n", "\n", "\n", "Let's define the problem. Consider the energy function\n", "\n", "$E(s_1,\\dots,s_n) = \\sum_{\\langle i,j \\rangle} J_{i,j}s_i s_j + \\sum_i h_i s_i$\n", "\n", "where here each $s_i, J_{i,j}$, and $h_i$ are either +1 or -1. Here each index i is associated with a bit on a square lattice, and the $\\langle i,j \\rangle$ notation means sums over neighboring bits on this lattice. The problem we would like to solve is, given $J_{i,j}$, and $h_i$, find an assignment of $s_i$ values that minimize $E$.\n", "\n", "How does a variational quantum algorithm work for this? One approach is to consider $n$ qubits and associate them with each of the bits in the classical problem. This maps the classical problem onto the quantum problem of minimizing the expectation value of the observable\n", "\n", "$H=\\sum_{\\langle i,j \\rangle} J_{i,j} Z_i Z_j + \\sum_i h_iZ_i$\n", "\n", "Then one defines a set of parameterized quantum circuits, i.e., a quantum circuit where the gates (or more general quantum operations) are parameterized by some values. This produces an ansatz state\n", "\n", "$|\\psi(p_1, p_2, \\dots, p_k)\\rangle$\n", "\n", "where $p_i$ are the parameters that produce this state (here we assume a pure state, but mixed states are of course possible).\n", "\n", "The variational algorithm then works by noting that one can obtain the value of the objective function for a given ansatz state by\n", "\n", "1. Prepare the ansatz state.\n", "2. Make a measurement which samples from some terms in H.\n", "3. Goto 1.\n", "\n", "Note that one cannot always measure $H$ directly (without the use of quantum phase estimation). So one often relies on the linearity of expectation values to measure parts of $H$ in step 2. One always needs to repeat the measurements to obtain an estimate of the expectation value. How many measurements needed to achieve a given accuracy is beyond the scope of this tutorial, but Cirq can help investigate this question.\n", "\n", "The above shows that one can use a quantum computer to obtain estimates of the objective function for the ansatz. This can then be used in an outer loop to try to obtain parameters for the lowest value of the objective function. For these best parameter, one can then use that best ansatz to produce samples of solutions to the problem, which obtain a hopefully good approximation for the lowest possible value of the objective function.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "cfsYdZw6_5pU" }, "source": [ "## Create a circuit on a Grid\n", "\n", "To build the above variational quantum algorithm using Cirq, one begins by building the appropriate circuit. Because the problem we have defined has a natural structure on a grid, we will use Cirq’s built-in `cirq.GridQubit`s as our qubits. We will demonstrate some of how this works in an interactive Python environment, the following code can be run in series in a Python environment where you have Cirq installed. For more about circuits and how to create them, see the [Tutorial](../start/basics.ipynb) or the [Circuits](../build/circuits.ipynb) page." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.575517Z", "iopub.status.busy": "2024-08-16T10:50:24.575071Z", "iopub.status.idle": "2024-08-16T10:50:24.579870Z", "shell.execute_reply": "2024-08-16T10:50:24.579066Z" }, "id": "5TV_rMxX_5pW" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(0, 2), cirq.GridQubit(1, 0), cirq.GridQubit(1, 1), cirq.GridQubit(1, 2), cirq.GridQubit(2, 0), cirq.GridQubit(2, 1), cirq.GridQubit(2, 2)]\n" ] } ], "source": [ "# define the length and width of the grid.\n", "length = 3\n", "\n", "# define qubits on the grid.\n", "qubits = cirq.GridQubit.square(length)\n", "\n", "print(qubits)" ] }, { "cell_type": "markdown", "metadata": { "id": "D3obTsFs_5pa" }, "source": [ "Here we see that we've created a bunch of `cirq.GridQubits`, which have a row and column, indicating their position on a grid.\n", "\n", "Now that we have some qubits, let us construct a `cirq.Circuit` on these qubits. For example, suppose we want to apply the Hadamard gate `cirq.H` to every qubit whose row index plus column index is even, and an `cirq.X` gate to every qubit whose row index plus column index is odd. To do this, we write:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.583261Z", "iopub.status.busy": "2024-08-16T10:50:24.582712Z", "iopub.status.idle": "2024-08-16T10:50:24.590399Z", "shell.execute_reply": "2024-08-16T10:50:24.589718Z" }, "id": "dy3VFNMx_5pa" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 0): ───H───\n", "\n", "(0, 1): ───X───\n", "\n", "(0, 2): ───H───\n", "\n", "(1, 0): ───X───\n", "\n", "(1, 1): ───H───\n", "\n", "(1, 2): ───X───\n", "\n", "(2, 0): ───H───\n", "\n", "(2, 1): ───X───\n", "\n", "(2, 2): ───H───\n" ] } ], "source": [ "circuit = cirq.Circuit()\n", "circuit.append(cirq.H(q) for q in qubits if (q.row + q.col) % 2 == 0)\n", "circuit.append(cirq.X(q) for q in qubits if (q.row + q.col) % 2 == 1)\n", "\n", "print(circuit)" ] }, { "cell_type": "markdown", "metadata": { "id": "_iFQ7Zwu_5pi" }, "source": [ "## Creating the Ansatz\n", "\n", "One convenient pattern is to use a python [Generator](https://wiki.python.org/moin/Generators) for defining sub-circuits or layers in our algorithm. We will define a function that takes in the relevant parameters and then yields the operations for the sub-circuit, and then this can be appended to the `cirq.Circuit`:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.594066Z", "iopub.status.busy": "2024-08-16T10:50:24.593543Z", "iopub.status.idle": "2024-08-16T10:50:24.599842Z", "shell.execute_reply": "2024-08-16T10:50:24.599172Z" }, "id": "rLayzy4P_5pj" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 0): ───X^0.1───\n", "\n", "(0, 1): ───X^0.1───\n", "\n", "(1, 0): ───X^0.1───\n", "\n", "(1, 1): ───X^0.1───\n" ] } ], "source": [ "def rot_x_layer(length, half_turns):\n", " \"\"\"Yields X rotations by half_turns on a square grid of given length.\"\"\"\n", "\n", " # Define the gate once and then re-use it for each Operation.\n", " rot = cirq.XPowGate(exponent=half_turns)\n", "\n", " # Create an X rotation Operation for each qubit in the grid.\n", " for i in range(length):\n", " for j in range(length):\n", " yield rot(cirq.GridQubit(i, j))\n", "\n", "# Create the circuit using the rot_x_layer generator\n", "circuit = cirq.Circuit()\n", "circuit.append(rot_x_layer(2, 0.1))\n", "print(circuit)" ] }, { "cell_type": "markdown", "metadata": { "id": "DrO5W2ie_5pl" }, "source": [ "Another important concept here is that the rotation gate is specified in *half turns* ($ht$). For a rotation about `X`, the gate is:\n", "\n", "$\\cos(ht * \\pi) I + i \\sin(ht * \\pi) X$\n", "\n", "There is a lot of freedom defining a variational ansatz. Here we will do a variation on a [QAOA strategy](https://arxiv.org/abs/1411.4028) and define an ansatz related to the problem we are trying to solve.\n", "\n", "First, we need to choose how the instances of the problem are represented. These are the values $J$ and $h$ in the Hamiltonian definition. We represent them as two-dimensional arrays (lists of lists). For $J$ we use two such lists, one for the row links and one for the column links.\n", "\n", "Here is a snippet that we can use to generate random problem instances:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.603358Z", "iopub.status.busy": "2024-08-16T10:50:24.602783Z", "iopub.status.idle": "2024-08-16T10:50:24.608375Z", "shell.execute_reply": "2024-08-16T10:50:24.607709Z" }, "id": "6E5BjSxF_5pm" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "transverse fields: [[-1, -1, -1], [1, -1, 1], [-1, 1, -1]]\n", "row j fields: [[1, 1, 1], [1, 1, 1]]\n", "column j fields: [[1, 1], [1, -1], [-1, -1]]\n" ] } ], "source": [ "import random\n", "def rand2d(rows, cols):\n", " return [[random.choice([+1, -1]) for _ in range(cols)] for _ in range(rows)]\n", "\n", "def random_instance(length):\n", " # transverse field terms\n", " h = rand2d(length, length)\n", " # links within a row\n", " jr = rand2d(length - 1, length)\n", " # links within a column\n", " jc = rand2d(length, length - 1)\n", " return (h, jr, jc)\n", "\n", "h, jr, jc = random_instance(3)\n", "print(f'transverse fields: {h}')\n", "print(f'row j fields: {jr}')\n", "print(f'column j fields: {jc}')" ] }, { "cell_type": "markdown", "metadata": { "id": "zsq_177Q_5po" }, "source": [ "In the code above, the actual values will be different for each individual run because they are using `random.choice`.\n", "\n", "Given this definition of the problem instance, we can now introduce our ansatz. It will consist of one step of a circuit made up of:\n", "\n", "0. Apply an initial mixing step that puts all the qubits into the $|+\\rangle=\\frac{1}{\\sqrt{2}}(|0\\rangle+|1\\rangle)$ state.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.611773Z", "iopub.status.busy": "2024-08-16T10:50:24.611231Z", "iopub.status.idle": "2024-08-16T10:50:24.614776Z", "shell.execute_reply": "2024-08-16T10:50:24.614148Z" }, "id": "50ccff9234b2" }, "outputs": [], "source": [ "def prepare_plus_layer(length):\n", " for i in range(length):\n", " for j in range(length):\n", " yield cirq.H(cirq.GridQubit(i, j))" ] }, { "cell_type": "markdown", "metadata": { "id": "49482d550033" }, "source": [ "1. Apply a `cirq.ZPowGate` for the same parameter for all qubits where the transverse field term $h$ is $+1$." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.617845Z", "iopub.status.busy": "2024-08-16T10:50:24.617523Z", "iopub.status.idle": "2024-08-16T10:50:24.621490Z", "shell.execute_reply": "2024-08-16T10:50:24.620837Z" }, "id": "XtYIZSef_5po" }, "outputs": [], "source": [ "def rot_z_layer(h, half_turns):\n", " \"\"\"Yields Z rotations by half_turns conditioned on the field h.\"\"\"\n", " gate = cirq.ZPowGate(exponent=half_turns)\n", " for i, h_row in enumerate(h):\n", " for j, h_ij in enumerate(h_row):\n", " if h_ij == 1:\n", " yield gate(cirq.GridQubit(i, j))" ] }, { "cell_type": "markdown", "metadata": { "id": "iSizAkjE_5pq" }, "source": [ "2. Apply a `cirq.CZPowGate` for the same parameter between all qubits where the coupling field term $J$ is $+1$. If the field is $-1$, apply `cirq.CZPowGate` conjugated by $X$ gates on all qubits." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.624958Z", "iopub.status.busy": "2024-08-16T10:50:24.624411Z", "iopub.status.idle": "2024-08-16T10:50:24.630588Z", "shell.execute_reply": "2024-08-16T10:50:24.629915Z" }, "id": "jo9pqBlJ_5pq" }, "outputs": [], "source": [ "def rot_11_layer(jr, jc, half_turns):\n", " \"\"\"Yields rotations about |11> conditioned on the jr and jc fields.\"\"\"\n", " cz_gate = cirq.CZPowGate(exponent=half_turns) \n", " for i, jr_row in enumerate(jr):\n", " for j, jr_ij in enumerate(jr_row):\n", " q = cirq.GridQubit(i, j)\n", " q_1 = cirq.GridQubit(i + 1, j)\n", " if jr_ij == -1:\n", " yield cirq.X(q)\n", " yield cirq.X(q_1)\n", " yield cz_gate(q, q_1)\n", " if jr_ij == -1:\n", " yield cirq.X(q)\n", " yield cirq.X(q_1)\n", "\n", " for i, jc_row in enumerate(jc):\n", " for j, jc_ij in enumerate(jc_row):\n", " q = cirq.GridQubit(i, j)\n", " q_1 = cirq.GridQubit(i, j + 1)\n", " if jc_ij == -1:\n", " yield cirq.X(q)\n", " yield cirq.X(q_1)\n", " yield cz_gate(q, q_1)\n", " if jc_ij == -1:\n", " yield cirq.X(q)\n", " yield cirq.X(q_1)" ] }, { "cell_type": "markdown", "metadata": { "id": "DI7wQure_5ps" }, "source": [ "3. Apply an `cirq.XPowGate` for the same parameter for all qubits. This is the method `rot_x_layer` we have written above.\n", "\n", "Putting all together, we can create a step that uses just three parameters. Below is the code, which uses the generator for each of the layers (note to advanced Python users: this code does not contain a bug in using `yield` due to the auto flattening of the `OP_TREE concept`. Typically, one would want to use `yield` from here, but this is not necessary):" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.633911Z", "iopub.status.busy": "2024-08-16T10:50:24.633375Z", "iopub.status.idle": "2024-08-16T10:50:24.655805Z", "shell.execute_reply": "2024-08-16T10:50:24.655154Z" }, "id": "M6Z2hxsG_5pt" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ┌──────┐ ┌──────────┐ ┌──────┐ ┌──────────┐\n", "(0, 0): ───H───Z^0.2────X──────────@──────────────X─────────X────@─────────────X───X^0.1───────────────────────────────────────────────\n", " │ │\n", "(0, 1): ───H───Z^0.2─────@─────────┼────X────────────────────────@^0.3─────────X───@───────X^0.1───────────────────────────────────────\n", " │ │ │\n", "(0, 2): ───H───Z^0.2────X┼─────────┼────@─────────X────────────────────────────────@^0.3───X^0.1───────────────────────────────────────\n", " │ │ │\n", "(1, 0): ───H───Z^0.2────X┼─────────@^0.3┼─────────X─────────X────@─────────────X───X───────@───────X───────X^0.1───────────────────────\n", " │ │ │ │\n", "(1, 1): ───H─────────────@^0.3─────X────┼──────────@────────X────┼────X────────────────────@^0.3───X───────X───────@───────X───X^0.1───\n", " │ │ │ │\n", "(1, 2): ───H───Z^0.2────X───────────────@^0.3─────X┼────────X────┼────@────────X───X───────────────────────────────@^0.3───X───X^0.1───\n", " │ │ │\n", "(2, 0): ───H───Z^0.2────X──────────────────────────┼─────────────@^0.3┼────────X───@───────X^0.1───────────────────────────────────────\n", " │ │ │\n", "(2, 1): ───H───Z^0.2────X──────────────────────────@^0.3────X─────────┼────────────@^0.3───X───────@───────X───────X^0.1───────────────\n", " │ │\n", "(2, 2): ───H───Z^0.2────X─────────────────────────────────────────────@^0.3────X───X───────────────@^0.3───X───────X^0.1───────────────\n", " └──────┘ └──────────┘ └──────┘ └──────────┘\n" ] } ], "source": [ "def initial_step(length):\n", " yield prepare_plus_layer(length)\n", "\n", "def one_step(h, jr, jc, x_half_turns, h_half_turns, j_half_turns):\n", " length = len(h)\n", " yield rot_z_layer(h, h_half_turns)\n", " yield rot_11_layer(jr, jc, j_half_turns)\n", " yield rot_x_layer(length, x_half_turns)\n", "\n", "h, jr, jc = random_instance(3)\n", "\n", "circuit = cirq.Circuit() \n", "circuit.append(initial_step(len(h)))\n", "circuit.append(one_step(h, jr, jc, 0.1, 0.2, 0.3))\n", "print(circuit)" ] }, { "cell_type": "markdown", "metadata": { "id": "y5E_9AYw_5pv" }, "source": [ "Here we see that we have chosen particular parameter values $(0.1, 0.2, 0.3)$." ] }, { "cell_type": "markdown", "metadata": { "id": "zAwTXwc7_5pv" }, "source": [ "## Simulation\n", "\n", "In Cirq, the simulators make a distinction between a *run* and a *simulation*. A *run* only allows for a simulation that mimics the actual quantum hardware. For example, it does not allow for access to the amplitudes of the wave function of the system, since that is not experimentally accessible. *Simulate* commands, however, are broader and allow different forms of simulation. When prototyping small circuits, it is useful to execute *simulate* methods, but one should be wary of relying on them when running against actual hardware." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.659342Z", "iopub.status.busy": "2024-08-16T10:50:24.658824Z", "iopub.status.idle": "2024-08-16T10:50:24.677064Z", "shell.execute_reply": "2024-08-16T10:50:24.676391Z" }, "id": "PXpn3xvT_5pv" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Counter({348: 3, 401: 2, 286: 2, 476: 2, 139: 2, 412: 2, 56: 2, 233: 2, 316: 2, 341: 2, 387: 2, 269: 1, 337: 1, 184: 1, 179: 1, 177: 1, 294: 1, 451: 1, 395: 1, 282: 1, 138: 1, 142: 1, 174: 1, 368: 1, 129: 1, 308: 1, 38: 1, 81: 1, 44: 1, 135: 1, 284: 1, 455: 1, 187: 1, 342: 1, 120: 1, 49: 1, 146: 1, 301: 1, 23: 1, 498: 1, 405: 1, 107: 1, 227: 1, 234: 1, 185: 1, 439: 1, 302: 1, 371: 1, 25: 1, 169: 1, 338: 1, 442: 1, 28: 1, 453: 1, 409: 1, 376: 1, 66: 1, 220: 1, 257: 1, 326: 1, 363: 1, 314: 1, 390: 1, 441: 1, 372: 1, 381: 1, 180: 1, 365: 1, 317: 1, 486: 1, 166: 1, 426: 1, 454: 1, 278: 1, 263: 1, 58: 1, 188: 1, 61: 1, 389: 1, 273: 1, 318: 1, 170: 1, 130: 1, 471: 1, 438: 1, 352: 1, 359: 1, 162: 1})\n" ] } ], "source": [ "simulator = cirq.Simulator()\n", "circuit = cirq.Circuit()\n", "circuit.append(initial_step(len(h)))\n", "circuit.append(one_step(h, jr, jc, 0.1, 0.2, 0.3))\n", "circuit.append(cirq.measure(*qubits, key='x'))\n", "results = simulator.run(circuit, repetitions=100)\n", "print(results.histogram(key='x'))" ] }, { "cell_type": "markdown", "metadata": { "id": "DEIjXRgt_5px" }, "source": [ "Note that we have run the simulation 100 times and produced a histogram of the counts of the measurement results. What are the keys in the histogram counter? Note that we have passed in the order of the qubits. This ordering is then used to translate the order of the measurement results to a register using a [big endian representation](https://en.wikipedia.org/wiki/Endianness).\n", "\n", "For our optimization problem, we want to calculate the value of the objective function for a given result run. One way to do this is using the raw measurement data from the result of `simulator.run`. Another way to do this is to provide to the histogram a method to calculate the objective: this will then be used as the key for the returned `Counter`." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.680439Z", "iopub.status.busy": "2024-08-16T10:50:24.679860Z", "iopub.status.idle": "2024-08-16T10:50:24.691769Z", "shell.execute_reply": "2024-08-16T10:50:24.691107Z" }, "id": "Loy-K3YY_5py" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Counter({3: 19, 1: 17, -1: 14, 7: 12, -3: 12, 5: 9, -5: 7, -7: 5, 9: 3, -9: 1, 11: 1})\n" ] } ], "source": [ "import numpy as np\n", "\n", "def energy_func(length, h, jr, jc):\n", " def energy(measurements):\n", " # Reshape measurement into array that matches grid shape.\n", " meas_list_of_lists = [measurements[i * length:(i + 1) * length]\n", " for i in range(length)]\n", " # Convert true/false to +1/-1.\n", " pm_meas = 1 - 2 * np.array(meas_list_of_lists).astype(np.int32)\n", "\n", " tot_energy = np.sum(pm_meas * h)\n", " for i, jr_row in enumerate(jr):\n", " for j, jr_ij in enumerate(jr_row):\n", " tot_energy += jr_ij * pm_meas[i, j] * pm_meas[i + 1, j]\n", " for i, jc_row in enumerate(jc):\n", " for j, jc_ij in enumerate(jc_row):\n", " tot_energy += jc_ij * pm_meas[i, j] * pm_meas[i, j + 1]\n", " return tot_energy\n", " return energy\n", "print(results.histogram(key='x', fold_func=energy_func(3, h, jr, jc)))" ] }, { "cell_type": "markdown", "metadata": { "id": "X_kzMzz1_5pz" }, "source": [ "One can then calculate the expectation value over all repetitions:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.695065Z", "iopub.status.busy": "2024-08-16T10:50:24.694519Z", "iopub.status.idle": "2024-08-16T10:50:24.704650Z", "shell.execute_reply": "2024-08-16T10:50:24.703959Z" }, "id": "GH8_ww5a_5p0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Value of the objective function 1.12\n" ] } ], "source": [ "def obj_func(result):\n", " energy_hist = result.histogram(key='x', fold_func=energy_func(3, h, jr, jc))\n", " return np.sum([k * v for k,v in energy_hist.items()]) / result.repetitions\n", "print(f'Value of the objective function {obj_func(results)}')" ] }, { "cell_type": "markdown", "metadata": { "id": "9vMxRCBD_5p2" }, "source": [ "### Parameterizing the Ansatz\n", "\n", "Now that we have constructed a variational ansatz and shown how to simulate it using Cirq, we can think about optimizing the value. \n", "\n", "On quantum hardware, one would most likely want to have the optimization code as close to the hardware as possible. As the classical hardware that is allowed to inter-operate with the quantum hardware becomes better specified, this language will be better defined. Without this specification, however, Cirq also provides a useful concept for optimizing the looping in many optimization algorithms. This is the fact that many of the value in the gate sets can, instead of being specified by a float, be specified by a `symply.Symbol`, and this `sympy.Symbol` can be substituted for a value specified at execution time.\n", "\n", "Luckily for us, we have written our code so that using parameterized values is as simple as passing `sympy.Symbol` objects where we previously passed float values." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.707938Z", "iopub.status.busy": "2024-08-16T10:50:24.707405Z", "iopub.status.idle": "2024-08-16T10:50:24.728775Z", "shell.execute_reply": "2024-08-16T10:50:24.728107Z" }, "id": "D49TnPrt_5p2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ┌────────┐ ┌──────────────┐ ┌────────┐ ┌──────────────┐\n", "(0, 0): ───H───Z^beta────X────────────@──────────────────X───────────X────@─────────────────X───X^alpha─────────────────────────────────────────────────────────M('x')───\n", " │ │ │\n", "(0, 1): ───H───Z^beta─────@───────────┼──────X────────────────────────────@^gamma───────────X───@─────────X^alpha───────────────────────────────────────────────M────────\n", " │ │ │ │\n", "(0, 2): ───H───Z^beta────X┼───────────┼──────@───────────X──────────────────────────────────────@^gamma───X^alpha───────────────────────────────────────────────M────────\n", " │ │ │ │\n", "(1, 0): ───H───Z^beta────X┼───────────@^gamma┼───────────X───────────X────@─────────────────X───X─────────@─────────X─────────X^alpha───────────────────────────M────────\n", " │ │ │ │ │\n", "(1, 1): ───H──────────────@^gamma─────X──────┼────────────@──────────X────┼──────X────────────────────────@^gamma───X─────────X─────────@─────────X───X^alpha───M────────\n", " │ │ │ │ │\n", "(1, 2): ───H───Z^beta────X───────────────────@^gamma─────X┼──────────X────┼──────@──────────X───X───────────────────────────────────────@^gamma───X───X^alpha───M────────\n", " │ │ │ │\n", "(2, 0): ───H───Z^beta────X────────────────────────────────┼───────────────@^gamma┼──────────X───@─────────X^alpha───────────────────────────────────────────────M────────\n", " │ │ │ │\n", "(2, 1): ───H───Z^beta────X────────────────────────────────@^gamma────X───────────┼──────────────@^gamma───X─────────@─────────X─────────X^alpha─────────────────M────────\n", " │ │ │\n", "(2, 2): ───H───Z^beta────X───────────────────────────────────────────────────────@^gamma────X───X───────────────────@^gamma───X─────────X^alpha─────────────────M────────\n", " └────────┘ └──────────────┘ └────────┘ └──────────────┘\n" ] } ], "source": [ "import sympy\n", "circuit = cirq.Circuit()\n", "alpha = sympy.Symbol('alpha')\n", "beta = sympy.Symbol('beta')\n", "gamma = sympy.Symbol('gamma')\n", "circuit.append(initial_step(len(h)))\n", "circuit.append(one_step(h, jr, jc, alpha, beta, gamma))\n", "circuit.append(cirq.measure(*qubits, key='x'))\n", "print(circuit)" ] }, { "cell_type": "markdown", "metadata": { "id": "StwKTU7R_5p5" }, "source": [ "Note now that the circuit's gates are parameterized.\n", "\n", "Parameters are specified at runtime using a `cirq.ParamResolver`, which is just a dictionary from `Symbol` keys to runtime values. \n", "\n", "For instance:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.732082Z", "iopub.status.busy": "2024-08-16T10:50:24.731561Z", "iopub.status.idle": "2024-08-16T10:50:24.736847Z", "shell.execute_reply": "2024-08-16T10:50:24.736203Z" }, "id": "XOmpCqRq_5p5" }, "outputs": [], "source": [ "resolver = cirq.ParamResolver({'alpha': 0.1, 'beta': 0.3, 'gamma': 0.7})\n", "resolved_circuit = cirq.resolve_parameters(circuit, resolver)" ] }, { "cell_type": "markdown", "metadata": { "id": "DEKrxQrL_5p7" }, "source": [ "resolves the parameters to actual values in the circuit.\n", "\n", "Cirq also has the concept of a *sweep*. A sweep is a collection of parameter resolvers. This runtime information is very useful when one wants to run many circuits for many different parameter values. Sweeps can be created to specify values directly (this is one way to get classical information into a circuit), or a variety of helper methods. For example suppose we want to evaluate our circuit over an equally spaced grid of parameter values. We can easily create this using `cirq.LinSpace`." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:24.740006Z", "iopub.status.busy": "2024-08-16T10:50:24.739591Z", "iopub.status.idle": "2024-08-16T10:50:26.614131Z", "shell.execute_reply": "2024-08-16T10:50:26.613300Z" }, "id": "z43HpXbX_5p7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OrderedDict([('alpha', 0.1), ('beta', 0.1), ('gamma', 0.1)]) 0.76\n", "OrderedDict([('alpha', 0.1), ('beta', 0.1), ('gamma', 0.30000000000000004)]) 0.44\n", "OrderedDict([('alpha', 0.1), ('beta', 0.1), ('gamma', 0.5)]) 0.06\n", "OrderedDict([('alpha', 0.1), ('beta', 0.1), ('gamma', 0.7000000000000001)]) -1.1\n", "OrderedDict([('alpha', 0.1), ('beta', 0.1), ('gamma', 0.9)]) 0.64\n", "OrderedDict([('alpha', 0.1), ('beta', 0.30000000000000004), ('gamma', 0.1)]) 2.26\n", "OrderedDict([('alpha', 0.1), ('beta', 0.30000000000000004), ('gamma', 0.30000000000000004)]) 1.86\n", "OrderedDict([('alpha', 0.1), ('beta', 0.30000000000000004), ('gamma', 0.5)]) 0.9\n", "OrderedDict([('alpha', 0.1), ('beta', 0.30000000000000004), ('gamma', 0.7000000000000001)]) 0.04\n", "OrderedDict([('alpha', 0.1), ('beta', 0.30000000000000004), ('gamma', 0.9)]) -0.96\n", "OrderedDict([('alpha', 0.1), ('beta', 0.5), ('gamma', 0.1)]) 2.84\n", "OrderedDict([('alpha', 0.1), ('beta', 0.5), ('gamma', 0.30000000000000004)]) 3.74\n", "OrderedDict([('alpha', 0.1), ('beta', 0.5), ('gamma', 0.5)]) 1.8\n", "OrderedDict([('alpha', 0.1), ('beta', 0.5), ('gamma', 0.7000000000000001)]) 0.36\n", "OrderedDict([('alpha', 0.1), ('beta', 0.5), ('gamma', 0.9)]) -0.74\n", "OrderedDict([('alpha', 0.1), ('beta', 0.7000000000000001), ('gamma', 0.1)]) 2.12\n", "OrderedDict([('alpha', 0.1), ('beta', 0.7000000000000001), ('gamma', 0.30000000000000004)]) 2.46\n", "OrderedDict([('alpha', 0.1), ('beta', 0.7000000000000001), ('gamma', 0.5)]) 2.14\n", "OrderedDict([('alpha', 0.1), ('beta', 0.7000000000000001), ('gamma', 0.7000000000000001)]) 1.08\n", "OrderedDict([('alpha', 0.1), ('beta', 0.7000000000000001), ('gamma', 0.9)]) 0.12\n", "OrderedDict([('alpha', 0.1), ('beta', 0.9), ('gamma', 0.1)]) 0.24\n", "OrderedDict([('alpha', 0.1), ('beta', 0.9), ('gamma', 0.30000000000000004)]) 0.9\n", "OrderedDict([('alpha', 0.1), ('beta', 0.9), ('gamma', 0.5)]) 1.36\n", "OrderedDict([('alpha', 0.1), ('beta', 0.9), ('gamma', 0.7000000000000001)]) -0.36\n", "OrderedDict([('alpha', 0.1), ('beta', 0.9), ('gamma', 0.9)]) 1.02\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.1), ('gamma', 0.1)]) 1.64\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.1), ('gamma', 0.30000000000000004)]) -0.32\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.1), ('gamma', 0.5)]) -1.5\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.1), ('gamma', 0.7000000000000001)]) -1.18\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.1), ('gamma', 0.9)]) 0.02\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.30000000000000004), ('gamma', 0.1)]) 4.56\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.30000000000000004), ('gamma', 0.30000000000000004)]) 4.8\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.30000000000000004), ('gamma', 0.5)]) 1.3\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.30000000000000004), ('gamma', 0.7000000000000001)]) 0.06\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.30000000000000004), ('gamma', 0.9)]) -0.18\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.5), ('gamma', 0.1)]) 4.26\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.5), ('gamma', 0.30000000000000004)]) 5.82\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.5), ('gamma', 0.5)]) 3.58\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.5), ('gamma', 0.7000000000000001)]) 0.6\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.5), ('gamma', 0.9)]) 0.22\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.7000000000000001), ('gamma', 0.1)]) 3.84\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.7000000000000001), ('gamma', 0.30000000000000004)]) 4.72\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.7000000000000001), ('gamma', 0.5)]) 4.26\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.7000000000000001), ('gamma', 0.7000000000000001)]) 1.8\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.7000000000000001), ('gamma', 0.9)]) 1.1\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.9), ('gamma', 0.1)]) 1.46\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.9), ('gamma', 0.30000000000000004)]) 3.66\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.9), ('gamma', 0.5)]) 2.66\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.9), ('gamma', 0.7000000000000001)]) 2.06\n", "OrderedDict([('alpha', 0.30000000000000004), ('beta', 0.9), ('gamma', 0.9)]) 0.46\n", "OrderedDict([('alpha', 0.5), ('beta', 0.1), ('gamma', 0.1)]) 1.44\n", "OrderedDict([('alpha', 0.5), ('beta', 0.1), ('gamma', 0.30000000000000004)]) -0.62\n", "OrderedDict([('alpha', 0.5), ('beta', 0.1), ('gamma', 0.5)]) -1.6\n", "OrderedDict([('alpha', 0.5), ('beta', 0.1), ('gamma', 0.7000000000000001)]) -0.3\n", "OrderedDict([('alpha', 0.5), ('beta', 0.1), ('gamma', 0.9)]) 0.0\n", "OrderedDict([('alpha', 0.5), ('beta', 0.30000000000000004), ('gamma', 0.1)]) 4.92\n", "OrderedDict([('alpha', 0.5), ('beta', 0.30000000000000004), ('gamma', 0.30000000000000004)]) 3.88\n", "OrderedDict([('alpha', 0.5), ('beta', 0.30000000000000004), ('gamma', 0.5)]) 1.16\n", "OrderedDict([('alpha', 0.5), ('beta', 0.30000000000000004), ('gamma', 0.7000000000000001)]) -0.44\n", "OrderedDict([('alpha', 0.5), ('beta', 0.30000000000000004), ('gamma', 0.9)]) -0.48\n", "OrderedDict([('alpha', 0.5), ('beta', 0.5), ('gamma', 0.1)]) 5.18\n", "OrderedDict([('alpha', 0.5), ('beta', 0.5), ('gamma', 0.30000000000000004)]) 4.58\n", "OrderedDict([('alpha', 0.5), ('beta', 0.5), ('gamma', 0.5)]) 2.44\n", "OrderedDict([('alpha', 0.5), ('beta', 0.5), ('gamma', 0.7000000000000001)]) 1.16\n", "OrderedDict([('alpha', 0.5), ('beta', 0.5), ('gamma', 0.9)]) 1.02\n", "OrderedDict([('alpha', 0.5), ('beta', 0.7000000000000001), ('gamma', 0.1)]) 4.64\n", "OrderedDict([('alpha', 0.5), ('beta', 0.7000000000000001), ('gamma', 0.30000000000000004)]) 4.14\n", "OrderedDict([('alpha', 0.5), ('beta', 0.7000000000000001), ('gamma', 0.5)]) 2.14\n", "OrderedDict([('alpha', 0.5), ('beta', 0.7000000000000001), ('gamma', 0.7000000000000001)]) 0.66\n", "OrderedDict([('alpha', 0.5), ('beta', 0.7000000000000001), ('gamma', 0.9)]) 0.72\n", "OrderedDict([('alpha', 0.5), ('beta', 0.9), ('gamma', 0.1)]) 4.16\n", "OrderedDict([('alpha', 0.5), ('beta', 0.9), ('gamma', 0.30000000000000004)]) 3.34\n", "OrderedDict([('alpha', 0.5), ('beta', 0.9), ('gamma', 0.5)]) 1.82\n", "OrderedDict([('alpha', 0.5), ('beta', 0.9), ('gamma', 0.7000000000000001)]) 0.54\n", "OrderedDict([('alpha', 0.5), ('beta', 0.9), ('gamma', 0.9)]) -1.32\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.1), ('gamma', 0.1)]) 0.26\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.1), ('gamma', 0.30000000000000004)]) -3.22\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.1), ('gamma', 0.5)]) -0.88\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.1), ('gamma', 0.7000000000000001)]) 0.94\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.1), ('gamma', 0.9)]) 0.4\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.30000000000000004), ('gamma', 0.1)]) 3.04\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.30000000000000004), ('gamma', 0.30000000000000004)]) 0.5\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.30000000000000004), ('gamma', 0.5)]) -1.5\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.30000000000000004), ('gamma', 0.7000000000000001)]) 0.16\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.30000000000000004), ('gamma', 0.9)]) 0.06\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.5), ('gamma', 0.1)]) 3.68\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.5), ('gamma', 0.30000000000000004)]) 2.24\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.5), ('gamma', 0.5)]) -0.46\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.5), ('gamma', 0.7000000000000001)]) -0.78\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.5), ('gamma', 0.9)]) 0.5\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.7000000000000001), ('gamma', 0.1)]) 4.16\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.7000000000000001), ('gamma', 0.30000000000000004)]) 2.28\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.7000000000000001), ('gamma', 0.5)]) 0.18\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.7000000000000001), ('gamma', 0.7000000000000001)]) -0.78\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.7000000000000001), ('gamma', 0.9)]) -1.08\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.9), ('gamma', 0.1)]) 2.86\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.9), ('gamma', 0.30000000000000004)]) 3.32\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.9), ('gamma', 0.5)]) -0.08\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.9), ('gamma', 0.7000000000000001)]) -0.8\n", "OrderedDict([('alpha', 0.7000000000000001), ('beta', 0.9), ('gamma', 0.9)]) -0.92\n", "OrderedDict([('alpha', 0.9), ('beta', 0.1), ('gamma', 0.1)]) -0.3\n", "OrderedDict([('alpha', 0.9), ('beta', 0.1), ('gamma', 0.30000000000000004)]) -1.38\n", "OrderedDict([('alpha', 0.9), ('beta', 0.1), ('gamma', 0.5)]) -0.28\n", "OrderedDict([('alpha', 0.9), ('beta', 0.1), ('gamma', 0.7000000000000001)]) 0.14\n", "OrderedDict([('alpha', 0.9), ('beta', 0.1), ('gamma', 0.9)]) 0.36\n", "OrderedDict([('alpha', 0.9), ('beta', 0.30000000000000004), ('gamma', 0.1)]) 1.48\n", "OrderedDict([('alpha', 0.9), ('beta', 0.30000000000000004), ('gamma', 0.30000000000000004)]) -0.56\n", "OrderedDict([('alpha', 0.9), ('beta', 0.30000000000000004), ('gamma', 0.5)]) -0.84\n", "OrderedDict([('alpha', 0.9), ('beta', 0.30000000000000004), ('gamma', 0.7000000000000001)]) -0.44\n", "OrderedDict([('alpha', 0.9), ('beta', 0.30000000000000004), ('gamma', 0.9)]) 0.82\n", "OrderedDict([('alpha', 0.9), ('beta', 0.5), ('gamma', 0.1)]) 1.76\n", "OrderedDict([('alpha', 0.9), ('beta', 0.5), ('gamma', 0.30000000000000004)]) 0.8\n", "OrderedDict([('alpha', 0.9), ('beta', 0.5), ('gamma', 0.5)]) -0.34\n", "OrderedDict([('alpha', 0.9), ('beta', 0.5), ('gamma', 0.7000000000000001)]) -0.88\n", "OrderedDict([('alpha', 0.9), ('beta', 0.5), ('gamma', 0.9)]) -0.82\n", "OrderedDict([('alpha', 0.9), ('beta', 0.7000000000000001), ('gamma', 0.1)]) 2.5\n", "OrderedDict([('alpha', 0.9), ('beta', 0.7000000000000001), ('gamma', 0.30000000000000004)]) 0.2\n", "OrderedDict([('alpha', 0.9), ('beta', 0.7000000000000001), ('gamma', 0.5)]) -0.5\n", "OrderedDict([('alpha', 0.9), ('beta', 0.7000000000000001), ('gamma', 0.7000000000000001)]) -0.92\n", "OrderedDict([('alpha', 0.9), ('beta', 0.7000000000000001), ('gamma', 0.9)]) 0.12\n", "OrderedDict([('alpha', 0.9), ('beta', 0.9), ('gamma', 0.1)]) 1.08\n", "OrderedDict([('alpha', 0.9), ('beta', 0.9), ('gamma', 0.30000000000000004)]) 0.36\n", "OrderedDict([('alpha', 0.9), ('beta', 0.9), ('gamma', 0.5)]) 0.66\n", "OrderedDict([('alpha', 0.9), ('beta', 0.9), ('gamma', 0.7000000000000001)]) -1.32\n", "OrderedDict([('alpha', 0.9), ('beta', 0.9), ('gamma', 0.9)]) -0.5\n" ] } ], "source": [ "sweep = (cirq.Linspace(key='alpha', start=0.1, stop=0.9, length=5)\n", " * cirq.Linspace(key='beta', start=0.1, stop=0.9, length=5)\n", " * cirq.Linspace(key='gamma', start=0.1, stop=0.9, length=5))\n", "results = simulator.run_sweep(circuit, params=sweep, repetitions=100)\n", "for result in results:\n", " print(result.params.param_dict, obj_func(result))" ] }, { "cell_type": "markdown", "metadata": { "id": "10JkH8Ka_5p9" }, "source": [ "### Finding the Minimum\n", "\n", "Now we have all the code, we do a simple grid search over values to find a minimal value. Grid search is not the best optimization algorithm, but is here simply illustrative." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T10:50:26.618024Z", "iopub.status.busy": "2024-08-16T10:50:26.617354Z", "iopub.status.idle": "2024-08-16T10:50:41.248061Z", "shell.execute_reply": "2024-08-16T10:50:41.247264Z" }, "id": "oFtTLBDq_5p-" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Minimum objective value is -4.92.\n" ] } ], "source": [ "sweep_size = 10\n", "sweep = (cirq.Linspace(key='alpha', start=0.0, stop=1.0, length=sweep_size)\n", " * cirq.Linspace(key='beta', start=0.0, stop=1.0, length=sweep_size)\n", " * cirq.Linspace(key='gamma', start=0.0, stop=1.0, length=sweep_size))\n", "results = simulator.run_sweep(circuit, params=sweep, repetitions=100)\n", "\n", "min = None\n", "min_params = None\n", "for result in results:\n", " value = obj_func(result)\n", " if min is None or value < min:\n", " min = value\n", " min_params = result.params\n", "print(f'Minimum objective value is {min}.')" ] }, { "cell_type": "markdown", "metadata": { "id": "Rjg59AG5_5p_" }, "source": [ "We've created a simple variational quantum algorithm using Cirq. Where to go next? Perhaps you can play around with the above code and work on analyzing the algorithms performance. Add new parameterized circuits and build an end to end program for analyzing these circuits." ] } ], "metadata": { "colab": { "name": "variational_algorithm.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 0 }