> ## 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.

# Cantor Set: Building a Fractal with Phase Change

In the previous tutorial, we built the **Cantor Set** one ternary digit at a time. We started with `0`, paired `0` with `2`, and used `interfere()` to grow the pattern.

This time, let's try something different. Instead of building the Cantor Set step by step, we start with all the values and **mark** the ones that belong to it.

We will do that with a **phase change**.

## Finding the Cantor values

Let's first remind ourselves what makes a value part of the Cantor Set.

Its ternary digits can only be:

```text theme={null}
0 or 2
```

A ternary digit `1` means that the value is in the middle third, so it is not part of the Cantor Set.

For example, `202` belongs to the Cantor Set because it contains only `0`s and `2`s. But `201` does not belong to the Cantor Set because it contains a `1`.

So we need a small function that can answer one question: *Is this value in the Cantor Set?*

We can write this function as:

```python theme={null}
def is_cantor(x: int, n: int) -> bool:
    if x >= 3 ** n:
        return False

    for _ in range(n):
        if x % 3 == 1:
            return False
        x //= 3

    return True
```

Let's see what it does.

We look at the ternary digits of `x`, one at a time.

If we find a `1`, we immediately return `False` because the value is not in the Cantor Set.

If we check all the digits without finding a `1`, we return `True` because the value belongs to the Cantor Set.

For three ternary digits, for example:

```text theme={null}
000  →  True
002  →  True
020  →  True
022  →  True

001  →  False
012  →  False
201  →  False
```

So now we have a way to recognize the values that belong to the Cantor Set.

## Starting with all the values

Now let's create the values that we want to check.

We use the code below:

```python theme={null}
n = 3
size = 5

u = quint.all(size=size)
```

Since `size = 5`, this gives us the values:

```text theme={null}
0, 1, 2, ..., 31
```

But our Cantor Set has `n = 3` ternary digits.

So the values with three ternary digits go from:

```text theme={null}
0 to 26
```

because:

```text theme={null}
3 ** 3 = 27
```

This is why we have this check at the beginning of `is_cantor()`:

```python theme={null}
if x >= 3 ** n:
    return False
```

It simply says: *If the value is outside the range we are using, it is not part of this Cantor Set.*

So values `27` to `31` are not part of the Cantor Set.

Now we have all the values we need.

At this point, nothing has been marked yet. All the values have the same phase, which is shown in the debugger:

<table>
  <tr>
    <td align="center">
      ![Debugger showing the values before applying the phase change](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_phase_before_1.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=37e7711255c1a615e85b32b83307209f)
    </td>

    <td align="center">
      ![Debugger showing the values before applying the phase change](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_phase_before_2.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=dad3ac3025ef30d41110f3c0d6453424)
    </td>
  </tr>
</table>

Now let's mark the Cantor values.

## Marking the Cantor values

We already know how to check whether a value belongs to the Cantor Set.

So we can use the result to choose a phase.

```python theme={null}
import math

def mark_cantor(x: int, n: int) -> float:
    return math.pi if is_cantor(x, n) else 0.0
```

This gives us a very simple rule:

```text theme={null}
Cantor value  →  phase π
Other value   →  phase 0
```

Why change the phase by `π`? Because a phase of `π` changes the direction of an amplitude.

So we can think of it as putting a small **minus sign** on the Cantor values.

We are not adding or removing any values. We are only changing their phase.

Now we can apply this rule with `add_phase()`:

```python theme={null}
u.add_phase(
    lambda x: mark_cantor(x, n)
)
```

The `lambda` simply tells `add_phase()` to use `mark_cantor()` for each value.

The phase function marks each value according to whether it belongs to the Cantor Set. Cantor values receive a phase of `π`, while all other values receive a phase of `0`.

And now we can see the result in the debugger.

<table>
  <tr>
    <td align="center">
      ![Debugger showing the Cantor values after applying the phase change](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_phase_after_1.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=7e3cd488dbc4313487c434ded9c71d07)
    </td>

    <td align="center">
      ![Debugger showing the Cantor values after applying the phase change](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_phase_after_2.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=8aaaae03087fd274740331a8b9326f4f)
    </td>
  </tr>
</table>

The Cantor values now have a different phase from the others.

So the Cantor Set is **marked by phase**.

## What changed?

In the previous tutorial, we built the Cantor Set using interference.

We started with:

```text theme={null}
0
```

and gradually created:

```text theme={null}
0  2
```

then:

```text theme={null}
00  02  20  22
```

This time, we do something different.

We start with all the values in `u` using `quint.all()`:

```text theme={null}
0  1  2  3  ...  31
```

Then we check each one.

If it belongs to the Cantor Set, we give it phase `π`. Otherwise, we give it phase `0`.

So the whole idea is:

```text theme={null}
Start with all values
        ↓
Check each value
        ↓
Is it in the Cantor Set?
     ↓          ↓
    yes        no
     ↓          ↓
 phase π      phase 0
```

The marked values correspond to the same Cantor Set construction we built before.

The only difference is how we create it.

This time, we don't grow the pattern with interference. We **mark it with phase**.

**That's the Cantor Set with phase in Rhyme!**


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