Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created December 8, 2022 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ischurov/139de5c0e95929bf5fc9317d98030292 to your computer and use it in GitHub Desktop.
Save ischurov/139de5c0e95929bf5fc9317d98030292 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "43bf84e4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Symmetry group contains 20 elements\n",
"Hilbert space dimension is 13\n",
"Ground state energy is -18.0617854180\n"
]
}
],
"source": [
"import numpy as np\n",
"import lattice_symmetries as ls\n",
"\n",
"# this is taken from examples/ folder verbatim\n",
"\n",
"number_spins = 10 # System size\n",
"hamming_weight = number_spins // 2 # Hamming weight (i.e. number of spin ups)\n",
"\n",
"# Constructing symmetries\n",
"symmetries = []\n",
"sites = np.arange(number_spins)\n",
"# Momentum in x direction with eigenvalue π\n",
"T = (sites + 1) % number_spins\n",
"symmetries.append(ls.Symmetry(T, sector=number_spins // 2))\n",
"# Parity with eigenvalue π\n",
"P = sites[::-1]\n",
"symmetries.append(ls.Symmetry(P, sector=1))\n",
"\n",
"# Constructing the group\n",
"symmetry_group = ls.Group(symmetries)\n",
"print(\"Symmetry group contains {} elements\".format(len(symmetry_group)))\n",
"\n",
"# Constructing the basis\n",
"basis = ls.SpinBasis(\n",
" symmetry_group, number_spins=number_spins, hamming_weight=hamming_weight, spin_inversion=-1\n",
")\n",
"basis.build() # Build the list of representatives, we need it since we're doing ED\n",
"print(\"Hilbert space dimension is {}\".format(basis.number_states))\n",
"\n",
"# Heisenberg Hamiltonian\n",
"# fmt: off\n",
"σ_x = np.array([ [0, 1]\n",
" , [1, 0] ])\n",
"σ_y = np.array([ [0 , -1j]\n",
" , [1j, 0] ])\n",
"σ_z = np.array([ [1, 0]\n",
" , [0, -1] ])\n",
"# fmt: on\n",
"σ_p = σ_x + 1j * σ_y\n",
"σ_m = σ_x - 1j * σ_y\n",
"\n",
"matrix = 0.5 * (np.kron(σ_p, σ_m) + np.kron(σ_m, σ_p)) + np.kron(σ_z, σ_z)\n",
"edges = [(i, (i + 1) % number_spins) for i in range(number_spins)]\n",
"hamiltonian = ls.Operator(basis, [ls.Interaction(matrix, edges)])\n",
"\n",
"# Diagonalize the Hamiltonian using ARPACK\n",
"eigenvalues, eigenstates = ls.diagonalize(hamiltonian, k=1)\n",
"print(\"Ground state energy is {:.10f}\".format(eigenvalues[0]))\n",
"assert np.isclose(eigenvalues[0], -18.06178542)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "7c052c10",
"metadata": {},
"outputs": [],
"source": [
"basis_nosym = ls.SpinBasis(\n",
" ls.Group([]), number_spins=number_spins, hamming_weight=hamming_weight, spin_inversion=-1\n",
")\n",
"basis_nosym.build() # Build the list of representatives, we need it since we're doing ED\n",
"hamiltonian_nosym = ls.Operator(basis_nosym, [ls.Interaction(matrix, edges)])"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "25f7027a",
"metadata": {},
"outputs": [],
"source": [
"# Diagonalize the Hamiltonian using ARPACK\n",
"eigenvalues_nosym, eigenstates_nosym = ls.diagonalize(hamiltonian_nosym, k=1)"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "fda09896",
"metadata": {},
"outputs": [],
"source": [
"assert np.isclose(eigenvalues_nosym, eigenvalues).all()\n",
"# eigenvalues are the same (as expected)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "3d5faae7",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.00152265],\n",
" [-0.01295336],\n",
" [ 0.04027096],\n",
" [-0.10848156],\n",
" [ 0.19194785],\n",
" [-0.05008341],\n",
" [ 0.08000569],\n",
" [-0.20277996],\n",
" [-0.15319953],\n",
" [ 0.59851874],\n",
" [-0.46279111],\n",
" [-0.28848702],\n",
" [ 0.46954424]])"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eigenstates"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "9f3ed6bb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.00068095],\n",
" [-0.00409621],\n",
" [ 0.00900486],\n",
" [-0.00900486],\n",
" [ 0.00409621],\n",
" [-0.00068095],\n",
" [ 0.00900486],\n",
" [-0.03430488],\n",
" [ 0.04292084],\n",
" [-0.02239798],\n",
" [ 0.00409621],\n",
" [ 0.02530002],\n",
" [-0.06412465],\n",
" [ 0.04292084],\n",
" [-0.00900486],\n",
" [ 0.02530002],\n",
" [-0.03430488],\n",
" [ 0.00900486],\n",
" [ 0.00900486],\n",
" [-0.00409621],\n",
" [ 0.00068095],\n",
" [-0.00900486],\n",
" [ 0.04292084],\n",
" [-0.06851291],\n",
" [ 0.04292084],\n",
" [-0.00900486],\n",
" [-0.06412465],\n",
" [ 0.18926824],\n",
" [-0.1463474 ],\n",
" [ 0.03430488],\n",
" [-0.09122761],\n",
" [ 0.1463474 ],\n",
" [-0.04292084],\n",
" [-0.04292084],\n",
" [ 0.02239798],\n",
" [-0.00409621],\n",
" [ 0.02530002],\n",
" [-0.09122761],\n",
" [ 0.09122761],\n",
" [-0.02530002],\n",
" [ 0.09122761],\n",
" [-0.18926824],\n",
" [ 0.06412465],\n",
" [ 0.06851291],\n",
" [-0.04292084],\n",
" [ 0.00900486],\n",
" [-0.02530002],\n",
" [ 0.06412465],\n",
" [-0.02530002],\n",
" [-0.04292084],\n",
" [ 0.03430488],\n",
" [-0.00900486],\n",
" [ 0.00900486],\n",
" [-0.00900486],\n",
" [ 0.00409621],\n",
" [-0.00068095],\n",
" [ 0.00409621],\n",
" [-0.02239798],\n",
" [ 0.04292084],\n",
" [-0.03430488],\n",
" [ 0.00900486],\n",
" [ 0.04292084],\n",
" [-0.1463474 ],\n",
" [ 0.1463474 ],\n",
" [-0.04292084],\n",
" [ 0.09122761],\n",
" [-0.18926824],\n",
" [ 0.06851291],\n",
" [ 0.06412465],\n",
" [-0.04292084],\n",
" [ 0.00900486],\n",
" [-0.03430488],\n",
" [ 0.1463474 ],\n",
" [-0.18926824],\n",
" [ 0.06412465],\n",
" [-0.18926824],\n",
" [ 0.46954424],\n",
" [-0.18926824],\n",
" [-0.18926824],\n",
" [ 0.1463474 ],\n",
" [-0.03430488],\n",
" [ 0.06412465],\n",
" [-0.18926824],\n",
" [ 0.09122761],\n",
" [ 0.1463474 ],\n",
" [-0.1463474 ],\n",
" [ 0.04292084],\n",
" [-0.03430488],\n",
" [ 0.04292084],\n",
" [-0.02239798],\n",
" [ 0.00409621],\n",
" [ 0.00900486],\n",
" [-0.04292084],\n",
" [ 0.06412465],\n",
" [-0.02530002],\n",
" [ 0.06851291],\n",
" [-0.18926824],\n",
" [ 0.09122761],\n",
" [ 0.09122761],\n",
" [-0.09122761],\n",
" [ 0.02530002],\n",
" [-0.04292084],\n",
" [ 0.1463474 ],\n",
" [-0.09122761],\n",
" [-0.1463474 ],\n",
" [ 0.18926824],\n",
" [-0.06412465],\n",
" [ 0.04292084],\n",
" [-0.06851291],\n",
" [ 0.04292084],\n",
" [-0.00900486],\n",
" [ 0.00900486],\n",
" [-0.03430488],\n",
" [ 0.02530002],\n",
" [ 0.04292084],\n",
" [-0.06412465],\n",
" [ 0.02530002],\n",
" [-0.02239798],\n",
" [ 0.04292084],\n",
" [-0.03430488],\n",
" [ 0.00900486],\n",
" [ 0.00409621],\n",
" [-0.00900486],\n",
" [ 0.00900486],\n",
" [-0.00409621],\n",
" [ 0.00068095]])"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eigenstates_nosym"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "8433c0be",
"metadata": {},
"outputs": [],
"source": [
"eigenstate_recovered = []\n",
"for basis_state in basis_nosym.states:\n",
" # take a basis state of the full (_nosym) basis, basis_state = σ\n",
" representative, character, norm = basis.state_info(basis_state)\n",
" # get the corresponding representative (\\tilde σ), character (χ*(g)) and norm sqrt(1/N_{\\tilde σ}) \n",
" # using the \"small\" basis\n",
" \n",
" coeff = eigenstates[basis.index(representative), 0]\n",
" # get the coefficient of the corresponding representative in eigenstate in \"small\" basis\n",
" # expected to get coefficient for |S_i>\n",
" \n",
" coeff_adj = coeff * character * norm\n",
" # adjust coeff by multiplying on character and norm\n",
" \n",
" eigenstate_recovered.append(coeff_adj)\n",
"eigenstate_recovered = np.real_if_close(np.array(eigenstate_recovered))"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "c9a37f4a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.18926824, -0.18926824, -0.18926824, -0.18926824, -0.18926824,\n",
" -0.18926824, -0.18926824, -0.18926824, -0.1463474 , -0.1463474 ,\n",
" -0.1463474 , -0.1463474 , -0.09122761, -0.09122761, -0.09122761,\n",
" -0.09122761, -0.06851291, -0.06851291, -0.06412465, -0.06412465,\n",
" -0.06412465, -0.06412465, -0.04292084, -0.04292084, -0.04292084,\n",
" -0.04292084, -0.04292084, -0.04292084, -0.04292084, -0.04292084,\n",
" -0.03430488, -0.03430488, -0.03430488, -0.03430488, -0.03430488,\n",
" -0.03430488, -0.03430488, -0.03430488, -0.02530002, -0.02530002,\n",
" -0.02530002, -0.02530002, -0.02239798, -0.02239798, -0.02239798,\n",
" -0.02239798, -0.00900486, -0.00900486, -0.00900486, -0.00900486,\n",
" -0.00900486, -0.00900486, -0.00900486, -0.00900486, -0.00409621,\n",
" -0.00409621, -0.00409621, -0.00409621, -0.00068095, -0.00068095,\n",
" 0.00068095, 0.00068095, 0.00068095, 0.00409621, 0.00409621,\n",
" 0.00409621, 0.00409621, 0.00409621, 0.00409621, 0.00900486,\n",
" 0.00900486, 0.00900486, 0.00900486, 0.00900486, 0.00900486,\n",
" 0.00900486, 0.00900486, 0.00900486, 0.00900486, 0.00900486,\n",
" 0.00900486, 0.02239798, 0.02530002, 0.02530002, 0.02530002,\n",
" 0.02530002, 0.02530002, 0.02530002, 0.03430488, 0.03430488,\n",
" 0.04292084, 0.04292084, 0.04292084, 0.04292084, 0.04292084,\n",
" 0.04292084, 0.04292084, 0.04292084, 0.04292084, 0.04292084,\n",
" 0.04292084, 0.04292084, 0.06412465, 0.06412465, 0.06412465,\n",
" 0.06412465, 0.06412465, 0.06412465, 0.06851291, 0.06851291,\n",
" 0.06851291, 0.09122761, 0.09122761, 0.09122761, 0.09122761,\n",
" 0.09122761, 0.09122761, 0.1463474 , 0.1463474 , 0.1463474 ,\n",
" 0.1463474 , 0.1463474 , 0.1463474 , 0.18926824, 0.18926824,\n",
" 0.46954424])"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(np.sort(eigenstates_nosym[:, 0]))\n",
"\n",
"# make sure that comparison is not failing due to different ordering of basis vectors\n",
"# (due to symmetries)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "c693cb51",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.13383286, -0.13383286, -0.13383286, -0.13383286, -0.13383286,\n",
" -0.13383286, -0.13383286, -0.13383286, -0.10348324, -0.10348324,\n",
" -0.10348324, -0.10348324, -0.06450766, -0.06450766, -0.06450766,\n",
" -0.06450766, -0.04844595, -0.04844595, -0.04534298, -0.04534298,\n",
" -0.04534298, -0.04534298, -0.03034962, -0.03034962, -0.03034962,\n",
" -0.03034962, -0.03034962, -0.03034962, -0.03034962, -0.03034962,\n",
" -0.02425722, -0.02425722, -0.02425722, -0.02425722, -0.02425722,\n",
" -0.02425722, -0.02425722, -0.02425722, -0.01788982, -0.01788982,\n",
" -0.01788982, -0.01788982, -0.01583777, -0.01583777, -0.01583777,\n",
" -0.01583777, -0.0063674 , -0.0063674 , -0.0063674 , -0.0063674 ,\n",
" -0.0063674 , -0.0063674 , -0.0063674 , -0.0063674 , -0.00289646,\n",
" -0.00289646, -0.00289646, -0.00289646, -0.0004815 , -0.0004815 ,\n",
" 0.0004815 , 0.0004815 , 0.0004815 , 0.00289646, 0.00289646,\n",
" 0.00289646, 0.00289646, 0.00289646, 0.00289646, 0.0063674 ,\n",
" 0.0063674 , 0.0063674 , 0.0063674 , 0.0063674 , 0.0063674 ,\n",
" 0.0063674 , 0.0063674 , 0.0063674 , 0.0063674 , 0.0063674 ,\n",
" 0.0063674 , 0.01583777, 0.01788982, 0.01788982, 0.01788982,\n",
" 0.01788982, 0.01788982, 0.01788982, 0.02425722, 0.02425722,\n",
" 0.03034962, 0.03034962, 0.03034962, 0.03034962, 0.03034962,\n",
" 0.03034962, 0.03034962, 0.03034962, 0.03034962, 0.03034962,\n",
" 0.03034962, 0.03034962, 0.04534298, 0.04534298, 0.04534298,\n",
" 0.04534298, 0.04534298, 0.04534298, 0.04844595, 0.04844595,\n",
" 0.04844595, 0.06450766, 0.06450766, 0.06450766, 0.06450766,\n",
" 0.06450766, 0.06450766, 0.10348324, 0.10348324, 0.10348324,\n",
" 0.10348324, 0.10348324, 0.10348324, 0.13383286, 0.13383286,\n",
" 0.33201791])"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sort(eigenstate_recovered)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "042ee432",
"metadata": {},
"outputs": [],
"source": [
"# nothing common!"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "97b02bc9",
"metadata": {},
"outputs": [],
"source": [
"# moreover:"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "078b439c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5000000000000001"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(eigenstate_recovered ** 2).sum()"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "0d0fd4a1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(eigenstates ** 2).sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e751a4e5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "LatSym",
"language": "python",
"name": "latsym"
},
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment