예제 #1
0
    def test_queue_no_context(self):
        """Test that the queue property returns an empty list if there is no underlying context."""
        qnode_mock = MagicMock()
        qnode_mock.queue = ["A"]

        rec = pu.Recorder(qnode_mock)

        assert rec.queue == ["A"]
예제 #2
0
    def test_append_op_no_context(self):
        """Test that the operation is appended when no context is supplied."""
        rec = pu.Recorder(None)

        op = qml.PauliZ(3)
        rec._append_op(op)

        assert rec._ops == [op]
예제 #3
0
    def test_attribute_spoofing_error(self):
        """Test that the proper error is raised if attribute spoofing is attemped
        with no underlying QNode."""
        rec = pu.Recorder(None)

        with pytest.raises(
                AttributeError,
                match="Attribute test of Recorder mock QNode does not exist"):
            rec.test
예제 #4
0
    def test_append_op_calls_underlying_context(self):
        """Test that the underlying context is called in _append_op."""
        qnode_mock = MagicMock()

        rec = pu.Recorder(qnode_mock)
        op = qml.PauliZ(3)
        rec._append_op(op)

        assert qnode_mock._append_op.call_args[0][0] == op
        assert rec._ops == [op]
예제 #5
0
    def test_context_method_spoofing(self):
        """Test that unknown methods are properly relayed to the underlying context."""
        class MethodMock:
            args = []

            def construct(self, arg):
                self.args.append(arg)

        qnode_mock = MethodMock()

        rec = pu.Recorder(qnode_mock)

        rec.construct("Test")
        assert qnode_mock.args[0] == "Test"
예제 #6
0
    def test_context_attribute_spoofing(self):
        """Test that unknown attributes are properly relayed to the underlying context."""
        class AssignmentMock:
            queue = ["A"]

        qnode_mock = AssignmentMock()
        rec = pu.Recorder(qnode_mock)

        assert rec.queue == ["A"]
        assert qnode_mock.queue == ["A"]

        rec.queue.append("B")

        assert rec.queue == ["A", "B"]
        assert qnode_mock.queue == ["A", "B"]
예제 #7
0
    def test_queue_no_context(self):
        """Test that the queue property returns an empty list if there is no underlying context."""
        rec = pu.Recorder(None)

        assert rec.queue == []