Skip to content

Commit cce1050

Browse files
cantwellclamberta
andauthored
Quantum chess docs (quantumlib#94)
* Added Quantum Chess Rest API tutorial. * Updated toc.yaml to include Quantum Chess REST API tutorial * Updated index.md with links and information. * Changed file names to lower case * Changed some language in index.md to conform with style guide. * Moved images to quantum_chess/images directory * moved images for concepts tutorial to quantum_chess/images directory * Added registered trademark on Quantum Chess to index.md * Changed file names to lowercase * removed ipynb's with capital names. * added notebooks with lowercase names * Update docs/_toc.yaml * Update docs/quantum_chess/quantum_chess_client.ipynb Co-authored-by: Billy Lamberta <[email protected]> * Update docs/quantum_chess/quantum_chess_rest_api.ipynb Co-authored-by: Billy Lamberta <[email protected]> * Update docs/quantum_chess/quantum_chess_rest_api.ipynb Co-authored-by: Billy Lamberta <[email protected]> * Update docs/quantum_chess/quantum_chess_client.ipynb Co-authored-by: Billy Lamberta <[email protected]> * Update docs/quantum_chess/quantum_chess_client.ipynb Co-authored-by: Billy Lamberta <[email protected]> * Update docs/_toc.yaml Co-authored-by: Billy Lamberta <[email protected]> * Update docs/quantum_chess/quantum_chess_client.ipynb Co-authored-by: Billy Lamberta <[email protected]> * Updating headings and language for clarity. * Ran nbfmt Co-authored-by: Billy Lamberta <[email protected]>
1 parent 14e5666 commit cce1050

13 files changed

+853
-10
lines changed

docs/_toc.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ toc:
3535
path: /cirq/experiments/quantum_chess
3636
- title: "Concepts"
3737
path: /cirq/experiments/quantum_chess/concepts
38+
title: "Quantum Chess REST API"
39+
path: /cirq/experiments/quantum_chess/quantum_chess_rest_api
40+
title: "Quantum Chess client"
41+
path: /cirq/experiments/quantum_chess/quantum_chess_client
3842

3943
- heading: "Hartree-Fock VQE"
4044
- title: "Overview"

docs/quantum_chess/concepts.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
"\n",
210210
"For quantum chess, we would like to do an analogous operation that preserves any quantum information. In order to do this, we will swap the two squares. We will do this by performing an **iSWAP** gate between the two qubits that represent those squares. Note that this operation will also add a phase of *i* onto the qubits in addition to swapping them, adding a uniquely quantum aspect to the move.\n",
211211
"\n",
212-
"![Standard Move](../images/qc_standard_move.png)\n",
212+
"![Standard Move](./images/qc_standard_move.png)\n",
213213
"\n",
214214
"For example, if we attempt to move a King piece from the 'a1' square to the 'b1' square, we will perform a iSWAP gate between the qubits that represent the 'a1' and 'b1' squares.\n",
215215
"\n",
@@ -219,7 +219,7 @@
219219
"\n",
220220
"This will be accomplished by doing a $\\sqrt{\\text{iSWAP}}$ between the source and the first target. This will create an equal superposition between the source and the first target square. We would finish the move by performing a iSWAP between the source square and second target square. This will create an equal superposition between the two target squares.\n",
221221
"\n",
222-
"![Split Move](../images/qc_split_move1.png)\n",
222+
"![Split Move](./images/qc_split_move1.png)\n",
223223
"\n",
224224
"For example, suppose that that we split move a King on 'a1' to the squares 'b1' and 'b2'. To do this, we would do a $\\sqrt{\\text{iSWAP}}$ between the qubits representing 'a1' and 'b1' followed by an **iSWAP** on the 'a1' and 'b2' qubits. A short-hand notation for this move is *Ks1^t1t2* for source square s1 and split targets t1 and t2. This example would be noted as *Ka1^b1b2*.\n",
225225
"\n",
@@ -231,7 +231,7 @@
231231
"\n",
232232
"This tutorial will only consider paths that are blocked by one possible piece. In this case, we will perform a **controlled-iSWAP** between the source square and target square controlled by the square that is potentially blocking the path.\n",
233233
"\n",
234-
"![Slide Move](../images/qc_slide_move.png)\n",
234+
"![Slide Move](./images/qc_slide_move.png)\n",
235235
"\n",
236236
"For example, suppose we are moving a rook from 'a1' to 'c1', but there is a split knight on 'b1'. To perform this slide move, we would do an iSWAP between the qubits that represent 'a1' and 'c1' controlled by the 'b1' qubit. This operation can result in two potential states. Either the rook is on 'c1' and the knight is not on 'b1' (path was open) or, alternatively, the rook is on 'a1' and the knight is on 'b1' (path was blocked). The quantum board state cannot be described without mentioning the relationship between the 'b1' and 'a1'/'c1' squares, so we can say that those squares are *entangled*.\n",
237237
"\n",
@@ -241,7 +241,7 @@
241241
"\n",
242242
"This can happen in a variety of circumstances, but we will only consider the case where a piece attempts to make a legal, standard move into a square with a piece of its own color. \n",
243243
"\n",
244-
"![Exclusion Move](../images/qc_exclusion_move.png)\n",
244+
"![Exclusion Move](./images/qc_exclusion_move.png)\n",
245245
"\n",
246246
"For instance, suppose we attempt to move a king from 'a1' to 'a2' and there is a split knight on the 'a2' square. We would then need to measure the 'a2' square. If the measurement is 1, then the knight occupies the square and the king cannot move. If the measurement is 0, the knight is not on the square and the king moves successfully. In this case, we have a classical 'mixture' of states. Half the time, we would get the king on 'a1' and the knight on 'a2'. The other half of the time, the king would be on 'a2'. However, since we have measured and projected the state to one of these possibilities, there would be no superposition or entanglement, and this part of the board would be in a classical state (definitively in either one configuration or the other).\n",
247247
"\n",
@@ -284,7 +284,7 @@
284284
"|000 010 000> + |000 100 000>\n",
285285
"```\n",
286286
"\n",
287-
"![Quantum king moves](../images/qc_qking_moves.png)\n",
287+
"![Quantum king moves](./images/qc_qking_moves.png)\n",
288288
"\n",
289289
"\n",
290290
"Now, suppose that we make two additional moves. First, we will split the \"half-king\" on 'b1' to 'c1' and 'c2'. Next, we will split the 'half king' on 'b2' to 'b3' and 'c3'.\n",
@@ -495,7 +495,7 @@
495495
"\n",
496496
"Let's start with a king on 'a1' and a queen on 'a3'. We will split the king onto 'b1' and 'b2' as before. Next, we will split move the king on 'b1' to 'c1' and 'c2'. Lastly, we will try to move the queen from 'a3' to 'b2'.\n",
497497
"\n",
498-
"![Queen trying to move into a king superposition](../images/qc_blocked_king.png)\n",
498+
"![Queen trying to move into a king superposition](./images/qc_blocked_king.png)\n",
499499
"\n",
500500
"This will trigger a condition known as \"double occupancy\". We are not allowed to have two pieces of different types in the same square. If the king is in 'b2', the queen cannot move there. If the king is not in 'b2', then the move is legal. Thus, we will need to do a measurement. Let's see how that works.\n"
501501
]
4.23 KB
Loading
Loading

