---
title: Partition (latest version)
description: API reference for samplomatic.partition.Partition in the latest version of samplomatic
source: https://quantum.cloud.ibm.com/docs/en/api/samplomatic/auto/partition-partition
---

# Partition

*class* `samplomatic.partition.Partition(num_elements_per_part: int, parts: Iterable[tuple[ElementT, ...]] | None = None)`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L30-L219)

Bases: [`Generic`](https://docs.python.org/3/library/typing.html#typing.Generic)\[`ElementT`]

A partition of a sequence of elements into equally-sized, non-overlapping subsets.

The [`Partition`](#samplomatic.partition.Partition "samplomatic.partition.Partition") class preserves the insertion order of the parts, as well as the order of the elements within the parts.

**Parameters**

- **num\_elements\_per\_part** – How many elements each part must have.
- **parts** – `None`, or the parts to initialize with.

**Methods Summary**

|                                                                                                                                  |                                                                                   |
| -------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| [`add`](#samplomatic.partition.Partition.add "samplomatic.partition.Partition.add")(part)                                        | Add a new part to this partition.                                                 |
| [`copy`](#samplomatic.partition.Partition.copy "samplomatic.partition.Partition.copy")()                                         | Copy this instance.                                                               |
| [`difference`](#samplomatic.partition.Partition.difference "samplomatic.partition.Partition.difference")(subtracted)             | Return the difference with respect to subtracted.                                 |
| [`from_elements`](#samplomatic.partition.Partition.from_elements "samplomatic.partition.Partition.from_elements")(elements)      | Construct a new instance in the special case that `num_elements_per_part` is one. |
| [`get_indices`](#samplomatic.partition.Partition.get_indices "samplomatic.partition.Partition.get_indices")(other)               | Get the indices of the parts of `other` in this partition.                        |
| [`intersection`](#samplomatic.partition.Partition.intersection "samplomatic.partition.Partition.intersection")(other\[, strict]) | Return a new partition that is the intersection with the other.                   |
| [`overlaps_with`](#samplomatic.partition.Partition.overlaps_with "samplomatic.partition.Partition.overlaps_with")(elements)      | Whether any iterable of elements overlaps with these elements.                    |
| [`restrict`](#samplomatic.partition.Partition.restrict "samplomatic.partition.Partition.restrict")(required)                     | Restrict to those parts with containment in the required set.                     |
| [`union`](#samplomatic.partition.Partition.union "samplomatic.partition.Partition.union")(\*all\_partitions)                     | Take the union of one or more partitions.                                         |

**Methods Documentation**

### add

`add(part: tuple[ElementT, ...])`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L150-L166)

Add a new part to this partition.

**Parameters**

**part** – The part to add.

**Raises**

- [**BuildError**](/docs/api/samplomatic/auto/exceptions-build-error "samplomatic.exceptions.BuildError") – If the part has the incorrect size.
- [**BuildError**](/docs/api/samplomatic/auto/exceptions-build-error "samplomatic.exceptions.BuildError") – If the part partially overlaps an existing part.

### copy

`copy() → T`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L66-L68)

Copy this instance.

### difference

`difference(subtracted: set[ElementT]) → Partition`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L96-L104)

Return the difference with respect to subtracted.

Order is maintained of those remaining parts.

### from\_elements

*classmethod* `from_elements(elements: Iterable[ElementT]) → T`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L168-L178)

Construct a new instance in the special case that `num_elements_per_part` is one.

**Parameters**

**elements** – The elements for each subsystem.

**Returns**

A new partition where each part is an element from `elements`.

### get\_indices

`get_indices(other: T) → ndarray[int64]`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L70-L84)

Get the indices of the parts of `other` in this partition.

**Parameters**

**other** – Some other partition whose elements are present in this partition.

**Returns**

A list of indices indicating the position of the parts of `other` in this partition.

### intersection

`intersection(other: Partition, strict: bool = False) → Partition`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L117-L148)

Return a new partition that is the intersection with the other.

The order of this partition is maintained, whereas the order of the other partition is irrelevant.

**Parameters**

- **other** – The other partition to take the intersection with.
- **strict** – Whether to error on partial overlaps.

**Raises**

- [**BuildError**](/docs/api/samplomatic/auto/exceptions-build-error "samplomatic.exceptions.BuildError") – If the two partitions have different numbers of elements per part.
- [**BuildError**](/docs/api/samplomatic/auto/exceptions-build-error "samplomatic.exceptions.BuildError") – If `strict` is `True` and `other` contains a part that partially overlaps with this partition.

### overlaps\_with

`overlaps_with(elements: Iterable[ElementT]) → bool`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L106-L115)

Whether any iterable of elements overlaps with these elements.

**Parameters**

**elements** – Any iterable of elements to check against.

**Returns**

Whether there is any overlap.

### restrict

`restrict(required: set[ElementT]) → Partition`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L86-L94)

Restrict to those parts with containment in the required set.

Order is maintained of those remaining parts.

### union

*classmethod* `union(*all_partitions: T) → T`

[GitHub](https://github.com/Qiskit/samplomatic/tree/main/samplomatic/partition.py#L180-L219)

Take the union of one or more partitions.

Order is maintained with earlier `all_partitions` taking precedence.

**Parameters**

**all\_partitions** – The partitions to take the union of.

**Returns**

The union of all the partitions.

**Raises**

- [**BuildError**](/docs/api/samplomatic/auto/exceptions-build-error "samplomatic.exceptions.BuildError") – If no partitions are given.
- [**BuildError**](/docs/api/samplomatic/auto/exceptions-build-error "samplomatic.exceptions.BuildError") – If the `num_elements_per_part` are incompatible.
- [**BuildError**](/docs/api/samplomatic/auto/exceptions-build-error "samplomatic.exceptions.BuildError") – If a partition partially overlaps another, or is reordered.
