Hamiltonian

class dopyqo.Hamiltonian(h_pq: ndarray, h_pqrs: ndarray, occupations: ndarray, constants: float | dict[str, float] = 0.0, reference_energy: float = 0.0)[source]

Bases: object

Defines a second-quantization Hamiltonian of the form

\[H = \sum_{pq} h_{pq} a_p^\dagger a_q + \frac{1}{2} \sum_{pqrs} h_{pqrs} a_p^\dagger a_q^\dagger a_r a_s + E_{\text{const.}}\]

with

\[h_{pqrs} = \int\!\!\int \phi^*_p(\mathbf{r}_1)\,\phi^*_q(\mathbf{r}_2) \frac{1}{\lvert\mathbf{r}_1 - \mathbf{r}_2\rvert} \phi_r(\mathbf{r}_2)\,\phi_s(\mathbf{r}_1) \,d\mathbf{r}_1\,d\mathbf{r}_2\]

\(h_{pq}\) and \(h_{pqrs}\) are defined in terms of N spatial orbitals.

Parameters:
  • h_pq (np.ndarray) – One-electron matrix elements. Shape (N, N).

  • h_pqrs (np.ndarray) – Two-electron matrix elements. Shape (N, N, N, N).

  • occupations (np.ndarray) – Occupations of the N spatial orbitals taking values either 0 or 1, and summing up to half of the number of electrons. Shape (N,)

  • constants (float | dict[str, float], optional) – Energy offset \(E_{\text{const.}}\) as float or dict of str, float pairs. Defaults to 0.0.

  • reference_energy (float, optional) – Reference energy used for VQE calculations. Defaults to 0.0.

to_qiskit_problem(auto_index_order=True) qiskit_nature.second_q.problems.ElectronicStructureProblem[source]

Create Qiskit ElectronicStructureProblem object.

Parameters:

auto_index_order (bool, optional) – auto_index_order parameter passed to Qiskit ElectronicIntegrals.from_raw_integrals. Defaults to True.

Returns:

Created Qiskit ElectronicStructureProblem

Return type:

ElectronicStructureProblem

hf_energy() float[source]

Calculate the mean-field energy of the single Slater determinant given by self.occupations. If this Slater determinant is the Hartree-Fock state, the energy is the Hartree-Fock energy.

The calculated energy is:

\[E = 2 \sum_i h_{ii} + \sum_{ij} (2 h_{ijji} - h_{ijij}) + E_{\text{const.}}\]

where the sums go over the occupied orbitals.

Returns:

The calculated mean-field energy

Return type:

float

hf_energy_qiskit(mapper: FermionicMapper | None = qiskit_nature.second_q.mappers.JordanWignerMapper)[source]

Calculate the mean-field energy of the single Slater determinant given by self.occupations. If this Slater determinant is the Hartree-Fock state, the energy is the Hartree-Fock energy.

The energy is calculated using Qiskit by using its HartreeFock class and statevector simulation.

The calculated energy is:

\[E = 2 \sum_i h_{ii} + \sum_{ij} (2 h_{ijji} - h_{ijij}) + E_{\text{const.}}\]

where the sums go over the occupied orbitals.

Parameters:

mapper (FermionicMapper | None, optional) – Fermion-to-qubit mapper. Defaults to JordanWignerMapper().

Returns:

The calculated mean-field energy

Return type:

float

fci_statevector() qiskit.quantum_info.Statevector[source]

Return the statevector of the FCI state

Returns:

Qiskit statevector object of the FCI state

Return type:

Statevector

vqe_statevector(vqe_params: ndarray | None = None) qiskit.quantum_info.Statevector[source]

Return the statevector of the state prepared by the TenCirChem VQE ansatz

Parameters:

vqe_params (np.ndarray | None, optional) – Parameters used for the VQE ansatz. If None, the parameters determined of a prior VQE optimization are used. Defaults to None.

Raises:

ValueError – If no TenCirChem VQE optimization has been performed and no parameters are given.

Returns:

Qiskit statevector object of the state prepared by the TenCirChem VQE ansatz

Return type:

Statevector

vqe_statevector_qiskit(vqe_params: ndarray | None = None) qiskit.quantum_info.Statevector[source]

Return the statevector of the state prepared by the Qiskit VQE ansatz

Parameters:

vqe_params (np.ndarray | None, optional) – Parameters used for the VQE ansatz. If None, the parameters determined of a prior VQE optimization are used. Defaults to None.

Raises:

