Unknown's avatar

About Chris Cantwell

Graduate Physics and Computer Science student at USC, and creator of Quantum Chess.

Developing an AI for Quantum Chess: Part 1

In January 2016, Caltech’s Institute for Quantum Information and Matter unveiled a YouTube video featuring an extraordinary chess showdown between actor Paul Rudd (a.k.a. Ant-Man) and the legendary Dr. Stephen Hawking. But this was no ordinary match—Rudd had challenged Hawking to a game of Quantum Chess. At the time, Fast Company remarked, “Here we are, less than 10 days away from the biggest advertising football day of the year, and one of the best ads of the week is a 12-minute video of quantum chess from Caltech.” But a Super Bowl ad for what, exactly?

For the past nine years, Quantum Realm Games, with continued generous support from IQIM and other strategic partnerships, has been tirelessly refining the rudimentary Quantum Chess prototype showcased in that now-viral video, transforming it into a fully realized game—one you can play at home or even on a quantum computer. And now, at long last, we’ve reached a major milestone: the launch of Quantum Chess 1.0. You might be wondering—what took us so long?

The answer is simple: developing an AI capable of playing Quantum Chess.

Before we dive into the origin story of the first-ever AI designed to master a truly quantum game, it’s important to understand what enables modern chess AI in the first place.

Chess AI is a vast and complex field, far too deep to explore in full here. For those eager to delve into the details, the Chess Programming Wiki serves as an excellent resource. Instead, this post will focus on what sets Quantum Chess AI apart from its classical counterpart—and the unique challenges we encountered along the way.

So, let’s get started!

Depth Matters

credit: https://www.freecodecamp.org/news/simple-chess-ai-step-by-step-1d55a9266977/

With Chess AI, the name of the game is “depth”, at least for versions based on the Minimax strategy conceived by John von Neumann in 1928 (we’ll say a bit about Neural Network based AI later). The basic idea is that the AI will simulate the possible moves each player can make, down to some depth (number of moves) into the future, then decide which one is best based on a set of evaluation criteria (minimizing the maximum loss incurred by the opponent). The faster it can search, the deeper it can go. And the deeper it can go, the better its evaluation of each potential next move is.

Searching into the future can be modelled as a branching tree, where each branch represents a possible move from a given position (board configuration). The average branching factor for chess is about 35. That means that for a given board configuration, there are about 35 different moves to choose from. So if the AI looks 2 ply (moves) ahead, it sees 35×35 moves on average, and this blows up quickly. By 4 ply, the AI already has 1.5 million moves to evaluate. 

Modern chess engines, like Stockfish and Leela, gain their strength by looking far into the future. Depth 10 is considered low in these cases; you really need 20+ if you want the engine to return an accurate evaluation of each move under consideration. To handle that many evaluations, these engines use strong heuristics to prune branches (the width of the tree), so that they don’t need to calculate the exponentially many leaves of the tree. For example, if one of the branches involves losing your Queen, the algorithm may decide to prune that branch and all the moves that come after. But as experienced players can see already, since a Queen sacrifice can sometimes lead to massive gains down the road, such a “naive” heuristic may need to be refined further before it is implemented. Even so, the tension between depth-first versus breadth-first search is ever present.

So I heard you like branches…

https://www.sciencenews.org/article/leonardo-da-vinci-rule-tree-branch-wrong-limb-area-thickness

The addition of split and merge moves in Quantum Chess absolutely explodes the branching factor. Early simulations have shown that it may be in the range of 100-120, but more work is needed to get an accurate count. For all we know, branching could be much bigger. We can get a sense by looking at a single piece, the Queen.

On an otherwise empty chess board, a single Queen on d4 has 27 possible moves (we leave it to the reader to find them all). In Quantum Chess, we add the split move: every piece, besides pawns, can move to any two empty squares it can reach legally. This adds every possible paired combination of standard moves to the list. 

But wait, there’s more! 

Order matters in Quantum Chess. The Queen can split to d3 and c4, but it can also split to c4 and d3. These subtly different moves can yield different underlying phase structures (given their implementation via a square-root iSWAP gate between the source square and the first target, followed by an iSWAP gate between the source and the second target), potentially changing how interference works on, say, a future merge move. So you get 27*26 = 702 possible moves! And that doesn’t include possible merge moves, which might add another 15-20 branches to each node of our tree. 

Do the math and we see that there are roughly 30 times as many moves in Quantum Chess for that queen. Even if we assume the branching factor is only 100, by ply 4 we have 100 million moves to search. We obviously need strong heuristics to do some very aggressive pruning. 

