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
def init_from_vector(cls, vector): """Create a QuState from a column vector. This method will create a QuState given a raw column vector as input. Example: [[.707], [0], [0], [.707]] Will create the QuState .707|00> + .707|11> Args: vector: a vector representing a state in Hilbert Space Note that this must have size length 2^n or else it is considered invalid. """ state_map = {} vector_len = len(vector) if vector_len % 2: message = ("Vector representations of quantum states must be a " "power of 2. Your vector has length %s" % str(vector_len)) raise ValueError(message) if len(vector): dimensionality = int(math.log(len(vector), 2)) i = 0 for element in vector: if element: bit_str = utils.int_to_bit_str(i, dimensionality) state_map[bit_str] = element[0] i += 1 return cls(state_map)
def init_from_vector(cls, vector): """Create a QuState from a column vector. This method will create a QuState given a raw column vector as input. Example: [[.707], [0], [0], [.707]] Will create the QuState .707|00> + .707|11> Args: vector: a vector representing a state in Hilbert Space Note that this must have size length 2^n or else it is considered invalid. """ state_map = {} vector_len = len(vector) if vector_len % 2: message = ("Vector representations of quantum states must be a " "power of 2. Your vector has length %s" % str(vector_len)) raise ValueError(message) if vector.any(): dimensionality = int(math.log(len(vector), 2)) i = 0 for element in vector: if element: bit_str = utils.int_to_bit_str(i, dimensionality) state_map[bit_str] = element[0] i += 1 return cls(state_map)
def test_init_from_map(self): self.assertRaises(qudot_errors.InvalidQuStateError, lambda : qudot.QuState(None)) self.assertRaises(qudot_errors.InvalidQuBitError, lambda : qudot.QuState({"}": 1})) # make map with even states in 4D Hilbert space qubit_map = {} for i in range(0, 8): if not i % 2: if i == 6: qubit_map[qudot_utils.int_to_bit_str(i, 4)] = 0.5j else: qubit_map[qudot_utils.int_to_bit_str(i, 4)] = 0.5 qu_state = qudot.QuState(qubit_map) column_vector = get_column_vector(self.base_vector) row_vector = get_row_vector(self.adj_vector) vectors_equal(column_vector, qu_state.ket) vectors_equal(row_vector, qu_state.bra) self.assertTrue(4 == qu_state.num_qubits) self.assertTrue(2**4 == qu_state.hilbert_dimension)
def test_init_from_map(self): self.assertRaises(qudot_errors.InvalidQuStateError, lambda: qudot.QuState(None)) self.assertRaises(qudot_errors.InvalidQuBitError, lambda: qudot.QuState({"}": 1})) # make map with even states in 4D Hilbert space qubit_map = {} for i in range(0, 8): if not i % 2: if i == 6: qubit_map[qudot_utils.int_to_bit_str(i, 4)] = 0.5j else: qubit_map[qudot_utils.int_to_bit_str(i, 4)] = 0.5 qu_state = qudot.QuState(qubit_map) column_vector = get_column_vector(self.base_vector) row_vector = get_row_vector(self.adj_vector) vectors_equal(column_vector, qu_state.ket) vectors_equal(row_vector, qu_state.bra) self.assertTrue(qu_state.num_qubits == 4) self.assertTrue(qu_state.hilbert_dimension == 2**4)
def qft_adder(qu_state1, value): """Add using the qft add algorithm. Args: qu_state1: the first quantum state value: a classical value bit string """ num_qubits = qu_state1.num_qubits qftn = algorithms.qft(num_qubits) qu_state1.apply_gate(qftn) state2 = utils.int_to_bit_str(value, num_qubits) for i in range(0, num_qubits): qubit = num_qubits - i for bit in range(1 + i, num_qubits + 1): ripple = bit - i if state2[bit - 1] == "1": phase_gate = qudot.QuGate.init_phase_gate(ripple) qu_state1.apply_gate(phase_gate, [qubit]) iqftn = algorithms.inverse_qft(num_qubits) qu_state1.apply_gate(iqftn)
def qft_adder(qu_state1, value): """Add using the qft add algorithm. Args: qu_state1: the first quantum state value: a classical value bit string """ num_qubits = qu_state1.num_qubits qftn = algorithms.qft(num_qubits) qu_state1.apply_gate(qftn) state2 = utils.int_to_bit_str(value, num_qubits) for i in range(0, num_qubits): qubit = num_qubits - i for bit in range(1+i, num_qubits+1): ripple = bit-i if state2[bit-1] == "1": phase_gate = qudot.QuGate.init_phase_gate(ripple) qu_state1.apply_gate(phase_gate, [qubit]) iqftn = algorithms.inverse_qft(num_qubits) qu_state1.apply_gate(iqftn)