{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "aLkmvZhfKNxL" }, "source": [ "## 6-1. OpenFermionの使い方 \n", "\n", "この節では量子化学計算用のPythonライブラリである、OpenFermion [1] を用いて、相互作用する電子系のハミルトニアンを、量子コンピュータ上で扱いやすい形に変換する方法を紹介する。OpenFermion には量子化学計算のオープンソースライブラリである [Psi4](http://www.psicode.org) および [PySCF](https://github.com/pyscf/pyscf) との接続が用意されており、これらのライブラリの詳細な使い方を理解しなくても、分子の構造を入力するだけで、量子化学計算において現れる電子系のハミルトニアンを得られるようになっている。ここでは PySCF を使用する。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## 各種ライブラリがインストールされていない場合は実行してください\n", "## Google Colaboratory上で実行する場合'You must restart the runtime in order to use newly installed versions.'と出ますが無視してください。\n", "## runtimeを再開するとクラッシュします。\n", "!pip install qulacs pyscf openfermion openfermionpyscf\n", "\n", "## Google Colaboratory / (Linux or Mac)のjupyter notebook 環境の場合にのみ実行してください。\n", "## Qulacsのエラーが正常に出力されるようになります。\n", "!pip3 install wurlitzer\n", "%load_ext wurlitzer" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": {}, "colab_type": "code", "id": "SHQqZCKoqKZy" }, "outputs": [], "source": [ "#必要なライブラリのインポート\n", "#エラーが出る場合は openfermion を v.1.0.0 以上にしてみてください\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from openfermion.chem import MolecularData\n", "from openfermion.transforms import get_fermion_operator, jordan_wigner, bravyi_kitaev\n", "from openfermion.linalg import get_sparse_operator\n", "from openfermion.ops import FermionOperator\n", "from openfermionpyscf import run_pyscf\n", "from pyscf import fci" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "V2xllO3RqKaC" }, "source": [ "### 水素分子を計算してみる\n", "openfermion では、分子を記述するデータを MolecularData というクラスに入力する。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": {}, "colab_type": "code", "id": "gjv2Tta9qKaH" }, "outputs": [], "source": [ "#define constants\n", "basis = \"sto-3g\" #basis set\n", "multiplicity = 1 #spin multiplicity\n", "charge = 0 #total charge for the molecule\n", "distance = 0.65\n", "geometry = [(\"H\",(0,0,0)),(\"H\", (0,0,distance))] #xyz coordinates for atoms\n", "description = str(distance) #description for the psi4 output file\n", "\n", "molecule = MolecularData(geometry, basis, multiplicity, charge, description)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "xDmWvEOFqKaS" }, "source": [ "### 変数の説明\n", "以下で上記のコード内で現れている変数の意味を説明する。\n", "\n", "#### basis: 基底関数\n", "分子軌道を表現するための基底関数を設定する。sto-3g, 6-31G などいろいろな基底関数系がある。\n", "\n", "ここで使った sto-3g (Slater Type Orbital - 3 gaussian) は Slater type orbital を 3つのgaussianで近似した基底関数である。\n", "\n", "Slater type orbital とは、水素原子の解に似せた軌道であり、動径方向の関数として\n", "\n", "$$R_{nl}(r) = r^{n-l} \\exp \\left(-\\frac{Z-s}{na_0}r\\right),$$\n", "\n", "を使用し、角度方向は球面調和関数$Y_{lm}(\\theta,\\phi)$を使用するものである。sto-3g では、この動径方向の波動関数$R_{nl}(r)$を、3つのgaussianで近似した関数を用いる。\n", "\n", "#### multiplicity: スピン多重度\n", "電子はスピン1/2を持っているので、1つの電子が孤立して存在しているときスピン多重度は2である。しかし水素分子の場合、基底状態では電子はsingletを組み、全体ではスピン0になっていると考えられる。スピン0は1状態のみなので、この場合ではスピン多重度は1とする。\n", "\n", "#### charge: 全電荷\n", "全体の電荷を入力する。イオンを考える場合は + になったり − になったりする。\n", "\n", "#### geometry: 原子核配置\n", "原子種とその座標を x,y,z で指定する。\n", "\n", "#### description\n", "pyscf が計算した出力結果は openfermion のライブラリが保存されているディレクトリ内に保存される。そのファイルの名前を決めるための変数である。" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "snrWX5QLqKaW" }, "source": [ "### PySCF による計算\n", "上記で設定した MolecularData を関数 `run_pyscf` に投げて PySCFによる量子化学計算を行ってみよう。数秒で終わるはずである。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": {}, "colab_type": "code", "id": "rw8wmzJNqKaZ" }, "outputs": [], "source": [ "molecule = run_pyscf(molecule,run_scf=1,run_fci=1) " ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "vAN3PVSSqKah" }, "source": [ "### HF & Full-CI energy\n", "PySCF の計算によって求まった Hartree-Fock エネルギーと Full-CI エネルギー (=厳密な基底エネルギー) を見てみよう。(1 Hartree = 27.2116 eV)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "id": "W2IzSYSnqKal", "outputId": "568d4e86-f002-453a-c775-4ce7ea9dd241" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HF energy: -1.1129965456691684 (Hartree)\n", "FCI energy: -1.1299047843229135 (Hartree)\n" ] } ], "source": [ "print(\"HF energy: {} (Hartree)\".format(molecule.hf_energy))\n", "print(\"FCI energy: {} (Hartree)\".format(molecule.fci_energy))" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "qycXJ0UdqKa1" }, "source": [ "### 1 電子積分 $h_{ij}$・2電子積分 $h_{ijkl}$\n", "1 電子積分や 2 電子積分といった量も MolecularData クラスに保存されている。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "id": "_eqIPOQQqKa5", "outputId": "71073959-0bf1-41ad-d01f-c9539b25c37b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-1.30950987e+00 1.98461056e-17]\n", " [ 8.14840166e-17 -4.10026381e-01]]\n" ] } ], "source": [ "print(molecule.one_body_integrals)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 221 }, "colab_type": "code", "id": "PBbYAhv-qKbE", "outputId": "9a005bf2-9ed7-45f3-d468-2e865783894d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[[ 6.91904405e-01 -4.16333634e-17]\n", " [-2.77555756e-17 1.76318452e-01]]\n", "\n", " [[-2.77555756e-17 1.76318452e-01]\n", " [ 6.79683914e-01 0.00000000e+00]]]\n", "\n", "\n", " [[[-4.16333634e-17 6.79683914e-01]\n", " [ 1.76318452e-01 8.32667268e-17]]\n", "\n", " [[ 1.76318452e-01 8.32667268e-17]\n", " [ 0.00000000e+00 7.14671111e-01]]]]\n" ] } ], "source": [ "print(molecule.two_body_integrals)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "plSb-CFnqKbQ" }, "source": [ "### 第二量子化形式のハミルトニアン\n", "openfermionはこれらの積分値から第二量子化形式のハミルトニアン\n", "\n", "$$H = \\sum_i h_{ij}c_i^\\dagger c_j + \\sum_{ijkl} h_{ijkl} c_i^\\dagger c_j^\\dagger c_k c_l$$\n", "\n", "を計算してくれる(第二量子化については、例えば参考文献[2]を参照)。 `get_molecular_hamiltonian` メソッドを呼ぶことでハミルトニアンが計算できる。\n", "\n", "表示は (3,1)が $c_3^\\dagger$, (1,0)が $c_1$ といった具合。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 663 }, "colab_type": "code", "id": "S6Pr10LqqKbS", "outputId": "1e644401-8218-4c71-c3e0-66f4c407bd6c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "() 0.8141187860307693\n", "((0, 1), (0, 0)) -1.309509868464871\n", "((1, 1), (1, 0)) -1.309509868464871\n", "((2, 1), (2, 0)) -0.4100263808117837\n", "((3, 1), (3, 0)) -0.4100263808117837\n", "((0, 1), (0, 1), (0, 0), (0, 0)) 0.3459522026149022\n", "((0, 1), (0, 1), (2, 0), (2, 0)) 0.0881592258051036\n", "((0, 1), (1, 1), (1, 0), (0, 0)) 0.3459522026149022\n", "((0, 1), (1, 1), (3, 0), (2, 0)) 0.0881592258051036\n", "((0, 1), (2, 1), (0, 0), (2, 0)) 0.0881592258051036\n", "((0, 1), (2, 1), (2, 0), (0, 0)) 0.3398419569652305\n", "((0, 1), (3, 1), (1, 0), (2, 0)) 0.0881592258051036\n", "((0, 1), (3, 1), (3, 0), (0, 0)) 0.3398419569652305\n", "((1, 1), (0, 1), (0, 0), (1, 0)) 0.3459522026149022\n", "((1, 1), (0, 1), (2, 0), (3, 0)) 0.0881592258051036\n", "((1, 1), (1, 1), (1, 0), (1, 0)) 0.3459522026149022\n", "((1, 1), (1, 1), (3, 0), (3, 0)) 0.0881592258051036\n", "((1, 1), (2, 1), (0, 0), (3, 0)) 0.0881592258051036\n", "((1, 1), (2, 1), (2, 0), (1, 0)) 0.3398419569652305\n", "((1, 1), (3, 1), (1, 0), (3, 0)) 0.0881592258051036\n", "((1, 1), (3, 1), (3, 0), (1, 0)) 0.3398419569652305\n", "((2, 1), (0, 1), (0, 0), (2, 0)) 0.33984195696523034\n", "((2, 1), (0, 1), (2, 0), (0, 0)) 0.0881592258051036\n", "((2, 1), (1, 1), (1, 0), (2, 0)) 0.33984195696523034\n", "((2, 1), (1, 1), (3, 0), (0, 0)) 0.0881592258051036\n", "((2, 1), (2, 1), (0, 0), (0, 0)) 0.0881592258051036\n", "((2, 1), (2, 1), (2, 0), (2, 0)) 0.35733555551906837\n", "((2, 1), (3, 1), (1, 0), (0, 0)) 0.0881592258051036\n", "((2, 1), (3, 1), (3, 0), (2, 0)) 0.35733555551906837\n", "((3, 1), (0, 1), (0, 0), (3, 0)) 0.33984195696523034\n", "((3, 1), (0, 1), (2, 0), (1, 0)) 0.0881592258051036\n", "((3, 1), (1, 1), (1, 0), (3, 0)) 0.33984195696523034\n", "((3, 1), (1, 1), (3, 0), (1, 0)) 0.0881592258051036\n", "((3, 1), (2, 1), (0, 0), (1, 0)) 0.0881592258051036\n", "((3, 1), (2, 1), (2, 0), (3, 0)) 0.35733555551906837\n", "((3, 1), (3, 1), (1, 0), (1, 0)) 0.0881592258051036\n", "((3, 1), (3, 1), (3, 0), (3, 0)) 0.35733555551906837\n", "\n" ] } ], "source": [ "print(molecule.get_molecular_hamiltonian())" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Lf7P1JckqKba" }, "source": [ "### 量子コンピュータの扱いやすい演算子に変換する\n", "量子コンピュータ上で一番扱いやすいのは、 Pauli 演算子 $I, X, Y, Z$ とそのテンソル積である。そこで、普通電子のハミルトニアンを量子コンピュータで扱うには、第二量子化形式のハミルトニアン\n", "\n", "$$H_{fermion} = \\sum_i h_{ij}c_i^\\dagger c_j + \\sum_{ijkl} h_{ijkl} c_i^\\dagger c_j^\\dagger c_k c_l$$\n", "\n", "を、\n", "\n", "$$H_{qubit} = \\sum_{P\\in \\{I,X,Y,Z\\}^{\\otimes n}} h_{P} P$$\n", "\n", "の形に変換する。様々な変換方法が提案されているが、ここでは Jordan-Wigner 変換と呼ばれている一番簡単なものを使う。Jordan-Wigner 変換では、分子軌道 $i$ を $i$ 番目の qubit に対応させ、その分子軌道を電子が占有しているという状況を $|1\\rangle$, そうでないときには $|0\\rangle$ で表すという約束をする。\n", "\n", "このような約束の下で、fermion の生成消滅演算子の反交換関係\n", "\n", "$$\n", "\\{c^\\dagger_i, c^\\dagger_j\\} = c^\\dagger_i c^\\dagger_j + c^\\dagger_j c^\\dagger_i = 0, \\:\n", "\\{c_i, c_j\\} = 0, \\:\n", "\\{c^\\dagger_i, c_j\\} = \\delta_{ij}\n", "$$\n", "\n", "を満たすようにパウリ演算子を構成すると、\n", "\n", "$$\n", "a^{\\dagger}_{j} \\leftrightarrow \\frac{X_j-iY_j}{2}\\otimes Z_{j-1}\\otimes Z_{j-2} \\cdots Z_{1}\n", "$$\n", "\n", "という対応関係を得る。\n", "\n", "Jordan-Wigner 変換以外の変換方式については、[2][3] などを参照されたい。\n", "\n", "openfermion では Jordan-Wigner 変換が実装されている。`jordan_wigner` 関数に `FermionOperator` を渡すことで、その演算子の Jordan-Wigner 変換に対応する `QubitOperator` を返してくれる。以下では、上で作り出した水素分子の `MolecularData` から `FermionOperator` を作り出し、Jordan-Wigner 変換することで水素分子のハミルトニアンを量子コンピュータの扱いやすい形に変換している。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 272 }, "colab_type": "code", "id": "ZOCCRVsLqKbd", "outputId": "0be2587e-a456-4de7-d7f4-c1fc9b1b49d0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0.0377511039464572+0j) [] +\n", "(-0.0440796129025518+0j) [X0 X1 Y2 Y3] +\n", "(0.0440796129025518+0j) [X0 Y1 Y2 X3] +\n", "(0.0440796129025518+0j) [Y0 X1 X2 Y3] +\n", "(-0.0440796129025518+0j) [Y0 Y1 X2 X3] +\n", "(0.18601648886230573+0j) [Z0] +\n", "(0.1729761013074511+0j) [Z0 Z1] +\n", "(0.12584136558006342+0j) [Z0 Z2] +\n", "(0.16992097848261523+0j) [Z0 Z3] +\n", "(0.18601648886230576+0j) [Z1] +\n", "(0.16992097848261523+0j) [Z1 Z2] +\n", "(0.12584136558006342+0j) [Z1 Z3] +\n", "(-0.26941693141632095+0j) [Z2] +\n", "(0.17866777775953419+0j) [Z2 Z3] +\n", "(-0.26941693141632095+0j) [Z3]\n" ] } ], "source": [ "jw_hamiltonian = jordan_wigner(get_fermion_operator(molecule.get_molecular_hamiltonian()))\n", "print(jw_hamiltonian)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "QsEdGQAsqKbq" }, "source": [ "このハミルトニアンから、Hartree-Fock (HF) エネルギーを計算してみよう。Jordan-Wigner 変換では、qubitの$\\left|0\\right\\rangle, \\left|1\\right\\rangle$と軌道の占有数が 1対1 対応していることから、HFエネルギーを計算するには、下から電子数分だけを詰めていった $\\left|1100\\right\\rangle$ に対する期待値をとれば良い。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "id": "EDZ_-903qKbr", "outputId": "25e7e773-4ba7-4692-cf68-dd1eb30219c5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0]]\n", "[[-1.11299655]] -1.1129965456691684\n" ] } ], "source": [ "#テンソル積を計算するための関数\n", "def kron_N(*ops):\n", " tmp = ops[0]\n", " for op in ops[1:]:\n", " tmp = np.kron(tmp,op)\n", " return tmp\n", "\n", "bra0 = np.array([[1,0]])\n", "bra1 = np.array([[0,1]])\n", "HFbra = kron_N(bra1, bra1, bra0, bra0)\n", "HFket = HFbra.T\n", "print(HFbra)\n", "jw_matrix = get_sparse_operator(jw_hamiltonian)\n", "print(np.real(HFbra.dot(jw_matrix.dot(HFket))), molecule.hf_energy)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "u8SoJJhpqKbw" }, "source": [ "pyscf の計算と殆ど一致していることが確認できる。" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "yz1epiKwqKby" }, "source": [ "次にハミルトニアンを対角化して、その結果がFull-CI (厳密解) エネルギーと一致することを確かめてみよう。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "S9jE0zgwqKb0", "outputId": "8d6302f7-6e33-413a-b4c5-cb20b8faca63" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1.129904784322913 -1.1299047843229135\n" ] } ], "source": [ "eigenenergies, eigenvecs = np.linalg.eigh(jw_matrix.toarray())\n", "print(eigenenergies[0], molecule.fci_energy)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "SnJXHStwqKb6" }, "source": [ "これも殆ど一致していることが確認できる。基底状態の波動関数 $\\left|\\psi_g\\right\\rangle$ は" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 153 }, "colab_type": "code", "id": "m2C66KS9qKb7", "outputId": "ee44b9d0-031d-4e6d-c65c-2b944bf701a0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. +0.j 0. +0.j 0. +0.j 0.09545811+0.j\n", " 0. +0.j 0. +0.j 0. +0.j 0. +0.j\n", " 0. +0.j 0. +0.j 0. +0.j 0. +0.j\n", " -0.99543345+0.j 0. +0.j 0. +0.j 0. +0.j]\n" ] } ], "source": [ "print(eigenvecs[:,0])" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "0wOL81vSqKcA" }, "source": [ "すなわち $\\left|\\psi_g \\right\\rangle \\approx - 0.995\\left|1100\\right\\rangle + 0.0955 \\left|0011\\right\\rangle$。Hartree-Fock解 $\\left|1100\\right\\rangle$ に対して少し二電子励起状態 $\\left|0011\\right\\rangle$ が混ざっていることがわかる。" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "51kE-kf9FKWs" }, "source": [ "### 参考文献\n", "\n", "[1] R. Babbush and J. McClean, “Announcing OpenFermion: The Open Source Package for Quantum Computers”, Google AI Blog, https://ai.googleblog.com/2017/10/announcing-openfermion-open-source.html \n", "[2] A. Tranter, P. J. Love, F. Mintert, and P. V. Convey, “A Comparison of the Bravyi and Jordan-Wigner Transformations for the Quantum Simulation of Quantum Chemistry“, [J. Chem. Theory Comput. 2018, 14, 11, 5617-5630](https://pubs.acs.org/doi/10.1021/acs.jctc.8b00450) \n", "[3] S. McArdle, S. Endo, A. Aspuru-Guzik, S. Benjamin, X. Yuan, “Quantum computational chemistry”, [arXiv:1808.10402](https://arxiv.org/abs/1808.10402)" ] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [], "name": "openfermion_basics.ipynb", "provenance": [], "version": "0.3.2" }, "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.7.5" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }