Hadamard Operations¶
The Hadamard operators create quantum superpositions on registers and are fundamental operations in quantum algorithms.
Overview¶
Operator |
Operation |
Type constraint |
Unitarity class |
|---|---|---|---|
|
Hadamard on an integer register |
Integer type |
SelfAdjoint |
|
Full Hadamard (all output states) |
Integer type |
SelfAdjoint |
|
Single-qubit Hadamard |
Boolean (size=1) |
SelfAdjoint |
|
Partial-qubit Hadamard |
Integer type |
SelfAdjoint |
—
Hadamard_Int¶
Operation: Applies a Hadamard to the specified qubits of an integer register
Mathematical definition:
Type constraints: UnsignedInteger or SignedInteger
import pysparq as ps
ps.System.clear()
# 4-bit register
ps.System.add_register("q", ps.UnsignedInteger, 4)
state = ps.SparseState()
print("Initial state:")
ps.pprint(state)
# [1 basis state]
# |q=0⟩ : (1+0j)
# Apply Hadamard to the first 2 bits
ps.Hadamard_Int("q", 2)(state)
print("\nAfter Hadamard:")
ps.pprint(state)
# [2 basis states]
# |q=0⟩ : (0.707+0j)
# |q=2⟩ : (0.707+0j)
—
Hadamard_Int_Full¶
Operation: Applies a full Hadamard to the entire register, creating a uniform superposition of all \(2^n\) states
Mathematical definition:
Type constraints: Integer type
Note: This creates \(2^n\) basis states; for large n this may cause memory problems.
ps.System.clear()
# 2-bit register
ps.System.add_register("q", ps.UnsignedInteger, 2)
state = ps.SparseState()
# Full Hadamard: creates 2^2 = 4 states
ps.Hadamard_Int_Full("q")(state)
ps.pprint(state)
# [4 basis states]
# |q=0⟩ : (0.5+0j)
# |q=1⟩ : (0.5+0j)
# |q=2⟩ : (0.5+0j)
# |q=3⟩ : (0.5+0j)
# Applying it again undoes it (self-adjoint)
ps.Hadamard_Int_Full("q")(state)
# Back to a single basis state
—
Hadamard_Bool¶
Operation: Single-qubit Hadamard
Matrix:
Type constraints: Boolean (the register size must be 1)
Bit constraints: None (operates on bit 0 by default)
ps.System.clear()
# Single-qubit register
ps.System.add_register("qubit", ps.Boolean, 1) # Must be 1 bit!
state = ps.SparseState()
ps.Hadamard_Bool("qubit")(state)
ps.pprint(state)
# [2 basis states]
# |qubit=0⟩ : (0.707+0j)
# |qubit=1⟩ : (0.707+0j)
—
Hadamard_Partial¶
Operation: Applies a Hadamard to the specified qubits of a register
Type constraints: Integer type
Bit constraints: The specified positions must be within the register range
ps.System.clear()
# 4-bit register
ps.System.add_register("q", ps.UnsignedInteger, 4)
state = ps.SparseState()
# Apply Hadamard only to bits 1 and 3
positions = {1, 3}
ps.Hadamard_Partial("q", positions)(state)
# Creates 2^2 = 4 superposition states (only positions 1 and 3 flip)
—
Use Cases¶
Uniform superposition¶
Used in quantum search, quantum sampling and similar algorithms:
# Create a uniform superposition of the address register
ps.System.add_register("addr", ps.UnsignedInteger, n)
state = ps.SparseState()
ps.Hadamard_Int_Full("addr")(state)
# addr is now uniformly distributed over all possible values
Single-qubit initialization¶
Used to initialize a control bit:
ps.System.add_register("ctrl", ps.Boolean, 1)
ps.Hadamard_Bool("ctrl")(state)
# ctrl is in the |+⟩ = (|0⟩ + |1⟩)/√2 state
Partial superposition¶
For selective superposition:
# Superpose only the low 2 bits
ps.Hadamard_Int("reg", 2)(state)
# Or superpose specific positions
ps.Hadamard_Partial("reg", {0, 2})(state)