Esempio n. 1
0
 def state_vector_call(self, state):
     state = K.reshape(state, self.tensor_shape)
     substate = K.gather_nd(K.transpose(state, self.order), self.result)
     norm = K.sum(K.square(K.abs(substate)))
     state = substate / K.cast(K.sqrt(norm), dtype=state.dtype)
     state = self._append_zeros(state, self.sorted_qubits, self.result)
     return K.reshape(state, self.flat_shape)
Esempio n. 2
0
 def probabilities(self, qubits=None, measurement_gate=None):
     unmeasured_qubits = tuple(i for i in range(self.nqubits)
                               if i not in qubits)
     tensor = self.tensor
     with K.on_cpu():
         state = K.reshape(K.square(K.abs(tensor)), self.nqubits * (2, ))
         return K.sum(state, axis=unmeasured_qubits)
Esempio n. 3
0
 def expectation(self, hamiltonian, normalize=False):
     statec = K.conj(self.tensor)
     hstate = hamiltonian @ self.tensor
     ev = K.real(K.sum(statec * hstate))
     if normalize:
         norm = K.sum(K.square(K.abs(self.tensor)))
         ev = ev / norm
     return ev
Esempio n. 4
0
    def _normalize(self, state):
        """Normalizes state by summing the norms of each state piece.

        To be used after ``Collapse`` gates because normalization should be
        applied collectively and not in each piece seperately.
        The full calculation happens on CPU. (may not be efficient)
        """
        total_norm = 0
        with K.device(self.memory_device):
            for piece in state.pieces:
                total_norm += K.sum(K.square(K.abs(piece)))
            total_norm = K.cast(K.sqrt(total_norm), dtype=state.dtype)
            for piece in state.pieces:
                piece.assign(piece / total_norm)
Esempio n. 5
0
def fidelity(state1, state2):
    return K.abs(K.sum(K.qnp.conj(state2) * state1))**2
Esempio n. 6
0
 def state_vector_call(self, state):
     return K.sqrt(K.sum(K.square(K.abs(state))))
Esempio n. 7
0
 def state_vector_call(self, state):
     return K.abs(K.sum(self.statec * state))
Esempio n. 8
0
 def probabilities(self, qubits=None, measurement_gate=None):
     unmeasured_qubits = tuple(i for i in range(self.nqubits)
                               if i not in qubits)
     state = K.reshape(K.square(K.abs(K.cast(self.tensor))),
                       self.nqubits * (2, ))
     return K.sum(K.cast(state, dtype="float64"), axis=unmeasured_qubits)