> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rhymeq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# interfere(): Combining Amplitudes in a Superposition

In the previous tutorial, we learned how a variable can hold multiple values at once.

For example:

```python theme={null}
from rhyme import qint

x = qint(14, 23)
```

Before we observe `x`, both `14` and `23` are part of the superposition. And when we call:

```python theme={null}
x.observe()
```

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:

```python theme={null}
from rhyme import superpose

tea = superpose("hot", "cold")
```

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:

```python theme={null}
tea.observe()
```

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:

```python theme={null}
def pair(tea: str) -> str: 
    if tea == "hot": return "cold" 
    if tea == "cold": return "hot"
    return tea
```

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.

```python theme={null}
def lr(tea: str) -> bool: 
    return tea == "hot"
```

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()`:

```python theme={null}
tea.interfere(pair, lr)
```

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 = \frac{1}{\sqrt{2}}
$$

$$
B = \frac{1}{\sqrt{2}}
$$

You can see these amplitudes in the Rhyme debugger:

![Rhyme debugger showing equal amplitudes of hot and cold](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/interfere.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=9192fc9ca88750b34a3e6eb2c44ca48a)

The `interfere()` operation then applies the interference transformation to these two amplitudes. For our `(hot, cold)` ordering, the resulting amplitudes are:

$$
hot \rightarrow \frac{A+B}{\sqrt{2}}
$$

$$
cold \rightarrow \frac{A-B}{\sqrt{2}}
$$

Let's substitute the original amplitudes.

For `hot`:

$$
hot \rightarrow
\frac{
\frac{1}{\sqrt{2}}+\frac{1}{\sqrt{2}}
}{\sqrt{2}}
=1
$$

and for `cold`:

$$
cold \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](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/interfere_observe.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=3c085db266d70197c8769e85d49acf9f)

So after interference:

$$
hot \rightarrow 1
$$

$$
cold \rightarrow 0
$$

The amplitude of `hot` becomes `1`, while the amplitude of `cold` becomes `0`.

We can finally observe it:

```python theme={null}
print(tea.observe())
```

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.](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/interfere_observe.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=3c085db266d70197c8769e85d49acf9f)

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 \rightarrow 1
$$

$$
cold \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:

```
pair(...)
lr(...)
```

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!**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.