qc2.algorithms.pennylane.oo_vqe =============================== .. py:module:: qc2.algorithms.pennylane.oo_vqe .. autoapi-nested-parse:: Module defining oo-VQE algorithm for PennyLane. Classes ------- .. autoapisummary:: qc2.algorithms.pennylane.oo_vqe.oo_VQE Module Contents --------------- .. py:class:: oo_VQE(qc2data=None, ansatz=None, active_space=None, mapper=None, device=None, optimizer=None, reference_state=None, init_circuit_params=None, init_orbital_params=None, freeze_active=False, max_iterations=50, conv_tol=1e-07, verbose=0) Bases: :py:obj:`qc2.algorithms.pennylane.vqe.VQE` Main class for orbital-optimized VQE with PennyLane. This class is responsible for optimizing both circuit and orbital parameters of simple molecules. Analytic first and second derivatives are considered in the orbital optimization part. .. attribute:: freeze_active If True, freezes the active space during optimization. :type: bool .. attribute:: orbital_params List of orbital optimization parameters. Defaults to a list with entries of zero. :type: List .. attribute:: circuit_params List of VQE circuit parameters. Defaults to a list with entries of zero. :type: List .. attribute:: oo_problem An instance of :class:`~qc2.algorithms.utils.orbital_optimization.OrbitalOptimization` problem class. Defaults to None. :type: OrbitalOptimization .. attribute:: max_iterations Maximum number of iterations for the combined circuit-orbitals parameters optimization. Defaults to 50. :type: int .. attribute:: conv_tol Convergence tolerance for the optimization. Defaults to 1e-7. :type: float .. attribute:: verbose Verbosity level. Defaults to 0. :type: int .. py:attribute:: freeze_active :value: False .. py:attribute:: orbital_params :value: None .. py:attribute:: circuit_params .. py:attribute:: oo_problem :value: None .. py:method:: _get_default_init_orbital_params(n_kappa: List) -> List :staticmethod: Set up the init orbital rotation parameters. :param n_kappa: number of orbital rotation parameters. :type n_kappa: List :returns: List of params values :rtype: List .. py:method:: run(*args, **kwargs) -> qc2.algorithms.algorithms_results.OOVQEResults Optimizes both the circuit and orbital parameters. :param \*args: - device_args (optional): ``qml.device`` arguments. - qnode_args (optional): ``qml.qnode`` arguments. :param \*\*kwargs: - device_kwargs (optional): ``qml.device`` keyword arguments. - qnode_kwargs (optional): ``qml.qnode`` keyword arguments. :returns: An instance of :class:`qc2.algorithms.pennylane.vqe.OOVQEResults` class with all oo-VQE info. :rtype: OOVQEResults **Example** >>> from ase.build import molecule >>> from qc2.ase import PySCF >>> from qc2.data import qc2Data >>> from qc2.algorithms.pennylane import oo_VQE >>> from qc2.algorithms.utils import ActiveSpace >>> >>> mol = molecule('H2O') >>> >>> hdf5_file = 'h2o.hdf5' >>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema') >>> qc2data.molecule.calc = PySCF() >>> qc2data.run() >>> qc2data.algorithm = oo_VQE( ... active_space=ActiveSpace( ... num_active_electrons=(2, 2), ... num_active_spatial_orbitals=4 ... ), ... optimizer=qml.GradientDescentOptimizer(stepsize=0.5), ... device="default.qubit" ... ) >>> results = qc2data.algorithm.run() .. py:method:: _circuit_optimization(theta: List, kappa: List, *args, **kwargs) -> Tuple[List, float] Get total energy and best circuit parameters for a given kappa. :param theta: List with circuit variational parameters. :type theta: List :param kappa: List with orbital rotation parameters. :type kappa: List :param \*args: - device_args (optional): ``qml.device`` arguments. - qnode_args (optional): ``qml.qnode`` arguments. :param \*\*kwargs: - device_kwargs (optional): ``qml.device`` keyword arguments. - qnode_kwargs (optional): ``qml.qnode`` keyword arguments. :returns: Optimized circuit parameters and associated energy. :rtype: Tuple[List, float] .. py:method:: _get_energy_from_parameters(theta: List, kappa: List, *args, **kwargs) -> float Calculates total energy given circuit and orbital parameters. :param theta: List with circuit variational parameters. :type theta: List :param kappa: List with orbital rotation parameters. :type kappa: List :param \*args: - device_args (optional): ``qml.device`` arguments. - qnode_args (optional): ``qml.qnode`` arguments. :param \*\*kwargs: - device_kwargs (optional): ``qml.device`` keyword arguments. - qnode_kwargs (optional): ``qml.qnode`` keyword arguments. :returns: Total ground-state energy for a given circuit and orbital parameters. :rtype: float .. py:method:: _get_rdms(theta: List, sum_spin=True, *args, **kwargs) -> Tuple[pennylane.numpy.ndarray, pennylane.numpy.ndarray] Calculates 1- and 2-electron reduced density matrices (RDMs). :param theta: circuit parameters at which RDMs are calculated. :type theta: List :param sum_spin: If True, the spin-summed 1-RDM and 2-RDM will be returned. If False, the full 1-RDM and 2-RDM will be returned. Defaults to True. :type sum_spin: bool :param \*args: - device_args (optional): ``qml.device`` arguments. - qnode_args (optional): ``qml.qnode`` arguments. :param \*\*kwargs: - device_kwargs (optional): ``qml.device`` keyword arguments. - qnode_kwargs (optional): ``qml.qnode`` keyword arguments. :returns: 1- and 2-RDMs. :rtype: Tuple[np.ndarray, np.ndarray]