예제 #1
0
    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))
예제 #2
0
    def test_unitary_conjugate_evals(self):
        """Tests that for each eigenvalue lambda of the unitary, lambda* is also an eigenvalue, as required."""
        for _ in range(100):
            matrix = np.random.randn(2, 2)
            matrix += matrix.conj().T
            qsve = QSVE(matrix)

            umat = qsve.unitary()

            evals, _ = np.linalg.eig(umat)

            for eval in evals:
                self.assertIn(np.conjugate(eval), evals)
예제 #3
0
    def test_unitary(self):
        """Tests for the unitary used for QPE."""
        for dim in [2, 4]:
            for _ in range(50):
                matrix = np.random.randn(dim, dim)
                matrix += matrix.conj().T

                qsve = QSVE(matrix)
                unitary = qsve.unitary()

                self.assertTrue(
                    np.allclose(unitary.conj().T @ unitary,
                                np.identity(dim**2)))
예제 #4
0
    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))