Skip to content
Current section
The Quantum Computing Enthusiast’s Hub
Saved
All sections

All sections

Saved
Skip Learn navigation
Encyclopedia
Topic

OpenQASM reference language

A language for describing quantum operations and associated classical control, with explicit versioned syntax.

Also called Open Quantum Assembly Language

Edition released Coverage: Overview · Technical
On this page

Definition and significance

OpenQASM provides a textual representation of quantum programs. Its language specification distinguishes quantum registers, classical values, gates, measurements and control structures. Version 3 adds features beyond the earlier circuit-oriented language; the exact language version and supported implementation subset must accompany code examples.

The language describes intended operations. A parser accepting a program does not prove a device supports every timing feature, calibration construct or control-flow operation it contains.

Mechanism and mathematical treatment

An original version-3 reference fragment is: OPENQASM 3.0; include "stdgates.inc"; qubit[2] q; bit[2] c; h q[0]; cx q[0], q[1]; c = measure q;. It declares two qubits and two classical bits, prepares a Bell state, and records computational measurements. The standard-gate include supplies named gate definitions; a reader should not infer a hardware-native pulse implementation from those names.

Program semantics include classical control and measurement side effects, so reversing a complete measured program is not the same as taking an adjoint of a unitary gate sequence. Timing constructs likewise require an implementation's interpretation.

Fully worked reference example

Before the measurement, the fragment's ideal state is (|00>+|11>)/sqrt(2) under the stated q0,q1 convention. After measurement, c records either 00 or 11 with probability 1/2. If the classical result is forgotten, the remaining quantum description is the mixture (|00><00|+|11><11|)/2, which lacks the off-diagonal coherence of the Bell state.

This distinction can be checked by the XX expectation: it is 1 before measurement and 0 for the forgotten-outcome mixture. The measurement statement therefore has scientific meaning beyond converting an amplitude vector into a displayed string.

Limitations and common misconceptions

An OpenQASM file is not an automatic portability guarantee across all providers. Supported gates, dynamic control and timing subsets differ. Record source version, parser or compiler version, and any implementation-specific extensions before describing a snippet as executable.

Scientific relationships

A textual program can specify gates, measurements and classical control.

A measured program with conditional correction

An original OpenQASM 3 reference program can be written as the following sequence: declare OPENQASM 3.0; include the standard gate library; declare qubit[2] q and bit[2] c; reset q; apply h q[0]; apply cx q[0],q[1]; assign c[0]=measure q[0]; if c[0] equals one, apply x q[1]; finally assign c[1]=measure q[1]. Spaces and statement boundaries are part of an actual source rendering; this prose sequence specifies the complete intended program without claiming parser execution.

Reset establishes |00>. The two gates prepare |Phi+>. The first measurement returns c0=0 or 1 with probability one-half and leaves the quantum state |00> or |11>, respectively. Conditional X on q1 changes the latter branch to |10> while leaving the former |00>. The final q1 measurement therefore always returns c1=0. The logical classical pairs (c0,c1) are (0,0) and (1,0), each with probability one-half.

If those pairs are serialized as c1c0 strings, the possible strings are 00 and 01. That serialization is an explicit presentation convention for this example, not a claim that every consumer of an OpenQASM program must display results in that order. The language binds named classical values; a result interface determines how they are packaged.

Why classical feedback is not a controlled unitary

The conditional X is chosen using a measured classical value. The program has already destroyed coherence between the two Bell branches. Replacing the measurement and classical condition by an unmeasured CNOT produces a different intermediate process, even if some final probabilities coincide. In the measured program, the first classical bit is a retained random record. A wholly unitary circuit cannot simply erase that record while preserving every other output without changing the interface.

If the first outcome is forgotten after correction, the quantum state is (I/2) on q0 tensor |0><0| on q1. If it is retained, the joint classical-quantum state includes a perfect correlation between c0 and q0. The distinction matters if later instructions use c0. Comparing only the final q1 distribution would certify too little: many incorrect programs also always return q1=0.

For example, applying the conditional X to q0 instead of q1 leaves q1 equal to the original random outcome. The final classical pairs become (0,0) and (1,1). A full branch trace detects this target mistake immediately. The first measurement is still random in both programs, so checking only its marginal cannot detect the defect.

Gate modifiers and reversibility boundaries

An inverse modifier on a unitary gate denotes its adjoint under the language's gate semantics. It does not turn an arbitrary measured subroutine into a reversible operation. The inverse of a sequence of unitary gates reverses their order and replaces each by its adjoint. Measurement and reset are nonunitary on the visible qubits, so that construction does not apply to the entire feedback program.

