How I used a Quantum Computer to decide what housemate gets the nicest room in the flat.

Framing the Problem

It’s a huge debate every year for students across the globe. They’re moving in together and excited for what cute movies they’ll watch together, the crazy nights out, having to deal with an awful landlord and building up great friendships. However, there’s one discussion everyone is scared of. Every house and flat has it. One of the rooms is nicer than the rest.

What’s the fairest way to decide who gets it?

Most people draw straws. Some housemates might decide that they REALLY like the bigger room and they offer to pay more rent than everyone else for it. Some groups decide to just pick a number between 1 or 10, or if they’re fancy they use a random number generator from online.

We have decided the fairest way to decide who gets the nicest room is by using a Quantum Circuit with 15 qubits to generate a random number 10 times for each of us, then take the mean value of said list of random numbers. Whichever housemate gets the highest average gets the best room!

There’s three housemates this year:

  • Myself
  • My Wonderful Boyfriend Matthew
  • My Ex-And-Soon-To-Be-Again dance partner Megan!

Read on to find out who wins!


Let’s Dive in to the Methodology!

First things first, we need to import all of the code we need.

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

IBMQ.load_account()
# In IBMQ Experience we don't need to set up our account

provider = IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_16_melbourne')
simulator = Aer.get_backend('qasm_simulator')

Our Quantum Computer in this tutorial is the IBMQ_16_Melbourne computer. We use this backend when we have tested our circuit locally on a simulator (provided by Qiskit Aer) and we know it works.

The circuit we implemented is as follows:

Fig. 1 – Our 15 Qubit Circuit

In Qiskit, the code looks like this:

q = QuantumRegister(15,'q')
c = ClassicalRegister(15,'c')
circuit = QuantumCircuit(q,c)

circuit.h(q) # Applies hadamard gate to all qubits
circuit.measure(q,c) # Measures all qubits

Basically what this code does is create our qubit register and define a register to read the results of our circuit. Next, we apply a Hadamard Gate to all qubits to place them in a superposition of the states |0> and |1>. When we measure these qubits, each of them randomly collapse with the value of either 0 or 1. When we put all 15 of them together, it gives us a binary number such as 101011110000001!

Next up, we define a dictionary containing each housemate. We execute this circuit for each housemate and then each housemate gets a random number!

housemates = {'Mark' : 0,
              'Matt': 0,
              'Megan': 0}

for name in housemates:
    job = execute(circuit, simulator, shots=100)            
    result = job.result()
    counts = result.get_counts(circuit)

    x = list(counts)
    y=[]
    for element in x:
        y.append(int(element,2))
    
    housemates[str(name)] = sum(y)/len(y)

As you can see above we execute the circuit using a simulator as our backend. When we are confident it works, we can actually send it to a real quantum computer with the line

job = execute(circuit, qcomp, shots=100)

This executes the circuit and adds it to the queue for the real computer. One circuit is executed for each person!

for housemate in housemates:
    print(housemate, housemates.get(housemate))

Now we get our results!

Mark 16567.3
Matt 17602.14
Megan 15017.242424242424

Sadly, I didn’t win. But there you have it! That’s how you use the crazy world of Quantum Mechanics and its truly random nature to your advantage and make a fair competition!

Hopefully you enjoyed this blog post.

One last thing:

I’m interesting in pursuing a PhD in Quantum Computing. If you are someone looking to supervise a student after Summer 2021, please contact me at mcunningham0410@gmail.com! I’d also really appreciate if you passed my name on to anyone else you know interested in supervising a PhD student in the field. Thank you!

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close