{ "cells": [ { "cell_type": "markdown", "id": "737bce11", "metadata": {}, "source": [ "#### Copyright 2021 The Cirq Developers" ] }, { "cell_type": "code", "execution_count": null, "id": "6db8c987", "metadata": {}, "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", "id": "88f3b242", "metadata": {}, "source": [ "### Cirq-web 3D circuit visualization" ] }, { "cell_type": "markdown", "id": "9f4089af", "metadata": {}, "source": [ "\n", "\n", " \n", " \n", "
\n", " Run in Google Colab\n", " \n", " View source on GitHub\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "99172e40", "metadata": {}, "outputs": [], "source": [ "!pip install --quiet cirq" ] }, { "cell_type": "code", "execution_count": null, "id": "fe0d0e69", "metadata": {}, "outputs": [], "source": [ "import cirq\n", "qubits = [cirq.GridQubit(x, y) for x in range(10) for y in range(10)]\n", "\n", "import cirq.testing\n", "from cirq.protocols import circuit_diagram_info\n", "circuit = cirq.testing.random_circuit(cirq.GridQubit.square(10), n_moments=5, op_density=.7)" ] }, { "cell_type": "code", "execution_count": null, "id": "81689b3f", "metadata": { "scrolled": false }, "outputs": [], "source": [ "\"\"\"\n", "This cell builds a 3D circuit diagram using a big list of operations provided TS Circuit class.\n", "\"\"\"\n", "import cirq_web\n", "from typing import Optional\n", "from cirq_web.circuits.symbols import SymbolInfo\n", "\n", "class FunkyHadamard(cirq_web.circuits.symbols.SymbolResolver):\n", " def resolve(self, operation: cirq.Operation) -> Optional[SymbolInfo]:\n", " if isinstance(operation.gate, cirq.HPowGate):\n", " return SymbolInfo(['Hello!'], ['yellow'])\n", " else:\n", " return None\n", " \n", "resolvers = list(cirq_web.circuits.symbols.DEFAULT_SYMBOL_RESOLVERS) + [\n", " FunkyHadamard()\n", "]\n", "\n", "from cirq_web import Circuit3D\n", "c3d = Circuit3D(circuit, resolvers, 2.5)\n", "display(c3d)" ] }, { "cell_type": "code", "execution_count": null, "id": "139acc3d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }