Overview

4 Quantum state and circuits: beyond one qubit

This chapter extends the single‑qubit foundations to multi‑qubit quantum computing, focusing on how to represent, evolve, and measure quantum states and how to express computations as circuits in Python. It introduces multi‑qubit state tables and the state vector view (a list of complex amplitudes), explains how measurement produces bitstrings whose frequencies reflect underlying probabilities, and revisits common computation patterns such as sampling, searching, and probability estimation. With these concepts in place, the chapter builds a hands‑on simulator, then demonstrates how to prepare useful states and distributions (uniform, binomial) and create quintessential entangled states (Bell states), all culminating in circuit‑level abstractions with registers and gate sequences.

Technically, an n‑qubit state is encoded as 2^n complex amplitudes whose squared magnitudes sum to one; probabilities and phases can be interconverted with amplitudes for analysis or construction. The chapter contrasts product states with entangled states and proves why Bell states cannot be decomposed into independent single‑qubit states. Gate actions are shown to recombine pairs of amplitudes that differ only in the target qubit’s bit, with efficient pair selection derived from binary indexing and “chunking.” A compact Python simulator validates states, initializes |0…0⟩, and applies gates via pairwise updates; controlled and multi‑controlled variants restrict which pairs are affected. Using these tools, the chapter prepares a uniform distribution by applying Hadamards to all qubits and encodes a binomial distribution by applying Ry rotations (with grouping by Hamming weight to match binomial counts), illustrating both amplitude design and probability shaping.

On top of these primitives, the chapter introduces circuit abstractions that mirror a common quantum SDK interface: quantum registers to index qubits and a circuit object that records transformations (x, h, ry, cx, mcx), executes them on a state, and supports measurement with repeated sampling to produce counts. Worked examples include simple multi‑qubit circuits, controlled operations, construction of Bell states, and measurement workflows that confirm theoretical probabilities. Throughout, the text emphasizes the trade‑offs between true quantum parallelism and classical simulation (where cost doubles with each added qubit), while highlighting the pedagogical advantage of simulators: full visibility into amplitudes, precise state preparation, and a smooth path from conceptual understanding to executable circuits used in later chapters.

A dependency diagram of concepts introduced in this chapter.
CH04 F01 gonciulea
Table 4.1 The general form af a two-qubit state.
CH04 T01 gonciulea
Table 4.2 The state table for a random two-qubit quantum state.
CH04 T02 gonciulea
State tables for two single-qubit states.
CH04 F02 gonciulea
Table 4.3 State table for the product state of the two single-qubit states in figure 4.2.
CH04 T03 gonciulea
We can think of two coins with independent bias as a product state and two dependent coins as a non-product state.
CH04 F03 gonciulea
State tables for first two Bell states.
CH04 F04 gonciulea
State tables for last two Bell states.
CH04 F05 gonciulea
Table 4.4 The amplitudes of an \(n\)-qubit quantum state where \(0\le k < 2^n\).
CH04 T04 gonciulea
Table 4.5 The probabilities and directions of a quantum state with \(n\) qubits where \(0\le k < 2^n\).
CH04 T05 gonciulea
Table 4.6 The amplitudes of an \(n\)-qubit quantum state where \(0\le k < 2^n\).
CH04 T06 gonciulea
Table 4.7 Table comprehension for quantum state with \(n\) qubits where \(0\le k < 2^n\).
CH04 T07 gonciulea
Table 4.8 Table comprehension for quantum state with \(n\) qubits where \(0\le k < 2^n\).
CH04 T08 gonciulea
Table 4.9 Table comprehension for quantum state with \(n\) qubits where \(0\le k < 2^n\).
CH04 T09 gonciulea
The pairs of outcomes for applying a single-qubit gate to a three-qubit system with target qubit in position 0.
CH04 F06 gonciulea
The pairs of outcomes for applying a single-qubit gate to a three-qubit system with target qubit in position 1.
CH04 F07 gonciulea
The pairs of outcomes for applying a single-qubit gate to a three-qubit system with target qubit in position 2.
CH04 F08 gonciulea
The pairs of outcomes and amplitudes of an \(n\)-qubit state (where \(0\le k < 2^n\)) before and after applying a gate transformation to target qubit \(t\).
CH04 F09 gonciulea
Pair generating strategy using chunks of outcomes for a three-qubit system and target qubit \(t = 1\).
CH04 F10 gonciulea
Amplitude pair processing for the X-gate applied to target qubit 0 in a three-qubit system.
CH04 F11 gonciulea
Amplitude pair processing for the X-gate applied to target qubit 2 in a three-qubit system.
CH04 F12 gonciulea
Butterfly diagrams of the pairs of outcomes in a three-qubit system for targets 0, 1 and 2.
CH04 F13 gonciulea
Butterfly diagrams of the pairs of outcomes in a three-qubit system for targets 0, 1 and 2.
CH04 F14 gonciulea
Table 4.10 The state table for the uniform probability distribution on eight outcomes.
CH04 T10 gonciulea
The pairs of outcomes for applying a controlled single-qubit gate to a three-qubit system with target qubit 0 with control qubit 2.
CH04 F15 gonciulea
The pairs of outcomes for applying a controlled single-qubit gate to a three-qubit system with target qubit 2 with control qubit 1.
CH04 F16 gonciulea
Pair processing for applying a controlled X-gate to a three-qubit state with target qubit 0 and control qubit 2.
CH04 F17 gonciulea
Pair processing for applying a controlled X-gate to a three-qubit state with target qubit 2 and control qubit 1.
CH04 F18 gonciulea
One measurement and the counts of previous measurement outcomes.
CH04 F19 gonciulea
A simple three-qubit circuit.
CH04 F20 gonciulea
Table 4.11 The state table after applying our simple circuit.
CH04 T11 gonciulea
Circuit for encoding a uniform distribution in a three-qubit state.
CH04 F21 gonciulea
Table 4.12 The state table for the uniform probability distribution on eight outcomes.
CH04 T12 gonciulea
The quantum circuit that applies an \(R_Y(\pi/3)\)-rotation to all three qubits of the system.
CH04 F22 gonciulea
Table 4.13 The three-qubit state prepared by applying \(R_Y(\pi/3)\) gates to every qubit.
CH04 T13 gonciulea
Table 4.14 The binomial distributions for \(p = \cos^2\frac{\pi}{3}\).
CH04 T14 gonciulea
Table 4.15 The state table for the circuit that applies an \(R_Y(\theta)\)-rotation to \(n\) qubits. Here \(o(k)\) denotes the number of 1s in the binary expansion of \(k\).
CH04 T15 gonciulea
Table 4.16 The general form of the three-qubit state prepared by applying \(R_Y(\theta)\) to every qubit.
CH04 T16 gonciulea
Table 4.17 The result of grouping outcomes with the same number of 1s.
CH04 T17 gonciulea
Table 4.18 The uniform distribution prepared by applying \(R_Y(\pi/2)\) gates to every qubit.
CH04 T18 gonciulea
Table 4.19 The binomial distributions for \(p = \cos^2\frac{\pi}{2}\).
CH04 T19 gonciulea
Table 4.20 The first Bell state table.
CH04 T20 gonciulea
Table 4.21 The third Bell state table.
CH04 T21 gonciulea

