Extended VQE

In Tutorial 1, we considered a canonical VQE calculation at a single geometry with no resource optimization. However, in general, this will only be the first step in an analysis of the quantum algorithm. We may wish to expand on this analysis by considering more molecular geometries or systems – for example, looking at the energetics of bond dissociation. We also may wish to compare optimization methods in order to assess their effectiveness at reducing the overall cost with regards to quantum computational resources.

In this tutorial, we will look at how to achieve these goals using InQuanto. We start by examining bond dissociation in molecular hydrogen using a canonical VQE approach. Then, we will look at a slightly larger system – the bending and stretching of water. As this is a larger system, we will have to introduce optimizations to enable the simulations to run on a standard laptop. Specifically, we introduce how to reduce the active space (and thus the number of qubits in the quantum computation) by freezing orbitals using the inquanto-pyscf driver. Finally, we look at one optimization strategy in InQuanto - Ansatz parameter reduction by point group symmetry.

[1]:
from pytket.extensions.qiskit import AerStateBackend
from inquanto.express import run_vqe
from inquanto.minimizers import MinimizerScipy
from inquanto.ansatzes import FermionSpaceAnsatzUCCSD
from inquanto.mappings import QubitMappingJordanWigner
from inquanto.extensions.pyscf import ChemistryDriverPySCFMolecularRHF
import datetime
import matplotlib.pyplot as plt
import numpy as np

H2 Bond Stretching

After imports, we start by examining bond dissociation in molecular hydrogen in order to present a general workflow.

[2]:
def hydrogen_vqe_energy(bond_length):
    basis = 'STO-3G'
    geometry = [["H", [0, 0, 0]], ["H", [0, 0, bond_length]]]
    charge = 0

    driver = ChemistryDriverPySCFMolecularRHF(basis=basis, geometry=geometry, charge=charge)
    fermionic_hamiltonian, fock_space, fock_state = driver.get_system()
    jw = QubitMappingJordanWigner
    qubit_hamiltonian = jw.operator_map(fermionic_hamiltonian)
    ansatz = FermionSpaceAnsatzUCCSD(fock_space, fock_state, jw)
    backend = AerStateBackend()
    minimizer = MinimizerScipy(method="L-BFGS-B", disp=False)
    vqe = run_vqe(ansatz, qubit_hamiltonian, backend=backend, with_gradient=True, minimizer=minimizer)

    ground_state_energy = vqe.generate_report()["final_value"]
    hartree_fock_energy = driver.mf_energy
    return ground_state_energy, hartree_fock_energy

print(hydrogen_vqe_energy(0.741))
# TIMER BLOCK-0 BEGINS AT 2024-09-23 15:34:07.619233
# TIMER BLOCK-0 ENDS - DURATION (s):  3.8022306 [0:00:03.802231]
(-1.1372744055294364, -1.116706137236105)

The code here does all that is necessary to generate a ground state energy using canonical VQE for the hydrogen molecule, as in Tutorial 1. Here, we have wrapped it in a function to allow us to easily view the change with bond length:

