{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "IYul_lq4tn3D" }, "source": [ "##### Copyright 2020 The OpenFermion Developers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2025-05-10T09:51:19.763190Z", "iopub.status.busy": "2025-05-10T09:51:19.762514Z", "iopub.status.idle": "2025-05-10T09:51:19.766885Z", "shell.execute_reply": "2025-05-10T09:51:19.766258Z" }, "id": "sP7GfWPstpBt" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "a43053f38ce0" }, "source": [ "# Introduction to the bosonic operators" ] }, { "cell_type": "markdown", "metadata": { "id": "8R3_GRhvt88K" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " View on QuantumAI\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", " \n", " Download notebook\n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "n5kB9jHTuI6B" }, "source": [ "## Setup\n", "\n", "Install the OpenFermion package:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:19.770842Z", "iopub.status.busy": "2025-05-10T09:51:19.770145Z", "iopub.status.idle": "2025-05-10T09:51:41.573093Z", "shell.execute_reply": "2025-05-10T09:51:41.572079Z" }, "id": "5cbe6b680387" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting openfermion\r\n", " Cloning https://github.com/quantumlib/OpenFermion.git (to revision master) to /tmpfs/tmp/pip-install-6qqdm9xj/openfermion_6eee17c023714e439223e49614e99339\r\n", " Running command git clone --filter=blob:none --quiet https://github.com/quantumlib/OpenFermion.git /tmpfs/tmp/pip-install-6qqdm9xj/openfermion_6eee17c023714e439223e49614e99339\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Resolved https://github.com/quantumlib/OpenFermion.git to commit 9423933ed05a150f9681e9a1e9cac40e29c81ead\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Installing build dependencies ... \u001b[?25l-" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \b\\" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \bdone\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[?25h Getting requirements to build wheel ... \u001b[?25l-" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \bdone\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25l-" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \bdone\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[?25hCollecting cirq-core (from openfermion)\r\n", " Using cached cirq_core-1.5.0-py3-none-any.whl.metadata (4.9 kB)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting deprecation (from openfermion)\r\n", " Using cached deprecation-2.1.0-py2.py3-none-any.whl.metadata (4.6 kB)\r\n", "Requirement already satisfied: h5py>=3.10.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (3.13.0)\r\n", "Requirement already satisfied: networkx in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (3.4.2)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting numpy<2.0,>=1.24 (from openfermion)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Using cached numpy-1.26.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (61 kB)\r\n", "Collecting pubchempy (from openfermion)\r\n", " Using cached pubchempy-1.0.4-py3-none-any.whl\r\n", "Requirement already satisfied: requests~=2.32.2 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (2.32.3)\r\n", "Requirement already satisfied: scipy~=1.15 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (1.15.3)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting sympy (from openfermion)\r\n", " Using cached sympy-1.14.0-py3-none-any.whl.metadata (12 kB)\r\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests~=2.32.2->openfermion) (3.4.2)\r\n", "Requirement already satisfied: idna<4,>=2.5 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests~=2.32.2->openfermion) (3.10)\r\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests~=2.32.2->openfermion) (2.4.0)\r\n", "Requirement already satisfied: certifi>=2017.4.17 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests~=2.32.2->openfermion) (2025.4.26)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: attrs>=21.3.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core->openfermion) (25.3.0)\r\n", "Collecting duet>=0.2.8 (from cirq-core->openfermion)\r\n", " Using cached duet-0.2.9-py3-none-any.whl.metadata (2.3 kB)\r\n", "Requirement already satisfied: matplotlib~=3.7 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core->openfermion) (3.10.3)\r\n", "Requirement already satisfied: pandas~=2.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core->openfermion) (2.2.3)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting sortedcontainers~=2.0 (from cirq-core->openfermion)\r\n", " Using cached sortedcontainers-2.4.0-py2.py3-none-any.whl.metadata (10 kB)\r\n", "Requirement already satisfied: typing_extensions>=4.2 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core->openfermion) (4.13.2)\r\n", "Requirement already satisfied: tqdm>=4.12 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core->openfermion) (4.67.1)\r\n", "Requirement already satisfied: contourpy>=1.0.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (1.3.2)\r\n", "Requirement already satisfied: cycler>=0.10 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (0.12.1)\r\n", "Requirement already satisfied: fonttools>=4.22.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (4.57.0)\r\n", "Requirement already satisfied: kiwisolver>=1.3.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (1.4.8)\r\n", "Requirement already satisfied: packaging>=20.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (25.0)\r\n", "Requirement already satisfied: pillow>=8 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (11.2.1)\r\n", "Requirement already satisfied: pyparsing>=2.3.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (3.2.3)\r\n", "Requirement already satisfied: python-dateutil>=2.7 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.7->cirq-core->openfermion) (2.9.0.post0)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pytz>=2020.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from pandas~=2.0->cirq-core->openfermion) (2025.2)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: tzdata>=2022.7 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from pandas~=2.0->cirq-core->openfermion) (2025.2)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: six>=1.5 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib~=3.7->cirq-core->openfermion) (1.17.0)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting mpmath<1.4,>=1.1.0 (from sympy->openfermion)\r\n", " Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)\r\n", "Using cached numpy-1.26.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)\r\n", "Using cached cirq_core-1.5.0-py3-none-any.whl (2.0 MB)\r\n", "Using cached sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)\r\n", "Using cached duet-0.2.9-py3-none-any.whl (29 kB)\r\n", "Using cached deprecation-2.1.0-py2.py3-none-any.whl (11 kB)\r\n", "Using cached sympy-1.14.0-py3-none-any.whl (6.3 MB)\r\n", "Using cached mpmath-1.3.0-py3-none-any.whl (536 kB)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Building wheels for collected packages: openfermion\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Building wheel for openfermion (pyproject.toml) ... \u001b[?25l-" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \b\\" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \b|" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \b/" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \b-" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \b\\" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \b|" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b \bdone\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[?25h Created wheel for openfermion: filename=openfermion-1.7.1.dev0-py3-none-any.whl size=44717459 sha256=1f31b0f6b06441f2fa14257620981b73c9a7b6c4c10bcf47257c7c8830e65edf\r\n", " Stored in directory: /tmpfs/tmp/pip-ephem-wheel-cache-17cpyj0a/wheels/7a/93/a6/5746203c4afd3b6e8d40a4edb587d608c51290286c5680f422\r\n", "Successfully built openfermion\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Installing collected packages: sortedcontainers, pubchempy, mpmath, sympy, numpy, duet, deprecation, cirq-core, openfermion\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Attempting uninstall: numpy\r\n", " Found existing installation: numpy 2.1.3\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Uninstalling numpy-2.1.3:\r\n", " Successfully uninstalled numpy-2.1.3\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully installed cirq-core-1.5.0 deprecation-2.1.0 duet-0.2.9 mpmath-1.3.0 numpy-1.26.4 openfermion-1.7.1.dev0 pubchempy-1.0.4 sortedcontainers-2.4.0 sympy-1.14.0\r\n" ] } ], "source": [ "try:\n", " import openfermion\n", "except ImportError:\n", " !pip install git+https://github.com/quantumlib/OpenFermion.git@master#egg=openfermion" ] }, { "cell_type": "markdown", "metadata": { "id": "952ae604e015" }, "source": [ "## The BosonOperator" ] }, { "cell_type": "markdown", "metadata": { "id": "e41da94a83e0" }, "source": [ "Bosonic systems, like Fermionic systems, are expressed using the bosonic creation and annihilation operators $b^\\dagger_k$ and $b_k$ respectively. Unlike fermions, however, which satisfy the Pauli exclusion principle and thus are distinguished by the canonical fermionic anticommutation relations, the bosonic ladder operators instead satisfy a set of commutation relations:\n", "\n", "$$\n", "\\begin{align}\n", "& [b_i^\\dagger, b_j^\\dagger] = 0, ~~~ [b_i, b_j] = 0, ~~~ [b_i, b^\\dagger_j] = \\delta_{ij}\n", "\\end{align}\n", "$$\n", "\n", "Any weighted sums of products of these operators are represented with the `BosonOperator` data structure in OpenFermion. Similarly to when we introduced the `FermionOperator`, the following are examples of valid `BosonOperator`s:\n", "\n", "$$\n", "\\begin{align}\n", "& a_1 \\nonumber \\\\\n", "& 1.7 b^\\dagger_3 \\nonumber \\\\\n", "&-1.7 \\, b^\\dagger_3 b_1 \\nonumber \\\\\n", "&(1 + 2i) \\, b^\\dagger_3 b^\\dagger_4 b_1 b_9 \\nonumber \\\\\n", "&(1 + 2i) \\, b^\\dagger_3 b^\\dagger_4 b_1 b_9 - 1.7 \\, b^\\dagger_3 b_1 \\nonumber\n", "\\end{align}\n", "$$\n", "\n", "The `BosonOperator` class is contained in `ops/_boson_operators.py`. The `BosonOperator` is derived from the `SymbolicOperator`, the same class that derives the `FermionOperator`. As such, the details of the class implementation are identical - as in the fermion case, the class is implemented as hash table (python dictionary). The keys of the dictionary encode the strings of ladder operators and values of the dictionary store the coefficients - the strings are subsequently encoded as a tuple of 2-tuples which we refer to as the \"terms tuple\".\n", "\n", "\n", "Each ladder operator is represented by a 2-tuple. The first element of the 2-tuple is an int indicating the quantum mode on which the ladder operator acts. The second element of the 2-tuple is Boole: 1 represents raising and 0 represents lowering. For instance, $b^\\dagger_8$ is represented in a 2-tuple as $(8, 1)$. Note that indices start at 0 and the identity operator is an empty list. \n", "\n", "$$\n", "\\begin{align}\n", "I & \\mapsto () \\nonumber \\\\\n", "b_1 & \\mapsto ((1, 0),) \\nonumber \\\\\n", "b^\\dagger_3 & \\mapsto ((3, 1),) \\nonumber \\\\\n", "b^\\dagger_3 b_1 & \\mapsto ((3, 1), (1, 0)) \\nonumber \\\\\n", "b^\\dagger_3 b^\\dagger_4 b_1 b_9 & \\mapsto ((3, 1), (4, 1), (1, 0), (9, 0)) \\nonumber\n", "\\end{align}\n", "$$\n", "\n", "Alternatively, the `BosonOperator` supports the string-based syntax introduced in the `FermionOperator`; in this case, the terms are separated by spaces, with the integer corresponding to the quantum mode the operator acts on, and `'^'` indicating the Hermitian conjugate:\n", "\n", "$$\n", "\\begin{align}\n", "I & \\mapsto \\textrm{\"\"} \\nonumber \\\\\n", "b_1 & \\mapsto \\textrm{\"1\"} \\nonumber \\\\\n", "b^\\dagger_3 & \\mapsto \\textrm{\"3^\"} \\nonumber \\\\\n", "b^\\dagger_3 b_1 & \\mapsto \\textrm{\"3^}\\;\\textrm{1\"} \\nonumber \\\\\n", "b^\\dagger_3 b^\\dagger_4 b_1 b_9 & \\mapsto \\textrm{\"3^}\\;\\textrm{4^}\\;\\textrm{1}\\;\\textrm{9\"} \\nonumber\n", "\\end{align}\n", "$$\n", "\n", "
\n", " Note that, unlike the `FermionOperator`, the bosonic creation operators of different indices commute. As a result, the `BosonOperator` automatically sorts groups of annihilation and creation operators in ascending order of the modes they act on.\n", "
\n", "\n", "\n", "Let's initialize our first term! We do it two different ways below." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:41.577785Z", "iopub.status.busy": "2025-05-10T09:51:41.576952Z", "iopub.status.idle": "2025-05-10T09:51:43.570656Z", "shell.execute_reply": "2025-05-10T09:51:43.569580Z" }, "id": "39d1ec24c9d5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0 [1 3^ 4^ 5]\n", "1.0 [1 3^ 4^ 5]\n" ] } ], "source": [ "from openfermion.ops import BosonOperator\n", "\n", "my_term = BosonOperator(((3, 1), (5, 0), (4, 1), (1, 0)))\n", "print(my_term)\n", "\n", "my_term = BosonOperator('3^ 5 4^ 1')\n", "print(my_term)" ] }, { "cell_type": "markdown", "metadata": { "id": "0a9e12ccd92d" }, "source": [ "Note the printed order differs from the code, since bosonic operators of different indices commute past each other.\n", "\n", "The preferred way to specify the coefficient in openfermion is to provide an optional coefficient argument. If not provided, the coefficient defaults to 1. In the code below, the first method is preferred. The multiplication in the second method actually creates a copy of the term, which introduces some additional cost. All inplace operands (such as +=) modify classes whereas binary operands such as + create copies.\n", "\n", "The additive and multiplicative identities can also be created:\n", "\n", "* `BosonOperator(())` and `BosonOperator('')` initialises the identity (`BosonOperator.identity()`).\n", "* `BosonOperator()` and `BosonOperator()` initialises the zero operator (`BosonOperator.zero()`)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.574132Z", "iopub.status.busy": "2025-05-10T09:51:43.573557Z", "iopub.status.idle": "2025-05-10T09:51:43.580520Z", "shell.execute_reply": "2025-05-10T09:51:43.579493Z" }, "id": "dac42b48422e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1.7 [1 3^]\n", "-1.7 [1 3^]\n", "True\n", "1.0 []\n", "True\n", "0\n" ] } ], "source": [ "good_way_to_initialize = BosonOperator('3^ 1', -1.7)\n", "print(good_way_to_initialize)\n", "\n", "bad_way_to_initialize = -1.7 * BosonOperator('3^ 1')\n", "print(bad_way_to_initialize)\n", "\n", "identity = BosonOperator('')\n", "print(identity == BosonOperator.identity())\n", "print(identity)\n", "\n", "zero_operator = BosonOperator()\n", "print(zero_operator == BosonOperator.zero())\n", "print(zero_operator)" ] }, { "cell_type": "markdown", "metadata": { "id": "97987980c581" }, "source": [ "Note that `BosonOperator` has only one attribute: .terms. This attribute is the dictionary which stores the term tuples." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.583769Z", "iopub.status.busy": "2025-05-10T09:51:43.583450Z", "iopub.status.idle": "2025-05-10T09:51:43.588414Z", "shell.execute_reply": "2025-05-10T09:51:43.587486Z" }, "id": "d0fdd98b0012" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1+2j) [1^ 3 4^ 9]\n", "{((1, 1), (3, 0), (4, 1), (9, 0)): (1+2j)}\n" ] } ], "source": [ "my_operator = BosonOperator('4^ 1^ 3 9', 1. + 2.j)\n", "print(my_operator)\n", "print(my_operator.terms)" ] }, { "cell_type": "markdown", "metadata": { "id": "dd0f0b072be5" }, "source": [ "### Methods and functions that act on the BosonOperator" ] }, { "cell_type": "markdown", "metadata": { "id": "532f85ad65ab" }, "source": [ "There are various functions and methods that act on the `BosonOperator`; these include the ability to normal order, double check if the operator is Hermitian, and calculate the Hermitian conjugate." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.591601Z", "iopub.status.busy": "2025-05-10T09:51:43.591288Z", "iopub.status.idle": "2025-05-10T09:51:43.595429Z", "shell.execute_reply": "2025-05-10T09:51:43.594464Z" }, "id": "52798c455b9b" }, "outputs": [], "source": [ "from openfermion.utils import hermitian_conjugated, is_hermitian\n", "from openfermion.transforms import normal_ordered" ] }, { "cell_type": "markdown", "metadata": { "id": "e5e898e7c471" }, "source": [ "`normal_ordered_boson` applies the bosonic commutation relations to write the operator using only normal-ordered terms; that is, that all creation operators are to the left of annihilation operators:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.598549Z", "iopub.status.busy": "2025-05-10T09:51:43.598015Z", "iopub.status.idle": "2025-05-10T09:51:43.605967Z", "shell.execute_reply": "2025-05-10T09:51:43.605069Z" }, "id": "0339bdaf4842" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = BosonOperator('0 0^', 1. + 2.j)\n", "H.is_normal_ordered()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.609285Z", "iopub.status.busy": "2025-05-10T09:51:43.608754Z", "iopub.status.idle": "2025-05-10T09:51:43.614634Z", "shell.execute_reply": "2025-05-10T09:51:43.613716Z" }, "id": "3d0f7bcb6a64" }, "outputs": [ { "data": { "text/plain": [ "(1+2j) [] +\n", "(1+2j) [0^ 0]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "normal_ordered(BosonOperator('0 0^', 1. + 2.j))" ] }, { "cell_type": "markdown", "metadata": { "id": "69378cf1928a" }, "source": [ "We can also use a boson operator method to check if the operator conserves the particle number - that is, for each qumode, the number of annihilation operators equals the number of creation operators." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.618041Z", "iopub.status.busy": "2025-05-10T09:51:43.617511Z", "iopub.status.idle": "2025-05-10T09:51:43.623096Z", "shell.execute_reply": "2025-05-10T09:51:43.622190Z" }, "id": "ef79ae4a3e1c" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H.is_boson_preserving()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.626263Z", "iopub.status.busy": "2025-05-10T09:51:43.625988Z", "iopub.status.idle": "2025-05-10T09:51:43.631490Z", "shell.execute_reply": "2025-05-10T09:51:43.630489Z" }, "id": "0c6858ea0499" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = BosonOperator('0 0^ 1^ ', 1. + 2.j)\n", "H.is_boson_preserving()" ] }, { "cell_type": "markdown", "metadata": { "id": "3dc0ec0da2e9" }, "source": [ "The Hermitian conjugated function returns the Hermitian conjugate of the operator, and its hermiticity can be checked using `is_hermitian`:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.634664Z", "iopub.status.busy": "2025-05-10T09:51:43.634379Z", "iopub.status.idle": "2025-05-10T09:51:43.639943Z", "shell.execute_reply": "2025-05-10T09:51:43.638961Z" }, "id": "b9af0096bc62" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_hermitian(H)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.643298Z", "iopub.status.busy": "2025-05-10T09:51:43.642796Z", "iopub.status.idle": "2025-05-10T09:51:43.648369Z", "shell.execute_reply": "2025-05-10T09:51:43.647462Z" }, "id": "6554a0e92519" }, "outputs": [ { "data": { "text/plain": [ "(1-2j) [0 0^ 1]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hermitian_conjugated(H)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.651485Z", "iopub.status.busy": "2025-05-10T09:51:43.650896Z", "iopub.status.idle": "2025-05-10T09:51:43.656011Z", "shell.execute_reply": "2025-05-10T09:51:43.655072Z" }, "id": "946e2e2eafa5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "0.5 [0 1^] +\n", "0.5 [0^ 1]\n" ] } ], "source": [ "H = BosonOperator('0 1^', 1/2.)\n", "H += BosonOperator('1 0^', 1/2.)\n", "print(is_hermitian(H))\n", "print(hermitian_conjugated(H))" ] }, { "cell_type": "markdown", "metadata": { "id": "5421d9012891" }, "source": [ "## The QuadOperator" ] }, { "cell_type": "markdown", "metadata": { "id": "d2e01ab8e1a2" }, "source": [ "Using the bosonic ladder operators, it is common to define the canonical position and momentum operators $\\hat{q}$ and $\\hat{p}$:\n", "\n", "$$ \\hat{q}_i = \\sqrt{\\frac{\\hbar}{2}}(\\hat{b}_i+\\hat{b}^\\dagger_i), ~~~ \\hat{p}_i = -i\\sqrt{\\frac{\\hbar}{2}}(\\hat{b}_i-\\hat{b}^\\dagger_i)$$\n", "\n", "These operators are Hermitian, and are referred to as the phase space quadrature operators. They satisfy the canonical commutation relation\n", "\n", "$$ [\\hat{q}_i, \\hat{p}_j] = \\delta_{ij}i\\hbar$$\n", "\n", "where the value of $\\hbar$ depends on convention, often taking values $\\hbar=0.5$, $1$, or $2$.\n", "\n", "In OpenFermion, the quadrature operators are represented by the `QuadOperator` class, and stored as a dictionary of tuples (as keys) and coefficients (as values). For example, the multi-mode quadrature operator $q_0 p_1 q_3$ is represented internally as `((0, 'q'), (1, 'p'), (3, 'q'))`. Alternatively, `QuadOperators` also support string input - using string input, the same operator is described by `'q0 p1 q3'`." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.659802Z", "iopub.status.busy": "2025-05-10T09:51:43.659080Z", "iopub.status.idle": "2025-05-10T09:51:43.664838Z", "shell.execute_reply": "2025-05-10T09:51:43.663867Z" }, "id": "41892572d462" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0 [q0 p1 q3]\n", "{((0, 'q'), (1, 'p'), (3, 'q')): 1.0}\n", "\n", "-77.0 [q0 p1 q3] +\n", "3.17 [q3 p4]\n" ] } ], "source": [ "from openfermion.ops import QuadOperator\n", "\n", "H = QuadOperator('q0 p1 q3')\n", "print(H)\n", "print(H.terms)\n", "\n", "H2 = QuadOperator('q3 p4', 3.17)\n", "H2 -= 77. * H\n", "print('')\n", "print(H2)" ] }, { "cell_type": "markdown", "metadata": { "id": "bd9cf3d47089" }, "source": [ "Note that quadrature operators of different indices commute; as such, like the `BosonOperator`, by default we sort quadrature operators such that the operators acting on the lowest numbered mode appear to the left." ] }, { "cell_type": "markdown", "metadata": { "id": "ab94957a0251" }, "source": [ "### Methods and functions that act on the QuadOperator\n" ] }, { "cell_type": "markdown", "metadata": { "id": "83ce3efcb3f8" }, "source": [ "Like the `BosonOperator`, there are various functions and methods that act on the `QuadOperator`; these include the ability to normal order, double check if the operator is Hermitian, and calculate the Hermitian conjugate." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.668739Z", "iopub.status.busy": "2025-05-10T09:51:43.667994Z", "iopub.status.idle": "2025-05-10T09:51:43.672312Z", "shell.execute_reply": "2025-05-10T09:51:43.671345Z" }, "id": "8a0b6b5a4b60" }, "outputs": [], "source": [ "from openfermion.utils import hermitian_conjugated, is_hermitian" ] }, { "cell_type": "markdown", "metadata": { "id": "af10c754c9bc" }, "source": [ "`normal_ordered_quad` is an arbitrary convention chosen in OpenFermion that allows us to compare two quadrature operators that might be equivalent, but written in different forms. It is simply defined as a quadrature operator that has all of the position operators $\\hat{q}$ to the left of the momentum operators $\\hat{q}$. All quadrature operators can be placed in this 'normal form' by making use of the canonical commutation relation." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.675639Z", "iopub.status.busy": "2025-05-10T09:51:43.675068Z", "iopub.status.idle": "2025-05-10T09:51:43.681278Z", "shell.execute_reply": "2025-05-10T09:51:43.680001Z" }, "id": "bd5ead97318e" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = QuadOperator('p0 q0', 1. + 2.j)\n", "H.is_normal_ordered()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.684442Z", "iopub.status.busy": "2025-05-10T09:51:43.683863Z", "iopub.status.idle": "2025-05-10T09:51:43.689506Z", "shell.execute_reply": "2025-05-10T09:51:43.688616Z" }, "id": "cdca60a49d9b" }, "outputs": [ { "data": { "text/plain": [ "(2-1j) [] +\n", "(1+2j) [q0 p0]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "normal_ordered(H)" ] }, { "cell_type": "markdown", "metadata": { "id": "1e9731609ee8" }, "source": [ "By default, we assume the value $\\hbar=1$ in the canonical commutation relation, but this can be modified by passing the `hbar` keyword argument to the function:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.692735Z", "iopub.status.busy": "2025-05-10T09:51:43.692425Z", "iopub.status.idle": "2025-05-10T09:51:43.698127Z", "shell.execute_reply": "2025-05-10T09:51:43.697262Z" }, "id": "9e3d12b35141" }, "outputs": [ { "data": { "text/plain": [ "(4-2j) [] +\n", "(1+2j) [q0 p0]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "normal_ordered(H, hbar=2)" ] }, { "cell_type": "markdown", "metadata": { "id": "fe4a46aa5dad" }, "source": [ "We can also use a quad operator method to check if the operator is **Gaussian** - that is, all terms in the quad operator are of quadratic order or lower:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.701317Z", "iopub.status.busy": "2025-05-10T09:51:43.701017Z", "iopub.status.idle": "2025-05-10T09:51:43.706666Z", "shell.execute_reply": "2025-05-10T09:51:43.705751Z" }, "id": "0c4253372d9d" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = QuadOperator('p0 q0', 1. + 2.j)\n", "H.is_gaussian()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.709997Z", "iopub.status.busy": "2025-05-10T09:51:43.709452Z", "iopub.status.idle": "2025-05-10T09:51:43.715123Z", "shell.execute_reply": "2025-05-10T09:51:43.714244Z" }, "id": "d2aa8581d1b1" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = QuadOperator('p0 q0 q1', 1. + 2.j)\n", "H.is_gaussian()" ] }, { "cell_type": "markdown", "metadata": { "id": "6df5bae47a58" }, "source": [ "The Hermitian conjugated function returns the Hermitian conjugate of the operator, and its hermiticity can be checked using `is_hermitian`:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.718211Z", "iopub.status.busy": "2025-05-10T09:51:43.717920Z", "iopub.status.idle": "2025-05-10T09:51:43.723553Z", "shell.execute_reply": "2025-05-10T09:51:43.722656Z" }, "id": "10029a18d3df" }, "outputs": [ { "data": { "text/plain": [ "(1+2j) [p0 p1 q1]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = QuadOperator('p0 q1 p1', 1-2j)\n", "hermitian_conjugated(H)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.726501Z", "iopub.status.busy": "2025-05-10T09:51:43.726232Z", "iopub.status.idle": "2025-05-10T09:51:43.731097Z", "shell.execute_reply": "2025-05-10T09:51:43.730149Z" }, "id": "954167974f66" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "-0.5 [p0 q0] +\n", "0.5 [q0 p0]\n" ] } ], "source": [ "H = QuadOperator('p0 q0', 1/2.)\n", "H += QuadOperator('q0 p0', -1/2.)\n", "print(is_hermitian(H))\n", "print(hermitian_conjugated(H))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.734513Z", "iopub.status.busy": "2025-05-10T09:51:43.733714Z", "iopub.status.idle": "2025-05-10T09:51:43.738938Z", "shell.execute_reply": "2025-05-10T09:51:43.738039Z" }, "id": "8fb0675ecb26" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "0.5 [p0 q0] +\n", "0.5 [q0 p0]\n" ] } ], "source": [ "H = QuadOperator('p0 q0', 1/2.)\n", "H += QuadOperator('q0 p0', 1/2.)\n", "print(is_hermitian(H))\n", "print(hermitian_conjugated(H))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.742203Z", "iopub.status.busy": "2025-05-10T09:51:43.741660Z", "iopub.status.idle": "2025-05-10T09:51:43.747346Z", "shell.execute_reply": "2025-05-10T09:51:43.746449Z" }, "id": "29e5c601ec1f" }, "outputs": [ { "data": { "text/plain": [ "0.5 [p0 q0] +\n", "0.5 [q0 p0]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hermitian_conjugated(H)" ] }, { "cell_type": "markdown", "metadata": { "id": "50a84c0b5396" }, "source": [ "## Converting between quadrature operators and bosonic operators" ] }, { "cell_type": "markdown", "metadata": { "id": "ae48bb4c0254" }, "source": [ "Converting between bosonic ladder operators and quadrature operators is simple - we just apply the definition of the $\\hat{q}$ and $\\hat{p}$ operators in terms of $\\hat{b}$ and $\\hat{b}^\\dagger$. Two functions are provided to do this automatically; `get_quad_operator` and `get_boson_operator`:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.750903Z", "iopub.status.busy": "2025-05-10T09:51:43.750305Z", "iopub.status.idle": "2025-05-10T09:51:43.754509Z", "shell.execute_reply": "2025-05-10T09:51:43.753615Z" }, "id": "ee6f8768ef75" }, "outputs": [], "source": [ "from openfermion.transforms import get_boson_operator, get_quad_operator" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.757931Z", "iopub.status.busy": "2025-05-10T09:51:43.757342Z", "iopub.status.idle": "2025-05-10T09:51:43.763208Z", "shell.execute_reply": "2025-05-10T09:51:43.762295Z" }, "id": "609b6483153d" }, "outputs": [ { "data": { "text/plain": [ "0.5 [p0 q0] +\n", "0.5 [q0 p0]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = QuadOperator('p0 q0', 1/2.)\n", "H += QuadOperator('q0 p0', 1/2.)\n", "H" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.766672Z", "iopub.status.busy": "2025-05-10T09:51:43.765915Z", "iopub.status.idle": "2025-05-10T09:51:43.772011Z", "shell.execute_reply": "2025-05-10T09:51:43.771028Z" }, "id": "30899a713341" }, "outputs": [ { "data": { "text/plain": [ "-0.5000000000000001j [0 0] +\n", "0.5000000000000001j [0^ 0^]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_boson_operator(H)" ] }, { "cell_type": "markdown", "metadata": { "id": "95ed065cfb41" }, "source": [ "Note that, since these conversions are dependent on the value of $\\hbar$ chosen, both accept a `hbar` keyword argument. As before, if not specified, the default value of $\\hbar$ is `hbar=1`." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.775695Z", "iopub.status.busy": "2025-05-10T09:51:43.774956Z", "iopub.status.idle": "2025-05-10T09:51:43.781296Z", "shell.execute_reply": "2025-05-10T09:51:43.780286Z" }, "id": "786ed58cea67" }, "outputs": [ { "data": { "text/plain": [ "(0.5+0j) [] +\n", "(1+0j) [p0 p0] +\n", "1.0 [q0 q0]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = BosonOperator('0 0^')\n", "normal_ordered(get_quad_operator(H, hbar=0.5), hbar=0.5)" ] }, { "cell_type": "markdown", "metadata": { "id": "10a2e94ff205" }, "source": [ "## Weyl quantization and symmetric ordering" ] }, { "cell_type": "markdown", "metadata": { "id": "02defd458960" }, "source": [ "We also provide support for the Weyl quantization - this maps a polynomial function of the form\n", "\n", "$$f(q_0,\\dots,q_{N-1},p_0\\dots,p_{N-1})=q_0^{m_0}\\cdots q_{N-1}^{m_{N-1}} p_0^{m_0}\\cdots p_{N-1}^{m_{N-1}}$$\n", "\n", "on the phase space to the corresponding combination of quadrature operators $\\hat{q}$ and $\\hat{p}$. To do so, we make use of the McCoy formula,\n", "\n", "$$q^m p^n \\rightarrow \\frac{1}{2^n} \\sum_{r=0}^{n} \\binom{n}{r} q^r p^m q^{n-r}.$$" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.784975Z", "iopub.status.busy": "2025-05-10T09:51:43.784249Z", "iopub.status.idle": "2025-05-10T09:51:43.788468Z", "shell.execute_reply": "2025-05-10T09:51:43.787520Z" }, "id": "39041da8098f" }, "outputs": [], "source": [ "from openfermion.transforms import weyl_polynomial_quantization, symmetric_ordering" ] }, { "cell_type": "markdown", "metadata": { "id": "3c24682b7ad5" }, "source": [ "For `weyl_polynomial_quantization`, the polynomial function in the phase space is provided in the form of a string, where 'q' or 'p' is the phase space quadrature variable, the integer directly following is the mode it is with respect to, and '^2' is the polynomial power. If the power is not provided, it is assumed to be '^1'." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.791450Z", "iopub.status.busy": "2025-05-10T09:51:43.791146Z", "iopub.status.idle": "2025-05-10T09:51:43.796822Z", "shell.execute_reply": "2025-05-10T09:51:43.795844Z" }, "id": "045dc06421f4" }, "outputs": [ { "data": { "text/plain": [ "0.5 [p0 q0] +\n", "0.5 [q0 p0]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weyl_polynomial_quantization('q0 p0')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.799820Z", "iopub.status.busy": "2025-05-10T09:51:43.799531Z", "iopub.status.idle": "2025-05-10T09:51:43.805227Z", "shell.execute_reply": "2025-05-10T09:51:43.804359Z" }, "id": "066f8195101b" }, "outputs": [ { "data": { "text/plain": [ "0.125 [p0 p0 p0 q0 q0 q1 q1 q1] +\n", "0.375 [p0 p0 q0 q0 p0 q1 q1 q1] +\n", "0.375 [p0 q0 q0 p0 p0 q1 q1 q1] +\n", "0.125 [q0 q0 p0 p0 p0 q1 q1 q1]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weyl_polynomial_quantization('q0^2 p0^3 q1^3')" ] }, { "cell_type": "markdown", "metadata": { "id": "f19b276cc8d1" }, "source": [ "McCoy's formula is also used to provide a function that returns the symmetric ordering of a `BosonOperator` or `QuadOperator`, $S(\\hat{O})$. Note that $S(\\hat{O})\\neq \\hat{O}$:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.809236Z", "iopub.status.busy": "2025-05-10T09:51:43.808140Z", "iopub.status.idle": "2025-05-10T09:51:43.814471Z", "shell.execute_reply": "2025-05-10T09:51:43.813548Z" }, "id": "d7ae23880302" }, "outputs": [ { "data": { "text/plain": [ "0.5 [p0 q0] +\n", "0.5 [q0 p0]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "symmetric_ordering(QuadOperator('q0 p0'))" ] }, { "cell_type": "markdown", "metadata": { "id": "ea7958e22c94" }, "source": [ "Consider the symmetric ordering of the square of the bosonic number operator, $\\hat{n} = \\hat{b}^\\dagger \\hat{b}$:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.817495Z", "iopub.status.busy": "2025-05-10T09:51:43.817211Z", "iopub.status.idle": "2025-05-10T09:51:43.821477Z", "shell.execute_reply": "2025-05-10T09:51:43.820483Z" }, "id": "36130eb01dad" }, "outputs": [], "source": [ "from openfermion.hamiltonians import number_operator\n", "n2 = number_operator(1, parity=1) * number_operator(1, parity=1)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.824455Z", "iopub.status.busy": "2025-05-10T09:51:43.824070Z", "iopub.status.idle": "2025-05-10T09:51:43.829508Z", "shell.execute_reply": "2025-05-10T09:51:43.828635Z" }, "id": "d7c86afc29c7" }, "outputs": [ { "data": { "text/plain": [ "1.0 [0^ 0 0^ 0]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n2" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.832734Z", "iopub.status.busy": "2025-05-10T09:51:43.832392Z", "iopub.status.idle": "2025-05-10T09:51:43.838001Z", "shell.execute_reply": "2025-05-10T09:51:43.837130Z" }, "id": "d3a2db524be9" }, "outputs": [ { "data": { "text/plain": [ "0.25 [0 0 0^ 0^] +\n", "0.5 [0 0^ 0^ 0] +\n", "0.25 [0^ 0^ 0 0]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sn2 = symmetric_ordering(n2)\n", "Sn2" ] }, { "cell_type": "markdown", "metadata": { "id": "2bbd7cdf62a3" }, "source": [ "We can use `normal_ordered_boson` to simplify this result:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.841095Z", "iopub.status.busy": "2025-05-10T09:51:43.840790Z", "iopub.status.idle": "2025-05-10T09:51:43.846597Z", "shell.execute_reply": "2025-05-10T09:51:43.845665Z" }, "id": "3858191c9ea9" }, "outputs": [ { "data": { "text/plain": [ "0.5 [] +\n", "2.0 [0^ 0] +\n", "1.0 [0^ 0^ 0 0]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sn2 = normal_ordered(Sn2)\n", "Sn2" ] }, { "cell_type": "markdown", "metadata": { "id": "50fb5cacb861" }, "source": [ "Therefore $S(\\hat{n}) = \\hat{b}^\\dagger \\hat{b}^\\dagger \\hat{b}\\hat{b} + 2\\hat{b}^\\dagger \\hat{b} + 0.5$. This is equivalent to $\\hat{n}^2+\\hat{n}+0.5$:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.849850Z", "iopub.status.busy": "2025-05-10T09:51:43.849538Z", "iopub.status.idle": "2025-05-10T09:51:43.855683Z", "shell.execute_reply": "2025-05-10T09:51:43.854791Z" }, "id": "7879325f20a2" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sn2 == normal_ordered(n2 + number_operator(1, parity=1) + 0.5*BosonOperator.identity())" ] }, { "cell_type": "markdown", "metadata": { "id": "172bf67ca8f6" }, "source": [ "## Bose-Hubbard Hamiltonian\n", "\n", "In addition to the bosonic operators discussed above, we also provide Bosonic Hamiltonians that describe specific models. The Bose-Hubbard Hamiltonian over a discrete lattice or grid described by nodes $V=\\{0,1,\\dots,N-1\\}$ is described by:\n", "\n", "$$H = - t \\sum_{\\langle i, j \\rangle} b_i^\\dagger b_{j + 1}\n", " + \\frac{U}{2} \\sum_{k=1}^{N-1} b_k^\\dagger b_k (b_k^\\dagger b_k - 1)\n", " - \\mu \\sum_{k=1}^N b_k^\\dagger b_k\n", " + V \\sum_{\\langle i, j \\rangle} b_i^\\dagger b_i b_j^\\dagger b_j.$$\n", " \n", "where\n", "\n", "- The indices $\\langle i, j \\rangle$ run over pairs\n", " $i$ and $j$ of adjacenct nodes (nodes that are connected) in the grid\n", "- $t$ is the tunneling amplitude\n", "- $U$ is the on-site interaction potential\n", "- $\\mu$ is the chemical potential\n", "- $V$ is the dipole or nearest-neighbour interaction potential\n", "\n", "The Bose-Hubbard Hamiltonian function provided in OpenFermion models a Bose-Hubbard model on a two-dimensional grid, with dimensions given by `[x_dimension, y_dimension]`. It has the form\n", "\n", "```python\n", "bose_hubbard(x_dimension, y_dimension, tunneling, interaction,\n", " chemical_potential=0., dipole=0., periodic=True)\n", "```\n", "\n", "where\n", "\n", "- `x_dimension` (int): The width of the grid.\n", "- `y_dimension` (int): The height of the grid.\n", "- `tunneling` (float): The tunneling amplitude $t$.\n", "- `interaction` (float): The attractive local interaction $U$.\n", "- `chemical_potential` (float, optional): The chemical potential $\\mu$ at each site. Default value is 0.\n", "- `periodic` (bool, optional): If True, add periodic boundary conditions. Default is True.\n", "- `dipole` (float): The attractive dipole interaction strength $V$.\n", "\n", "Below is an example of a Bose-Hubbard Hamiltonian constructed in OpenFermion." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.858714Z", "iopub.status.busy": "2025-05-10T09:51:43.858426Z", "iopub.status.idle": "2025-05-10T09:51:43.864976Z", "shell.execute_reply": "2025-05-10T09:51:43.864044Z" }, "id": "2699f571f408" }, "outputs": [ { "data": { "text/plain": [ "-1.0 [0 1^] +\n", "-1.0 [0 2^] +\n", "-0.5 [0^ 0] +\n", "0.5 [0^ 0 0^ 0] +\n", "-1.0 [0^ 1] +\n", "-1.0 [0^ 2] +\n", "-1.0 [1 3^] +\n", "-0.5 [1^ 1] +\n", "0.5 [1^ 1 1^ 1] +\n", "-1.0 [1^ 3] +\n", "-1.0 [2 3^] +\n", "-0.5 [2^ 2] +\n", "0.5 [2^ 2 2^ 2] +\n", "-1.0 [2^ 3] +\n", "-0.5 [3^ 3] +\n", "0.5 [3^ 3 3^ 3]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from openfermion.hamiltonians import bose_hubbard, fermi_hubbard\n", "bose_hubbard(2, 2, 1, 1)" ] }, { "cell_type": "markdown", "metadata": { "id": "d58d4ec176e2" }, "source": [ "## Sparse bosonic operators" ] }, { "cell_type": "markdown", "metadata": { "id": "04673b1d4a9c" }, "source": [ "Like the fermionic operators, OpenFermion contains the capability to represent bosonic operators as a sparse matrix (`sparse.csc_matrix`). However, as the fermionic operators can be represented as finite matrices, this is not the case of bosonic systems, as they inhabit a infinite-dimensional Fock space. Instead, a integer truncation value $N$ need to be provided - the returned sparse operator will be of size $N^{M}\\times N^{M}$, where $M$ is the number of modes in the system, and acts on the truncated Fock basis $\\{\\left|{0}\\right\\rangle, \\left|{1}\\right\\rangle, \\dots, \\left|{N-1}\\right\\rangle\\}$." ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.868303Z", "iopub.status.busy": "2025-05-10T09:51:43.867964Z", "iopub.status.idle": "2025-05-10T09:51:43.872335Z", "shell.execute_reply": "2025-05-10T09:51:43.871342Z" }, "id": "c8f604fa519b" }, "outputs": [], "source": [ "from openfermion.linalg import boson_operator_sparse" ] }, { "cell_type": "markdown", "metadata": { "id": "4a4895d9de71" }, "source": [ "The function `boson_operator_sparse` acts on both `BosonOperator`s and `QuadOperator`s:" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.875641Z", "iopub.status.busy": "2025-05-10T09:51:43.875115Z", "iopub.status.idle": "2025-05-10T09:51:43.881776Z", "shell.execute_reply": "2025-05-10T09:51:43.880781Z" }, "id": "a081c187c79a" }, "outputs": [], "source": [ "H = boson_operator_sparse(BosonOperator('0^ 0'), 5)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.885222Z", "iopub.status.busy": "2025-05-10T09:51:43.884443Z", "iopub.status.idle": "2025-05-10T09:51:43.891099Z", "shell.execute_reply": "2025-05-10T09:51:43.890120Z" }, "id": "ab0fd120866b" }, "outputs": [ { "data": { "text/plain": [ "array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n", " [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n", " [0.+0.j, 0.+0.j, 2.+0.j, 0.+0.j, 0.+0.j],\n", " [0.+0.j, 0.+0.j, 0.+0.j, 3.+0.j, 0.+0.j],\n", " [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 4.+0.j]])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H.toarray()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "execution": { "iopub.execute_input": "2025-05-10T09:51:43.894759Z", "iopub.status.busy": "2025-05-10T09:51:43.894008Z", "iopub.status.idle": "2025-05-10T09:51:43.903098Z", "shell.execute_reply": "2025-05-10T09:51:43.902179Z" }, "id": "54151b8cd511" }, "outputs": [ { "data": { "text/plain": [ "array([[0. +0.j, 0.70710678+0.j, 0. +0.j, 0. +0.j,\n", " 0. +0.j],\n", " [0.70710678+0.j, 0. +0.j, 1. +0.j, 0. +0.j,\n", " 0. +0.j],\n", " [0. +0.j, 1. +0.j, 0. +0.j, 1.22474487+0.j,\n", " 0. +0.j],\n", " [0. +0.j, 0. +0.j, 1.22474487+0.j, 0. +0.j,\n", " 1.41421356+0.j],\n", " [0. +0.j, 0. +0.j, 0. +0.j, 1.41421356+0.j,\n", " 0. +0.j]])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = boson_operator_sparse(QuadOperator('q0'), 5, hbar=1)\n", "H.toarray()" ] } ], "metadata": { "colab": { "name": "bosonic_operators.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "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.17" } }, "nbformat": 4, "nbformat_minor": 1 }