qxmt.models.vqe.basic module

qxmt.models.vqe.basic module#

class qxmt.models.vqe.basic.BasicVQE(device, hamiltonian, ansatz, diff_method='adjoint', max_steps=100, min_steps=None, tol=1e-06, verbose=True, optimizer_settings=None, init_params_config=None, logger=<Logger qxmt.models.vqe.basic (INFO)>)

Bases: BaseVQE

Basic implementation of the Variational Quantum Eigensolver (VQE).

This class provides a basic implementation of VQE using PennyLane’s optimization tools. It supports gradient-based optimization of quantum circuits to find the ground state energy of a given Hamiltonian. SciPy optimizers can now use PennyLane’s automatic differentiation for efficient gradient computation.

Parameters:
  • device (BaseDevice) – Quantum device to use for the VQE.

  • hamiltonian (BaseHamiltonian) – Hamiltonian to find the ground state of.

  • ansatz (BaseAnsatz) – Quantum circuit ansatz to use.

  • diff_method (Literal[None, 'best', 'device', 'backprop', 'adjoint', 'parameter-shift', 'hadamard', 'finite-diff', 'spsa'] | None) – Method to use for differentiation. Defaults to “adjoint”.

  • max_steps (int) – Maximum number of optimization steps. Defaults to 100.

  • min_steps (int | None) – Minimum number of optimization steps. Defaults to 1/10 of max_steps.

  • tol (float) – Tolerance for the optimization. Defaults to 1e-6.

  • verbose (bool) – Whether to output progress during optimization. Defaults to True.

  • optimizer_settings (dict[str, Any] | None) – Settings for the optimizer. For SciPy optimizers, you can set “gradient_method”: “numerical” to use SciPy’s numerical differentiation instead of PennyLane’s automatic differentiation.

  • logger (Logger) – Logger object for output. Defaults to module-level logger.

  • init_params_config (dict[str, Any] | None)

Example

Using SciPy optimizer with PennyLane automatic differentiation:

optimizer_settings = {
    "name": "scipy.BFGS",
    "params": {
        "gradient_method": "autodiff",  # Use PennyLane autodiff (default)
        "options": {"gtol": 1e-6}
    }
}
vqe = BasicVQE(device, hamiltonian, ansatz, optimizer_settings=optimizer_settings)
vqe.optimize()

Using SciPy numerical differentiation:

optimizer_settings = {
    "name": "scipy.BFGS",
    "params": {
        "gradient_method": "numerical",  # Use SciPy numerical gradients
    }
}
cost_history

List of cost values during optimization.

params_history

List of parameter values during optimization.

optimize(init_params=None)

Optimize the ansatz parameters to find the ground state.

This method performs gradient-based optimization of the ansatz parameters to minimize the expectation value of the Hamiltonian.

Parameters:

init_params (Optional[qml.numpy.ndarray | np.ndarray]) – Initial parameters for the ansatz. If None, the ansatz parameters are initialized to zero.

Return type:

None

Note

The optimization history (cost and parameters) is stored in the class attributes cost_history and params_history.