Quantum Gates
Introduction
Import the modules
from qiskit import QuantumCircuit, execute, Aer
from qiskit.visualization import plot_histogram, plot_bloch_vector
from math import pi
a = 0
b = 1
t = 2
Plot the quantum circuit
qc.draw('mpl')
Plot the results
backend = Aer.get_backend('statevector_simulator') # Tell Qiskit how to simulate our circuit
qc.measure_all()
qc.draw()
result = execute(qc,backend).result() #.get_statevector() #.get_counts()
counts = result.get_counts()
plot_histogram(counts)
Theory
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
Two Qubit Quantum Gates
The reversibel gates are eg. identity, or CNOT.
Eg. .

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)
qc.h(0) # Apply H-gate to the first:
qc.cx(0,1) # Apply a CNOT:
Any controlled quantum gate is and in Qiskit formalism is written in matrix as
-
Controlled H
-
Controlled Y
-
Controlled Z
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(1)
qc.cx(0,1)
qc.h(1)
Controlled-Y is
qc = QuantumCircuit(2)
# a controlled-Y
qc.sdg(1)
qc.cx(0,1)
qc.s(1)
or Controlled-H is
qc = QuantumCircuit(2)
# a controlled-H
qc.ry(pi/4,1)
qc.cx(0,1)
qc.ry(-pi/4,1)
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)
Three Qubit Gates

For universal computations we need more qubits. Eg. the AND gate is not reversible, and thus we need eg. Toffoli (CCNOT) gate.
Toffoli gate performs 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 X} on target qubit if both control cubits are set to state 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 |1\rangle} .
qc = QuantumCircuit(3)
# Toffoli with control qubits a and b and target t
qc.ccx(a,b,t)
Toffoli using CNOTs uses fewer gates.
qc = QuantumCircuit(3)
qc.ch(a,t)
qc.cz(b,t)
qc.ch(a,t)
AND gate is Toffoli gate with . . .

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{CCNOT}(x,y,z) = (x,y,( x \and y ) \otimes z )} gives the reversible NAND 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{NAND}(x,y) = \text{CCNOT}(x,y,1) = (x,y,( x \and y ) \otimes 1 )}
NAND gate is