diff --git a/classiqVQE (1).ipynb b/classiqVQE (1).ipynb new file mode 100644 index 00000000..902dc88f --- /dev/null +++ b/classiqVQE (1).ipynb @@ -0,0 +1,512 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Q0vwl2hHcZgZ", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f5146467-7b81-42d9-a237-e55a10b88c84" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Collecting classiq\n", + " Downloading classiq-0.41.2-py3-none-any.whl (398 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m398.1/398.1 kB\u001b[0m \u001b[31m4.2 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[31m59.2 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[31m44.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[31m7.4 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[31m51.6 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[31m48.9 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[31m67.7 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.11.0)\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.2.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[31m6.9 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[31m2.0 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.51.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[31m4.2 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\n", + " Uninstalling sympy-1.12:\n", + " Successfully uninstalled sympy-1.12\n", + " Attempting uninstall: pydantic\n", + " Found existing installation: pydantic 2.7.1\n", + " Uninstalling pydantic-2.7.1:\n", + " Successfully uninstalled pydantic-2.7.1\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.41.2 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 classiq" + ] + }, + { + "cell_type": "code", + "source": [ + "import classiq\n", + "classiq.authenticate()" + ], + "metadata": { + "id": "MJ6SGr3bcfZo", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "eb6856ce-25d1-4b9c-c138-c7e57dd2c7a2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Your user code: DZKT-ZPXN\n", + "If a browser doesn't automatically open, please visit this URL from any trusted device: https://auth.classiq.io/activate?user_code=DZKT-ZPXN\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "The Variational Quantum Eigensolver (VQE) is a quantum algorithm used to find the ground state energy of a given quantum Hamiltonian. Here's a brief explanation of how VQE works:\n", + "\n", + " VQE starts by initializing a parameterized quantum circuit known as the ansatz. The ansatz represents a trial wavefunction that is used to approximate the ground state of the Hamiltonian.\n", + "\n", + "The initialized ansatz is applied to a quantum state, and the energy expectation value of the Hamiltonian is measured. This measurement is performed using quantum operations and classical post-processing.\n", + "\n", + "The parameters of the ansatz are then adjusted iteratively to minimize the energy expectation value. This optimization process is typically performed using classical optimization algorithms such as COBYLA or SPSA.\n", + "\n", + "The optimization process continues until a convergence criterion is met, such as reaching a specified number of iterations or achieving a desired energy threshold.\n", + "\n", + "Once the optimization converges, the final parameters of the ansatz correspond to an approximation of the ground state of the Hamiltonian. The energy associated with these parameters is the estimated ground state energy.\n", + "\n", + " The estimated ground state energy obtained from VQE can be compared to the exact ground state energy (if known) or used for further analysis in quantum chemistry, materials science, or other applications." + ], + "metadata": { + "id": "FhwzTeYcLFN9" + } + }, + { + "cell_type": "markdown", + "source": [ + "we start by importing the neccessary libraries" + ], + "metadata": { + "id": "KZ6kNLebLTTM" + } + }, + { + "cell_type": "code", + "source": [ + "import classiq\n", + "\n", + "\n", + "\n", + "from classiq.execution import ExecutionPreferences, IBMBackendPreferences\n", + "from classiq.synthesis import set_execution_preferences\n", + "\n", + "\n", + "from classiq.synthesis import set_preferences\n", + "\n", + "\n", + "\n", + "from classiq import synthesize\n", + "\n", + "\n", + "from classiq import show\n", + "\n", + "\n", + "from classiq import execute\n", + "from classiq.execution import ExecutionDetails" + ], + "metadata": { + "id": "qt7O6o3qchxm" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "from classiq import (\n", + " RY,\n", + " CArray,\n", + " CInt,\n", + " CReal,\n", + " Input,\n", + " Output,\n", + " QArray,\n", + " QBit,\n", + " allocate,\n", + " bind,\n", + " create_model,\n", + " qfunc,\n", + " repeat,\n", + ")\n", + "from classiq.qmod.symbolic import pi" + ], + "metadata": { + "id": "i41VsU63q0aB" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "\n", + "import numpy as np\n", + "from classiq.applications.chemistry import PauliOperator\n", + "from classiq.execution import OptimizerType" + ], + "metadata": { + "id": "OrMSp_OZcqEH" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Here we define Hamiltonian and RY Gate" + ], + "metadata": { + "id": "tXgwRsltLgmc" + } + }, + { + "cell_type": "code", + "source": [ + "hamiltonian_to_minimise = PauliOperator(pauli_list=[(\"Z\",1), (\"X\", 0.1)])\n", + "\n", + "#print(hamiltonian_to_minimise.to_matrix())" + ], + "metadata": { + "id": "t_-Euc7kkUxa" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Define the ansatz using a single RY gate\n", + "def create_ansatz(theta):\n", + " circuit = QuantumCircuit()\n", + " circuit.add_ry(0, theta)\n", + " return circuit\n" + ], + "metadata": { + "id": "o1enIiVCs3W8" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Define function to evaluate expectation value\n", + "def evaluate_expectation(theta, hamiltonian):\n", + " circuit = create_ansatz(theta)\n", + " # Execute the circuit and measure the expectation value\n", + " # This part will depend on the specific functions provided by Classiq SDK\n", + " result = Classiq.execute(circuit, hamiltonian)\n", + " return result\n", + "\n", + "# Define the VQE algorithm\n", + "def vqe(hamiltonian):\n", + " initial_theta = 0.0\n", + " result = minimize(evaluate_expectation, initial_theta, args=(hamiltonian), method='COBYLA')\n", + " return result.x, result.fun\n" + ], + "metadata": { + "id": "BYee-9eys6ak" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "hamiltonian_to_minimise = PauliOperator(pauli_list=[(\"Z\",1), (\"X\", 0.1)])\n", + "\n", + "#print(hamiltonian_to_minimise.to_matrix())" + ], + "metadata": { + "id": "aOeNbBRKwq4o" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "The Hamiltonian\n", + "𝑍\n", + "+\n", + "0.1\n", + "𝑋\n", + "Z+0.1X is defined using the SummedFermionicOperator. This represents a combination of two terms: a\n", + "𝑍\n", + "Z term with coefficient 1.0 and an\n", + "𝑋\n", + "X term with coefficient 0.1.\n", + "\n", + " The Hamiltonian problem is defined using the HamiltonianProblem class, which encapsulates the Hamiltonian and information about the number of particles.\n", + "\n", + "The parameters for the ansatz, which is a crucial part of the VQE algorithm, are defined using the HEAParameters class. This includes the number of qubits, connectivity map (in this case, none), number of repetitions (layers), and the gates to be used (here, only RY gates).\n", + "\n", + " The chemistry model is constructed using the construct_chemistry_model function. This function takes the Hamiltonian problem, ansatz parameters, and execution parameters as input and constructs the corresponding quantum model.\n", + "\n", + " The execution preferences, including the backend preferences and the number of shots, are set using the set_execution_preferences function.\n", + "\n", + " The quantum program (qprog) is synthesized using the synthesize function.\n", + "\n", + " The synthesized quantum program is executed using the execute function, and the results are obtained. In VQE, the main result of interest is the energy of the system, which is extracted from the execution results." + ], + "metadata": { + "id": "11zFVC4eLyGk" + } + }, + { + "cell_type": "code", + "source": [ + "# Variational Quantum Eigensolver (VQE) using Classiq's SDK\n", + "\n", + "# Required Libraries\n", + "import numpy as np\n", + "from classiq import construct_chemistry_model, execute, show, synthesize, write_qmod\n", + "from classiq.applications.chemistry import (\n", + " ChemistryExecutionParameters,\n", + " FermionicOperator,\n", + " HamiltonianProblem,\n", + " HEAParameters,\n", + " SummedFermionicOperator,\n", + ")\n", + "from classiq.execution import OptimizerType\n", + "from classiq.synthesis import set_execution_preferences\n", + "from classiq.execution import ClassiqBackendPreferences, ClassiqSimulatorBackendNames, ExecutionPreferences\n", + "\n", + "# Define the Hamiltonian Z + 0.1X\n", + "# Z is the Pauli Z operator and X is the Pauli X operator\n", + "hamiltonian = SummedFermionicOperator(\n", + " op_list=[\n", + " (FermionicOperator(op_list=[(\"+\", 0), (\"-\", 0)]), 1.0), # Z term\n", + " (FermionicOperator(op_list=[(\"+\", 1), (\"-\", 1)]), 0.1) # 0.1X term\n", + " ]\n", + ")\n", + "\n", + "# Define the Hamiltonian problem\n", + "ham_problem = HamiltonianProblem(hamiltonian=hamiltonian, num_particles=[1, 1])\n", + "\n", + "# Define the Ansatz parameters\n", + "hwea_params = HEAParameters(\n", + " num_qubits=2, # Since we have a two-qubit Hamiltonian\n", + " connectivity_map=[], # No connectivity needed for a single qubit\n", + " reps=1, # Single layer of RY gate\n", + " one_qubit_gates=[\"ry\"], # Using RY gate for the ansatz\n", + " two_qubit_gates=[]\n", + ")\n", + "\n", + "# Construct the chemistry model\n", + "qmod = construct_chemistry_model(\n", + " chemistry_problem=ham_problem,\n", + " use_hartree_fock=False, # Not using Hartree-Fock state\n", + " ansatz_parameters=hwea_params,\n", + " execution_parameters=ChemistryExecutionParameters(\n", + " optimizer=OptimizerType.COBYLA,\n", + " max_iteration=100,\n", + " initial_point=None,\n", + " ),\n", + ")\n", + "\n", + "# Set execution preferences\n", + "backend_preferences = ClassiqBackendPreferences(\n", + " backend_name=ClassiqSimulatorBackendNames.SIMULATOR\n", + ")\n", + "\n", + "qmod = set_execution_preferences(\n", + " qmod,\n", + " execution_preferences=ExecutionPreferences(\n", + " num_shots=1000, backend_preferences=backend_preferences\n", + " ),\n", + ")\n", + "\n", + "# Write the quantum model to a file (optional)\n", + "write_qmod(qmod, name=\"simple_ry_ansatz\")\n", + "\n", + "# Synthesize the quantum program\n", + "qprog = synthesize(qmod)\n", + "\n", + "# Show the quantum circuit\n", + "show(qprog)\n", + "\n", + "# Execute the quantum program and get results\n", + "results = execute(qprog).result()\n", + "vqe_result = results[0].value\n", + "vqe_result.energy\n" + ], + "metadata": { + "id": "fmDO_00fGGJo", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "cd5a5a11-df67-4031-88a2-e8ec4eda530e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Opening: https://platform.classiq.io/circuit/7d5ee4e6-7b23-40f2-a758-aee563638fef?version=0.41.2\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4.163336342344337e-17" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ] + }, + { + "cell_type": "code", + "source": [ + "result = execute(qprog).result()\n", + "chemistry_result_dict = result[0].value" + ], + "metadata": { + "id": "wJ2T5G0dIRyJ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "results = execute(qprog).result()\n", + "chemistry_result = results[0].value\n", + "chemistry_result.energy" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "iWKxVQTEJPlB", + "outputId": "971a276d-0b8a-4677-d473-c576f7ebbc09" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4.163336342344337e-17" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + } + ] +} \ No newline at end of file