<jupyter_start><jupyter_text>prepared by <NAME> (<EMAIL>) | November 07, 2018 I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.$ \newcommand{\bra}[1]{\langle 1|} $$ \newcommand{\ket}[1]{|1\rangle} $$ \newcommand{\braket}[2]{\langle 1|2\rangle} $$ \newcommand{\inner}[2]{\langle 1,2\rangle} $$ \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 } $ SuperpositionThere is no classical counterpart of the concept "superposition".But, we can still use a classical analogy that might help us to give some intuitions. Probability distribution Suppose that Asja starts in $ \myvector{1\\0} $ and secretly applies the probabilistic operator $ \mymatrix{cc}{ 0.3 & 0.6 \\ 0.7 & 0.4 } $.Because she applies her operator secretly, our information about her state is probabilistic, which is calculated as$$    \myvector{0.3 \\ 0.7} = \mymatrix{cc}{ 0.3 & 0.6 \\ 0.7 & 0.4 } \myvector{1\\0}.$$Asja is either in state 0 or in state 1.However, from our point of view, Asja is in state 0 and state 1 with probabilities $ 0.3 $ and $ 0.7 $, respectively.We can say that Asja in a probability distribution of states 0 and 1, being in both states at the same time.On the other hand, if we observe Asja's state, then our information about Asja becomes deterministic: either $ \myvector{1 \\ 0} $ or $ \myvector{0 \\ 1} $.We can say that after measurement the probabilistic state $ \myvector{0.3 \\ 0.7}  $ collapses to either $ \myvector{1 \\ 0} $ or $ \myvector{0 \\ 1} $.  The third experiment Now, we can explain the following experiment.  The initial Step The photon is in state $ \ket{v_0} = \vzero $.  The first step Hadamard is applied:$ \ket{v_1} = \hadamard \vzero = \vhadamardzero $.At this point, the photon is in a superposition of state $ \ket{0} $ and state $ \ket{1} $, being in both states with the amplitudes $ \frac{1}{\sqrt{2}} $ and $ \frac{1}{\sqrt{2}} $, respectively.The state of photon is $ \ket{v_1} = \vhadamardzero $, and we can represent it also as follows:$ \ket{v_1} =  \frac{1}{\sqrt{2}} \ket{0} + \frac{1}{\sqrt{2}} \ket{1} $.  The second step Hadamard is applied again:We write the affect of Hadamard on states $ \ket{0} $ and $ \ket{1} $ as follows:$ H \ket{0} = \frac{1}{\sqrt{2}} \ket{0} + \frac{1}{\sqrt{2}} \ket{1} $$ H \ket{1} = \frac{1}{\sqrt{2}} \ket{0} - \frac{1}{\sqrt{2}} \ket{1} $This representation helps us to see clearly why the state $ \ket{1} $ disappears.Now, let's see the affect of Hadamard on the quantum state $ \ket{v_1} =  \frac{1}{\sqrt{2}} \ket{0} + \frac{1}{\sqrt{2}} \ket{1} $:$ \ket{v_2} = H \ket{v_1} = H \mybigpar{ \frac{1}{\sqrt{2}} \ket{0} + \frac{1}{\sqrt{2}} \ket{1} } = \frac{1}{\sqrt{2}} H \ket{0} + \frac{1}{\sqrt{2}} H \ket{1} $We can replace $ H\ket{0} $ and $ H\ket{1} $ as described above.$ \ket{v_2} $ is formed by the summation of the following terms:$ \frac{1}{\sqrt{2}} H \ket{0} = \frac{1}{2} \ket{0} + \frac{1}{2} \ket{1}  $$ \frac{1}{\sqrt{2}} H \ket{1} = \frac{1}{2} \ket{0} - \frac{1}{2} \ket{1}  $The amplitude of $ \ket{0} $ becomes 1, but the amplitude of $ \ket{1} $ becomes 0 because of cancellation.$ \ket{v_2} = 1 \cdot \ket{0} + 0 \cdot \ket{1} = \ket{0} $.The photon was in both states at the same time with certain amplitudes.After the second Hadamard, the results are interfered with each other.The interference can be constructive or destructive.In our examples, the resulting $ \ket{0} $s are interfered constructively, but the resulting $ \ket{1} $s are interfered destructively.  Observations Probabilistic systems: If there is a nonzero transition to a state, then it contributes to the probability of this state positively. Quantum systems: If there is a nonzero transition to a state, then we cannot make such an interpretation without knowing the other transtions to this state.If it is the only transition, then it contributes to the amplitude (and probability) of the state, and it does not matter whether the sign of the transition is positive or negative.If there is more than one transition, then depending on the summation of all transitions, we can determine whether a specific transition contributes or not.As a simple rule, if the final amplitude of the state and nonzero transition have the same sign, then it is a positive contribution; and, if they have the opposite signs, then it is a negative contribution.  Task 1 [on paper]Start in state $ \ket{u_0} =  \ket{1} $.Apply Hadamard operator to $ \ket{u_0} $, i.e, find $ \ket{u_1} = H \ket{u_0} $.Apply Hadamard operator to $\ket{u_1}$, i.e, find $ \ket{u_2} = H \ket{u_1} $.Observe the constructive and destructive interferences, when calculating $ \ket{u_2} $.  Being in a superposition A quantum system can be in more than one state with nonzero amplitudes.Then, we say that our system is in a superposition of these states.When evolving from a superposition, the resulting transitions may affect each other constructively and destructively. This can happen only because of having both negative or positive amplitudes. Otherwise, all nonzero transitions are added up to each other as in probababilistic systems.  Measurement We can measure a quantum system, and then the system is observed in one of its states.This is the most basic measurement in quantum computing. (There are more generic measurement operators, but we will not cover them.)The probability of the system to be observed in a specified state is the square value of its amplitude.     If the amplitude of a state is zero, then this state cannot be observed.      If the amplitude of a state is nonzero, then this state can be observed. For example, if the system is in quantum state $$    \myrvector{ -\frac{\sqrt{2}}{\sqrt{3}} \\ \frac{1}{\sqrt{3}} },$$then, after a measurement, we can observe the system in state $\ket{0} $ with probability $ \frac{2}{3} $ and in state $\ket{1}$ with probability $ \frac{1}{3} $.  Collapsing After the measurement, the system collapses to the observed state, and so the system is no longer in a superposition.Thus, the information kept in a superposition is lost. In the above example, when the system is observed in state $\ket{0}$, then the new state becomes $ \myvector{1 \\ 0} $. If it is observed in state $\ket{1}$, then the new state becomes $ \myvector{0 \\ 1} $.  Task 2 We have a quantum system with four states: $\ket{00}$, $ \ket{01} $, $\ket{10}$, and $ \ket{11} $.We can also say that our system has two qubits.Suppose that the system is in the following state:$ \myrvector{ \dfrac{ 1 }{  \sqrt{3} - \sqrt{ 5 + 2\sqrt{6}} } \\ \\ \dfrac{1}{ \sqrt{3} - \sqrt{ 7 + 2\sqrt{12} } } \\ \\ \dfrac{  1 }{ \sqrt{5} - \sqrt{  13 + 2\sqrt{40} }  } \\ \\ \dfrac{1}{ \sqrt{ 7 } - \sqrt{ 15 + 2 \sqrt{56} } } }.  $  Find the probability of observing the system in state $\ket{00}$, $ \ket{01} $, $\ket{10}$, or $ \ket{11} $.  You may write a function to calculate the dominator of each fraction automatically, where its value is determined by three values $a$, $ b$, and $ c $ by assuming the form $  \sqrt{a} - \sqrt{b + 2 \sqrt{c} } $.  Verify that the total probability is 1 (or almost 1).<jupyter_code>#
# your solution is here
#<jupyter_output><empty_output><jupyter_text>click for our solution  Z-gate (operator) The indentity operator $ I = \mymatrix{cc}{1 & 0 \\ 0 & 1} $ does not affect the computation.What about the following operator?$ Z = \Z $.It is very similar to the identity operator.Consider the quantum state $ \ket{u} = \myvector{ \frac{3}{5} \\ \frac{4}{5} }  $.Let's calculate the new quantum state after appying $ Z $ to $ \ket{u} $:$ \ket{u'} = Z \ket{u} =  \Z  \myvector{ \frac{3}{5} \\ \frac{4}{5} } =  \myrvector{ \frac{3}{5} \\ -\frac{4}{5} } $. The quantum states $ \ket{u} $ and $ \ket{u'} $ look similar. The probabilities of observing the state 0 and state 1 are the same when the system is in $ \ket{u} $ or $ \ket{u'} $.On the other hand, they are far away from each other as shown below: For example, by applying Hadamard to each of them, the probability of observing the state 0 and state 1 may change (?)To observe this, we can do a simple experiment.   Task 3 Create a quantum ciruit with 5 qubits.Apply h-gate (Hadamard operator) to each qubit.Apply z-gate ($Z$ operator) to randomly picked qubits. (e.g., $ mycircuit.z(qreg[i]) $)Apply h-gate to each qubit Measure each qubit.Execute your program 1000 times.Compare the outcomes of the qubits affected by z-gates, and the outcomes of the qubits not affected by z-gates.Does z-gate change the outcome?Why?<jupyter_code># let's import all necessary objects and methods for quantum circuits
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer
# let's import randrange for random choices
from random import randrange

#
# your code is here
#<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup