Mastering Quantum Computing with IBM QX

5 (2 reviews total)
By Dr. Christine Corbett Moran
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. What is Quantum Computing?

About this book

Quantum computing is set to disrupt the industry. IBM Research has made quantum computing available to the public for the first time, providing cloud access to IBM QX from any desktop or mobile device. Complete with cutting-edge practical examples, this book will help you understand the power of quantum computing in the real world.

Mastering Quantum Computing with IBM QX begins with the principles of quantum computing and the areas in which they can be applied. You'll explore the IBM Ecosystem, which enables quantum development with Quantum Composer and Qiskit. As you progress through the chapters, you'll implement algorithms on the quantum processor and learn how quantum computations are actually performed.

By the end of the book, you will completely understand how to create quantum programs of your own, the impact of quantum computing on your business, and how to future-proof your programming career.

Publication date:
January 2019


Chapter 1. What is Quantum Computing?

In this chapter, we will give an overview of what quantum computing is in contrast to classic computing, focusing on potential advantages quantum computers offer, compared to classic computers. We then go over the history of classic and quantum computing and provide an overview of the present day state of the art in computation. Finally, we provide an overview and an introduction to the approach taken in the book to teaching programming on quantum computers, including an outline of the chapter structure as well as instructions to download and set up necessary software tools. By the end of this chapter, you will be able to describe what a quantum computer is, why they are useful, and their present state of the art. We will also set up the environment to start developing your own quantum computing simulator in Python, to interact with existing quantum computer simulators and hardware via the Python library Qiskit, and to run directly on real quantum computing hardware, IBM QX.

The following topics will be covered in this chapter: 

  • What are quantum computers?
  • The history, present, and future of quantum computing
  • Setting up and running Python code examples
  • Setting up and running IBM QX examples
  • Setting up and running Qiskit examples

Technical requirements

You will need a Python 3.4+ installation and Git installed. You should ensure you have access to a Unix-like command line, whatever your operating system of choice. This could be the on OS X, a Terminal on any Linux machine, or Cygwin or the Bash shell on Windows. Code for this chapter is available in a Jupyter Notebook under The preferred way of interfacing with code is within a Python virtual environment in a Jupyter Notebook, but feel free to use whatever setup works best for you.


What are quantum computers?

In this book, I call the central processing units that power our laptops, desktops, servers, and mobile phones today, classical computers, to contrast them from quantum computers. These classical computers, made of transistors, function by manipulating bits, pieces of data which are either 0 or 1, to perform computation. Quantum computers on the other hand are able to exploit the properties of quantum physics to perform some computations in a different manner, the quantum properties of superposition and entanglement, to perform computations much more efficiently than on a classical computer. It is important to note that any quantum computation can be simulated on a classical computer, but, for specific types of computations, the simulation would take so long or use so much memory as to be practically impossible. It is these problems that quantum computing professionals seek out because they show the benefits of quantum computation over classical computation.

Quantum computing can be performed physically in a variety of fashions. Some quantum computers use the properties of quantum mechanics, including entanglement and superposition, to solve one particular type of optimization problem, that of finding a global minimum of a specific type of function. These computers are called quantum annealing computers, and while there are many optimization problems which can be reformulated in this fashion there are a variety of algorithms they are too specialized to run. Examples of quantum computers which use quantum annealing are D-Wave machines.

This book focuses on universal gate quantum computing. It is the most general form of quantum computation, capable of performing any quantum computation desired, and has analogs to concepts in classic computing such as bits, gates, and circuits. Examples of universal gate quantum computers include those of IBM and Rigetti. 

General purpose quantum computers are more difficult to design robustly, but special purpose quantum computers such as quantum annealing machines may ultimately have limited use. 

Uses of quantum computers

Quantum supremacy is when a quantum computing algorithm offers significant speedup compared to the best possible algorithm on a classical computer, and this speedup is demonstrated on a quantum computer. When quantum supremacy is achieved over a given classical algorithm, it then makes sense to run the algorithm on a quantum computer instead of a classical computer as the quantum computer will be more efficient. At the time of writing this book, there are a variety of areas which offer the promise of quantum supremacy, but no areas which have convincingly demonstrated it yet. These areas include machine learning and artificial intelligence, cryptography and computer security, searching, sampling and optimization, quantum chemistry and quantum dynamics, and more. But, because no area has demonstrated quantum supremacy as of the writing of this book, at present, it makes more sense to run algorithms on a classical computer instead of a quantum computer.

