Building up the molecular Hamiltonian
Note
If you aim to take full advantage of qc2 and utilize its suite of algorithm classes,
you do not need to worry about all the specifics mentioned in this section.
When instantiating VQEBASE and its child classes,
most of the qc2Data` methods discussed below are executed automatically
through _init_qubit_hamiltonian()
In addition to run(), qc2Data
provides a suite of methods capable of directly reading
from the QCSchema or FCIdump data files
and use such info to construct the molecular qubit Hamiltonian. These are get_transformed_hamiltonian(), get_active_space_hamiltonian(),
get_fermionic_hamiltonian() and get_qubit_hamiltonian().
Of particular relevance is the get_qubit_hamiltonian() method.
Internally, this method takes the active-space electronic Hamiltonian in second quantization,
which is constructed by get_fermionic_hamiltonian(),
and applies an appropriate fermion-to-qubit mapping to it,
such as the Jordan-Wigner or Bravyi-Kitaev transformations [TCC+22].
Below, you’ll find an example of how to set up and build the molecular Hamiltonian for H2
from a QCSchema formatted hdf5 file obtained using qc2-ASE DIRAC.
1from ase.build import molecule
2from qiskit_nature.second_q.mappers import JordanWignerMapper
3from qc2.ase import DIRAC
4from qc2.data import qc2Data
5
6# set ASE Atoms object
7mol = molecule('H2')
8
9# instantiate qc2Data class
10qc2data = qc2Data(
11 molecule=mol,
12 filename='h2.hdf5',
13 schema='qcschema'
14)
15
16# attach a DIRAC qc2-ASE calculator
17qc2data.molecule.calc = DIRAC()
18
19# run calculator
20qc2data.run()
21
22# set up qubit Hamiltonian and core energy based on given activate space
23e_core, qubit_op = qc2data.get_qubit_hamiltonian(
24 num_electrons=(1, 1),
25 num_spatial_orbitals=2,
26 mapper=JordanWignerMapper(),
27 format='qiskit'
28)
Here, qubit_op is a Qiskit-formatted SparsePauliOp operator, which can be directly used in subsequent hybrid classical-quantum calculations
with Qiskit Nature. If the format parameter is set to 'pennylane', then qubit_op is formatted
as a Pennylane Operator and should be used accordingly. Also, it’s worth noting that we have used the JordanWignerMapper() from Qiskit Nature,
a library that currently serves as the basis for all qc2Data methods.