Qiskit basics



1. Introduction
2. Install
3. Advanced Circuits
4. Measurements
5. Tips


1. Introduction

Qiskit it an open software-development kit for quantum computation.

The backend is the entity which executes quantum programs, like mathkernel in Mathematica. We choose a backend either from a real-device or a simulator. Qiskit has four elements

  • Terra, access hardware
  • Aer, simulator backends
  • Aqua, library of algorithms
  • Ignis, noise calibration (will be obsolete)
    There are two simulator backends, one is Qiskit Aer (if executed on your local computer) and the other is IBM Quantum (if executed on IBM Cloud).

Then we choose a method in the backends, which should not change during the execution.

The most basic method is aer_simulator (without option). Aer has four method options, stabilizer, statevector, density_matrix and matrix_product_state. We may choose qasm_simulator and print the measurement results of each shot. QASM Simulator is essentially same as IBMQ QASM Simulator, which is the provider of a real device of IBM. The max number of shots is 8192 in ibmq_qasm_simulator, and 10^7 in qasm_simulator.
(QASM = Quantum ASseMbly language )

2. Install

To begin with, we should install qiskit to our (local/virtual/cloud) environment.

!pip install qiskit
!pip install pylatexenc
!pip install matplotlib

to upgrade the version, use

!pip install -U qiskit

Alternatively, try

!python3 -m pip install qiskit
!python3 -m pip install pylatexenc
!python3 -m pip matplotlib

Here I assumed that you run these codes inside a Jupyter notebook file. Namely, you created a virtual environment, opened a jupyter notebook, and installed Qiskit modules as above. Suppose that you want to run another python script in your code, and the new code contains

from qiskit import *

It is likely that you get the warning, ModuleNotFoundError: No module named 'qiskit'. This is because pip's ' are created for each virtual environment of Python. Usually, pip install XXX installs the module XXX to the Python specified by which pip, like "/usr/local/bin/python3". When you created a virtual environment, you need to install modules using the pip associated to that Python. For this purpose, execute

python3 -m pip install XXX

inside the virtual environment (this is exactly what we did when installing jupyter notebook!) See this article for more information.

It is not recommended to download the source code for the module, and add the system path using sys.path.append(). The modules are only partially installed in this method, Python may not be able to resolve the dependency. Finally, do not forget upgrading modules to the latest version, when necessary.

3. Advanced Circuits

The method to_instruction() converts a set of custom-made gates into an “instruction” and an instruction can be appended to another circuit

  • the instruction can be used in the form append(instruction, [qubit list])
  • qubit list should be written in the ascending order, i.e. [q[0], q[1], …, q[L-1]]

Parameter(‘name’) construct a new parameter, to be used in parameterized circuits

  • The name can be any Unicode character
  • bind_parameters substitutes a numerical value to a function. This should be done before sending the circuit to the backend; see Tutorial

4. Measurements

  • by default, the counts gives b = ( qL-1 qL-2 ... q1 q0 ), where qn = q[n]
  • by changing the order of classical qubits, we flip b to bT = ( q0 q1 ... qL-1 )
  • the output format is a tuple of (text, integer) representing (bit_string, count)
  • execute().result() gives a dict data type of Python

5. Tips

  • X gate is [[0,1],[1,0]], which flips qubit
  • We should reset the circuit inside for loops