Example #1
0
 def circuit():
     qml.GateFabric(
         weights=weights,
         wires=wires,
         init_state=init_state,
     )
     return qml.expval(qml.PauliZ(0))
Example #2
0
 def test_id(self):
     """Tests that the id attribute can be set."""
     init_state = qml.math.array([1, 1, 0, 0])
     template = qml.GateFabric(weights=np.random.random(size=(1, 1, 2)),
                               wires=range(4),
                               init_state=init_state,
                               id="a")
     assert template.id == "a"
Example #3
0
def circuit_template(weights):
    qml.GateFabric(weights,
                   range(4),
                   init_state=qml.math.array([1, 1, 0, 0]),
                   include_pi=True)
    return qml.expval(qml.PauliZ(0))
Example #4
0
 def circuit2():
     qml.GateFabric(weights,
                    wires=["z", "a", "k", "r"],
                    init_state=init_state)
     return qml.expval(qml.Identity("z"))
Example #5
0
 def circuit():
     qml.GateFabric(weights, wires=range(4), init_state=init_state)
     return qml.expval(qml.Identity(0))
Example #6
0
 def circuit(weight):
     qml.GateFabric(weight,
                    wires,
                    init_state=init_state,
                    include_pi=True)
     return qml.state()
Example #7
0
 def circuit(weight):
     qml.GateFabric(weight, wires, init_state=init_state)
     return qml.expval(qml.PauliZ(0))
Example #8
0
    def test_operations(self, layers, qubits, init_state, include_pi):
        """Test the correctness of the GateFabric template including the gate count
        and order, the wires each operation acts on and the correct use of parameters
        in the circuit."""

        weights = np.random.normal(0, 2 * np.pi, (layers, qubits // 2 - 1, 2))

        if not include_pi:
            n_gates = 1 + (qubits - 2) * layers
            exp_gates = (([qml.DoubleExcitation] + [qml.OrbitalRotation]) *
                         (qubits // 2 - 1)) * layers
        else:
            n_gates = 1 + 3 * (qubits // 2 - 1) * layers
            exp_gates = (([qml.OrbitalRotation] + [qml.DoubleExcitation] +
                          [qml.OrbitalRotation]) * (qubits // 2 - 1)) * layers

        op = qml.GateFabric(weights,
                            wires=range(qubits),
                            init_state=init_state,
                            include_pi=include_pi)
        queue = op.expand().operations

        # number of gates
        assert len(queue) == n_gates

        # initialization
        assert isinstance(queue[0], qml.BasisEmbedding)

        # order of gates
        for op1, op2 in zip(queue[1:], exp_gates):
            assert isinstance(op1, op2)

        # gate parameter
        params = qml.math.array([
            queue[i].parameters for i in range(1, n_gates)
            if queue[i].parameters != []
        ])

        if include_pi:
            weights = qml.math.insert(weights,
                                      0,
                                      [[np.pi] * (qubits // 2 - 1)] * layers,
                                      axis=2)

        assert qml.math.allclose(params.flatten(), weights.flatten())

        # gate wires
        wires = range(qubits)
        qwires = [
            wires[i:i + 4] for i in range(0, len(wires), 4)
            if len(wires[i:i + 4]) == 4
        ]
        if len(wires) // 2 > 2:
            qwires += [
                wires[i:i + 4] for i in range(2, len(wires), 4)
                if len(wires[i:i + 4]) == 4
            ]

        exp_wires = []
        for _ in range(layers):
            for wire in qwires:
                if include_pi:
                    exp_wires.append(list(wire))
                exp_wires.append(list(wire))
                exp_wires.append(list(wire))

        res_wires = [queue[i].wires.tolist() for i in range(1, n_gates)]
        assert res_wires == exp_wires