qp.typing.AbstractArray

class AbstractArray(shape, dtype)[source]

Bases: object

An 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, -1 marks an axis whose size is unknown.

  • dtype (type) – the data type of the array. Can either be a builtin like float or int, or a numpy dtype like np.complex64.

>>> from pennylane.typing import AbstractArray
>>> AbstractArray((4, 2), float)
AbstractArray((4, 2), float64, weak_type=True)

Use -1 for an axis with unknown size:

>>> aa = AbstractArray((-1, 2), np.int32)
>>> isinstance(np.ones((4, 2), np.int32), aa)
True

Use Ellipsis when the rank itself is unknown:

>>> isinstance(np.ones((4, 2)), AbstractArray(..., float))
True

Elementwise arithmetic returns an AbstractArray with 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 -, +, and abs and their NumPy equivalents preserve shape and infer the output dtype, including real dtypes for the absolute value of complex inputs.

T

Transpose view of the array.

ndim

Number of dimensions.

size

Total number of elements.

shape

dtype

shape_fixed

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>

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 the AbstractArray.

Parameters:

val (Any) – input value to check for compatibility

Returns:

True if val is compatible, False otherwise

Return type:

bool

For shapes, the following conditions must be met to be considered compatible:

  • If the AbstractArray allows any rank, i.e., shape = ..., then the input value can have any shape.

  • If the AbstractArray has size -1 for any of the axes, the input value can have any size for those axes, but must have the same size as the AbstractArray for the rest of the axes.

  • If the AbstractArray has 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 the AbstractArray’s dtype are compatible. For example, np.bool and np.int32 can be safely cast to np.int64, but np.float32 cannot 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 the AbstractArray exactly.

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