{ "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": "2024-08-16T10:40:31.341305Z", "iopub.status.busy": "2024-08-16T10:40:31.340762Z", "iopub.status.idle": "2024-08-16T10:40:31.344961Z", "shell.execute_reply": "2024-08-16T10:40:31.344313Z" }, "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": [ "\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": "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": "2024-08-16T10:40:31.348590Z", "iopub.status.busy": "2024-08-16T10:40:31.348052Z", "iopub.status.idle": "2024-08-16T10:40:46.494188Z", "shell.execute_reply": "2024-08-16T10:40:46.493174Z" }, "id": "zs5J6wAXqvtW" }, "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" ] }, { "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": "2024-08-16T10:40:46.499438Z", "iopub.status.busy": "2024-08-16T10:40:46.497795Z", "iopub.status.idle": "2024-08-16T10:40:46.574385Z", "shell.execute_reply": "2024-08-16T10:40:46.573657Z" }, "id": "pbHCUPLpq5WE" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Your quantum virtual machine rainbow is ready, here is the qubit grid: \n", "========================\n", "\n", " (3, 2)\n", " │\n", " │\n", " (4, 1)───(4, 2)───(4, 3)\n", " │ │ │\n", " │ │ │\n", "(5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4)\n", " │ │ │ │\n", " │ │ │ │\n", " (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5)\n", " │ │ │ │\n", " │ │ │ │\n", " (7, 2)───(7, 3)───(7, 4)───(7, 5)───(7, 6)\n", " │ │ │\n", " │ │ │\n", " (8, 3)───(8, 4)───(8, 5)\n", " │\n", " │\n", " (9, 4)\n" ] } ], "source": [ "# @title Choose a processor (\"rainbow\" or \"weber\")\n", "processor_id = \"rainbow\" # @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": "2024-08-16T10:40:46.577991Z", "iopub.status.busy": "2024-08-16T10:40:46.577394Z", "iopub.status.idle": "2024-08-16T10:40:46.584071Z", "shell.execute_reply": "2024-08-16T10:40:46.583372Z" }, "id": "FCoKJhGri8lR" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4, 1): ───X^0.5───M───\n" ] } ], "source": [ "# create your device ready circuit here!\n", "q0 = cirq.GridQubit(4, 1)\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": "2024-08-16T10:40:46.587423Z", "iopub.status.busy": "2024-08-16T10:40:46.586877Z", "iopub.status.idle": "2024-08-16T10:40:46.632769Z", "shell.execute_reply": "2024-08-16T10:40:46.632094Z" }, "id": "bFjnNSqRZsFu" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Circuit successfully executed on your quantum virtual machine rainbow\n", "QVM runtime: 0.04129s (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\"')" ] } ], "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.10.14" } }, "nbformat": 4, "nbformat_minor": 0 }