def queue(self): QueuingContext.append(self, owns=tuple(self.obs)) for o in self.obs: try: QueuingContext.update_info(o, owner=self) except AttributeError: pass return self
def __enter__(self): if not QueuingContext.recording(): # if the tape is the first active queuing context # monkeypatch the operations to support the new queuing context with contextlib.ExitStack() as stack: for mock in mock_operations(): stack.enter_context(mock) self._stack = stack.pop_all() QueuingContext.append(self) return super().__enter__()
def __enter__(self): QuantumTape._lock.acquire() try: if not QueuingContext.recording(): # if the tape is the first active queuing context # monkeypatch the operations to support the new queuing context with contextlib.ExitStack() as stack: for mock in mock_operations(): stack.enter_context(mock) self._stack = stack.pop_all() QueuingContext.append(self) return super().__enter__() except Exception as _: QuantumTape._lock.release() raise
def __exit__(self, exception_type, exception_value, traceback): super().__exit__(exception_type, exception_value, traceback) if not QueuingContext.recording(): # remove the monkeypatching self._stack.__exit__(exception_type, exception_value, traceback) self._process_queue()
def test_get_info_none(self): """Test that get_info returns None if there is no active queuing context""" A = qml.RZ(0.5, wires=1) with AnnotatedQueue() as q: q.append(A, inv=True) assert QueuingContext.get_info(A) is None
def mock_queuing_context(monkeypatch): """A mock instance of the abstract QueuingContext class.""" with monkeypatch.context() as m: m.setattr(QueuingContext, "__abstractmethods__", frozenset()) m.setattr( QueuingContext, "_append", lambda self, operator: self.queue.append(operator), ) m.setattr( QueuingContext, "_remove", lambda self, operator: self.queue.remove(operator), ) context = QueuingContext() context.queue = [] yield context
def __exit__(self, exception_type, exception_value, traceback): try: super().__exit__(exception_type, exception_value, traceback) if not QueuingContext.recording(): # remove the monkeypatching self._stack.__exit__(exception_type, exception_value, traceback) self._process_queue() finally: QuantumTape._lock.release()
def test_update_info(self): """Test that update_info correctly updates an annotation""" A = qml.RZ(0.5, wires=1) with AnnotatedQueue() as q: q.append(A, inv=True) assert QueuingContext.get_info(A) == {"inv": True} assert q._get_info(A) == {"inv": True} q._update_info(A, inv=False, owner=None) assert q._get_info(A) == {"inv": False, "owner": None}
def three_mock_queuing_contexts(monkeypatch): """A list of three mock instances of the abstract QueuingContext class.""" with monkeypatch.context() as m: m.setattr(QueuingContext, "__abstractmethods__", frozenset()) m.setattr( QueuingContext, "_append", lambda self, operator: self.queue.append(operator), ) m.setattr( QueuingContext, "_remove", lambda self, operator: self.queue.remove(operator), ) contexts = [QueuingContext() for _ in range(3)] for context in contexts: context.queue = [] yield contexts
def test_remove_no_context(self): """Test that remove does not fail when no context is present.""" QueuingContext.remove(qml.PauliZ(0))
def test_append_no_context(self): """Test that append does not fail when no context is present.""" QueuingContext.append(qml.PauliZ(0))