From 9f38a48ca9ce56e3c599d515e4cd5cb32606e9d6 Mon Sep 17 00:00:00 2001 From: Marek Gluza Date: Fri, 5 Apr 2024 09:22:44 +0200 Subject: [PATCH] imporoving testing notebook --- ...volution_oracles_and_gci_transpiling.ipynb | 123 +++++++++++++++++- 1 file changed, 119 insertions(+), 4 deletions(-) diff --git a/examples/dbi/dbi_group_commutators_evolution_oracles_and_gci_transpiling.ipynb b/examples/dbi/dbi_group_commutators_evolution_oracles_and_gci_transpiling.ipynb index b306551982..c0577fc300 100644 --- a/examples/dbi/dbi_group_commutators_evolution_oracles_and_gci_transpiling.ipynb +++ b/examples/dbi/dbi_group_commutators_evolution_oracles_and_gci_transpiling.ipynb @@ -11,7 +11,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "765f8da6", + "id": "173074d1", "metadata": {}, "outputs": [], "source": [ @@ -25,10 +25,103 @@ " print(reduce(str.__add__, out[0]))" ] }, + { + "cell_type": "code", + "execution_count": 10, + "id": "eb3a4f7f", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "@pytest.mark.parametrize(\"nqubits\", [3])\n", + "def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):\n", + " h0 = random_hermitian(2**nqubits, backend=backend)\n", + " d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))\n", + " dbi = DoubleBracketIteration(\n", + " Hamiltonian(nqubits, h0, backend=backend),\n", + " mode=DoubleBracketGeneratorType.group_commutator,\n", + " )\n", + "\n", + " for s in np.linspace(0,.01,NSTEPS):\n", + " u = dbi.eval_dbr_unitary(s,mode = DoubleBracketRotationType.single_commutator)\n", + " v = dbi.eval_dbr_unitary(s, mode = DoubleBracketRotationType.group_commutator)\n", + "\n", + " assert np.linalg.norm( u - v ) < 10 * s * np.linalg.norm(h0) * np.linalg.norm(d)\n", + "\n" + ] + } + ], + "source": [ + "print_function_source_code(test_double_bracket_iteration_eval_dbr_unitary)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "ceb004ac", + "metadata": {}, + "outputs": [], + "source": [ + "import qibo\n", + "backend = qibo.backends.construct_backend(\"numpy\")\n", + "nqubits = 3" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "50a41bdc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "numpy" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "backend" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "c0e53a8d", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "ename": "UnboundLocalError", + "evalue": "local variable 'operator' referenced before assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_9225/3866532243.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtest_double_bracket_iteration_eval_dbr_unitary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbackend\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnqubits\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~/Projects_git/DBI_qibo/qibo/examples/dbi/../../tests/test_models_dbi.py\u001b[0m in \u001b[0;36mtest_double_bracket_iteration_eval_dbr_unitary\u001b[0;34m(backend, nqubits)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinspace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m.01\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mNSTEPS\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---> 63\u001b[0;31m \u001b[0mu\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdbi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval_dbr_unitary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mmode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDoubleBracketRotationType\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msingle_commutator\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 64\u001b[0m \u001b[0mv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdbi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval_dbr_unitary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDoubleBracketRotationType\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup_commutator\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;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m~/Projects_git/DBI_qibo/qibo/src/qibo/models/dbi/double_bracket.py\u001b[0m in \u001b[0;36meval_dbr_unitary\u001b[0;34m(self, step, mode, d)\u001b[0m\n\u001b[1;32m 139\u001b[0m )\n\u001b[1;32m 140\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 141\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0moperator\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 142\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 143\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mstaticmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'operator' referenced before assignment" + ] + } + ], + "source": [ + "test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits)" + ] + }, { "cell_type": "code", "execution_count": 2, - "id": "cdf50e7c", + "id": "02929afc", "metadata": {}, "outputs": [ { @@ -64,7 +157,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "8688a26d", + "id": "de085a6e", "metadata": {}, "outputs": [ { @@ -80,10 +173,32 @@ "set_backend('numpy')" ] }, + { + "cell_type": "markdown", + "id": "b33d65d0", + "metadata": {}, + "source": [ + "# Show that double bracket iteration group commutator and dbi converge for small s BHMM\n", + "\n", + "This is testing the following:\n", + "\n", + "`dbi` runs $V = e^{-sW}$ and rotates $H_1 = V^\\dagger H_0 V$.\n", + "\n", + "`gci` runs $Q = GC$ and rotates $J_1 = Q^\\dagger H_0 Q$.\n", + "\n", + "`dbi2` runs $R = GC$ and rotates $K_1 = R^\\dagger H_0 R$.\n", + "\n", + "We assert that gci and dbi2 should be within machine precision for the correct sorting.\n", + "$$||J_1-K_1||\\le2 ||H_0||\\,||R-Q||\\le \\epsilon$$\n", + "\n", + "We assert that gci and dbi should be within the approximation bound of the GC\n", + "$$||J_1-H_1||\\le2 ||H_0||\\,||R-V||\\le C ||H_0|| s^{3/2}$$\n" + ] + }, { "cell_type": "code", "execution_count": 8, - "id": "1add4aa2", + "id": "99b58bba", "metadata": {}, "outputs": [], "source": [