def test_random_layer_numgates(self, n_subsystems):
        """Test that random_layer() uses the correct number of gates."""
        n_rots = 5
        weights = np.random.randn(n_rots)

        with qml.utils.OperationRecorder() as rec:
            random_layer(weights=weights, wires=range(n_subsystems), ratio_imprim=0.3,
                         imprimitive=qml.CNOT, rotations=[RX, RY, RZ], seed=42)

        types = [type(q) for q in rec.queue]
        assert len(types) - types.count(qml.CNOT) == n_rots
    def test_random_layer_weights(self, n_subsystems, tol):
        """Test that random_layer() uses the correct weights."""
        np.random.seed(12)
        n_rots = 5
        weights = np.random.randn(n_rots)

        with qml.utils.OperationRecorder() as rec:
            random_layer(weights=weights, wires=range(n_subsystems), ratio_imprim=0.3,
                         imprimitive=qml.CNOT, rotations=[RX, RY, RZ], seed=4)

        params = [q.parameters for q in rec.queue]
        params_flat = [item for p in params for item in p]
        assert np.allclose(weights.flatten(), params_flat, atol=tol)
    def test_random_layer_randomwires(self, n_subsystems):
        """Test that  random_layer() picks random wires."""
        n_rots = 500
        weights = np.random.randn(n_rots)

        with qml.utils.OperationRecorder() as rec:
            random_layer(weights=weights, wires=range(n_subsystems), ratio_imprim=0.3,
                         imprimitive=qml.CNOT, rotations=[RX, RY, RZ], seed=42)

        wires = [q._wires for q in rec.queue]
        wires_flat = [item for w in wires for item in w]
        mean_wire = np.mean(wires_flat)
        assert np.isclose(mean_wire, (n_subsystems - 1) / 2, atol=0.05)
    def test_random_layer_gate_types(self, n_subsystems, impr, rots):
        """Test that  random_layer() uses the correct types of gates."""
        n_rots = 20
        weights = np.random.randn(n_rots)

        with qml.utils.OperationRecorder() as rec:
            random_layer(weights=weights, wires=range(n_subsystems), ratio_imprim=0.3,
                         imprimitive=impr, rotations=rots, seed=42)

        types = [type(q) for q in rec.queue]
        unique = set(types)
        gates = {impr, *rots}
        assert unique == gates
    def test_random_layer_ratio_imprimitive(self, ratio):
        """Test that  random_layer() has the right ratio of imprimitive gates."""
        n_rots = 500
        n_wires = 2
        impr = CNOT
        weights = np.random.randn(n_rots)

        with qml.utils.OperationRecorder() as rec:
            random_layer(weights=weights, wires=range(n_wires), ratio_imprim=ratio,
                         imprimitive=CNOT, rotations=[RX, RY, RZ], seed=42)

        types = [type(q) for q in rec.queue]
        ratio_impr = types.count(impr) / len(types)
        assert np.isclose(ratio_impr, ratio, atol=0.05)