[3]:
h2_bond_lengths = np.linspace(0.4,2.0,20)
h2_results = [hydrogen_vqe_energy(x) for x in h2_bond_lengths]
print(h2_results)
# TIMER BLOCK-1 BEGINS AT 2024-09-23 15:34:12.516675
# TIMER BLOCK-1 ENDS - DURATION (s):  3.9892883 [0:00:03.989288]
# TIMER BLOCK-2 BEGINS AT 2024-09-23 15:34:17.473250
# TIMER BLOCK-2 ENDS - DURATION (s):  3.4605232 [0:00:03.460523]
# TIMER BLOCK-3 BEGINS AT 2024-09-23 15:34:21.597441
# TIMER BLOCK-3 ENDS - DURATION (s):  3.3837125 [0:00:03.383712]
# TIMER BLOCK-4 BEGINS AT 2024-09-23 15:34:25.595372
# TIMER BLOCK-4 ENDS - DURATION (s):  3.0837872 [0:00:03.083787]
# TIMER BLOCK-5 BEGINS AT 2024-09-23 15:34:29.499589
# TIMER BLOCK-5 ENDS - DURATION (s):  3.0614044 [0:00:03.061404]
# TIMER BLOCK-6 BEGINS AT 2024-09-23 15:34:33.292163
# TIMER BLOCK-6 ENDS - DURATION (s):  2.8407735 [0:00:02.840774]
# TIMER BLOCK-7 BEGINS AT 2024-09-23 15:34:36.692157
# TIMER BLOCK-7 ENDS - DURATION (s):  2.6530918 [0:00:02.653092]
# TIMER BLOCK-8 BEGINS AT 2024-09-23 15:34:39.999917
# TIMER BLOCK-8 ENDS - DURATION (s):  2.3483380 [0:00:02.348338]
# TIMER BLOCK-9 BEGINS AT 2024-09-23 15:34:42.716474
# TIMER BLOCK-9 ENDS - DURATION (s):  2.4950823 [0:00:02.495082]
# TIMER BLOCK-10 BEGINS AT 2024-09-23 15:34:45.519155
# TIMER BLOCK-10 ENDS - DURATION (s):  2.1871271 [0:00:02.187127]
# TIMER BLOCK-11 BEGINS AT 2024-09-23 15:34:48.183608
# TIMER BLOCK-11 ENDS - DURATION (s):  2.0441255 [0:00:02.044126]
# TIMER BLOCK-12 BEGINS AT 2024-09-23 15:34:50.587154
# TIMER BLOCK-12 ENDS - DURATION (s):  2.0699998 [0:00:02.070000]
# TIMER BLOCK-13 BEGINS AT 2024-09-23 15:34:53.116108
# TIMER BLOCK-13 ENDS - DURATION (s):  1.9776200 [0:00:01.977620]
# TIMER BLOCK-14 BEGINS AT 2024-09-23 15:34:55.426151
# TIMER BLOCK-14 ENDS - DURATION (s):  2.0250066 [0:00:02.025007]
# TIMER BLOCK-15 BEGINS AT 2024-09-23 15:34:57.959668
# TIMER BLOCK-15 ENDS - DURATION (s):  2.0978484 [0:00:02.097848]
# TIMER BLOCK-16 BEGINS AT 2024-09-23 15:35:00.555649
# TIMER BLOCK-16 ENDS - DURATION (s):  1.8218271 [0:00:01.821827]
# TIMER BLOCK-17 BEGINS AT 2024-09-23 15:35:02.634087
# TIMER BLOCK-17 ENDS - DURATION (s):  1.8862051 [0:00:01.886205]
# TIMER BLOCK-18 BEGINS AT 2024-09-23 15:35:04.928465
# TIMER BLOCK-18 ENDS - DURATION (s):  1.8986914 [0:00:01.898691]
# TIMER BLOCK-19 BEGINS AT 2024-09-23 15:35:07.392818
# TIMER BLOCK-19 ENDS - DURATION (s):  1.7153837 [0:00:01.715384]
# TIMER BLOCK-20 BEGINS AT 2024-09-23 15:35:09.606870
# TIMER BLOCK-20 ENDS - DURATION (s):  1.4614484 [0:00:01.461448]
[(-0.9141497046270836, -0.9043613941635398), (-1.039644193368418, -1.0278952240485908), (-1.1027234512173354, -1.0885821110334752), (-1.1303984654811357, -1.1133931546411708), (-1.1373027360323475, -1.1169158055488677), (-1.1320031440770355, -1.1076586023375483), (-1.119647652656638, -1.0906923776851998), (-1.103357320131612, -1.0690432214496197), (-1.0850885605499416, -1.04456492118589), (-1.0661536707290789, -1.0184750668009017), (-1.047492324194345, -0.9916426659510071), (-1.0297900705318137, -0.9647203151980028), (-1.0135255910407297, -0.9382014307919535), (-0.998995920249617, -0.9124510069368059), (-0.9863403143934909, -0.8877296363816177), (-0.9755678355211701, -0.8642149983652552), (-0.9665878877681977, -0.8420201240601859), (-0.9592413973101761, -0.8212077601664908), (-0.9533300339756416, -0.8018012103709642), (-0.9486411121756362, -0.7837926542773532)]

