qp.typing.AbstractArray¶
- class AbstractArray(shape, dtype)[source]¶
Bases:
objectAn abstract representation of an array that contains the shape and dtype attributes necessary for resource calculations.
- Parameters:
shape (tuple[int, ...] | types.EllipsisType) – the dimensions of the array.
()corresponds to a scalar.Ellipsis(...) means the number of axes is unknown. Within a shape tuple,-1marks an axis whose size is unknown.dtype (type) – the data type of the array. Can either be a
builtinlikefloatorint, or a numpy dtype likenp.complex64.
>>> from pennylane.typing import AbstractArray >>> AbstractArray((4, 2), float) AbstractArray((4, 2), float64, weak_type=True)
Use
-1for an axis with unknown size:>>> aa = AbstractArray((-1, 2), np.int32) >>> isinstance(np.ones((4, 2), np.int32), aa) True
Use
Ellipsiswhen the rank itself is unknown:>>> isinstance(np.ones((4, 2)), AbstractArray(..., float)) True
Elementwise arithmetic returns an
AbstractArraywith broadcast shape and NumPy-promoted dtype. Builtin numeric dtypes participate as weak types, and the result remains weak when both operands are weak. Unknown dimensions are accepted unless known axes conflict; compatibility may still depend on their concrete sizes. An unknown rank produces an unknown result rank. The corresponding NumPy arithmetic ufuncs are supported for ordinary calls without keyword options; reductions and other ufunc methods are not supported. Unary-,+, andabsand their NumPy equivalents preserve shape and infer the output dtype, including real dtypes for the absolute value of complex inputs.Attributes
Transpose view of the array.
Number of dimensions.
Total number of elements.
- T¶
Transpose view of the array.
- ndim¶
Number of dimensions.
- size¶
Total number of elements.
- shape = <dataclasses._MISSING_TYPE object>¶
- dtype = <dataclasses._MISSING_TYPE object>¶
- shape_fixed = <dataclasses._MISSING_TYPE object>¶
Methods
is_compatible_with(val)Check whether an input value is compatible with an
AbstractArray.- is_compatible_with(val)[source]¶
Check whether an input value is compatible with an
AbstractArray. A value is considered to be compatible if it has a shape and dtype that can be safely cast to the shape and dtype of theAbstractArray.- Parameters:
val (Any) – input value to check for compatibility
- Returns:
Trueifvalis compatible,Falseotherwise- Return type:
bool
For shapes, the following conditions must be met to be considered compatible:
If the
AbstractArrayallows any rank, i.e.,shape = ..., then the input value can have any shape.If the
AbstractArrayhas size-1for any of the axes, the input value can have any size for those axes, but must have the same size as theAbstractArrayfor the rest of the axes.If the
AbstractArrayhas a fixed shape, then the shape of the input value must match exactly.
For dtypes, the following conditions must be met to be considered compatible:
If the
AbstractArray’s dtype is weak, i.e., it was initialized with a Python builtin number type (int,float, etc.), then any dtypes that can be safely cast to theAbstractArray’s dtype are compatible. For example,np.boolandnp.int32can be safely cast tonp.int64, butnp.float32cannot be.If the
AbstractArray’s dtype is not weak, i.e., it was initialized with a dtype with a specific precision, then the dtype of the input value must match the dtype of theAbstractArrayexactly.
If all the above conditions are met, an input value will be considered compatible.
Example
>>> aa = AbstractArray((-1, 2), int) >>> aa.is_compatible_with(np.ones((5, 2), dtype=np.int32)) True >>> aa.is_compatible_with(np.ones((5, 2), dtype=np.bool)) True >>> aa.is_compatible_with(np.ones((5, 3), dtype=np.int32)) False >>> aa.is_compatible_with(np.ones((5, 2), dtype=np.float64)) False >>> aa = AbstractArray((3, 2), np.int32) >>> aa.is_compatible_with(np.ones((3, 2), dtype=np.int32)) True >>> aa.is_compatible_with(np.ones((5, 2), dtype=np.int32)) False >>> aa.is_compatible_with(np.ones((3, 2), dtype=np.int16)) False