Curriculum / Real-World Quantum Python / Patterns for Real Quantum Hardware
Patterns for Real Quantum Hardware
Learn the practical patterns for submitting and retrieving results from real quantum cloud hardware.
Patterns for Real Quantum Hardware
Production quantum code must handle the realities of cloud quantum APIs: job queuing, result retrieval, circuit transpilation, and hardware calibration. Simulator code runs in milliseconds; hardware jobs run in minutes to hours, fail silently, and return noisy results.
The Hardware Reality Gap
| Aspect | Simulator | Real Hardware |
|---|---|---|
| Execution time | Milliseconds | 1 minute to 6 hours |
| Noise | None (statevector) | Gate errors 0.1–3%, T1/T2 decoherence |
| Connectivity | All-to-all | Linear, grid, or heavy-hex topology |
| Gate set | Any unitary | Hardware native gates (CX, ECR, CNOT) |
| Availability | Always | Calibration windows, queue position |
These differences require production quantum code to be more resilient than typical simulator code.
Job Management Patterns
Asynchronous job submission: Quantum cloud APIs are asynchronous. You submit a job, receive a job ID, and poll until completion. Synchronous wrappers hide this but block for minutes. Structure code to submit all jobs first, then collect results:
# Bad: synchronous blocking (one job at a time)
for circuit in circuits:
result = backend.run(circuit).result() # blocks for minutes
# Good: submit all, collect all
job_ids = [backend.run(c).job_id() for c in circuits]
results = [backend.retrieve_job(jid).result() for jid in job_ids]Result caching: The same circuit with the same parameters will return statistically equivalent results. Cache by (circuit_name, params, shots) to avoid redundant expensive jobs. Use a disk-backed cache (e.g., shelve or SQLite) for persistence across sessions.
Retry with backoff: Hardware backends occasionally fail (queue timeout, calibration error, cloud outage). Always retry with exponential backoff:
for attempt in range(max_retries):
try:
result = backend.run(circuit).result()
break
except IBMRuntimeError:
time.sleep(2 ** attempt) # exponential backoffTranspilation Overhead
Real hardware has a native gate set (e.g., IBM uses {CX, ID, RZ, SX, X}) and limited qubit connectivity. Qiskit's transpiler converts logical circuits to hardware-native form:
- 1.Unrolling: decompose complex gates into native gates
- 2.Routing: insert SWAP gates to route logical qubits to connected physical qubits
- 3.Optimization: cancel redundant gates (from the circuit optimization lesson earlier in this track)
Transpilation overhead is significant: a 10-gate logical circuit may become 30–50 hardware gates after routing.
This is the opening of the lesson. The full walkthrough, the interactive circuit, and the graded challenge continue inside myqubit.
How this lesson works
A hands-on coding challenge. You write Qiskit-compatible Python in the browser editor, run it instantly via WebAssembly, watch the circuit and Bloch sphere react, and pass automatic output checks. The AI tutor Qubitus gives Socratic hints if you get stuck.
Part of: Real-World Quantum Python
Write production-quality quantum Python, circuit optimization, hybrid algorithms, cloud backends, noise modeling, and software engineering patterns.
This lesson is part of Pro
Unlock Real-World Quantum Python and all 10 tracks with Pro: $12.99/month, $79/year, or $97 lifetime. Start with the free track first if you are new.