{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# read_general" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 概要" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "この関数は関数read_jwをベースとして改良し、OpenfermionによるJordan-Wigner変換結果のみならず、Bravyi-Kitaev変換結果に対しても係数及びPauli演算子の情報を読み取るものである\\\n", "汎用性の観点から今後はこちらの関数の使用を推奨する" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 引数一覧" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|argument name|type|role|\n", "|---|---|---|\n", "|jw_inf|list(elements:str)|OpenfermionによるJordan-Wigner変換もしくはBravyi-Kitaev変換結果|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 戻り値" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "num_list(list):引数のうち係数に関する情報を格納したlist\\\n", "各要素はcomplex型をとる\\\n", "ope_list(list):引数のうちPauli行列積に関する情報を格納したlist\\\n", "各要素はstr型をとる" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Python code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```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", "metadata": {}, "source": [ "#### 実行例" ] }, { "cell_type": "code", "execution_count": 27, "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": 28, "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": 29, "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": 30, "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": { "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.11.6" } }, "nbformat": 4, "nbformat_minor": 2 }