The Many-Worlds Interpretation of JavaScript

May 15, 2020 JavaScript, Quantum Mechanics,

Illustration: Hachibu

Edit: Thanks to the commenters on Hacker News for pointing out that I was mixing up the Many-Worlds Interpretation (Level III) with a Max Tegmark Level II Multiverse.

Introduction

What if you could explore your code in another universe? (Cue music: Also sprach Zarathustra, Op. 30 – Strauss) This is the question that popped into my mind after watching Sean Carroll’s talk on the many-worlds interpretation of quantum mechanics.

Hopefully, by the end of this blog post you’ll understand:

  1. the many-worlds interpretation of quantum mechanics in layman’s terms.
  2. how I answered this question using JavaScript and the Babel compiler framework.

Many-What The F***?

The many-worlds interpretation of quantum mechanics imagines our universe as one node in an infinitely branching tree of universes where every possible quantum outcome exists in its own universe. And each time a universe branches, it creates a child universe where the outcomes are slightly different from the parent universe.

graph TD y("Universe") y --> y_0("Universe 0") y --> y_1("Universe 1") y_0 --> y_00("Universe 00") y_0 --> y_01("Universe 01") y_1 --> y_10("Universe 10") y_1 --> y_11("Universe 11")

FizzBuzz in the Multiverse

Imagine that you are in a job interview, and you are asked to write FizzBuzz (which is probably a hint that you are in a simulation [1] ). You decide to use a for-loop so you write the following code.

 1for (let i = 1; i <= 100; i++) {
 2  if (i % 15 === 0) {
 3    console.log('FizzBuzz');
 4  } else if (i % 3 === 0) {
 5    console.log('Fizz');
 6  } else if (i % 5 === 0) {
 7    console.log('Buzz');
 8  } else {
 9    console.log(i);
10  }
11}

According to the many-worlds interpretation of quantum mechanics, your universe branched into many universes the moment you decided to use a for-loop. In this universe you wrote a for-loop, but in another universe you wrote a while-loop.

Many-Worlds JavaScript Interpreter

I’ve written a JavaScript command-line tool that allows you to explore your code in alternate universes. It’s essentially a Babel plugin that randomly applies transformations to your code that could be attributed to human decision making therefore simulating code you might have written in another universe.

Here’s a list of all the transformations that might be applied to your code in order to simulate outcomes in other universes.

  • Transform for-loops into while-loops.
  • Shuffle arrays and function call arguments.
  • Swap binary expression operands.
  • Swap bitwise, equality, logical and math operators.
  • Create numeric off-by-one errors.
  • Decrementing instead of incrementing inside of update expressions.

You can find the command-line tool here: github.com/hachibu/many-worlds-javascript-interpreter.

Conclusion

If we run the FizzBuzz example through the command-line tool, it will print out a binary tree of universes containing code you might have written in other universes during your simulated interview.

$ bin/run examples/fizzbuzz.js --recursion-depth 1
Universe {
  depth: 0,
  data: {
    // Original parent universe
    name: 'World 0',
    code: 'for (let i = 1; i <= 100; i++) {\n' +
      '  if (i % 15 === 0) {\n' +
      "    console.log('FizzBuzz');\n" +
      '  } else if (i % 3 === 0) {\n' +
      "    console.log('Fizz');\n" +
      '  } else if (i % 5 === 0) {\n' +
      "    console.log('Buzz');\n" +
      '  } else {\n' +
      '    console.log(i);\n' +
      '  }\n' +
      '}'
  },
  children: [
    Universe {
      depth: 1,
      data: {
        // Alternate child universe
        name: 'World 0 => World 0₀',
        code: 'for (let i = 2; i <= 99; i++) {\n' +
          '  if (0 === 14 % i) {\n' +
          "    console.log('FizzBuzz');\n" +
          '  } else if (2 % i === 1) {\n' +
          "    console.log('Fizz');\n" +
          '  } else if (0 == 6 / i) {\n' +
          "    console.log('Buzz');\n" +
          '  } else {\n' +
          '    console.log(i);\n' +
          '  }\n' +
          '}'
      }
    },
    Universe {
      depth: 1,
      data: {
        // Another alternate child universe
        name: 'World 0 => World 0₁',
        code: 'let i = 2;\n' +
          '\n' +
          'while (101 >= i) {\n' +
          '  if (15 % i === 0) {\n' +
          "    console.log('FizzBuzz');\n" +
          '  } else if (i % 4 === 1) {\n' +
          "    console.log('Fizz');\n" +
          '  } else if (i % 4 <= 0) {\n' +
          "    console.log('Buzz');\n" +
          '  } else {\n' +
          '    console.log(i);\n' +
          '  }\n' +
          '\n' +
          '  i++\n' +
          '}'
      }
    }
  ]
}
  1. “The simulation hypothesis or simulation theory proposes that all of reality, including the Earth and the universe, is in fact an artificial simulation, most likely a computer simulation.” – wikipedia.org
Thanks to Marín Alcaraz, Michael Geraci and Ryan Smith for editing.
2019-2020, Hachibu.