Flipping a Coin With a Quantum Computer

Anyone with an AWS account can flip a coin using quantum mechanics

Steven Heidel
Aug 25 · 5 min read
Image for post
Image for post
An ion trap quantum computer.

Amazon recently announced its latest AWS offering: Quantum computing as a service. It’s called Amazon Braket (pronounced “bracket”). Last week it moved from a closed beta to general availability.

Here’s how you can run your first quantum program in just a few minutes.

Step 1: Set Up Your Account

The first thing you need to do is create an AWS account if you don’t have one already. Go to https://aws.amazon.com and follow the instructions.

Next, you’ll need to activate Amazon Braket by going to https://aws.amazon.com/braket/ and clicking “Get Started with Amazon Braket.”

This will walk you through some terms and conditions, which you’ll need to agree with to proceed. You’ll also be prompted to create an S3 bucket for storing the results of quantum programs. You’ll also have to set up access rules for the account. Fortunately, there are default settings filled in for you already. I just left every option alone. Make a note of the name of the S3 bucket for later.

Image for post
Image for post

Next, click “notebooks” on the left and create a new notebook instance to run your code. Name it something meaningful and select the “ml.t3.medium” instance type. Keep in mind that this costs money to run. It is included in the AWS free tier, so you may be eligible to run up to 250 hours for free.

There are some other settings for permissions and networking. Again, I just left the defaults.

Step 2: Write Some Code

Click the notebook URL shown on the notebook instance list. The URL will look something like: amazon-braket-[name].notebook.[region].sagemaker.aws

This opens up a Jupyter Notebook environment. Click “New” on the top right then choose conda_braket. (This is the name of the Python virtual environment with all the needed dependencies pre-installed).

Copy and run this code:

If all goes well, you will see a result like:

Counter({'0': 505, '1': 495})

We performed a simulation of running a single-qubit Hadamard gate and then measured the result 1,000 times. The counter shows the number of times the qubit was measured in the ground state ‘0’ and the excited state ‘1’.

The Hadamard gate puts the qubit in a superposition where it will have an equal probability of being measured in either state. This is exactly what we want: a coin flip with a 50/50 chance.

But this example was just simulating a quantum computer. We want to use the real thing!

Step 3: Choose a Quantum Computer

Image for post
Image for post
The three hardware offerings from Amazon Braket, taken from https://aws.amazon.com/braket/.

Amazon offers three different quantum computing devices to choose from:

  1. Gate-based superconducting qubits from Rigetti Computing
  2. Gate-based ion traps from IonQ
  3. Quantum annealing from D-Wave

The third option, quantum annealing from D-Wave, is a different paradigm of quantum computing that won’t work with the above code. So our choice is between using superconducting qubits or ion traps.

I’m going to choose the superconducting qubits from Rigetti since they have a cheaper per-shot rate than IonQ… and because I used to work at Rigetti.

In a new cell, copy the following. Here is where you’ll need the S3 folder from earlier because, unlike the free simulator, you don’t want to lose any results from real hardware. If you forgot the name of the S3 folder, you can look it up back in the AWS console under the S3 header.

We’re reusing the coin_flip_circuit we created in the last step.

If all goes well it should respond with:

'CREATED'

Now in a new cell (do not rerun the previous cell since that will enqueue another task!) keep running task.state() to see the status change to 'QUEUED'.

What’s happened? Your task hasn’t run on the quantum computer yet because it is waiting in line. Also, some of the quantum computers are only accessible during certain times of the day. It takes a lot of work to maintain properly calibrated quantum computing hardware, so it will only be available sparingly. Check the Devices tab on the Amazon Braket AWS console page to see the schedule for each device.

In my case, I was scheduled to wait several hours for the next available slot. If you don’t want to keep my notebook server running for that amount of time, then you can run task.id and make a note of the returned value.

Later, you can run the following in a new cell (or even a new notebook) to recover the task:

Once task.state() returns 'COMPLETED' then you can access the results just like in the local simulator:

Again if all goes well, you’ll see a result like this:

Counter({'0': 593, '1': 407})

If you have the patience to repeat this a few times, you’ll likely notice that the ‘0’s seem to dominate the results. This is not a mistake, but more a reflection that today’s quantum computers aren’t perfectly accurate. In our example circuit, this results in more ‘0’s than ‘1’s.

By the way: To run on the ion traps instead replace device/qpu/rigetti/Aspen-8 with device/qpu/ionq/ionQdevice in the code above. It would be a good idea to reduce the number of shots since running 1000 shots costs 35 cents on Rigetti but 10 dollars on IonQ.

Step 4: Put It All Together

A complete coin flip example could look something like this:

Note: there is a minimum of 10 shots in Braket for some reason, but we only need one coin flip, so we ignore the other 9 shots.

This will (eventually) print either HEADS! or TAILS! depending on the measured state of the qubit.

Make sure to stop your notebook instance once you’re done!

Comparing with a classical coin flip

Here’s my review of the experience using a quantum computer to flip a coin vs. just flipping a physical coin.

Flipping a coin with a quantum computer:

  • 🚫 biased towards tails (although there are ways to work around this)
  • 🚫 costs money each flip
  • 🚫 only available during business hours
  • ✅ cool and quantum

Flipping a physical coin:

  • ✅ fair coin
  • ✅ you need a coin, but you get to keep it afterward
  • ✅ available any time
  • 🚫 boring and classical

Overall, flipping a coin with a quantum computer is neat but not a very good way to decide who goes first in a football game. Nevertheless, quantum computers can still be a good source of randomness through the use of a randomness extractor. Some companies will go to great lengths to get a reliable source of truly random bits.

There are many other uses for a quantum computer, some of which you can find in the “Braket examples” folder on the notebook server. For instance, one notebook shows how to simulate molecule binding energies using the Variational Quantum Eigensolver (VQE) algorithm. There are other examples as well for basic quantum algorithms, quantum teleportation, and quantum optimization. As a next step, you can look through these and run the code there as well against real quantum devices.

Happy quantum computing!

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store