in Standard/src/Synthesis/MatrixUtils.cs [15:37]
public static bool IsMatrixUnitary(Complex[,] matrix, double tol = 1e-10)
{
int n = matrix.GetLength(0);
if (matrix.GetLength(1) != n) return false; // Unitary matrix must be square.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Complex dotProduct = 0.0;
for (int k = 0; k < n; k++)
{
dotProduct += matrix[i, k] * Complex.Conjugate(matrix[j, k]);
}
Complex expectedDotProduct = (i == j) ? 1.0 : 0.0;
if ((dotProduct - expectedDotProduct).Magnitude > tol)
{
return false;
}
}
}
return true;
}