Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Learn Quantum Computing with Python and IBM Quantum
  • Toc
  • feedback
Learn Quantum Computing with Python and IBM Quantum

Learn Quantum Computing with Python and IBM Quantum

By : Robert Loredo
close
Learn Quantum Computing with Python and IBM Quantum

Learn Quantum Computing with Python and IBM Quantum

By: Robert Loredo

Overview of this book

IBM Quantum Lab is a platform that enables developers to learn the basics of quantum computing by allowing them to run experiments on a quantum computing simulator and on several real quantum computers. Updated with new examples and changes to the platform, this edition begins with an introduction to the IBM Quantum dashboard and Quantum Information Science Kit (Qiskit) SDK. You will become well versed with the IBM Quantum Composer interface as well as the IBM Quantum Lab. You will learn the differences between the various available quantum computers and simulators. Along the way, you’ll learn some of the fundamental principles regarding quantum mechanics, quantum circuits, qubits, and the gates that are used to perform operations on qubits. As you build on your knowledge, you’ll understand the functionality of IBM Quantum and the developer-focused resources it offers to address key concerns like noise and decoherence within a quantum system. You’ll learn how to monitor and optimize your quantum circuits. Lastly, you’ll look at the fundamental quantum algorithms and understand how they can be applied effectively. By the end of this quantum computing book, you'll know how to build quantum programs and will have gained a practical understanding of quantum computation that you can apply to your business.
Table of Contents (18 chapters)
close
14
Other Book You May Enjoy
15
Index

Creating a quantum circuit with the Quantum Composer

Now that we know where everything is in the Composer, we will create our first quantum circuit. This will help you to get a better understanding of how all these components work together, and it will show you how these components provide insights such as the current state and its probabilistic estimation as you build your first quantum experiment.

Building a quantum circuit with classical bit behaviors

We are all familiar with some of the basic classic bit gates such as NOT, AND, OR, and XOR. The behavior that these classic gates perform on a bit can be reproduced on a quantum circuit using quantum gates. Our first experiment will cover these basic building blocks, which will help you to understand the correlation between quantum and classical algorithms.

Our first experiment will be to simulate a classical gate, specifically a NOT gate. The NOT gate is used to change the value of the qubit, in this case from the |0〉 state to the |1〉 state, and vice versa. We will cover details on how this gate operates on qubits in Chapter 6, Understanding Quantum Logic Gates.

To simulate a NOT gate on a quantum circuit, follow these steps:

  1. From the open Composer editor that you previously created and titled MyFirstCircuit, reduce the number of qubits and classical bits down to just one of each if you have not already. This will simplify the visualization of the results for us. You may have to reopen the other views such as qsphere. to get the updated changes.
  2. Next, click and drag the NOT gate, which is visually represented by the symbol, from the list of gates down onto the first qubit, as shown in the following screenshot:
Graphical user interface, application

Description automatically generated

Figure 2.12: Add an X (NOT) gate to the first qubit

  1. Next, click and drag the measurement operation onto the first qubit, q0, just after the NOT gate:
Graphical user interface, application, Word

Description automatically generated

Figure 2.13: Add a measurement operator to the first qubit

  1. By taking a measurement of the qubit and having its value sent out to the pertaining classic bit, we are essentially reading the state of the qubit. You can see this by the connecting arrow between the measurement operator and the classical bit. It also includes the index of the classical bit, the result of which the measurement operator will write out, which in this case is the bit in position 0.

Note that the result bits, similar to the qubits, will be ordered from left to right: c2c1c0, for example.

A measurement occurs when you want to observe the state of the qubit. What this means is that we will collapse the state of the qubit to either a 0 or a 1. In this example, it is straightforward that when we measure the qubit after the NOT gate, the reading will be 1. This is because since the initial state is set to 0, applying a NOT gate will flip it from 0 to 1.

Before we run this experiment, let’s note a few things. First, note that the classic bits are all on one line. This is mostly to save space. Next is to note that all the views are updated as we add gates and operators. As mentioned earlier, this is the system computing these classically to provide us with an ideal result. We have not yet specified which quantum computer to run this circuit on, hence the results you are seeing are what the classical system is computing and not real-time results from a quantum computer.

  1. Select the Setup and run button located at the top right of the Composer view. This will display the run settings, illustrated as follows:

    A screenshot of a computer

Description automatically generated

    Figure 2.14: The run settings view

  2. The run dialog prompts you to take two steps:
    • First, select which quantum system you would like to run the experiment on. Select any of the options you wish to run. In this example, we’ll select ibm_brisbane.
    • The second step first allows you to select the Provider. There are different providers—ibm-q/open/main is for open free quantum devices, and if you are a member of the IBM Quantum Network then you’ll have a provider that assigns you to the available premium quantum devices.

    For now, leave it at the default setting. This step also prompts you to select a number of Shots of the quantum circuit you wish to run. What this means is how many times you wish the quantum circuit to run during your experiment to obtain a reliable overall result. For now, let’s set it to 8000.

  3. Now that you have selected your run options, let’s run the circuit. Click Run on ibm_brisbane. If you selected a different device, it will indicate it accordingly.

