# How I Use Quantum Computing to Play Dungeons & Dragons

You pry open the hefty cellar door and find yourself face-to-face with a rather portly goblin. Do you attack? Enchant him, then sneak away? Or do you attempt to befriend him by offering to make tea?

Dungeons & Dragons is not only a game of improvisation, it’s a game of randomness. Even if you decide to whip up a nice hot beverage as a way to placate the goblin, a devastating dice roll might render you the perfect snack to accompany the tea.

Now, whenever ‘randomness’ enters the picture I think ‘quantum’. So why not marry quantum computing with D&D? This is exactly what my friend, Soham, a Qiskit Advocate who tames dragons on Sundays, and I decided to do for IndiQ’s recent Quantum Game Jam.

We used IBM’s open source framework, Qiskit, to build The D&D μStarter Kit, a helper tool app for D&D players.

At the moment it has three functionalities, which implement quantum computing and ad-libs to capture the randomness and improvisation that make a D&D campaign fun and unique:

- A character generator that provides a race, class, name, and a one-line ad-libbed backstory
- A dice roller to determine ability scores or to use during gameplay
- An ad-libbed D&D scene

Random numbers, letters, and words are generated by implementing what is known as *quantum superposition*. To see just how this generation works, let’s look at our dice roller.

# Quantum Dice

I’ll be honest, nothing beats the feel of physically rolling a nice d20. The rattle in your palm, the anticipation as you let go, the *plonk! *as it hits the table (or the floor, if you’re anything like me). And finally, the Big Reveal: Which fateful face points up?

But not all campaigns can be conducted in person — especially these days — and not everyone who plays D&D has a complete set of dice. That’s where online dice rollers come in handy. They are, essentially, random number generators.

The thing is, random number generators are in actuality *pseudo *random; they definitely serve the purpose of rolling dice, say, very well, but they’re governed by underlying functions and patterns. It is believed that quantum computers can provide *truly* random numbers, which is an ongoing research interest in the community beyond the scope of this piece.

Suffice it to say that quantum computing is an excellent candidate for dice requirements, and frankly, really cool!

The D&D μStarter Kit’s dice roller makes use of quantum circuits in which each *qubit* is placed in a *superposition state*.

Let me back up for a second there: *Bits* are the little computing units your computer uses to store a value of either 0 or 1. *Qubits *are what quantum computers use. Like bits, qubits ultimately give us a 0 or 1 as an output upon measurement, but they can also be in a superposition of 0 and 1 prior to measurement, making them very different from bits. For the present context, a ‘superposition state’ implies an equal superposition of 0 and 1.

When a qubit in such a superposition state is measured, you’re as likely to get a 0 as a 1. This is not unlike you flipping a coin: You’re as likely to get a heads as you are a tails! Measuring a qubit in a superposition state is essentially asking the qubit to pick a random number from two options: 0 or 1.

Operational entities called *quantum gates* can be applied to qubits to make them behave the way we want them to, similar to gates operating on bits on your own computer. The *Hadamard* gate is a single qubit gate that transforms a qubit’s state from the 0 state,|0>, to a 50–50 superposition state, |0>+|1>. (The little symbols |> that I’m using are simply a convention to represent a quantum state, called Dirac notation. There’s also a normalization factor involved, but I’m omitting that here because it’s not relevant for this discussion.)

Upon measurement, you either get the |0> state or the |1> state. Essentially, you’re left with a 0 or 1!

Here’s a pictorial representation of what happens to a single qubit:

When you have more than one qubit in such a superposition state, you have even more possible outcomes!

Suppose you apply two Hadamards on qubits that are each in the |0> state. You’re left with two qubits in superposition states, which means, the collective state of the two qubits is |00>+|01>+|10>+|11>. This means that 00, 01, 10, and 11 are all equally likely to be obtained after measurement:

Right away you can see that having two qubits in superposition states is equivalent to a d4! When rolling a d4 you get one of four possible outcomes that are each equally likely to be obtained. You can easily assign labels the way I have: 00 as 1, 01 as 2, etc.

This is essentially how the dice roller works in the D&D μStarter Kit. We take an appropriate number of qubits according to which die is rolled, place them in superposition states through Hadamard operations, and then measure!

When you have 2 qubits, you can have 2² = 4 possible outcomes. When you have 3, there are 2³ = 8 possible outcomes. You may be wondering: what happens when you need a d6? You can’t have a number of qubits between 2 and 3!

The way we’ve implemented in-between die numbers such as d6 is to take more qubits than required, and then roll until we get a number between (and including) 1 and 6. For d6 we take 3 qubits, and if either 7 or 8 is obtained once the qubits are measured, the outcome is rejected and the quantum die is rolled again. This isn’t the most elegant of solutions, but it ensures that each number is still equally likely to be picked.

# Character Generation

The character generator provides a name, race, class, and one-line backstory. If the race selected is dwarf, then a clan name is provided as well!

These entities are randomly generated through qubits in superposition states in the same way random numbers are selected for the dice roller. But rather than outputting a number, the quantum computer pulls from letter, word, and backstory banks. An appropriate number of qubits is associated with each bank. For instance, if there are 32 backstories in the backstory bank, then 5 qubits are used in order to select one of them (2⁵ = 32).

The backstories in particular have an extra degree of randomness by being ad-libbed. The user is prompted for one of the words, and the quantum computer randomly pulls at least one word from the word banks as though it plays ad-libs alongside the user!

Here’s a sample output:

The user input is highlighted in orange, and the quantum computer’s selection is highlighted in blue.

Class selection is a tad more complex than simply pulling from a word bank; a class is selected once the race has been selected, and its probability of being chosen depends on how synergetic it is with the race. More detail about class selection is provided in the Github repo. Note: the class and race selections are based on the D&D 5E Player’s Handbook.

The D&D μStarter Kit also offers a fun ad-libbed D&D scene, which is essentially a longer version of a backstory. It takes multiple user inputs and the quantum computer chooses many words as well!

Do try it out! If you get some silly, entertaining outputs, we’d love to see them! Drop them in the comments or tag Soham and me on Twitter at [at]dragonbornmonk and [at]radhapyari.

Go get them goblins.

**The D&D μStarter Kit**

## D&D Resources:

## Quantum Resources:

If you want to read more about applying quantum computing to daily life, see How I Use Quantum Computing to Make Tea.

Cover photo by Clint Bustrillos on Unsplash.