Quantum supremacy can provide the building blocks to power innovations and disruptions in medicine, finance and business processes, and more. As of the writing of this book, quantum supremacy has not been clearly demonstrated in any area on any machine, although engineers and scientists have a variety algorithms to offer the potential, quantum hardware still needs to improve before this potential is realized. These hardware improvements necessary to enable quantum supremacy may include, for example, making more qubits available, making the qubits more stable, and increasing the error correction of those qubits. Google, IBM, and others expect to demonstrate quantum supremacy in the near future because they expect to be able to make these improvements.

Why is quantum computing important? There's no one reason that outshines the others, but here are some comments from experts in the field highlighting why they work on it:

  • Harnessing quantum computation is a worthy challenge that pushes humanity to be its best
  • Quantum computing is needed because digital (classic) computing is running into the limits of its capability and quantum computing is the next frontier
  • The universe is a quantum world, so to understand it we need quantum computation
  • Quantum computers will help us tackle problems too difficult for a classic computer
  • Developing new hardware based on new physics has historically helped progress in technology quantum computation should be no different
  • Quantum computers will let us do things we can't do today

Experts weigh in – why is quantum computing important?

Quantum computing is important because the universe is a fundamentally quantum place. We have had great success over the past century building computing systems that allow us to model, simulate, analyze and interpret the world around us. However, as those computing systems have been based on classic logic, they are limited that they can only efficiently model a universe that approximates a classic one. We know, with astounding precision that the universe in which we exist is fundamentally quantum. And so in order to understand the universe as it actually exists, we must use a quantum representation. We do not know for sure if our own universe is itself a simulation. But we can say that if it is, it's running on a quantum computer.

– Aaron Van Devender, PhD; Chief Scientist, Founders Fund

Because quantum computing will play a major role in the future of computing, studying the field isn't just for physicists and researchers. Software engineers, executives, investors, and more need to develop fluency as well. Here, an expert states why:

In many cases, executives care because they have to—because of hype or pressure or whatever. Today, though, what they need to understand is quite limited. Commercially applicable quantum algorithms are still many years from being realized. I think in any company with a potential stake in new technology, the most important thing is due diligence and the skills to spot charlatans. Quantum computing should be on the radar and you should know how to evaluate the claims of quantum salespeople. 

– Dr. Chris Ferrie, Centre for Quantum Software and Information, University of Technology Sydney


History, present, and future of quantum computing

In this section we will focus on the history, state of the art, and future of universal gate quantum computing, as it is the model used for IBM QX and the focus of this book.

History of quantum computing

Quantum computing has made great strides in recent years but it is not a new idea. Quantum physics was developed in the 1920s and 1930s. In the early 1980s, physicist Richard Feynman encouraged computer scientists to develop new models of computation based on quantum physics, which showed the promise of performing computation in a different and more efficient manner. In the 1990s, the field of quantum computing progressed when the first algorithms were developed that could run more efficiently on quantum computers than on classic computers. In the 2000s, practical quantum computers began to be able to implement some of the theoretical algorithms from the 1990s for very small inputs. 

Important historical developments in quantum computing include:

  • Feynman's introduction of quantum computing (1981-1982)
  • The demonstration that quantum computing can perform better than classic computing (1985)
  • Shor's algorithm (1994)
  • Quantum error correction (1995)
  • Grover's algorithm (1996)
  • The quantum threshold theorem (1999)
  • Cross pollination from different quantum computing systems in recent years

Present/current state of quantum computing 

At the time of writing this book, IBM offers a 5-qubit and 16-qubit computer available in the cloud via IBM QX and has a 50-qubit quantum computer available to research partners and a 100-qubit quantum computer under way. Rigetti Computing offers an 8-qubit device. While these computers currently demonstrate interesting principles of quantum computation and allow for prototyping and testing algorithms, they are not yet large enough in terms of qubit size and robust enough to exhibit quantum supremacy.

