Skip to main content
The playground runs OpenQASM 3.0, the third major version of the Open Quantum Assembly Language. This page documents the syntax constructs you can use, with examples drawn from the built-in example programs.

Version declaration

Every program must begin with a version declaration on the first non-comment line.

Qubit declarations

Declare a register of qubits with a size, or a single named qubit without brackets.
You can use a const int as the register size:

Reset

reset initializes one or more qubits to the |0⟩ state. Always reset qubits before use.

Built-in gate: U

U(theta, phi, lambda) is the universal single-qubit gate. All other gates are defined in terms of it.

Gate definitions

Define reusable gates with the gate keyword. The body expresses the gate in terms of U or other already-defined gates.
Parameterized gates accept angle arguments:

Controlled gates

Use ctrl @ to add a control qubit to any gate. Use ctrl(n) @ to add n control qubits.
The Shor’s algorithm example uses a Toffoli gate (ccx) and a 4-controlled X gate:

Gate applications

Apply a gate by naming it followed by its qubit arguments. Use bracket notation to address individual qubits in a register.
You can also apply a gate to an entire register in one statement:

Measurement

measure collapses qubits and records outcomes. Measured qubits remain in the circuit; subsequent gates still apply.
The quantum teleportation example measures two qubits mid-circuit, then the remaining qubit is acted on by classical corrections:

Subroutine definitions

Use def to define reusable subroutines. Parameters are typed with qubit or qubit[n].
Subroutines can also accept fixed-size qubit registers:
Call subroutines by name:

Constants

Declare compile-time integer or float constants with const.
Constants can be used as register sizes and in expressions:

Variables and expressions

Declare runtime integer variables and use arithmetic expressions:

For loops

Iterate over an integer range with for int:
Nested loops are supported:

Comments

Single-line comments start with //. There are no block comments.

Complete example: Bell state

The Bell state program demonstrates version declaration, gate definitions, qubit declarations, reset, gate application, and the structure of a minimal program.

Complete example: Grover’s algorithm

This example shows constants, integer variables, for loops, complex subroutines, and multi-register operations.