From 386891ee1aaa28a8deb7eeb1875461188d985af7 Mon Sep 17 00:00:00 2001 From: Mattia Robbiano Date: Tue, 23 Sep 2025 00:37:11 +0200 Subject: [PATCH] updaged examples --- examples/quimb_intro/quimb_introduction.ipynb | 217 +++++++++--------- examples/quimb_intro/test.py | 52 ----- 2 files changed, 106 insertions(+), 163 deletions(-) delete mode 100644 examples/quimb_intro/test.py diff --git a/examples/quimb_intro/quimb_introduction.ipynb b/examples/quimb_intro/quimb_introduction.ipynb index 266b49d..73a831b 100644 --- a/examples/quimb_intro/quimb_introduction.ipynb +++ b/examples/quimb_intro/quimb_introduction.ipynb @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "b0a1da82", "metadata": {}, "outputs": [], @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 23, "id": "64162116-1555-4a68-811c-01593739d622", "metadata": {}, "outputs": [], @@ -69,7 +69,7 @@ "\n", "quimb_backend.setup_backend_specifics(\n", " qimb_backend=\"jax\", \n", - " optimizer='auto-hq'\n", + " contractions_optimizer=ctg_opt\n", " )" ] }, @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "id": "4a22a172-f50d-411d-afa3-fa61937c7b3a", "metadata": {}, "outputs": [], @@ -102,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1", "metadata": {}, "outputs": [ @@ -124,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98", "metadata": {}, "outputs": [], @@ -147,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "id": "2ee03e94-d794-4a51-9e76-01e8d8a259ba", "metadata": {}, "outputs": [], @@ -175,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf", "metadata": {}, "outputs": [ @@ -196,33 +196,33 @@ "text/plain": [ "{'nqubits': 4,\n", " 'backend': qibotn (quimb),\n", - " 'measures': Counter({'0011': 8,\n", + " 'measures': Counter({'1000': 14,\n", + " '1011': 9,\n", + " '0101': 5,\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", + " '1101': 19,\n", + " '0011': 6,\n", + " '0100': 3,\n", + " '1111': 9,\n", + " '0111': 4,\n", + " '0110': 4,\n", + " '1110': 2,\n", + " '1100': 1}),\n", + " 'measured_probabilities': {'1101': np.float64(0.12331159869893256),\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", + " '1111': np.float64(0.10184806171791962),\n", + " '0000': np.float64(0.08390937969317269),\n", + " '0011': np.float64(0.07571277233522114),\n", " '0101': np.float64(0.05622305772698622),\n", " '1010': np.float64(0.03872758515126756),\n", + " '0111': np.float64(0.04029185074729259),\n", " '0110': np.float64(0.05146064807369214),\n", + " '0100': np.float64(0.07142939529687138),\n", + " '1110': np.float64(0.07174919872959985),\n", " '1100': np.float64(0.013605984872668404)},\n", " 'prob_type': 'default',\n", " 'statevector': Array([[ 0.08809624-0.27594998j],\n", @@ -243,7 +243,7 @@ " [-0.01729754-0.31866732j]], dtype=complex64)}" ] }, - "execution_count": 8, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -268,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "id": "c0443efc-21ef-4ed5-9cf4-785d204a1881", "metadata": {}, "outputs": [ @@ -277,7 +277,7 @@ "output_type": "stream", "text": [ "Probabilities:\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", + " {'1101': np.float64(0.12331159869893256), '1000': np.float64(0.11330883548333587), '0010': np.float64(0.09466860481989385), '1011': np.float64(0.053499396925872744), '1111': np.float64(0.10184806171791962), '0000': np.float64(0.08390937969317269), '0011': np.float64(0.07571277233522114), '0101': np.float64(0.05622305772698622), '1010': np.float64(0.03872758515126756), '0111': np.float64(0.04029185074729259), '0110': np.float64(0.05146064807369214), '0100': np.float64(0.07142939529687138), '1110': np.float64(0.07174919872959985), '1100': np.float64(0.013605984872668404)}\n", "\n", "State:\n", " [[ 0.08809624-0.27594998j]\n", @@ -321,7 +321,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "id": "37385485-e8a3-4ab0-ad44-bcc4e9da24ca", "metadata": {}, "outputs": [ @@ -348,17 +348,61 @@ }, { "cell_type": "code", - "execution_count": 11, - "id": "ddecc910-7804-4199-8577-a7db38a16db8", + "execution_count": 20, + "id": "9712783d", + "metadata": {}, + "outputs": [], + "source": [ + "# define Hamiltonian\n", + "operators = [\"zz\", \"xz\", \"z\"]\n", + "qubits = [\"01\", \"02\", \"3\"]\n", + "coefficients = [\"0.5\", \"-1.5\", \"1\"]\n", + "hamiltonian = (operators, qubits, coefficients)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "163b70a3-814a-4a62-a98a-2ffca933a544", "metadata": {}, "outputs": [ { - "name": "stderr", + "name": "stdout", "output_type": "stream", "text": [ - "[Qibo 0.2.20|INFO|2025-09-20 16:43:42]: Using qibojit (numba) backend on /CPU:0\n" + "Expectation value: 0.7143570184707642\n", + "Elapsed time: 0.1498 seconds\n" ] - }, + } + ], + "source": [ + "start = time.time()\n", + "expval = quimb_backend.expectation(\n", + " circuit=circuit,\n", + " operators_list=hamiltonian[0],\n", + " sites_list=hamiltonian[1],\n", + " coeffs_list=hamiltonian[2]\n", + " )\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": 18, + "id": "c8760074", + "metadata": {}, + "outputs": [ { "data": { "text/latex": [ @@ -368,7 +412,7 @@ "-1.5*X0*Z2 + 0.5*Z0*Z1 + Z3" ] }, - "execution_count": 11, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -385,41 +429,7 @@ }, { "cell_type": "code", - "execution_count": 13, - "id": "163b70a3-814a-4a62-a98a-2ffca933a544", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Expectation value: 0.7143489122390747\n", - "Elapsed time: 12.4550 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": 14, + "execution_count": 19, "id": "e2d05707", "metadata": {}, "outputs": [ @@ -428,7 +438,7 @@ "output_type": "stream", "text": [ "Expectation value: 0.7143570920618565\n", - "Elapsed time: 0.5871 seconds\n" + "Elapsed time: 0.5597 seconds\n" ] } ], @@ -458,7 +468,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "id": "8df55c5f", "metadata": {}, "outputs": [ @@ -474,9 +484,7 @@ } ], "source": [ - "# grad of this circuit returning nan for some reason...\n", - "\n", - "def build_circuit(nqubits, nlayers):\n", + "def build_circuit_A(nqubits, nlayers):\n", " \"\"\"Construct a parametric quantum circuit.\"\"\"\n", " circ = Circuit(nqubits)\n", " for _ in range(nlayers):\n", @@ -509,7 +517,7 @@ } ], "source": [ - "def build_circuit(nqubits, nlayers):\n", + "def build_circuit_B(nqubits, nlayers):\n", " circ = Circuit(nqubits)\n", " for _ in range(nlayers):\n", " for q in range(nqubits):\n", @@ -528,31 +536,7 @@ }, { "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, + "execution_count": null, "id": "0943482e", "metadata": {}, "outputs": [ @@ -560,27 +544,38 @@ "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" + "[ 2.57090405e-02 -2.24703997e-02 -2.82387018e-01 -1.02654770e-01\n", + " 1.03672206e-01 1.03572123e-01 -9.93297935e-01 3.26367974e-01\n", + " -8.58561993e-01 -3.14284384e-01 -1.22645356e-01 -2.13570029e-01\n", + " -4.55642402e-01 1.11669600e-02 -2.92290837e-01 -1.91316485e-01\n", + " 2.78813928e-01 8.80600572e-01 5.07975101e-01 -1.97107181e-01\n", + " -4.69740361e-01 -8.50831568e-02 4.45045829e-01 3.42172906e-02\n", + " -8.43066633e-01 1.86891228e-01 4.52477366e-01 7.36747682e-03\n", + " -6.28291368e-01 -9.38566178e-02 5.43581992e-02 3.57441790e-02\n", + " 5.15162945e-04 2.55566716e-01 -3.20922613e-01 4.96513635e-01]\n", + "Elapsed time: 11.3724 seconds\n" ] } ], "source": [ "import jax\n", + "import time\n", "\n", - "def f(params):\n", + "def f(circuit, hamiltonian, params):\n", " circuit.set_parameters(params)\n", " return quimb_backend.expectation(\n", " circuit=circuit,\n", - " observable=hamiltonian,\n", + " operators_list=hamiltonian[0],\n", + " sites_list=hamiltonian[1],\n", + " coeffs_list=hamiltonian[2]\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" + "start = time.time()\n", + "grad = jax.grad(f, argnums=2)(circuit, hamiltonian, parameters)\n", + "elapsed = time.time() - start\n", + "print(grad)\n", + "print(f\"Elapsed time: {elapsed:.4f} seconds\")" ] } ], diff --git a/examples/quimb_intro/test.py b/examples/quimb_intro/test.py deleted file mode 100644 index 2eccd91..0000000 --- a/examples/quimb_intro/test.py +++ /dev/null @@ -1,52 +0,0 @@ -import numpy as np -import jax -from qibo.backends import construct_backend -from qibo import Circuit, gates, hamiltonians -from qibo.symbols import Z, X, Y - -# construct qibotn backend -quimb_backend = construct_backend(backend="qibotn", platform="quimb") - -quimb_backend.setup_backend_specifics( - qimb_backend="jax", - optimizer='auto-hq' - ) - -quimb_backend.configure_tn_simulation( - max_bond_dimension=10 -) - - -# define Hamiltonian -form = 0.5 * Z(0) * Z(1) +- 1.5 * X(0) * Z(2) + Z(3) -hamiltonian = hamiltonians.SymbolicHamiltonian(form) - - -# define circuit -def build_circuit(nqubits, nlayers): - """Construct a more complex Qibo parametric quantum circuit without CNOT gates.""" - circ = Circuit(nqubits) - 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.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 = 6 -circuit = build_circuit(nqubits=nqubits, nlayers=3) - -def f(params): - circuit.set_parameters(params) - return quimb_backend.expectation( - circuit=circuit, - observable=hamiltonian, - ) - -parameters = np.random.uniform(-np.pi, np.pi, size=len(circuit.get_parameters())) -print(jax.value_and_grad(f)(parameters))