From 271a44576dd4837e73efdce7b8597f30cc69abf2 Mon Sep 17 00:00:00 2001 From: Mattia Robbiano Date: Sat, 20 Sep 2025 17:00:07 +0200 Subject: [PATCH] fixing parametrization flag and updating relevant test scripts --- examples/quimb_intro/quimb_introduction.ipynb | 713 +++++++----------- examples/quimb_intro/test.py | 14 +- src/qibotn/backends/quimb.py | 46 +- 3 files changed, 284 insertions(+), 489 deletions(-) diff --git a/examples/quimb_intro/quimb_introduction.ipynb b/examples/quimb_intro/quimb_introduction.ipynb index 6153e90..266b49d 100644 --- a/examples/quimb_intro/quimb_introduction.ipynb +++ b/examples/quimb_intro/quimb_introduction.ipynb @@ -19,9 +19,9 @@ "source": [ "import time\n", "import numpy as np\n", - "from scipy import stats\n", + "# from scipy import stats\n", "\n", - "import qibo\n", + "# import qibo\n", "from qibo import Circuit, gates, hamiltonians\n", "from qibo.backends import construct_backend" ] @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "b0a1da82", "metadata": {}, "outputs": [], @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "64162116-1555-4a68-811c-01593739d622", "metadata": {}, "outputs": [], @@ -62,7 +62,7 @@ "quimb_backend = construct_backend(backend=\"qibotn\", platform=\"quimb\")\n", "\n", "# set number of qubits\n", - "nqubits = 25\n", + "nqubits = 4\n", "\n", "# set numpy random seed\n", "np.random.seed(42)\n", @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 3, "id": "4a22a172-f50d-411d-afa3-fa61937c7b3a", "metadata": {}, "outputs": [], @@ -102,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 4, "id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1", "metadata": {}, "outputs": [ @@ -110,83 +110,10 @@ "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" + "0: ─RY─RZ─o─────X─RY─RZ─o─────X─RY─RZ─o─────X─M─\n", + "1: ─RY─RZ─X─o───|─RY─RZ─X─o───|─RY─RZ─X─o───|─M─\n", + "2: ─RY─RZ───X─o─|─RY─RZ───X─o─|─RY─RZ───X─o─|─M─\n", + "3: ─RY─RZ─────X─o─RY─RZ─────X─o─RY─RZ─────X─o─M─\n" ] } ], @@ -197,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 6, "id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98", "metadata": {}, "outputs": [], @@ -220,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 7, "id": "2ee03e94-d794-4a51-9e76-01e8d8a259ba", "metadata": {}, "outputs": [], @@ -248,235 +175,84 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 8, "id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf", "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", + "/home/mattia/main_env/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + }, { "data": { "text/plain": [ - "{'nqubits': 25,\n", + "{'nqubits': 4,\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", + " 'measures': Counter({'0011': 8,\n", + " '0010': 12,\n", + " '0111': 4,\n", + " '1011': 7,\n", + " '0000': 8,\n", + " '1110': 14,\n", + " '0101': 4,\n", + " '1010': 4,\n", + " '1000': 14,\n", + " '1111': 8,\n", + " '0100': 6,\n", + " '1101': 8,\n", + " '1100': 1,\n", + " '0110': 2}),\n", + " 'measured_probabilities': {'1110': np.float64(0.07174919872959985),\n", + " '1000': np.float64(0.11330883548333587),\n", + " '0010': np.float64(0.09466860481989385),\n", + " '0011': np.float64(0.07571277233522114),\n", + " '0000': np.float64(0.08390937969317269),\n", + " '1111': np.float64(0.10184806171791962),\n", + " '1101': np.float64(0.12331159869893256),\n", + " '1011': np.float64(0.053499396925872744),\n", + " '0100': np.float64(0.07142939529687138),\n", + " '0111': np.float64(0.04029185074729259),\n", + " '0101': np.float64(0.05622305772698622),\n", + " '1010': np.float64(0.03872758515126756),\n", + " '0110': np.float64(0.05146064807369214),\n", + " '1100': np.float64(0.013605984872668404)},\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)}" + " 'statevector': Array([[ 0.08809624-0.27594998j],\n", + " [-0.05174781+0.04471217j],\n", + " [ 0.00470147+0.30764672j],\n", + " [-0.27208942+0.0409893j ],\n", + " [ 0.18807822+0.18988408j],\n", + " [ 0.2237706 +0.07842042j],\n", + " [-0.18900308+0.12545314j],\n", + " [ 0.17105256-0.10503749j],\n", + " [ 0.24859734-0.22695419j],\n", + " [-0.0411739 -0.06230037j],\n", + " [ 0.17371392-0.09247189j],\n", + " [-0.22748128+0.0418529j ],\n", + " [ 0.09444095+0.06846087j],\n", + " [-0.21784972-0.2754144j ],\n", + " [-0.17359753+0.20399286j],\n", + " [-0.01729754-0.31866732j]], dtype=complex64)}" ] }, - "execution_count": 23, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Simple execution (defaults)\n", + "# # Simple execution (defaults)\n", "outcome = quimb_backend.execute_circuit(circuit=circuit, nshots=100, return_array=True)\n", "\n", - "# Print outcome\n", + "# # Print outcome\n", "vars(outcome)" ] }, @@ -492,38 +268,36 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 9, "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", + " {'1110': np.float64(0.07174919872959985), '1000': np.float64(0.11330883548333587), '0010': np.float64(0.09466860481989385), '0011': np.float64(0.07571277233522114), '0000': np.float64(0.08390937969317269), '1111': np.float64(0.10184806171791962), '1101': np.float64(0.12331159869893256), '1011': np.float64(0.053499396925872744), '0100': np.float64(0.07142939529687138), '0111': np.float64(0.04029185074729259), '0101': np.float64(0.05622305772698622), '1010': np.float64(0.03872758515126756), '0110': np.float64(0.05146064807369214), '1100': np.float64(0.013605984872668404)}\n", + "\n", + "State:\n", + " [[ 0.08809624-0.27594998j]\n", + " [-0.05174781+0.04471217j]\n", + " [ 0.00470147+0.30764672j]\n", + " [-0.27208942+0.0409893j ]\n", + " [ 0.18807822+0.18988408j]\n", + " [ 0.2237706 +0.07842042j]\n", + " [-0.18900308+0.12545314j]\n", + " [ 0.17105256-0.10503749j]\n", + " [ 0.24859734-0.22695419j]\n", + " [-0.0411739 -0.06230037j]\n", + " [ 0.17371392-0.09247189j]\n", + " [-0.22748128+0.0418529j ]\n", + " [ 0.09444095+0.06846087j]\n", + " [-0.21784972-0.2754144j ]\n", + " [-0.17359753+0.20399286j]\n", + " [-0.01729754-0.31866732j]]\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": [ @@ -547,7 +321,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 10, "id": "37385485-e8a3-4ab0-ad44-bcc4e9da24ca", "metadata": {}, "outputs": [ @@ -555,83 +329,10 @@ "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" + "0: ─RY─RZ─o─────X─RY─RZ─o─────X─RY─RZ─o─────X─M─\n", + "1: ─RY─RZ─X─o───|─RY─RZ─X─o───|─RY─RZ─X─o───|─M─\n", + "2: ─RY─RZ───X─o─|─RY─RZ───X─o─|─RY─RZ───X─o─|─M─\n", + "3: ─RY─RZ─────X─o─RY─RZ─────X─o─RY─RZ─────X─o─M─\n" ] } ], @@ -647,60 +348,53 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 11, "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" + "[Qibo 0.2.20|INFO|2025-09-20 16:43:42]: Using qibojit (numba) backend on /CPU:0\n" ] }, + { + "data": { + "text/latex": [ + "$\\displaystyle - 1.5 X_{0} Z_{2} + 0.5 Z_{0} Z_{1} + Z_{3}$" + ], + "text/plain": [ + "-1.5*X0*Z2 + 0.5*Z0*Z1 + Z3" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from qibo.symbols import Z, X, I\n", + "# We can create a symbolic Hamiltonian\n", + "form = 0.5 * Z(0) * Z(1) +- 1.5 * X(0) * Z(2) + Z(3)\n", + "hamiltonian = hamiltonians.SymbolicHamiltonian(form)\n", + "\n", + "# Let's show it\n", + "hamiltonian.form" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "163b70a3-814a-4a62-a98a-2ffca933a544", + "metadata": {}, + "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Expectation value: -0.9053105195111129\n", - "Elapsed time: 20.0296 seconds\n" + "Expectation value: 0.7143489122390747\n", + "Elapsed time: 12.4550 seconds\n" ] } ], @@ -725,7 +419,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 14, "id": "e2d05707", "metadata": {}, "outputs": [ @@ -733,8 +427,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Expectation value: -0.9053096709847108\n", - "Elapsed time: 319.1915 seconds\n" + "Expectation value: 0.7143570920618565\n", + "Elapsed time: 0.5871 seconds\n" ] } ], @@ -753,6 +447,141 @@ "source": [ "They match! 🥳" ] + }, + { + "cell_type": "markdown", + "id": "d2d119fc", + "metadata": {}, + "source": [ + "### Derivative of the extimation function" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "8df55c5f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0: ─RY─RZ─o─────X─RY─RZ─o─────X─RY─RZ─o─────X─M─\n", + "1: ─RY─RZ─X─o───|─RY─RZ─X─o───|─RY─RZ─X─o───|─M─\n", + "2: ─RY─RZ───X─o─|─RY─RZ───X─o─|─RY─RZ───X─o─|─M─\n", + "3: ─RY─RZ─────X─o─RY─RZ─────X─o─RY─RZ─────X─o─M─\n" + ] + } + ], + "source": [ + "# grad of this circuit returning nan for some reason...\n", + "\n", + "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\n", + "\n", + "circuit = build_circuit(nqubits=nqubits, nlayers=3)\n", + "circuit.draw()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b02de56b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0: ─RY─RZ─RX─o─x─────────RY─RZ─RX─o─x─────────RY─RZ─RX─o─x─────────M─\n", + "1: ─RY─RZ─RX─X─x─o─x─────RY─RZ─RX─X─x─o─x─────RY─RZ─RX─X─x─o─x─────M─\n", + "2: ─RY─RZ─RX─────X─x─o─x─RY─RZ─RX─────X─x─o─x─RY─RZ─RX─────X─x─o─x─M─\n", + "3: ─RY─RZ─RX─────────X─x─RY─RZ─RX─────────X─x─RY─RZ─RX─────────X─x─M─\n" + ] + } + ], + "source": [ + "def build_circuit(nqubits, nlayers):\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.RX(q=q, theta=0.))\n", + " for q in range(nqubits - 1):\n", + " circ.add(gates.CNOT(q, q + 1))\n", + " circ.add(gates.SWAP(q, q + 1))\n", + " circ.add(gates.M(*range(nqubits)))\n", + " return circ\n", + "\n", + "circuit = build_circuit(nqubits=nqubits, nlayers=3)\n", + "circuit.draw()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "b803250f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Array(1.4999985, dtype=float32)" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "quimb_backend.expectation(\n", + " circuit=circuit, \n", + " observable=hamiltonian,\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "0943482e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(Array(0.4465402, dtype=float32), Array([-1.5755819e-01, 9.7801067e-02, -1.2350259e-01, 1.3670625e-01,\n", + " 3.6954228e-03, -1.7437905e-02, 2.7746204e-01, -1.0357879e-01,\n", + " 1.1504190e-01, -4.5175910e-02, -4.8447326e-02, 1.4743687e-01,\n", + " -3.0708680e-01, 2.0652822e-01, 1.9298886e-01, 5.1306009e-02,\n", + " -3.3362946e-01, -7.5548244e-01, -3.0034758e-02, -5.2868712e-01,\n", + " 4.8458660e-01, -2.9802322e-08, 8.0767423e-02, 0.0000000e+00], dtype=float32))\n" + ] + } + ], + "source": [ + "import jax\n", + "\n", + "def f(params):\n", + " circuit.set_parameters(params)\n", + " return quimb_backend.expectation(\n", + " circuit=circuit,\n", + " observable=hamiltonian,\n", + " )\n", + "\n", + "parameters = np.random.uniform(-np.pi, np.pi, size=len(circuit.get_parameters()))\n", + "print(jax.value_and_grad(f)(parameters))\n" + ] } ], "metadata": { diff --git a/examples/quimb_intro/test.py b/examples/quimb_intro/test.py index da499f2..2eccd91 100644 --- a/examples/quimb_intro/test.py +++ b/examples/quimb_intro/test.py @@ -24,21 +24,23 @@ hamiltonian = hamiltonians.SymbolicHamiltonian(form) # define circuit def build_circuit(nqubits, nlayers): - """Construct a Qibo parametric quantum circuit.""" + """Construct a more complex Qibo parametric quantum circuit without CNOT gates.""" circ = Circuit(nqubits) - for _ in range(nlayers): + for layer in range(nlayers): for q in range(nqubits): circ.add(gates.RY(q=q, theta=0.)) circ.add(gates.RZ(q=q, theta=0.)) - [circ.add(gates.CZ(q % nqubits, (q + 1) % nqubits)) for q in range(nqubits)] + circ.add(gates.RX(q=q, theta=0.)) + # Add controlled rotations and SWAPs for entanglement + for q in range(nqubits - 1): + circ.add(gates.CNOT(q, q + 1)) + circ.add(gates.SWAP(q, q + 1)) circ.add(gates.M(*range(nqubits))) return circ -nqubits = 4 +nqubits = 6 circuit = build_circuit(nqubits=nqubits, nlayers=3) -quimb_circuit = quimb_backend._qibo_circuit_to_quimb(circuit) - def f(params): circuit.set_parameters(params) return quimb_backend.expectation( diff --git a/src/qibotn/backends/quimb.py b/src/qibotn/backends/quimb.py index 3f09859..e4ebd5d 100644 --- a/src/qibotn/backends/quimb.py +++ b/src/qibotn/backends/quimb.py @@ -376,45 +376,6 @@ class QuimbBackend(QibotnBackend, NumpyBackend): return A_new, B_new, C_new - # def _qibo_circuit_to_quimb(self, qibo_circ, quimb_circuit_type=qtn.Circuit, **circuit_kwargs): - # """ - # Convert a Qibo Circuit to a Quimb Circuit. - - # Parameters - # ---------- - # qibo_circ : qibo.models.circuit.Circuit - # The circuit to convert. - # quimb_circuit_type : type - # The Quimb circuit class to use (Circuit, CircuitMPS, etc). - # circuit_kwargs : dict - # Extra arguments to pass to the Quimb circuit constructor. - - # Returns - # ------- - # circ : quimb.tensor.circuit.Circuit - # The converted circuit. - # """ - # nqubits = qibo_circ.nqubits - # quimb_gates = [] - # circ = quimb_circuit_type(nqubits, **circuit_kwargs) - - # for gate in qibo_circ.queue: - # gname = getattr(gate, "name", None) - # qname = GATE_MAP.get(gname, None) - # if qname is None: - # continue # skip measurements and unknown gates - - # # Handle parametrized gates (Qibo: .parameters, Quimb: expects flat tuple) - # params = getattr(gate, "parameters", ()) - # qubits = getattr(gate, "qubits", ()) - - # # Quimb expects (*params, *qubits) - # gate_spec = (qname,) + tuple(params) + tuple(qubits) - # quimb_gates.append(gate_spec) - - # circ.apply_gates(quimb_gates) - # return circ - def _qibo_circuit_to_quimb(self, qibo_circ, quimb_circuit_type=qtn.Circuit, **circuit_kwargs): """ Convert a Qibo Circuit to a Quimb Circuit. @@ -446,8 +407,11 @@ class QuimbBackend(QibotnBackend, NumpyBackend): qubits = getattr(gate, "qubits", ()) # Check if the gate is parametrized - is_parametrized = isinstance(gate, ParametrizedGate) - + is_parametrized = ( + isinstance(gate, ParametrizedGate) + and getattr(gate, "trainable", True) + ) + # import pdb; pdb.set_trace() if is_parametrized: circ.apply_gate( qname,