ValueError – If no Qiskit VQE optimization has been performed and no parameters are given.

Returns:

Qiskit statevector object of the state prepared by the Qiskit VQE ansatz

Return type:

Statevector

run_qiskit_ansatz(parameters: np.ndarray, UCCSD_reps: int = 1, mapper: FermionicMapper | None = qiskit_nature.second_q.mappers.JordanWignerMapper) float[source]

Run the Qiskit UCCSD ansatz and return the estimated energy

Parameters:
  • parameters (np.ndarray) – Parameters used for the ansatz.

  • UCCSD_reps (int, optional) – Number of times the UCCSD ansatz is repeated with new parameters. Defaults to 1.

  • mapper (FermionicMapper | None, optional) – Used Fermion-to-qubit mapping. Defaults to JordanWignerMapper().

Returns:

The energy of the ansatz

Return type:

float

run_tcc_ansatz(parameters: ndarray, UCCSD_reps: int = 1, excitations: list[tuple[int, ...]] | None = None, qiskit_equivalent: bool = False) float[source]

Run the TenCirChem UCCSD ansatz and return the estimated energy

Parameters:
  • parameters (np.ndarray) – Parameters used for the VQE ansatz.

  • UCCSD_reps (int, optional) – Number of times the UCCSD ansatz is repeated with new parameters. Defaults to 1.

  • excitations (list[tuple[int, ...]] | None, optional) – Excitations used in the ansatz. If None all double and single excitations are used. Defaults to None.

  • qiskit_equivalent (bool, optional) – If set to True, the same excitations and ordering as in Qiskit is used. With this, the provided parameters give the same result as the run_qiskit_ansatz function. Defaults to False.

Returns:

The energy of the ansatz

Return type:

float

solve_vqe(optimizer: VQEOptimizers = VQEOptimizers.L_BFGS_B, UCCSD_reps: int = 1, qiskit_equivalent: bool = False, initial_params: ndarray | None = None, maxiter: int | None = None, excitations: list[tuple[int, ...]] | ExcitationPools = ExcitationPools.SINGLES_DOUBLES) OptimizeResult[source]

Run a VQE optimization with TenCirChem and a UCCSD ansatz. The initial state, that is prepared before the parametrized ansatz is applied, is a Slater determinant matching self.occupations.

Parameters:
  • optimizer (dopyqo.VQEOptimizers, optional) – Classical optimizer used in the VQE. Defaults to dopyqo.VQEOptimizers.L_BFGS_B.

  • UCCSD_reps (int, optional) – Number of times the UCCSD ansatz is repeated with new parameters. Defaults to 1.

  • qiskit_equivalent (bool, optional) – If set to True, the same excitations and ordering as in Qiskit is used. With this, the provided parameters give the same result as the solve_vqe_qiskit function. Defaults to False.

  • initial_params (np.ndarray | None, optional) – Parameters to start the VQE optimization with. Defaults to None.

  • maxiter (int | None, optional) – Maximum number of optimizer iterations before stopping the optimization. If None, large defaults are used. Defaults to None.

  • excitations (list[tuple[int, ...]] | dopyqo.ExcitationPools, optional) – Excitations used in the ansatz. Defaults to dopyqo.ExcitationPools.SINGLES_DOUBLES where all double and single excitations are used.

Returns:

Scipy optimization result

Return type:

scipy.optimize.OptimizeResult

solve_vqe_qiskit(optimizer: dopyqo.VQEOptimizers = VQEOptimizers.L_BFGS_B, UCCSD_reps: int = 1, mapper: FermionicMapper | None = qiskit_nature.second_q.mappers.JordanWignerMapper, maxiter: int | None = None, excitations: list[tuple[int, ...]] | dopyqo.ExcitationPools = ExcitationPools.SINGLES_DOUBLES) VQEResult[source]

Run a VQE optimization with Qiskit and a UCCSD ansatz. The initial state, that is prepared before the parametrized ansatz is applied, is a Slater determinant matching self.occupations.

Parameters:
  • mapper (FermionicMapper | None, optional) – Used Fermion-to-qubit mapping. Defaults to JordanWignerMapper().

  • optimizer (dopyqo.VQEOptimizers, optional) – Classical optimizer used in the VQE. Defaults to dopyqo.VQEOptimizers.L_BFGS_B.

  • maxiter (int | None, optional) – Maximum number of optimizer iterations before stopping the optimization. If None, large defaults are used. Defaults to None.

  • UCCSD_reps (int, optional) – Number of times the UCCSD ansatz is repeated with new parameters. Defaults to 1.

