Path: blob/main/notebooks/ch-demos/first-quantum-game.ipynb
3855 views
Your First Quantum Game
If you've already read some of this textbook, you should know at least a few quantum gates. If you are wondering what to do with that knowledge, perhaps making a game can be the answer. Making simple games can be a fun way to try out new programming knowledge.
If we are going to make a game, we'll need a game engine. Here we'll introduce a simple game engine that can run here in the Jupyter notebooks of this textbook. With this we'll make make a very simple game based on single qubit gates.
First, we import the game engine.
A few simple examples
A minimal example of using this game engine is to simply set all the pixels to a certain colour.
Now let's set one specific pixel to a different colour.
We'll move this around using the arrow buttons.
Walking off the edge of the screen results in an error. We can fix this.
Here the engine.X and engine.Y coordinates are still allowed to go beyond the screen, but the pixel is displayed such that it wraps back round. We can interpret this as the pixel moving on to another screen.
Now let's move towards giving our pixel a more exciting world to explore. We'll use a function to decide what colour each point should be. We'll start by not changing very much.
Now let's change get_color to create a beach.
We'll now put a single qubit circuit inside this terrain generator, so that we can experiment with single qubit gates by making terrain. For that we'll need some Qiskit tools.
First, let's see what the results from a circuit look like. Here's an example with just a ry gate for a given angle of rotation.
Here the angle means that the result of a measurement at the end of this circuit would be equally likely to give 0 or 1. For other angles we can get a bias towards one or the other. This is exactly the kind of behaviour we would expect from these gates, as explained in Chapter 1.
Let's use the probability of the output '1' as a height, and set the colour accordingly.
By adding more complex rotations, and thinking about how these gates combine, we can build on this to create more complex terrain.
Now experiment by making your own circuits to generate more interesting terrain, and use it to explore what simple quantum gates can do. Or perhaps make a completely new game of your own!
How to use the game engine
To make full use of the game engine, you'll need all the details of how it works. It gives us the ability to make games for a low-pixel screen (8x8 by default), controlled by a D-pad and five buttons. Note that the screen will only update when a button is pressed.
The game engine is all based around the QiskitGameEngine object. You can call this whatever you like. In the following we will call it engine.
The screen
The pixels can be addressed in the functions using engine.screen. The pixel at position (x,y) is addressed as engine.screen.pixel[x,y]. It has three methods:
set_color(color)- The argumentcoloris a string: either'grey','green','blue','orange'or'red'.set_brightness(bright)- The argumentbrightis a Boolean value:Falsefor dim andTruefor bright.set_text(text)- The argument is text to be displayed.
Note that most pixels won't contain more than a few characters. A larger piece of text can be written on the long pixel at the bottom of the screen, which is accessed with engine.screen.pixel['text'].
The controller
The controller is accessed using engine.controller. Its buttons are addressed using the keys 'down', 'up', 'left', 'right', 'A', 'B', 'X', 'Y' and 'next'. Each is a Jupyter widget button object. Pressing any of these buttons will cause the next_frame function to run. Use the value attribute of each button to determine whether the button has been pressed (True) or not (False).
The game loop
Games are made by defining two functions, start and next_frame. The start function runs when the game begins, and next_frame runs every time a button is pressed to move the game along. Both should have a single argument: the class engine. All parameters required for the game should be defined as attributes to the engine class.
Putting it all together
The game is started by initiating the QiskitGameEngine object with the start and next_frame functions. You can also choose a size other than the default 8x8 grid using the keyword argument L.
Note that the grid size can be accessed in the start and next_frame functions as engine.L. You can also choose a size other than the default 8x8 grid using the kwarg L.