Phase 0.1 — Complex Numbers: Session 21
H⊗H, Gate Identities, and Two New Two-Qubit Gates: CZ and SWAP
This session pushes the gate-by-gate shortcut through its first real workout — tracking signs through H⊗H — then reveals a powerful identity for composing tensor-product gates, and introduces two new two-qubit gates: CZ (phase-only entangling) and SWAP (literal qubit exchange).
Part 1: Sign-Tracking with H⊗I and I⊗H
(H⊗I)|10⟩ — Where the Minus Sign Comes From
(H⊗I)|10⟩ = (H|1⟩) ⊗ (I|0⟩)Since H|1⟩ = (|0⟩-|1⟩)/√2 and I|0⟩ = |0⟩:
= [(|0⟩-|1⟩)/√2] ⊗ |0⟩= (|00⟩ - |10⟩)/√2The minus sign appears precisely because H|1⟩ = |-⟩.
(I⊗H)|11⟩
(I⊗H)|11⟩ = (I|1⟩) ⊗ (H|1⟩)I|1⟩ = |1⟩, H|1⟩ = (|0⟩-|1⟩)/√2= |1⟩ ⊗ (|0⟩-|1⟩)/√2= (|10⟩ - |11⟩)/√2The Shortcut to Remember
H|0⟩ = (|0⟩+|1⟩)/√2 = |+⟩ ← plus sign
H|1⟩ = (|0⟩-|1⟩)/√2 = |-⟩ ← minus signWhenever H acts on a 1, expect a minus sign in the result. This single fact lets you predict signs without re-deriving them each time.
(H⊗I)|01⟩
(H⊗I)|01⟩ = (H|0⟩) ⊗ (I|1⟩) = [(|0⟩+|1⟩)/√2] ⊗ |1⟩ = (|01⟩+|11⟩)/√2Positive sign throughout — because here H acts on |0⟩, not |1⟩.
Part 2: H⊗H on |00⟩
The Calculation
(H⊗H)|00⟩ = (H|0⟩) ⊗ (H|0⟩)= [(|0⟩+|1⟩)/√2] ⊗ [(|0⟩+|1⟩)/√2]The two normalization factors multiply:
(1/√2)(1/√2) = 1/2Expanding All Four Terms
(H⊗H)|00⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩)/2As a Vector
(1/2)[1, 1, 1, 1]ᵀEach outcome has probability |1/2|² = 1/4 — a perfectly uniform distribution across all four basis states.
Part 3: H⊗H on |11⟩ — Tracking Every Sign
The Calculation
(H⊗H)|11⟩ = (H|1⟩) ⊗ (H|1⟩) = [(|0⟩-|1⟩)/√2] ⊗ [(|0⟩-|1⟩)/√2]= (|0⟩-|1⟩)(|0⟩-|1⟩)/2Expanding with Sign Rules
(+)(+) = + |00⟩: +
(+)(-) = - |01⟩: -
(-)(+) = - |10⟩: -
(-)(-) = + |11⟩: +The Result
(H⊗H)|11⟩ = (|00⟩ - |01⟩ - |10⟩ + |11⟩)/2Measurement Probabilities
The amplitudes are 1/2, -1/2, -1/2, 1/2. Squaring:
P(00) = P(01) = P(10) = P(11) = 1/4Same probabilities as (H⊗H)|00⟩ — but a genuinely different quantum state, since the signs differ. The difference is hidden in relative phase, invisible to a direct computational-basis measurement but potentially revealed by further gates through interference. This is the same lesson from Session 4, now playing out at the two-qubit level.
Part 4: The Gate Composition Identity
The Pattern
(A⊗B)(C⊗D) = (AC)⊗(BD)whenever the matrix dimensions match. Applying two tensor-product gates in sequence is the same as applying the composed gates on each qubit independently, then tensoring the results.
Applying It to H⊗H Twice
(H⊗H)(H⊗H) = (HH)⊗(HH) = H²⊗H²Since H² = I:
= I⊗IApplying H⊗H twice does nothing overall — without expanding a single basis state.
The Consequence
Since we started from |11⟩:
(H⊗H)² |11⟩ = (I⊗I)|11⟩ = |11⟩Applying H⊗H to (|00⟩-|01⟩-|10⟩+|11⟩)/2 a second time returns exactly |11⟩ — the original state, with no residual sign or phase.
A General Habit Worth Building
Before expanding a large tensor-product calculation, check for a gate identity that simplifies it. (H⊗H)² = I⊗I saved an entire four-term expansion here. This pattern-recognition instinct becomes increasingly valuable as circuits grow.
Verifying with a Different Starting State
Starting from |01⟩ and applying H⊗H twice:
(H⊗H)²|01⟩ = (I⊗I)|01⟩ = |01⟩The identity holds regardless of which basis state you start from — it’s a property of the gate sequence, not the particular input.
Part 5: The CZ Gate (Controlled-Z)
The Rule
CZ acts differently from CNOT — it doesn’t flip any bit. It adds a phase of -1, but only to |11⟩:
|00⟩ → |00⟩
|01⟩ → |01⟩
|10⟩ → |10⟩
|11⟩ → -|11⟩Applying CZ to |Φ⁺⟩
CZ[(|00⟩+|11⟩)/√2]|00⟩ is untouched; |11⟩ picks up a minus sign:
= (|00⟩ - |11⟩)/√2A New Bell State
|Φ⁻⟩ = (|00⟩ - |11⟩)/√2This is the third Bell state you’ve now constructed (after |Φ⁺⟩ and |Ψ⁺⟩).
Part 6: Comparing |Φ⁺⟩ and |Φ⁻⟩
Identical Direct Measurement Probabilities
Both states give:
P(00) = 1/2, P(11) = 1/2, P(01) = P(10) = 0Measuring either state directly in the computational basis, you cannot tell them apart.
The Hidden Difference
|Φ⁺⟩ = (|00⟩ + |11⟩)/√2 ← relative phase 0°
|Φ⁻⟩ = (|00⟩ - |11⟩)/√2 ← relative phase 180°Why This Matters
This mirrors exactly the single-qubit |+⟩ vs |-⟩ distinction from early in the series: same computational-basis probabilities, different relative phase, and that phase becomes observable once you apply additional gates that convert phase differences into amplitude differences — interference, now at the two-qubit level.
Part 7: The SWAP Gate
The Rule
SWAP does exactly what its name suggests — exchanges the two qubits:
SWAP|ab⟩ = |ba⟩The Truth Table
|00⟩ → |00⟩ (symmetric, no change)
|01⟩ → |10⟩ (swapped)
|10⟩ → |01⟩ (swapped)
|11⟩ → |11⟩ (symmetric, no change)Applying SWAP to |01⟩
SWAP|01⟩ = |10⟩Straightforward: the first qubit (0) and second qubit (1) trade places, giving 1 first and 0 second.
Why SWAP Is Simpler Than CNOT or CZ
Unlike CNOT (which conditionally flips) or CZ (which conditionally phases), SWAP has no control qubit at all — it treats both qubits symmetrically, just exchanging their roles. Notably, |00⟩ and |11⟩ are fixed points (swapping identical bits changes nothing), while |01⟩ and |10⟩ trade places with each other.
Part 8: Code Implementation
import numpy as npdef tensor(*mats):
result = mats[0]
for m in mats[1:]:
result = np.kron(result, m)
return resultH = (1/np.sqrt(2)) * np.array([[1, 1], [1, -1]])
I = np.eye(2)ket_0 = np.array([1, 0], dtype=complex)
ket_1 = np.array([0, 1], dtype=complex)def basis_vec(bits):
v = np.array([1], dtype=complex)
for bit in bits:
v = np.kron(v, ket_0 if bit == '0' else ket_1)
return v# === H⊗H on |00⟩ and |11⟩ ===
print("=== H⊗H Results ===")
HH = tensor(H, H)
result_00 = HH @ basis_vec('00')
result_11 = HH @ basis_vec('11')
print(f"(H⊗H)|00⟩ = {result_00.real}")
print(f"(H⊗H)|11⟩ = {result_11.real}")probs_00 = np.abs(result_00)**2
probs_11 = np.abs(result_11)**2
print(f"Probabilities for |00⟩ case: {probs_00}")
print(f"Probabilities for |11⟩ case: {probs_11}")
print(f"Same probabilities, different state? {np.allclose(probs_00, probs_11)} / {not np.allclose(result_00, result_11)}\n")# === Verify (H⊗H)^2 = I⊗I ===
print("=== Gate Composition Identity ===")
HH_squared = HH @ HH
II = tensor(I, I)
print(f"(H⊗H)² == I⊗I? {np.allclose(HH_squared, II)}\n")# Apply (H⊗H)^2 to |11⟩, should return |11⟩
double_applied = HH @ result_11
print(f"(H⊗H) applied again to (H⊗H)|11⟩: {double_applied.real}")
print(f"Matches original |11⟩? {np.allclose(double_applied, basis_vec('11'))}\n")# === CZ Gate ===
print("=== CZ Gate ===")
CZ = np.diag([1, 1, 1, -1]).astype(complex)
phi_plus = (basis_vec('00') + basis_vec('11')) / np.sqrt(2)
phi_minus = CZ @ phi_plus
print(f"|Φ⁺⟩ = {phi_plus.real}")
print(f"CZ|Φ⁺⟩ = {phi_minus.real} (this is |Φ⁻⟩)\n")# Compare measurement probabilities
probs_phi_plus = np.abs(phi_plus)**2
probs_phi_minus = np.abs(phi_minus)**2
print(f"P(|Φ⁺⟩) = {probs_phi_plus}")
print(f"P(|Φ⁻⟩) = {probs_phi_minus}")
print(f"Identical probabilities? {np.allclose(probs_phi_plus, probs_phi_minus)}\n")# === SWAP Gate ===
print("=== SWAP Gate ===")
SWAP = np.array([
[1,0,0,0],
[0,0,1,0],
[0,1,0,0],
[0,0,0,1]
], dtype=complex)for label in ['00', '01', '10', '11']:
result = SWAP @ basis_vec(label)
idx = np.argmax(np.abs(result))
result_label = ['00','01','10','11'][idx]
print(f"SWAP|{label}⟩ = |{result_label}⟩")# Verify SWAP is its own inverse
print(f"\nSWAP² == I⊗I? {np.allclose(SWAP @ SWAP, II)}")
Output:
=== H⊗H Results ===
(H⊗H)|00⟩ = [0.5 0.5 0.5 0.5]
(H⊗H)|11⟩ = [ 0.5 -0.5 -0.5 0.5]
Probabilities for |00⟩ case: [0.25 0.25 0.25 0.25]
Probabilities for |11⟩ case: [0.25 0.25 0.25 0.25]
Same probabilities, different state? True / True=== Gate Composition Identity ===
(H⊗H)² == I⊗I? True(H⊗H) applied again to (H⊗H)|11⟩: [0. 0. 0. 1.]
Matches original |11⟩? True=== CZ Gate ===
|Φ⁺⟩ = [0.70710678 0. 0. 0.70710678]
CZ|Φ⁺⟩ = [ 0.70710678 0. 0. -0.70710678] (this is |Φ⁻⟩)P(|Φ⁺⟩) = [0.5 0. 0. 0.5]
P(|Φ⁻⟩) = [0.5 0. 0. 0.5]
Identical probabilities? True=== SWAP Gate ===
SWAP|00⟩ = |00⟩
SWAP|01⟩ = |10⟩
SWAP|10⟩ = |01⟩
SWAP|11⟩ = |11⟩SWAP² == I⊗I? True
Part 9: Summary Table
Gate Effect Key Property H⊗H Equal superposition of all 4 states (with signs depending on input) (H⊗H)²=I⊗I CZ Phases |11⟩ by -1, leaves others unchanged Turns |Φ⁺⟩ into |Φ⁻⟩ SWAP |ab⟩ → |ba⟩ Self-inverse; |00⟩,|11⟩ are fixed points Composition identity (A⊗B)(C⊗D)=(AC)⊗(BD) Simplifies repeated tensor-product gates
Part 10: Mastery Check
1. Compute (H⊗I)|11⟩ using the gate-by-gate shortcut, tracking the sign carefully.
Get Dalveer Singh’s stories in your inbox
Join Medium for free to get updates from this writer.
2. Verify (H⊗H)|01⟩ by expanding all four terms with correct signs.
3. Using the composition identity, simplify (X⊗X)(X⊗X) without expanding any basis states.
4. Apply CZ to |Ψ⁺⟩ = (|01⟩+|10⟩)/√2. Does CZ change this state at all? Why or why not?
5. What is SWAP(|Ψ⁺⟩) for |Ψ⁺⟩ = (|01⟩+|10⟩)/√2? Explain your answer using the symmetry of the state.
6. Is CZ its own inverse? Verify using the diagonal matrix form diag(1,1,1,-1).
7. Explain in your own words why |Φ⁺⟩ and |Φ⁻⟩ are experimentally indistinguishable using only computational-basis measurements, but are still different quantum states.
8. Using the composition identity, what is (CZ)(CZ) equal to? (Hint: think about what two -1 phases on |11⟩ give you.)
What Comes Next
You now have:
✅ Reliable sign-tracking through H⊗I, I⊗H, and H⊗H calculations ✅ The gate composition identity (A⊗B)(C⊗D)=(AC)⊗(BD) — a powerful simplification tool ✅ CZ gate — phase-only entangling, producing |Φ⁻⟩ ✅ SWAP gate — literal qubit exchange, with |00⟩ and |11⟩ as fixed points ✅ A third Bell state and the general pattern of phase-distinguished entangled pairs
The next session will:
- Complete the full set of four Bell states (
|Φ⁺⟩, |Φ⁻⟩, |Ψ⁺⟩, |Ψ⁻⟩) and show how CNOT, CZ, and X/Z gates convert between them - Formally prove that Bell states cannot be factored into product states
- Introduce Toffoli (CCNOT), the three-qubit controlled gate essential for reversible classical computation inside quantum circuits
The Growing Toolkit
Single-qubit gates: X, Y, Z, H, S, R_x, R_y, R_z
Two-qubit gates: CNOT, CZ, SWAP
Composition rule: (A⊗B)(C⊗D) = (AC)⊗(BD)
Entangled states: |Φ⁺⟩, |Φ⁻⟩, |Ψ⁺⟩ (so far)Every circuit you’ll encounter from here is built from combinations of exactly these pieces.
Ready? Work through all eight mastery questions — especially #3 and #8, which test whether the composition identity has become a genuine shortcut rather than just a fact you’ve memorized.




