Skip to content

Commit

Permalink
Adds a tutorial for single/two qubit heatmaps (#3880)
Browse files Browse the repository at this point in the history
- Adds a tutorial for single / two qubit heatmaps
  • Loading branch information
tanujkhattar committed Mar 18, 2021
1 parent 2b591a5 commit 584b221
Show file tree
Hide file tree
Showing 3 changed files with 411 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/_book.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ upper_tabs:
- title: "Issues / requests / questions"
path: /cirq/support

### Visualizations
- heading: "Visualization"
- title: "Heatmaps"
path: /cirq/visualization/heatmaps

#### Contributor Guide ####
- heading: "Contributor guide"
- title: "Contribute"
Expand Down Expand Up @@ -168,6 +173,8 @@ upper_tabs:
path: /cirq/tutorials/google/start
- title: "Floquet calibration"
path: /cirq/tutorials/google/floquet
- title: "Visualizing Calibration Metrics"
path: /cirq/tutorials/google/visualizing_calibration_metrics

#### AQT HARDWARE ####
- heading: "AQT hardware"
Expand Down
264 changes: 264 additions & 0 deletions docs/tutorials/google/visualizing_calibration_metrics.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "7bd5137ff0b2"
},
"source": [
"##### Copyright 2021 The Cirq Developers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "60d1a836f542"
},
"outputs": [],
"source": [
"# 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": "8c466cef02c8"
},
"source": [
"# Calibrations\n",
"The [tutorial on calibration](https://quantumai.google/cirq/google/calibration) gives an introduction to calibration\n",
"metrics and how to retrieve them from the Google Quantum Computing Service (QCS). In this tutorial, we show to visualize these calibration metrics\n",
"via single qubit heatmaps and two qubit interaction heatmaps."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2f4bb2cdfcb6"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://quantumai.google/cirq/tutorials/google/visualizing_calibration_metrics>\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/master/docs/tutorials/google/visualizing_calibration_metrics.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/master/docs/tutorials/google/visualizing_calibration_metrics.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/tutorials/google/visualizing_calibration_metrics.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9e4d953fcc28"
},
"outputs": [],
"source": [
"try:\n",
" import cirq\n",
"except ImportError:\n",
" print(\"installing cirq...\")\n",
" !pip install --quiet cirq\n",
" print(\"installed cirq.\")\n",
" import cirq\n",
"\n",
"import os\n",
"from random import random\n",
"from itertools import combinations"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "89469d76dcf4"
},
"source": [
"## Authenticate and install Cirq\n",
"\n",
"For details of authentication and installation, please see [go/quantum-engine-quickstart](http://go/quantum-engine-quickstart).\n",
"\n",
"Note: The below code will install the latest stable release of cirq. If you need the latest and greatest features and don't mind if a few things aren't quite working correctly, you can install the pre-release version of `cirq` using `pip install --pre cirq` instead of `pip install cirq` to get the most up-to-date features of cirq.\n",
"\n",
"1. Enter the Cloud project ID you'd like to use in the `project_id` field.\n",
"2. Then run the cell below (and go through the auth flow for access to the project id you entered).\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/quantumlib/Cirq/master/docs/images/run-code-block.png\" alt=\"Quantum Engine console\">"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cb23e15bc7bc"
},
"outputs": [],
"source": [
"# The Google Cloud Project id to use.\n",
"project_id = '' #@param {type:\"string\"}\n",
"\n",
"if project_id == 'YOUR_PROJECT_ID':\n",
" if 'GOOGLE_CLOUD_PROJECT' not in os.environ:\n",
" raise Exception(\"Please setup project_id in this cell or set the `GOOGLE_CLOUD_PROJECT` env var to your project id.\")\n",
" project_id = os.environ['GOOGLE_CLOUD_PROJECT']\n",
"else:\n",
" os.environ['GOOGLE_CLOUD_PROJECT'] = project_id\n",
"\n",
"def authenticate_user():\n",
" \"\"\"Runs the user through the Colab OAuth process.\n",
"\n",
" Checks for Google Application Default Credentials and runs interactive login\n",
" if the notebook is executed in Colab. In case the notebook is executed in Jupyter notebook\n",
" or other IPython runtimes, no interactive login is provided, it is assumed that the\n",
" `GOOGLE_APPLICATION_CREDENTIALS` env var is set or `gcloud auth application-default login`\n",
" was executed already.\n",
"\n",
" For more information on using Application Default Credentials see\n",
" https://cloud.google.com/docs/authentication/production\n",
" \"\"\"\n",
" in_colab = False\n",
" try:\n",
" from IPython import get_ipython\n",
" in_colab = 'google.colab' in str(get_ipython())\n",
" except:\n",
" # Notebook is not executed within IPython. Assuming external authentication.\n",
" return\n",
"\n",
" if in_colab:\n",
" from google.colab import auth\n",
" print(\"Getting OAuth2 credentials.\")\n",
" print(\"Press enter after entering the verification code.\")\n",
" auth.authenticate_user(clear_output=False)\n",
" print(\"Authentication complete.\")\n",
" else:\n",
" print(\"Notebook is not executed with Colab, assuming Application Default Credentials are setup.\")\n",
"\n",
"authenticate_user()\n",
"\n",
"print(\"Successful authentication to Google Cloud.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f97d4bc65b8d"
},
"source": [
"## Heatmaps for Calibration Metrics\n",
"\n",
"Calibration Metrics about the performance of the quantum processor can be retrieved from the Google QCS."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1c1d7b666085"
},
"outputs": [],
"source": [
"# Uncomment and replace PROCESSOR_ID.\n",
"# cals = cirq.google.get_engine_calibration(PROCESSOR_ID, project_id)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "007b9feb5ac7"
},
"source": [
"As an example, we create the calibration object with dummy data as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ed5775cf1356"
},
"outputs": [],
"source": [
"metrics = {'single_qubit_errors' : {}, 'two_qubit_errors': {}}\n",
"for qubit in cirq.google.Sycamore.qubits:\n",
" metrics['single_qubit_errors'].update({(qubit,) : [random()]})\n",
"for q_1, q_2 in combinations(cirq.google.Sycamore.qubits, 2):\n",
" if q_1.is_adjacent(q_2):\n",
" metrics['two_qubit_errors'].update({(q_1, q_2) : [random()]})\n",
"cals = cirq.google.Calibration(metrics=metrics)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c7769da3130c"
},
"source": [
"You can plot the single qubit heatmaps by fetching the single qubit metrics from calibration data as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5e1a0f192b9e"
},
"outputs": [],
"source": [
"cals.heatmap('single_qubit_errors').plot()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dcdbcb0d2982"
},
"source": [
"Similarly, you can fetch two-qubit metrics by passing the appropriate key, and plot the two qubit interaction heatmaps.\n",
"\n",
"You can also change various properties of the plot by passing appropriate `kwargs` to the `update_config` method as shown below"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "d0e0f4f94379"
},
"outputs": [],
"source": [
"two_qubit_heatmap = cals.heatmap('two_qubit_errors')\n",
"two_qubit_heatmap.update_config(annotation_format='.2f')\n",
"two_qubit_heatmap.plot()"
]
}
],
"metadata": {
"colab": {
"name": "visualizing_calibration_metrics.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 584b221

Please sign in to comment.