Пример #1
0
    def test_evaluate(self, x, y, tol):
        """Tests correct evaluation"""
        dev = qml.device("default.qubit", wires=2)

        def circuit(x, y):
            qml.RX(x, wires=[0])
            qml.RY(y, wires=[1])
            qml.CNOT(wires=[0, 1])
            return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

        node = BaseQNode(circuit, dev)
        res = node.evaluate([x, y], {})
        expected = np.sin(y) * np.cos(x)
        assert np.allclose(res, expected, atol=tol, rtol=0)
Пример #2
0
    def test_obs_evaluate(self, x, y, tol):
        """Tests correct evaluation swapping out the observables"""
        dev = qml.device("default.qubit", wires=2)

        def circuit(x, y):
            qml.RX(x, wires=[0])
            qml.RY(y, wires=[1])
            qml.CNOT(wires=[0, 1])
            return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

        node = BaseQNode(circuit, dev)

        # test standard evaluation
        node = BaseQNode(circuit, dev)
        res = node.evaluate([x, y], {})
        expected = np.sin(y) * np.cos(x)
        assert np.allclose(res, expected, atol=tol, rtol=0)

        # hot-swap the observable
        res = node.evaluate_obs([qml.PauliZ(0) @ qml.PauliZ(1)], [x, y], {})
        expected = np.cos(y)
        assert np.allclose(res, expected, atol=tol, rtol=0)