qrisp.QuantumArray.__add__#
- QuantumArray.__add__(other: QuantumArray | QuantumVariable | 'ArrayLike') QuantumArray[source]#
Performs element-wise addition.
- Parameters:
- otherQuantumArray | QuantumVariable | ArrayLike
The array or scalar to be added. If an array is provided, it must have the same shape as the original QuantumArray. If a scalar is provided, it will be added to each element of the QuantumArray.
- Returns:
- QuantumArray
A new QuantumArray containing the element-wise sum. If a QuantumArray or QuantumVariable is provided, the
qtypeof 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, theqtypeof the output will be the same as theqtypeof self. This may lead to overflow.
- Raises:
- TypeError
If the qtypes of self and other are incompatible for addition.
- ValueError
If other is an array (QuantumArray or numpy/jax array) and its shape does not match the shape of self.
Examples
Adding 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([[2, 0], [0, 2]]): 1.0}
Adding a scalar to 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 + 2 >>> print(r_array) # {OutcomeArray([[3, 2], [2, 3]]): 1.0}
Adding a numpy array to 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([[2, 0], [0, 2]]): 1.0}