The Qmod language is advancing from release to release in ways that make algorithm development easier and more productive. In case you missed recent release notes or the additions to the Qmod reference manual, below are a couple of highlights.
One area of improvement is the syntax of numeric assignments. It has been generalized to support arbitrary right-hand-side expressions in both out-of-place and in-place assignments, including constant values. Here is a little example in Qmod/Python that demonstrates its use -
@qfunc
def main(x: Output[QNum], y: Output[QNum], res: Output[QNum]):
x |= 5
allocate(2, y)
hadamard_transform(y)
res |= 3 * x + y
Remember that the operator '|=' is used for out-of-place assignment (this is because = cannot be overloaded in Python). At line 2, 'x' is assigned the constant value 5, and its type is inferred to be a 3-qubit unsigned integer. This used to be done with the library function 'prepare_int()', which is not needed anymore. In line 6, 'res' is assigned the evaluation of the quantum expression '3 * x + y', where 'y' is in an equal superposition of the four values in its domain - [0, 1, 2, 3]. Printing the execution results for 1000 shots may look like this -
state={'x': 5, 'y': 3, 'res': 18} shots=273
state={'x': 5, 'y': 1, 'res': 16} shots=256
state={'x': 5, 'y': 0, 'res': 15} shots=240
state={'x': 5, 'y': 2, 'res': 17} shots=231
Also, the in-place-add operator is now supported generally, in addition to in-place-xor. This is important, as many algorithms involve incrementing a variable or integrating multiple values in a variable. As an example, consider summing up an array of numbers in superposition. This can be coded directly as follows (this time in Qmod/Native) -
qfunc sum_array(data: qnum[], res: qnum) {
repeat (i: data.len) {
res += data[i];
}
}
Note that coding the same in Qmod/Python would look very similar, using the Python-function syntactic form of 'in-place-add inplace_add(data[i], res)' because lambda expressions in Python cannot accommodate assignment statements using '+='.
Check it out yourself on the Classiq platform!