Skip to main content
In the previous tutorial, we learned how a variable can hold multiple values at once. For example:
Before we observe x, both 14 and 23 are part of the superposition. And when we call:
we get one of the values, that is either 14 or 23 . But what if we don’t want to simply accept whichever value we get? What if we could change the superposition before observing it? Let’s try that with something a little more important than node IDs.

Which tea do we get?

Suppose someone is making us tea, but it is in a superposition of being hot and being cold. So we don’t know whether it will be hot or cold when we observe it. We can represent this situation with a quantum string:
Here, superpose() creates equal superposition of the values hot and cold. So hot and cold each have a 50% chance of being observed. If we observe it:
we get either "hot" or "cold". But let’s say I have a preference. I only like hot tea. Can we do something about that before observing the tea?

Making values interfere

Rhyme provides an operation called interfere() that transforms amplitudes in a superposition by combining them pairwise. To use it, we first need to tell Rhyme which values in the superposition we want to pair. For our tea, there are only two values, so we pair them:
This is just an ordinary ordinary Python function. It says that hot is paired with cold, and cold is paired with hot. But there is a small question here. If hot and cold form a pair, is the pair (hot, cold) or (cold, hot)? This is what lr specifies.
Here, lr tells Rhyme to use the ordering (hot, cold) for the pair. In other words, lr determines the “left” element of the pair. Now we can call interfere():
But what does interfere() actually do?

The interference formula

Let’s look at what happens to the amplitudes of hot and cold. Before interference, our equal superposition has the following amplitudes. Let A be the amplitude of hot and B be the amplitude of cold, then: A=12A = \frac{1}{\sqrt{2}} B=12B = \frac{1}{\sqrt{2}} You can see these amplitudes in the Rhyme debugger:
Rhyme debugger showing equal amplitudes of hot and cold
The interfere() operation then applies the interference transformation to these two amplitudes. For our (hot, cold) ordering, the resulting amplitudes are: hot→A+B2hot \rightarrow \frac{A+B}{\sqrt{2}} cold→A−B2cold \rightarrow \frac{A-B}{\sqrt{2}} Let’s substitute the original amplitudes. For hot: hot→12+122=1hot \rightarrow \frac{ \frac{1}{\sqrt{2}}+\frac{1}{\sqrt{2}} }{\sqrt{2}} =1 and for cold: cold→12−122=0cold \rightarrow \frac{ \frac{1}{\sqrt{2}}-\frac{1}{\sqrt{2}} }{\sqrt{2}} =0 The resulting amplitudes are shown in the debugger:
Rhyme debugger showing resulting amplitudes of hot and cold
So after interference: hot→1hot \rightarrow 1 cold→0cold \rightarrow 0 The amplitude of hot becomes 1, while the amplitude of cold becomes 0. We can finally observe it:
Now it gives us hot. The debugger still shows the same value after the observation:
The debugger still shows the same value after the observation.
Nothing has changed because only hot had a non-zero amplitude before the observation. The interfere() operation had already reduced the amplitude of cold to zero. So observe() simply gives us the only value that can be observed: hot→1hot \rightarrow 1 cold→0cold \rightarrow 0 interfere() had shaped the superposition while observe() gave us the result. Although the last two screenshots are the same, the first screenshot demonstrates the effect of interfere(), while the second screenshot demonstrates what happens when we observe a value that already has a non-zero amplitude. That makes both screenshots useful rather than redundant.

Why does this work?

The important part is the sequence: superpose() ↓ interfere() ↓ observe() We started with two values. interfere() transforms the amplitudes so that the amplitude of hot becomes 1 and the amplitude of cold becomes 0. And notice something important about the code. We don’t have to write a function that manipulates amplitudes directly. We simply write two ordinary Python functions:
and tell Rhyme how we want the values to interact. That’s the interesting part of Rhyme: we can describe the logic of the quantum operation using familiar Python. In the previous tutorial, we learned how to create a superposition and observe it. Now we have learned how to shape a superposition before observing it. superpose() sets values in superposition. interfere() tranforms their amplitudes. observe() gives us the final value. That’s interference in Rhyme!