1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "metadata": {
6    "id": "CP4Mg2LwZ0ee"
7   },
8   "source": [
9    "##### Copyright 2020 The Cirq Developers"
10   ]
11  },
12  {
13   "cell_type": "code",
14   "execution_count": null,
15   "metadata": {
16    "cellView": "form",
17    "id": "OgHgJkWtZ08Q"
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": "nrqc5AdQWHND"
38   },
39   "source": [
40    "# Google Quantum Computing Service"
41   ]
42  },
43  {
44   "cell_type": "markdown",
45   "metadata": {
46    "id": "OQdp_uLYZz8l"
47   },
48   "source": [
49    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
50    "  <td>\n",
51    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/google/concepts\"><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/google/concepts.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/google/concepts.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/google/concepts.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": "code",
67   "execution_count": null,
68   "metadata": {
69    "id": "bd9529db1c0b"
70   },
71   "outputs": [],
72   "source": [
73    "try:\n",
74    "    import cirq\n",
75    "except ImportError:\n",
76    "    print(\"installing cirq...\")\n",
77    "    !pip install --quiet cirq\n",
78    "    print(\"installed cirq.\")"
79   ]
80  },
81  {
82   "cell_type": "markdown",
83   "metadata": {
84    "id": "EkFikKuDV3D1"
85   },
86   "source": [
87    "*Run quantum computing programs on Google’s quantum processors*\n",
88    "\n",
89    "Quantum Computing Service gives customers access to Google's quantum computing hardware.  Programs that are written in Cirq, an open-source quantum computing program language, can be sent to run on a quantum computer in Google’s quantum computing lab in Santa Barbara, CA. You will be able to do  arbitrary single qubit rotations as well as several choices of two qubit gates.  Measurements of all qubits as a final step are also supported. \n",
90    "\n",
91    "Access is currently only granted to those on an approved list.  Apply to become an early access partner with this [questionnaire](https://docs.google.com/forms/d/1DfUWu4zUAJ87GKy-ZoTHrFri5IwIteKtMxKfsy3lmHE).\n"
92   ]
93  },
94  {
95   "cell_type": "markdown",
96   "metadata": {
97    "id": "4gv7nr20bVNu"
98   },
99   "source": [
100    "## Concepts\n",
101    "\n",
102    "Quantum Computing Service uses Cirq, an open source Python framework for creating quantum programs and running them against simulators or against real quantum computers.    Here we provide a conceptual overview of how to run a quantum program on a quantum processor.\n",
103    "\n",
104    "Installing Cirq:"
105   ]
106  },
107  {
108   "cell_type": "markdown",
109   "metadata": {
110    "id": "9A-6IYPeb6_g"
111   },
112   "source": [
113    "### Circuits\n",
114    "\n",
115    "The language of quantum computing is the quantum circuit model.  Cirq is our open source framework which allows one to write a quantum circuit model in Python.  To learn more about Cirq itself, we recommend starting with the [Tutorial](../tutorials/basics.ipynb).  Conceptually Cirq can be thought of as a way to create an object in Python that contains the information about a quantum circuit.  Here, for example, we create a single circuit made up of square root of NOT gates, controlled-Z gates, and a measurement:"
116   ]
117  },
118  {
119   "cell_type": "code",
120   "execution_count": null,
121   "metadata": {
122    "id": "E2VjfwVubc8V"
123   },
124   "outputs": [
125    {
126     "name": "stdout",
127     "output_type": "stream",
128     "text": [
129      "0: ───X^0.5───@────────────────\n",
130      "              │\n",
131      "1: ───X^0.5───@───@────────────\n",
132      "                  │\n",
133      "2: ───X^0.5───────@───M('m')───\n"
134     ]
135    }
136   ],
137   "source": [
138    "import cirq\n",
139    "\n",
140    "q0, q1, q2 = cirq.LineQubit.range(3)\n",
141    "circuit = cirq.Circuit(\n",
142    "    (cirq.X ** 0.5).on_each(q0, q1, q2),\n",
143    "    cirq.CZ(q0, q1),\n",
144    "    cirq.CZ(q1, q2),\n",
145    "    cirq.measure(q2, key='m')\n",
146    ")\n",
147    "print(circuit)"
148   ]
149  },
150  {
151   "cell_type": "markdown",
152   "metadata": {
153    "id": "P-_8Gl-HZTZH"
154   },
155   "source": [
156    "### Quantum Engine API\n",
157    "\n",
158    "Quantum Engine is the name of the cloud API which one can call to run the circuits you create in Cirq on simulator or on quantum computers.  When access is enabled, users can write code in Cirq that makes a call to Google’s servers to run a circuit that they have built in Cirq on simulators or on quantum computers.  Read more about how to do this using cirq's `Engine` class [here](./engine.ipynb).\n",
159    "\n",
160    "![Quantum Engine Conceptual Diagram](../images/engine_diagram.png)"
161   ]
162  },
163  {
164   "cell_type": "markdown",
165   "metadata": {
166    "id": "zxWBUwkfZVBI"
167   },
168   "source": [
169    "### Quantum Programs\n",
170    "\n",
171    "In Cirq, one creates a `Circuit` in Python.  If one then wants to run it using Quantum Engine, one must then upload this Circuit (or Schedule) to the Quantum Engine.  The uploaded version of the Circuit/Schedule is called a Program.  Programs are not the Python code itself, but a representation of the Circuit in a hardware specific language.  Though you will need to make sure that your circuit uses only gates and qubits compatible with the hardware (see `cirq_google.optimized_for_sycamore()` to help with this), the `Engine` class and its corresponding `sampler` will translate the circuit to this serialized form for you.\n",
172    "\n",
173    "![Quantum Program Conceptual Diagram](../images/engine_program.png)\n",
174    "\n",
175    "When you upload a program to Quantum Engine, the program is given a name.  The organizational unit of Quantum Engine is the cloud project (see below), so when you create a program it is identified by the project id and the program id.  We write this as a path\n",
176    "\n",
177    "```\n",
178    "<PROJECT_ID>/programs/<PROGRAM_ID>\n",
179    "```"
180   ]
181  },
182  {
183   "cell_type": "markdown",
184   "metadata": {
185    "id": "0lv_Baq4ZYpO"
186   },
187   "source": [
188    "### Cloud Project\n",
189    "\n",
190    "Quantum Engine interfaces with Google Cloud.  In Google Cloud one can create different projects with different names.  For instance, you might create a project that is for your experiments on quantum computers, and you might create one to manage your side business running servers that day trade Star Wars memorabilia.  Everything under your cloud project can be seen by navigating to the Google Cloud console.  Here, for example, we see the programs that have been uploaded to a quantum projected called “Quantum Cloud Client”:\n",
191    "\n",
192    "![Quantum Cloud Conceptual Diagram](../images/engine_cloud.png)\n",
193    "\n",
194    "See the [Getting Started](../tutorials/google/start.ipynb) guide for step-by-step instructions on how to create a project and enable the API."
195   ]
196  },
197  {
198   "cell_type": "markdown",
199   "metadata": {
200    "id": "gqrO-LvBZbbp"
201   },
202   "source": [
203    "### Jobs\n",
204    "\n",
205    "Once a circuit has been uploaded as a program to Quantum Engine, you can run this program by creating a job.  When you create a job on Quantum Engine, the quantum engine then is responsible for routing your program and the information about the job to the appropriate simulator or quantum computer.  Note that there is a time limit for job length, so you will want to separate your programs into multiple jobs below this limit.\n",
206    "\n",
207    "![Quantum Jobs Conceptual Diagram](../images/engine_job.png)\n",
208    "\n",
209    "Quantum Engine is designed so that you can upload a program once and then, if that program has different parameters (see below), you can run this program multiple times by creating multiple jobs for a given program.  Jobs are associated with programs, so they have names which include everything about your program, plus a job id:\n",
210    "\n",
211    "```\n",
212    "<project id>/programs/<program id>/jobs/<job id>  \n",
213    "```"
214   ]
215  },
216  {
217   "cell_type": "markdown",
218   "metadata": {
219    "id": "w2N-aTOgZgIe"
220   },
221   "source": [
222    "### Parameterized Circuits and Job Context\n",
223    "\n",
224    "Circuits in Cirq can have quantum gates that are “parameterized”.  For example, a gate may represent a rotation of a qubit about the Z axis of the bloch sphere by an angle theta.  For parameterized circuit this angle theta is just specified by a variable, say “my-theta”.  In order to run such a circuit you will need to be able to tell the circuit what value of “my-theta” should be supplied.  In fact you can create a job for a program with parameterized gates which says run this for multiple parameter values.  Or each time you create a new job, you can specify new parameter values.  These parameter values are supplied in a “job context\"."
225   ]
226  },
227  {
228   "cell_type": "markdown",
229   "metadata": {
230    "id": "wrQvQ4hvZio-"
231   },
232   "source": [
233    "### Results\n",
234    "\n",
235    "Once a job has been scheduled to run on a machine (simulator or quantum computer), the machine returns the results of the program to the Quantum Engine.  The Quantum Engine stores these results.  Users can then query for these results (typically one pulls every fraction of a second):\n",
236    "\n",
237    "![Quantum Results Conceptual Diagram](../images/engine_result.png)\n",
238    "\n",
239    "There is only one result for one job, so results can be identified via the identity of the job followed by result:\n",
240    "\n",
241    "```\n",
242    "<project id>/programs/<program id>/jobs/<job id>/result\n",
243    "```"
244   ]
245  },
246  {
247   "cell_type": "markdown",
248   "metadata": {
249    "id": "A2rh2gYzZkFf"
250   },
251   "source": [
252    "### Processors\n",
253    "\n",
254    "When creating a job to run a program, one must specify where the job should run.  This can be a very specific machine to run on, or it can be specified more generally.  Each location that we can schedule a job to run on is described as a processor.  Different processors are available to different users, so processors appear under a project and have a processor id.\n",
255    "\n",
256    "```\n",
257    "<project id>/processors/<processor id>\n",
258    "```\n",
259    "\n",
260    "For example, one processor is the “rainbow” processor, which corresponds to a quantum computer located in Santa Barbara.  Another processor is “xmonsim” which is a fleet of simulators for the xmon architecture which all run in Google’s production datacenters.\n",
261    "\n",
262    "Processor have state associated with them.  For instance, a processor can be “online” or they can be in “maintenance” mode."
263   ]
264  },
265  {
266   "cell_type": "markdown",
267   "metadata": {
268    "id": "nghWy0ALZnOL"
269   },
270   "source": [
271    "### Calibration Metrics\n",
272    "\n",
273    "Processors that are quantum computers periodically undergo calibrations to maintain the quality of the programs that can be run on these processors.  During this calibration metrics about the performance of the quantum computer is collected.  This calibration data is stored by Quantum Engine and users can then query for the state of the calibration (one can ask for the latest calibration, or what the state of calibration was at a given point in time).  Calibrations are also available for past jobs.\n",
274    "\n",
275    "Calibration metrics and can be retrieved via the console or via cirq calls.  See more details on the [Calibration Metrics](calibration.ipynb) page."
276   ]
277  },
278  {
279   "cell_type": "markdown",
280   "metadata": {
281    "id": "DiHuFRL-c9SP"
282   },
283   "source": [
284    "### Reservations\n",
285    "\n",
286    "Processor schedules are managed by a reservation system, assigning blocks of time as \"time slots\" with four possible states:\n",
287    "\n",
288    "*  OPEN_SWIM: Anyone with processor access may run jobs.  There may be additional restrictions per processor that restrict large jobs.\n",
289    "*  MAINTENANCE: restrictions behave the same as during open swim, but there are no guarantees about the availability of the processor or quality of jobs run during this period.\n",
290    "*  RESERVATION: The processor is reserved for restricted use by a user or set of users. Reservations are rooted under a project and editors of that project may run jobs during that project's reservations while using any project they wish. Additional users may also be whitelisted to specific reservations.\n",
291    "*  UNALLOCATED: The processor has not been scheduled for any purpose. A reservation may be made during this time. If nothing is scheduled during this block, it will default to behaving as open swim.\n",
292    "\n",
293    "During non-reservation times, jobs are restricted to run for at most 5 minutes and may have no more than 1,500,000 total shots on the processor.\n",
294    "\n",
295    "The schedule of a processor is \"frozen\" for 4 hours into the immediate future so that users can rely on it for the coming day.\n",
296    "\n",
297    "The ability to make reservations is restricted by budgets that are delegated by Quantum Computing Service admins. These are provided on a weekly basis and communicated either during the weekly user sync or by email and are good for one week in the future, but this policy is subject to change.\n",
298    "\n",
299    "You can use this [reservation colab utility](../tutorials/google/reservations.ipynb)  to view the processor schedule and make reservations."
300   ]
301  }
302 ],
303 "metadata": {
304  "colab": {
305   "collapsed_sections": [],
306   "name": "concepts.ipynb",
307   "toc_visible": true
308  },
309  "kernelspec": {
310   "display_name": "Python 3",
311   "name": "python3"
312  }
313 },
314 "nbformat": 4,
315 "nbformat_minor": 0
316}
317