Quantum Gates: Difference between revisions
(Created page with "== Introduction == == Quantum Gates of One Qubit == There are only two reversible gates, also ''identity'' (return the input unchanged) and NOT (return the opposite of the i...") |
|||
Line 52: | Line 52: | ||
out = execute(qc,backend).result().get_statevector() | out = execute(qc,backend).result().get_statevector() | ||
plot_bloch_multivector(out) | plot_bloch_multivector(out) | ||
</syntaxhighlight> | |||
== Two Qubit Quantum Gates == | |||
The reversibel gates are eg. ''identity'', or CNOT. | |||
Eg. <math>X\otimes H = \begin{bmatrix} 0 &H\\H&0\end{bmatrix}</math>. | |||
<syntaxhighlight> | |||
backend = Aer.get_backend('unitary_simulator') | |||
unitary = execute(qc,backend).result().get_unitary() | |||
# | |||
# In Jupyter Notebooks we can display this nicely using Latex. | |||
# If not using Jupyter Notebooks you may need to remove the | |||
# array_to_latex function and use print(unitary) instead. | |||
from qiskit_textbook.tools import array_to_latex | |||
array_to_latex(unitary, pretext="\\text{Circuit = }\n") | |||
</syntaxhighlight> | |||
[[File:Cnot.svg|thumb|CNOT gate as a pictoram.]] | |||
Eg. CNOT is a conditional gate that performs an X-gate on the second qubit, if the state of the first qubit (control) is <math>|1\rangle</math>. <math>CNOT= \begin{bmatrix} 1 & 0 & 0 & 0 \\ | |||
0 & 0 & 0 & 1 \\ | |||
0 & 0 & 1 & 0 \\ | |||
0 & 1 & 0 & 0 \\ | |||
\end{bmatrix} | |||
</math>. This matrix swaps the amplitudes of |01⟩ and |11⟩ in the statevector. <math>\text{CNOT}(x,y) = (x, x\otimes y)</math>. | |||
CNOT if a control qubit is on the superposition: | |||
<math>\text{CNOT}|{-}0\rangle = |{-}0\rangle</math> | |||
<math>\text{CNOT}|{-}1\rangle = -|{-}1\rangle</math> | |||
<math>\text{CNOT}|0{+}\rangle = \tfrac{1}{\sqrt{2}}(|00\rangle + |11\rangle)</math>, which is ''Bell State''. Entanglement, but [https://arxiv.org/abs/quant-ph/0212023 no-communication theorem]. | |||
<math>\text{CNOT}|{+}{+}\rangle = |{+}{+}\rangle)</math>. Unchanged. | |||
<math>\text{CNOT}|{-}{+}\rangle = \tfrac{1}{\sqrt{2}}(|{-}0\rangle -|{-}1\rangle) = |{-}{-} \rangle</math> | |||
<math>\text{CNOT}|{+}{-}\rangle = </math>. | |||
<math>\text{CNOT}|{-}{-}\rangle = \tfrac{1}{2}(|00\rangle - |01\rangle - |10\rangle + |11\rangle) = |{-}{-}\rangle </math>. Affects the state of the control qubit, only. | |||
<syntaxhighlight> | |||
qc = QuantumCircuit(2) | |||
# Apply H-gate to the first: | |||
qc.h(0) | |||
# Apply a CNOT: | |||
qc.cx(0,1) | |||
qc.draw('mpl') | |||
# | |||
# Let's see the result: | |||
backend = Aer.get_backend('statevector_simulator') | |||
final_state = execute(qc,backend).result().get_statevector() | |||
# Print the statevector neatly: | |||
array_to_latex(final_state, pretext="\\text{Statevector = }") | |||
# | |||
results = execute(qc,backend).result().get_counts() | |||
plot_histogram(results) | |||
</syntaxhighlight> | |||
Any controlled quantum gate is <math>\text{Controlled-U} = \begin{bmatrix}I &0\\0&U\end{bmatrix}</math> and in Qiskit formalism is written in matrix as | |||
<math>\text{Controlled-U} = | |||
\begin{bmatrix} | |||
1 & 0 & 0 & 0 \\ | |||
0 & u_{00} & 0 & u_{01} \\ | |||
0 & 0 & 1 & 0 \\ | |||
0 & u_{10} & 0 & u_{11}\\ | |||
\end{bmatrix} | |||
</math> | |||
Controlled-Z. Because <math>H X H = Z</math> and <math>H Z H = X</math> we can write | |||
<syntaxhighlight> | |||
qc = QuantumCircuit(2) | |||
# also a controlled-Z | |||
qc.h(t) | |||
qc.cx(c,t) | |||
qc.h(t) | |||
qc.draw('mpl') | |||
</syntaxhighlight> | |||
Controlled-Y is | |||
<syntaxhighlight> | |||
qc = QuantumCircuit(2) | |||
# a controlled-Y | |||
qc.sdg(t) | |||
qc.cx(c,t) | |||
qc.s(t) | |||
qc.draw('mpl') | |||
</syntaxhighlight> | |||
or Controlled-H is | |||
<syntaxhighlight> | |||
qc = QuantumCircuit(2) | |||
# a controlled-H | |||
qc.ry(pi/4,t) | |||
qc.cx(c,t) | |||
qc.ry(-pi/4,t) | |||
qc.draw('mpl') | |||
</syntaxhighlight> | |||
Swap gate | |||
An arbitrary controlled-controlled-U for any single-qubit rotation U. We need <math>V = \sqrt U</math> and <math>V^\dagger</math> | |||
<syntaxhighlight> | |||
#The controls are qubits a and b, and the target is qubit t. | |||
#Subroutines cu1(theta,c,t) and cu1(-theta,c,t) need to be defined | |||
qc = QuantumCircuit(3) | |||
qc.cu1(theta,b,t) | |||
qc.cx(a,b) | |||
qc.cu1(-theta,b,t) | |||
qc.cx(a,b) | |||
qc.cu1(theta,a,t) | |||
qc.draw('mpl') | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 11:32, 26 November 2020
Introduction
Quantum Gates of One Qubit
There are only two reversible gates, also identity (return the input unchanged) and NOT (return the opposite of the input), but neither is universal.
Identity gate.
Pauli X gate.
Pauli Y gate
Pauli Z gate
R gate
S gate or gate
T gate
U1 gate:
U2 gate:
qc = QuantumCircuit(1)
qc.x(0)
#qc.y(0) # Y-gate on qubit 0
#qc.z(0) # Z-gate on qubit 0
#qc.rz(pi/4, 0)
#qc.s(0) # Apply S-gate to qubit 0
#qc.sdg(0) # Apply Sdg-gate to qubit 0
qc.t(0) # Apply T-gate to qubit 0
qc.tdg(0) # Apply Tdg-gate to qubit 0
qc.draw('mpl')
# Let's see the result
backend = Aer.get_backend('statevector_simulator')
out = execute(qc,backend).result().get_statevector()
plot_bloch_multivector(out)
Two Qubit Quantum Gates
The reversibel gates are eg. identity, or CNOT.
Eg. .
backend = Aer.get_backend('unitary_simulator')
unitary = execute(qc,backend).result().get_unitary()
#
# In Jupyter Notebooks we can display this nicely using Latex.
# If not using Jupyter Notebooks you may need to remove the
# array_to_latex function and use print(unitary) instead.
from qiskit_textbook.tools import array_to_latex
array_to_latex(unitary, pretext="\\text{Circuit = }\n")

