Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/notebooks/ch-demos/coin-game.ipynb
3855 views
Kernel: Python 3

Quantum Coin Game

Introduction

What is Quantum Coin Game ?

Quantum Coin Game is one of the fundamental concept of quantum computing, which uses simple implementation of quantum gates or more precisely uses the wierdness of quantum mechanics, to win about 97% of the time, when played against an opponent. Flipping of coin and say heads or tails.

Where the concept came from ?

The concept of Quantum Coin Game came from the idea of classical coin game which can only show heads and tails. But since the game utilizes the concepts of quantum mechanics, it would be interesting to see what could be the outcome of the whole experiment.

What is the main idea of this game ?

The main concept of this game is how the quantum computer uses the power of quantum superposition, which tells an object can exists in 2 different states at the same time, to win absolutely everytime.

NOTE: To learn more about quantum superposition, link to "Qiskit Textbook" superposition page.

What are the rules of this game ?

  1. Quantum Computer plays a move but it is not revealed to the Opponent(Human).

  2. Opponent(Human) plays a move and it is also not revealed to the Quantum Computer.

  3. Finally Quantum Computer plays a move.

  4. Results are shown. If its heads, then Quantum Computer wins. Else, Opponent(Human) wins.

NOTE: "Playing a move" refers to "Flipping the coin" and we consider the coin as fair coin.

NOTE: Refer to Shohini's Ted Talk

Play it

# Importing all the necessary library from qiskit import QuantumCircuit, Aer, IBMQ, QuantumRegister, ClassicalRegister, execute from qiskit.tools.jupyter import * from qiskit.visualization import * import qiskit.tools.jupyter import ipywidgets as widgets # Layout button_p = widgets.Button( description='Play') gate_p = widgets.Dropdown( options=[('Identity', 'i'), ('Bit Flip', 'x')], description='Choice: ', disabled=False, ) out_p = widgets.Output() def on_button_clicked(b): with out_p: # Initial Circuit circuit_p = QuantumRegister(1, 'circuit') measure_p = ClassicalRegister(1, 'result') qc_p = QuantumCircuit(circuit_p, measure_p) # Turn 1 qc_p.h(circuit_p[0]) # Turn 2 if gate_p.value == 'i': qc_p.i(circuit_p[0]) if gate_p.value == 'x': qc_p.x(circuit_p[0]) # Turn 3 qc_p.h(circuit_p[0]) # Measure qc_p.measure(circuit_p, measure_p) # QASM backend_p = Aer.get_backend('aer_simulator') job_p = execute(qc_p, backend_p, shots=8192) res_p = job_p.result().get_counts() # Result if len(res_p) == 1 and list(res_p.keys())[0] == '0': print("You Lose to Quantum. Quantum Computer Wins") if len(res_p) == 1 and list(res_p.keys())[0] == '1': print("You Win against Quantum Computer") if len(res_p) == 2: print("Either Quantum or You Wins") button_p.on_click(on_button_clicked) widgets.VBox([gate_p, button_p, out_p])
VBox(children=(Dropdown(description='Choice: ', options=(('Identity', 'i'), ('Bit Flip', 'x')), value='i'), Bu…

Analogy

Now that we know what is a quantum coin game, what is it based on and most importantly what are the rules of this game, lets convert the concept of this game in quantum computing terminology.

  • The 'coin' in flipping a coin we referring here is a 'single qubit gate'.

ψ=[αβ]|\psi\rangle=\begin{bmatrix}\alpha \\ \beta\end{bmatrix}

where α,βC\alpha, \beta \in \mathbb{C} and α2+β2=1|\alpha|^2 + |\beta|^2 = 1

  • "Flipping" the coin is application of the bit-flip operator

X=[0110]X = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}
  • The "heads" state is defined as 0=[10]|0\rangle = \begin{bmatrix} 1 \\ 0 \end{bmatrix}

and "tails" as

1=[01]|1\rangle = \begin{bmatrix} 0 \\ 1 \end{bmatrix}
  • The quantum computer "plays" by applying the Hadamard HH operator H=12[1111]H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}

Approach

Lets see how to approach the game in quantum computing terminology-

  • The coin is initialized to the 0|0\rangle "heads" state.

  • The computer plays, applying the Hadamard HH operator to the coin (operators are applied using matrix multiplication). H0=12(0+1)H|0\rangle = \frac{1}{\sqrt2}(|0\rangle + |1\rangle)

The coin enters the H0=+=12[11]H|0\rangle = |+\rangle = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 \\ 1 \end{bmatrix} state.

  • The human plays, choosing whether to flip the coin (apply the XX operator) or do nothing (apply the II operator). However, since the XX operator just flips the state vector upside down, XX has no effect. Same goes for II.

