<jupyter_start><jupyter_text>prepared by <NAME>                 This cell contains some macros. If there is a problem with displaying mathematical formulas, please run this cell to load these macros. $ \newcommand{\bra}[1]{\langle 1|} $$ \newcommand{\ket}[1]{|1\rangle} $$ \newcommand{\braket}[2]{\langle 1|2\rangle} $$ \newcommand{\dot}[2]{ 1 \cdot 2} $$ \newcommand{\biginner}[2]{\left\langle 1,2\right\rangle} $$ \newcommand{\mymatrix}[2]{\left( \begin{array}{1} 2\end{array} \right)} $$ \newcommand{\myvector}[1]{\mymatrix{c}{1}} $$ \newcommand{\myrvector}[1]{\mymatrix{r}{1}} $$ \newcommand{\mypar}[1]{\left( 1 \right)} $$ \newcommand{\mybigpar}[1]{ \Big( 1 \Big)} $$ \newcommand{\sqrttwo}{\frac{1}{\sqrt{2}}} $$ \newcommand{\dsqrttwo}{\dfrac{1}{\sqrt{2}}} $$ \newcommand{\onehalf}{\frac{1}{2}} $$ \newcommand{\donehalf}{\dfrac{1}{2}} $$ \newcommand{\hadamard}{ \mymatrix{rr}{ \sqrttwo & \sqrttwo \\ \sqrttwo & -\sqrttwo }} $$ \newcommand{\vzero}{\myvector{1\\0}} $$ \newcommand{\vone}{\myvector{0\\1}} $$ \newcommand{\vhadamardzero}{\myvector{ \sqrttwo \\  \sqrttwo } } $$ \newcommand{\vhadamardone}{ \myrvector{ \sqrttwo \\ -\sqrttwo } } $$ \newcommand{\myarray}[2]{ \begin{array}{1}2\end{array}} $$ \newcommand{\X}{ \mymatrix{cc}{0 & 1 \\ 1 & 0}  } $$ \newcommand{\Z}{ \mymatrix{rr}{1 & 0 \\ 0 & -1}  } $$ \newcommand{\Htwo}{ \mymatrix{rrrr}{ \frac{1}{2} & \frac{1}{2} & \frac{1}{2} & \frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & \frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} & \frac{1}{2} } } $$ \newcommand{\CNOT}{ \mymatrix{cccc}{1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0} } $$ \newcommand{\norm}[1]{ \left\lVert 1 \right\rVert } $$ \newcommand{\pstate}[1]{ \lceil \mspace{-1mu} 1 \mspace{-1.5mu} \rfloor } $   Solutions for  Cirq Introduction  Task 1    Create a quantum circuit with 10 qubits. 1. Apply $H$ gate to qubit 0. 2. Apply nine $CNOT$ gates where qubit $0$ is the control and qubit $i$ is the target for $i=1\cdots9$.Draw your circuit.  Solution<jupyter_code>import cirq
from cirq import H, CX

# Create quantum and classical registers with 10 qubits
qlist = cirq.LineQubit.range(10)

# Create a new circuit
circuit = cirq.Circuit()

# Apply H gate
circuit.append(H(qlist[0]))

for i in range(1,10):
    circuit.append(CX(qlist[0],qlist[i]))

# Single line solution
# circuit.append(CX(qlist[0],qlist[i]) for i in range(1,10))
    
print(circuit)<jupyter_output><empty_output><jupyter_text>Task 2    Create a quantum circuit with 10 qubits. 1. Apply $H$ gates to all qubits.2. Apply $X$ gate to qubit 0 controlled by qubits 1-92. Apply $H$ gates to all qubits.Draw your circuit.  Solution<jupyter_code>import cirq
from cirq import H, X

# Create quantum and classical registers with 10 qubits
qlist = cirq.LineQubit.range(10)

# Create a new circuit
circuit = cirq.Circuit()

# Apply H to all qubits
circuit.append(H.on_each(*qlist))

# Qubits 1-9 are the control and qubit 0 is the target
# Using controlled by is more convenient in this case
circuit.append(X(qlist[0]).controlled_by(*qlist[1:10]))

# Apply H to all qubits
circuit.append(H.on_each(*qlist))

# Draw circuit
print(circuit)<jupyter_output><empty_output><jupyter_text>Task 3    Implement the circuit in Task 1. Measure all the qubits and simulate your circuit for 1000 times.  Solution<jupyter_code>import cirq
from cirq import H, CX, measure

# Create quantum and classical registers with 10 qubits
qlist = cirq.LineQubit.range(10)

# Create a new circuit
circuit = cirq.Circuit()

# Apply H gate
circuit.append(H(qlist[0]))

# Apply CNOT
circuit.append(CX(qlist[0],qlist[i]) for i in range(1,10))

# Measure both qubits, result is the label
circuit.append(measure(*qlist, key='result'))

# This is the local simulator 
s = cirq.Simulator()

# circuit is the circuit to be simulated
# repetition is the how many times we want to run the circuit
samples=s.run(circuit, repetitions=1000)

def bitstring(bits):
    return "".join(str(int(b)) for b in bits)

counts = samples.histogram(key="result",fold_func=bitstring)
print(counts)<jupyter_output><empty_output><jupyter_text>Task 4    Create a quantum circuit with 4 qubits. Apply Hadamard gate to each qubit and $CZ$ gate to qubits 0 and 1. Use the simulator without measuring the circuit. Check the entries with negative sign.  Solution<jupyter_code>import cirq
from cirq import H, CZ

# Create 4 quantum bits
qlist = cirq.LineQubit.range(4)

# Create a new circuit
circuit = cirq.Circuit()

# Apply H gate
circuit.append(H.on_each(*qlist))

# Apply CZ
circuit.append(CZ(qlist[0],qlist[1]))

s = cirq.Simulator()
results=s.simulate(circuit)
print(results)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1729021348552103, 'fraction_numerical': 0.03614457831325301, 'mean_word_length': 4.420389461626575, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '134682', 'license': 'Apache-2.0', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1640, 'n_words': 465, 'path': 'nickel/D00_Cirq_Introduction_Solutions.ipynb', 'repo_name': 'asif-saad/qNickel', 'size': '4737'}