
Learn Quantum Computing with Python and IBM Quantum
By :

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.
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:
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.NOT
gate, which is visually represented by the Figure 2.12: Add an X (NOT) gate to the first qubit
NOT
gate:Figure 2.13: Add a measurement operator to the first qubit
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.
Figure 2.14: The run settings view
ibm_brisbane
.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
.
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:
Figure 2.15: The Composer jobs view displaying the job status for the selected circuit
Figure 2.16: The Jobs results view
First, we have the Status timeline view, as illustrated in Figure 2.17:
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.
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.
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:
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.
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.
Change the font size
Change margin width
Change background colour