qc2.algorithms.qiskit ===================== .. py:module:: qc2.algorithms.qiskit .. autoapi-nested-parse:: qc2 algorithms package for qiskit. Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/qc2/algorithms/qiskit/iqpe/index /autoapi/qc2/algorithms/qiskit/oo_vqe/index /autoapi/qc2/algorithms/qiskit/pebase/index /autoapi/qc2/algorithms/qiskit/qpe/index /autoapi/qc2/algorithms/qiskit/vqe/index Classes ------- .. autoapisummary:: qc2.algorithms.qiskit.VQE qc2.algorithms.qiskit.oo_VQE qc2.algorithms.qiskit.QPE qc2.algorithms.qiskit.IQPE Package Contents ---------------- .. py:class:: VQE(qc2data=None, ansatz=None, active_space=None, mapper=None, estimator=None, optimizer=None, reference_state=None, init_params=None, verbose=0) Bases: :py:obj:`qc2.algorithms.base.vqe_base.VQEBASE` Main class for the VQE algorithm with Qiskit-Nature. This class initializes and executes the VQE algorithm using specified quantum components like ansatz, optimizer, and estimator. .. attribute:: active_space Describes the active space for quantum simulation. Defaults to ``ActiveSpace((2, 2), 2)``. :type: ActiveSpace .. attribute:: mapper Strategy for fermionic-to-qubit mapping. Defaults to :class:`qiskit.JordanWignerMapper`. :type: QubitMapper .. attribute:: estimator Method for estimating the expectation value. Defaults to :class:`qiskit.Estimator` :type: BaseEstimator .. attribute:: optimizer Optimization routine for circuit variational parameters. Defaults to :class:`qiskit_algorithms.SLSQP`. :type: qiskit.Optmizer .. attribute:: reference_state Reference state for the VQE algorithm. Defaults to :class:`qiskit.HartreeFock`. :type: QuantumCircuit .. attribute:: ansatz The ansatz for the VQE algorithm. Defaults to :class:`qiskit.UCCSD`. :type: UCC .. attribute:: params List of initial VQE circuit parameters. Defaults to a list with entries of zero. :type: List .. attribute:: verbose Verbosity level. Defaults to 0. :type: int .. py:attribute:: active_space .. py:attribute:: mapper .. py:attribute:: estimator .. py:attribute:: optimizer .. py:attribute:: reference_state .. py:attribute:: ansatz .. py:attribute:: params .. py:attribute:: verbose :value: 0 .. py:method:: _get_default_reference(active_space: qc2.algorithms.utils.active_space.ActiveSpace, mapper: qiskit_nature.second_q.mappers.QubitMapper) -> qiskit.circuit.QuantumCircuit :staticmethod: Set up the default reference state circuit based on Hartree Fock. :param active_space: description of the active space. :type active_space: ActiveSpace :param mapper: mapper class instance. :type mapper: mapper :returns: Hartree-Fock circuit as the reference state. :rtype: QuantumCircuit .. py:method:: _get_default_ansatz(active_space: qc2.algorithms.utils.active_space.ActiveSpace, mapper: qiskit_nature.second_q.mappers.QubitMapper, reference_state: qiskit.circuit.QuantumCircuit) -> qiskit_nature.second_q.circuit.library.UCC :staticmethod: Set up the default UCC ansatz from a Hartree Fock reference state. :param active_space: Description of the active space. :type active_space: ActiveSpace :param mapper: Mapper class instance. :type mapper: QubitMapper :param reference_state: Reference state circuit. :type reference_state: QuantumCircuit :returns: UCC ansatz quantum circuit. :rtype: UCC .. py:method:: _get_default_init_params(nparams: List) -> List :staticmethod: Generates a list of initial circuit parameters for the ansatz. :param nparams: Number of parameters in the ansatz. :type nparams: int :returns: List of initial parameter values (all zeros). :rtype: List[float] .. py:method:: run(*args, **kwargs) -> qc2.algorithms.algorithms_results.VQEResults Executes the VQE algorithm. :param \*args: Variable length argument list to be passed to the :class:`qiskit_algorithm.VQE` class. :param \*\*kwargs: Arbitrary keyword arguments to be passed to the :class:`qiskit_algorithm.VQE` class. :returns: An instance of :class:`qc2.algorithms.qiskit.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.qiskit 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=SLSQP(), ... estimator=Estimator(), ... ) >>> results = qc2data.algorithm.run() .. py:class:: oo_VQE(qc2data=None, ansatz=None, active_space=None, mapper=None, estimator=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.qiskit.vqe.VQE` Main class for orbital-optimized VQE with Qiskit-Nature. This class extends the VQE class to include orbital optimization. It supports customized ansatzes, active space definitions, qubit mapping strategies, estimation methods, and optimization routines. Orbital optimization is performed alongside VQE parameter optimization using analytic first and second derivatives .. 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:attribute:: max_iterations :value: 50 .. py:attribute:: conv_tol :value: 1e-07 .. py:method:: run() -> qc2.algorithms.algorithms_results.OOVQEResults Optimizes both the circuit and orbital parameters. :returns: An instance of :class:`qc2.algorithms.qiskit.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.qiskit 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 ... ), ... mapper="jw", ... optimizer=SLSQP(), ... estimator=Estimator(), ... ) >>> results = qc2data.algorithm.run() .. py:method:: _circuit_optimization(theta: List, kappa: List) -> 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 :returns: Optimized circuit parameters and associated energy. :rtype: Tuple[List, float] .. py:method:: _get_energy_from_parameters(theta: List, kappa: List) -> 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 :returns: Total ground-state energy for a given circuit and orbital parameters. :rtype: float .. py:method:: _get_rdms(theta: List, sum_spin=True) -> Tuple[numpy.ndarray, 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 :returns: 1- and 2-RDMs. :rtype: Tuple[np.ndarray, np.ndarray] .. py:class:: QPE(qc2data=None, num_evaluation_qubits=None, active_space=None, mapper=None, sampler=None, reference_state=None, verbose=0) Bases: :py:obj:`qc2.algorithms.qiskit.pebase.PEBase` .. py:attribute:: num_evaluation_qubits :value: 3 .. py:attribute:: solver .. py:class:: IQPE(qc2data=None, num_iterations=None, active_space=None, mapper=None, sampler=None, reference_state=None, verbose=0) Bases: :py:obj:`qc2.algorithms.qiskit.pebase.PEBase` .. py:attribute:: num_iterations :value: 3 .. py:attribute:: solver