VQE

VQE

In Divi, we offer two different VQE modes. The first one is a standard single-instance ground-state energy estimation, and the latter is the hyperparameter sweep mode. We will provide examples to demonstrate modes in this section.

Vanilla VQE

For our VQE implementation, we integrate tightly with PennyLane’s qchem module and their Hamiltonian objects. As such, the VQE constructor accepts either a Molecule object, out of which the molecular Hamiltonian is generated, or the Hamiltonian itself.

Apart from the molecular information, the constructor also takes as input an ansatz, which can be selected from the available ansatze in the VQEAnsatz class, as well as the number of ansatz layers, the optimizer and the maximum number of optimization iterations.In the case where the input is a Hamiltonian, the number of electrons present in the given system must be provided when the chosen ansatz is UCCSD or Hartree-Fock.

An example of how to initialize a VQE object with a molecule is shown below:

import time

from divi.parallel_simulator import ParallelSimulator
from divi.qprog import VQE, VQEAnsatz
from divi.qprog.optimizers import Optimizers

import pennylane as qml

h2_mol = qml.qchem.Molecule(
    symbols=["H", "H"], coordinates=np.array([(0, 0, 0), (0, 0, 0.5)])
)

vqe_problem = VQE(
    molecule=h2_mol,
    ansatz=VQEAnsatz.HARTREE_FOCK,
    n_layers=1,
    optimizer=Optimizer.L_BFGS_B,
    max_iterations=3,
    backend=ParallelSimulator(),
)

vqe_problem.run()
energies = vqe_problem.losses[-1]

print(f"Minimum Energy Achieved: {min(energies.values()):.4f}")
print(f"Total circuits: {vqe_problem.total_circuit_count}")

In the case of a Hamiltonian input, the input would be passed to the constructor as follows:

ham, _ = qml.qchem.molecular_hamiltonian(h2_mol)

vqe_problem = VQE(
    hamiltonian=ham,
    n_electrons=h2_mol.n_electrons,
    ### Remaining inputs remain the same ###
)

In the example above, we attempt to compute the ground state energy of a hydrogen molecule (H₂). To extract the energy at the end of the optimization step, we simply access the last item of the losses class variable, which stores the losses of each iteration in the form of a dictionary mapping a parameter’s ID to its actual values. For L-BFGS-B, an iteration uses one set of parameters, and so the min(energies.values()) bit you see in the example is a bit redundant. If we were to use Monte-Carlo sampling, we would have as many losses as the sample points, and so the use of the min function becomes more salient.

VQE Hyperparameter Sweep

By sweeping over physical parameters like bond length and varying the ansatz, this mode enables large-scale quantum chemistry simulations — efficiently distributing the workload across cloud or hybrid backends.

This mode is particularly useful for the study molecular behavior and reaction dynamics. It also allows one to compare ansatz performance and optimizer robustness. All through a single class!

Configuring the Molecular Transformations

Divi uses Z-matrices to correctly and accurately modify molecules according to the users needs. These modifications can be declared and configured using the MoleculeTransformer class, which takes as input the base molecule onto which the transformations are applied. Additionally, these arguments are used to define the specifics of the modifications:

  • atom_connectivity: The connectivity structure of the molecule, provided as a list of tuples of indices of the atoms that have a bond between them. When not provided, the molecule would be assumed to have a chain structure (i.e. the connectivity would look like [(0, 1), (1, 2), ...]).

  • bonds_to_transform: A subset of the bonds listed in atom_connectivity to be modified. If this argument is not provided, all bonds will be affected.

  • bond_modifiers: A list of actual numeric changes to apply to the chosen bonds. This has two modes: scale and delta. If the provided list contains only strictly positive values, scale mode will be activated, where the values represent a multiplier to apply to the original bond length. Otherwise, the delta mode is enabled, where the provided values act as additives to the original bond length. One can trivially provide 1 and 0 for the scale and delta modes respectively to include the base molecule as an experiment.

  • alignment_atoms: For debugging purposes, the output molecules can be aligned using Kabsch algorithm, where users provide a list of indices of reference atoms that act as the “spine” of the whole molecule. An example of such would be the carbon chain of an alkane group.

Code Example

The example below demonstrates how to use Divi’s VQEHyperparameterSweep class to run a parallelized VQE simulation across multiple bond lengths and ansatz types for a hydrogen molecule (H₂).

from divi.qprog import VQEAnsatz, VQEHyperparameterSweep, MoleculeTransformer
from divi.qprog.optimizers import MonteCarloOptimizer
from divi import QoroService

q_service = QoroService(QORO_API_KEY, shots=5000)

mol = qml.qchem.Molecule(
    symbols=["H", "H"], coordinates=np.array([(0, 0, 0), (0, 0, 0.5)])
)

transformer = MoleculeTransformer(
    base_molecule=mol, bond_modifiers=[-0.4, -0.25, 0, 0.25, 0.4]
)

optim = MonteCarloOptimizer(n_param_sets=10, n_best_sets=3)

vqe_problem = VQEHyperparameterSweep(
    molecule_transformer=transformer,
    ansatze=[VQEAnsatz.HARTREE_FOCK, VQEAnsatz.UCCSD],
    max_iterations=1,
    optimizer=optim,
    backend=q_service,
)

vqe_problem.create_programs()
vqe_problem.run(blocking=True)
vqe_problem.aggregate_results()

print(f"Total circuits: {vqe_problem.total_circuit_count}")
print(f"Simulation time: {vqe_problem.total_run_time}")

vqe_problem.visualize_results()

What’s Happening?

Step Description
VQEHyperparameterSweep(...) Initializes a batch of VQE programs over a range of bond lengths and ansatz strategies.
molecule_transformer=... The transformer declaring the changes to apply to the molecule. In this instance, we are contracting all bonds by -0.4, -0.25 Å and stretching them by 0.25 and 0.4 Å, in addition to the base molecule
ansatze=[HARTREE_FOCK, UCCSD] Runs two different quantum circuit models for comparison.
create_programs() Constructs all circuits for each (bond modifier, ansatz) pair.
run(blocking=True) Executes all VQE circuits — possibly in parallel. Block the script until all programs finish executing.
aggregate_results() Collects and merges the final energy values for plotting.
visualize_results() Displays a graph of energy vs. bond length for each ansatz.

Visualization

Divi comes built with visualization tools that allows the user to compare the approaches. The above example produces this plot for example. This is an ongoing effort, the goal is to provide dashboards for better visualization and a more in-depth comparison.

Parallelized VQE energy levels

The result of running parallelized VQE

Why Parallelize VQE?

  • VQE is an iterative algorithm requiring multiple circuit evaluations per step.
  • Sweeping over bond lengths and ansatze creates hundreds of circuits.
  • Parallelizing execution reduces total compute time and helps saturate available QPU/GPU/CPU resources.