Eg. CNOT is a conditional gate that performs an X-gate on the second qubit, if the state of the first qubit (control) is . . This matrix swaps the amplitudes of |01⟩ and |11⟩ in the statevector. .
CNOT if a control qubit is on the superposition:
, which is Bell State. Entanglement, but no-communication theorem.
. Unchanged.
.
. Affects the state of the control qubit, only.
qc = QuantumCircuit(2)
# Apply H-gate to the first:
qc.h(0)
# Apply a CNOT:
qc.cx(0,1)
qc.draw('mpl')
#
# Let's see the result:
backend = Aer.get_backend('statevector_simulator')
final_state = execute(qc,backend).result().get_statevector()
# Print the statevector neatly:
array_to_latex(final_state, pretext="\\text{Statevector = }")
#
results = execute(qc,backend).result().get_counts()
plot_histogram(results)
Any controlled quantum gate is and in Qiskit formalism is written in matrix as Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \text{Controlled-U} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & u_{00} & 0 & u_{01} \\ 0 & 0 & 1 & 0 \\ 0 & u_{10} & 0 & u_{11}\\ \end{bmatrix} }
Controlled-Z. Because Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle H X H = Z} and Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle H Z H = X} we can write
qc = QuantumCircuit(2)
# also a controlled-Z
qc.h(t)
qc.cx(c,t)
qc.h(t)
qc.draw('mpl')
Controlled-Y is
qc = QuantumCircuit(2)
# a controlled-Y
qc.sdg(t)
qc.cx(c,t)
qc.s(t)
qc.draw('mpl')
or Controlled-H is
qc = QuantumCircuit(2)
# a controlled-H
qc.ry(pi/4,t)
qc.cx(c,t)
qc.ry(-pi/4,t)
qc.draw('mpl')
Swap gate
An arbitrary controlled-controlled-U for any single-qubit rotation U. We need Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle V = \sqrt U} and Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle V^\dagger}
#The controls are qubits a and b, and the target is qubit t.
#Subroutines cu1(theta,c,t) and cu1(-theta,c,t) need to be defined
qc = QuantumCircuit(3)
qc.cu1(theta,b,t)
qc.cx(a,b)
qc.cu1(-theta,b,t)
qc.cx(a,b)
qc.cu1(theta,a,t)
qc.draw('mpl')