{ "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-16T09:11:18.473620Z", "iopub.status.busy": "2024-08-16T09:11:18.473363Z", "iopub.status.idle": "2024-08-16T09:11:18.477402Z", "shell.execute_reply": "2024-08-16T09:11:18.476754Z" }, "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": "igOQCrBOcF5d" }, "source": [ "# Devices" ] }, { "cell_type": "markdown", "metadata": { "id": "LHRAvc9TcHOH" }, "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": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:18.480701Z", "iopub.status.busy": "2024-08-16T09:11:18.480214Z", "iopub.status.idle": "2024-08-16T09:11:35.695721Z", "shell.execute_reply": "2024-08-16T09:11:35.694909Z" }, "id": "bd9529db1c0b" }, "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" ] } ], "source": [ "try:\n", " import cirq\n", "except ImportError:\n", " print(\"installing cirq...\")\n", " !pip install --quiet cirq\n", " print(\"installed cirq.\")\n", " import cirq" ] }, { "cell_type": "markdown", "metadata": { "id": "x6s7QnYKAdo7" }, "source": [ "## Validation basics\n", "\n", "When you are looking to run an algorithm on a real quantum computer (not a simulated one), there are often many additional constraints placed on the circuits you would like to run. Qubit connectivity, algorithm layout and the types of gates used in the circuit all become much more important. Cirq uses the abstract class `Device` to represent the constraints of an actual quantum processor. An example implementation of a device can be seen in the `cirq_google.Sycamore` class:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:35.700223Z", "iopub.status.busy": "2024-08-16T09:11:35.699494Z", "iopub.status.idle": "2024-08-16T09:11:36.051449Z", "shell.execute_reply": "2024-08-16T09:11:36.050707Z" }, "id": "BFcKF8Xp-A2S" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " (0, 5)───(0, 6)\n", " │ │\n", " │ │\n", " (1, 4)───(1, 5)───(1, 6)───(1, 7)\n", " │ │ │ │\n", " │ │ │ │\n", " (2, 3)───(2, 4)───(2, 5)───(2, 6)───(2, 7)───(2, 8)\n", " │ │ │ │ │ │\n", " │ │ │ │ │ │\n", " (3, 2)───(3, 3)───(3, 4)───(3, 5)───(3, 6)───(3, 7)───(3, 8)───(3, 9)\n", " │ │ │ │ │ │ │ │\n", " │ │ │ │ │ │ │ │\n", " (4, 1)───(4, 2)───(4, 3)───(4, 4)───(4, 5)───(4, 6)───(4, 7)───(4, 8)───(4, 9)\n", " │ │ │ │ │ │ │ │\n", " │ │ │ │ │ │ │ │\n", "(5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4)───(5, 5)───(5, 6)───(5, 7)───(5, 8)\n", " │ │ │ │ │ │ │\n", " │ │ │ │ │ │ │\n", " (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5)───(6, 6)───(6, 7)\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": [ "import cirq_google\n", "import networkx as nx\n", "\n", "my_device = cirq_google.Sycamore\n", "print(my_device)" ] }, { "cell_type": "markdown", "metadata": { "id": "bN7O0XCLAH7G" }, "source": [ "This string representation of the device indicates the structure of the device and the connectivity of the qubits. In Sycamore's case, two-qubit gates can only be executed on qubits that are adjacent in the grid. Other constraints, like supported gates, are not shown in this representation.\n", "\n", "You can access all of the constraints indirectly by validating moments, operations and circuits with the `validate_***` method to verify if that structure would work on the device or not. In general, the `validate_***` method will tell you what part of your operation/moment/circuit does not fit the device's constraints, and why. All devices support this functionality. For the Sycamore device:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.055204Z", "iopub.status.busy": "2024-08-16T09:11:36.054540Z", "iopub.status.idle": "2024-08-16T09:11:36.059288Z", "shell.execute_reply": "2024-08-16T09:11:36.058633Z" }, "id": "53z2ucqZAFz7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Qubit not on device: cirq.GridQubit(7, 7).\n" ] } ], "source": [ "op1 = cirq.X(cirq.GridQubit(7, 7))\n", "\n", "try:\n", " my_device.validate_operation(op1)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": { "id": "jkgV8lhEB7L5" }, "source": [ "The previous example used a qubit that wasn't on the device, making the operation invalid. Most `validate_operation` implementations also take into account things like supported gates and connectivity as well:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.062578Z", "iopub.status.busy": "2024-08-16T09:11:36.062032Z", "iopub.status.idle": "2024-08-16T09:11:36.067630Z", "shell.execute_reply": "2024-08-16T09:11:36.066965Z" }, "id": "VM3kDY6QCRPY" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Qubit pair is not valid on device: (cirq.GridQubit(7, 4), cirq.GridQubit(7, 6)).\n" ] } ], "source": [ "q1, q2, q3 = cirq.GridQubit(7, 4), cirq.GridQubit(7, 5), cirq.GridQubit(7, 6)\n", "op1 = cirq.H(q1)\n", "op2 = cirq_google.SYC(q1, q3)\n", "\n", "try:\n", " my_device.validate_operation(op1)\n", "except Exception as e:\n", " print(e)\n", "\n", "try:\n", " my_device.validate_operation(op2)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": { "id": "EcTgQg5xCxzG" }, "source": [ "These validation operations can also be used with moments of operations and full circuits:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.070768Z", "iopub.status.busy": "2024-08-16T09:11:36.070348Z", "iopub.status.idle": "2024-08-16T09:11:36.076144Z", "shell.execute_reply": "2024-08-16T09:11:36.075477Z" }, "id": "EiolyMKLC262" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Qubit pair is not valid on device: (cirq.GridQubit(7, 4), cirq.GridQubit(7, 6)).\n" ] } ], "source": [ "op1 = cirq.X(q2)\n", "op2 = cirq_google.SYC(q1, q3)\n", "try:\n", " my_device.validate_moment(cirq.Moment([op1, op2]))\n", "except Exception as e:\n", " print(e)\n", "\n", "my_circuit = cirq.Circuit(\n", " cirq.PhasedXPowGate(phase_exponent=0.3)(q1),\n", " cirq.PhasedXPowGate(phase_exponent=0.3)(q2),\n", " cirq_google.SYC(q1, q2),\n", " cirq_google.SYC(q2, q3),\n", ")\n", "my_device.validate_circuit(my_circuit)" ] }, { "cell_type": "markdown", "metadata": { "id": "-sNjRTCKDb2D" }, "source": [ "`op1` is allowed on qubit `q2`, but `op2` has the same invalid qubit target error as before. `validate_moment` finds this error by iterating the moment and stopping once the invalid operation is found. On the other hand, `my_circuit` satisfies all the device constraints and could be run on a Sycamore device, so `validate_circuit` does not throw an exception for it." ] }, { "cell_type": "markdown", "metadata": { "id": "BrxB0eCUDvlI" }, "source": [ "## Metadata features\n", "\n", "Some devices will also expose additional information via the `metadata` property. Metadata is usually exposed via the an instance (or subclass instance) of the `cirq.DeviceMetadata` class. You can access the metadata information of the Sycamore device with the `metadata` attribute:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.079370Z", "iopub.status.busy": "2024-08-16T09:11:36.078954Z", "iopub.status.idle": "2024-08-16T09:11:36.082533Z", "shell.execute_reply": "2024-08-16T09:11:36.081888Z" }, "id": "clsvdcXzEOIV" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "metadata = my_device.metadata\n", "print(type(metadata))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.085721Z", "iopub.status.busy": "2024-08-16T09:11:36.085142Z", "iopub.status.idle": "2024-08-16T09:11:36.091932Z", "shell.execute_reply": "2024-08-16T09:11:36.091302Z" }, "id": "LvaYZjANb7_n" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(type(metadata), cirq.DeviceMetadata)" ] }, { "cell_type": "markdown", "metadata": { "id": "dd2c56096511" }, "source": [ "The Sycamore device is a 2d grid device that exposes a `cirq.GridDeviceMetadata` with a uniform set of gates across all the qubits as well as a planar nearest neighbor connectivity graph. You can explore the properties below, starting with `qubit_set` and `nx_graph`, which are common to all instances and subclasses of the `cirq.DeviceMetadata` class.\n", "\n", "First, the set of qubits available are available in the `qubit_set` attribute." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.095164Z", "iopub.status.busy": "2024-08-16T09:11:36.094614Z", "iopub.status.idle": "2024-08-16T09:11:36.098222Z", "shell.execute_reply": "2024-08-16T09:11:36.097578Z" }, "id": "90ad647d9f10" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "frozenset({cirq.GridQubit(7, 3), cirq.GridQubit(4, 8), cirq.GridQubit(5, 4), cirq.GridQubit(6, 1), cirq.GridQubit(5, 8), cirq.GridQubit(5, 6), cirq.GridQubit(6, 3), cirq.GridQubit(6, 5), cirq.GridQubit(2, 6), cirq.GridQubit(1, 4), cirq.GridQubit(7, 5), cirq.GridQubit(9, 4), cirq.GridQubit(3, 3), cirq.GridQubit(4, 1), cirq.GridQubit(2, 4), cirq.GridQubit(4, 3), cirq.GridQubit(8, 4), cirq.GridQubit(6, 7), cirq.GridQubit(5, 3), cirq.GridQubit(2, 8), cirq.GridQubit(5, 1), cirq.GridQubit(1, 6), cirq.GridQubit(3, 5), cirq.GridQubit(3, 7), cirq.GridQubit(4, 5), cirq.GridQubit(4, 7), cirq.GridQubit(5, 7), cirq.GridQubit(5, 5), cirq.GridQubit(6, 2), cirq.GridQubit(2, 3), cirq.GridQubit(6, 4), cirq.GridQubit(3, 9), cirq.GridQubit(0, 5), cirq.GridQubit(4, 9), cirq.GridQubit(7, 2), cirq.GridQubit(7, 4), cirq.GridQubit(1, 5), cirq.GridQubit(7, 6), cirq.GridQubit(1, 7), cirq.GridQubit(4, 2), cirq.GridQubit(2, 5), cirq.GridQubit(8, 3), cirq.GridQubit(6, 6), cirq.GridQubit(4, 4), cirq.GridQubit(2, 7), cirq.GridQubit(5, 2), cirq.GridQubit(5, 0), cirq.GridQubit(8, 5), cirq.GridQubit(3, 2), cirq.GridQubit(4, 6), cirq.GridQubit(3, 4), cirq.GridQubit(3, 6), cirq.GridQubit(3, 8), cirq.GridQubit(0, 6)})\n" ] } ], "source": [ "print(metadata.qubit_set)" ] }, { "cell_type": "markdown", "metadata": { "id": "cb11944347e9" }, "source": [ "The `nx_graph` attribute details which of the `54` different qubits are connected to one another. Connected qubit pairs can execute two-qubit gates between them." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.101453Z", "iopub.status.busy": "2024-08-16T09:11:36.100927Z", "iopub.status.idle": "2024-08-16T09:11:36.104576Z", "shell.execute_reply": "2024-08-16T09:11:36.103892Z" }, "id": "3cd3a9c81874" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Graph with 54 nodes and 88 edges\n" ] } ], "source": [ "print(metadata.nx_graph)" ] }, { "cell_type": "markdown", "metadata": { "id": "a3aaadba9455" }, "source": [ "`cirq.GridDeviceMetadata` has some attributes that are not automatically included in `cirq.DeviceMetadata`, including `gateset`, which indicates the types and families of Cirq gates that are accepted by all qubits across the device." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.107789Z", "iopub.status.busy": "2024-08-16T09:11:36.107277Z", "iopub.status.idle": "2024-08-16T09:11:36.110873Z", "shell.execute_reply": "2024-08-16T09:11:36.110223Z" }, "id": "JbwrPD_AETiX" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gateset: \n", "Type GateFamily: cirq_google.experimental.ops.coupler_pulse.CouplerPulse\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq_google.experimental.ops.coupler_pulse.CouplerPulse)`\n", "\n", "Type GateFamily: cirq.ops.measurement_gate.MeasurementGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.measurement_gate.MeasurementGate)`\n", "\n", "Instance GateFamily: I\n", "Accepts `cirq.Gate` instances `g` s.t. `g == I`\n", "\n", "Type GateFamily: cirq.ops.phased_x_z_gate.PhasedXZGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.phased_x_z_gate.PhasedXZGate)`\n", "\n", "Type GateFamily: cirq.ops.common_gates.XPowGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.XPowGate)`\n", "\n", "Type GateFamily: cirq.ops.common_gates.YPowGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.YPowGate)`\n", "\n", "Type GateFamily: cirq.ops.common_gates.HPowGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.HPowGate)`\n", "\n", "Type GateFamily: cirq.ops.phased_x_gate.PhasedXPowGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.phased_x_gate.PhasedXPowGate)`\n", "\n", "Type GateFamily: cirq.ops.clifford_gate.SingleQubitCliffordGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.clifford_gate.SingleQubitCliffordGate)`\n", "\n", "Type GateFamily: cirq.ops.common_gates.ZPowGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`\n", "Accepted tags: [cirq_google.PhysicalZTag()]\n", "\n", "FSimGateFamily: allow_symbol=False; atol=1e-06\n", "`cirq_google.FSimGateFamily` which accepts any instance of gate types in\n", "gate_types_to_check: [cirq.ops.fsim_gate.FSimGate,cirq.ops.fsim_gate.PhasedFSimGate,cirq.ops.swap_gates.ISwapPowGate,cirq.ops.phased_iswap_gate.PhasedISwapPowGate,cirq.ops.common_gates.CZPowGate,cirq.ops.identity.IdentityGate]\n", "which matches (across types), via instance check / value equality, a gate in\n", "gates_to_accept: [ISWAP**0.5]\n", "\n", "Instance GateFamily: ISWAP**0.5\n", "Accepts `cirq.Gate` instances `g` s.t. `g == ISWAP**0.5`\n", "\n", "FSimGateFamily: allow_symbol=False; atol=1e-06\n", "`cirq_google.FSimGateFamily` which accepts any instance of gate types in\n", "gate_types_to_check: [cirq.ops.fsim_gate.FSimGate,cirq.ops.fsim_gate.PhasedFSimGate,cirq.ops.swap_gates.ISwapPowGate,cirq.ops.phased_iswap_gate.PhasedISwapPowGate,cirq.ops.common_gates.CZPowGate,cirq.ops.identity.IdentityGate]\n", "which matches (across types), via instance check / value equality, a gate in\n", "gates_to_accept: [ISWAP**-0.5]\n", "\n", "Instance GateFamily: ISWAP**-0.5\n", "Accepts `cirq.Gate` instances `g` s.t. `g == ISWAP**-0.5`\n", "\n", "FSimGateFamily: allow_symbol=False; atol=1e-06\n", "`cirq_google.FSimGateFamily` which accepts any instance of gate types in\n", "gate_types_to_check: [cirq.ops.fsim_gate.FSimGate,cirq.ops.fsim_gate.PhasedFSimGate,cirq.ops.swap_gates.ISwapPowGate,cirq.ops.phased_iswap_gate.PhasedISwapPowGate,cirq.ops.common_gates.CZPowGate,cirq.ops.identity.IdentityGate]\n", "which matches (across types), via instance check / value equality, a gate in\n", "gates_to_accept: [SYC]\n", "\n", "Instance GateFamily: SYC\n", "Accepts `cirq.Gate` instances `g` s.t. `g == SYC`\n", "\n", "Type GateFamily: cirq.ops.common_gates.ZPowGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.common_gates.ZPowGate)`\n", "Ignored tags: [cirq_google.PhysicalZTag()]\n", "\n", "Type GateFamily: cirq.ops.wait_gate.WaitGate\n", "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.wait_gate.WaitGate)`\n" ] } ], "source": [ "print(metadata.gateset)" ] }, { "cell_type": "markdown", "metadata": { "id": "fo6ZTYN4HBVL" }, "source": [ "These metadata features can be useful when designing/building algorithms around certain device information in order to tailor them for that device." ] }, { "cell_type": "markdown", "metadata": { "id": "RvC4xz_OHT3s" }, "source": [ "## The `cirq.Device` interface\n", "\n", "For advanced users (such as vendors) it is also possible to implement your own Device with its own unique constraints and metadata information. Below is an example of a fictitious custom device:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.114286Z", "iopub.status.busy": "2024-08-16T09:11:36.113929Z", "iopub.status.idle": "2024-08-16T09:11:36.122255Z", "shell.execute_reply": "2024-08-16T09:11:36.121586Z" }, "id": "cA7AdVWJCpRr" }, "outputs": [], "source": [ "class MyDevice(cirq.Device):\n", " \"\"\"Five qubits on a line, supporting X/Y/Z and CZ between neighbors.\"\"\"\n", "\n", " def __init__(self):\n", " # Specify the qubits available to the device\n", " self._qubits = set(cirq.LineQubit.range(5))\n", " # Specify which gates are valid\n", " self._supported_gates = cirq.Gateset(\n", " cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate, cirq.CZPowGate\n", " )\n", "\n", " def validate_operation(self, operation):\n", " \"\"\"Check to make sure `operation` is valid.\n", "\n", " `operation` must be on qubits found on the device\n", " and if it is a two qubit gate the qubits must be adjacent\n", "\n", " Raises:\n", " ValueError: if operation acts on qubits not found on the device.\n", " ValueError: if two qubit gates have non-local interactions.\n", " ValueError: if the operation is not in the supported gates.\n", " \"\"\"\n", " # Ensure that the operation's qubits are available on the device\n", " if any(x not in self._qubits for x in operation.qubits):\n", " raise ValueError(\"Using qubits not found on device.\")\n", "\n", " # Ensure that the operation's qubits are adjacent if there are two of them\n", " if len(operation.qubits) == 2:\n", " p, q = operation.qubits\n", " if not p.is_adjacent(q):\n", " raise ValueError('Non-local interaction: {}'.format(repr(operation)))\n", "\n", " # Ensure that the operation itself is a supported one\n", " if operation not in self._supported_gates:\n", " raise ValueError(\"Unsupported operation type.\")\n", "\n", " def validate_circuit(self, circuit):\n", " \"\"\"Check to make sure `circuit` is valid.\n", "\n", " Calls validate_operation on all operations as well as imposing\n", " a global limit on the total number of CZ gates.\n", "\n", " Raises:\n", " ValueError: if `validate_operation` raises for any operation in the\n", " circuit.\n", " ValueError: if there are more than 10 CZ gates in the entire circuit.\n", " \"\"\"\n", " # Call Device's `validate_operation`, which calls the `validate_operation`\n", " # function specified above on each operation in the circuit\n", " super().validate_circuit(circuit)\n", " # Ensure that no more than 10 two-qubit CZ gates exist in the circuit\n", " cz_count = sum(1 for mom in circuit for op in mom if len(op.qubits) == 2)\n", " if cz_count > 10:\n", " raise ValueError(\"Too many total CZs\")\n", "\n", " @property\n", " def metadata(self):\n", " \"\"\"MyDevice GridDeviceMetadata.\"\"\"\n", " # Since `MyDevice` is planar it is a good idea to subclass the\n", " # GridDeviceMetadata class to communicate additional device information to\n", " # the user.\n", " return cirq.GridDeviceMetadata(\n", " qubit_pairs=[(p, q) for p in self._qubits for q in self._qubits if p.is_adjacent(q)],\n", " gateset=self._supported_gates,\n", " )" ] }, { "cell_type": "markdown", "metadata": { "id": "9oRUQuGGI7MJ" }, "source": [ "At absolute minimum, when creating a custom `Device`, you should inherit from `cirq.Device` and overwrite the `__init__` and `validate_operation` methods. \n", "\n", "This custom device can now be used to validate circuits:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.125505Z", "iopub.status.busy": "2024-08-16T09:11:36.124959Z", "iopub.status.idle": "2024-08-16T09:11:36.131345Z", "shell.execute_reply": "2024-08-16T09:11:36.130708Z" }, "id": "uFKsR1qLe24M" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Too many total CZs\n" ] } ], "source": [ "my_custom_device = MyDevice()\n", "\n", "my_circuit = cirq.Circuit(\n", " cirq.X(cirq.LineQubit(0)),\n", " cirq.X(cirq.LineQubit(2)),\n", " cirq.X(cirq.LineQubit(4)),\n", " cirq.CZ(*cirq.LineQubit.range(2)),\n", ")\n", "too_many_czs = cirq.Circuit(cirq.CZ(*cirq.LineQubit.range(2)) for _ in range(11))\n", "\n", "# my_circuit is valid for my_custom_device.\n", "my_custom_device.validate_circuit(my_circuit)\n", "\n", "# each operation of too_many_czs is valid individually...\n", "for moment in too_many_czs:\n", " for op in moment:\n", " my_custom_device.validate_operation(op)\n", "\n", "# But the device has global constraints which the circuit does not meet:\n", "try:\n", " my_custom_device.validate_circuit(too_many_czs)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": { "id": "eG7JY2IVZGEQ" }, "source": [ "By default, the `validate_circuit` method of the `cirq.Device` class simply calls `validate_moment` on all the moments, which calls `validate_operation` on all the operations. It is advisable to maintain this behavior in your custom device, which can be implemented as above, by calling `super().validate_***` when writing each method.\n", "\n", "Depending on the scoping of constraints the custom device has, certain less local constraints might be better placed in `validate_moment` and certain global constraints might belong in `validate_circuit`. In addition to this, you can also add metadata options to your device. You can define a metadata subclass of `cirq.DeviceMetadata` or you can use an inbuilt metadata class like `cirq.GridDeviceMetadata`:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-08-16T09:11:36.134791Z", "iopub.status.busy": "2024-08-16T09:11:36.134236Z", "iopub.status.idle": "2024-08-16T09:11:36.245898Z", "shell.execute_reply": "2024-08-16T09:11:36.245204Z" }, "id": "bbTat2DMf1VE" }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAApQAAAHzCAYAAACe1o1DAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA2GElEQVR4nO3deXxU9b3G8Wcmgdwm4gIYWpUUK8vtRVSEGMwlYJRFEEIWluwsAQKERQQEI6AFLla0VnvdLgWtBVpQK0gREZBIA4SsJASkQqiYuBFIQCAJCZM5949qS6siYWZyZvm8/53wOw+8IDz5/s75HYthGIYAAACAK2Q1OwAAAAA8G4USAAAADqFQAgAAwCEUSgAAADiEQgkAAACHUCgBAADgEAolAAAAHEKhBAAAgEMolAAAAHAIhRIAAAAOoVACAADAIRRKAAAAOIRCCQAAAIdQKAEAAOAQCiUAAAAcQqEEAACAQyiUAAAAcAiFEgAAAA6hUAIAAMAhFEoAAAA4hEIJAAAAh1AoAQAA4BAKJQAAABxCoQQAAIBDKJQAAABwCIUSAAAADqFQAgAAwCEUSgAAADiEQgkAAACHUCgBAADgEAolAAAAHEKhBAAAgEMolAAAAHAIhRIAAAAOoVACAADAIRRKAAAAOIRCCQAAAIf4mx0AAADA09TU23SsqkYNNrta+lvVoU2QggJ8t1b57u8cAACgCY4cP6s1ueXK+qhS5dW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AolAAAAHEKhBAAAgEMolAAAAHAIhRIAAAAOoVACAADAIRRKAAAAOIRCCQAAAIdQKAEAAOAQCiUAAAAcQqEEAACAQyiUAAAAcAiFEgAAAA6hUAIAAMAhFEoAAAA4hEIJAAAAh1AoAQAA4BAKJQAAABxCoQQAAIBDKJQAAABwCIUSAAAADqFQAgAAwCEUSgAAADiEQgkAAACHUCgBAADgEAolAAAAHEKhBAAAgEMolAAAAHAIhRIAAAAOoVACAADAIRRKAAAAOOT/Aedx8Oe5UGliAAAAAElFTkSuQmCC", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "my_metadata = my_custom_device.metadata\n", "\n", "# Display device graph:\n", "nx.draw(my_metadata.nx_graph)" ] }, { "cell_type": "markdown", "metadata": { "id": "ddabb2b0bcc3" }, "source": [ "## Use in a virtual Engine\n", "\n", "`Device`s can also be used to specify `cirq.SimulatedLocalEngine` instances, which let you validate and simulate circuits using the same interface that the quantum hardware does. Read more in the [Virtual Engine Interface](../simulate/virtual_engine_interface.ipynb) page. \n", "\n", "Additionally, these virtual `Engine`s can be combined with noisy simulation that attempts to mimic existing hardware devices with the [Quantum Virtual Machine](../simulate/quantum_virtual_machine.ipynb). " ] }, { "cell_type": "markdown", "metadata": { "id": "OZxolGY3I82H" }, "source": [ "# Summary\n", "\n", "Devices in Cirq are used to specify constraints on circuits that are imposed by quantum hardware devices. You can check that an operation, moment, or circuit is valid on a particular `cirq.Device` by using `validate_operation`, `validate_moment`, or `validate_circuit` respectively. You can also create your own custom device objects to specify constraints for a new or changed device. Device objects, custom and otherwise, also can carry around metadata that may be useful for the validation process or other processes." ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "devices.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 }