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)

Installing your API key and accessing your provider

Now that you have installed Qiskit®, you can immediately start creating your quantum programs and run these on local simulators. If, however, at some point, you want to run your quantum code on actual IBM Quantum® hardware, you must install your own unique API key locally.

API keys on IBM Quantum Experience®

If you are running your Qiskit® programs in the IBM Quantum Experience® notebook environment, your API key is automatically registered.

Getting ready

Before you can install your API key, you must first have an IBM Quantum Experience® account. If you have not yet created one, go back and do it (see the Creating your IBM Quantum Experience® account section).

How to do it...

Let's take a look at how to install the API key locally:

  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, find the Qiskit in local environment section, and click Copy token.
  4. You can now paste your token in a temporary location or keep it in the clipboard.
  5. On your local machine, access your Qiskit® environment. We have done this one, but here's a repeat of the process if you are using Anaconda.
  6. Activate your virtual environment:
    $ conda activate environment_name
  7. Open Python:
    $(environment_name) … $  python3

    Verify that the Python header displays and that you are running the correct version of Python:

    Python 3.7.6 (default, Jan  8 2020, 13:42:34) 
    [Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
    Type "help", "copyright", "credits" or "license" for more information.
  8. Get the required IBMQ class:
    >>> from qiskit import IBMQ		
  9. Install your API token locally:
    >>> IBMQ.save_account('MY_API_TOKEN')

    Here, instead of MY_API_TOKEN, paste in the API token that you just copied from IBM Quantum Experience®: Keep the single quotes as they are required for the command.

  10. Load your account.

    Now that the token is in place, let's verify that all is in order and that your account has the correct privileges:

    >>> IBMQ.load_account()

    This should display the following output:

    <AccountProvider for IBMQ(hub='ibm-q', group='open', project='main')>

    This is the provider information for your account, with hub, group, and project.

How it works...

The main class that you import for this exercise is IBMQ, which is a toolbox for working with the quantum hardware and software that is provided by IBM in the cloud.

In this chapter, we used save.account() to store your account locally. As we go forward, in the recipes where we will access the IBM Quantum® machines, we will use the IBMQ.load_account() and IBMQ.get_provider() classes in your quantum programs to make sure that you have the correct access.

Updating your API key

If for some reason, you need to create a new API token on IBM Quantum Experience® and update your locally saved token, you can use the following command:

>>> IBMQ.save_account('NEW_API_TOKEN', overwrite=True)

There's more…

In the code that follows in the recipes in this cookbook, we will set a provider variable to hold the provider information for your account by using the following command:

>>> provider = IBMQ.get_provider()

We can then use the provider information when selecting the IBM Quantum® computer, or backend, to run your quantum programs on. In the following example, we select a quantum computer that is called IBM Q 5 Yorktown (ibmqx2) as our backend. The internal reference for this machine is ibmqx2:

>>> backend = provider.get_backend('ibmqx2')