def test_SaveAndLoad(self): H, n = Hamiltonians.longrange(7) # test saving and loading by both file name # and file object f = 'save_test.msc' with self.subTest(m='save'): H.save(f) with self.subTest(m='load_str'): Hf = do.Load(f) self.assertEqual(H, Hf) # also make sure that the generated PETSc matrix # works r, msg = check_dnm_np(Hf, n) self.assertTrue(r, msg=msg) with self.subTest(m='load_file'): with open(f, 'rb') as fd: Hf = do.Load(fd) self.assertEqual(H, Hf) # also make sure that the generated PETSc matrix # works r, msg = check_dnm_np(Hf, n) self.assertTrue(r, msg=msg) COMM_WORLD.barrier() if COMM_WORLD.rank == 0: remove(f)
def save(self, fout): """ Save the MSC representation of the operator to disk. Can be loaded again through :class:`Load`. Parameters ---------- fout : str The path to the file to save the operator in. """ # only process 0 should save if COMM_WORLD.rank == 0: # The file format is: # L,nterms,masks,signs,coefficients # where each is just a binary blob, one after the other. # do this first so that we haven't already created the file if # it fails for some reason msc = self.get_MSC() with open(fout, mode='wb') as f: # write the chain length to the file. This is the only parameter # that we save other than the MSC representation. L = self.L if L is None: raise ValueError('L must be set before saving to disk.') # cast it to the type that C will be looking for int_t = msc.dtype[0].type L = int_t(L) f.write(L.tobytes()) # write out the length of the MSC representation size = int_t(msc.size) f.write(size.tobytes()) f.write(msc['masks'].tobytes()) f.write(msc['signs'].tobytes()) f.write(msc['coeffs'].tobytes()) COMM_WORLD.barrier()
def getprocessorinfo(): try: name = os.uname()[1] except: import platform name = platform.uname()[1] from petsc4py.PETSc import COMM_WORLD rank = COMM_WORLD.getRank() return (rank, name)
def _repr_latex_(self): # don't print if we aren't process 0 # this causes there to still be some awkward white space, # but at least it doesn't print the tex a zillion times if COMM_WORLD.getRank() != 0: return '' return '$' + self.build_tex(signs='-') + '$'
def getprocessorinfo(): from petsc4py.PETSc import COMM_WORLD rank = COMM_WORLD.getRank() name = os.uname()[1] return (rank, name)
import numpy as np from scipy.linalg import expm from dynamite import config from dynamite.tools import build_state, vectonumpy from dynamite.operators import Sigmax, Sigmay, Sigmaz, Load from petsc4py.PETSc import COMM_WORLD, NormType from numpy_operators import * try: import qutip as qtp except ImportError: qtp = None CW = COMM_WORLD.tompi4py() PROC_0 = CW.Get_rank() == 0 def dnm_to_np(H): dim = 2**H.L if PROC_0: ret = np.ndarray((dim, dim), dtype=np.complex128) s1 = build_state(H.L) s2 = build_state(H.L) s1.set(0) for i in range(dim): if i > 0: s1.setValue(i - 1, 0)