Guest User

Untitled

a guest
Sep 11th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 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.                     MPO h = H; //Storing the Hamiltonian to h
  117.                             auto ampo = AutoMPO(sites);
  118.                     if (sgi == sgj && i != j) {
  119.                     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
  120.                     MPO cc = toMPO(ampo);
  121.                                         MPS h_mps = applyMPO(h,Phi_0); MPS cc_mps = applyMPO(cc,Phi_0); //We apply the cc and the h
  122.  
  123.                     double elem = -(inner(Phi_0,h,cc_mps) - inner(Phi_0,cc, h_mps));//Computing the elements
  124.                     PAUSE
  125.                             G[2*(i-1)+sgi][2*(j-1)+sgj] = elem;
  126.                             }
  127.                                 }
  128.                         }
  129.         }
  130.     }
  131. }
  132.  
  133. int main()
  134. {
  135.     generate_H(); //Storing the Hubbard Hamiltonian to the public variable "H"
  136.         double cicoeffs[8]; //These are some coefficients for creating the MPS "State"
  137.     cicoeffs[0] = 0.55727;
  138.     cicoeffs[1] = -0.557423;   
  139.     cicoeffs[2] = 0.43516; 
  140.     cicoeffs[3] = 0.435161;
  141.     cicoeffs[4] = 0.557338;
  142.     cicoeffs[5] = -0.557365;   
  143.     cicoeffs[6] = 0.43516; 
  144.     cicoeffs[7] = 0.43515; 
  145.  
  146.     double cicoeff[8];
  147.     for (int i=0; i<8; i++)
  148.     cicoeff[i] = cicoeffs[i];
  149.  
  150.     create_state(cicoeff); //Creating "State" which is a product of dimers
  151.    
  152.     cout << inner(State,H,State) << endl; //Computing the energy of state
  153.  
  154.  
  155. //----------------------------------------
  156. //And here comes the "problematic code"
  157. //Initializing the matrix in which we store the matrix elements in question
  158. double G[Nspins*2][Nspins*2];
  159. for (int p=0; p<Nspins; p++){
  160.         for (int q=0; q<Nspins; q++)
  161.         {for (int pp=0; pp<=1; pp++) { for (int qq=0;qq<=1;qq++) {
  162. G[2*p+pp][2*q+qq] = 0;}}}}
  163. //Finished initializing the matrix
  164.  
  165. computeG(G);//Computing the commutator of the question
  166.  
  167. //Printing the elements (it never reaches here, except if we use removeQNs)
  168. for (int p=0; p<Nspins; p++){
  169.         for (int q=0; q<Nspins; q++)
  170.         {for (int pp=0; pp<=1; pp++)  {
  171.                 double elem = G[2*p+pp][2*q+pp];
  172.                 if (abs(elem) > 1e-6) cout << elem << " " << p << " " << q << " " << pp << endl;
  173.         }}}
  174.            
  175.  
  176.   return 0;
  177.  
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment