How to make quantum mechanics contradict itself with FunQy and IBM Q Experience

Ryan Vandersmith
8 min readJan 17, 2019

A few months ago, I came across a particularly mind-boggling Ars Technica article about a recent paper claiming that quantum theory is self-contradictory. If you don’t feel like clicking to the article yourself, the basic idea is that if you have a quantum system that contains knowledge of quantum mechanics (i.e. makes decisions based on quantum mechanical predictions), reality can contradict itself for different observers.

The paper presents a “Gedankenexperiment” (which is just a frilly German word for “thought experiment”) in which four different “agents” try to decide on the outcome of a coin flip. However, two of these agents are trapped in what are essentially Schrödinger's boxes (the cat ones), making everything vastly more complicated.

It gets *very* complicated.

I honestly still get confused by the diagrams in the paper, so for those among us who don’t speak bra-ket, here is the algorithm illustrated with the graceful, elegant simplicity of Microsoft Paint:

There! Cleared everything right up for you.

After an hour of drawing this abomination, I realized that this probably does very little to clarify the paradox. In the illustration (which loosely mirrors the setup in the Ars Technica article), you can see some sort of interaction in which box “A” flips a coin and sends the corresponding state to “B”, which creates a green arrow inside an egg. Then, as “a.” and “b.” measure this from the outside with their cheese graters, their eggs are different, causing the universe to collapse. You can think of the green arrows as representing quantum states, where “sideways” is a superposition of “up” and “down”. When the coin flips “heads”, the quantum state visible to “A” will be an “down” arrow, and if “tails” the state will be a “sideways” superposition arrow. If the coin flip happens to be “tails” and “b.” measures a “down” arrow, “B” actually has a 50/50 chance of measuring “down” because his measurement still accounts for an unresolved superposition between the original coin flip of “heads” or “tails”. Bleeeeeeeeargh. This contradiction is extremely important to our understanding of quantum theory, but it’s amazingly difficult f̶o̶r̶ ̶m̶e̶ to explain.

Coming from a software engineering background, I would much rather see this written as an executable block of code rather than trying to think about coins and boxes and arrows. Since quantum computers are slowly becoming more accessible for the everyday enthusiast, I also wanted to make this algorithm available for anyone to splinter reality for their own twisted enjoyment.

If you haven’t yet delved into the quagmires of quantum computing, even the simplest algorithms tend to look really horrifying for beginners, involving enormous unitary matrices and circuits that require a fairly in-depth understanding of linear algebra, signal processing, circuit diagramming, and of course quantum mechanical “bra-ket” notation. Just look at the Wikipedia page for quantum logic gates, which is possibly the “simplest” concept in quantum computing.

I’m personally not a fan of this approach, so I built my own hybrid quantum programming language, FunQy, for writing algorithms that can be understandable by those familiar with JavaScript, Python, C#, Haskell, Rust, and any other mainstream programming language with features such as first-class functions and polymorphic data types.

Before diving into the implementation, let’s clarify some of FunQy’s language constructs so that the code is easier to follow. The language’s standard library defines the boolean values F and T, which are also equivalent to orthogonal qubit state vectors (usually written as |0⟩ and |1⟩). The operator ^ designates a superposition between two states, and should not be confused with the bitwise XOR operator from most programming languages. For example, the expression F ^ T corresponds to the quantum state (|0⟩+|1⟩)/√2, which has an equal chance of being measured as F or T. Measurement is designated using the measure() function, which collapses a state to one constituent value based on its probability distribution from quantum mechanics.

It is also possible to entangle states using expressions such as if a then t else f , which returns the value t to the extent that a equals T, and f to the extent that a equals F. For those who are interested in the specifics, I highly recommend checking out the documentation and examples on FunQy’s GitHub repository.

And now, for the code that breaks reality:

let coin = F ^ T
let r: Bool = measure(coin)
let box1 = if r then (F ^ T) else F
let box1_m: Bool = measure(box1)
let box2 = if coin then box1_m else F
let m1 = box1_m
let m2: Bool = measure(box2)
print (m1, m2)

Turns out it only takes about 8 lines of code to fracture the universe.

Here’s an in-depth breakdown with comments:

// Define the superposition of both possible coin flip outcomes. 
1.
let coin = F ^ T
// Simulate flipping a coin by sampling from the (coin) superposition. The result will be either (F) or (T) with equal probabilities.
2. let r: Bool = measure(coin)
// Create a quantum state dependent on the coin flip (r), which is the superposition (F ^ T) if (r == T), otherwise (F).
3. let box1 = if r then (F ^ T) else F
// Measure the previous state (collapse the wave function from the second box's perspective).
4. let box1_m: Bool = measure(box1)
// Define the state of the second box according to the measurement of the first box. This step encodes the "knowledge of quantum mechanics" because, from this perspective, the coin flip is still a superposition. Swapping (coin) with (r) here will resolve the discrepancy, but this corresponds to a different experimental setup and therefore does not resolve the paradox.
5. let box2 = if coin then box1_m else F
// Measure (box1) as an external observer. The wave function has already collapsed, so it will be the same as (box1_m).
6. let m1 = box1_m
// Measure (box2) as an external observer. According to quantum theory, this measurement depends on unresolved superpositions from both the coin flip (r) and the second box's measurement (box1_m).
7. let m2: Bool = measure(box2)
// Return a tuple of measurements to show that the external measurements (m1, m2) do not necessarily agree. Quantum theory expects these to always be the same, but there is a 25% chance of a discrepancy due to the superposition in line 5.
8. print (m1, m2)

Running this code repeatedly through the FunQy simulator confirms that the measurements do, in fact, occasionally disagree:

$ funqy eval Contradiction.fqy
>> (F, F)
$ funqy eval Contradiction.fqy
>> (F, F)
$ funqy eval Contradiction.fqy
>> (F, F)
$ funqy eval Contradiction.fqy
>> (T, T)
$ funqy eval Contradiction.fqy
>> (F, F)
$ funqy eval Contradiction.fqy
>> (F, F)
$ funqy eval Contradiction.fqy
>> (T, F)

$ funqy eval Contradiction.fqy
>> (F, F)

Even better, you can try this yourself using FunQy Online, which is a (delightfully low-budget) online interpreter for writing FunQy code. It works on both desktop and mobile, and comes preloaded with the code from this article so you don’t have to worry about copy-pasting anything.

In case you are familiar with quantum circuits, I also converted the code to OpenQASM, which has become more or less the assembly equivalent for quantum computing architectures.

Here’s what the (approximate) QASM version looks like:

include "qelib1.inc";
qreg q[5];
creg c[5];
// [0]: Box #2 perspective
// [1]: Coin flip superposition
// [2]: (unused)
// [3]: Coin flip result
// [4]: Box #1 perspective
// IBM Q displays result bits in the order 43210
// Outcomes matching [1---1] and [0---0] indicate box measurements being the same, while outcomes matching [0---1] and [1---0] indicate box measurements being different. Current quantum computers are very noisy and will tend to have significantly less-than-ideal measurements. This algorithm is technically not correct due to all the current limitations in IBM Q, but it is close enough for running something conceptually similar on a real quantum computer. // Not-so-random coin flip (comment this out for 'heads')
x q[3];
// Controlled-H gate
h q[4];
sdg q[4];
cx q[3],q[4];
h q[4];
t q[4];
cx q[3],q[4];
t q[4];
h q[4];
s q[4];
x q[4];
barrier q;// Coin flip superposition
h q[1];
// Controlled-H gate
h q[0];
sdg q[0];
cx q[1],q[0];
h q[0];
t q[0];
cx q[1],q[0];
t q[0];
h q[0];
s q[0];
x q[0];
barrier q;measure q -> c;

If you’re feeling adventurous, you can copy/paste the above code into good ol’ IBM Q Experience, which is a publicly available platform for interacting with real-life quantum computers. I designed the above code for the ibmqx4 backend, which is the default as of writing this article. It’s quite viscerally satisfying to run this on a real (not simulated) quantum computer just for the novelty of forcing hundreds of new parallel universes into existence, or whatever is happening to make this contradiction possible.

Speaking of which, what actually is happening? In order for the paradox to be resolved, at least one of the following must be true in our universe:

  1. Quantum mechanics only applies to certain aspects of the universe, such as on different scales or in different contexts (this corresponds to using r instead of coin on line 5 of our implementation, which would resolve the contradiction).
  2. Quantum mechanics is logically inconsistent, meaning that contradictions are allowed to exist between different perspectives (conventional quantum mechanics now implies this, which is bad for anyone who wants to do anything, you know, logical with it).
  3. Quantum mechanics can result in more than one measurement simultaneously for a given event (I might build a “multiverse” backend for FunQy to show how crazy this would be if this is true).

I hope you enjoyed learning about this particular glitch in the matrix. If you would like to continue reading about the contradiction, I would highly recommend checking out the original paper (another search term is the Frauchiger-Renner theorem). We’re always looking for feedback and contributions to the FunQy repository, which has been chugging along as hands-down the best high-level language for exploring the hypothetical strengths and limitations of quantum computing (for instance, I also wrote a FunQy script to build a quantum superposition of the most likely Elder Scrolls VI locations).

Quantum computing is a very unique and awesome field in computer science, and I hope that this article inspires someone out there to play around with this code or even try to write their own quantum algorithm. Definitely let me know in the comments below if you’re that person, and be sure to share this article with anyone else who might get a kick out of this as well.

Cheers!

~ Ryan

--

--

Ryan Vandersmith

Enthusiastic programming language designer and full-stack Progressive Web App developer.