diff --git a/community/QClass_2024/Submissions/HW3/Mohammadreza_Khodajou_Masouleh_HW3_QClass2024.ipynb b/community/QClass_2024/Submissions/HW3/Mohammadreza_Khodajou_Masouleh_HW3_QClass2024.ipynb new file mode 100644 index 00000000..e4bbf492 --- /dev/null +++ b/community/QClass_2024/Submissions/HW3/Mohammadreza_Khodajou_Masouleh_HW3_QClass2024.ipynb @@ -0,0 +1,506 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "fc72d98f-8993-4bd4-a545-23d122f3df71", + "metadata": { + "id": "fc72d98f-8993-4bd4-a545-23d122f3df71" + }, + "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": { + "id": "56eda6d8-76c4-4862-b914-0c4598d67274" + }, + "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": "code", + "execution_count": 1, + "id": "XvJSENg4UQeh", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XvJSENg4UQeh", + "outputId": "e25318b0-f308-4e3d-a898-681c900d09b8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting classiq\n", + " Downloading classiq-0.42.0-py3-none-any.whl (401 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m401.5/401.5 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting ConfigArgParse<2.0.0,>=1.5.3 (from classiq)\n", + " Downloading ConfigArgParse-1.7-py3-none-any.whl (25 kB)\n", + "Collecting Pyomo<6.6,>=6.5 (from classiq)\n", + " Downloading Pyomo-6.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.7 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.7/10.7 MB\u001b[0m \u001b[31m26.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting black<25.0,>=24.0 (from classiq)\n", + " Downloading black-24.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.8/1.8 MB\u001b[0m \u001b[31m39.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting httpx<1,>=0.23.0 (from classiq)\n", + " Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: keyring<24.0.0,>=23.5.0 in /usr/lib/python3/dist-packages (from classiq) (23.5.0)\n", + "Requirement already satisfied: matplotlib<4.0.0,>=3.4.3 in /usr/local/lib/python3.10/dist-packages (from classiq) (3.7.1)\n", + "Collecting networkx<3.0.0,>=2.5.1 (from classiq)\n", + " Downloading networkx-2.8.8-py3-none-any.whl (2.0 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m40.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: numexpr<3.0.0,>=2.7.3 in /usr/local/lib/python3.10/dist-packages (from classiq) (2.10.0)\n", + "Requirement already satisfied: numpy<2.0.0,>=1.20.1 in /usr/local/lib/python3.10/dist-packages (from classiq) (1.25.2)\n", + "Collecting packaging<23.0,>=22.0 (from classiq)\n", + " Downloading packaging-22.0-py3-none-any.whl (42 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m42.6/42.6 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: pandas<3.0.0,>=1.4.0 in /usr/local/lib/python3.10/dist-packages (from classiq) (2.0.3)\n", + "Requirement already satisfied: plotly<6.0.0,>=5.7.0 in /usr/local/lib/python3.10/dist-packages (from classiq) (5.15.0)\n", + "Collecting pydantic<2.0.0,>=1.9.1 (from classiq)\n", + " Downloading pydantic-1.10.15-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m37.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: scipy<2.0.0,>=1.10.1 in /usr/local/lib/python3.10/dist-packages (from classiq) (1.11.4)\n", + "Collecting sympy<1.11.0,>=1.9.0 (from classiq)\n", + " Downloading sympy-1.10.1-py3-none-any.whl (6.4 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.4/6.4 MB\u001b[0m \u001b[31m18.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: tabulate<1,>=0.8.9 in /usr/local/lib/python3.10/dist-packages (from classiq) (0.9.0)\n", + "Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from black<25.0,>=24.0->classiq) (8.1.7)\n", + "Collecting mypy-extensions>=0.4.3 (from black<25.0,>=24.0->classiq)\n", + " Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)\n", + "Collecting pathspec>=0.9.0 (from black<25.0,>=24.0->classiq)\n", + " Downloading pathspec-0.12.1-py3-none-any.whl (31 kB)\n", + "Requirement already satisfied: platformdirs>=2 in /usr/local/lib/python3.10/dist-packages (from black<25.0,>=24.0->classiq) (4.2.2)\n", + "Requirement already satisfied: tomli>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from black<25.0,>=24.0->classiq) (2.0.1)\n", + "Requirement already satisfied: typing-extensions>=4.0.1 in /usr/local/lib/python3.10/dist-packages (from black<25.0,>=24.0->classiq) (4.12.1)\n", + "Requirement already satisfied: anyio in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->classiq) (3.7.1)\n", + "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->classiq) (2024.6.2)\n", + "Collecting httpcore==1.* (from httpx<1,>=0.23.0->classiq)\n", + " Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m3.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->classiq) (3.7)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->classiq) (1.3.1)\n", + "Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->classiq)\n", + " Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m3.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib<4.0.0,>=3.4.3->classiq) (1.2.1)\n", + "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib<4.0.0,>=3.4.3->classiq) (0.12.1)\n", + "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib<4.0.0,>=3.4.3->classiq) (4.53.0)\n", + "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib<4.0.0,>=3.4.3->classiq) (1.4.5)\n", + "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib<4.0.0,>=3.4.3->classiq) (9.4.0)\n", + "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib<4.0.0,>=3.4.3->classiq) (3.1.2)\n", + "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib<4.0.0,>=3.4.3->classiq) (2.8.2)\n", + "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0.0,>=1.4.0->classiq) (2023.4)\n", + "Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0.0,>=1.4.0->classiq) (2024.1)\n", + "Requirement already satisfied: tenacity>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from plotly<6.0.0,>=5.7.0->classiq) (8.3.0)\n", + "Collecting ply (from Pyomo<6.6,>=6.5->classiq)\n", + " Downloading ply-3.11-py2.py3-none-any.whl (49 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.6/49.6 kB\u001b[0m \u001b[31m1.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hRequirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy<1.11.0,>=1.9.0->classiq) (1.3.0)\n", + "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib<4.0.0,>=3.4.3->classiq) (1.16.0)\n", + "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio->httpx<1,>=0.23.0->classiq) (1.2.1)\n", + "Installing collected packages: ply, sympy, Pyomo, pydantic, pathspec, packaging, networkx, mypy-extensions, h11, ConfigArgParse, httpcore, black, httpx, classiq\n", + " Attempting uninstall: sympy\n", + " Found existing installation: sympy 1.12.1\n", + " Uninstalling sympy-1.12.1:\n", + " Successfully uninstalled sympy-1.12.1\n", + " Attempting uninstall: pydantic\n", + " Found existing installation: pydantic 2.7.3\n", + " Uninstalling pydantic-2.7.3:\n", + " Successfully uninstalled pydantic-2.7.3\n", + " Attempting uninstall: packaging\n", + " Found existing installation: packaging 24.0\n", + " Uninstalling packaging-24.0:\n", + " Successfully uninstalled packaging-24.0\n", + " Attempting uninstall: networkx\n", + " Found existing installation: networkx 3.3\n", + " Uninstalling networkx-3.3:\n", + " Successfully uninstalled networkx-3.3\n", + "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", + "torch 2.3.0+cu121 requires nvidia-cublas-cu12==12.1.3.1; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-cuda-cupti-cu12==12.1.105; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-cuda-nvrtc-cu12==12.1.105; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-cuda-runtime-cu12==12.1.105; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-cudnn-cu12==8.9.2.26; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-cufft-cu12==11.0.2.54; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-curand-cu12==10.3.2.106; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-cusolver-cu12==11.4.5.107; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-cusparse-cu12==12.1.0.106; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-nccl-cu12==2.20.5; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\n", + "torch 2.3.0+cu121 requires nvidia-nvtx-cu12==12.1.105; platform_system == \"Linux\" and platform_machine == \"x86_64\", which is not installed.\u001b[0m\u001b[31m\n", + "\u001b[0mSuccessfully installed ConfigArgParse-1.7 Pyomo-6.5.0 black-24.4.2 classiq-0.42.0 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 mypy-extensions-1.0.0 networkx-2.8.8 packaging-22.0 pathspec-0.12.1 ply-3.11 pydantic-1.10.15 sympy-1.10.1\n" + ] + } + ], + "source": [ + " !pip install -U classiq" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "HryEIcq_UWzG", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "HryEIcq_UWzG", + "outputId": "cba0edbf-73c1-46ea-d967-f020003e3a37" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your user code: ZCPV-PRDZ\n", + "If a browser doesn't automatically open, please visit this URL from any trusted device: https://auth.classiq.io/activate?user_code=ZCPV-PRDZ\n" + ] + } + ], + "source": [ + "import classiq\n", + "classiq.authenticate()" + ] + }, + { + "cell_type": "markdown", + "id": "d41e969d-f6a7-4ff7-9660-19ce6c97ba6b", + "metadata": { + "id": "d41e969d-f6a7-4ff7-9660-19ce6c97ba6b" + }, + "source": [ + "### Part 1" + ] + }, + { + "cell_type": "markdown", + "id": "f710d6f4-d40b-42d5-b524-c6acb8059fe2", + "metadata": { + "id": "f710d6f4-d40b-42d5-b524-c6acb8059fe2" + }, + "source": [ + "Given the following Hamiltonian:" + ] + }, + { + "cell_type": "markdown", + "id": "6ba8a6f1-3259-4492-a1ca-3abde7530cd4", + "metadata": { + "id": "6ba8a6f1-3259-4492-a1ca-3abde7530cd4" + }, + "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": { + "id": "736d275c-9a5a-4c08-b891-3078430dc6c1" + }, + "source": [ + "Complete the following code" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "19266c11-6acc-4edb-910f-2d0dfe80a6c8", + "metadata": { + "id": "19266c11-6acc-4edb-910f-2d0dfe80a6c8" + }, + "outputs": [], + "source": [ + "from typing import List\n", + "from classiq import *\n", + "\n", + "HAMILTONIAN = QConstant(\"HAMILTONIAN\",\n", + " List[PauliTerm],\n", + " [PauliTerm(pauli=[Pauli.I, Pauli.I], coefficient=-1.0523),\n", + " PauliTerm(pauli=[Pauli.I, Pauli.Z], coefficient=0.3979),\n", + " PauliTerm(pauli=[Pauli.Z, Pauli.I], coefficient=-0.3979),\n", + " PauliTerm(pauli=[Pauli.Z, Pauli.Z], coefficient=-0.0112),\n", + " PauliTerm(pauli=[Pauli.X, Pauli.X], coefficient=0.1809)]) #TODO: Complete Hamiltonian" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "0bb68899-2076-45c0-8868-131f38aa3b78", + "metadata": { + "id": "0bb68899-2076-45c0-8868-131f38aa3b78" + }, + "outputs": [], + "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", + " repeat (q.len, lambda i: U(angles[0], angles[1], angles[2], 0, q[i]))\n", + "\n", + "@cfunc\n", + "def cmain() -> None:\n", + " res = vqe(\n", + " HAMILTONIAN, # TODO: complete the missing argument\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)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "0563c1a8-7aec-4da9-9105-6b16c5c24382", + "metadata": { + "id": "0563c1a8-7aec-4da9-9105-6b16c5c24382" + }, + "outputs": [], + "source": [ + "execution = execute(qprog)\n", + "res = execution.result()\n", + "# execution.open_in_ide()\n", + "#print(res)\n", + "vqe_result = res[0].value #TODO: complete the line" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "XYbvABk_FwQJ", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XYbvABk_FwQJ", + "outputId": "16644151-f0a9-4995-e6b4-d66e543f118e", + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Optimal energy: -1.062161328125\n", + "Optimal parameters: {'angles_0': 0.13568678083490543, 'angles_1': -5.744986654083372, 'angles_2': 5.700228943470761}\n", + "Eigenstate: {'01': (0.07654655446197431+0j), '10': (0.09375+0j), '00': (0.9926487608917869+0j)}\n" + ] + } + ], + "source": [ + "print(f\"Optimal energy: {vqe_result.energy}\")\n", + "print(f\"Optimal parameters: {vqe_result.optimal_parameters}\")\n", + "print(f\"Eigenstate: {vqe_result.eigenstate}\")" + ] + }, + { + "cell_type": "markdown", + "id": "5df11dfc-3e3a-4191-bd47-d522ca3dcbfa", + "metadata": { + "id": "5df11dfc-3e3a-4191-bd47-d522ca3dcbfa" + }, + "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": "70pHpqecuObf", + "metadata": { + "id": "70pHpqecuObf" + }, + "source": [ + "*No! The value in the session was derived, according to different contributions, associated with the two-atom H_2 molecule, having taken into account various ways of communication through potential between them (atoms being effectively the qubits). This problem doesn't cover for those. Qubits are not talking to each other!*" + ] + }, + { + "cell_type": "markdown", + "id": "4f0e0bea-b12f-43ad-94e8-100fedf2b57f", + "metadata": { + "id": "4f0e0bea-b12f-43ad-94e8-100fedf2b57f" + }, + "source": [ + "### Part 2" + ] + }, + { + "cell_type": "markdown", + "id": "66882248-de08-4a6e-b33c-647f015f7d79", + "metadata": { + "id": "66882248-de08-4a6e-b33c-647f015f7d79" + }, + "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", + "execution_count": 38, + "id": "bb39be9e-4994-44e5-84d8-c99bc8b77145", + "metadata": { + "id": "bb39be9e-4994-44e5-84d8-c99bc8b77145" + }, + "outputs": [], + "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", + "\n", + " allocate(2, q)\n", + " repeat (q.len, lambda i: U(angles[0], angles[1], angles[2], 0, q[i]))\n", + " CX(q[0], q[1])\n", + "\n", + "\n", + "@cfunc\n", + "def cmain() -> None:\n", + " res = vqe(\n", + " HAMILTONIAN, # TODO: complete the missing argument\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)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "112a1590-283c-4f79-8035-72936561102d", + "metadata": { + "id": "112a1590-283c-4f79-8035-72936561102d" + }, + "outputs": [], + "source": [ + "execution = execute(qprog)\n", + "res = execution.result()\n", + "# execution.open_in_ide()\n", + "vqe_result = res[0].value #TODO: complete the line" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "06500e4c-a04b-4cfa-a84d-41f96a0c68eb", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "collapsed": true, + "id": "06500e4c-a04b-4cfa-a84d-41f96a0c68eb", + "jupyter": { + "outputs_hidden": true + }, + "outputId": "51f2aa86-143f-4e92-a3b3-3b900e42fd83" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Optimal energy: -1.8525221679687498\n", + "Optimal parameters: {'angles_0': 3.00289263258954, 'angles_1': -3.506224563747043, 'angles_2': -4.715418449895681}\n", + "Eigenstate: {'11': (0.06987712429686843+0j), '10': (0.05846339666834283+0j), '01': (0.995840960570512+0j)}\n" + ] + } + ], + "source": [ + "print(f\"Optimal energy: {vqe_result.energy}\")\n", + "print(f\"Optimal parameters: {vqe_result.optimal_parameters}\")\n", + "print(f\"Eigenstate: {vqe_result.eigenstate}\")" + ] + }, + { + "cell_type": "markdown", + "id": "30a635d7-2f15-4c94-a94b-f4270f17aed8", + "metadata": { + "id": "30a635d7-2f15-4c94-a94b-f4270f17aed8" + }, + "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?" + ] + }, + { + "cell_type": "markdown", + "id": "j4jmzMoL186t", + "metadata": { + "id": "j4jmzMoL186t" + }, + "source": [ + "*Evidently, the value of the optimal energy here bears a striking resemblence to the value of the problem discussed in the session. Almost surprisingly so, even though running the cell more than once introduces minimal differences. The entangling two-qubit gate CNOT is responsible for the conversation between the qubits, and that is the reason behind this similarity in values. Nevertheless, it's quite surprising admittedly to be able to simulate the impact of those sophisticated potential terms + the solid ansatz by a CNOT!*" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "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.10.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}