Skip to content

Commit 235a0c6

Browse files
Add documentation for cirq google's support for google internal gates (#6301)
1 parent 455f50b commit 235a0c6

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

Diff for: docs/google/internal_gates.ipynb

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "SzKwuqYESWwm"
7+
},
8+
"source": [
9+
"##### Copyright 2023 The Cirq Developers"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"cellView": "form",
17+
"id": "4yPUsdJxSXFq"
18+
},
19+
"outputs": [],
20+
"source": [
21+
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
22+
"# you may not use this file except in compliance with the License.\n",
23+
"# You may obtain a copy of the License at\n",
24+
"#\n",
25+
"# https://www.apache.org/licenses/LICENSE-2.0\n",
26+
"#\n",
27+
"# Unless required by applicable law or agreed to in writing, software\n",
28+
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
29+
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
30+
"# See the License for the specific language governing permissions and\n",
31+
"# limitations under the License."
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {
37+
"id": "dVkNQc0WSIwk"
38+
},
39+
"source": [
40+
"# Google Internal Gates in Cirq"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {
46+
"id": "zC1qlUJoSXhm"
47+
},
48+
"source": [
49+
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
50+
" <td>\n",
51+
" <a target=\"_blank\" href=\"https://quantumai.google/cirq/google/internal_gates\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
52+
" </td>\n",
53+
" <td>\n",
54+
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/master/docs/google/internal_gates.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
55+
" </td>\n",
56+
" <td>\n",
57+
" <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/master/docs/google/internal_gates.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
58+
" </td>\n",
59+
" <td>\n",
60+
" <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/google/internal_gates.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
61+
" </td>\n",
62+
"</table>"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 5,
68+
"metadata": {
69+
"id": "bd9529db1c0b"
70+
},
71+
"outputs": [],
72+
"source": [
73+
"try:\n",
74+
" import cirq\n",
75+
" import cirq_google\n",
76+
"except ImportError:\n",
77+
" print(\"installing cirq...\")\n",
78+
" !pip install --quiet cirq\n",
79+
" print(\"installed cirq.\")\n",
80+
" import cirq\n",
81+
" import cirq_google"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"Google has a wealth of gates implemented internally that don't exist or have equivalents in Cirq. `cirq_google.InternalGate` allows the creation of Cirq circuits that contain place holder operations that will get translated to the correct internal gate."
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"## InternalGate\n",
96+
"Instances of [cirq_google.InternalGate](https://github.com/quantumlib/Cirq/blob/61d967112ba23cc839b0e922bd42878024a3e738/cirq-google/cirq_google/ops/internal_gate.py#L20) act as placeholder objects for google internal gates. During translation, the correct gate is identified through the `gate_module` and `gate_name` properties. Then an instance of that gate is created using the `kwargs` arguments passed to the `InternalGate` constructor."
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 6,
102+
"metadata": {},
103+
"outputs": [
104+
{
105+
"data": {
106+
"text/plain": [
107+
"cirq_google.InternalGate(gate_name=\"GATE_NAME\", gate_module=\"GATE_MODULE\", num_qubits=2, )"
108+
]
109+
},
110+
"execution_count": 6,
111+
"metadata": {},
112+
"output_type": "execute_result"
113+
}
114+
],
115+
"source": [
116+
"internal_gate_args = {\n",
117+
" # Arguments to be passed to the constructor of the internal gate.\n",
118+
"}\n",
119+
"internal_gate = cirq_google.InternalGate(\n",
120+
" gate_module='GATE_MODULE', # Module of class. \n",
121+
" gate_name='GATE_NAMPE', # Class name.\n",
122+
" num_qubits=2, # Number of qubits that the gate acts on.\n",
123+
" **internal_gate_args)\n",
124+
"internal_gate"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 7,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/html": [
135+
"<pre style=\"overflow: auto; white-space: pre;\">0: ───GATE_MODULE.GATE_NAMPE()───\n",
136+
"\n",
137+
"1: ───#2─────────────────────────</pre>"
138+
],
139+
"text/plain": [
140+
"0: ───GATE_MODULE.GATE_NAMPE()───\n",
141+
"\n",
142+
"1: ───#2─────────────────────────"
143+
]
144+
},
145+
"execution_count": 7,
146+
"metadata": {},
147+
"output_type": "execute_result"
148+
}
149+
],
150+
"source": [
151+
"cirq.Circuit(internal_gate(*cirq.LineQubit.range(2)))"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"## Notes\n",
159+
"1. InternalGate is serializable.\n",
160+
"1. Values of `kwargs` must be serializable as [api.v2.ArgValue](https://github.com/quantumlib/Cirq/blob/61d967112ba23cc839b0e922bd42878024a3e738/cirq-google/cirq_google/api/v2/program.proto#L281)\n",
161+
"1. If a value is not serializable as `api.v2.ArgValue` (e.g. a value with unit) then the translator will need to updated to know what to do for that gate."
162+
]
163+
}
164+
],
165+
"metadata": {
166+
"colab": {
167+
"name": "_template.ipynb",
168+
"toc_visible": true
169+
},
170+
"kernelspec": {
171+
"display_name": "Python 3",
172+
"name": "python3"
173+
},
174+
"language_info": {
175+
"codemirror_mode": {
176+
"name": "ipython",
177+
"version": 3
178+
},
179+
"file_extension": ".py",
180+
"mimetype": "text/x-python",
181+
"name": "python",
182+
"nbconvert_exporter": "python",
183+
"pygments_lexer": "ipython3",
184+
"version": "3.10.13"
185+
}
186+
},
187+
"nbformat": 4,
188+
"nbformat_minor": 0
189+
}

0 commit comments

Comments
 (0)