├── .gitignore ├── environment.yml ├── README.md ├── [4] Coding challenge.ipynb ├── [1] How to write a quantum program in 10 lines of code.ipynb ├── [2] Entangling two quantum dice.ipynb └── [3] Rolling the quantum dice multiple times.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | channels: 2 | - conda-forge 3 | - defaults 4 | dependencies: 5 | - matplotlib 6 | - jupyter 7 | - pip: 8 | - pyquil 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pyquil-quantum-dice 2 | 3 | _Getting started with PyQuil: Quantum Dice example_ 4 | 5 | These examples are based on Dave Yen's blog post: ["How to write a quantum program in 10 lines of code (for beginners)"](https://medium.com/rigetti/how-to-write-a-quantum-program-in-10-lines-of-code-for-beginners-540224ac6b45). 6 | 7 | 8 | ## Running these notebooks in the cloud 9 | 10 | To run these example notebooks using http://mybinder.org, go to http://go.rigetti.com/quantum-dice. 11 | 12 | 13 | ## Installing locally 14 | 15 | You can also install run the notebooks on your laptop. To create a virtual environment, run: 16 | ``` 17 | conda env create -n pyquil-demo -f environment.yml 18 | ``` 19 | 20 | This will install all dependencies, such as `pyquil`. 21 | 22 | To activate the environment, run: 23 | ``` 24 | conda ativate pyquil-demo 25 | ``` 26 | 27 | To start the jupyter notebook server, run: 28 | ``` 29 | jupyter notebook 30 | ``` 31 | 32 | This starts the notebook server, so you can run the example code in the notebooks. Have fun! -------------------------------------------------------------------------------- /[4] Coding challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Coding challenge:\n", 8 | "\n", 9 | "We introduced how qubits, superposition, and quantum gates work in quantum programming, and built a quantum program in just 10 lines of code. We also demonstrated the exponential scaling power of quantum computers (N qubits = 2^N bits).\n", 10 | "\n", 11 | "_As a challenge for the next step in your quantum programming journey, how would you build a quantum dice with a generalized N number of sides?_\n", 12 | "\n", 13 | "_(Optional:) How would you build an N-sided quantum dice that is entangled to another N-sided quantum dice?_" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": null, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [] 22 | } 23 | ], 24 | "metadata": { 25 | "kernelspec": { 26 | "display_name": "Python 3", 27 | "language": "python", 28 | "name": "python3" 29 | }, 30 | "language_info": { 31 | "codemirror_mode": { 32 | "name": "ipython", 33 | "version": 3 34 | }, 35 | "file_extension": ".py", 36 | "mimetype": "text/x-python", 37 | "name": "python", 38 | "nbconvert_exporter": "python", 39 | "pygments_lexer": "ipython3", 40 | "version": "3.6.5" 41 | } 42 | }, 43 | "nbformat": 4, 44 | "nbformat_minor": 2 45 | } 46 | -------------------------------------------------------------------------------- /[1] How to write a quantum program in 10 lines of code.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# How to write a quantum program in 10 lines of code (for beginners)\n", 8 | "_Build an 8-sided dice that runs on a quantum computer_\n", 9 | "\n", 10 | "This example is based on Dave Yen's blog post: [\"How to write a quantum program in 10 lines of code (for beginners)\"](https://medium.com/rigetti/how-to-write-a-quantum-program-in-10-lines-of-code-for-beginners-540224ac6b45)." 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## Get API key" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "Go to http://forest.rigetti.com/ and request a Forest API key.\n", 25 | "You will receive an e-mail containing your API key and user ID.\n", 26 | "\n", 27 | "Copy the key and enter them in the field below. After the key and user ID have been filled out, evaluate the cell with SHIFT + ENTER. This will create the config file which will be used to connect to the Rigetti Forest API." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "%%writefile .pyquil_config\n", 37 | "[Rigetti Forest]\n", 38 | "key: \n", 39 | "user_id: " 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "Now first import the dependencies. Evaluate these cells with SHIFT+ENTER." 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "from pyquil.quil import Program\n", 56 | "from pyquil.api import QVMConnection\n", 57 | "from pyquil.gates import H\n", 58 | "\n", 59 | "from functools import reduce" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "Create a connection to the Quantum Virtual Machine (QVM)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "qvm = QVMConnection()" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "Apply the Hadamard gate to three qubits to generate 8 possible randomized results" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "dice = Program(H(0), H(1), H(2))" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "There are 8 possible results: `[[0,0,0], [0,0,1], [0,1,1], [1,1,1], [1,1,0], [1,0,0], [0,1,0]] [0,0,1]]`" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "Measure the qubits to get a result, i.e. roll the dice" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "roll_dice = dice.measure_all()\n", 115 | "result = qvm.run(roll_dice)\n", 116 | "result" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "Example result: `[[0,1,0]]`. The QVM returns a list of lists, where the inner list contains the result for one dice roll." 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "Format and print the result as a dice value between 1 and 8" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "dice_value = reduce(lambda x, y: 2*x + y, result[0], 0) + 1\n", 140 | "\n", 141 | "print(\"Your quantum dice roll returned:\", dice_value)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.5" 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 2 173 | } 174 | -------------------------------------------------------------------------------- /[2] Entangling two quantum dice.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Entangling two quantum dice using the CNOT gate\n", 8 | "\n", 9 | "_Let's play around more with the quantum dice. What if we could entangle two dice together using quantum entanglement?_" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "Import the dependencies." 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "%pylab inline\n", 26 | "import pylab as pl\n", 27 | "from functools import reduce\n", 28 | "\n", 29 | "from pyquil.api import QVMConnection\n", 30 | "from pyquil.quil import Program\n", 31 | "from pyquil.gates import H" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "Create the QVM connection." 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "qvm = QVMConnection()" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## Quantum entanglement" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "Let's create two dice that are entangled! To do this, we first need to import the `CNOT` gate." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": null, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "from pyquil.gates import CNOT" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "To entangle two qubits, we can run the following program:" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "entanglement = Program(H(0), CNOT(0, 1))\n", 87 | "print(entanglement)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "This program rotates one qubit into the superposition state, and then uses the _conditional not_ or `CNOT` gate to conditionally \"flip\" a second qubit, depending on the measurement outcome of the first. This means that if the first qubit returns |0>, the second qubit will stay in |0>, while if the first qubit returns |1>, the second qubit is flipped into |1>." 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "Let's run the program! You will see that because of quantum superposition, the two qubits will always give the same result." 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "qvm.run(entanglement.measure_all(), trials=5)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "## Entangling quantum dice" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "We know how to make one 8-sided quantum dice. Now, to create a second dice and entangle it to the first (qubits 0, 1 and 2), all we need to do is use three more qubits (3, 4 and 5) to encode the second dice, and apply `CNOT` gates to these qubits. This means we need to entangle qubit 0 with qubit 3, qubit 1 with qubit 4 and qubit 2 with qubit 5." 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Create the first quantum dice state (qubits 0, 1 and 2)." 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "dice = Program(H(0), H(1), H(2))" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "Entangle the dice to a second quantum dice (qubits 3, 4 and 5)." 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "entangled_dice = dice + Program(CNOT(0, 3), CNOT(1, 4), CNOT(2, 5))" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "Roll the dice!" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "roll_dice = entangled_dice.measure_all()\n", 173 | "result = qvm.run(roll_dice)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "Get the values for the dice. Remember on what qubits we encoded the dice." 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "( q0, q1, q2, q3, q4, q5 ) = result[0] \n", 190 | "dice1 = [q0, q1, q2]\n", 191 | "dice2 = [q3, q4, q5]\n", 192 | "\n", 193 | "dice1_value = reduce(lambda x, y: 2*x + y, dice1, 0) + 1\n", 194 | "dice2_value = reduce(lambda x, y: 2*x + y, dice2, 0) + 1" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": null, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "dice1_value, dice2_value" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [] 212 | } 213 | ], 214 | "metadata": { 215 | "kernelspec": { 216 | "display_name": "Python 3", 217 | "language": "python", 218 | "name": "python3" 219 | }, 220 | "language_info": { 221 | "codemirror_mode": { 222 | "name": "ipython", 223 | "version": 3 224 | }, 225 | "file_extension": ".py", 226 | "mimetype": "text/x-python", 227 | "name": "python", 228 | "nbconvert_exporter": "python", 229 | "pygments_lexer": "ipython3", 230 | "version": "3.6.5" 231 | } 232 | }, 233 | "nbformat": 4, 234 | "nbformat_minor": 2 235 | } 236 | -------------------------------------------------------------------------------- /[3] Rolling the quantum dice multiple times.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Rolling the quantum dice multiple times" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "In the previous notebooks, we only rolled the dice _once_. Here, we will run the quantum program multiple times using the `trials` argument as to roll the dice multiple times!" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Import the dependencies." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "%pylab inline\n", 31 | "import pylab as pl\n", 32 | "from functools import reduce\n", 33 | "\n", 34 | "from pyquil.api import QPUConnection, QVMConnection, get_devices\n", 35 | "from pyquil.quil import Program\n", 36 | "from pyquil.gates import H, MEASURE" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "Create the QVM connection." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "qvm = QVMConnection()" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "## Roll an 8-sided quantum dice" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "To easily run this multiple times, let's create some functions: one to convert binary values to decimal values, and one to roll a dice _num_ times." 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "def bin_to_dec(values: list):\n", 76 | " \"\"\"Convert binary to decimal value\"\"\"\n", 77 | " return reduce(lambda x, y: 2*x + y, values, 0) + 1\n", 78 | "\n", 79 | "def roll_d8(num: int = 10):\n", 80 | " \"\"\"Roll an 8-sided dice times\"\"\"\n", 81 | " dice = Program(H(0), H(1), H(2))\n", 82 | " roll_dice = dice.measure_all()\n", 83 | " result = qvm.run(roll_dice, trials=num)\n", 84 | " dice_values = [bin_to_dec(r) for r in result]\n", 85 | "\n", 86 | " return dice_values" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Roll the dice once:" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "roll_d8(1)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "Example result: `[1]`" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "Roll the dice 10 times:" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "roll_d8(10)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "Example result: `[1, 5, 1, 3, 7, 8, 4, 4, 3, 3]`" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "Now, roll the dice 100 times and store the result in a variable." 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "rolls = roll_d8(100)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "We can then plot the result in a histogram to see the probability distribution. Try playing around with the number of rolls (N) and see how the probability distribution changes." 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "def plot_hist(rolls, title=\"8-sided dice rolls\", num_sides: int = 8):\n", 165 | " \"\"\"Plot a histogram for 8-sided dice rolls\"\"\"\n", 166 | " pl.hist(rolls, bins=[i + .5 for i in range(num_sides+1)], rwidth=0.8)\n", 167 | " pl.ylabel(\"Number of rolls\")\n", 168 | " pl.xlabel(\"Dice roll result\")\n", 169 | " pl.title(title)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "plot_hist(rolls)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "## Entangling two 8-sided quantum dice" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Let's create two dice that are entangled! To do this, we first need to import the `CNOT` gate." 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "from pyquil.gates import CNOT" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "To entangle two qubits, we can run the following program:" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "entanglement = Program(H(0), CNOT(0, 1))\n", 218 | "print(entanglement)" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "This program rotates one qubit into the superposition state, and then uses the _conditional not_ or `CNOT` gate to conditionally \"flip\" a second qubit, depending on the measurement outcome of the first. This means that if the first qubit returns |0>, the second qubit will stay in |0>, while if the first qubit returns |1>, the second qubit is flipped into |1>." 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "Let's run the program! You will see that because of quantum superposition, the two qubits will always give the same result." 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "qvm.run(entanglement.measure_all(), trials=5)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "Let's create a function that rolls two dice, and optionally entangles them." 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "def roll_two_d8s(num: int = 10, entangled: bool = True):\n", 258 | " \"\"\"Roll two entangled 8-sided dice times\"\"\"\n", 259 | " # Create the first quantum dice state (qubits 0, 1 and 2).\n", 260 | " dice = Program(H(0), H(1), H(2))\n", 261 | " \n", 262 | " if entangled:\n", 263 | " # Entangle the dice to a second quantum dice (qubits 3, 4 and 5).\n", 264 | " two_dice = dice + Program(CNOT(0, 3), CNOT(1, 4), CNOT(2, 5))\n", 265 | " else:\n", 266 | " two_dice = dice + Program(H(3), H(4), H(5))\n", 267 | "\n", 268 | " # Roll the dice!\n", 269 | " roll_dice = two_dice.measure_all()\n", 270 | " result = qvm.run(roll_dice, trials=num)\n", 271 | " \n", 272 | " # Get the values for the dice by converting binary to decimal values.\n", 273 | " dice1_values, dice2_values = [], []\n", 274 | " for ( q0, q1, q2, q3, q4, q5 ) in result:\n", 275 | " # Dice 1 is encoded in qubits 0, 1 and 2:\n", 276 | " dice1_values += [bin_to_dec([q0, q1, q2])]\n", 277 | "\n", 278 | " # Dice 2 is encoded in qubits 3, 4 and 5:\n", 279 | " dice2_values += [bin_to_dec([q3, q4, q5])]\n", 280 | " \n", 281 | " # Return the result.\n", 282 | " return dice1_values, dice2_values" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "Plot the values to compare probability distributions, before and after entangling!" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": null, 295 | "metadata": {}, 296 | "outputs": [], 297 | "source": [ 298 | "rolls1, rolls2 = roll_two_d8s(100, entangled=False)\n", 299 | "print(\"Number of times the two dice gave the same result:\", sum(np.array(rolls1) == np.array(rolls2)))\n", 300 | "\n", 301 | "pl.figure(figsize=(14, 4))\n", 302 | "pl.subplot(121)\n", 303 | "plot_hist(rolls1, \"Dice A\")\n", 304 | "pl.subplot(122)\n", 305 | "plot_hist(rolls2, \"Dice B\")" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": null, 311 | "metadata": {}, 312 | "outputs": [], 313 | "source": [ 314 | "rolls1, rolls2 = roll_two_d8s(100, entangled=True)\n", 315 | "print(\"Number of times the two dice gave the same result:\", sum(np.array(rolls1) == np.array(rolls2)))\n", 316 | "\n", 317 | "pl.figure(figsize=(14, 4))\n", 318 | "pl.subplot(121)\n", 319 | "plot_hist(rolls1, \"Dice A\")\n", 320 | "pl.subplot(122)\n", 321 | "plot_hist(rolls2, \"Dice B\")" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [] 330 | } 331 | ], 332 | "metadata": { 333 | "kernelspec": { 334 | "display_name": "Python 3", 335 | "language": "python", 336 | "name": "python3" 337 | }, 338 | "language_info": { 339 | "codemirror_mode": { 340 | "name": "ipython", 341 | "version": 3 342 | }, 343 | "file_extension": ".py", 344 | "mimetype": "text/x-python", 345 | "name": "python", 346 | "nbconvert_exporter": "python", 347 | "pygments_lexer": "ipython3", 348 | "version": "3.6.5" 349 | } 350 | }, 351 | "nbformat": 4, 352 | "nbformat_minor": 2 353 | } 354 | --------------------------------------------------------------------------------