For a simple unitary subroutine applying R_Z(alpha) followed by R_Y(beta), the inverse applies R_Y(-beta) followed by R_Z(-alpha). Reversing the angles without reversing order generally fails. A controlled modifier also makes the subroutine's overall phase relevant to a larger circuit. A source-level gate definition should therefore preserve the phase convention used by any later controlled instance.

The standard-gate include supplies mathematical gate definitions. It does not assert that a device has a native pulse implementing each named gate. A target compiler can decompose them while preserving the required semantics and meeting its supported control-flow subset. Acceptance by a parser establishes syntax and perhaps static checks; it is not evidence that the complete program can run on every target.

Timing statements and version-specific features

A barrier constrains reordering across a boundary; it is not by itself a specified physical wait duration. A delay expresses a duration and requires a target timing interpretation. These constructs can change scheduling without changing an ideal untimed unitary description, while decoherence can make their physical effects significant. Timing and logical transformations should therefore be represented separately in a scientific account.

The live OpenQASM documentation accessed for this extension contains features with explicit later-version annotations. This example uses only a stated version-three core subset and does not assume that every feature on the live site belongs to the 3.0 grammar. The complete source block below pins OpenQASM 3.0 and identifies its unrun status. A separate executed NumPy branch calculation verifies the ideal probabilities and mixed state. No parser, compiler, or hardware execution is claimed here.

OpenQASM 3.0 reference program

This complete source uses the OpenQASM 3.0 core declarations, standard gates, projective measurement, and conditional branch. Reset supplies an explicit initial state. The first measurement records the Bell-branch label; the conditional X maps both branches to a zero target. Thus the ordered pair (c[0], c[1]) is (0,0) or (1,0), each with probability one half. A presentation that prints the register in reverse index order displays 00 or 01. The program itself does not prescribe a host application's histogram string order.

The reset, assignment-style measurement, comparison, and if-block semantics were checked against the official OpenQASM live specification accessed on 2026-12-03. This snippet declares version 3.0 and uses none of the later-version features identified in that documentation. No parser, compiler, SDK, or device executed this source. The separately executed NumPy program in the next section verifies its ideal branch calculation without claiming to validate a particular implementation's OpenQASM support.

openqasm · OpenQASM 3.0 core; stdgates.inc · Not executed or parsed; syntax and ideal semantics checked against official specification.

OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
bit[2] c;
reset q;
h q[0];
cx q[0], q[1];
c[0] = measure q[0];
if (c[0] == 1) {
    x q[1];
}
c[1] = measure q[1];

Executed reference: retained measurement branches

Each projector produces an unnormalized branch vector whose squared norm is its probability. The conditional target flip is applied before branch density matrices are added. The program prints branch records (0, 0, 0.5) and (1, 0, 0.5), where each tuple gives the first measurement, the final target bit, and branch probability. It then prints diagonal [0.5, 0, 0.5, 0] for the state obtained after forgetting the classical record. The off-diagonal entries vanish because a measurement has separated the alternatives. Adding branch vectors before forming a density matrix would incorrectly restore coherence. The retained records and the mixed quantum state therefore answer different, explicitly specified questions.

python · Python 3.12.16; NumPy 2.3.5 · Executed locally as an ideal mathematical reference; no quantum SDK or device used.

import numpy as np
I = np.eye(2, dtype=complex)
X = np.array([[0, 1], [1, 0]], dtype=complex)
bell = np.array([1, 0, 0, 1], dtype=complex) / np.sqrt(2)
rho = np.zeros((4, 4), dtype=complex)
records = []
for bit in (0, 1):
    projector = np.diag([1 - bit, bit])
    branch = np.kron(projector, I) @ bell
    if bit:
        branch = np.kron(I, X) @ branch
    probability = float(np.vdot(branch, branch).real)
    rho += np.outer(branch, branch.conj())
    target_one = np.sum(np.abs(branch.reshape(2, 2)[:, 1]) ** 2)
    assert np.isclose(target_one, 0)
    records.append((bit, 0, round(probability, 8)))
print(records)
print(np.diag(rho).real)
assert np.allclose(rho, np.diag([.5, 0, .5, 0]))

Sources

  • OpenQASM project · Standards or specification · openqasm.com
    Verification scope

    Recorded source check ·

    Official instruction and classical-control documentation inspected for reset, assignment-style measurement, comparison and if-block syntax.

    The live specification contains explicitly marked later-version features. The supplied source declares 3.0 and uses its core subset. No parser, compiler, SDK or device execution is claimed.