qc2.algorithms.pennylane.vqe ============================ .. py:module:: qc2.algorithms.pennylane.vqe .. autoapi-nested-parse:: Module defining VQE algorithm for PennyLane. Classes ------- .. autoapisummary:: qc2.algorithms.pennylane.vqe.VQE Module Contents --------------- .. py:class:: VQE(qc2data=None, ansatz=None, active_space=None, mapper=None, device=None, optimizer=None, reference_state=None, init_params=None, max_iterations=50, conv_tol=1e-07, verbose=0) Bases: :py:obj:`qc2.algorithms.base.vqe_base.VQEBASE` Main class for the VQE algorithm with PennyLane. This class initializes and executes the VQE algorithm using specified quantum components like ansatz, optimizer, and estimator. .. attribute:: ansatz The ansatz for the VQE algorithm. Defaults to ``qml.UCCSD``. :type: Callable .. attribute:: active_space Instance of :class:`~qc2.algorithm.utils.activate_space.ActiveSpace`. Defaults to ``ActiveSpace((2, 2), 2)``. :type: ActiveSpace .. attribute:: mapper Strategy for fermionic-to-qubit mapping. Defaults to ``JordanWignerMapper``. :type: QubitMapper .. attribute:: device Device for estimating the expectation value. Defaults to ``default.qubit``. :type: qml.device .. attribute:: optimizer Optimization routine for circuit variational parameters. Defaults to ``qml.GradientDescentOptimizer``. :type: qml.optimizer .. attribute:: reference_state Reference state for the VQE algorithm. Defaults to ``qml.qchem.hf_state``. :type: qml.ref_state .. attribute:: params List of initial VQE circuit parameters. Defaults to a list with entries of zero. :type: List .. 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 .. attribute:: circuit Quantum circuit generated for the VQE algorithm. :type: QNode .. py:attribute:: active_space .. py:attribute:: device :value: 'default.qubit' .. py:attribute:: mapper .. py:attribute:: qubits .. py:attribute:: electrons .. py:attribute:: optimizer .. py:attribute:: reference_state .. py:attribute:: ansatz .. py:attribute:: params .. py:attribute:: max_iterations :value: 50 .. py:attribute:: conv_tol :value: 1e-07 .. py:attribute:: verbose :value: 0 .. py:attribute:: circuit :value: None .. py:method:: _get_default_reference(qubits: int, electrons: int) -> pennylane.numpy.ndarray :staticmethod: Generate the default reference state for the ansatz. :param qubits: Number of qubits in the circuit. :type qubits: int :param electrons: Number of electrons in the system. :type electrons: int :returns: Reference state vector. :rtype: np.ndarray .. py:method:: _get_default_ansatz(qubits: int, electrons: int, reference_state: pennylane.numpy.ndarray) -> Callable :staticmethod: Create the default ansatz function for the VQE circuit. :param qubits: Number of qubits in the circuit. :type qubits: int :param electrons: Number of electrons in the system. :type electrons: int :param reference_state: Reference state for the ansatz. :type reference_state: np.ndarray :returns: Function that applies the UCCSD ansatz. :rtype: Callable .. py:method:: _get_default_init_params(qubits: int, electrons: int) -> pennylane.numpy.ndarray :staticmethod: Generate default initial parameters for the ansatz. :param qubits: Number of qubits in the circuit. :type qubits: int :param electrons: Number of electrons in the system. :type electrons: int :returns: Array of initial parameter values. :rtype: np.ndarray .. py:method:: _build_circuit(dev: str, qubits: int, ansatz: Callable, qubit_op: pennylane.operation.Operator, device_args=None, device_kwargs=None, qnode_args=None, qnode_kwargs=None) -> pennylane.QNode :staticmethod: Builds and return PennyLane QNode. :param dev: PennyLane quantum device. :type dev: str :param qubits: Number of qubits in the circuit. :type qubits: int :param ansatz: Ansatz function for the circuit. :type ansatz: Callable :param qubit_op: Qubit operator for the Hamiltonian. :type qubit_op: Operator :param device_args: Additional arguments for the quantum device. Defaults to None. :type device_args: list, optional :param device_kwargs: Additional keyword arguments for the quantum device. Defaults to None. :type device_kwargs: dict, optional :param qnode_args: Additional arguments for the QNode. Defaults to None. :type qnode_args: list, optional :param qnode_kwargs: Additional keyword arguments for the QNode. Defaults to None. :type qnode_kwargs: dict, optional :returns: PennyLane qnode with built-in ansatz. :rtype: QNode .. py:method:: run(*args, **kwargs) -> qc2.algorithms.algorithms_results.VQEResults Executes VQE algorithm. :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.VQEResults` class with all VQE info. :rtype: VQEResults **Example** >>> from ase.build import molecule >>> from qc2.ase import PySCF >>> from qc2.data import qc2Data >>> from qc2.algorithms.pennylane import 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 = 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()