quantum-chess

git clone git://git.codymlewis.com/quantum-chess.git
Log | Files | Refs | LICENSE

commit 3e4b2dfe1c24980cb59a0639a83886db89c888ec
parent c0a3c7adffabdfcc2b175dcd77d6a185568e3050
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Tue, 31 Jul 2018 22:53:49 +1000

Started adding enums to the cmd program

Diffstat:
Msrc/WebApp/Main.py | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/WebApp/static/scripts/chess.js | 19+------------------
Msrc/WebApp/templates/index.html | 3++-
Msrc/cmd/bishop.py | 9++++-----
Msrc/cmd/functions.py | 25++++++++++++++++++-------
Msrc/cmd/piece.py | 12++++++------
6 files changed, 113 insertions(+), 37 deletions(-)

diff --git a/src/WebApp/Main.py b/src/WebApp/Main.py @@ -1,3 +1,4 @@ +from qiskit import QuantumProgram from flask import ( Blueprint, render_template, url_for, redirect, current_app, g, session, request, flash ) @@ -10,3 +11,83 @@ def index(): @bp.route("/home/") def home(): return render_template("index.html") + +def evalQubit(qrNo): # check if the most occurring qubit is all ones + qp = QuantumProgram() + try: + qr = qp.create_quantum_register("qr",qrNo) + cr = qp.create_classical_register("cr",qrNo) + qc = qp.create_circuit("superposition", [qr], [cr]) + + isPiece = '' + for i in range(qrNo): + qc.h(qr[i]) + isPiece = isPiece + '1' + qc.measure(qr, cr) + result = qp.execute(["superposition"], backend="local_qasm_simulator", shots=1024) + counts = result.get_counts("superposition") + return isPiece == getGreatestCount(qrNo, counts) + except QISKITError as ex: + print("There was an error in the circuit! Error = {}".format(ex)) + except RegisterSizeError as ex: + print("Error in the number of registers! Error = {}".format(ex)) + +def getGreatestCount(qrNo,counts): + # increment throught dict, find greatest value, return index + greatestCount = '' + gc = 0 + perms = 2**qrNo + for i in range(0, perms): + index = bin(i)[2:] + if len(index) < qrNo: + while len(index) < qrNo: + index = '0' + index + if counts[index] > gc: + greatestCount = index + gc = counts[index] + return greatestCount + +def splitMovement(movement): + # split the string movement into individual characters and store them in the returned array + # movement key: f = up dia left, u = up, q = up dia right, r = right, l = left, h = down dia left, d = down, g = down dia right + # Note: The move movement directions are from a birds eye view with the white pieces on the bottom + moveArr = [] + for i in range(len(movement)): + moveArr.append(movement[i:i+1]) + return moveArr +class Piece: + def __init__(self, superposNum, frstSuperPos, col, idT): + self.superposNo = superposNum + self.firstSuperPos = frstSuperPos + self.colour = col + self.idTag = self.colour + idT + + def getId(self): + return self.idTag + + def getSuperPosNum(self): + return self.superPosNo + + def superposition(self): + self.superposNo += 1 + self.idTag = self.idTag + str(self.superposNo) + if self.firstSuperPos: + self.firstSuperPos = False + + def attack(self,enemy,movement): + if self.canAttack(movement): + return enemy.die() + else: + return False,False + + def canAttack(self,movement): + return self.canMove(movement) + + def die(self): + if self.superposNo > 0: + return True, self.observe() + return True, False + + def observe(self): + # check the Qubit stored in this piece + return evalQubit(self.superposNo)+ \ No newline at end of file diff --git a/src/WebApp/static/scripts/chess.js b/src/WebApp/static/scripts/chess.js @@ -1,20 +1,3 @@ -var Piece = { - id : "", - color : "", - firstSuperPos : true, - superPosNum : 0, - superPosition : function() { - if(this.firstSuperPos) { - this.firstSuperPos = false; - } - }, - // attack : attack(), -}; $(document).ready(function() { - var canvas = document.getElementById("qccanvas"); - var ctx = canvas.getContext("2d"); - for(var i = 0; i < 300; i += 20) { - ctx.fillRect(i, i, i + 40, i + ); - } - ctx.fillRect(0, 0, 280, 138); + var board = ChessBoard("board", "start"); }); \ No newline at end of file diff --git a/src/WebApp/templates/index.html b/src/WebApp/templates/index.html @@ -10,7 +10,8 @@ <div class="container-fluid bg-light"> <div class="container"> <!-- <h1 class="display-1">Quantum Chess</h1> --> - <canvas class="col-12" id="qccanvas"></canvas> + <!-- <canvas class="col-12" id="qccanvas"></canvas> --> + <div id="board" class="col-12"></div> </div> </div> {% endblock %} \ No newline at end of file diff --git a/src/cmd/bishop.py b/src/cmd/bishop.py @@ -1,19 +1,18 @@ # bishop.py - QuantumChess # Author: Cody Lewis # Date: 24-FEB-2018 -# Mod.: 24-FEB-2018 import piece import functions class Bishop(piece.Piece): - def __init__(self,superposNum,frstSuperPos,col,idT): + def __init__(self, superposNum, frstSuperPos, col, idT): idT = 'B ' + str(idT) - piece.Piece.__init__(self,superposNum,frstSuperPos,col,idT) + piece.Piece.__init__(self, superposNum, frstSuperPos, col, idT) def canMove(self,movement): moveArr = functions.splitMovement(movement) direction = moveArr[0] - if(direction == 'f' or direction == 'q' or direction == 'h' or direction == 'g'): - for i in range(1,len(moveArr)): + if direction == functions.Direction.UPLEFT or direction == functions.Direction.UPRIGHT or direction == functions.Direction.DOWNLEFT or direction == functions.Direction.DOWNRIGHT: + for i in range(1, len(moveArr)): if(direction != moveArr[i]): return False return True diff --git a/src/cmd/functions.py b/src/cmd/functions.py @@ -4,12 +4,13 @@ # Mod.: 04-MAY-2018 # Description: # defines misc. functions for the Quantum Chess program +from enum import Enum from qiskit import QuantumProgram def evalQubit(qrNo): # check if the most occurring qubit is all ones qp = QuantumProgram() try: - qr = qp.create_quantum_register("qr",qrNo) - cr = qp.create_classical_register("cr",qrNo) + qr = qp.create_quantum_register("qr", qrNo) + cr = qp.create_classical_register("cr", qrNo) qc = qp.create_circuit("superposition", [qr], [cr]) isPiece = '' @@ -25,17 +26,17 @@ def evalQubit(qrNo): # check if the most occurring qubit is all ones except RegisterSizeError as ex: print("Error in the number of registers! Error = {}".format(ex)) -def getGreatestCount(qrNo,counts): +def getGreatestCount(qrNo, counts): # increment throught dict, find greatest value, return index greatestCount = '' gc = 0 perms = 2**qrNo - for i in range(0,perms): + for i in range(0, perms): index = bin(i)[2:] - if(len(index) < qrNo): - while(len(index) < qrNo): + if len(index) < qrNo: + while len(index) < qrNo: index = '0' + index - if(counts[index] > gc): + if counts[index] > gc: greatestCount = index gc = counts[index] return greatestCount @@ -48,3 +49,13 @@ def splitMovement(movement): for i in range(len(movement)): moveArr.append(movement[i:i+1]) return moveArr + +class Direction(Enum): + UPLEFT = "f" + UP = "u" + UPRIGHT = "q" + RIGHT = "r" + LEFT = "l" + DOWN = "d" + DOWNLEFT = "h" + DOWNRIGHT = "g" diff --git a/src/cmd/piece.py b/src/cmd/piece.py @@ -5,7 +5,7 @@ # Description: Defines the super class of a piece in Quantum Chess import functions class Piece: - def __init__(self,superposNum,frstSuperPos,col,idT): + def __init__(self, superposNum, frstSuperPos, col, idT): self.superposNo = superposNum self.firstSuperPos = frstSuperPos self.colour = col @@ -20,11 +20,11 @@ class Piece: def superposition(self): self.superposNo += 1 self.idTag = self.idTag + str(self.superposNo) - if(self.firstSuperPos): + if self.firstSuperPos: self.firstSuperPos = False def attack(self,enemy,movement): - if(self.canAttack(movement)): + if self.canAttack(movement): return enemy.die() else: return False,False @@ -33,9 +33,9 @@ class Piece: return self.canMove(movement) def die(self): - if(self.superposNo > 0): - return True,self.observe() - return True,False + if self.superposNo > 0: + return True, self.observe() + return True, False def observe(self): # check the Qubit stored in this piece