Exemplo n.º 1
0
def test_main_engine_init_defaults():
    eng = _main.MainEngine()
    eng_list = []
    current_engine = eng.next_engine
    while not current_engine.is_last_engine:
        eng_list.append(current_engine)
        current_engine = current_engine.next_engine
    assert isinstance(eng_list[-1].next_engine, Simulator)
    from projectq.setups.default import default_engines
    for engine, expected in zip(eng_list, default_engines()):
        assert type(engine) == type(expected)
Exemplo n.º 2
0
	def __init__(self, backend=None, engine_list=None):
		"""
		Initialize the main compiler engine and all compiler engines.
		
		Sets 'next_engine'- and 'main_engine'-attributes of all compiler engines
		and adds the back-end as the last engine.
		
		Args:
			backend (BasicEngine): Backend to send the circuit to.
			engine_list (list<BasicEngine>): List of engines / backends to use as
				compiler engines.
			
		Example:
			.. code-block:: python
				
				from projectq import MainEngine
				eng = MainEngine() # will load default setup using Simulator backend
			
		Alternatively, one can specify all compiler engines explicitly, e.g., 
		
		Example:
			.. code-block:: python
				
				from projectq.cengines import TagRemover,AutoReplacer,LocalOptimizer
				from projectq.backends import Simulator
				from projectq import MainEngine
				engines = [AutoReplacer(), TagRemover(), LocalOptimizer(3)]
				eng = MainEngine(Simulator(), engines)
		"""
		BasicEngine.__init__(self)
		
		if backend is None:
			backend = Simulator()
		else: # Test that backend is BasicEngine object
			if not isinstance(backend, BasicEngine):
				raise UnsupportedEngineError(
					"\nYou supplied a backend which is not supported,\n" +
					"i.e. not an instance of BasicEngine.\n"+
					"Did you forget the brackets to create an instance?\n" +
					"E.g. MainEngine(backend=Simulator) instead of \n" +
					"     MainEngine(backend=Simulator())")
		if engine_list is None:
			try:
				engine_list = projectq.default_engines()
			except AttributeError:
				from projectq.setups.default import default_engines
				engine_list = default_engines()
		else: # Test that engine list elements are all BasicEngine objects
			if not isinstance(engine_list, list):
				raise UnsupportedEngineError(
					"\n The engine_list argument is not a list!\n")
			for current_eng in engine_list:
				if not isinstance(current_eng, BasicEngine):
					raise UnsupportedEngineError(
						"\nYou supplied an unsupported engine in engine_list,\n" +
						"i.e. not an instance of BasicEngine.\n"+
						"Did you forget the brackets to create an instance?\n" +
						"E.g. MainEngine(engine_list=[AutoReplacer]) instead of \n" +
						"     MainEngine(engine_list=[AutoReplacer()])")
		
		engine_list.append(backend)

		# Test that user did not supply twice the same engine instance
		num_different_engines = len(set([id(item) for item in engine_list]))
		if len(engine_list) != num_different_engines:
			raise UnsupportedEngineError(
				"\n Error:\n You supplied twice the same engine as backend" +
				" or item in engine_list. This doesn't work. Create two \n" +
				" separate instances of a compiler engine if it is needed\n" +
				" twice.\n")
		
		self._qubit_idx = int(0)
		for i in range(len(engine_list) - 1):
			engine_list[i].next_engine = engine_list[i + 1]
			engine_list[i].main_engine = self
		engine_list[-1].main_engine = self
		engine_list[-1].is_last_engine = True
		self.next_engine = engine_list[0]
		self.main_engine = self
		self.active_qubits = weakref.WeakSet()
		self._measurements = dict()
		self.dirty_qubits = set()
		
		# In order to terminate an example code without eng.flush or Measure
		self._delfun = lambda x: x.flush(deallocate_qubits=True)
		atexit.register(self._delfun, self)