forked from qiskit-community/qiskit-aqua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussian_utils.py
84 lines (66 loc) · 2.42 KB
/
gaussian_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# 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
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
""" Gaussian Utility Methods """
import logging
from subprocess import Popen, PIPE
from shutil import which
from qiskit.chemistry import QiskitChemistryError
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"""
if _G16PROG is None:
raise QiskitChemistryError(
"Could not locate {} executable '{}'. Please check that it is installed correctly."
.format(_GAUSSIAN_16_DESC, _GAUSSIAN_16))
def run_g16(cfg: str) -> str:
"""
Runs Gaussian 16. We capture stdout and if error log the last 10 lines that
should include the error description from Gaussian.
Args:
cfg: configuration
Returns:
Text log output
Raises:
QiskitChemistryError: Failed run or log not captured.
"""
process = None
try:
process = Popen(_GAUSSIAN_16, stdin=PIPE, stdout=PIPE, universal_newlines=True)
stdout, _ = process.communicate(cfg)
process.wait()
except Exception as ex:
if process is not None:
process.kill()
raise QiskitChemistryError('{} run has failed'.format(_GAUSSIAN_16_DESC)) from ex
if process.returncode != 0:
errmsg = ""
if stdout is not None:
lines = stdout.splitlines()
start = 0
if len(lines) > 10:
start = len(lines) - 10
for i in range(start, len(lines)):
logger.error(lines[i])
errmsg += lines[i] + "\n"
raise QiskitChemistryError(
'{} process return code {}\n{}'.format(
_GAUSSIAN_16_DESC, process.returncode, errmsg))
all_text = ""
if stdout is not None:
lines = stdout.splitlines()
for line in lines:
all_text += line + "\n"
logger.debug("Gaussian output:\n%s", all_text)
return all_text