pub struct MpsState { /* private fields */ }Expand description
Matrix Product State quantum simulator.
Represents quantum states as a chain of tensors, enabling efficient simulation of circuits with bounded entanglement. Can handle hundreds to thousands of qubits when bond dimension stays manageable.
Implementations§
Source§impl MpsState
impl MpsState
Sourcepub fn new(num_qubits: usize) -> Result<Self>
pub fn new(num_qubits: usize) -> Result<Self>
Initialize the |00…0> product state.
Each tensor has bond dimension 1 and physical dimension 2, with the amplitude concentrated on the |0> basis state.
Sourcepub fn new_with_config(num_qubits: usize, config: MpsConfig) -> Result<Self>
pub fn new_with_config(num_qubits: usize, config: MpsConfig) -> Result<Self>
Initialize |00…0> with explicit configuration.
Sourcepub fn new_with_seed(
num_qubits: usize,
seed: u64,
config: MpsConfig,
) -> Result<Self>
pub fn new_with_seed( num_qubits: usize, seed: u64, config: MpsConfig, ) -> Result<Self>
Initialize |00…0> with a deterministic seed for reproducibility.
pub fn num_qubits(&self) -> usize
Sourcepub fn max_bond_dimension(&self) -> usize
pub fn max_bond_dimension(&self) -> usize
Current maximum bond dimension across all bonds in the MPS chain.
Sourcepub fn truncation_error(&self) -> f64
pub fn truncation_error(&self) -> f64
Accumulated truncation error from bond-dimension truncations.
pub fn measurement_record(&self) -> &[MeasurementOutcome]
Sourcepub fn apply_single_qubit_gate(
&mut self,
qubit: usize,
matrix: &[[Complex; 2]; 2],
)
pub fn apply_single_qubit_gate( &mut self, qubit: usize, matrix: &[[Complex; 2]; 2], )
Apply a 2x2 unitary to a single qubit.
Contracts the gate matrix with the physical index of tensor[qubit]: new_tensor(l, i’, r) = Sum_i matrix[i’][i] * tensor(l, i, r)
This does not change bond dimensions.
Sourcepub fn apply_two_qubit_gate_adjacent(
&mut self,
q1: usize,
q2: usize,
matrix: &[[Complex; 4]; 4],
) -> Result<()>
pub fn apply_two_qubit_gate_adjacent( &mut self, q1: usize, q2: usize, matrix: &[[Complex; 4]; 4], ) -> Result<()>
Apply a 4x4 unitary gate to two adjacent qubits.
The algorithm:
- Contract tensors at q1 and q2 into a combined 4-index tensor.
- Apply the 4x4 gate matrix on the two physical indices.
- Reshape into a matrix and perform truncated QR decomposition.
- Split back into two MPS tensors, respecting max_bond_dim.
Sourcepub fn apply_two_qubit_gate(
&mut self,
q1: usize,
q2: usize,
matrix: &[[Complex; 4]; 4],
) -> Result<()>
pub fn apply_two_qubit_gate( &mut self, q1: usize, q2: usize, matrix: &[[Complex; 4]; 4], ) -> Result<()>
Apply a 4x4 gate to any pair of qubits.
If the qubits are adjacent, delegates directly. Otherwise, uses SWAP gates to move the qubits next to each other, applies the gate, then swaps back to restore qubit ordering.
Sourcepub fn measure(&mut self, qubit: usize) -> Result<MeasurementOutcome>
pub fn measure(&mut self, qubit: usize) -> Result<MeasurementOutcome>
Measure a single qubit projectively.
- Compute the probability of |0> by locally contracting the MPS.
- Sample the outcome.
- Collapse the tensor at the measured qubit by projecting.
- Renormalize.
Sourcepub fn apply_gate(&mut self, gate: &Gate) -> Result<Vec<MeasurementOutcome>>
pub fn apply_gate(&mut self, gate: &Gate) -> Result<Vec<MeasurementOutcome>>
Apply a gate from the Gate enum, returning any measurement outcomes.