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

Reviewing the results of your quantum circuit on Quantum Lab Notebooks

In this section, we'll conclude this chapter by running the circuit on a quantum simulator and a real device. We'll then review the results by following these steps:

  1. From the open Notebook, enter and run the following in the next empty cell:

    backend = Aer.get_backend('qasm_simulator')

    The preceding code generates a backend object that will connect to the specified simulator or device. In this case, we are generating a backend object linked to the QASM simulator.

  2. In the next empty cell, let's run the execute function. This function takes in three parameters—the circuit we wish to run, the backend we want to run it on, and how many shots we wish to execute. The returned object will be a job object with the contents of the executed circuit on the backend. The code for this can be seen here:

    job_simulator = execute(qc, backend, shots=1024)

  3. We now want to extract the results from the job object. In order to do that, we will call the result function, illustrated as follows, and save it in a new variable:

    result_simulator = job_simulator.result()

  4. Since we ran our experiment with 1024 shots, we want to get the results from the counts. In order to do that, we can call the get_counts() method by passing in our circuit as the argument. Once we receive the counts, let's print out the results by running the following code:

    counts = result_simulator.get_counts(qc)

    print(counts)

    Note that the count results, shown as follows, may be different from your count results, which are based on the randomness of the qubits. But overall, the results will be similar by approximately 50%:

    Figure 3.10 – Count results from the quantum circuit

  5. Finally, let's visualize the result counts by plotting them using a histogram. We'll first import the plot_histogram method from the qiskit.visualization library and pass our counts in as an argument, as follows:

    from qiskit.visualization import plot_histogram

    plot_histogram(counts)

    As you can see in the following screenshot, the results are very similar to our results from the Circuit Composer in that 50% of the time our results are 00, and the other half of the time they are 11:

Figure 3.11 – Histogram view of the count results

Now that we have run our quantum circuit on a simulator, let's run this same quantum circuit on a real quantum computer.

Executing a quantum circuit on a quantum computer

To run this quantum circuit on a quantum device, we will continue with the following steps:

  1. The only change you need to update in the steps from the preceding section is to go from running on a simulator to a real device in Step 1 from the previous steps, which is where you specify the name of the backend. In Step 1, we set the backend to the qasm_simulator. In this step, we will update to an actual device. So, let's first get a list of backends from our providers by running the following code in a new cell:

    provider.backends()

    The preceding method will return a list of all the simulators and real devices currently available to you, shown as follows. Note that the devices listed may change over time, so the results shown may be different when you run the method:

    Figure 3.12 – List of available quantum computers (quantum devices)

  2. The only change you need to update from the steps in the previous section is to specify which quantum computer from the list of backend devices you wish to run the experiment. In the previous steps, we set the backend to the qasm_simulator, whereas in this step we will update our backend to use a real device from the list. In this case, we'll choose ibmq_vigo. This list may appear different to you, so pick one from your list if ibmq_vigo is not listed. To do this, run the following code in a new cell:

    backend = provider.get_backend('ibmq_vigo')

    The preceding code assigns the ibmq_vigo quantum computer as our backend.

  3. From the previous steps, repeat Step 2 to Step 5 to run the circuit on a real device. Your results will seem a little different. Rather than just the 00 and 11 results, you will see that there are some 01 and 10 results, shown in the screenshot that follows, albeit only a small percentage of the time.

    This is due to noise from the real device, which is why they are often referred to as Noisy Intermediate-Scale Quantum (NISQ) systems or near-term devices. The noise can come from an array of things, such as ambient noise and decoherence. Details about the different types of noise and their effects will be discussed in detail in Chapter 11, Mitigating Quantum Errors Using Ignis.

    The results can be seen in the following screenshot:

Figure 3.13 – Histogram plot of results

Congratulations! You have just completed running a quantum circuit on both a quantum simulator and a real quantum device using the Quantum Lab Notebooks. As you can see, by using the Notebook you can use many built-in Qiskit methods to create circuits and run them on various machines with a simple line of code, whereas on the Circuit Composer you would have to make various changes that would take a lot of time to complete.