<jupyter_start><jupyter_text>prepared by <NAME> (QLatvia) and <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{\stateplus}{\myvector{ \sqrttwo \\  \sqrttwo } } $$ \newcommand{\stateminus}{ \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 } $$ \newcommand{\Y}{ \mymatrix{rr}{0 & -i \\ i & 0} } $ $ \newcommand{\S}{ \mymatrix{rr}{1 & 0 \\ 0 & i} } $ $ \newcommand{\T}{ \mymatrix{rr}{1 & 0 \\ 0 & e^{i \frac{pi}{4}}} } $ $ \newcommand{\Sdg}{ \mymatrix{rr}{1 & 0 \\ 0 & -i} } $ $ \newcommand{\Tdg}{ \mymatrix{rr}{1 & 0 \\ 0 & e^{-i \frac{pi}{4}}} } $$ \newcommand{\qgate}[1]{ \mathop{\textit{1} } }$  Global and local phase  A generic qubit can be in state $\ket{\psi} = \alpha \ket{0} + \beta \ket{1}$, where $\alpha, \beta \in \mathbb{C}$ and $\mathopen|\alpha\mathclose|^2 + \mathopen|\beta\mathclose|^2 = 1$. Both amplitudes can be complex values, and each complex number has a real and an imaginary part. Therefore, we have 4 different numbers that describe the state of a qubit. Can we have more concise way to represent the state of a qubit? Understanding of complex numbers and global and local phase can help us to improve the situation.  Another representation of a state At the given moment we have 4 numbers to represent the state of a qubit. We can reduce this to three numbers, since $\mathopen|\alpha\mathclose|^2 + \mathopen|\beta\mathclose|^2 = 1$. How the result will look like? Since there is a correspondence between two amplitudes and complex numbers can be represented in polar form, the community has found the following representation of the state of a qubit with three angles:$$\ket{\psi} = e^{i\delta} ( \cos{\frac{\theta}{2}} \ket{0} + e^{i\phi} \sin{\frac{\theta}{2}} \ket{1} ),$$where $0 \leq \theta \leq \pi$ and $0 \leq \phi, \delta < 2\pi$.  Global phase Suppose that we have a qubit and its state is either $ \ket{\psi} $ or $ e^{i\delta} \ket{\psi} $, where $0 \leq \delta < 2\pi$.Is there any sequence of one-qubit gates such that we can measure different results after applying them? All one-qubit gates are $ 2 \times 2 $ matrices, and their application is represented by a single matrix: $ A_n \cdot \cdots \cdot A_2 \cdot A_1 = A $.By linearity, if $ A \ket{\psi} = \ket{u} $, then $ A e^{i\delta} \ket{\psi} = e^{i\delta} \ket{u} $. Thus, after measurement, the probabilities of observing state $ \ket{0} $ and state $ \ket{1} $ are the same in both cases. Therefore, we cannot distinguish them.Even though the states $ \ket{0} $ and $ e^{i\delta} \ket{0} $ are different mathematically, they are assumed the same from the physical point of view.  The complex number $ e^{i\delta} $ in front of $ e^{i\delta} \ket{\psi} $ is called as _global phase_.Therefore, two vectors that differ by a global phase factor, in quantum mechanics are considered equivalent, and so we have $ \ket{\psi} \equiv e^{i\delta} \ket{\psi} $ for any quantum state $\ket{\psi}$ and any $0 \leq \delta < 2\pi$.Now we can describe the state of a qubit with two numbers - angles $\theta$ and $\phi$:$$\ket{\psi} = \cos{\frac{\theta}{2}} \ket{0} + e^{i\phi} \sin{\frac{\theta}{2}} \ket{1},$$where $0 \leq \theta \leq \pi$ and $0 \leq \phi < 2\pi$.  Local phase In our last form of the state representation we have an amplitude multiplier $e^{i\phi}$. What if we have a similar multiplier also for the amplitude of state $\ket{0}$? Then we would have the following state: $$e^{i\gamma} \cos{\frac{\theta}{2}} \ket{0} + e^{i\phi} \sin{\frac{\theta}{2}} \ket{1} = e^{i\gamma}(\cos{\frac{\theta}{2}} \ket{0} + e^{i(\phi-\gamma)} \sin{\frac{\theta}{2}} \ket{1}) = \cos{\frac{\theta}{2}} \ket{0} + e^{i(\phi-\gamma)} \sin{\frac{\theta}{2}} \ket{1}.$$ Therefore, there is no need for second such multiplier, and having one related to the state $\ket{1}$ is a convention.One more useful fact is that $\mathopen|e^{i\phi}\mathclose| = 1$ for any $\phi$. This means that this multiplier does not affect the probabilities to observe state $\ket{0}$ or $\ket{1}$ after the measurement. This means that only parameter $\theta$ influences the probability to observe state $\ket{0}$ or $\ket{1}$ after the measurement.Although $e^{i\phi}$ does not influence the measurement outcome, it influences the overall computation, as you could notice in previous notebook, where we affected the phase for the state $\ket{1}$. The number $e^{i\phi}$ shows additional relation between states $\ket{0}$ and $\ket{1}$, like one more dimension added to the computation, and is called local phase. As you have noticed, local phase indeed is important for the computation.  Task 1 Find the probabilities of observing states $\ket{0}$ and $\ket{1}$ for the qubits in the following states:    $\cos{\frac{\pi}{3}} \ket{0} + e^{i\pi} \sin{\frac{\pi}{3}} \ket{1}$    $\cos{\frac{\pi}{4}} \ket{0} + e^{i\frac{\pi}{4}} \sin{\frac{\pi}{4}} \ket{1}$    $\cos{\frac{\pi}{6}} \ket{0} + i \sin{\frac{\pi}{6}} \ket{1}$ click for our solution  Task 2 Implement a function that calculates the probabilities of observing states $\ket{0}$ and $\ket{1}$ given the angle of a quantum state. Assuming that the quantum state is of the form $\cos{\frac{\theta}{2}} \ket{0} + e^{i\phi} \sin{\frac{\theta}{2}} \ket{1}$, $\theta$ and $\phi$ should be provided to the function as parameters. Check the quantum states given in Task 1 and compare the results.<jupyter_code>#
# your solution is here
#<jupyter_output><empty_output><jupyter_text>click for our solution  An overall phase is significant for controlled operations   $\qgate{CRZ}$ and $\qgate{CP}$ gates We have seen that an overall complex phase factor $e^{i\varphi}$, called *global phase*, can be dropped or inserted into the mathematical expressions for quantum states $\ket{\psi}$. In the previous sections we have also observed that an overall multiplication of single qubit rotations are also irrelevant. For example, the phase gate,$$\qgate{P}(\lambda) = \mymatrix{rr}{1 & 0 \\ 0& e^{i\lambda} }$$and the $z$-rotation gate$$\qgate{RZ}(\lambda) = \mymatrix{rr}{ e^{-i\lambda/2} & 0 \\ 0& e^{i\lambda/2} }$$are related to each other as$$    \qgate{RZ}'(\lambda) = e^{i\lambda/2} \qgate{RZ}(\lambda),$$and lead to the same rotation on the Bloch sphere. We might think that their controlled versions, $\qgate{CRZ}$ and $\qgate{CP}$ will also lead to the same operations:<jupyter_code>from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
lambda_angle = pi/4
circuit1 = QuantumCircuit(2,2)
circuit1.crz(lambda_angle,1,0)
circuit1.draw(output='mpl')
circuit2 = QuantumCircuit(2,2)
circuit2.cp(lambda_angle, 1,0)
circuit2.draw(output='mpl')<jupyter_output><empty_output><jupyter_text>These two controlled gates are, however, not equivalent. We can tell this immediately, by considering the effect of swapping the two qubits: $\qgate{CRZ}'(\lambda) := \qgate{SWAP} \qgate{CRZ}(\lambda) \qgate{SWAP}$ and $\qgate{CP}'(\lambda) := \qgate{SWAP} \qgate{CP}(\lambda) \qgate{SWAP}$. Then the basis states will be mapped as    $ \qgate{CRZ}'(\lambda) \ket{00} = \ket{00} $,     $ \qgate{CRZ}'(\lambda) \ket{01} = e^{-i\lambda/2} \ket{01} $,     $ \qgate{CRZ}'(\lambda) \ket{10} = \ket{10}$,     $ \qgate{CRZ}'(\lambda) \ket{11} = e^{i\lambda/2} \ket{11}$,and    $ \qgate{CP}'(\lambda) \ket{00} = \ket{00} $,     $ \qgate{CP}'(\lambda) \ket{01} = \ket{01} $,     $ \qgate{CP}'(\lambda) \ket{10} = \ket{10}$,     $ \qgate{CP}'(\lambda) \ket{11} = e^{i\lambda} \ket{11}$. We see that while $RZ$ changes, the $\qgate{CP}$ stays the same if we swap the to qubits. This latter property is reflected also in the quantum circuit notation: it is denoted by symmetric symbol.  How the two $\qgate{CRZ}$ and $\qgate{CP}$ are related to each other? It turns out that this is very simple: the difference is just a single $\qgate{P}(\lambda/2)$ on the *control* qubit. Let us consider the quantum circuit depicted below.<jupyter_code>circuit3 = QuantumCircuit(2,2)
circuit3.p(lambda_angle/2,1)
circuit3.crz(lambda_angle,1,0)
circuit3.draw(output='mpl')<jupyter_output><empty_output><jupyter_text>The effect of this circuit on the basis states can be computed:    $ \qgate{RZ}(\lambda) (\qgate{P}(\lambda) \otimes I) \ket{00} = \qgate{RZ}(\lambda) \ket{00} = \ket{00} $,     $ \qgate{RZ}(\lambda) (\qgate{P}(\lambda) \otimes I) \ket{01} = \qgate{RZ}(\lambda) \ket{01} = \ket{01} $,     $ \qgate{RZ}(\lambda) (\qgate{P}(\lambda) \otimes I) \ket{10} = \qgate{RZ}(\lambda) e^{i\lambda/2} \ket{10} =  e^{i\lambda/2}  \qgate{RZ}(\lambda)\ket{10} =\ket{10}$,     $ \qgate{RZ}(\lambda) (\qgate{P}(\lambda) \otimes I) \ket{11} = \qgate{RZ}(\lambda) e^{i\lambda/2} \ket{11} = e^{i\lambda/2} \qgate{RZ}(\lambda) \ket{11} =e^{i\lambda} \ket{11}$, and is the same as the effect of the $\qgate{CP}(\lambda)$ gate. We can also convince ourselves that the order of $\qgate{RZ}$ and $\qgate{P}$ does not make any difference, i.e. we can shift the $\qgate{P}$ gate left-right along the quantum wire. Will this phase ever make a difference in the measurement statistics? The code below shows an example where it indeed does! The above relation to the single qubit phase $\qgate{P}$ can help us to understanding why.<jupyter_code>from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer

