def test_unitary_evals_matrix_singular_values_identity_2by2(self): """Tests that the eigenvalues of the unitary are the matrix singular values.""" # Dimension of system dim = 2 # Define the matrix and QSVE object matrix = np.identity(dim) qsve = QSVE(matrix) # Get the (normalized) singular values of the matrix sigmas = qsve.singular_values_classical(normalized=True) # Get the eigenvalues of the QSVE unitary evals, _ = np.linalg.eig(qsve.unitary()) # Make sure there are the correct number of eigenvalues self.assertEqual(len(evals), dim**2) qsigmas = [] for eval in evals: qsigma = qsve.unitary_eval_to_singular_value(eval) if qsigma not in qsigmas: qsigmas.append(qsigma) self.assertTrue(np.allclose(qsigmas, sigmas))
def test_unitary_evals_to_matrix_singular_vals(self): """Tests QSVE.unitary() by ensuring the eigenvalues of the unitary relate to the singular values of the input matrix in the expected way. """ for _ in range(100): matrix = np.random.randn(2, 2) matrix += matrix.conj().T qsve = QSVE(matrix) sigmas = qsve.singular_values_classical(normalized=True) umat = qsve.unitary() evals, _ = np.linalg.eig(umat) qsigmas = [] for eval in evals: qsigma = qsve.unitary_eval_to_singular_value(eval) qsigma = round(qsigma, 4) if qsigma not in qsigmas: qsigmas.append(qsigma) self.assertTrue( np.allclose(sorted(qsigmas), sorted(sigmas), atol=1e-3))