Skip to content

mat_inner_product

qoptcraft.math.mat_inner_product

Matrix inner product and norm.

hs_inner_product(matrix_1, matrix_2)

Hilbert-Schmidt product of two matrices.

Parameters:

Name Type Description Default
matrix_1 Matrix

first matrix

required
matrix_2 Matrix

second matrix

required

Raises:

Type Description
ValueError

Matrix type is not array or scipy sparse.

Returns:

Name Type Description
Number Number

scalar value of the product.

Source code in qoptcraft/math/mat_inner_product.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def hs_inner_product(matrix_1: Matrix, matrix_2: Matrix) -> Number:
    """Hilbert-Schmidt product of two matrices.

    Args:
        matrix_1 (Matrix): first matrix
        matrix_2 (Matrix): second matrix

    Raises:
        ValueError: Matrix type is not array or scipy sparse.

    Returns:
        Number: scalar value of the product.
    """
    result = (matrix_1.conj().T @ matrix_2).trace()
    # assert not np.isnan(result), "Matrix inner product is not a number."
    return result

hs_norm(matrix)

Hilbert-Schmidt norm of a matrix

Parameters:

Name Type Description Default
matrix Matrix

a matrix.

required

Returns:

Name Type Description
Number float

the norm.

Source code in qoptcraft/math/mat_inner_product.py
50
51
52
53
54
55
56
57
58
59
def hs_norm(matrix: Matrix) -> float:
    """Hilbert-Schmidt norm of a matrix

    Args:
        matrix (Matrix): a matrix.

    Returns:
        Number: the norm.
    """
    return np.sqrt(np.real(hs_inner_product(matrix, matrix)))

hs_scalar_product(matrix_1, matrix_2)

Hilbert-Schmidt product of two matrices.

Parameters:

Name Type Description Default
matrix_1 Matrix

first matrix

required
matrix_2 Matrix

second matrix

required

Raises:

Type Description
ValueError

Matrix type is not array or scipy sparse.

Returns:

Name Type Description
Number Number

scalar value of the product.

Source code in qoptcraft/math/mat_inner_product.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def hs_scalar_product(matrix_1: Matrix, matrix_2: Matrix) -> Number:
    """Hilbert-Schmidt product of two matrices.

    Args:
        matrix_1 (Matrix): first matrix
        matrix_2 (Matrix): second matrix

    Raises:
        ValueError: Matrix type is not array or scipy sparse.

    Returns:
        Number: scalar value of the product.
    """
    result = 0.5 * (matrix_1.conj().T @ matrix_2 + matrix_2.conj().T @ matrix_1).trace()
    # assert not np.isnan(result), "Matrix inner product is not a number."
    return result