def test_basic_split(self):
     """Test spliting the fermion operators for a simple case.
     """
     test_ops = FermionOperator('1 1^', 1.0)
     test_ops = normal_ordered(test_ops)
     terms, _ = split_openfermion_tensor(test_ops)
     self.assertEqual(FermionOperator('1^ 1', -1.0), terms[2])
    def test_transform_to_spin_broken(self):
        """Check the conversion between number and spin broken
        representations
        """
        in_ops = FermionOperator('5^ 7', 1.0)
        in_ops += FermionOperator('0^ 2^ 1 3', 2.0)
        in_ops += FermionOperator('5^ 6 1 7', 3.0)

        ref_ops = FermionOperator('7^ 5', -1.0)
        ref_ops += FermionOperator('3^ 2^ 1^ 0^', -2.0)
        ref_ops += FermionOperator('7^ 1^ 6 5', 3.0)
        test = normal_ordered(transform_to_spin_broken(in_ops))
        self.assertEqual(ref_ops, test)
Пример #3
0
    def __init__(
        self,
        operators: Union[FermionOperator, str],
        conserve_spin: bool = True,
        e_0: complex = 0.0 + 0.0j,
    ) -> None:
        """Initializes a SparseHamiltonian.

        Args:
            operators: Operator with a coefficient in the FermionOperator
                       format.
            conserve_spin: Whether or not to conserve the Sz symmetry.
            e_0: Scalar part of the Hamiltonian.
        """
        if isinstance(operators, str):
            ops = FermionOperator(operators, 1.0)
        else:
            ops = operators

        ops = normal_ordered(ops)

        work = ops.terms.pop((), None)
        if work is not None:
            e_0 += work

        super().__init__(e_0=e_0)

        self._operators: List[Tuple[complex, List[Tuple[int, int]],
                                    List[Tuple[int, int]]]] = []
        self._conserve_spin = conserve_spin

        self._rank = 0
        for prod in ops.terms:
            self._rank = max(self._rank, len(prod))

        for oper in ops.get_operators():
            (
                coeff,
                phase,
                alpha_block,
                beta_block,
            ) = hamiltonian_utils.gather_nbody_spin_sectors(oper)

            alpha_out: List[Tuple[int, int]] = []
            beta_out: List[Tuple[int, int]] = []
            for alpha in alpha_block:
                alpha_out.append((alpha[0] // 2, alpha[1]))
            for beta in beta_block:
                beta_out.append((beta[0] // 2, beta[1]))
            self._operators.append((coeff * phase, alpha_out, beta_out))