Lesson 6 of 15
The Z Gate
The Phase Flip Gate
The Pauli-Z gate flips the phase (sign) of the amplitude while leaving unchanged:
As a matrix:
Z = egin{pmatrix} 1 & 0 \ 0 & -1 end{pmatrix}
On a general state [alpha, eta]:
Z[alpha, eta] = [alpha, -eta]
def z_gate(state):
return [state[0], -state[1]]
The Z gate has no classical equivalent — it introduces a phase that is invisible when measuring in the computational basis, but becomes observable when combined with other gates.
For example, Z transforms into :
import math
s = 1 / math.sqrt(2)
plus = [s, s] # |+⟩
minus = z_gate(plus) # |−⟩ = [s, -s]
print(round(minus[0], 4)) # 0.7071
print(round(minus[1], 4)) # -0.7071
Your Task
Implement z_gate(state) that applies the Pauli-Z phase flip.
Python runtime loading...
Loading...
Click "Run" to execute your code.