Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Quantum Computing in Practice with Qiskit® and IBM Quantum Experience®

You're reading from   Quantum Computing in Practice with Qiskit® and IBM Quantum Experience® Practical recipes for quantum computer coding at the gate and algorithm level with Python

Arrow left icon
Product type Paperback
Published in Nov 2020
Publisher Packt
ISBN-13 9781838828448
Length 408 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Hassi Norlen Hassi Norlen
Author Profile Icon Hassi Norlen
Hassi Norlen
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Chapter 1: Preparing Your Environment 2. Chapter 2: Quantum Computing and Qubits with Python FREE CHAPTER 3. Chapter 3: IBM Quantum Experience® – Quantum Drag and Drop 4. Chapter 4: Starting at the Ground Level with Terra 5. Chapter 5: Touring the IBM Quantum® Hardware with Qiskit® 6. Chapter 6: Understanding the Qiskit® Gate Library 7. Chapter 7: Simulating Quantum Computers with Aer 8. Chapter 8: Cleaning Up Your Quantum Act with Ignis 9. Chapter 9: Grover's Search Algorithm 10. Chapter 10: Getting to Know Algorithms with Aqua 11. Other Books You May Enjoy

Downloading the code samples

The recipes in this book include short, and some not so short, sample programs that will lead you through your first steps in programming quantum computers. You can type in these programs directly from the instructions in the book if you want, but for convenience, you can also grab the sample code directly from the Packt Cookbook GitHub organization.

The Python samples are written for Python 3.5+ and the Qiskit® extension that you installed in your Python environment. The Python samples all have the extension: .py.

Getting ready

While you can type the recipes directly into your Python environment, or into Jupyter Notebooks on IBM Quantum Experience® or on your local Anaconda environment, it is somewhat more efficient to download or use Git to clone the sample code to your local environment. The advantage of cloning is that you can later refresh your local files from the remote repository if any updates are made.

If you do not plan to use Git, but instead to download the recipes as a compressed file, continue on with How to do it.

To use Git to clone the sample code, you must first do the following:

  1. Get a GitHub account. These are free and you can sign up for one at https://github.com.
  2. Install Git in your local environment. For more information, see https://git-scm.com/book/en/v2/Getting-Started-Installing-Git.
  3. If you are a user interface person, you might also want to install GitHub Desktop, available here: https://desktop.github.com/.

How to do it...

You have several different options to download the sample recipes to your local machine.

For each, start by opening your web browser and then go to the Quantum-Computing-in-Practice-with-Qiskit-and-IBM-Quantum-Experience GitHub repository at https://github.com/PacktPublishing/Quantum-Computing-in-Practice-with-Qiskit-and-IBM-Quantum-Experience.

Downloading the repository as a compressed file

The easiest way to get the recipes is to just grab the sample files as a compressed directory and decompress it on your local machine:

  1. At the preceding URL, click the Clone or download button and select Download zip.
  2. Download the compressed file and select a location.
  3. Decompress the file.

Cloning the repository using git

  1. Click the Clone or download button and copy the URL.
  2. Open your command line and navigate to the location where you want to clone the directory.
  3. Enter the following command:
    $ git clone https://github.com/PacktPublishing/Quantum-Computing-in-Practice-with-Qiskit-and-IBM-Quantum-Experience.git

    The command should result in something like the following:

    Cloning into 'Quantum-Computing-in-Practice-with-Qiskit-and-IBM-Quantum-Experience'...
    remote: Enumerating objects: 250, done.
    remote: Counting objects: 100% (250/250), done.
    remote: Compressing objects: 100% (195/195), done.
    remote: Total 365 (delta 106), reused 183 (delta 54), pack-reused 115
    Receiving objects: 100% (365/365), 52.70 MiB | 5.42 MiB/s, done.
    Resolving deltas: 100% (153/153), done.

Cloning the repository using GitHub Desktop

  1. Click the Clone or download button and select Open in desktop.
  2. In the GitHub Desktop dialog, select a directory to clone the repository to and click OK.

