Skip to content

Commit

Permalink
remove subprocess and popen (#6792)
Browse files Browse the repository at this point in the history
Remake of #6791
Context:
Had subprocess.Popen in the original code and should be removed for
security purposes

Description of the Change:
Changed to use Python standard lib

Benefits:
Security
  • Loading branch information
austingmhuang authored Jan 9, 2025
1 parent b54bc3e commit bfd95b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
9 changes: 6 additions & 3 deletions doc/releases/changelog-0.40.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@
* Added support to build a vibrational Hamiltonian in Taylor form.
[(#6523)](https://github.com/PennyLaneAI/pennylane/pull/6523)

* Added support to build a vibrational Hamiltonian in the Christiansen form.
[(#6560)](https://github.com/PennyLaneAI/pennylane/pull/6560)

<h3>Improvements 🛠</h3>

<h4>QChem improvements</h4>
Expand Down Expand Up @@ -494,6 +491,12 @@ such as `shots`, `rng` and `prng_key`.
for the horizontal Cartan subalgebra instead of `$\mathfrak{h}$`.
[(#6747)](https://github.com/PennyLaneAI/pennylane/pull/6747)

<h4>Construct vibrational Hamiltonians 🫨</h4>

* Added support to build a vibrational Hamiltonian in the Christiansen form.
[(#6560)](https://github.com/PennyLaneAI/pennylane/pull/6560)
[(#6792)](https://github.com/PennyLaneAI/pennylane/pull/6792)

<h3>Breaking changes 💔</h3>

* The default graph coloring method of `qml.dot`, `qml.sum`, and `qml.pauli.optimize_measurements` for grouping observables was changed
Expand Down
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 bfd95b4

Please sign in to comment.