{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# read_jw" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 概要" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "この関数は正方行列のパウリ行列積の線型結合による表現を入力情報とし、係数部分と行列積部分に分解し出力するものである" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 引数一覧" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|argument name|type|role|\n", "|---|---|---|\n", "|jw_inf|list(elements:str)|正方行列のパウリ行列の線形結合による表現|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 戻り値" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "num_list(list):線型結合で用いる係数を格納したlist\\\n", "各要素はfloat型をとる\\\n", "ope_list(list):線型結合で用いるパウリ行列積を格納したlist\\\n", "各要素はstr型をとる" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Python code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "def read_jw(jw_inf):\n", " \"\"\"\n", " This function reads out the coefficients and Pauli matrix product information from the results of the Jordan–Wigner 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", " read_counter = 0 # A switch to initiate reading\n", " minus_counter = 0 # A switch to treat reading result of coefficient as less than 0\n", " plus_counter = 0 # A switch to read imaginary part of coefficient\n", " num_list = [] # A list to store the results of the reading process for coefficients\n", " sub_num_list = \"\" # A temporary 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_ope_list = \"\" # A temporary list to store the results of the reading process for Pauli matrix product\n", " e_sub = \"\" # A temporary list to store the part of the coefficient expressed using e (exponential notation)\n", " for i in range(len(jw_inf)):\n", " # Determine whether to interpret each item as a real part of coefficient, an imaginally part of coefficient, part of the coefficient expressed using e (exponential notation), a Pauli matrix product, or to ignore it.\n", " # 1: real part of coefficient, 2: Pauli matrix product, 3: imaginally part of coefficient, 4: ignore, 5: part of coefficient expressed using e (exponential notation)\n", " if jw_inf[i] == \"(\":\n", " read_counter = 1\n", " elif jw_inf[i] == \"[\":\n", " read_counter = 2\n", " elif jw_inf[i] == \"+\":\n", " read_counter = 3\n", " plus_counter += 1\n", " elif jw_inf[i] == \"]\":\n", " read_counter = 4\n", " elif jw_inf[i] == \"e\":\n", " read_counter = 5\n", " # Read the real part of coefficient as type str (ex: 0.2450065090650131)\n", " if read_counter == 1:\n", " one_counter = 0\n", " if jw_inf[i+1] == \"+\":\n", " one_counter += 1\n", " if jw_inf[i+1] == \"e\":\n", " one_counter += 1\n", " if jw_inf[i+1] == \".\":\n", " one_counter += 2\n", " if jw_inf[i+1] == \"-\":\n", " one_counter += 3\n", " if one_counter == 0:\n", " sub_num_list += jw_inf[i+1]\n", " if one_counter == 2:\n", " true_add_num = int(sub_num_list)\n", " sub_num_list = \"\"\n", " if one_counter == 3:\n", " minus_counter = 1\n", " # Read the Pauli matrix product as type str (ex: 'X0X1Y2Y3')\n", " elif read_counter == 2:\n", " sub_switch = 0\n", " if jw_inf[i+1] == \"]\":\n", " sub_switch = 1\n", " if jw_inf[i+1] == \" \":\n", " sub_switch = 1\n", " if sub_switch < 1:\n", " sub_ope_list += jw_inf[i+1]\n", " # Store the reading result for coefficient as type float\n", " elif read_counter == 3 and plus_counter % 2 == 1:\n", " len_snl = len(sub_num_list)\n", " add_ele = true_add_num + float(int(sub_num_list))/10**(len_snl)\n", " if e_sub != \"\":\n", " add_ele *= (10**int(e_sub))\n", " num_list.append(add_ele*(-1)**minus_counter)\n", " e_sub = \"\"\n", " sub_num_list = \"\"\n", " read_counter = 0\n", " minus_counter = 0\n", " # Store the reading result for Pauli matrix product\n", " elif read_counter == 4:\n", " ope_list.append(sub_ope_list)\n", " sub_ope_list = \"\"\n", " read_counter = 0\n", " # Read the part of the coefficient expressed using e (exponential notation) as type str (ex: -5)\n", " elif read_counter == 5:\n", " e_switch = 0\n", " if jw_inf[i+1] == \"0\":\n", " e_switch = 1\n", " if jw_inf[i+1] == \"+\":\n", " e_switch = 1\n", " if e_switch < 1:\n", " e_sub += jw_inf[i+1]\n", " return num_list, ope_list\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 実行例" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pitbe" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-0.2300259962688794, -0.04711430043380896, 0.04711430043380896, 0.04711430043380896, -0.04711430043380896, 0.153592312841566, 0.16276753098254845, 0.1135597567900135, 0.16067405722382247, 0.153592312841566, 0.16067405722382247, 0.1135597567900135, -0.17274355195698893, 0.1688588772467303, -0.17274355195698887]\n", "['', 'X0X1Y2Y3', 'X0Y1Y2X3', 'Y0X1X2Y3', 'Y0Y1X2X3', 'Z0', 'Z0Z1', 'Z0Z2', 'Z0Z3', 'Z1', 'Z1Z2', 'Z1Z3', 'Z2', 'Z2Z3', 'Z3']\n" ] } ], "source": [ "jw_hamiltonian = '(-0.2300259962688794+0j) [] + (-0.04711430043380896+0j) [X0 X1 Y2 Y3] + (0.04711430043380896+0j) [X0 Y1 Y2 X3] + (0.04711430043380896+0j) [Y0 X1 X2 Y3] + (-0.04711430043380896+0j) [Y0 Y1 X2 X3] + (0.153592312841566+0j) [Z0] + (0.16276753098254843+0j) [Z0 Z1] + (0.1135597567900135+0j) [Z0 Z2] + (0.16067405722382247+0j) [Z0 Z3] + (0.153592312841566+0j) [Z1] + (0.16067405722382247+0j) [Z1 Z2] + (0.1135597567900135+0j) [Z1 Z3] + (-0.17274355195698893+0j) [Z2] + (0.16885887724673027+0j) [Z2 Z3] + (-0.17274355195698887+0j) [Z3]'\n", "# 今回は簡単のために'openfermion'の関数'jordan_wigner'の結果を代入する形をとっている\n", "\n", "re_num, re_ope = pitbe.read_jw(str(jw_hamiltonian))\n", "print(re_num)\n", "print(re_ope)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 注意点" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "前述の通り入力するデータはライブラリ「openfermion」の関数「jordan_wigner」による出力結果を想定している\\\n", "この手法の出力結果以外を代入した場合、正しく実行されないこともあることに注意されたし" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }