|
| 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