{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "DkA0Fobtb9dM"
   },
   "source": [
    "##### Copyright 2022 The Cirq Developers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cellView": "form",
    "execution": {
     "iopub.execute_input": "2025-08-12T09:28:17.621537Z",
     "iopub.status.busy": "2025-08-12T09:28:17.621304Z",
     "iopub.status.idle": "2025-08-12T09:28:17.624846Z",
     "shell.execute_reply": "2025-08-12T09:28:17.624223Z"
    },
    "id": "tUshu7YfcAAW"
   },
   "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": "_JFVRPQ1l17m"
   },
   "source": [
    "# QVM Creation Template"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "E6JaKuNTl9SA"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/simulate/qvm_builder_code\"><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/main/docs/simulate/qvm_builder_code.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/main/docs/simulate/qvm_builder_code.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/simulate/qvm_builder_code.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8e6bafdbda4d"
   },
   "source": [
    "This notebook includes a couple of clean and succinct code blocks that you can build on or copy and paste elsewhere in order to make use of the [Quantum Virtual Machine](./quantum_virtual_machine.ipynb) without worrying about how it works inside. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Lfira0gPf0Gd"
   },
   "source": [
    "## **Install** Cirq and qsim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "cellView": "form",
    "execution": {
     "iopub.execute_input": "2025-08-12T09:28:17.627395Z",
     "iopub.status.busy": "2025-08-12T09:28:17.627184Z",
     "iopub.status.idle": "2025-08-12T09:28:33.414819Z",
     "shell.execute_reply": "2025-08-12T09:28:33.413908Z"
    },
    "id": "zs5J6wAXqvtW"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "installing cirq...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "installed cirq.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "installing qsimcirq...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "installed qsimcirq.\n"
     ]
    }
   ],
   "source": [
    "# @title Install `cirq_google` and `qsimcirq`\n",
    "\n",
    "try:\n",
    "    import cirq\n",
    "    import cirq_google\n",
    "except ImportError:\n",
    "    print(\"installing cirq...\")\n",
    "    !pip install --quiet cirq-google\n",
    "    print(\"installed cirq.\")\n",
    "    import cirq\n",
    "    import cirq_google\n",
    "\n",
    "try:\n",
    "    import qsimcirq\n",
    "except ImportError:\n",
    "    print(\"installing qsimcirq...\")\n",
    "    !pip install --quiet qsimcirq\n",
    "    print(f\"installed qsimcirq.\")\n",
    "    import qsimcirq\n",
    "\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "p2JAfQa8gVSe"
   },
   "source": [
    "## Create a **Quantum Virtual Machine**.\n",
    "\n",
    "Instantiate a `cirq.SimulatedLocalEngine` that uses the [Virtual Engine Interface](./virtual_engine_interface.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cellView": "form",
    "execution": {
     "iopub.execute_input": "2025-08-12T09:28:33.418322Z",
     "iopub.status.busy": "2025-08-12T09:28:33.417353Z",
     "iopub.status.idle": "2025-08-12T09:28:33.772322Z",
     "shell.execute_reply": "2025-08-12T09:28:33.771530Z"
    },
    "id": "pbHCUPLpq5WE"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your quantum virtual machine willow_pink is ready, here is the qubit grid: \n",
      "========================\n",
      "\n",
      "                                                        (0, 6)────(0, 7)────(0, 8)\n",
      "                                                        │         │         │\n",
      "                                                        │         │         │\n",
      "                                              (1, 5)────(1, 6)────(1, 7)────(1, 8)\n",
      "                                              │         │         │         │\n",
      "                                              │         │         │         │\n",
      "                                    (2, 4)────(2, 5)────(2, 6)────(2, 7)────(2, 8)────(2, 9)────(2, 10)\n",
      "                                    │         │         │         │         │         │         │\n",
      "                                    │         │         │         │         │         │         │\n",
      "                           (3, 3)───(3, 4)────(3, 5)────(3, 6)────(3, 7)────(3, 8)────(3, 9)────(3, 10)\n",
      "                           │        │         │         │         │         │         │         │\n",
      "                           │        │         │         │         │         │         │         │\n",
      "                  (4, 2)───(4, 3)───(4, 4)────(4, 5)────(4, 6)────(4, 7)────(4, 8)────(4, 9)────(4, 10)────(4, 11)───(4, 12)\n",
      "                  │        │        │         │         │         │         │         │         │          │         │\n",
      "                  │        │        │         │         │         │         │         │         │          │         │\n",
      "         (5, 1)───(5, 2)───(5, 3)───(5, 4)────(5, 5)────(5, 6)────(5, 7)────(5, 8)────(5, 9)────(5, 10)────(5, 11)───(5, 12)\n",
      "         │        │        │        │         │         │         │         │         │         │          │         │\n",
      "         │        │        │        │         │         │         │         │         │         │          │         │\n",
      "(6, 0)───(6, 1)───(6, 2)───(6, 3)───(6, 4)────(6, 5)────(6, 6)────(6, 7)────(6, 8)────(6, 9)────(6, 10)────(6, 11)───(6, 12)───(6, 13)───(6, 14)\n",
      "                  │        │        │         │         │         │         │         │         │          │         │         │\n",
      "                  │        │        │         │         │         │         │         │         │          │         │         │\n",
      "                  (7, 2)───(7, 3)───(7, 4)────(7, 5)────(7, 6)────(7, 7)────(7, 8)────(7, 9)────(7, 10)────(7, 11)───(7, 12)───(7, 13)\n",
      "                  │        │        │         │         │         │         │         │         │          │         │\n",
      "                  │        │        │         │         │         │         │         │         │          │         │\n",
      "                  (8, 2)───(8, 3)───(8, 4)────(8, 5)────(8, 6)────(8, 7)────(8, 8)────(8, 9)────(8, 10)────(8, 11)───(8, 12)\n",
      "                                    │         │         │         │         │         │         │          │\n",
      "                                    │         │         │         │         │         │         │          │\n",
      "                                    (9, 4)────(9, 5)────(9, 6)────(9, 7)────(9, 8)────(9, 9)────(9, 10)────(9, 11)\n",
      "                                    │         │         │         │         │         │         │\n",
      "                                    │         │         │         │         │         │         │\n",
      "                                    (10, 4)───(10, 5)───(10, 6)───(10, 7)───(10, 8)───(10, 9)───(10, 10)\n",
      "                                                        │         │         │         │\n",
      "                                                        │         │         │         │\n",
      "                                                        (11, 6)───(11, 7)───(11, 8)───(11, 9)\n",
      "                                                        │         │         │\n",
      "                                                        │         │         │\n",
      "                                                        (12, 6)───(12, 7)───(12, 8)\n"
     ]
    }
   ],
   "source": [
    "# @title Choose a processor (\"willow_pink\" or \"rainbow\" or \"weber\")\n",
    "# (see cirq_google.engine.list_virtual_processors() for available names)\n",
    "processor_id = \"willow_pink\"  # @param {type:\"string\"}\n",
    "\n",
    "# Instantiate an engine.\n",
    "sim_engine = cirq_google.engine.create_default_noisy_quantum_virtual_machine(\n",
    "    processor_id=processor_id, simulator_class=qsimcirq.QSimSimulator\n",
    ")\n",
    "print(\n",
    "    \"Your quantum virtual machine\",\n",
    "    processor_id,\n",
    "    \"is ready, here is the qubit grid:\",\n",
    "    \"\\n========================\\n\",\n",
    ")\n",
    "print(sim_engine.get_processor(processor_id).get_device())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TJfN17frwo-0"
   },
   "source": [
    "## **Create** a device-ready circuit."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BYxi9xpXjJdI"
   },
   "source": [
    "To learn how to create a device ready circuit, have a look at the [QVM Circuit Preparation](./qvm_basic_example.ipynb) page."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-08-12T09:28:33.774889Z",
     "iopub.status.busy": "2025-08-12T09:28:33.774648Z",
     "iopub.status.idle": "2025-08-12T09:28:33.780437Z",
     "shell.execute_reply": "2025-08-12T09:28:33.779711Z"
    },
    "id": "FCoKJhGri8lR"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 3): ───X^0.5───M───\n"
     ]
    }
   ],
   "source": [
    "# create your device ready circuit here!\n",
    "q0 = cirq.GridQubit(6, 3)\n",
    "your_circuit = cirq.Circuit([(cirq.X**0.5)(q0), cirq.measure(q0)])\n",
    "print(your_circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Zxv0RtJuhaof"
   },
   "source": [
    "## **Execute** Your circuit on the Quantum Virtual Machine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "cellView": "form",
    "execution": {
     "iopub.execute_input": "2025-08-12T09:28:33.782792Z",
     "iopub.status.busy": "2025-08-12T09:28:33.782541Z",
     "iopub.status.idle": "2025-08-12T09:28:33.857997Z",
     "shell.execute_reply": "2025-08-12T09:28:33.857219Z"
    },
    "id": "bFjnNSqRZsFu"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Circuit successfully executed on your quantum virtual machine willow_pink\n",
      "QVM runtime: 0.07056s (3000 reps)\n",
      "You can now print or plot \"results\"\n"
     ]
    }
   ],
   "source": [
    "# @title Enter the name of your device ready circuit and execute it on the Quantum Virtual Machine\n",
    "circuit = your_circuit  # @param\n",
    "\n",
    "reps = 3000\n",
    "start = time.time()\n",
    "results = sim_engine.get_sampler(processor_id).run(circuit, repetitions=reps)\n",
    "elapsed = time.time() - start\n",
    "\n",
    "print('Circuit successfully executed on your quantum virtual machine', processor_id)\n",
    "print(f'QVM runtime: {elapsed:.04g}s ({reps} reps)')\n",
    "print('You can now print or plot \"results\"')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-08-12T09:28:33.860925Z",
     "iopub.status.busy": "2025-08-12T09:28:33.860335Z",
     "iopub.status.idle": "2025-08-12T09:28:33.872743Z",
     "shell.execute_reply": "2025-08-12T09:28:33.872014Z"
    },
    "id": "98bf142f5be6"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "measurements: Counter({1: 1506, 0: 1494})\n"
     ]
    }
   ],
   "source": [
    "print(f\"measurements: {results.histogram(key=q0)}\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "qvm_builder_code.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.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
