Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Formatting Rules

This document defines the formatting rules for qasmfmt. Rules are based on analysis of examples from the official OpenQASM repository.


1. Version Declaration

// Before
OPENQASM  3.0;

// After
OPENQASM 3.0;
  • Single space between OPENQASM and version number

2. Include Statement

// Before
include"stdgates.inc";

// After
include "stdgates.inc";
  • Single space between include and file path

3. Declarations

3.1 Qubit Declaration

// Before
qubit[2]q;
qubitq;

// After
qubit[2] q;
qubit q;
  • No space between type and [
  • Single space between ] and identifier
  • Single space between type and identifier (when no array)

3.2 Classical Bit Declaration

// Before
bit[4]c;
bit c=0;

// After
bit[4] c;
bit c = 0;
  • Same rules as qubit declaration
  • Spaces around = for initialization

3.3 Constant/Variable Declaration

// Before
const int[32]n=3;

// After
const int[32] n = 3;

4. Gate Calls

4.1 Basic Gates

// Before
hq[0];

// After
h q[0];
  • Single space between gate name and arguments

4.2 Multiple Arguments

// Before
cxq[0],q[1];

// After
cx q[0], q[1];
  • Single space after comma
  • No space before comma

4.3 Parameterized Gates

// Before
rz(pi/4)q[0];
cphase(pi/2)q[0],q[1];

// After
rz(pi / 4) q[0];
cphase(pi / 2) q[0], q[1];
  • No space between gate name and (
  • Spaces around operators inside parameters
  • Single space between ) and arguments

5. Operators

5.1 Arithmetic Operators

// Before
pi/4+pi/8

// After
pi / 4 + pi / 8
  • Spaces around +, -, *, /

5.2 Comparison Operators

// Before
c==1
n!=0

// After
c == 1
n != 0
  • Spaces around ==, !=, <, >, <=, >=

5.3 Logical Operators

// Before
a&&b
c||d

// After
a && b
c || d

5.4 Assignment Operator

// Before
c=measure q;

// After
c = measure q;

6. Control Flow

6.1 If Statement

// Before
if(c==1)x q;
if(c==1){x q;}

// After
if (c == 1) x q;
if (c == 1) { x q; }
  • Single space between if and (
  • Single space between ) and statement/{

6.2 If-Else Statement

if (c == 1) {
  x q;
} else {
  z q;
}
  • Single space between } and else
  • Single space between else and {

6.3 While Statement

// Before
while(flags!=0){...}

// After
while (flags != 0) {
  ...
}

6.4 For Statement

for int i in [0:n] {
  ...
}

7. Gate Definition

// Before
gate mygate(theta)q{rz(theta)q;h q;}

// After
gate mygate(theta) q {
  rz(theta) q;
  h q;
}
  • Single space between gate and gate name
  • Single space between ) and qubit arguments
  • Single space between arguments and {
  • Body indented by 2 spaces
  • } on its own line

8. Subroutine Definition (def)

// Before
def myfunc(qubit q)->bit{bit b;measure q->b;return b;}

// After
def myfunc(qubit q) -> bit {
  bit b;
  measure q -> b;
  return b;
}
  • Spaces around ->
  • Body indented by 2 spaces

9. Measure Statement

9.1 Assignment Form

// Before
c=measure q;

// After
c = measure q;

9.2 Arrow Form

// Before
measure q->c;

// After
measure q -> c;
  • Spaces around ->

10. Barrier / Reset

barrier q;
reset q;
  • Single space between keyword and arguments

11. Indentation

  • Indent size: 2 spaces (default)
  • Style: Spaces only (no tabs)
gate mygate q {
  h q;
  if (c == 1) {
    x q;
  }
}

12. Blank Lines

12.1 After Include

OPENQASM 3.0;
include "stdgates.inc";

qubit q;
  • One blank line after include block

12.2 Around Gate/Def Definitions

qubit q;

gate mygate q {
  h q;
}

h q;
  • One blank line before and after gate/def definitions

12.3 Consecutive Blank Lines

  • Multiple consecutive blank lines are collapsed to one

13. Trailing

13.1 Trailing Whitespace

  • Trailing whitespace on each line is removed

13.2 Trailing Newline

  • Single newline at end of file (default)

Rule Summary

CategoryRuleExample
SpacingAfter includeinclude "file";
SpacingBetween type and identifierqubit[2] q;
SpacingBetween gate and argumentsh q;
SpacingAfter commacx q[0], q[1];
SpacingAround operatorsa + b, c == 1
SpacingBefore braces{ ... }
SpacingAfter if/whileif (cond)
IndentationInside blocks2 spaces
Blank linesAfter include1 line
Blank linesAround gate/def1 line