<jupyter_start><jupyter_text>Coin toss quantum circuitIn this exercise we create a quantum circuit that simulates the probabilistic nature of a single qubit in superposition. The one qubit circuit initializes the qubit in the ground state $|0\rangle$ and then uses a Hadamard gate to put the qubit in superposition $|\psi\rangle = \left(|0\rangle+|1\rangle\right)/\sqrt{2}.$  Measuring the qubit causes it to collapse into one of the states $|0\rangle$ or $|1\rangle$ with a 50% probability, i.e. a coin toss.   In this exercise we introduce the Hadamard gate, which puts a qubit in superposition.```        ┌───┐  q_0: |0>┤ H ├          └───┘  ```We also introduce the X gate that flips the qubit from $|0\rangle$ to $|1\rangle$ and vice versa.```        ┌───┐q_0: |0>┤ X ├        └───┘``` Import the required libraries.<jupyter_code>from qiskit import QuantumCircuit, execute, Aer

# Import Blochsphere visualization
from qiskit.visualization import plot_bloch_multivector, plot_histogram<jupyter_output><empty_output><jupyter_text>As we will be using the Bloch sphere visualization (`plot_bloch_multivector`) a bit, here's a quick function that calculates the state vector ($|\psi\rangle$) for the circuit to let you display the Bloch vector for any given state.<jupyter_code>def get_psi(circuit): 
    global psi
    backend = Aer.get_backend('statevector_simulator') 
    psi = execute(circuit, backend).result().get_statevector(circuit)<jupyter_output><empty_output><jupyter_text>Create an empty quantum circuit. We start out with the qubit in the $|0\rangle$ state.<jupyter_code>qc = QuantumCircuit(1,1)

# Print out the circuit
print(qc)

# Display the Bloch sphere
get_psi(qc)
plot_bloch_multivector(psi)<jupyter_output>q_0: |0>
        
 c_0: 0<jupyter_text>Add a Hadamard (super position) gate to the quantum circuit. This puts the qubit in a superposition: $|\psi\rangle = \left(|0\rangle+|1\rangle\right)/\sqrt{2}.$<jupyter_code>qc.h(0)

# Print out the circuit
print(qc)

# Display the Bloch sphere
get_psi(qc)
plot_bloch_multivector(psi)<jupyter_output>┌───┐
q_0: |0>┤ H ├
        └───┘
 c_0: 0 ═════<jupyter_text>Finally, add a measurement gate to complete the circuit.<jupyter_code># Add measure gate
qc.measure(0,0)
print(qc)<jupyter_output>┌───┐┌─┐
q_0: |0>┤ H ├┤M├
        └───┘└╥┘
 c_0: 0 ══════╩═<jupyter_text>Set the backend to a local simulator.<jupyter_code>backend = Aer.get_backend('qasm_simulator')<jupyter_output><empty_output><jupyter_text>Create a quantum job that runs just one shot to simulate a coin toss. Then run the job and display the result; either 0 for up (base) or 1 for down (excited). Display the result as a histogram.<jupyter_code>job = execute(qc, backend, shots=1)
counts  = job.result().get_counts()
print(counts)
plot_histogram(counts)<jupyter_output>{'1': 1}<jupyter_text>Now, lets run a thousand coin tosses in a row and see what we get.<jupyter_code>job = execute(qc, backend, shots=1000)
counts  = job.result().get_counts()
print(counts)
plot_histogram(counts)<jupyter_output>{'1': 523, '0': 477}<jupyter_text>In the histogram we see that we get 0 and 1 with ~50% probability. Which is what we expect from tossing some coins. We can also do our coin flip with the qubit starting in the $|1\rangle$ state by first flipping the qubit by using the X gate. The Hadamard gate still flips the qubit to the equator, but now on the -X side.<jupyter_code>qc2 = QuantumCircuit(1,1)
qc2.x(0)
qc2.h(0)
print(qc2)
# Display the Bloch sphere
get_psi(qc2)
plot_bloch_multivector(psi)<jupyter_output>┌───┐┌───┐
q_0: |0>┤ X ├┤ H ├
        └───┘└───┘
 c_0: 0 ══════════<jupyter_text>Add the measure gate and run the circuit 1,000 times.<jupyter_code>qc2.measure(0,0)

job2 = execute(qc2, backend, shots=1000)
counts2  = job2.result().get_counts()
print(counts2)
plot_histogram(counts2)<jupyter_output>{'1': 527, '0': 473}
{'fraction_non_alphanumeric': 0.12470832253046409, 'fraction_numerical': 0.024630541871921183, 'mean_word_length': 5.2326332794830375, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 40, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '121661', 'license': 'Apache-2.0', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1367, 'n_words': 439, 'path': 'qiskit_advocates/meetups/09_23_19-Hassi_Norlen-Morgan_State_Uni/1_1_coin_toss.ipynb', 'repo_name': 'ordmoj/qiskit-community-tutorials', 'size': '3857'}