Wrap file opening in caching in with statements
@@ -131,21 +131,19 @@ class CircuitCache:
if len(ops) > 0:
raise AquaError("Circuit shape does not match qobj, found extra {} in circuit".format(type_and_qubits))
if self.cache_file is not None and len(self.cache_file) > 0:
- cache_handler = open(self.cache_file, 'wb')
+ with open(self.cache_file, 'wb') as cache_handler:
qobj_dicts = [qob.to_dict() for qob in self.qobjs]
pickle.dump({'qobjs': qobj_dicts,
'mappings': self.mappings,
'transpile': self.cache_transpiled_circuits},
cache_handler,
protocol=pickle.HIGHEST_PROTOCOL)
- cache_handler.close()
logger.debug("Circuit cache saved to file: {}".format(self.cache_file))
def try_loading_cache_from_file(self):
if len(self.qobjs) == 0 and self.cache_file is not None and len(self.cache_file) > 0:
- cache_handler = open(self.cache_file, "rb")
+ with open(self.cache_file, "rb") as cache_handler:
cache = pickle.load(cache_handler, encoding="ASCII")
- cache_handler.close()
self.qobjs = [Qobj.from_dict(qob) for qob in cache['qobjs']]
self.mappings = cache['mappings']
self.cache_transpiled_circuits = cache['transpile']
