Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 39 ยท Quantum Programming Tools

OpenQASM and Running Circuits on Real Quantum Hardware

Learn OpenQASM 3 syntax, write a tiny QASM interpreter, and understand how a job travels to a quantum computer: transpiling, routing, queues and shots.

Quantum SDKs are Python (or Q#, or C++) libraries. Underneath them, circuits are exchanged as plain text in a common format: OpenQASM (Open Quantum Assembly Language). It is to quantum circuits what assembly or LLVM IR is to classical programs: a vendor-neutral, low-level description that tools and hardware can read.

This lesson shows you OpenQASM 3, lets you run a small subset of it with a ~40-line interpreter, and walks through the life of a job on a real device: transpiling, routing around limited connectivity, queues, shots, calibration data and costs.

OpenQASM in 5 minutes

A Bell-state program in OpenQASM 3:

Text
OPENQASM 3.0;
include "stdgates.inc";

qubit[2] q;
bit[2] c;

h q[0];
cx q[0], q[1];
c = measure q;
  • qubit[2] q; declares a quantum register; bit[2] c; a classical one.
  • Gates (h, x, cx, rz(pi/2), ...) come from stdgates.inc or your own gate definitions.
  • c = measure q; reads qubits into classical bits.
  • OpenQASM 3 also has classical control flow (if, for, while), timing (delay, duration) and subroutines, which are needed for real-time feedback like error correction.

OpenQASM 2 (older, still very common) uses qreg q[2]; creg c[2]; measure q -> c; and has no control flow beyond simple if(c==1). Qiskit can export and import both (qiskit.qasm3.dumps(circuit) and qiskit.qasm2.dumps).

A tiny interpreter

To make the format concrete, here is a minimal simulator that understands this subset: qubit[n] q;, gates h, x, z, s, t, rz(angle), cx, cz, swap, and measure. Parsing is regular expressions, simulation is the state-vector code you know.

Python Run OpenQASM text with a 40-line interpreter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The GHZ program gives only 000 and 111, and the H, Rz(pi/2), H sandwich gives a 50/50 outcome, as a rotation by 90 degrees about Z between two Hadamards should.

From circuit to hardware

A job sent to a real device goes through these stages:

flowchart LR
    A["Your circuit"] --> B["Transpile: layout, routing, native gates"]
    B --> C["Submit job (shots, parameters)"]
    C --> D["Queue"]
    D --> E["Calibrate and run on QPU"]
    E --> F["Counts or expectation values"]
    F --> G["Mitigate and analyze"]

A job's journey; most waiting time is in the queue, not in the run.

  1. Transpile. Map the circuit onto physical qubits, insert SWAPs, rewrite gates in the native basis. Do it against the exact backend you will use, because calibrations (which qubits are good) change daily.
  2. Submit. Choose the number of shots (repeats; typically 1,000 to 100,000), plus options such as error suppression or mitigation levels. Since each shot returns one bit string, estimating an outcome probability to precision epsilon needs about 1/epsilon^2 shots.
  3. Queue. Shared devices have queues from seconds to hours. Batching many circuits in one session or batch avoids queuing each one separately.
  4. Run. The device is recalibrated regularly; a shot of a small circuit takes tens of microseconds to a few milliseconds, but resetting qubits between shots takes time as well.
  5. Results. You receive counts per bit string (or expectation values from an Estimator) and device metadata.

Connectivity and routing

Most chips only allow two-qubit gates between neighboring qubits (a line, a grid, a heavy-hex lattice). A CNOT between distant qubits is implemented by swapping the qubits next to each other first. Each SWAP costs three CNOTs, so routing can multiply the error of a circuit.

Python How many SWAPs does routing cost on a line of qubits?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The star pattern and especially the all-to-all pattern pay a big routing tax on a line. Hardware with better connectivity (trapped ions and neutral atoms offer all-to-all or flexible connections) or a smarter layout reduces it. This is also why circuit depth and CNOT count after transpiling matter more than your original gate count.

Practical advice

  • Start in simulation, then run tiny circuits on hardware, because queue time and cost add up. Free tiers on cloud providers exist for small machines.
  • Check calibration data (T1, T2, gate and readout errors per qubit) and pick the best qubit region; compilers can do this automatically, but you should know what they optimize.
  • Use enough shots and report error bars. Statistical error shrinks as 1/sqrt(shots); systematic hardware error does not.
  • Mitigate readout and gate errors where the cost is acceptable, as shown in NISQ and error mitigation.
  • Compare against a classical baseline. Exact simulation up to roughly 30 to 40 qubits is cheap, which makes it a good sanity check for small tests.
  • Mind the cost model. Some vendors charge per task plus per shot, others by device time. Estimate before submitting large sweeps.
  • Keep the QASM or the circuit object in version control; it makes experiments reproducible, along with the backend name and calibration timestamp.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is OpenQASM?

    It is the common exchange format for circuits, like assembly for quantum programs.
  2. 2. Why do routing steps hurt result quality on hardware with limited connectivity?

    Each SWAP costs three CNOTs, which multiplies the error accumulated in the circuit.
  3. 3. About how does statistical error scale with the number of shots?

    Estimating a probability to precision epsilon needs about 1/epsilon^2 shots.
  4. 4. Which step must be done against the exact target device?

    Layout and routing depend on that device's connectivity, native gates and current calibration.

Summary

  • OpenQASM is the plain-text exchange format for circuits; OpenQASM 3 adds classical control flow and timing.
  • A hardware job is transpiled for the backend, queued, run for many shots, then analyzed and possibly error-mitigated.
  • Limited connectivity forces SWAPs, so depth and two-qubit gate count after routing decide quality.
  • Use simulation first, check calibrations, report error bars and keep experiments reproducible.

Next: VQE and Variational Algorithms