qrisp.QuantumArray.__sub__#

QuantumArray.__sub__(other: QuantumArray | QuantumVariable | 'ArrayLike') QuantumArray[source]#

Performs element-wise subtraction.

Parameters:
otherQuantumArray | QuantumVariable | ArrayLike

The array or scalar to be subtracted. If an array is provided, it must have the same shape as the original QuantumArray. If a scalar is provided, it will be subtracted from each element of the QuantumArray.

Returns:
QuantumArray

A new QuantumArray containing the element-wise difference. If a QuantumArray or QuantumVariable is provided, the qtype of the output will be determined by the qtypes of the two input objects to prevent overflow. If a classical scalar or numpy array is provided, the qtype of the output will be the same as the qtype of self. This may lead to overflow.

Raises:
TypeError

If the qtypes of self and other are incompatible for subtraction.

ValueError

If other is an array (QuantumArray or numpy/jax array) and its shape does not match the shape of self.

Examples

Subtracting two QuantumArrays of QuantumFloats element-wise:

>>> import numpy as np
>>> from qrisp import QuantumArray, QuantumFloat
>>> a_array = QuantumArray(QuantumFloat(2), shape=(2,2))
>>> b_array = QuantumArray(QuantumFloat(2), shape=(2,2))
>>> a_array[:] = np.eye(2)
>>> b_array[:] = np.eye(2)
>>> r_array = a_array - b_array
>>> print(r_array)
# {OutcomeArray([[0, 0], [0, 0]]): 1.0}

Subtracting a scalar from a QuantumArray of QuantumFloats: Overflow occurs here, which is why the output is 3 instead of -1.

>>> import numpy as np
>>> from qrisp import QuantumArray, QuantumFloat
>>> a_array = QuantumArray(QuantumFloat(2), shape=(2,2))
>>> a_array[:] = np.eye(2)
>>> r_array = a_array - 2
>>> print(r_array)
# {OutcomeArray([[3, 2], [2, 3]]): 1.0}

Subtracting a numpy array from a QuantumArray of QuantumFloats:

>>> import numpy as np
>>> from qrisp import QuantumArray, QuantumFloat
>>> a_array = QuantumArray(QuantumFloat(2), shape=(2,2))
>>> a_array[:] = np.eye(2)
>>> r_array = a_array - np.eye(2)
>>> print(r_array)
# {OutcomeArray([[0, 0], [0, 0]]): 1.0}