Beispiel #1
0
def mock_qnode(mock_device):
    """Provides a circuit for the subsequent tests of the operation queue"""

    def circuit(x):
        qml.RX(x, wires=[0])
        qml.CNOT(wires=[0, 1])
        qml.RY(0.4, wires=[0])
        qml.RZ(-0.2, wires=[1])
        return qml.expval(qml.PauliX(0)), qml.expval(qml.PauliZ(1))

    node = BaseQNode(circuit, mock_device)
    node._construct([1.0], {})
    return node
Beispiel #2
0
    def test_print_applied_with_probs(self, mock_device):
        """Test that printing applied gates works correctly when probs are returned"""

        H = np.array([[0, 1], [1, 0]])

        def circuit(x):
            qml.RX(x, wires=[0])
            qml.CNOT(wires=[0, 1])
            qml.SWAP(wires=[1, 0])
            qml.RZ(-0.2, wires=[1])
            return qml.probs(wires=[0]), qml.var(qml.Hermitian(H, wires=1))

        expected_qnode_print = textwrap.dedent(
            """\
            Operations
            ==========
            RX({x}, wires=[0])
            CNOT(wires=[0, 1])
            SWAP(wires=[1, 0])
            RZ(-0.2, wires=[1])

            Observables
            ===========
            probs(wires=[0])
            var(Hermitian(array([[0, 1],
                   [1, 0]]), wires=[1]))"""
        )

        node = BaseQNode(circuit, mock_device)

        # test before construction
        f = io.StringIO()

        with contextlib.redirect_stdout(f):
            node.print_applied()
            out = f.getvalue().strip()

        assert out == "QNode has not yet been executed."

        # construct QNode
        f = io.StringIO()
        node._set_variables([0.1], {})
        node._construct([0.1], {})

        with contextlib.redirect_stdout(f):
            node.print_applied()
            out = f.getvalue().strip()

        assert out == expected_qnode_print.format(x=0.1)
    def test_operation_appending(self, mock_device):
        """Tests that operations are correctly appended."""
        CNOT = qml.CNOT(wires=[0, 1])

        def circuit(x):
            qml.QueuingContext.append_operator(CNOT)
            qml.RY(0.4, wires=[0])
            qml.RZ(-0.2, wires=[1])

            return qml.expval(qml.PauliX(0)), qml.expval(qml.PauliZ(1))

        qnode = BaseQNode(circuit, mock_device)
        qnode._construct([1.0], {})

        assert qnode.ops[0].name == "CNOT"
        assert qnode.ops[1].name == "RY"
        assert qnode.ops[2].name == "RZ"
        assert qnode.ops[3].name == "PauliX"
Beispiel #4
0
    def test_operation_removal(self, mock_device):
        """Tests that operations are correctly removed."""
        def circuit(x):
            RX = qml.RX(x, wires=[0])
            qml.CNOT(wires=[0, 1])
            qml.RY(0.4, wires=[0])
            qml.RZ(-0.2, wires=[1])

            qml._current_context._remove_op(RX)

            return qml.expval(qml.PauliX(0)), qml.expval(qml.PauliZ(1))

        qnode = BaseQNode(circuit, mock_device)
        qnode._construct([1.0], {})

        assert qnode.ops[0].name == "CNOT"
        assert qnode.ops[1].name == "RY"
        assert qnode.ops[2].name == "RZ"
        assert qnode.ops[3].name == "PauliX"