#### Overview of this book

Dancing with Python helps you learn Python and quantum computing in a practical way. It will help you explore how to work with numbers, strings, collections, iterators, and files. The book goes beyond functions and classes and teaches you to use Python and Qiskit to create gates and circuits for classical and quantum computing. Learn how quantum extends traditional techniques using the Grover Search Algorithm and the code that implements it. Dive into some advanced and widely used applications of Python and revisit strings with more sophisticated tools, such as regular expressions and basic natural language processing (NLP). The final chapters introduce you to data analysis, visualizations, and supervised and unsupervised machine learning. By the end of the book, you will be proficient in programming the latest and most powerful quantum computers, the Pythonic way.
Free Chapter
Chapter 1: Doing the Things That Coders Do
Part I: Getting to Know Python
Chapter 2: Working with Expressions
Chapter 3: Collecting Things Together
Chapter 4: Stringing You Along
Chapter 5: Computing and Calculating
Chapter 6: Defining and Using Functions
Chapter 7: Organizing Objects into Classes
Chapter 8: Working with Files
PART II: Algorithms and Circuits
Chapter 9: Understanding Gates and Circuits
Chapter 10: Optimizing and Testing Your Code
Chapter 11: Searching for the Quantum Improvement
PART III: Advanced Features and Libraries
Chapter 12: Searching and Changing Text
Chapter 13: Creating Plots and Charts
Chapter 14: Analyzing Data
Chapter 15: Learning, Briefly
References
Other Books You May Enjoy
Index
Appendices
Appendix B: Staying Current
Appendix C: The Complete UniPoly Class
Appendix D: The Complete Guitar Class Hierarchy
Appendix E: Notices
Appendix F: Production Notes

# 1.11 Qubits

In the first section of this chapter, we looked at data. Starting with the bits 0 and 1, we built integers, and I indicated that we could also represent floating-point numbers. Let’s switch from the arithmetic and algebra of numbers to geometry.

A single point is 0-dimensional. Two points are just two 0-dimensional objects. Let’s label these points 0 and 1. When we draw an infinite line through the two points and consider all the points on the line, we get a 1-dimensional object. We can represent each such point as (x), or just x, where x is a real number. If we have another line drawn vertically and perpendicular to the first, we get a plane. A plane is 2-dimensional, and we can represent every point in it with coordinates (x, y). The point (0, 0) is the origin.

This is the Cartesian geometry you learned in school. As we move from zero dimensions to two in Figure 1.7, we can represent more information. In an imprecise sense, we have more room in which to work. We can even link together the geometry of the plane with complex numbers, which extend the real numbers. We see those in section 5.5.

Now I want you to think about taking the standard 2-dimensional plane and wrapping it completely around a sphere. If that request strikes you as bizarre, consider this: start with a sphere and balance the plane on top. Make the origin of the plane sit right on the north pole of the sphere. Now start uniformly bending the plane down over the sphere, compressing it together as you move out from the origin.

Carry this to its completion, and the “infinity” ∞ of the plane sits at the south pole. In mathematical terms, this is a stereographic projection of the plane onto the sphere.

That was mind-warping as well as plane-warping! The sphere sits in three dimensions, but we can think about its surface as two-dimensional.

Exercise 1.12

We use longitude and latitude measured in degrees to locate any location on our planet. Those are the two coordinates for the “dimensions” of the surface of the Earth. Look at an image of the planet with longitude lines drawn and see how they get closer as you approach the poles. This is very different from the usual xy-plane, where the lines stay equally spaced.

With that geometric discussion as the warm-up, I am now ready to introduce the qubit, which is short for “quantum bit.” While a bit can only be 0 or 1, a qubit can exist in more states. Qubits are surprising, fascinating, and powerful. They follow strange rules which may not initially seem natural to you. According to physics, these rules may be how nature itself works at the level of electrons and photons.

A qubit starts in an initial state. We use the notation |0⟩ and |1⟩ when we are talking about a qubit instead of the 0 and 1 for a bit. For a bit, the only non-trivial operation you can perform is switching 0 to 1 and vice versa. We can move a qubit’s state to any point on the sphere shown in the center of Figure 1.9. We can represent more information and have more room in which to work.

This sphere is called the Bloch sphere, named after physicist Felix Bloch.

Things get even better when we have multiple qubits. One qubit holds two pieces of information, and two qubits hold four. That’s not surprising, but if we add a third qubit, we can represent eight pieces of information. Every time we add a qubit, we double its capacity. For 10 qubits, that’s 1,024. For 100 qubits, we can represent 1,267,650,600,228,229,401,496,703,205,376 pieces of information. This illustrates exponential behavior since we are looking at 2the number of qubits.

You’re going to see how to use bits, numbers, qubits, strings, and many other useful objects and structures as we progress through this book. Before we leave this section, let me point out several weird yet intriguing qubit features.

• While we can perform operations and change the state of a qubit, the moment we look at the qubit, the state collapses to 0 or 1. We call the operation that moves the qubit state to one of the two bit states “measurement.”
• Just as we saw that bits have meaning when they are parts of numbers and strings, presumably the measured qubit values 0 and 1 have meaning as data.
• Probability is involved in determining whether we get 0 or 1 at the end.
• We use qubits in algorithms to take advantage of their exponential scaling and other underlying mathematics. With these, we hope eventually to solve some significant but currently intractable problems. These are problems for which classical systems alone will never have enough processing power, memory, or accuracy.

Scientists and developers are now creating quantum algorithms for use cases in financial services and artificial intelligence (AI). They are also looking at precisely simulating physical systems involving chemistry. These may have future applications in materials science, agriculture, energy, and healthcare.