Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
"First, understand Qmod through an example. \n",
"\n",
"The task is to design a quantum algorithm that coherently computes the arithmetic operation $y=x^2+1$, for a quantum variable $|x\\rangle$ that is a superposition of all the numbers between $0$ and $7$:\n",
"$\\begin{equation}\n",
"$$\n",
"|x\\rangle = \\frac{1}{\\sqrt{8}}(|0\\rangle+|1\\rangle+\\dots +|7\\rangle.\n",
"\\end{equation}$\n",
"$$\n",
"The expected output is \n",
"\n",
"$\\begin{equation}\n",
"$$\n",
"|x\\rangle |y\\rangle = |x\\rangle |x^2+1\\rangle = \\frac{1}{\\sqrt{8}}\\sum_{i=0}^{7}|i\\rangle|i^2+1\\rangle,\n",
"\\end{equation}$\n",
"$$\n",
"where $|x\\rangle$ is entangled to $|y\\rangle$.\n"
]
},
Expand All @@ -52,19 +52,18 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from classiq import *\n",
"\n",
"\n",
"@qfunc\n",
"def main(x: Output[QNum], y: Output[QNum]):\n",
"\n",
" allocate(3, x)\n",
" hadamard_transform(x) # creates a uniform superposition\n",
" y |= x**2 + 1"
" y |= x**2 + 1\n",
"qmod= create_model(main)"
]
},
{
Expand All @@ -76,11 +75,19 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"quantum_program = synthesize(create_model(main))\n",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/2tG4SoTsSuRAR3abUk4RE5Ui1xr?version=0.66.1\n"
]
}
],
"source": [
"quantum_program = synthesize(qmod)\n",
"show(quantum_program)"
]
},
Expand All @@ -89,7 +96,7 @@
"metadata": {},
"source": [
"<div style=\"text-align:center;\">\n",
" <img src=\"https://docs.classiq.io/resources/design.gif\">\n",
" <img src=\"https://docs.classiq.io/resources/design_basic_gif.gif\">\n",
"</div>"
]
},
Expand Down Expand Up @@ -199,7 +206,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -223,7 +230,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.7"
},
"vscode": {
"interpreter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,40 @@
"machine_precision": 8,
"custom_hardware_settings": {
"basis_gates": [
"tdg",
"u",
"sx",
"t",
"r",
"u2",
"ry",
"id",
"z",
"cz",
"sdg",
"x",
"cz",
"ry",
"u1",
"cx",
"rx",
"u2",
"p",
"sx",
"sxdg",
"sdg",
"h",
"y",
"cy",
"rz",
"s",
"y",
"u1",
"t",
"h",
"rz"
"rx",
"tdg",
"u",
"z"
],
"is_symmetric_connectivity": true
},
"debug_mode": true,
"synthesize_all_separately": false,
"output_format": ["qasm"],
"optimization_level": 3,
"output_format": [
"qasm"
],
"pretty_qasm": true,
"transpilation_option": "auto optimize",
"timeout_seconds": 300,
"random_seed": 4059461065
"random_seed": 507008224
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"\n",
"Simply put, quantum operations are functions of functions applied on quantum objects that are very common in quantum computing, hence receiving a special place in the Qmod language. More accurately, these are built-in statements in the Qmod language. A statement is a building block of a programming language, such that programming languages are composed from statements. For example, `if` and `for` loops are common statements in programming languages such as Python.\n",
"\n",
"There are a few quantum operation statements in Qmod, and here we focus on arguably the most useful one: `control`. It applies a specified quantum function conditioned on some state (value) of a given quantum variable. Other quantum operations are `invert`, `power`, and `within_apply`. See all the quantum operations [here](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/control/).\n",
"There are a few quantum operation statements in Qmod, and here we focus on arguably the most useful one: `control`. It applies a specified quantum function conditioned on some state (value) of a given quantum variable. Other key quantum operations are `invert`, `power`, `within_apply` and `bind`. \n",
"\n",
"A list of all quantum operations can be [found here](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/control/).\n",
"\n",
"Examine the `control` statement using a concrete example."
]
Expand All @@ -31,10 +33,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The task is to prepare a quantum number `x` in a superposition of all possible integers between $0$ to $15$, i.e., $|x\\rangle = \\frac{1}{\\sqrt{16}}(|0\\rangle + |1\\rangle + \\dots + |15\\rangle)$. Then, prepare another quantum number `y` that is in the state $|17\\rangle$ only if $|x\\rangle$ is in the state $|15\\rangle$, otherwise the state of `y` should be $|0\\rangle$. Mathematically, the task is to prepare this state:\n",
"$\\begin{equation}\n",
"|x\\rangle|y\\rangle = \\frac{1}{\\sqrt{16}}(|0\\rangle|0\\rangle+|1\\rangle|0\\rangle+\\dots+|14\\rangle|0\\rangle+|15\\rangle|17\\rangle).\n",
"\\end{equation}$\n",
"The task is to prepare a quantum number `x` in a superposition of all possible integers between $0$ to $15$, i.e., $|x\\rangle = \\frac{1}{\\sqrt{16}}(|0\\rangle + |1\\rangle + \\dots + |15\\rangle)$. \n",
"Then, prepare another quantum number `y` that is in the state $|17\\rangle$ only if $|x\\rangle$ is in the state $|15\\rangle$, otherwise the state of `y` should be $|0\\rangle$. Mathematically, the task is to prepare this state:\n",
"$$\n",
"|x\\rangle|y\\rangle = \\frac{1}{\\sqrt{16}}(|0\\rangle|0\\rangle+|1\\rangle|0\\rangle+\\dots+|14\\rangle|0\\rangle+|15\\rangle|17\\rangle)\n",
"$$\n",
"\n",
"How to approach this task? You have already seen how to create a uniform superposition with the `hadamard_transform`. Now, conditioned on the value of $|x\\rangle$ being $|15\\rangle$, prepare the state of $|y\\rangle$ to be $|17\\rangle$ with the `inplace_xor` function."
]
Expand All @@ -43,18 +46,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<details>\n",
"<summary> Numeric Assignment </summary>\n",
"<details markdown>\n",
"<summary markdown> Numeric Assignment </summary>\n",
"\n",
"Numeric assignments provide simple operations (using Qmod and SDK) and functions (SDK only) for performing arithmetic and logical operations. The `inplace_xor` is the fufunction is equivalent to the `^=` operation. In the SDK, lambda (as a function rather than an operation).\n",
"Numeric assignments provide simple operations (using Qmod and SDK) and functions (SDK only) for performing arithmetic and logical operations. The `inplace_xor` is a function that is equivalent to the `^=` operation. In the SDK, lambda (as a function rather than an operation).\n",
"Read more [here](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/numeric-assignment/).\n",
"\n",
"</details>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -70,14 +73,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In the Python syntax, `control` is a Python function with two arguments. The `ctrl` argument is the condition for which the `stmt_block` operand is applied. The `stmt_block` argument is the specific operation/function to apply, given that the condition is satisfied. Passing a function as an argument for another function is done with the `lambda:` keyword. **So, anytime you pass a function as an argument in the Python SDK, use the prefix `lambda:`**.\n",
"\n",
"<details>\n",
"<summary>More on `lambda:`</summary>\n",
" \n",
"The `lambda:` keyword is used to define inline functions.\n",
"\n",
"</details>\n",
"In the Python syntax, `control` is a Python function with two arguments. The `ctrl` argument is the condition for which the `stmt_block` operand is applied. The `stmt_block` argument is the specific operation/function to apply, given that the condition is satisfied. Passing a function as an argument for another function is done with the `lambda:` keyword, used to define inline functions. **So, anytime you pass a function as an argument in the Python SDK, use the prefix `lambda:`** ([read more](https://docs.classiq.io/latest/qmod-reference/language-reference/operators/?h=lambda)).\n",
"\n",
"In the native syntax, `control` is embedded in the Qmod language such that the condition is specified with the `()` parentheses, and the operand to apply is specified within the scope of the `control`, i.e., within the `{}` curly brackets.\n",
"\n",
Expand All @@ -86,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -111,9 +107,17 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/2tGBRg4EEWzMv3njP2fkDXTpWYY?version=0.66.1\n"
]
}
],
"source": [
"quantum_program = synthesize(create_model(main))\n",
"show(quantum_program)"
Expand All @@ -124,7 +128,8 @@
"metadata": {},
"source": [
"<div style=\"text-align:center;\">\n",
" <img src=\"https://docs.classiq.io/resources/quantum_operations_synthesize.gif\">\n",
" <img src=\"https://docs.classiq.io/resources/Design_operations_first.gif\n",
"\">\n",
"</div>"
]
},
Expand All @@ -137,14 +142,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'x': 1, 'y': 0}: 150, {'x': 0, 'y': 0}: 141, {'x': 4, 'y': 0}: 140, {'x': 3, 'y': 0}: 140, {'x': 8, 'y': 0}: 133, {'x': 15, 'y': 17}: 132, {'x': 10, 'y': 0}: 132, {'x': 14, 'y': 0}: 131, {'x': 9, 'y': 0}: 131, {'x': 13, 'y': 0}: 123, {'x': 11, 'y': 0}: 120, {'x': 12, 'y': 0}: 120, {'x': 2, 'y': 0}: 118, {'x': 6, 'y': 0}: 117, {'x': 5, 'y': 0}: 115, {'x': 7, 'y': 0}: 105]\n"
"[{'x': 8, 'y': 0}: 146, {'x': 6, 'y': 0}: 146, {'x': 12, 'y': 0}: 135, {'x': 13, 'y': 0}: 134, {'x': 1, 'y': 0}: 133, {'x': 9, 'y': 0}: 131, {'x': 10, 'y': 0}: 127, {'x': 2, 'y': 0}: 127, {'x': 5, 'y': 0}: 126, {'x': 3, 'y': 0}: 125, {'x': 4, 'y': 0}: 124, {'x': 15, 'y': 17}: 123, {'x': 11, 'y': 0}: 121, {'x': 14, 'y': 0}: 121, {'x': 0, 'y': 0}: 116, {'x': 7, 'y': 0}: 113]\n"
]
}
],
Expand All @@ -159,15 +164,15 @@
"metadata": {},
"source": [
"<div style=\"text-align:center;\">\n",
" <img src=\"https://docs.classiq.io/resources/quantum_operations_execute.gif\">\n",
" <img src=\"https://docs.classiq.io/resources/Design_operation_second.gif\">\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See that you receive $16$ possible values for `x` and `y`, and $y=0$ in all the pairs of values except when $x=15$, as defined!"
"You may notice that you receive $16$ possible values for `x` and `y`, where $y=0$ for all the values of $x$ except when $x=15$, as defined!"
]
},
{
Expand All @@ -189,12 +194,12 @@
"\n",
"* In the Python SDK, write the quantum operations just like any other Python function. The arguments of the quantum operation that are functions by themselves must be passed with the `lambda:` keyword. In the above example, the `stmt_block` argument of the `control` function is a function by itself (`inplace_xor`), hence it is prefixed with `lambda:`.\n",
"\n",
"* Other quantum operations are `power` (raising a unitary to some power), `invert` (applying the inverse of a unitary), and `within_apply` (applying two unitaries $U$ and $V$ as $UVU^\\dagger$). See a detailed [description of the quantum operators](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/power/).\n"
"* Other quantum operations are `power` (raising a unitary to some power), `invert` (applying the inverse of a unitary), `within_apply` (applying two unitaries $U$ and $V$ as $UVU^\\dagger$) and `bind` (rewiring qubit(s) referenced by a source variable to a destination variable). For more information, see the detailed description of [each quantum operator](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/power/).\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -218,7 +223,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.7"
},
"vscode": {
"interpreter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,40 @@
"machine_precision": 8,
"custom_hardware_settings": {
"basis_gates": [
"r",
"id",
"x",
"cz",
"z",
"tdg",
"u1",
"rz",
"ry",
"u2",
"cx",
"y",
"x",
"h",
"u2",
"u",
"id",
"sxdg",
"s",
"p",
"sdg",
"sx",
"h",
"cy",
"z",
"cx",
"t",
"sx",
"u1",
"rz",
"rx",
"cy"
"sdg",
"sxdg",
"ry",
"r",
"tdg"
],
"is_symmetric_connectivity": true
},
"debug_mode": true,
"synthesize_all_separately": false,
"output_format": ["qasm"],
"optimization_level": 3,
"output_format": [
"qasm"
],
"pretty_qasm": true,
"transpilation_option": "auto optimize",
"timeout_seconds": 300,
"random_seed": 158899354
"random_seed": 552212661
}
}
}
Loading
Loading