Exercises: Circuit Optimization
These need qiskit, qiskit-aer, and qiskit-ibm-runtime. Solutions to starred exercises are in
Answers to Selected Exercises.
A standing rule for this chapter: no optimization result counts until you have (a) verified the unitary is preserved and (b) measured the effect on output fidelity with an error bar. Case Study 1 is what happens otherwise.
The levels
28.1 ★ Transpile a QFT(5), a Grover-like circuit, and an EfficientSU2 ansatz at all four levels
on FakeSherbrooke. Report depth, two-qubit count, total gates, and transpile time.
28.2 ★ Verify with Operator.from_circuit that all four levels preserve the unitary on a
FakeManilaV2 circuit. What does the comparison give if you omit the layout?
28.3 ★ Find a circuit for which level 2 produces a deeper circuit than level 1. Report both metrics.
28.4 ★★ Level 0 exists to make a circuit runnable, not to optimize it. Confirm this by inspecting the level-0 pass manager. What passes does it contain, and why is each necessary?
28.5 ★★ Measure the transpile time at each level for circuits of 5, 8, and 11 qubits. How does the cost scale, and at what size does level 3 become expensive enough to notice?
Does it help?
28.6 ★★ Reproduce §28.3: run level 1 and level 2 circuits on a noise model over 8 transpiler seeds and report the mean, standard deviation, and difference with a standard error.
28.7 ★★ Is the difference significant at 2σ? At 1σ? How many seeds would you need to resolve a difference of 0.003 in this quantity?
28.8 ★★ Measure the level 0 → level 1 improvement. Is it significant? By how many σ?
28.9 ★★ Compute the fraction of noiseless signal retained at each level. Comment on the absolute number, not just the ratios.
28.10 ★★★ Construct a circuit where depth clearly dominates (long, few two-qubit gates) and one where gate count clearly dominates (shallow, many two-qubit gates). Confirm the levels rank differently on the two.
28.11 ★★★ Build a predictive model: given depth, two-qubit count, and Chapter 12's measured error rates, estimate the surviving fidelity. Test it against your measurements from 28.6. How good is it?
Levels 2 and 3
28.12 ★ Print the optimization stage of the level 1, 2, and 3 preset pass managers. Which passes does each add?
28.13 ★★ Confirm that levels 2 and 3 produce identical output for QFT(5) at all 8 seeds, and different output for QFT(8). Why does circuit size matter?
28.14 ★★ Across 5 circuits × 8 seeds, count how often levels 2 and 3 differ and which wins. Reproduce 14/40 and 12-versus-2.
28.15 ★★ Read the documentation for MinimumPoint and FixedPoint. Explain in two sentences why
substituting one for the other can only help.
28.16 ★★★ VF2PostLayout re-examines the layout after routing using measured error rates. Build a
case where it changes the answer, and quantify the improvement.
The passes
28.17 ★ Build the wasteful circuit from §28.5 and run each of the four passes alone. Reproduce the table.
28.18 ★ Confirm every pass preserves the unitary exactly.
28.19 ★★ Why does CommutativeCancellation alone outperform the other three? Trace what it does to
the CX RZ CX fragment.
28.20 ★★ RemoveIdentityEquivalent did nothing here. Construct a circuit where it does something,
and state its criterion.
28.21 ★★ Show that pass order changes the output. Find a circuit where it changes the gate count, not just the basis.
28.22 ★★★ Build a custom PassManager that beats level 3 on one of your circuits. What did you
exploit that the preset does not?
Protection
28.23 ★ Show that H H transpiles to depth 0 and H barrier H does not.
28.24 ★★ Show that wrapping Chapter 25's id noise slot in barriers does not save it. Explain
the difference between the two cases in 28.23 and 28.24.
28.25 ★★ Write slots_survived(circuit, backend, level, instruction, expected) and use it to
assert that a dynamical-decoupling sequence survives transpilation.
28.26 ★★★ Get an id (or a delay) to survive optimization at level 1 by some means other than
optimization_level=0. What does it cost you?
Approximation
28.27 ★ Sweep approximation_degree from 1.0 to 0.5 on a QFT(4) targeting FakeManilaV2 and
report two-qubit count, depth, and process fidelity.
28.28 ★ At what degree do the two-qubit gates disappear entirely? What is the fidelity there?
28.29 ★★ Does approximation_degree=0.99 give 99% fidelity? Explain what the parameter actually
controls.
28.30 ★★ Using Chapter 12's median two-qubit error, compute whether the 1.0 → 0.99 trade is a net win or a net loss on that device. Show the arithmetic.
28.31 ★★★ Find a circuit and noise level where approximation is a net win, and demonstrate it end to end on a noise model.
Project
28.32 ★★ (Project Checkpoint) Build vqelab/optimization.py with two_qubit_count, profile,
OptimizationReport, significantly_better, verify_equivalence, count_ops_delta,
slots_survived, and approximation_sweep. Write tests asserting:
- All four levels preserve the unitary exactly.
verify_equivalenceraises on a backend too large to build the operator.OptimizationReport.better_thanRAISES when neither side has measured fidelity.- A difference of $+0.0028 \pm 0.0065$ is NOT better, even though the mean is higher.
- A difference of the level 0 → 1 size is better.
significantly_betterrequires fidelity on both sides.- The standard error shrinks as $1/\sqrt{n}$.
- On the Grover-like circuit, level 2 is deeper AND uses fewer two-qubit gates.
- Level 1 more than halves level 0's depth.
- Levels 2 and 3 are not always identical — demonstrate on a large enough circuit.
- QFT(5) cannot distinguish them at any seed.
- Barriers do NOT protect an
idgate, though the barriers themselves survive. - A barrier does stop
H Hcancelling. count_ops_deltanames the deleted instruction.approximation_degree=0.99gives fidelity below 0.95.- At 0.9 there are zero two-qubit gates and fidelity below 0.5.
- Approximation fidelity decreases monotonically.
approximation_sweeprefuses on a backend where the fidelity cannot be verified.
Tests 3, 4, 12 and 16 are the ones this chapter exists to encode.
28.33 ★★★ Extend the module with noisy_profile(circuit, backend, level, seeds, shots) that fills
in fidelity_mean and fidelity_std automatically, so better_than becomes usable rather than a
refusal.
Going further
28.34 ★★ Apply the whole chapter to Chapter 23's Shor circuit for $N=15$. How much does optimization reduce its 3,368 gates, and does the reduction change the success probability?
28.35 ★★★ The transpiler does not optimize $T$ count. Take a Clifford+$T$ circuit, count its $T$ gates before and after level 3, and explain the result. Then look up a $T$-count optimization method and estimate what it would save.
28.36 ★★★ Chapter 22's AQFT drops rotations below a cutoff — a numerical argument, not a circuit
identity. Compare AQFT against approximation_degree on the same QFT: which gives better fidelity per
two-qubit gate removed, and why?