def circuit(weights):
     _random_layer(weights=weights,
                   wires=range(n_subsystems),
                   ratio_imprim=0.3,
                   imprimitive=qml.CNOT,
                   rotations=[RX, RY, RZ],
                   seed=4)
     return qml.expval(qml.PauliZ(0))
 def circuit(weights):
     _random_layer(weights=weights,
                   wires=range(n_wires),
                   ratio_imprim=ratio,
                   imprimitive=CNOT,
                   rotations=[RX, RY, RZ],
                   seed=42)
     return qml.expval(qml.PauliZ(0))
 def circuit(weights):
     _random_layer(weights=weights,
                   wires=range(n_subsystems),
                   ratio_imprim=0.3,
                   imprimitive=impr,
                   rotations=rots,
                   seed=42)
     return qml.expval(qml.PauliZ(0))
    def test_random_layer_numgates(self, n_subsystems):
        """Test that _random_layer() uses the correct number of gates."""
        n_rots = 5
        dev = qml.device('default.qubit', wires=n_subsystems)
        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
        dev = qml.device('default.qubit', wires=n_subsystems)
        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
        dev = qml.device('default.qubit', wires=n_subsystems)
        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
        dev = qml.device('default.qubit', wires=n_subsystems)
        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
        dev = qml.device('default.qubit', wires=n_wires)
        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)
示例#9
0
 def circuit(weights):
     _random_layer(weights=weights, wires=range(n_subsystems))
     return qml.expval(qml.PauliZ(0))
示例#10
0
 def circuit(weights):
     _random_layer(weights=weights,
                   wires=range(n_wires),
                   ratio_imprim=ratio,
                   imprimitive=CNOT)
     return qml.expval(qml.PauliZ(0))