Skip to main content
IBM Quantum Platform
This page is from a dev version of Qiskit SDK. This is a new interface that does not exist in the stable version.

ObservablesArray

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

GitHub

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) – Specify the copy kwarg of the object_array() function when initializing observables.
  • num_qubits (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) – If true, coerce entries into the internal format and validate them. If false, the input should already be an array-like.

Raises

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

Apply a transpiler layout to this ObservablesArray.

Parameters

  • layout (TranspileLayout |list[int] | None) – Either a 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 | None) – The number of qubits to expand the array to. If not provided then if layout is a 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 with the provided layout applied.

Raises

QiskitError

Return type

ObservablesArray

coerce

classmethod coerce(observables)

GitHub

Coerce ObservablesArrayLike into ObservableArray.

Parameters

observables (str |Pauli |SparsePauliOp |SparseObservable |Mapping[str |Pauli, float] | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool |int |float |complex |bytes | _NestedSequence[bool |int |float |complex |str |bytes]) – an object to be observables array.

Returns

A coerced observables array.

Return type

ObservablesArray

coerce_observable

classmethod coerce_observable(observable)

GitHub

Format an observable-like object into the internal format.

Parameters

observable (str |Pauli |SparsePauliOp |SparseObservable |Mapping[str |Pauli, float]) – The observable-like to format.

Returns

The coerced observable.

Raises

  • TypeError – If the input cannot be formatted because its type is not valid.
  • ValueError – If the input observable is invalid or empty.

Return type

SparseObservable

copy

copy()

GitHub

Return a deep copy of the array.

equivalent

equivalent(other, tol=1e-08)

GitHub

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

Parameters

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

ravel

ravel()

GitHub

Return a new array with one dimension.

The returned array has a shape given by (size, ), where the size is the size of this array.

Returns

A new flattened array.

Return type

ObservablesArray

reshape

reshape(*shape)

GitHub

Return a new array with a different shape.

This results in a new view of the same arrays.

Parameters

shape (int |Iterable[int]) – The shape of the returned array.

Returns

A new array.

Return type

ObservablesArray

slice

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

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

GitHub

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 if an integer is given for every array axis, otherwise, a new ObservablesArray.

sparse_observables_array

sparse_observables_array(copy=False)

GitHub

Convert to a numpy.ndarray with elements of type SparseObservable.

Parameters

copy (bool) – Whether to make a new array instance with new sparse observables as elements.

Returns

A numpy.ndarray with elements of type SparseObservable.

Return type

ndarray

tolist

tolist()

GitHub

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:

>>> 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 | str | Pauli | SparsePauliOp | SparseObservable | Mapping[str | Pauli, float]

validate

validate()

GitHub

Validate the consistency in observables array.

Was this page helpful?
Report a bug, typo, or request content on GitHub.