Returns:

Qiskit VQEResult object

Return type:

VQEResult

solve_vqe_adapt(optimizer: VQEOptimizers = VQEOptimizers.L_BFGS_B, maxiter: int | None = None, excitation_pool: list[tuple[int, ...]] | ExcitationPools = ExcitationPools.SINGLES_DOUBLES, drain_pool: bool = True, conv_threshold: float = 1e-13, selection_criterion: AdaptSelectionCriterion = AdaptSelectionCriterion.ENERGY) OptimizeResult[source]

Run a ADAPT-VQE optimization with TenCirChem given a pool of excitations. The initial state, that is prepared before the parametrized ansatz is applied, is a Slater determinant matching self.occupations. All appended excitations are optimized after every appended excitation. The criterion with which a excitation is selected from the pool can be set with the selection_criterion parameter.

Parameters:
  • optimizer (dopyqo.VQEOptimizers, optional) – Classical optimizer used in the VQE. Defaults to dopyqo.VQEOptimizers.L_BFGS_B.

  • maxiter (int | None, optional) – Maximum number of optimizer iterations used for optimizing all appended excitations before stopping the optimization. If None, large defaults are used. Defaults to None.

  • excitation_pool (list[tuple[int, ...]] | dopyqo.ExcitationPools, optional) – Excitations in the ADAPT pool. Defaults to dopyqo.ExcitationPools.SINGLES_DOUBLES where all double and single excitations are in the pool.

  • drain_pool (bool, optional) – If set to True an excitation is removed from the pool if it was appended to the ansatz. This means each operator can only appear once in the ansatz. Defaults to True.

  • conv_threshold (float, optional) – Excitations are appended to the ansatz until their initial impact drops below this threshold value. Defaults to 1e-13.

  • selection_criterion (dopyqo.AdaptSelectionCriterion, optional) – Used operator selection criterion. When dopyqo.AdaptSelectionCriterion.ENERGY is used, every appended excitation is initialized with its optimal parameter.Defaults to dopyqo.AdaptSelectionCriterion.ENERGY.

Returns:

Scipy optimization result

Return type:

scipy.optimize.OptimizeResult

solve_hf() float[source]

Perform a Hartree-Fock (HF) calculation with PySCF. The PySCF RHF object is saved in self.mf.

Returns:

HF energy

Return type:

float

solve_fci(n_energies=1) ndarray[source]

Perform a FCI calculation using PySCF. If the matrix elements self.h_pq and self.h_pqrs are real, the function self.solve_fci_spin1 is executed which uses the PySCF solver pyscf.fci.direct_spin1.FCISolver(). If the matrix elements are complex, the function self.solve_fci_general is executed which uses the PySCF solver pyscf.fci.direct_spin1.FCISolver().

Parameters:

n_energies (int, optional) – Number of energies the FCI solver will compute. Defaults to 1.

Returns:

Computed FCI energies as a numpy array.

Return type:

np.ndarray

solve_fci_spin1(n_energies=1) ndarray[source]

Perform a FCI calculation using PySCF solver pyscf.fci.direct_spin1.FCISolver().

Parameters:

n_energies (int, optional) – Number of energies the FCI solver will compute. Defaults to 1.

Returns:

Computed FCI energies as a numpy array.

Return type:

np.ndarray

solve_fci_uhf(n_energies=1) ndarray[source]

Perform a FCI calculation using the PySCF solver pyscf.fci.direct_uhf.FCISolver().

Parameters:

n_energies (int, optional) – Number of energies the FCI solver will compute. Defaults to 1.

Returns:

Computed FCI energies as a numpy array.

Return type:

np.ndarray

solve_fci_general(n_energies=1) ndarray[source]

Perform a FCI calculation using the PySCF solver pyscf.fci.direct_spin1.FCISolver().

Parameters:

n_energies (int, optional) – Number of energies the FCI solver will compute. Defaults to 1.

Returns:

Computed FCI energies as a numpy array.

Return type:

np.ndarray

solve_CCSD() float[source]

Perform a CCSD calculation using the PySCF solver pyscf.cc.RCCSD().

Returns:

Computed CCSD energy.

Return type:

float

solve_CCSD_t() ndarray[source]

Perform a CCSD(T) calculation using the PySCF solver pyscf.cc.CCSD() in combination with the .ccsd_t() PySCF function.

Returns:

Computed CCSD(T) energy.

Return type:

float