From 1f46e5c268487c2299f190144b5625bf87d086e2 Mon Sep 17 00:00:00 2001 From: Albert Frisch Date: Wed, 10 Apr 2019 16:57:39 +0200 Subject: [PATCH] reintroduce fidelity and probability output --- .../general/linear_systems_of_equations.ipynb | 187 ++++++++++++------ 1 file changed, 127 insertions(+), 60 deletions(-) diff --git a/qiskit/aqua/general/linear_systems_of_equations.ipynb b/qiskit/aqua/general/linear_systems_of_equations.ipynb index d99261ada..58e2a5e06 100644 --- a/qiskit/aqua/general/linear_systems_of_equations.ipynb +++ b/qiskit/aqua/general/linear_systems_of_equations.ipynb @@ -38,10 +38,19 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "qiskit.providers.ibmq.ibmqprovider\n" + ] + } + ], "source": [ "from qiskit.aqua import run_algorithm\n", "from qiskit.aqua.input import LinearSystemInput\n", + "from qiskit.quantum_info import state_fidelity\n", "from qiskit.aqua.algorithms.classical import ExactLPsolver\n", "import numpy as np" ] @@ -73,7 +82,13 @@ " 'provider': 'qiskit.BasicAer',\n", " 'name': 'statevector_simulator'\n", " }\n", - "}" + "}\n", + "\n", + "def fidelity(hhl, ref):\n", + " solution_hhl_normed = hhl / np.linalg.norm(hhl)\n", + " solution_ref_normed = ref / np.linalg.norm(ref)\n", + " fidelity = state_fidelity(solution_hhl_normed, solution_ref_normed)\n", + " print(\"fidelity %f\" % fidelity)" ] }, { @@ -94,7 +109,7 @@ "1 & 0 \\\\\n", "0 & 2\n", "\\end{bmatrix}$$ with the vector $$\\vec{b}= \\left( \\begin{array}{c}1 \\\\ 4 \\end{array} \\right)$$\n", - "The `result` dictionary contains several return values. The HHL solution for $\\vec{x}$ is accessible by the key `'solution_hhl'`. For comparison, also the classical solution of the linear system of equations is calculated using standard linear algebra functions in numpy. The fidelity between the HHL solution and the classical solution is also given in the output. Furthermore, the probability is shown with which HHL was running successfully, i.e. the HHL ancillary qubit has been measured to be $|1\\rangle$." + "The `result` dictionary contains several return values. The HHL solution for $\\vec{x}$ is accessible by the key `'solution'`. For comparison, also the classical solution of the linear system of equations is calculated using standard linear algebra functions in numpy. The fidelity between the HHL solution and the classical solution is also given in the output. Furthermore, the probability is shown with which HHL was running successfully, i.e. the HHL ancillary qubit has been measured to be $|1\\rangle$." ] }, { @@ -114,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -122,7 +137,9 @@ "output_type": "stream", "text": [ "solution [1.05859+0.j 1.99245+0.j]\n", - "classical solution [1. 2.]\n" + "classical solution [1. 2.]\n", + "probability 0.024630\n", + "fidelity 0.999389\n" ] } ], @@ -130,8 +147,11 @@ "result = run_algorithm(params)\n", "print(\"solution \", np.round(result['solution'], 5))\n", "\n", - "classical_result = ExactLPsolver(matrix, vector).run()\n", - "print(\"classical solution \", np.round(classical_result['solution'], 5))" + "result_ref = ExactLPsolver(matrix, vector).run()\n", + "print(\"classical solution \", np.round(result_ref['solution'], 5))\n", + "\n", + "print(\"probability %f\" % result['probability_result'])\n", + "fidelity(result['solution'], result_ref['solution'])" ] }, { @@ -143,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -151,7 +171,9 @@ "output_type": "stream", "text": [ "solution [0.84664+0.j 2.01762+0.j]\n", - "classical solution [1. 2.]\n" + "classical solution [1. 2.]\n", + "probability 0.361437\n", + "fidelity 0.995605\n" ] } ], @@ -164,8 +186,11 @@ "result = run_algorithm(params2)\n", "print(\"solution \", np.round(result['solution'], 5))\n", "\n", - "classical_result = ExactLPsolver(matrix, vector).run()\n", - "print(\"classical solution \", np.round(classical_result['solution'], 5))" + "result_ref = ExactLPsolver(matrix, vector).run()\n", + "print(\"classical solution \", np.round(result_ref['solution'], 5))\n", + "\n", + "print(\"probability %f\" % result['probability_result'])\n", + "fidelity(result['solution'], result_ref['solution'])" ] }, { @@ -177,21 +202,21 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "circuit_depth 12256\n", - "circuit_width 7\n" + "circuit_width 7\n", + "circuit_depth 12256\n" ] } ], "source": [ - "print(\"circuit_depth\", result['circuit_info']['depth'])\n", - "print(\"circuit_width\", result['circuit_info']['width'])" + "print(\"circuit_width\", result['circuit_info']['width'])\n", + "print(\"circuit_depth\", result['circuit_info']['depth'])" ] }, { @@ -216,13 +241,14 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "matrix = [[1, 3], [3, 2]]\n", "vector = [1, 1]\n", - "params['input'] = {\n", + "params3 = params\n", + "params3['input'] = {\n", " 'name': 'LinearSystemInput',\n", " 'matrix': matrix,\n", " 'vector': vector\n", @@ -231,7 +257,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -239,16 +265,21 @@ "output_type": "stream", "text": [ "solution [0.22147+0.j 0.22034-0.j]\n", - "classical solution [0.14286 0.28571]\n" + "classical solution [0.14286 0.28571]\n", + "probability 0.424639\n", + "fidelity 0.898454\n" ] } ], "source": [ - "result = run_algorithm(params)\n", + "result = run_algorithm(params3)\n", "print(\"solution \", np.round(result['solution'], 5))\n", "\n", - "classical_result = ExactLPsolver(matrix, vector).run()\n", - "print(\"classical solution \", np.round(classical_result['solution'], 5))" + "result_ref = ExactLPsolver(matrix, vector).run()\n", + "print(\"classical solution \", np.round(result_ref['solution'], 5))\n", + "\n", + "print(\"probability %f\" % result['probability_result'])\n", + "fidelity(result['solution'], result_ref['solution'])" ] }, { @@ -260,21 +291,21 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "circuit_depth 30254\n", - "circuit_width 7\n" + "circuit_width 7\n", + "circuit_depth 30254\n" ] } ], "source": [ - "print(\"circuit_depth\", result['circuit_info']['depth'])\n", - "print(\"circuit_width\", result['circuit_info']['width'])" + "print(\"circuit_width\", result['circuit_info']['width'])\n", + "print(\"circuit_depth\", result['circuit_info']['depth'])" ] }, { @@ -305,7 +336,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -318,7 +349,8 @@ " [0, 0, 0, 0, 0, 0, 1, 0],\n", " [1, 0, 0, 0, 0, 0, 0, 5]]\n", "vector = [1, 0, 0, 0, 0, 0, 0, 1]\n", - "params['input'] = {\n", + "params4 = params\n", + "params4['input'] = {\n", " 'name': 'LinearSystemInput',\n", " 'matrix': matrix,\n", " 'vector': vector\n", @@ -327,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -336,16 +368,21 @@ "text": [ "solution [ 0.18195-0.j 0. -0.j 0. -0.j -0. +0.j 0. +0.j\n", " -0. +0.j -0. -0.j 0.18041+0.j]\n", - "classical solution [0.21053 0. 0. 0. 0. 0. 0. 0.15789]\n" + "classical solution [0.21053 0. 0. 0. 0. 0. 0. 0.15789]\n", + "probability 0.935566\n", + "fidelity 0.981173\n" ] } ], "source": [ - "result = run_algorithm(params)\n", + "result = run_algorithm(params4)\n", "print(\"solution \", np.round(result['solution'], 5))\n", "\n", - "classical_result = ExactLPsolver(matrix, vector).run()\n", - "print(\"classical solution \", np.round(classical_result['solution'], 5))" + "result_ref = ExactLPsolver(matrix, vector).run()\n", + "print(\"classical solution \", np.round(result_ref['solution'], 5))\n", + "\n", + "print(\"probability %f\" % result['probability_result'])\n", + "fidelity(result['solution'], result_ref['solution'])" ] }, { @@ -357,21 +394,21 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "circuit_depth 315281\n", - "circuit_width 9\n" + "circuit_width 9\n", + "circuit_depth 315281\n" ] } ], "source": [ - "print(\"circuit_depth\", result['circuit_info']['depth'])\n", - "print(\"circuit_width\", result['circuit_info']['width'])" + "print(\"circuit_width\", result['circuit_info']['width'])\n", + "print(\"circuit_depth\", result['circuit_info']['depth'])" ] }, { @@ -390,7 +427,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -409,16 +446,16 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ - "params3 = params\n", - "params3[\"reciprocal\"] = {\n", + "params5 = params\n", + "params5[\"reciprocal\"] = {\n", " \"name\": \"Lookup\",\n", " \"negative_evals\": True\n", "}\n", - "params3[\"eigs\"] = {\n", + "params5[\"eigs\"] = {\n", " \"expansion_mode\": \"suzuki\",\n", " \"expansion_order\": 2,\n", " \"name\": \"EigsQPE\",\n", @@ -426,13 +463,13 @@ " \"num_ancillae\": 6,\n", " \"num_time_slices\": 70\n", "}\n", - "params3[\"initial_state\"] = {\n", + "params5[\"initial_state\"] = {\n", " \"name\": \"CUSTOM\"\n", "}\n", - "params3[\"iqft\"] = {\n", + "params5[\"iqft\"] = {\n", " \"name\": \"STANDARD\"\n", "}\n", - "params3[\"qft\"] = {\n", + "params5[\"qft\"] = {\n", " \"name\": \"STANDARD\"\n", "}" ] @@ -446,9 +483,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "random matrix:\n", + "[[ 0.284-0.j -0.257-0.051j -0.124+0.033j 0.038+0.023j]\n", + " [-0.257+0.051j 0.404+0.j 0.067-0.079j 0.054+0.055j]\n", + " [-0.124-0.033j 0.067+0.079j 0.282-0.j 0.043+0.004j]\n", + " [ 0.038-0.023j 0.054-0.055j 0.043-0.004j 0.206-0.j ]]\n", + "solution [ 79.9768 +4.52073j 60.28272 +3.09211j 37.51853 -9.5858j\n", + " -35.02324+26.46894j]\n", + "classical solution [ 76.1399 +1.92451j 57.30622 +1.20141j 35.96381-10.07775j\n", + " -32.03837+25.90593j]\n", + "probability 0.256771\n", + "fidelity 0.999946\n" + ] + } + ], "source": [ "# set the random seed to get the same pseudo-random matrix for every run\n", "np.random.seed(1)\n", @@ -460,31 +515,43 @@ "print(np.round(m, 3))\n", "\n", "algo_input = LinearSystemInput(matrix=matrix, vector=vector)\n", - "hhl = HHL.init_params(params3, algo_input)\n", + "hhl = HHL.init_params(params5, algo_input)\n", "backend = BasicAer.get_backend('statevector_simulator')\n", "quantum_instance = QuantumInstance(backend=backend)\n", - "result_hhl = hhl.run(quantum_instance)\n", + "result = hhl.run(quantum_instance)\n", "print(\"solution \", np.round(result['solution'], 5))\n", "\n", - "classical_result = ExactLPsolver(matrix, vector).run()\n", - "print(\"classical solution \", np.round(classical_result['solution'], 5))" + "result_ref = ExactLPsolver(matrix, vector).run()\n", + "print(\"classical solution \", np.round(result_ref['solution'], 5))\n", + "\n", + "print(\"probability %f\" % result['probability_result'])\n", + "fidelity(result['solution'], result_ref['solution'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The circuit depth and width are" + "The circuit width and depth are" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "circuit_width 12\n", + "circuit_depth 973537\n" + ] + } + ], "source": [ - "print(\"circuit_depth\", result_hhl['circuit_info']['depth'])\n", - "print(\"circuit_width\", result_hhl['circuit_info']['width']" + "print(\"circuit_width\", result['circuit_info']['width'])\n", + "print(\"circuit_depth\", result['circuit_info']['depth'])" ] }, { @@ -511,7 +578,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.7.1" } }, "nbformat": 4,