We have successfully generated a potential energy curve for the dissociation of the H2 molecule using VQE and, as a reference, Hartree-Fock. We can now separate the VQE and HF results and plot them:

[4]:
h2_vqe_results,h2_hf_results = zip(*h2_results)
plt.plot(h2_bond_lengths,h2_vqe_results,label='VQE')
plt.plot(h2_bond_lengths,h2_hf_results,label='HF')
plt.xlabel('H-H Bond Length/Å')
plt.ylabel('Ground state energy/Ha')
plt.legend()
[4]:
<matplotlib.legend.Legend at 0x7f269b4f2cd0>
../_images/tutorials_InQ_tut_vqe_2_7_1.png

Here we can see the improvement obtained by using UCCSD VQE – indeed, this system is sufficiently low in number of spin-orbitals that UCCSD is exact (i.e. FCI-level). On the other hand, we can also observe the increasing inaccuracy of (restricted) Hartree-Fock at higher bond lengths.

H2O Bending - active space reduction

H2O in an STO-3G basis is a 14 spin-orbital (and thus 14 qubit for conventional qubit mappings) system. This is within the capacity of a classical computer to simulate, but such a simulation may perhaps require more resources than is practical for this tutorial. We can reduce the active spin-orbital space by freezing orbitals. While our purpose here is to demonstrate, in a real experiment it may be necessary to freeze orbitals in order to reduce the (exponentially growing) resources to a level that is actually implementable. In InQuanto, orbital freezing is performed by passing the frozen parameter to the driver:

[5]:
def water_bending_vqe_energy(bond_angle):

    x_h2 = np.sin(bond_angle / 360 * np.pi)
    x_h1 = -x_h2
    y_h1 = np.cos(bond_angle / 360 * np.pi)
    y_h2 = y_h1

    geometry = [['H', [x_h1, y_h1, 0.]], ['O', [0., 0., 0.]], ['H', [x_h2, y_h2, 0.]]]
    basis = 'STO-3G'
    charge = 0
    frozen = [0]

    driver = ChemistryDriverPySCFMolecularRHF(basis=basis, geometry=geometry, charge=charge, frozen=frozen)
    fermionic_hamiltonian, fock_space, fock_state = driver.get_system()
    jw = QubitMappingJordanWigner
    qubit_hamiltonian = jw.operator_map(fermionic_hamiltonian)
    ansatz = FermionSpaceAnsatzUCCSD(fock_space, fock_state, jw)
    backend = AerStateBackend()
    minimizer = MinimizerScipy(method="L-BFGS-B", disp=False)
    vqe = run_vqe(ansatz, qubit_hamiltonian, backend=backend, with_gradient=True, minimizer=minimizer)

    ground_state_energy = vqe.generate_report()["final_value"]
    hartree_fock_energy = driver.mf_energy
    return ground_state_energy, hartree_fock_energy

print(water_bending_vqe_energy(104.5))
# TIMER BLOCK-21 BEGINS AT 2024-09-23 15:35:16.278157
# TIMER BLOCK-21 ENDS - DURATION (s): 149.6519089 [0:02:29.651909]
(-75.01966834467385, -74.96466253913087)

This block may take up to a minute to run, as the system is a bit bigger than molecular hydrogen. Here, we have asked the driver to freeze the lowest energy spatial orbital (i.e. the core electrons). Note that frozen orbitals are specified as a list of indices of spatial orbitals, not spin-orbitals - so every orbital frozen in this way will save two qubits. Note that for consistency, we have here specified the geometry in Cartesian co-ordinates by explicitly calculating the position of each atom. It is also possible in InQuanto to specify geometries in z-matrix format.

As before, we have successfully calculated the VQE and HF energy at (roughly) the equilibrium geometry. We can again calculate the effect of changing the bond angle and plot the results (this may take a few minutes to run - reducing the amount of data points generated will speed it up if needed):

[6]:
h2o_bond_angles = np.linspace(45.,180.,10)
h2o_bending_results = [water_bending_vqe_energy(x) for x in h2o_bond_angles]
# TIMER BLOCK-22 BEGINS AT 2024-09-23 15:37:49.076580
# TIMER BLOCK-22 ENDS - DURATION (s): 153.0670829 [0:02:33.067083]
# TIMER BLOCK-23 BEGINS AT 2024-09-23 15:40:25.092865
# TIMER BLOCK-23 ENDS - DURATION (s): 140.7399766 [0:02:20.739977]
# TIMER BLOCK-24 BEGINS AT 2024-09-23 15:42:47.743317
# TIMER BLOCK-24 ENDS - DURATION (s): 92.1810489 [0:01:32.181049]
# TIMER BLOCK-25 BEGINS AT 2024-09-23 15:44:21.838298
# TIMER BLOCK-25 ENDS - DURATION (s): 89.0485358 [0:01:29.048536]
# TIMER BLOCK-26 BEGINS AT 2024-09-23 15:45:52.674037
# TIMER BLOCK-26 ENDS - DURATION (s): 89.0692643 [0:01:29.069264]
# TIMER BLOCK-27 BEGINS AT 2024-09-23 15:47:23.581150
# TIMER BLOCK-27 ENDS - DURATION (s): 97.2221374 [0:01:37.222137]
# TIMER BLOCK-28 BEGINS AT 2024-09-23 15:49:02.638114
# TIMER BLOCK-28 ENDS - DURATION (s): 98.0095019 [0:01:38.009502]
# TIMER BLOCK-29 BEGINS AT 2024-09-23 15:50:42.588165
# TIMER BLOCK-29 ENDS - DURATION (s): 107.2418132 [0:01:47.241813]
# TIMER BLOCK-30 BEGINS AT 2024-09-23 15:52:31.645497
# TIMER BLOCK-30 ENDS - DURATION (s): 115.7171976 [0:01:55.717198]
# TIMER BLOCK-31 BEGINS AT 2024-09-23 15:54:28.729498
# TIMER BLOCK-31 ENDS - DURATION (s): 86.7374110 [0:01:26.737411]
[7]:
h2o_angle_vqe_results,h2o_angle_hf_results = zip(*h2o_bending_results)
plt.plot(h2o_bond_angles,h2o_angle_vqe_results,label='VQE')
plt.plot(h2o_bond_angles,h2o_angle_hf_results,label='HF')
plt.xlabel('HOH bond angle /°')
plt.ylabel('Ground state energy/Ha')
plt.legend()
[7]:
<matplotlib.legend.Legend at 0x7f2691f6a790>
../_images/tutorials_InQ_tut_vqe_2_13_1.png

H2O Stretching - symmetry-allowed excitations

In larger systems, we may be interested in benchmarking the cost of VQE with various optimization schemes. Choosing optimization schemes is a question of balancing the need for accuracy and resource constraints. Several resource constraints occur when performing quantum algorithms – for instance, the number of qubits and the circuit length. Similar to space and time in classical computing, certain optimization schemes may reduce cost in one metric while having a detrimental effect on others. Such tradeoff in resources applies to VQE itself; VQE as an algorithm is designed to replace the (extremely) long quantum circuits of the phase estimation algorithm with significantly more but much shorter circuits.

