qc2.algorithms.pennylane.vqe

Module defining VQE algorithm for PennyLane.

Module Contents

Classes

VQE

Main class for the VQE algorithm with PennyLane.

class qc2.algorithms.pennylane.vqe.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)[source]

Bases: 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.

ansatz

The ansatz for the VQE algorithm. Defaults to qml.UCCSD.

Type:

Callable

active_space

Instance of ActiveSpace. Defaults to ActiveSpace((2, 2), 2).

Type:

ActiveSpace

mapper

Strategy for fermionic-to-qubit mapping. Defaults to JordanWignerMapper.

Type:

QubitMapper

device

Device for estimating the expectation value. Defaults to default.qubit.

Type:

qml.device

optimizer

Optimization routine for circuit variational parameters. Defaults to qml.GradientDescentOptimizer.

Type:

qml.optimizer

reference_state

Reference state for the VQE algorithm. Defaults to qml.qchem.hf_state.

Type:

qml.ref_state

params

List of initial VQE circuit parameters. Defaults to a list with entries of zero.

Type:

List

max_iterations

Maximum number of iterations for the combined circuit-orbitals parameters optimization. Defaults to 50.

Type:

int

conv_tol

Convergence tolerance for the optimization. Defaults to 1e-7.

Type:

float

verbose

Verbosity level. Defaults to 0.

Type:

int

circuit

Quantum circuit generated for the VQE algorithm.

Type:

QNode

static _get_default_reference(qubits: int, electrons: int) pennylane.numpy.ndarray[source]

Generate the default reference state for the ansatz.

Parameters:
  • qubits (int) – Number of qubits in the circuit.

  • electrons (int) – Number of electrons in the system.

Returns:

Reference state vector.

Return type:

np.ndarray

static _get_default_ansatz(qubits: int, electrons: int, reference_state: pennylane.numpy.ndarray) Callable[source]

Create the default ansatz function for the VQE circuit.

Parameters:
  • qubits (int) – Number of qubits in the circuit.

  • electrons (int) – Number of electrons in the system.

  • reference_state (np.ndarray) – Reference state for the ansatz.

Returns:

Function that applies the UCCSD ansatz.

Return type:

Callable

static _get_default_init_params(qubits: int, electrons: int) pennylane.numpy.ndarray[source]

Generate default initial parameters for the ansatz.

Parameters:
  • qubits (int) – Number of qubits in the circuit.

  • electrons (int) – Number of electrons in the system.

Returns:

Array of initial parameter values.

Return type:

np.ndarray

static _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[source]

Builds and return PennyLane QNode.

Parameters:
  • dev (str) – PennyLane quantum device.

  • qubits (int) – Number of qubits in the circuit.

  • ansatz (Callable) – Ansatz function for the circuit.

  • qubit_op (Operator) – Qubit operator for the Hamiltonian.

  • device_args (list, optional) – Additional arguments for the quantum device. Defaults to None.

  • device_kwargs (dict, optional) – Additional keyword arguments for the quantum device. Defaults to None.

  • qnode_args (list, optional) – Additional arguments for the QNode. Defaults to None.

  • qnode_kwargs (dict, optional) – Additional keyword arguments for the QNode. Defaults to None.

Returns:

PennyLane qnode with built-in ansatz.

Return type:

QNode

run(*args, **kwargs) qc2.algorithms.algorithms_results.VQEResults[source]

Executes VQE algorithm.

Parameters:
  • *args

    • device_args (optional): qml.device arguments.

    • qnode_args (optional): qml.qnode arguments.

  • **kwargs

    • device_kwargs (optional): qml.device keyword arguments.

    • qnode_kwargs (optional): qml.qnode keyword arguments.

Returns:

An instance of qc2.algorithms.pennylane.vqe.VQEResults class with all VQE info.

Return type:

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()