def test_singular_values_random4x4(self): """Tests computing the singular values for random 4 x 4 matrices.""" for _ in range(10): matrix = np.random.randn(4, 4) matrix += matrix.conj().T qsve = QSVE(matrix) print("Matrix:") print(matrix) sigmas = qsve.singular_values_classical() print("Sigmas:", sigmas) n = 6 qsigmas = qsve.top_singular_values(nprecision_bits=n, init_state_row_and_col=None, shots=50000, ntop=4) print("QSigmas:", qsigmas) print("Max theory error:", qsve.max_error(n)) self.assertTrue( qsve.has_value_close_to_singular_values( qsigmas, qsve.max_error(n))) print("Success!\n\n")
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_singular_values_identity8(self): """Tests QSVE gets close to the correct singular values for the 8 x 8 identity matrix.""" qsve = QSVE(np.identity(8)) sigma = max(qsve.singular_values_classical()) for n in range(3, 7): qsigma = qsve.top_singular_values(nprecision_bits=n, init_state_row_and_col=None, shots=50000, ntop=3) self.assertTrue(abs(sigma - qsigma[0]) < qsve.max_error(n))
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))