toqito.helper.update_odometer

Updates the odometer.

Module Contents

toqito.helper.update_odometer.update_odometer(old_ind, upper_lim)[source]

Increase a vector as odometer.

Increases the last entry of the vector old_ind by 1, unless that would make it larger than the last entry of the vector upper_lim. In this case, it sets the last entry to 0 and instead increases the second-last entry of old_ind, unless that would make it larger than the second-last entry of upper_lim. In this case, it sets the second-last entry to 0 and instead increases the third-last entry of old_ind (and so on; it works like an odometer).

This function is useful when you want to have k nested loops, but k isn’t specified beforehand. For example, instead of looping over i and j going from 1 to 3, you could loop over a single variable going from 1 to 3^2 and set [i, j] = update_odometer([i, j], [3, 3]) at each step within the loop.

This function is adapted from QETLAB [@QETLAB_link].

Examples

```python exec=”1” source=”above” from toqito.helper import update_odometer import numpy as np vec = np.array([0, 0]) upper_lim = np.array([3, 2]) for j in range(0, np.prod(upper_lim)-1):

vec = update_odometer(vec, upper_lim) print(vec)

```

Parameters:
  • old_ind (list[int] | numpy.ndarray) – The initial vector.

  • upper_lim (list[int] | numpy.ndarray) – The upper limit on which to increase the odometer to.

Returns:

The updated vector.

Return type:

list[int] | numpy.ndarray