But where do we get strong heuristics for a new game? We don’t have centuries of play to study and determine which sequences of moves are good and which aren’t. This brings us to our first attempt at a Quantum Chess AI. Enter StoQfish.

StoQfish

Quantum Chess is based on chess (in fact, you can play regular Chess all the way through if you and your opponent decide to make no quantum moves), which means that chess skill matters. Could we make a strong chess engine work as a quantum chess AI? Stockfish is open source, and incredibly strong, so we started there.

Given the nature of quantum states, the first thing you think about when you try to adapt a classical strategy into a quantum one, is to split the quantum superposition underlying the state of the game into a series of classical states and then sample them according to their (squared) amplitude in the superposition. And that is exactly what we did. We used the Quantum Chess Engine to generate several chess boards by sampling the current state of the game, which can be thought of as a quantum superposition of classical chess configurations, according to the underlying probability distribution. We then passed these boards to Stockfish. Stockfish would, in theory, return its own weighted distribution of the best classical moves. We had some ideas on how to derive split moves from this distribution, but let’s not get ahead of ourselves.

This approach had limited success and significant failures. Stockfish is highly optimized for classical chess, which means that there are some positions that it cannot process. For example, consider the scenario where a King is in superposition of being captured and not captured; upon capture of one of these Kings, samples taken after such a move will produce boards without a King! Similarly, what if a King in superposition is in check, but you’re not worried because the other half of the King is well protected, so you don’t move to protect it? The concept of check is a problem all around, because Quantum Chess doesn’t recognize it. Things like moving “through check” are completely fine.

You can imagine then why whenever Stockfish encounters a board without a King it crashes. In classical Chess, there is always a King on the board. In Quantum Chess, the King is somewhere in the chess multiverse, but not necessarily in every board returned by the sampling procedure. 

You might wonder if we couldn’t just throw away boards that weren’t valid. That’s one strategy, but we’re sampling probabilities so if we throw out some of the data, then we introduce bias into the calculation, which leads to poor outcomes overall.

We tried to introduce a King onto boards where he was missing, but that became its own computational problem: how do you reintroduce the King in a way that doesn’t change the assessment of the position?

We even tried to hack Stockfish to abandon its obsession with the King, but that caused a cascade of other failures, and tracing through the Stockfish codebase became a problem that wasn’t likely to yield a good result.

This approach wasn’t working, but we weren’t done with Stockfish just yet. Instead of asking Stockfish for the next best move given a position, we tried asking Stockfish to evaluate a position. The idea was that we could use the board evaluations in our own Minimax algorithm. However, we ran into similar problems, including the illegal position problem.

So we decided to try writing our own minimax search, with our own evaluation heuristics. The basics are simple enough. A board’s value is related to the value of the pieces on the board and their location. And we could borrow from Stockfish’s heuristics as we saw fit. 

This gave us Hal 9000. We were sure we’d finally mastered quantum AI. Right? Find out what happened, in the next post.

Quantum Chess

Two years ago, as a graduate student in Physics at USC,  I began work on a game whose mechanics were based on quantum mechanics. When I had a playable version ready, my graduate adviser, Todd Brun, put me in contact with IQIM’s Spiros Michalakis, who had already worked with Google to design qCraft, a mod introducing quantum mechanics into Minecraft. Spiros must have seen potential in my clunky prototype and our initial meeting turned into weekly brainstorming lunches at Caltech’s Chandler cafeteria. More than a year later, the game had evolved into Quantum Chess and we began talking about including a video showing some gameplay at an upcoming Caltech event celebrating Feynman’s quantum legacy. The next few months were a whirlwind. Somehow this video turned into a Quantum Chess battle for the future of humanity, between Stephen Hawking and Paul Rudd. And it was being narrated by Keanu Reeves! The video, called Anyone Can Quantum, and directed by Alex Winter, premiered at Caltech’s One Entangled Evening on January 26, 2016 and has since gone viral. If you haven’t watched it, now would be a good time to do so (if you are at work, be prepared to laugh quietly).

So, what exactly is Quantum Chess and how does it make use of quantum physics? It is a modern take on the centuries-old game of strategy that endows each chess piece with quantum powers. You don’t need to know quantum mechanics to play the game. On the other hand, understanding the rules of chess might help [1].  But if you already know the basics of regular chess, you can just start playing. Over time, your brain will get used to some of the strange quantum behavior of the chess pieces and the battles you wage in Quantum Chess will make regular chess look like tic-tac-toe [2].

Quantum ChessIn this post, I will discuss the concept of quantum superposition and how it plays a part in the game. There will be more posts to follow that will discuss entanglement, interference, and quantum measurement [3].

In quantum chess, players have the ability to perform quantum moves in addition to the standard chess moves. Each time a player chooses to move a piece, they can indicate whether they want to perform a standard move, or a quantum move. A quantum move creates a superposition of boards. If any of you ever saw Star Trek 3D Chess, you can think of this in a similar way.

Star Trek 3D Chess

There are multiple boards on which pieces exist. However, in Quantum Chess, the number of possible boards is not fixed, it can increase or decrease. All possible boards exist in a superposition. The player is presented with a single board that represents the entire superposition. In Quantum Chess, any individual move will act on all boards at the same time.  Each time a player makes a quantum move, the number of possible boards present in the superposition doubles. Let’s look at some pictures that might clarify things.

The Quantum Chess board begins in the same configuration as standard chess.

InitialConfigAll pawns move the same as they would in standard chess, but all other pieces get a choice of two movement types, standard or quantum. Standard moves act exactly as they would in standard chess. However, quantum moves, create superpositions. Let’s look at an example of a quantum move for the white queen.

QueenQuantumMoveIn this diagram, we see what happens when we perform a quantum move of the white queen from D1 to D3. We get two possible boards. On one board the queen did not move at all. On the other, the queen did move. Each board has a 50% chance of “existence”. Showing every possible board, though, would get quite complicated after just a few moves. So, the player view of the game is a single board. After the same quantum queen move, the player sees this:

PlayerViewQQD1D3The teal colored “fill” of each queen shows the probability of finding the queen in that space; the same queen, existing in different locations on the board. The queen is in a superposition of being in two places at once. On their next turn, the player can choose to move any one of their pieces.   

So, let’s talk about moving the queen, again. You may be wondering, “What happens if I want to move a piece that is in a superposition?” The queen exists in two spaces. You choose which of those two positions you would like to move from, and you can perform the same standard or quantum moves from that space. Let’s look at trying to perform a standard move, instead of a quantum move, on the queen that now exists in a superposition. The result would be as follows:

StandardSuperpositionQueenMoveThe move acts on all boards in the superposition. On any board where the queen is in space D3, it will be moved to B5. On any board where the queen is still in space D1, it will not be moved. There is a 50% chance that the queen is still in space D1 and a 50% chance that it is now located in B5. The player view, as illustrated below, would again be a 50/50 superposition of the queen’s position. This was just an example of a standard move on a piece in a superposition, but a quantum move would work similarly.

PlayerViewQueenMove

Some of you might have noticed the quantum move basically gives you a 50% chance to pass your turn. Not a very exciting thing to do for most players. That’s why I’ve given the quantum move an added bonus. With a quantum move, you can choose a target space that is up to two standard moves away! For example, the queen could choose a target that is forward two spaces and then left two spaces. Normally, this would take two turns: The first turn to move from D1 to D3 and the second turn to move from D3 to B3. A quantum move gives you a 50% chance to move from D1 to B3 in a single turn!

Let’s look at a quantum queen move from D1 to B3.

QQD1B3Just like the previous quantum move we looked at, we get a 50% probability that the move was successful and a 50% probability that nothing happened. As a player, we would see the board below.

QuantumQueenD1toB3There is a 50% chance the queen completed two standard moves in one turn! Don’t worry though, things are not just random. The fact that the board is a superposition of boards and that movement is unitary (just a fancy word for how quantum things evolve) can lead to some interesting effects. I’ll end this post here. Now, I hope I’ve given you some idea of how superposition is present in Quantum Chess. In the next post I’ll go into entanglement and a bit more on the quantum move!

Notes:

[1] For those who would like to know more about chess, here is a good link.

[2] If you would like to see a public release of Quantum Chess (and get a copy of the game), consider supporting the Kickstarter campaign.

[3] I am going to be describing aspects of the game in terms of probability and multiple board states. For those with a scientific or technical understanding of how quantum mechanics works, this may not appear to be very quantum. I plan to go into a more technical description of the quantum aspects of the game in a later post. Also, a reminder to the non-scientific audience. You don’t need to know quantum mechanics to play this game. In fact, you don’t even need to know what I’m going to be describing here to play! These posts are just for those with an interest in how concepts like superposition, entanglement, and interference can be related to how the game works.