Quantum Computing Master Course — 090. Amplitude Method Laboratory
Lesson 090: Amplitude Method Laboratory
Smartyfy Quantum Computing Master Course
Prerequisites and objectives
This laboratory compares three objects that are easy to confuse: an exact ideal success probability, a distribution over amplitude-estimation outputs, and a finite classical sampling estimate. You will reproduce amplification curves, check a phase-estimation error statement, and explain which resource each calculation counts.
The program uses only Python’s standard library. It was executed locally for this lesson. It is a classical simulation of small ideal mathematical models, with no quantum hardware, remote account, or access credentials.
Set the register and oracle conventions
For n qubits, the state is a length N=2^n array. Index x represents the computational basis label written most-significant bit first. Thus index five in a three-qubit array means |101⟩. The marked set supplies the predicate directly for this small simulation.
Each iteration first negates marked amplitudes, then maps every amplitude a to 2μ−a, where μ is the arithmetic mean after marking. This implements the uniform-state reflection after the phase oracle. The mean is an amplitude mean and may be complex.
The simulation stores all N amplitudes because it runs on a classical computer. Accessing every array entry is a classical simulation cost; it is not evidence that a physical quantum computer makes all its amplitudes readable.
Exact curve before sampling
For N=8 and one marked label, the theoretical curve is Pt=sin²((2t+1)θ), with θ=arcsin(1/√8). At t=0,1,2,3 the exact probabilities are 1/8,25/32,121/128,169/512.
| Iterations t | Exact probability | Decimal probability |
|---|---|---|
| 0 | 1/8 | 0.125 |
| 1 | 25/32 | 0.78125 |
| 2 | 121/128 | 0.9453125 |
| 3 | 169/512 | 0.330078125 |
The program checks these values against its full state-array updates rather than calculating both sides with the same sine formula. Agreement therefore tests the sign flip, reflection ordering, amplitude mean, and probability extraction together.
Normalization is also checked after each iteration. A normalization test alone would be insufficient: many incorrect unitary sequences preserve norm while giving the wrong answer. The exact reference probabilities provide an additional targeted test.
Change the marked fraction deliberately
The next tests use sixteen basis labels. Four marks give initial success one quarter and reach certainty after one standard iteration. Twelve marks start at three quarters but reach zero after one iteration.
These cases expose overshooting without a subtle numerical comparison. They also show why a test suite should include easy-looking instances with many solutions. A procedure tuned only for rare solutions can perform badly there.
To investigate a different marked set with the same count, change the labels while keeping their number fixed. Uniform preparation and ideal symmetric marking give the same total success curve. A nonuniform preparation would break that count-only conclusion.
Build the amplitude-estimation distribution
For initial success p, the amplification rotation has phase fractions φ=arcsin√p/π and 1−φ, with equal eigenstate weights in the prepared state. Canonical phase estimation with M=2^m bins therefore produces an equal mixture of the two corresponding phase-estimation distributions.
For one phase, the amplitude of bin y is the finite sum M^−1 Σj exp(2πij(φ−y/M)), with j from zero through M−1. The program evaluates this sum directly using sine and cosine, squares its magnitude, and averages the two branch probabilities.
The two branches are averaged as probabilities because their eigenstates are orthogonal when the phase register alone is measured. Adding their amplitudes as though they were the same target state would give an incorrect interference term.
Every bin y is mapped to p̃=sin²(πy/M). Both symmetric phase branches yield the same underlying p in the exact case. For p=0.1 and M=32, the program sums the probability of bins satisfying the standard error bound 2π√(p(1−p))/M+π²/M².
The verified covered mass exceeds 8/π², as required by the canonical theorem. This finite example checks consistency with that theorem; checking one instance is not a proof for all inputs.
Runnable ideal-model checks
import math, random
def grover_curve(n, marked, steps):
N = 1 << n
state = [complex(1 / math.sqrt(N))] * N
result = []
for t in range(steps + 1):
result.append(sum(abs(state[x]) ** 2 for x in marked))
if t != steps:
state = [-a if x in marked else a
for x, a in enumerate(state)]
mean = sum(state) / N
state = [2 * mean - a for a in state]
assert abs(sum(abs(a) ** 2 for a in state) - 1) < 1e-12
return result
curve = grover_curve(3, {5}, 3)
expected = [1/8, 25/32, 121/128, 169/512]
assert all(abs(a-b) < 1e-12 for a, b in zip(curve, expected))
assert abs(grover_curve(4, set(range(4)), 1)[1] - 1) < 1e-12
assert abs(grover_curve(4, set(range(12)), 1)[1]) < 1e-12
print('N=8 curve:', [round(p, 9) for p in curve])
def phase_probability(phi, y, M):
amplitude = sum(complex(math.cos(2*math.pi*j*(phi-y/M)),
math.sin(2*math.pi*j*(phi-y/M)))
for j in range(M)) / M
return abs(amplitude) ** 2
p, M = 0.1, 32
phi = math.asin(math.sqrt(p)) / math.pi
distribution = [(phase_probability(phi, y, M) +
phase_probability(1-phi, y, M))/2
for y in range(M)]
assert abs(sum(distribution) - 1) < 1e-12
bound = 2*math.pi*math.sqrt(p*(1-p))/M + math.pi**2/M**2
covered = sum(weight for y, weight in enumerate(distribution)
if abs(math.sin(math.pi*y/M)**2-p) <= bound + 1e-12)
assert covered >= 8/math.pi**2
print('AE bound:', round(bound, 6), 'covered probability:', round(covered, 6))
rng = random.Random(90)
S = 400
estimate = sum(rng.random() < p for _ in range(S)) / S
print('Classical sample estimate:', estimate, 'standard deviation:',
math.sqrt(p*(1-p)/S))
Read the sampling result correctly
The last lines generate four hundred independent pseudo-random Bernoulli trials with success probability p=0.1. Their empirical mean is a classical estimator of p. Its theoretical standard deviation is √(p(1−p)/400)=0.015.
A fixed random seed makes this example repeatable in the local environment. The particular observed estimate is not a new exact probability, and a deviation larger than one standard deviation would not by itself signal faulty code.
The sampling calculation already receives p as a program input. It illustrates sampling variability; it does not solve an unknown physical problem or benchmark a quantum device against a practical classical competitor.
Compare resources without mixing units
The state-array Grover simulation touches every amplitude per iteration. Its runtime scales with array size. The theoretical query algorithm instead counts one ideal phase-oracle use per Grover iteration, while preparation, reflection, and verification have separate implementation costs.
The amplitude-estimation model with M=32 corresponds to five phase controls and, under literal controlled-power implementation, thirty-one applications of the amplification operation. It does not use only five amplification calls merely because it has five control qubits.
The direct finite-sum probability calculation is a classical way of predicting that circuit’s output distribution. It is not an efficient replacement for the circuit on arbitrary large problems, because the model was supplied with the answer parameter p.
Keep those distinctions in any report: theoretical oracle count, classical simulation work, and observed physical runtime answer different questions. The laboratory validates formulas and conventions rather than establishing a performance advantage.
Four exercises with worked solutions
-
What probability drop occurs between two and three iterations in the eight-label example?
Solution. Subtract 169/512 from 121/128=484/512. The drop is 315/512=0.615234375. An extra ideal iteration can therefore substantially reduce success.
-
How many marks are required for the sixteen-label test whose one-iteration success is one?
Solution. Four marks give p=4/16=1/4 and θ=π/6. The output angle 3θ=π/2 has unit good-subspace probability.
-
What sample count reduces the Bernoulli standard deviation from 0.015 to 0.0075 at the same p?
Solution. Halving standard deviation requires four times as many independent trials because it scales as 1/√S. The required count is sixteen hundred.
-
Why average the two phase-branch probabilities rather than their complex amplitudes?
Solution. The corresponding target eigenstates are orthogonal and unobserved. Marginalizing that target adds branch probabilities; treating the branches as one target state would invent cross terms.
Five explained multiple-choice questions
-
The reflection uses the mean of: A. Amplitudes after the phase flip. B. Squared magnitudes only. C. Amplitudes before the phase flip. D. Only the marked amplitudes.
Answer: A. The linear map is a→2μ−a, with μ the amplitude mean.
-
A normalized simulated state proves the whole algorithm correct: A. Always. B. If the oracle is applied after diffusion. C. If each gate is individually unitary. D. No; incorrect unitaries can also preserve norm.
Answer: D. Reference probabilities and convention checks test additional properties.
-
The M=32 controlled powers require, under literal implementation: A. Five Q calls. B. Thirty-one Q calls. C. Thirty-two measurements of Q. D. Thirty-two Q calls including a Q at power zero.
Answer: B. The powers sum to 1+2+4+8+16=31.
-
The seeded Bernoulli result is: A. A hardware measurement record. B. The exact p. C. A guaranteed bound on every repeated estimate. D. One reproducible classical sample estimate.
Answer: D. It demonstrates finite-sample variation in a supplied probability model.
-
At marked fraction three quarters, one standard iteration: A. Guarantees success. B. Leaves success at three quarters. C. Reduces ideal success to zero. D. Raises ideal success to fifteen sixteenths.
Answer: C. The rotation reaches angle π and loses its good component.
Pitfalls and summary
Validate exact probabilities independently of normalization, distinguish phase-branch mixing from coherent addition, and label sample estimates clearly. Ideal simulation tests are valuable evidence about mathematical implementation, while resource claims require their own stated model.
Primary sources and further study
These sources support the definitions and context. The numerical examples, exercises, and explanations here were prepared for this lesson. Research papers are optional advanced reading.