Example #1
0
    def measure(self, qustate_format=None):
        """Measure the QuState

        Measurement of the state will result in one of the possible outcomes
        with the given probability. Also, the state WILL COLLAPSE to the
        outcome so this changes the state. The probability outcomes can be
        checked by having an ensemble of the same states and performing a
        measurement on each one. As the ensemble gets larger the probable
        outcomes will match the probability amplitudes magnitude squared
        """
        rand = random.random()
        start = 0
        index = 0
        for amplitude in self.ket:
            probability = measurement_probability(amplitude[0])
            if start <= rand <= start + probability:
                possibility = index
                self._collapse(possibility)
                if qustate_format == "bitstring":
                    return utils.int_to_bit_str(possibility, self._num_qubits)

                return utils.int_to_dirac_str(possibility, self._num_qubits)
            start += probability
            index += 1

        return None
Example #2
0
    def measure(self, qustate_format=None):
        """Measure the QuState

        Measurement of the state will result in one of the possible outcomes
        with the given probability. Also, the state WILL COLLAPSE to the
        outcome so this changes the state. The probability outcomes can be
        checked by having an ensemble of the same states and performing a
        measurement on each one. As the ensemble gets larger the probable
        outcomes will match the probability amplitudes magnitude squared
        """
        rand = random.random()
        start = 0
        index = 0
        for amplitude in self.ket:
            probability = measurement_probability(amplitude[0])
            if start <= rand <= start + probability:
                possibility = index
                self._collapse(possibility)
                if qustate_format == "bitstring":
                    return utils.int_to_bit_str(possibility, self._num_qubits)

                return utils.int_to_dirac_str(possibility, self._num_qubits)
            start += probability
            index += 1

        return None
Example #3
0
 def __str__(self):
     """Dirac notation of qubit """
     my_str = []
     index = 0
     for element in self.ket:
         if element:
             if my_str:
                 my_str.append(" + ")
             my_str.append(self._amplitude_str(element[0]))
             bit_str = utils.int_to_dirac_str(index, self._num_qubits)
             my_str.append(bit_str)
         index += 1
     return "".join(my_str)
Example #4
0
 def __str__(self):
     """Dirac notation of qubit """
     my_str = []
     index = 0
     for element in self.ket:
         if element:
             if my_str:
                 my_str.append(" + ")
             my_str.append(self._amplitude_str(element[0]))
             bit_str = utils.int_to_dirac_str(index, self._num_qubits)
             my_str.append(bit_str)
         index += 1
     return "".join(my_str)
Example #5
0
    def possible_measurements(self, qubit_index=-1):
        """Returns all possible measurements and their probability.

        The return value will be a map where the key is a string of the
        possible state and the value is its probability of outcome. You
        can return the measurement probability of a specific qubit by
        using qubit_index param. Example, the third qubit of the state
        1/sqrt(2)|0010> + 1/sqrt(2)|1110> can be 1 with probability 1

        Args:
            qubit_index: use if you want to know the possible
                         measurements of a specific qubit. First
                         qubit is 1.

        Raises:
            ValueError: if you specify a qubit that is out of range
        """
        states_map = {}
        index = 0
        for element in self.ket:
            if element:
                probablility = measurement_probability(element[0])
                dirac_str = utils.int_to_dirac_str(index, self._num_qubits)
                states_map[dirac_str] = probablility
            index += 1

        if qubit_index >= 0:
            if qubit_index > self._num_qubits:
                raise ValueError("qubit_index=%s is out of range" %
                                 str(qubit_index))
            qubit_map = {}
            for dirac_state in states_map:
                state_index = utils.dirac_str_to_int(dirac_state)
                amplitude = self._state[state_index][0]
                qubit = utils.DIRAC_STR % dirac_state[qubit_index]
                probablility = measurement_probability(amplitude)
                if qubit in qubit_map:
                    old_probability = qubit_map[qubit]
                    qubit_map[qubit] = old_probability + probablility
                else:
                    qubit_map[qubit] = probablility

            return qubit_map
        else:
            return states_map
Example #6
0
    def possible_measurements(self, qubit_index=-1):
        """Returns all possible measurements and their probability.

        The return value will be a map where the key is a string of the
        possible state and the value is its probability of outcome. You
        can return the measurement probability of a specific qubit by
        using qubit_index param. Example, the third qubit of the state
        1/sqrt(2)|0010> + 1/sqrt(2)|1110> can be 1 with probability 1

        Args:
            qubit_index: use if you want to know the possible
                         measurements of a specific qubit. First
                         qubit is 1.

        Raises:
            ValueError: if you specify a qubit that is out of range
        """
        states_map = {}
        index = 0
        for element in self.ket:
            if element:
                probablility = measurement_probability(element[0])
                dirac_str = utils.int_to_dirac_str(index, self._num_qubits)
                states_map[dirac_str] = probablility
            index += 1

        if qubit_index >= 0:
            if qubit_index > self._num_qubits:
                raise ValueError("qubit_index=%s is out of range" %
                                 str(qubit_index))
            qubit_map = {}
            for dirac_state in states_map:
                state_index = utils.dirac_str_to_int(dirac_state)
                amplitude = self._state[state_index][0]
                qubit = utils.DIRAC_STR % dirac_state[qubit_index]
                probablility = measurement_probability(amplitude)
                if qubit in qubit_map:
                    old_probability = qubit_map[qubit]
                    qubit_map[qubit] = old_probability + probablility
                else:
                    qubit_map[qubit] = probablility

            return qubit_map
        else:
            return states_map