def check_controlled_gate_phase(cgate):
    for i in range(4):
        circuit4 = QuantumCircuit(2,2)
        if (i&1)==1: circuit4.x(0)
        if ((i>>1)&1)==1: circuit4.x(1)
        
        lambda_angle=2*pi

        circuit4.h(1)
        if (cgate=='crz'):
            circuit4.crz(lambda_angle, 1,0)
        if (cgate=='cp'):
            circuit4.cp(lambda_angle, 1,0)
        circuit4.h(1)
      
        if i==0:
            display(circuit4.draw(output='mpl'))
    
        circuit4.measure([0,1], [0,1])
    
        job = execute(circuit4, Aer.get_backend('qasm_simulator'),shots=1024)
        print(format(i,"02b"),"🠖", job.result().get_counts(circuit4))


print("Checking CRZ gate")
check_controlled_gate_phase('crz')

print("\n\n---------------------------------")
print("Checking CP gate")
check_controlled_gate_phase('cp')<jupyter_output>Checking CRZ gate
{'fraction_non_alphanumeric': 0.18843060071880882, 'fraction_numerical': 0.029180215642649325, 'mean_word_length': 3.9249894648124735, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 20, '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': '316378', 'license': 'Apache-2.0', 'n_tokens_mistral': 4073, 'n_tokens_neox': 3839, 'n_words': 1264, 'path': 'silver/.ipynb_checkpoints/C05_Global_And_Local_Phase-checkpoint.ipynb', 'repo_name': 'asif-saad/qSilver', 'size': '11703'}