OpenQASM 3¶
This page covers the language features, operators, built-in functions, constants, and known limitations of the OpenQASM 3.x implementation in QuantumRingsLib. For import and execution instructions, see QASM Import & Execution.
Supported operators¶
The tables below show which operators are supported for each data type. ✔ = supported, ✘ = not supported.
Unary operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Bitwise NOT |
✔ |
✔ |
✘ |
✘ |
✔ |
✔ |
✘ |
|
Logical NOT (tests if zero) |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
|
Unary plus (multiplies literal by +1) |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
|
Unary minus (multiplies literal by -1) |
✔ |
✔ |
✔ |
✔ |
✘ |
✘ |
✔ |
Power operator
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Power |
✔ |
✔ |
✔ |
✔ |
✘ |
✘ |
✔ |
Multiplicative operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Multiplication |
✔ |
✔ |
✔ |
✔ |
✘ |
✘ |
✔ |
|
Division |
✔ |
✔ |
✔ |
✔ |
✘ |
✘ |
✔ |
|
Modulus |
✔ |
✔ |
✔ |
✘ |
✘ |
✘ |
✔ |
Additive operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Addition |
✔ |
✔ |
✔ |
✔ |
✘ |
✘ |
✔ |
|
Subtraction |
✔ |
✔ |
✔ |
✔ |
✘ |
✘ |
✔ |
Shift operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Left shift |
✔ |
✔ |
✘ |
✘ |
✘ |
✔ |
✘ |
|
Right shift |
✔ |
✔ |
✘ |
✘ |
✘ |
✔ |
✘ |
Relational operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Less than |
✔ |
✔ |
✔ |
✘ |
✘ |
✘ |
✔ |
|
Less than or equal to |
✔ |
✔ |
✔ |
✘ |
✘ |
✘ |
✔ |
|
Greater than |
✔ |
✔ |
✔ |
✘ |
✘ |
✘ |
✔ |
|
Greater than or equal to |
✔ |
✔ |
✔ |
✘ |
✘ |
✘ |
✔ |
Equality operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Equals |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
|
Does not equal |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
✔ |
Bitwise operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Bitwise AND |
✔ |
✔ |
✘ |
✘ |
✔ |
✔ |
✘ |
|
Bitwise XOR |
✔ |
✔ |
✘ |
✘ |
✔ |
✔ |
✘ |
|
Bitwise OR |
✔ |
✔ |
✘ |
✘ |
✔ |
✔ |
✘ |
Logical operators
Symbol |
Description |
UINT |
INT |
FLOAT |
COMPLEX |
BOOL |
BIT |
Angle |
|---|---|---|---|---|---|---|---|---|
|
Logical AND |
✔ |
✔ |
✘ |
✘ |
✔ |
✔ |
✘ |
|
Logical OR |
✔ |
✔ |
✘ |
✘ |
✔ |
✔ |
✘ |
Operator precedence¶
Operators are evaluated in the following order (1 is highest):
# |
Operator class |
Operators |
|---|---|---|
1 |
Primary |
Functions, variables, constants, literals |
2 |
Unary |
|
3 |
Power |
|
4 |
Multiplicative |
|
5 |
Additive |
|
6 |
Shift |
|
7 |
Relative |
|
8 |
Equality |
|
9 |
Bitwise AND |
|
10 |
Bitwise XOR |
|
11 |
Bitwise OR |
|
12 |
Logical AND |
|
13 |
Logical OR |
|
Built-in functions¶
Function |
Description |
|---|---|
|
Returns the number of elements in an array. See the OpenQASM specification for full details. |
|
Trigonometric and inverse trigonometric functions. |
|
Natural exponential. |
|
Natural logarithm. |
|
Square root. |
|
Cube root. |
|
Ceiling (round up to nearest integer). |
|
Floor (round down to nearest integer). |
|
Modulus. |
|
Bitwise rotate left / right. |
|
Count the number of set bits. |
Built-in constants¶
Constant |
Value |
|---|---|
|
3.14159265358979323846264338327950288 |
|
2 × pi |
|
2.718281828459045 |
Machine accuracy¶
For equality checks, the numeric limits of single and double precision IEEE 754 standards are used.
Feature limitations¶
The following features have known limitations in the current release.
Gate modifiers
Only up to four
ctrl/negctrlmodifiers are supported. For example, the following statement uses six modifiers and is not supported:negctrl(3) @ ctrl @ negctrl(2) x a[0], a[1], a[2], b[0], b[1], b[2], f; // ✘The
powmodifier supports integer exponents only. Fractional power is not supported:pow(0.5) @ x q[0]; // ✘invandpowmodifiers cannot be mixed withctrland/ornegctrlmodifiers.Only the following gates support the
invmodifier:csx,dcx,iswap,swap,cswap,t,tdg,s,sdg,sx,sxdg,cs,csdg,p,r,rv,rx,ry,rz,u,u0,u1,u2,u3,cp,crx,cry,crz,cu,cu1,cu3,rxx,ryy,rzx,rzz,h,x,y,z,cx,cy,cz,ecrGate results are not optimized when modifiers are used.
Control flow in gates
Loops and conditionals are not supported inside user-defined gates. For example, the following definition is not supported:
gate entangler q { for uint i in [0:n-2] { cx q[i], q[i+1]; } }
Users are advised to define gates as subroutines. The previous code can be written as follows:
qubit[10] q_1;
def entangler (qubit[10] q)
{
for uint i in [0:sizeof(q)-2] { cx q[i], q[i+1]; }
}
entangler(q_1);
Reserved keywords
OpenQASM reserved keywords and gate names (including x, y, z, i) cannot
be used as variable, constant, subroutine, or user-defined gate names.
OpenPulse
cal and defcal blocks are not supported.
pragma and annotations
All pragma statements and @ annotation directives are ignored.
input / output
Only the
bitdata type can be marked asoutput.A
bitregister cannot be marked as bothinputandoutput.
Register dimensions
Register sizes must be determinable at compile time. The following is allowed:
const int[64] xx = 5;
qubit[xx] input_1; // ✔
The following is not allowed:
int[64] xx = 5 + 5 * measure b;
qubit[xx] input_1; // ✘
Mixing precision
Similar precision should be used throught the program. float is a single precision number. angle is double precision by default. Mixing both could result in numeric differences. If the angle data type is used, use float[64].
Parameter broadcasting
Parameter broadcasting for subroutines is not supported. The following is allowed:
const int[64] xx = 5;
qubit[xx] input_1; // ✔
The following is not allowed:
int[64] xx = 5 * measure b;
qubit[xx] input_1; // ✘
Unicode
Unicode character sets and Unicode identifiers are not currently supported.