From 641277c62b21e3770be9cb48282b3879309118c0 Mon Sep 17 00:00:00 2001 From: Mattia Robbiano Date: Mon, 8 Sep 2025 18:28:00 +0200 Subject: [PATCH] added tests for quimb backend --- .gitignore | 3 - examples/quimb_intro/quimb_introduction.ipynb | 779 ++++++++++++++++++ 2 files changed, 779 insertions(+), 3 deletions(-) create mode 100644 examples/quimb_intro/quimb_introduction.ipynb diff --git a/.gitignore b/.gitignore index 39fb956..7f051b8 100644 --- a/.gitignore +++ b/.gitignore @@ -160,6 +160,3 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ .devenv - -# Cotengra cache -examples/quimb_intro/ctg_cache/ \ No newline at end of file diff --git a/examples/quimb_intro/quimb_introduction.ipynb b/examples/quimb_intro/quimb_introduction.ipynb new file mode 100644 index 0000000..6153e90 --- /dev/null +++ b/examples/quimb_intro/quimb_introduction.ipynb @@ -0,0 +1,779 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "656bb283-ac6d-48d2-a029-3c417c9961f8", + "metadata": {}, + "source": [ + "## Introduction to Quimb backend in QiboTN\n", + "\n", + "#### Some imports" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6722d94e-e311-48f9-b6df-c6d829bf67fb", + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "import numpy as np\n", + "from scipy import stats\n", + "\n", + "import qibo\n", + "from qibo import Circuit, gates, hamiltonians\n", + "from qibo.backends import construct_backend" + ] + }, + { + "cell_type": "markdown", + "id": "a009a5e0-cfd4-4a49-9f7c-e82f252c6147", + "metadata": {}, + "source": [ + "#### Some hyper parameters" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b0a1da82", + "metadata": {}, + "outputs": [], + "source": [ + "import cotengra as ctg\n", + "ctg_opt = ctg.ReusableHyperOptimizer(\n", + " max_time=10,\n", + " minimize='combo',\n", + " slicing_opts=None,\n", + " parallel=True,\n", + " progbar=True\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64162116-1555-4a68-811c-01593739d622", + "metadata": {}, + "outputs": [], + "source": [ + "# construct qibotn backend\n", + "quimb_backend = construct_backend(backend=\"qibotn\", platform=\"quimb\")\n", + "\n", + "# set number of qubits\n", + "nqubits = 25\n", + "\n", + "# set numpy random seed\n", + "np.random.seed(42)\n", + "\n", + "quimb_backend.setup_backend_specifics(\n", + " qimb_backend=\"jax\", \n", + " optimizer='auto-hq'\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "252f5cd1-5932-4de6-8076-4a357d50ebad", + "metadata": {}, + "source": [ + "#### Constructing a parametric quantum circuit" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "4a22a172-f50d-411d-afa3-fa61937c7b3a", + "metadata": {}, + "outputs": [], + "source": [ + "def build_circuit(nqubits, nlayers):\n", + " \"\"\"Construct a parametric quantum circuit.\"\"\"\n", + " circ = Circuit(nqubits)\n", + " for _ in range(nlayers):\n", + " for q in range(nqubits):\n", + " circ.add(gates.RY(q=q, theta=0.))\n", + " circ.add(gates.RZ(q=q, theta=0.))\n", + " [circ.add(gates.CNOT(q%nqubits, (q+1)%nqubits) for q in range(nqubits))]\n", + " circ.add(gates.M(*range(nqubits)))\n", + " return circ" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 : ─RY─RZ─o───────────────────────────────────────────────X─RY─RZ─o────── ...\n", + "1 : ─RY─RZ─X─o─────────────────────────────────────────────|─RY─RZ─X─o──── ...\n", + "2 : ─RY─RZ───X─o───────────────────────────────────────────|─RY─RZ───X─o── ...\n", + "3 : ─RY─RZ─────X─o─────────────────────────────────────────|─RY─RZ─────X─o ...\n", + "4 : ─RY─RZ───────X─o───────────────────────────────────────|─RY─RZ───────X ...\n", + "5 : ─RY─RZ─────────X─o─────────────────────────────────────|─RY─RZ──────── ...\n", + "6 : ─RY─RZ───────────X─o───────────────────────────────────|─RY─RZ──────── ...\n", + "7 : ─RY─RZ─────────────X─o─────────────────────────────────|─RY─RZ──────── ...\n", + "8 : ─RY─RZ───────────────X─o───────────────────────────────|─RY─RZ──────── ...\n", + "9 : ─RY─RZ─────────────────X─o─────────────────────────────|─RY─RZ──────── ...\n", + "10: ─RY─RZ───────────────────X─o───────────────────────────|─RY─RZ──────── ...\n", + "11: ─RY─RZ─────────────────────X─o─────────────────────────|─RY─RZ──────── ...\n", + "12: ─RY─RZ───────────────────────X─o───────────────────────|─RY─RZ──────── ...\n", + "13: ─RY─RZ─────────────────────────X─o─────────────────────|─RY─RZ──────── ...\n", + "14: ─RY─RZ───────────────────────────X─o───────────────────|─RY─RZ──────── ...\n", + "15: ─RY─RZ─────────────────────────────X─o─────────────────|─RY─RZ──────── ...\n", + "16: ─RY─RZ───────────────────────────────X─o───────────────|─RY─RZ──────── ...\n", + "17: ─RY─RZ─────────────────────────────────X─o─────────────|─RY─RZ──────── ...\n", + "18: ─RY─RZ───────────────────────────────────X─o───────────|─RY─RZ──────── ...\n", + "19: ─RY─RZ─────────────────────────────────────X─o─────────|─RY─RZ──────── ...\n", + "20: ─RY─RZ───────────────────────────────────────X─o───────|─RY─RZ──────── ...\n", + "21: ─RY─RZ─────────────────────────────────────────X─o─────|─RY─RZ──────── ...\n", + "22: ─RY─RZ───────────────────────────────────────────X─o───|─RY─RZ──────── ...\n", + "23: ─RY─RZ─────────────────────────────────────────────X─o─|─RY─RZ──────── ...\n", + "24: ─RY─RZ───────────────────────────────────────────────X─o─RY─RZ──────── ...\n", + "\n", + "0 : ... ─────────────────────────────────────────X─RY─RZ─o──────────────────── ...\n", + "1 : ... ─────────────────────────────────────────|─RY─RZ─X─o────────────────── ...\n", + "2 : ... ─────────────────────────────────────────|─RY─RZ───X─o──────────────── ...\n", + "3 : ... ─────────────────────────────────────────|─RY─RZ─────X─o────────────── ...\n", + "4 : ... ─o───────────────────────────────────────|─RY─RZ───────X─o──────────── ...\n", + "5 : ... ─X─o─────────────────────────────────────|─RY─RZ─────────X─o────────── ...\n", + "6 : ... ───X─o───────────────────────────────────|─RY─RZ───────────X─o──────── ...\n", + "7 : ... ─────X─o─────────────────────────────────|─RY─RZ─────────────X─o────── ...\n", + "8 : ... ───────X─o───────────────────────────────|─RY─RZ───────────────X─o──── ...\n", + "9 : ... ─────────X─o─────────────────────────────|─RY─RZ─────────────────X─o── ...\n", + "10: ... ───────────X─o───────────────────────────|─RY─RZ───────────────────X─o ...\n", + "11: ... ─────────────X─o─────────────────────────|─RY─RZ─────────────────────X ...\n", + "12: ... ───────────────X─o───────────────────────|─RY─RZ────────────────────── ...\n", + "13: ... ─────────────────X─o─────────────────────|─RY─RZ────────────────────── ...\n", + "14: ... ───────────────────X─o───────────────────|─RY─RZ────────────────────── ...\n", + "15: ... ─────────────────────X─o─────────────────|─RY─RZ────────────────────── ...\n", + "16: ... ───────────────────────X─o───────────────|─RY─RZ────────────────────── ...\n", + "17: ... ─────────────────────────X─o─────────────|─RY─RZ────────────────────── ...\n", + "18: ... ───────────────────────────X─o───────────|─RY─RZ────────────────────── ...\n", + "19: ... ─────────────────────────────X─o─────────|─RY─RZ────────────────────── ...\n", + "20: ... ───────────────────────────────X─o───────|─RY─RZ────────────────────── ...\n", + "21: ... ─────────────────────────────────X─o─────|─RY─RZ────────────────────── ...\n", + "22: ... ───────────────────────────────────X─o───|─RY─RZ────────────────────── ...\n", + "23: ... ─────────────────────────────────────X─o─|─RY─RZ────────────────────── ...\n", + "24: ... ───────────────────────────────────────X─o─RY─RZ────────────────────── ...\n", + "\n", + "0 : ... ───────────────────────────X─M─\n", + "1 : ... ───────────────────────────|─M─\n", + "2 : ... ───────────────────────────|─M─\n", + "3 : ... ───────────────────────────|─M─\n", + "4 : ... ───────────────────────────|─M─\n", + "5 : ... ───────────────────────────|─M─\n", + "6 : ... ───────────────────────────|─M─\n", + "7 : ... ───────────────────────────|─M─\n", + "8 : ... ───────────────────────────|─M─\n", + "9 : ... ───────────────────────────|─M─\n", + "10: ... ───────────────────────────|─M─\n", + "11: ... ─o─────────────────────────|─M─\n", + "12: ... ─X─o───────────────────────|─M─\n", + "13: ... ───X─o─────────────────────|─M─\n", + "14: ... ─────X─o───────────────────|─M─\n", + "15: ... ───────X─o─────────────────|─M─\n", + "16: ... ─────────X─o───────────────|─M─\n", + "17: ... ───────────X─o─────────────|─M─\n", + "18: ... ─────────────X─o───────────|─M─\n", + "19: ... ───────────────X─o─────────|─M─\n", + "20: ... ─────────────────X─o───────|─M─\n", + "21: ... ───────────────────X─o─────|─M─\n", + "22: ... ─────────────────────X─o───|─M─\n", + "23: ... ───────────────────────X─o─|─M─\n", + "24: ... ─────────────────────────X─o─M─\n" + ] + } + ], + "source": [ + "circuit = build_circuit(nqubits=nqubits, nlayers=3)\n", + "circuit.draw()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98", + "metadata": {}, + "outputs": [], + "source": [ + "# Setting random parameters\n", + "circuit.set_parameters(\n", + " parameters=np.random.uniform(-np.pi, np.pi, len(circuit.get_parameters())),\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "fd0cea52-03f5-4366-a01a-a5a84aa8ebc7", + "metadata": {}, + "source": [ + "#### Setting up the tensor network simulator\n", + "\n", + "Depending on the simulator, various parameters can be set. One can customize the tensor network execution via the `backend.configure_tn_simulation` function, whose face depends on the specific backend provider." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "2ee03e94-d794-4a51-9e76-01e8d8a259ba", + "metadata": {}, + "outputs": [], + "source": [ + "# Customization of the tensor network simulation in the case of quimb backend\n", + "# Here we use only some of the possible arguments\n", + "quimb_backend.configure_tn_simulation(\n", + " #ansatz=\"MPS\",\n", + " max_bond_dimension=10\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "648d85b8-445d-4081-aeed-1691fbae67be", + "metadata": {}, + "source": [ + "#### Executing through the backend\n", + "\n", + "The `backend.execute_circuit` method can be used then. We can simulate results in three ways:\n", + "1. reconstruction of the final state only if `return_array` is set to `True`;\n", + "2. computation of the relevant probabilities of the final state.\n", + "3. reconstruction of the relevant state's frequencies (only if `nshots` is not `None`)." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'nqubits': 25,\n", + " 'backend': qibotn (quimb),\n", + " 'measures': Counter({'0010101100100011101100110': 1,\n", + " '1101010011101101100010011': 1,\n", + " '0100010101110001100001001': 1,\n", + " '1100001000000010101011101': 1,\n", + " '0100101000100110010010000': 1,\n", + " '0100000111111100111001100': 1,\n", + " '0010101100000111100000000': 1,\n", + " '0101111100011000101100111': 1,\n", + " '0111111001101110011000111': 1,\n", + " '1111011100010111101010110': 1,\n", + " '1001000111111111111000000': 1,\n", + " '1110010011010011000010000': 1,\n", + " '1001101011101101100000000': 1,\n", + " '1010101100011110001110111': 1,\n", + " '0111010110011101010101010': 1,\n", + " '1000110100110010010011101': 1,\n", + " '0010010001010110000011100': 1,\n", + " '1110110010010000110100101': 1,\n", + " '1110010110011111101110101': 1,\n", + " '0101001000011111100010111': 1,\n", + " '0111011101111111101101111': 1,\n", + " '0011111111100001011000010': 1,\n", + " '0110001100010001000101011': 1,\n", + " '0100101101000010101101111': 1,\n", + " '0101010010010010000111001': 1,\n", + " '0000000011111101101011110': 1,\n", + " '0000100100100001101011010': 1,\n", + " '1010111100000101010101110': 1,\n", + " '0110011011000001010101100': 1,\n", + " '1111001101101001111111111': 1,\n", + " '0011001011011110000010000': 1,\n", + " '1001100100010000010010000': 1,\n", + " '1011111101100001111110011': 1,\n", + " '0011101011101010010111001': 1,\n", + " '1111100010010010101100110': 1,\n", + " '0101100001011110010100010': 1,\n", + " '0100101110010011000011000': 1,\n", + " '1011001011101011101000101': 1,\n", + " '0000101111101100010100010': 1,\n", + " '1101000010100000011000110': 1,\n", + " '1100110110101111110010001': 1,\n", + " '1100001011010011010111101': 1,\n", + " '1000101011010011111001101': 1,\n", + " '1100001001010101110010100': 1,\n", + " '0110101101110000001100110': 1,\n", + " '0111110111010101101000110': 1,\n", + " '1110010111101010101000101': 1,\n", + " '1000110010011010000110000': 1,\n", + " '1001011101010100010010001': 1,\n", + " '0100110100010001000101000': 1,\n", + " '0000100010100010010100010': 1,\n", + " '1010110110011100011000101': 1,\n", + " '0000110010100001101001010': 1,\n", + " '0100101000111100110010111': 1,\n", + " '1011101011011110000001011': 1,\n", + " '1010011111111101011110110': 1,\n", + " '1100110111100010000011110': 1,\n", + " '0100110100100000101100111': 1,\n", + " '1110111100011110001011101': 1,\n", + " '0011001100110011101000110': 1,\n", + " '1000110000111001101011101': 1,\n", + " '0100101101000010101011111': 1,\n", + " '0101010000000110010101001': 1,\n", + " '0110111101101010010111000': 1,\n", + " '0010010010100010101010101': 1,\n", + " '1100001000101011101100101': 1,\n", + " '1010101000011101101101011': 1,\n", + " '1000010111001110010011110': 1,\n", + " '0000010001110001100000011': 1,\n", + " '0011010000101101111101100': 1,\n", + " '1011010000100011000010000': 1,\n", + " '1110100011001010000110000': 1,\n", + " '1101000111110111001100101': 1,\n", + " '0110001101010001110011010': 1,\n", + " '1001011000100110101010101': 1,\n", + " '0000110010100000000110111': 1,\n", + " '1111001000001000111001000': 1,\n", + " '0011100011100010010100001': 1,\n", + " '0111111110010000001010111': 1,\n", + " '1011100100101101101011110': 1,\n", + " '0111001011100110001000001': 1,\n", + " '0111001000110000111101111': 1,\n", + " '1101111011001110000000000': 1,\n", + " '0010111101011101010010100': 1,\n", + " '1100100010010001111101000': 1,\n", + " '0101011111100010101010100': 1,\n", + " '1110100111011111111000001': 1,\n", + " '1100000010101100001000110': 1,\n", + " '0111011011010001011001011': 1,\n", + " '0111000000100001101010011': 1,\n", + " '1001111100110001111101000': 1,\n", + " '0011010010100101010101100': 1,\n", + " '1010111011011001101100011': 1,\n", + " '1001000100001110000110000': 1,\n", + " '1011011110100101010100101': 1,\n", + " '0101100010011101111111001': 1,\n", + " '0000100000101110000001000': 1,\n", + " '1110010111000010111001000': 1,\n", + " '1011011101101101111011101': 1,\n", + " '0111100010010001111001011': 1}),\n", + " 'measured_probabilities': {'0010101100100011101100110': np.float64(1.4287479948554065e-07),\n", + " '1101010011101101100010011': np.float64(6.840535900713192e-07),\n", + " '0100010101110001100001001': np.float64(2.4297656493808975e-08),\n", + " '1100001000000010101011101': np.float64(8.093409548935148e-07),\n", + " '0100101000100110010010000': np.float64(4.2843525768921185e-07),\n", + " '0100000111111100111001100': np.float64(5.4788880664882446e-08),\n", + " '0010101100000111100000000': np.float64(6.350945339007582e-09),\n", + " '0101111100011000101100111': np.float64(1.6189384309546533e-09),\n", + " '0111111001101110011000111': np.float64(4.231070771736262e-07),\n", + " '1111011100010111101010110': np.float64(8.809840523521098e-08),\n", + " '1001000111111111111000000': np.float64(2.2649917694883763e-09),\n", + " '1110010011010011000010000': np.float64(2.9400450442747213e-06),\n", + " '1001101011101101100000000': np.float64(3.564993949972801e-07),\n", + " '1010101100011110001110111': np.float64(2.0353895972901936e-07),\n", + " '0111010110011101010101010': np.float64(9.272342830457222e-08),\n", + " '1000110100110010010011101': np.float64(1.0554195999421195e-07),\n", + " '0010010001010110000011100': np.float64(4.926758473909799e-08),\n", + " '1110110010010000110100101': np.float64(3.6907560113556863e-07),\n", + " '1110010110011111101110101': np.float64(1.0912695706428317e-07),\n", + " '0101001000011111100010111': np.float64(2.3749237133846456e-07),\n", + " '0111011101111111101101111': np.float64(4.4313847930510314e-07),\n", + " '0011111111100001011000010': np.float64(2.879407202649115e-09),\n", + " '0110001100010001000101011': np.float64(2.2970898298927726e-08),\n", + " '0100101101000010101101111': np.float64(1.2403396951734157e-06),\n", + " '0101010010010010000111001': np.float64(3.2580433289301146e-06),\n", + " '0000000011111101101011110': np.float64(7.551781140278402e-07),\n", + " '0000100100100001101011010': np.float64(7.342186380985404e-06),\n", + " '1010111100000101010101110': np.float64(2.0803417549284257e-07),\n", + " '0110011011000001010101100': np.float64(6.536548485677532e-09),\n", + " '1111001101101001111111111': np.float64(6.662093312934175e-08),\n", + " '0011001011011110000010000': np.float64(9.152885414596061e-07),\n", + " '1001100100010000010010000': np.float64(1.1822334160129414e-06),\n", + " '1011111101100001111110011': np.float64(1.2742285932601216e-05),\n", + " '0011101011101010010111001': np.float64(8.001218354241628e-07),\n", + " '1111100010010010101100110': np.float64(9.3255563882172e-07),\n", + " '0101100001011110010100010': np.float64(1.0626891034969866e-06),\n", + " '0100101110010011000011000': np.float64(1.5652631724013031e-07),\n", + " '1011001011101011101000101': np.float64(1.025423997199104e-06),\n", + " '0000101111101100010100010': np.float64(6.003473256257238e-07),\n", + " '1101000010100000011000110': np.float64(1.085658621941692e-06),\n", + " '1100110110101111110010001': np.float64(4.659708973943698e-09),\n", + " '1100001011010011010111101': np.float64(5.295910969809775e-07),\n", + " '1000101011010011111001101': np.float64(3.6779048150728553e-07),\n", + " '1100001001010101110010100': np.float64(4.92126869361337e-09),\n", + " '0110101101110000001100110': np.float64(7.741427610734003e-08),\n", + " '0111110111010101101000110': np.float64(2.399267980010788e-07),\n", + " '1110010111101010101000101': np.float64(5.667518009732859e-07),\n", + " '1000110010011010000110000': np.float64(2.084260073059544e-06),\n", + " '1001011101010100010010001': np.float64(2.6571545078690544e-08),\n", + " '0100110100010001000101000': np.float64(3.079779061777933e-08),\n", + " '0000100010100010010100010': np.float64(6.929792821857359e-06),\n", + " '1010110110011100011000101': np.float64(4.630180771716902e-08),\n", + " '0000110010100001101001010': np.float64(9.116530823953186e-07),\n", + " '0100101000111100110010111': np.float64(1.3249324853060952e-07),\n", + " '1011101011011110000001011': np.float64(1.1907229585114808e-06),\n", + " '1010011111111101011110110': np.float64(4.56149319722231e-09),\n", + " '1100110111100010000011110': np.float64(5.257006816629565e-07),\n", + " '0100110100100000101100111': np.float64(5.776773429581827e-07),\n", + " '1110111100011110001011101': np.float64(1.9990466754187882e-07),\n", + " '0011001100110011101000110': np.float64(4.3593985418091113e-08),\n", + " '1000110000111001101011101': np.float64(3.6389888110895923e-07),\n", + " '0100101101000010101011111': np.float64(7.622087666902094e-07),\n", + " '0101010000000110010101001': np.float64(2.994365575033701e-07),\n", + " '0110111101101010010111000': np.float64(3.147129865281865e-08),\n", + " '0010010010100010101010101': np.float64(3.7955795123562516e-07),\n", + " '1100001000101011101100101': np.float64(2.483089067956661e-07),\n", + " '1010101000011101101101011': np.float64(3.09860035910458e-07),\n", + " '1000010111001110010011110': np.float64(4.718847603022761e-08),\n", + " '0000010001110001100000011': np.float64(1.618347020880399e-08),\n", + " '0011010000101101111101100': np.float64(2.307864975407703e-07),\n", + " '1011010000100011000010000': np.float64(1.604607088007449e-07),\n", + " '1110100011001010000110000': np.float64(5.735844296703352e-07),\n", + " '1101000111110111001100101': np.float64(7.018930935740711e-10),\n", + " '0110001101010001110011010': np.float64(1.426676825693035e-07),\n", + " '1001011000100110101010101': np.float64(1.3658924161666397e-07),\n", + " '0000110010100000000110111': np.float64(7.127088059066376e-07),\n", + " '1111001000001000111001000': np.float64(9.382957208112125e-08),\n", + " '0011100011100010010100001': np.float64(1.5633352068001958e-07),\n", + " '0111111110010000001010111': np.float64(6.24916842986095e-07),\n", + " '1011100100101101101011110': np.float64(5.277053774965453e-08),\n", + " '0111001011100110001000001': np.float64(2.706733332328552e-07),\n", + " '0111001000110000111101111': np.float64(8.740290927759748e-07),\n", + " '1101111011001110000000000': np.float64(1.1390459236761492e-07),\n", + " '0010111101011101010010100': np.float64(2.851294124498577e-06),\n", + " '1100100010010001111101000': np.float64(8.627935077391886e-06),\n", + " '0101011111100010101010100': np.float64(6.937375172146743e-08),\n", + " '1110100111011111111000001': np.float64(2.517698486277455e-07),\n", + " '1100000010101100001000110': np.float64(2.1300563007206324e-07),\n", + " '0111011011010001011001011': np.float64(5.8246768046662493e-08),\n", + " '0111000000100001101010011': np.float64(3.123499806377365e-07),\n", + " '1001111100110001111101000': np.float64(4.777318918055056e-07),\n", + " '0011010010100101010101100': np.float64(5.494449035514909e-07),\n", + " '1010111011011001101100011': np.float64(1.1425290362328705e-06),\n", + " '1001000100001110000110000': np.float64(3.266349767666358e-08),\n", + " '1011011110100101010100101': np.float64(5.947082800379298e-07),\n", + " '0101100010011101111111001': np.float64(3.5373491729489938e-06),\n", + " '0000100000101110000001000': np.float64(3.041450478019988e-07),\n", + " '1110010111000010111001000': np.float64(2.0041305331234744e-07),\n", + " '1011011101101101111011101': np.float64(2.701698118830075e-07),\n", + " '0111100010010001111001011': np.float64(1.3996687152906713e-06)},\n", + " 'prob_type': 'default',\n", + " 'statevector': Array([[-5.8646699e-05-1.0826049e-04j],\n", + " [-1.9374731e-05+8.6432210e-06j],\n", + " [ 7.7656114e-05-3.3436768e-04j],\n", + " ...,\n", + " [ 1.4206149e-05+6.0087572e-05j],\n", + " [ 1.2372108e-04-1.6373204e-05j],\n", + " [ 3.0789568e-05+3.2571501e-05j]], dtype=complex64)}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Simple execution (defaults)\n", + "outcome = quimb_backend.execute_circuit(circuit=circuit, nshots=100, return_array=True)\n", + "\n", + "# Print outcome\n", + "vars(outcome)" + ] + }, + { + "cell_type": "markdown", + "id": "84ec0b48-f6b4-495c-93b8-8e42d1a8b0df", + "metadata": {}, + "source": [ + "---\n", + "\n", + "One can access to the specific contents of the simulation outcome." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "c0443efc-21ef-4ed5-9cf4-785d204a1881", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[Qibo 0.2.20|ERROR|2025-09-08 17:57:29]: Tensor network simulation cannot be used to reconstruct statevector for >= 20 .\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Probabilities:\n", + " {'0010101100100011101100110': np.float64(1.4287479948554065e-07), '1101010011101101100010011': np.float64(6.840535900713192e-07), '0100010101110001100001001': np.float64(2.4297656493808975e-08), '1100001000000010101011101': np.float64(8.093409548935148e-07), '0100101000100110010010000': np.float64(4.2843525768921185e-07), '0100000111111100111001100': np.float64(5.4788880664882446e-08), '0010101100000111100000000': np.float64(6.350945339007582e-09), '0101111100011000101100111': np.float64(1.6189384309546533e-09), '0111111001101110011000111': np.float64(4.231070771736262e-07), '1111011100010111101010110': np.float64(8.809840523521098e-08), '1001000111111111111000000': np.float64(2.2649917694883763e-09), '1110010011010011000010000': np.float64(2.9400450442747213e-06), '1001101011101101100000000': np.float64(3.564993949972801e-07), '1010101100011110001110111': np.float64(2.0353895972901936e-07), '0111010110011101010101010': np.float64(9.272342830457222e-08), '1000110100110010010011101': np.float64(1.0554195999421195e-07), '0010010001010110000011100': np.float64(4.926758473909799e-08), '1110110010010000110100101': np.float64(3.6907560113556863e-07), '1110010110011111101110101': np.float64(1.0912695706428317e-07), '0101001000011111100010111': np.float64(2.3749237133846456e-07), '0111011101111111101101111': np.float64(4.4313847930510314e-07), '0011111111100001011000010': np.float64(2.879407202649115e-09), '0110001100010001000101011': np.float64(2.2970898298927726e-08), '0100101101000010101101111': np.float64(1.2403396951734157e-06), '0101010010010010000111001': np.float64(3.2580433289301146e-06), '0000000011111101101011110': np.float64(7.551781140278402e-07), '0000100100100001101011010': np.float64(7.342186380985404e-06), '1010111100000101010101110': np.float64(2.0803417549284257e-07), '0110011011000001010101100': np.float64(6.536548485677532e-09), '1111001101101001111111111': np.float64(6.662093312934175e-08), '0011001011011110000010000': np.float64(9.152885414596061e-07), '1001100100010000010010000': np.float64(1.1822334160129414e-06), '1011111101100001111110011': np.float64(1.2742285932601216e-05), '0011101011101010010111001': np.float64(8.001218354241628e-07), '1111100010010010101100110': np.float64(9.3255563882172e-07), '0101100001011110010100010': np.float64(1.0626891034969866e-06), '0100101110010011000011000': np.float64(1.5652631724013031e-07), '1011001011101011101000101': np.float64(1.025423997199104e-06), '0000101111101100010100010': np.float64(6.003473256257238e-07), '1101000010100000011000110': np.float64(1.085658621941692e-06), '1100110110101111110010001': np.float64(4.659708973943698e-09), '1100001011010011010111101': np.float64(5.295910969809775e-07), '1000101011010011111001101': np.float64(3.6779048150728553e-07), '1100001001010101110010100': np.float64(4.92126869361337e-09), '0110101101110000001100110': np.float64(7.741427610734003e-08), '0111110111010101101000110': np.float64(2.399267980010788e-07), '1110010111101010101000101': np.float64(5.667518009732859e-07), '1000110010011010000110000': np.float64(2.084260073059544e-06), '1001011101010100010010001': np.float64(2.6571545078690544e-08), '0100110100010001000101000': np.float64(3.079779061777933e-08), '0000100010100010010100010': np.float64(6.929792821857359e-06), '1010110110011100011000101': np.float64(4.630180771716902e-08), '0000110010100001101001010': np.float64(9.116530823953186e-07), '0100101000111100110010111': np.float64(1.3249324853060952e-07), '1011101011011110000001011': np.float64(1.1907229585114808e-06), '1010011111111101011110110': np.float64(4.56149319722231e-09), '1100110111100010000011110': np.float64(5.257006816629565e-07), '0100110100100000101100111': np.float64(5.776773429581827e-07), '1110111100011110001011101': np.float64(1.9990466754187882e-07), '0011001100110011101000110': np.float64(4.3593985418091113e-08), '1000110000111001101011101': np.float64(3.6389888110895923e-07), '0100101101000010101011111': np.float64(7.622087666902094e-07), '0101010000000110010101001': np.float64(2.994365575033701e-07), '0110111101101010010111000': np.float64(3.147129865281865e-08), '0010010010100010101010101': np.float64(3.7955795123562516e-07), '1100001000101011101100101': np.float64(2.483089067956661e-07), '1010101000011101101101011': np.float64(3.09860035910458e-07), '1000010111001110010011110': np.float64(4.718847603022761e-08), '0000010001110001100000011': np.float64(1.618347020880399e-08), '0011010000101101111101100': np.float64(2.307864975407703e-07), '1011010000100011000010000': np.float64(1.604607088007449e-07), '1110100011001010000110000': np.float64(5.735844296703352e-07), '1101000111110111001100101': np.float64(7.018930935740711e-10), '0110001101010001110011010': np.float64(1.426676825693035e-07), '1001011000100110101010101': np.float64(1.3658924161666397e-07), '0000110010100000000110111': np.float64(7.127088059066376e-07), '1111001000001000111001000': np.float64(9.382957208112125e-08), '0011100011100010010100001': np.float64(1.5633352068001958e-07), '0111111110010000001010111': np.float64(6.24916842986095e-07), '1011100100101101101011110': np.float64(5.277053774965453e-08), '0111001011100110001000001': np.float64(2.706733332328552e-07), '0111001000110000111101111': np.float64(8.740290927759748e-07), '1101111011001110000000000': np.float64(1.1390459236761492e-07), '0010111101011101010010100': np.float64(2.851294124498577e-06), '1100100010010001111101000': np.float64(8.627935077391886e-06), '0101011111100010101010100': np.float64(6.937375172146743e-08), '1110100111011111111000001': np.float64(2.517698486277455e-07), '1100000010101100001000110': np.float64(2.1300563007206324e-07), '0111011011010001011001011': np.float64(5.8246768046662493e-08), '0111000000100001101010011': np.float64(3.123499806377365e-07), '1001111100110001111101000': np.float64(4.777318918055056e-07), '0011010010100101010101100': np.float64(5.494449035514909e-07), '1010111011011001101100011': np.float64(1.1425290362328705e-06), '1001000100001110000110000': np.float64(3.266349767666358e-08), '1011011110100101010100101': np.float64(5.947082800379298e-07), '0101100010011101111111001': np.float64(3.5373491729489938e-06), '0000100000101110000001000': np.float64(3.041450478019988e-07), '1110010111000010111001000': np.float64(2.0041305331234744e-07), '1011011101101101111011101': np.float64(2.701698118830075e-07), '0111100010010001111001011': np.float64(1.3996687152906713e-06)}\n", + "\n" + ] + }, + { + "ename": "NotImplementedError", + "evalue": "Tensor network simulation cannot be used to reconstruct statevector for >= 20 .", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_44825/1053899561.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"\u001b[0m\u001b[0;34mProbabilities:\\n \u001b[0m\u001b[0;34m{\u001b[0m\u001b[0moutcome\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprobabilities\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\\n\u001b[0m\u001b[0;34m\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"\u001b[0m\u001b[0;34mState:\\n \u001b[0m\u001b[0;34m{\u001b[0m\u001b[0moutcome\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\\n\u001b[0m\u001b[0;34m\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m~/repo/qibotn/src/qibotn/result.py\u001b[0m in \u001b[0;36mstate\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnqubits\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatevector\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 63\u001b[0;31m raise_error(\n\u001b[0m\u001b[1;32m 64\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;34mf\"\u001b[0m\u001b[0;34mTensor network simulation cannot be used to reconstruct statevector for >= 20 .\u001b[0m\u001b[0;34m\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/main_env/lib/python3.12/site-packages/qibo/config.py\u001b[0m in \u001b[0;36mraise_error\u001b[0;34m(exception, message)\u001b[0m\n\u001b[1;32m 44\u001b[0m \"\"\"\n\u001b[1;32m 45\u001b[0m \u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merror\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 46\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mexception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 47\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNotImplementedError\u001b[0m: Tensor network simulation cannot be used to reconstruct statevector for >= 20 ." + ] + } + ], + "source": [ + "print(f\"Probabilities:\\n {outcome.probabilities()}\\n\")\n", + "print(f\"State:\\n {outcome.state()}\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "dd84f1f3-7aa5-4ad1-ae09-81e0aff75b5b", + "metadata": {}, + "source": [ + "### Compute expectation values\n", + "\n", + "Another important feature of this backend is the `expectation` function. In fact, we can compute expectation values of given observables thorugh a Qibo-friendly interface.\n", + "\n", + "---\n", + "\n", + "Let's start by importing some symbols, thanks to which we can build our observable." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "37385485-e8a3-4ab0-ad44-bcc4e9da24ca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 : ─RY─RZ─o───────────────────────────────────────────────X─RY─RZ─o────── ...\n", + "1 : ─RY─RZ─X─o─────────────────────────────────────────────|─RY─RZ─X─o──── ...\n", + "2 : ─RY─RZ───X─o───────────────────────────────────────────|─RY─RZ───X─o── ...\n", + "3 : ─RY─RZ─────X─o─────────────────────────────────────────|─RY─RZ─────X─o ...\n", + "4 : ─RY─RZ───────X─o───────────────────────────────────────|─RY─RZ───────X ...\n", + "5 : ─RY─RZ─────────X─o─────────────────────────────────────|─RY─RZ──────── ...\n", + "6 : ─RY─RZ───────────X─o───────────────────────────────────|─RY─RZ──────── ...\n", + "7 : ─RY─RZ─────────────X─o─────────────────────────────────|─RY─RZ──────── ...\n", + "8 : ─RY─RZ───────────────X─o───────────────────────────────|─RY─RZ──────── ...\n", + "9 : ─RY─RZ─────────────────X─o─────────────────────────────|─RY─RZ──────── ...\n", + "10: ─RY─RZ───────────────────X─o───────────────────────────|─RY─RZ──────── ...\n", + "11: ─RY─RZ─────────────────────X─o─────────────────────────|─RY─RZ──────── ...\n", + "12: ─RY─RZ───────────────────────X─o───────────────────────|─RY─RZ──────── ...\n", + "13: ─RY─RZ─────────────────────────X─o─────────────────────|─RY─RZ──────── ...\n", + "14: ─RY─RZ───────────────────────────X─o───────────────────|─RY─RZ──────── ...\n", + "15: ─RY─RZ─────────────────────────────X─o─────────────────|─RY─RZ──────── ...\n", + "16: ─RY─RZ───────────────────────────────X─o───────────────|─RY─RZ──────── ...\n", + "17: ─RY─RZ─────────────────────────────────X─o─────────────|─RY─RZ──────── ...\n", + "18: ─RY─RZ───────────────────────────────────X─o───────────|─RY─RZ──────── ...\n", + "19: ─RY─RZ─────────────────────────────────────X─o─────────|─RY─RZ──────── ...\n", + "20: ─RY─RZ───────────────────────────────────────X─o───────|─RY─RZ──────── ...\n", + "21: ─RY─RZ─────────────────────────────────────────X─o─────|─RY─RZ──────── ...\n", + "22: ─RY─RZ───────────────────────────────────────────X─o───|─RY─RZ──────── ...\n", + "23: ─RY─RZ─────────────────────────────────────────────X─o─|─RY─RZ──────── ...\n", + "24: ─RY─RZ───────────────────────────────────────────────X─o─RY─RZ──────── ...\n", + "\n", + "0 : ... ─────────────────────────────────────────X─RY─RZ─o──────────────────── ...\n", + "1 : ... ─────────────────────────────────────────|─RY─RZ─X─o────────────────── ...\n", + "2 : ... ─────────────────────────────────────────|─RY─RZ───X─o──────────────── ...\n", + "3 : ... ─────────────────────────────────────────|─RY─RZ─────X─o────────────── ...\n", + "4 : ... ─o───────────────────────────────────────|─RY─RZ───────X─o──────────── ...\n", + "5 : ... ─X─o─────────────────────────────────────|─RY─RZ─────────X─o────────── ...\n", + "6 : ... ───X─o───────────────────────────────────|─RY─RZ───────────X─o──────── ...\n", + "7 : ... ─────X─o─────────────────────────────────|─RY─RZ─────────────X─o────── ...\n", + "8 : ... ───────X─o───────────────────────────────|─RY─RZ───────────────X─o──── ...\n", + "9 : ... ─────────X─o─────────────────────────────|─RY─RZ─────────────────X─o── ...\n", + "10: ... ───────────X─o───────────────────────────|─RY─RZ───────────────────X─o ...\n", + "11: ... ─────────────X─o─────────────────────────|─RY─RZ─────────────────────X ...\n", + "12: ... ───────────────X─o───────────────────────|─RY─RZ────────────────────── ...\n", + "13: ... ─────────────────X─o─────────────────────|─RY─RZ────────────────────── ...\n", + "14: ... ───────────────────X─o───────────────────|─RY─RZ────────────────────── ...\n", + "15: ... ─────────────────────X─o─────────────────|─RY─RZ────────────────────── ...\n", + "16: ... ───────────────────────X─o───────────────|─RY─RZ────────────────────── ...\n", + "17: ... ─────────────────────────X─o─────────────|─RY─RZ────────────────────── ...\n", + "18: ... ───────────────────────────X─o───────────|─RY─RZ────────────────────── ...\n", + "19: ... ─────────────────────────────X─o─────────|─RY─RZ────────────────────── ...\n", + "20: ... ───────────────────────────────X─o───────|─RY─RZ────────────────────── ...\n", + "21: ... ─────────────────────────────────X─o─────|─RY─RZ────────────────────── ...\n", + "22: ... ───────────────────────────────────X─o───|─RY─RZ────────────────────── ...\n", + "23: ... ─────────────────────────────────────X─o─|─RY─RZ────────────────────── ...\n", + "24: ... ───────────────────────────────────────X─o─RY─RZ────────────────────── ...\n", + "\n", + "0 : ... ───────────────────────────X─M─\n", + "1 : ... ───────────────────────────|─M─\n", + "2 : ... ───────────────────────────|─M─\n", + "3 : ... ───────────────────────────|─M─\n", + "4 : ... ───────────────────────────|─M─\n", + "5 : ... ───────────────────────────|─M─\n", + "6 : ... ───────────────────────────|─M─\n", + "7 : ... ───────────────────────────|─M─\n", + "8 : ... ───────────────────────────|─M─\n", + "9 : ... ───────────────────────────|─M─\n", + "10: ... ───────────────────────────|─M─\n", + "11: ... ─o─────────────────────────|─M─\n", + "12: ... ─X─o───────────────────────|─M─\n", + "13: ... ───X─o─────────────────────|─M─\n", + "14: ... ─────X─o───────────────────|─M─\n", + "15: ... ───────X─o─────────────────|─M─\n", + "16: ... ─────────X─o───────────────|─M─\n", + "17: ... ───────────X─o─────────────|─M─\n", + "18: ... ─────────────X─o───────────|─M─\n", + "19: ... ───────────────X─o─────────|─M─\n", + "20: ... ─────────────────X─o───────|─M─\n", + "21: ... ───────────────────X─o─────|─M─\n", + "22: ... ─────────────────────X─o───|─M─\n", + "23: ... ───────────────────────X─o─|─M─\n", + "24: ... ─────────────────────────X─o─M─\n" + ] + } + ], + "source": [ + "# We are going to compute the expval of an Hamiltonian\n", + "# On the state prepared by the following circuit\n", + "circuit.draw()\n", + "\n", + "circuit.set_parameters(\n", + " np.random.randn(len(circuit.get_parameters()))\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "ddecc910-7804-4199-8577-a7db38a16db8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle 3.6 X_{0} + 3.6 X_{1} + 3.6 X_{10} + 3.6 X_{11} + 3.6 X_{12} + 3.6 X_{13} + 3.6 X_{14} + 3.6 X_{15} + 3.6 X_{16} + 3.6 X_{17} + 3.6 X_{18} + 3.6 X_{19} + 3.6 X_{2} + 3.6 X_{20} + 3.6 X_{21} + 3.6 X_{22} + 3.6 X_{23} + 3.6 X_{24} + 3.6 X_{3} + 3.6 X_{4} + 3.6 X_{5} + 3.6 X_{6} + 3.6 X_{7} + 3.6 X_{8} + 3.6 X_{9} + 8.4 Z_{0} + 8.4 Z_{1} + 8.4 Z_{10} + 8.4 Z_{11} + 8.4 Z_{12} + 8.4 Z_{13} + 8.4 Z_{14} + 8.4 Z_{15} + 8.4 Z_{16} + 8.4 Z_{17} + 8.4 Z_{18} + 8.4 Z_{19} + 8.4 Z_{2} + 8.4 Z_{20} + 8.4 Z_{21} + 8.4 Z_{22} + 8.4 Z_{23} + 8.4 Z_{24} + 8.4 Z_{3} + 8.4 Z_{4} + 8.4 Z_{5} + 8.4 Z_{6} + 8.4 Z_{7} + 8.4 Z_{8} + 8.4 Z_{9}$" + ], + "text/plain": [ + "3.6*X0 + 3.6*X1 + 3.6*X10 + 3.6*X11 + 3.6*X12 + 3.6*X13 + 3.6*X14 + 3.6*X15 + 3.6*X16 + 3.6*X17 + 3.6*X18 + 3.6*X19 + 3.6*X2 + 3.6*X20 + 3.6*X21 + 3.6*X22 + 3.6*X23 + 3.6*X24 + 3.6*X3 + 3.6*X4 + 3.6*X5 + 3.6*X6 + 3.6*X7 + 3.6*X8 + 3.6*X9 + 8.4*Z0 + 8.4*Z1 + 8.4*Z10 + 8.4*Z11 + 8.4*Z12 + 8.4*Z13 + 8.4*Z14 + 8.4*Z15 + 8.4*Z16 + 8.4*Z17 + 8.4*Z18 + 8.4*Z19 + 8.4*Z2 + 8.4*Z20 + 8.4*Z21 + 8.4*Z22 + 8.4*Z23 + 8.4*Z24 + 8.4*Z3 + 8.4*Z4 + 8.4*Z5 + 8.4*Z6 + 8.4*Z7 + 8.4*Z8 + 8.4*Z9" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from qibo.symbols import Z, X, I\n", + "\n", + "form = 0\n", + "for i in range(nqubits):\n", + " for _ in range(12):\n", + " form += 0.7 * Z(i)\n", + " form += 0.3 * X(i)\n", + "\n", + "hamiltonian = hamiltonians.SymbolicHamiltonian(form)\n", + "\n", + "hamiltonian.form\n" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "163b70a3-814a-4a62-a98a-2ffca933a544", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/mattia/main_env/lib/python3.12/site-packages/quimb/tensor/circuit.py:215: SyntaxWarning: Unsupported operation ignored: creg\n", + " warnings.warn(\n", + "/home/mattia/main_env/lib/python3.12/site-packages/quimb/tensor/circuit.py:215: SyntaxWarning: Unsupported operation ignored: measure\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Expectation value: -0.9053105195111129\n", + "Elapsed time: 20.0296 seconds\n" + ] + } + ], + "source": [ + "start = time.time()\n", + "expval = quimb_backend.expectation(\n", + " circuit=circuit,\n", + " observable=hamiltonian,\n", + ")\n", + "elapsed = time.time() - start\n", + "print(f\"Expectation value: {expval}\")\n", + "print(f\"Elapsed time: {elapsed:.4f} seconds\")" + ] + }, + { + "cell_type": "markdown", + "id": "90663e28", + "metadata": {}, + "source": [ + "Try with Qibo (which is by default using the Qibojit backend)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "e2d05707", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Expectation value: -0.9053096709847108\n", + "Elapsed time: 319.1915 seconds\n" + ] + } + ], + "source": [ + "start = time.time()\n", + "result = hamiltonian.expectation(circuit().state())\n", + "elapsed = time.time() - start\n", + "print(f\"Expectation value: {result}\")\n", + "print(f\"Elapsed time: {elapsed:.4f} seconds\")" + ] + }, + { + "cell_type": "markdown", + "id": "94df291c-9ddc-4b2e-8442-5fca00784bd8", + "metadata": {}, + "source": [ + "They match! 🥳" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "main_env", + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}