diff --git a/examples/quimb_intro/quimb_introduction.ipynb b/examples/quimb_intro/quimb_introduction.ipynb index 73a831b..70fdf45 100644 --- a/examples/quimb_intro/quimb_introduction.ipynb +++ b/examples/quimb_intro/quimb_introduction.ipynb @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 3, "id": "64162116-1555-4a68-811c-01593739d622", "metadata": {}, "outputs": [], @@ -67,10 +67,7 @@ "# set numpy random seed\n", "np.random.seed(42)\n", "\n", - "quimb_backend.setup_backend_specifics(\n", - " qimb_backend=\"jax\", \n", - " contractions_optimizer=ctg_opt\n", - " )" + "quimb_backend.setup_backend_specifics(qimb_backend=\"jax\")" ] }, { @@ -83,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "id": "4a22a172-f50d-411d-afa3-fa61937c7b3a", "metadata": {}, "outputs": [], @@ -102,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1", "metadata": {}, "outputs": [ @@ -124,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98", "metadata": {}, "outputs": [], @@ -147,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "id": "2ee03e94-d794-4a51-9e76-01e8d8a259ba", "metadata": {}, "outputs": [], @@ -175,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf", "metadata": {}, "outputs": [ @@ -196,34 +193,32 @@ "text/plain": [ "{'nqubits': 4,\n", " 'backend': qibotn (quimb),\n", - " 'measures': Counter({'1000': 14,\n", - " '1011': 9,\n", - " '0101': 5,\n", - " '0010': 12,\n", + " 'measures': Counter({'1010': 9,\n", + " '0100': 8,\n", + " '1101': 15,\n", + " '1011': 4,\n", + " '1111': 12,\n", + " '1000': 13,\n", " '0000': 8,\n", - " '1010': 4,\n", - " '1101': 19,\n", + " '0010': 6,\n", " '0011': 6,\n", - " '0100': 3,\n", - " '1111': 9,\n", - " '0111': 4,\n", - " '0110': 4,\n", - " '1110': 2,\n", - " '1100': 1}),\n", + " '0101': 8,\n", + " '1110': 5,\n", + " '0110': 5,\n", + " '0111': 1}),\n", " 'measured_probabilities': {'1101': np.float64(0.12331159869893256),\n", " '1000': np.float64(0.11330883548333587),\n", - " '0010': np.float64(0.09466860481989385),\n", - " '1011': np.float64(0.053499396925872744),\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", + " '0000': np.float64(0.08390937969317269),\n", + " '0101': np.float64(0.05622305772698622),\n", + " '0010': np.float64(0.09466860481989385),\n", + " '0011': np.float64(0.07571277233522114),\n", " '1110': np.float64(0.07174919872959985),\n", - " '1100': np.float64(0.013605984872668404)},\n", + " '0110': np.float64(0.05146064807369214),\n", + " '1011': np.float64(0.053499396925872744),\n", + " '0111': np.float64(0.04029185074729259)},\n", " 'prob_type': 'default',\n", " 'statevector': Array([[ 0.08809624-0.27594998j],\n", " [-0.05174781+0.04471217j],\n", @@ -243,7 +238,7 @@ " [-0.01729754-0.31866732j]], dtype=complex64)}" ] }, - "execution_count": 10, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -268,7 +263,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "id": "c0443efc-21ef-4ed5-9cf4-785d204a1881", "metadata": {}, "outputs": [ @@ -277,7 +272,7 @@ "output_type": "stream", "text": [ "Probabilities:\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", + " {'1101': np.float64(0.12331159869893256), '1000': np.float64(0.11330883548333587), '1111': np.float64(0.10184806171791962), '1010': np.float64(0.03872758515126756), '0100': np.float64(0.07142939529687138), '0000': np.float64(0.08390937969317269), '0101': np.float64(0.05622305772698622), '0010': np.float64(0.09466860481989385), '0011': np.float64(0.07571277233522114), '1110': np.float64(0.07174919872959985), '0110': np.float64(0.05146064807369214), '1011': np.float64(0.053499396925872744), '0111': np.float64(0.04029185074729259)}\n", "\n", "State:\n", " [[ 0.08809624-0.27594998j]\n", @@ -307,7 +302,7 @@ }, { "cell_type": "markdown", - "id": "dd84f1f3-7aa5-4ad1-ae09-81e0aff75b5b", + "id": "9531f9d6", "metadata": {}, "source": [ "### Compute expectation values\n", @@ -321,57 +316,99 @@ }, { "cell_type": "code", - "execution_count": 12, - "id": "37385485-e8a3-4ab0-ad44-bcc4e9da24ca", + "execution_count": 10, + "id": "647f2073", "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" - ] - } - ], + "outputs": [], "source": [ - "# We are going to compute the expval of an Hamiltonian\n", - "# On the state prepared by the following circuit\n", - "circuit.draw()\n", + "import numpy as np\n", + "import jax\n", + "from qibo.backends import construct_backend\n", + "from qibo import Circuit, gates" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "74c63a41", + "metadata": {}, + "outputs": [], + "source": [ + "# construct qibotn backend\n", + "quimb_backend = construct_backend(backend=\"qibotn\", platform=\"quimb\")\n", "\n", - "circuit.set_parameters(\n", - " np.random.randn(len(circuit.get_parameters()))\n", + "quimb_backend.setup_backend_specifics(\n", + " qimb_backend =\"jax\", \n", + " contractions_optimizer='auto-hq'\n", + " )\n", + "\n", + "quimb_backend.configure_tn_simulation(\n", + " max_bond_dimension=10\n", ")" ] }, { "cell_type": "code", - "execution_count": 20, - "id": "9712783d", + "execution_count": 19, + "id": "b2a0decb", "metadata": {}, "outputs": [], "source": [ "# define Hamiltonian\n", - "operators = [\"zz\", \"xz\", \"z\"]\n", - "qubits = [\"01\", \"02\", \"3\"]\n", - "coefficients = [\"0.5\", \"-1.5\", \"1\"]\n", + "operators = [\"xzy\", \"yxzy\", \"zy\"]\n", + "qubits = [\"011\", \"0112\", \"01\"]\n", + "coefficients = [\"1\", \"2\", \"j\"]\n", "hamiltonian = (operators, qubits, coefficients)" ] }, { "cell_type": "code", - "execution_count": 25, - "id": "163b70a3-814a-4a62-a98a-2ffca933a544", + "execution_count": 18, + "id": "bd734be8", + "metadata": {}, + "outputs": [], + "source": [ + "# define circuit\n", + "def build_circuit(nqubits, nlayers):\n", + " circ = Circuit(nqubits)\n", + " for layer 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", + "def build_circuit_problematic(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.CNOT(q%nqubits, (q+1)%nqubits) for q in range(nqubits))]\n", + " circ.add(gates.M(*range(nqubits)))\n", + " return circ\n", + "\n", + "\n", + "nqubits = 4\n", + "circuit = build_circuit(nqubits=nqubits, nlayers=3)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "fe63ff24", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Expectation value: 0.7143570184707642\n", - "Elapsed time: 0.1498 seconds\n" + "Expectation value: 0.0\n", + "Elapsed time: 0.1071 seconds\n" ] } ], @@ -391,7 +428,7 @@ }, { "cell_type": "markdown", - "id": "90663e28", + "id": "d976a849", "metadata": {}, "source": [ "Try with Qibo (which is by default using the Qibojit backend)\n" @@ -399,52 +436,31 @@ }, { "cell_type": "code", - "execution_count": 18, - "id": "c8760074", - "metadata": {}, - "outputs": [ - { - "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": 18, - "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": 19, - "id": "e2d05707", + "execution_count": 22, + "id": "fb1436c8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Expectation value: 0.7143570920618565\n", - "Elapsed time: 0.5597 seconds\n" + "Expectation value: 1.5\n", + "Elapsed time: 0.0501 seconds\n" ] } ], "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", + "sym_hamiltonian = hamiltonians.SymbolicHamiltonian(form)\n", + "\n", + "# Let's show it\n", + "sym_hamiltonian.form\n", + "\n", + "# Compute expectation value\n", "start = time.time()\n", - "result = hamiltonian.expectation(circuit().state())\n", + "result = sym_hamiltonian.expectation(circuit().state())\n", "elapsed = time.time() - start\n", "print(f\"Expectation value: {result}\")\n", "print(f\"Elapsed time: {elapsed:.4f} seconds\")" @@ -452,7 +468,7 @@ }, { "cell_type": "markdown", - "id": "94df291c-9ddc-4b2e-8442-5fca00784bd8", + "id": "77bef077", "metadata": {}, "source": [ "They match! 🥳" @@ -460,107 +476,35 @@ }, { "cell_type": "markdown", - "id": "d2d119fc", + "id": "50130ae6", "metadata": {}, "source": [ - "### Derivative of the extimation function" + "We can also compute gradient of expectation function" ] }, { "cell_type": "code", - "execution_count": null, - "id": "8df55c5f", + "execution_count": 23, + "id": "6a3b26e4", "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" + "[ 8.19939339e-10 -3.14190913e-08 -2.99498648e-09 -1.03641796e-07\n", + " 8.48652704e-10 1.00297093e-07 -6.75429277e-08 -9.78565140e-09\n", + " -5.11915417e-08 1.29225235e-08 -7.44280655e-08 -3.49115048e-08\n", + " -4.98508879e-09 6.80729357e-08 -3.29755920e-08 4.20008526e-08\n", + " -2.89742630e-08 1.18602941e-07 -2.88252178e-08 5.57985391e-09\n", + " -3.17434115e-08 -1.03342952e-08 1.34079716e-08 -7.05437886e-09\n", + " -4.34059650e-08 -2.18019203e-08 -5.36932561e-08 -6.38544009e-08\n", + " 5.85312279e-08 8.45709067e-08 -1.12777876e-09 -6.41545981e-08\n", + " 7.25317406e-08 4.10035668e-08 -1.29046382e-08 6.07501676e-08]\n" ] } ], "source": [ - "def build_circuit_A(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_B(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": null, - "id": "0943482e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[ 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(circuit, hamiltonian, params):\n", " circuit.set_parameters(params)\n", " return quimb_backend.expectation(\n", @@ -571,11 +515,7 @@ " )\n", "\n", "parameters = np.random.uniform(-np.pi, np.pi, size=len(circuit.get_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\")" + "print(jax.grad(f, argnums=2)(circuit, hamiltonian, parameters))\n" ] } ],