Creating a quantum circuit using Quantum Lab Notebooks
Quantum Lab Notebooks provided to you via the IBM Quantum Experience platform will help you generate robust experiments that allow you to create quantum circuits and integrate those circuits with classical experiments or applications. Quantum Lab Notebooks generally contain a set of cells that you can use to write, test, and run your code in each cell individually.
You can also include Markdown language in the cells to capture any notes or non-code content, to help keep track of your learning or project. In this section, we will recreate the same quantum circuit you completed in Chapter 2, Circuit Composer – Creating a Quantum Circuit, only this time you will be using the Qiskit Notebook. So, let's get started!
Launching a Notebook from the Quantum Lab
To create a quantum circuit, let's start by launching the Quantum Lab Notebook from the Quantum Lab view. From the left panel under Tools, select Quantum Lab to launch the view, as illustrated in the following screenshot:
Now that you have the Quantum Lab view open, let's take a look at what each component of the Notebook provides.
Familiarizing yourself with the Quantum Lab components
In this section, we will become familiar with each of the components that make up the Quantum Lab view. As you see in Figure 3.2 (starting from the top section, where you can see there are quick links to the Qiskit tutorials), the quick links are grouped into three sections, as follows:
- The first one is for starters, titled 1_start_here.ipynb. This will review the introductory functions and features of Qiskit.
- The second group contains more advanced level tutorials.
- The third contains tutorials specific to certain fundamentals such as optimization, artificial intelligence, and many other domains.
Under the quick links is the list of all previously saved notebooks. You can choose to open any of those listed, or you can create or import notebooks by selecting either the New Notebook + or Import button respectively, as illustrated in the following screenshot:
In the next step, we will create a new Notebook.
Creating a new Notebook
In this section, we will review the various functionalities available to ensure that you have a good understanding of all the different features available to you.
In the following screenshot, we can see the landing page of the Circuit Composer editor view:
The following points provide a description of the functions and features and what they contribute to the creation of a quantum circuit:
- When the Notebook loads up, you'll notice the first cell contains autogenerated code that includes some from Qiskit. Qiskit will be discussed in detail in Chapter 7, Introducing Qiskit and its Elements.
The autogenerated code functions help to get your code up and running by adding some libraries and objects that are common when creating and running a quantum circuit. We'll review details of these objects further so that you can understand what each line pertains to and what the objects are generally used for.
Important note
Note that these may change as new features are added or updated to Qiskit, so the content of these lines may alter over time.
- The following lines of code, which can also be seen in the preceding screenshot, contain the most commonly used objects from the Qiskit library and the code for loading of your account details so that you can connect to the quantum systems.
This is the first line of the autogenerated code block in your Notebook:
%matplotlib inline
The preceding code imports the Matplotlib plotting library, which provides the ability to embed plots and publication quality figures into applications. Details about Matplotlib can be found on their home page here: https://matplotlib.org/
The next line imports four Qiskit objects that are commonly used to create and run a quantum circuit. QuantumCircuit is used to create a new circuit, which is a list of instructions bound to some registers. execute is an asynchronous call to run a circuit and return a job instance handle. Aer and IBMQ are providers for backend simulators and devices and to manage account details, respectively.
In the following code snippet, you can see we import each of these from the qiskit package:
from qiskit import QuantumCircuit, execute, Aer, IBMQ
transpile and assemble are compiler objects used to translate and compile circuits, while assemble provides a list of circuit schedules, as shown in the following code snippet:
from qiskit.compiler import transpile, assemble
- The following lines in the autogenerated block of code import all tools and objects from the Jupyter Notebook and visualization libraries, respectively. Jupyter Notebooks is what the Qiskit Notebook is built upon, so it will leverage existing features already familiar to those of you who compose experiments on a Jupyter Notebook.
These features include creating new files, running kernels, and triggering cells. The visualization library includes many features used to visualize results from experiments such as histograms and bar charts, and in various formats, including Matplotlib, Latex, and so on. The code can be seen here:
from qiskit.tools.jupyter import *
%from qiskit.visualization import *
- And finally, the IBMQ.load_account() function loads your account information, particularly the application programming interface (API) token that was assigned to you when you initially registered. This is done if you desire to run an experiment on an actual device; the loading of your API token and other account information needed to run an experiment is already available without any extra work on your part. The following code snippet shows this:
provider = IBMQ.load_account()
This way, the content of your experiment is not cluttered with information that is not relevant to your experiment and conclusions.
Now that we are familiar with the autogenerated code, we'll take a quick look at the Qiskit Notebook itself. You'll note that its layout is very similar to that of a Jupyter Notebook, so those who are already familiar with Jupyter Notebooks will undoubtedly recognize the layout.
Learning about the Qiskit Notebook
For those who are new to the Qiskit Notebook, there are a couple of things to note that will help you understand how coding and running your code work. Those of you who are already familiar with Jupyter Notebooks can skip this section.
The Quantum Lab Notebooks run code one cell at a time. As shown in the following screenshot, a cell is a section of the Notebook that can contain text, metadata, and source code, such that it encapsulates the autogenerated code we looked at earlier. It simplifies coding by breaking up the code into these cells. The cells can be run individually by selecting the cell with your mouse and clicking the Run button, as illustrated in the following screenshot:
From the preceding screenshot, in the top row of the Qiskit Notebook menu options you will see some usual operations you would find in a typical document editor, as follows:
- File: Save, Checkpoint, Print Preview, Close, and Halt
- Edit: Modify Cells, Move Cells, Merge Cells, and so on
- View: Toggle Headers, Toolbars, and other views
- Insert: Insert cells above or below selected cell
- Cell: Run cell, run all cells, and more
- Help: Provides support content
There is one specific operation in the Notebooks menu list to take note of, and that is the Kernel. For those of you with an existing version(s) of Python, do take note that Qiskit, at the time of this writing, is running on Python version 3.
To confirm this, you can select Kernel from the drop-down menu and note that there is a Change Kernel option. You will see Python 3 as the only option. However, if you install Qiskit on your local machine that contains other versions of Python, you might see them listed here as well. I mention this so as to ensure you have the correct kernel selected to run Qiskit experiments. Otherwise, you may encounter some errors due to version incompatibility.
Another thing to note is related to the various different formats in which you can download a Qiskit Notebook. By selecting the File | Download as option you will see the various formats, as shown in the following screenshot:
Up to now, you should be familiar with the functionality and features available on the Qiskit Notebook. These are features that make it easy to share experiments and make quick changes to them. We can now start creating and running quantum experiments on our notebooks using Qiskit.
Opening and importing existing Quantum Lab Notebook
Oftentimes, we wish to share our experiments with others and run others' experiments ourselves. Importing a Qiskit Notebook is easy in that the Quantum Lab home page has a link to Import button, next to the New Notebook + button, as shown in the following screenshot:
This will launch your machine's file dialog to select the Qiskit Notebook you wish to import into your workspace on IBM Quantum Experience (IQX). To open an existing Qiskit Notebook, or one you have just imported, simply go back to your Qiskit Notebook page and select the Notebook you wish to open from the file list at the bottom of the view, as illustrated in the following screenshot:
Now that you are familiar with the Notebooks and the autogenerated code, let's quickly create the same circuit we generated in the previous chapter, only this time we will create it using only the Notebook.
Developing a quantum circuit on Quantum Lab Notebooks
Let's take a quick look at the quantum circuit we created in the previous chapter. For convenience, the circuit is given as follows:
The preceding circuit comprises of two gates—Hadamard and Controlled-Not—and two measurement operations on 2 qubits, respectively. This circuit was very easily constructed on the Circuit Composer; however, as we learn more about quantum circuits and begin to work on more complicated algorithms and circuits, it will be difficult to leverage a user interface such as the Circuit Composer. So, we will code the construction of quantum circuits and algorithms as we move forward instead.
To create the previous quantum circuit on a Notebook, follow these steps:
- From the Quantum Lab Notebook, create a new Notebook and enter the following code in an empty cell:
qc = QuantumCircuit(2,2)
qc.h(0)
qc.cx(0, 1)
The preceding code creates QuantumCircuit. The two parameters pertain to the number of quantum bits (qubits) and classic bits we want to create, respectively. In this example, we will create two of each.
The second line adds a Hadamard gate onto the first qubit. Note that the index values of the qubits are 0 based. The third line adds a Controlled-Not gate that entangles the first qubit (q0) as the control to the second qubit (q1), the target. The parameters in the function pertain to the control and target, respectively.
- Now, select Run to run the cell. Once the cell has completed running, you should see the output display InstructionSet of the results and a new cell generated below the one you just ran, as shown in the preceding code snippet. InstructionSet is a class of instruction collections and their contexts (classic and quantum arguments), where each context is stored separately per each instruction.
Here is the output we are given after running the preceding code:
<qiskit.circuit.instructionset.InstructionSet at 0x7fc632176eb8>
- Next, we will add the measurement operators to our circuit so that we can observe our results classically. Notice in the following code snippet that we are using the range method so as to simplify the mapping of each qubit to its respective classic bit:
qc.measure(range(2), range(2))
- Run the preceding cell to include the measurement operators to our circuit. Now that we have created the circuit and included the same gates and operations we did in Chapter 2, Circuit Composer – Creating a Quantum Circuit, let's draw the circuit and compare. Draw the circuit using the draw function, shown as follows:
qc.draw()
You should now see the following results, after the draw method is complete:
Notice that the preceding circuit is identical to that which you created earlier. The only difference is the initial number of qubits. The Circuit Composer defaults to 5 qubits, whereas here we specified only 2. The circuit will run the same, both on the simulator and on an actual quantum device, since we are only using 2 qubits.
In this section, we have learned to navigate the Quantum Lab Notebook. We also learned how to open an existing Notebook, along with opening, creating, and importing the Notebook. We also saw how to develop a quantum circuit.
Now, we will move on to review the results of the quantum circuit.