└── how_to_solve_qubo.ipynb /how_to_solve_qubo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "19cf4efc-ea31-4b90-b4f1-8217ce8cdd1d", 6 | "metadata": {}, 7 | "source": [ 8 | "\n", 9 | "\n", 10 | "# Events organization\n", 11 | "\n", 12 | "Events: $E_1$, $E_2$, $E_3$, $E_4$ and $E_5$\n", 13 | "\n", 14 | "People: $P_1$, $P_2$, $P_3$ and $P_4$.\n", 15 | "\n", 16 | "\n", 17 | "Preferences:\n", 18 | "- $P_1$ want to go to $E_1$ and $E_4$.\n", 19 | "- $P_2$ want to go to $E_2$ and $E_3$.\n", 20 | "- $P_3$ want to go to $E_4$ and $E_5$.\n", 21 | "- $P_4$ want to go to $E_3$ and $E_4$.\n", 22 | "\n", 23 | "Binary variables:\n", 24 | "\n", 25 | "- $x_i = 0$ if the i-th event is on Saturday\n", 26 | "- $x_i = 1$ if the i-th event is on Sunday\n", 27 | "\n", 28 | "Formulation:\n", 29 | "\n", 30 | "$f(x_1,x_2,x_3,x_4,x_5) = 4 + x_1 + x_2 + 2x_3 + 3x_4 + x_5 -2x_1x_4-2x_2x_3-2x_4x_5-2x_3x_4$\n", 31 | "\n", 32 | "Change of variable:\n", 33 | "\n", 34 | "$x_i = \\frac{1-z_i}{2}$\n", 35 | "\n", 36 | "- $z_i = 1$ if the i-th event is on Saturday\n", 37 | "- $z_i = -1$ if the i-th event is on Sunday\n", 38 | "\n", 39 | "New formulation:\n", 40 | "\n", 41 | "$f(z_1,z_2,z_3,z_4,z_5) = 6 - \\frac{1}{2}z_1z_4- \\frac{1}{2}z_2z_3- \\frac{1}{2}z_4z_5- \\frac{1}{2}z_3z_4$" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "id": "6f9bf672-c7ca-4ef2-aad2-8325293254e5", 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | " (-6.0) [I1]\n", 55 | "+ (0.5) [Z1 Z4]\n", 56 | "+ (0.5) [Z2 Z3]\n", 57 | "+ (0.5) [Z4 Z5]\n", 58 | "+ (0.5) [Z3 Z4]\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "import pennylane as qml\n", 64 | "from pennylane import numpy as np\n", 65 | "\n", 66 | "H = - 6 * qml.Identity(1) + \\\n", 67 | " 0.5 * qml.PauliZ(1) @ qml.PauliZ(4) + \\\n", 68 | " 0.5 * qml.PauliZ(2) @ qml.PauliZ(3) + \\\n", 69 | " 0.5 * qml.PauliZ(4) @ qml.PauliZ(5) + \\\n", 70 | " 0.5 * qml.PauliZ(3) @ qml.PauliZ(4) \n", 71 | " \n", 72 | "print(H)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 4, 78 | "id": "b89f50b5-5d5d-464c-bd05-c989a10e9761", 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "tensor(-4., requires_grad=True)" 85 | ] 86 | }, 87 | "execution_count": 4, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "dev = qml.device(\"default.qubit\", wires = H.wires)\n", 94 | "\n", 95 | "@qml.qnode(dev)\n", 96 | "def circuit(params):\n", 97 | " \n", 98 | " for param, wire in zip(params, H.wires):\n", 99 | " qml.RY(param, wires = wire)\n", 100 | " \n", 101 | " return qml.expval(H)\n", 102 | "\n", 103 | "circuit([0,0,0,0,0])" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 5, 109 | "id": "085873a7-09bb-43f4-9b1b-d7ecb7490802", 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "tensor(-8., requires_grad=True)" 116 | ] 117 | }, 118 | "execution_count": 5, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "params = np.random.rand(len(H.wires))\n", 125 | "opt = qml.AdagradOptimizer(stepsize = 0.5)\n", 126 | "epochs = 200\n", 127 | "\n", 128 | "for epoch in range(epochs):\n", 129 | " params = opt.step(circuit, params)\n", 130 | " \n", 131 | "circuit(params)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 6, 137 | "id": "0b500e66-56bd-4098-a382-9e6d30bab8af", 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "tensor([0, 1, 0, 1, 0], requires_grad=True)" 144 | ] 145 | }, 146 | "execution_count": 6, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "dev = qml.device(\"default.qubit\", wires = H.wires, shots = 1)\n", 153 | "\n", 154 | "@qml.qnode(dev)\n", 155 | "def circuit(params):\n", 156 | " \n", 157 | " for param, wire in zip(params, H.wires):\n", 158 | " qml.RY(param, wires = wire)\n", 159 | " \n", 160 | " return qml.sample()\n", 161 | "\n", 162 | "circuit(params)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "id": "90631edd-cd02-48df-a2c1-6eb40d3cc395", 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [] 172 | } 173 | ], 174 | "metadata": { 175 | "kernelspec": { 176 | "display_name": "Python 3.8.13", 177 | "language": "python", 178 | "name": "python3" 179 | }, 180 | "language_info": { 181 | "codemirror_mode": { 182 | "name": "ipython", 183 | "version": 3 184 | }, 185 | "file_extension": ".py", 186 | "mimetype": "text/x-python", 187 | "name": "python", 188 | "nbconvert_exporter": "python", 189 | "pygments_lexer": "ipython3", 190 | "version": "3.8.13" 191 | }, 192 | "vscode": { 193 | "interpreter": { 194 | "hash": "bf04860b618df62924ac577937eb78853ed0fe0f51833c65adb948f2cfa38ea7" 195 | } 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 5 200 | } 201 | --------------------------------------------------------------------------------