def test_normal_ordered(self):
        """test normal_ordered method"""
        with self.subTest("Test for creation operator"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                orig = FermionicOp("+")
            fer_op = orig.normal_ordered()
            targ = FermionicOp("+_0", display_format="sparse")
            self.assertFermionEqual(fer_op, targ)
            self.assertEqual(orig.display_format, "sparse")

        with self.subTest("Test for annihilation operator"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                orig = FermionicOp("-")
            fer_op = orig.normal_ordered()
            targ = FermionicOp("-_0", display_format="sparse")
            self.assertFermionEqual(fer_op, targ)
            self.assertEqual(orig.display_format, "sparse")

        with self.subTest("Test for number operator"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                orig = FermionicOp("N")
            fer_op = orig.normal_ordered()
            targ = FermionicOp("+_0 -_0", display_format="sparse")
            self.assertFermionEqual(fer_op, targ)
            self.assertEqual(orig.display_format, "sparse")

        with self.subTest("Test for empty operator"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                orig = FermionicOp("E")
            fer_op = orig.normal_ordered()
            targ = FermionicOp([("", 1), ("+_0 -_0", -1)],
                               display_format="sparse")
            self.assertFermionEqual(fer_op, targ)
            self.assertEqual(orig.display_format, "sparse")

        with self.subTest("Test for multiple operators 1"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                orig = FermionicOp("-+")
            fer_op = orig.normal_ordered()
            targ = FermionicOp([("-_0 +_1", 1)], display_format="sparse")
            self.assertFermionEqual(fer_op, targ)
            self.assertEqual(orig.display_format, "sparse")

        with self.subTest("Test for multiple operators 2"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                orig = 3 * FermionicOp("E+-")
            fer_op = orig.normal_ordered()
            targ = FermionicOp([("+_1 -_2", 3), ("+_0 -_0 +_1 -_2", -3)],
                               display_format="sparse")
            self.assertFermionEqual(fer_op, targ)
            self.assertEqual(orig.display_format, "sparse")

        with self.subTest("Test normal ordering simplifies"):
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=UserWarning)
                orig = FermionicOp([("-_1 +_2", 3), ("+_2 -_1", -3)],
                                   display_format="sparse")
            fer_op = orig.normal_ordered()
            expected = [((("-", 1), ("+", 2)), 6)]
            self.assertEqual(fer_op._data, expected)
    def test_h2(self):
        """Test H2 molecule"""
        with self.subTest("Excitation edges 1"):
            assert np.alltrue(
                _bksf_edge_list_fermionic_op(
                    FermionicOp("+-+-", display_format="dense")) == np.array(
                        [[0, 1], [2, 3]]))

        with self.subTest("Excitation edges 2"):
            assert np.alltrue(
                _bksf_edge_list_fermionic_op(
                    FermionicOp("+--+", display_format="dense")) == np.array(
                        [[0, 1], [3, 2]]))

        ## H2 from pyscf with sto-3g basis
        h2_fop = FermionicOp(
            [
                ("+-+-", (0.18128880821149607 + 0j)),
                ("+--+", (-0.18128880821149607 + 0j)),
                ("-++-", (-0.18128880821149607 + 0j)),
                ("-+-+", (0.18128880821149604 + 0j)),
                ("IIIN", (-0.4759487152209648 + 0j)),
                ("IINI", (-1.2524635735648986 + 0j)),
                ("IINN", (0.48217928821207245 + 0j)),
                ("INII", (-0.4759487152209648 + 0j)),
                ("ININ", (0.697393767423027 + 0j)),
                ("INNI", (0.6634680964235684 + 0j)),
                ("NIII", (-1.2524635735648986 + 0j)),
                ("NIIN", (0.6634680964235684 + 0j)),
                ("NINI", (0.6744887663568382 + 0j)),
                ("NNII", (0.48217928821207245 + 0j)),
            ],
            display_format="dense",
        )

        expected_pauli_op = SparsePauliOp.from_list([
            ("IIII", (-0.8126179630230767 + 0j)),
            ("IIZZ", (0.17119774903432952 + 0j)),
            ("IYYI", (0.04532220205287402 + 0j)),
            ("IZIZ", (0.17119774903432955 + 0j)),
            ("IZZI", (0.34297063344496626 + 0j)),
            ("XIIX", (0.04532220205287402 + 0j)),
            ("YIIY", (0.04532220205287402 + 0j)),
            ("YZZY", (0.04532220205287402 + 0j)),
            ("ZIIZ", (0.3317340482117842 + 0j)),
            ("ZIZI", (-0.22278593040418454 + 0j)),
            ("ZXXZ", (0.04532220205287402 + 0j)),
            ("ZYYZ", (0.04532220205287402 + 0j)),
            ("ZZII", (-0.22278593040418454 + 0j)),
            ("ZZZZ", (0.24108964410603623 + 0j)),
        ])

        pauli_sum_op = BravyiKitaevSuperFastMapper().map(h2_fop)

        op1 = _sort_simplify(expected_pauli_op)
        op2 = _sort_simplify(pauli_sum_op.primitive)

        with self.subTest("Map H2 frome sto3g basis, number of terms"):
            self.assertEqual(len(op1), len(op2))

        with self.subTest("Map H2 frome sto3g basis result"):
            self.assertEqual(op1, op2)

        with self.subTest("Sparse FermionicOp input"):
            h2_fop_sparse = h2_fop.normal_ordered()
            pauli_sum_op_from_sparse = BravyiKitaevSuperFastMapper().map(
                h2_fop_sparse)
            op2_from_sparse = _sort_simplify(
                pauli_sum_op_from_sparse.primitive)
            self.assertEqual(op1, op2_from_sparse)

        with self.subTest("Test accepting identity with zero coefficient"):
            h2_fop_zero_term = h2_fop + FermionicOp([("IIII", 0.0)],
                                                    display_format="dense")
            pauli_sum_op_extra = BravyiKitaevSuperFastMapper().map(
                h2_fop_zero_term)
            op3 = _sort_simplify(pauli_sum_op_extra.primitive)
            self.assertEqual(op1, op3)

        with self.subTest("Test zero FermiOp"):
            fermi_op = FermionicOp([("++--", 0.0)], display_format="dense")
            pauli_op = BravyiKitaevSuperFastMapper().map(fermi_op).primitive
            x = np.array([], dtype="bool")
            z = np.array([], dtype="bool")
            expected_pauli_op = SparsePauliOp(PauliList.from_symplectic(x, z),
                                              coeffs=[0.0])
            self.assertEqual(pauli_op, expected_pauli_op)