qc2.data

qc2 DATA package.

Submodules

Classes

qc2Data

Main qc2 class.

Package Contents

class qc2.data.qc2Data(filename: str = 'qchem_data.hdf5', molecule: ase.Atoms = Atoms(), algorithm: qc2.algorithms.base.base_algorithm.BaseAlgorithm = BaseAlgorithm(), *, schema: str = 'qcschema')[source]

Main qc2 class.

This class orchestrates classical qchem programs and python libraries for quantum computing.

_schema

Format in which to save qchem data. Options are qcschema or fcidump. Defaults to qcschema.

Type:

str

filename

The path to the HDF5 or fcidump file used to save/read qchem data.

Type:

str

molecule

Attribute representing the molecular structure as an ASE ase.atoms.Atoms instance.

Type:

Atoms

algorithm

Instance of the algorithm to be run. Examples are VQE and oo_VQE.

Type:

BaseAlgorithm

_schema = 'qcschema'
_filename = 'qchem_data.hdf5'
_molecule = None
property molecule: ase.Atoms

Returns the molecule attribute.

Returns:

Molecule as an ASE ase.atoms.Atoms object.

_algorithm = None
property algorithm: qc2.algorithms.base.base_algorithm.BaseAlgorithm

Returns the chosen algorithm.

Returns:

Instance of an algorithm class, e.g., VQE.

_check_filename_extension() None[source]

Ensures that files have proper extensions.

run() None[source]

Runs ASE qchem calculator and saves the data into a formated file.

Returns:

None

Example

>>> from ase.build import molecule
>>> from qc2.ase import DIRAC
>>> from qc2.data import qc2Data
>>>
>>> mol = molecule('H2')
>>>
>>> hdf5_file = 'h2.hdf5'
>>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>>
>>> fcidump_file = 'h2.fcidump'
>>> qc2data = qc2Data(fcidump_file, mol, schema='fcidump')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
read_schema() qiskit_nature.second_q.formats.qcschema.QCSchema | qiskit_nature.second_q.formats.fcidump.FCIDump[source]

Reads and stores data in QCSchema or FCIDump.

Reads and stores the required data from an HDF5 or FCIDump file as either a QCSchema or FCIDump dataclass instance.

Returns:

Instance of QCSchema or FCIDump dataclass.

Return type:

Union[QCSchema, FCIDump]

Notes

See qiskit_nature/second_q/formats for more information on the supported data formats.

Example

>>> from ase.build import molecule
>>> from qc2.ase import DIRAC
>>> from qc2.data import qc2Data
>>>
>>> mol = molecule('H2')
>>>
>>> hdf5_file = 'h2.hdf5'
>>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>> qcschema = qc2data.read_schema()
>>>
>>> fcidump_file = 'h2.fcidump'
>>> qc2data = qc2Data(fcidump_file, mol, schema='fcidump')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>> fcidump = qc2data.read_schema()
process_schema(*, basis: str = 'molecular') qiskit_nature.second_q.problems.ElectronicStructureProblem[source]

Creates an instance of ElectronicStructureProblem.

Reads data using the read_schema() method and converts it into an instance of ElectronicStructureProblem based on the specified schema format (fcidump or qcschema) and electronic basis as defined by qiskit.ElectronicBasis.

Parameters:

basis (str, optional) – The basis in which to construct the ElectronicStructureProblem. Options are atomic or molecular. Defaults to molecular.

Returns:

An instance representing the ElectronicStructureProblem.

Return type:

ElectronicStructureProblem

Notes

  • For fcidump schema, the conversion is done using the fcidump_to_problem function from qiskit_nature/second_q/formats/fcidump_translator.py.

  • For qcschema schema, the conversion is done using the qcschema_to_problem function from qiskit_nature/second_q/formats/qcschema_translator.py.

  • Dipoles are excluded when converting QCSchema data.

Example

>>> from ase.build import molecule
>>> from qc2.ase import DIRAC
>>> from qc2.data import qc2Data
>>>
>>> mol = molecule('H2')
>>>
>>> hdf5_file = 'h2.hdf5'
>>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>> es_problem = qc2data.process_schema(basis='atomic')
>>>
>>> fcidump_file = 'h2.fcidump'
>>> qc2data = qc2Data(fcidump_file, mol, schema='fcidump')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>> es_problem = qc2data.process_schema()
get_active_space_hamiltonian(num_electrons: int | Tuple[int, int], num_spatial_orbitals: int, *, initial_es_problem: qiskit_nature.second_q.problems.ElectronicStructureProblem | None = None) Tuple[qiskit_nature.second_q.problems.ElectronicStructureProblem, float, qiskit_nature.second_q.hamiltonians.ElectronicEnergy][source]

Builds the active-space reduced Hamiltonian.

Parameters:
  • num_electrons (Union[int, Tuple[int, int]]) – The number of active electrons. If a tuple is provided, it represents alpha and beta active electrons.

  • num_spatial_orbitals (int) – The number of spatial orbitals.

  • initial_es_problem (Optional[ElectronicStructureProblem]) – Initial instance of ElectronicStructureProblem. If None, it is instantiated internally. Defaults to None.

Returns:

  • active_space_es_problem (ElectronicStructureProblem): final active space transformed ElectronicStructureProblem.

  • core_energy (float): The core energy, which includes the nuclear repulsion energy and the energy of inactive orbitals.

  • active_space_hamiltonian (ElectronicEnergy): Instance of ElectronicEnergy, the active-space reduced Hamiltonian.

Return type:

Tuple[ElectronicStructureProblem, float, ElectronicEnergy]

Notes

  • The active-space reduced Hamiltonian is obtained by transforming the original electronic structure problem’s Hamiltonian using an ActiveSpaceTransformer.

  • The core energy is computed as the sum of the nuclear repulsion energy and the energy of inactive orbitals.

Example

>>> from ase.build import molecule
>>> from qc2.ase import DIRAC
>>> from qc2.data import qc2Data
>>>
>>> mol = molecule('H2')
>>> hdf5_file = 'h2.hdf5'
>>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>> n_electrons = (1, 1)
>>> n_spatial_orbitals = 2
>>> (es_problem, e_core, ham) = qc2data.get_active_space_hamiltonian(
...     n_electrons, n_spatial_orbitals
... )
get_transformed_hamiltonian(*, initial_es_problem: qiskit_nature.second_q.problems.ElectronicStructureProblem, matrix_transform_a: numpy.ndarray, matrix_transform_b: numpy.ndarray | None = None, initial_basis: str = 'atomic', final_basis: str = 'molecular') Tuple[qiskit_nature.second_q.problems.ElectronicStructureProblem, qiskit_nature.second_q.hamiltonians.ElectronicEnergy][source]

Transforms the Hamiltonian from one basis set to another.

Parameters:
  • initial_es_problem (ElectronicStructureProblem) – The original electronic structure problem.

  • matrix_transform_a (np.ndarray) – The transformation matrix for alpha spin orbitals.

  • matrix_transform_b (np.ndarray, optional) – The transformation matrix for beta spin orbitals.

  • initial_basis (str, optional) – The initial basis set. Defaults to atomic.

  • final_basis (str, optional) – The final basis set to transform to. Defaults to molecular.

Returns:

  • transformed_es_problem (ElectronicStructureProblem): An instance of the transformed ElectronicStructureProblem.

  • transformed_hamiltonian (ElectronicEnergy): An instance of ElectronicEnergy, the transformed Hamiltonian.

Return type:

Tuple[ElectronicStructureProblem, ElectronicEnergy]

Example

>>> from ase.build import molecule
>>> from qc2.ase import PySCF
>>> from qc2.data import qc2Data
>>>
>>> mol = molecule('H2')
>>> hdf5_file = 'h2.hdf5'
>>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema')
>>> qc2data.molecule.calc = PySCF(...)  # => specify qchem calculator
>>> qc2data.run()
>>> ao_es_problem = qc2data.process_schema(basis='atomic')
>>> mo_coeff_a = np.array(
...     [[0.54884228,  1.21245192],
...      [0.54884228, -1.21245192]]
... )
>>> mo_es_problem, hamiltonian = qc2data.get_transformed_hamiltonian(
...     initial_es_problem=ao_es_problem,
...     matrix_transform_a=mo_coeff_a,
...     initial_basis="atomic",
...     final_basis="molecular"
... )
get_fermionic_hamiltonian(num_electrons: int | Tuple[int, int], num_spatial_orbitals: int, *, transform: bool = False, initial_es_problem: qiskit_nature.second_q.problems.ElectronicStructureProblem | None = None, matrix_transform_a: numpy.ndarray | None = None, matrix_transform_b: numpy.ndarray | None = None, initial_basis: str = 'atomic', final_basis: str = 'molecular') Tuple[qiskit_nature.second_q.problems.ElectronicStructureProblem, float, qiskit_nature.second_q.operators.FermionicOp][source]

Builds the fermionic Hamiltonian of a target molecule.

This method constructs the electronic Hamiltonian in 2nd-quantization based on the provided parameters. It can optionally perform a basis set transformation if the transform flag is set to True.

Parameters:
  • num_electrons (Union[int, Tuple[int, int]]) – The number of active electrons. If this is a tuple, it represents the number of alpha- and beta-spin electrons, respectively. If this is a number, it is interpreted as the total number of active electrons, should be even, and implies that the number of alpha and beta electrons equals half of this value, respectively.

  • num_spatial_orbitals (int) – The number of active orbitals.

  • transform (bool, optional) – If True, performs a basis transformation. Defaults to False.

  • initial_es_problem (ElectronicStructureProblem, optional) – The initial electronic structure problem. Required if transform is True. Defaults to None.

  • matrix_transform_a (np.ndarray, optional) – Transformation matrix for alpha spin orbitals. Required if transform is True. Defaults to None.

  • matrix_transform_b (np.ndarray, optional) – Transformation matrix for beta spin orbitals. Required if transform is True. Defaults to None.

  • initial_basis (str, optional) – The initial basis set. Defaults to “atomic”.

  • final_basis (str, optional) – The final basis set to transform to. Defaults to “molecular”.

Returns:

  • core_energy (float): The core energy after active space transformation.

  • es_problem (ElectronicStructureProblem): An instance of the ElectronicStructureProblem.

  • second_q_op (FermionicOp): An instance of FermionicOp representing the fermionic Hamiltonian in 2nd quantization.

Return type:

Tuple[float, ElectronicStructureProblem, FermionicOp]

Raises:

ValueError – If num_electrons or num_spatial_orbitals is None. Or If initial_es_problem is None for transform equal True.

Notes

Based on the qiskit-nature modules: qiskit_nature/second_q/problems/electronic_structure_problem.py qiskit_nature/second_q/transformers/active_space_transformer.py

Example

>>> from ase.build import molecule
>>> from qc2.ase import DIRAC
>>> from qc2.data import qc2Data
>>>
>>> mol = molecule('H2')
>>> hdf5_file = 'h2.hdf5'
>>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>> n_electrons = (1, 1)
>>> n_spatial_orbitals = 2
>>> (es_prob, e_core, op) = qc2data.get_fermionic_hamiltonian(
...     n_electrons, n_spatial_orbitals
... )
get_qubit_hamiltonian(num_electrons: int | Tuple[int, int], num_spatial_orbitals: int, mapper: qiskit_nature.second_q.mappers.QubitMapper = JordanWignerMapper(), *, format: str = 'qiskit', transform: bool = False, initial_es_problem: qiskit_nature.second_q.problems.ElectronicStructureProblem | None = None, matrix_transform_a: numpy.ndarray | None = None, matrix_transform_b: numpy.ndarray | None = None, initial_basis: str = 'atomic', final_basis: str = 'molecular') Tuple[float, qiskit.quantum_info.SparsePauliOp | PennyLaneOperatorType][source]

Generates the qubit Hamiltonian of a target molecule.

This method generates the qubit Hamiltonian representation of a target molecule. It can optionally perform a basis set transformation if the transform flag is True.

Parameters:
  • num_electrons (Union[int, Tuple[int, int]]) – The number of active electrons. If this is a tuple, it represents the number of alpha- and beta-spin electrons, respectively. If this is a number, it is interpreted as the total number of active electrons, should be even, and implies that the number of alpha and beta electrons equals half of this value, respectively.

  • num_spatial_orbitals (int) – The number of active orbitals.

  • mapper (QubitMapper, optional) – The qubit mapping strategy to convert fermionic operators to qubit operators. Defaults to JordanWignerMapper().

  • format (str, optional) – The format in which to return the qubit Hamiltonian. Supported formats are qiskit and pennylane. Defaults to qiskit.

  • transform (bool, optional) – If True, performs a basis transformation. Defaults to False.

  • initial_es_problem (ElectronicStructureProblem, optional) – The initial electronic structure problem. Required if transform is True. Defaults to None.

  • matrix_transform_a (np.ndarray, optional) – Transformation matrix for alpha spin orbitals. Required if transform is True. Defaults to None.

  • matrix_transform_b (np.ndarray, optional) – Transformation matrix for beta spin orbitals. Required if transform is True. Defaults to None.

  • initial_basis (str, optional) – The initial basis set. Defaults to atomic.

  • final_basis (str, optional) – The final basis set to transform to. Defaults to molecular.

Returns:

  • core_energy (float): The core energy after active space transformation.

  • qubit_op (Union[SparsePauliOp, Operator]): If the format is qiskit, it returns a SparsePauliOp representing the qubit Hamiltonian in the qiskit format. If the format is pennylane, it returns a Operator instance representing the qubit Hamiltonian in the PennyLane format.

Return type:

Tuple[float, Union[SparsePauliOp, Operator]]

Raises:

TypeError – If the provided format is not supported (not qiskit nor pennylane).

Example

>>> from ase.build import molecule
>>> from qc2.ase import DIRAC
>>> from qc2.data import qc2Data
>>>
>>> mol = molecule('H2')
>>> hdf5_file = 'h2.hdf5'
>>> qc2data = qc2Data(hdf5_file, mol, schema='qcschema')
>>> qc2data.molecule.calc = DIRAC(...)  # => specify qchem calculator
>>> qc2data.run()
>>> n_electrons = (1, 1)
>>> n_spatial_orbitals = 2
>>> mapper = BravyiKitaevMapper()
>>> (e_core, qubit_op) = qc2data.get_qubit_hamiltonian(
...     n_electrons, n_spatial_orbitals, mapper, format='qiskit'
... )