{ "cells": [ { "cell_type": "markdown", "id": "67b95d89", "metadata": {}, "source": [ "## read_general" ] }, { "cell_type": "markdown", "id": "a4a5b637", "metadata": {}, "source": [ "### **Outline**\n", "This function is an improved version of the [read_jw](../eng/read_jw_en.ipynb) function.\n", "\n", "It is capable of reading both the coefficients and Pauli operator information from transformation results obtained via OpenFermion, including not only Jordan–Wigner but also Bravyi–Kitaev transformations.\n", "\n", "For greater generality, the use of this function is recommended going forward." ] }, { "cell_type": "markdown", "id": "21378cfa", "metadata": {}, "source": [ "### **Index List**\n", "|argument name|type|role|\n", "|---|---|---|\n", "|jw_inf|list(elements:str)|Result of Jordan-Wigner or Bravyi-Kitaev Transformation by Openfermion|" ] }, { "cell_type": "markdown", "id": "1dedcef5", "metadata": {}, "source": [ "### **Return**\n", "num_list(list):\\\n", "A list containing coefficient-related information among the input arguments.\\\n", "Each element in the list is of type complex.\\\n", "ope_list(list):\\\n", "A list containing information on the production of Pauli matrices products among the input arguments.\\\n", "Each element in the list is of type str." ] }, { "cell_type": "markdown", "id": "eff99dd9", "metadata": {}, "source": [ "### **Python code**\n", "```python\n", "def read_general(jw_inf):\n", " \"\"\"\n", " This function reads out the coefficients and Pauli matrix product information from the results of the Jordan–Wigner and Bravyi–Kitaev transformations performed using OpenFermion.\n", "\n", " Parameters:\n", " jw_inf: the results of the Jordan–Wigner and Bravyi–Kitaev transformations performed using OpenFermion\n", "\n", " Returns:\n", " (list, list): The coefficients information and Pauli matrix product information.\n", " \"\"\"\n", " # Local Values\n", " num_list = [] # A list to store the results of the reading process for coefficients\n", " ope_list = [] # A list to store the results of the reading process for Pauli matrix product\n", " sub_num_list = \"\" # A temporary list to store the results of the reading process for coefficients\n", " sub_ope_list = \"\" # A temporary list to store the results of the reading process for Pauli matrix product\n", " convert_list = [] # A temporary list to store the results of the reading process for coefficients before transform the type of coeffcient information from str into complex\n", " ope_con_list = [] # A temporary list to store the results of the reading process for Pauli matrix product before deleting space between Pauli matrix product\n", " read_counter = 1 # A switch to initiate reading\n", " preserve_switch = 0 # A switch to store the contents of sum_num_list into convert_list\n", " # Check each contents (excluding the first) to determine whether to interpret it as a coefficient, as part of a Pauli matrix product, or to ignore it.\n", " for i in range(len(jw_inf)-1):\n", " # Determine whether to interpret each item as a coefficient, a Pauli matrix product, or to ignore it.\n", " # 1: coefficient, 2: Pauli matrix product, 3, 4: ignore\n", " if jw_inf[i+1] == \"[\":\n", " read_counter = 2\n", " elif jw_inf[i+1] == \"]\":\n", " read_counter = 3\n", " # Read the coefficient as type str (ex: (0.2450065090650131+0j))\n", " if read_counter == 1:\n", " sub_num_list += jw_inf[i]\n", " preserve_switch = 1\n", " # Read the Pauli matrix product as type str (ex: 'X0 X1 Y2 Y3')\n", " if read_counter == 2:\n", " # Store the result of reading for the coefficient\n", " if preserve_switch == 1:\n", " convert_list.append(sub_num_list)\n", " sub_num_list = \"\"\n", " preserve_switch = 0\n", " sub_ope_list += jw_inf[i+1]\n", " # Store the result of reading for Pauli stirng\n", " if read_counter == 3:\n", " ope_con_list.append(sub_ope_list)\n", " sub_ope_list = \"\"\n", " read_counter = 4\n", " countdown = 4\n", " # Ready for reading the coefficient\n", " if read_counter == 4:\n", " if countdown == 0:\n", " read_counter = 1\n", " else:\n", " countdown -= 1\n", " # Convert type of the coefficient reading result into complex\n", " for i in range(len(convert_list)):\n", " if convert_list[i][0] != '(':\n", " num_list.append(complex(convert_list[i]))\n", " else:\n", " convert_list[i] = convert_list[i][1:-1]\n", " num_list.append(complex(convert_list[i]))\n", " # Remove unnecessary spaces from the Pauli matrix reading result \n", " for i in range(len(ope_con_list)):\n", " ope_con_list[i] = ope_con_list[i][1:]\n", " if 0 < len(ope_con_list[i]):\n", " ope_con_list[i] = ope_con_list[i].replace(\" \", \"\")\n", " ope_list.append(ope_con_list[i])\n", " \n", " return num_list, ope_list\n", "```" ] }, { "cell_type": "markdown", "id": "45ada0a2", "metadata": {}, "source": [ "### **Sample Run**" ] }, { "cell_type": "code", "execution_count": null, "id": "c6e5a023", "metadata": {}, "outputs": [], "source": [ "from openfermion.transforms import get_fermion_operator, jordan_wigner, bravyi_kitaev\n", "from openfermion.chem import MolecularData\n", "from openfermionpyscf import run_pyscf\n", "from pyscf import fci, gto, dft, tddft, scf, tools, mcscf\n", "import pitbe" ] }, { "cell_type": "code", "execution_count": null, "id": "957b85ef", "metadata": {}, "outputs": [], "source": [ "basis = \"sto-3g\"\n", "multiplicity = 1\n", "charge = 0\n", "geometry = [[\"H\", [0,0,0]],[\"H\", [0,0,0.865]]]\n", "description = \"tmp\"\n", "molecule = MolecularData(geometry, basis, multiplicity, charge, description)\n", "molecule = run_pyscf(molecule,run_scf=1,run_cisd=1,run_ccsd=1,run_fci=1)\n", "fermionic_hamiltonian = get_fermion_operator(molecule.get_molecular_hamiltonian())\n", "jw_hamiltonian = jordan_wigner(fermionic_hamiltonian)\n", "bk_hamiltonian = bravyi_kitaev(fermionic_hamiltonian)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b317404e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "---Jordan-Wigner Transformation---\n", "(-0.2300259962688794+0j) [] +\n", "(-0.04711430043380896+0j) [X0 X1 Y2 Y3] +\n", "(0.04711430043380896+0j) [X0 Y1 Y2 X3] +\n", "(0.04711430043380896+0j) [Y0 X1 X2 Y3] +\n", "(-0.04711430043380896+0j) [Y0 Y1 X2 X3] +\n", "(0.153592312841566+0j) [Z0] +\n", "(0.16276753098254843+0j) [Z0 Z1] +\n", "(0.1135597567900135+0j) [Z0 Z2] +\n", "(0.16067405722382247+0j) [Z0 Z3] +\n", "(0.153592312841566+0j) [Z1] +\n", "(0.16067405722382247+0j) [Z1 Z2] +\n", "(0.1135597567900135+0j) [Z1 Z3] +\n", "(-0.17274355195698893+0j) [Z2] +\n", "(0.16885887724673027+0j) [Z2 Z3] +\n", "(-0.17274355195698887+0j) [Z3]\n", "---Reading Result---\n", "Coefficient\n", "[(-0.2300259962688794+0j), (-0.04711430043380896+0j), (0.04711430043380896+0j), (0.04711430043380896+0j), (-0.04711430043380896+0j), (0.153592312841566+0j), (0.16276753098254843+0j), (0.1135597567900135+0j), (0.16067405722382247+0j), (0.153592312841566+0j), (0.16067405722382247+0j), (0.1135597567900135+0j), (-0.17274355195698893+0j), (0.16885887724673027+0j), (-0.17274355195698887+0j)]\n", "Pauli matrix product\n", "['', 'X0X1Y2Y3', 'X0Y1Y2X3', 'Y0X1X2Y3', 'Y0Y1X2X3', 'Z0', 'Z0Z1', 'Z0Z2', 'Z0Z3', 'Z1', 'Z1Z2', 'Z1Z3', 'Z2', 'Z2Z3', 'Z3']\n" ] } ], "source": [ "print('---Jordan-Wigner Transformation---')\n", "print(jw_hamiltonian)\n", "print('---Reading Result---')\n", "print('Coefficient')\n", "print(pitbe.read_general(str(jw_hamiltonian))[0])\n", "print('Pauli matrix product')\n", "print(pitbe.read_general(str(jw_hamiltonian))[1])" ] }, { "cell_type": "code", "execution_count": null, "id": "a1c465a2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "---Bravyi-Kitaev Transformation---\n", "(-0.2300259962688794+0j) [] +\n", "(0.04711430043380896+0j) [X0 Z1 X2] +\n", "(0.04711430043380896+0j) [X0 Z1 X2 Z3] +\n", "(0.04711430043380896+0j) [Y0 Z1 Y2] +\n", "(0.04711430043380896+0j) [Y0 Z1 Y2 Z3] +\n", "(0.153592312841566+0j) [Z0] +\n", "(0.153592312841566+0j) [Z0 Z1] +\n", "(0.16067405722382247+0j) [Z0 Z1 Z2] +\n", "(0.16067405722382247+0j) [Z0 Z1 Z2 Z3] +\n", "(0.1135597567900135+0j) [Z0 Z2] +\n", "(0.1135597567900135+0j) [Z0 Z2 Z3] +\n", "(0.16276753098254843+0j) [Z1] +\n", "(-0.17274355195698887+0j) [Z1 Z2 Z3] +\n", "(0.16885887724673027+0j) [Z1 Z3] +\n", "(-0.17274355195698893+0j) [Z2]\n", "---Reading Result---\n", "Coefficient\n", "[(-0.2300259962688794+0j), (0.04711430043380896+0j), (0.04711430043380896+0j), (0.04711430043380896+0j), (0.04711430043380896+0j), (0.153592312841566+0j), (0.153592312841566+0j), (0.16067405722382247+0j), (0.16067405722382247+0j), (0.1135597567900135+0j), (0.1135597567900135+0j), (0.16276753098254843+0j), (-0.17274355195698887+0j), (0.16885887724673027+0j), (-0.17274355195698893+0j)]\n", "Pauli matrix product\n", "['', 'X0Z1X2', 'X0Z1X2Z3', 'Y0Z1Y2', 'Y0Z1Y2Z3', 'Z0', 'Z0Z1', 'Z0Z1Z2', 'Z0Z1Z2Z3', 'Z0Z2', 'Z0Z2Z3', 'Z1', 'Z1Z2Z3', 'Z1Z3', 'Z2']\n" ] } ], "source": [ "print('---Bravyi-Kitaev Transformation---')\n", "print(bk_hamiltonian)\n", "print('---Reading Result---')\n", "print('Coefficient')\n", "print(pitbe.read_general(str(bk_hamiltonian))[0])\n", "print('Pauli matrix product')\n", "print(pitbe.read_general(str(bk_hamiltonian))[1])" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }