Cram sheet — Section 8: Operate with OpenQASM
One-screen review layer for this section: short background primers per objective, then the key facts — each backed by an official source or by an executed proof from the question bank.
Structure types in OpenQASM 3 programs
OpenQASM 3 is a typed language
OpenQASM 2 knew only two aggregate declarations, qreg and creg. OpenQASM 3 replaces them with a real type system: quantum registers become qubit[n] name;, classical registers become bit[n] name;, and a family of classical types joins them.
| Type | Notes |
|---|---|
bool, int[n], uint[n], float[n] | the C-like scalars, each sized by its designator |
complex[float[n]] | the designator is a FLOAT type, never a bare bit width: complex[float[64]] c; |
bit[n], angle[n], duration, stretch | the four "special" types, the ones with no C equivalent |
| not in the language | string, char, real, unsigned |
Two things matter for the exam. First, what Qiskit EMITS: circuits export their registers as bit[n]/qubit[n], loose clbits as bare bit. Second, what Qiskit can PARSE BACK: the importer supports only a subset of the type system, and the official feature table is the authority on which types survive the round trip.
Sources: qasm-feature-table · qasm3 · types.html
Must know:
- Qiskit exports classical registers as
bit[n] c;even at width 1; looseClbits become barebit _bit0;. — ⚙️ proven ins8-q010 - An unbound
Parameterexports to OpenQASM 3 as aninput float[64]declaration — the circuit becomes a parameterized program. — ⚙️ proven ins8-q020 - Casting rules: in a binary operation the LESSER type is promoted (
complex>float>int/uint, wider beats narrower);booland scalarbitare interchangeable as values;bit[n]converts toint[m]/uint[m]/angle[m]only whenm == n; nothing casts to or fromduration; and a width designator must be aconstexpression. — 📖 types.html
Traps:
- The feature table's Qiskit column documents
bitas supported (mapped toClbit/ClassicalRegister) whiledurationandstretchdeclarations are unparseable. — 📖 qasm-feature-table
Interpret OpenQASM semantics
Reading v2 and v3 programs side by side
The same circuit reads differently in the two versions, and four tells answer every "which version is this?" question.
| Feature | OpenQASM 2 | OpenQASM 3 |
|---|---|---|
| header | OPENQASM 2.0; | OPENQASM 3.0; |
| standard include | include "qelib1.inc"; | include "stdgates.inc"; |
| registers | qreg q[2]; creg c[2]; | qubit[2] q; bit[2] c; |
| measurement | measure q[0] -> c[0]; (arrow) | c[0] = measure q[0]; (assignment) |
| conditionals | whole-register equality only: if (c == 1) ... | real if/else, loops, per-bit conditions |
| global phase | not expressible | gphase |
Control flow is the deepest semantic difference, and exporters enforce these language limits at export time: OpenQASM 2 cannot express a per-bit if_test condition or a global phase.
Sources: interoperate-qiskit-qasm2 · interoperate-qiskit-qasm3
Must know:
- OpenQASM 2 conditionals accept only whole-register equality; exporting a per-clbit
if_testraisesQASM2ExportError('OpenQASM 2 only supports register-equality conditions'). — ⚙️ proven ins8-q013 - Custom gates round-trip as
gate mygate ... { ... }declarations in both versions — neither exporter inlines them. — ⚙️ proven ins8-q019
Traps:
qasm2.dumpsSILENTLY drops a circuit's global phase — no error, no warning — whileqasm3.dumpsemitsgphase. — ⚙️ proven ins8-q017
Interoperate different versions of OpenQASM with Qiskit
The four functions, and the one asymmetry that matters
Both qiskit.qasm2 and qiskit.qasm3 expose the same quartet, and the argument tells the functions apart.
| Function | Direction | qiskit.qasm2 | qiskit.qasm3 |
|---|---|---|---|
dumps(qc) | circuit to string | native | native |
dump(qc, stream) | circuit to an OPEN text stream | native | native |
loads(s) | string to circuit | native | needs qiskit-qasm3-import |
load(path) | filesystem PATH to circuit | native | needs qiskit-qasm3-import |
The one asymmetry to memorize: OpenQASM 3 IMPORT is not part of Qiskit itself, it needs the separate qiskit-qasm3-import package, while OpenQASM 3 export and all of qasm2 ship with Qiskit. Mixing up dump and load arguments — a path where a stream belongs, or the reverse — raises immediately.
Must know:
QuantumCircuit.from_qasm_str/from_qasm_filestill exist in Qiskit 2.x for OpenQASM 2 input; the instance method.qasm()was removed. — ⚙️ proven ins8-q012QuantumCircuit.from_qasm_strkeeps the program's own declarations:qreg a[2]; creg m[2];gives registers namedaandm, never the defaultq/c, andmeasure a -> m;expands index by index —a[0]intom[0],a[1]intom[1]. — ⚙️ proven ins8-q029
Traps:
qasm3.loads/loadraiseMissingOptionalLibraryErrorunlessqiskit-qasm3-importis installed;qasm3.dumps/dumpand all ofqasm2are native. — ⚙️ proven ins8-q021qasm2.loadsaccepts only theOPENQASM 2.0;header — feeding it a v3 program raisesQASM2ParseErrorimmediately. — ⚙️ proven ins8-q015
Interact with the Qiskit IBM Runtime REST API
Runtime over plain HTTPS
Everything the primitives do through Python can be done over plain HTTPS. Authentication is two-step: exchange your IBM Cloud API key for an IAM bearer token, then send that token plus a Service-CRN header identifying your instance on every Runtime call. Submitting a primitive job is one request, and results are a sub-resource of the job.
| Request | What it does |
|---|---|
POST /api/v1/jobs | submit a primitive job |
GET /api/v1/jobs/{id} | job details |
GET /api/v1/jobs/{id}/results | the final result |
GET /api/v1/jobs/{id}/logs, /metrics | diagnostics |
POST /api/v1/jobs/{id}/cancel | cancel the job |
DELETE /api/v1/jobs/{id} | remove a job already in a terminal state |
Sources: cloud-setup-rest-api · sampler-rest-api · jobs
Must know:
- IAM token exchange:
POSTthe IBM Cloud API key with the apikey grant type; the bearer token expires after 3600 seconds (expires_in: 3600). — 📖 cloud-setup-rest-api - Submitting a Sampler job over REST is
POST /v1/jobswith'program_id': 'sampler'and your circuits insideparams.pubs. — 📖 sampler-rest-api
Traps:
- Every Runtime REST call after authentication carries two headers: the IAM bearer token and
Service-CRN(the instance identifier — it is not itself a credential). — 📖 cloud-setup-rest-api
- Name the version from the header, the include file, the register keywords and the measure syntax.
- Declare OpenQASM 3 registers as
qubit[n]andbit[n];qreg/cregare OpenQASM 2 only. - Give a
complexdeclaration a float designator —complex[float[64]] c;— never a bare bit width. - Promote the lesser type in a binary operation, and size a
bit[n]cast to the same width. - Remember only OpenQASM 3 IMPORT needs
qiskit-qasm3-import; export and all ofqasm2are native. - Match each function to its argument:
dumps/loadsstrings,dumpan open stream,loada path. - Expect OpenQASM 2 export to reject a per-bit condition and to drop a global phase silently.
- Send the IAM bearer token plus the
Service-CRNheader on every Runtime REST request.
Every fact above is sourced: 📖 links go to official documentation, ⚙️ marks facts observed by executing code against the pinned Qiskit stack (the linked section page shows the proof evidence on its practice questions).