liveBook · Manning

Grover's algorithm

As explained in Building Quantum Software with Python

Grover’s algorithm is a quantum search algorithm that provides a quadratic speedup for finding a marked item in an unstructured search space.

Grover’s algorithm searches an unstructured space by increasing the measurement probability of outcomes that satisfy a search criterion. It can provide a quadratic speedup over classical search for suitable problems, including search, optimization, and machine-learning applications. section 6.0 section 6.3

Build the algorithm from a state-preparation operator, an oracle, and repeated amplification:

  • Prepare a starting state A, often with equal magnitudes across possible outcomes. section 6.2.4
  • Use an oracle O to tag desired outcomes by multiplying their amplitudes by −1. section 6.3
  • Apply the inversion operator to increase tagged amplitudes and decrease untagged amplitudes. section 6.3
  • Repeat the Grover iterate an appropriate number of times, then measure; the amplified outcomes are more likely to appear. section 6.2.4 section 12.1.2

Three-qubit search

Represent eight items with three qubits and mark outcomes 1, 3, and 7. Prepare a uniform state, construct a phase oracle for those outcomes, and apply the Grover circuit. In this example, one iteration is optimal; reports can inspect amplitude changes after the iteration. section 6.2.4

Searching encoded function values

After encoding f(k) = k + 1, use an oracle that matches the leading 1 in the value register to search for outputs at least 4. One Grover iteration amplifies the qualifying key–value pair. section 10.2

Listing 6.9 Function to create the magnitude amplification circuit
def grover_circuit(A, O, iterations):
    n = sum(A.regs)
    q = QuantumRegister(n)
    qc = QuantumCircuit(q)

    qc.append(A, q)

    for i in range(1, iterations + 1):
        qc.append(grover_iterate_circuit(A, O), q)
        qc.report(f'iteration_{i}')

    return qc

Use the circuit-building listing to see how state preparation, repeated Grover iterates, and per-iteration reports become executable Python structure. section 6.2.4

How Building Quantum Software with Python covers it

How deep it goes
Explains state preparation, phase marking, inversion, iteration, and circuit construction, including amplitude reports and concrete Python examples.
Examples
Demonstrates three-qubit search, marked outcomes, encoded functions, range predicates, and sign-based searches using Python circuit listings.
What it assumes
Assumes quantum-oracle and state-preparation concepts while grounding the search problem in classical query checking and binary outcome encoding.
What you can do after
Apply it via quantum circuits by preparing a state, supplying an oracle, selecting iterations, inspecting amplitudes, and measuring candidate outcomes.
How it connects
Connects quantum measurement with amplitude amplification, quantum oracles, state preparation, unstructured search, optimization, and function encoding.

Also explained in

Compare how 3 books explain Grover's algorithm →

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.