Exemple #1
0
    def _extract_active_space(self, arr):
        if self.active_space is None:
            return arr

        if isinstance(arr, ClosedShellAmplitudes):
            result = {}
            for k, v in arr.__dict__.items():
                if v is not None:
                    result[k] = self._extract_active_space(arr=v)

            return ClosedShellAmplitudes(**result)
        asd = self.active_space
        aocc = [i for i in asd.active_orbitals if i in asd.reference_orbitals]
        avir = [
            i for i in asd.active_orbitals if i not in asd.reference_orbitals
        ]
        assert self.n_orbitals == len(aocc) + len(avir)
        assert self.n_electrons == len(aocc) * 2

        n_orb_total = len(self.orbitals)
        n_electrons_total = self.n_electrons + 2 * len(
            asd.frozen_reference_orbitals)
        nocc = n_electrons_total // 2
        nvirt = n_orb_total - nocc
        avir = [x - nocc for x in avir]
        nav = len(avir)
        nao = len(aocc)

        arr_shape = arr.shape
        final_shape = []
        active_sets = []

        for i in arr_shape:
            if i == nocc:
                final_shape.append(nao)
                active_sets.append(aocc)
            elif i == nvirt:
                final_shape.append(nav)
                active_sets.append(avir)
            else:
                assert i == nocc + nvirt
                final_shape.append(nao + nav)
                active_sets.append(aocc + avir)

        final_shape = tuple(final_shape)

        def func(*args):
            result = 1
            for i in range(len(args)):
                result *= args[i] in active_sets[i]
            return result

        c = numpy.fromfunction(function=numpy.vectorize(func),
                               shape=arr_shape,
                               dtype=numpy.int)

        return numpy.extract(condition=c, arr=arr).reshape(final_shape)
Exemple #2
0
    def compute_amplitudes(
            self,
            method: str,
            options: dict = None,
            filename: str = None,
            *args,
            **kwargs) -> typing.Union[Amplitudes, ClosedShellAmplitudes]:

        if options is None:
            options = {}

        options['basis'] = self.parameters.basis_set

        if method.lower() == "mp2":
            return self.compute_mp2_amplitudes()
        try:
            psi4.core.clean_options()
            psi4.core.clean_variables()
            energy, wfn = self._run_psi4(method=method,
                                         options=options,
                                         point_group='c1',
                                         ref_wfn=self.ref_wfn.c1_deep_copy(
                                             self.ref_wfn.basisset()),
                                         filename=filename,
                                         *args,
                                         **kwargs)
            all_amplitudes = wfn.get_amplitudes()
            closed_shell = isinstance(wfn.reference_wavefunction(),
                                      psi4.core.RHF)
            if closed_shell:
                return self._extract_active_space(
                    ClosedShellAmplitudes(
                        **{k: v.to_array()
                           for k, v in all_amplitudes.items()}))
            else:
                assert (self.active_space is None
                        )  # only for closed-shell currently
                return Amplitudes(
                    **{k: v.to_array()
                       for k, v in all_amplitudes.items()})
        except Exception as err:
            raise TequilaPsi4Exception("\nFailed to compute {} amplitudes.\n" \
                                       "Make sure that you don't read in previous wavefunctions."
                                       "Active spaces might get you in trouble.".format(method))