Once your experiment begins, you should see an entry of this experiment in the Composer jobs view in the left panel on the Composer view, indicating that your experiment is Pending. While the job is Pending, it will display the status of the job, accordingly.

Note that depending on how busy the selected device is, you may have to wait for some time for your job to complete.

Once completed, you will see the status for the specified job as Completed, illustrated as follows:

Graphical user interface, text, application, chat or text message

Description automatically generated

Figure 2.15: The Composer jobs view displaying the job status for the selected circuit

  1. Upon completion, open your experiment from the list by clicking on the job. This opens the Jobs results view:
Graphical user interface, text, application

Description automatically generated

Figure 2.16: The Jobs results view

  1. Once you have the job opened, you can see some basic information about the job, such as the job ID across the top, followed by the date and time the job was completed, the backend it was run on, and three views that contain details about the job itself, such as the status, details, and results. You will also see a button at the top right that will provide the same information in the views, only in a separate window. Let’s review the views next.

First, we have the Status timeline view, as illustrated in Figure 2.17:

A screenshot of a phone

Description automatically generated

Figure 2.17: The jobs status timeline view

Here you can see the timeline that represents the time it took to complete your circuit. Each step represents the different processes that your circuit completes as it is executed on the quantum system:

Creatinged: The date and time the job instance was added to the queue to run on a specific quantum system.

In queue: The length of time your job was in the queue prior to running on the quantum system.

Running: The time it takes from moving out of the queue and running on a quantum system before returning the results back. Time in system is the actual time that the circuit is running on a quantum system, separate from the time it is on the classical components. For example, transforming the circuit from digital to analog and analog to digital is not included in the time in system value.

Completed: The date and time the job had completed running on the quantum system.

Next is the Details view, as seen in the following figure, which provides you with the details of the job; in this case it was sent from MyFirstCircuit. It also provides information such as the program, the number of shots, the number of circuits, and the instance. The instance is the provision of the quantum system; since we are using open free devices, this is categorized as an open system.

If you are a premium user, you will likely run in a mode specific to your provider, details of which you can obtain from your administrative provider.

A screenshot of a computer

Description automatically generated

Figure 2.18: The Details view

Finally, the Result – histogram view, illustrated in Figure 2.19, shows you the results of your experiment as rendered on a histogram.

A screenshot of a graph

Description automatically generated

Figure 2.19: Job results – histogram view

In this view, the x axis represents the frequency of each state that resulted after each shot of your circuit. The y axis represents each state that had a result.

All these views can be seen on a separate page altogether by clicking the See more details button, located at the top right of the report. This will provide the same details regarding your experiment, plus it will include the transpiled circuit diagram. The transpiled diagram will show you the same circuit, only it will use the basis gates of the specified quantum system. We will cover what basis gates are and how they are transpiled into the circuit in a later chapter. For now, think of it as a circuit using gates that are specific to the quantum system, as illustrated in the following screenshot:

A screenshot of a computer

Description automatically generated

Figure 2.20: The Details view with the original circuit (left) and the transpiled circuit (right)

The diagram of the circuits is just one of the three representations of the circuit. The other two tabs will display the Qasm and Qiskit representations. Keep in mind that depending on the size of the device that you ran this on, you may see all qubits listed (which could range over 100 qubits). In this case I truncated the view so you only see a few qubits to save space.

Now that we have the results from running our first quantum circuit, let’s take a closer look at our results and see what we got back.

Reviewing your results

The histogram result in Figure 2.19 provides information about the outcome of our experiment. Some parts might seem straightforward, but let’s review the details. It may seem trivial now, but later when we work on more elaborate quantum algorithms, understanding the results will prove invaluable.

There are two axes to the results. Along the y axis, we have all the possible states (or measurement outcomes) of our circuit. This is what the measurement operation observed when measuring the qubit. Recall that we measured the first qubit, so from the least significant bit (on the far right), q0 is in the right-most position within each possible state result. Therefore, as we add more qubits, they are appended to the left of the previous qubit. For example, a three-qubit system would be set in the following order, q2, q1S, q0. We know that our likely result of is correct due to the fact that we placed a NOT gate on the first qubit, which changes its state from 0 to 1. If we were to add two more qubits, then the second and third qubit would simply take a measurement that equates to measuring the initial state, which we know to be 0, creating a likely result of .

The x axis provides the results for each of the possible states. Since we ran the experiment 8000 times, the results show that we a have very high chance of the first qubit resulting in the state of 1. The reason why the result is not 100% is due to noise from the quantum device. We will cover the topic of noise in later chapters, but for now we can be confident of a high probability, based on our results, that the NOT gate worked.

In this section, we simulated a simple NOT gate operation on a qubit and ran the circuit on a quantum device.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete