Ejemplo n.º 1
0
            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
Ejemplo n.º 2
0
    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__()
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
0
    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}
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
    def test_remove_no_context(self):
        """Test that remove does not fail when no context is present."""

        QueuingContext.remove(qml.PauliZ(0))
Ejemplo n.º 11
0
    def test_append_no_context(self):
        """Test that append does not fail when no context is present."""

        QueuingContext.append(qml.PauliZ(0))