예제 #1
0
    def test_remove_operator_multiple_queues(self, three_mock_queuing_contexts):
        """Test that remove_operator removes the operator from the queue."""

        op = qml.PauliZ(0)
        assert not three_mock_queuing_contexts[0].queue
        assert not three_mock_queuing_contexts[1].queue
        assert not three_mock_queuing_contexts[2].queue

        with three_mock_queuing_contexts[0]:
            with three_mock_queuing_contexts[1]:
                with three_mock_queuing_contexts[2]:
                    QueuingContext.append_operator(op)

                    assert len(three_mock_queuing_contexts[0].queue) == 1
                    assert op in three_mock_queuing_contexts[0].queue

                    assert len(three_mock_queuing_contexts[1].queue) == 1
                    assert op in three_mock_queuing_contexts[1].queue

                    assert len(three_mock_queuing_contexts[2].queue) == 1
                    assert op in three_mock_queuing_contexts[2].queue

                    QueuingContext.remove_operator(op)

        assert not three_mock_queuing_contexts[0].queue
        assert not three_mock_queuing_contexts[1].queue
        assert not three_mock_queuing_contexts[2].queue
예제 #2
0
    def test_append_operator(self, mock_queuing_context):
        """Test that append_operator appends the operator to the queue."""

        op = qml.PauliZ(0)
        assert not mock_queuing_context.queue

        with mock_queuing_context:
            QueuingContext.append_operator(op)

        assert len(mock_queuing_context.queue) == 1
        assert op in mock_queuing_context.queue
예제 #3
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
예제 #4
0
    def test_remove_operator(self, mock_queuing_context):
        """Test that remove_operator removes the operator from the queue."""

        op = qml.PauliZ(0)
        assert not mock_queuing_context.queue

        with mock_queuing_context:
            QueuingContext.append_operator(op)

            assert len(mock_queuing_context.queue) == 1
            assert op in mock_queuing_context.queue

            QueuingContext.remove_operator(op)

        assert not mock_queuing_context.queue
예제 #5
0
    def test_remove_operator_not_in_list(self, mock_queuing_context):
        """Test that remove_operator does not fail when the operator to be removed is not in the queue."""

        op1 = qml.PauliZ(0)
        op2 = qml.PauliZ(1)
        assert not mock_queuing_context.queue

        with mock_queuing_context:
            QueuingContext.append_operator(op1)

            assert len(mock_queuing_context.queue) == 1
            assert op1 in mock_queuing_context.queue

            QueuingContext.remove_operator(op2)

        assert len(mock_queuing_context.queue) == 1
        assert op1 in mock_queuing_context.queue
예제 #6
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
예제 #7
0
    def test_remove_no_context(self):
        """Test that remove does not fail when no context is present."""

        QueuingContext.remove(qml.PauliZ(0))
예제 #8
0
    def test_append_no_context(self):
        """Test that append does not fail when no context is present."""

        QueuingContext.append(qml.PauliZ(0))