Book Image

Quantum Computing in Practice with Qiskit® and IBM Quantum Experience®

By : Hassi Norlen
5 (1)
Book Image

Quantum Computing in Practice with Qiskit® and IBM Quantum Experience®

5 (1)
By: Hassi Norlen

Overview of this book

IBM Quantum Experience® is a leading platform for programming quantum computers and implementing quantum solutions directly on the cloud. This book will help you get up to speed with programming quantum computers and provide solutions to the most common problems and challenges. You’ll start with a high-level overview of IBM Quantum Experience® and Qiskit®, where you will perform the installation while writing some basic quantum programs. This introduction puts less emphasis on the theoretical framework and more emphasis on recent developments such as Shor’s algorithm and Grover’s algorithm. Next, you’ll delve into Qiskit®, a quantum information science toolkit, and its constituent packages such as Terra, Aer, Ignis, and Aqua. You’ll cover these packages in detail, exploring their benefits and use cases. Later, you’ll discover various quantum gates that Qiskit® offers and even deconstruct a quantum program with their help, before going on to compare Noisy Intermediate-Scale Quantum (NISQ) and Universal Fault-Tolerant quantum computing using simulators and actual hardware. Finally, you’ll explore quantum algorithms and understand how they differ from classical algorithms, along with learning how to use pre-packaged algorithms in Qiskit® Aqua. By the end of this quantum computing book, you’ll be able to build and execute your own quantum programs using IBM Quantum Experience® and Qiskit® with Python.
Table of Contents (12 chapters)

Keeping your Qiskit® environment up to date

Qiskit® is an open source programming environment that is in continuous flux. Over the course of writing this book, I have passed through many minor and major version updates of the software.

It is generally a good idea to stay updated with the latest version, but with some updates, components of the code might change behavior. It is always a good idea to have a good look at the release notes for each new version. Sometimes changes are introduced that will change the way your code behaves. In those cases, you might want to hold off on upgrading until you have verified that your code still works as expected.

If you are using Anaconda environments, then you can maintain more than one environment at different Qiskit® levels, to have a fallback environment in case an upgraded Qiskit® version breaks your code.

Qiskit® moves fast

The IBM Quantum Experience® Notebook environment always runs the latest version of Qiskit®, and it might be a good idea to test drive your code in that environment before you upgrade your local environment.

You can also subscribe to notification updates, to find out when a new release has been offered:

  1. Log in to IBM Quantum Experience® at
  2. On the IBM Quantum Experience® dashboard, find your user icon in the upper-right corner, click it, and select My account.
  3. On the account page, under Notification settings, set Updates and new feature announcements to On.

Getting ready

Before you begin, verify which version of Qiskit® you are running for each of your environments (if you have more than one).

For each environment, launch Python, either from the command line, from an IDE such as Spyder, or as a Jupyter notebook, then execute the following code:

>>> import qiskit
>>> qiskit.__qiskit_version__

If you have an old version of Qiskit® installed, the preceding code might result in the following output:

{'qiskit-terra': '0.9.0', 'qiskit-aer': '0.3.0', 'qiskit-ibmq-provider': '0.3.0', 'qiskit-aqua': '0.6.0', 'qiskit': '0.12.0'}

You can then go to the Qiskit® release notes to find out if there is a more up-to-date version available:

This is a lot of steps just to make sure. The whole process is automated in Python. To go down that path, go to the next section.

How to do it...

  1. Activate your virtual environment:
    $ conda activate environment_name
  2. Run the following command to check for outdated pip packages for your virtual environment:
    (environment_name) … $  pip list --outdated
  3. This will return a list of all your pip packages that are currently outdated and list the available versions:
    Example:Package                  Version  Latest   Type 
    ------------------------ -------- -------- -----
    qiskit                   0.19.6   0.21.0   sdist
    qiskit-aer               0.5.2    0.6.1    wheel
    qiskit-aqua              0.7.3    0.7.5    wheel
    qiskit-ibmq-provider     0.7.2    0.9.0    wheel
    qiskit-ignis             0.3.3    0.4.0    wheel
    qiskit-terra             0.14.2   0.15.1   wheel 
  4. It is then a breeze to update Qiskit® using pip:
    (environment_name) … $  pip install qiskit --upgrade
  5. From the command line, verify that Qiskit® is installed:
    (environment_name)… $ pip show qiskit

    This will result in an output similar to the following:

    Name: qiskit
    Version: 0.21.0
    Summary: Software for developing quantum computing programs
    Author: Qiskit Development Team
    Author-email: [email protected]
    License: Apache 2.0
    Location: /Users/hassi/opt/anaconda3/envs/packt_qiskit/lib/python3.7/site-packages
    Requires: qiskit-aer, qiskit-terra, qiskit-aqua, qiskit-ignis, qiskit-ibmq-provider
  6. Verify that Qiskit® is integrated with Python in your isolated environment.

    Open Python:

    (environment_name)… $ python3

    Import Qiskit®:

    >>> import qiskit

    List the version details:

    >>> qiskit.__qiskit_version__

    This should display the versions of the installed Qiskit® components:

    {'qiskit-terra': '0.15.2', 'qiskit-aer': '0.6.1', 'qiskit-ignis': '0.4.0', 'qiskit-ibmq-provider': '0.9.0', 'qiskit-aqua': '0.7.5', 'qiskit': '0.21.0'} 

Congratulations, your Qiskit® upgrade worked; you are now running the latest code!

How it works...

Depending on how you consume this book, you might be looking over this process as part of your first read-through, and no upgrades were available. If so, go ahead and bookmark this recipe, and come back to it at a later time when there has been a Qiskit® upgrade.

The pip tool will manage your upgrade for you for each virtual environment. As I mentioned before, it might be a good idea to do a staged upgrade of your environments if you have more than one.

For example, you can upgrade one environment and test run your quantum programs in that environment to make sure that the new version did not break anything in your code.

OK, with this you should be reasonably set with one or more Qiskit® environments on which to run your quantum programs. If you feel ready for it, you can now take the plunge and go directly to Chapter 4, Starting at the Ground Level with Terra, to start writing quantum programs in Python using Qiskit®. If you are up for some prep work to get a feel for what programming a quantum computer is all about, start with Chapter 2, Quantum Computing and Qubits with Python, to get an introduction to qubits and gates, or Chapter 3, IBM Quantum Experience® – Quantum Drag and Drop, to get a visual feel for quantum programs by using the IBM Quantum Experience® drag-and-drop programming interface.

No matter which path you take, don't worry, we'll let Python do the hard work. Again, have fun!