CODE #5: HADAMARD GATE IN QUANTUM COMPUTING (PYTHON)
Welcome back to this series of articles, where we explore different coding blocks and their applications. Quantum computing is the bridge between computer science and quantum physics, and as such feels overly complicated as either discipline is thought of as arcane except for a subset of experts.
Today, we’ll take a look at a concept in quantum computing called the Hadamard gate. I will be expository so that you aren’t lost.
The interest of using Python for this piece is because I heard of QuTiP, which is just one of several Python libraries related to quantum computing. Here’s the official introduction:
“QuTiP is open-source software for simulating the dynamics of open quantum systems. The QuTiP library depends on the excellent Numpy, Scipy, and Cython numerical packages. In addition, graphical output is provided by Matplotlib. QuTiP aims to provide user-friendly and efficient numerical simulations of a wide variety of Hamiltonians, including those with arbitrary time-dependence, commonly found in a wide range of physics applications such as quantum optics, trapped ions, superconducting circuits, and quantum nanomechanical resonators. QuTiP is freely available for use and/or modification on all major platforms such as Linux, Mac OSX, and Windows. Being free of any licensing fees, QuTiP is ideal for exploring quantum mechanics and dynamics in the classroom.” [1]
Because quantum computing is, at its core, about computation, one has to put it into parallel with traditional computing. The Central Processing Unit typically has the capability to store data and operate on it. Using transistors and working with binary digits, or bits. Something we can track with assembly. Still, computers don’t do that out of magic, and we have referred to transistors, an electronics component.
Electronics function with circuitry, and one such type of component is the gate, which we abstract as logic gate. We can think of the Hadamard gate as unitary operator, which, taking a single qubit in, often formalized as |0> or |1> — though of any α∣0⟩+β∣1⟩ — operates on it the Hadamard transform. If one measures it, the ψ wave function collapses.
One should be aware that the Hadamard gate is reversible, by virtue of being a unitary operator.
Down the line, we can say that the Hadamard gate shapes amplitude, across basis states. It also helps creating interference.
“Note the minus sign in the second result. While both states yield a 50/50 probability if measured, they have different phases. This “relative phase” is what allows quantum algorithms to perform interference — canceling out wrong answers (destructive interference) while amplifying the right ones.” [3]
If you’re a nerd, you’ve probably heard about Shor’s algorithm, possibly the most famous algorithm in the entire field and for a reason. It deals with cryptography, a sensitive part of cybersecurity. That means for any sensationalist outlet, it is juicy.
“Shor’s algorithm is a quantum algorithm for finding the prime factors of an integer. It was developed in 1994 by the American mathematician Peter Shor. It is one of the few known quantum algorithms with compelling potential applications and strong evidence of superpolynomial speedup compared to best known classical (non-quantum) algorithms. However, beating classical computers may require quantum computers with millions of qubits due to the overhead caused by quantum error correction.” [2]
Well, the Hadamard gate is actually one of its initial building blocks.
# INCLUDES
import numpy as np
from qutip import basis, Qobj
import time
# VARIABLES
start = time.perf_counter()
H = Qobj(np.array([[1, 1], [1, -1]], dtype=float) / np.sqrt(2))
ket0 = basis(2, 0)
psi = H * ket0
probabilities = np.abs(psi.full().ravel())**2
elapsed = time.perf_counter() - start
# PRINTING
print(f"State: {psi}")
print(f"Probabilities: {probabilities}")
print(f"Time: {elapsed:.6f}s")We import NumPy for faster vector operations, QuTiP obviously for simulating components relevant to quantum computing, and time for good measure.
Then, we define the starting time, H the Hadamard gate, and psi the wave function that is to go through H.
Get Émilia Lilith-Lolita Hoarfrost’s stories in your inbox
Join Medium for free to get updates from this writer.
Probabilities uses a NumPy function abs(), over the result of full(), converting a Qobj to a NumPy array, and ravel() done to convert to unidimensional, times 2. It’s for the Born rule.
State: Quantum object: dims=[[2], [1]], shape=(2, 1), type='ket', dtype=Dense
Qobj data =
[[0.70710678]
[0.70710678]]
Probabilities: [0.5 0.5]
Time: 0.017856sA common representation for qubits is the Bloch sphere.
“This is the gate you’ll use more than any other. It takes a qubit sitting at the north pole [this should just be a visualization, not understood literally], definitely |0⟩, and moves it to the equator of the Bloch sphere. At the equator the qubit is in equal superposition, exactly 50% chance of measuring 0 and 50% chance of measuring 1.
What makes Hadamard special is not the randomness. It’s that the two possible outcomes carry phase information. The state is not just “either 0 or 1 with equal probability”, it’s a specific combination where the amplitudes can interfere with each other later. That interference is what quantum algorithms are actually built on.” [5]
import numpy as np
from qutip import basis, Qobj
H = Qobj([[1, 1], [1, -1]]) / np.sqrt(2)
psi0 = basis(2, 0) # could be basis(2, 1)!
psi1 = H * psi0
psi2 = H * psi1
p1 = np.abs(psi1.full().ravel())**2
p2 = np.abs(psi2.full().ravel())**2
print("After H:", p1)
print("After H^2:", p2)
print("H^2 ≈ I:", (H*H).full())And its output:
After H: [0.5 0.5]
After H^2: [1. 0.]
H^2 ≈ I: [[ 1.00000000e+00+0.j -2.23711432e-17+0.j]
[-2.23711432e-17+0.j 1.00000000e+00+0.j]]However, notice that if the circuit had a measurement between the two Hadamard gates, then the initial qubit would not revert back all of the time, instead being put into superposition. [7]
import numpy as np
from qutip import basis, Qobj
from random import choices
H = Qobj([[1, 1], [1, -1]]) / np.sqrt(2)
ket0 = basis(2, 0)
def measure(psi):
p = np.abs(psi.full().ravel())**2
return choices([0, 1], weights=p)[0]
psi = ket0
print("start:", "|0>")
psi = H * psi
m1 = measure(psi)
print("after H1:", np.abs(psi.full().ravel())**2)
print("measure 1:", f"|{m1}>")
psi = basis(2, m1)
psi = H * psi
m2 = measure(psi)
print("after H2:", np.abs(psi.full().ravel())**2)
print("measure 2:", f"|{m2}>")py .\main.py
start: |0>
after H1: [0.5 0.5]
measure 1: |1>
after H2: [0.5 0.5]
measure 2: |1>
py .\main.py
start: |0>
after H1: [0.5 0.5]
measure 1: |0>
after H2: [0.5 0.5]
measure 2: |0>But why do so many algorithms use the Hadamard gate?
Most quantum algorithms start with a Hadamard on every qubit, putting the whole system into a uniform superposition over every possible input. The algorithm then evaluates them all at once. You see this opening move in Shor’s algorithm, Grover’s search, the Deutsch–Jozsa algorithm, and many others. [6]
In this article, we have seen through code excerpts utilizing the NumPy and QuTiP libraries to explore a fundamental part of quantum computing algorithmic circuits: the Hadamard gate. The Hadamard gate is a unitary operator, that means it is reversible. In fact, two Hadamard gates nullify one another. Except if a measurement occurred between them. We have also seen that Bloch spheres were representations based on a pure state for a qubit, and that quantum computing gates sometimes represent themselves as rotations [see 9]. Anyway, Hadamard gates are used to put a whole system in uniform superposition over every possible input. “Simple as it is, the Hadamard gate plays a central role in theory and practice of quantum circuits, notably where in its combination with the CNOT gate it is used to produce Bell states (such as in the quantum teleportation protocol).” [10]
References
[1] Landing page. “QuTiP — Quantum Toolbox in Python”, QuTiP, c. 2026. https://qutip.org/
[2] Entry. “Shor’s algorithm”, Wikipedia, 2004–2026. https://en.wikipedia.org/wiki/Shor%27s_algorithm
[3] Entry. “Hadamard Gate”, QuEra, c. 2026. https://www.quera.com/glossary/hadamard-gate
[4] Entry. “Bloch sphere”, Wikipedia, 2006–2026. https://en.wikipedia.org/wiki/Bloch_sphere
[5] Article. “Quantum Gates Explained: From Theory to Circuit in Plain English”, Vishal Mysore, Medium, March 29, 2026. https://medium.com/@visrow/quantum-gates-explained-from-theory-to-circuit-in-plain-english-ae84266451c4
[6] Entry. “The Hadamard gate”, Sohum Thakkar, qolour, May 7, 2026. https://www.qolour.io/concepts/hadamard-gate
[7] Video. “Logic Gates Rotate Qubits”, Josh’s Channel, YouTube, August 20, 2021. https://www.youtube.com/watch?v=ZBaXPY_0TNI
[8] Entry. “Deutsch–Jozsa algorithm”, Wikipedia, 2007–2026. https://en.wikipedia.org/wiki/Deutsch%E2%80%93Jozsa_algorithm
[9] Thread. “Visual interpretation, on the Bloch sphere, when Hadamard gate is applied twice”, asked February 22, 2017, modified January 27, 2022, Physics Stack Exchange. Answer by HeinzKurt, January 19, 2019. https://physics.stackexchange.com/questions/313959/visual-interpretation-on-the-bloch-sphere-when-hadamard-gate-is-applied-twice
[10] Entry. “Hadamard gate”, Urs Schreiber, nLab, October 2, 2022-February 8, 2025. https://ncatlab.org/nlab/show/Hadamard+gate
Follow, clap, highlight, repost for more content and to show appreciation. Comment to discuss. Look at the Technology list for similar-minded articles.







