Search⌘ K
AI Features

Quantum Computing Libraries and Frameworks

Learn how to use popular quantum computing libraries Qiskit and Cirq for running quantum algorithms. Understand the general coding approach including defining circuits, choosing simulators, and executing algorithms on quantum systems. This lesson prepares you to implement quantum algorithms with practical Python code leveraging these frameworks.

In the previous chapter, we created our very own quantum computing simulator. While it was fundamental, we could use it to run elementary quantum algorithms. However, for more advanced algorithms, we would need to add support for parametric gates and then some.

Knowing that, we now transition to using robust quantum computing frameworks and libraries that implement all the required features required to run quantum algorithms. In this course, we will be learning to use:

  • Qiskit, and
  • Cirq

Both Qiskit(developed by IBM Quantum) and Cirq (developed by Google) are popular quantum computing libraries/frameworks developed in Python. They provide a decent high-level programming interface for complex quantum hardware. They also support running quantum algorithms on simulators or real quantum computers, which is perfect for our use case. Though, as stated earlier, we will only be using simulators in this course.

The general coding technique

Before we dive into our first algorithm in the next lesson, we think it is useful to know the general technique of coding quantum algorithms in these frameworks and libraries.

In most cases, you will first start by importing the library itself and then choose your simulator. Most libraries provide more than one type of simulator, each with its own quirks and features. Then you will define a quantum circuit of nn qubits. Then you will write the gates for the circuit you want to code, in sequence, specifying the qubits they will act on. At this point, it is helpful to draw out your circuit to get a visual representation of what you’re coding This isn’t a prerequisite for running your quantum algorithm but it can be helpful. Next you will input this circuit along with your quantum system to the simulator or real quantum computer and tell it to run the algorithm for a certain number of times. Finally, you will measure the system, reading the output state in the form of a bit string. That will be the result of running your quantum algorithm on the qubit system.

A key point to note here is that in the rest of the course that follows, the focus will be on explaining key techniques and their usage in quantum algorithms. We’ll discuss the circuits, and we’ll certainly dive deep into their explanation as well. But since this course would not be complete without including proper code, we will provide Python code for each algorithm. However, it will be the general code taken from the documentation of these libraries and is freely available for your perusal online. Again, the focus here is not to learn one library or framework and dive deep into it. An exposé on Qiskit or Cirq is a separate course for a separate time.

With that out of the way, let’s now install Qiskit and Cirq on our computers. But obviously, you don’t need to do the following steps since we will be using the integrated code environment and widgets for this course. The following installation steps are for when you decide to run the algorithms locally on your own machines.

Installing Qiskit

To install Qiskit, you can run the following commands in the shell on your computer.

Shell
pip install qiskit
pip install qiskit[visualization]

Installing Cirq

To install Cirq, you can run the following commands in the shell on your computer.

Shell
python -m pip install --upgrade pip
python -m pip install cirq
python -m pip install cirq-core[contrib]
sudo apt-get install texlive-latex-base latexmk
python -c 'import cirq; print(cirq.google.Foxtail)'
# should print:
# (0, 0)───(0, 1)───(0, 2)───(0, 3)───(0, 4)───(0, 5)───(0, 6)───(0, 7)───(0, 8)───(0, 9)───(0, 10)
# │ │ │ │ │ │ │ │ │ │ │
# │ │ │ │ │ │ │ │ │ │ │
# (1, 0)───(1, 1)───(1, 2)───(1, 3)───(1, 4)───(1, 5)───(1, 6)───(1, 7)───(1, 8)───(1, 9)───(1, 10)