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

# Radio Access Network

> [card] [View on GitHub](https://github.com/Classiq/classiq-library/blob/main/applications/telecom/radio_access_network/radio_access_network_positioning_antennas.ipynb)
>
> Open this notebook in GitHub to run it yourself

Radio Access Network (RAN) is an important part of network communication systems, responsible for connecting used devices such as smartphones and radio machines to a wireless network.

Optimizing the Radio Access Network involves various tasks that make it challenging to optimize a network efficiently.

These tasks include resource allocation, locating transmission devices, such as antennas, to enhance coverage according to the overall consumption. Ideally, the optimization of a RAN will maximize the resource utilization of the network, save operational costs to the owner of the network.

In this case of RAN, the solution is the positions of the set of antennas we have in a region that that has consumers spread in various locations.

Finding good positions of antennas with a limited number of antennas is very complex to optimize and find a good solution in polynomial time.

##

1. Define Problem Classically with Pyomo

* We have a limited number of antennas defined by $N$.
* We have a set of potential locations: $\{1,2,3,...,M\}$, where $M>N$
* We limit certain locations with an overlap, not to use 2 antennas.

#

## Mathematical Definition

Each location is a binary variable $x_{i}$ that is 1 if we put antenna there and 0 if we don't put in that location.

Each location is charachterized with certain consumption $c_{i}$.

Mathematically, it is translated into objective function which aims to maximized its coverage:

$$
\max_{x} \sum_{i} c_{i}x_{i}
$$

Now, we add the constraints, such as the number of antennas:

$$
\sum_{i} x_{i} \leq N
$$

We can also add a constraint that prevent an ovelap between antennas.

All sets of neighboring antenna sites $\{n_{0},n_{1},...,n_{k}\}$ will have only 1 anntenna on the ground:

$$
\sum_{i}^{k} x_{n_{i}} == 1
$$

#

## Defining Pyomo Model

```python theme={null}
# Import relevant packages

import random

import matplotlib.pyplot as plt
import numpy as np
import pyomo.environ as pyo

random.seed(0)
np.random.seed(0)

# potenital locations
M = 13
# number of antennas
N = 7

# consumption coefficient
c_vec = np.random.rand(1, M)[0]

neighbors1 = [1, 3, 4]
neighbors2 = [0, 5]
neighbors3 = [6, 9]
```

```python theme={null}

model = pyo.ConcreteModel()

# define the variables
model.x = pyo.Var(range(M), domain=pyo.Binary)

x_variables = np.array(list(model.x.values()))

# constriants
model.num_antennas = pyo.Constraint(expr=sum(x_variables[i] for i in range(M)) <= N)

model.neigh1 = pyo.Constraint(expr=sum(x_variables[i] for i in neighbors1) == 1)
model.neigh2 = pyo.Constraint(expr=sum(x_variables[i] for i in neighbors2) == 1)
model.neigh3 = pyo.Constraint(expr=sum(x_variables[i] for i in neighbors3) == 1)

model.obj = pyo.Objective(expr=x_variables @ c_vec, sense=pyo.maximize)
```

## Define QAOA Parameters and Synthesize

In order to solve the Pyomo model defined above, we use the Classiq combinatorial optimization engine via the `CombinatorialProblem` class.

For the quantum part of the QAOA algorithm, define the number of repetitions (`num_layers`) and the `penalty_factor` to get results that satisfy your constraints. Be careful! A large penalty can also bring you away from the optimized solution:

```python theme={null}
from classiq import *
from classiq.applications.combinatorial_optimization import CombinatorialProblem

combi = CombinatorialProblem(pyo_model=model, num_layers=4, penalty_factor=3.0)
```

The classical optimization settings — such as the maximum number of iterations (`maxiter`) — are provided later, when we call `combi.optimize`.

Lastly, we load the model, based on the problem and algorithm parameters, which we can use to solve the problem:

```python theme={null}
qmod = combi.get_model()
```

We also set the quantum backend we want to execute on:

```python theme={null}
execution_preferences = ExecutionPreferences(
    num_shots=3000,
    # backend_preferences=ClassiqBackendPreferences(backend_name="aer_simulator")
)
```

We can now synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:

```python theme={null}
qprog = combi.get_qprog()
show(qprog)
```

> [!NOTE]
>
> **Output:**
>
> ```
>
> Quantum program link: https://platform.classiq.io/circuit/3IoSFSLLrBp7gqAR5FeoK7vCBH3
>
>
> ```

> [!NOTE]
>
> **Output:**
>
> ```
> https://platform.classiq.io/circuit/3IoSFSLLrBp7gqAR5FeoK7vCBH3?login=True&version=22
>
>
> ```

## Executing the Hybrid Algorithm

We now solve the problem by running the hybrid optimization loop with `combi.optimize`:

```python theme={null}
optimized_params = combi.optimize(
    execution_preferences=execution_preferences, maxiter=60, quantile=1.0
)
```

> [!NOTE]
>
> **Output:**
>
> ```
> /home/dor/Sources/Classiq/library-13/lib/python3.13/site-packages/classiq/applications/combinatorial_optimization/combinatorial_problem.py:258: ClassiqDeprecationWarning: ExecutionSession.minimize is deprecated; use variational_minimize instead.
>     result = _es.minimize(
>
>
> ```

We can check the convergence of the run:

```python theme={null}
plt.plot(combi.cost_trace)
plt.xlabel("Iterations")
plt.ylabel("Cost")
plt.title("Cost convergence")
```

> [!NOTE]
>
> **Output:**
>
> ```
>
> Text(0.5, 1.0, 'Cost convergence')
>
>
> ```

![output](https://mintcdn.com/classiq/tcPvHr2JZgblscSZ/explore/applications/telecom/radio_access_network/radio_access_network_positioning_antennas_files/radio_access_network_positioning_antennas_1.png?fit=max\&auto=format\&n=tcPvHr2JZgblscSZ\&q=85\&s=fca1f763c9294cdd2fe8eac48c78c9f5)

## Analyze Results

We can also examine the statistics of the algorithm:

```python theme={null}
optimization_result = combi.sample(optimized_params)
optimization_result.sort_values(by="cost", ascending=True).head(5)
```

| | solution | probability | cost |
| - | - | - | - |
| 713 | \{'x': \[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1],... | 0.000333 | -4.724866 |
| 545 | \{'x': \[1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0],... | 0.000333 | -4.225212 |
| 1957 | \{'x': \[0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0],... | 0.000333 | -4.202724 |
| 506 | \{'x': \[1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0],... | 0.000333 | -3.915615 |
| 1174 | \{'x': \[1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],... | 0.000333 | -3.781915 |

```python theme={null}
optimization_result.hist("cost", weights=optimization_result["probability"])
```

> [!NOTE]
>
> **Output:**
>
> ```
> array([[<Axes: title={'center': 'cost'}>]], dtype=object)
>
>
> ```

![output](https://mintcdn.com/classiq/tcPvHr2JZgblscSZ/explore/applications/telecom/radio_access_network/radio_access_network_positioning_antennas_files/radio_access_network_positioning_antennas_2.png?fit=max\&auto=format\&n=tcPvHr2JZgblscSZ\&q=85\&s=00edaf6d5de0f9880c732beec3693f7c)


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