def test_cvneuralnet_uses_correct_weights(self, gaussian_device_4modes,
                                              weights):
        """Tests that the CVNeuralNetLayers template uses the weigh parameters correctly."""

        with qml.utils.OperationRecorder() as rec:
            CVNeuralNetLayers(*weights, wires=range(4))

        # Test that gates appear in the right order for each layer:
        # BS-R-S-BS-R-D-K
        for l in range(2):
            gates = [
                qml.Beamsplitter, qml.Rotation, qml.Squeezing,
                qml.Beamsplitter, qml.Rotation, qml.Displacement
            ]

            # count the position of each group of gates in the layer
            num_gates_per_type = [0, 6, 4, 4, 6, 4, 4, 4]
            s = np.cumsum(num_gates_per_type)
            gc = l * sum(num_gates_per_type) + np.array(
                list(zip(s[:-1], s[1:])))

            # loop through expected gates
            for idx, g in enumerate(gates):
                # loop through where these gates should be in the queue
                for opidx, op in enumerate(rec.queue[gc[idx, 0]:gc[idx, 1]]):
                    # check that op in queue is correct gate
                    assert isinstance(op, g)

                    # test that the parameters are correct
                    res_params = op.parameters

                    if idx == 0:
                        # first BS
                        exp_params = [
                            weights[0][l][opidx], weights[1][l][opidx]
                        ]
                    elif idx == 1:
                        # first rot
                        exp_params = [weights[2][l][opidx]]
                    elif idx == 2:
                        # squeezing
                        exp_params = [
                            weights[3][l][opidx], weights[4][l][opidx]
                        ]
                    elif idx == 3:
                        # second BS
                        exp_params = [
                            weights[5][l][opidx], weights[6][l][opidx]
                        ]
                    elif idx == 4:
                        # second rot
                        exp_params = [weights[7][l][opidx]]
                    elif idx == 5:
                        # displacement
                        exp_params = [
                            weights[8][l][opidx], weights[9][l][opidx]
                        ]

                    assert res_params == exp_params
Example #2
0
 def circuit(*weights):
     CVNeuralNetLayers(*weights, wires=range(4))
     return qml.expval(qml.X(0))
Example #3
0
 def circuit(weights):
     CVNeuralNetLayers(*weights, wires=range(N))
     return qml.expval.X(wires=0)
Example #4
0
 def circuit(weights):
     CVNeuralNetLayers(*weights, wires=range(n_subsystems))
     return qml.expval.Identity(0)