Merge branch 'master' of s3miclassical.com:strong_simulation_stabilizer_rank
[strong_simulation_stabilizer_rank.git] / strongsim3_relerr.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <complex.h>
6 #include <time.h>
7 #include "matrix.h"
8 #include "exponentialsum.h"
9 #include "shrinkstar.h"
10 #include "extend.h"
11 #include "measurepauli.h"
12 #include "innerproduct.h"
13 #include "randomstabilizerstate.h"
14
15 #define ZEROTHRESHOLD (0.00000001)
16
17 int readPaulicoeffs(int *omega, int *alpha, int *beta, int *gamma, int *delta, int numqubits);
18
19 // order of matrix elements is [row][column]!!!
20
21 int main(int argc, char *argv[])
22 {
23
24   if(argc != 2) {
25     printf("strongsim3_rellerr argument: \"number of stabilizer state samples\"\n");
26     exit(0);
27   }
28
29   int NUMSTABSTATESAMPLES = atoi(argv[1]);        // number of stabilizer state samples
30
31   int N;                  // number of qubits
32   scanf("%d", &N);
33
34   if(N%3 != 0) {
35     printf("'N' needs to be a multiple of 3 for a k=3 tensor factor decomposition!\n");
36     return 1;
37   }
38
39   int T;              // number of T gate magic states (set to the first 'K' of the 'N' qubits -- the rest are set to the '0' computational basis state)
40   scanf("%d", &T);
41
42   int omega[N]; // max of N measurements
43   int alpha[N][N], beta[N][N], gamma[N][N], delta[N][N]; // max of N measurements of N Paulis
44   int Paulicounter = 0;
45
46   int i, j, k, l, m;
47
48   FILE *fp;
49   char buff[255];
50
51   srand((unsigned)time(NULL)); // seeding the random number generator for randomstabilizerstate()
52   
53   fp = fopen("Pd.txt", "r");
54
55   if(fscanf(fp, "%s", buff) == EOF) {
56     printf("Error: Pd.txt should start with the number N of P(d) of values tabulated.");
57     return 1;
58   }
59   
60   double** Pd;
61
62   int PdN = atoi(buff);
63
64   Pd = calloc(PdN, sizeof(double*));
65   for(i=0; i<PdN; i++) 
66     Pd[i] = calloc(PdN+1, sizeof(double));
67
68   double tmp;
69   
70   for(i=1; i<PdN; i++) {
71     tmp = 0.0;
72     for(j=0; j<=i; j++) {
73       if(fscanf(fp, "%s", buff) == EOF) {
74         printf("Error: expected more values tabulated for P(d) for N=%d", PdN);
75         return 1;
76       }
77       Pd[i][j] = atof(buff);
78       //printf("%e ", Pd[i][j]);
79       tmp += Pd[i][j];
80     }
81     //printf("\n");
82     //printf("total=%f\n", tmp);
83   }
84
85
86   double complex coeffa = -0.25*(1.0-I)*(-1.0-I+sqrt(2.0))*csqrt(-I);
87   double complex coeffb = 0.25*(-1.0-I)*(1.0-I+sqrt(2.0))*csqrt(I);
88   double complex coeffc = 0.25*(-1.0-I)*(-1.0+I+sqrt(2.0))*csqrt(I);
89
90   int n1 = 3; int k1 = 1; int (*(G1[])) = { (int[]) {1, 1, 1}, (int[]) {0, 1, 0}, (int[]) {0, 0, 1}}; int (*(GBar1[])) = { (int[]) {1, 0, 0}, (int[]) {1, 1, 0}, (int[]) {1, 0, 1}}; int h1[] = {1, 1, 0}; int Q1 = 0; int D1[] = {2}; int (*(J1[])) = { (int[]) {4} };
91   int n2 = 3; int k2 = 3; int (*(G2[])) = { (int[]) {1, 0, 0}, (int[]) {0, 1, 0}, (int[]) {0, 0, 1} }; int (*(GBar2[])) = { (int[]) {1, 0, 0}, (int[]) {0, 1, 0}, (int[]) {0, 0, 1} }; int h2[] = {0, 0, 0}; int Q2 = 2; int D2[] = {2, 2, 0}; int (*(J2[])) = { (int[]) {4, 0, 0}, (int[]) {0, 4, 0}, (int[]) {0, 0, 0} };
92   int n3 = 3; int k3 = 3; int (*(G3[])) = { (int[]) {1, 0, 0}, (int[]) {0, 1, 0}, (int[]) {0, 0, 1} }; int (*(GBar3[])) = { (int[]) {1, 0, 0}, (int[]) {0, 1, 0}, (int[]) {0, 0, 1} }; int h3[] = {0, 0, 0}; int Q3 = 2; int D3[] = {6, 6, 0}; int (*(J3[])) = { (int[]) {4, 4, 4}, (int[]) {4, 4, 4}, (int[]) {4, 4, 0} };
93
94
95   int *K; int ***G; int ***GBar; int **h; int *Q; int **D; int ***J;
96   double complex Gamma[(int)pow(3,N/3)]; // prefactor in front of resultant state
97   G = calloc(pow(3,N/3),sizeof(int*)); GBar = calloc(pow(3,N/3),sizeof(int*));
98   h = calloc(pow(3,N/3),sizeof(int*));
99   
100   J = calloc(pow(3,N/3),sizeof(int*)); D = calloc(pow(3,N/3),sizeof(int*)); Q = calloc(pow(3,N/3),sizeof(int));
101
102   K = calloc(pow(3,N/3), sizeof(int));
103
104   int origK, origQ, *origD;
105   int **origJ;
106   int **origG, **origGBar;
107   int *origh;
108   double complex origGamma;
109
110   int combination; // a particular combination from the linear combo of stabilizer states making up the tensor factors multiplied together
111   
112
113   for(j=0; j<pow(3,N/3); j++) { // there will be 3^(N/3) combinations when using k=12 tensor factors
114
115     combination = j;
116
117     K[j] = 0.0;
118     
119     for(k=0; k<N/3; k++) {
120       K[j] += (((combination%3)==2)*k3 + ((combination%3)==1)*k2 + ((combination%3)==0)*k1);
121       combination /= 3;
122     }
123     combination = j;
124
125     Gamma[j] = 1.0;
126
127     G[j] = calloc(N, sizeof(int*)); GBar[j] = calloc(N, sizeof(int*));
128     h[j] = calloc(N, sizeof(int));
129
130     if(K[j] > 0) {
131       J[j] = calloc(K[j], sizeof(int*)); D[j] = calloc(K[j], sizeof(int));
132       for(k=0; k<K[j]; k++)
133         J[j][k] = calloc(K[j], sizeof(int));
134     }
135
136     for(k=0; k<N; k++) {
137       G[j][k] = calloc(N, sizeof(int)); GBar[j][k] = calloc(N, sizeof(int));
138     }
139
140     int Kcounter = 0; // Kcounter keeps track of the K<=N that we have added already to the G rows etc. for each combination that is indexed by the digits (base 3) of 'j' in that we go through with 'k'
141     int Kcombo; // Kcombo stores the k<(n1=n2=n3) dimension of the member of the combination that we are currently adding
142     for(k=0; k<N/3; k++) {
143
144       Q[j] += (((combination%3)==2)*Q3 + ((combination%3)==1)*Q2 + ((combination%3)==0)*Q1);
145       
146
147       Gamma[j] *= (((combination%3)==2)*coeffc + ((combination%3)==1)*coeffb + ((combination%3)==0)*coeffa);
148
149       Kcombo = (((combination%3)==2)*k3 + ((combination%3)==1)*k2 + ((combination%3)==0)*k1);
150       for(l=0; l<Kcombo; l++) {
151         // D1 has a different number of rows 'l' than D2 and D3 so you need to use something like 'switch' to check combination%3 without going out of bound of J1
152         switch(combination%3) {
153         case 0:
154           D[j][Kcounter+l] = D1[l];
155           break;
156         case 1:
157           D[j][Kcounter+l] = D2[l];
158           break;
159         case 2:
160           D[j][Kcounter+l] = D3[l];
161           break;
162         default:
163           printf("error");
164           return 1;
165           }
166         for(m=0; m<Kcombo; m++) {
167           // J1 has a different number of rows 'l' than J2 and J3 so you need to use something like 'switch' to check combination%3 without going out of bound of J1
168           switch(combination%3) {
169           case 0:
170             J[j][Kcounter+l][Kcounter+m] = J1[l][m];
171             break;
172           case 1:
173             J[j][Kcounter+l][Kcounter+m] = J2[l][m];
174             break;
175           case 2:
176             J[j][Kcounter+l][Kcounter+m] = J3[l][m];
177             break;
178           default:
179             printf("error");
180             return 1;
181           }
182         }
183       }
184
185       for(l=0; l<n1; l++) { // assuming n1=n2=n3
186         h[j][k*n1+l] = (((combination%3)==2)*h3[l] + ((combination%3)==1)*h2[l] + ((combination%3)==0)*h1[l]);
187       }
188       // only filling the K[j] first rows of G and GBar here corresponding to the basis for D and J
189       for(l=0; l<Kcombo; l++) {
190         for(m=0; m<n1; m++) { // assuming n1=n2=n3
191           G[j][Kcounter+l][k*n1+m] = (((combination%3)==2)*G3[l][m] + ((combination%3)==1)*G2[l][m] + ((combination%3)==0)*G1[l][m]);
192           GBar[j][Kcounter+l][k*n1+m] = (((combination%3)==2)*GBar3[l][m] + ((combination%3)==1)*GBar2[l][m] + ((combination%3)==0)*GBar1[l][m]);
193         }
194       }
195       Kcounter = Kcounter + Kcombo;
196       
197       /* printf("intermediate G[%d]:\n", j); */
198       /* printMatrix(G[j], N, N); */
199       /* printf("intermediate GBar[%d]:\n", j); */
200       /* printMatrix(GBar[j], N, N); */
201       //memcpy(origG[j][k], G[j][k], N*sizeof(int)); memcpy(origGBar[j][k], GBar[j][k], N*sizeof(int));
202
203       //memcpy(origJ[j][k], J[j][k], K[j]*sizeof(int));
204       
205       combination /= 3; // shift to the right by one (in base-7 arithmetic)
206     }
207     //printf("!\n");
208
209     // now need to fill the N-Kcounter remaining rows of G and GBar that are outside the spanning basis states of D and J
210     combination = j;
211     for(k=0; k<(N/3); k++) {
212       Kcombo = (((combination%3)==2)*k3 + ((combination%3)==1)*k2 + ((combination%3)==0)*k1);
213       //printf("Kcounter=%d\n", Kcounter);
214       // G and GBar rows that are outside the first 'k' spanning basis states
215       for(l=Kcombo; l<n1; l++) { // assuming n1=n2=n3
216         //printf("l=%d\n", l);
217         for(m=0; m<n1; m++) { // assuming n1=n2=n3
218           /* printf("m=%d\n", m); */
219           /* printf("Kcounter+l=%d\n", Kcounter+l); */
220           /* printf("k*n1+m=%d\n", k*n1+m); */
221           G[j][Kcounter+l-Kcombo][k*n1+m] = (((combination%3)==2)*G3[l][m] + ((combination%3)==1)*G2[l][m] + ((combination%3)==0)*G1[l][m]);
222           GBar[j][Kcounter+l-Kcombo][k*n1+m] = (((combination%3)==2)*GBar3[l][m] + ((combination%3)==1)*GBar2[l][m] + ((combination%3)==0)*GBar1[l][m]);
223         }
224       }
225       Kcounter = Kcounter + (n1-Kcombo);
226
227       /* printf("intermediate G[%d]:\n", j); */
228       /* printMatrix(G[j], N, N); */
229       /* printf("intermediate GBar[%d]:\n", j); */
230       /* printMatrix(GBar[j], N, N); */
231       
232       combination /= 3;
233     }
234
235     /*printf("G[%d]:\n", j);
236     printMatrix(G[j], N, N);
237     printf("GBar[%d]:\n", j);
238     printMatrix(GBar[j], N, N);
239
240     printf("h[%d]:\n", j);
241     printVector(h[j], N);
242
243     printf("J[%d]:\n", j);
244     printMatrix(J[j], K[j], K[j]);
245     
246     printf("D[%d]:\n", j);
247     printVector(D[j], K[j]);
248
249     printf("Q[%d]=%d\n", j, Q[j]);*/
250
251   }
252   //exit(0);
253
254   while(readPaulicoeffs(&omega[Paulicounter], alpha[Paulicounter], beta[Paulicounter], gamma[Paulicounter], delta[Paulicounter], N)) {
255
256     if((Paulicounter+1) > N) {
257       printf("Error: Number of Paulis is greater than N!\n");
258       return 1;
259     }
260     
261     // Let's break up the Ys into Xs and Zs in the order Z X, as required to pass to measurepauli()
262     // Y_i = -I*Z*X
263     for(i=0; i<N; i++) {
264       if(delta[Paulicounter][i]){
265         omega[Paulicounter] += 3; // -I = I^3
266         beta[Paulicounter][i] = delta[Paulicounter][i];
267         gamma[Paulicounter][i] = delta[Paulicounter][i];
268       }
269     }
270
271     /*printf("*******\n");
272     printf("*******\n");
273     printf("omega=%d\n", omega);
274     printf("X:\n");
275     printVector(gamma, N);
276     printf("Z:\n");
277     printVector(beta, N);
278     printf("*******\n");
279     printf("*******\n");*/
280
281     //for(j=0; j<pow(3,N/3); j++) { // the kets
282
283       /*printf("========\n");
284       printf("before:\n");
285       printf("K=%d\n", K[j]);
286       printf("h:\n");
287       printVector(h[j], N);
288       printf("Gamma[%d]=%lf+%lf\n", j, creal(Gamma[j]), cimag(Gamma[j]));
289       printf("G:\n");
290       printMatrix(G[j], N, N);
291       printf("GBar:\n");
292       printMatrix(GBar[j], N, N);
293       printf("Q=%d\n", Q[j]);
294       printf("D:\n");
295       printVector(D[j], K[j]);
296       printf("J:\n");
297       printMatrix(J[j], K[j], K[j]);*/
298       //Gamma[j] *= measurepauli(N, &K[j], h[j], G[j], GBar[j], &Q[j], &D[j], &J[j], omega, gamma, beta);
299       /*printf("\nafter:\n");
300       printf("K=%d\n", K[j]);
301       printf("h:\n");
302       printVector(h[j], N);
303       printf("Gamma[%d]=%lf+%lf\n", j, creal(Gamma[j]), cimag(Gamma[j]));
304       printf("G:\n");
305       printMatrix(G[j], N, N);
306       printf("GBar:\n");
307       printMatrix(GBar[j], N, N);
308       printf("Q=%d\n", Q[j]);
309       printf("D:\n");
310       printVector(D[j], K[j]);
311       printf("J:\n");
312       printMatrix(J[j], K[j], K[j]);*/
313
314     //}
315
316     Paulicounter++;
317   }
318
319   double complex amplitude = 0.0 + 0.0*I;
320   for(i=0; i<NUMSTABSTATESAMPLES; i++) { // the bras
321     //printf("i=%d\n", i);
322
323     randomstabilizerstate(N, &origK, &origh, &origG, &origGBar, &origQ, &origD, &origJ, Pd);
324
325     origGamma = 1.0 + 0.0*I;
326     
327     for(k=0; k<Paulicounter; k++) {
328       origGamma *= measurepauli(N, &origK, origh, origG, origGBar, &origQ, &origD, &origJ, omega[k], gamma[k], beta[k]);
329       //printf("k=%d\n", k);
330   }
331     /*printf("origK=%d\n", origK);
332     printf("origG:\n");
333     printMatrix(origG, N, N);
334     printf("origGBar:\n");
335     printMatrix(origGBar, N, N);
336     printf("origh:\n");
337     printVector(origh, N);*/
338
339     double complex stabstateaverage = 0.0 + 0.0*I;
340     
341     for(j=0; j<pow(3,N/3); j++) {
342       //printf("j=%d\n", j);
343       double complex newamplitude = innerproduct(N, K[j], h[j], G[j], GBar[j], Q[j], D[j], J[j], N, origK, origh, origG, origGBar, origQ, origD, origJ);
344       stabstateaverage = stabstateaverage + origGamma*Gamma[j]*newamplitude;
345     }
346     amplitude = amplitude + conj(stabstateaverage)*stabstateaverage/((double)(NUMSTABSTATESAMPLES))*pow(2.0,T);
347
348     deallocate_mem(&origG, N);
349     deallocate_mem(&origGBar, N);
350     free(origh);
351     deallocate_mem(&origJ, origK);
352     free(origD);
353   }
354
355   printf("amplitude:\n");
356   if(creal(amplitude+ZEROTHRESHOLD)>0)
357     printf("%.10lf %c %.10lf I\n", cabs(creal(amplitude)), cimag(amplitude+ZEROTHRESHOLD)>0?'+':'-' , cabs(cimag(amplitude)));
358   else
359     printf("%.10lf %c %.10lf I\n", creal(amplitude), cimag(amplitude+ZEROTHRESHOLD)>0?'+':'-' , cabs(cimag(amplitude)));
360   
361   
362
363   for(i=0; i<PdN; i++) 
364     free(Pd[i]);
365   free(Pd);
366
367   return 0;
368 }
369
370 int readPaulicoeffs(int *omega, int *alpha, int *beta, int *gamma, int *delta, int numqubits)
371 {
372     
373   int newomega, newalpha, newbeta, newgamma, newdelta;
374   int i;
375
376   if(scanf("%d", &newomega) != EOF) {
377     *omega = newomega;
378     for(i=0; i<numqubits; i++) {
379       if(scanf("%d %d %d %d", &newalpha, &newbeta, &newgamma, &newdelta) == EOF) {
380         printf("Error: Too few input coeffs!\n");
381         exit(0);
382       }
383       if(newalpha+newbeta+newgamma+newdelta > 1) {
384         printf("Error: Too many coefficients are non-zero at Pauli %d!\n", i);
385         exit(0);
386       }
387       alpha[i] = newalpha; beta[i] = newbeta; gamma[i] = newgamma; delta[i] = newdelta;
388     }
389     return 1;
390   } else
391     return 0;
392     
393 }