def test_mul(self): new_tensor = self.polynomial_tensor_a * self.polynomial_tensor_b self.assertEqual(new_tensor, self.polynomial_tensor_axb) new_tensor_1 = self.polynomial_tensor_a * 2. new_tensor_2 = 2. * self.polynomial_tensor_a self.assertEqual( new_tensor_1, PolynomialTensor({ (): self.constant * 2., (1, 0): self.one_body_a * 2., (1, 1, 0, 0): self.two_body_a * 2. })) self.assertEqual( new_tensor_2, PolynomialTensor({ (): self.constant * 2., (1, 0): self.one_body_a * 2., (1, 1, 0, 0): self.two_body_a * 2. })) self.assertEqual(get_fermion_operator(new_tensor_1), get_fermion_operator(self.polynomial_tensor_a) * 2.) self.assertEqual(get_fermion_operator(new_tensor_2), get_fermion_operator(self.polynomial_tensor_a) * 2.)
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 test_iter_and_str(self): 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[0, 1] = 11.0 two_body[0, 1, 1, 0] = 22.0 polynomial_tensor = PolynomialTensor({ (): self.constant, (1, 0): one_body, (1, 1, 0, 0): two_body }) want_str = ('() 23.0\n((0, 1), (1, 0)) 11.0\n' '((0, 1), (1, 1), (1, 0), (0, 0)) 22.0\n') self.assertEqual(str(polynomial_tensor), want_str) self.assertEqual(polynomial_tensor.__repr__(), want_str)
def test_different_keys_mult(self): result = self.polynomial_tensor_a * self.polynomial_tensor_operand expected = PolynomialTensor({ (1, 0): numpy.multiply(self.one_body_a, self.one_body_operand) }) self.assertEqual(result, expected)
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_div(self): new_tensor = self.polynomial_tensor_a / 2. self.assertEqual( new_tensor, PolynomialTensor({ (): self.constant / 2., (1, 0): self.one_body_a / 2., (1, 1, 0, 0): self.two_body_a / 2. })) self.assertEqual(get_fermion_operator(new_tensor), get_fermion_operator(self.polynomial_tensor_a) / 2.)
def test_idiv(self): new_tensor = copy.deepcopy(self.polynomial_tensor_a) new_tensor /= 3. self.assertEqual( new_tensor, PolynomialTensor({ (): self.constant / 3., (1, 0): self.one_body_a / 3., (1, 1, 0, 0): self.two_body_a / 3. })) self.assertEqual(get_fermion_operator(new_tensor), get_fermion_operator(self.polynomial_tensor_a) / 3.)
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)
def test_different_keys_sub(self): result = self.polynomial_tensor_a - self.polynomial_tensor_operand expected = PolynomialTensor({ (): self.constant, (1, 0): numpy.subtract(self.one_body_a, self.one_body_operand), (1, 1, 0, 0): self.two_body_a, (0, 0, 1, 1): self.two_body_operand }) self.assertEqual(result, expected)
def test_mod(self): new_constant = 2.0 new_one_body = numpy.zeros_like(self.one_body_a) new_one_body[0, 1] = 2 new_two_body = numpy.zeros_like(self.two_body_a) new_two_body[0, 1, 0, 1] = 1 new_two_body[1, 1, 0, 0] = 2 new_tensor = PolynomialTensor({ (): new_constant, (1, 0): new_one_body, (1, 1, 0, 0): new_two_body }) assert new_tensor == (self.polynomial_tensor_a % 3)
def setUp(self): self.n_qubits = 2 self.constant = 23.0 one_body_a = numpy.zeros((self.n_qubits, self.n_qubits)) two_body_a = numpy.zeros( (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits)) one_body_a[0, 1] = 2 one_body_a[1, 0] = 3 two_body_a[0, 1, 0, 1] = 4 two_body_a[1, 1, 0, 0] = 5 self.one_body_a = one_body_a self.two_body_a = two_body_a self.polynomial_tensor_a = PolynomialTensor({ (): self.constant, (1, 0): one_body_a, (1, 1, 0, 0): two_body_a }) self.one_body_operand = numpy.zeros((self.n_qubits, self.n_qubits)) self.two_body_operand = numpy.zeros( (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits)) self.one_body_operand[0, 1] = 6 self.one_body_operand[1, 0] = 7 self.two_body_operand[0, 1, 0, 1] = 8 self.two_body_operand[1, 1, 0, 0] = 9 self.polynomial_tensor_operand = PolynomialTensor({ (1, 0): self.one_body_operand, (0, 0, 1, 1): self.two_body_operand }) self.polynomial_tensor_a_with_zeros = PolynomialTensor({ (): self.constant, (1, 0): one_body_a, (1, 1, 0, 0): two_body_a, (1, 1, 0, 0, 0, 0): numpy.zeros([self.n_qubits] * 6) }) one_body_na = numpy.zeros((self.n_qubits, self.n_qubits)) two_body_na = numpy.zeros( (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits)) one_body_na[0, 1] = -2 one_body_na[1, 0] = -3 two_body_na[0, 1, 0, 1] = -4 two_body_na[1, 1, 0, 0] = -5 self.polynomial_tensor_na = PolynomialTensor({ (): -self.constant, (1, 0): one_body_na, (1, 1, 0, 0): two_body_na }) one_body_b = numpy.zeros((self.n_qubits, self.n_qubits)) two_body_b = numpy.zeros( (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits)) one_body_b[0, 1] = 1 one_body_b[1, 0] = 2 two_body_b[0, 1, 0, 1] = 3 two_body_b[1, 0, 0, 1] = 4 self.polynomial_tensor_b = PolynomialTensor({ (): self.constant, (1, 0): one_body_b, (1, 1, 0, 0): two_body_b }) one_body_ab = numpy.zeros((self.n_qubits, self.n_qubits)) two_body_ab = numpy.zeros( (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits)) one_body_ab[0, 1] = 3 one_body_ab[1, 0] = 5 two_body_ab[0, 1, 0, 1] = 7 two_body_ab[1, 0, 0, 1] = 4 two_body_ab[1, 1, 0, 0] = 5 self.polynomial_tensor_ab = PolynomialTensor({ (): 2.0 * self.constant, (1, 0): one_body_ab, (1, 1, 0, 0): two_body_ab }) constant_axb = self.constant * self.constant one_body_axb = numpy.zeros((self.n_qubits, self.n_qubits)) two_body_axb = numpy.zeros( (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits)) one_body_axb[0, 1] = 2 one_body_axb[1, 0] = 6 two_body_axb[0, 1, 0, 1] = 12 self.polynomial_tensor_axb = PolynomialTensor({ (): constant_axb, (1, 0): one_body_axb, (1, 1, 0, 0): two_body_axb }) self.n_qubits_plus_one = self.n_qubits + 1 one_body_c = numpy.zeros( (self.n_qubits_plus_one, self.n_qubits_plus_one)) two_body_c = numpy.zeros( (self.n_qubits_plus_one, self.n_qubits_plus_one, self.n_qubits_plus_one, self.n_qubits_plus_one)) one_body_c[0, 1] = 1 one_body_c[1, 0] = 2 two_body_c[0, 1, 0, 1] = 3 two_body_c[1, 0, 0, 1] = 4 self.polynomial_tensor_c = PolynomialTensor({ (): self.constant, (1, 0): one_body_c, (1, 1, 0, 0): two_body_c }) one_body_hole = numpy.zeros((self.n_qubits, self.n_qubits)) two_body_hole = numpy.zeros( (self.n_qubits, self.n_qubits, self.n_qubits, self.n_qubits)) one_body_hole[0, 1] = 2 one_body_hole[1, 0] = 3 two_body_hole[0, 1, 0, 1] = 4 two_body_hole[1, 1, 0, 0] = 5 self.polynomial_tensor_hole = PolynomialTensor({ (): self.constant, (0, 1): one_body_hole, (0, 0, 1, 1): two_body_hole }) 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)) one_body_spinful[0, 1] = 2 one_body_spinful[1, 0] = 3 one_body_spinful[2, 3] = 6 one_body_spinful[3, 2] = 7 two_body_spinful[0, 1, 0, 1] = 4 two_body_spinful[1, 1, 0, 0] = 5 two_body_spinful[2, 1, 2, 3] = 8 two_body_spinful[3, 3, 2, 2] = 9 self.polynomial_tensor_spinful = PolynomialTensor({ (): self.constant, (1, 0): one_body_spinful, (1, 1, 0, 0): two_body_spinful })