<jupyter_start><jupyter_text>Hands-on Exercise1. Create a one qbit circuit and Run it to show the superposition phenomenon (uses the Hadamard gate)2. Create and Run the Bell State Circuit to show the entanglement phenomenon (uses the CNOT gate)3. Homework exercises to learn other gates<jupyter_code><jupyter_output><empty_output><jupyter_text>Environment setup<jupyter_code>try:
    import cirq
except ImportError:
    print("installing cirq...")
    !pip install --quiet cirq
    print("installed cirq.")<jupyter_output><empty_output><jupyter_text>Example-1: Create Superposition - The Hadarmard GateThe Hadamard gate corresponds to multiplying the qbit with the following matrix$ H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}$.This maps the basis state $|0\rangle$ to $\frac{|0\rangle + |1\rangle}{\sqrt{2}}$ and  $|1\rangle$ to $\frac{|0\rangle - |1\rangle}{\sqrt{2}}$This means that the qbit will be 50% of time on 0 and 50% of on 1.So, to see this happening we need to do three steps:1. Create a circuit with Hadamard gate 2. Simulate its execution3. Measure the results  Create the circuit with the Hadamard gate<jupyter_code># Create a cicuit to put one qbit in superposition 
hello_world_circuit = cirq.Circuit()
q = cirq.NamedQubit("qbit_hello_from_quantum_realm")
hello_world_circuit.append(cirq.H(q))
print(hello_world_circuit)<jupyter_output>qbit_hello_from_quantum_realm: ───H───<jupyter_text>Simulate(execute) the circuit<jupyter_code># Initialize Simulator
s=cirq.Simulator()

print('Simulate the circuit:')
results=s.simulate(hello_world_circuit)
print(results)
print()<jupyter_output>Simulate the circuit:
measurements: (no measurements)
output vector: 0.707|0⟩ + 0.707|1⟩<jupyter_text>Run(sample) the resultsThis is necessary because we are not using a Quantum Computer here. The results printed before are only single-point measure.1. First we need to add a measurement gate2. Second we call run, which does the sampling<jupyter_code>#Add a measurement so we can emulate the superposition by sampling the result
hello_world_circuit.append(cirq.measure(q, key='result'))
print(hello_world_circuit)
# Now we can sample, which is done by the command run
print('Sample the circuit:')
samples=s.run(hello_world_circuit, repetitions=1000)
# Print a histogram of results
print(samples.histogram(key='result'))<jupyter_output>Sample the circuit:
Counter({0: 519, 1: 481})<jupyter_text>Example-2: Create the Bell State Circuit that we learned in classBesides the Hadarmard Gate, we will use the Control Not (CNOT) gate.The CNOT gate flips the second qbit (called the target qbit) if and only if the first qbit (the control qbit) is  ${\displaystyle |1\rangle }$.   * So if we apply the CNOT gate to the quantum state:        $a|00\rangle + b|01\rangle + c|10\rangle + d|11\rangle$            we will obtain this:      $a|00\rangle + b|01\rangle + c|11\rangle + d|10\rangle$<jupyter_code># Create a circuit to generate a Bell State:
# 1/sqrt(2) * ( |00⟩ + |11⟩ )
bell_circuit = cirq.Circuit()
q0, q1 = cirq.LineQubit.range(2)
bell_circuit.append(cirq.H(q0))
bell_circuit.append(cirq.CNOT(q0,q1))

# print circuit
print("Bell States circuit")
print(bell_circuit)<jupyter_output>Bell States circuit
0: ───H───@───
          │
1: ───────X───<jupyter_text>Simuate the executiong of the circuit<jupyter_code># Initialize Simulator
s=cirq.Simulator()

print('Simulate the circuit:')
results=s.simulate(bell_circuit)
print(results)
print()<jupyter_output>Simulate the circuit:
measurements: (no measurements)
output vector: 0.707|00⟩ + 0.707|11⟩<jupyter_text>Run(sample) the results<jupyter_code># However for sampling, we need to add a measurement at the end
bell_circuit.append(cirq.measure(q0, q1, key='result'))
print(bell_circuit)


# Now we can sample, which is done by the command run
print('Sample the circuit:')
samples=s.run(bell_circuit, repetitions=2000)
# Print a histogram of results
print(samples.histogram(key='result'))

#create a copy of previous circuit to compare measurements
bell_circuit_2 = cirq.Circuit()
q0, q1 = cirq.LineQubit.range(2)
bell_circuit_2.append(cirq.H(q0))
bell_circuit_2.append(cirq.CNOT(q0,q1))

# Initialize Simulator
s=cirq.Simulator()

print('Simulate the circuit 2:')
results=s.simulate(bell_circuit)
print(results)
print()

#Add two measurements
bell_circuit_2.append(cirq.measure(q0, key='result q0'))
bell_circuit_2.append(cirq.measure(q1, key='result q1'))
print(bell_circuit_2)

print('Sample the circuit 2:')
samples=s.run(bell_circuit_2, repetitions=2000)
# Print a histogram of results
print(samples.histogram(key='result q0'))
print(samples.histogram(key='result q1'))<jupyter_output>Simulate the circuit 2:
measurements: result=00
output vector: |00⟩

0: ───H───@───M('result q0')───
          │
1: ───────X───M('result q1')───
Sample the circuit 2:
Counter({1: 1002, 0: 998})
Counter({1: 1002, 0: 998})
{'fraction_non_alphanumeric': 0.12162162162162163, 'fraction_numerical': 0.03153153153153153, 'mean_word_length': 5.978571428571429, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '42461', 'license': 'MIT', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1654, 'n_words': 492, 'path': 'Intro_BellStatesCircuit.ipynb', 'repo_name': 'hpi-sam/QuantumProgramming', 'size': '4884'}