Book Image

Dancing with Python

By : Robert S. Sutor
Book Image

Dancing with Python

By: Robert S. Sutor

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.
Table of Contents (29 chapters)
2
Part I: Getting to Know Python
10
PART II: Algorithms and Circuits
14
PART III: Advanced Features and Libraries
19
References
20
Other Books You May Enjoy
Appendices
Appendix C: The Complete UniPoly Class
Appendix D: The Complete Guitar Class Hierarchy
Appendix F: Production Notes

1.6 Conditional processing

When you write code, you create instructions for a computer that state what steps the computer must take to accomplish some task. In that sense, you are specifying a recipe. Depending on some conditions, like if a bank balance is positive or a car’s speed exceeds a stated limit, the sequence of steps may vary. We call how your code moves from one step to another its flow.

Let’s make some bread to show where a basic recipe has points where we should make choices.

Photo of two loaves of bread
Figure 1.1: Loaves of bread. Photo by the author.

Ingredients

  • 1 ½ tablespoons sugar
  • ¾ cup warm water (slightly warmer than body temperature)
  • 2 ¼ teaspoons active dry yeast (1 package)
  • 1 teaspoon salt
  • ½ cup milk
  • 1 tablespoon unsalted butter
  • 2 cups all-purpose flour

Straight-through recipe

  1. Combine sugar, water, and yeast in a large bowl. Let sit for 10 minutes.
  2. Mix in the salt, milk, butter, and flour. Stir with a spatula or wooden spoon for 5 minutes.
  3. Turn out the dough onto a floured counter or cutting board. Knead for 10 minutes.
  4. Butter a large bowl, place the dough in it, turn the dough over to cover with butter.
  5. Cover the bowl and let it sit in a warm place for 1 hour.
  6. Punch down the dough and knead for 5 minutes. Place in a buttered baking pan for 30 minutes.
  7. Bake in a pre-heated 375° F (190° C, gas mark 5) oven for 45 minutes.
  8. Turn out the loaf of bread and let it cool.

Problems

I call that a “straight-through” recipe because you do one step after another from the beginning to the end. The flow proceeds in a straight line with no detours. However, the recipe is too simple because it does not include conditions that must be met before moving to later steps.

  • In step 1, your bread will not rise if the yeast is dead. We must ensure that the mixture is active and foamy.
  • In step 2, we need to add more flour in little portions until the stirred dough pulls away from the bowl.
  • In step 3, if the dough gets too sticky, we need to add even more flour.
  • In step 5, an hour is only an estimate for the time it will take for the dough to double in size. It may take more or less time.
  • In step 6, we want the dough to double in size in the loaf pan.
  • In step 7, 45 minutes is an estimate for the baking time for the top of the bread to turn golden brown.

Conditional recipe

  1. Combine sugar, water, and yeast in a large bowl. Let sit for 10 minutes. If the mixture is foamy, then continue to the next step. Otherwise, discard the mixture, and combine sugar, water, and fresh yeast in a large bowl. Let sit for 10 minutes.
  2. Mix in the salt, milk, butter, and flour. Stir with a spatula or wooden spoon for 5 minutes. If the mixture does not pull aside from the sides of the bowl, then add 1 tablespoon of flour and stir for 1 more minute. Otherwise, continue to the next step.
  3. Turn out the dough onto a floured counter or cutting board. Knead for 10 minutes. If the mixture is sticky, then add 1 tablespoon of flour and stir for 1 more minute. Otherwise, continue to the next step.
  4. Butter a large bowl, place the dough in it, turn the dough over to cover with butter.
  5. Cover the bowl and let it sit in a warm place for 1 hour. If the dough has not doubled in size, then let it sit covered in the bowl for 15 more minutes. Otherwise, continue to the next step.
  6. Punch down the dough and knead for 5 minutes. Place in a buttered baking pan for 30 minutes. If the dough has not doubled in size, then let it sit covered in the baking pan for 15 more minutes. Otherwise, continue to the next step.
  7. Bake in a pre-heated 375° F (190° C, gas mark 5) oven for 45 minutes. If the top of the bread is not golden brown, then bake for 5 more minutes. Otherwise, continue to the next step.
  8. Turn out the loaf of bread and let cool.

Figure 1.2 is a flowchart, and it shows what is happening in the first step. The rectangles are things to do, and the diamond is a decision to be made that includes a condition.

A basic flowchart with a condition
Figure 1.2: A flowchart for the first step of bread making

You may find it useful to use such a chart to map out the flow of your code.

These instructions are better. We test several conditions to determine the next course of action. We still are not checking whether a condition is met after repeating a step multiple times.