Summary

  • A quantum system made up of \(n\) qubits can be represented by an array of \(2^n\) complex numbers, where the sum of squared magnitudes of the complex numbers is 1.
  • State tables provide a visual way of understanding quantum states, connecting the mathematical and programmatic models.
  • A quantum transformation applies a quantum gate to one qubit (the target qubit). It can also be controlled by using other qubits as control qubits.
  • A quantum transformation recombines pairs of amplitudes determined by the target and control qubits according to the specific gate formula.
  • Quantum circuits use quantum transformations to create useful states and probability distributions.
  • Qubits can be organized into registers to make it easier to represent logical variables.
  • We can write a simple Python simulator to simulate the basic principles of quantum computing.

FAQ

How is an n‑qubit quantum state represented in this chapter’s simulator?A quantum state is a list (state vector) of 2^n complex numbers, one amplitude per possible measurement outcome. The list index k (0 ≤ k < 2^n) corresponds to the outcome’s integer value. For a valid state, the list length must be a power of two and the sum of squared magnitudes ∑k |z_k|^2 must equal 1.
What are “measurement counts” and how do they relate to probabilities?Each execution returns a single n‑bit outcome. Repeating the computation many times yields a histogram of outcomes called counts. The relative frequencies approximate the true outcome probabilities determined by the state amplitudes. The chapter highlights three patterns: sampling from a distribution, searching for specific outcomes, and estimating probabilities.
How do I initialize and validate a multi‑qubit state in Python?Use an initializer that creates a length‑2^n list with amplitude 1 at index 0 and 0 elsewhere (the all‑zero basis state). To directly set a custom state, validate: (1) length is a power of two, and (2) the amplitudes are normalized (sum of squared magnitudes equals 1). The book’s prepare_state function performs these checks.
How are single‑qubit gates applied to multi‑qubit states?Gates act on a target qubit t by recombining amplitudes in pairs whose outcome indices differ only at bit t. In decimal indexing, paired indices differ by 2^t. For each pair (z0, z1), a 2×2 gate matrix [a b; c d] produces new amplitudes (a z0 + b z1, c z0 + d z1). The simulator iterates over all such pairs.
What strategy does the simulator use to find amplitude pairs efficiently?It uses a “chunking” pattern: for target t, traverse outcomes in chunks of size 2^t. Each chunk with bit t=0 is paired with the subsequent chunk with bit t=1. Within each position k0 in the first chunk, the corresponding partner index is k1 = k0 + 2^t.
What are controlled transformations and how are they simulated?Controlled gates only act on pairs whose control qubit positions have value 1. In code, after generating target‑bit pairs, a filter checks that the control bits are set (e.g., via is_bit_set). Multi‑controlled gates require all specified control bits to be 1. The pair update (matrix multiply) is the same; fewer pairs are processed.
How can I prepare a uniform probability distribution over all outcomes?Apply a Hadamard gate to every qubit starting from the all‑zero state. This sets all amplitudes to the same magnitude 1/√(2^n) (up to global phases), making every outcome equiprobable. In the circuit API: for each qubit q[i], call h(q[i]).
How are product states and entangled states illustrated?A product state arises by multiplying amplitudes from two independent single‑qubit states (tensor product). Not all two‑qubit states are products; Bell states are examples of entangled states where outcomes are correlated (e.g., only 00 and 11 occur with equal probability). Entanglement typically arises when using controlled gates.
How do I simulate measurement and get counts in Python?Compute probabilities as |z_k|^2 and sample indices using random.choices with those weights for a specified number of shots. Aggregate samples into a counts dictionary. The resulting frequencies approximate the true probabilities.
How do I encode a Binomial distribution with the simulator?Apply an RY(θ) rotation to each qubit. With p = cos^2(θ/2), the state’s outcome probabilities depend on the Hamming weight (number of 1s) of each outcome. Group outcomes by Hamming weight to obtain the Binomial(n, p) distribution over k = 0..n: P(k) = C(n, k) p^k (1 − p)^(n−k).

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