{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "aVqb4qlbXjI-" }, "source": [ "##### Copyright 2020 The OpenFermion Developers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2024-06-30T09:21:57.803182Z", "iopub.status.busy": "2024-06-30T09:21:57.802880Z", "iopub.status.idle": "2024-06-30T09:21:57.807060Z", "shell.execute_reply": "2024-06-30T09:21:57.806409Z" }, "id": "Dk8ubIqnXl2B" }, "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": "kybNYuM7YeyH" }, "source": [ "# Lowering qubit requirements using binary codes" ] }, { "cell_type": "markdown", "metadata": { "id": "cuXwrTlaXtYo" }, "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": "TtLEYcV8Yp1t" }, "source": [ "## Setup\n", "\n", "Install the OpenFermion package:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-06-30T09:21:57.810629Z", "iopub.status.busy": "2024-06-30T09:21:57.810096Z", "iopub.status.idle": "2024-06-30T09:22:11.071323Z", "shell.execute_reply": "2024-06-30T09:22:11.070332Z" }, "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-pjdn9b7d/openfermion_524ffe7a91314a6a90e42f988212ccba\r\n", " Running command git clone --filter=blob:none --quiet https://github.com/quantumlib/OpenFermion.git /tmpfs/tmp/pip-install-pjdn9b7d/openfermion_524ffe7a91314a6a90e42f988212ccba\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Resolved https://github.com/quantumlib/OpenFermion.git to commit 1064a68220722727472519b233ba009d5c14a7d5\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 \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~=1.0 (from openfermion)\r\n", " Using cached cirq_core-1.4.1-py3-none-any.whl.metadata (1.8 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>=2.8 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (3.11.0)\r\n", "Requirement already satisfied: networkx in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (3.3)\r\n", "Requirement already satisfied: numpy>=1.11.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (1.26.4)\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.18 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (2.32.3)\r\n", "Requirement already satisfied: scipy>=1.1.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from openfermion) (1.14.0)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting sympy (from openfermion)\r\n", " Using cached sympy-1.12.1-py3-none-any.whl.metadata (12 kB)\r\n", "Requirement already satisfied: attrs>=21.3.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core~=1.0->openfermion) (23.2.0)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting duet>=0.2.8 (from cirq-core~=1.0->openfermion)\r\n", " Using cached duet-0.2.9-py3-none-any.whl.metadata (2.3 kB)\r\n", "Requirement already satisfied: matplotlib~=3.0 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core~=1.0->openfermion) (3.9.0)\r\n", "Requirement already satisfied: pandas in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core~=1.0->openfermion) (2.2.2)\r\n", "Collecting sortedcontainers~=2.0 (from cirq-core~=1.0->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~=1.0->openfermion) (4.12.2)\r\n", "Requirement already satisfied: tqdm in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from cirq-core~=1.0->openfermion) (4.66.4)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: charset-normalizer<4,>=2 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests>=2.18->openfermion) (3.3.2)\r\n", "Requirement already satisfied: idna<4,>=2.5 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests>=2.18->openfermion) (3.7)\r\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests>=2.18->openfermion) (2.2.2)\r\n", "Requirement already satisfied: certifi>=2017.4.17 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from requests>=2.18->openfermion) (2024.6.2)\r\n", "Requirement already satisfied: packaging in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from deprecation->openfermion) (24.1)\r\n", "Collecting mpmath<1.4.0,>=1.1.0 (from sympy->openfermion)\r\n", " Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: contourpy>=1.0.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.0->cirq-core~=1.0->openfermion) (1.2.1)\r\n", "Requirement already satisfied: cycler>=0.10 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.0->cirq-core~=1.0->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.0->cirq-core~=1.0->openfermion) (4.53.0)\r\n", "Requirement already satisfied: kiwisolver>=1.3.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.0->cirq-core~=1.0->openfermion) (1.4.5)\r\n", "Requirement already satisfied: pillow>=8 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.0->cirq-core~=1.0->openfermion) (10.3.0)\r\n", "Requirement already satisfied: pyparsing>=2.3.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.0->cirq-core~=1.0->openfermion) (3.1.2)\r\n", "Requirement already satisfied: python-dateutil>=2.7 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from matplotlib~=3.0->cirq-core~=1.0->openfermion) (2.9.0.post0)\r\n", "Requirement already satisfied: pytz>=2020.1 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from pandas->cirq-core~=1.0->openfermion) (2024.1)\r\n", "Requirement already satisfied: tzdata>=2022.7 in /tmpfs/src/tf_docs_env/lib/python3.10/site-packages (from pandas->cirq-core~=1.0->openfermion) (2024.1)\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.0->cirq-core~=1.0->openfermion) (1.16.0)\r\n", "Using cached cirq_core-1.4.1-py3-none-any.whl (1.9 MB)\r\n", "Using cached deprecation-2.1.0-py2.py3-none-any.whl (11 kB)\r\n", "Using cached sympy-1.12.1-py3-none-any.whl (5.7 MB)\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Using cached duet-0.2.9-py3-none-any.whl (29 kB)\r\n", "Using cached mpmath-1.3.0-py3-none-any.whl (536 kB)\r\n", "Using cached sortedcontainers-2.4.0-py2.py3-none-any.whl (29 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 \bdone\r\n", "\u001b[?25h Created wheel for openfermion: filename=openfermion-1.7.0.dev0-py3-none-any.whl size=1209626 sha256=90a4e03d5863898dd9c32c33cc43fa1dd651691094539c409c45ec31795a14a1\r\n", " Stored in directory: /tmpfs/tmp/pip-ephem-wheel-cache-st1dx4j_/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, duet, deprecation, cirq-core, openfermion\r\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Successfully installed cirq-core-1.4.1 deprecation-2.1.0 duet-0.2.9 mpmath-1.3.0 openfermion-1.7.0.dev0 pubchempy-1.0.4 sortedcontainers-2.4.0 sympy-1.12.1\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": "4cc6d04dc630" }, "source": [ "## Introduction\n", "\n", "Molecular Hamiltonians are known to have certain symmetries that are not taken into account by mappings like the Jordan-Wigner or Bravyi-Kitaev transform. The most notable of such symmetries is the conservation of the total number of particles in the system. Since those symmetries effectively reduce the degrees of freedom of the system, one is able to reduce the number of qubits required for simulation by utilizing binary codes (arXiv:1712.07067). \n", "\n", "We can represent the symmetry-reduced Fermion basis by binary vectors of a set $\\mathcal{V} \\ni \\boldsymbol{\\nu}$, with $ \\boldsymbol{\\nu} = (\\nu_0, \\, \\nu_1, \\dots, \\, \\nu_{N-1} ) $, where every component $\\nu_i \\in \\lbrace 0, 1 \\rbrace $ and $N$ is the total number of Fermion modes. These binary vectors $ \\boldsymbol{\\nu}$ are related to the actual basis states by: $$\n", "\\left[\\prod_{i=0}^{N-1} (a_i^{\\dagger})^{\\nu_i} \\right] \\left|{\\text{vac}}\\right\\rangle \\, ,\n", "$$ where $ (a_i^\\dagger)^{0}=1$. The qubit basis, on the other hand, can be characterized by length-$n$ binary vectors $\\boldsymbol{\\omega}=(\\omega_0, \\, \\dots , \\, \\omega_{n-1})$, that represent an $n$-qubit basis state by:\n", "$$ \\left|{\\omega_0}\\right\\rangle \\otimes \\left|\\omega_1\\right\\rangle \\otimes \\dots \\otimes \\left|{\\omega_{n-1}}\\right\\rangle \\, . $$ \n", "Since $\\mathcal{V}$ is a mere subset of the $N$-fold binary space, but the set of the vectors $\\boldsymbol{\\omega}$ spans the entire $n$-fold binary space we can assign every vector $\\boldsymbol{\\nu}$ to a vector $ \\boldsymbol{\\omega}$, such that $n