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
Classical Foreach
The foreach statement iterates through the elements of a classical array. The use of foreach iteration variables is more restrictive than that of the repeat and consequently its compilation and execution are more efficient.Syntax
- Python
- Native
iteration callable accepts one or more iteration variables.Semantics
- Invoke the iteration block once for every element of values.
- values must be a value of type
CArray[CReal]orCArray[CArray[CReal]], and its values must be known at compile-time. - If the iteration block accepts a single iteration variable, the elements of values will be bound to it sequentially.
- In the case that values is a nested array (
CArray[CArray[CReal]]), the number of iteration variables must correspond to the length of the inner array. At each iteration, the inner array is unpacked into the iteration variables, with each variable assigned the corresponding scalar value in the specified order. - The iteration variables are link-time: They can be used, e.g., as angles in rotation gates, but not in quantum expressions or as array indices.
- 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).
Examples
In the following example, theforeach statement iterates through the elements
of the classical list [0.1, 0.2] and assigns its elements into the iteration
variable i.
This is equivalent to calling RX and Y twice, once with angle 0.1 and
once with 0.2.
- Python
- Native
foreach statement iterates through the elements
of the classical list [[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]] and assigns its elements into the iteration
variables i and j.
In each iteration, the elements of the nested arrays are “unpacked” into the
iteration variables.
The first iteration assigns 0.1 to i and 0.2 into j, the second
iteration assigns 0.3 into i and 0.4 to j, and the third
iteration assigns 0.5 into i and 0.6 to j.
- Python
- Native