<jupyter_start><jupyter_text>1.7 Exercise 7 - Random Number Generator---In this exercise we want to demonstrate a pratical use of a Quantum Computer. To get us started, let us first explain how it so happens that random numbers, generated by computers, aren't so random after all.. Weird right? Here you were thinking that random numbers were random, and then some text from the internet tells you that you're wrong! But, why is are random numbers in fact not random? Well, let <NAME>, Professor of Computer Science and Engineering at MIT’s Computer Science and Artificial Intelligence Laboratory explain:“One thing that traditional computer systems aren’t good at is coin flipping. They’re deterministic, which means that if you ask the same question you’ll get the same answer every time. In fact, such machines are specifically and carefully programmed to eliminate randomness in results. They do this by following rules and relying on algorithms when they compute.”So, let's say that you want to play a game where you are in position of a digital computer generating random numbers. You and your friends then have to generate an equal amount of random numbers. The person who gets most numbers matching or closest to the numbers generated by the computer wins.Sounds fun right? Well you are actually wrong. It just so happens that all of your friends are computers and because computers can only generate psudo-random numbers, your friends are able to guess the most numbers right. Not so fun, am I right? --- So what do you do?---Of course you develop a simple algorithm with a quantum computer, which no classical computer is able to predict!And given that there are only a minimal amount of quantum computers in the world the posibility of you being friends with one of them, is very unlikely.. --- 1.7.0 Import of libraries for the programYou know this part<jupyter_code>from qiskit import * 
# You should know by now that this is how you build a Quantum Circuit. This time with only a single Qubit.
qr = QuantumRegister(1)

cr = ClassicalRegister(1)

circuit = QuantumCircuit(qr, cr)
# Here we add an H gate, the gate that puts Qubits in superposition
circuit. # ⚠️ Finish the command by adding a H gate to your first and only Qubit in the circuit

# Lastly a measure of the Qubit
circuit.measure(qr, cr);<jupyter_output><empty_output><jupyter_text>--- 1.7.1 Setting up our backendFirstly, we choose a backend, like before. To save you from waiting we have chosen the simulator backend. If you want, you can set up the real Quantum Computer like earlier.. if you think you know how by now.    Note: In this example we are running on a quantum simulator, but to get a truly random number we would need to run it on a real quantum computer.<jupyter_code>backend = BasicAer.get_backend('qasm_simulator')<jupyter_output><empty_output><jupyter_text>--- 1.7.2 Running our programSecondly, we run our circuit. You might notice that it says "shots=8" and "memory=True"Shots is the amount of times the circuit will run. Memory=True just tells the computer to store the result from each Shot for later access.<jupyter_code>job = execute(circuit, backend, shots=8, memory=True)
data = job.result().get_memory()
print(data)<jupyter_output><empty_output><jupyter_text>The operation returns a list containing ones and zeros. The techy people out there will notice that this is similar to a byte. Because bytes represent numeric numbers, we can transform this list into just that, a numeric number, that we all recognise. --- 1.7.3 Extracting the byte from the list generated aboveThirdly, we need to do a little bit of programming magic to transform the byte to a number. - For the less techy people: We take the list, remove the brackets and commas. This way the numbers are displayed nicely in a row.- For the techy people: You probably know what's going on here.<jupyter_code>string = ""
for index in range (0,len(data)):
    string += data[index]
print(string)<jupyter_output><empty_output><jupyter_text>The string above contains the 1 and 0's from the list, and are now ready to be transformed into their numeric value! --- 1.7.4 Converting a byte to its numeric valueFourthly, we convert the byte to an easier to read numeric number. The simple piece of code below is what does the transformation from string containing a binary number to numeric number. Easy.<jupyter_code>int(string, 2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0540846345326997, 'fraction_numerical': 0.005883684091423399, 'mean_word_length': 4.688545688545688, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '120635', 'license': 'MIT', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1090, 'n_words': 667, 'path': 'notebook-exercises/1.7 - Random Number Generator.ipynb', 'repo_name': 'gubiithefish/IBM-Quantum-Experience-Hands-on-Session2', 'size': '4423'}