array_swap

guppylang.std.array.array_swap(arr: array[L, n], idx: int, idx2: int) None[source]

Swap two elements in an array at indices idx and idx2.

Exchanges the elements at two indices within the array. This operation uses HUGR’s native swap operation from the collections.array extension,

The swap happens in-place. Panics if either index is out of bounds.

Works with both copyable and linear types (like qubits).

Args:

arr: The array to modify idx: Index of first element to swap idx2: Index of second element to swap

arr = array(10, 20, 30, 40)
array_swap(arr, 0, 3)
# arr is now [40, 20, 30, 10]