Skip to content

Commit

Permalink
uploaded hw3
Browse files Browse the repository at this point in the history
  • Loading branch information
BogachanA authored and Nadav138 committed Jun 13, 2024
1 parent eb24113 commit abe4dba
Showing 1 changed file with 355 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,355 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fc72d98f-8993-4bd4-a545-23d122f3df71",
"metadata": {},
"source": [
"# H₂ Molecule Homework Assignment\n",
"### Quantum Software Development Journey: From Theory to Application with Classiq - Part 3\n",
"\n",
"- Similarly to what we have done in class, in this exercise we will implement the VQE on H2 molecule.\n",
"- This time instead of using the built-in methods and functions (such as `Molecule` and `MoleculeProblem`) to difne and solve the problem, you will be provided with a two qubits Hamiltonian."
]
},
{
"cell_type": "markdown",
"id": "56eda6d8-76c4-4862-b914-0c4598d67274",
"metadata": {},
"source": [
"## Submission\n",
"- Submit the completed Jupyter notebook and report via GitHub. Ensure all files are correctly named and organized.\n",
"- Use the Typeform link provided in the submission folder to confirm your submission.\n",
"\n",
"## Additional Resources\n",
"- [Classiq Documentation](https://docs.classiq.io/latest/)\n",
"- The notebook from live session #3\n",
"\n",
"## Important Dates\n",
"- **Assignment Release:** 22.5.2024\n",
"- **Submission Deadline:** 3.6.2024 (7 A.M GMT+3)\n",
"\n",
"---\n",
"\n",
"Happy coding and good luck!"
]
},
{
"cell_type": "markdown",
"id": "d41e969d-f6a7-4ff7-9660-19ce6c97ba6b",
"metadata": {},
"source": [
"### Part 1"
]
},
{
"cell_type": "markdown",
"id": "f710d6f4-d40b-42d5-b524-c6acb8059fe2",
"metadata": {},
"source": [
"Given the following Hamiltonian:"
]
},
{
"cell_type": "markdown",
"id": "6ba8a6f1-3259-4492-a1ca-3abde7530cd4",
"metadata": {},
"source": [
"$$\n",
"\\hat{H} = -1.0523 \\cdot (I \\otimes I) + 0.3979 \\cdot (I \\otimes Z) - 0.3979 \\cdot (Z \\otimes I) - 0.0112 \\cdot (Z \\otimes Z) + 0.1809 \\cdot (X \\otimes X)\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "736d275c-9a5a-4c08-b891-3078430dc6c1",
"metadata": {},
"source": [
"Complete the following code"
]
},
{
"cell_type": "code",
"id": "19266c11-6acc-4edb-910f-2d0dfe80a6c8",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-01T16:33:52.134223Z",
"start_time": "2024-06-01T16:33:52.126343Z"
}
},
"source": [
"from typing import List\n",
"from classiq import *\n",
"\n",
"HAMILTONIAN = QConstant(\"HAMILTONIAN\", List[PauliTerm], [\n",
" PauliTerm([Pauli.I, Pauli.I], -1.0523),\n",
" PauliTerm([Pauli.I, Pauli.Z], 0.3979),\n",
" PauliTerm([Pauli.Z, Pauli.I], -0.3979),\n",
" PauliTerm([Pauli.Z, Pauli.Z], -0.0112),\n",
" PauliTerm([Pauli.X, Pauli.X], 0.1809),\n",
"]) "
],
"outputs": [],
"execution_count": 16
},
{
"cell_type": "code",
"id": "0bb68899-2076-45c0-8868-131f38aa3b78",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-01T16:33:59.604983Z",
"start_time": "2024-06-01T16:33:56.089312Z"
}
},
"source": [
"@qfunc\n",
"def main(q: Output[QArray[QBit]], angles: CArray[CReal, 3]) -> None:\n",
" # TODO: Create an ansatz which allows each qubit to have arbitrary rotation\n",
" allocate(2, q)\n",
" U(angles[0], angles[1], angles[2],0 ,q[0])\n",
" U(angles[0], angles[1], angles[2],0 ,q[1])\n",
"\n",
"\n",
"@cfunc\n",
"def cmain() -> None:\n",
" res = vqe(\n",
" HAMILTONIAN,\n",
" False,\n",
" [],\n",
" optimizer=Optimizer.COBYLA,\n",
" max_iteration=1000,\n",
" tolerance=0.001,\n",
" step_size=0,\n",
" skip_compute_variance=False,\n",
" alpha_cvar=1.0,\n",
" )\n",
" save({\"result\": res})\n",
"\n",
"qmod = create_model(main, classical_execution_function=cmain) #TODO: complete the line, use classical_execution_function\n",
"qprog = synthesize(qmod)\n",
"show(qprog)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/111f8e13-9abd-473e-9d4b-2eef47baaf6e?version=0.41.2\n"
]
}
],
"execution_count": 17
},
{
"cell_type": "code",
"id": "0563c1a8-7aec-4da9-9105-6b16c5c24382",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-01T16:31:20.654078Z",
"start_time": "2024-06-01T16:31:16.095403Z"
}
},
"source": [
"execution = execute(qprog)\n",
"res = execution.result()\n",
"# execution.open_in_ide()\n",
"vqe_result = res[0].value"
],
"outputs": [],
"execution_count": 11
},
{
"cell_type": "code",
"id": "de17cfc0-8e64-4493-b4c2-4a97fc9797a0",
"metadata": {
"scrolled": true,
"ExecuteTime": {
"end_time": "2024-06-01T16:31:20.660831Z",
"start_time": "2024-06-01T16:31:20.656030Z"
}
},
"source": [
"print(f\"Optimal energy: {vqe_result.energy}\")\n",
"print(f\"Optimal parameters: {vqe_result.optimal_parameters}\")\n",
"print(f\"Eigenstate: {vqe_result.eigenstate}\")"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimal energy: -1.0753044921875001\n",
"Optimal parameters: {'angles_0': 3.6207585292094877, 'angles_1': 4.926017593441268, 'angles_2': 3.315794817100976}\n",
"Eigenstate: {'01': (0.23070036031614688+0j), '00': (0.0625+0j), '10': (0.23280725665236468+0j), '11': (0.9426939455623973+0j)}\n"
]
}
],
"execution_count": 12
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"11\n",
"Optimal parameters: {'angles_0': -3.0292653800801213, 'angles_1': 0.13084292242999915, 'angles_2': -5.614248543217801}\n",
"Eigenstate: {'10': (0.07328774624724109+0j), '11': (0.07328774624724109+0j), '01': (0.9946144039274718+0j)}\n"
],
"id": "7ba7a9d3b1ff8dca"
},
{
"cell_type": "markdown",
"id": "5df11dfc-3e3a-4191-bd47-d522ca3dcbfa",
"metadata": {},
"source": [
"Does it similar to the `optimal energy` we calculated in class? \\\n",
"Does it similar to the `total energy` we calculated in class?"
]
},
{
"cell_type": "markdown",
"id": "4f0e0bea-b12f-43ad-94e8-100fedf2b57f",
"metadata": {},
"source": [
"### Part 2"
]
},
{
"cell_type": "markdown",
"id": "66882248-de08-4a6e-b33c-647f015f7d79",
"metadata": {},
"source": [
"**Now, we want to have a more interesting ansatz in our `main`.** \n",
"Add **one** line of code to the `main` function you created in Part 1 that creates **entanglement** between the two qubits. \n",
"Which gate should you use?"
]
},
{
"cell_type": "code",
"id": "bb39be9e-4994-44e5-84d8-c99bc8b77145",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-01T16:34:44.927782Z",
"start_time": "2024-06-01T16:34:41.614760Z"
}
},
"source": [
"@qfunc\n",
"def main(q: Output[QArray[QBit]], angles: CArray[CReal, 3]) -> None:\n",
" # TODO: Create an ansatz which allows each qubit to have arbitrary rotation\n",
" allocate(2, q)\n",
" U(angles[0], angles[1], angles[2],0 ,q[0])\n",
" U(angles[0], angles[1], angles[2],0 ,q[1])\n",
" CX(q[0], q[1])\n",
"\n",
"@cfunc\n",
"def cmain() -> None:\n",
" res = vqe(\n",
" HAMILTONIAN,\n",
" False,\n",
" [],\n",
" optimizer=Optimizer.COBYLA,\n",
" max_iteration=1000,\n",
" tolerance=0.001,\n",
" step_size=0,\n",
" skip_compute_variance=False,\n",
" alpha_cvar=1.0,\n",
" )\n",
" save({\"result\": res})\n",
"\n",
"qmod = create_model(main, classical_execution_function=cmain) #TODO: complete the line, use classical_execution_function\n",
"qprog = synthesize(qmod)\n",
"show(qprog)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/8e1369c2-c531-4ecb-9311-2ca674455f30?version=0.41.2\n"
]
}
],
"execution_count": 18
},
{
"cell_type": "code",
"id": "112a1590-283c-4f79-8035-72936561102d",
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-01T16:35:14.125876Z",
"start_time": "2024-06-01T16:35:09.772261Z"
}
},
"source": [
"execution = execute(qprog)\n",
"res = execution.result()\n",
"# execution.open_in_ide()\n",
"vqe_result = res[0].value #TODO: complete the line"
],
"outputs": [],
"execution_count": 19
},
{
"cell_type": "code",
"id": "06500e4c-a04b-4cfa-a84d-41f96a0c68eb",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
},
"ExecuteTime": {
"end_time": "2024-06-01T16:35:15.429400Z",
"start_time": "2024-06-01T16:35:15.421186Z"
}
},
"source": [
"print(f\"Optimal energy: {vqe_result.energy}\")\n",
"print(f\"Optimal parameters: {vqe_result.optimal_parameters}\")\n",
"print(f\"Eigenstate: {vqe_result.eigenstate}\")"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimal energy: -1.83920859375\n",
"Optimal parameters: {'angles_0': -2.984295902257786, 'angles_1': -6.986408481569447, 'angles_2': -1.3331417701993054}\n",
"Eigenstate: {'11': (0.06987712429686843+0j), '10': (0.07967217989988726+0j), '01': (0.9943689110435825+0j)}\n"
]
}
],
"execution_count": 20
},
{
"cell_type": "markdown",
"id": "30a635d7-2f15-4c94-a94b-f4270f17aed8",
"metadata": {},
"source": [
"Does it similar to the `optimal energy` we calculated in class? \\\n",
"Does it similar to the `total energy` we calculated in class? \\\n",
"What can we learn about the provided form this result Hamiltonian?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit abe4dba

Please sign in to comment.