Cody M. Lewis

Quantum Chess

Git Repo

1 Introduction

This was one of my first quantum programs, and essentially uses the quantum programming as a random number generator. I initially started with the command line interface and it is the most refined form of the program, but there is also a web based interface using python flask.

2 Rules

It is a game of chess, only the pieces may be placed into "super-position" where a piece is cloned, but if it gets attacked there is a chance that all of the pieces that it was cloned from get destroyed as well. There is no check, so instead checkmate occurs when all of the opponent's kings are destroyed.

3 How it Works

Most of the pieces and boards codes are implementing standard chess rules, I will only explain the quantum programming parts here.

In there is the superposition method:

def superposition(self):
       self.superposNo += 1
       self.idTag = self.idTag + str(self.superposNo)
       if self.firstSuperPos:
           self.firstSuperPos = False

This sets up the clone with the number of times this piece has been cloned within the clone's id, this is useful for later when this piece is attacked.

When attacked the piece executes the die method, and if super-positioned it will execute the observe function, which will perform a superposition based calculation:

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 functions.evalQubit(self.superposNo)

In there is Quantum programming function evalQubit, this uses Qubits as a random number generator:

from qiskit import *
def evalQubit(qrNo): # check if the most occurring qubit is all ones
        qr = QuantumRegister(qrNo)
        cr = ClassicalRegister(qrNo)
        qc = QuantumCircuit(qr, cr)

        isPiece = ''
        for i in range(qrNo):
            isPiece = isPiece + '1'
        qc.measure(qr, cr)
        result = execute(qc, Aer.get_backend("local_qasm_simulator")).result()
        counts = result.get_counts(qc)
        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))

First it creates a Qubit register and a classical bit register, these are like arrays of bits. The 2 registers are then linked together in a circuit, so the Qubits can be measured and observed against the classical bits allowing for a 1 or 0 result. Next, each of the Qubits are passed through a H-Gate, this performs a single Qubit Quantum Fourier Transform and places each of the Qubits into a state where they have an equal probability of being observed as 1 or 0. Then, the transformed Qubits are measured against the classical bits. Since quantum computers are susceptible to noise this measurement will be performed a number of times in the background, the counts of the various measurements is then stored. And finally, the resulting observations of the Qubits is checked for whether all 1s have been measured the most, if it has then True is returned and all super-positioned children of the piece is destroyed else False, and only the current piece is destroyed.

Then the rest of the program is just implementing a chess game.