docs/quantum_chess/index.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@ fun and mentally engaging way. Games can also offer an interesting test
88
bed for near term quantum devices because they can be tailored to support
99
varying amounts of quantum behavior through simple rule changes,
1010
which can be useful when dealing with limited resources.
11-
[arxiv:https://arxiv.org/abs/1906.05836](https://arxiv.org/abs/1906.05836)
12-
and this module
13-
explores the design of a variant of Chess which is built on top of unitary
11+
12+
Quantum Chess<sup>&reg;</sup> is a variant of Chess, which is built on top of unitary
1413
dynamics and includes non-trivial quantum effects such as superposition,
15-
entanglement, and interference.
14+
entanglement, and interference. With the introduction of the split move, players
15+
can place their pieces in superposition on multiple squares at once. For a full
16+
description of the rules, and to learn more about the game, check out the
17+
[Quantum Chess website](https://www.quantumchess.net). To learn more about the math behind
18+
the game, see this [paper on arxiv](https://arxiv.org/abs/1906.05836)
19+
.
20+
21+
This module explores an implementation of Quantum Chess that runs using Cirq. In
22+
[Concepts](./concepts) you will explore various quantum computing concepts, such as error
23+
mitigation, post-selection, and qubit mapping, without requiring specific domain
24+
knowledge, like quantum chemistry. And in [Quantum Chess REST API] you will implement
25+
a fully functional server, that defines the specific REST endpoints required by the
26+
Quantum Chess Engine to offload its quantum state handling to an external resource.
27+
A similar server was used in [this presentation](https://youtu.be/ec-Mb8OJuRg) to run Quantum Chess on Google's
28+
Rainbow chip during the Google Quantum AI Summer symposium.
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "NvFMw17BoLbq"
7+
},
8+
"source": [
9+
"##### Copyright 2020 Google"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"cellView": "form",
17+
"id": "CvJ_QrYPoM8L"
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": "3EidIixoF1Ms"
38+
},
39+
"source": [
40+
"# Quantum Chess REST Client"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {
46+
"id": "OYeSZNZBoUK2"
47+
},
48+
"source": [
49+
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
50+
" <td>\n",
51+
" <a target=\"_blank\" href=\"https://www.example.org/cirq/experiments/quantum_chess/quantum_chess_client\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on QuantumLib</a>\n",
52+
" </td>\n",
53+
" <td>\n",
54+
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/ReCirq/blob/master/docs/quantum_chess/quantum_chess_client.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
55+
" </td>\n",
56+
" <td>\n",
57+
" <a target=\"_blank\" href=\"https://github.com/quantumlib/ReCirq/blob/master/docs/quantum_chess/quantum_chess_client.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
58+
" </td>\n",
59+
" <td>\n",
60+
" <a href=\"https://storage.googleapis.com/tensorflow_docs/ReCirq/docs/quantum_chess/quantum_chess_client.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
61+
" </td>\n",
62+
"</table>"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {
68+
"id": "NsP1vVXYuo_-"
69+
},
70+
"source": [
71+
"This is a basic client meant to test the server implemented at the end of the [Quantum Chess REST API](./quantum_chess_rest_api.ipynb) documentation. You must run that previous Colab for this one to work."
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {
77+
"id": "b3600ee25c8e"
78+
},
79+
"source": [
80+
"## Setup"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"metadata": {
87+
"id": "zn40ny5Puj1A"
88+
},
89+
"outputs": [],
90+
"source": [
91+
"!pip install git+https://github.com/quantumlib/ReCirq/ -q\n",
92+
"!pip install requests -q"
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {
98+
"id": "glA7YB4VxPDL"
99+
},
100+
"source": [
101+
"The server for the Quantum Chess Rest API endpoints should provide you with an ngrok url when you run it. **Paste the url provided by your server in the form below**. If your server is running, the following code should produce the message: \"Running Flask on Google Colab!\""
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {
108+
"id": "4JOEEu-ewQ-X"
109+
},
110+
"outputs": [],
111+
"source": [
112+
"url = 'http://bd626d83c9ec.ngrok.io/' #@param {type:\"string\"}\n",
113+
"!curl -s $url"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {
119+
"id": "pgvtoLl_YltL"
120+
},
121+
"source": [
122+
"You should be able to see the server output indicting a connection was made."
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {
128+
"id": "8GewRWMqGe0G"
129+
},
130+
"source": [
131+
"## Initialization"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {
137+
"id": "0Lm1rW1TXUzI"
138+
},
139+
"source": [
140+
"Make a simple request to initialize a board with the starting occupancy state of all pieces. Using the bitboard format, the initial positions of pieces are given by the hex 0xFFFF00000000FFFF. This initializes all squares in ranks 1, 2, 7, and 8 to be occupied."
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": null,
146+
"metadata": {
147+
"id": "YiDU-GISwi5Z"
148+
},
149+
"outputs": [],
150+
"source": [
151+
"import requests\n",
152+
"\n",
153+
"init_board_json = { 'init_basis_state' : 0xFFFF00000000FFFF }\n",
154+
"response = requests.post(url + '/quantumboard/init', json=init_board_json)\n",
155+
"\n",
156+
"print(response.content)"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {
162+
"id": "e855abc508f0"
163+
},
164+
"source": [
165+
"## Superposition"
166+
]
167+
},
168+
{
169+
"cell_type": "markdown",
170+
"metadata": {
171+
"id": "CTpB3ARVXiph"
172+
},
173+
"source": [
174+
"With the board initialized, you can execute a few moves to see what happens. You can create superposition by executing a split move from b1 to a3 and c3. Watch the server output to see the execution of this move."
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {
181+
"id": "0dXVki0VTH8u"
182+
},
183+
"outputs": [],
184+
"source": [
185+
"from recirq.quantum_chess.enums import MoveType, MoveVariant\n",
186+
"from recirq.quantum_chess.bit_utils import square_to_bit\n",
187+
"\n",
188+
"split_b1_a3_c3 = {'square1' : square_to_bit('b1'), 'square2' : square_to_bit('a3'), 'square3' : square_to_bit('c3'), \n",
189+
" 'type' : int(MoveType.SPLIT_JUMP.value), 'variant': int(MoveVariant.BASIC.value)}\n",
190+
"response = requests.post(url + '/quantumboard/do_move', json=split_b1_a3_c3)\n",
191+
"print(response.content)\n"
192+
]
193+
},
194+
{
195+
"cell_type": "markdown",
196+
"metadata": {
197+
"id": "003a7ae0e0fc"
198+
},
199+
"source": [
200+
"## Entanglement"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {
206+
"id": "mbJ7ogpZXnB_"
207+
},
208+
"source": [
209+
"You can see, in the probabilities returned, a roughly 50/50 split for two of the squares. A pawn two-step move, from c2 to c4, will entangle the pawn on c2 with the piece in superposition on a3 and c3."
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": null,
215+
"metadata": {
216+
"id": "2AmPlYGBygU2"
217+
},
218+
"outputs": [],
219+
"source": [
220+
"move_c2_c4 = {'square1' : square_to_bit('c2'), 'square2' : square_to_bit('c4'), 'square3' : 0,'type' : int(MoveType.PAWN_TWO_STEP.value), 'variant': int(MoveVariant.BASIC.value)}\n",
221+
"response = requests.post(url + '/quantumboard/do_move', json=move_c2_c4)\n",
222+
"print(response.content)"
223+
]
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {
228+
"id": "15903f3609ec"
229+
},
230+
"source": [
231+
"## Measurement"
232+
]
233+
},
234+
{
235+
"cell_type": "markdown",
236+
"metadata": {
237+
"id": "n0AxRGrDX62w"
238+
},
239+
"source": [
240+
"The probability distribution returned doesn't show the entanglement, but it still exists in the underlying state. You can see this by doing a move that forces a measurement. An excluded move from d1 to c2 will force a measurement of the c2 square. In the server output you should see the collapse of the state, with c2, c3, c4, and a3 taking definite 0 or 100% probabilities."
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": null,
246+
"metadata": {
247+
"id": "_CuMRZxnXIRe"
248+
},
249+
"outputs": [],
250+
"source": [
251+
"move_d1_c2 = {'square1' : square_to_bit('d1'), 'square2' : square_to_bit('c2'), 'square3' : 0, 'type' : int(MoveType.JUMP.value), 'variant': int(MoveVariant.EXCLUDED.value)}\n",
252+
"response = requests.post(url + '/quantumboard/do_move', json=move_d1_c2)\n",
253+
"print(response.content)"
254+
]
255+
},
256+
{
257+
"cell_type": "markdown",
258+
"metadata": {
259+
"id": "WBi1MAw35dEj"
260+
},
261+
"source": [
262+
"You can see the entanglement correlation by running the following cell a few times. There should be two different outcomes, the first with both c2 and c3 are 100%, and the second with c4 and a3 both 100%."
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"execution_count": null,
268+
"metadata": {
269+
"id": "Ioatj4Ty5VNt"
270+
},
271+
"outputs": [],
272+
"source": [
273+
"response = requests.post(url + '/quantumboard/undo_last_move')\n",
274+
"print(response.content)\n",
275+
"response = requests.post(url + '/quantumboard/do_move', json=move_d1_c2)\n",
276+
"print(response.content)"
277+
]
278+
}
279+
],
280+
"metadata": {
281+
"colab": {
282+
"collapsed_sections": [],
283+
"name": "quantum_chess_client.ipynb",
284+
"toc_visible": true
285+
},
286+
"kernelspec": {
287+
"display_name": "Python 3",
288+
"name": "python3"
289+
}
290+
},
291+
"nbformat": 4,
292+
"nbformat_minor": 0
293+
}

0 commit comments

Comments
 (0)