Skip to content

common

are_matrices_equivalent_up_to_global_phase(matrix_a, matrix_b)

Checks whether two matrices are equivalent up to a global phase.

Parameters:

Name Type Description Default
matrix_a NDArray[complex128]

first matrix.

required
matrix_b NDArray[complex128]

second matrix.

required

Returns:

Type Description
bool

Whether two matrices are equivalent up to a global phase.

Source code in opensquirrel\common.py
def are_matrices_equivalent_up_to_global_phase(
    matrix_a: NDArray[np.complex128], matrix_b: NDArray[np.complex128]
) -> bool:
    """Checks whether two matrices are equivalent up to a global phase.

    Args:
        matrix_a: first matrix.
        matrix_b: second matrix.

    Returns:
        Whether two matrices are equivalent up to a global phase.
    """
    first_non_zero = next(
        (i, j) for i in range(matrix_a.shape[0]) for j in range(matrix_a.shape[1]) if abs(matrix_a[i, j]) > ATOL
    )

    if abs(matrix_b[first_non_zero]) < ATOL:
        return False

    phase_difference = matrix_a[first_non_zero] / matrix_b[first_non_zero]

    return np.allclose(matrix_a, phase_difference * matrix_b)