<jupyter_start><jupyter_text>MeasurementsQubits are a superposition of $|0\rangle$ and $|1\rangle$. We write an arbitrary qubit $|\psi\rangle$ as:$|\psi\rangle = \alpha|0\rangle + \beta|1\rangle$When a measurement is made, however, the output will be either 0 or 1. There is a $|\alpha^{2}|$ probability that the measurement will be 0 and a $|\beta^{2}|$ probability that the measurement will be 1.Because the measurement is either 0 or 1, $|\alpha^{2}| + |\beta^{2}| = 1$To try to guess $|\alpha^{2}|$ and $|\beta^{2}|$, a quantum computer (or simulator) can count each measurement and calculate the count divided by number of tries to discover the approximate probabilities $|\alpha^{2}|$ and $|\beta^{2}|$.We may extract the counts of each measurement from a circuit using the **qasm_simulator** in Qiskit, or running on a real quantum computer on IBM Q Experience.<jupyter_code>from qiskit import *
from qiskit.visualization import plot_histogram<jupyter_output><empty_output><jupyter_text>The Qiskit simulators are available in the _Aer_ module. For this notebook, we will explore mostly the **qasm_simulator** because we are interested in simulating the measurements that a quantum computer would give.<jupyter_code>simulator = Aer.get_backend("qasm_simulator")<jupyter_output><empty_output><jupyter_text>By default, all qubits start as $|0\rangle$. We can verify this by using an empty circuit. In the following code, we use a system with no gates and one measurement device to ensure that 0 is always measured.<jupyter_code>circuit = QuantumCircuit(1, 1)
circuit.measure(0, 0)
circuit.draw(output="mpl")
job = execute(circuit, backend=simulator, shots=2**15)
result = job.result()
counts = result.get_counts()
print(counts)<jupyter_output>{'0': 32768}<jupyter_text>The simulator returns a dictionary where each key is a measurement and each value is the count of that measurement. Often, it is useful to have a histogram of the counts: that is possible with the **plot_histogram** from *qiskit.visualization*<jupyter_code>plot_histogram(counts)<jupyter_output><empty_output><jupyter_text>For a single qubit, there are two possible measurements: 0 and 1. To achieve a $|1\rangle$ component in our qubit, we can use a $X$ gate<jupyter_code>circuit = QuantumCircuit(1, 1)
circuit.x(0)
circuit.measure(0, 0)
circuit.draw(output="mpl")
counts = execute(circuit, backend=simulator, shots=2**15).result().get_counts()
plot_histogram(counts)<jupyter_output><empty_output><jupyter_text>If there is superposition, then it is possible that some of the times the measurement will be 0 and others it will be 1.An example of gate that generates superposition is $H$. Let's see how the measurements perform when that happens<jupyter_code>circuit = QuantumCircuit(1, 1)
circuit.h(0)
circuit.measure(0, 0)
circuit.draw(output="mpl")
counts = execute(circuit, backend=simulator, shots=2**15).result().get_counts()
plot_histogram(counts)<jupyter_output><empty_output><jupyter_text>Global PhaseOne possible thought is: is it possible to differentiate between $|0\rangle$, $-|0\rangle$ or $i|0\rangle$?We can build circuits to achieve those states and compare!<jupyter_code># Circuit to generate -|0>
circuit = QuantumCircuit(1, 1)
circuit.x(0)
circuit.z(0)
circuit.x(0)
circuit.measure(0, 0)
circuit.draw(output="mpl")
counts = execute(circuit, backend=simulator, shots=2**15).result().get_counts()
plot_histogram(counts)
# Circuit to generate i|0>
circuit = QuantumCircuit(1, 1)
circuit.x(0)
circuit.y(0)
circuit.measure(0, 0)
circuit.draw(output="mpl")
counts = execute(circuit, backend=simulator, shots=2**15).result().get_counts()
plot_histogram(counts)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11482993197278911, 'fraction_numerical': 0.021496598639455782, 'mean_word_length': 6.658333333333333, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '559862', 'license': 'MIT', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1113, 'n_words': 392, 'path': 'Basic_Quantum/Measurements.ipynb', 'repo_name': 'IvanIsCoding/Quantum', 'size': '3675'}