Ejemplo n.º 1
0
    def probabilities(self, qubits=None, measurement_gate=None):
        order = (tuple(sorted(qubits)) +
                 tuple(i for i in range(self.nqubits) if i not in qubits))
        order = order + tuple(i + self.nqubits for i in order)
        shape = 2 * (2**len(qubits), 2**(self.nqubits - len(qubits)))

        state = K.reshape(self.tensor, 2 * self.nqubits * (2, ))
        state = K.reshape(K.transpose(state, order), shape)
        state = K.einsum("abab->a", state)

        return K.reshape(K.cast(state, dtype='DTYPE'), len(qubits) * (2, ))
Ejemplo n.º 2
0
def test_apply_gate(nqubits, target, dtype, compile, einsum_str):
    """Check that ``K.op.apply_gate`` agrees with einsum gate implementation."""
    def apply_operator(state, gate):
        qubits = qubits_tensor(nqubits, [target])
        return K.op.apply_gate(state, gate, qubits, nqubits, target,
                               get_threads())

    state = random_complex((2**nqubits, ), dtype=dtype)
    gate = random_complex((2, 2), dtype=dtype)

    target_state = K.reshape(state, nqubits * (2, ))
    target_state = K.einsum(einsum_str, target_state, gate)
    target_state = target_state.numpy().ravel()

    if compile:
        apply_operator = K.compile(apply_operator)
    state = apply_operator(state, gate)
    np.testing.assert_allclose(target_state, state, atol=_atol)
Ejemplo n.º 3
0
 def density_matrix_partial_trace(self, state):
     self._set_nqubits(state)
     state = K.reshape(state, 2 * self.nqubits * (2, ))
     state = K.transpose(state, self.cache.einsum_order)
     state = K.reshape(state, self.cache.einsum_shape)
     return K.einsum("abac->bc", state)
Ejemplo n.º 4
0
 def __call__(self, cache: str, state: K.Tensor,
              gate: K.Tensor) -> K.Tensor:
     return K.einsum(cache, state, gate)