liveBook · Manning

Table comprehension

As explained in Building Quantum Software with Python

A table comprehension is a tabular representation of quantum-state values corresponding to the entries generated by a state expression.

A table comprehension is a tabular view of the values generated by a quantum-state expression. For an n-qubit state, each row associates an outcome k, where 0 ≤ k < 2^n, with its amplitude and derived quantities. This turns a state vector into a developer-friendly structure for inspection and simulation. section 4.2.2

Inspecting an eight-entry state

After validating an eight-amplitude state with prepare_state, a basic table can be generated with `[[k, state[k]] for k in range(len(state))]`. Each row contains an outcome index and its complex amplitude. An expanded comprehension adds direction in degrees, magnitude, and probability: `abs(state[k])` gives magnitude, while `abs(state[k]) ** 2` gives outcome probability. For example, outcome 5 has amplitude approximately 0.58403 − 0.6318i, magnitude 0.86038, direction −47.25°, and probability 0.74026. section 4.2.2

Use table comprehensions to:

  • Display outcome indices alongside amplitudes in a simulator. section 4.2.2
  • Convert amplitudes into probabilities and directions using squared magnitude and atan2. section 4.2.2
  • Reconstruct amplitudes from probabilities and directions with square roots, cosine, and sine. section 4.2.2
  • Compare amplitude pairs before and after applying a single-qubit gate to a target qubit. section 4.3.1
Figure 4.9 The amplitudes of an n-qubit quantum state with outcomes 0 ≤ k < 2n
figure

Browse the n-qubit amplitude layout to connect outcome indices with the rows generated by a table comprehension. section 4.2.2

How Building Quantum Software with Python covers it

How deep it goes
Implements Python comprehensions and conversion formulas, stopping short of direct real-hardware state control.
Examples
Uses an eight-amplitude state, expanded outcome tables, inverse reconstruction, and target-qubit pairs in three-qubit systems.
What it assumes
Assumes Python list comprehensions, complex numbers, and basic trigonometric functions while deriving state-table quantities explicitly.
What you can do after
Build it from scratch with Python list comprehensions that inspect, convert, reconstruct, and compare simulated quantum-state values.
How it connects
Connects quantum states with amplitudes, probabilities, phase direction, binary representation, and quantum gates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Quantum Software with Python. The section links point to the parts of the book it draws on.