Future of quantum computing

 At the time of this book's writing, IBM estimates that exhibiting quantum supremacy for a specialized application should be possible with 50-100-qubits. This should happen in the near future. Demonstrating quantum supremacy for one specialized application is different to being able to realize the full potential of quantum computing. To have truly useful quantum computers across a variety of fields with universal gate quantum computers, the quantum computing hardware must advance to incorporate more qubits that work together robustly for a longer period of time. Many of these qubits will not be used in the core computation, but rather for error correction to deal with errors introduced by the environment during the course of computation.

The outlook for quantum computing going forward is a topic of debate, and some experts argue it's difficult to predict. Some ideas about where quantum computing is heading in the near future include:

  • Hybrid quantum systems that are made of different quantum computing techniques
  • Continued steady progress in hardware and big leaps in quantum algorithms
  • Real world practical applications

In the far future, we may see these advances enabling large quantum computers, and encouraging their wide-scale use.


Experts weigh in – what is the outlook for quantum computing in the future?

The big near term milestones will be a demonstration of quantum computational supremacy (performing a computation on a quantum processor that takes much longer to compute using conventional high-performance computing), a demonstration of quantum advantage (demonstrating that a quantum processor can perform a -useful- computation faster than conventional computing resources) and a demonstration of fault-tolerance with active error correction. I would expect each of these to occur within the next ten years, and probably within the next five. The demonstration of quantum computational supremacy is likely to be the first to occur, and is likely within the next two years. Within fifty years, I would certainly expect quantum computers to be much more similar to todays computer architectures in terms of having multiple components dedicated to different functions (processing, storage, communication) and in terms of being used as general purpose computing devices rather than single purpose co-processors.

– Dr. Joe Fitzsimons, Principal Investigator, Centre for Quantum Technologies; Founder, Horizon Quantum Computing


Setting up and running Python code examples

In this section I will show you how to set up a Python virtual environment for the Python code examples in the book, set up a checkout, and run simple examples. We will be working with Python 3.4+, so if you have Python 2 installed on your system, for some of the commands, you may need to type python3 to call the correct version of Python in the commands to be followed.

Get the book code

You can clone the code for this book using the following steps:

  1. Clone the repository containing all the code examples with the following command:
git clone
  1. Navigate to the directory using the following command:
cd Mastering-Quantum-Computing-with-IBM-QX
  1. Create a virtual environment in which to run all code examples using the following command:
python -m venv MQC
  1. You must always make sure you are running in the correct virtual environment before trying to run chapter examples or attempting chapter projects as follows:
source MQC/bin/activate
  1. Next install the requirements.txt file present in the GitHub link, for running chapter examples and attempting chapter projects by using the following command:
pip install -r requirements.txt
  1. Check your installation by running the following command:
python chapter01/
  1. Debug, if there are any failures.

That's it. You are now ready to take advantage of the Python examples and learn by doing chapter projects. If you'd rather not run in a virtual environment, make sure you have installed all the modules listed in requirements.txt via pip, your preferred package manager, or the source.

Set up Jupyter Notebook

Jupyter Notebooks are convenient workbooks to organize Python code, text and the results of running code all in one place. This book illustrates all the Python focused examples within a Jupyter Notebook although, in principle, you can execute the Python code in any Python interpreter you choose. If you want to run a Jupyter Notebook within a Python virtual environment, run within that environment: pip install ipykernel and ipython kernel install --user --name=bookkernel. Then after launching Jupyter Notebook, navigate to Kernel | bookkernel in the notebook's UI. To test out your Jupyter Notebook setup, run the following command:

jupyter notebook

Now open the notebook Hello Quantum World.ipynb by navigating to it from the Jupyter Notebook GUI. Run the example line. If you have trouble at any step, consider an online tutorial or consulting Jupyter's documentation at


Setting up and running IBM QX examples

In this section you will setup an account with IBM QX and run a simple program:

  1. Setup a new account at
  2. After you sign in with your new account, click on the Composer tab under the Learn drop-down menu.

Here you will see something that looks like five wires, or if you are familiar with sheet music, like a music score, without any music notes. This is an interface to the IBM QX quantum computers called the quantum composer and we will use it to program and then run our first quantum program in simulation. In a later chapter, this interface will be explored in detail and we will run our quantum programs on the IBM hardware itself.

Hello quantum world

Every classic bit either 0 or 1 can be written as either a "0" or a "1" qubit, which to show that it is a qubit, is written as a name surrounded by | and >. So, for example, the qubit named "0" is written as |"0"> and the qubit named "1" is written as |"1">. Throughout this book, the qubits will always be written as names surrounded by quotation marks and | and > to indicate that they are, indeed, qubits.

Each wire in the quantum composer starts at a quantum equivalent of the 0 bit, the |"0"> qubit. To get the output of a quantum computation, we measure the qubits we are interested in, and the measurement will always output either a 0 or a 1 classic bit. So we have a classic input in bits, that we translate to quantum qubits, after which we can perform quantum computations. Then, to get the output, we perform a quantum measurement which gives us classic bits out.

For our first quantum program, we will input five 0 bits, then measure the output. We should get 00000 out. The way we program measurement in the IBM QX is with the pink box 

. Drag one to each wire, for a total of five. It doesn't matter which order they are in in this case:

Now, click Simulate, name your experiment Hello Quantum World, click OK, and within seconds you will get the output. Not surprisingly it is 00000 100% of the time:

Congratulations, you just wrote and executed your first quantum program.

Any quantum program can be simulated on a classic computer. The question is, how efficient is the simulation? For small quantum programs, for example ones dealing with five qubits (quantum bits), the results can be easily simulated on a classic computer, like our laptops or desktops at home, or remotely on IBM Cloud compute services. As the programs get larger in terms of the number of quantum bits they manipulate, simulating gets more computationally intense, meaning they won't run efficiently on our own machines, and it is quicker to use IBM's simulation infrastructure. At a certain size, simulating a quantum program in a classic computer is so computationally difficult, it isn't worth bothering. Luckily, IBM and other companies are working on quantum computing that can run such programs efficiently by executing them not in simulation, but in physical hardware.

API key

The IBM QX allows us to interface with IBM's remote quantum hardware and quantum simulation devices. We can execute code to simulate a quantum computer on our local machines, but it is good to have the option to run on IBM's simulation architecture or IBM's quantum computing hardware as we so choose. To do this, we will need an API key. Find the API key within the IBM QX menu under your account name called My Account (It should be in the upper right of the user interface). Click on Advanced, then Regenerate, and then on Copy API Token, to copy your API token. Insert this token into the Chapter 1 Jupyter Notebook and make note of it for future use, or come back to this section when you need it.


Setting up and running Qiskit examples

Qiskit is an open source quantum computing framework for writing quantum experiments, programs, and applications. It provides tools for simulating quantum computation, as well as providing a Python interface to the IBM QX via an API, among many other features. We won't go into Qiskit in detail in this chapter, but we will make sure you can install Qiskit and run examples.

If you followed the instructions to set up a virtual environment for the exercises in this book, Qiskit should already be installed. If not, make sure you have installed Qiskit via pip, your preferred package manager, or the source. Qiskit documentation and source is available at

  1. Check your installation of Qiskit within the Jupyter Notebook of this chapter by executing the following command:
from qiskit import IBMQ
  1. Now that that's working, insert your API token in the following code from the previous subsection:

A variety of backends are available, some physical quantum computing hardware, and other quantum simulators, either locally or in the cloud.

  1. You can get a list of available remote backends with the following code:

Here's an example list:

[<IBMQBackend('ibmqx4') from IBMQ()>, <IBMQBackend('ibmq_16_melbourne') from IBMQ()>, <IBMQBackend('ibmq_qasm_simulator') from IBMQ()>]

As IBM adds and modifies its capabilities, and as you get increased access (some hardware configurations are only available via application or industry partnership), this list may change, so make sure to execute the code yourself and choose a backend off of the list that you have access to. Pick an item off the list that ends in _qasm_simulator as your backend. For example, I might choose the following based on the preceding list:

backend = IBMQ.get_backend('ibmq_qasm_simulator') 

Next, we will execute our first quantum program in simulation.