X+=+X|+\rangle=|+\rangleI+=+I|+\rangle=|+\rangle

No matter what, the state is +|+\rangle after the human plays.

  • The computer plays, applying the Hadamard HH operator again, taking the coin to the 0|0⟩ "heads" state. H+=0H|+\rangle = |0\rangle

# Importing all the necessary library from qiskit import QuantumCircuit, Aer, IBMQ, QuantumRegister, ClassicalRegister, execute from qiskit.tools.jupyter import * from qiskit.visualization import * import qiskit.tools.jupyter import ipywidgets as widgets
# Building the initial circuit def initial_circuit(): circuit = QuantumRegister(1, 'circuit') measure = ClassicalRegister(1, 'result') qc = QuantumCircuit(circuit, measure) qc.draw('mpl') return qc, circuit, measure
# Widget Initialization gate = widgets.Dropdown( options=[('Identity', 'i'), ('Bit Flip', 'x')], description='Choice: ', disabled=False, )

Optimal Strategy

Using the above approach the possibility table reduces to-

Start State Quantum Classical Quantum Result Who Wins?
0|0\rangle HH II HH 0|0\rangle Quantum
0|0\rangle HH XX HH 0|0\rangle Quantum

Now lets look at the possibilities-

  1. Quantum Computer Wins ( 0|0\rangle ):

22=100%\frac{2}{2} = 100 \%
  1. Classical Human Wins ( 1|1\rangle ):

02=0%\frac{0}{2} = 0 \%
  1. Either Quantum Computer or Classical Human Wins ( 0+1|0\rangle + |1\rangle ):

02=0%\frac{0}{2} = 0 \%

This table shows the quantum computer wins 100%100\% of the time. But in Shohini's talk it is  97%~97\%, due to errors.

Lets play this version using Qiskit

Building the initial circuit

qc, circuit, measure = initial_circuit()

Turn 1. Quantum Computer

# Use H Gate qc.h(circuit[0]) qc.draw('mpl')
Image in a Jupyter notebook

Turn 2. Classical Human

if gate.value == 'i': qc.i(circuit[0]) if gate.value == 'x': qc.x(circuit[0]) qc.draw('mpl')
Image in a Jupyter notebook

Turn 3. Quantum Computer

Quantum Computer uses Hadamard HH on its first turn

# Used H Gate qc.h(circuit[0]) qc.draw('mpl')
Image in a Jupyter notebook

Measurement

qc.measure(circuit, measure) qc.draw('mpl')
Image in a Jupyter notebook

QASM_Simulator

backend = Aer.get_backend('aer_simulator') job = execute(qc, backend, shots=8192) res = job.result().get_counts() print(res) plot_histogram(res)
{'0': 8192}
Image in a Jupyter notebook

Lets see who wins

if len(res) == 1 and list(res.keys())[0] == '0': print("Quantum Computer Wins") if len(res) == 1 and list(res.keys())[0] == '1': print("Classical Human Wins") if len(res) == 2: print("Either Quantum Computer or Classical Human Wins")
Quantum Computer Wins

Running on Quantum Computer

provider = IBMQ.load_account() backend_real = provider.get_backend('ibmq_manila') job_real = execute(qc, backend_real, shots=8192) res_real = job_real.result().get_counts() print(res_real) plot_histogram(res_real)
{'0': 8134, '1': 58}
Image in a Jupyter notebook

Unlike the perfect simulation, the real quantum computer only wins ~99 %99\ \% of the time, the 1 %1\ \% in which it loses is due to errors. Quantum computers have improved a bit since Shohini's talk where the error is closer to 3 %3\ \%.

Conclusion

This simple and yet fun little game shows the basic quantum states 0|0\rangle, 1|1\rangle, +|+\rangle and |−\rangle, plus the common ways of moving between them with the XX, HH, II, ZZ gates.

References

This notebook is inspired from:

Quick Exercise

The rules of the game we learned so far are the main rules of the game. But, think of other variations of the game as well, tweak the game a little could result in significant change in answer. Such as-

  1. What if, instead of quantum computer taking first turn, the classical human take the first turn ?

  2. What if, instead of representing head as 0|0\rangle, the tail is represented as 0|0\rangle ?

  3. What if, instead of using fair coin, we used unfair coin ?

  4. What if, instead of playing against a classical human, the quantum computer plays with another quantum computer ?

  5. What if, instead of having 3 turns, there are nn number of turns ?

  6. What if, instead of using all gates, restrict the use of some gates ?

and many more variations are possible.

Version Information

%qiskit_version_table