<jupyter_start><jupyter_text>Playing with Parametrized Gates - Case 1<jupyter_code>%matplotlib inline
%config InlineBackend.figure_format = 'svg'
from IPython.display import clear_output<jupyter_output><empty_output><jupyter_text>Objective: **To implement a circuit that returns |01⟩ and |10⟩ with equal probabilities, using only CNOTs, RXs and RYs.** Producing the usual Bell-state $|\psi_+\rangle = \frac{1}{\sqrt{2}} \lgroup |01\rangle + |10\rangle \rgroup$<jupyter_code>from qiskit import QuantumCircuit

std_cirq=QuantumCircuit(2)
std_cirq.h(0)  # Hadamard gate on q0
std_cirq.x(1)  # X gate on q1
std_cirq.cx(0,1)  # CNOT on q1 controlled on q0
std_cirq.draw('mpl')<jupyter_output><empty_output><jupyter_text>Visualizing:<jupyter_code>from qiskit import Aer, execute
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_histogram

backend = Aer.get_backend('statevector_simulator')
#state = execute(std_cirq,backend).result().get_statevector()
results = execute(std_cirq,backend).result().get_counts()
plot_histogram(results, color='darkturquoise')<jupyter_output><empty_output><jupyter_text>Trying to pull up a circuit with Rx, Ry and CNOT gates to achieve the required states **Calculating the angle values for `Rx` and `Ry` gates in the above circuit:**    We know that:    We have to generate equal probabilities of $|01\rangle$ and $|10\rangle$ in state $|\psi\rangle = \alpha\lvert01\rangle + \beta\lvert10\rangle$i.e.,$$\left\lVert\alpha^2\right\rVert = \left\lVert\beta^2\right\rVert = 0.5$$and hence, $\alpha = \beta = \frac{1}{\sqrt{2}}$That means:$$\begin{aligned}    |\psi\rangle &= \frac{1}{\sqrt{2}}|01\rangle + \frac{1}{\sqrt{2}}|10\rangle \\    &= \begin{bmatrix}         0 \\         \frac{1}{\sqrt{2}} \\        \frac{1}{\sqrt{2}} \\        0 \\    \end{bmatrix}\\\end{aligned}    $$**As we see in the diagram:**$$\begin{aligned}    |\psi_2\rangle &= CNOT |\psi\rangle \\    &= \begin{bmatrix}         1 & 0 & 0 & 0 \\         0 & 0 & 0 & 1 \\        0 & 0 & 1 & 0 \\        0 & 1 & 0 & 0 \\    \end{bmatrix} \cdot    \begin{bmatrix}         0 \\         \frac{1}{\sqrt{2}} \\        \frac{1}{\sqrt{2}} \\        0 \\    \end{bmatrix}\\    &= \begin{bmatrix}         0 \\         0 \\        \frac{1}{\sqrt{2}} \\        \frac{1}{\sqrt{2}} \\    \end{bmatrix}\\    \\    &= \frac{1}{\sqrt{2}}|01\rangle + \frac{1}{\sqrt{2}}|11\rangle \\    \\    &= \lgroup\frac{1}{\sqrt{2}}|0\rangle + \frac{1}{\sqrt{2}}|1\rangle\rgroup \otimes |1\rangle \\\end{aligned}$$The sets both the qubits in a completely separable state, so we can work on them individually.$$\begin{aligned}    |q_0\rangle &= \frac{1}{\sqrt{2}}|0\rangle + \frac{1}{\sqrt{2}}|1\rangle \\    &= \begin{bmatrix}         \frac{1}{\sqrt{2}} \\        \frac{1}{\sqrt{2}} \\    \end{bmatrix}\\    \\    |q_1\rangle &= |1\rangle \\\end{aligned}$$**The unitary matrices for $RY(\theta_1)$ and $RX(\theta_2)$ gates are given by:**$$\begin{aligned}    RY(\theta_1) & =     \begin{bmatrix}         cos(\frac{\theta_1}{2}) & -sin(\frac{\theta_1}{2}) \\         sin(\frac{\theta_1}{2}) & cos(\frac{\theta_1}{2})    \end{bmatrix}\\    RX(\theta_2) & =     \begin{bmatrix}         cos(\frac{\theta_2}{2}) & -isin(\frac{\theta_2}{2}) \\         -isin(\frac{\theta_2}{2}) & cos(\frac{\theta_2}{2})    \end{bmatrix}\\\end{aligned}$$**Applying the gates on $q_0$:**$$\begin{aligned}    |q_0\rangle &=    \begin{bmatrix}        \cos\frac{\theta_2}{2} & -i\sin\frac{\theta_2}{2} \\        -i\sin\frac{\theta_2}{2} & \cos\frac{\theta_2}{2}     \end{bmatrix}\cdot    \begin{bmatrix}        \cos\frac{\theta_1}{2} & -\sin\frac{\theta_1}{2} \\        \sin\frac{\theta_1}{2} & \cos\frac{\theta_1}{2}     \end{bmatrix}\cdot    \begin{bmatrix}        1 \\        0      \end{bmatrix} \\    &= \begin{bmatrix}        \cos\frac{\theta_2}{2} & -i\sin\frac{\theta_2}{2} \\        -i\sin\frac{\theta_2}{2} & \cos\frac{\theta_2}{2}     \end{bmatrix}\cdot    \begin{bmatrix}        \cos\frac{\theta_1}{2} \\        \sin\frac{\theta_1}{2}      \end{bmatrix} \\    &= \begin{bmatrix}        \cos\frac{\theta_2}{2}\cos\frac{\theta_1}{2} - i\sin\frac{\theta_2}{2}\sin\frac{\theta_1}{2}  \\        \cos\frac{\theta_2}{2}\sin\frac{\theta_1}{2} - i\sin\frac{\theta_2}{2}\cos\frac{\theta_1}{2}     \end{bmatrix} \\\end{aligned}$$**Equating with values of $|q_0\rangle$**$$\begin{aligned}    \begin{bmatrix}        \cos\frac{\theta_2}{2}\cos\frac{\theta_1}{2} - i\sin\frac{\theta_2}{2}\sin\frac{\theta_1}{2}  \\        \cos\frac{\theta_2}{2}\sin\frac{\theta_1}{2} - i\sin\frac{\theta_2}{2}\cos\frac{\theta_1}{2}     \end{bmatrix}    &= \begin{bmatrix}        \frac{1}{\sqrt{2}} \\        \frac{1}{\sqrt{2}}      \end{bmatrix} \\\end{aligned}$$We get:$$\begin{aligned}    \cos\frac{\theta_2}{2}\cos\frac{\theta_1}{2} &= \frac{1}{\sqrt{2}}, \\    \cos\frac{\theta_2}{2}\sin\frac{\theta_1}{2} &= \frac{1}{\sqrt{2}},\end{aligned}$$and$$\begin{aligned}    \sin\frac{\theta_2}{2}\sin\frac{\theta_1}{2} &= 0 \\    \sin\frac{\theta_2}{2}\cos\frac{\theta_1}{2} &= 0\end{aligned}$$Solving the above, we get: $\theta_1 = \frac{\pi}{2}$; $\theta_2 = \pi$**Applying the $Rx(\theta_3)$ gate to get an effect of X gate on $q_1$:**i.e., $Rx(\theta_3) * |0\rangle = |1\rangle$$$\begin{aligned}    \begin{bmatrix}         \cos(\frac{\theta_3}{2}) & -i\sin(\frac{\theta_3}{2}) \\         -i\sin(\frac{\theta_3}{2}) & \cos(\frac{\theta_3}{2})     \end{bmatrix} \cdot    \begin{bmatrix}        1 \\        0      \end{bmatrix}    & = \begin{bmatrix}        0 \\        1      \end{bmatrix} \\    \begin{bmatrix}         \cos(\frac{\theta_3}{2}) \\         -i\sin(\frac{\theta_3}{2})     \end{bmatrix}    & = \begin{bmatrix}        0 \\        1      \end{bmatrix}\end{aligned}$$Apply a global phase `i` on RHS, and we get:$$\begin{aligned}        \begin{bmatrix}         \cos(\frac{\theta_3}{2}) \\         -i\sin(\frac{\theta_3}{2})     \end{bmatrix}    & = \begin{bmatrix}        0 \\        i      \end{bmatrix}\end{aligned}$$Solving the above, we get: $\theta_3 = \pi$Now that we have the angle values for $\theta_1$, $\theta_2$ and $\theta_3$, building the circuit:<jupyter_code>from numpy import pi

cirq = QuantumCircuit(2)
cirq.ry(pi/2, 0) 
cirq.rx(pi, 0)
cirq.rx(pi, 1)
cirq.cx(0,1)

cirq.draw('mpl')<jupyter_output><empty_output><jupyter_text>Visualizing:<jupyter_code>results = execute(cirq,backend).result().get_counts()
plot_histogram(results, color='deepskyblue')<jupyter_output><empty_output><jupyter_text>Plotting on the Q-sphere:<jupyter_code>from qiskit.visualization import plot_state_qsphere

sv = Statevector.from_label('00').evolve(cirq)
plot_state_qsphere(sv.data, show_state_phases=True)
import qiskit
qiskit.__qiskit_version__<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21328775267538644, 'fraction_numerical': 0.03864447086801427, 'mean_word_length': 3.0487364620938626, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '595004', 'license': 'Apache-2.0', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2467, 'n_words': 418, 'path': 'Playing with Parametrized Gates - Case-1.ipynb', 'repo_name': 'ertogrul/Playing-with-parametrized-quantum-gates', 'size': '6728'}