def generate_random_number(): """Generates a random number between 0-15. Returns: int: A random number between 0-15. """ # Allocate 4 qubits. qubits = cirq.LineQubit.range(4) # Set up circuit. # Apply Hadamard gate to all qubits. circuit = cirq.Circuit() for qubit in qubits: circuit.append(cirq.H(qubit)) # Measure all qubits. for i in range(4): circuit.append(cirq.measure(qubits[i], key=f'qubit{i}')) # Initialise simulator and run circuit. simulator = cirq.Simulator() result = simulator.run(circuit, repetitions=1) # Convert result as binary digits into integer. qubit_results = [] for i in range(4): qubit_results.append(result.data[f'qubit{i}'][0]) random_number = big_endian_bits_to_int(qubit_results) # Return random number. return random_number
def _tuple_of_big_endian_int(bit_groups: Iterable[Any]) -> Tuple[int, ...]: """Returns the big-endian integers specified by groups of bits. Args: bit_groups: Groups of descending bits, each specifying a big endian integer with the 1s bit at the end. Returns: A tuple containing the integer for each group. """ return tuple(value.big_endian_bits_to_int(bits) for bits in bit_groups)
def data(self) -> pd.DataFrame: if self._data is None: # Convert to a DataFrame with columns as measurement keys, rows as # repetitions and a big endian integer for individual measurements. converted_dict = {} for key, val in self._measurements.items(): converted_dict[key] = [ value.big_endian_bits_to_int(m_vals) for m_vals in val ] self._data = pd.DataFrame(converted_dict) return self._data
def data(self) -> pd.DataFrame: if self._data is None: # Convert to a DataFrame with columns as measurement keys, rows as # repetitions and a big endian integer for individual measurements. converted_dict = {} for key, val in self._measurements.items(): converted_dict[key] = [value.big_endian_bits_to_int(m_vals) for m_vals in val] # Note that when a numpy array is produced from this data frame, # Pandas will try to use np.int64 as dtype, but will upgrade to # object if any value is too large to fit. self._data = pd.DataFrame(converted_dict, dtype=np.int64) return self._data
def dataframe_from_measurements( measurements: Mapping[str, np.ndarray]) -> pd.DataFrame: """Converts the given measurements to a pandas dataframe. This can be used by subclasses as a default implementation for the data property. Note that subclasses should typically memoize the result to avoid recomputing. """ # Convert to a DataFrame with columns as measurement keys, rows as # repetitions and a big endian integer for individual measurements. converted_dict = { key: [value.big_endian_bits_to_int(m_vals) for m_vals in val] for key, val in measurements.items() } # Note that when a numpy array is produced from this data frame, # Pandas will try to use np.int64 as dtype, but will upgrade to # object if any value is too large to fit. return pd.DataFrame(converted_dict, dtype=np.int64)