qc2.algorithms.pennylane

qc2 algorithms package for pennylane.

Submodules

Package Contents

Classes

VQE

Main class for the VQE algorithm with PennyLane.

oo_VQE

Main class for orbital-optimized VQE with PennyLane.

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

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

freeze_active

If True, freezes the active space during optimization.

Type:

bool

orbital_params

List of orbital optimization parameters. Defaults to a list with entries of zero.

Type:

List

circuit_params

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

Type:

List

oo_problem

An instance of OrbitalOptimization problem class. Defaults to None.

Type:

OrbitalOptimization

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

static _get_default_init_orbital_params(n_kappa: List) List[source]

Set up the init orbital rotation parameters.

Parameters:

n_kappa (List) – number of orbital rotation parameters.

Returns:

List of params values

Return type:

List

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

Optimizes both the circuit and orbital parameters.

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.OOVQEResults class with all oo-VQE info.

Return type:

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()
_circuit_optimization(theta: List, kappa: List, *args, **kwargs) Tuple[List, float][source]

Get total energy and best circuit parameters for a given kappa.

Parameters:
  • theta (List) – List with circuit variational parameters.

  • kappa (List) – List with orbital rotation 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:

Optimized circuit parameters and associated energy.

Return type:

Tuple[List, float]

_get_energy_from_parameters(theta: List, kappa: List, *args, **kwargs) float[source]

Calculates total energy given circuit and orbital parameters.

Parameters:
  • theta (List) – List with circuit variational parameters.

  • kappa (List) – List with orbital rotation 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:

Total ground-state energy for a given circuit and orbital parameters.

Return type:

float

_get_rdms(theta: List, sum_spin=True, *args, **kwargs) Tuple[pennylane.numpy.ndarray, pennylane.numpy.ndarray][source]

Calculates 1- and 2-electron reduced density matrices (RDMs).

Parameters:
  • theta (List) – circuit parameters at which RDMs are calculated.

  • sum_spin (bool) – 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.

  • *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:

1- and 2-RDMs.

Return type:

Tuple[np.ndarray, np.ndarray]