Dancing with Python

Book description

Develop skills in Python and Quantum Computing by implementing exciting algorithms, mathematical functions, classical searching, data analysis, plotting data, machine learning techniques, and quantum circuits.

Key Features

  • Create quantum circuits and algorithms using Qiskit and run them on quantum computing hardware and simulators
  • Learn the Pythonic way to write elegant and efficient code
  • Delve into Python's advanced features, including machine learning, analyzing data, and searching

Book Description

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.

What you will learn

  • Explore different quantum gates and build quantum circuits with Qiskit and Python
  • Write succinct code the Pythonic way using magic methods, iterators, and generators
  • Analyze data, build basic machine learning models, and plot the results
  • Search for information using the quantum Grover Search Algorithm
  • Optimize and test your code to run efficiently

Who this book is for

The book will help you get started with coding for Python and Quantum Computing.

Basic familiarity with algebra, geometry, trigonometry, and logarithms is required as the book does not cover the detailed mathematics and theory of quantum computing. You can check out the author's Dancing with Qubits book, also published by Packt, for an approachable and comprehensive introduction to quantum computing.

Table of contents

  1. Preface
    1. Why did I write this book?
    2. For whom did I write this book?
    3. What does this book cover?
    4. What conventions do I use in this book?
    5. Download the example code files
    6. Download the color images
    7. Get in touch
  2. Chapter 1: Doing the Things That Coders Do
    1. 1.1 Data
    2. 1.2 Expressions
    3. 1.3 Functions
    4. 1.4 Libraries
    5. 1.5 Collections
    6. 1.6 Conditional processing
    7. 1.7 Loops
    8. 1.8 Exceptions
    9. 1.9 Records
    10. 1.10 Objects and classes
    11. 1.11 Qubits
    12. 1.12 Circuits
    13. 1.13 Summary
  3. Part I: Getting to Know Python
  4. Chapter 2: Working with Expressions
    1. 2.1 Numbers
    2. 2.2 Strings
    3. 2.3 Lists
    4. 2.4 Variables and assignment
    5. 2.5 True and False
    6. 2.6 Arithmetic
    7. 2.7 String operations
    8. 2.8 List operations
    9. 2.9 Printing
    10. 2.10 Conditionals
    11. 2.11 Loops
    12. 2.12 Functions
    13. 2.13 Summary
  5. Chapter 3: Collecting Things Together
    1. 3.1 The big three
    2. 3.2 Lists
    3. 3.3 The joy of O(1)
    4. 3.4 Tuples
    5. 3.5 Comprehensions
    6. 3.6 What does “Pythonic” mean?
    7. 3.7 Nested comprehensions
    8. 3.8 Parallel traverse
    9. 3.9 Dictionaries
    10. 3.10 Sets
    11. 3.11 Summary
  6. Chapter 4: Stringing You Along
    1. 4.1 Single, double, and triple quotes
    2. 4.2 Testing for substrings
    3. 4.3 Accessing characters
    4. 4.4 Creating strings
    5. 4.5 Strings and iterations
    6. 4.6 Strings and slicing
    7. 4.7 String tests
    8. 4.8 Splitting and stripping
    9. 4.9 Summary
  7. Chapter 5: Computing and Calculating
    1. 5.1 Using Python modules
    2. 5.2 Integers
    3. 5.3 Floating-point numbers
    4. 5.4 Rational numbers
    5. 5.5 Complex numbers
    6. 5.6 Symbolic computation
    7. 5.7 Random numbers
    8. 5.8 Quantum randomness
    9. 5.9 Summary
  8. Chapter 6: Defining and Using Functions
    1. 6.1 The basic form
    2. 6.2 Parameters and arguments
    3. 6.3 Naming conventions
    4. 6.4 Return values
    5. 6.5 Keyword arguments
    6. 6.6 Default argument values
    7. 6.7 Formatting conventions
    8. 6.8 Nested functions
    9. 6.9 Variable scope
    10. 6.10 Functions are objects
    11. 6.11 Anonymous functions
    12. 6.12 Recursion
    13. 6.13 Summary
  9. Chapter 7: Organizing Objects into Classes
    1. 7.1 Objects
    2. 7.2 Classes, methods, and variables
    3. 7.3 Object representation
    4. 7.4 Magic methods
    5. 7.5 Attributes and properties
    6. 7.6 Naming conventions and encapsulation
    7. 7.7 Commenting Python code
    8. 7.8 Documenting Python code
    9. 7.9 Enumerations
    10. 7.10 More polynomial magic
    11. 7.11 Class variables
    12. 7.12 Class and static methods
    13. 7.13 Inheritance
    14. 7.14 Iterators
    15. 7.15 Generators
    16. 7.16 Objects in collections
    17. 7.17 Creating modules
    18. 7.18 Summary
  10. Chapter 8: Working with Files
    1. 8.1 Paths and the file system
    2. 8.2 Moving around the file system
    3. 8.3 Creating and removing directories
    4. 8.4 Lists of files and folders
    5. 8.5 Names and locations
    6. 8.6 Types of files
    7. 8.7 Reading and writing files
    8. 8.8 Saving and restoring data
    9. 8.9 Summary
  11. PART II: Algorithms and Circuits
  12. Chapter 9: Understanding Gates and Circuits
    1. 9.1 The software stack
    2. 9.2 Boolean operations and bit logic gates
    3. 9.3 Logic circuits
    4. 9.4 Simplifying bit expressions
    5. 9.5 Universality for bit gates
    6. 9.6 Quantum gates and operations
    7. 9.7 Quantum circuits
    8. 9.8 Universality for quantum gates
    9. 9.9 Summary
  13. Chapter 10: Optimizing and Testing Your Code
    1. 10.1 Testing your code
    2. 10.2 Timing how long your code takes to run
    3. 10.3 Optimizing your code
    4. 10.4 Looking for orphan code
    5. 10.5 Defining and using decorators
    6. 10.6 Summary
  14. Chapter 11: Searching for the Quantum Improvement
    1. 11.1 Classical searching
    2. 11.2 Quantum searching via Grover
    3. 11.3 Oracles
    4. 11.4 Inversion about the mean
    5. 11.5 Amplitude amplification
    6. 11.6 Searching over two qubits
    7. 11.7 Summary
  15. PART III: Advanced Features and Libraries
  16. Chapter 12: Searching and Changing Text
    1. 12.1 Core string search and replace methods
    2. 12.2 Regular expressions
    3. 12.3 Introduction to Natural Language Processing
    4. 12.4 Summary
  17. Chapter 13: Creating Plots and Charts
    1. 13.1 Function plots
    2. 13.2 Bar charts
    3. 13.3 Histograms
    4. 13.4 Pie charts
    5. 13.5 Scatter plots
    6. 13.6 Moving to three dimensions
    7. 13.7 Summary
  18. Chapter 14: Analyzing Data
    1. 14.1 Statistics
    2. 14.2 Cats and commas
    3. 14.3 pandas DataFrames
    4. 14.4 Data cleaning
    5. 14.5 Statistics with pandas
    6. 14.6 Converting categorical data
    7. 14.7 Cats by gender in each locality
    8. 14.8 Are all tortoiseshell cats female?
    9. 14.9 Cats in trees and circles
    10. 14.10 Summary
  19. Chapter 15: Learning, Briefly
    1. 15.1 What is machine learning?
    2. 15.2 Cats again
    3. 15.3 Feature scaling
    4. 15.4 Feature selection and reduction
    5. 15.5 Clustering
    6. 15.6 Classification
    7. 15.7 Linear regression
    8. 15.8 Concepts of neural networks
    9. 15.9 Quantum machine learning
    10. 15.10 Summary
  20. Appendices
  21. Appendix A: Tools
    1. A.1 The operating system command line
    2. A.2 Installing Python
    3. A.3 Installing Python modules and packages
    4. A.4 Installing a virtual environment
    5. A.5 Installing the Python packages used in this book
    6. A.6 The Python interpreter
    7. A.7 IDLE
    8. A.8 Visual Studio Code
    9. A.9 Jupyter notebooks
    10. A.10 Installing and setting up Qiskit
    11. A.11 The IBM Quantum Composer and Lab
    12. A.12 Linting
  22. Appendix B: Staying Current
    1. B.1 python.org
    2. B.2 qiskit.org
    3. B.3 Python expert sites
    4. B.4 Asking questions and getting answers
  23. Appendix C: The Complete UniPoly Class
  24. Appendix D: The Complete Guitar Class Hierarchy
  25. Appendix E: Notices
    1. E.1 Photos, images, and diagrams
    2. E.2 Data
    3. E.3 Trademarks
    4. E.4 Python 3 license
  26. Appendix F: Production Notes
  27. References
  28. Other Books You May Enjoy
  29. Index
    1. Index Formatting Examples

Product information

  • Title: Dancing with Python
  • Author(s): Robert S. Sutor
  • Release date: August 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781801077859