This will be the same program we executed in the simulator on the IBM QX website by programming it in the quantum composer. This time we will program it in Qiskit. Recall, this program starts with an input of the quantum equivalent of five "0" bits, five quantum "0" bits. It then performs no action before it outputs the classic equivalent of the quantum bits. Since they haven't changed, the output should be 00000. 

We will learn more about this code, line by line, in future chapters. For now, know that for each line of code we can do the same thing in the quantum composer.

  1. Execute your code on the IBM simulator by running the following:
q = qiskit.QuantumRegister(5)
c = qiskit.ClassicalRegister(5)
qc = qiskit.QuantumCircuit(q, c)
qc.measure(q, c)
job_exp = qiskit.execute(qc, backend=backend)

The code may take some time to complete. Now we want to visualize our results. Just as before in the GUI, we can see the results graphically with the following code:

from import plot_histogram

Indeed, we do get 00000 100% of the time.



In this chapter, we learned that quantum computers exploit the properties of quantum physics, superposition and entanglement, to perform some computations much more efficiently than on a classic computer. We learned that while quantum computing has been shown to theoretically offer significant speedup over classic computers in a variety of areas, quantum computers are under development and aren't yet robust enough to exhibit this speedup. We introduced areas which will be impacted by quantum supremacy which include machine learning and artificial intelligence, cryptography and computer security, searching, sampling and optimization, quantum chemistry and quantum dynamics and more. 

In the next section, we will be introducing qubits in more depth, and cover how to simulate, represent, and measure qubits.



Exercises and questions

  1. Create a new quantum score called Playing Around and drag some boxes from the user interface to the wires. Then add in measurement boxes for each of the wires and click Simulate. What was your result?
  2. Try changing the number of qubits and classic bits in your first quantum program from five to two. When you run it, what are your results?
  3. Let's run your first quantum program on an actual quantum computer instead of a simulator! You can choose to do this either from the quantum composer user interface, or from Qiskit. If you choose to do from the quantum composer user interface, do as you did in the Hello quantum world section, only, this time, instead of clicking Simulate, click Run. If you choose to do this from Qiskit, then change the backend to an option beginning with ibmq_5_, and then Run. Note that, in either case, the run will take a lot longer. What is your result? Why do you think it is different from the simulation?
  4. If you wanted a qubit halfway between 0 and 1, where would it lie on the sphere we have used to visualize a qubit?

About the Author

  • Dr. Christine Corbett Moran

    Dr. Christine Corbett Moran is a researcher and engineer at NASA JPL, specializing in cybersecurity. She also serves as a guest scientist at Caltech, specializing in astrophysics. Her research spans fundamental physics and computer science, and she has published peer-reviewed papers on astrophysics, astronomy, artificial intelligence, and quantum computing, garnering thousands of citations. Her software products range from iOS applications to quantum computing simulators and have received millions of downloads. She has a PhD and master's in Astrophysics from the University of Zurich, and a B.S. in Computer Science and Engineering, and a B.S. in Physics from MIT. She can be found on Twitter at @corbett.

    Browse publications by this author

Latest Reviews

(2 reviews total)
It's a book on QC that does not require you to know QM, does not put up a barrier with the algebraic formulations all the time, and simply let's you enjoy this new emerging tech. Just a small note, I recommend using python 3.6+ instead of 3.4+.
I can't download it yet because I got it as a preorder, but I can access its content with the Packt app, looks very complete and throughout im impressed I can get this degree of information on such an advanced and emerging technology at this price.

Recommended For You

Dancing with Qubits

Explore the principles and practicalities of quantum computing

By Robert S. Sutor
Python Machine Learning - Third Edition

Applied machine learning with a solid foundation in theory. Revised and expanded for TensorFlow 2, GANs, and reinforcement learning.

By Sebastian Raschka and 1 more
Deep Learning with TensorFlow 2 and Keras - Second Edition

Build machine and deep learning systems with the newly released TensorFlow 2 and Keras for the lab, production, and mobile devices

By Antonio Gulli and 2 more
Hands-On Neural Networks with TensorFlow 2.0

A comprehensive guide to developing neural network-based solutions using TensorFlow 2.0

By Paolo Galeone