Quantum Computing Assignment - "Guess" Game

“Not only is the Universe stranger than we think, it is stranger than we can think.”
― Werner Heisenberg

Quantum Computing Assignment

  • flip a coin
  • guess heads or tails
  • explain who wins

A Beginner's Guide to Quantum Computing (10:04)

A quantum computer isn't just a more powerful version of the computers we use today; it's something else entirely, based on emerging scientific understanding -- and more than a bit of uncertainty. Enter the quantum wonderland with TED Fellow Shohini Ghose and learn how this technology holds the potential to transform medicine, create unbreakable encryption and even teleport information.

Shohini Ghose: https://www.wlu.ca/academics/faculties/faculty-of-science/faculty-profiles/shohini-ghose/index.html

How To Win A Quantum Coin Flip

This paper, “Playing with Quantum Computers” (https://arxiv.org/pdf/2108.06271.pdf) talked about the difficulty of learning quantum computing. A great way to cover basic concepts is using this game. We’re going to cover the game of “quantum penny flip” where Alice will always win. It’s a great introduction to basic quantum gates and matrix math as well as thinking “quantumly”.

We’re going to use the IBM Quantum experience here for demonstration of the gates: https://quantum-computing.ibm.com/

Unfortunately, this lab has been removed.  

The steps of Quantum Coin Flip are:

  1. Alice prepares her Coin in some state in a box where no one can see it
  2. Bob decides if he will flip the coin - apply an X gate, or don’t apply any gate
  3. Alice applies any gate
  4. We measure the outcome

Now remember this coin is a qubit, it’s not like you can rub it and see if the coin is up or down. So the fun part of this. Is there a way that Alice can always win the game of Quantum Coin Flip? Turns out - yes! We can make it so that the initial state of the qubit will always be the final outcome, no matter what Bob does. The trick is that while Bob can only bit flip the qubit, Alice can use any qubit that she wants - including the Hadamard gate.

This Hadamard gate is one of the key pieces of quantum information science. You apply the Hadamard gate to a qubit to put the qubit in a perfect superposition (like a fair flip of the coin), which means that a measurement will have equal probabilities to become 1 or 0. The winning strategy for Alice is to use the Hadamard gate in her preparation, and the Hadamard gate for her second move. This means she will always win, whether Bob flips the coin using the X gate, or uses the identity gate (remains unchanged). 

This is a good demonstration of how quantum circuits work. We start with the preparation steps, then do operations, and then finally measure the qubit. Of course if we measure the qubit before the end, superposition is destroyed.

Python Code for the Coin Flip Game

Quantum Coin Game:
Access to the code has been restricted.  

 

However, for information purposes only, this would have been the code.  

==================================

# Import all the necessary libraries
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 the screen
button_p = widgets.Button(
    description='Play the Game!!!')
gate_p = widgets.Dropdown(
    # options=[('Identity Matrix', 'i'), ('Bit Flip', 'x')],
    options=[('Heads', 'i'), ('Tails', 'x')],
    description='Select: ',
    disabled=False,
)
out_p = widgets.Output()
def on_button_clicked(b):
    with out_p:
        
        # Initialize the Quantum 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 the Quantum Circuit  
        qc_p.measure(circuit_p, measure_p)
        
        # QASM (Quantum Assembly Language)
        backend_p = Aer.get_backend('aer_simulator')
        job_p = execute(qc_p, backend_p, shots=8192)
        res_p = job_p.result().get_counts()
        # print(res_p)
        
        # Print the Result
        if len(res_p) == 1 and list(res_p.keys())[0] == '0':
            print("You Lose to the Quantum Computer. The Quantum Computer Wins")
        if len(res_p) == 1 and list(res_p.keys())[0] == '1':
            print("You Win against the Quantum Computer")
        if len(res_p) == 2:
            print("Either Quantum Computer or You Win!")

# Run the game by clicking on the button
button_p.on_click(on_button_clicked)
widgets.VBox([gate_p, button_p, out_p])