8 #include "exponentialsum.h"
9 #include "shrinkstar.h"
11 #include "measurepauli.h"
12 #include "innerproduct.h"
13 #include "randomstabilizerstate.h"
15 #define ZEROTHRESHOLD (0.00000001)
17 int readPaulicoeffs(int *omega, int *alpha, int *beta, int *gamma, int *delta, int numqubits);
19 // order of matrix elements is [row][column]!!!
21 int main(int argc, char *argv[])
25 printf("strongsim3_rellerr argument: \"number of stabilizer state samples\"\n");
29 int NUMSTABSTATESAMPLES = atoi(argv[1]); // number of stabilizer state samples
31 int N; // number of qubits
35 printf("'N' needs to be a multiple of 3 for a k=3 tensor factor decomposition!\n");
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)
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
51 srand((unsigned)time(NULL)); // seeding the random number generator for randomstabilizerstate()
53 fp = fopen("Pd.txt", "r");
55 if(fscanf(fp, "%s", buff) == EOF) {
56 printf("Error: Pd.txt should start with the number N of P(d) of values tabulated.");
64 Pd = calloc(PdN, sizeof(double*));
66 Pd[i] = calloc(PdN+1, sizeof(double));
70 for(i=1; i<PdN; i++) {
73 if(fscanf(fp, "%s", buff) == EOF) {
74 printf("Error: expected more values tabulated for P(d) for N=%d", PdN);
77 Pd[i][j] = atof(buff);
78 //printf("%e ", Pd[i][j]);
82 //printf("total=%f\n", tmp);
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);
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} };
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*));
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));
102 K = calloc(pow(3,N/3), sizeof(int));
104 int origK, origQ, *origD;
106 int **origG, **origGBar;
108 double complex origGamma;
110 int combination; // a particular combination from the linear combo of stabilizer states making up the tensor factors multiplied together
113 for(j=0; j<pow(3,N/3); j++) { // there will be 3^(N/3) combinations when using k=12 tensor factors
119 for(k=0; k<N/3; k++) {
120 K[j] += (((combination%3)==2)*k3 + ((combination%3)==1)*k2 + ((combination%3)==0)*k1);
127 G[j] = calloc(N, sizeof(int*)); GBar[j] = calloc(N, sizeof(int*));
128 h[j] = calloc(N, sizeof(int));
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));
137 G[j][k] = calloc(N, sizeof(int)); GBar[j][k] = calloc(N, sizeof(int));
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++) {
144 Q[j] += (((combination%3)==2)*Q3 + ((combination%3)==1)*Q2 + ((combination%3)==0)*Q1);
147 Gamma[j] *= (((combination%3)==2)*coeffc + ((combination%3)==1)*coeffb + ((combination%3)==0)*coeffa);
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) {
154 D[j][Kcounter+l] = D1[l];
157 D[j][Kcounter+l] = D2[l];
160 D[j][Kcounter+l] = D3[l];
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) {
170 J[j][Kcounter+l][Kcounter+m] = J1[l][m];
173 J[j][Kcounter+l][Kcounter+m] = J2[l][m];
176 J[j][Kcounter+l][Kcounter+m] = J3[l][m];
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]);
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]);
195 Kcounter = Kcounter + Kcombo;
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));
203 //memcpy(origJ[j][k], J[j][k], K[j]*sizeof(int));
205 combination /= 3; // shift to the right by one (in base-7 arithmetic)
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
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]);
225 Kcounter = Kcounter + (n1-Kcombo);
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); */
235 /*printf("G[%d]:\n", j);
236 printMatrix(G[j], N, N);
237 printf("GBar[%d]:\n", j);
238 printMatrix(GBar[j], N, N);
240 printf("h[%d]:\n", j);
241 printVector(h[j], N);
243 printf("J[%d]:\n", j);
244 printMatrix(J[j], K[j], K[j]);
246 printf("D[%d]:\n", j);
247 printVector(D[j], K[j]);
249 printf("Q[%d]=%d\n", j, Q[j]);*/
254 while(readPaulicoeffs(&omega[Paulicounter], alpha[Paulicounter], beta[Paulicounter], gamma[Paulicounter], delta[Paulicounter], N)) {
256 if((Paulicounter+1) > N) {
257 printf("Error: Number of Paulis is greater than N!\n");
261 // Let's break up the Ys into Xs and Zs in the order Z X, as required to pass to measurepauli()
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];
271 /*printf("*******\n");
273 printf("omega=%d\n", omega);
275 printVector(gamma, N);
277 printVector(beta, N);
279 printf("*******\n");*/
281 //for(j=0; j<pow(3,N/3); j++) { // the kets
283 /*printf("========\n");
285 printf("K=%d\n", K[j]);
287 printVector(h[j], N);
288 printf("Gamma[%d]=%lf+%lf\n", j, creal(Gamma[j]), cimag(Gamma[j]));
290 printMatrix(G[j], N, N);
292 printMatrix(GBar[j], N, N);
293 printf("Q=%d\n", Q[j]);
295 printVector(D[j], K[j]);
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]);
302 printVector(h[j], N);
303 printf("Gamma[%d]=%lf+%lf\n", j, creal(Gamma[j]), cimag(Gamma[j]));
305 printMatrix(G[j], N, N);
307 printMatrix(GBar[j], N, N);
308 printf("Q=%d\n", Q[j]);
310 printVector(D[j], K[j]);
312 printMatrix(J[j], K[j], K[j]);*/
319 double complex amplitude = 0.0 + 0.0*I;
320 for(i=0; i<NUMSTABSTATESAMPLES; i++) { // the bras
321 //printf("i=%d\n", i);
323 randomstabilizerstate(N, &origK, &origh, &origG, &origGBar, &origQ, &origD, &origJ, Pd);
325 origGamma = 1.0 + 0.0*I;
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);
331 /*printf("origK=%d\n", origK);
333 printMatrix(origG, N, N);
334 printf("origGBar:\n");
335 printMatrix(origGBar, N, N);
337 printVector(origh, N);*/
339 double complex stabstateaverage = 0.0 + 0.0*I;
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;
346 amplitude = amplitude + conj(stabstateaverage)*stabstateaverage/((double)(NUMSTABSTATESAMPLES))*pow(2.0,T);
348 deallocate_mem(&origG, N);
349 deallocate_mem(&origGBar, N);
351 deallocate_mem(&origJ, origK);
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)));
359 printf("%.10lf %c %.10lf I\n", creal(amplitude), cimag(amplitude+ZEROTHRESHOLD)>0?'+':'-' , cabs(cimag(amplitude)));
370 int readPaulicoeffs(int *omega, int *alpha, int *beta, int *gamma, int *delta, int numqubits)
373 int newomega, newalpha, newbeta, newgamma, newdelta;
376 if(scanf("%d", &newomega) != EOF) {
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");
383 if(newalpha+newbeta+newgamma+newdelta > 1) {
384 printf("Error: Too many coefficients are non-zero at Pauli %d!\n", i);
387 alpha[i] = newalpha; beta[i] = newbeta; gamma[i] = newgamma; delta[i] = newdelta;