Semantic Rules
This page documents compile-time rules enforced by the semantic analyser (src/bloch/semantics). Errors report 1-based line and column.
Names and scopes
- Lexical scoping for
{ ... }blocks and functions. - No shadowing: redeclaring a variable in any active scope is an error.
- Parameters live in the function scope and may not be redeclared.
Variables
finalvariables cannot be assigned to after declaration.- A variable must be declared before use.
- Only
qubitmay be declared with commas:qubit q0, q1;. Other types must be one per declaration. @trackedmay only annotatequbitorqubit[]; otherwise it is an error.
Functions
@quantumfunctions must returnbitorvoid.- Non-void functions must have at least one
returnalong all paths. voidfunctions may notreturna value.- Calling an undefined function is an error. Built-in gates are recognised as functions for arity/type checks.
Calls and built-ins
- Built-in gate signatures (also used for type-checking):
h(qubit) -> voidx(qubit) -> voidy(qubit) -> voidz(qubit) -> voidrx(qubit, float) -> voidry(qubit, float) -> voidrz(qubit, float) -> voidcx(qubit, qubit) -> void
- Argument counts and types are checked. Example:
rx(q, 1);is invalid (1isintnotfloat). - Assigning the result of a
voidfunction (user-defined or built-in) is an error.
Postfix operators
i++andi--are only valid on non-finalvariables of typeintonly.- Postfix targets must be variables (not expressions like
(a+b)++).
Arrays
- Element type must be a primitive (
int, float, char, string, bit, qubit). qubit[]cannot be initialised with an array literal; use a fixed size (qubit[N] r;).- Fixed-size arrays without an initialiser are default-initialised (
0,0.0,"",'\0', or newly allocated qubits). - Array literals type-check all elements; permissive conversions are applied where implemented at runtime:
int[]acceptsint,bit, andfloat(truncated toint).float[]acceptsfloat,int, andbit(promoted tofloat).bit[]accepts onlybit.string[]accepts onlystring.char[]accepts onlychar.
- Array element assignment checks index bounds and value type.
- Assignment into an array must target a variable array (e.g.,
a[i] = ...;).
Measurement and reset
measure q;is a statement that collapses a qubit and records a classical bit.measure qis also an expression that returns abitvalue.reset q;returns the qubit to|0>even if previously measured.
@tracked variables and shots
@trackedqubits and registers accumulate outcome counts at scope exit. If not measured, an outcome of"?"is recorded.- With
--shots=N, the runtime executes the programNtimes and aggregates tracked outcomes across runs.