From 25457ca524a0451bd0e2329c588a82475f186db8 Mon Sep 17 00:00:00 2001 From: Tobias Stollenwerk Date: Wed, 5 Jun 2024 09:07:01 +0200 Subject: [PATCH] added template for exercise 04 --- template-exercise-04.ipynb | 325 +++++++++++++++++++++++++++++++++++++ 1 file changed, 325 insertions(+) create mode 100644 template-exercise-04.ipynb diff --git a/template-exercise-04.ipynb b/template-exercise-04.ipynb new file mode 100644 index 0000000..ae9e91b --- /dev/null +++ b/template-exercise-04.ipynb @@ -0,0 +1,325 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "0f158b5c-02a3-4e8b-8cd4-352bbb052d20", + "metadata": {}, + "outputs": [], + "source": [ + "import qiskit\n", + "# numerics\n", + "import numpy as np\n", + "# functional programmin tools\n", + "import itertools\n", + "import functools\n", + "import operator\n", + "# plotting\n", + "import matplotlib.pyplot as plt\n", + "%matplotlib inline\n", + "plt.rcParams['figure.dpi'] = 125\n", + "import seaborn\n", + "# data analysis\n", + "import pandas as pd\n", + "# qiskit \n", + "from qiskit import QuantumCircuit, transpile, assemble\n", + "from qiskit_aer import QasmSimulator, AerSimulator\n", + "from qiskit.quantum_info import Pauli\n", + "from qiskit.visualization import array_to_latex, plot_histogram\n", + "from qiskit.quantum_info import Kraus, SuperOp, SparsePauliOp, Operator, DensityMatrix, Statevector\n", + "from qiskit_aer.noise import NoiseModel, QuantumError, ReadoutError, depolarizing_error\n", + "from qiskit.circuit import ParameterVector" + ] + }, + { + "cell_type": "markdown", + "id": "ba7eed79-5767-491c-9fbf-64e25bfd126b", + "metadata": {}, + "source": [ + "# Excercise 4.1 - QAOA\n", + "\n", + "$G=(V,E)$, where $V=\\{v_0,v_1,v_2,v_3 \\}$ and $E=\\{(v_0,v_1), (v_1,v_2), (v_1,v_3), (v_2,v_3)) \\}$. The Trotterized time evolution operator is\n", + "\n", + "$$U= \\prod_{m=1}^p U_M(\\beta_m)U_f(\\gamma_m)= exp(-i \\beta_1 H_M \\Delta t)exp(-i \\gamma_1 H_f \\Delta t) \\cdots exp(-i \\beta_p H_M \\Delta t)exp(-i \\gamma_p H_f \\Delta t),$$\n", + "\n", + "for $H_M=\\sum_{i=1}^{\\vert V \\vert} X_i$ with ground state $\\ket{+}^{\\otimes \\vert V \\vert}$ and $H_f=\\sum_{(u,v) \\in E}Z_u Z_v$ with a ground state that encodes the solution to the MaxCut problem." + ] + }, + { + "cell_type": "markdown", + "id": "402963ce-15fe-4304-856e-b5bc7e983b86", + "metadata": {}, + "source": [ + "### Template to (a)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "27190a39-f9dd-494e-87aa-d57d08e8abcd", + "metadata": {}, + "outputs": [], + "source": [ + "E = [(0, 1), (1, 2), (1, 3), (2, 3)]\n", + "V = [0, 1, 2, 3]\n", + "\n", + "#To Do: Generate H_f and H_mix" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bfc300d5-cc45-4eb9-9845-c3f03c582e83", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_keys([(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)])\n" + ] + } + ], + "source": [ + "basis_states = {}\n", + "ket = {0: Statevector([1, 0]), 1: Statevector([0, 1])}\n", + "for i, bitstring in enumerate(itertools.product([0, 1], repeat=4)):\n", + " state = ket[bitstring[0]]\n", + " for k in range(len(bitstring) - 1):\n", + " state = state.tensor(ket[bitstring[k + 1]])\n", + " basis_states[bitstring] = state\n", + "print(basis_states.keys())" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ecd764a5-d292-42fc-855b-7b8e49686ceb", + "metadata": {}, + "outputs": [], + "source": [ + "#To Do: Calculate exact expectation values" + ] + }, + { + "cell_type": "markdown", + "id": "d2692241-a9b3-4233-99ea-b3ec63db6219", + "metadata": {}, + "source": [ + "### Template to (c) and (d)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "b54d6cd8-8a4f-4648-9b0a-f0a9697f7f1e", + "metadata": {}, + "outputs": [], + "source": [ + "# Create the QAOA circuit\n", + "def qaoa_circuit(gamma, beta,V , E):\n", + " #To Do: Create QAOA circuit for gamma and beta and a given graph\n", + " qc = QuantumCircuit()\n", + " return qc" + ] + }, + { + "cell_type": "markdown", + "id": "c43c7a07-3770-4a6e-ba39-803f7a7dc26b", + "metadata": {}, + "source": [ + "evaluate QAOA should do:\n", + "\n", + "$c_{i,j,k,l}=(\\frac{counts}{n-shots})^{1/2}$ for bitstring 'ijkl', such that $\\sum_{i,j,k,l=0,1}\\vert c_{i,j,k,l} \\vert^{2}=1$\n", + "\n", + "\n", + "get state:\n", + "$\\ket{\\psi}=\\sum_{i,j,k,l=0,1}c_{i,j,k,l} \\ket{i,j,k,l}$ \n", + "\n", + "\n", + "calculate expextation value:\n", + "$E=\\bra{\\psi}H_f \\ket{\\psi}=\\sum_{i,j,k,l=0,1}\\vert c_{i,j,k,l} \\vert^2 \\bra{i,j,k,l}H_f \\ket{i,j,k,l}$" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "361b5ee1-62a4-4ffd-b50f-bae12fa1ac07", + "metadata": {}, + "outputs": [], + "source": [ + "# Evaluate the QAOA circuit\n", + "def evaluate_qaoa(gamma, beta, V, E, n_shots=1000):\n", + " qc = qaoa_circuit(gamma, beta,V, E)\n", + " qc.measure(range(len(V)), range(len(V)))\n", + " \n", + " backend = AerSimulator()\n", + " transpiled_qc = transpile(qc, backend)\n", + " qobj = assemble(transpiled_qc)\n", + "\n", + " result = backend.run(transpiled_qc).result()\n", + " counts = result.get_counts(qc)\n", + " \n", + " #To Do: Calculate expectation value for a given result\n", + " obj_val = 0\n", + " \n", + " return obj_val\n", + "\n", + "# Optimize QAOA parameters\n", + "def optimize_qaoa(V, E, n_shots=1000, n_steps=50):\n", + " # Initial parameters, equidistant grid of gamma and beta\n", + "\n", + "\n", + " #To Do: get optimal values for gamma, beta and optimal (minimal) expectation value \n", + " return gamma_opt, beta_opt, obj_value_opt" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "06ecb115-c52d-49b3-a989-98e653f389fa", + "metadata": {}, + "outputs": [], + "source": [ + "# To Do: Run the optimization to get gamma_opt, beta_opt (replace assignment below)\n", + "gamma_opt = 0.0\n", + "beta_opt = 0.0" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "62d3b9c1-a70a-448a-98b0-9a3465b5ec15", + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJEAAAA5CAYAAAA2l8mgAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAABM5AAATOQGPwlYBAAAAp0lEQVR4nO3SQQ0AIBDAMMC/50PEHoSkVbDH9szMguC8DuB/JiIzEZmJyExEZiIyE5GZiMxEZCYiMxGZichMRGYiMhORmYjMRGQmIjMRmYnITERmIjITkZmIzERkJiIzEZmJyExEZiIyE5GZiMxEZCYiMxGZichMRGYiMhORmYjMRGQmIjMRmYnITERmIjITkZmIzERkJiIzEZmJyExEZiIyE5GZiOwC04gEbgwPCtEAAAAASUVORK5CYII=", + "text/plain": [ + "
" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "gamma=gamma_opt\n", + "beta=beta_opt\n", + "\n", + "#run the circuit for optimal values\n", + "qc = qaoa_circuit(gamma, beta, V, E)\n", + "qc.measure_all()\n", + "qc.draw('mpl')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "caa86992-7afd-442e-a80f-f9ce3e557d6e", + "metadata": {}, + "outputs": [], + "source": [ + "#To Do: get counts i.e. ground states of H_f\n" + ] + }, + { + "cell_type": "markdown", + "id": "7bd875df-0e77-4f41-a113-0bfda0f705e3", + "metadata": {}, + "source": [ + "## Exercise 2" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "4bda40b3-daa8-4094-bcc0-97c60e2f09ad", + "metadata": {}, + "outputs": [], + "source": [ + "from scipy import linalg\n", + "from scipy.linalg import expm\n", + "from scipy.optimize import minimize" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "774d9bec-c265-4e31-aabb-29d36ff4c080", + "metadata": {}, + "outputs": [], + "source": [ + "#define X and Z Matrix\n", + "Z=np.array([[1,0],\n", + " [0,-1]])\n", + "X=np.array([[0,1],\n", + " [1,0]])\n", + "\n", + "\n", + "def s_function_1(t):\n", + " y=np.sin(np.pi*t/(2*T))**2\n", + " return y\n", + "\n", + "def s_function_2(t):\n", + " y=np.sin(np.pi*t/(2*T))**4\n", + " return y\n", + "\n", + "def get_Hamiltonian_1(t):\n", + " #to do: get hamiltonian for s_1(x)\n", + " return H\n", + "\n", + "def get_Hamiltonian_2(t):\n", + " #to do: get hamiltonian for s_2(x)\n", + " return H\n", + "\n", + "def get_time_evol_operator(T):\n", + " #to do: get time evolution operator for H_1\n", + " return U\n", + "\n", + "def get_time_evol_operator_2(T):\n", + " #to do: get time evolution operator for H_2\n", + " return U" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "927390d3-654c-49c1-acf0-3d0444d9ee1b", + "metadata": {}, + "outputs": [], + "source": [ + "#To Do: plot fidelity" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}