SwiftQuantum IDE

OpenQASM Editor Online: Open, Read and Run a .qasm File in the Browser

What an OpenQASM 2.0 file contains, how to read one, and exactly which statements SwiftQuantum IDE's QASM tab imports into its visual editor, which it skips, and which it reports.

What an OpenQASM file is

OpenQASM, short for Open Quantum Assembly Language, is a text format for writing down a quantum circuit. It is the format most quantum tools can read and write, which makes it the usual way a circuit travels between a textbook, a framework, a simulator and a hardware queue. A `.qasm` file is nothing more than this text, so you can open it in any editor and read it line by line.

The widely exchanged version is OpenQASM 2.0. It is a small language: it declares registers, applies gates to qubits, and measures. A newer version, OpenQASM 3, adds classical types, control flow and timing, and uses different syntax. This page is about OpenQASM 2.0 files, because that is what SwiftQuantum IDE reads.

What a typical file contains

A minimal OpenQASM 2.0 file that prepares a Bell pair looks like this:

```

OPENQASM 2.0;

include "qelib1.inc";

qreg q[2];

creg c[2];

h q[0];

cx q[0],q[1];

measure q[0] -> c[0];

measure q[1] -> c[1];

```

Read it top to bottom:

  • Header. `OPENQASM 2.0;` states the language version. `include "qelib1.inc";` pulls in the standard gate library that defines names such as `h`, `cx` and `rz`.
  • Registers. `qreg q[2];` declares a quantum register named `q` holding two qubits, indexed `q[0]` and `q[1]`. `creg c[2];` declares a classical register of two bits to hold measurement results.
  • Gate statements. `h q[0];` applies a Hadamard to the first qubit. `cx q[0],q[1];` applies a controlled-NOT with `q[0]` as control and `q[1]` as target. Parameterised gates take an angle in parentheses, for example `rz(pi/4) q[0];`.
  • Measurement. `measure q[0] -> c[0];` reads qubit zero into classical bit zero.

Every statement ends with a semicolon, and `//` starts a comment. Larger files add `barrier` statements that separate sections, `gate` blocks that define custom gates, and occasionally `if (c==1)` conditions on a classical register. Those last two are where many online viewers stop understanding the file, so it helps to know what the tool you are using actually parses.

What an online OpenQASM editor should do

If you hold a `.qasm` file and want to see what it does, the useful questions are simple:

  • Can it show the text with colouring so keywords, gate names and numbers are easy to tell apart?
  • Can it draw the circuit from the text, so you check the structure visually?
  • Can it run the circuit on a simulator and show the outcome?
  • Does it tell you what it could not parse, by statement, rather than silently dropping lines? A diagram that is missing a gate without saying so is worse than no diagram.
  • Can it write the circuit back out as OpenQASM after you edit it?

The rest of this page describes how SwiftQuantum IDE's QASM tab answers each of these, exactly as it is built.

The QASM tab in SwiftQuantum IDE

SwiftQuantum IDE is a browser-based circuit editor and simulator. Alongside the drag-and-drop editor it has a QASM tab, which is a code editor with OpenQASM syntax colouring. Using the editor requires signing in.

Viewing and editing. The tab shows the drawn circuit as OpenQASM 2.0 text. You can type or paste your own text over it, and a sync action regenerates the text from whatever is currently on the canvas. The colouring recognises the common OpenQASM keywords and gate names, including a few, such as `sdg`, `tdg` and `u3`, that the importer below does not accept. Colouring is for reading; it is not a promise that a statement will import.

Export. The export action downloads the current text as a `.qasm` file named after your circuit. The text is generated from the same resolved gate list the simulator uses, so what you download is what you ran.

Import. The import action parses the text in the editor and replaces the drawn circuit with the result. It accepts this subset of OpenQASM 2.0:

  • Single-qubit gates `h`, `x`, `y`, `z`, `s`, `t`
  • Rotations `rx`, `ry`, `rz` with an angle expression built from numbers, `pi`, `+ - * /`, unary minus and parentheses
  • Two- and three-qubit gates `cx` (also written `cnot`), `cz`, `swap`, `ccx` and `cswap`
  • `measure q[i] -> c[i]`
  • One quantum register, declared with `qreg`

Comments are stripped. The `OPENQASM`, `include`, `creg` and `barrier` statements are skipped, since they do not change the drawn circuit. Everything else is reported.

What is not imported. Custom `gate` definitions, the generic `u1`, `u2` and `u3` gates, the inverse phase gates `sdg` and `tdg`, `reset`, classical `if` control, and multiple quantum registers are outside the subset. OpenQASM 3 text is not imported at all. These limits are stated here rather than discovered, because the importer tells you about them too.

How problems are reported. Each statement the importer cannot handle produces an error naming the statement number and the reason, for example an unsupported gate name, an operand it cannot read, or a malformed angle. The supported statements are still drawn, so you get the circuit the parser understood plus a message showing the first three problems. If the text declares no register and contains no gates, or declares a register outside the size the editor supports, the import is refused with a message rather than drawing an empty canvas.

A working routine for a .qasm file

1. Open the IDE, switch to the QASM tab, and paste the file's contents.

2. Press Import. Read the message: either a gate and qubit count, or a short list of statements that did not import.

3. For each reported statement, rewrite it in the supported subset. `sdg` and `tdg` are the inverses of `s` and `t`, and both can be written as `rz` rotations; the difference is a global phase, which does not change any measurement result. `u1(λ)` is likewise an `rz` rotation up to global phase. A `gate` block can be expanded inline at each call site. Two quantum registers can be renumbered into one.

4. Import again, then run. After a state-vector run the IDE shows a histogram of outcome probabilities, an amplitude table, and a Bloch sphere for a selected qubit. If you want to understand which engine runs your circuit and why there is a qubit ceiling, see how the online quantum circuit simulator works.

5. Export when you are done. The download is OpenQASM 2.0 and can go back into any tool that reads it.

If you do not have a file to start with, the public gallery contains example circuits, including a Bell state, a GHZ state and a surface-code syndrome round, each defined as OpenQASM and loadable into the editor.

Why the subset is the subset

The importer draws into a visual editor whose palette is fixed: Pauli gates, Hadamard, S and T, the three rotations, CNOT, CZ, SWAP, Toffoli, Fredkin and measurement. Every statement it imports maps to one of those blocks on the canvas. A `gate` definition or a `u3` call has no single block to become, so rather than guess, the importer reports it and leaves the decision to you. That is a narrower reader than a framework's parser, and it is described as such on purpose.

Two things the QASM tab does not do are worth stating plainly. It does not validate your file against the OpenQASM 2.0 specification; it parses what it can draw. And it does not run anything on quantum hardware; the circuit runs on the IDE's classical simulation engines in your browser, which are available depending on your plan.

You can compare plans on the pricing page, or create an account and open the QASM tab with the free state-vector engine.

Paste your .qasm file into SwiftQuantum IDE's QASM tab, import it, and run it in your browser.

Get started