Overview

Appendix A. Math refresher

This appendix offers a concise refresher on the mathematics most often encountered when writing and reasoning about quantum programs. It connects what a quantum computer returns—measurement outcomes—to binary strings, and it frames quantum states as collections of amplitudes whose magnitudes and directions determine measurement probabilities. The discussion balances intuition and computation: it motivates why programmers need fluency with bit-level representations today, and it shows how complex numbers provide a compact, practical language for working with amplitudes, interference, and simple gate actions.

The review of binary strings starts with base conversion, showing how each bit encodes a power of two and how adding a bit doubles the representable range (2^n patterns for n bits). It introduces binary trees as a visual tool for building and interpreting strings, and explains how concatenation, prefixes, and suffixes naturally compose or divide bitstrings (and their tree paths). Finally, it outlines how to encode negative integers using the two’s complement mapping: the available bit patterns are split so that the first half represent non-negative values and the second half represent negative values, yielding a seamless wraparound that aligns with common computing practice.

The treatment of amplitudes uses two complementary views. Geometrically, amplitudes are vectors beginning at the origin and confined to the unit circle, with squared magnitudes summing to one; algebraically, they are complex numbers, added and multiplied componentwise using i^2 = −1, with magnitude and phase also expressible in polar form. The appendix introduces conjugation and its role in computing magnitudes, notes Python’s built-in complex type (using j), and sketches how quantum gates recombine pairs of amplitudes via 2×2 complex transforms while preserving total probability. To aid intuition and debugging, it proposes a color-bar visualization: map phase (direction) to hue on a color wheel and magnitude to bar length, producing clear, compact depictions of an entire quantum state.

Figure A.1 The number 104 written in decimal (base 10) form.
APPA F01 gonciulea
Figure A.2 Example of representing the same number in decimal form and binary form.
APPA F02 gonciulea
Figure A.3 The decimal value of binary strings of length one, two, and three.
APPA F03 gonciulea
Figure A.4 Adding a digit to the binary string means two times as many values can be represented.
APPA F04 gonciulea
Figure A.5 Building three-digit binary strings by prepending 0 and 1 to the two-digit strings
APPA F05 gonciulea
Figure A.6 Binary tree view of binary strings of length one, two, and three.
APPA F06 gonciulea
Figure A.7 The binary strings of length three for encoding non-negatives (left) and encoding negatives (right).
APPA F07 gonciulea
Figure A.8 A vector defined by a magnitude and a direction.
APPA F08 gonciulea
Figure A.9 A "clock" representation of a quantum amplitude inside the unit circle.
APPA F09 gonciulea
Figure A.10 The (x, y) coordinates of the endpoint of a vector define a complex number \(z = x + iy\).
APPA F10 gonciulea
Figure A.12 Mapping an amplitude to a colored bar.
APPA F12 gonciulea
Table A.1 A tabular representation of the state of a three-qubit quantum system using colored bars.
APPA T01 gonciulea

FAQ

Why are quantum measurement outcomes represented as binary strings?Because each measured qubit collapses to a classical bit value, 0 or 1. A full measurement across multiple qubits yields a sequence of bits, which we write as a binary string. Interpreting quantum results therefore requires comfort with binary representations.
How do I convert a binary string to a decimal value?Scan bits from right to left, multiply each bit by its corresponding power of 2, and sum. For example, 1011 = 1·2^3 + 0·2^2 + 1·2^1 + 1·2^0 = 8 + 0 + 2 + 1 = 11. In general, with bits b_{n-1}…b_1b_0, the value is Σ b_k·2^k.
How many numbers can n bits represent, and what changes when I add one more bit?n bits represent 2^n distinct values. Adding one bit doubles the representable values (from 2^n to 2^(n+1)) because it introduces a new highest power of two.
How can I visualize all binary strings of a given length using a binary tree?Use a tree where each level corresponds to one bit: branch 0 “up” and 1 “down” (or vice versa). A path from the root to a leaf encodes a unique binary string. Adding a level adds one bit and doubles the leaves (strings).
How are negative integers encoded with a fixed number of bits (two’s complement), and what range do n bits cover?Split the 2^n codes into two halves. The first half (starting with MSB 0) encodes 0 to 2^(n−1)−1. The second half (starting with MSB 1) encodes negatives by subtracting 2^n from the unsigned value, yielding −2^(n−1) to −1. Example for n=3: 100–111 map to −4, −3, −2, −1.
What is a quantum amplitude and why must the sum of squared magnitudes equal 1?An amplitude is a complex-valued “arrow” (vector) associated with each possible outcome; its squared magnitude is that outcome’s probability contribution. Probabilities must sum to 1, so the sum of all amplitude squared magnitudes equals 1. Each amplitude’s magnitude is therefore ≤ 1, and all arrows fit inside the unit circle.
How do complex numbers represent amplitudes in algebraic (rectangular) form, and how do I add/multiply them?Write z = x + i y with real x and y (Python uses j for i). Addition: (x0 + i y0) + (x1 + i y1) = (x0 + x1) + i(y0 + y1). Multiplication: (x0 + i y0)(x1 + i y1) = (x0x1 − y0y1) + i(x0y1 + x1y0). In Python: z = 0.2 + 0.3j; z.real, z.imag, abs(z) give real part, imaginary part, and magnitude.
What is the polar (trigonometric) form of a complex number, and how do I convert between forms (including conjugates)?Polar form uses magnitude r and angle θ: z = r(cos θ + i sin θ). Convert: x = r cos θ, y = r sin θ; r = √(x² + y²), θ = atan2(y, x). The conjugate flips the sign of the imaginary part: z̄ = x − i y = r(cos(−θ) + i sin(−θ)). A key identity: z·z̄ = r² = |z|².
How do quantum operations recombine a pair of amplitudes?For a pair (z0, z1), a 2×2 complex matrix [[a, b], [c, d]] produces new amplitudes z0' = a z0 + b z1 and z1' = c z0 + d z1. This linear operation preserves total probability when the matrix is unitary. In code you can apply these two formulas directly; full matrix libraries aren’t required for small simulations.
How do colored bars visualize amplitude magnitude and phase?Place the amplitude as an arrow inside the unit circle. Map its direction (angle) to a hue on a color wheel; the bar’s color encodes phase. Set saturation high for visibility, and set the bar’s length to the amplitude’s magnitude. A state’s full set of amplitudes can be shown as a bar chart where each bar’s length and hue reflect magnitude and phase.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Quantum Software with Python ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Quantum Software with Python ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Quantum Software with Python ebook for free