Classical Control Flow
Loops and conditionals on classical expressions are useful means to describe reusable building blocks. Qmod has two basic forms - the repeat statement and the if statement.Classical Repeat
Syntax
- Python
- Native
Semantics
- Invoke the iteration block count times, binding the index variable to the respective iteration number - 0, 1,… count-1.
- Inside the statement block, use of quantum variables declared outside it is restricted to contexts where the variable is initialized and remains initialized (see Quantum Variables)
Example
The following example defines a useful function - applying the Hadamard function across all qubits in a qubit array - using repeat. Note that a similar function is available in the Classiq open-library.- Python
- Native
Classical If
Syntax
- Python
- Native
_
suffix. This is the case with if_ and else_ in the second function.Semantics
- Invoke the then block if condition evaluates to
trueand otherwise invoke the else block - Inside the statement block, use of quantum variables declared outside it is restricted to contexts where the variable is initialized and remains initialized (see Quantum Variables)
Example
- Python
- Native