qrisp.QuantumArray.__matmul__#

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

Performs matrix multiplication.

Parameters:
otherQuantumArray | ArrayLike

The array to be multiplied. Can be either a QuantumArray or a classical array (e.g. numpy array) of compatible shape. If self is a QuantumArray of QuantumModulus, other must be a classical array of integers. If self is a QuantumArray of QuantumFloat, other can be either a QuantumArray of QuantumFloat or a classical array of integers or floats.

Returns:
QuantumArray
A new QuantumArray containing the multiplication result.

The qtype of the output array is the same as the qtype of self.

Raises:
ValueError

If the shapes of self and other are incompatible for matrix multiplication.

TypeError
If the types of self and other are incompatible for matrix multiplication:
  • If self is not a QuantumArray of QuantumFloat or QuantumModulus, matrix multiplication is not supported.

  • If other is a QuantumArray but not of QuantumFloat or QuantumModulus, matrix multiplication is not supported.

NotImplementedError
If matrix multiplication between the given types is not supported:
  • If self is a QuantumArray of QuantumModulus, matrix multiplication with another QuantumArray is not supported. Other must be a classical array of integers.

  • If self is a QuantumArray of QuantumFloat, matrix multiplication is not supported in tracing mode.

Examples

Multiplying two QuantumArrays of QuantumFloats.

>>> 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([[1, 0], [0, 1]]): 1.0}

Multiplying a QuantumArray of QuantumFloats with a classical numpy array.

>>> import numpy as np
>>> from qrisp import QuantumArray, QuantumFloat
>>> a_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([[1, 0], [0, 1]]): 1.0}

Multiplying a QuantumArray of QuantumModulus with a classical numpy array.

>>> import numpy as np
>>> from qrisp import QuantumArray, QuantumModulus
>>> a_array = QuantumArray(QuantumModulus(7), shape=(2,2))
>>> a_array[:] = np.array([[1, 2], [3, 4]])
>>> b_array = np.array([[1, 2], [3, 4]])
>>> r_array = a_array @ b_array
>>> print(r_array)
# {OutcomeArray([[0, 3], [1, 1]]): 1.0}