def test_rotate_basis_identical(self):
        rotation_matrix_identical = numpy.zeros((self.n_qubits, self.n_qubits))
        rotation_matrix_identical[0, 0] = 1
        rotation_matrix_identical[1, 1] = 1

        one_body = numpy.zeros((self.n_qubits, self.n_qubits))
        two_body = numpy.zeros(
            (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits))
        one_body_spinful = numpy.zeros((2 * self.n_qubits, 2 * self.n_qubits))
        two_body_spinful = numpy.zeros((2 * self.n_qubits, 2 * self.n_qubits,
                                        2 * self.n_qubits, 2 * self.n_qubits))
        i = 0
        j = 0
        for p in range(self.n_qubits):
            for q in range(self.n_qubits):
                one_body[p, q] = i
                one_body_spinful[p, q] = i
                one_body_spinful[p + self.n_qubits, q + self.n_qubits] = i
                i = i + 1
                for r in range(self.n_qubits):
                    for s in range(self.n_qubits):
                        two_body[p, q, r, s] = j
                        two_body_spinful[p, q, r, s] = j
                        two_body_spinful[p + self.n_qubits, q +
                                         self.n_qubits, r + self.n_qubits, s +
                                         self.n_qubits] = j
                        j = j + 1
        polynomial_tensor = PolynomialTensor({
            (): self.constant,
            (1, 0): one_body,
            (1, 1, 0, 0): two_body
        })
        want_polynomial_tensor = PolynomialTensor({
            (): self.constant,
            (1, 0): one_body,
            (1, 1, 0, 0): two_body
        })
        polynomial_tensor_spinful = PolynomialTensor({
            ():
            self.constant,
            (1, 0):
            one_body_spinful,
            (1, 1, 0, 0):
            two_body_spinful
        })
        want_polynomial_tensor_spinful = PolynomialTensor({
            ():
            self.constant,
            (1, 0):
            one_body_spinful,
            (1, 1, 0, 0):
            two_body_spinful
        })

        polynomial_tensor.rotate_basis(rotation_matrix_identical)
        polynomial_tensor_spinful.rotate_basis(rotation_matrix_identical)
        self.assertEqual(polynomial_tensor, want_polynomial_tensor)
        self.assertEqual(polynomial_tensor_spinful,
                         want_polynomial_tensor_spinful)
    def do_rotate_basis_high_order(self, order):
        key = (1,) * (order // 2) + (0,) * ((order + 1) // 2)
        shape = (1,) * order
        num = numpy.random.rand()
        rotation = numpy.exp(numpy.random.rand() * numpy.pi * 2j)

        polynomial_tensor = PolynomialTensor({key: numpy.zeros(shape) + num})

        # If order is odd, there are one more 0 than 1 in key
        if order % 2 == 1:
            num *= rotation
        want_polynomial_tensor = PolynomialTensor(
            {key: numpy.zeros(shape) + num})

        polynomial_tensor.rotate_basis(numpy.array([[rotation]]))

        return polynomial_tensor, want_polynomial_tensor
    def test_rotate_basis_reverse(self):
        rotation_matrix_reverse = numpy.zeros((self.n_qubits, self.n_qubits))
        rotation_matrix_reverse[0, 1] = 1
        rotation_matrix_reverse[1, 0] = 1

        one_body = numpy.zeros((self.n_qubits, self.n_qubits))
        two_body = numpy.zeros(
            (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits))
        one_body_reverse = numpy.zeros((self.n_qubits, self.n_qubits))
        two_body_reverse = numpy.zeros(
            (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits))
        i = 0
        j = 0
        i_reverse = pow(self.n_qubits, 2) - 1
        j_reverse = pow(self.n_qubits, 4) - 1
        for p in range(self.n_qubits):
            for q in range(self.n_qubits):
                one_body[p, q] = i
                i = i + 1
                one_body_reverse[p, q] = i_reverse
                i_reverse = i_reverse - 1
                for r in range(self.n_qubits):
                    for s in range(self.n_qubits):
                        two_body[p, q, r, s] = j
                        j = j + 1
                        two_body_reverse[p, q, r, s] = j_reverse
                        j_reverse = j_reverse - 1
        polynomial_tensor = PolynomialTensor({
            (): self.constant,
            (1, 0): one_body,
            (1, 1, 0, 0): two_body
        })
        want_polynomial_tensor = PolynomialTensor({
            (): self.constant,
            (1, 0): one_body_reverse,
            (1, 1, 0, 0): two_body_reverse
        })
        polynomial_tensor.rotate_basis(rotation_matrix_reverse)
        self.assertEqual(polynomial_tensor, want_polynomial_tensor)