Introduction to the S gate in Qiskit 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 the S gate and how to implement on IBM quantum devices in Qiskit.

The S gate (otherwise known as the √Z gate) is a gate that performs a rotation of π/2 around the Z axis. It is represented by the following matrix:

Much like the Z gate if we initialise the qubit to |0〉and apply an S gate the qubits state will remain |0〉:

However if we first initialise the qubits state to |1〉:

Which has transformed the state from |1〉to i|1〉

It is important to note that since the S gate performs a rotation of π/2 if we apply two S gates we will get the equivalent of a Z gate:

Applying the first S gate:

Then the second S gate:

Which gives us −|1〉. This is correct as if we applied a Z gate to a qubit that is initialized to |1〉we would also get −|1〉

Since two S gates are equivalent to a Z gate we can flip a qubit from |0〉to |1〉by putting the qubit in to superposition, applying two S gates and then applying a closing Hadamard gate. To see how this works let’s go through step by step.

First initialise the qubit and apply a Hadamard gate:


Which has put the qubit in to superposition. Now if we apply the first S gate:

Now if we apply the second S gate:

and finally if we apply the closing Hadamard gate:

Which gives us |1〉as the final state! Note the full code below if for this example.

Impementation

The S gate can be implemented very easily in Qiskit using the following line of code:

circuit.s(q[0])

Where q[0] is the qubit we are applying the S gate to.

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

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

backend = provider.get_backend('ibmq_qasm_simulator')

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

circuit = QuantumCircuit(q,c)

circuit.h(q[0]) #Applying a Hadamard gate
circuit.s(q[0]) #Applying an S gate
circuit.s(q[0])
circuit.h(q[0]) #Applying another Hadamard gate to bring the qubit out of superposition
circuit.measure(q,c) #Measuring the qubit

print(circuit)

job = execute(circuit, backend, shots=8192)

job_monitor(job)
counts = job.result().get_counts()

print(counts)

Output

Output showing the circuit and that the qubit has flipped from 0 to 1