Ejemplo n.º 1
0
 def test_s_squared_operator(self):
     op = s_squared_operator(2)
     s_minus = (FermionOperator(((1, 1), (0, 0))) +
                FermionOperator(((3, 1), (2, 0))))
     s_plus = (FermionOperator(((0, 1), (1, 0))) +
               FermionOperator(((2, 1), (3, 0))))
     s_z = (FermionOperator(((0, 1), (0, 0)), 0.5) -
            FermionOperator(((1, 1), (1, 0)), 0.5) +
            FermionOperator(((2, 1), (2, 0)), 0.5) -
            FermionOperator(((3, 1), (3, 0)), 0.5))
     expected = s_minus * s_plus + s_z * s_z + s_z
     self.assertTrue(op.isclose(expected))
Ejemplo n.º 2
0
    def test_uccsd_singlet_symmetries(self):
        """Test that the singlet generator has the correct symmetries."""
        test_orbitals = 8
        test_electrons = 4

        packed_amplitude_size = uccsd_singlet_paramsize(
            test_orbitals, test_electrons)
        packed_amplitudes = randn(int(packed_amplitude_size))
        generator = uccsd_singlet_generator(packed_amplitudes, test_orbitals,
                                            test_electrons)

        # Construct symmetry operators
        sz = sz_operator(test_orbitals)
        s_squared = s_squared_operator(test_orbitals)

        # Check the symmetries
        comm_sz = normal_ordered(commutator(generator, sz))
        comm_s_squared = normal_ordered(commutator(generator, s_squared))
        zero = FermionOperator()

        self.assertEqual(comm_sz, zero)
        self.assertEqual(comm_s_squared, zero)
Ejemplo n.º 3
0
    def get_operators(self, guess="minao", pyscf_mol=None):
        if mpi.main_rank:
            # Run electronic structure calculations
            if pyscf_mol is None:
                self, pyscf_mol = run_pyscf_mod(guess,
                                                self.n_orbitals,
                                                self.n_electrons,
                                                self,
                                                run_casci=self.run_fci)

            # 'n_electrons' and 'n_orbitals' must not be 'None'.
            n_core_orbitals = (self.n_electrons - self.n_electrons) // 2
            occupied_indices = list(range(n_core_orbitals))
            active_indices = list(
                range(n_core_orbitals, n_core_orbitals + self.n_orbitals))

            hf_energy = self.hf_energy
            fci_energy = self.fci_energy

            mo_coeff = self.canonical_orbitals.astype(float)
            natom = pyscf_mol.natm
            #atom_charges = pyscf_mol.atom_charges().reshape(-1, 1)
            atom_charges = pyscf_mol.atom_charges().reshape(1, -1)
            atom_coords = pyscf_mol.atom_coords()
            rint = pyscf_mol.intor("int1e_r")

            Hamiltonian = self.get_molecular_hamiltonian(
                occupied_indices=occupied_indices,
                active_indices=active_indices)

            # Dipole operators from dipole integrals (AO)
            rx = create_1body_operator(mo_coeff,
                                       rint[0],
                                       ao=True,
                                       n_active_orbitals=self.n_orbitals)
            ry = create_1body_operator(mo_coeff,
                                       rint[1],
                                       ao=True,
                                       n_active_orbitals=self.n_orbitals)
            rz = create_1body_operator(mo_coeff,
                                       rint[2],
                                       ao=True,
                                       n_active_orbitals=self.n_orbitals)
            Dipole = np.array([rx, ry, rz])
        else:
            Hamiltonian = None
            Dipole = None
            hf_energy = None
            fci_energy = None
            mo_coeff = None
            natom = None
            atom_charges = None
            atom_coords = None

        # MPI broadcasting
        Hamiltonian = mpi.comm.bcast(Hamiltonian, root=0)
        Dipole = mpi.comm.bcast(Dipole, root=0)
        hf_energy = mpi.comm.bcast(hf_energy, root=0)
        fci_energy = mpi.comm.bcast(fci_energy, root=0)
        mo_coeff = mpi.comm.bcast(mo_coeff, root=0)
        natom = mpi.comm.bcast(natom, root=0)
        atom_charges = mpi.comm.bcast(atom_charges, root=0)
        atom_coords = mpi.comm.bcast(atom_coords, root=0)

        # Put values in self
        self.hf_energy = hf_energy
        self.fci_energy = fci_energy
        self.mo_coeff = mo_coeff
        self.natom = natom
        self.atom_charges = atom_charges
        self.atom_coords = atom_coords

        # Print out some results
        print_geom(self.geometry)
        prints("E[FCI] = ", fci_energy)
        prints("E[HF]  = ", hf_energy)
        prints("")

        S2 = s_squared_operator(self.n_orbitals)
        Number = number_operator(self.n_orbitals)

        return Hamiltonian, S2, Number, Dipole
Ejemplo n.º 4
0
 def test_s_squared_operator_invalid_input(self):
     with self.assertRaises(TypeError):
         s_squared_operator('a')