Skip to content
Open
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 @@ -83,7 +83,7 @@
"\n",
"show(qprog) # Visualize the quantum program for analysis\n",
"\n",
"# Execute and print the results:\n",
"# Execute and print the results\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" display(res.dataframe)"
Expand Down Expand Up @@ -168,12 +168,19 @@
"\n",
"@qfunc\n",
"def main(x: Output[QNum[3, SIGNED, 0]]):\n",
" allocate(x)\n",
" # TODO: Create GHZ state with QNum (gives 0 and -1)\n",
" # Hint: Is it any different from the previous main implementation?\n",
" pass\n",
"\n",
"\n",
"# TODO: Synthesize the model, show, execute and print results"
"qprog = synthesize(main)\n",
"show(qprog) # Visualize the quantum program for analysis\n",
"\n",
"# Execute and print the results\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" display(res.dataframe)"
Comment on lines +181 to +183
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" display(res.dataframe)"
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
"display(res.dataframe)"

It's recommended to have the with block contain the minimum that's required to be inside, and have the rest outside the block

],
"outputs": [],
"execution_count": null
Expand Down Expand Up @@ -236,13 +243,20 @@
" allocate(b)\n",
" # TODO: Put a and b in equal superposition\n",
"\n",
" # TODO:Assign the value of 3*a + b to c\n",
" # TODO: Assign the value of 3*a + b to c\n",
" allocate(1, c) # Placeholder - replace with actual assignment\n",
"\n",
" # Print out c's inferred size in qubits\n",
" print(f\"The size of c is {c.size}\")\n",
"\n",
"\n",
"# TODO: Synthesize the model, show, execute and print results"
"qprog = synthesize(main)\n",
"show(qprog) # Visualize the quantum program for analysis\n",
"\n",
"# Execute and print the results\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" display(res.dataframe)"
],
"outputs": [],
"execution_count": null
Expand Down Expand Up @@ -288,7 +302,7 @@
" # TODO: Put a and b in equal superposition\n",
"\n",
" # TODO: Assign the value of 3*a + b to c\n",
" # Hint: Is it any different from the previous main implementation?\n",
" allocate(1, c) # Placeholder - replace with actual assignment\n",
"\n",
" # Print out the numeric attributes of the inferred type\n",
" print(\"Numeric attributes of c:\")\n",
Expand All @@ -297,7 +311,13 @@
" )\n",
"\n",
"\n",
"# TODO: Synthesize the model, show, execute and print results"
"qprog = synthesize(main)\n",
"show(qprog) # Visualize the quantum program for analysis\n",
"\n",
"# Execute and print the results\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" display(res.dataframe)"
],
"outputs": [],
"execution_count": null
Expand Down Expand Up @@ -345,7 +365,13 @@
" # TODO : Flip the state of flag if x < 0.5\n",
"\n",
"\n",
"# TODO: Synthesize the model, show, execute and print results"
"qprog = synthesize(main)\n",
"show(qprog) # Visualize the quantum program for analysis\n",
"\n",
"# Execute and print the results\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" display(res.dataframe)"
],
"outputs": [],
"execution_count": null
Expand Down Expand Up @@ -419,10 +445,10 @@
" grover_operator(v)\n",
"\n",
"\n",
"# Synthesize the model, show, execute and print results\n",
"qprog = synthesize(main)\n",
"show(qprog)\n",
"show(qprog) # Visualize the quantum program for analysis\n",
"\n",
"# Execute and print the results\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" display(res.dataframe)"
Expand All @@ -445,19 +471,17 @@
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 6: Computing x² Using Phase Operations"
]
"source": "### Exercise 6: Phase Arithmetic"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Part A: Phase Encoding x²\n",
"#### Part A\n",
"\n",
"Compute the expression $x * y$ in the phase of their respective states. Use a coefficient to distribute all possible states over the $2\\pi$ phase rotation.\n",
"\n",
"Compute x² in the phase of its state. To actually view the phases of the states simulate the program using state-vector simulation.\n",
"Expand the quantum program visualization down to the gate-level implementation. How is the 'phase' statement synthesized?\n",
"Inspect the resulting phases of the different states. Do they match the phase expression?\n",
"To actually view the phases of the states, simulate the program using state-vector simulation. Expand the quantum program visualization down to the gate-level implementation. How is the *phase* statement synthesized? Inspect the resulting phases of the different states in the printout. Do they match the phase expression over `x` and `y`?\n",
"\n",
"Further reading: [Phase Statement](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/phase/)"
]
Expand All @@ -473,11 +497,12 @@
"\n",
"\n",
"@qfunc\n",
"def main(x: Output[QNum[3]]):\n",
"def main(x: Output[QNum[2]], y: Output[QNum[2]]):\n",
" allocate(x)\n",
" # TODO: put x in uniform superposition\n",
" allocate(y)\n",
" # TODO: put x and y in uniform superposition\n",
"\n",
" # TODO: Encode x² into the phase\n",
" # TODO: Encode x * y into the phase with a normalization coefficient to 2pi\n",
"\n",
"\n",
"# Synthesize the model and show\n",
Expand Down Expand Up @@ -517,7 +542,7 @@
"source": [
"#### Bonus: Fourier Arithmetic\n",
"\n",
"Create a quantum program that computes res = x² by encoding the result in the phase of a Fourier basis. Then transform the result back to the computational basis. Inspect the execution results to validate the correctness of your algorithm."
"Create a quantum program that computes $y = x^2$ by computing $x^2$ in the Fourier basis. Then transform the result back to the computational basis. Inspect the execution results to validate the correctness of your algorithm."
]
},
{
Expand All @@ -530,12 +555,12 @@
"\n",
"\n",
"@qfunc\n",
"def main(x: Output[QNum[3]], res: Output[QNum[4]]):\n",
"def main(x: Output[QNum[3]], y: Output[QNum[4]]):\n",
" allocate(x)\n",
" hadamard_transform(x)\n",
" allocate(res)\n",
" allocate(y)\n",
"\n",
" # TODO: Use within_apply with QFT and phase operations\n",
" # TODO: Use within_apply and function qft() to transform into and out of the Fourier basis\n",
"\n",
"\n",
"# Synthesize the model and show\n",
Expand Down Expand Up @@ -677,7 +702,7 @@
"\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" print(\"Ex.4A Results:\")\n",
" print(\"Ex.3A Results:\")\n",
" display(res.dataframe)"
],
"outputs": [],
Expand All @@ -694,7 +719,6 @@
"cell_type": "code",
"metadata": {},
"source": [
"# Part B: Fractioned quantum numbers (explicit syntax)\n",
"from classiq import *\n",
"\n",
"\n",
Expand Down Expand Up @@ -724,7 +748,7 @@
"\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" print(\"Ex.4B Results:\")\n",
" print(\"Ex.3B Results:\")\n",
" display(res.dataframe)"
],
"outputs": [],
Expand Down Expand Up @@ -760,7 +784,7 @@
"\n",
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" print(\"Ex.3 Results:\")\n",
" print(\"Ex.4 Results:\")\n",
" display(res.dataframe)"
],
"outputs": [],
Expand Down Expand Up @@ -790,7 +814,7 @@
"\n",
"@qfunc\n",
"def phase_oracle(v: MyProblemVars):\n",
" # Apply a phase flip if the state of v satisfies the equation (use field access in the form `v.a` etc.)\n",
" # Apply a phase flip if the state of v satisfies the equation (use field access in the form 'v.a' etc.)\n",
" control(3 * v.a + v.b + 2 * v.c == 9, lambda: phase(pi))\n",
"\n",
"\n",
Expand Down Expand Up @@ -831,16 +855,7 @@
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solution 6: Computing x² Using Phase Operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Part A: Phase Encoding x²"
]
"source": "### Solution 6: Phase Arithmetic"
},
{
"cell_type": "code",
Expand All @@ -853,26 +868,28 @@
"\n",
"\n",
"@qfunc\n",
"def main(x: Output[QNum[3]]):\n",
"def main(x: Output[QNum[2]], y: Output[QNum[2]]):\n",
" allocate(x)\n",
" # Put x in uniform superposition\n",
" allocate(y)\n",
" # Put x and y in uniform superposition\n",
" hadamard_transform(x)\n",
" hadamard_transform(y)\n",
"\n",
" # Encode x² into the phase\n",
" phase(x**2, 2 * np.pi / 8)\n",
" # Encode x * y into the phase with a normalization coefficient to 2pi\n",
" phase(x * y, np.pi / 8)\n",
"\n",
"\n",
"# Synthesize the model and show\n",
"qprog = synthesize(main)\n",
"# show(qprog)\n",
"show(qprog)\n",
"\n",
"# Specify execution preferences for state vector simulation\n",
"preferences = ExecutionPreferences(\n",
" num_shots=1,\n",
" backend_preferences=ClassiqBackendPreferences(backend_name=\"simulator_statevector\"),\n",
")\n",
"\n",
"# Execute and print results:\n",
"# Execute and print results\n",
"with ExecutionSession(qprog, preferences) as es:\n",
" res = es.sample()\n",
" print(\"Ex.6A Results:\")\n",
Expand All @@ -884,9 +901,7 @@
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Bonus: Fourier Arithmetic\n"
]
"source": "#### Bonus\n"
},
{
"cell_type": "code",
Expand All @@ -898,17 +913,16 @@
"\n",
"\n",
"@qfunc\n",
"def main(x: Output[QNum[3]], res: Output[QNum[4]]):\n",
"def main(x: Output[QNum[3]], y: Output[QNum[4]]):\n",
" allocate(x)\n",
" hadamard_transform(x)\n",
" allocate(res)\n",
" allocate(y)\n",
"\n",
" # Use within_apply with QFT and phase operations\n",
" # Use within_apply and function qft() to transform into and out of the Fourier basis\n",
" within_apply(\n",
" lambda: qft(res),\n",
" lambda: phase(\n",
" res * (x**2), 2 * np.pi / (2**res.size)\n",
" ), # evaluates res += x**2 in the Fourier basis\n",
" lambda: qft(y),\n",
" # Evaluates y += x**2 in the Fourier basis\n",
" lambda: phase(y * (x**2), 2 * np.pi / (2**y.size)),\n",
" )\n",
"\n",
"\n",
Expand All @@ -919,7 +933,6 @@
"with ExecutionSession(qprog) as es:\n",
" res = es.sample()\n",
" print(\"Ex.6B Results:\")\n",
" res.dataframe.sort_values(by=\"x\", inplace=True) # Sort by x for better readability\n",
" display(res.dataframe)"
],
"outputs": [],
Expand Down
Loading
Loading