Advertisement
Guest User

Untitled

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