Advertisement
Guest User

Untitled

a guest
Sep 12th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | None | 0 0
  1. #include "itensor/all.h"
  2. #include <iostream>
  3. #include <chrono>
  4. #include <cstdlib>
  5. #include <stdio.h>
  6. #include <ctime>
  7. #include <string>
  8. #include <set>
  9. #include <vector>
  10. #include <functional>
  11. #include <algorithm>
  12.  
  13. using namespace itensor;
  14. using namespace std;
  15. using namespace std::chrono;
  16.  
  17. #define Nspins 4
  18.  
  19. auto sites = Hubbard(Nspins);
  20. MPO H; //The hubbard hamiltonian
  21. MPS State; //The state for which I want to compute the matrix elements in question
  22.  
  23.  
  24. void create_state(double *cicoeff)
  25. {
  26.  
  27.     auto psi = MPS(sites);
  28.     double normalization;
  29.  
  30.     for(int n = 1; n <= Nspins; n += 2)
  31.         {
  32.  
  33.         auto s1 = sites(n);
  34.         auto s2 = sites(n+1);
  35.  
  36.         normalization = 0;
  37.         int pos = n/8;
  38.         for (int i=pos; i<=pos+4-1; i++)
  39.           normalization += pow(cicoeff[i],2);
  40.  
  41.         auto wf = ITensor(s1,s2);
  42.         wf.set(s1(2),s2(3), cicoeff[pos]/sqrt(normalization));
  43.         wf.set(s1(3),s2(2), cicoeff[pos+1]/sqrt(normalization));
  44.         wf.set(s1(1),s2(4), cicoeff[pos+2]/sqrt(normalization));
  45.         wf.set(s1(4),s2(1), cicoeff[pos+3]/sqrt(normalization));
  46.        
  47.         psi.ref(n) = ITensor(s1);
  48.         psi.ref(n+1) = ITensor(s2);
  49.         auto [U,S,V] = svd(wf,{inds(psi.ref(n))},{"Cutoff=",1E-8});
  50.         psi.ref(n) = U;
  51.         psi.ref(n+1) = S*V;
  52.  
  53.       }
  54.    
  55.       State = psi;
  56.  
  57. }
  58.  
  59. string convertToString(char* a)
  60. {
  61.     int i, size=strlen(a);
  62.     string s = "";
  63.     for (i = 0; i < size; i++) {
  64.         s = s + a[i];
  65.     }
  66.     return s;
  67. }
  68.  
  69. void generate_H()
  70. {
  71.  
  72.     double U = 1.0;
  73.     auto ampo = AutoMPO(sites);
  74.  
  75.         for(int b = 1; b < Nspins; ++b)
  76.             {
  77.                 ampo += -1.0,"Cdagup",b,"Cup",b+1; //Hopping for spin up
  78.                 ampo += -1.0,"Cdagup",b+1,"Cup",b; // <-
  79.                 ampo += -1.0,"Cdagdn",b,"Cdn",b+1; //Hopping for spin down
  80.                 ampo += -1.0,"Cdagdn",b+1,"Cdn",b;// <-
  81.                 ampo += U, "Nup", b, "Ndn", b;
  82.             }
  83.                 ampo += U, "Nup", Nspins, "Ndn", Nspins;
  84.                 ampo += -1.0,"Cdagup",1,"Cup",Nspins; //Hopping for spin up
  85.                 ampo += -1.0,"Cdagup",Nspins,"Cup",1; // <-
  86.                 ampo += -1.0,"Cdagdn",1,"Cdn",Nspins; //Hopping for spin down
  87.                 ampo += -1.0,"Cdagdn",Nspins,"Cdn",1;// <-
  88.     H = toMPO(ampo);
  89.  
  90. }
  91.  
  92.  
  93. void computeG(double G[8][8])
  94. {
  95.     for (int i=1; i<=Nspins; i++)
  96.     {
  97.         for (int j=1; j<=Nspins; j++)
  98.         {
  99.             for (int sgi=0; sgi<=1; sgi+=1) { //Loop for up/dn
  100.                 for (int sgj=0; sgj<=1; sgj+=1) { //Loop for up/dn
  101.                      char Cd[7] = "Cdag"; char C[4] = "C";
  102.                         char Sgi[3], Sgj[3];
  103.         if (sgi == 0) {
  104.             strcpy(Sgi,"dn");
  105.         }
  106.         if (sgj == 0) {
  107.             strcpy(Sgj,"dn");
  108.         }
  109.         if (sgi == 1) {
  110.             strcpy(Sgi,"up");
  111.         }
  112.         if (sgj == 1) {
  113.             strcpy(Sgj, "up");
  114.         }
  115.                     MPS Phi_0 = State;//Storing State to Phi_0
  116.  
  117.  
  118. for (int ii=3; ii<=4; ii+=2) {
  119.     auto ind = Index(QN(),ii,"i");
  120.     Phi_0.ref(ii) *= setElt(ind(ii));
  121.     Phi_0.ref(ii-1) *= setElt(dag(ind(ii)));
  122. }
  123.  
  124.  
  125.                     MPO h = H; //Storing the Hamiltonian to h
  126.                             auto ampo = AutoMPO(sites);
  127.                     if (sgi == sgj && i != j) {
  128.                     ampo += convertToString(Cd)+convertToString(Sgi), i, convertToString(C)+convertToString(Sgj), j;//This line basically generates the operator C!_i*C_j with up/down depending on sgi and sgj
  129.                     MPO cc = toMPO(ampo);
  130.                                         MPS h_mps = applyMPO(h,Phi_0); MPS cc_mps = applyMPO(cc,Phi_0); //We apply the cc and the h
  131.                     h_mps.noPrime(); cc_mps.noPrime();
  132.                     double elem = -(inner(Phi_0,h,cc_mps) - inner(Phi_0,cc, h_mps));//Computing the elements
  133.                             G[2*(i-1)+sgi][2*(j-1)+sgj] = elem;
  134.                             }
  135.                                 }
  136.                         }
  137.         }
  138.     }
  139. }
  140.  
  141. int main()
  142. {
  143.     generate_H(); //Storing the Hubbard Hamiltonian to the public variable "H"
  144.         double cicoeffs[8]; //These are some coefficients for creating the MPS "State"
  145.     cicoeffs[0] = 0.55727;
  146.     cicoeffs[1] = -0.557423;   
  147.     cicoeffs[2] = 0.43516; 
  148.     cicoeffs[3] = 0.435161;
  149.     cicoeffs[4] = 0.557338;
  150.     cicoeffs[5] = -0.557365;   
  151.     cicoeffs[6] = 0.43516; 
  152.     cicoeffs[7] = 0.43515; 
  153.  
  154.     double cicoeff[8];
  155.     for (int i=0; i<8; i++)
  156.     cicoeff[i] = cicoeffs[i];
  157.  
  158.     create_state(cicoeff); //Creating "State" which is a product of dimers
  159.    
  160.     cout << inner(State,H,State) << endl; //Computing the energy of state
  161.  
  162.  
  163. //----------------------------------------
  164. //And here comes the "problematic code"
  165. //Initializing the matrix in which we store the matrix elements in question
  166. double G[Nspins*2][Nspins*2];
  167. for (int p=0; p<Nspins; p++){
  168.         for (int q=0; q<Nspins; q++)
  169.         {for (int pp=0; pp<=1; pp++) { for (int qq=0;qq<=1;qq++) {
  170. G[2*p+pp][2*q+qq] = 0;}}}}
  171. //Finished initializing the matrix
  172.  
  173. computeG(G);//Computing the commutator of the question
  174.  
  175. //Printing the elements (it never reaches here, except if we use removeQNs)
  176. for (int p=0; p<Nspins; p++){
  177.         for (int q=0; q<Nspins; q++)
  178.         {for (int pp=0; pp<=1; pp++)  {
  179.                 double elem = G[2*p+pp][2*q+pp];
  180.                 if (abs(elem) > 1e-6) cout << elem << " " << p+1 << " " << q+1 << " " << pp << endl;
  181.         }}}
  182.            
  183.  
  184.   return 0;
  185.  
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement