qiskit-advocate / qamp-fall-22

Qiskit advocate mentorship program (QAMP) fall 22 cohort (Sep - Dec 2022)
15 stars 7 forks source link

QNim #29

Open JavaFXpert opened 1 year ago

JavaFXpert commented 1 year ago

Description

One of the first games in which a human played against the computer is Nim, so it is an interesting and fitting game to implement on a quantum computer. This Wikipedia article describes the game, strategies and variants: https://en.wikipedia.org/wiki/Nim

Deliverables

A game with a user interface conducive to a human playing Nim against an IBM Quantum computer, both a simulator and real device. The UI could be implemented, for example, as ipywidgets in the IBM Quantum Lab. One idea would be to create a UI or even build a device that emulates the Nimatron, which was first exhibited in 1940 at the New York World's Fair.

Solution requirements

  1. The human and QC (quantum computer) will play according to the rules for Nim misère play, in which the goal is to ensure that the opponent is forced to take the last remaining object.
  2. The starting game configuration will be four piles, each consisting of zero - seven objects. The default will be one, three, five and seven objects in the four piles, but must be configurable.
  3. Either player may make the first move.
  4. The QC's gameplay strategy must be based upon mathematical theory for Nim, played as a misère game. This strategy would be implemented when dynamically generating a circuit based upon the remaining piles of objects each time it is the QC's turn to play.
  5. The QC's move is based solely on measuring the generated quantum circuit. It is permissible to specify multiple shots in the measurement, and to take the most frequently occurring result. Whenever mathematical theory for Nim indicates that more than one winning move is available, the quantum circuit must make each of those moves equally likely to be the result of measuring the circuit.
  6. If the result of a measurement is a nonsensical move for the QC, then the QC automatically loses the game.
  7. The UI must indicate which player won the game.

Stretch goals

Mentors details

Number of mentees

3

Type of mentees

ritu-thombre99 commented 1 year ago

Hi @JavaFXpert Ritu here. This seems really interesting to me. Can we chat more about this project?

JavaFXpert commented 1 year ago

Of course!

Regards, James (Jim) Weaver

On Aug 19, 2022, at 5:20 AM, Ritu Thombre @.***> wrote:

 Hi @JavaFXpert Ritu here. This seems really interesting to me. Can we chat more about this project?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.

hamzakamel1 commented 1 year ago

I'm very interested in this project, As I have a background in Game development and as it is a major in mu university. I would love to join this project as a mentee

JavaFXpert commented 1 year ago

Awesome!

On Sun, Aug 28, 2022, at 6:22 AM, Hamza Kamel Ahmed wrote:

I'm very interested in this project, As I have a background in Game development and as it is a major in mu university. I would love to join this project as a mentee

— Reply to this email directly, view it on GitHub https://github.com/qiskit-advocate/qamp-fall-22/issues/29#issuecomment-1229426583, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACLZ5WWH63MIVHOLV5TQ2CTV3M4VVANCNFSM56Z5TLUQ. You are receiving this because you were mentioned.Message ID: @.***>

JavaFXpert commented 1 year ago

@ritu-thombre99 and @hamzakamel1 Please reply to my message on Slack on when we can meet to discuss details of the project.

GemmaDawson commented 1 year ago

Please add your Checkpoint 1 presentation materials.

ritu-thombre99 commented 1 year ago

Link to the jupyter notebook - QAMP Checkpoint 1 - QNim

ritu-thombre99 commented 1 year ago

Checkpoint 2 update

Mathematical framework for QNim was completed for checkpoint 1, which included strategy development for quantum computer to play game of Nim against a human or a classical computer. A basic prototype UI using Kivy framework in python was also developed for QNim during checkpoint. Kivy framework is discontinued as it has very limited frontend configuration capacity. Since checkpoint 1, pygame, which is a game development library in python, is used to create a visually appealing UI with a retro theme. Nim board consists of 7x4 grid of bulbs similar to Nimatron, which was displayed during 1940 New York fair. The new UI has the following functionalities:

  1. Home page a. Start new game button b. Continue saved game button c. Quit button
  2. Configuration page a. User can select how many bulbs they want to be in an on state. User can select from minimum of zero to maximum of seven bulbs for four different rows. At least one bulb (from 28 present on the board) should be in an on state in order to start the game. Default board configuration is 1,3,5,7 on state bulbs for the four rows. b. User can select the turn i.e. which player will play first (either user themselves or quantum computer)
  3. Game page a. Turn button (selecting button with quantum computer image will trigger quantum computer to play the next move) b. Game board which has bulbs in on/off state based upon selections made on configuration page. Bulbs in on state can be turned off by pressing them. c. During quantum computer’s turn, an emoji animation will be displayed to show if quantum computer is happy or frowning depending upon whether it can play the winnable move. d. Back button (current game state will be saved to play later) e. Quit button

Plans for checkpoint 3:

Currently it is difficult to implement asynchronous behaviour in pygame. As a result, pygame will not register any clicks when quantum operations are happening in the background during quantum computer’s turn. This leads to game window becoming non responsive until quantum operations are finished. This may not be noticeable if local qasm_simulator is used as a backend, since it is local and faster. But the lag will be considerable if one wants to play against ibmq_qasm_simulator or a real IBM quantum computers. To deal with this issue, we are planning to implement QNim as a web application through a client server architecture. Server will be implemented using flask REST API which can be called by a client. This REST API will take in the game board state and send the quantum computer move to client in a JSON format. Client will be developed using python frameworks (flask or Django) which have AJAX functionality to handle the required asynchronous behaviour of the QNim game.

ritu-thombre99 commented 1 year ago

Visual representation

Mathematical framework: mathematical framework

Experiment results: final_with_label

Gameplay video - https://youtu.be/vqE8HICiBqA

https://user-images.githubusercontent.com/42207923/199692070-c071147f-d5ec-4ee6-b71b-b99cb4fc8ea8.mp4

Game screenshots :

Home page: home

Home page with current saved game: home_with_continue

Configuration page: config

Player turn: game_player_turn

QC turn: game_qc_turn

Player won: game_won

QC won: game_lost

GemmaDawson commented 1 year ago

@ritu-thombre99 - please upload your Final Showcase presentation materials, and if needed, update the project Title and/or description.

ritu-thombre99 commented 1 year ago

Final checkpoint update

UI using pygame for QNim was completed for checkpoint 2. Since checkpoint 2, QNim is implemented in Roblox. The player's character enters the world and a QNim board is booted, which has a grid of 7x4 neon pink lights. During the player's turn, lights which can be switched off are neon green. To call the quantum computer to play, the player can type qc turn in the chatbox. A REST API is created using flask which takes in the current board state through parameters in the URL and returns the quantum move. Lua code in the roblox will call this API using GET request when it is QC's turn to play. Initially API was hosted locally and board state was passed in headers as follows: API hosted on localhost

Currently, the API is hosted on the Anvil server and can be accessed publicly. This version of API takes in the board state as params in the URL as follows: API hosted on anvil server

Roblox UI:

roblox UI

Future plans:

  1. Implement a two-player version
  2. Create a quantum computer NPC represeted by a robot, which can interact with players, similar to emoticons showed in pygame version
  3. Using openai API and gpt, let player interact with quantum computer NPC. The player can tell QC NPC their move, for example if a player wants to turn off 3 bulbs in row 2, they can type it in the chat box and QC NPC can perform this action for the player. So the player can have two ways to play the game, using chatbox or button events (QNim board)
  4. As soon as the player is booted into the world, QC NPC can provide instructions on how QNim is played and ask the player if they want to start the turn.
GemmaDawson commented 1 year ago

Congratulations on completing all the requirements for QAMP Fall 2022!! 🌟🌟🌟