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

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
.
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:
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.
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:
$ 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.
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.
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
The following sections cover how to run the script in the environments that we have available.
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.
<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 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
Figure 1.3 – Running your Qiskit® code on IBM Quantum Experience®
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.
Change the font size
Change margin width
Change background colour