From aea52ce2dceb4f48689a4742841cda7629c4fc5d Mon Sep 17 00:00:00 2001 From: "Dynex [DNX]" <113523376+dynexcoin@users.noreply.github.com> Date: Thu, 20 Jun 2024 17:55:31 +0200 Subject: [PATCH] Add files via upload --- QuantumnQueenProblem.ipynb | 186 +++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 QuantumnQueenProblem.ipynb diff --git a/QuantumnQueenProblem.ipynb b/QuantumnQueenProblem.ipynb new file mode 100644 index 0000000..bad80e3 --- /dev/null +++ b/QuantumnQueenProblem.ipynb @@ -0,0 +1,186 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0ad2e6ba-25c6-4046-9383-aa0a97c61d4d", + "metadata": {}, + "source": [ + "# Quantum Game Theory - 8 Queen Problem" + ] + }, + { + "cell_type": "markdown", + "id": "df808299-0a82-47ce-8d26-dcbd7dffd5cf", + "metadata": {}, + "source": [ + "The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other; thus, a solution requires that no two queens share the same row, column, or diagonal. There are 92 solutions. The problem was first posed in the mid-19th century. In the modern era, it is often used as an example problem for various computer programming techniques.\n", + "\n", + "The eight queens puzzle is a special case of the more general n queens problem of placing n non-attacking queens on an n×n chessboard. Solutions exist for all natural numbers n with the exception of n = 2 and n = 3. Although the exact number of solutions is only known for n ≤ 27, the asymptotic growth rate of the number of solutions is approximately (0.143 n)n. \n", + "\n", + "Chess composer Max Bezzel published the eight queens puzzle in 1848. Franz Nauck published the first solutions in 1850. Nauck also extended the puzzle to the n queens problem, with n queens on a chessboard of n×n squares\n", + "\n", + "https://en.wikipedia.org/wiki/Eight_queens_puzzle" + ] + }, + { + "cell_type": "markdown", + "id": "76c16453-0a46-4d2b-b969-c2d387a913d1", + "metadata": {}, + "source": [ + "Coded by Y3TI & Sam Rahmeh" + ] + }, + { + "cell_type": "markdown", + "id": "c6e19829-aecc-40f4-8cdc-5ad52734638c", + "metadata": {}, + "source": [ + "### Import Libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d5a90bf0-b803-459e-b582-fd5cd3120669", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (2050042617.py, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[3], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m from 'nqueen/util.py' import constraint, displayBoard\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "from util import constraint, displayBoard\n", + "from sympy import *\n", + "from IPython.display import display\n", + "import dimod\n", + "import dynex\n", + "from util import constraint, displayBoard\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.colors as mcolors\n", + "import numpy as np\n", + "import matplotlib.image as mpimg\n", + "import matplotlib.offsetbox as offsetbox\n", + "init_printing()\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "id": "5357a17c-acda-40d8-977b-a46ca4538d47", + "metadata": {}, + "source": [ + "### Compute on DYNEX" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed2be420-e965-4b1b-8336-bec94f6fdd42", + "metadata": {}, + "outputs": [], + "source": [ + "def NQueen2BQM(n):\n", + " bqm = dimod.BinaryQuadraticModel('BINARY')\n", + " for row in range(n):\n", + " bqm.add_linear_equality_constraint(\n", + " [(row * n + col, 1.0) for col in range(n)],\n", + " constant=-1.0,\n", + " lagrange_multiplier=n)\n", + " for i in range(n):\n", + " for j in range(n):\n", + " if i < j:\n", + " for k in range(n):\n", + " bqm.add_interaction(i * n + k, j * n + k, 2 * n)\n", + " if k + (j - i) < n:\n", + " bqm.add_interaction(i * n + k, j * n + (k + (j - i)), 2 * n)\n", + " if k - (j - i) >= 0:\n", + " bqm.add_interaction(i * n + k, j * n + (k - (j - i)), 2 * n)\n", + " return bqm\n", + "\n", + "def Visualize(board):\n", + " n = len(board)\n", + " cmap = mcolors.ListedColormap(['#f5ecce', '#614532'])\n", + " img = mpimg.imread('queen.png')\n", + " fig, ax = plt.subplots()\n", + " ax.matshow(np.add.outer(range(n), range(n)) % 2 == 0, cmap=cmap)\n", + " plt.axis(\"off\") \n", + " ax_width = ax.get_window_extent().width / ax.figure.dpi * fig.dpi\n", + " ax_height = ax.get_window_extent().height / ax.figure.dpi * fig.dpi\n", + " cell_size = min(ax_width, ax_height) / n\n", + " img_height, img_width = img.shape[:2]\n", + " scale_width = cell_size / img_width\n", + " scale_height = cell_size / img_height\n", + " scale = min(scale_width, scale_height)\n", + " for r in range(n):\n", + " for c in range(n):\n", + " if board[r][c] == 'Q':\n", + " box = offsetbox.OffsetImage(img, zoom=scale)\n", + " ab = offsetbox.AnnotationBbox(box, (c, r), frameon=False)\n", + " ax.add_artist(ab)\n", + " scale_factor = 1.1 \n", + " fig.set_size_inches(n * scale_factor, n * scale_factor)\n", + " plt.show()\n", + " return fig\n", + "\n", + "def SolveNQueens(n):\n", + " bqm = NQueen2BQM(n)\n", + " model = dynex.BQM(bqm)\n", + " sampler = dynex.DynexSampler(model, mainnet=False, description='Quantum 8 Queen Problem')\n", + " sampleset = sampler.sample(num_reads=10000, annealing_time=500, debugging=False, alpha=10, beta=1)\n", + " solution = sampleset.first.sample\n", + " board = [['.' for _ in range(n)] for _ in range(n)]\n", + " for i in range(n):\n", + " for j in range(n):\n", + " if solution[i * n + j] == 1:\n", + " board[i][j] = 'Q'\n", + " Visualize(board)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c2724afe-c279-4643-8954-a1d25750cd05", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"[DYNEX] 8 Queen Problem Solver\")\n", + "n = 8\n", + "SolveNQueens(n)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "341db188-999b-4262-b8e3-70177e5efe89", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}