Introduction to the RZ gate with code

Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.

Introduction

In this tutorial we will explore how to implement the RY gate on IBM’s Quantum Computers with Qiskit.

The RY gate is a rotational gate that does a rotation around the Z-axis by a specified amount which is normally denoted by λ.

The operation of the RZ gate can be described using the following matrix:

CodeCogsEqn (7).png

Using the matrix multiplication we can see how the RZ gate operates on the qubits state. For our first example lets initialise the qubits state to |0〉and set λ to be π. This should flip the qubits state from |0〉 to -i|0〉

The associated column vector for |0〉is:

CodeCogsEqn (90).gif

Then multiply the column vector by the RZ matrix:

Which means the qubit has changed from |0〉to −i|0〉

Now lets see what happens when we set the qubit to |1〉and set λ to π.

CodeCogsEqn (9).png

Which has changed the qubits state from |1〉to i|1〉

The best way to see how the RZ gate affects a qubits state is with a HRZH circuit. This is where the qubit put in to superposition with a Hadamard gate then a RZ gate is applied where λ = π.

Then the qubit will be brought out of superposition using another Hadamard gate.

HRZH circuit

HRZH circuit

If we set the qubit to |0〉and apply a Hadamard gate the qubits state will become:

CodeCogsEqn (10).png

Then we apply the RZ gate:

CodeCogsEqn (11).png

Then we bring the qubit out of superposition using another Hadamard gate:

With two Hadamard gates only the qubit should returned back to |0〉. However because we applied a RZ gate with λ=π the qubit has flipped from |0〉to -i|1〉

Implementation

In Qiskit we can implement an RZ gate very easily using the following function:

circuit.rz(pi,q[0])

Where pi is the rotation amount and q[0] is the qubit we want to apply the RZ gate to.

Go to the code section for the full code example. The example shows how the RZ gate affects the qubit state with different values for λ.

How to run the program

Copy and paste the code below in to a python file

  1. Enter your API token in the IBMQ.enable_account('Insert API token here') part

  2. Save and run

Code

from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, execute,IBMQ
from qiskit.tools.monitor import job_monitor
import numpy as np

pi = np.pi

IBMQ.enable_account('ENTER API KEY HERE')
provider = IBMQ.get_provider(hub='ibm-q')

backend = provider.get_backend('ibmq_qasm_simulator')

def rz(rotation):
    circuit = QuantumCircuit(q,c)

    circuit.h(q[0]) # Hadamard gate 
    circuit.rz(rotation,q[0]) # RZ gate
    circuit.h(q[0]) # Hadamard gate 
    circuit.measure(q,c) # Qubit Measurment

    print(circuit)

    job = execute(circuit, backend, shots=8192)
    
    job_monitor(job)
    counts = job.result().get_counts()

    print(counts)
    
q = QuantumRegister(1,'q')
c = ClassicalRegister(1,'c')

rotation = 0
rz(rotation)

rotation = pi/2
rz(rotation)

rotation = pi
rz(rotation)

Output

Output showing results when Lambda is Pi, Pi/2, and 0

Output showing results when Lambda is Pi, Pi/2, and 0