pygsti.tools.listtools

Utility functions for working with lists

Module Contents

Functions

remove_duplicates_in_place(l, index_to_test=None)

Remove duplicates from the list passed as an argument.

remove_duplicates(l, index_to_test=None)

Remove duplicates from the a list and return the result.

compute_occurrence_indices(lst)

A 0-based list of integers specifying which occurrence, i.e. enumerated duplicate, each list item is.

find_replace_tuple(t, alias_dict)

Replace elements of t according to rules in alias_dict.

find_replace_tuple_list(list_of_tuples, alias_dict)

Applies find_replace_tuple() on each element of list_of_tuples.

apply_aliases_to_circuits(list_of_circuits, alias_dict)

Applies alias_dict to the circuits in list_of_circuits.

sorted_partitions(n)

Iterate over all sorted (decreasing) partitions of integer n.

partitions(n)

Iterate over all partitions of integer n.

partition_into(n, nbins)

Iterate over all partitions of integer n into nbins bins.

_partition_into_slow(n, nbins)

Helper function for partition_into that performs the same task for

incd_product(*args)

Like itertools.product but returns the first modified (incremented) index along with the product tuple itself.

pygsti.tools.listtools.remove_duplicates_in_place(l, index_to_test=None)

Remove duplicates from the list passed as an argument.

Parameters
  • l (list) – The list to remove duplicates from.

  • index_to_test (int, optional) – If not None, the index within the elements of l to test. For example, if all the elements of l contain 2 tuples (x,y) then set index_to_test == 1 to remove tuples with duplicate y-values.

Returns

None

pygsti.tools.listtools.remove_duplicates(l, index_to_test=None)

Remove duplicates from the a list and return the result.

Parameters
  • l (iterable) – The list/set to remove duplicates from.

  • index_to_test (int, optional) – If not None, the index within the elements of l to test. For example, if all the elements of l contain 2 tuples (x,y) then set index_to_test == 1 to remove tuples with duplicate y-values.

Returns

list – the list after duplicates have been removed.

pygsti.tools.listtools.compute_occurrence_indices(lst)

A 0-based list of integers specifying which occurrence, i.e. enumerated duplicate, each list item is.

For example, if lst = [ ‘A’,’B’,’C’,’C’,’A’] then the returned list will be [ 0 , 0 , 0 , 1 , 1 ]. This may be useful when working with DataSet objects that have collisionAction set to “keepseparate”.

Parameters

lst (list) – The list to process.

Returns

list

pygsti.tools.listtools.find_replace_tuple(t, alias_dict)

Replace elements of t according to rules in alias_dict.

Parameters
  • t (tuple or list) – The object to perform replacements upon.

  • alias_dict (dictionary) – Dictionary whose keys are potential elements of t and whose values are tuples corresponding to a sub-sequence that the given element should be replaced with. If None, no replacement is performed.

Returns

tuple

pygsti.tools.listtools.find_replace_tuple_list(list_of_tuples, alias_dict)

Applies find_replace_tuple() on each element of list_of_tuples.

Parameters
  • list_of_tuples (list) – A list of tuple objects to perform replacements upon.

  • alias_dict (dictionary) – Dictionary whose keys are potential elements of t and whose values are tuples corresponding to a sub-sequence that the given element should be replaced with. If None, no replacement is performed.

Returns

list

pygsti.tools.listtools.apply_aliases_to_circuits(list_of_circuits, alias_dict)

Applies alias_dict to the circuits in list_of_circuits.

Parameters
  • list_of_circuits (list) – A list of circuits to make replacements in.

  • alias_dict (dict) – A dictionary whose keys are layer Labels (or equivalent tuples or strings), and whose values are Circuits or tuples of labels.

Returns

list

pygsti.tools.listtools.sorted_partitions(n)

Iterate over all sorted (decreasing) partitions of integer n.

A partition of n here is defined as a list of one or more non-zero integers which sum to n. Sorted partitions (those iterated over here) have their integers in decreasing order.

Parameters

n (int) – The number to partition.

pygsti.tools.listtools.partitions(n)

Iterate over all partitions of integer n.

A partition of n here is defined as a list of one or more non-zero integers which sum to n. Every partition is iterated over exacty once - there are no duplicates/repetitions.

Parameters

n (int) – The number to partition.

pygsti.tools.listtools.partition_into(n, nbins)

Iterate over all partitions of integer n into nbins bins.

Here, unlike in :function:`partition`, a “partition” is allowed to contain zeros. For example, (4,1,0) is a valid partition of 5 using 3 bins. This function fixes the number of bins and iterates over all possible length- nbins partitions while allowing zeros. This is equivalent to iterating over all usual partitions of length at most nbins and inserting zeros into all possible places for partitions of length less than nbins.

Parameters
  • n (int) – The number to partition.

  • nbins (int) – The fixed number of bins, equal to the length of all the partitions that are iterated over.

pygsti.tools.listtools._partition_into_slow(n, nbins)

Helper function for partition_into that performs the same task for a general number n.

pygsti.tools.listtools.incd_product(*args)

Like itertools.product but returns the first modified (incremented) index along with the product tuple itself.

Parameters

*args (iterables) – Any number of iterable things that we’re taking the product of.