Commit 85d5440b authored by Valerio's avatar Valerio
Browse files

Solution ex 11 and text last exercise ;)

parent 0583f9b8
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tenpy.models.lattice import Lattice\n",
"from tenpy.models.model import CouplingMPOModel\n",
"from tenpy.networks.site import SpinSite\n",
"from tenpy.tools.params import get_parameter\n",
"from tenpy.algorithms import dmrg\n",
"from tenpy.networks.mps import MPS\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import time\n",
"import random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Model Definition"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Create a ComplingMPOModel to describe the system.\n",
"#The parameters are passed via model_params\n",
"class Heisenberg(CouplingMPOModel):\n",
"\n",
" def __init__(self,model_params):\n",
" CouplingMPOModel.__init__(self,model_params)\n",
" \n",
" def init_lattice(self, model_params):\n",
" #Here initialize the type of lattice considered\n",
" lattice = get_parameter(model_params, 'lattice', self.name, False)\n",
" return lattice\n",
" \n",
" def init_terms(self, model_params):\n",
" D= get_parameter(model_params, 'D', 0., self.name, True)\n",
" #Try to get also the J and lambda paramter in the same way\n",
" #J= ...\n",
" #lam= ...\n",
" \n",
" #Here implement the couplings of the chain\n",
" \n",
" #D term\n",
" self.add_onsite(D, 0, 'Sz Sz') \n",
" \n",
" #Heisenberg interaction\n",
" self.add_coupling(J, 0, 'Sx', 0, 'Sx', 1,) \n",
" #Implement the remaining couplings for the Heisenberg exchange\n",
" #Have a look at the documentation of the function add_coupling()\n",
" #\n",
" #\n",
" \n",
" #Quadratic Heisenberg term, the one with lambda\n",
" #Implement this term yourself\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Phase diagram for $\\lambda=0$ and varying $D$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the model "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L=2 #Chain length (2 for the iMPS)\n",
"J=1 #Anitferromagnetic interaction\n",
"lam=0 #Quadratic term\n",
"Dmin=-1\n",
"Dmax=2\n",
"points=30\n",
"d=np.linspace(Dmin,Dmax,points,endpoint=True) #Values considered for D\n",
"\n",
"#Definition of the lattice model\n",
"#Define the local Hilbert space, have a look at the documentation of SpinSite\n",
"#site=SpinSite(...)\n",
"#Define the lattice by looking at the documentation of Lattice\n",
"#Inifinite MPS requires periodic boundaries for the lattice\n",
"#lat=Lattice(...) #We choose an infinite MPS "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Perform iDMRG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"entEnt=[]\n",
"entSp=[]\n",
"stagMag=[]\n",
"for D in d:\n",
" \n",
" #Define the paramters of the model\n",
" model_params={\n",
" 'verbose':0,\n",
" 'J':J,\n",
" 'D':D,\n",
" 'lambda':lam, \n",
" 'lattice':lat \n",
" }\n",
"\n",
" #Create the model\n",
" chain=Heisenberg(model_params)\n",
"\n",
" #Define the paramters for the DMRG\n",
" dmrg_paramsGs = {\n",
" 'trunc_params': {\n",
" 'chi_max': 20 #Maximum bond dimension\n",
" },\n",
" 'verbose': 1\n",
" }\n",
" \n",
" #Initialize the MPS with an arbitrary product state\n",
" product_state=['up',0] \n",
" #Have a look at the documentation MPS.from_product_state()\n",
" #psiGs=MPS.from_product_state(...)\n",
" \n",
" #Perform iDMRG\n",
" #Check documentation of dmrg.run\n",
" #info=dmrg.run(...)\n",
" \n",
" #Check documentation for expectation_value(). Which operator do you want to measure for magnetization?\n",
" #mag=psiGs.expectation_value(...) #Measure magnetization\n",
" #Implement function to compute staggered magnetization from mag\n",
" #staggered=...\n",
" stagMag.append(staggered) #Compute staggered magnetization\n",
" \n",
" #Check documentation of entanglement_spectrum() and compute it\n",
" #spectrum=...\n",
" entSp.append(spectrum[0])\n",
" \n",
" #Check documentation of entanglement_entropy() and compute it\n",
" #entropy=...\n",
" entEnt.append(entropy[0])\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Study staggered magnetization"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(14,5))\n",
"plt.plot(d,stagMag,'o',ls='-',c='k')\n",
"plt.xlabel('D');\n",
"plt.ylabel('Staggered magnetization');\n",
"plt.axvspan(-1, -0.33, alpha=0.2, color='red')\n",
"plt.axvspan(-0.33, 1, alpha=0.2, color='b')\n",
"plt.axvspan(1, 2, alpha=0.2, color='g')\n",
"plt.xlim(-1,2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Study of the entanglement entropy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(14,5))\n",
"plt.tight_layout()\n",
"plt.plot(d,entEnt,'o',ls='-',c='k')\n",
"plt.xlabel('D');\n",
"plt.ylabel('Entanglement Entropy');\n",
"plt.axvspan(-1, -0.33, alpha=0.2, color='red')\n",
"plt.axvspan(-0.33, 1, alpha=0.2, color='b')\n",
"plt.axvspan(1, 2, alpha=0.2, color='g')\n",
"plt.xlim(-1,2);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Study of the entanglement spectrum"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(14,5))\n",
"plt.tight_layout()\n",
"\n",
"for i in range(len(d)): \n",
" for j in range(int(len(entSp[i])/2)):\n",
" if abs(entSp[i][2*j]-entSp[i][2*j+1])<1e-4:\n",
" plt.scatter(d[i]-0.01,entSp[i][2*j],c='k',s=50,marker='.')\n",
" plt.scatter(d[i]+0.01,entSp[i][2*j+1],c='k',s=50,marker='.')\n",
" plt.plot([d[i]-0.03,d[i]+0.03],[entSp[i][2*j],entSp[i][2*j]],c='k')\n",
" else:\n",
" plt.scatter(d[i],entSp[i][2*j],c='k',s=50,marker='.')\n",
" plt.scatter(d[i],entSp[i][2*j+1],c='k',s=50,marker='.')\n",
" plt.plot([d[i]-0.03,d[i]+0.03],[entSp[i][2*j],entSp[i][2*j]],c='k')\n",
" plt.plot([d[i]-0.03,d[i]+0.03],[entSp[i][2*j+1],entSp[i][2*j+1]],c='k')\n",
" \n",
"plt.xlabel('D');\n",
"plt.ylabel('Entanglement Spectrum');\n",
"plt.axvspan(-1.1, -0.33, alpha=0.2, color='red')\n",
"plt.axvspan(-0.33, 1, alpha=0.2, color='b')\n",
"plt.axvspan(1, 2.1, alpha=0.2, color='g')\n",
"plt.xlim(-1.1,2.1);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Phase diagram for $D=0$ and varying $\\lambda$ "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Do the same studies for this new situation."
]
}
],
"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.7.0"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
``` python
from tenpy.models.lattice import Lattice
from tenpy.models.model import CouplingMPOModel
from tenpy.networks.site import SpinSite
from tenpy.tools.params import get_parameter
from tenpy.algorithms import dmrg
from tenpy.networks.mps import MPS
import numpy as np
import matplotlib.pyplot as plt
import time
import random
```
%% Cell type:markdown id: tags:
# Model Definition
%% Cell type:code id: tags:
``` python
#Create a ComplingMPOModel to describe the system.
#The parameters are passed via model_params
class Heisenberg(CouplingMPOModel):
def __init__(self,model_params):
CouplingMPOModel.__init__(self,model_params)
def init_lattice(self, model_params):
#Here initialize the type of lattice considered
lattice = get_parameter(model_params, 'lattice', self.name, False)
return lattice
def init_terms(self, model_params):
D= get_parameter(model_params, 'D', 0., self.name, True)
#Try to get also the J and lambda paramter in the same way
#J= ...
#lam= ...
#Here implement the couplings of the chain
#D term
self.add_onsite(D, 0, 'Sz Sz')
#Heisenberg interaction
self.add_coupling(J, 0, 'Sx', 0, 'Sx', 1,)
#Implement the remaining couplings for the Heisenberg exchange
#Have a look at the documentation of the function add_coupling()
#
#
#Quadratic Heisenberg term, the one with lambda
#Implement this term yourself
```
%% Cell type:markdown id: tags:
# Phase diagram for $\lambda=0$ and varying $D$
%% Cell type:markdown id: tags:
## Define the model
%% Cell type:code id: tags:
``` python
L=2 #Chain length (2 for the iMPS)
J=1 #Anitferromagnetic interaction
lam=0 #Quadratic term
Dmin=-1
Dmax=2
points=30
d=np.linspace(Dmin,Dmax,points,endpoint=True) #Values considered for D
#Definition of the lattice model
#Define the local Hilbert space, have a look at the documentation of SpinSite
#site=SpinSite(...)
#Define the lattice by looking at the documentation of Lattice
#Inifinite MPS requires periodic boundaries for the lattice
#lat=Lattice(...) #We choose an infinite MPS
```
%% Cell type:markdown id: tags:
## Perform iDMRG
%% Cell type:code id: tags:
``` python
entEnt=[]
entSp=[]
stagMag=[]
for D in d:
#Define the paramters of the model
model_params={
'verbose':0,
'J':J,
'D':D,
'lambda':lam,
'lattice':lat
}
#Create the model
chain=Heisenberg(model_params)
#Define the paramters for the DMRG
dmrg_paramsGs = {
'trunc_params': {
'chi_max': 20 #Maximum bond dimension
},
'verbose': 1
}
#Initialize the MPS with an arbitrary product state
product_state=['up',0]
#Have a look at the documentation MPS.from_product_state()
#psiGs=MPS.from_product_state(...)
#Perform iDMRG
#Check documentation of dmrg.run
#info=dmrg.run(...)
#Check documentation for expectation_value(). Which operator do you want to measure for magnetization?
#mag=psiGs.expectation_value(...) #Measure magnetization
#Implement function to compute staggered magnetization from mag
#staggered=...
stagMag.append(staggered) #Compute staggered magnetization
#Check documentation of entanglement_spectrum() and compute it
#spectrum=...
entSp.append(spectrum[0])
#Check documentation of entanglement_entropy() and compute it
#entropy=...
entEnt.append(entropy[0])
```
%% Cell type:markdown id: tags:
## Study staggered magnetization
%% Cell type:code id: tags:
``` python
plt.figure(figsize=(14,5))
plt.plot(d,stagMag,'o',ls='-',c='k')
plt.xlabel('D');
plt.ylabel('Staggered magnetization');
plt.axvspan(-1, -0.33, alpha=0.2, color='red')
plt.axvspan(-0.33, 1, alpha=0.2, color='b')
plt.axvspan(1, 2, alpha=0.2, color='g')
plt.xlim(-1,2)
```
%% Cell type:markdown id: tags:
## Study of the entanglement entropy
%% Cell type:code id: tags:
``` python
plt.figure(figsize=(14,5))
plt.tight_layout()
plt.plot(d,entEnt,'o',ls='-',c='k')
plt.xlabel('D');
plt.ylabel('Entanglement Entropy');
plt.axvspan(-1, -0.33, alpha=0.2, color='red')
plt.axvspan(-0.33, 1, alpha=0.2, color='b')
plt.axvspan(1, 2, alpha=0.2, color='g')
plt.xlim(-1,2);
```
%% Cell type:markdown id: tags:
## Study of the entanglement spectrum
%% Cell type:code id: tags:
``` python
plt.figure(figsize=(14,5))
plt.tight_layout()
for i in range(len(d)):
for j in range(int(len(entSp[i])/2)):
if abs(entSp[i][2*j]-entSp[i][2*j+1])<1e-4:
plt.scatter(d[i]-0.01,entSp[i][2*j],c='k',s=50,marker='.')
plt.scatter(d[i]+0.01,entSp[i][2*j+1],c='k',s=50,marker='.')
plt.plot([d[i]-0.03,d[i]+0.03],[entSp[i][2*j],entSp[i][2*j]],c='k')
else:
plt.scatter(d[i],entSp[i][2*j],c='k',s=50,marker='.')
plt.scatter(d[i],entSp[i][2*j+1],c='k',s=50,marker='.')
plt.plot([d[i]-0.03,d[i]+0.03],[entSp[i][2*j],entSp[i][2*j]],c='k')
plt.plot([d[i]-0.03,d[i]+0.03],[entSp[i][2*j+1],entSp[i][2*j+1]],c='k')
plt.xlabel('D');
plt.ylabel('Entanglement Spectrum');
plt.axvspan(-1.1, -0.33, alpha=0.2, color='red')
plt.axvspan(-0.33, 1, alpha=0.2, color='b')
plt.axvspan(1, 2.1, alpha=0.2, color='g')
plt.xlim(-1.1,2.1);
```
%% Cell type:markdown id: tags:
# Phase diagram for $D=0$ and varying $\lambda$
%% Cell type:code id: tags:
``` python
#Do the same studies for this new situation.
```
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from scipy import integrate\n",
"from scipy.linalg import expm \n",
"from pylab import *\n",
"import numpy as np\n",
"from svd_robust import svd\n",
"from ed_ising import ising_gs\n",
"import scipy.sparse.linalg.eigen.arpack as arp"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\"\"\" Conventions:\n",
"B[i,a,b] has axes (physical, left virtual, right virtual),\n",
"W[a,b,i,j] has axes (virtual left, virtual right, physical out, physical in)\n",
"S[i] are schmidt values between sites (i, i+1),\n",
"H_bond[i] is the bond hamiltonian between (i,i+1) with (only physical)\n",
"axes (out left, out right, in left, in right)\"\"\"\n",
"\n",
"def init_fm_mps(L):\n",
" \"\"\" Return FM Ising MPS\"\"\"\n",
" d = 2\n",
" B = []\n",
" s = []\n",
" for i in range(L):\n",
" B.append(np.zeros([2,1,1])); B[-1][0,0,0]=1\n",
" s.append(np.ones([1]))\n",
" s.append(np.ones([1]))\n",
" return B,s\n",
"\n",
"def init_ising_H_mpo(g,J,L):\n",
" \"\"\" Returns hamiltonian in MPO form\"\"\"\n",
" s0 = np.eye(2)\n",
" sx = np.array([[0.,1.],[1.,0.]])\n",
" sy = np.array([[0.,-1j],[1j,0.]])\n",
" sz = np.array([[1.,0.],[0.,-1.]])\n",
" d = 2\n",
" \n",
" w_list = []\n",
" for i in range(L):\n",
" w = np.zeros((3,3,d,d),dtype=np.float)\n",
" w[0,:2] = [s0,sz]\n",
" w[0:,2] = [g*sx, -J*sz, s0]\n",
" w_list.append(np.real(w))\n",
" return w_list\n",
"\n",
"def init_ising_H_bond(g,J,L):\n",
" \"\"\" Returns bond hamiltonian \"\"\"\n",
" sx = np.array([[0.,1.],[1.,0.]])\n",
" sy = np.array([[0.,-1j],[1j,0.]])\n",
" sz = np.array([[1.,0.],[0.,-1.]])\n",
" d = 2\n",
" \n",
" H_bond = []\n",
" for i in range(L-2):\n",
" H_bond.append(np.reshape(-J*np.kron(sz,sz) + g*np.kron(sx,np.eye(2)),(d,d,d,d)))\n",
" H_bond.append(np.reshape(-J*np.kron(sz,sz)+g*np.kron(sx,np.eye(2))+g*np.kron(np.eye(2),sx),(d,d,d,d)))\n",
" \n",
" return H_bond"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def bond_expectation(B,s,H_bond):\n",
" \" Expectation value for a bond operator \"\n",
" E=[]\n",
" L = len(B)\n",
" for i_bond in range(L-1):\n",
" BB = np.tensordot(B[i_bond],B[i_bond+1],axes=(2,1))\n",
" sBB = np.tensordot(np.diag(s[i_bond]),BB,axes=(1,1))\n",
" C = np.tensordot(sBB,H_bond[i_bond],axes=([1,2],[2,3]))\n",
" sBB=np.conj(sBB)\n",
" E.append(np.squeeze(np.tensordot(sBB,C,axes=([0,3,1,2],[0,1,2,3]))).item()) \n",
" return E\n",
"\n",
"def entanglement_entropy(s):\n",
" \" Returns the half chain entanglement entropy \"\n",
" S=[]\n",
" L = len(s)\n",
" for i_bond in range(L):\n",
" x=s[i_bond][s[i_bond]>10**(-20)]**2\n",
" S.append(-np.inner(np.log(x),x))\n",
" return(S)"
]
},