Quantum RPG Character Generator (Partial)

When I first learned about cloud-based quantum computing, my mind was blown. And even after working with real quantum computers for a while, the notion of quantum gaming has been even more mindblowing. Quantum computers are still so limited in what they can do, what kind of games could you possibly play with them?

Well, the first ever quantum game was a variation of Rock-Paper-Scissors. At its core, it’s a random number generator. While that’s not going to win any awards from the gaming industry, it got me thinking about gaming and random number generation. I’m old, and so the first thing that popped into my mind was Dungeons & Dragons.

Back in the day, when you started to create a new D&D character, you randomly determined your character’s base attributes by rolling three six-sided dice. You would roll these three dice six times and generate base strength (STR), dexterity (DEX), intelligence (INT), wisdom (WIS), constitution (CON), and charisma (CHA) scores ranging from 3, the lowest, to 18, the highest.

Conveniently, in binary, 4 bits can range from a low of 0000 (decimal 0) to a high of 1111 (decimal 15). If you simply add 3 manually, then measuring 4 random bits will give you from a low of 3 to a high of 18. Perfect. All I need is four quantum bits (qubits).

So, for a proof-of-concept, I decided to roll some quantum dice six times, and that would give me values for a new character’s base attributes. It is important to note that you can easily modify the following code to run on a real quantum computer, but I ran it on a simulator because I don’t need to know the probabilities of each outcome (each outcome has a 1/16 probability).

Two key points before getting to the Python code:

  • You will need an IBM Q Experience account to actually run this, but registration is free
  • Running this on a real quantum computer is true random number generation; you can’t possibly predict how electrons are going to behave

from qiskit import Aer, ClassicalRegister, execute, QuantumCircuit, QuantumRegister

q = QuantumRegister(4) # initialize 4 quantum registers (qubits)
c = ClassicalRegister(4) # initialize 4 classical registers to measure the 4 qubits
qc = QuantumCircuit(q, c) # initialize the circuit
backend = Aer.get_backend('qasm_simulator') # modify this line to run this code on a real quantum computer

print("Quantum RPG Character Generator (Partial)") # this is just a proof-of-concept partial generator

attributes = ("STR", "DEX", "INT", "WIS", "CON", "CHA") # someone played Dungeons & Dragons as a kid... might've been me....

i = 0
while i < 4:
qc.h(q[i]) # put all 4 qubits into superposition states so that each will measure as a 0 or 1 completely at random
i = i + 1

for i in range(6):
qc.measure(q, c) # collapse the superpositions and get 4 random digits
m = str(execute(qc, backend, shots=1, memory=True).result().get_memory()) # store the 4 digits in a variable so they can be manipulated
diceroll = str((int(m[2])*8) + (int(m[3])*4) + (int(m[4])*2) + (int(m[5])*1) + 3) # use the digits as a binary of length 4 and convert it to decimal with a range of 0-15; simulate a 3d6 dice roll by adding 3, giving a range of 3-18
print(attributes[i] + ": " + diceroll)

1 Comment

Leave a comment