You can now browse the recipes in this cookbook. Each chapter includes one or more recipes. If you want, you can copy and paste the recipes directly into your Python environment, or into Jupyter Notebooks on IBM Quantum Experience® or on your local Anaconda environment.

Opening a recipe file

So far, you have done everything with the command line. So how about you grab the following Python program and run it from your favorite Python interpreters, such as Anaconda Spyder or Jupyter Notebooks?

If you have downloaded the sample files, the recipe file is available in the following local directory:

<The folder where you downloaded the files>/https://github.com/PacktPublishing/Quantum-Computing-in-Practice-with-Qiskit-and-IBM-Quantum-Experience/blob/master/Chapter01/ch1_r1_version.py

The ch1_r1_version.py code sample lists the version numbers of the Qiskit® components that we just installed:

# Import Qiskit
import qiskit
# Set versions variable to the current Qiskit versions
versions=qiskit.__qiskit_version__
# Print the version number for the Qiskit components
print("Qiskit components and versions:")
print("===============================")
 
for i in versions:
    print (i, versions[i]) 

When run, the preceding code should give an output similar to the following:

Figure 1.2 – A list of the Qiskit® components and versions

Figure 1.2 – A list of the Qiskit® components and versions

The following sections cover how to run the script in the environments that we have available.

Python scripts in Spyder

In your local environment, you can now open the Python scripts in the Python interpreter of your choice; for example, Spyder, which is included with Anaconda:

Important

Be sure that you run your interpreter in the virtual environment in which you installed Qiskit®. Otherwise, it will not be able to find Qiskit®, and the program will not run correctly.

  1. Open your Anaconda user interface.
  2. Select your virtual environment.
  3. Click the Spyder tile. If Spyder is not yet installed for your virtual environment, it will now install. This might take a while. Be patient!
  4. In Spyder, open the Python script that is included with this chapter:
    <The folder where you downloaded the files>/https://github.com/PacktPublishing/Quantum-Computing-in-Practice-with-Qiskit-and-IBM-Quantum-Experience/blob/master/Chapter01/ch1_r1_version.py
  5. Click Run. The script will now pull out the version numbers of the installed Qiskit® components. You can also open the Python scripts in a Jupyter notebook, for example, in the online IBM Quantum Experience® environment, but this takes a little extra work.

Jupyter Notebooks in Anaconda

  1. Open your Anaconda user interface.
  2. Select your virtual environment.
  3. Click the Jupyter Notebooks tile. If Jupyter Notebooks is not yet installed for your virtual environment, it will now install.
  4. Your default web browser opens at your root directory. Browse to and open the following:
    <The folder where you downloaded the files>/https://github.com/PacktPublishing/Quantum-Computing-in-Practice-with-Qiskit-and-IBM-Quantum-Experience/blob/master/Chapter01/ch1_r1_version.py
  5. The sample script opens in a Jupyter text editor. You can now see the code but not run it.
  6. Go back to the Jupyter browser and click New | Notebook.
  7. Copy and paste the Python script code into the new notebook. You can now click Run and see the code execute.

Jupyter Notebooks in IBM Quantum Experience®

  1. To run the Python scripts in the online IBM Quantum Experience® notebooks, log in to IBM Quantum Experience® at https://quantum-computing.ibm.com/login.
  2. On the main dashboard, click on the Quantum Lab left-menu icon (A picture containing drawing

Description automatically generated), then click New Notebook and follow the process we discussed in the Jupyter Notebooks in Anaconda section.:
Figure 1.3 – Running your Qiskit® code on IBM Quantum Experience®

Figure 1.3 – Running your Qiskit® code on IBM Quantum Experience®

How it works...

The Qiskit®-based Python code that we will be going through in the chapters that follow can be run in any Python environment that meets the Qiskit® requirements, so you have free reins to pick the environment that suits you. And with that environment, you can also freely pick your favorite tools to run the programs.

For this book, I have tested running the code in both the Spyder editor that comes as standard with Anaconda and with the Jupyter Notebook environments on both IBM Quantum Experience® and Anaconda.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime