Ejemplo n.º 1
0
 def circuit(weight):
     qml.kUpCCGSD(weight,
                  wires=wires,
                  k=k,
                  delta_sz=0,
                  init_state=init_state)
     return qml.state()
Ejemplo n.º 2
0
def circuit_template(weights):
    qml.kUpCCGSD(
        weights,
        wires=range(4),
        k=1,
        delta_sz=0,
        init_state=np.array([1, 1, 0, 0]),
    )
    return qml.expval(qml.PauliZ(0))
Ejemplo n.º 3
0
 def circuit():
     qml.kUpCCGSD(
         weights=weights,
         wires=wires,
         k=k,
         delta_sz=delta_sz,
         init_state=init_state,
     )
     return qml.expval(qml.PauliZ(0))
Ejemplo n.º 4
0
 def circuit2():
     qml.kUpCCGSD(
         weights,
         wires=["z", "a", "k", "e"],
         k=1,
         delta_sz=0,
         init_state=np.array([0, 1, 0, 1]),
     )
     return qml.expval(qml.Identity("z"))
Ejemplo n.º 5
0
 def circuit():
     qml.kUpCCGSD(
         weights,
         wires=range(4),
         k=1,
         delta_sz=0,
         init_state=np.array([0, 1, 0, 1]),
     )
     return qml.expval(qml.Identity(0))
Ejemplo n.º 6
0
 def test_id(self):
     """Test that the id attribute can be set."""
     template = qml.kUpCCGSD(
         qml.math.array([[0.55, 0.72, 0.6, 0.54, 0.42, 0.65]]),
         wires=range(4),
         k=1,
         delta_sz=0,
         init_state=qml.math.array([1, 1, 0, 0]),
         id="a",
     )
     assert template.id == "a"
Ejemplo n.º 7
0
    def test_excitations_wires_kupccgsd(self, wires, delta_sz,
                                        generalized_singles_wires,
                                        generalized_pair_doubles_wires):
        """Test the correctness of the wire indices for the generalized singles and paired doubles excitaitons
        used by the template."""

        shape = qml.kUpCCGSD.shape(k=1, n_wires=len(wires), delta_sz=delta_sz)
        weights = np.pi / 2 * qml.math.ones(shape)

        ref_state = qml.math.array([1, 1, 0, 0])

        op = qml.kUpCCGSD(weights,
                          wires=wires,
                          k=1,
                          delta_sz=delta_sz,
                          init_state=ref_state)
        gen_singles_wires, gen_doubles_wires = op.s_wires, op.d_wires

        assert gen_singles_wires == generalized_singles_wires
        assert gen_doubles_wires == generalized_pair_doubles_wires
Ejemplo n.º 8
0
    def test_kupccgsd_operations(self, k, delta_sz, init_state, wires):
        """Test the correctness of the k-UpCCGSD template including the gate count
        and order, the wires the operation acts on and the correct use of parameters
        in the circuit."""

        # wires for generalized single excitation terms
        sz = np.array(
            [0.5 if (i % 2 == 0) else -0.5 for i in range(len(wires))])
        gen_single_terms_wires = [
            wires[r:p + 1] if r < p else wires[p:r + 1][::-1]
            for r in range(len(wires)) for p in range(len(wires))
            if sz[p] - sz[r] == delta_sz and p != r
        ]

        # wires for generalized pair coupled cluser double exictation terms
        pair_double_terms_wires = [[wires[r:r + 2], wires[p:p + 2]]
                                   for r in range(0,
                                                  len(wires) - 1, 2)
                                   for p in range(0,
                                                  len(wires) - 1, 2) if p != r]

        n_excit_terms = len(gen_single_terms_wires) + len(
            pair_double_terms_wires)
        weights = np.random.normal(0, 2 * np.pi, (k, n_excit_terms))

        n_gates = 1 + n_excit_terms * k
        exp_unitary = [qml.FermionicDoubleExcitation
                       ] * len(pair_double_terms_wires)
        exp_unitary += [qml.FermionicSingleExcitation
                        ] * len(gen_single_terms_wires)

        op = qml.kUpCCGSD(weights,
                          wires=wires,
                          k=k,
                          delta_sz=delta_sz,
                          init_state=init_state)
        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_unitary):
            assert isinstance(op1, op2)

        # gate parameter
        params = np.zeros((k, n_excit_terms))
        for i in range(1, n_gates):
            gate_index = (i - 1) % n_excit_terms
            if gate_index < len(pair_double_terms_wires):
                gate_index += len(gen_single_terms_wires)
            else:
                gate_index -= len(pair_double_terms_wires)
            params[(i - 1) //
                   n_excit_terms][gate_index] = queue[i].parameters[0]

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

        # gate wires
        exp_wires = ([np.concatenate(w) for w in pair_double_terms_wires] +
                     gen_single_terms_wires) * k
        res_wires = [queue[i].wires.tolist() for i in range(1, n_gates)]
        for wires1, wires2 in zip(exp_wires, res_wires):
            assert np.all(wires1 == wires2)