Home | Algorithms | Commercialization | Data Science | Information Theories | Quantum Theories | Lab | Linear Algebra |
<< Square Root of Quantum Gates | Toffoli Gate >> |
$\require{cancel} \newcommand{\Ket}[1]{\left|{#1}\right\rangle} \newcommand{\Bra}[1]{\left\langle{#1}\right|} \newcommand{\Braket}[1]{\left\langle{#1}\right\rangle} \newcommand{\Rsr}[1]{\frac{1}{\sqrt{#1}}} \newcommand{\RSR}[1]{1/\sqrt{#1}} \newcommand{\Verti}{\rvert} \newcommand{\HAT}[1]{\hat{\,#1~}} \DeclareMathOperator{\Tr}{Tr}$
First created in July 2018
$T$ Gate is also called the $\pi/8$ gate. It is a shift of $\pi/4$ about the $Z$-axis.
$\displaystyle T=R_z(\pi/4) =e^{-i{\pi\over 8}Z}.~~~ T:=\begin{bmatrix}1 & 0\\0 & e^{i\pi/4}\end{bmatrix} .$
It is improper to call the $T$ Gate the $\pi/4$ gate even though it is a rotation of $\pi/4$ only.
A closer look shows $\displaystyle e^{-i{\pi\over 8}Z} =\cos\left({-\pi\over 8}\right)I+i\sin\left({-\pi\over8}\right)Z\\ =\begin{bmatrix} \cos\left({-\pi\over 8}\right)+i\sin\left({-\pi\over8}\right) & 0\\ 0 & \cos\left({-\pi\over 8}\right)-i\sin\left({-\pi\over8}\right) \end{bmatrix} =\begin{bmatrix} e^{-i\pi/8} & 0\\ 0 & e^{i\pi/8} \end{bmatrix} .$
To illustrate the $T$ operation, we can apply it repetitively to $+X$ and see each successive $X$-projection and $Y$-projection.
Two examples here:
We first prepare $\Ket+$ by $H\Ket0$, then rotate it by $T^n$.
Measuring on $+X$, we rotate the result by $H$ ($+X$ to $+Z$).
The whole sequence is $HT^nH\Ket0$.
# quantum_phase.py
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute
# Define the Quantum and Classical Registers
q = QuantumRegister(1)
c = ClassicalRegister(1)
# Build the circuits
circuits = []
pre = QuantumCircuit(q, c)
pre.h(q)
pre.barrier()
middle = QuantumCircuit(q, c)
meas_x = QuantumCircuit(q, c)
meas_x.barrier()
meas_x.h(q)
meas_x.measure(q, c)
exp_vector = range(0,8)
for exp_index in exp_vector:
circuits.append(pre + middle + meas_x)
middle.t(q)
# Execute the circuits
shots = 1024
job = execute(circuits, backend = 'local_qasm_simulator', shots=shots, seed=8)
result = job.result()
# Print the result
for exp_index in exp_vector:
data = result.get_counts(circuits[exp_index])
try:
p0 = data['0']/shots
except KeyError:
p0 = 0
try:
p1 = data['1']/shots
except KeyError:
p1 = 0
print('exp {}: [{}, {}] X length = {}'.format(exp_index, p0, p1, p0-p1))
The above logic prepares the initial state to $\Ket+$ ($+X$) using "pre.h(q)", goes through the $T^n$ process, then measures along the $X$-axis using "meas_x.h(q)"; "meas_x.measure(q, c)".
$\begin{array}{cccc} n & T^nH\Ket0 & HT^nH\Ket0 & X\text{-Projection} \\\hline 0 & \Rsr2(\Ket0+\Ket1) & \Ket0 & 1\\ 1 & \Rsr2\left[\Ket0+\left(\Rsr2+i\Rsr2\right)\Ket1\right] & {\sqrt{2+\sqrt2}\over2}\Ket0-i{\sqrt{2-\sqrt2}\over2}\Ket1 & \Rsr2\\ 2 & \Rsr2(\Ket0+i\Ket1) & \Rsr2\Ket0-i\Rsr2\Ket1) & 0\\ 3 & \Rsr2\left[\Ket0+\left(-\Rsr2+i\Rsr2\right)\Ket1\right] & {\sqrt{2-\sqrt2}\over2}\Ket0-i{\sqrt{2+\sqrt2}\over2}\Ket1 & -\Rsr2 \\ 4 & \Rsr2(\Ket0-\Ket1) & \Ket1 & -1 \\ 5 & \Rsr2\left[\Ket0+\left(-\Rsr2-i\Rsr2\right)\Ket1\right] & {\sqrt{2-\sqrt2}\over2}\Ket0+i{\sqrt{2+\sqrt2}\over2}\Ket1 & -\Rsr2 \\ 6 & \Rsr2(\Ket0-i\Ket1) & \Rsr2\Ket0+i\Rsr2\Ket1) & 0 \\ 7 & \Rsr2\left[\Ket0+\left(\Rsr2-i\Rsr2\right)\Ket1\right] & {\sqrt{2+\sqrt2}\over2}\Ket0+i{\sqrt{2-\sqrt2}\over2}\Ket1 & \Rsr2 \\ 8 & \Rsr2(\Ket0+\Ket1) & \Ket0 & 1 \end{array}$
Note: $\cos(\pi/8)={\sqrt{2+\sqrt2}\over2},~~\sin(\pi/8)={\sqrt{2-\sqrt2}\over2}.$
$T^nH\Ket0 =\Rsr2\left[\Ket0+\left(\cos(n\pi/4)+i\sin(n\pi/4)\right)\Ket1\right].~~ HT^nH\Ket0 =\cos(n\pi/8)\Ket0-i\sin(n\pi/8)\Ket1 .$
$TH\Ket0 =\Rsr2\left[\Ket0+\left(\Rsr2+i\Rsr2\right)\Ket1\right].~~ HTH\Ket0 ={\sqrt{2+\sqrt2}\over2}\Ket0-i{\sqrt{2-\sqrt2}\over2}\Ket1 .$
If $HT^nH$ forms an angle $\theta/2$ with the basis vector $\Ket0$ in the vector space, the probability of measuring $\Ket0$ is $\cos^2(\theta/2)$ and $\Ket1 \sin^2(\theta/2)$.
The difference of these two probabilities $\cos^2(\theta/2)-\sin^2(\theta/2)=\cos(\theta)$, which is the projection of $HT^nH$ on $Z$-axis, same as the projection of $T^nH$ on the $X$-axis (X length).
Now we try the same on the $Y$-axis.
As it is a measurement on $+Y$, when we do the inverse, we cannot use $H$ as it only gets $+Y$ to $-Y$. We need to use the $S$ gate.
$S=T^2=R_z(\pi/2)$ and $S^{-1}=S^\dagger=R_z(-\pi/2)$ which will get $+Y$ to $+X$. A further $H$ will bring it to $+Z$.
The whole sequence is $HS^\dagger T^nH\Ket0$.
# quantum_phase_meas_y.py
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute
# Define the Quantum and Classical Registers
q = QuantumRegister(1)
c = ClassicalRegister(1)
# Build the circuit
circuits = []
pre = QuantumCircuit(q, c)
pre.h(q)
pre.barrier()
middle = QuantumCircuit(q, c)
meas_y = QuantumCircuit(q, c)
meas_y.barrier()
meas_y.s(q).inverse()
meas_y.h(q)
meas_y.measure(q, c)
exp_vector = range(0,8)
for exp_index in exp_vector:
circuits.append(pre + middle + meas_y)
middle.t(q)
# Execute the circuits
shots = 1024
job = execute(circuits, backend = 'local_qasm_simulator', shots=shots, seed=8)
result = job.result()
# Print the result
for exp_index in exp_vector:
data = result.get_counts(circuits[exp_index])
try:
p0 = data['0']/shots
except KeyError:
p0 = 0
try:
p1 = data['1']/shots
except KeyError:
p1 = 0
print('exp {}: [{}, {}] Y length = {}'.format(exp_index, p0, p1, p0-p1))
The above logic prepares the initial state to $\Ket+$ ($+X$) using "pre.h(q)", goes through the $T^n$ process, then measures along the $Y$-axis by first rotating it by $-\pi/2$ about $Z$-axis then applying the Hadamard gate. i.e. "meas_y.s(q).inverse()"; "meas_y.h(q)"; "meas_y.measure(q, c)".
The operation is $HS^{-1}T^nH$, where $S=T^2$, so $S^\dagger=S^{-1}=T^{-2}$ and the sequence is $HS^\dagger T^nH.$
$\begin{array}{cccc} n & T^nH\Ket0 & HS^\dagger T^nH\Ket0 & Y\text{-Projection} \\\hline 0 & \Rsr2(\Ket0+\Ket1) & \Rsr2\Ket0+i\Rsr2\Ket1 & 0\\ ~\\ 1 & \Rsr2\left[\Ket0+\left(\Rsr2+i\Rsr2\right)\Ket1\right] & {\sqrt{2+\sqrt2}\over2}\Ket0+i{\sqrt{2-\sqrt2}\over2}\Ket1 & \Rsr2\\ ~\\ 2 & \Rsr2(\Ket0+i\Ket1) & \Ket0 & 1\\ ~\\ 3 & \Rsr2\left[\Ket0+\left(-\Rsr2+i\Rsr2\right)\Ket1\right] & {\sqrt{2+\sqrt2}\over2}\Ket0-i{\sqrt{2-\sqrt2}\over2}\Ket1 & \Rsr2\\ ~\\ 4 & \Rsr2(\Ket0-\Ket1) & \Rsr2\Ket0-i\Rsr2\Ket1 & 0\\ ~\\ 5 & \Rsr2\left[\Ket0+\left(-\Rsr2-i\Rsr2\right)\Ket1\right] & {\sqrt{2-\sqrt2}\over2}\Ket0-i{\sqrt{2+\sqrt2}\over2}\Ket1 & -\Rsr2\\ ~\\ 6 & \Rsr2(\Ket0-i\Ket1) & \Ket1 & -1\\ ~\\ 7 & \Rsr2\left[\Ket0+\left(\Rsr2-i\Rsr2\right)\Ket1\right] & {\sqrt{2-\sqrt2}\over2}\Ket0+i{\sqrt{2+\sqrt2}\over2}\Ket1 & -\Rsr2\\ ~\\ 8 & \Rsr2(\Ket0+\Ket1) & \Rsr2\Ket0+i\Rsr2\Ket1 & 0 \end{array}$
Note: $\cos(\pi/8)={\sqrt{2+\sqrt2}\over2},~~\sin(\pi/8)={\sqrt{2-\sqrt2}\over2}.$
$T^nH\Ket0 =\Rsr2\left[\Ket0+\left(\cos(n\pi/4)+i\sin(n\pi/4)\right)\Ket1\right].~~ HS^\dagger T^nH\Ket0 =\cos((n-2)\pi/8)\Ket0-i\sin((n-2)\pi/8)\Ket1 .$
$TH\Ket0 =\Rsr2\left[\Ket0+\left(\Rsr2+i\Rsr2\right)\Ket1\right].~~ HS^\dagger TH\Ket0 =\Rsr2\Ket0+i\Rsr2\Ket1 .$
<< Square Root of Quantum Gates | Top | Toffoli Gate >> |