Quantum Gates

A step-by-step walk-through of some common single qubit gates

Navaneeth Dinesh
Analytics Vidhya
8 min readApr 12, 2021

--

Photo by Michael Dziedzic on Unsplash

As introduced in the previous articles, gates can be considered as operations on qubits that change their state from one to another. Since we represent our qubit states as vectors, we can consider quantum gates as matrices that can transform the vectors. Since quantum theory is unitary, quantum gates are represented as unitary matrices. Another way to understand gate operations is to think of them as rotations around the block sphere.

Any normalized pure state can be written in the following form:

Here ϕ describes the relative phase and its values range from [0,2π]. On the other hand, θ ∈ [0,π] determines the probability that the measurement results in |0⟩ or |1⟩. We had seen that all normalized pure states can be illustrated on the surface of a sphere with radius |r| = 1 which we call the Bloch sphere. Coordinates of such a state on the Bloch sphere is given by the Bloch vector:

Accordingly, the various states can be represented as follows —

Please note that here

We will soon visualize the rotations on the Bloch sphere using qiskit when gates operate upon it.

Now that you have got a little bit of background, let us explore some commonly used gates in quantum computing. This article will focus on single-qubit gates.

Pauli X Gate

Pauli X gate or the bit-flip gate is the quantum equivalent of the classical NOT gate. It is represented by the unitary matrix:

We had already seen the Pauli X-gate in action in our previous article. Internally, it rotates the quantum state around the x-axis by π radians. Now looking at the image of the Bloch sphere above, we see that |+⟩ and |-⟩ lies on the x-axis or we can say that |+⟩ and |-⟩ are the two eigenstates of the X-gate. Hence these states are not affected by the Pauli X operation. To see how it affects other states let's do some programming.

First, let’s make the necessary imports and set up the statevector_simulator for viewing the state vector on the Bloch sphere.

from qiskit import QuantumCircuit, assemble, Aer
from qiskit.visualization import plot_histogram, plot_bloch_multivector
svsim = Aer.get_backend('statevector_simulator')

Let’s define a quantum circuit with 1 qubit and see how the circuit and the orientation of the state vector look like.

qc = QuantumCircuit(1)
qc.draw(output='mpl')
qobj = assemble(qc)
state = svsim.run(qobj).result().get_statevector()
plot_bloch_multivector(state)

After applying the X gate our output would be as follows

qc.x(0)
qc.draw(output=’mpl’)
qobj = assemble(qc)
state = svsim.run(qobj).result().get_statevector()
plot_bloch_multivector(state)

Notice how the orientation of the vector has flipped downwards. Mathematically this is what is happening behind the scenes,

As an exercise try and see what would happen if we start with |i⟩.

Pauli Z and Y gates

Pauli Z gate is a phase flip gate that causes rotation around the z-axis by π radians.

Since|0⟩ and |1⟩ lie on the z-axis, the Z-gate will not affect these states. To put it in other terms |0⟩ and |1⟩ are the two eigenstates of the Z-gate. On the other hand, it flips |+⟩ to |-⟩ and |-⟩ to |+⟩. To see this in action let’s initialize the state vector to |+⟩ and visualize how Z gate transforms the vector.

qc = QuantumCircuit(1) 
initial_state = [1/sqrt(2),1/sqrt(2)]
qc.initialize(initial_state, 0)
qobj = assemble(qc)
state = svsim.run(qobj).result().get_statevector()
plot_bloch_multivector(state)
qc.z(0)
qobj = assemble(qc)
state = svsim.run(qobj).result().get_statevector()
plot_bloch_multivector(state)

The Pauli Y gate can be considered as a bit and phase flip gate that causes rotation around the y-axis by π radians.

Try to find out how the Y-gate affects the orientation of various vectors on a block sphere using the same method we had used for the other Pauli gates.

These Pauli matrices together with the identity matrix form a basis of 2X2 matrices. Thus any qubit rotation can be written as a linear combination of these gates.

The Hadamard Gate

The Hadamard gate is one of the most frequently used gates in Quantum computing. Applying the Hadamard Gate to a qubit that is in state |0⟩ brings the qubit in a superposition state where the probability of measuring 0 is equal to the probability of measuring 1. Pictorially, this means

Image courtesy: Manning publication

The action of the H-gate can be thought of as a rotation around the Bloch vector [1,0,1] (the line between the x & z-axis).

Image courtesy:
Physics Stack Exchange

Let us understand how mathematically such an operation is done.

The Hadamard matrix is given by

Applying H-gate over the |0⟩ state vector yields the following:

|+⟩ is a superposition state because if we apply the Born’s rule, we will find the P(0) = P(1) = 1/2.

Try to program a simple circuit employing the Hadamard gate in the same way we had done for the other gates. Analyze how it affects different state vectors especially|0⟩ and |1⟩. We can use the h() method of the QuantumCircuit class to achieve this.

qc.h(0)

The R𝓏 gate

The Rz-gate or the Rϕ-gate would be the first parametrized gate that we will be introduced to. By “parametrized” we mean that it accepts a parameter and performs an operation based on this parameter. The parameter accepted here is ϕ and the operation performed is rotation around the z-axis by ϕ radians. The matrix for this gate is given by

Since the states |0⟩ and |1⟩ lie along the z-axis, they will not be affected by the gate. Try initializing the state vector to |+⟩ and passing it to the Rz-gate as follows:

qc.rz(pi/4,0) # First argument is ϕ and second one is the qubit no.

You will see the vector changing from one state to another as in the figure to the left. Note that the state vector rotates in the anti-clockwise direction by π/4 radians.

I, S, and T gate

The I, S, and T gate are specific cases of the more general Rϕ gate.

The I-gate does not do anything in particular. Its matrix is the Identity matrix itself. It is considered a gate because they are often useful in calculations. Also, they are significant in a way that they can be used to specify a “none” operation when considering real hardware.

The S-gate is an Rϕ gate with ϕ = π/2 or in other terms, it rotates the vector π/2 radians around the z-axis. Unlike other gates, the S-gate is not its own inverse. Although it is still unitary. The S-gate and its inverse, S-dagger or Sdg, is given by

The S-dagger, as you might have guessed, rotates the vector by π/2 radians around the z-axis in the clockwise direction. It’s interesting to note that 2 consecutive S gate operation is equivalent to performing a Z gate operation. Due to this, the S gate is also referred to as the √Z-gate.

The T-gate is again a specific case of Rϕ gate with ϕ = π/4. It follows the same behavior as that of the S-gate. The T-gate and its inverse, T-dagger or Tdg, is given by

Applying four T-gate is equivalent to performing a single Z-gate.

General U-gates

Let's start with the U₁-gate. The matrix for this gate is equivalent to the Rϕ gate itself and is given by

We also have a U₂-gate whose matrix is given by

Both are parametrized gates but most importantly, they are derived from the all-encompassing U₃-gate. In fact, every gate introduced in this article can be represented using the U₃-gate. It is, as expected, a parametrized gate whose matrix is given by

One can observe that U₁ = U₃(0,0,λ) and U₂ = U₃(π/2,ϕ,λ).

Reference

Qiskit textbook — https://qiskit.org/textbook/preface.html

--

--

Navaneeth Dinesh
Analytics Vidhya

Artificial Intelligence | Machine learning | Quantum Computing