Initialize
Some checks failed
Build wheels / build (ubuntu-latest, 3.11) (push) Has been cancelled
Build wheels / build (ubuntu-latest, 3.12) (push) Has been cancelled
Build wheels / build (ubuntu-latest, 3.13) (push) Has been cancelled
docs / evaluate-label (push) Has been cancelled
Tests / check (push) Has been cancelled
docs / deploy-docs (push) Has been cancelled
Tests / build (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / build (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / build (ubuntu-latest, 3.13) (push) Has been cancelled
Some checks failed
Build wheels / build (ubuntu-latest, 3.11) (push) Has been cancelled
Build wheels / build (ubuntu-latest, 3.12) (push) Has been cancelled
Build wheels / build (ubuntu-latest, 3.13) (push) Has been cancelled
docs / evaluate-label (push) Has been cancelled
Tests / check (push) Has been cancelled
docs / deploy-docs (push) Has been cancelled
Tests / build (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / build (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / build (ubuntu-latest, 3.13) (push) Has been cancelled
This commit is contained in:
581
examples/qmatchatea_intro/qmatchatea_introduction.ipynb
Normal file
581
examples/qmatchatea_intro/qmatchatea_introduction.ipynb
Normal file
@@ -0,0 +1,581 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "656bb283-ac6d-48d2-a029-3c417c9961f8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Introduction to Quantum Matcha Tea backend in QiboTN\n",
|
||||
"\n",
|
||||
"#### Some imports"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "6722d94e-e311-48f9-b6df-c6d829bf67fb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import time\n",
|
||||
"import numpy as np\n",
|
||||
"from scipy import stats\n",
|
||||
"\n",
|
||||
"import qibo\n",
|
||||
"from qibo import Circuit, gates, hamiltonians\n",
|
||||
"from qibo.backends import construct_backend"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a009a5e0-cfd4-4a49-9f7c-e82f252c6147",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Some hyper parameters"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "64162116-1555-4a68-811c-01593739d622",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# construct qibotn backend\n",
|
||||
"qmatcha_backend = construct_backend(backend=\"qibotn\", platform=\"qmatchatea\")\n",
|
||||
"\n",
|
||||
"# set number of qubits\n",
|
||||
"nqubits = 4\n",
|
||||
"\n",
|
||||
"# set numpy random seed\n",
|
||||
"np.random.seed(42)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "252f5cd1-5932-4de6-8076-4a357d50ebad",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Constructing a parametric quantum circuit"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "4a22a172-f50d-411d-afa3-fa61937c7b3a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1",
|
||||
"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": [
|
||||
"circuit = build_circuit(nqubits=nqubits, nlayers=3)\n",
|
||||
"circuit.draw()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Setting random parameters\n",
|
||||
"circuit.set_parameters(\n",
|
||||
" parameters=np.random.uniform(-np.pi, np.pi, len(circuit.get_parameters())),\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fd0cea52-03f5-4366-a01a-a5a84aa8ebc7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Setting up the tensor network simulator\n",
|
||||
"\n",
|
||||
"Depending on the simulator, various parameters can be set. One can customize the tensor network execution via the `backend.configure_tn_simulation` function, whose face depends on the specific backend provider."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "2ee03e94-d794-4a51-9e76-01e8d8a259ba",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Customization of the tensor network simulation in the case of qmatchatea\n",
|
||||
"# Here we use only some of the possible arguments\n",
|
||||
"qmatcha_backend.configure_tn_simulation(\n",
|
||||
" ansatz=\"MPS\",\n",
|
||||
" max_bond_dimension=10,\n",
|
||||
" cut_ratio=1e-6,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "648d85b8-445d-4081-aeed-1691fbae67be",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Executing through the backend\n",
|
||||
"\n",
|
||||
"The `backend.execute_circuit` method can be used then. We can simulate results in three ways:\n",
|
||||
"1. reconstruction of the final state (statevector like, only if `nqubits < 20` due to Quantum Matcha Tea setup) only if `return_array` is set to `True`;\n",
|
||||
"2. computation of the relevant probabilities of the final state. There are three way of doing so, but we will see it directly into the docstrings;\n",
|
||||
"3. reconstruction of the relevant state's frequencies (only if `nshots` is not `None`)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'nqubits': 4,\n",
|
||||
" 'backend': QMatchaTeaBackend(),\n",
|
||||
" 'measures': None,\n",
|
||||
" 'measured_probabilities': {'U': {'0000': (0.0, 0.08390937969317301),\n",
|
||||
" '0001': (0.08390937969317301, 0.08858639088838134),\n",
|
||||
" '0010': (0.08858639088838131, 0.1832549957082757),\n",
|
||||
" '0011': (0.1832549957082757, 0.25896776804349736),\n",
|
||||
" '0100': (0.2589677680434974, 0.33039716334036867),\n",
|
||||
" '0101': (0.33039716334036867, 0.386620221067355),\n",
|
||||
" '0110': (0.3866202210673549, 0.4380808691410473),\n",
|
||||
" '0111': (0.4380808691410473, 0.47837271988834),\n",
|
||||
" '1000': (0.47837271988834, 0.5916815553716759),\n",
|
||||
" '1001': (0.5916815553716759, 0.5972581739037379),\n",
|
||||
" '1010': (0.5972581739037378, 0.6359857590550054),\n",
|
||||
" '1011': (0.6359857590550054, 0.6894851559808782),\n",
|
||||
" '1100': (0.6894851559808783, 0.7030911408535467),\n",
|
||||
" '1101': (0.7030911408535467, 0.8264027395524797),\n",
|
||||
" '1110': (0.8264027395524797, 0.8981519382820797),\n",
|
||||
" '1111': (0.8981519382820797, 0.9999999999999998)},\n",
|
||||
" 'E': [None],\n",
|
||||
" 'G': [None]},\n",
|
||||
" 'prob_type': 'U',\n",
|
||||
" 'statevector': None}"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Simple execution (defaults)\n",
|
||||
"outcome = qmatcha_backend.execute_circuit(circuit=circuit)\n",
|
||||
"\n",
|
||||
"# Print outcome\n",
|
||||
"vars(outcome)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "60501c3d-2a44-421f-b434-4a508714b132",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'nqubits': 4,\n",
|
||||
" 'backend': QMatchaTeaBackend(),\n",
|
||||
" 'measures': None,\n",
|
||||
" 'measured_probabilities': {'U': [None],\n",
|
||||
" 'E': [None],\n",
|
||||
" 'G': {'1110': 0.07174919872960005,\n",
|
||||
" '1111': 0.10184806171792007,\n",
|
||||
" '0010': 0.09466860481989439,\n",
|
||||
" '0011': 0.07571277233522165}},\n",
|
||||
" 'prob_type': 'G',\n",
|
||||
" 'statevector': array([ 0.08809627-0.27595005j, 0.24859731-0.22695421j,\n",
|
||||
" 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": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Execution with a specific probability type\n",
|
||||
"# We use here \"E\", which is cutting some of the components if under a threshold\n",
|
||||
"# We also retrieve the statevector\n",
|
||||
"outcome = qmatcha_backend.execute_circuit(\n",
|
||||
" circuit=circuit,\n",
|
||||
" prob_type=\"G\",\n",
|
||||
" prob_threshold=0.3,\n",
|
||||
" return_array=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Print outcome\n",
|
||||
"vars(outcome)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "84ec0b48-f6b4-495c-93b8-8e42d1a8b0df",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"\n",
|
||||
"One can access to the specific contents of the simulation outcome."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "c0443efc-21ef-4ed5-9cf4-785d204a1881",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Probabilities:\n",
|
||||
" [0.0717492 0.10184806 0.0946686 0.07571277]\n",
|
||||
"\n",
|
||||
"State:\n",
|
||||
" [ 0.08809627-0.27595005j 0.24859731-0.22695421j 0.18807826+0.18988408j\n",
|
||||
" 0.09444097+0.06846085j 0.00470148+0.30764671j 0.17371395-0.09247188j\n",
|
||||
" -0.18900305+0.12545316j -0.17359753+0.20399288j -0.0517478 +0.04471215j\n",
|
||||
" -0.0411739 -0.06230031j 0.22377064+0.07842041j -0.21784975-0.27541439j\n",
|
||||
" -0.27208941+0.04098933j -0.22748127+0.04185292j 0.17105258-0.10503745j\n",
|
||||
" -0.01729753-0.31866731j]\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(f\"Probabilities:\\n {outcome.probabilities()}\\n\")\n",
|
||||
"print(f\"State:\\n {outcome.state()}\\n\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9f477388-ca45-409a-a0ce-6603ec294e42",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"\n",
|
||||
"But frequencies cannot be accessed, since no shots have been set."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8e9413c7-602a-44ed-a50c-1c3dd4dd7494",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"\n",
|
||||
"We can then repeat the execution by setting the number of shots"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "68122cd3-662f-4fd1-bb9c-d33b6f5448dd",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'nqubits': 4,\n",
|
||||
" 'backend': QMatchaTeaBackend(),\n",
|
||||
" 'measures': {'0000': 92,\n",
|
||||
" '0001': 7,\n",
|
||||
" '0010': 85,\n",
|
||||
" '0011': 79,\n",
|
||||
" '0100': 81,\n",
|
||||
" '0101': 55,\n",
|
||||
" '0110': 47,\n",
|
||||
" '0111': 39,\n",
|
||||
" '1000': 117,\n",
|
||||
" '1001': 7,\n",
|
||||
" '1010': 38,\n",
|
||||
" '1011': 53,\n",
|
||||
" '1100': 22,\n",
|
||||
" '1101': 129,\n",
|
||||
" '1110': 74,\n",
|
||||
" '1111': 99},\n",
|
||||
" 'measured_probabilities': {'U': [None],\n",
|
||||
" 'E': {'0000': 0.08390937969317301,\n",
|
||||
" '0010': 0.09466860481989439,\n",
|
||||
" '0011': 0.07571277233522165,\n",
|
||||
" '0100': 0.07142939529687124,\n",
|
||||
" '0101': 0.05622305772698632,\n",
|
||||
" '0110': 0.05146064807369245,\n",
|
||||
" '1000': 0.11330883548333581,\n",
|
||||
" '1011': 0.053499396925872765,\n",
|
||||
" '1101': 0.12331159869893296,\n",
|
||||
" '1110': 0.07174919872960005,\n",
|
||||
" '1111': 0.10184806171792007},\n",
|
||||
" 'G': [None]},\n",
|
||||
" 'prob_type': 'E',\n",
|
||||
" 'statevector': array([ 0.08809627-0.27595005j, 0.24859731-0.22695421j,\n",
|
||||
" 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": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Execution with a specific probability type\n",
|
||||
"# We use here \"E\", which is cutting some of the components if under a threshold\n",
|
||||
"outcome = qmatcha_backend.execute_circuit(\n",
|
||||
" circuit=circuit,\n",
|
||||
" nshots=1024,\n",
|
||||
" prob_type=\"E\",\n",
|
||||
" prob_threshold=0.05,\n",
|
||||
" return_array=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Print outcome\n",
|
||||
"vars(outcome)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "ef0e9591-ccca-4cdd-a81b-2bfb3caaf3d0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Frequencies:\n",
|
||||
" {'0000': 92, '0001': 7, '0010': 85, '0011': 79, '0100': 81, '0101': 55, '0110': 47, '0111': 39, '1000': 117, '1001': 7, '1010': 38, '1011': 53, '1100': 22, '1101': 129, '1110': 74, '1111': 99}\n",
|
||||
"\n",
|
||||
"Probabilities:\n",
|
||||
" [0.08390938 0.0946686 0.07571277 0.0714294 0.05622306 0.05146065\n",
|
||||
" 0.11330884 0.0534994 0.1233116 0.0717492 0.10184806]\n",
|
||||
"\n",
|
||||
"State:\n",
|
||||
" [ 0.08809627-0.27595005j 0.24859731-0.22695421j 0.18807826+0.18988408j\n",
|
||||
" 0.09444097+0.06846085j 0.00470148+0.30764671j 0.17371395-0.09247188j\n",
|
||||
" -0.18900305+0.12545316j -0.17359753+0.20399288j -0.0517478 +0.04471215j\n",
|
||||
" -0.0411739 -0.06230031j 0.22377064+0.07842041j -0.21784975-0.27541439j\n",
|
||||
" -0.27208941+0.04098933j -0.22748127+0.04185292j 0.17105258-0.10503745j\n",
|
||||
" -0.01729753-0.31866731j]\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Frequencies and probabilities\n",
|
||||
"print(f\"Frequencies:\\n {outcome.frequencies()}\\n\")\n",
|
||||
"print(f\"Probabilities:\\n {outcome.probabilities()}\\n\")\n",
|
||||
"print(f\"State:\\n {outcome.state()}\\n\") # Only if return_array = True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "dd84f1f3-7aa5-4ad1-ae09-81e0aff75b5b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Compute expectation values\n",
|
||||
"\n",
|
||||
"Another important feature of this backend is the `expectation` function. In fact, we can compute expectation values of given observables thorugh a Qibo-friendly interface.\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n",
|
||||
"Let's start by importing some symbols, thanks to which we can build our observable."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "0b46e315-7786-4247-bd2a-83ea1c5842eb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from qibo.symbols import Z, X"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "37385485-e8a3-4ab0-ad44-bcc4e9da24ca",
|
||||
"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": [
|
||||
"# We are going to compute the expval of an Hamiltonian\n",
|
||||
"# On the state prepared by the following circuit\n",
|
||||
"circuit.draw()\n",
|
||||
"\n",
|
||||
"circuit.set_parameters(\n",
|
||||
" np.random.randn(len(circuit.get_parameters()))\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "ddecc910-7804-4199-8577-a7db38a16db8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[Qibo 0.2.15|INFO|2025-02-12 14:36:17]: 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": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 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": 15,
|
||||
"id": "163b70a3-814a-4a62-a98a-2ffca933a544",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.4355195352502318"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# And compute its expectation value\n",
|
||||
"qmatcha_backend.expectation(\n",
|
||||
" circuit=circuit,\n",
|
||||
" observable=hamiltonian,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "2d8c4a9c-eca3-49d0-bdbf-ab054172c4e5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.43551953525022985"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Try with Qibo (which is by default using the Qibojit backend)\n",
|
||||
"hamiltonian = hamiltonians.SymbolicHamiltonian(form)\n",
|
||||
"hamiltonian.expectation(circuit().state())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "94df291c-9ddc-4b2e-8442-5fca00784bd8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"They match! 🥳"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
572
examples/quimb_intro/quimb_introduction.ipynb
Normal file
572
examples/quimb_intro/quimb_introduction.ipynb
Normal file
@@ -0,0 +1,572 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "656bb283-ac6d-48d2-a029-3c417c9961f8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Introduction to Quimb backend in QiboTN\n",
|
||||
"\n",
|
||||
"#### Some imports"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "6722d94e-e311-48f9-b6df-c6d829bf67fb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import time\n",
|
||||
"import numpy as np\n",
|
||||
"# from scipy import stats\n",
|
||||
"\n",
|
||||
"# import qibo\n",
|
||||
"from qibo import Circuit, gates, hamiltonians\n",
|
||||
"from qibo.backends import construct_backend"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0c5a8939",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Some hyper parameters"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "64162116-1555-4a68-811c-01593739d622",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# construct qibotn backend\n",
|
||||
"quimb_backend = construct_backend(backend=\"qibotn\", platform=\"quimb\")\n",
|
||||
"\n",
|
||||
"# set number of qubits\n",
|
||||
"nqubits = 4\n",
|
||||
"\n",
|
||||
"# set numpy random seed\n",
|
||||
"np.random.seed(42)\n",
|
||||
"\n",
|
||||
"quimb_backend.setup_backend_specifics(quimb_backend=\"jax\", contractions_optimizer='auto-hq')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "926cfea5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Quimb accepts different methods for optimizing the way it does contractions, that we pass through \"contractions_optimizer\". \n",
|
||||
"We could also define our own cotengra contraction optimizer! \n",
|
||||
"\n",
|
||||
"cotengra is a Python library designed for **optimising contraction trees** and performing efficient contractions of large tensor‐networks.\n",
|
||||
"You can find it here: [https://github.com/jcmgray/cotengra](https://github.com/jcmgray/cotengra)\n",
|
||||
"\n",
|
||||
"For the sake of this tutorial however the default \"auto-hq\" will be fine :) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b0a1da82",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import cotengra as ctg\n",
|
||||
"ctg_opt = ctg.ReusableHyperOptimizer(\n",
|
||||
" max_time=10,\n",
|
||||
" minimize='combo',\n",
|
||||
" slicing_opts=None,\n",
|
||||
" parallel=True,\n",
|
||||
" progbar=True\n",
|
||||
")\n",
|
||||
"# quimb_backend.setup_backend_specifics(quimb_backend=\"jax\", contractions_optimizer='ctg_opt')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "252f5cd1-5932-4de6-8076-4a357d50ebad",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Constructing a parametric quantum circuit"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "4a22a172-f50d-411d-afa3-fa61937c7b3a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "76f23c57-6d08-496b-9a27-52fb63bbfcb1",
|
||||
"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": [
|
||||
"circuit = build_circuit(nqubits=nqubits, nlayers=3)\n",
|
||||
"circuit.draw()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "07b2c097-cea2-42ec-8f1d-b4bbb5b71d98",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Setting random parameters\n",
|
||||
"circuit.set_parameters(\n",
|
||||
" parameters=np.random.uniform(-np.pi, np.pi, len(circuit.get_parameters())),\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fd0cea52-03f5-4366-a01a-a5a84aa8ebc7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Setting up the tensor network simulator\n",
|
||||
"\n",
|
||||
"Depending on the simulator, various parameters can be set. One can customize the tensor network execution via the `backend.configure_tn_simulation` function, whose face depends on the specific backend provider."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "2ee03e94-d794-4a51-9e76-01e8d8a259ba",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Customization of the tensor network simulation in the case of quimb backend\n",
|
||||
"# Here we use only some of the possible arguments\n",
|
||||
"quimb_backend.configure_tn_simulation(\n",
|
||||
" #ansatz=\"MPS\",\n",
|
||||
" max_bond_dimension=10\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "648d85b8-445d-4081-aeed-1691fbae67be",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Executing through the backend\n",
|
||||
"\n",
|
||||
"The `backend.execute_circuit` method can be used then. We can simulate results in three ways:\n",
|
||||
"1. reconstruction of the final state only if `return_array` is set to `True`;\n",
|
||||
"2. computation of the relevant probabilities of the final state.\n",
|
||||
"3. reconstruction of the relevant state's frequencies (only if `nshots` is not `None`)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "35a244c3-adba-4b8b-b28c-0ab592b0f7cf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/andrea/python_envs/3.11/lib/python3.11/site-packages/quimb/tensor/circuit.py:215: SyntaxWarning: Unsupported operation ignored: creg\n",
|
||||
" warnings.warn(\n",
|
||||
"/home/andrea/python_envs/3.11/lib/python3.11/site-packages/quimb/tensor/circuit.py:215: SyntaxWarning: Unsupported operation ignored: measure\n",
|
||||
" warnings.warn(\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'nqubits': 4,\n",
|
||||
" 'backend': qibotn (quimb),\n",
|
||||
" 'measures': Counter({'1101': 14,\n",
|
||||
" '1000': 12,\n",
|
||||
" '0010': 11,\n",
|
||||
" '0011': 11,\n",
|
||||
" '0110': 9,\n",
|
||||
" '0000': 8,\n",
|
||||
" '1010': 7,\n",
|
||||
" '1110': 6,\n",
|
||||
" '0100': 5,\n",
|
||||
" '1111': 5,\n",
|
||||
" '1011': 5,\n",
|
||||
" '0101': 4,\n",
|
||||
" '0111': 1,\n",
|
||||
" '0001': 1,\n",
|
||||
" '1100': 1}),\n",
|
||||
" 'measured_probabilities': {'1101': np.float64(0.12331159869893284),\n",
|
||||
" '1000': np.float64(0.11330883548333684),\n",
|
||||
" '0010': np.float64(0.0946686048198943),\n",
|
||||
" '0011': np.float64(0.07571277233522157),\n",
|
||||
" '0110': np.float64(0.051460648073692314),\n",
|
||||
" '0000': np.float64(0.08390937969317334),\n",
|
||||
" '1010': np.float64(0.03872758515126775),\n",
|
||||
" '1110': np.float64(0.07174919872960006),\n",
|
||||
" '0100': np.float64(0.07142939529687146),\n",
|
||||
" '1111': np.float64(0.10184806171791994),\n",
|
||||
" '1011': np.float64(0.053499396925872716),\n",
|
||||
" '0101': np.float64(0.05622305772698606),\n",
|
||||
" '0111': np.float64(0.040291850747292815),\n",
|
||||
" '0001': np.float64(0.004677011195208322),\n",
|
||||
" '1100': np.float64(0.013605984872668443)},\n",
|
||||
" 'prob_type': 'default',\n",
|
||||
" 'statevector': Array([[ 0.08809626-0.27595j ],\n",
|
||||
" [-0.05174781+0.04471214j],\n",
|
||||
" [ 0.00470146+0.30764672j],\n",
|
||||
" [-0.27208942+0.04098931j],\n",
|
||||
" [ 0.18807825+0.1898841j ],\n",
|
||||
" [ 0.22377063+0.07842041j],\n",
|
||||
" [-0.18900302+0.12545316j],\n",
|
||||
" [ 0.17105258-0.10503745j],\n",
|
||||
" [ 0.24859732-0.22695422j],\n",
|
||||
" [-0.04117391-0.0623003j ],\n",
|
||||
" [ 0.17371394-0.09247189j],\n",
|
||||
" [-0.22748126+0.04185291j],\n",
|
||||
" [ 0.09444097+0.06846087j],\n",
|
||||
" [-0.21784975-0.2754144j ],\n",
|
||||
" [-0.17359754+0.20399287j],\n",
|
||||
" [-0.01729751-0.31866732j]], dtype=complex64)}"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# # Simple execution (defaults)\n",
|
||||
"outcome = quimb_backend.execute_circuit(circuit=circuit, nshots=100, return_array=True)\n",
|
||||
"\n",
|
||||
"# # Print outcome\n",
|
||||
"vars(outcome)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "84ec0b48-f6b4-495c-93b8-8e42d1a8b0df",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"\n",
|
||||
"One can access to the specific contents of the simulation outcome."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "c0443efc-21ef-4ed5-9cf4-785d204a1881",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Probabilities:\n",
|
||||
" {'1101': np.float64(0.12331159869893284), '1000': np.float64(0.11330883548333684), '0010': np.float64(0.0946686048198943), '0011': np.float64(0.07571277233522157), '0110': np.float64(0.051460648073692314), '0000': np.float64(0.08390937969317334), '1010': np.float64(0.03872758515126775), '1110': np.float64(0.07174919872960006), '0100': np.float64(0.07142939529687146), '1111': np.float64(0.10184806171791994), '1011': np.float64(0.053499396925872716), '0101': np.float64(0.05622305772698606), '0111': np.float64(0.040291850747292815), '0001': np.float64(0.004677011195208322), '1100': np.float64(0.013605984872668443)}\n",
|
||||
"\n",
|
||||
"State:\n",
|
||||
" [[ 0.08809626-0.27595j ]\n",
|
||||
" [-0.05174781+0.04471214j]\n",
|
||||
" [ 0.00470146+0.30764672j]\n",
|
||||
" [-0.27208942+0.04098931j]\n",
|
||||
" [ 0.18807825+0.1898841j ]\n",
|
||||
" [ 0.22377063+0.07842041j]\n",
|
||||
" [-0.18900302+0.12545316j]\n",
|
||||
" [ 0.17105258-0.10503745j]\n",
|
||||
" [ 0.24859732-0.22695422j]\n",
|
||||
" [-0.04117391-0.0623003j ]\n",
|
||||
" [ 0.17371394-0.09247189j]\n",
|
||||
" [-0.22748126+0.04185291j]\n",
|
||||
" [ 0.09444097+0.06846087j]\n",
|
||||
" [-0.21784975-0.2754144j ]\n",
|
||||
" [-0.17359754+0.20399287j]\n",
|
||||
" [-0.01729751-0.31866732j]]\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(f\"Probabilities:\\n {outcome.probabilities()}\\n\")\n",
|
||||
"print(f\"State:\\n {outcome.state()}\\n\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9531f9d6",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Compute expectation values\n",
|
||||
"\n",
|
||||
"Another important feature of this backend is the `expectation` function. In fact, we can compute expectation values of given observables thorugh a Qibo-friendly interface.\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n",
|
||||
"Let's start by importing some symbols, thanks to which we can build our observable."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "647f2073",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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",
|
||||
"quimb_backend.setup_backend_specifics(\n",
|
||||
" quimb_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": 18,
|
||||
"id": "b2a0decb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from qibo.symbols import X, Z, Y\n",
|
||||
"from qibo.hamiltonians import XXZ\n",
|
||||
"\n",
|
||||
"# define Hamiltonian\n",
|
||||
"hamiltonian = XXZ(4, dense=False, backend=quimb_backend)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"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: 2.0\n",
|
||||
"Elapsed time: 0.0268 seconds\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"start = time.time()\n",
|
||||
"expval = hamiltonian.expectation(circuit)\n",
|
||||
"\n",
|
||||
"elapsed = time.time() - start\n",
|
||||
"print(f\"Expectation value: {expval}\")\n",
|
||||
"print(f\"Elapsed time: {elapsed:.4f} seconds\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d976a849",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Try with Qibo (which is by default using the Qibojit backend)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "fb1436c8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[Qibo 0.2.21|INFO|2025-10-27 16:24:00]: Using numpy backend on /CPU:0\n",
|
||||
"WARNING:root:Calculation of expectation values starting from the state is deprecated, use the ``expectation_from_state`` method if you really need it, or simply pass the circuit you want to calculate the expectation value from.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Expectation value: 2.0\n",
|
||||
"Elapsed time: 0.0360 seconds\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sym_hamiltonian = XXZ(4, dense=False, backend=None)\n",
|
||||
"\n",
|
||||
"# Let's show it\n",
|
||||
"sym_hamiltonian.form\n",
|
||||
"\n",
|
||||
"# Compute expectation value\n",
|
||||
"start = time.time()\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\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "77bef077",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"They match! 🥳"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "50130ae6",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can also compute gradient of expectation function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"id": "6a3b26e4",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/andrea/python_envs/3.11/lib/python3.11/site-packages/quimb/tensor/circuit.py:4927: UserWarning: Unsupported options for computing local_expectation with an MPS circuit supplied, ignoring: R, None, None, jax, None\n",
|
||||
" warnings.warn(\n",
|
||||
"/home/andrea/python_envs/3.11/lib/python3.11/site-packages/quimb/tensor/circuit.py:4927: UserWarning: Unsupported options for computing local_expectation with an MPS circuit supplied, ignoring: R, None, None, jax, None\n",
|
||||
" warnings.warn(\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[-0.24630009 0.8370421 -0.11103702 -0.12855841 0.41325414 -0.0628037\n",
|
||||
" 0.51638705 0.794163 -0.27972788 -1.0718998 0.02731732 1.0153619\n",
|
||||
" -0.34494495 1.5744264 0.26920277 -0.36333832 0.12331417 0.5196531\n",
|
||||
" 1.1294655 0.29257926 -0.18237355 0.8914014 -0.9471657 0.3492473\n",
|
||||
" -0.3477673 0.24325958 0.04818404 -0.87983793 0.47196424 0.36605012\n",
|
||||
" 1.005 0.65054715 -0.94860053 0.14459445 0.36571163 -0.2550101 ]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def f(circuit, hamiltonian, params):\n",
|
||||
" circuit.set_parameters(params)\n",
|
||||
" return hamiltonian.expectation(\n",
|
||||
" circuit=circuit,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"parameters = np.random.uniform(-np.pi, np.pi, size=len(circuit.get_parameters()))\n",
|
||||
"print(jax.grad(f, argnums=2)(circuit, hamiltonian, parameters))\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "aeafa5a6-2afa-429c-a101-effa84bac1d2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user