JSFiddle - React, Tailwind, and code Playground
JavaScript
function r(g) { //rad from grad
return g*2*Math.PI/360;
}
function d(a, p) { // detect or not a photon with angle p with detector at angle a
return Math.random() < (Math.cos(r(p-a)*2)+1)/2;
}
// perform the experiment once, simulated classically
// return a key for sorting the trial based on detector relative angles, and if there is a coincidence
function classic() {
var a=Math.random()<.5?0:45;
var b=Math.random()<.5?22.5:67.5;
var p=Math.floor(Math.random()*360);
var da=d(a,p);
var db=d(b,p);
return [a+"-"+b, da == db];
}
// perform the experiment once, with the qm "magic FTL" step
function qm() {
var a=Math.random()<.5?0:45;
var b=Math.random()<.5?22.5:67.5;
var p=Math.floor(Math.random()*360);
var da=d(a,p);
p = da?a:a+90; // magic FTL - measuring the photon here changes it there.
var db=d(b,p);
return [a+"-"+b, da == db];
}
function test(f, count) {
var r={};
for (var i=0;i<count;i++) {
var t=f();
r[t[0]] = r[t[0]] || {coincident:0, total: 0};
r[t[0]].total++;
if (t[1]) r[t[0]].coincident++;
r[t[0]].rate = r[t[0]].coincident / r[t[0]].total;
}
return r;
}
function c(a,b) {
return Math.pow(Math.cos(r(a-b)), 2);
}
document.write('<pre>classic:\n');
document.write(JSON.stringify(test(classic, 10000), null, 2));
document.write('</pre>');
document.write('<pre>qm:\n');
document.write(JSON.stringify(test(qm, 10000), null, 2));
document.write('</pre>');