The Hadamard gate


Overview

The Hadamard gate is a single-qubit operation. The Hadamard gate is the simplest method for generating a random bit in a qubit. Hadamard is quantum operator for quantum coin-flipping.

H=1/sqrt(2)*([1,1], [1,-1])

How it works:

∣0⟩ -> (∣0⟩+∣1⟩) / sqrt(2)

∣1⟩ -> (∣0⟩-∣1⟩) / sqrt(2)

Imports

In [1]:
import qiskit
from qiskit import QuantumCircuit, Aer, execute

Build

In [2]:
# Create a circuit with one qubit, apply Hadamard gate, measure the result and draw the circuit.
qc = QuantumCircuit(1) # quantum circuit with one qubit
qc.h(0) # applying Hadamard gate
qc.measure_all() # measure all qubits at the same time, creates a new ClassicalRegister with a size equal to the number of qubits being measured

Execute

In [3]:
# Every time you run this you will get a different random results
backend = Aer.get_backend('statevector_simulator') # creating an AER provider which returns quantum state vector
result = execute(qc, backend).result() # executing experiment
out_state = result.get_statevector() # return the state vector at the end of the execution
print(out_state)
[0.+0.j 1.+0.j]

Analyze

In [4]:
qc.draw(output='mpl') # drawing a a matplotlib figure object for the circuit diagram
Out[4]:

Notes

In [5]:
qiskit.__version__
Out[5]:
'0.16.1'