{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quantum Circuit with Qiskit Runtime\n", "\n", "This notebook demonstrates how to set up a simple quantum circuit using Qiskit Runtime and measure performance using an Active Graph Network (AGN). The notebook is based on our discussion and includes dependency installation, circuit definition, benchmarking, and troubleshooting steps." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Install Qiskit and Qiskit IBM Runtime\n", "\n", "First, install the necessary dependencies for Qiskit and Qiskit IBM Runtime. Make sure you restart the kernel after installing these packages to avoid compatibility issues." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: qiskit in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (1.2.4)\n", "Requirement already satisfied: qiskit-ibm-runtime in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (0.32.0)\n", "Requirement already satisfied: rustworkx>=0.15.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.15.1)\n", "Requirement already satisfied: numpy<3,>=1.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.23.5)\n", "Requirement already satisfied: scipy>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: sympy>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: dill>=0.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.3.9)\n", "Requirement already satisfied: python-dateutil>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (2.9.0)\n", "Requirement already satisfied: stevedore>=3.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (5.3.0)\n", "Requirement already satisfied: typing-extensions in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (4.12.2)\n", "Requirement already satisfied: symengine<0.14,>=0.11 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.13.0)\n", "Requirement already satisfied: requests>=2.19 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.32.3)\n", "Requirement already satisfied: requests-ntlm>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (1.1.0)\n", "Requirement already satisfied: urllib3>=1.21.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.2.3)\n", "Requirement already satisfied: websocket-client>=1.5.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (1.8.0)\n", "Requirement already satisfied: ibm-platform-services>=0.22.6 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (0.59.0)\n", "Requirement already satisfied: pydantic>=2.5.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.8.2)\n", "Requirement already satisfied: ibm-cloud-sdk-core<4.0.0,>=3.22.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (3.22.0)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.20.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (2.20.1)\n", "Requirement already satisfied: six>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from python-dateutil>=2.8.0->qiskit) (1.16.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (2024.8.30)\n", "Requirement already satisfied: ntlm-auth>=1.0.2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (1.5.0)\n", "Requirement already satisfied: cryptography>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (43.0.3)\n", "Requirement already satisfied: pbr>=2.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from stevedore>=3.0.0->qiskit) (6.1.0)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from sympy>=1.3->qiskit) (1.3.0)\n", "Requirement already satisfied: cffi>=1.12 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (1.17.1)\n", "Requirement already satisfied: PyJWT<3.0.0,>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-cloud-sdk-core<4.0.0,>=3.22.0->ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (2.9.0)\n", "Requirement already satisfied: pycparser in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (2.22)\n" ] } ], "source": [ "!pip install -U qiskit qiskit-ibm-runtime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Initialize Qiskit Runtime Service\n", "\n", "After installing the dependencies, import Qiskit Runtime Service and initialize it. This service allows you to access IBM Quantum backends and execute circuits with improved efficiency." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "Qiskit is installed in an invalid environment that has both Qiskit >=1.0 and an earlier version. You should create a new virtual environment, and ensure that you do not mix dependencies between Qiskit <1.0 and >=1.0. Any packages that depend on 'qiskit-terra' are not compatible with Qiskit 1.0 and will need to be updated. Qiskit unfortunately cannot enforce this requirement during environment resolution. See https://qisk.it/packaging-1-0 for more detail.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit_ibm_runtime\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m QiskitRuntimeService\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m 4\u001b[0m os\u001b[38;5;241m.\u001b[39menviron[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mIBMQ_API_KEY\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m86a3bf838145fe6863cd15adf39b4678715f07c28e66cf2e1e3c7a9f9020a4f4215b71b9c664db90690e99ed2760c6e973c993257ace55c7ae6d5dfacd2d13fc\u001b[39m\u001b[38;5;124m'\u001b[39m\n", "File \u001b[0;32m~/Git/AIMO/.conda/lib/python3.10/site-packages/qiskit_ibm_runtime/__init__.py:204\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mlogging\u001b[39;00m\n\u001b[1;32m 202\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mwarnings\u001b[39;00m\n\u001b[0;32m--> 204\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mqiskit_runtime_service\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m QiskitRuntimeService\n\u001b[1;32m 205\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mibm_backend\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m IBMBackend\n\u001b[1;32m 206\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mruntime_job\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m RuntimeJob\n", "File \u001b[0;32m~/Git/AIMO/.conda/lib/python3.10/site-packages/qiskit_ibm_runtime/qiskit_runtime_service.py:23\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mcollections\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m OrderedDict\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtyping\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Dict, Callable, Optional, Union, List, Any, Type, Sequence\n\u001b[0;32m---> 23\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mproviders\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbackend\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BackendV2 \u001b[38;5;28;01mas\u001b[39;00m Backend\n\u001b[1;32m 24\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mproviders\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexceptions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m QiskitBackendNotFoundError\n\u001b[1;32m 25\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mproviders\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mproviderutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m filter_backends\n", "File \u001b[0;32m~/Git/AIMO/.conda/lib/python3.10/site-packages/qiskit/__init__.py:38\u001b[0m\n\u001b[1;32m 36\u001b[0m _suppress_error \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39menviron\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQISKIT_SUPPRESS_1_0_IMPORT_ERROR\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mint\u001b[39m(_major) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _suppress_error:\n\u001b[0;32m---> 38\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\n\u001b[1;32m 39\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQiskit is installed in an invalid environment that has both Qiskit >=1.0\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m and an earlier version.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 41\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m You should create a new virtual environment, and ensure that you do not mix\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 42\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m dependencies between Qiskit <1.0 and >=1.0.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Any packages that depend on \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mqiskit-terra\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m are not compatible with Qiskit 1.0 and\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m will need to be updated.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 45\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Qiskit unfortunately cannot enforce this requirement during environment resolution.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 46\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m See https://qisk.it/packaging-1-0 for more detail.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 47\u001b[0m )\n\u001b[1;32m 49\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_accelerate\u001b[39;00m\n\u001b[1;32m 50\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_numpy_compat\u001b[39;00m\n", "\u001b[0;31mImportError\u001b[0m: Qiskit is installed in an invalid environment that has both Qiskit >=1.0 and an earlier version. You should create a new virtual environment, and ensure that you do not mix dependencies between Qiskit <1.0 and >=1.0. Any packages that depend on 'qiskit-terra' are not compatible with Qiskit 1.0 and will need to be updated. Qiskit unfortunately cannot enforce this requirement during environment resolution. See https://qisk.it/packaging-1-0 for more detail." ] } ], "source": [ "from qiskit_ibm_runtime import QiskitRuntimeService\n", "\n", "import os\n", "os.environ['IBMQ_API_KEY'] = '86a3bf838145fe6863cd15adf39b4678715f07c28e66cf2e1e3c7a9f9020a4f4215b71b9c664db90690e99ed2760c6e973c993257ace55c7ae6d5dfacd2d13fc'" ] }, { "cell_type": "code", "execution_count": 4, "id": "10d152b8", "metadata": {}, "outputs": [], "source": [ "import os\n", "from qiskit_ibm_runtime import QiskitRuntimeService\n", " \n", "# Retrieve API key from environment variable\n", "api_key = os.getenv('IBMQ_API_KEY')\n", "\n", "if not api_key:\n", " raise EnvironmentError(\"IBMQ_API_KEY environment variable not set.\")\n", " \n", "# Initialize the Qiskit Runtime service using the API key\n", "service = QiskitRuntimeService(channel=\"ibm_quantum\", token=api_key)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Define the Quantum Circuit\n", "\n", "This section defines a quantum circuit based on our previous discussions. This circuit uses a few gates on four qubits. Adjust the `theta` values as needed to test different configurations." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from qiskit import QuantumCircuit\n", "\n", "# Define a simple quantum circuit\n", "def qiskit_circuit(theta):\n", " qc = QuantumCircuit(4)\n", " qc.rx(theta[0], 0)\n", " qc.ry(theta[1], 1)\n", " qc.cx(0, 1)\n", " qc.ry(theta[2], 2)\n", " qc.cx(1, 2)\n", " qc.rx(theta[3], 3)\n", " qc.measure_all()\n", " return qc\n", "\n", "# Define theta values and create the circuit\n", "theta_values = [0.3, 0.7, 1.2, 0.5]\n", "qc = qiskit_circuit(theta_values)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Run the Circuit on IBM Quantum Backend with Qiskit Runtime\n", "\n", "Use the `Sampler` primitive to run the circuit on a suitable IBM Quantum backend. The `Sampler` manages the execution efficiently, and results are returned directly." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Available backends: [, , ]\n" ] }, { "ename": "AttributeError", "evalue": "__enter__", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[12], line 34\u001b[0m\n\u001b[1;32m 31\u001b[0m qc \u001b[38;5;241m=\u001b[39m qiskit_circuit(theta_values)\n\u001b[1;32m 33\u001b[0m \u001b[38;5;66;03m# Run the sampler using Qiskit Runtime\u001b[39;00m\n\u001b[0;32m---> 34\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m service \u001b[38;5;28;01mas\u001b[39;00m runtime_service:\n\u001b[1;32m 35\u001b[0m \u001b[38;5;66;03m# Initialize the sampler\u001b[39;00m\n\u001b[1;32m 36\u001b[0m sampler \u001b[38;5;241m=\u001b[39m Sampler()\n\u001b[1;32m 38\u001b[0m \u001b[38;5;66;03m# Execute the circuit using the sampler\u001b[39;00m\n", "\u001b[0;31mAttributeError\u001b[0m: __enter__" ] } ], "source": [ "from qiskit import QuantumCircuit\n", "from qiskit_ibm_runtime import QiskitRuntimeService, Sampler\n", "import os\n", "\n", "# Retrieve API key from environment variable\n", "api_key = os.getenv('IBMQ_API_KEY')\n", "if not api_key:\n", " raise EnvironmentError(\"IBMQ_API_KEY environment variable not set.\")\n", "\n", "# Initialize the Qiskit Runtime service using the API key\n", "service = QiskitRuntimeService(channel=\"ibm_quantum\", token=api_key)\n", "\n", "# List available backends to find a suitable one\n", "available_backends = service.backends()\n", "print(\"Available backends:\", available_backends)\n", "\n", "# Define a simple quantum circuit\n", "def qiskit_circuit(theta):\n", " qc = QuantumCircuit(4)\n", " qc.rx(theta[0], 0)\n", " qc.ry(theta[1], 1)\n", " qc.cx(0, 1)\n", " qc.ry(theta[2], 2)\n", " qc.cx(1, 2)\n", " qc.rx(theta[3], 3)\n", " qc.measure_all()\n", " return qc\n", "\n", "# Define theta values and create the circuit\n", "theta_values = [0.3, 0.7, 1.2, 0.5]\n", "qc = qiskit_circuit(theta_values)\n", "\n", "# Run the sampler using Qiskit Runtime\n", "with service as runtime_service:\n", " # Initialize the sampler\n", " sampler = Sampler()\n", "\n", " # Execute the circuit using the sampler\n", " job = sampler.run(circuits=qc, shots=1024)\n", " result = job.result()\n", " counts = result.get_counts(qc)\n", " print(\"Results using Qiskit Runtime Sampler:\", counts)" ] }, { "cell_type": "code", "execution_count": 18, "id": "b2140276", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: qiskit in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (1.2.4)\n", "Requirement already satisfied: qiskit-ibm-runtime in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (0.32.0)\n", "Requirement already satisfied: matplotlib in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (3.9.2)\n", "Collecting python-dotenv\n", " Downloading python_dotenv-1.0.1-py3-none-any.whl.metadata (23 kB)\n", "Requirement already satisfied: rustworkx>=0.15.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.15.1)\n", "Requirement already satisfied: numpy<3,>=1.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.23.5)\n", "Requirement already satisfied: scipy>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: sympy>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: dill>=0.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.3.9)\n", "Requirement already satisfied: python-dateutil>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (2.9.0)\n", "Requirement already satisfied: stevedore>=3.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (5.3.0)\n", "Requirement already satisfied: typing-extensions in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (4.12.2)\n", "Requirement already satisfied: symengine<0.14,>=0.11 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.13.0)\n", "Requirement already satisfied: requests>=2.19 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.32.3)\n", "Requirement already satisfied: requests-ntlm>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (1.1.0)\n", "Requirement already satisfied: urllib3>=1.21.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.2.3)\n", "Requirement already satisfied: websocket-client>=1.5.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (1.8.0)\n", "Requirement already satisfied: ibm-platform-services>=0.22.6 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (0.59.0)\n", "Requirement already satisfied: pydantic>=2.5.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.8.2)\n", "Requirement already satisfied: contourpy>=1.0.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from matplotlib) (1.3.0)\n", "Requirement already satisfied: cycler>=0.10 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from matplotlib) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from matplotlib) (4.54.1)\n", "Requirement already satisfied: kiwisolver>=1.3.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from matplotlib) (1.4.7)\n", "Requirement already satisfied: packaging>=20.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from matplotlib) (24.1)\n", "Requirement already satisfied: pillow>=8 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from matplotlib) (11.0.0)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from matplotlib) (3.2.0)\n", "Requirement already satisfied: ibm-cloud-sdk-core<4.0.0,>=3.22.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (3.22.0)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.20.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (2.20.1)\n", "Requirement already satisfied: six>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from python-dateutil>=2.8.0->qiskit) (1.16.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (2024.8.30)\n", "Requirement already satisfied: ntlm-auth>=1.0.2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (1.5.0)\n", "Requirement already satisfied: cryptography>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (43.0.3)\n", "Requirement already satisfied: pbr>=2.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from stevedore>=3.0.0->qiskit) (6.1.0)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from sympy>=1.3->qiskit) (1.3.0)\n", "Requirement already satisfied: cffi>=1.12 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (1.17.1)\n", "Requirement already satisfied: PyJWT<3.0.0,>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-cloud-sdk-core<4.0.0,>=3.22.0->ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (2.9.0)\n", "Requirement already satisfied: pycparser in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (2.22)\n", "Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n", "Installing collected packages: python-dotenv\n", "Successfully installed python-dotenv-1.0.1\n" ] } ], "source": [ "!pip install qiskit qiskit-ibm-runtime matplotlib python-dotenv" ] }, { "cell_type": "code", "execution_count": 4, "id": "d5b83a7a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found existing installation: qiskit 1.2.4\n", "Uninstalling qiskit-1.2.4:\n", " Successfully uninstalled qiskit-1.2.4\n", "Found existing installation: qiskit-ibm-runtime 0.32.0\n", "Uninstalling qiskit-ibm-runtime-0.32.0:\n", " Successfully uninstalled qiskit-ibm-runtime-0.32.0\n", "Found existing installation: qiskit-terra 0.46.3\n", "Uninstalling qiskit-terra-0.46.3:\n", " Successfully uninstalled qiskit-terra-0.46.3\n", "\u001b[33mWARNING: Skipping qiskit-aer as it is not installed.\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Skipping qiskit-ignis as it is not installed.\u001b[0m\u001b[33m\n", "\u001b[0mFound existing installation: qiskit-ibmq-provider 0.20.2\n", "Uninstalling qiskit-ibmq-provider-0.20.2:\n", " Successfully uninstalled qiskit-ibmq-provider-0.20.2\n", "Collecting qiskit\n", " Using cached qiskit-1.2.4-cp38-abi3-macosx_11_0_arm64.whl.metadata (12 kB)\n", "Requirement already satisfied: rustworkx>=0.15.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.15.1)\n", "Requirement already satisfied: numpy<3,>=1.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.23.5)\n", "Requirement already satisfied: scipy>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: sympy>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: dill>=0.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.3.9)\n", "Requirement already satisfied: python-dateutil>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (2.9.0)\n", "Requirement already satisfied: stevedore>=3.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (5.3.0)\n", "Requirement already satisfied: typing-extensions in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (4.12.2)\n", "Requirement already satisfied: symengine<0.14,>=0.11 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.13.0)\n", "Requirement already satisfied: six>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from python-dateutil>=2.8.0->qiskit) (1.16.0)\n", "Requirement already satisfied: pbr>=2.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from stevedore>=3.0.0->qiskit) (6.1.0)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from sympy>=1.3->qiskit) (1.3.0)\n", "Using cached qiskit-1.2.4-cp38-abi3-macosx_11_0_arm64.whl (4.5 MB)\n", "Installing collected packages: qiskit\n", "Successfully installed qiskit-1.2.4\n" ] } ], "source": [ "!pip uninstall qiskit qiskit-ibm-runtime qiskit-terra qiskit-aer qiskit-ignis qiskit-ibmq-provider -y\n", "!pip install qiskit" ] }, { "cell_type": "code", "execution_count": 3, "id": "2dfabb36", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: qiskit_ibm_runtime in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (0.32.0)\n", "Requirement already satisfied: requests>=2.19 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (2.32.3)\n", "Requirement already satisfied: requests-ntlm>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (1.1.0)\n", "Requirement already satisfied: numpy>=1.13 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (1.23.5)\n", "Requirement already satisfied: urllib3>=1.21.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (2.2.3)\n", "Requirement already satisfied: python-dateutil>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (2.9.0)\n", "Requirement already satisfied: websocket-client>=1.5.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (1.8.0)\n", "Requirement already satisfied: ibm-platform-services>=0.22.6 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (0.59.0)\n", "Requirement already satisfied: pydantic>=2.5.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (2.8.2)\n", "Requirement already satisfied: qiskit>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit_ibm_runtime) (1.2.4)\n", "Requirement already satisfied: ibm-cloud-sdk-core<4.0.0,>=3.22.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-platform-services>=0.22.6->qiskit_ibm_runtime) (3.22.0)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit_ibm_runtime) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.20.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit_ibm_runtime) (2.20.1)\n", "Requirement already satisfied: typing-extensions>=4.6.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit_ibm_runtime) (4.12.2)\n", "Requirement already satisfied: six>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from python-dateutil>=2.8.0->qiskit_ibm_runtime) (1.16.0)\n", "Requirement already satisfied: rustworkx>=0.15.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit>=1.1.0->qiskit_ibm_runtime) (0.15.1)\n", "Requirement already satisfied: scipy>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit>=1.1.0->qiskit_ibm_runtime) (1.13.1)\n", "Requirement already satisfied: sympy>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit>=1.1.0->qiskit_ibm_runtime) (1.13.1)\n", "Requirement already satisfied: dill>=0.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit>=1.1.0->qiskit_ibm_runtime) (0.3.9)\n", "Requirement already satisfied: stevedore>=3.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit>=1.1.0->qiskit_ibm_runtime) (5.3.0)\n", "Requirement already satisfied: symengine<0.14,>=0.11 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit>=1.1.0->qiskit_ibm_runtime) (0.13.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit_ibm_runtime) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit_ibm_runtime) (3.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit_ibm_runtime) (2024.8.30)\n", "Requirement already satisfied: ntlm-auth>=1.0.2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit_ibm_runtime) (1.5.0)\n", "Requirement already satisfied: cryptography>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit_ibm_runtime) (43.0.3)\n", "Requirement already satisfied: cffi>=1.12 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->qiskit_ibm_runtime) (1.17.1)\n", "Requirement already satisfied: PyJWT<3.0.0,>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-cloud-sdk-core<4.0.0,>=3.22.0->ibm-platform-services>=0.22.6->qiskit_ibm_runtime) (2.9.0)\n", "Requirement already satisfied: pbr>=2.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from stevedore>=3.0.0->qiskit>=1.1.0->qiskit_ibm_runtime) (6.1.0)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from sympy>=1.3->qiskit>=1.1.0->qiskit_ibm_runtime) (1.3.0)\n", "Requirement already satisfied: pycparser in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=1.1.0->qiskit_ibm_runtime) (2.22)\n" ] } ], "source": [ "\n", "!pip install qiskit_ibm_runtime" ] }, { "cell_type": "code", "execution_count": 20, "id": "e201d494", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.2.4\n" ] } ], "source": [ "import qiskit\n", "print(qiskit.__version__)" ] }, { "cell_type": "code", "execution_count": 9, "id": "a01acdd4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: qiskit in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (1.2.4)\n", "Collecting qiskit-ibm-runtime\n", " Using cached qiskit_ibm_runtime-0.32.0-py3-none-any.whl.metadata (19 kB)\n", "Requirement already satisfied: rustworkx>=0.15.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.15.1)\n", "Requirement already satisfied: numpy<3,>=1.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.23.5)\n", "Requirement already satisfied: scipy>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: sympy>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (1.13.1)\n", "Requirement already satisfied: dill>=0.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.3.9)\n", "Requirement already satisfied: python-dateutil>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (2.9.0)\n", "Requirement already satisfied: stevedore>=3.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (5.3.0)\n", "Requirement already satisfied: typing-extensions in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (4.12.2)\n", "Requirement already satisfied: symengine<0.14,>=0.11 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit) (0.13.0)\n", "Requirement already satisfied: requests>=2.19 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.32.3)\n", "Requirement already satisfied: requests-ntlm>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (1.1.0)\n", "Requirement already satisfied: urllib3>=1.21.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.2.3)\n", "Requirement already satisfied: websocket-client>=1.5.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (1.8.0)\n", "Requirement already satisfied: ibm-platform-services>=0.22.6 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (0.59.0)\n", "Requirement already satisfied: pydantic>=2.5.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from qiskit-ibm-runtime) (2.8.2)\n", "Requirement already satisfied: ibm-cloud-sdk-core<4.0.0,>=3.22.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (3.22.0)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.20.1 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (2.20.1)\n", "Requirement already satisfied: six>=1.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from python-dateutil>=2.8.0->qiskit) (1.16.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests>=2.19->qiskit-ibm-runtime) (2024.8.30)\n", "Requirement already satisfied: ntlm-auth>=1.0.2 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (1.5.0)\n", "Requirement already satisfied: cryptography>=1.3 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (43.0.3)\n", "Requirement already satisfied: pbr>=2.0.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from stevedore>=3.0.0->qiskit) (6.1.0)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from sympy>=1.3->qiskit) (1.3.0)\n", "Requirement already satisfied: cffi>=1.12 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (1.17.1)\n", "Requirement already satisfied: PyJWT<3.0.0,>=2.8.0 in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from ibm-cloud-sdk-core<4.0.0,>=3.22.0->ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (2.9.0)\n", "Requirement already satisfied: pycparser in /Users/callum/Git/AIMO/.conda/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (2.22)\n", "Using cached qiskit_ibm_runtime-0.32.0-py3-none-any.whl (3.0 MB)\n", "Installing collected packages: qiskit-ibm-runtime\n", "Successfully installed qiskit-ibm-runtime-0.32.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install -U qiskit qiskit-ibm-runtime" ] }, { "cell_type": "code", "execution_count": 5, "id": "588c9add", "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "Qiskit is installed in an invalid environment that has both Qiskit >=1.0 and an earlier version. You should create a new virtual environment, and ensure that you do not mix dependencies between Qiskit <1.0 and >=1.0. Any packages that depend on 'qiskit-terra' are not compatible with Qiskit 1.0 and will need to be updated. Qiskit unfortunately cannot enforce this requirement during environment resolution. See https://qisk.it/packaging-1-0 for more detail.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[5], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mlogging\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtyping\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m List\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m QuantumCircuit\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit_ibm_runtime\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m QiskitRuntimeService, Sampler, Options\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mproviders\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mibmq\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m IBMQBackend\n", "File \u001b[0;32m~/Git/AIMO/.conda/lib/python3.10/site-packages/qiskit/__init__.py:38\u001b[0m\n\u001b[1;32m 36\u001b[0m _suppress_error \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39menviron\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQISKIT_SUPPRESS_1_0_IMPORT_ERROR\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mint\u001b[39m(_major) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _suppress_error:\n\u001b[0;32m---> 38\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\n\u001b[1;32m 39\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQiskit is installed in an invalid environment that has both Qiskit >=1.0\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m and an earlier version.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 41\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m You should create a new virtual environment, and ensure that you do not mix\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 42\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m dependencies between Qiskit <1.0 and >=1.0.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Any packages that depend on \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mqiskit-terra\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m are not compatible with Qiskit 1.0 and\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m will need to be updated.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 45\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Qiskit unfortunately cannot enforce this requirement during environment resolution.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 46\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m See https://qisk.it/packaging-1-0 for more detail.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 47\u001b[0m )\n\u001b[1;32m 49\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_accelerate\u001b[39;00m\n\u001b[1;32m 50\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mqiskit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_numpy_compat\u001b[39;00m\n", "\u001b[0;31mImportError\u001b[0m: Qiskit is installed in an invalid environment that has both Qiskit >=1.0 and an earlier version. You should create a new virtual environment, and ensure that you do not mix dependencies between Qiskit <1.0 and >=1.0. Any packages that depend on 'qiskit-terra' are not compatible with Qiskit 1.0 and will need to be updated. Qiskit unfortunately cannot enforce this requirement during environment resolution. See https://qisk.it/packaging-1-0 for more detail." ] } ], "source": [ "import os\n", "import sys\n", "import logging\n", "from typing import List\n", "\n", "from qiskit import QuantumCircuit\n", "from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options\n", "from qiskit.providers.ibmq import IBMQBackend\n", "from qiskit.visualization import plot_histogram\n", "import matplotlib.pyplot as plt\n", "\n", "# Configure logging\n", "logging.basicConfig(\n", " level=logging.INFO,\n", " format='%(asctime)s [%(levelname)s] %(message)s',\n", " handlers=[\n", " logging.StreamHandler(sys.stdout)\n", " ]\n", ")\n", "logger = logging.getLogger(__name__)\n", "\n", "def get_api_key() -> str:\n", " \"\"\"Retrieve the IBMQ API key from environment variables.\"\"\"\n", " api_key = os.getenv('IBMQ_API_KEY')\n", " if not api_key:\n", " logger.error(\"IBMQ_API_KEY environment variable not set.\")\n", " raise EnvironmentError(\"IBMQ_API_KEY environment variable not set.\")\n", " logger.info(\"API key retrieved successfully.\")\n", " return api_key\n", "\n", "def initialize_service(api_key: str) -> QiskitRuntimeService:\n", " \"\"\"Initialize the Qiskit Runtime service with the provided API key.\"\"\"\n", " try:\n", " service = QiskitRuntimeService(channel=\"ibm_quantum\", token=api_key)\n", " logger.info(\"Qiskit Runtime Service initialized successfully.\")\n", " return service\n", " except Exception as e:\n", " logger.error(f\"Failed to initialize Qiskit Runtime Service: {e}\")\n", " raise\n", "\n", "def list_available_backends(service: QiskitRuntimeService) -> List[IBMQBackend]:\n", " \"\"\"List available IBM Quantum backends.\"\"\"\n", " try:\n", " backends = service.backends()\n", " logger.info(f\"Available backends: {[backend.name() for backend in backends]}\")\n", " return backends\n", " except Exception as e:\n", " logger.error(f\"Failed to retrieve backends: {e}\")\n", " raise\n", "\n", "def select_best_backend(backends: List[IBMQBackend]) -> IBMQBackend:\n", " \"\"\"\n", " Select the best available backend based on certain criteria.\n", " For demonstration, we'll select the least busy backend.\n", " \"\"\"\n", " try:\n", " backend = service.backend.ibmq_least_busy(backends)\n", " logger.info(f\"Selected backend: {backend.name()}\")\n", " return backend\n", " except Exception as e:\n", " logger.error(f\"Failed to select the best backend: {e}\")\n", " raise\n", "\n", "def create_quantum_circuit(theta: List[float]) -> QuantumCircuit:\n", " \"\"\"Define and return a simple quantum circuit based on theta values.\"\"\"\n", " qc = QuantumCircuit(4)\n", " qc.rx(theta[0], 0)\n", " qc.ry(theta[1], 1)\n", " qc.cx(0, 1)\n", " qc.ry(theta[2], 2)\n", " qc.cx(1, 2)\n", " qc.rx(theta[3], 3)\n", " qc.measure_all()\n", " logger.debug(f\"Quantum Circuit created with theta values: {theta}\")\n", " return qc\n", "\n", "def run_sampler(service: QiskitRuntimeService, backend: IBMQBackend, circuit: QuantumCircuit, shots: int = 1024) -> dict:\n", " \"\"\"Initialize the sampler, execute the circuit, and return the counts.\"\"\"\n", " try:\n", " # Configure sampler options if needed\n", " sampler_options = Options(shots=shots)\n", " sampler_options.backend = backend\n", "\n", " sampler = Sampler(options=sampler_options)\n", " logger.info(\"Sampler initialized successfully.\")\n", "\n", " # Execute the circuit using the sampler\n", " job = sampler.run(circuits=circuit)\n", " logger.info(\"Sampler job submitted.\")\n", "\n", " # Retrieve the results\n", " result = job.result()\n", " counts = result.get_counts(circuit)\n", " logger.info(\"Sampler job completed successfully.\")\n", " return counts\n", " except Exception as e:\n", " logger.error(f\"Failed to run sampler: {e}\")\n", " raise\n", "\n", "def visualize_results(counts: dict):\n", " \"\"\"Visualize the measurement results as a histogram.\"\"\"\n", " try:\n", " plot_histogram(counts)\n", " plt.title(\"Quantum Circuit Measurement Results\")\n", " plt.show()\n", " logger.info(\"Results visualized successfully.\")\n", " except Exception as e:\n", " logger.error(f\"Failed to visualize results: {e}\")\n", " raise\n", "\n", "def main():\n", " \"\"\"Main function to execute the quantum sampling workflow.\"\"\"\n", " try:\n", " api_key = get_api_key()\n", " service = initialize_service(api_key)\n", " available_backends = list_available_backends(service)\n", "\n", " if not available_backends:\n", " logger.error(\"No available backends found.\")\n", " sys.exit(1)\n", "\n", " backend = select_best_backend(available_backends)\n", "\n", " # Define theta values\n", " theta_values = [0.3, 0.7, 1.2, 0.5]\n", " logger.info(f\"Theta values: {theta_values}\")\n", "\n", " # Create quantum circuit\n", " qc = create_quantum_circuit(theta_values)\n", "\n", " # Run sampler\n", " counts = run_sampler(service, backend, qc, shots=1024)\n", "\n", " # Print and visualize results\n", " logger.info(f\"Measurement Results: {counts}\")\n", " visualize_results(counts)\n", "\n", " except Exception as e:\n", " logger.critical(f\"An unexpected error occurred: {e}\")\n", " sys.exit(1)\n", "\n", "if __name__ == \"__main__\":\n", " main()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Benchmarking Execution Time\n", "\n", "This section includes code to benchmark the execution time of running the quantum circuit multiple times to evaluate the AGN's performance. We will measure and plot the time taken for each execution." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import time\n", "import matplotlib.pyplot as plt\n", "\n", "def benchmark_circuit(sampler, qc, iterations=100):\n", " exec_times = []\n", " for _ in range(iterations):\n", " start_time = time.time()\n", " job = sampler.run(circuits=qc, shots=1024)\n", " result = job.result()\n", " counts = result.get_counts(qc)\n", " end_time = time.time()\n", " exec_times.append(end_time - start_time)\n", " return exec_times\n", "\n", "# Run the benchmark\n", "with Sampler(session=service) as sampler:\n", " exec_times = benchmark_circuit(sampler, qc)\n", "\n", "# Plot the execution times\n", "plt.plot(exec_times, label=\"Execution Time per Iteration\")\n", "plt.xlabel(\"Iteration\")\n", "plt.ylabel(\"Execution Time (s)\")\n", "plt.title(\"AGN Execution Time Benchmark\")\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Troubleshooting\n", "\n", "If you encounter any issues, consider the following steps:\n", "\n", "- **Update Qiskit**: Ensure you have the latest version of Qiskit and Qiskit IBM Runtime installed.\n", "- **Restart Kernel**: After updating, restart the notebook kernel to load the latest packages.\n", "- **Check Available Backends**: Verify available backends with `service.backends()` if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# List available backends to troubleshoot issues\n", "print(service.backends())" ] }, { "cell_type": "code", "execution_count": 7, "id": "45f34dd4", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'torch' has no attribute 'zeros'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[7], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Initialize the state tensor on the GPU\u001b[39;00m\n\u001b[1;32m 4\u001b[0m num_qubits \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m4\u001b[39m\n\u001b[0;32m----> 5\u001b[0m state \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros\u001b[49m(\u001b[38;5;241m2\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mnum_qubits, dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mcomplex64)\u001b[38;5;241m.\u001b[39mto(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmps\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 6\u001b[0m state[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m \u001b[38;5;66;03m# Start in |0...0> state\u001b[39;00m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Define rotation gates as tensor operations\u001b[39;00m\n", "\u001b[0;31mAttributeError\u001b[0m: module 'torch' has no attribute 'zeros'" ] } ], "source": [ "import torch\n", "\n", "# Initialize the state tensor on the GPU\n", "num_qubits = 4\n", "state = torch.zeros(2**num_qubits, dtype=torch.complex64).to('mps')\n", "state[0] = 1 # Start in |0...0> state\n", "\n", "# Define rotation gates as tensor operations\n", "def rx(theta):\n", " return torch.tensor([\n", " [torch.cos(theta/2), -1j * torch.sin(theta/2)],\n", " [-1j * torch.sin(theta/2), torch.cos(theta/2)]\n", " ], dtype=torch.complex64).to('cuda')\n", "\n", "# Apply an RX rotation to the first qubit\n", "def apply_single_qubit_gate(state, gate, target_qubit, num_qubits):\n", " tensor_op = torch.eye(1 << num_qubits, dtype=torch.complex64).to('cuda')\n", " indices = torch.arange(1 << num_qubits)\n", " \n", " # Apply gate only to the target qubit's indices\n", " for i in range(2):\n", " tensor_op[(indices >> target_qubit & 1) == i, (indices >> target_qubit & 1) == i] = gate[i, i]\n", "\n", " return tensor_op @ state\n", "\n", "# Define an AGN-inspired operation sequence\n", "theta_values = [0.3, 0.7, 1.2, 0.5]\n", "for i, theta in enumerate(theta_values):\n", " gate = rx(theta)\n", " state = apply_single_qubit_gate(state, gate, target_qubit=i, num_qubits=num_qubits)\n", "\n", "# Compute probabilities (similar to measurement probabilities in quantum computing)\n", "probs = torch.abs(state)**2\n", "print(probs)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }