Ejemplo n.º 1
0
def test_erpa_eom_ham_lih():
    filename = os.path.join(DATA_DIRECTORY, "H1-Li1_sto-3g_singlet_1.45.hdf5")
    molecule = MolecularData(filename=filename)
    reduced_ham = make_reduced_hamiltonian(
        molecule.get_molecular_hamiltonian(), molecule.n_electrons)
    rha_fermion = get_fermion_operator(reduced_ham)
    permuted_hijkl = np.einsum('ijlk', reduced_ham.two_body_tensor)
    opdm = np.diag([1] * molecule.n_electrons + [0] *
                   (molecule.n_qubits - molecule.n_electrons))
    tpdm = 2 * wedge(opdm, opdm, (1, 1), (1, 1))
    rdms = InteractionRDM(opdm, tpdm)
    dim = 3  # so we don't do the full basis.  This would make the test long
    full_basis = {}  # erpa basis.  A, B basis in RPA language
    cnt = 0
    # start from 1 to make test shorter
    for p, q in product(range(1, dim), repeat=2):
        if p < q:
            full_basis[(p, q)] = cnt
            full_basis[(q, p)] = cnt + dim * (dim - 1) // 2
            cnt += 1
    for rkey in full_basis.keys():
        p, q = rkey
        for ckey in full_basis.keys():
            r, s = ckey
            for sigma, tau in product([0, 1], repeat=2):
                test = erpa_eom_hamiltonian(permuted_hijkl, tpdm,
                                            2 * q + sigma, 2 * p + sigma,
                                            2 * r + tau, 2 * s + tau).real
                qp_op = FermionOperator(
                    ((2 * q + sigma, 1), (2 * p + sigma, 0)))
                rs_op = FermionOperator(((2 * r + tau, 1), (2 * s + tau, 0)))
                erpa_op = normal_ordered(
                    commutator(qp_op, commutator(rha_fermion, rs_op)))
                true = rdms.expectation(get_interaction_operator(erpa_op))
                assert np.isclose(true, test)
Ejemplo n.º 2
0
 def test_commutator(self):
     operator_a = FermionOperator('')
     self.assertEqual(FermionOperator.zero(),
                      commutator(operator_a, self.fermion_operator))
     operator_b = QubitOperator('X1 Y2')
     self.assertEqual(commutator(self.qubit_operator, operator_b),
                      (self.qubit_operator * operator_b -
                       operator_b * self.qubit_operator))
Ejemplo n.º 3
0
    def test_commutator_hopping_operators(self):
        com = commutator(3 * FermionOperator('1^ 2'), FermionOperator('2^ 3'))
        com = normal_ordered(com)
        self.assertEqual(com, FermionOperator('1^ 3', 3))

        com = commutator(3 * BosonOperator('1^ 2'), BosonOperator('2^ 3'))
        com = normal_ordered(com)
        self.assertTrue(com == BosonOperator('1^ 3', 3))
Ejemplo n.º 4
0
    def test_commutes_number_operators(self):
        com = commutator(FermionOperator('4^ 3^ 4 3'), FermionOperator('2^ 2'))
        com = normal_ordered(com)
        self.assertEqual(com, FermionOperator.zero())

        com = commutator(BosonOperator('4^ 3^ 4 3'), BosonOperator('2^ 2'))
        com = normal_ordered(com)
        self.assertTrue(com == BosonOperator.zero())
Ejemplo n.º 5
0
    def test_commutes_identity(self):
        com = commutator(FermionOperator.identity(),
                         FermionOperator('2^ 3', 2.3))
        self.assertEqual(com, FermionOperator.zero())

        com = commutator(BosonOperator.identity(), BosonOperator('2^ 3', 2.3))
        self.assertTrue(com == BosonOperator.zero())

        com = commutator(QuadOperator.identity(), QuadOperator('q2 p3', 2.3))
        self.assertTrue(com == QuadOperator.zero())
Ejemplo n.º 6
0
    def test_commutes_no_intersection(self):
        com = commutator(FermionOperator('2^ 3'), FermionOperator('4^ 5^ 3'))
        com = normal_ordered(com)
        self.assertEqual(com, FermionOperator.zero())

        com = commutator(BosonOperator('2^ 3'), BosonOperator('4^ 5^ 3'))
        com = normal_ordered(com)
        self.assertTrue(com == BosonOperator.zero())

        com = commutator(QuadOperator('q2 p3'), QuadOperator('q4 q5 p3'))
        com = normal_ordered(com)
        self.assertTrue(com == QuadOperator.zero())
