Create a Simple Quantum Circuit to Simulate Flipping a Coin

Overview

This article explains how to compose, run and view the results of a simple quantum circuit using IBM’s Quantum Composer.

The circuit will generate a series of 0s and 1s. We’ll assign 0 to indicate an imaginary coin landing on heads and a 1 indicating our imaginary coin landing on tails.

What We’ll Cover

We’ll cover how to:

  • Create a quantum circuit by dragging and dropping gates and operations using IBM’s Quantum Composer;
  • Run the circuit on an IBM quantum computer or simulator.
  • View the results of the circuit.

What’s the Purpose of the Circuit?

The quantum circuit randomly generates a series of 0s and 1s.

We’ll use this stream of 0s and 1s to simulate the flipping of a coin.

When the circuit generates a 0, we’ll consider an imaginary coin that was flipped and landed on heads. When we see a 1 from the circuit, we’ll associate that with the coin landing on tails.

We’ll run the circuit a few times and count the number of 0s and 1s. Then we’ll view the results in a histogram. The histogram will tell us how often our imaginary coin landed on heads versus tails.

We should get about an equal number of 0s and 1s after a sufficient number of runs on a quantum computer or simulator. We assume the quantum circuit randomly generates 0s and 1s.

For example, if we flip a real coin 1024 times, we’d expect to see heads about 512 times and see tails about 512.

Therefore, we’d expect to see about 512–0s and 512–1s for our simple circuit.

How Does it Work?

The circuit contains three items:

Reset Gate

Reset Gate

Hadamard Gate

Hadamard Gate

Measurement Operation

Measurement Operation

Our finished circuit will look like this:

Simple Quantum Circuit

Each time we run the circuit, the following steps occur:

  1. The Reset gate sends a 0 to the Hadamard gate.
  2. The Hadamard gate enters into Superposition.
  3. The Measurement operation determines the state of the Hadamard gate and returns a value, either 0 or 1.
  4. The IBM quantum execution tool counts the number of 0s and 1s.

When we run the circuit a few times we’d expect to see a random series of 0s and 1s: 011000100011111000…

Once we run the circuit a few times, the IBM quantum execution tool generates a histogram of the result.

Please note, for this article, we’re not delving into the quantum physics of the circuit to explain how the circuit randomly generates 0s and 1s. See Quantum Computing in Action by Manning Publications at Manning.com for a detailed explanation.

See the IBM Quantum Documentation for a description of quantum gates and operations.

IBM Quantum Account

If you want to get hands-on and try composing and executing a circuit yourself, you’ll need to create and log into an IBM quantum account. Follow this link to create your account: Create an IBMId Account.

Creating the Circuit

Browse to IBM’s Quantum Computing website.

Log in to your IBM Quantum account. (I assume you’ve created or will create your IBM account on your own.)

Start IBM’s graphical quantum circuit composer by clicking on Launch Composer.

Click on Launch Composer

The site displays the Composer (due to Medium’s limitations on image size, I had to break up the Composer image into smaller images.)

We’ll compose our circuit on the Q0 line in the Composer area:

Quantum Circuit Composer Area

We can name our circuit in the Untitled circuit area.

Below the name box, are the gates and operations we’ll use to create our circuit.

Circuit Name and Quantum Circuit Gates and Operations

We’ll put gates and operations on the composition area.

Circuit operations occur left-to-right. Therefore, we’ll add our gates and operations starting from the left and building to the right.

  1. Drag and Drop the Reset Gate.
  2. Drag and drop the Hadamard gate to the Q0 line to the right of the Reset gate.
  3. Drag and drop the Measurement gate to the right of the Hadamard gate.

Your completed circuit should look like this:

Completed Circuit

Setup and Run the Circuit

Now that we have composed our simple circuit, we run it on an IBM quantum computer or simulator.

Click on Setup and Run in the upper right-hand corner.

Click this Button to Configure and Execute your Circuit

The Composer shows the “Setup and run your circuit” form.

We’ll Setup and Run our Quantum Circuit From This Form.

On the left-hand side below Step 1 and below the search box is a scrollable list of quantum computers: real and simulators.

Scroll through the list of real quantum computers and simulators.

We see the ibma_manila real quantum computer has 446 jobs. If we select this job, we’ll wait a long time for our simple circuit job to run.

We look further down the list of quantum computers and discover the simulator_stabilizer. We see it has one pending job.

We expect our circuit job to run very soon or immediately after starting it due to the small number of jobs.

Select the simulator_stabilizer quantum computer.

Let’s look at the right-side under Step 2.

We leave the Provider as “ibm-q/open/main.”

Next, we want to select the number of shots or times the quantum computer will execute the circuit.

We’ll explain the shots count in a future article or video.

For now, let’s leave it set to 1024.

Now we’re ready to run our simple circuit.

Click on Run on simulator_stabilizer in the lower right-hand corner.

Click to Run Your Circuit

The Composer Jobs section lists your quantum circuit jobs.

Red Arrow Points to Composer Jobs Icon

Click on the Composer Jobs icon.

List of Quantum Circuit Jobs

We see our job completed. Occasionally, you may need to wait a little bit for the quantum computer or simulator to run your job.

Click on the job. The Composer expands to show the job details.

Quantum Circuit Job Details

We’re interested in the results of the circuit.

So, click on Result — Histogram.

Quantum Circuit Job Result Histogram

When we measure the Hadamard gate, sometimes the result will be a 0, and sometimes the result will be a 1.

This histogram shows us the number of times we measured a 0 and the number of times we measured a 1 from the Hadamard gate.

We expect the number of times we measure 0 to be 50% of 1024 or 512 and the number of times we measure 1 to be 50% of 1024 or 512.

Our job looks like we measured a 0 about 500 times, and the gate measured 1 about 524 times.

We add 500 + 524, we get 1024.

1024 is the number of shots we selected to run our circuit.

Summary

We composed a circuit to randomly generate a string of 0s and 1s, similar to flipping a coin, running the circuit job on a quantum simulator, and viewing the results in the histogram.

Beginning quantum computing students, can quickly learn how to build simple quantum circuits, run the circuits on a real or simulator quantum computer and see the results using IBM’s Quantum Composer.

If you have any questions, please reply to this article.

Next: Creating and running a quantum circuit to simulate throwing a single die or pair of dice.

--

--

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