Skip to content

Commit

Permalink
remove subprocess and popen
Browse files Browse the repository at this point in the history
  • Loading branch information
austingmhuang committed Jan 9, 2025
1 parent b54bc3e commit 71c5107
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions pennylane/labs/vibrational/christiansen_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
"""Utility functions related to the construction of the taylor form Hamiltonian."""
import itertools
import subprocess
from pathlib import Path

import h5py
import numpy as np
Expand Down Expand Up @@ -516,7 +516,7 @@ def _load_cform_onemode(num_proc, nmodes, quad_order):
for rank in range(num_proc):
f = h5py.File("cform_H1data" + f"_{rank}" + ".hdf5", "r+")
local_ham_cform_onebody = f["H1"][()]
chunk = np.array_split(local_ham_cform_onebody, nmode_combos)[mode_combo] #
chunk = np.array_split(local_ham_cform_onebody, nmode_combos)[mode_combo]
l1 += len(chunk)
local_chunk[l0:l1] = chunk
l0 += len(chunk)
Expand Down Expand Up @@ -767,17 +767,16 @@ def christiansen_integrals(pes, n_states=16, cubic=False):
local_ham_cform_onebody = _cform_onemode(pes, n_states)
comm.Barrier()

f = h5py.File("cform_H1data" + f"_{rank}" + ".hdf5", "w")
f.create_dataset("H1", data=local_ham_cform_onebody)
f.close()
file_path = Path(f"cform_H1data_{rank}.hdf5")
with h5py.File(file_path, "w") as f:
f.create_dataset("H1", data=local_ham_cform_onebody)
comm.Barrier()

ham_cform_onebody = None
if rank == 0:
ham_cform_onebody = _load_cform_onemode(size, len(pes.freqs), n_states)
process = subprocess.Popen("rm " + "cform_H1data*", stdout=subprocess.PIPE, shell=True)
_, _ = process.communicate()

for path in Path.cwd().glob("cform_H1data*"):
path.unlink()
comm.Barrier()
ham_cform_onebody = comm.bcast(ham_cform_onebody, root=0)

Expand All @@ -786,34 +785,32 @@ def christiansen_integrals(pes, n_states=16, cubic=False):
local_ham_cform_twobody += _cform_twomode_kinetic(pes, n_states)
comm.Barrier()

f = h5py.File("cform_H2data" + f"_{rank}" + ".hdf5", "w")
f.create_dataset("H2", data=local_ham_cform_twobody)
f.close()
file_path = Path(f"cform_H2data_{rank}.hdf5")
with h5py.File(file_path, "w") as f:
f.create_dataset("H2", data=local_ham_cform_twobody)
comm.Barrier()

ham_cform_twobody = None
if rank == 0:
ham_cform_twobody = _load_cform_twomode(size, len(pes.freqs), n_states)
process = subprocess.Popen("rm " + "cform_H2data*", stdout=subprocess.PIPE, shell=True)
_, _ = process.communicate()

for path in Path.cwd().glob("cform_H2data*"):
path.unlink()
comm.Barrier()
ham_cform_twobody = comm.bcast(ham_cform_twobody, root=0)

if cubic:
local_ham_cform_threebody = _cform_threemode(pes, n_states)

f = h5py.File("cform_H3data" + f"_{rank}" + ".hdf5", "w")
f.create_dataset("H3", data=local_ham_cform_threebody)
f.close()
file_path = Path(f"cform_H3data_{rank}.hdf5")
with h5py.File(file_path, "w") as f:
f.create_dataset("H3", data=local_ham_cform_threebody)
comm.Barrier()

ham_cform_threebody = None
if rank == 0:
ham_cform_threebody = _load_cform_threemode(size, len(pes.freqs), n_states)
process = subprocess.Popen("rm " + "cform_H3data*", stdout=subprocess.PIPE, shell=True)
_, _ = process.communicate()

for path in Path.cwd().glob("cform_H3data*"):
path.unlink()
comm.Barrier()
ham_cform_threebody = comm.bcast(ham_cform_threebody, root=0)

Expand All @@ -838,52 +835,51 @@ def christiansen_integrals_dipole(pes, n_states=16):
local_dipole_cform_onebody = _cform_onemode_dipole(pes, n_states)
comm.Barrier()

f = h5py.File("cform_D1data" + f"_{rank}" + ".hdf5", "w")
f.create_dataset("D1", data=local_dipole_cform_onebody)
f.close()
file_path = Path(f"cform_D1data_{rank}.hdf5")
with h5py.File(file_path, "w") as f:
f.create_dataset("D1", data=local_dipole_cform_onebody)
comm.Barrier()

dipole_cform_onebody = None
if rank == 0:
dipole_cform_onebody = _load_cform_onemode_dipole(size, len(pes.freqs), n_states)
process = subprocess.Popen("rm " + "cform_D1data*", stdout=subprocess.PIPE, shell=True)
_, _ = process.communicate()

for path in Path.cwd().glob("cform_D1data*"):
path.unlink()
comm.Barrier()
dipole_cform_onebody = comm.bcast(dipole_cform_onebody, root=0)

if pes.localized is True or pes.dipole_level > 1:
local_dipole_cform_twobody = _cform_twomode_dipole(pes, n_states)
comm.Barrier()

f = h5py.File("cform_D2data" + f"_{rank}" + ".hdf5", "w")
f.create_dataset("D2", data=local_dipole_cform_twobody)
f.close()
file_path = Path(f"cform_D2data_{rank}.hdf5")
with h5py.File(file_path, "w") as f:
f.create_dataset("D2", data=local_dipole_cform_twobody)
comm.Barrier()

dipole_cform_twobody = None
if rank == 0:
dipole_cform_twobody = _load_cform_twomode_dipole(size, len(pes.freqs), n_states)
process = subprocess.Popen("rm " + "cform_D2data*", stdout=subprocess.PIPE, shell=True)
_, _ = process.communicate()
for path in Path.cwd().glob("cform_D2data*"):
path.unlink()
comm.Barrier()
dipole_cform_twobody = comm.bcast(dipole_cform_twobody, root=0)

if pes.localized is True or pes.dipole_level > 2:
local_dipole_cform_threebody = _cform_threemode_dipole(pes, n_states)
comm.Barrier()

f = h5py.File("cform_D3data" + f"_{rank}" + ".hdf5", "w")
f.create_dataset("D3", data=local_dipole_cform_threebody)
f.close()
file_path = Path(f"cform_D3data_{rank}.hdf5")
with h5py.File(file_path, "w") as f:
f.create_dataset("D3", data=local_dipole_cform_threebody)
comm.Barrier()

dipole_cform_threebody = None
if rank == 0:
dipole_cform_threebody = _load_cform_threemode_dipole(size, len(pes.freqs), n_states)
process = subprocess.Popen("rm " + "cform_D3data*", stdout=subprocess.PIPE, shell=True)
_, _ = process.communicate()
for path in Path.cwd().glob("cform_D3data*"):
path.unlink()
comm.Barrier()

dipole_cform_threebody = comm.bcast(dipole_cform_threebody, root=0)

D_arr = [dipole_cform_onebody, dipole_cform_twobody, dipole_cform_threebody]
Expand Down

0 comments on commit 71c5107

Please sign in to comment.