|  | 
| 12 | 12 | """Tests for operator_utils.""" | 
| 13 | 13 | 
 | 
| 14 | 14 | import os | 
|  | 15 | +import json | 
| 15 | 16 | 
 | 
| 16 | 17 | import itertools | 
| 17 | 18 | 
 | 
| @@ -473,13 +474,20 @@ def setUp(self): | 
| 473 | 474 |         with open(os.path.join(DATA_DIRECTORY, self.bad_operator_filename), 'w') as fid: | 
| 474 | 475 |             fid.write(bad_op) | 
| 475 | 476 | 
 | 
|  | 477 | +        self.bad_type_filename = 'bad_type_operator.data' | 
|  | 478 | +        with open(os.path.join(DATA_DIRECTORY, self.bad_type_filename), 'w') as f: | 
|  | 479 | +            json.dump(['InvalidOperatorType', {}], f) | 
|  | 480 | + | 
| 476 | 481 |     def tearDown(self): | 
| 477 | 482 |         file_path = os.path.join(DATA_DIRECTORY, self.file_name + '.data') | 
| 478 | 483 |         if os.path.isfile(file_path): | 
| 479 | 484 |             os.remove(file_path) | 
| 480 | 485 |         file_path = os.path.join(DATA_DIRECTORY, self.bad_operator_filename) | 
| 481 | 486 |         if os.path.isfile(file_path): | 
| 482 | 487 |             os.remove(file_path) | 
|  | 488 | +        file_path = os.path.join(DATA_DIRECTORY, self.bad_type_filename) | 
|  | 489 | +        if os.path.isfile(file_path): | 
|  | 490 | +            os.remove(file_path) | 
| 483 | 491 | 
 | 
| 484 | 492 |     def test_save_sympy_plaintext(self): | 
| 485 | 493 |         operator = FermionOperator('1^', sympy.Symbol('x')) | 
| @@ -594,6 +602,46 @@ def test_save_bad_type(self): | 
| 594 | 602 |         with self.assertRaises(TypeError): | 
| 595 | 603 |             save_operator('ping', 'somewhere') | 
| 596 | 604 | 
 | 
|  | 605 | +    def test_save_and_load_complex_json(self): | 
|  | 606 | +        fermion_op = FermionOperator('1^ 2', 1 + 2j) | 
|  | 607 | +        boson_op = BosonOperator('1^ 2', 1 + 2j) | 
|  | 608 | +        qubit_op = QubitOperator('X1 Y2', 1 + 2j) | 
|  | 609 | +        quad_op = QuadOperator('q1 p2', 1 + 2j) | 
|  | 610 | + | 
|  | 611 | +        save_operator(fermion_op, self.file_name) | 
|  | 612 | +        loaded_op = load_operator(self.file_name) | 
|  | 613 | +        self.assertEqual(fermion_op, loaded_op) | 
|  | 614 | + | 
|  | 615 | +        save_operator(boson_op, self.file_name, allow_overwrite=True) | 
|  | 616 | +        loaded_op = load_operator(self.file_name) | 
|  | 617 | +        self.assertEqual(boson_op, loaded_op) | 
|  | 618 | + | 
|  | 619 | +        save_operator(qubit_op, self.file_name, allow_overwrite=True) | 
|  | 620 | +        loaded_op = load_operator(self.file_name) | 
|  | 621 | +        self.assertEqual(qubit_op, loaded_op) | 
|  | 622 | + | 
|  | 623 | +        save_operator(quad_op, self.file_name, allow_overwrite=True) | 
|  | 624 | +        loaded_op = load_operator(self.file_name) | 
|  | 625 | +        self.assertEqual(quad_op, loaded_op) | 
|  | 626 | + | 
|  | 627 | +    def test_saved_json_content(self): | 
|  | 628 | +        import json | 
|  | 629 | + | 
|  | 630 | +        qubit_op = QubitOperator('X1 Y2', 1 + 2j) | 
|  | 631 | +        save_operator(qubit_op, self.file_name) | 
|  | 632 | + | 
|  | 633 | +        file_path = get_file_path(self.file_name, None) | 
|  | 634 | +        with open(file_path, 'r') as f: | 
|  | 635 | +            data = json.load(f) | 
|  | 636 | + | 
|  | 637 | +        self.assertEqual(len(data), 2) | 
|  | 638 | +        self.assertEqual(data[0], 'QubitOperator') | 
|  | 639 | + | 
|  | 640 | +        # The key is stringified tuple | 
|  | 641 | +        # The value is a list [real, imag] | 
|  | 642 | +        expected_terms = {"((1, 'X'), (2, 'Y'))": [1.0, 2.0]} | 
|  | 643 | +        self.assertEqual(data[1], expected_terms) | 
|  | 644 | + | 
| 597 | 645 | 
 | 
| 598 | 646 | class GetFileDirTest(unittest.TestCase): | 
| 599 | 647 |     def setUp(self): | 
|  | 
0 commit comments