ObservablesArray
class qiskit.primitives.ObservablesArray(observables, num_qubits=None, copy=True, validate=True)
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
copykwarg of theobject_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)
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 andnum_qubitsare 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
layoutis aTranspileLayoutthe number of the transpiler output circuit qubits will be used by default. Iflayoutis 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
…
Return type
coerce
classmethod coerce(observables)
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
coerce_observable
classmethod coerce_observable(observable)
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
copy
equivalent
equivalent(other, tol=1e-08)
Compute whether the observable arrays are equal within a given tolerance.
Parameters
- other (ObservablesArray) – Another observables array to compare with.
- tol (float) – The tolerance to provide to
simplifyduring 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
ravel
ravel()
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
reshape
reshape(*shape)
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
slice
slice(args: int | tuple[int, ...]) → SparseObservable
slice(args: int | slice | None | tuple[Union[int, slice, NoneType], ...]) → ObservablesArray
Take a slice of the observables in this array.
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)
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
tolist
tolist()
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]