# Lunatic program on Linux and Smaky ?

1. Sep 2, 2016

### jk22

I'm simulating Bell's theorem of quantum mechanics on a PC. I have guests Linux and Smaky systems and wrote codes in resp. C and Basic doing basically the same but obtain different results on both systems. On Linux the results of CHSH oscillate around 2, while on the Smaky it's always above 2 ?

Code (C):

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

#define PI 4.0*atan(1.0)

double cof=.8;
double eps=.00001;
double omega=200.0;

double MAX_INT=4294967296.0/2.0-1.0;

//local function for the result in A depending on the angle of measurement and the parameter

int A(double angle, double lambda)
{
int resa=0;
double pa=0.0, choice=0.0;

//set probabilities

if(pa<eps) pa=eps;
if(pa>1.0-eps) pa=1.0-eps;

//choose +/- for A

choice=(double)rand()/MAX_INT;
pa=.5+cos(angle-lambda)/cof;
if(choice<pa)
resa=1;
else
resa=-1;

return(resa);
}

//local deterministic result in A

/*
int A(double angle, double lambda)
{
int resa=0;
double v=cos(angle-lambda);

if(v>0.0)
resa=1;
else
resa=-1;
return(resa);
}
*/

int main(void)
{
int a,b,ap,bp;

int i=0;
int na=0,nb=0;
double choice;
double anga,angb,angap,angbp,maxangb;
double phi;
double cov1=0.0,cov2=0.0,cov3=0.0,cov4=0.0;
int vrmax=20000;
int angdiv=100;
int vrmin=10;
int resa, resb;

int avga1,avga2,avga3,avga4,avgb1,avgb2,avgb3,avgb4;

double avgcov1=0.0, avgcov2=0.0, avgcov3=0.0, avgcov4=0.0;
FILE *varout;

anga=0.0;
angb=PI/4.0;
angap=PI/2.0;
angbp=3.0*PI/4.0;

srand(time(NULL));

bool stop=false;

varout=fopen("var2.out","w");

double maxavgs=-6.0;
double maxcof=0.0;

double avgs,pm4f,pm2f,pzf,p2f,p4f,sumpf;
int S=0;

//for(int j=0;j<angdiv;j++)
{
//   angb=PI*(double)j/(double)angdiv;

int pm4=0,pm2=0,pzero=0,p2=0,p4=0;
S=0;

//calculate probabilities of -4 -2 0 2 4 :
na=nb=0;

stop=false;
avgcov1=0.0;
avgcov2=0.0;
avgcov3=0.0;
avgcov4=0.0;
avga1=avgb1=avga2=avgb2=avga3=avgb3=avga4=avgb4=0;

for(i=0;!stop;i++)
{
int resa,resb;

phi=(double)rand()/MAX_INT*2.0*PI;
//angb=PI/(double)angdiv*(double)j;

resa=-A(anga,phi);
resb=A(phi,angb);

avga1+=resa;
avgb1+=resb;
cov1=resa*resb;

phi=(double)rand()/MAX_INT*2.0*PI;
resa=-A(anga,phi);
resb=A(phi,angbp);
avga2+=resa;
avgb2+=resb;
cov2=resa*resb;

phi=(double)rand()/MAX_INT*2.0*PI;
resa=-A(angap,phi);
resb=A(phi,angb);
avga3+=resa;
avgb3+=resb;
cov3=resa*resb;

phi=(double)rand()/MAX_INT*2.0*PI;
resa=-A(angap,phi);
resb=A(phi,angbp);
avga4+=resa;
avgb4+=resb;
cov4=resa*resb;

avgcov1+=cov1;
avgcov2+=cov2;
avgcov3+=cov3;
avgcov4+=cov4;

switch((int)(cov1-cov2+cov3+cov4))
{
case -4 : pm4++; break;
case -2 : pm2++; break;
case 0 : pzero++; break;
case 2 : p2++; break;
case 4 : p4++; break;
}

S+=(int)(cov1-cov2+cov3+cov4);

if(i>=vrmax-1) stop=true;
}

cov1/=(double)i;

//probabilities of measurement results floating

pm4f=(double)pm4/(double)vrmax;
pm2f=(double)pm2/(double)vrmax;
pzf=(double)pzero/(double)vrmax;
p2f=(double)p2/(double)vrmax;
p4f=(double)p4/(double)vrmax;

sumpf=pm4f+pm2f+pzf+p2f+p4f;

//compute maximal average of S taking into account the variance

avgs=fabs((double)S-(double)(avga1*avgb1-avga2*avgb2+avga3*avgb3+avga4*avgb4)/(double)vrmax);
avgs/=(double)vrmax;

//avgs=sqrt(pm4f*16.0+pm2f*4.0+p2f*4.0+16.0*p4f-avgs*avgs)/2.0;
//fprintf(varout,"%lf %lf %lf\n",cof,avgs,fabs((double)S/(double)vrmax));

fprintf(varout,"%lf %lf %lf\n",angb,avgcov1/(double)i,avgs);
cov1=0.0;

if(avgs>maxavgs)
{
maxavgs=avgs;
maxcof=cof;
maxangb=angb;
}

}  //end loop angdiv

printf("p-4=%lf p-2=%lf p0=%lf sumprob=%lf\n",pm4f,pm2f,pzf,sumpf);
printf("S=%lf Covariances : %lf %lf %lf %lf\n",(double)S/(double)vrmax, avgcov1/(double)vrmax, avgcov2/(double)vrmax, avgcov3/(double)vrmax, avgcov4/(double)vrmax);
printf("Sumcov=%lf\n",(avgcov1-avgcov2+avgcov3+avgcov4)/(double)vrmax);

printf("maxavgS=%lf angb=%lf\n",maxavgs, maxangb);
fclose(varout);
}

Here S oscillate and can be smaller than 2.

In Basic I wrote the same algorithm for Smaky emulating a 68040 processor.

In the latter the result is always above 2, namely around 2.08.

What could explain this difference ? Could it be that the skewness is positive so that by simulating it we obtain a numerical average higher ?

Then why would the Linux program give other results ?

2. Sep 2, 2016

### Staff: Mentor

Number precision may be the reason.

https://msdn.microsoft.com/en-us/library/ae55hdtk.aspx

and for C

https://www.cs.princeton.edu/courses/archive/fall09/cos323/precepts/precept2.html

Check to see if you are using the same/equivalent datatypes.

Also each language may be using a different implementation of numerical methods each with its own peculiarities.

You could try implementing it in MATLAB or in Python (see the Anaconda distribution for prepackaged python numpy and scipy modules).

I had a similar experience years ago when I implemented a simple "what day of the week" from a date algorithm in several languages including fortran and honeywell basic for a Boy Scouts Explorer open house.

One mother put in her son's birthday in each program. One said Sunday and said Monday and she wanted to know why and I humorously said maybe he was born at midnight. She was not amused, told her husband who turned out to an upper level computer center manager for my site and I never heard the end of it.

The moral: No good deed goes unpunished.

Upon investigation, I found one language rounded up and the other had truncated the answer. The algorithm expected the rounding up to work correctly.

Last edited: Sep 2, 2016
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted