Published
Edited
Jan 16, 2020
1 star
Insert cell
md`# Bell's theorem
<sup>under construction</sup>`
Insert cell
d3 = require("https://d3js.org/d3.v5.min.js")
Insert cell
// given 2 states in 3 dimentions, we have 2^3 = 8 state combinations
states = [
[0, 0, 0], // 0
[0, 0, 1], // 1
[0, 1, 0], // 2
[0, 1, 1], // 3
[1, 0, 0], // 4
[1, 0, 1], // 5
[1, 1, 0], // 6
[1, 1, 1], // 7
]
Insert cell
// we pick randomly two dimentions out of three
// we could pick the same dimention twice
observations = [
[0,0], // 0
[0,1], // 1
[0,2], // 2
[1,0], // 3
[1,1], // 4
[1,2], // 5
[2,0], // 6
[2,1], // 7
[2,2], // 8
]
Insert cell
classical_data = {
const result = {
name: 'Experiment',
children: []
}
let count = 0
let equals = 0
let non_equals = 0
states.forEach(s => {
//
const state = {
name: `state: [${s[0]}, ${s[1]}, ${s[2]}]`,
children: []
}
observations.forEach(o => {
const o0 = s[o[0]]
const o1 = s[o[1]]
count++
if (o0 === o1) {
equals++
}
else {
non_equals++
}
const observation = {
name: `observation #${count}: ${o[0]} and ${o[1]} (${o0} and ${o1} are ${ o0 === o1 ? '' : 'not ' } equal) (equals: ${equals}, non-equals: ${non_equals}, probability: ${Math.floor(equals * 100 / count)}%)`
}
state.children.push(observation)
})
result.children.push(state)
})
return d3.hierarchy(result)
}
Insert cell
Insert cell
dx = 12
Insert cell
dy = 120
Insert cell
function graph(root, {
label = d => d.data.id,
highlight = () => false,
marginLeft = 50
} = {}) {
const tree = d3.tree().nodeSize([dx, dy])
root = tree(root);

let x0 = Infinity;
let x1 = -x0;
root.each(d => {
if (d.x > x1) x1 = d.x;
if (d.x < x0) x0 = d.x;
});

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, x1 - x0 + dx * 2])
.style("overflow", "visible");
const g = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${marginLeft},${dx - x0})`);
const link = g.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr("stroke", d => highlight(d.source) && highlight(d.target) ? "red" : null)
.attr("stroke-opacity", d => highlight(d.source) && highlight(d.target) ? 1 : null)
.attr("d", treeLink);
const node = g.append("g")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${d.y},${d.x})`);

node.append("circle")
.attr("fill", d => highlight(d) ? "red" : d.children ? "#555" : "#999")
.attr("r", 2.5);

node.append("text")
.attr("fill", d => highlight(d) ? "red" : null)
.attr("dy", "0.31em")
.attr("x", d => d.children ? -6 : 6)
.attr("text-anchor", d => d.children ? "end" : "start")
.text(label)
.clone(true).lower()
.attr("stroke", "white");
return svg.node();
}
Insert cell
graph(classical_data, {label: d => d.data.name})
Insert cell
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
Insert cell
random(0, 1)
Insert cell
class Quantum {
constructor (x, y, z) {
this._x = x
this._y = y
this._z = z
}
get x() {
if(this._x === undefined) {
this._x = random(0, 1)
}
this._y = undefined
this._z = undefined
return this._x
}
get y() {
if(this._y === undefined) {
this._y = random(0, 1)
}
this._x = undefined
this._z = undefined
return this._y
}
get z() {
if(this._z === undefined) {
this._z = random(0, 1)
}
this._x = undefined
this._y = undefined
return this._z
}
}
Insert cell
q = new Quantum(0,0,0)
Insert cell
q.x
Insert cell
q
Insert cell
q.y
Insert cell
q
Insert cell
q.z
Insert cell
max_i = 100000
Insert cell
{
let e = 0
let ne = 0

for (let i = 0; i < max_i; i++) {
if (random(0,1) === random(0,1)) {
e++
}
else {
ne++
}
}
return Math.floor(e * 100 / (e + ne))
}
Insert cell
dimentionName = (index) => {
switch(index) {
case 0:
return 'x'
case 1:
return 'y'
case 2:
return 'z'
default:
throw Error('out of range')
}
}
Insert cell
{
let e = 0
let ne = 0

for (let i = 0; i < 100000; i++) {
const quantum = new Quantum(0, 0, 0)
const o0 = quantum[dimentionName(random(0,2))]
const o1 = quantum[dimentionName(random(0,2))]
if (o0 === o1) {
e++
}
else {
ne++
}
}
return Math.floor(e * 100 / (e + ne))
}
Insert cell
quantum_data = {
const result = {
name: 'Experiment',
children: []
}
let count = 0
let equals = 0
let non_equals = 0
states.forEach(s => {
//
const state = {
name: `state: [${s[0]}, ${s[1]}, ${s[2]}]`,
children: []
}
const quantum = new Quantum(s[0], s[1], s[2])
observations.forEach(o => {
const o0 = quantum[dimentionName(o[0])]
const o1 = quantum[dimentionName(o[1])]
count++
if (o0 === o1) {
equals++
}
else {
non_equals++
}
const observation = {
name: `observation #${count}: ${o[0]} and ${o[1]} (${o0} and ${o1} are ${ o0 === o1 ? '' : 'not ' } equal) (equals: ${equals}, non-equals: ${non_equals}, probability: ${Math.floor(equals * 100 / count)}%)`
}
state.children.push(observation)
})
result.children.push(state)
})
return d3.hierarchy(result)
}
Insert cell
graph(quantum_data, {label: d => d.data.name})
Insert cell
{
let equals = 0
const count = 100000
for (let i = 0; i < count; i++) {
const q = new Quantum(random(0,1), random(0,1), random(0,1))
if (q[dimentionName(random(0,2))] === q[dimentionName(random(0,2))]) {
equals++
}
}
return equals / count
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more