Many such optimization schemes are available in InQuanto and examples of their use can be found in the examples directory. In this tutorial, we will look at the use of point group symmetry to exclude unphysical symmetry-violating excitations. This is a technique commonly used in quantum chemistry codes on classical computers, and can substantially reduce the number of Ansatz parameters. In turn, the quantum circuit length can be reduced, as is the difficulty of classical optimization (and consequentially the number of individual VQE shots required, and thus the overall runtime). As this technique is simply removing excitations that are unphysical, it is essentially “free” with regards to other computational resources.

First, we modify our VQE routine to incorporate point group symmetry – this time, in the context of symmetric bond stretching:

[8]:
def water_stretching_vqe_energy(bond_length):

    x_h2 = bond_length * np.sin(104.45 / 360 * np.pi)
    x_h1 = -x_h2
    y_h1 = bond_length * np.cos(104.45 / 360 * np.pi)
    y_h2 = y_h1

    geometry = [['H', [x_h1, y_h1, 0.]], ['O', [0., 0., 0.]], ['H', [x_h2, y_h2, 0.]]]
    basis = 'STO-3G'
    charge = 0
    frozen = [0]

    driver = ChemistryDriverPySCFMolecularRHF(basis=basis, geometry=geometry, charge=charge, frozen=frozen,point_group_symmetry=True)
    fermionic_hamiltonian, fock_space, fock_state = driver.get_system()
    jw = QubitMappingJordanWigner
    qubit_hamiltonian = jw.operator_map(fermionic_hamiltonian)
    ansatz = FermionSpaceAnsatzUCCSD(fock_space, fock_state, jw)
    backend = AerStateBackend()
    minimizer = MinimizerScipy(method="L-BFGS-B", disp=False)
    vqe = run_vqe(ansatz, qubit_hamiltonian, backend=backend, with_gradient=True, minimizer=minimizer)

    ground_state_energy = vqe.generate_report()["final_value"]
    hartree_fock_energy = driver.mf_energy
    return ground_state_energy, hartree_fock_energy
print(water_stretching_vqe_energy(1.))
# TIMER BLOCK-32 BEGINS AT 2024-09-23 15:55:57.124847
# TIMER BLOCK-32 ENDS - DURATION (s): 31.0824402 [0:00:31.082440]
(-75.01969733754339, -74.96468314023912)

Here, incorporating point group symmetry is as simple as passing point_group_symmetry=True to the driver. Note that the ability to use point group symmetry is reliant on the capacity of the underlying classical quantum chemistry package (in this case, PySCF). We then generate a plot of the change in the ground state energy as the bonds stretch:

[9]:
h2o_bond_lengths = np.linspace(0.6,2.,10)
h2o_stretching_results = [water_stretching_vqe_energy(x) for x in h2o_bond_lengths]
# TIMER BLOCK-33 BEGINS AT 2024-09-23 15:56:29.735611
# TIMER BLOCK-33 ENDS - DURATION (s): 27.7259454 [0:00:27.725945]
# TIMER BLOCK-34 BEGINS AT 2024-09-23 15:56:58.974011
# TIMER BLOCK-34 ENDS - DURATION (s): 27.8207614 [0:00:27.820761]
# TIMER BLOCK-35 BEGINS AT 2024-09-23 15:57:28.260302
# TIMER BLOCK-35 ENDS - DURATION (s): 30.9017611 [0:00:30.901761]
# TIMER BLOCK-36 BEGINS AT 2024-09-23 15:58:00.578911
# TIMER BLOCK-36 ENDS - DURATION (s): 31.1680076 [0:00:31.168008]
# TIMER BLOCK-37 BEGINS AT 2024-09-23 15:58:33.141350
# TIMER BLOCK-37 ENDS - DURATION (s): 33.8284279 [0:00:33.828428]
# TIMER BLOCK-38 BEGINS AT 2024-09-23 15:59:08.399508
# TIMER BLOCK-38 ENDS - DURATION (s): 42.5848047 [0:00:42.584805]
# TIMER BLOCK-39 BEGINS AT 2024-09-23 15:59:52.414016
# TIMER BLOCK-39 ENDS - DURATION (s): 38.9578873 [0:00:38.957887]
# TIMER BLOCK-40 BEGINS AT 2024-09-23 16:00:32.833302
# TIMER BLOCK-40 ENDS - DURATION (s): 48.4212424 [0:00:48.421242]
# TIMER BLOCK-41 BEGINS AT 2024-09-23 16:01:22.684842
# TIMER BLOCK-41 ENDS - DURATION (s): 62.1268340 [0:01:02.126834]
# TIMER BLOCK-42 BEGINS AT 2024-09-23 16:02:26.344463
# TIMER BLOCK-42 ENDS - DURATION (s): 68.9516516 [0:01:08.951652]
[10]:
h2o_lengths_vqe_results,h2o_lengths_hf_results = zip(*h2o_stretching_results)
plt.plot(h2o_bond_lengths,h2o_lengths_vqe_results,label='VQE')
plt.plot(h2o_bond_lengths,h2o_lengths_hf_results,label='HF')
plt.xlabel('OH bond length /Å')
plt.ylabel('Ground state energy/Ha')
plt.legend()
[10]:
<matplotlib.legend.Legend at 0x7f2691f85550>
../_images/tutorials_InQ_tut_vqe_2_18_1.png

