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
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
Terminal.app 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Â https://github.com/PacktPublishing/Mastering-Quantum-Computing-with-IBM-QX. 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.
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.Â
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.
- 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
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
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.
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
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.
Â 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.
- 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.
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
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.
- Clone the repository containing all the code examples with the following command:
git clone https://github.com/PacktPublishing/Mastering-Quantum-Computing-with-IBM-QX
- Navigate to the directory using the following command:
- Create a virtual environment in which to run all code examples using the following command:
python -m venv MQC
- 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:
- Next install the
requirements.txtfile present in the GitHub link, for running chapter examples and attempting chapter projects by using the following command:
pip install -r requirements.txt
- Check your installation by running the following command:
- 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Â
pip, your preferred package manager, or the source.
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
bookkernel in the notebook's UI. To test out your Jupyter Notebook setup, run the following command:
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Â https://jupyter-notebook.readthedocs.io/en/stable/.
- Setup a new account atÂ https://quantumexperience.ng.bluemix.net/qx/signup.
- After you sign in with your new account, click on the
Composertab under the
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.
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:
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.
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Â
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.
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Â https://github.com/QISKit.
- Check your installation of Qiskit within the Jupyter Notebook of this chapter by executing the following command:
from qiskit import IBMQ
- 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.
- 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
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.
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 qiskit.tools.visualization import plot_histogram plot_histogram(job_exp.result().get_counts(qc))
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.
- Create a new quantum score called
Playing Aroundand 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?
- 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?
- 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
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?
- If you wanted a qubit halfway between 0 and 1, where would it lie on the sphere we have used to visualize a qubit?