This guide explains the most common quantum gates, how they work, and includes simple examples using the Cirq library.
1. Identity Gate (I)#
- What It Does: Leaves the qubit unchanged.
- When to Use: To hold a qubit in its current state or as a placeholder.
Sample Code:
import cirq
q0 = cirq.NamedQubit("q0")
circuit = cirq.Circuit(cirq.I(q0)) # Apply Identity gate
print("Identity Gate Circuit:")
print(circuit)
2. NOT Gate (X)#
- What It Does: Flips the state of a qubit:
- If the qubit is off (
0
), it turns on (1
). - If the qubit is on (
1
), it turns off (0
).
- If the qubit is off (
Sample Code:
circuit = cirq.Circuit(cirq.X(q0)) # Apply NOT gate
print("NOT Gate Circuit:")
print(circuit)
3. Hadamard Gate (H)#
- What It Does: Creates a superposition:
- The qubit becomes “partly on and partly off.”
- When to Use: To start quantum algorithms or prepare for interference.
Sample Code:
circuit = cirq.Circuit(cirq.H(q0)) # Apply Hadamard gate
print("Hadamard Gate Circuit:")
print(circuit)
4. Z Gate#
- What It Does: Tweaks the “on” state (
1
) of the qubit without flipping it. - When to Use: To prepare for interference or phase-based algorithms.
Sample Code:
circuit = cirq.Circuit(cirq.Z(q0)) # Apply Z gate
print("Z Gate Circuit:")
print(circuit)
5. RY Gate (Rotation-Y)#
- What It Does: Rotates the qubit state around the Y-axis of the Bloch sphere by a specified angle.
- When to Use: To create arbitrary superpositions with real amplitudes.
Sample Code:
import numpy as np
circuit = cirq.Circuit(cirq.ry(np.pi/4)(q0)) # Apply RY gate with π/4 rotation
print("RY Gate Circuit:")
print(circuit)
6. CNOT Gate (Controlled-NOT)#
- What It Does: Flips the target qubit only if the control qubit is on (
1
). - When to Use: To link two qubits or create entanglement.
Sample Code:
q1 = cirq.NamedQubit("q1")
circuit = cirq.Circuit(cirq.CNOT(q0, q1)) # Apply CNOT gate
print("CNOT Gate Circuit:")
print(circuit)
7. Toffoli Gate (Controlled-Controlled-NOT)#
- What It Does: Flips the target qubit only if both control qubits are on.
- When to Use: For more complex logic like an AND operation.
Sample Code:
q2 = cirq.NamedQubit("q2")
circuit = cirq.Circuit(cirq.TOFFOLI(q0, q1, q2)) # Apply Toffoli gate
print("Toffoli Gate Circuit:")
print(circuit)
8. Phase Gates (S and T)#
- What They Do: Subtly adjust the qubit’s behavior without flipping it.
- When to Use: To fine-tune circuits for interference effects.
Sample Code:
circuit = cirq.Circuit(cirq.S(q0)) # Apply S gate (90-degree phase shift)
circuit.append(cirq.T(q0)) # Apply T gate (45-degree phase shift)
print("Phase Gates Circuit:")
print(circuit)
9. Swap Gate#
- What It Does: Swaps the states of two qubits.
- When to Use: To rearrange qubits in a circuit.
Sample Code:
circuit = cirq.Circuit(cirq.SWAP(q0, q1)) # Apply Swap gate
print("Swap Gate Circuit:")
print(circuit)
10. Controlled Gates#
- What They Do: Perform an operation on a target qubit only if a control qubit is in a certain state.
- When to Use: For conditional logic or dependencies between qubits.
Sample Code:
circuit = cirq.Circuit(cirq.ControlledGate(cirq.X)(q0, q1)) # Custom controlled gate
print("Controlled Gate Circuit:")
print(circuit)
Key Rules for Quantum Gates#
- Reversible: Every gate can be undone (e.g., applying the same gate twice).
- Superposition: Gates like Hadamard make qubits partly “on” and “off.”
- Entanglement: Multi-qubit gates (like CNOT) create dependencies between qubits.
- Interference: Phase gates adjust probabilities in subtle ways.
Use these gates to build quantum circuits and explore powerful quantum algorithms.