Programming practice: Qiskit, Cirq, PennyLane
Sooner or later you will write a quantum program, for a course, a project, or to run on real hardware through the cloud. Three open-source frameworks dominate: IBM’s Qiskit, Google’s Cirq, and Xanadu’s PennyLane. They describe the same mathematics with different conventions, and the most common beginner’s bug, getting qubit order backwards, comes from exactly those differences. This chapter writes one circuit three ways and checks each against the book’s own simulator.
On the plate: the Bell state, sampled by the book’s simulator.
One circuit, three frameworks
A Hadamard on qubit 0, then a CNOT from 0 to 1, then measure: the Bell state of Chapter 99. In Qiskit 2.x:
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
res = StatevectorSampler().run([qc], shots=1000).result()
print(res[0].data.meas.get_counts()) # about 500 '00', 500 '11'
In Cirq:
import cirq q0, q1 = cirq.LineQubit.range(2) c = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1, key="m")) print(cirq.Simulator().run(c, repetitions=1000).histogram(key="m"))
In PennyLane:
import pennylane as qml
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def bell():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
return qml.probs(wires=[0, 1])
print(bell()) # [0.5, 0, 0, 0.5]
All three agree with the plate: half 00, half 11, never 01 or 10. Established
The qubit-order bug
Now apply X to qubit 0 only, and read the output as each framework prints it. Choose: , with X on qubit or . Qiskit writes bit strings little-endian: qubit 0 is the rightmost character, so X on qubit 0 prints “01”. Cirq and PennyLane, like this book (Chapter 98), put qubit 0 first: “10”, or index 2 in a probability vector. Neither convention is wrong. Mixing them silently swaps qubits and gives answers that look plausible. Always test with a circuit whose answer you know. Established
Transpiling, hardware, and error mitigation
Before running on a device, a transpiler rewrites the circuit into the backend’s native gates and routes it onto its connectivity (Chapter 142); in Qiskit, generate_preset_pass_manager(optimization_level=3, backend=backend) does this, and the gate count can grow several-fold. On today’s noisy hardware, error mitigation estimates noise-free expectation values without full error correction. Zero-noise extrapolation (ZNE) deliberately amplifies noise, for example by repeating gates, measures at several noise levels, and extrapolates to zero: with values at the normal noise and at triple the noise, a linear fit gives . Probabilistic error cancellation (PEC) inverts a learned noise model by sampling, unbiased but with a sampling cost that grows exponentially with circuit size. Kristan Temme, Sergey Bravyi and Jay Gambetta introduced both in 2017. Mitigation is a bridge, not a substitute for error correction. Established
Frameworks change fast; the code here was checked against Qiskit 2.x, Cirq 1.6 and PennyLane 0.45 in November 2026.
What you now have
The same circuit in Qiskit, Cirq and PennyLane, the little-endian versus big-endian qubit-order trap, transpilation, and zero-noise extrapolation and probabilistic error cancellation with their costs.
· · ·
Three questions to carry with you.
And one to work out. An expectation value is 0.80 at normal noise and 0.50 at triple noise. What does linear ZNE estimate?
Sources: Qiskit 2.x documentation (IBM, 2026). Cirq 1.6 documentation (Google). PennyLane 0.45 release notes (Xanadu, May 2026). K. Temme, S. Bravyi and J. M. Gambetta, Phys. Rev. Lett. 119, 180509 (2017). Last checked 18 November 2026. Checked by the plate: Bell-state sampling with the book’s state-vector simulator.