Two-Qubit Gates
Two-qubit gates are unitary transformations that operate on pairs of qubits, enabling entanglement and multi-qubit correlations. Each gate applies a 4×4 unitary matrix in the {|00⟩, |01⟩, |10⟩, |11⟩} computational basis.CNOT Gate (Controlled-NOT)
The CNOT gate flips the target qubit if and only if the control qubit is |1⟩. This is the primary entangling gate in quantum computing.Matrix Representation
In the basis ordering {|00⟩, |01⟩, |10⟩, |11⟩}:- |00⟩ → |00⟩
- |01⟩ → |01⟩
- |10⟩ → |11⟩ (flip target)
- |11⟩ → |10⟩ (flip target)
Implementation
Fromquantum_computer.py:996-1018:
Usage Example
Alternative Syntax
The CNOT gate can also be called using thecx method:
CZ Gate (Controlled-Z)
The CZ gate applies a phase flip of -1 to the |11⟩ state while leaving all other states unchanged. This gate is symmetric in its two qubits.Matrix Representation
- |00⟩ → |00⟩
- |01⟩ → |01⟩
- |10⟩ → |10⟩
- |11⟩ → -|11⟩ (phase flip)
Implementation
Fromquantum_computer.py:1020-1041:
Usage Example
Properties
- Symmetric: CZ(i,j) = CZ(j,i) - the order of qubits doesn’t matter
- Self-inverse: CZ² = I
- Diagonal in computational basis
SWAP Gate
The SWAP gate exchanges the quantum states of two qubits.Matrix Representation
- |00⟩ → |00⟩
- |01⟩ → |10⟩ (swap)
- |10⟩ → |01⟩ (swap)
- |11⟩ → |11⟩
Implementation
Fromquantum_computer.py:1043-1064:
Usage Example
Use Cases
- Qubit routing: Move quantum information between non-adjacent qubits
- Circuit optimization: Rearrange qubit ordering
- Quantum sorting algorithms
Gate Registry
Two-qubit gates are registered in_GATE_REGISTRY (quantum_computer.py:1157-1174):
Technical Implementation
All two-qubit gates use the_two_qubit_unitary function (quantum_computer.py:787-840), which:
- Identifies amplitude quadruplets: For each group of 4 basis states that share all bits except the control and target bits
- Applies 4×4 unitary transformation:
- Handles complex amplitudes: Each amplitude is a (2, G, G) spatial wavefunction with real and imaginary channels
- Preserves entanglement: Works within the joint Hilbert space structure
Bit Ordering Convention
Fromquantum_computer.py:799-806:
Creating Entanglement
Two-qubit gates are essential for creating entangled states that cannot be represented as tensor products of single-qubit states:See Also
- Single-Qubit Gates - H, X, Y, Z operations
- Rotation Gates - Parameterized gates
- Multi-controlled gates (Toffoli/CCX, MCZ) for advanced algorithms