Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Dancing with Python

You're reading from   Dancing with Python Learn to code with Python and Quantum Computing

Arrow left icon
Product type Paperback
Published in Aug 2021
Publisher Packt
ISBN-13 9781801077859
Length 744 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Robert S. Sutor Robert S. Sutor
Author Profile Icon Robert S. Sutor
Robert S. Sutor
Arrow right icon
View More author details
Toc

Table of Contents (29) Chapters Close

Preface 1. Chapter 1: Doing the Things That Coders Do 2. Part I: Getting to Know Python FREE CHAPTER
3. Chapter 2: Working with Expressions 4. Chapter 3: Collecting Things Together 5. Chapter 4: Stringing You Along 6. Chapter 5: Computing and Calculating 7. Chapter 6: Defining and Using Functions 8. Chapter 7: Organizing Objects into Classes 9. Chapter 8: Working with Files 10. PART II: Algorithms and Circuits
11. Chapter 9: Understanding Gates and Circuits 12. Chapter 10: Optimizing and Testing Your Code 13. Chapter 11: Searching for the Quantum Improvement 14. PART III: Advanced Features and Libraries
15. Chapter 12: Searching and Changing Text 16. Chapter 13: Creating Plots and Charts 17. Chapter 14: Analyzing Data 18. Chapter 15: Learning, Briefly 19. References
20. Other Books You May Enjoy
21. Index
Appendices
1. Appendix A: Tools 2. Appendix B: Staying Current 3. Appendix C: The Complete UniPoly Class
4. Appendix D: The Complete Guitar Class Hierarchy
5. Appendix E: Notices 6. Appendix F: Production Notes

1.1 Data

If you think of all the digital data and information stored in the cloud, on the web, on your phone, on your laptop, in hard and solid-state drives, and in every computer transaction, consideration comes down to just two things: 0 and 1.

These are bits, and with bits we can represent everything else in what we call “classical computers.” These systems date back to ideas from the 1940s. There is an additional concept for quantum computers, that of a qubit or “quantum bit.” The qubit extends the bit and is manipulated in quantum circuits and gates. Before we get to qubits, however, let’s consider the bit more closely.

First, we can interpret 0 as false and 1 as true. Thinking in terms of data, what would you say if I asked you the question, “Do you like Punk Rock?” We can store your response as 0 or 1 and then use it in further processing, such as making music recommendations to you. When we talk about true and false, we call them Booleans instead of bits.

Second, we can treat 0 and 1 as the numbers 0 and 1. While that’s nice, if the maximum number we can talk about is 1, we can’t do any significant computation. So, we string together more bits to make larger numbers. The binary numbers 00, 01, 10, and 11 are the same as 0, 1, 2, and 3 when we use a decimal representation. Using even more bits, we represent 72 decimal as 1001000 binary and 83,694 decimal as 10100011011101110 binary.

decimal: 72 = 7 × 101 + 2 × 100
binary: 1001000 = 1 × 26 + 0 × 25 + 0 × 24 + 1 × 23 + 0 × 22 + 0 × 21 + 0 × 20

Exercise 1.1

How would you represent 245 decimal in binary? What is the decimal representation of 1111 binary?

With slightly more sophisticated representations, we can store and use negative numbers. We can also create numbers with decimal points, also known as floating-point numbers. [FPA] We use floating-point numbers to represent or approximate real numbers. Whatever programming language we use, we must have a convenient way to work with all these kinds of numbers.

When we think of information more generally, we don’t just consider numbers. There are words, sentences, names, and other textual data. In the same way that we can encode numbers using bits, we create characters for text. Using the Extended ASCII standard, for example, we can create my nickname, “Bob”: [EAS]

01000010 → B
01101111 → o
01100010 → b

Each run of zeros and ones on the left-hand side has 8 bits. This is a byte. One thousand (103) bytes is a kilobyte, one million (106) is a megabyte, and one billion (109) is a gigabyte.

If we limit ourselves to a byte to represent characters, we can only represent 256 = 28 of them. If you look at the symbols on a keyboard, then imagine other alphabets, letters with accents and umlauts and other marks, mathematical symbols, and emojis, you will count well more than 256. In programming, we use the Unicode standard to represent many sets of characters and ways of encoding them using multiple bytes. [UNI]

Exercise 1.2

How can you create 256 different characters if you use 8 bits? How many could you form if you used 7 or 10?

When we put characters next to each other, we get strings. Thus "abcd" is a string of length four. I’ve used the double quotes to delimit the beginning and end of the string. They are not part of the string itself. Some languages treat characters as special objects unto themselves, while others consider them to be strings of length one.

This is a good start for our programming needs: we have bits and Booleans, numbers, characters, and strings of multiple characters to form text. Now we need to do something with these kinds of data.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime