Learn Quantum Computing with Python and IBM Quantum Experience
上QQ阅读APP看书,第一时间看更新

Creating our first quantum circuit

Now that we know where everything is in the Circuit 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 current state and 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 classic algorithms.

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

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

  1. From the open composer circuit that you previously created and titled My First circuit, 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:

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

  2. Next, click and drag the measurement operation onto the first qubit, just after the NOT gate. 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.

    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 pretty 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. Therefore, we expect the measurement to read 1.

  3. Click and drag another measurement operation onto the second qubit. We'll do this just to contrast the difference between what we would see when we measure a qubit in the initial state, and after a NOT gate.
  4. Before we run this experiment, let's note a few things. First, note that the classic bits are all on one line (as shown in the following screenshot). This is mostly to save space. In lieu of having three additional wires where each represents a classic bit, a single wire is used to denote the classic bits. They are labeled c3 to indicate a set of three classic bits:

    Figure 2.13 – Add a measurement operation to the first qubit

    The second thing to notice is that the measurement operations match the qubit number to the classic bit number; in this case, qubit 0 will read out to bit 0, and qubit 1 will read out to bit 1, where bit 0 is the least significant bit.

  5. Select the Run Settings drop-down option located at the top right of the circuit composer view. This will display the run settings, illustrated as follows:

    Figure 2.14 – Run settings drop-down view

  6. The run dialog provides you with three options:

    First, to select which backend device you would like to run the experiment with, the choices are either on a simulator called ibmq_qasm_simulator or on an actual quantum device. Select any of the options you wish to run. In this example, we'll select ibmq_valencia.

    The second option allows you to select the Provider. There are different providers – the open/main is for the open free quantum devices, and if you are a member of the IBM Q Network then you'll have a provider that assigns you to the available premium quantum devices. For now, leave it at the default setting.

    The last option allows you to select how many 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. For now, since this is a simple experiment, let's simply set it to the default value, 1024.

  7. Now that you have selected your run options, let's run the circuit. Click Run on ibmq_valencia. If you selected a different device, it will indicate it accordingly.
  8. Once your experiment begins, you should see an entry of this experiment in the Pending Jobs view to the right of the Composer view. This indicates that your experiment is pending. Once completed, you will see it in the Results view shown as follows:

    Figure 2.15 – Results view displaying pending and completed jobs for the selected circuit

    While the job is in the Pending jobs list, it will display the status of the job. Once completed, it will automatically move from the Pending jobs to the Completed jobs list.

  9. Upon completion, open your experiment from the Completed jobs list by clicking on the job. This opens the experiment results view; you will see details regarding your experiment at the top of the report, as illustrated in the following screenshot:

Figure 2.16 – Completed job result overview

This view provides details about the results such as the Time taken during each task, the Backend system it had run on, the number of Shots, the current Status, and the total time taken to execute. As you look further down the view you will see a histogram of the results from the circuit you just ran on the backend, as illustrated in the following screenshot:

Figure 2.17 – Histogram representation of the circuit results

When you further scroll down the view/page you will see the diagram of the circuit you created, illustrated in the following screenshot:

Figure 2.18 – Circuit diagram of the circuit

The diagram of the circuit is just one of the three representations of the circuit. The other two tabs will display the QASM and Qiskit representations.

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.21 provides information about the outcome of your experiment. Some parts might seem straightforward, but let's review the details.

It may seem trivial now, but later on when we work on more elaborate quantum algorithms, understanding the results will prove invaluable.

There are two axes to the results. Along the x axis, we have all the possible states of our circuit. This is what the measurement operations observed when measuring the qubits. Recall that we measured the first and second qubits, so from least significant bit (on the far right), we see that the first two bits are set to 1 and 0 respectively. We know that this is correct due to the fact we placed a NOT gate on the first qubit, which changes its state from 0 to 1. For the second qubit, we simply took a measurement that equates to simply measuring the initial state, which we know to be 0.

The y axis provides the probability of the measurement. Since we ran the experiment 1024 times, the results show that we have approximately a 95% probability of the first qubit resulting in the state of 001. The reason why the probability is 95% and 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 to a pretty high of probability that the NOT gate worked.

So, when would the probability be different? We'll explore this in the following experiment.

In this section, we simulated a simple NOT gate operation on a qubit and ran the circuit on a quantum device. Pretty simple and straightforward. So now that you were able to create and run your first quantum program, let's start learning something a little more interesting than just changing the state of a qubit.