pygsti.tools.slicetools

Utility functions for working with Python slice objects

Module Contents

Functions

length(s)

Returns the length (the number of indices) contained in a slice.

shift(s, offset)

Returns a new slice whose start and stop points are shifted by offset.

intersect(s1, s2)

Returns the intersection of two slices (which must have the same step).

intersect_within(s1, s2)

Returns the intersection of two slices (which must have the same step).

indices(s[, n])

Returns a list of the indices specified by slice s.

list_to_slice(lst[, array_ok, require_contiguous])

Returns a slice corresponding to a given list of (integer) indices, if this is possible.

to_array(slc_or_list_like)

Returns slc_or_list_like as an index array (an integer numpy.ndarray).

divide(slc, max_len)

Divides a slice into sub-slices based on a maximum length (for each sub-slice).

slice_of_slice(slc, base_slc)

A slice that is the composition of base_slc and slc.

slice_hash(slc)

pygsti.tools.slicetools.length(s)

Returns the length (the number of indices) contained in a slice.

Parameters

sslice

The slice to operate upon.

Returns

int

pygsti.tools.slicetools.shift(s, offset)

Returns a new slice whose start and stop points are shifted by offset.

Parameters

sslice

The slice to operate upon.

offsetint

The amount to shift the start and stop members of s.

Returns

slice

pygsti.tools.slicetools.intersect(s1, s2)

Returns the intersection of two slices (which must have the same step).

Parameters

s1slice

First slice.

s2slice

Second slice.

Returns

slice

pygsti.tools.slicetools.intersect_within(s1, s2)

Returns the intersection of two slices (which must have the same step). and the sub-slice of s1 and s2 that specifies the intersection.

Furthermore, s2 may be an array of indices, in which case the returned slices become arrays as well.

Parameters

s1slice

First slice. Must have definite boundaries (start & stop cannot be None).

s2slice or numpy.ndarray

Second slice or index array.

Returns

intersectionslice or numpy.ndarray

The intersection of s1 and s2.

subslice1slice or numpy.ndarray

The portion of s1 that yields intersection.

subslice2slice or numpy.ndarray

The portion of s2 that yields intersection.

pygsti.tools.slicetools.indices(s, n=None)

Returns a list of the indices specified by slice s.

Parameters

sslice

The slice to operate upon.

nint, optional

The number of elements in the array being indexed, used for computing negative start/stop points.

Returns

list of ints

pygsti.tools.slicetools.list_to_slice(lst, array_ok=False, require_contiguous=True)

Returns a slice corresponding to a given list of (integer) indices, if this is possible.

If not, array_ok determines the behavior.

Parameters

lstlist

The list of integers to convert to a slice (must be contiguous if require_contiguous == True).

array_okbool, optional

If True, an integer array (of type numpy.ndarray) is returned when lst does not correspond to a single slice. Otherwise, an AssertionError is raised.

require_contiguousbool, optional

If True, then lst will only be converted to a contiguous (step=1) slice, otherwise either a ValueError is raised (if array_ok is False) or an array is returned.

Returns

numpy.ndarray or slice

pygsti.tools.slicetools.to_array(slc_or_list_like)

Returns slc_or_list_like as an index array (an integer numpy.ndarray).

Parameters

slc_or_list_likeslice or list

A slice, list, or array.

Returns

numpy.ndarray

pygsti.tools.slicetools.divide(slc, max_len)

Divides a slice into sub-slices based on a maximum length (for each sub-slice).

For example: divide(slice(0,10,2), 2) == [slice(0,4,2), slice(4,8,2), slice(8,10,2)]

Parameters

slcslice

The slice to divide

max_lenint

The maximum length (i.e. number of indices) allowed in a sub-slice.

Returns

list of slices

pygsti.tools.slicetools.slice_of_slice(slc, base_slc)

A slice that is the composition of base_slc and slc.

So that when indexing an array a, a[slice_of_slice(slc, base_slc)] == a[base_slc][slc]

Parameters

slcslice

the slice to take out of base_slc.

base_slcslice

the original “base” slice to act upon.

Returns

slice

pygsti.tools.slicetools.slice_hash(slc)