How do you learn to code in this new world that involves both classical and quantum hardware?
One way to do it is to learn classical computing by itself. This is the traditional way of doing it, using a language such as C, C++, JavaScript, Java, Go, or Python. Along the way, you would learn how to use extra functionality in libraries of code along with the programming tools or from a third-party provider. Examples of these are the C++ Standard Library; the Java Platform, Enterprise Edition; the Python Standard Library; or the thousands of Python packages listed in the Python Package Index.
Once you have the philosophy, syntax, structure, and idioms of the classical programming language understood, you then learn quantum computing on top of that. For example, you could use the Qiskit open source quantum computing software development kit (SDK) along with Python. These mesh together and operate exceptionally well. Thousands of people are already Qiskit coders. If you know Python, this is a great approach.
But what if you are learning to code or have only a small amount of experience? What if I could offer you the chance to learn classical and quantum computing in a unified manner? Would it be useful if I could help you understand the concepts of both so that you don’t see them as different disciplines? That’s what I do in this book.
I talk about aspects of quantum computing throughout the book and in many places I provide pointers to sections in my quantum computing book Dancing with Qubits. That book is not a prerequisite for Dancing with Python, but the referenced sections will help you learn more about the topics if you wish to go deeper.
I first talk about qubits, “quantum bits”, in section 1.11. The main chapters that discuss quantum computing are 9 and 11:
9 Understanding Gates and Circuits
Classical computers use logical gates to manipulate bits. Using them, we assemble circuits to implement more complicated processes like addition and multiplication. Eventually, we get all the software that runs on computers everywhere.
Quantum computers use qubits to significantly extend the power of bits, as we saw in section 1.11. We assemble these into quantum circuits to implement algorithms.
There is a strong connection between classical and quantum computing, and a quantum computing system is a classical computing system extended with one or more quantum devices. These devices are the physical implementations of qubits and the software and hardware that control them.
This chapter examines bits and qubits, gates that operate upon them, and how we assemble them into circuits.
9.1 The software stack
9.2 Boolean operations and bit logic gates
9.3 Logic circuits
9.4 Simplifying bit expressions
9.5 Universality for bit gates
9.6 Quantum gates and operations
9.7 Quantum circuits
9.8 Universality for quantum gates
9.9 Summary
11 Searching for the Quantum Improvement
By considering new approaches and getting clever, we can develop classical algorithms that are faster than you might have expected. Using quantum techniques, we can go a step further: perform some operations faster than seems possible.
This chapter compares classical and quantum search techniques to see how extending our basic information unit from the bit to the qubit can show remarkable improvements. Note that I only discuss mainstream “universal” quantum computing and not limited-purpose systems that perform operations like simulated annealing.
11.1 Classical searching
11.2 Quantum searching via Grover
11.3 Oracles
11.4 Inversion about the mean
11.5 Amplitude amplification
11.6 Searching over two qubits
11.7 Summary
If you wish to learn more about quantum computing after reading this book, I suggest you look at
My new book Dancing with Python: Learn Python software development from scratch and get started with quantum computing is now available for purchase from Amazon and Packt Publishing.
Develop skills in Python by implementing exciting algorithms, including mathematical functions, classical searching, data analysis, plotting data, machine learning techniques, and quantum circuits.
Key Features
Learn Python basics to write elegant and efficient code
Create quantum circuits and algorithms using Qiskit and run them on quantum computing hardware and simulators
Delve into Python’s advanced features, including machine learning, analyzing data, and searching
Contributors
About the author
About the reviewer
Contents
List of Figures
Preface
Why did I write this book?
For whom did I write this book?
What does this book cover?
What conventions do I use in this book?
Get in touch
3.1 The big three
3.2 Lists
3.3 The joy of O(1)
3.4 Tuples
3.5 Comprehensions
3.6 What does “Pythonic” mean?
3.7 Nested comprehensions
3.8 Parallel traverse
3.9 Dictionaries
3.10 Sets
3.11 Summary
4 Stringing You Along
4.1 Single, double, and triple quotes
4.2 Testing for substrings
4.3 Accessing characters
4.4 Creating strings
4.5 Strings and iterations
4.6 Strings and slicing
4.7 String tests
4.8 Splitting and stripping
4.9 Summary
7.1 Objects
7.2 Classes, methods, and variables
7.3 Object representation
7.4 Magic methods
7.5 Attributes and properties
7.6 Naming conventions and encapsulation
7.7 Commenting Python code
7.8 Documenting Python code
7.9 Enumerations
7.10 More polynomial magic
7.11 Class variables
7.12 Class and static methods
7.13 Inheritance
7.14 Iterators
7.15 Generators
7.16 Objects in collections
7.17 Creating modules
7.18 Summary
8 Working with Files
8.1 Paths and the file system
8.2 Moving around the file system
8.3 Creating and removing directories
8.4 Lists of files and folders
8.5 Names and locations
8.6 Types of files
8.7 Reading and writing files
8.8 Saving and restoring data
8.9 Summary
II Algorithms and Circuits
9 Understanding Gates and Circuits
9.1 The software stack
9.2 Boolean operations and bit logic gates
9.3 Logic circuits
9.4 Simplifying bit expressions
9.5 Universality for bit gates
9.6 Quantum gates and operations
9.7 Quantum circuits
9.8 Universality for quantum gates
9.9 Summary
10 Optimizing and Testing Your Code
10.1 Testing your code
10.2 Timing how long your code takes to run
10.3 Optimizing your code
10.4 Looking for orphan code
10.5 Defining and using decorators
10.6 Summary
11 Searching for the Quantum Improvement
11.1 Classical searching
11.2 Quantum searching via Grover
11.3 Oracles
11.4 Inversion about the mean
11.5 Amplitude amplification
11.6 Searching over two qubits
11.7 Summary
III Advanced Features and Libraries
12 Searching and Changing Text
12.1 Core string search and replace methods
12.2 Regular expressions
12.3 Introduction to Natural Language Processing
12.4 Summary
13 Creating Plots and Charts
13.1 Function plots
13.2 Bar charts
13.3 Histograms
13.4 Pie charts
13.5 Scatter plots
13.6 Moving to three dimensions
13.7 Summary
14 Analyzing Data
14.1 Statistics
14.2 Cats and commas
14.3 pandas DataFrames
14.4 Data cleaning
14.5 Statistics with pandas
14.6 Converting categorical data
14.7 Cats by gender in each locality
14.8 Are all tortoiseshell cats female?
14.9 Cats in trees and circles
14.10 Summary
15 Learning, Briefly
15.1 What is machine learning?
15.2 Cats again
15.3 Feature scaling
15.4 Feature selection and reduction
15.5 Clustering
15.6 Classification
15.7 Linear regression
15.8 Concepts of neural networks
15.9 Quantum machine learning
15.10 Summary
Appendices
A Tools
A.1 The operating system command line
A.2 Installing Python
A.3 Installing Python modules and packages
A.4 Installing a virtual environment
A.5 Installing the Python packages used in this book
A.6 The Python interpreter
A.7 IDLE
A.8 Visual Studio Code
A.9 Jupyter notebooks
A.10 Installing and setting up Qiskit
A.11 The IBM Quantum Composer and Lab
A.12 Linting
I’m pleased to announce that the Amazon Kindle version of my quantum computing book Dancing with Qubits is now available!
This book provides a comfortable and conversational introduction to quantum computing. I take you through the mathematics you need at a pace that allows you to understand not just “what” but also “why.” When we get to quantum computing, concepts like superposition and entanglement are shown to be natural ideas building on what we’ve already seen, and then illustrated via gates, circuits, and algorithms.
Throughout the book, I highlight important results, provide questions to answer, and give links to references where you can learn more. This allows the book to be used for self-study or as a textbook.
Important ideas like Quantum Volume are explained to give you a head start for reading more advanced texts and research papers. I provide many references to related content in math, physics, quantum computing, AI, and financial services. Dancing with Qubits concludes with questions for you to think about and ask experts so that you can gauge progress in the field over the next few years.
Features of the Kindle edition
The text will get larger or smaller as you wish and you can change to a font that is comfortable for you to read.
There are links throughout the book to other sections and the references in each chapter.
Many of the references have links to external sources, such as arxiv or Nature for research papers.
The content is in color, if your Kindle device supports it.
You can search for terms throughout the book.
I’ve maximized the number of mathematical expressions that are expressed textually (see below) to improve the reading experience.
The print version of Dancing with Qubits still has the full, rich mathematical formatting, albeit in black and white. In essence, whether you choose the print or Kindle version, the content is consistent and the formatting is the best I know how to produce for each medium.
Technical Notes
Here are a few comments about the production of the Kindle version, in case you are interested.
The original content for Dancing with Qubits is in LaTeX. From that I can produce the black and white print version, a color PDF eBook, and an epub3 file from which the Amazon Kindle and several other MOBI eBook versions are created.
I used make4ht and tex4ht to go from the LaTeX source files to HTML. While very powerful, the documentation is scarce and I spent many hours trying to figure how to make things work and then writing sed and Python scripts to fix things that were not quite right.
I wrote Python scripts to create the various files needed for epub3, such as opf and navigation, and to break the 30,000+ line HTML file into smaller XHTML files. I used tidy several times to format the HTML and XHTML.
The epub3 validators in several free epub3 editing apps either skipped problems entirely or gave false negatives. I found pagina EPUB-Checker to be the best software for validation.
I wanted to maximize the amount of HTML formatting I could use and MathML is not available in a practical sense for all eBook formats. tex4ht produced very inconsistent results. So while I could express $x_2$ as x2 in the text without extra fonts, more two-dimensional objects like matrices had to be represented using images. I created macros to produce the right format based on what kind of document I was trying to produce.
I used tikz/pgf and quantikz for the figures, especially the quantum circuit diagrams. I externalized the figures as JPEG images. It took quite a bit to figure out how to get them to be the right size for the Kindle version.
Some math expressions in the book and chapter tables of contents have weird spacing if they involve subscripts or superscripts. This is an artifact of the Kindle software. This did not happen, for example, when I viewed the book in the Apple Books app.
Yesterday was very exciting because I received my first printed copy of the book. There’s just something about holding a physical, printed book that you’ve labored over for many months. Others are starting to get their copies too, and I hope that within a couple of weeks everyone who pre-ordered the print version will have copies in hand.
What if you ordered the eBook? Wasn’t that an option on Amazon? Why isn’t it listed there now? Why was your Kindle eBook order canceled?
As for Amazon, let’s just say that they ultimately wanted a reflowable version of the book rather than a fixed format version. As I’ve mentioned before, this presents many challenges to producing beautiful math. The reflowable format allows you to change the font and font size you use on your Kindle or in the Kindle app. It makes it easier to read on small devices. I understand the value.
Hence, I’m now working to create a reflowable version with one guiding principle: as much as possible, I must have a single source for the book that will produce future versions of the printed, fixed-format PDF, and reflowable formats
Here’s the strategy:
Use tools like make4ht to translate the original LaTeX into HTML. Convert the HTML into MOBI and EPUB3 formats with a minimal amount of hand editing.
Use standard HTML entities and CSS for as much of the in-sentence math as possible.
Avoid using extra fonts within the HTML.
Use automatically-generated PNG files for any other math.
Use automatically-generated SVG and PNG files for figures coming from LaTeX tikz environments. (This is not working at all right now.)
Use new macros for the math to produce either LaTeX or HTML versions.
Use a small Python program I wrote to convert simple math to new markup using the new macros.
There is much work to do but things are looking promising. I can’t now give you any estimated time of arrival for the Kindle reflowable version or any guarantee that it will arrive eventually, but it’s my strong intention to make it happen. There are unknown unknowns yet to be discovered.
A final point: despite several people staring at the text, a few errors crept in. These are mostly typos or omitted words. I’m keeping track of these on the corrections and clarifications page for the book. I incorporate these fixes into the text as they are discovered and I hope that future versions of the book, printed and digital, include them.
Before I discuss what and I how I wrote, let me talk about the markup of the book. By “markup” I mean the underlying format of the content that determines its structure such as the title page, table of contents, parts, chapters, sections, paragraphs, bibliography, and the index, along with font styles and sizes.
In my experience, most publishers, both traditional and online, prefer you to use Microsoft Word to create the book, and it has its own underlying markup language that you typically never see. In a more-or-less what-you-see-is-what-you-get way, you can write and style the book.The publishing workflow is often based on this choice.
My requirements for the book creation process included:
beautiful math rendering, both in sentences and displayed multi-part formulas,
built-in support for generating diagrams,
easy methods to change formatting throughout the book quickly, and
good support for working quickly on a large text.
Regarding the size of the book, in early 2019 I thought the book would come in around 300 pages and I would have a complete draft on September 1. I ended up writing a book with slightly more than 500 pages with the first full draft delivered on October 9. I had full drafts of various chapters before then, but that was the first time there were no sections with TODO markers.
Word has come a long way on many of these requirements, especially the math, though it can be very laborious to create a book with hundreds or thousands of formulas. Here’s the real problem though: eBooks with math in them often look terrible if you put them in a reflowable format. That is, if you let, say, your Amazon Kindle change the fonts and the line widths, the math just doesn’t look right.
People argue about this forever, but there is an excellent chance that you will end up with fuzzy, misaligned expressions that are the wrong size compared to the surrounding text. So, I early on made the decision that the eBook would not be reflowable. Since that was the case, there was no reason for me to stick with Word. I decided to markup the book in LaTeX. Luckily, Andrew Waldron at Packt Publishing agreed. [Though see this later development regarding the eBook.]
With LaTeX, you have complete and arbitrary control over all parts of the formatting. There are thousands of packages that make your life easier by providing significant functionality that you would not want to write yourself.
LaTeX has
the best math formatting facilities of any system,
a full macro programming language for formatting control and calculations, and
easy ways to break a document into sections so you can work on one part at a time.
If you get into macro programming, things can get complicated. I’ve been doing it for 30 years, so it doesn’t faze me. Here are two good books on LaTeX to get you started: