Skip to content

Commit b6adbf1

Browse files
authored
Merge pull request #50 from LLNL/python-windows-fixes
Minor windows fixes to quandary.py
2 parents 95cc41f + 6f9a60b commit b6adbf1

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

quandary.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, copy
1+
import os, os.path, copy
22
import numpy as np
33
from subprocess import run, PIPE, Popen, call
44
import matplotlib.pyplot as plt
@@ -502,12 +502,12 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
502502
# If given, write the target gate to file
503503
if len(self.targetgate) > 0:
504504
gate_vectorized = np.concatenate((np.real(self.targetgate).ravel(order='F'), np.imag(self.targetgate).ravel(order='F')))
505-
self._gatefilename = "./targetgate.dat"
506-
with open(datadir+"/"+self._gatefilename, "w") as f:
505+
self._gatefilename = "targetgate.dat"
506+
with open(os.path.join(datadir, self._gatefilename), "w", newline='\n') as f:
507507
for value in gate_vectorized:
508508
f.write("{:20.13e}\n".format(value))
509509
if self.verbose:
510-
print("Target gate written to ", datadir+"/"+self._gatefilename)
510+
print("Target gate written to ", os.path.join(datadir, self._gatefilename))
511511

512512
# If given, write the target state to file
513513
if len(self.targetstate) > 0:
@@ -516,12 +516,12 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
516516
else:
517517
state = self.targetstate
518518
vectorized = np.concatenate((np.real(state).ravel(order='F'), np.imag(state).ravel(order='F')))
519-
self._gatefilename = "./targetstate.dat"
520-
with open(datadir+"/"+self._gatefilename, "w") as f:
519+
self._gatefilename = "targetstate.dat"
520+
with open(os.path.join(datadir, self._gatefilename), "w", newline='\n') as f:
521521
for value in vectorized:
522522
f.write("{:20.13e}\n".format(value))
523523
if self.verbose:
524-
print("Target state written to ", datadir+"/"+self._gatefilename)
524+
print("Target state written to ", os.path.join(datadir, self._gatefilename))
525525

526526
# If given, write the initial state to file
527527
if self.initialcondition[0:4]=="file":
@@ -530,19 +530,19 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
530530
else:
531531
state = self._initialstate
532532
vectorized = np.concatenate((np.real(state).ravel(order='F'), np.imag(state).ravel(order='F')))
533-
self._initstatefilename = "./initialstate.dat"
534-
with open(datadir+"/"+self._initstatefilename, "w") as f:
533+
self._initstatefilename = "initialstate.dat"
534+
with open(os.path.join(datadir, self._initstatefilename), "w", newline='\n') as f:
535535
for value in vectorized:
536536
f.write("{:20.13e}\n".format(value))
537537
if self.verbose:
538-
print("Initial state written to ", datadir+"/"+self._initstatefilename)
538+
print("Initial state written to ", os.path.join(datadir, self._initstatefilename))
539539

540540

541541
# If not standard Hamiltonian model, write provided Hamiltonians to a file
542542
if not self.standardmodel:
543543
# Write non-standard Hamiltonians to file
544-
self._hamiltonian_filename= "./hamiltonian.dat"
545-
with open(datadir+"/" + self._hamiltonian_filename, "w") as f:
544+
self._hamiltonian_filename= "hamiltonian.dat"
545+
with open(os.path.join(datadir, self._hamiltonian_filename), "w", newline='\n') as f:
546546
f.write("# Hsys \n")
547547
Hsyslist = list(np.array(self.Hsys).flatten(order='F'))
548548
for value in Hsyslist:
@@ -552,20 +552,20 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
552552
for iosc in range(len(self.Ne)):
553553
# Real part, if given
554554
if len(self.Hc_re)>iosc and len(self.Hc_re[iosc])>0:
555-
with open(datadir+"/" + self._hamiltonian_filename, "a") as f:
555+
with open(os.path.join(datadir, self._hamiltonian_filename), "a", newline='\n') as f:
556556
Hcrelist = list(np.array(self.Hc_re[iosc]).flatten(order='F'))
557557
f.write("# Oscillator {:d} Hc_real \n".format(iosc))
558558
for value in Hcrelist:
559559
f.write("{:20.13e}\n".format(value))
560560
# Imaginary part, if given
561561
if len(self.Hc_im)>iosc and len(self.Hc_im[iosc])>0:
562-
with open(datadir+"/" + self._hamiltonian_filename, "a") as f:
562+
with open(os.path.join(datadir, self._hamiltonian_filename), "a", newline='\n') as f:
563563
Hcimlist = list(np.array(self.Hc_im[iosc]).flatten(order='F'))
564564
f.write("# Oscillator {:d} Hc_imag \n".format(iosc))
565565
for value in Hcimlist:
566566
f.write("{:20.13e}\n".format(value))
567567
if self.verbose:
568-
print("Hamiltonian operators written to ", datadir+"/"+self._hamiltonian_filename)
568+
print("Hamiltonian operators written to ", os.path.join(datadir, self._hamiltonian_filename))
569569

