refactor: adapt the example to the new expectation function

This commit is contained in:
MatteoRobbiati
2025-02-10 13:20:35 +01:00
parent bb76e2b64d
commit 5b63877876

View File

@@ -12,7 +12,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 14,
"id": "6722d94e-e311-48f9-b6df-c6d829bf67fb", "id": "6722d94e-e311-48f9-b6df-c6d829bf67fb",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -38,7 +38,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 15,
"id": "64162116-1555-4a68-811c-01593739d622", "id": "64162116-1555-4a68-811c-01593739d622",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -63,7 +63,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 16,
"id": "4a22a172-f50d-411d-afa3-fa61937c7b3a", "id": "4a22a172-f50d-411d-afa3-fa61937c7b3a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -82,7 +82,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 18,
"id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1", "id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -104,7 +104,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 19,
"id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98", "id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -127,7 +127,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 20,
"id": "34452bfd-2287-4b38-8099-e072239eab74", "id": "34452bfd-2287-4b38-8099-e072239eab74",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -157,7 +157,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 21,
"id": "221ef886-5578-4200-a019-dcafa51aada3", "id": "221ef886-5578-4200-a019-dcafa51aada3",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -169,7 +169,8 @@
"\u001b[0;34m\u001b[0m \u001b[0mcircuit\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mcircuit\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0minitial_state\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0minitial_state\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mnshots\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mnshots\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mprob_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'U'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mprob_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mreturn_array\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mprob_kwargs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mprob_kwargs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\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;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n",
@@ -180,18 +181,18 @@
" - Frequencies (if the number of shots is specified).\n", " - Frequencies (if the number of shots is specified).\n",
" - Probabilities computed using various methods.\n", " - Probabilities computed using various methods.\n",
"\n", "\n",
"The following probability computation methods are available, as implemented \n", "The following probability computation methods are available, as implemented\n",
"in Quantum Matcha Tea:\n", "in Quantum Matcha Tea:\n",
" - **\"E\" (Even):** Probabilities are computed by evenly descending the probability tree, \n", " - **\"E\" (Even):** Probabilities are computed by evenly descending the probability tree,\n",
" pruning branches (states) with probabilities below a threshold.\n", " pruning branches (states) with probabilities below a threshold.\n",
" - **\"G\" (Greedy):** Probabilities are computed by following the most probable states \n", " - **\"G\" (Greedy):** Probabilities are computed by following the most probable states\n",
" in descending order until reaching a given coverage (sum of probabilities).\n", " in descending order until reaching a given coverage (sum of probabilities).\n",
" - **\"U\" (Unbiased):** An optimal probability measure that is unbiased and designed \n", " - **\"U\" (Unbiased):** An optimal probability measure that is unbiased and designed\n",
" for best performance. See https://arxiv.org/abs/2401.10330 for details.\n", " for best performance. See https://arxiv.org/abs/2401.10330 for details.\n",
"\n", "\n",
"Args:\n", "Args:\n",
" circuit: A Qibo circuit to execute.\n", " circuit: A Qibo circuit to execute.\n",
" initial_state: The initial state of the system (default is the vacuum state \n", " initial_state: The initial state of the system (default is the vacuum state\n",
" for tensor network simulations).\n", " for tensor network simulations).\n",
" nshots: The number of shots for shot-noise simulation (optional).\n", " nshots: The number of shots for shot-noise simulation (optional).\n",
" prob_type: The probability computation method. Must be one of:\n", " prob_type: The probability computation method. Must be one of:\n",
@@ -203,7 +204,7 @@
" - For \"E\" and \"G\", requires ``prob_threshold``.\n", " - For \"E\" and \"G\", requires ``prob_threshold``.\n",
"\n", "\n",
"Returns:\n", "Returns:\n",
" TensorNetworkResult: An object with methods to reconstruct the state, \n", " TensorNetworkResult: An object with methods to reconstruct the state,\n",
" compute probabilities, and generate frequencies.\n", " compute probabilities, and generate frequencies.\n",
"\u001b[0;31mFile:\u001b[0m ~/Documents/PhD/qibotn/src/qibotn/backends/qmatchatea.py\n", "\u001b[0;31mFile:\u001b[0m ~/Documents/PhD/qibotn/src/qibotn/backends/qmatchatea.py\n",
"\u001b[0;31mType:\u001b[0m method\n" "\u001b[0;31mType:\u001b[0m method\n"
@@ -219,7 +220,31 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 22,
"id": "f271d3f3-c7f2-49b3-94e2-d6e843a169d0",
"metadata": {},
"outputs": [],
"source": [
"circuit = build_circuit(nqubits=4, nlayers=2)\n",
"\n",
"# Setting random parameters\n",
"circuit.set_parameters(\n",
" parameters=np.random.uniform(-np.pi, np.pi, len(circuit.get_parameters())),\n",
")\n",
"\n",
"# We first define the useful objects \n",
"convergence_parameters = QCConvergenceParameters(cut_ratio=1e-12, max_bond_dimension=1100)\n",
"\n",
"# And then call the dedicate configuration function\n",
"qmatcha_backend.configure_tn_simulation(\n",
" convergence_params=convergence_parameters,\n",
" ansatz=\"MPS\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf", "id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -229,43 +254,43 @@
"{'nqubits': 4,\n", "{'nqubits': 4,\n",
" 'backend': QMatchaTeaBackend(),\n", " 'backend': QMatchaTeaBackend(),\n",
" 'measures': None,\n", " 'measures': None,\n",
" 'measured_probabilities': {'U': {'0000': (0.0, 0.08390937969317301),\n", " 'measured_probabilities': {'U': {'0000': (0.0, 0.06042361322153613),\n",
" '0001': (0.08390937969317301, 0.08858639088838134),\n", " '0001': (0.06042361322153613, 0.16103484648184754),\n",
" '0010': (0.08858639088838131, 0.1832549957082757),\n", " '0010': (0.1610348464818476, 0.3885436985884956),\n",
" '0011': (0.1832549957082757, 0.25896776804349736),\n", " '0011': (0.3885436985884956, 0.4596882048691001),\n",
" '0100': (0.2589677680434974, 0.33039716334036867),\n", " '0100': (0.4596882048691, 0.46449216980829905),\n",
" '0101': (0.33039716334036867, 0.386620221067355),\n", " '0101': (0.46449216980829905, 0.47672801247815266),\n",
" '0110': (0.3866202210673549, 0.4380808691410473),\n", " '0110': (0.47672801247815266, 0.5016979158974251),\n",
" '0111': (0.4380808691410473, 0.47837271988834),\n", " '0111': (0.5016979158974251, 0.5398303779135739),\n",
" '1000': (0.47837271988834, 0.5916815553716759),\n", " '1000': (0.539830377913574, 0.6539396910265846),\n",
" '1001': (0.5916815553716759, 0.5972581739037379),\n", " '1001': (0.6539396910265846, 0.7584098289087688),\n",
" '1010': (0.5972581739037378, 0.6359857590550054),\n", " '1010': (0.7584098289087688, 0.7781986778747929),\n",
" '1011': (0.6359857590550054, 0.6894851559808782),\n", " '1011': (0.7781986778747929, 0.8661307878495997),\n",
" '1100': (0.6894851559808783, 0.7030911408535467),\n", " '1100': (0.8661307878495996, 0.8908084103632258),\n",
" '1101': (0.7030911408535467, 0.8264027395524797),\n", " '1101': (0.8908084103632258, 0.9107085647613623),\n",
" '1110': (0.8264027395524797, 0.8981519382820797),\n", " '1110': (0.9107085647613623, 0.9481703292728131),\n",
" '1111': (0.8981519382820797, 0.9999999999999998)},\n", " '1111': (0.9481703292728131, 0.9999999999999999)},\n",
" 'E': [None],\n", " 'E': [None],\n",
" 'G': [None]},\n", " 'G': [None]},\n",
" 'prob_type': 'U',\n", " 'prob_type': 'U',\n",
" 'statevector': array([ 0.08809627-0.27595005j, 0.24859731-0.22695421j,\n", " 'statevector': array([-0.13931487-0.20252155j, -0.21974897+0.25655351j,\n",
" 0.18807826+0.18988408j, 0.09444097+0.06846085j,\n", " 0.02884164-0.06302479j, -0.15237615+0.03819857j,\n",
" 0.00470148+0.30764671j, 0.17371395-0.09247188j,\n", " -0.3172761 -0.35615268j, -0.10121749+0.09769272j,\n",
" -0.18900305+0.12545316j, -0.17359753+0.20399288j,\n", " 0.14826026-0.0546699j , -0.1814861 -0.06726486j,\n",
" -0.0517478 +0.04471215j, -0.0411739 -0.06230031j,\n", " -0.10907416+0.29784906j, 0.30276076+0.11316387j,\n",
" 0.22377064+0.07842041j, -0.21784975-0.27541439j,\n", " 0.07771728+0.07871383j, -0.12379363+0.0676409j ,\n",
" -0.27208941+0.04098933j, -0.22748127+0.04185292j,\n", " -0.21860099+0.15283362j, 0.06862868+0.28848261j,\n",
" 0.17105258-0.10503745j, -0.01729753-0.31866731j])}" " -0.04640848-0.18968056j, -0.22262942-0.04760056j])}"
] ]
}, },
"execution_count": 8, "execution_count": 23,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"# Simple execution (defaults)\n", "# Simple execution (defaults)\n",
"outcome = qmatcha_backend.execute_circuit(circuit=circuit)\n", "outcome = qmatcha_backend.execute_circuit(circuit=circuit, return_array=True)\n",
"\n", "\n",
"# Print outcome\n", "# Print outcome\n",
"vars(outcome)" "vars(outcome)"
@@ -273,7 +298,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 26,
"id": "60501c3d-2a44-421f-b434-4a508714b132", "id": "60501c3d-2a44-421f-b434-4a508714b132",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -284,30 +309,25 @@
" 'backend': QMatchaTeaBackend(),\n", " 'backend': QMatchaTeaBackend(),\n",
" 'measures': None,\n", " 'measures': None,\n",
" 'measured_probabilities': {'U': [None],\n", " 'measured_probabilities': {'U': [None],\n",
" 'E': {'0000': 0.08390937969317301,\n", " 'E': [None],\n",
" '0010': 0.09466860481989439,\n", " 'G': {'0010': 0.227508852106648,\n",
" '0011': 0.07571277233522165,\n", " '0011': 0.07114450628060452,\n",
" '0100': 0.07142939529687124,\n", " '1000': 0.227508852106648,\n",
" '0101': 0.05622305772698632,\n", " '1001': 0.07114450628060452,\n",
" '0110': 0.05146064807369245,\n", " '0000': 0.06042361322153613,\n",
" '1000': 0.11330883548333581,\n", " '0001': 0.1006112332603114}},\n",
" '1011': 0.053499396925872765,\n", " 'prob_type': 'G',\n",
" '1101': 0.12331159869893296,\n", " 'statevector': array([-0.13931487-0.20252155j, -0.21974897+0.25655351j,\n",
" '1110': 0.07174919872960005,\n", " 0.02884164-0.06302479j, -0.15237615+0.03819857j,\n",
" '1111': 0.10184806171792007},\n", " -0.3172761 -0.35615268j, -0.10121749+0.09769272j,\n",
" 'G': [None]},\n", " 0.14826026-0.0546699j , -0.1814861 -0.06726486j,\n",
" 'prob_type': 'E',\n", " -0.10907416+0.29784906j, 0.30276076+0.11316387j,\n",
" 'statevector': array([ 0.08809627-0.27595005j, 0.24859731-0.22695421j,\n", " 0.07771728+0.07871383j, -0.12379363+0.0676409j ,\n",
" 0.18807826+0.18988408j, 0.09444097+0.06846085j,\n", " -0.21860099+0.15283362j, 0.06862868+0.28848261j,\n",
" 0.00470148+0.30764671j, 0.17371395-0.09247188j,\n", " -0.04640848-0.18968056j, -0.22262942-0.04760056j])}"
" -0.18900305+0.12545316j, -0.17359753+0.20399288j,\n",
" -0.0517478 +0.04471215j, -0.0411739 -0.06230031j,\n",
" 0.22377064+0.07842041j, -0.21784975-0.27541439j,\n",
" -0.27208941+0.04098933j, -0.22748127+0.04185292j,\n",
" 0.17105258-0.10503745j, -0.01729753-0.31866731j])}"
] ]
}, },
"execution_count": 9, "execution_count": 26,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -317,8 +337,9 @@
"# We use here \"E\", which is cutting some of the components if under a threshold\n", "# We use here \"E\", which is cutting some of the components if under a threshold\n",
"outcome = qmatcha_backend.execute_circuit(\n", "outcome = qmatcha_backend.execute_circuit(\n",
" circuit=circuit,\n", " circuit=circuit,\n",
" prob_type=\"E\",\n", " prob_type=\"G\",\n",
" prob_threshold=0.05,\n", " prob_threshold=0.9,\n",
" return_array=True,\n",
")\n", ")\n",
"\n", "\n",
"# Print outcome\n", "# Print outcome\n",
@@ -337,7 +358,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 27,
"id": "c0443efc-21ef-4ed5-9cf4-785d204a1881", "id": "c0443efc-21ef-4ed5-9cf4-785d204a1881",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -346,15 +367,15 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Probabilities:\n", "Probabilities:\n",
" {'0000': 0.08390937969317301, '0010': 0.09466860481989439, '0011': 0.07571277233522165, '0100': 0.07142939529687124, '0101': 0.05622305772698632, '0110': 0.05146064807369245, '1000': 0.11330883548333581, '1011': 0.053499396925872765, '1101': 0.12331159869893296, '1110': 0.07174919872960005, '1111': 0.10184806171792007}\n", " [0.22750885 0.07114451 0.22750885 0.07114451 0.06042361 0.10061123]\n",
"\n", "\n",
"State:\n", "State:\n",
" [ 0.08809627-0.27595005j 0.24859731-0.22695421j 0.18807826+0.18988408j\n", " [-0.13931487-0.20252155j -0.21974897+0.25655351j 0.02884164-0.06302479j\n",
" 0.09444097+0.06846085j 0.00470148+0.30764671j 0.17371395-0.09247188j\n", " -0.15237615+0.03819857j -0.3172761 -0.35615268j -0.10121749+0.09769272j\n",
" -0.18900305+0.12545316j -0.17359753+0.20399288j -0.0517478 +0.04471215j\n", " 0.14826026-0.0546699j -0.1814861 -0.06726486j -0.10907416+0.29784906j\n",
" -0.0411739 -0.06230031j 0.22377064+0.07842041j -0.21784975-0.27541439j\n", " 0.30276076+0.11316387j 0.07771728+0.07871383j -0.12379363+0.0676409j\n",
" -0.27208941+0.04098933j -0.22748127+0.04185292j 0.17105258-0.10503745j\n", " -0.21860099+0.15283362j 0.06862868+0.28848261j -0.04640848-0.18968056j\n",
" -0.01729753-0.31866731j]\n", " -0.22262942-0.04760056j]\n",
"\n" "\n"
] ]
} }
@@ -376,7 +397,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 28,
"id": "8a868f3e-8383-47ee-a93a-27c5f85e48c5", "id": "8a868f3e-8383-47ee-a93a-27c5f85e48c5",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -396,7 +417,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 29,
"id": "68122cd3-662f-4fd1-bb9c-d33b6f5448dd", "id": "68122cd3-662f-4fd1-bb9c-d33b6f5448dd",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -405,47 +426,37 @@
"text/plain": [ "text/plain": [
"{'nqubits': 4,\n", "{'nqubits': 4,\n",
" 'backend': QMatchaTeaBackend(),\n", " 'backend': QMatchaTeaBackend(),\n",
" 'measures': {'0000': 88,\n", " 'measures': {'0000': 75,\n",
" '0001': 7,\n", " '0001': 113,\n",
" '0010': 90,\n", " '0010': 202,\n",
" '0011': 78,\n", " '0011': 59,\n",
" '0100': 72,\n", " '0100': 9,\n",
" '0101': 56,\n", " '0101': 14,\n",
" '0110': 47,\n", " '0110': 30,\n",
" '0111': 41,\n", " '0111': 37,\n",
" '1000': 120,\n", " '1000': 130,\n",
" '1001': 7,\n", " '1001': 113,\n",
" '1010': 41,\n", " '1010': 19,\n",
" '1011': 53,\n", " '1011': 95,\n",
" '1100': 20,\n", " '1100': 19,\n",
" '1101': 129,\n", " '1101': 26,\n",
" '1110': 73,\n", " '1110': 39,\n",
" '1111': 102},\n", " '1111': 44},\n",
" 'measured_probabilities': {'U': [None],\n", " 'measured_probabilities': {'U': [None],\n",
" 'E': {'0000': 0.08390937969317301,\n", " 'E': {'0000': 0.06042361322153613,\n",
" '0010': 0.09466860481989439,\n", " '0001': 0.1006112332603114,\n",
" '0011': 0.07571277233522165,\n", " '0010': 0.227508852106648,\n",
" '0100': 0.07142939529687124,\n", " '0011': 0.07114450628060452,\n",
" '0101': 0.05622305772698632,\n", " '1000': 0.11410931311301056,\n",
" '0110': 0.05146064807369245,\n", " '1001': 0.10447013788218423,\n",
" '1000': 0.11330883548333581,\n", " '1011': 0.08793210997480687,\n",
" '1011': 0.053499396925872765,\n", " '1111': 0.05182967072718676},\n",
" '1101': 0.12331159869893296,\n",
" '1110': 0.07174919872960005,\n",
" '1111': 0.10184806171792007},\n",
" 'G': [None]},\n", " 'G': [None]},\n",
" 'prob_type': 'E',\n", " 'prob_type': 'E',\n",
" 'statevector': array([ 0.08809627-0.27595005j, 0.24859731-0.22695421j,\n", " 'statevector': None}"
" 0.18807826+0.18988408j, 0.09444097+0.06846085j,\n",
" 0.00470148+0.30764671j, 0.17371395-0.09247188j,\n",
" -0.18900305+0.12545316j, -0.17359753+0.20399288j,\n",
" -0.0517478 +0.04471215j, -0.0411739 -0.06230031j,\n",
" 0.22377064+0.07842041j, -0.21784975-0.27541439j,\n",
" -0.27208941+0.04098933j, -0.22748127+0.04185292j,\n",
" 0.17105258-0.10503745j, -0.01729753-0.31866731j])}"
] ]
}, },
"execution_count": 12, "execution_count": 29,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -466,7 +477,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 30,
"id": "ef0e9591-ccca-4cdd-a81b-2bfb3caaf3d0", "id": "ef0e9591-ccca-4cdd-a81b-2bfb3caaf3d0",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -475,7 +486,7 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Frequencies:\n", "Frequencies:\n",
" {'0000': 88, '0001': 7, '0010': 90, '0011': 78, '0100': 72, '0101': 56, '0110': 47, '0111': 41, '1000': 120, '1001': 7, '1010': 41, '1011': 53, '1100': 20, '1101': 129, '1110': 73, '1111': 102}\n", " {'0000': 75, '0001': 113, '0010': 202, '0011': 59, '0100': 9, '0101': 14, '0110': 30, '0111': 37, '1000': 130, '1001': 113, '1010': 19, '1011': 95, '1100': 19, '1101': 26, '1110': 39, '1111': 44}\n",
"\n" "\n"
] ]
} }
@@ -495,7 +506,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 31,
"id": "9e9632d5-28dd-4846-8457-c579d0cb9453", "id": "9e9632d5-28dd-4846-8457-c579d0cb9453",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -563,7 +574,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 32,
"id": "5620f476-1bc7-4cf8-9868-b127c715b6ec", "id": "5620f476-1bc7-4cf8-9868-b127c715b6ec",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -582,7 +593,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 33,
"id": "32d605e9-672a-40b6-8a1a-eb17bbc3c45e", "id": "32d605e9-672a-40b6-8a1a-eb17bbc3c45e",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -621,7 +632,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 34,
"id": "0b46e315-7786-4247-bd2a-83ea1c5842eb", "id": "0b46e315-7786-4247-bd2a-83ea1c5842eb",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -631,7 +642,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 35,
"id": "37385485-e8a3-4ab0-ad44-bcc4e9da24ca", "id": "37385485-e8a3-4ab0-ad44-bcc4e9da24ca",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -639,10 +650,10 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"0: ─RY─RZ─o─────X─RY─RZ─o─────X─RY─RZ─o─────X─M─\n", "0: ─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", "1: ─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", "2: ─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" "3: ─RY─RZ─────X─o─RY─RZ─────X─o─M─\n"
] ]
} }
], ],
@@ -658,33 +669,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 36,
"id": "ddecc910-7804-4199-8577-a7db38a16db8", "id": "ddecc910-7804-4199-8577-a7db38a16db8",
"metadata": {}, "metadata": {},
"outputs": [ "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": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# We can create an Hamiltonian form\n", "# We can create an Hamiltonian form\n",
"form = 0.5 * Z(0) * Z(1) +- 1.5 * X(0) * Z(2) + Z(3)\n", "form = 0.5 * Z(0) * Z(1) +- 1.5 * X(0) * Z(2) + Z(3)\n",
"form" "hamiltonian = hamiltonians.SymbolicHamiltonian(form)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 37,
"id": "a6599df3-989e-4131-8664-3451d0cc4372", "id": "a6599df3-989e-4131-8664-3451d0cc4372",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -693,24 +690,26 @@
"text/plain": [ "text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mqmatcha_backend\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexpectation\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcircuit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobservable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mSignature:\u001b[0m \u001b[0mqmatcha_backend\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexpectation\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcircuit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobservable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n",
"Compute the expectation value of a Qibo-friendly ``observable``\n", "Compute the expectation value of a Qibo-friendly ``observable`` on\n",
"on the Tensor Network constructed from a Qibo ``circuit``.\n", "the Tensor Network constructed from a Qibo ``circuit``.\n",
"\n", "\n",
"This method takes a Qibo-style symbolic Hamiltonian (e.g., `X(0)*Z(1) + 2.0*Y(2)*Z(0)`) \n", "This method takes a Qibo-style symbolic Hamiltonian (e.g., `X(0)*Z(1) + 2.0*Y(2)*Z(0)`)\n",
"as the observable, converts it into a Quantum Matcha Tea (qmatchatea) observable\n", "as the observable, converts it into a Quantum Matcha Tea (qmatchatea) observable\n",
"(using `TNObsTensorProduct` and `TNObsWeightedSum`), and computes its expectation \n", "(using `TNObsTensorProduct` and `TNObsWeightedSum`), and computes its expectation\n",
"value using the provided circuit.\n", "value using the provided circuit.\n",
"\n", "\n",
"Args:\n", "Args:\n",
" circuit: A Qibo quantum circuit object on which the expectation value\n", " circuit: A Qibo quantum circuit object on which the expectation value\n",
" is computed. The circuit should be compatible with the qmatchatea\n", " is computed. The circuit should be compatible with the qmatchatea\n",
" Tensor Network backend.\n", " Tensor Network backend.\n",
" observable: The observable whose expectation value we want to compute. \n", " observable: The observable whose expectation value we want to compute.\n",
" This must be provided in the symbolic Hamiltonian form supported by Qibo\n", " This must be provided in the symbolic Hamiltonian form supported by Qibo\n",
" (e.g., `X(0)*Y(1)` or `Z(0)*Z(1) + 1.5*Y(2)`).\n", " (e.g., `X(0)*Y(1)` or `Z(0)*Z(1) + 1.5*Y(2)`).\n",
"\n", "\n",
"Returns:\n", "Returns:\n",
" qmatchatea.SimulationResult [TEMPORARY]\n", " qibotn.TensorNetworkResult class, providing methods to retrieve\n",
" probabilities, frequencies and state always according to the chosen\n",
" simulation setup.\n",
"\u001b[0;31mFile:\u001b[0m ~/Documents/PhD/qibotn/src/qibotn/backends/qmatchatea.py\n", "\u001b[0;31mFile:\u001b[0m ~/Documents/PhD/qibotn/src/qibotn/backends/qmatchatea.py\n",
"\u001b[0;31mType:\u001b[0m method\n" "\u001b[0;31mType:\u001b[0m method\n"
] ]
@@ -725,17 +724,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 38,
"id": "163b70a3-814a-4a62-a98a-2ffca933a544", "id": "163b70a3-814a-4a62-a98a-2ffca933a544",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"0.41329220819521995" "-0.27540575238693377"
] ]
}, },
"execution_count": 22, "execution_count": 38,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -743,30 +742,23 @@
"source": [ "source": [
"qmatcha_backend.expectation(\n", "qmatcha_backend.expectation(\n",
" circuit=circuit,\n", " circuit=circuit,\n",
" observable=form,\n", " observable=hamiltonian,\n",
")" ")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 39,
"id": "2d8c4a9c-eca3-49d0-bdbf-ab054172c4e5", "id": "2d8c4a9c-eca3-49d0-bdbf-ab054172c4e5",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[Qibo 0.2.16|INFO|2025-01-28 22:13:05]: Using qibojit (numba) backend on /CPU:0\n"
]
},
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"0.4132922081952206" "-0.275405752386936"
] ]
}, },
"execution_count": 23, "execution_count": 39,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }