Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
# (C) Copyright IBM 2021, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -81,7 +81,8 @@ def driver_class_from_type(
raise MissingOptionalLibraryError(
libname=driver_type, name="ElectronicStructureDriverType"
)
Comment thread
mrossinek marked this conversation as resolved.
driver_class.check_installed()
# instantiating the object will check if the driver is installed
_ = driver_class()
Comment thread
mrossinek marked this conversation as resolved.
driver_class.check_method_supported(method)

logger.debug("%s found from type %s.", driver_class.__name__, driver_type.value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2021.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -18,10 +18,10 @@
from qiskit_nature.properties.second_quantization.vibrational import (
VibrationalStructureDriverResult,
)
import qiskit_nature.optionals as _optionals
from ...units_type import UnitsType
from ..vibrational_structure_driver import VibrationalStructureDriver
from ...molecule import Molecule
from .gaussian_utils import check_valid
from .gaussian_log_driver import GaussianLogDriver
from .gaussian_log_result import GaussianLogResult

Expand Down Expand Up @@ -75,9 +75,10 @@ def __init__(
# If running from a jcf we need Gaussian™ 16 so check if we have a
# valid install.
if self._logfile is None:
check_valid()
_optionals.HAS_GAUSSIAN.require_now("GaussianForcesDriver __init__")

@staticmethod
@_optionals.HAS_GAUSSIAN.require_in_call
def from_molecule(
molecule: Molecule,
basis: str = "sto-3g",
Expand All @@ -98,7 +99,6 @@ def from_molecule(
"""
# Ignore kwargs parameter for this driver
del driver_kwargs
GaussianForcesDriver.check_installed()
basis = GaussianForcesDriver.to_driver_basis(basis)

if molecule.units == UnitsType.ANGSTROM:
Expand Down Expand Up @@ -130,16 +130,6 @@ def to_driver_basis(basis: str) -> str:
return "sto-3g"
return basis

@staticmethod
def check_installed() -> None:
"""
Checks if Gaussian is installed and available

Raises:
MissingOptionalLibraryError: if not installed.
"""
check_valid()

def run(self) -> VibrationalStructureDriverResult:
if self._logfile is not None:
glr = GaussianLogResult(self._logfile)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2021.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -16,14 +16,16 @@
import logging

from qiskit_nature import QiskitNatureError
import qiskit_nature.optionals as _optionals

from ..base_driver import BaseDriver
from .gaussian_utils import check_valid, run_g16
from .gaussian_utils import run_g16
from .gaussian_log_result import GaussianLogResult

logger = logging.getLogger(__name__)


@_optionals.HAS_GAUSSIAN.require_in_instance
class GaussianLogDriver(BaseDriver):
"""Gaussian™ 16 log driver.

Expand All @@ -47,8 +49,6 @@ def __init__(self, jcf: Union[str, List[str]]) -> None:
Raises:
QiskitNatureError: Invalid Input
"""
GaussianLogDriver.check_installed()

if not isinstance(jcf, list) and not isinstance(jcf, str):
raise QiskitNatureError(f"Invalid input for Gaussian Log Driver '{jcf}'")

Expand All @@ -58,16 +58,6 @@ def __init__(self, jcf: Union[str, List[str]]) -> None:
self._jcf = jcf
super().__init__()

@staticmethod
def check_installed() -> None:
"""
Checks if Gaussian is installed and available

Raises:
MissingOptionalLibraryError: if not installed.
"""
check_valid()

def run(self) -> GaussianLogResult: # type: ignore
"""Runs the driver to produce a result given the supplied job control file.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2021.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -14,32 +14,11 @@

import logging
from subprocess import Popen, PIPE
from shutil import which
from qiskit.exceptions import MissingOptionalLibraryError
from qiskit_nature import QiskitNatureError
import qiskit_nature.optionals as _optionals

logger = logging.getLogger(__name__)

_GAUSSIAN_16 = "g16"
_GAUSSIAN_16_DESC = "Gaussian 16"

_G16PROG = which(_GAUSSIAN_16)


def check_valid() -> None:
"""
Checks if Gaussian is installed and available

Raises:
MissingOptionalLibraryError: if not installed.
"""
if _G16PROG is None:
raise MissingOptionalLibraryError(
libname=_GAUSSIAN_16,
name=_GAUSSIAN_16_DESC,
msg="Please check that it is installed correctly",
)


def run_g16(cfg: str) -> str:
"""
Expand All @@ -58,14 +37,16 @@ def run_g16(cfg: str) -> str:
"""
process = None
try:
with Popen(_GAUSSIAN_16, stdin=PIPE, stdout=PIPE, universal_newlines=True) as process:
with Popen(
_optionals.GAUSSIAN_16, stdin=PIPE, stdout=PIPE, universal_newlines=True
) as process:
stdout, _ = process.communicate(cfg)
process.wait()
except Exception as ex:
if process is not None:
process.kill()

raise QiskitNatureError(f"{_GAUSSIAN_16_DESC} run has failed") from ex
raise QiskitNatureError(f"{_optionals.GAUSSIAN_16_DESC} run has failed") from ex

if process.returncode != 0:
errmsg = ""
Expand All @@ -78,7 +59,7 @@ def run_g16(cfg: str) -> str:
logger.error(lines[i])
errmsg += lines[i] + "\n"
raise QiskitNatureError(
f"{_GAUSSIAN_16_DESC} process return code {process.returncode}\n{errmsg}"
f"{_optionals.GAUSSIAN_16_DESC} process return code {process.returncode}\n{errmsg}"
)

all_text = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
from qiskit_nature import QiskitNatureError
from qiskit_nature.constants import PERIODIC_TABLE
from qiskit_nature.properties.second_quantization.electronic import ElectronicStructureDriverResult
import qiskit_nature.optionals as _optionals

from ...qmolecule import QMolecule
from .gaussian_utils import check_valid, run_g16
from .gaussian_utils import run_g16
from ..electronic_structure_driver import ElectronicStructureDriver, MethodType
from ...molecule import Molecule
from ...units_type import UnitsType
Expand All @@ -36,6 +37,7 @@
logger = logging.getLogger(__name__)


@_optionals.HAS_GAUSSIAN.require_in_instance
class GaussianDriver(ElectronicStructureDriver):
"""
Qiskit Nature driver using the Gaussian™ 16 program.
Expand All @@ -62,7 +64,6 @@ def __init__(
QiskitNatureError: Invalid Input
"""
super().__init__()
GaussianDriver.check_installed()
if not isinstance(config, str) and not isinstance(config, list):
raise QiskitNatureError(f"Invalid config for Gaussian Driver '{config}'")

Expand All @@ -72,6 +73,7 @@ def __init__(
self._config = config

@staticmethod
@_optionals.HAS_GAUSSIAN.require_in_call
def from_molecule(
molecule: Molecule,
basis: str = "sto-3g",
Expand All @@ -91,7 +93,6 @@ def from_molecule(
"""
# Ignore kwargs parameter for this driver
del driver_kwargs
GaussianDriver.check_installed()
GaussianDriver.check_method_supported(method)
basis = GaussianDriver.to_driver_basis(basis)

Expand Down Expand Up @@ -124,16 +125,6 @@ def to_driver_basis(basis: str) -> str:
return "sto-3g"
return basis

@staticmethod
def check_installed() -> None:
"""
Checks if Gaussian is installed and available

Raises:
MissingOptionalLibraryError: if not installed.
"""
check_valid()

@staticmethod
def check_method_supported(method: MethodType) -> None:
"""
Expand Down
32 changes: 9 additions & 23 deletions qiskit_nature/drivers/second_quantization/psi4d/psi4driver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2021.
# (C) Copyright IBM 2018, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -19,12 +19,11 @@
import tempfile
import warnings
from pathlib import Path
from shutil import which
from typing import Union, List, Optional, Any, Dict

from qiskit.exceptions import MissingOptionalLibraryError
from qiskit_nature import QiskitNatureError
from qiskit_nature.properties.second_quantization.electronic import ElectronicStructureDriverResult
import qiskit_nature.optionals as _optionals

from ...qmolecule import QMolecule
from ..electronic_structure_driver import ElectronicStructureDriver, MethodType
Expand All @@ -34,11 +33,8 @@

logger = logging.getLogger(__name__)

PSI4 = "psi4"

PSI4_APP = which(PSI4)


@_optionals.HAS_PSI4.require_in_instance
class PSI4Driver(ElectronicStructureDriver):
"""
Qiskit Nature driver using the PSI4 program.
Expand All @@ -61,7 +57,6 @@ def __init__(
QiskitNatureError: Invalid Input
"""
super().__init__()
PSI4Driver.check_installed()
if not isinstance(config, str) and not isinstance(config, list):
raise QiskitNatureError(f"Invalid config for PSI4 Driver '{config}'")

Expand All @@ -71,6 +66,7 @@ def __init__(
self._config = config

@staticmethod
@_optionals.HAS_PSI4.require_in_call
def from_molecule(
molecule: Molecule,
basis: str = "sto3g",
Expand All @@ -91,7 +87,6 @@ def from_molecule(
"""
# Ignore kwargs parameter for this driver
del driver_kwargs
PSI4Driver.check_installed()
PSI4Driver.check_method_supported(method)
basis = PSI4Driver.to_driver_basis(basis)

Expand Down Expand Up @@ -124,17 +119,6 @@ def to_driver_basis(basis: str) -> str:
return "sto-3g"
return basis

@staticmethod
def check_installed() -> None:
"""
Checks if PSI4 is installed and available

Raises:
MissingOptionalLibraryError: if not installed.
"""
if PSI4_APP is None:
raise MissingOptionalLibraryError(libname="PSI4", name="PSI4Driver")

@staticmethod
def check_method_supported(method: MethodType) -> None:
"""
Expand Down Expand Up @@ -228,7 +212,7 @@ def _run_psi4(input_file, output_file):
process = None
try:
with subprocess.Popen(
[PSI4, input_file, output_file],
[_optionals.PSI4, input_file, output_file],
stdout=subprocess.PIPE,
universal_newlines=True,
) as process:
Expand All @@ -238,7 +222,7 @@ def _run_psi4(input_file, output_file):
if process is not None:
process.kill()

raise QiskitNatureError(f"{PSI4} run has failed") from ex
raise QiskitNatureError(f"{_optionals.PSI4} run has failed") from ex

if process.returncode != 0:
errmsg = ""
Expand All @@ -247,4 +231,6 @@ def _run_psi4(input_file, output_file):
for i, _ in enumerate(lines):
logger.error(lines[i])
errmsg += lines[i] + "\n"
raise QiskitNatureError(f"{PSI4} process return code {process.returncode}\n{errmsg}")
raise QiskitNatureError(
f"{_optionals.PSI4} process return code {process.returncode}\n{errmsg}"
)
Loading