---
title: ObservablesArray (latest version)
description: API reference for qiskit.primitives.ObservablesArray in the latest version of qiskit
source: https://eu-de.quantum.cloud.ibm.com/docs/en/api/qiskit/qiskit.primitives.ObservablesArray
---

# ObservablesArray

*class* `qiskit.primitives.ObservablesArray(observables, num_qubits=None, copy=True, validate=True)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L50-L382)

Bases: `ShapedMixin`

An ND-array of Hermitian observables for an `Estimator` primitive.

Initialize an observables array.

**Parameters**

- **observables** (*ObservablesArrayLike*) – An array-like of basis observable compatible objects.
- **copy** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – Specify the `copy` kwarg of the `object_array()` function when initializing observables.
- **num\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – The number of qubits of the observables. If not specified, the number of qubits will be inferred from the observables. If specified, then the specified number of qubits must match the number of qubits in the observables.
- **validate** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If true, coerce entries into the internal format and validate them. If false, the input should already be an array-like.

**Raises**

[**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If `validate=True` and the input observables array is not valid.

## Attributes

### ndim

### num\_qubits

The number of qubits each observable acts on.

### shape

### size

## Methods

### apply\_layout

`apply_layout(layout, num_qubits=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L344-L373)

Apply a transpiler layout to this [`ObservablesArray`](#qiskit.primitives.ObservablesArray "qiskit.primitives.ObservablesArray").

**Parameters**

- **layout** ([*TranspileLayout*](/docs/api/qiskit/qiskit.transpiler.TranspileLayout "qiskit.transpiler.TranspileLayout")  *|*[*list*](https://docs.python.org/3/library/stdtypes.html#list)*\[*[*int*](https://docs.python.org/3/library/functions.html#int)*] | None*) – Either a [`TranspileLayout`](/docs/api/qiskit/qiskit.transpiler.TranspileLayout "qiskit.transpiler.TranspileLayout"), a list of integers or None. If both layout and `num_qubits` are none, a deep copy of the array is returned.
- **num\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – The number of qubits to expand the array to. If not provided then if `layout` is a [`TranspileLayout`](/docs/api/qiskit/qiskit.transpiler.TranspileLayout "qiskit.transpiler.TranspileLayout") the number of the transpiler output circuit qubits will be used by default. If `layout` is a list of integers the permutation specified will be applied without any expansion. If layout is None, the array will be expanded to the given number of qubits.

**Returns**

A new [`ObservablesArray`](#qiskit.primitives.ObservablesArray "qiskit.primitives.ObservablesArray") with the provided layout applied.

**Raises**

[**QiskitError**](/docs/api/qiskit/exceptions#qiskit.exceptions.QiskitError "qiskit.exceptions.QiskitError") –

…

**Return type**

[ObservablesArray](#qiskit.primitives.ObservablesArray "qiskit.primitives.ObservablesArray")

### coerce

*classmethod* `coerce(observables)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L305-L317)

Coerce ObservablesArrayLike into ObservableArray.

**Parameters**

**observables** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)  *|*[*Pauli*](/docs/api/qiskit/qiskit.quantum_info.Pauli "qiskit.quantum_info.operators.symplectic.pauli.Pauli")  *|*[*SparsePauliOp*](/docs/api/qiskit/qiskit.quantum_info.SparsePauliOp "qiskit.quantum_info.operators.symplectic.sparse_pauli_op.SparsePauliOp")  *|*[*SparseObservable*](/docs/api/qiskit/qiskit.quantum_info.SparseObservable "qiskit.quantum_info.SparseObservable")  *|*[*Mapping*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)*\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)  *|*[*Pauli*](/docs/api/qiskit/qiskit.quantum_info.Pauli "qiskit.quantum_info.operators.symplectic.pauli.Pauli")*,* [*float*](https://docs.python.org/3/library/functions.html#float)*] | ArrayLike*) – an object to be observables array.

**Returns**

A coerced observables array.

**Return type**

[*ObservablesArray*](#qiskit.primitives.ObservablesArray "qiskit.primitives.containers.observables_array.ObservablesArray")

### coerce\_observable

*classmethod* `coerce_observable(observable)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L246-L303)

Format an observable-like object into the internal format.

**Parameters**

**observable** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)  *|*[*Pauli*](/docs/api/qiskit/qiskit.quantum_info.Pauli "qiskit.quantum_info.operators.symplectic.pauli.Pauli")  *|*[*SparsePauliOp*](/docs/api/qiskit/qiskit.quantum_info.SparsePauliOp "qiskit.quantum_info.operators.symplectic.sparse_pauli_op.SparsePauliOp")  *|*[*SparseObservable*](/docs/api/qiskit/qiskit.quantum_info.SparseObservable "qiskit.quantum_info.SparseObservable")  *|*[*Mapping*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)*\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)  *|*[*Pauli*](/docs/api/qiskit/qiskit.quantum_info.Pauli "qiskit.quantum_info.operators.symplectic.pauli.Pauli")*,* [*float*](https://docs.python.org/3/library/functions.html#float)*]*) – The observable-like to format.

**Returns**

The coerced observable.

**Raises**

- [**TypeError**](https://docs.python.org/3/library/exceptions.html#TypeError) – If the input cannot be formatted because its type is not valid.
- [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If the input observable is invalid or empty.

**Return type**

[*SparseObservable*](/docs/api/qiskit/qiskit.quantum_info.SparseObservable "qiskit.quantum_info.SparseObservable")

### copy

`copy()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L340-L342)

Return a deep copy of the array.

### equivalent

`equivalent(other, tol=1e-08)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L319-L338)

Compute whether the observable arrays are equal within a given tolerance.

**Parameters**

- **other** ([*ObservablesArray*](#qiskit.primitives.ObservablesArray "qiskit.primitives.containers.observables_array.ObservablesArray")) – Another observables array to compare with.
- **tol** ([*float*](https://docs.python.org/3/library/functions.html#float)) – The tolerance to provide to [`simplify`](/docs/api/qiskit/qiskit.quantum_info.SparseObservable#simplify "qiskit.quantum_info.SparseObservable.simplify") during checking.

**Returns**

Whether the two observables arrays have the same shape and number of qubits, and if so, whether they are equal within tolerance.

**Return type**

[bool](https://docs.python.org/3/library/functions.html#bool)

### ravel

`ravel()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L230-L239)

Return a new array with one dimension.

The returned array has a [`shape`](#qiskit.primitives.ObservablesArray.shape "qiskit.primitives.ObservablesArray.shape") given by `(size, )`, where the size is the [`size`](#qiskit.primitives.ObservablesArray.size "qiskit.primitives.ObservablesArray.size") of this array.

**Returns**

A new flattened array.

**Return type**

[*ObservablesArray*](#qiskit.primitives.ObservablesArray "qiskit.primitives.containers.observables_array.ObservablesArray")

### reshape

`reshape(*shape)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L216-L228)

Return a new array with a different shape.

This results in a new view of the same arrays.

**Parameters**

**shape** ([*int*](https://docs.python.org/3/library/functions.html#int)  *|*[*Iterable*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterable)*\[*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – The shape of the returned array.

**Returns**

A new array.

**Return type**

[*ObservablesArray*](#qiskit.primitives.ObservablesArray "qiskit.primitives.containers.observables_array.ObservablesArray")

### slice

`slice(args: int | tuple[int, ...]) → SparseObservable`

`slice(args: int | slice | None | tuple[int | slice | None, ...]) → ObservablesArray`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L199-L214)

Take a slice of the observables in this array.

> **Note**
>
> This method does not copy observables; modifying the returned observables will affect this instance.

**Returns**

A single [`SparseObservable`](/docs/api/qiskit/qiskit.quantum_info.SparseObservable "qiskit.quantum_info.SparseObservable") if an integer is given for every array axis, otherwise, a new [`ObservablesArray`](#qiskit.primitives.ObservablesArray "qiskit.primitives.ObservablesArray").

### sparse\_observables\_array

`sparse_observables_array(copy=False)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L168-L178)

Convert to a [`numpy.ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray) with elements of type [`SparseObservable`](/docs/api/qiskit/qiskit.quantum_info.SparseObservable "qiskit.quantum_info.SparseObservable").

**Parameters**

**copy** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – Whether to make a new array instance with new sparse observables as elements.

**Returns**

A [`numpy.ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray) with elements of type [`SparseObservable`](/docs/api/qiskit/qiskit.quantum_info.SparseObservable "qiskit.quantum_info.SparseObservable").

**Return type**

[*ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)

### tolist

`tolist()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L133-L152)

Convert to a nested list.

Similar to Numpy’s `tolist` method, the level of nesting depends on the dimension of the observables array. In the case of dimension 0 the method returns a single observable (`dict` in the case of a weighted sum of Paulis) instead of a list.

**Examples::**

Return values for a one-element list vs one element:

```python
>>> from qiskit.primitives.containers.observables_array import ObservablesArray
>>> oa = ObservablesArray.coerce(["Z"])
>>> print(type(oa.tolist()))
<class 'list'>
>>> oa = ObservablesArray.coerce("Z")
>>> print(type(oa.tolist()))
<class 'dict'>
```

**Return type**

[list](https://docs.python.org/3/library/stdtypes.html#list) | [str](https://docs.python.org/3/library/stdtypes.html#str) | [*Pauli*](/docs/api/qiskit/qiskit.quantum_info.Pauli "qiskit.quantum_info.operators.symplectic.pauli.Pauli") | [*SparsePauliOp*](/docs/api/qiskit/qiskit.quantum_info.SparsePauliOp "qiskit.quantum_info.operators.symplectic.sparse_pauli_op.SparsePauliOp") | [*SparseObservable*](/docs/api/qiskit/qiskit.quantum_info.SparseObservable "qiskit.quantum_info.SparseObservable") | [*Mapping*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)\[[str](https://docs.python.org/3/library/stdtypes.html#str) | [*Pauli*](/docs/api/qiskit/qiskit.quantum_info.Pauli "qiskit.quantum_info.operators.symplectic.pauli.Pauli"), [float](https://docs.python.org/3/library/functions.html#float)]

### validate

`validate()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/primitives/containers/observables_array.py#L375-L382)

Validate the consistency in observables array.
