π§± Matrix Product State (MPS) Simulation
For circuits with larger qubit counts (e.g., ), standard statevector simulation becomes memory-prohibitive due to the exponential growth of the state space ( complex floats). The MPSSimulator, defined in mps_simulator.py, uses a Tensor Network representation to simulate low-entanglement quantum circuits up to 20+ qubits.
π Tensor Network Representation
Instead of storing a single vector of size , the Matrix Product State (MPS) model factorizes the state into a 1D chain of local tensors, one for each qubit:
Each local tensor is a rank-3 tensor of shape :
- : Physical index dimension ( for qubits, representing and ).
- : Left virtual bond index.
- : Right virtual bond index.
- : Bond dimension (dimension of the virtual indices), which scales with the amount of entanglement between qubits.
The global state vector is reconstructed by contracting the virtual indices:
The simulator starts in the state, represented by bond dimensions of :
# Initial state tensors mapping
self.tensors = [np.array([[[1.0], [0.0]]], dtype=complex) for _ in range(n)]β‘ Gate Application Mechanics
1. Single-Qubit Gates (Local Contraction)
Applying a gate to qubit only affects its local tensor . The simulator contracts the gate matrix with the physical index of , which does not change the bond dimensions:
def _apply_single_qubit(self, q, gate):
# Contract physical index 'j' with gate index 'j'
self.tensors[q] = np.einsum('ijk,aj->iak', self.tensors[q], gate)2. Two-Qubit Gates (Contract, SVD, and Truncate)
For a two-qubit gate (like CNOT) acting on adjacent qubits and :
- Contract: Combine adjacent tensors and into a single rank-4 tensor of shape :
python
combined = np.einsum('ijk,klm->ijlm', self.tensors[q1], self.tensors[q2]) - Apply Gate: Multiply by the gate matrix (e.g. CNOT).
- Reshape: Reshape the tensor into a matrix of shape .
- SVD: Compute the Singular Value Decomposition (SVD):
- Truncate: Keep only the largest singular values, up to
max_bond_dim(default ). This step discards high-frequency entanglement to keep the tensors compressed. - Reconstruct: Re-normalize, split the singular values, and reshape the matrices back into two rank-3 tensors and .
β οΈ Nearest-Neighbor Connectivity Constraint
Because the SVD step splits adjacent tensors in the 1D chain, the MPS simulator only supports gates between adjacent qubits:
If a two-qubit gate is applied to non-adjacent qubits, the simulator raises a ValueError. This enforces the use of the transpiler's SWAP insertion routing.
π Projective Measurements on MPS
To measure qubit , the simulator projects its local tensor and re-normalizes the state:
- Calculate Probability: Computes the probability of measuring by contracting the physical index for state :
python
prob_0 = np.sum(np.abs(tensors[q][:, 0, :])**2) - State Collapse: Based on a random sample:
- If : zero out the entry:
tensors[q][:, 1, :] = 0. - If : zero out the entry:
tensors[q][:, 0, :] = 0.
- If : zero out the entry:
- Renormalize: Renormalizes the tensor:
python
norm = np.linalg.norm(self.tensors[q]) if norm > 0: self.tensors[q] = self.tensors[q] / norm