Syntax¶
Consider the Bell state example to see how the language syntax works.
qfunc H(inout target : qbit);
qfunc CX(inout control: qbit, inout target : qbit);
qfunc main(input a: qbit, input b: qbit) {
H(a);
CX(a, b);
}
Syntactic Elements¶
Function declaration¶
qfunc H(inout TARGET : qbit);
A function declaration is a way to tell the parser that “this function exists in the engine. I’m just using it.”
All the builtin functions that exist in the JSON model also exist in the QMOD model, and you can declare them, such as X, Y, etc.
Function ports¶
inout TARGET : qbit
When you want to pass variables to a function, you need to define them as function ports.
A port to a function contains:
port type
- can be input (“eats the qubit”) inout (“borrows the qubit”) output (“spews a qubit”)
name
- the name of the port
type
- Right now only a single qbit is supported.
Function definition¶
qfunc main(...) {
...
}
qfunc
defines a function and its implementation,
it begins with qfunc
to signify it is a quantum function, contains a name, ports, parameters (see second example) and a body (defined with {}).
Function call¶
CX(a, b);
Function call is also a type of statement, it invokes another function that is defined or declared functions can be called. autocompletion is supported - please use it to find the functions.
Another example to consider is a circuit with a single RX with angle 0.5.
qfunc RX<theta: real>(inout target : qbit);
qfunc allocate<num_qubits: int>(output target : qbit[num_qubits]);
qfunc main() {
a: qbit;
allocate<1>(a);
RX<0.5>(a);
}
Function parameters¶
theta : real
When you want a function to receive classical parameters, you need to declare them.
a port declaration to a function contains:
name
- the name of the parameter.
classical type
- the type of the parameter. Supports int
or real
.
Parametric function definition¶
qfunc foo<...>(...) {
...
}
The classical parameters of a function are defined in the <...>
section of the declaration.
Parametric function call¶
RX<0.5>(a);
Passing classical parameters to a function is done in the <...>
section of the call.
For now the values may be literals (int or real), or an identifier relating to a classical parameter that is in the scope of the calling function.
Variable declaration¶
a: qbit;
Variable declaration is a type of statement (;
is needed at the end of each statement in the function).
It declares a variable that can be used throughout the function.
It closely relates to a function port, the only difference is that there is no need for the direction.
The size of the variable is stated by qbit[size]
. The default size of a variable is 1.
Variable allocation¶
allocate<1>(a);
A variable starts in an uninitialized state.
To initialize it you should allocate qubits for it by using the allocate
function
with the necessary number of qubits as a parameter.