Running quantum-classical algorithms via qc2Data class

As key component of qc2, the qc2Data class is designed not only to smoothly connect with qc2-ASE calculators (see Running qc2-ASE calculators via qc2Data class) but also to seamlessly integrate with its built-in package of native of Algorithms. This is precisely the goal of qc2, and it is what users should expect in actual qc2 runs.

This connection is facilitated through the qc2Data’s algorithm attribute. Below, we present examples of complete hybrid quantum-classical runs using qc2. For more detailed guidance, users are referred to the Tutorials section and examples directory.

In the first code snippet, we show how to perform a full VQE calculaton for hydrogen molecule. This uses fcidump as qchem data schema and Qiskit-Nature with the SLSQP optimizer, qiskit.Estimator and Bravyi-Kitaev mapper.

 1from ase.build import molecule
 2
 3from qiskit_algorithms.optimizers import SLSQP
 4from qiskit.primitives import Estimator
 5
 6from qc2.data import qc2Data
 7from qc2.ase import PySCF
 8from qc2.algorithms.qiskit import VQE
 9from qc2.algorithms.utils import ActiveSpace
10
11# set ASE Atoms object
12mol = molecule('H2')
13
14# attach a qc2-ASE calculator to Atoms object
15mol.calc = PySCF()  # => defaults to HF/sto-3g
16
17# instantiate qc2Data class
18qc2data = qc2Data(
19    molecule=mol,
20    filename='h2.fcidump',
21    schema='fcidump'
22)
23
24# run qc2-ASE calculator
25qc2data.run()
26
27# instantiate VQE algorithm class
28qc2data.algorithm = VQE(
29    active_space=ActiveSpace(
30        num_active_electrons=(1, 1),
31        num_active_spatial_orbitals=2
32    ),
33    mapper="bk",
34    optimizer=SLSQP(),
35    estimator=Estimator(),
36)
37
38# run vqe
39results = qc2data.algorithm.run()

The second example shows a oo-VQE run for water and PennyLane. This now uses qcschema as data format with the qc2-ASE Psi4 calculator. Extra options for PennyLane’s device and QNode are also added.

 1from ase.build import molecule
 2
 3import pennylane as qml
 4
 5from qc2.data import qc2Data
 6from qc2.ase import Psi4
 7from qc2.algorithms.pennylane import oo_VQE
 8from qc2.algorithms.utils import ActiveSpace
 9
10# set ASE Atoms object
11mol = molecule('H2O')
12
13# instantiate qc2Data class
14qc2data = qc2Data(
15    molecule=mol,
16    filename='h2o.hdf5',
17    schema='qcschema'
18)
19
20# one can also attach qc2-ASE calculator later on to the molecule attribute
21qc2data.molecule.calc = Psi4(method="hf", basis="sto-3g")
22
23# run qc2-ASE calculator
24qc2data.run()
25
26# instantiate oo-VQE class
27qc2data.algorithm = oo_VQE(
28    active_space=ActiveSpace(
29        num_active_electrons=(2, 2),
30        num_active_spatial_orbitals=4
31    ),
32    mapper='jw',
33    optimizer=qml.GradientDescentOptimizer(stepsize=0.5),
34    device='default.qubit'
35)
36
37# run oo-VQE algorithm with special device and QNode options if needed
38results = qc2data.algorithm.run(
39    device_kwargs={"shots": None},
40    qnode_kwargs={"diff_method": "best"}
41)

where results in both cases correspond to instances of OOVQEResults and VQEResults classes, respectively.