Exercises: Hardware-Aware Programming

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: any claim that a hardware-aware change helped must be measured on a noise model, with a standard error, comparing like for like. Chapter 28 §28.3 is why.


The chip

29.1 ★ Build the undirected adjacency of FakeSherbrooke's coupling map and report the degree distribution. Confirm the maximum degree is 3.

29.2 ★ coupling_map.neighbors(q) returns only successors. Show that using it directly gives a wrong degree distribution, and say how many qubits appear to have zero neighbours.

29.3 ★ How many edges would a complete graph on 127 qubits have? How many does this chip have?

29.4 ★★ Find the longest connected chain on the chip. How many qubits can a purely linear circuit use without any routing?

29.5 ★★ The heavy-hex lattice trades connectivity for reduced crosstalk. Look up why, and state the trade in one sentence.


Shape

29.6 ★ Transpile EfficientSU2(6, reps=3) with entanglement set to full, circular, and linear. Report logical two-qubit count, hardware count, and the ratio.

29.7 ★ Confirm the linear pattern has a routing overhead of exactly 1.0. What does that mean geometrically?

29.8 ★★ circular is linear plus one edge per repetition. Compute the marginal hardware cost of those edges and compare it with the total cost of the linear ones.

29.9 ★★ Does the overhead ordering survive at optimization level 3? At what point does the transpiler close the gap, if ever?

29.10 ★★ Repeat the comparison at 4, 6, 8, and 10 qubits. How does the full-entanglement overhead scale with qubit count?

29.11 ★★★ Construct an entanglement pattern that matches a branch of the heavy-hex lattice (using the degree-3 qubits) rather than a chain. Does it beat linear on overhead? On fidelity?


The measurement

29.12 ★★ Reproduce §29.3: run all four shape/level combinations on a noise model over 5 seeds and report means and standard deviations.

29.13 ★★ Isolate the two variables. What is shape worth at a fixed level? What is level worth at a fixed shape? What is the ratio?

29.14 ★★ Is the hardware-aware-at-level-1 versus naive-at-level-3 gap significant? By how many standard errors?

29.15 ★★ On the linear ansatz, level 3 is deeper than level 1 and scores better. Chapter 28 found the opposite on a different circuit. Explain why both can be true.

29.16 ★★★ Build a predictive model of $1-\text{TVD}$ from depth and two-qubit count using Chapter 12's measured error rates. Test it against all six configurations. Where does it fail?


Layout

29.17 ★ Find a connected 6-qubit chain on FakeSherbrooke by graph search alone. Print the measured two-qubit error of each edge.

29.18 ★ Compute the survival product of that chain. Why is it exactly zero?

29.19 ★★ Pin the circuit to that chain with initial_layout and measure the fidelity. Compare against the automatic layout.

29.20 ★★ Write usable_path(backend, path) rejecting any path with a dead edge. Apply it to all connected 6-chains: what fraction are unusable?

29.21 ★★ Search all connected 6-chains for the highest survival product. Does it beat the transpiler's automatic layout by that metric? Does it beat it in measured fidelity?

29.22 ★★★ The survival product mispredicted. Extend it with readout error and $T_1$/$T_2$ and re-rank. Does the extended metric now agree with the measurement?

29.23 ★★★ Read the VF2Layout and VF2PostLayout source or documentation. What does their scoring function include that your survival product does not?


Design

29.24 ★★ Take Chapter 24's QAOA MaxCut circuit. Its interaction graph is the problem graph — but the assignment of problem vertices to physical qubits is yours. Find a good embedding and measure the overhead saved.

29.25 ★★ Write a circuit using cx and transpile it to FakeSherbrooke (ecr basis). Count the translation cost. Then write the same circuit natively and compare.

29.26 ★★★ Use mid-circuit measurement and reset (Chapter 9) to run a circuit needing 8 logical qubits on 5 physical ones. What does it cost in depth, and does the trade pay on a noise model?

29.27 ★★★ Chapter 25 §25.10 noted the surface code needs only nearest-neighbour connectivity on a 2D grid. Explain how that constraint is the same design principle as this chapter's, applied one level down.


Project

29.28 ★★ (Project Checkpoint) Build vqelab/hardware.py with undirected_neighbours, edge_error, usable_path, chain_survival, find_chain, best_chain_by_survival, routing_overhead, entanglement_comparison, and recommend_layout. Write tests asserting:

  1. The chip is not a complete graph; maximum degree is 3.
  2. undirected_neighbours fixes the directed-adjacency gotcha — every qubit has ≥1 neighbour.
  3. Linear entanglement has routing overhead exactly 1.0.
  4. All-to-all entanglement has overhead above 3.
  5. The extra circular edges cost more hardware gates than all the necessary ones.
  6. Overhead is ordered linear < circular < full.
  7. routing_overhead rejects a circuit with no two-qubit gates.
  8. [7,6,5,4,3,2] is connected, has exactly two dead edges, and has survival exactly 0.0.
  9. edge_error returns 1.0 — not None, not an exception — for a non-existent pair.
  10. The error-aware chain is usable and has survival above 0.9.
  11. recommend_layout returns None by default, mentioning VF2Layout.
  12. An override requires a stated reason, and the resulting chain is still checked for dead links.
  13. An override still warns the metric is incomplete.
  14. chain_survival's docstring says it is INCOMPLETE and quotes the measurement where it mispredicted.
  15. Shape changes gate count more than optimization level does.
  16. The hardware-aware circuit is more than 5× shallower.

Tests 3, 5, 8 and 11 are the ones this chapter exists to encode.

29.29 ★★★ Extend the module with best_shape(problem_graph, backend) that, given an interaction graph, returns the closest embeddable subgraph and the edges you would have to drop. Report what dropping them costs in expressibility.


Going further

29.30 ★★ Run the expressibility check §29.6 calls for: optimize Chapter 24's H₂ VQE noiselessly with both full and linear ansätze. Does the linear one reach chemical accuracy?

29.31 ★★★ Repeat for a harder Hamiltonian (LiH, or H₂ at a stretched bond length). At what point does linear entanglement stop being sufficient?

29.32 ★★★ Trapped-ion devices have all-to-all connectivity (Chapter 17). Re-run §29.2's overhead comparison against a trapped-ion coupling map. Which of this chapter's conclusions survive, and which are specific to superconducting hardware?