Esempio n. 1
0
    def to_autograd(self):
        """Apply the Autograd interface to the internal quantum tape."""
        self.interface = "autograd"
        self.dtype = AutogradInterface.dtype

        if self.qtape is not None:
            AutogradInterface.apply(self.qtape)
 def cost(a, b, device):
     with AutogradInterface.apply(QuantumTape()) as tape:
         qml.RY(a, wires=0)
         qml.RX(b, wires=0)
         expval(qml.PauliZ(0))
     assert tape.trainable_params == {0}
     return tape.execute(device)
        def cost(x, device):
            with AutogradInterface.apply(QuantumTape()) as tape:
                qml.Hadamard(wires=[0])
                qml.CNOT(wires=[0, 1])
                sample(qml.PauliZ(0))
                sample(qml.PauliX(1))

            return tape.execute(device)
    def test_interface_str(self):
        """Test that the interface string is correctly identified as autograd"""
        with AutogradInterface.apply(QuantumTape()) as tape:
            qml.RX(0.5, wires=0)
            expval(qml.PauliX(0))

        assert tape.interface == "autograd"
        assert isinstance(tape, AutogradInterface)
        def cost(a, device):
            with AutogradInterface.apply(QuantumTape()) as qtape:
                qml.RY(a[0], wires=0)
                qml.RX(a[1], wires=0)
                expval(qml.PauliZ(0))

            qtape.jacobian_options = {"h": 1e-8, "order": 2}
            return qtape.execute(dev)
        def cost(x, y, device):
            with AutogradInterface.apply(QuantumTape()) as tape:
                qml.RX(x, wires=[0])
                qml.RY(y, wires=[1])
                qml.CNOT(wires=[0, 1])
                expval(qml.PauliZ(0))
                probs(wires=[1])

            return tape.execute(device)
    def test_get_parameters(self):
        """Test that the get_parameters function correctly sets and returns the
        trainable parameters"""
        a = np.array(0.1, requires_grad=True)
        b = np.array(0.2, requires_grad=False)
        c = np.array(0.3, requires_grad=True)
        d = np.array(0.4, requires_grad=False)

        with AutogradInterface.apply(QuantumTape()) as tape:
            qml.Rot(a, b, c, wires=0)
            qml.RX(d, wires=1)
            qml.CNOT(wires=[0, 1])
            expval(qml.PauliX(0))

        assert tape.trainable_params == {0, 2}
        assert np.all(tape.get_parameters() == [a, c])
        def cost_fn(a, p, device):
            tape = QuantumTape()

            with tape:
                qml.RX(a, wires=0)
                U3(*p, wires=0)
                expval(qml.PauliX(0))

            tape = AutogradInterface.apply(tape.expand())

            assert tape.trainable_params == {1, 2, 3, 4}
            assert [i.name
                    for i in tape.operations] == ["RX", "Rot", "PhaseShift"]
            assert np.all(
                np.array(tape.get_parameters()) ==
                [p[2], p[0], -p[2], p[1] + p[2]])

            return tape.execute(device=device)
    def test_reusing_quantum_tape(self, tol):
        """Test re-using a quantum tape by passing new parameters"""
        a = np.array(0.1, requires_grad=True)
        b = np.array(0.2, requires_grad=True)

        dev = qml.device("default.qubit", wires=2)

        with AutogradInterface.apply(QuantumTape()) as tape:
            qml.RY(a, wires=0)
            qml.RX(b, wires=1)
            qml.CNOT(wires=[0, 1])
            expval(qml.PauliZ(0))
            expval(qml.PauliY(1))

        assert tape.trainable_params == {0, 1}

        def cost(a, b):
            tape.set_parameters([a, b])
            return tape.execute(dev)

        jac_fn = qml.jacobian(cost)
        jac = jac_fn(a, b)

        a = np.array(0.54, requires_grad=True)
        b = np.array(0.8, requires_grad=True)

        res2 = cost(2 * a, b)
        expected = [np.cos(2 * a), -np.cos(2 * a) * np.sin(b)]
        assert np.allclose(res2, expected, atol=tol, rtol=0)

        jac_fn = qml.jacobian(lambda a, b: cost(2 * a, b))
        jac = jac_fn(a, b)
        expected = [
            [-2 * np.sin(2 * a), 0],
            [2 * np.sin(2 * a) * np.sin(b), -np.cos(2 * a) * np.cos(b)],
        ]
        assert np.allclose(jac, expected, atol=tol, rtol=0)