|
13 | 13 | "source": [ |
14 | 14 | "Classical variables and operations lie at the heart of the Qmod language, but they are complemented by classical control flow and operations, making it easier to design quantum algorithms with hybrid quantum-classical logic. With the latest updates, Qmod now fully supports Python's classical operations, such as `for` loops, `if-else` statements, and more, smoothly integrating with quantum workflows.\n", |
15 | 15 | "\n", |
16 | | - "In Qmod, equivalent types for common Python `int`, `float`, `list`, and `bool` types are denoted by `int`, `real`, `array`, and `bool` in the native syntax and by `CInt`, `CReal`, `CArray`, and `CBool` in the Python SDK. The Qmod types can be used in quantum workflows, though Python types can also be passed as classical parameters. For example, when writing quantum workflows, it is possible to use both the lenght of a quantum array, which is a Qmod quantity, or a Python `int` quantity to control the action of a loop. This ensures the type consistency required for quantum control flow operations like `repeat` (the Qmod equivalent of a Python `for` loop) and `if-else` statements, which enable sophisticated hybrid control logic, see a [detailed description](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/classical-control-flow/). Additionally, Qmod supports arrays of classical values and more configurable data structures called `Struct`s (equivalent to Python dataclasses without methods). See the full list of [classical variables and types supported in Qmod](https://docs.classiq.io/latest/qmod-reference/language-reference/classical-types/#structs). \n", |
| 16 | + "In Qmod, equivalent types for common Python `int`, `float`, `list`, and `bool` types are denoted by `int`, `real`, `array`, and `bool` in the native syntax and by `CInt`, `CReal`, `CArray`, and `CBool` in the Python SDK. The Qmod types can be used in quantum workflows, though Python types can also be passed as classical parameters. For example, when writing quantum workflows, it is possible to use both the length of a quantum array, which is a Qmod quantity, or a Python `int` quantity to control the action of a loop. This ensures the type consistency required for quantum control flow operations like `repeat` (the Qmod equivalent of a Python `for` loop) and `if-else` statements, which enable sophisticated hybrid control logic, see a [detailed description](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/classical-control-flow/). Additionally, Qmod supports arrays of classical values and more configurable data structures called `Struct`s (equivalent to Python dataclasses without methods). See the full list of [classical variables and types supported in Qmod](https://docs.classiq.io/latest/qmod-reference/language-reference/classical-types/#structs). \n", |
17 | 17 | "\n", |
18 | | - "While Qmod enables the integration of Python types within its quantum workflows, the converse is not true. Python control flow constructs, such as `range`, `for` loops and `if-else` statements, are central to managing logic and iteration in classical programming. These constructs enable dynamic behavior, such as iterating over lists or conditionally executing code based on runtime values. However, it is not possible to use Qmod-specific quantities, such as the `len` of a quantum array, within Python’s native control flow. Qmod quantities remain tightly integrated within the Qmod execution environment and cannot directly influence Python logic, ensuring a clear separation between classical Python constructs and Qmod quantum operations." |
| 18 | + "While Qmod enables the integration of Python types within its quantum workflows, the converse is not true with the capabilities covered so far. Python control flow constructs, such as `range`, `for` loops and `if-else` statements, cannot yet incorporate Qmod-specific quantities such as the `len` of a quantum array, within Python’s native control flow. Qmod quantities remain tightly integrated within the Qmod execution environment and cannot directly influence Python logic, ensuring a clear separation between classical Python constructs and Qmod quantum operations." |
19 | 19 | ] |
20 | 20 | }, |
21 | 21 | { |
|
33 | 33 | "\n", |
34 | 34 | "- **`range`**: Generate sequences of classical values to iterate quantum operations dynamically over classical types within Qmod types. \n", |
35 | 35 | "- **`if` statements**: Define conditional logic for generating and applying quantum operations using Qmod types as classical control parameters.\n", |
36 | | - "- **Threat quantum constants as classical**: Converts parameters intricate to the quantum systems to Python types. This allows one to use, for example, integration of functions from external packages such as `math.sqrt` over classical quantities of a quantum object, such as the `len` of a quantum array.\n", |
| 36 | + "- **Treating quantum constants as classical**: Converts parameters intricate to the quantum systems to Python types. This allows one to use, for example, integration of functions from external packages such as `math.sqrt` over classical quantities of a quantum object, such as the `len` of a quantum array.\n", |
37 | 37 | "\n", |
38 | 38 | "\n", |
39 | 39 | "Generative functions provide an elegant way to manage hybrid classical-quantum workflows, enhancing the expressiveness of your quantum algorithms. For more details and examples, refer to the [Generative Functions documentation](https://docs.classiq.io/latest/qmod-reference/language-reference/generative-functions/?h=ge).\n", |
|
60 | 60 | "\n", |
61 | 61 | "The argument of the `main` function is a qubit array named `x`. Initalize it to the state $|0000000000\\rangle$ with $10$ qubits using the `allocate` function.\n", |
62 | 62 | "\n", |
63 | | - "After that, by allowing generative functions by setting `@qfunc(generative=True)`, it is possible to use the lenght of the qubit array `x` as a classical variable, and apply quantum operations over qubits dynamically, using Python's `if` and `for` statements over the qmod `x.len` quantity, according to the pre-defined rule of flipping the state of the even qubits. \n", |
| 63 | + "After that, by allowing generative functions by setting `@qfunc(generative=True)`, it is possible to use the length of the qubit array `x` as a classical variable, and apply quantum operations over qubits dynamically, using Python's `if` and `for` statements over the qmod `x.len` quantity, according to the pre-defined rule of flipping the state of the even qubits. \n", |
64 | 64 | "\n", |
65 | 65 | "The condition of whether `i` is even evaluated is using the `%` modulo operation, which calculates the reminder of the integer `i` divided by $2$." |
66 | 66 | ] |
|
174 | 174 | "cell_type": "markdown", |
175 | 175 | "metadata": {}, |
176 | 176 | "source": [ |
177 | | - "In this example, the task is to prepare the $N$-qubit state $|0001111000\\dots\\rangle$, where the state $|1\\rangle$ is prepared between the qubits over the integer interval $(N/3, N/2)$. For this, it is possible to create a program that applies the $X$ operation to the correct qubits. In this example, consider a total of $N=24$ qubits." |
| 177 | + "In this example, the task is to prepare the $N$-qubit state $|00011111000\\dots\\rangle$, where the state $|1\\rangle$ is prepared in the qubits over the integer interval $(N/3, N/2)$. For this, it is possible to create a program that apply alternately the $X$ operation to the correct qubits. In this example, consider a total of $N=24$ qubits." |
178 | 178 | ] |
179 | 179 | }, |
180 | 180 | { |
|
187 | 187 | "@qfunc(generative=True)\n", |
188 | 188 | "def main(x: Output[QArray]):\n", |
189 | 189 | " allocate(24, x)\n", |
| 190 | + " k=0\n", |
190 | 191 | " for i in range(x.len//3,x.len//2):\n", |
191 | 192 | " X(x[i])" |
192 | 193 | ] |
|
0 commit comments