Ejemplo n.º 7
0
    def test_split_operator_error_operator_VT_order_against_definition(self):
        hamiltonian = (normal_ordered(fermi_hubbard(3, 3, 1., 4.0)) -
                       2.3 * FermionOperator.identity())
        potential_terms, kinetic_terms = (
            diagonal_coulomb_potential_and_kinetic_terms_as_arrays(hamiltonian)
        )
        potential = sum(potential_terms, FermionOperator.zero())
        kinetic = sum(kinetic_terms, FermionOperator.zero())

        error_operator = (
            split_operator_trotter_error_operator_diagonal_two_body(
                hamiltonian, order='V+T'))

        # V-then-T ordered double commutators: [V, [T, V]] + [T, [T, V]] / 2
        inner_commutator = normal_ordered(commutator(kinetic, potential))
        error_operator_definition = normal_ordered(
            commutator(potential, inner_commutator))
        error_operator_definition += normal_ordered(
            commutator(kinetic, inner_commutator)) / 2.0
        error_operator_definition /= 12.0

        self.assertEqual(error_operator, error_operator_definition)
Ejemplo n.º 8
0
    def test_canonical_boson_commutation_relations(self):
        op_1 = BosonOperator('3')
        op_1_dag = BosonOperator('3^')
        op_2 = BosonOperator('4')
        op_2_dag = BosonOperator('4^')
        zero = BosonOperator()
        one = BosonOperator('')

        self.assertTrue(one == normal_ordered(commutator(op_1, op_1_dag)))
        self.assertTrue(zero == normal_ordered(commutator(op_1, op_2)))
        self.assertTrue(zero == normal_ordered(commutator(op_1, op_2_dag)))
        self.assertTrue(zero == normal_ordered(commutator(op_1_dag, op_2)))
        self.assertTrue(zero == normal_ordered(commutator(op_1_dag, op_2_dag)))
        self.assertTrue(one == normal_ordered(commutator(op_2, op_2_dag)))
Ejemplo n.º 9
0
    def test_canonical_quad_commutation_relations(self):
        q1 = QuadOperator('q3')
        p1 = QuadOperator('p3')
        q2 = QuadOperator('q4')
        p2 = QuadOperator('p4')
        zero = QuadOperator()
        one = QuadOperator('')
        hbar = 2.

        self.assertTrue(1j * hbar *
                        one == normal_ordered(commutator(q1, p1), hbar))
        self.assertTrue(zero == normal_ordered(commutator(q1, q2), hbar))
        self.assertTrue(zero == normal_ordered(commutator(q1, p2), hbar))
        self.assertTrue(zero == normal_ordered(commutator(p1, q2), hbar))
        self.assertTrue(zero == normal_ordered(commutator(p1, p2), hbar))
        self.assertTrue(1j * hbar *
                        one == normal_ordered(commutator(q2, p2), hbar))
Ejemplo n.º 10
0
 def test_commutator_hopping_with_double_number_two_intersections(self):
     com = commutator(FermionOperator('2^ 3'), FermionOperator('3^ 2^ 3 2'))
     com = normal_ordered(com)
     self.assertEqual(com, FermionOperator.zero())
Ejemplo n.º 11
0
 def test_commutator_hopping_with_single_number(self):
     com = commutator(FermionOperator('1^ 2', 1j), FermionOperator('1^ 1'))
     com = normal_ordered(com)
     self.assertEqual(com, -FermionOperator('1^ 2') * 1j)
Ejemplo n.º 12
0
 def test_commutator_not_same_type(self):
     with self.assertRaises(TypeError):
         commutator(self.fermion_operator, self.qubit_operator)
Ejemplo n.º 13
0
 def test_commutator_operator_b_bad_type(self):
     with self.assertRaises(TypeError):
         commutator(self.qubit_operator, "hello")
Ejemplo n.º 14
0
 def test_commutator_operator_a_bad_type(self):
     with self.assertRaises(TypeError):
         commutator(1, self.fermion_operator)
Ejemplo n.º 15
0
 def test_ndarray_input(self):
     """Test when the inputs are numpy arrays."""
     X = pauli_matrix_map['X'].toarray()
     Y = pauli_matrix_map['Y'].toarray()
     Z = pauli_matrix_map['Z'].toarray()
     self.assertTrue(numpy.allclose(commutator(X, Y), 2.j * Z))