示例#1
0
    def test_seed(self):
        """Test that the circuit is fixed by the seed."""
        weights = np.random.random((2, 3))

        op1 = qml.RandomLayers(weights, wires=range(2), seed=41)
        op2 = qml.RandomLayers(weights, wires=range(2), seed=42)
        op3 = qml.RandomLayers(weights, wires=range(2), seed=42)

        queue1 = op1.expand().operations
        queue2 = op2.expand().operations
        queue3 = op3.expand().operations

        assert not all(g1.name == g2.name for g1, g2 in zip(queue1, queue2))
        assert all(g2.name == g3.name for g2, g3 in zip(queue2, queue3))
示例#2
0
    def test_number_gates(self, n_layers, n_rots):
        """Test that the number of gates is correct."""
        weights = np.random.randn(n_layers, n_rots)

        op = qml.RandomLayers(weights, wires=range(2))
        ops = op.expand().operations

        gate_names = [g.name for g in ops]
        assert len(gate_names) - gate_names.count("CNOT") == n_layers * n_rots
示例#3
0
    def test_ratio_imprimitive(self, ratio):
        """Test the ratio of imprimitive gates."""
        n_rots = 500
        weights = np.random.random(size=(1, n_rots))

        op = qml.RandomLayers(weights, wires=range(2), ratio_imprim=ratio)
        queue = op.expand().operations

        gate_names = [gate.name for gate in queue]
        ratio_impr = gate_names.count("CNOT") / len(gate_names)
        assert np.isclose(ratio_impr, ratio, atol=0.05)
示例#4
0
    def test_random_wires(self):
        """Test that random wires are picked for the gates. This is done by
        taking the mean of all wires, which should be 1 for labels [0, 1, 2]"""
        n_rots = 5000
        weights = np.random.random(size=(2, n_rots))

        op = qml.RandomLayers(weights, wires=range(3))
        queue = op.expand().operations

        gate_wires = [gate.wires.labels for gate in queue]
        wires_flat = [item for w in gate_wires for item in w]
        mean_wire = np.mean(wires_flat)

        assert np.isclose(mean_wire, 1, atol=0.05)
示例#5
0
 def circuit2():
     qml.RandomLayers(weights, wires=["z", "a", "k"])
     return qml.expval(qml.Identity("z"))
示例#6
0
 def circuit():
     qml.RandomLayers(weights, wires=range(3))
     return qml.expval(qml.Identity(0))
示例#7
0
def circuit_template(weights):
    qml.RandomLayers(weights, range(3), seed=42)
    return qml.expval(qml.PauliZ(0))
示例#8
0
 def test_id(self):
     """Tests that the id attribute can be set."""
     template = qml.RandomLayers(np.random.random(size=(1, 3)),
                                 wires=range(3),
                                 id="a")
     assert template.id == "a"
示例#9
0
 def circuit(phi):
     qml.RandomLayers(phi, wires=list(range(4)))
     return qml.expval(qml.PauliZ(0))