Lunatic program on Linux and Smaky ?

  • Thread starter Thread starter jk22
  • Start date Start date
  • Tags Tags
    Linux Program
Click For Summary
SUMMARY

The forum discussion centers on discrepancies in results obtained from simulating Bell's theorem using C on a Linux system and Basic on a Smaky system. The Linux implementation yields results oscillating around 2, while the Smaky consistently produces results above 2, approximately 2.08. Key factors influencing these differences include numerical precision, data type implementations, and the inherent characteristics of the programming languages used. The discussion highlights the importance of ensuring equivalent data types and numerical methods across different programming environments.

PREREQUISITES
  • Understanding of Bell's theorem in quantum mechanics
  • Familiarity with C programming and its numerical precision
  • Knowledge of Basic programming, particularly on Smaky systems
  • Awareness of numerical methods and their implementation in different programming languages
NEXT STEPS
  • Investigate numerical precision in C programming, focusing on data types and their impacts
  • Explore MATLAB and Python for implementing Bell's theorem simulations
  • Research differences in numerical methods across programming languages
  • Examine the effects of rounding and truncation in computational algorithms
USEFUL FOR

Researchers in quantum mechanics, software developers working with numerical simulations, and educators teaching programming concepts related to numerical precision and algorithm implementation.

jk22
Messages
732
Reaction score
25
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 ?

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 parameterint 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 ?
 
Technology news on Phys.org
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:
  • Like
Likes   Reactions: QuantumQuest and jim mcnamara