Ejemplo n.º 1
0
    def test_param_no_observables(self, operable_mock_CV_device_2_wires):
        """Tests that a parameter has 0 gradient if it is not followed by any observables"""
        par = [0.4]

        def qf(x):
            qml.Displacement(x, 0, wires=[0])
            qml.Squeezing(0.3, x, wires=[0])
            qml.Rotation(1.3, wires=[1])
            return qml.expval(qml.X(1))

        q = CVQNode(qf, operable_mock_CV_device_2_wires)
        q._construct(par, {})
        assert q.par_to_grad_method == {0: "0"}
Ejemplo n.º 2
0
    def test_param_not_differentiable(self, operable_mock_CV_device_2_wires):
        """Tests that a parameter is not differentiable if used in an operation
        where grad_method=None"""
        par = [0.4]

        def qf(x):
            qml.FockState(x, wires=[0])
            qml.Rotation(1.3, wires=[0])
            return qml.expval(qml.X(0))

        q = CVQNode(qf, operable_mock_CV_device_2_wires)
        q._construct(par, {})
        assert q.par_to_grad_method == {0: None}
Ejemplo n.º 3
0
    def test_correct_method_non_gaussian_preceeding_one_param(
            self, operable_mock_CV_device_2_wires):
        """Tests that a non-Gaussian preceeding one parameter fallsback to finite-diff"""
        par = [0.4, -2.3]

        def qf(x, y):
            qml.Kerr(y, wires=[1])
            qml.Displacement(x, 0, wires=[0])
            qml.Beamsplitter(0.2, 1.7, wires=[0, 1])
            return qml.expval(qml.X(0)), qml.expval(qml.X(1))

        q = CVQNode(qf, operable_mock_CV_device_2_wires)
        q._construct(par, {})
        assert q.par_to_grad_method == {0: "A", 1: "F"}
Ejemplo n.º 4
0
    def test_correct_method_non_gaussian_successor_one_param(
            self, operable_mock_CV_device_2_wires):
        """Tests that a non-Gaussian succeeding a parameter fallsback to finite-diff"""
        par = [0.4, -2.3]

        def qf(x, y):
            qml.Displacement(x, 0, wires=[0])
            qml.CubicPhase(0.2, wires=[0])
            qml.Squeezing(0.3, y, wires=[1])
            qml.Rotation(1.3, wires=[1])
            # nongaussian succeeding x but not y
            return qml.expval(qml.X(0)), qml.expval(qml.X(1))

        q = CVQNode(qf, operable_mock_CV_device_2_wires)
        q._construct(par, {})
        assert q.par_to_grad_method == {0: "F", 1: "A"}
Ejemplo n.º 5
0
    def test_correct_method_non_gaussian_observable(
            self, operable_mock_CV_device_2_wires):
        """Tests that a non-Gaussian observable one parameter fallsback to finite-diff"""
        par = [0.4, -2.3]

        def qf(x, y):
            qml.Displacement(x, 0,
                             wires=[0])  # followed by nongaussian observable
            qml.Beamsplitter(0.2, 1.7, wires=[0, 1])
            qml.Displacement(y, 0, wires=[1])  # followed by order-2 observable
            return qml.expval(qml.FockStateProjector(np.array([2]),
                                                     0)), qml.expval(
                                                         qml.NumberOperator(1))

        q = CVQNode(qf, operable_mock_CV_device_2_wires)
        q._construct(par, {})
        assert q.par_to_grad_method == {0: "F", 1: "A"}
Ejemplo n.º 6
0
    def test_correct_method_non_gaussian_successor_all_params(
            self, operable_mock_CV_device_2_wires):
        """Tests that a non-Gaussian succeeding all parameters fallsback to finite-diff"""
        par = [0.4, -2.3]

        def qf(x, y):
            qml.Displacement(x, 0, wires=[0])
            qml.Displacement(1.2, y, wires=[1])
            qml.Beamsplitter(0.2, 1.7, wires=[0, 1])
            qml.Rotation(1.9, wires=[0])
            qml.Kerr(0.3, wires=[
                1
            ])  # nongaussian succeeding both x and y due to the beamsplitter
            return qml.expval(qml.X(0)), qml.expval(qml.X(1))

        q = CVQNode(qf, operable_mock_CV_device_2_wires)
        q._construct(par, {})
        assert q.par_to_grad_method == {0: "F", 1: "F"}