コード例 #1
0
    def test_shift_identity(self):
        """Tests shifting the identity matrix in QSVE."""
        # Matrix for QSVE
        matrix = np.identity(2)

        # Get a QSVE object
        qsve = QSVE(matrix)

        # Shift the matrix
        qsve.shift_matrix()

        # Get the correct shifted matrix
        correct = matrix + np.linalg.norm(matrix) * np.identity(2)

        # Make sure the QSVE shifted matrix is correct
        self.assertTrue(np.allclose(qsve.matrix, correct))
コード例 #2
0
    def test_shift(self):
        """Tests shifting an input matrix to QSVE."""
        # Matrix for QSVE
        matrix = np.array([[1, 2], [2, 4]], dtype=np.float64)

        # Compute the correct norm for testing the shift
        norm_correct = np.linalg.norm(matrix)

        # Get a QSVE object
        qsve = QSVE(matrix)

        # Get the BinaryTree's (one for each row of the matrix)
        tree1 = deepcopy(qsve.get_tree(0))
        tree2 = deepcopy(qsve.get_tree(1))

        # Shift the matrix
        qsve.shift_matrix()

        # Get the correct shifted matrix
        correct = matrix + norm_correct * np.identity(2)

        # Make sure the QSVE shifted matrix is correct
        self.assertTrue(np.allclose(qsve.matrix, correct))

        # Get the new BinaryTrees after shifting
        new_tree1 = qsve.get_tree(0)
        new_tree2 = qsve.get_tree(1)

        # Get the new correct tree values
        correct_new_tree1_values = np.array(
            [tree1._values[0] + norm_correct, tree1._values[1]])
        correct_new_tree2_values = np.array(
            [tree2._values[0], tree2._values[1] + norm_correct])

        # Make sure the BinaryTrees in the qsve object were updated correctly
        self.assertTrue(
            np.array_equal(new_tree1._values, correct_new_tree1_values))
        self.assertTrue(
            np.array_equal(new_tree2._values, correct_new_tree2_values))