q_matmul(q_array_0, q_array_1, output_array=None, res_bit_shape='eq', phase_tolerant=False)[source]#

Matrix multiplication for QuantumArrays.


The first factor of the matrix multiplication.


The second factor of the matrix multiplication.

output_arrayQuantumArray, optional

The QuantumArray to store the results in. By default, a new QuanumArray is created.

res_bit_shapestr or QuantumFloat, optional

Specification of the dimension of the output bitshape of the output QuantumArray. Possible are “eq”, which will take the bitshape equal two the first factor, “safe” which automatically determines the bitshape such that there can be no overflow, or a QuantumFloat which has the desired bitshape. The default is “eq”.

phase_tolerantbool, optional

If set to True, the required gate count is reduced but each constellation of computational basis states of the inputs will introduce a different phase. This is helpful when it’s clear that this function will be at some point uncomputed, resulting in the cancelation of these phases. The default is False.


The result of the matrix multiplication.


Tried to perform matrix multiplication with differing contraction index size.


We multiply a QuantumArray with a multiply of the identity matrix (np.eye):

>>> import numpy as np
>>> from qrisp import QuantumFloat, QuantumArray, q_matmul
>>> qf = QuantumFloat(4,0, signed = False)
>>> q_arr_0 = QuantumArray(qf, shape = (2,2))
>>> q_arr_1 = QuantumArray(qf, shape = (2,2))
>>> q_arr_0[:] = [[1,2],[3,4]]
>>> q_arr_1[:] = 2*np.eye(2)
>>> res = q_matmul(q_arr_0, q_arr_1)
>>> print(res)
{OutcomeArray([[2, 4],
               [6, 8]]): 1.0}