Lesson 11 of 15
Quantum Circuits
Composing Gates
A quantum circuit is a sequence of gates applied to qubits. Gates are applied left to right (first gate first).
We can represent a circuit as a list of gate functions and apply them in order:
def apply_gates(state, gates):
for gate in gates:
state = gate(state)
return state
Example:
Applying these three gates in sequence to is equivalent to applying the Z gate:
import math
def hadamard(state):
s = 1 / math.sqrt(2)
a, b = state[0], state[1]
return [s * (a + b), s * (a - b)]
def x_gate(state):
return [state[1], state[0]]
zero = [1.0, 0.0]
result = apply_gates(zero, [hadamard, x_gate, hadamard])
print(round(result[0], 4)) # 1.0 — same as Z|0⟩ = |0⟩
print(round(result[1], 4)) # 0.0
This is called a circuit identity: .
Your Task
Implement apply_gates(state, gates) and verify three circuit identities.
Python runtime loading...
Loading...
Click "Run" to execute your code.