Path: blob/main/notebooks/ch-labs/Lab01_QuantumCircuits.ipynb
3855 views
Lab 1 Quantum Circuits
Part 1: Classical logic gates with quantum circuits
Goal
Create quantum circuit functions that can compute the XOR, AND, NAND and OR gates using the NOT gate (expressed as x in Qiskit), the CNOT gate (expressed as cx in Qiskit) and the Toffoli gate (expressed as ccx in Qiskit).
An implementation of the NOT gate is provided as an example.
📓 XOR gate
Takes two binary strings as input and gives one as output.
The output is '0' when the inputs are equal and '1' otherwise.
📓 AND gate
Takes two binary strings as input and gives one as output.
The output is '1' only when both the inputs are '1'.
📓 NAND gate
Takes two binary strings as input and gives one as output.
The output is '0' only when both the inputs are '1'.
📓 OR gate
Takes two binary strings as input and gives one as output.
The output is '1' if either input is '1'.
Part 2: AND gate on Quantum Computer
Goal
Execute AND gate on a real quantum system and learn how the noise properties affect the result.
In Part 1 you made an AND gate from quantum gates, and executed it on the simulator. Here in Part 2 you will do it again, but instead run the circuits on a real quantum computer. When using a real quantum system, one thing you should keep in mind is that present day quantum computers are not fault tolerant; they are noisy.
The 'noise' in a quantum system is the collective effects of all the things that should not happen, but nevertheless do. Noise results in outputs are not always what we would expect. There is noise associated with all processes in a quantum circuit: preparing the initial state, applying gates, and qubit measurement. For the gates, noise levels can vary between different gates and between different qubits. cx gates are typically more noisy than any single qubit gate.
Here we will use the quantum systems from the IBM Quantum Experience. If you do not have access, you can do so here.
Now that you are ready to use the real quantum computer, let's begin.
Step 1. Choosing a device
First load the account from the credentials saved on disk by running the following cell:
After your account is loaded, you can see the list of providers that you have access to by running the cell below. Each provider offers different systems for use. For open users, there is typically only one provider ibm-q/open/main:
Let us grab the provider using get_provider. The command, provider.backends( ) shows you the list of backends that are available to you from the selected provider.
Among these options, you may pick one of the systems to run your circuits on. All except the ibmq_qasm_simulator all are real quantum computers that you can use. The differences among these systems resides in the number of qubits, their connectivity, and the system error rates.
Upon executing the following cell you will be presented with a widget that displays all of the information about your choice of the backend. You can obtain information that you need by clicking on the tabs. For example, backend status, number of qubits and the connectivity are under configuration tab, where as the Error Map tab will reveal the latest noise information for the system.
For our AND gate circuit, we need a backend with three or more qubits, which is true for all the real systems except for ibmq_armonk. Below is an example of how to filter backends, where we filter for number of qubits, and remove simulators:
One convenient way to choose a system is using the least_busy function to get the backend with the lowest number of jobs in queue. The downside is that the result might have relatively poor accuracy because, not surprisingly, the lowest error rate systems are the most popular.
Real quantum computers need to be recalibrated regularly, and the fidelity of a specific qubit or gate can change over time. Therefore, which system would produce results with less error can vary.
In this exercise, we select one of the IBM Quantum systems: ibmq_quito.
Step 2. Define AND function for a real device
We now define the AND function. We choose 8192 as the number of shots, the maximum number of shots for open IBM systems, to reduce the variance in the final result. Related information is well explained here.
Qiskit Transpiler
It is important to know that when running a circuit on a real quantum computer, circuits typically need to be transpiled for the backend that you select so that the circuit contains only those gates that the quantum computer can actually perform. Primarily this involves the addition of swap gates so that two-qubit gates in the circuit map to those pairs of qubits on the device that can actually perform these gates. The following cell shows the AND gate represented as a Toffoli gate decomposed into single- and two-qubit gates, which are the only types of gate that can be run on IBM hardware.
In addition, there are often optimizations that the transpiler can perform that reduce the overall gate count, and thus total length of the input circuits. Note that the addition of swaps to match the device topology, and optimizations for reducing the length of a circuit are at odds with each other. In what follows we will make use of initial_layout that allows us to pick the qubits on a device used for the computation and optimization_level, an argument that allows selecting from internal defaults for circuit swap mapping and optimization methods to perform.
You can learn more about transpile function in depth here.
Let's modify AND function in Part1 properly for the real system with the transpile step included.
When you submit jobs to quantum systems, job_monitor will start tracking where your submitted job is in the pipeline.
First, examine ibmq_quito through the widget by running the cell below.
📓 Determine three qubit initial layout considering the error map and assign it to the list variable layout2.
📓 Describe the reason for your choice of initial layout.
your answer:
Execute AND gate on ibmq_quito by running the cell below.
Step 3. Interpret the result
There are several quantities that distinguish the circuits. Chief among them is the circuit depth. Circuit depth is defined in detail here (See the Supplementary Information and click the Quantum Circuit Properties tab). Circuit depth is proportional to the number of gates in a circuit, and loosely corresponds to the runtime of the circuit on hardware. Therefore, circuit depth is an easy to compute metric that can be used to estimate the fidelity of an executed circuit.
A second important value is the number of nonlocal (multi-qubit) gates in a circuit. On IBM Quantum systems, the only nonlocal gate that can physically be performed is the CNOT gate. Recall that CNOT gates are the most expensive gates to perform, and thus the total number of these gates also serves as a good benchmark for the accuracy of the final output.
Circuit depth and result accuracy
Running the cells below will display the four transpiled AND gate circuit diagrams with the corresponding inputs that executed on ibm_lagos and their circuit depths with the success probability for producing correct answer.
📓 Explain reason for the dissimilarity of the circuits. Describe the relations between the property of the circuit and the accuracy of the outcomes.
your answer: