예제 #1
0
 def circuit(weights):
     ParticleConservingU1(weights, wires, init_state=init_state)
     return qml.expval(qml.PauliZ(0))
예제 #2
0
    def test_particle_conserving_u1_operations(self):
        """Test the correctness of the ParticleConservingU1 template including the gate count
        and order, the wires each operation acts on and the correct use of parameters
        in the circuit."""

        qubits = 4
        layers = 2
        weights = particle_conserving_u1_normal(layers, qubits)

        gates_per_u1 = 16
        gates_per_layer = gates_per_u1 * (qubits - 1)
        gate_count = layers * gates_per_layer + 1

        gates_u1 = [
            qml.CZ,
            qml.CRot,
            qml.PhaseShift,
            qml.CNOT,
            qml.PhaseShift,
            qml.CNOT,
            qml.PhaseShift,
        ]
        gates_ent = gates_u1 + [qml.CZ, qml.CRot] + gates_u1

        wires = Wires(range(qubits))

        nm_wires = [wires.subset([l, l + 1]) for l in range(0, qubits - 1, 2)]
        nm_wires += [wires.subset([l, l + 1]) for l in range(1, qubits - 1, 2)]

        with pennylane._queuing.OperationRecorder() as rec:
            ParticleConservingU1(weights,
                                 wires,
                                 init_state=np.array([1, 1, 0, 0]))

        assert gate_count == len(rec.queue)

        # check initialization of the qubit register
        assert isinstance(rec.queue[0], qml.BasisState)

        # check all quantum operations
        idx_CRot = 8
        for l in range(layers):
            for i in range(qubits - 1):
                exp_wires = self._wires_gates_u1(nm_wires[i])

                phi = weights[l, i, 0]
                theta = weights[l, i, 1]

                for j, exp_gate in enumerate(gates_ent):
                    idx = gates_per_layer * l + gates_per_u1 * i + j + 1

                    # check that the gates are applied in the right order
                    assert isinstance(rec.queue[idx], exp_gate)

                    # check the wires the gates act on
                    assert rec.queue[idx]._wires == Wires(exp_wires[j])

                    # check that parametrized gates take the parameters \phi and \theta properly
                    if exp_gate is qml.CRot:

                        if j < idx_CRot:
                            exp_params = [-phi, np.pi, phi]
                        if j > idx_CRot:
                            exp_params = [phi, np.pi, -phi]
                        if j == idx_CRot:
                            exp_params = [0, 2 * theta, 0]

                        assert rec.queue[idx].parameters == exp_params

                    elif exp_gate is qml.PhaseShift:

                        if j < idx_CRot:
                            exp_params = -phi
                            if j == idx_CRot / 2:
                                exp_params = phi
                        if j > idx_CRot:
                            exp_params = phi
                            if j == (3 * idx_CRot + 2) / 2:
                                exp_params = -phi

                        assert rec.queue[idx].parameters == exp_params
예제 #3
0
 def circuit(weights):
     ParticleConservingU1(weights,
                          wires,
                          init_state=np.array([1, 1, 0, 0]))
     return [qml.expval(qml.PauliZ(w)) for w in range(N)]