Exercises: Google Cirq
All of these run locally — pip install cirq. Solutions to starred exercises are in
Answers to Selected Exercises.
On Windows, put sys.stdout.reconfigure(encoding="utf-8") at the top of any script that prints a
circuit diagram.
Warm-up
14.1 ★ Build a Bell state in Cirq, print the circuit, and run it for 4096 repetitions. Report the
histogram. Then explain why the keys are 0 and 3 rather than '00' and '11'.
14.2 cirq.H(q[0]) returns a value rather than mutating a circuit. Store three operations in a
list, print the list, then build two different circuits from the same list using different insertion
strategies. What does this tell you about where a Cirq circuit's structure lives?
14.3 ★ Build the same 3-qubit GHZ state in Qiskit and Cirq. Compare the histograms. Do they agree?
Now do the same for a circuit that applies X to qubit 0 only. Do they still agree?
14.4 Print cirq.unitary(cirq.H), cirq.unitary(cirq.CNOT), and cirq.unitary(cirq.CZ). Compare
each to the matrices in Appendix B. Does CNOT's
matrix match Qiskit's, or is it transposed in the way §14.5 would predict?
Moments
14.5 ★ Build a circuit with three H gates using each of the four insertion strategies, both as a
single append call and as a loop. Reproduce §14.4's table. Which strategy differs between the two
forms, and why?
14.6 ★ Construct the same 4-qubit circuit twice: once with EARLIEST and once with NEW. Verify
the final state vectors are identical (np.allclose) and the depths differ. By what factor?
14.7 ★★ Write depth_report(circuit) printing, for each moment, its index, the operations in it,
and the qubits it touches. Run it on a transpiled GHZ circuit and identify which moments could in
principle be merged.
14.8 ★★ Build a circuit with an explicit cirq.Moment list where one moment contains two
operations on overlapping qubits. What happens? What does the error tell you about what a moment
guarantees?
14.9 ★★ §14.3 notes that a moment lasts as long as its slowest operation, so depth is a proxy for duration rather than a synonym. Construct a 2-moment circuit and a 4-moment circuit where the 2-moment one plausibly takes longer in wall-clock time. What information would you need from a device to settle it?
Endianness
14.10 ★ Reproduce §14.5's central measurement: apply X to qubit 0 of a 2-qubit register in both
frameworks and report the index of the nonzero amplitude.
14.11 ★ Show that reversing the bits of a Bell-state histogram leaves it unchanged, and that
reversing the bits of an X-on-qubit-0 histogram does not. Explain in two sentences why this makes the
Bell state useless as a port verification.
14.12 ★★ Write reverse_bits(value, n) and prove by exhaustive test that it is its own inverse for
$n \leq 5$. Then explain what that property implies about scattering conversions through a codebase.
14.13 ★★ Write cirq_statevector_to_qiskit(state) that reorders a Cirq state vector into Qiskit's
convention. Verify it on: (a) X on qubit 0, (b) X on qubit 1, (c) a state with a nontrivial phase,
such as H then S. Why is (c) worth including?
14.14 ★★ Find a 3-qubit computational basis state other than the palindromes for which a reversed-endianness bug would be invisible. How many of the eight 3-qubit basis states are palindromes? What fraction of random targets would a buggy port get right by luck?
14.15 ★★★ Case Study 1's team also compared unitaries and found them to match. Explain precisely
why cirq.unitary(circuit) and Qiskit's Operator(circuit) can agree while the port is broken. Then
construct a comparison that would have caught it.
Gatesets and sweeps
14.16 ★ Apply cirq.optimize_for_target_gateset with CZTargetGateset to a Bell circuit. Report
the gate sequence before and after, and verify the unitary is preserved.
14.17 ★★ Compare CZTargetGateset and SqrtIswapTargetGateset on the same 3-qubit GHZ circuit.
Which produces fewer two-qubit gates? Fewer moments? Relate this to Chapter 10's finding that the
native gate set determines the cost.
14.18 ★ Build a parameterized ry(t) circuit and sweep $t$ over $[0, 2\pi]$ in 9 steps. Plot or
tabulate $P(1)$ against $\sin^2(t/2)$.
14.19 ★★ Create twelve sympy symbols theta1 … theta12, bind them with a ParamResolver, and
confirm each gate receives the value you intended. Then reproduce Chapter 8's failure in Qiskit with
the same names, and quantify how many of the twelve land in the wrong gate.
14.20 ★★ Build a two-parameter sweep with cirq.Product or cirq.Zip. What is the difference
between the two, and how many circuits does each produce for two 5-point ranges?
Noise and devices
14.21 ★ Apply cirq.depolarize(0.05) to a Bell circuit with with_noise. Report the moment count
before and after, and the resulting error fraction.
14.22 ★★ Reproduce Chapter 11's phase-damping result in Cirq: run a Bell state with
phase_damp(γ) for γ = 0.0, 0.3, 0.6 and confirm the computational-basis histograms are
indistinguishable. Then construct the $X$-basis measurement that does separate them.
14.23 ★★ Print cirq.kraus() for depolarize(0.05), amplitude_damp(0.1), and phase_damp(0.1).
How many Kraus operators does each have? Verify that each channel's operators satisfy
$\sum_k K_k^\dagger K_k = I$.
14.24 ★★ Load cirq_google.Sycamore and report its qubit count and number of coupling pairs. Then
find the longest chain of connected qubits — the Cirq analogue of Chapter 12's connected_paths.
14.25 ★★★ Chapter 12 scored layouts using calibration data. Attempt the same on Sycamore and
document precisely what you cannot do and why. What would Cirq need to expose for Chapter 12's method
to transfer?
Project
14.26 ★★ (Project Checkpoint) Build vqelab/translate.py with reverse_bits, histogram and
state-vector converters, qiskit_to_cirq (raising NotImplementedError on unsupported gates), and
assert_same_state. Write tests asserting:
reverse_bitsis its own inverse for $n \leq 4$.reverse_bits(2, 2) == 1andreverse_bits(4, 3) == 1.Xon qubit 0 lands at index 2 in Cirq and index 1 in Qiskit.- A Bell histogram is invariant under bit reversal — assert the weakness explicitly.
- An asymmetric histogram is not invariant.
- A translated 3-qubit circuit with
x,h,rz, andcxmatches viaassert_same_state. - The translator raises on
ccx. - Barriers are dropped rather than rejected.
Test 4 is unusual — it asserts that a test you might rely on cannot work. Explain in a comment why it belongs in the suite.
14.27 ★★★ Extend translate.py with cirq_to_qiskit, the reverse direction. Then write a
property test: generate random circuits from a fixed gate set, round-trip them Qiskit → Cirq → Qiskit,
and assert the final states match. How many random circuits do you need before you would trust it?
Going further
14.28 ★★★ Implement Chapter 12's connected_paths and score_chain for a Cirq GridDevice, using
graph distance in place of calibration-weighted error. Compare the best 5-qubit chain your scorer picks
on Sycamore to the one a naive left-to-right assignment would pick.
14.29 ★★★ Cirq exposes cirq.RouteCQC for routing. Route a circuit requiring non-adjacent
interactions onto Sycamore's topology and count the inserted SWAPs. Compare the overhead to Chapter
10's Qiskit measurements on a 127-qubit line. Does 2-D connectivity help as much as you would expect?