-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
4,472 additions
and
2,879 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Custom Hamiltonian\n", | ||
"\n", | ||
"[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/block-hczhai/block2-preview/blob/master/docs/source/tutorial/custom-hamiltonian.ipynb)" | ||
], | ||
"metadata": { | ||
"id": "gEis3BAAaJLD" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"!pip install block2==0.5.2rc13 -qq --progress-bar off --extra-index-url=https://block-hczhai.github.io/block2-preview/pypi/\n", | ||
"!pip install pyscf==2.3.0 -qq --progress-bar off" | ||
], | ||
"metadata": { | ||
"id": "2FXmEVkEaJxX" | ||
}, | ||
"execution_count": 1, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"\n", | ||
"In this tutorial, we provide an example python script for performing DMRG using custom Hamiltonians, where the operators and states at local Hilbert space at every site can be redefined. It is also possible to use different local Hilbert space for different sites. New letters can be introduced for representing new operators.\n", | ||
"\n", | ||
"Note the following examples is only supposed to work in the ``SZ`` mode.\n", | ||
"\n", | ||
"## The Hubbard Model\n", | ||
"\n", | ||
"In the following example, we implement a custom Hamiltonian for the Hubbard model. In the standard implementation, the on-site term was represented as ``cdCD``. Here we instead introduce a single letter ``N`` for the ``cdCD`` term. For each letter in ``cdCDN`` (representing elementary operators), we define its matrix representation in the local basis in ``site_ops``. The quantum number and number of states in each quantum number at each site (which defines the local Hilbert space) is set in ``site_basis``.\n", | ||
"\n" | ||
], | ||
"metadata": { | ||
"id": "aAO2KvxWaSjo" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"from pyblock2.driver.core import DMRGDriver, SymmetryTypes, MPOAlgorithmTypes\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"L = 8\n", | ||
"U = 2\n", | ||
"N_ELEC = 8\n", | ||
"\n", | ||
"driver = DMRGDriver(scratch=\"./tmp\", symm_type=SymmetryTypes.SZ, n_threads=4)\n", | ||
"driver.initialize_system(n_sites=L, n_elec=N_ELEC, spin=0)\n", | ||
"\n", | ||
"# [Part A] Set states and matrix representation of operators in local Hilbert space\n", | ||
"site_basis, site_ops = [], []\n", | ||
"Q = driver.bw.SX # quantum number wrapper (n_elec, 2 * spin, point group irrep)\n", | ||
"\n", | ||
"for k in range(L):\n", | ||
" basis = [(Q(0, 0, 0), 1), (Q(1, 1, 0), 1), (Q(1, -1, 0), 1), (Q(2, 0, 0), 1)] # [0ab2]\n", | ||
" ops = {\n", | ||
" \"\": np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), # identity\n", | ||
" \"c\": np.array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0]]), # alpha+\n", | ||
" \"d\": np.array([[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]), # alpha\n", | ||
" \"C\": np.array([[0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, -1, 0, 0]]), # beta+\n", | ||
" \"D\": np.array([[0, 0, 1, 0], [0, 0, 0, -1], [0, 0, 0, 0], [0, 0, 0, 0]]), # beta\n", | ||
" \"N\": np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]), # cdCD\n", | ||
" }\n", | ||
" site_basis.append(basis)\n", | ||
" site_ops.append(ops)\n", | ||
"\n", | ||
"# [Part B] Set Hamiltonian terms\n", | ||
"driver.ghamil = driver.get_custom_hamiltonian(site_basis, site_ops)\n", | ||
"b = driver.expr_builder()\n", | ||
"\n", | ||
"b.add_term(\"cd\", np.array([[i, i + 1, i + 1, i] for i in range(L - 1)]).ravel(), -1)\n", | ||
"b.add_term(\"CD\", np.array([[i, i + 1, i + 1, i] for i in range(L - 1)]).ravel(), -1)\n", | ||
"b.add_term(\"N\", np.array([i for i in range(L)]), U)\n", | ||
"\n", | ||
"# [Part C] Perform DMRG\n", | ||
"mpo = driver.get_mpo(b.finalize(adjust_order=True), algo_type=MPOAlgorithmTypes.FastBipartite)\n", | ||
"mps = driver.get_random_mps(tag=\"KET\", bond_dim=250, nroots=1)\n", | ||
"energy = driver.dmrg(mpo, mps, n_sweeps=10, bond_dims=[250] * 4 + [500] * 4,\n", | ||
" noises=[1e-4] * 4 + [1e-5] * 4 + [0], thrds=[1e-10] * 8, dav_max_iter=30, iprint=1)\n", | ||
"print(\"DMRG energy = %20.15f\" % energy)" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "XuXZpeFTb5e2", | ||
"outputId": "878cdd30-3bb5-4b47-d7de-6d1ae8bfdef3" | ||
}, | ||
"execution_count": 2, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"\n", | ||
"Sweep = 0 | Direction = forward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 0.342 | E = -6.2256341447 | DW = 2.65e-16\n", | ||
"\n", | ||
"Sweep = 1 | Direction = backward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 0.456 | E = -6.2256341447 | DE = -1.07e-14 | DW = 4.93e-16\n", | ||
"\n", | ||
"Sweep = 2 | Direction = forward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 0.585 | E = -6.2256341447 | DE = -1.78e-15 | DW = 9.81e-17\n", | ||
"\n", | ||
"Sweep = 3 | Direction = backward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 0.694 | E = -6.2256341447 | DE = -3.55e-15 | DW = 1.20e-16\n", | ||
"\n", | ||
"Sweep = 4 | Direction = forward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 0.873 | E = -6.2256341447 | DE = -8.88e-16 | DW = 4.50e-20\n", | ||
"\n", | ||
"Sweep = 5 | Direction = backward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 1.028 | E = -6.2256341447 | DE = -2.66e-15 | DW = 4.87e-20\n", | ||
"\n", | ||
"Sweep = 6 | Direction = forward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 1.178 | E = -6.2256341447 | DE = 5.33e-15 | DW = 3.86e-20\n", | ||
"\n", | ||
"Sweep = 7 | Direction = backward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 1.320 | E = -6.2256341447 | DE = 1.78e-15 | DW = 4.94e-20\n", | ||
"\n", | ||
"Sweep = 8 | Direction = forward | Bond dimension = 500 | Noise = 0.00e+00 | Dav threshold = 1.00e-09\n", | ||
"Time elapsed = 1.472 | E = -6.2256341447 | DE = 0.00e+00 | DW = 2.86e-20\n", | ||
"\n", | ||
"DMRG energy = -6.225634144657919\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## The Hubbard-Holstein Model\n", | ||
"\n", | ||
"The above script can be easily extended to treat phonons." | ||
], | ||
"metadata": { | ||
"id": "AoKOfFPEbrHw" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"from pyblock2.driver.core import DMRGDriver, SymmetryTypes, MPOAlgorithmTypes\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"N_SITES_ELEC, N_SITES_PH, N_ELEC = 4, 4, 4\n", | ||
"N_PH, U, OMEGA, G = 11, 2, 0.25, 0.5\n", | ||
"L = N_SITES_ELEC + N_SITES_PH\n", | ||
"\n", | ||
"driver = DMRGDriver(scratch=\"./tmp\", symm_type=SymmetryTypes.SZ, n_threads=4)\n", | ||
"driver.initialize_system(n_sites=L, n_elec=N_ELEC, spin=0)\n", | ||
"\n", | ||
"# [Part A] Set states and matrix representation of operators in local Hilbert space\n", | ||
"site_basis, site_ops = [], []\n", | ||
"Q = driver.bw.SX # quantum number wrapper (n_elec, 2 * spin, point group irrep)\n", | ||
"\n", | ||
"for k in range(L):\n", | ||
" if k < N_SITES_ELEC:\n", | ||
" # electron part\n", | ||
" basis = [(Q(0, 0, 0), 1), (Q(1, 1, 0), 1), (Q(1, -1, 0), 1), (Q(2, 0, 0), 1)] # [0ab2]\n", | ||
" ops = {\n", | ||
" \"\": np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), # identity\n", | ||
" \"c\": np.array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0]]), # alpha+\n", | ||
" \"d\": np.array([[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]), # alpha\n", | ||
" \"C\": np.array([[0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, -1, 0, 0]]), # beta+\n", | ||
" \"D\": np.array([[0, 0, 1, 0], [0, 0, 0, -1], [0, 0, 0, 0], [0, 0, 0, 0]]), # beta\n", | ||
" }\n", | ||
" else:\n", | ||
" # phonon part\n", | ||
" basis = [(Q(0, 0, 0), N_PH)]\n", | ||
" ops = {\n", | ||
" \"\": np.identity(N_PH), # identity\n", | ||
" \"E\": np.diag(np.sqrt(np.arange(1, N_PH)), k=-1), # ph+\n", | ||
" \"F\": np.diag(np.sqrt(np.arange(1, N_PH)), k=1), # ph\n", | ||
" }\n", | ||
" site_basis.append(basis)\n", | ||
" site_ops.append(ops)\n", | ||
"\n", | ||
"# [Part B] Set Hamiltonian terms in Hubbard-Holstein model\n", | ||
"driver.ghamil = driver.get_custom_hamiltonian(site_basis, site_ops)\n", | ||
"b = driver.expr_builder()\n", | ||
"\n", | ||
"# electron part\n", | ||
"b.add_term(\"cd\", np.array([[i, i + 1, i + 1, i] for i in range(N_SITES_ELEC - 1)]).ravel(), -1)\n", | ||
"b.add_term(\"CD\", np.array([[i, i + 1, i + 1, i] for i in range(N_SITES_ELEC - 1)]).ravel(), -1)\n", | ||
"b.add_term(\"cdCD\", np.array([[i, i, i, i] for i in range(N_SITES_ELEC)]).ravel(), U)\n", | ||
"\n", | ||
"# phonon part\n", | ||
"b.add_term(\"EF\", np.array([[i + N_SITES_ELEC, ] * 2 for i in range(N_SITES_PH)]).ravel(), OMEGA)\n", | ||
"\n", | ||
"# interaction part\n", | ||
"b.add_term(\"cdE\", np.array([[i, i, i + N_SITES_ELEC] for i in range(N_SITES_ELEC)]).ravel(), G)\n", | ||
"b.add_term(\"cdF\", np.array([[i, i, i + N_SITES_ELEC] for i in range(N_SITES_ELEC)]).ravel(), G)\n", | ||
"b.add_term(\"CDE\", np.array([[i, i, i + N_SITES_ELEC] for i in range(N_SITES_ELEC)]).ravel(), G)\n", | ||
"b.add_term(\"CDF\", np.array([[i, i, i + N_SITES_ELEC] for i in range(N_SITES_ELEC)]).ravel(), G)\n", | ||
"\n", | ||
"# [Part C] Perform DMRG\n", | ||
"mpo = driver.get_mpo(b.finalize(adjust_order=True), algo_type=MPOAlgorithmTypes.FastBipartite)\n", | ||
"mps = driver.get_random_mps(tag=\"KET\", bond_dim=250, nroots=1)\n", | ||
"energy = driver.dmrg(mpo, mps, n_sweeps=10, bond_dims=[250] * 4 + [500] * 4,\n", | ||
" noises=[1e-4] * 4 + [1e-5] * 4 + [0], thrds=[1e-10] * 8, dav_max_iter=30, iprint=1)\n", | ||
"print(\"DMRG energy = %20.15f\" % energy)" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "JqphdawZboo9", | ||
"outputId": "919844ac-eed3-44f2-af2b-dd6ac5d5ee55" | ||
}, | ||
"execution_count": 3, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"\n", | ||
"Sweep = 0 | Direction = forward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 108.945 | E = -6.9568929229 | DW = 3.62e-09\n", | ||
"\n", | ||
"Sweep = 1 | Direction = backward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 142.030 | E = -6.9568932112 | DE = -2.88e-07 | DW = 3.07e-19\n", | ||
"\n", | ||
"Sweep = 2 | Direction = forward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 154.346 | E = -6.9568932112 | DE = -1.78e-15 | DW = 1.38e-19\n", | ||
"\n", | ||
"Sweep = 3 | Direction = backward | Bond dimension = 250 | Noise = 1.00e-04 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 165.954 | E = -6.9568932112 | DE = -1.78e-15 | DW = 6.71e-20\n", | ||
"\n", | ||
"Sweep = 4 | Direction = forward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 176.280 | E = -6.9568932112 | DE = -8.88e-16 | DW = 7.26e-20\n", | ||
"\n", | ||
"Sweep = 5 | Direction = backward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 186.605 | E = -6.9568932112 | DE = 2.66e-15 | DW = 6.17e-20\n", | ||
"\n", | ||
"Sweep = 6 | Direction = forward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 196.209 | E = -6.9568932112 | DE = -1.78e-15 | DW = 9.34e-20\n", | ||
"\n", | ||
"Sweep = 7 | Direction = backward | Bond dimension = 500 | Noise = 1.00e-05 | Dav threshold = 1.00e-10\n", | ||
"Time elapsed = 205.734 | E = -6.9568932112 | DE = 2.66e-15 | DW = 6.36e-20\n", | ||
"\n", | ||
"Sweep = 8 | Direction = forward | Bond dimension = 500 | Noise = 0.00e+00 | Dav threshold = 1.00e-09\n", | ||
"Time elapsed = 215.378 | E = -6.9568932112 | DE = -9.77e-15 | DW = 7.87e-20\n", | ||
"\n", | ||
"DMRG energy = -6.956893211180049\n" | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.