Hadamard gate with Code

This is a simple program for beginners demonstrating Quantum Superpositioning with the Hadamard gate.

This tutorial is an excerpt from our upcoming textbook Introduction to Quantum Computing with Qiskit.

What is the Hadamard gate?

A Hadamard gate is one of the most important gates in quantum computing. Simply put it puts a qubit in to superposition of states such that if the qubit is |0〉then the state will become:

and if the state is |1〉then the state will become:

2021-08-03 22_02_27-Window.png

This means that when the qubit is measured it will collapse to either |0〉or |1〉with equal probability.

In terms of rotations this corresponds to a rotation of pi radians around the Z-axis followed by pi/2 (90 degrees) around the Y-axis. This means that the pure state will be on the equator of the bloch sphere.

Note that if the state is |0〉then the resulting state will be on the equator of the bloch sphere (known as |+〉) but if it is |1〉then it will be on the opposite side of the equator (known as |−〉).

In quantum computing logic gates can be described using matrices. The matrix associated with the hadamard gate is:

CodeCogsEqn (13).gif

By multiplying the qubits state by the matrix above we can see how the logic gate affects the qubits state. For example if we initialise the qubit to |0〉and apply the hadamard gate :

CodeCogsEqn (14).gif
CodeCogsEqn (17).gif

Which has mapped:

2021-08-03 22_10_28-Window.png

If we instead initialise the qubit to |1〉and apply a Hadamard gate:

CodeCogsEqn (18).gif

Which has mapped:

2021-08-03 22_14_01-Window.png

Implementation

In Qiskit the Hadamard gate can be demonstrated easily by simply applying a Hadamard gate to a qubit and then measuring it.

Circuit diagram of the program

Circuit diagram of the program

Device used

The ibmq_qasm_simulator is used which simulates a real quantum device. Note that results from this simulator contain no errors unlike on the real quantum devices which are fairly noisy.

Note: This program requires that you have an API token. To get one sign up to IBM Q Experience and get your token here: https://quantum-computing.ibm.com/account

Source

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

IBMQ.enable_account('Insert account token here') # Get this from your IBM Q account
provider = IBMQ.get_provider(hub='ibm-q')

q = QuantumRegister(1,'q') # Initialise quantum register
c = ClassicalRegister(1,'c') # Initialise classical register

circuit = QuantumCircuit(q,c) # Initialise circuit
circuit.h(q[0]) # Put Qubit 0 in to superposition using hadamard gate 
circuit.measure(q,c) # Measure qubit

backend = provider.get_backend('ibmq_qasm_simulator') # Set device to IBMs quantum simulator
job = execute(circuit, backend, shots=1024) # Execute job and run program 1024 times  
           
job_monitor(job)
counts = job.result().get_counts()

print('RESULT: ',counts) # Print result 
print('Press any key to close')
input()

Output

Output showing the qubit measurements when measured 1024 times

Output showing the qubit measurements when measured 1024 times