570570
# Initializing the control parameter vector 'pcof0'
571571
# 1. If the initial parameter vector (list) is given with the 'pcof0' argument, the list will be dumped to a file with name self.pcof0_filename := "pcof0.dat".
@@ -578,12 +578,12 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
578578
if len(pcof0) > 0: # pcof0 is an argument to __dump(), while self.pcof0 is stored in the object
579579
writeme = pcof0
580580
if len(writeme)>0:
581-
self.pcof0_filename = "./pcof0.dat"
582-
with open(datadir+"/"+self.pcof0_filename, "w") as f:
581+
self.pcof0_filename = "pcof0.dat"
582+
with open(os.path.join(datadir, self.pcof0_filename), "w", newline='\n') as f:
583583
for value in writeme:
584584
f.write("{:20.13e}\n".format(value))
585585
if self.verbose:
586-
print("Initial control parameters written to ", datadir+"/"+self.pcof0_filename)
586+
print("Initial control parameters written to ", os.path.join(datadir, self.pcof0_filename))
587587
read_pcof0_from_file = True
588588
elif len(self.pcof0_filename) > 0:
589589
print("Using the provided filename '", self.pcof0_filename, "' in the control_initialization command")
@@ -698,8 +698,8 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
698698
mystring += "rand_seed = "+str(int(self.rand_seed))+ "\n"
699699

700700
# Write the file
701-
outpath = datadir+"/config.cfg"
702-
with open(outpath, "w") as file:
701+
outpath = os.path.join(datadir, "config.cfg")
702+
with open(outpath, "w", newline='\n') as file:
703703
file.write(mystring)
704704

705705
if self.verbose:
@@ -726,10 +726,8 @@ def get_results(self, *, datadir="./", ignore_failure=False):
726726
population : Evolution of the population of each oscillator, of each initial condition. (expectedEnergy[oscillator][initialcondition])
727727
"""
728728

729-
dataout_dir = datadir + "/"
730-
731729
# Get control parameters
732-
filename = dataout_dir + "/params.dat"
730+
filename = os.path.join(datadir, "params.dat")
733731
try:
734732
pcof = np.loadtxt(filename).astype(float)
735733
except:
@@ -738,7 +736,7 @@ def get_results(self, *, datadir="./", ignore_failure=False):
738736
pcof=[]
739737

740738
# Get optimization history information
741-
filename = dataout_dir + "/optim_history.dat"
739+
filename = os.path.join(datadir, "optim_history.dat")
742740
try:
743741
optim_hist_tmp = np.loadtxt(filename)
744742
except:
@@ -774,7 +772,7 @@ def get_results(self, *, datadir="./", ignore_failure=False):
774772
for iosc in range(len(self.Ne)):
775773
for iinit in range(ninits):
776774
iid = iinit if not self._lindblad_solver else iinit*ninits + iinit
777-
filename = dataout_dir + "./expected"+str(iosc)+".iinit"+str(iid).zfill(4)+".dat"
775+
filename = os.path.join(datadir, f"expected{iosc}.iinit{str(iid).zfill(4)}.dat")
778776
try:
779777
x = np.loadtxt(filename)
780778
expectedEnergy[iosc].append(x[:,1]) # 0th column is time, second column is expected energy
@@ -787,7 +785,7 @@ def get_results(self, *, datadir="./", ignore_failure=False):
787785
for iosc in range(len(self.Ne)):
788786
for iinit in range(ninits):
789787
iid = iinit if not self._lindblad_solver else iinit*ninits + iinit
790-
filename = dataout_dir + "./population"+str(iosc)+".iinit"+str(iid).zfill(4)+".dat"
788+
filename = os.path.join(datadir, f"population{iosc}.iinit{str(iid).zfill(4)}.dat")
791789
try:
792790
x = np.loadtxt(filename)
793791
population[iosc].append(x[:,1:].transpose()) # first column is time
@@ -802,17 +800,17 @@ def get_results(self, *, datadir="./", ignore_failure=False):
802800
for iinit in range(self._ninit):
803801
file_index = str(iinit).zfill(4)
804802
try:
805-
xre = np.loadtxt(f"{dataout_dir}/rho_Re.iinit{file_index}.dat", skiprows=1, usecols=range(1, ndim+1))[-1]
803+
xre = np.loadtxt(os.path.join(datadir, f"rho_Re.iinit{file_index}.dat"), skiprows=1, usecols=range(1, ndim+1))[-1]
806804
uT[:, iinit] = xre
807805
except:
808-
name = dataout_dir+"/rho_Re.iinit"+str(file_index)+".dat"
806+
name = os.path.join(datadir, f"rho_Re.iinit{file_index}.dat")
809807
if not ignore_failure:
810808
print("Can't read from ", name)
811809
try:
812-
xim = np.loadtxt(f"{dataout_dir}/rho_Im.iinit{file_index}.dat", skiprows=1, usecols=range(1, ndim+1))[-1]
810+
xim = np.loadtxt(os.path.join(datadir, f"rho_Im.iinit{file_index}.dat"), skiprows=1, usecols=range(1, ndim+1))[-1]
813811
uT[:, iinit] += 1j * xim
814812
except:
815-
name = dataout_dir+"/rho_Im.iinit"+str(file_index)+".dat"
813+
name = os.path.join(datadir, f"rho_Im.iinit{file_index}.dat")
816814
if not ignore_failure:
817815
print("Can't read from ", name)
818816
# uT[:, iinit] = xre + 1j * xim
@@ -823,7 +821,7 @@ def get_results(self, *, datadir="./", ignore_failure=False):
823821
ft = []
824822
for iosc in range(len(self.Ne)):
825823
# Read the control pulse file
826-
filename = dataout_dir + "./control"+str(iosc)+".dat"
824+
filename = os.path.join(datadir, f"control{iosc}.dat")
827825
try:
828826
x = np.loadtxt(filename)
829827
except:

0 commit comments

Comments
 (0)