Qiskit: Z Gate

The Z Gate is an important gate in Quantum Computing. Like many other gates in Quantum Computing, Z Gate is a Pauli Gate. Qiskit provides a method for applying Z Gate on a Qubit. In this chapter of the Qiskit Tutorial, you will learn about Z Gate and how to apply Z Gate on a Qubit in Qiskit.


Z Gate

The Z Gate acts on a single Qubit. In terms of its effect on a state represented in terms of the standard basis pairs |0> and |1>, it has no effect on the coefficients of |0> to |1>. Hence, the application of Z Gate does not alter the probabilities of the state of the Qubit collapsing into |0> or |1> upon measurement. However, the Z Gate does flip the phase of the Qubit by 180° or π radians.

Note– The Z Gate is also known as Phase flip Gate.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Z Gate will change the state of the Qubit to Ψ = 1/sqrt(3) |0> - sqrt(2/3) |1>.

Before the Z Gate was applied to the Qubit, the probabilities of getting |0> and |1> were 1/3 and 2/3 respectively. After the Z Gate was applied on the Qubit, the probabilities of getting |0> and |1> were the same. Therefore, there was no change in the probabilities of getting |0> and |1>.This is demonstrated in the table below-

Before applying Z GateAfter applying Z Gate
Probability of Getting 0 on measurement1/31/3
Probability of Getting 1 on measurement2/32/3

Although, there is a change in the phase of the Qubit by 180° or π radians which can be noticed with the change of sign for the coefficient of |1>.


Z Gate: Bloch Sphere

The Z Gate performs a rotation of 180° or π radians about the Z-axis on the Bloch Sphere.

Example

In this example, we will look at how the application of Z gate on the Qubit affects its representation on the Bloch Sphere. Remember how Z Gate performs a rotation of 180° or π radians about the Z-axis on the Bloch Sphere.

In the below figure, the Z Gate is applied on a Qubit in the state Ψ = |i>, and the resulting state of the Qubit is Ψ = |-i>.

Qiskit Z Gate effect

In the below figure, the Z Gate is applied on a Qubit in the state Ψ = |+> and the resulting state of the Qubit is Ψ = |->.

Qiskit Z Gate effect

Note– Since Z Gate performs rotation about the Z-axis, it will have no effect on a vector that lies on the Z-axis.


Z Gate: Pauli Matrix

The Z Gate in Quantum Computing is represented by the matrix Z or σz

Pauli Matrix for Z Gate

The resulting state of a Qubit after the application of Z Gate can also be calculated by multiplying the Matrix for Z Gate with the vector representing the state of the Qubit.

Example

In this example, we will apply the Z gate to a Qubit, and calculate the resulting state by Multiplying it with the Pauli Matrix for Z Gate.

Let the state of the Qubit be Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>

After Applying the Z Gate, the resulting state of the Qubit can be calculated by-

Calculating result of Z Gate applied on a Qubit

Similarly as before, the probability of getting |0> and |1> are the same whereas there is a flip in the phase.


Inverse of Z Gate

The Z is its own inverse. Therefore, applying Z Gate to the same Qubit twice will result in the original state of the Qubit.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Z Gate will change the state of the Qubit to Ψ = 1/sqrt(3) |0> - sqrt(2/3) |1>.

After application of the Z Gate again to the same Qubit, the resulting state of the Qubit will be Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>.

Since applying Z Gate twice to the same Qubit results in the same state, Z Gate is its own inverse.


Z Gate in Qiskit

The Z Gate in Qiskit can be applied to any Qubit by calling the z() method on the Quantum Circuit(an instance of QuantumCircuit class) and passing it an integer for the Qubit on which Z Gate is to be applied.

Example

In this example, we will be applying Z Gate on the first Qubit in the Quantum Circuit, which will contain 2 Qubits.

# Creating a Quantum Register with 1 Qubit
qr = qiskit.QuantumRegister(2)

# Creating a Quantum Circuit
circuit = QuantumCircuit(qr)

# Applying Z Gate on the first Qubit
circuit.z(0)

# Drawing the Quantum Circuit
circuit.draw()

This will result in the following Quantum Circuit being drawn-

Notice that the Z Gate is applied to the first Qubit. Also notice, that Qubits follow a 0 based indexing.