My daughter loves playing Tic-Tac-Toe, it's simple, quick, and endlessly fun. Watching her strategize made me wonder: what if we could add a quantum twist to the game? Could we take this classic game and make it probabilistic, unpredictable, and, in a way, "truly quantum"? That curiosity led to Quantum Tic-Tac-Toe, a CLI game where cells can exist in a superposition, and your moves can collapse into deterministic ownership. You can find the source code here.
Why Quantum Tic-Tac-Toe?
Quantum computing is fundamentally about probabilities and superpositions. A qubit can be 0, 1, or both at the same time…until it's measured. I wanted a simple, interactive way to demonstrate this idea, and a well-known, yet simple game like Tic-Tac-Toe seemed perfect.
Empty cell: available.
Deterministic cell: a player owns it.
Superposed cell: a quantum "maybe," indicated with a ?.
Every move has a chance to push a cell into superposition, and certain actions collapse the board, assigning ownership based on quantum measurements.
Building the Game
I implemented the game in Python using Qiskit. This was really overkill for what I was building, but I wanted to incorporate some concepts specific to the qiskit library here. Each superposed cell is represented by a qubit. We tried a few different approaches before finally settling on the logic presented below. Our original approach was to maintain superpositions indefinitely. This made it unbelievably difficult for anyone to win, as all superposed cells had to collapse in just the right way. We could have made some adjustments to make this more likely, but ultimately, we decided on a simpler gameplay.
The core logic is simple:
First move on empty cell: deterministic ownership.
Move on already owned cell: push it into superposition.
Collapse: simulate a quantum measurement for each superposed cell to determine ownership.
For the first version, I used a single-shot collapse. This means that each cell in superposition is measured once, and the result immediately determines the owner.
def collapse_superposition(self):
superposed_indices = self.get_superposition_cells()
if not superposed_indices:
return
print(f"\nCollapsing {len(superposed_indices)} superposed cells using independent quantum measurement...")
simulator = AerSimulator()
for cell_idx in superposed_indices:
# Build a separate circuit for each cell
qc = QuantumCircuit(1, 1)
qc.h(0) # put qubit in superposition
qc.measure(0, 0)
# Execute with 100 shots
compiled_circuit = transpile(qc, simulator)
job = simulator.run(compiled_circuit, shots=100)
result = job.result()
counts = result.get_counts(compiled_circuit)
measurement = list(counts.keys())[0]
clean_measurement = measurement.replace(' ', '')
bit_value = int(clean_measurement)
owner = 'X' if not bit_value else 'O'
self.board[cell_idx] = {'state': 'deterministic', 'owner': owner}
print(f"Cell {cell_idx} collapsed to: {owner}")
After the measurement, X
or O
is assigned deterministically based on the outcome.
Findings from the Single-Shot Approach
Playing the game with the single-shot collapse revealed some interesting behaviors:
Highly unpredictable outcomes: because each collapse only measures once, the board can swing dramatically from turn to turn.
Harder to plan strategy: you might try to push a cell into superposition to gain a probabilistic advantage, only for it to collapse against you immediately.
Fun emergent patterns: watching how a cell "flips" from superposition to deterministic ownership captures the spirit of quantum uncertainty beautifully.
In short: single-shot collapses are exciting, chaotic, and very "quantum-like."
Multi-Shot Collapse: A More Scientific Approach
For those wanting a more statistical quantum simulation, I also implemented a multi-shot collapse. Here, each cell is measured 100 times, and the majority vote determines ownership.
def collapse_superposition(self): # multi-shot collapse
"""Collapse all superposed cells using independent quantum measurements per cell"""
superposed_indices = self.get_superposition_cells()
if not superposed_indices:
return
print(f"\nCollapsing {len(superposed_indices)} superposed cells using independent quantum measurement...")
simulator = AerSimulator()
for cell_idx in superposed_indices:
# Build a separate circuit for each cell
qc = QuantumCircuit(1, 1)
qc.h(0) # put qubit in superposition
qc.measure(0, 0)
# Execute with 100 shots
compiled_circuit = transpile(qc, simulator)
job = simulator.run(compiled_circuit, shots=100)
result = job.result()
counts = result.get_counts(compiled_circuit)
# Majority vote for this cell
ones = counts.get('1', 0)
zeros = counts.get('0', 0)
owner = 'O' if ones >= zeros else 'X'
self.board[cell_idx] = {'state': 'deterministic', 'owner': owner}
print(f"Cell {cell_idx} collapsed to: {owner}")
This approach leans closer to 50/50 outcomes and reduces the randomness compared to the single-shot version. It's more predictable, and players can start to develop strategies around probability, rather than pure chaos.
Lessons Learned
- Quantum mechanics can be fun and interactive. Using a simple game, you can demonstrate superposition, measurement, and probability without any heavy math.
- Randomness vs. strategy: single-shot collapses feel wildly quantum, while multi-shot collapses introduce a kind of probabilistic predictability.
- Python + Qiskit is accessible: you don't need a real quantum computer to experiment, and simulators like Aer make it easy to explore concepts locally.
Try It Yourself
I've open-sourced the Quantum Tic-Tac-Toe CLI game, so you can try it out and see how quantum unpredictability affects your strategy.
Highlights for experimentation:
- Play using single-shot collapse…expect chaos.
- Try the multi-shot approach, and see how probabilities smooth the outcomes.
- Push deterministic cells into superposition, and watch the quantum dice roll!
Quantum Tic-Tac-Toe isn't just a fun Python project…it's a hands-on introduction to quantum behavior. Whether you're a developer curious about quantum computing, or just someone who wants a quirky new take on Tic-Tac-Toe, this project gives a small taste of how quantum mechanics works in practice.
There's one scenario in the game that I didn't cover, see if you can find the bug!
Top comments (0)