1
- import os , copy
1
+ import os , os . path , copy
2
2
import numpy as np
3
3
from subprocess import run , PIPE , Popen , call
4
4
import matplotlib .pyplot as plt
@@ -502,12 +502,12 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
502
502
# If given, write the target gate to file
503
503
if len (self .targetgate ) > 0 :
504
504
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 :
507
507
for value in gate_vectorized :
508
508
f .write ("{:20.13e}\n " .format (value ))
509
509
if self .verbose :
510
- print ("Target gate written to " , datadir + "/" + self ._gatefilename )
510
+ print ("Target gate written to " , os . path . join ( datadir , self ._gatefilename ) )
511
511
512
512
# If given, write the target state to file
513
513
if len (self .targetstate ) > 0 :
@@ -516,12 +516,12 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
516
516
else :
517
517
state = self .targetstate
518
518
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 :
521
521
for value in vectorized :
522
522
f .write ("{:20.13e}\n " .format (value ))
523
523
if self .verbose :
524
- print ("Target state written to " , datadir + "/" + self ._gatefilename )
524
+ print ("Target state written to " , os . path . join ( datadir , self ._gatefilename ) )
525
525
526
526
# If given, write the initial state to file
527
527
if self .initialcondition [0 :4 ]== "file" :
@@ -530,19 +530,19 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
530
530
else :
531
531
state = self ._initialstate
532
532
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 :
535
535
for value in vectorized :
536
536
f .write ("{:20.13e}\n " .format (value ))
537
537
if self .verbose :
538
- print ("Initial state written to " , datadir + "/" + self ._initstatefilename )
538
+ print ("Initial state written to " , os . path . join ( datadir , self ._initstatefilename ) )
539
539
540
540
541
541
# If not standard Hamiltonian model, write provided Hamiltonians to a file
542
542
if not self .standardmodel :
543
543
# 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 :
546
546
f .write ("# Hsys \n " )
547
547
Hsyslist = list (np .array (self .Hsys ).flatten (order = 'F' ))
548
548
for value in Hsyslist :
@@ -552,20 +552,20 @@ def __dump(self, *, pcof0=[], runtype="simulation", datadir="./run_dir"):
552
552
for iosc in range (len (self .Ne )):
553
553
# Real part, if given
554
554
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 :
556
556
Hcrelist = list (np .array (self .Hc_re [iosc ]).flatten (order = 'F' ))
557
557
f .write ("# Oscillator {:d} Hc_real \n " .format (iosc ))
558
558
for value in Hcrelist :
559
559
f .write ("{:20.13e}\n " .format (value ))
560
560
# Imaginary part, if given
561
561
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 :
563
563
Hcimlist = list (np .array (self .Hc_im [iosc ]).flatten (order = 'F' ))
564
564
f .write ("# Oscillator {:d} Hc_imag \n " .format (iosc ))
565
565
for value in Hcimlist :
566
566
f .write ("{:20.13e}\n " .format (value ))
567
567
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 ) )
569
569
570
570
# Initializing the control parameter vector 'pcof0'
571
571
# 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"):
578
578
if len (pcof0 ) > 0 : # pcof0 is an argument to __dump(), while self.pcof0 is stored in the object
579
579
writeme = pcof0
580
580
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 :
583
583
for value in writeme :
584
584
f .write ("{:20.13e}\n " .format (value ))
585
585
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 ) )
587
587
read_pcof0_from_file = True
588
588
elif len (self .pcof0_filename ) > 0 :
589
589
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"):
698
698
mystring += "rand_seed = " + str (int (self .rand_seed ))+ "\n "
699
699
700
700
# 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 :
703
703
file .write (mystring )
704
704
705
705
if self .verbose :
@@ -726,10 +726,8 @@ def get_results(self, *, datadir="./", ignore_failure=False):
726
726
population : Evolution of the population of each oscillator, of each initial condition. (expectedEnergy[oscillator][initialcondition])
727
727
"""
728
728
729
- dataout_dir = datadir + "/"
730
-
731
729
# Get control parameters
732
- filename = dataout_dir + "/ params.dat"
730
+ filename = os . path . join ( datadir , " params.dat")
733
731
try :
734
732
pcof = np .loadtxt (filename ).astype (float )
735
733
except :
@@ -738,7 +736,7 @@ def get_results(self, *, datadir="./", ignore_failure=False):
738
736
pcof = []
739
737
740
738
# Get optimization history information
741
- filename = dataout_dir + "/ optim_history.dat"
739
+ filename = os . path . join ( datadir , " optim_history.dat")
742
740
try :
743
741
optim_hist_tmp = np .loadtxt (filename )
744
742
except :
@@ -774,7 +772,7 @@ def get_results(self, *, datadir="./", ignore_failure=False):
774
772
for iosc in range (len (self .Ne )):
775
773
for iinit in range (ninits ):
776
774
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")
778
776
try :
779
777
x = np .loadtxt (filename )
780
778
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):
787
785
for iosc in range (len (self .Ne )):
788
786
for iinit in range (ninits ):
789
787
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")
791
789
try :
792
790
x = np .loadtxt (filename )
793
791
population [iosc ].append (x [:,1 :].transpose ()) # first column is time
@@ -802,17 +800,17 @@ def get_results(self, *, datadir="./", ignore_failure=False):
802
800
for iinit in range (self ._ninit ):
803
801
file_index = str (iinit ).zfill (4 )
804
802
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 ]
806
804
uT [:, iinit ] = xre
807
805
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")
809
807
if not ignore_failure :
810
808
print ("Can't read from " , name )
811
809
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 ]
813
811
uT [:, iinit ] += 1j * xim
814
812
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")
816
814
if not ignore_failure :
817
815
print ("Can't read from " , name )
818
816
# uT[:, iinit] = xre + 1j * xim
@@ -823,7 +821,7 @@ def get_results(self, *, datadir="./", ignore_failure=False):
823
821
ft = []
824
822
for iosc in range (len (self .Ne )):
825
823
# Read the control pulse file
826
- filename = dataout_dir + "./ control" + str ( iosc ) + " .dat"
824
+ filename = os . path . join ( datadir , f" control{ iosc } .dat")
827
825
try :
828
826
x = np .loadtxt (filename )
829
827
except :
0 commit comments