We have successfully demonstrated that the point group symmetry reductions yield the correct ground state energy. However, we have not yet looked at the resource reductions obtained. We can adapt our VQE wrapper functions to return this, but for simplicity here we just look at one configuration:

[11]:
x_h2 = np.sin(104.45 / 360 * np.pi)
x_h1 = -x_h2
y_h1 = np.cos(104.45 / 360 * np.pi)
y_h2 = y_h1

geometry = [['H', [x_h1, y_h1, 0.]], ['O', [0., 0., 0.]], ['H', [x_h2, y_h2, 0.]]]
basis = 'STO-3G'
charge = 0
frozen = [0]

driver_with_symmetry = ChemistryDriverPySCFMolecularRHF(basis=basis, geometry=geometry, charge=charge, frozen=frozen,point_group_symmetry=True)
driver_without_symmetry = ChemistryDriverPySCFMolecularRHF(basis=basis, geometry=geometry, charge=charge, frozen=frozen,point_group_symmetry=False)
fermionic_hamiltonian_with_symmetry, fock_space_with_symmetry, fock_state_with_symmetry = driver_with_symmetry.get_system()
fermionic_hamiltonian_without_symmetry, fock_space_without_symmetry, fock_state_without_symmetry = driver_without_symmetry.get_system()
jw = QubitMappingJordanWigner()

ansatz_with_symmetry = FermionSpaceAnsatzUCCSD(fock_space_with_symmetry, fock_state_with_symmetry, jw)
ansatz_without_symmetry = FermionSpaceAnsatzUCCSD(fock_space_without_symmetry, fock_state_without_symmetry, jw)

For simplicity, we restrict ourselves to looking at the impact on the resources required for generating the Ansatz state. Note that many quantum resources can be estimated without actually running VQE in this manner; this dramatically decreases the resources necessary to perform an experiment.

If we generate a report for each Ansatz:

[12]:
print('### ANSATZ RESOURCES WITH SYMMETRY REDUCTION ###')
print(ansatz_with_symmetry.generate_report())
print('\n### ANSATZ RESOURCES WITHOUT SYMMETRY REDUCTION ###')
print(ansatz_without_symmetry.generate_report())
### ANSATZ RESOURCES WITH SYMMETRY REDUCTION ###
{'ansatz_circuit_depth': 841, 'ansatz_circuit_gates': 2260, 'n_parameters': 30, 'n_qubits': 12}

### ANSATZ RESOURCES WITHOUT SYMMETRY REDUCTION ###
{'ansatz_circuit_depth': 2798, 'ansatz_circuit_gates': 7032, 'n_parameters': 92, 'n_qubits': 12}