use num_complex::Complex;
use tpt_math_linalg::tpt_math_linalg_dense::DMatrix;
use crate::tensor::{self, Circuit};
use crate::{State, StateError};
const IDENTITY: [Complex<f64>; 4] = [
Complex::new(1.0, 0.0),
Complex::new(0.0, 0.0),
Complex::new(0.0, 0.0),
Complex::new(1.0, 0.0),
];
#[derive(Clone, Debug, PartialEq)]
pub struct Matrix {
dim: usize,
data: Vec<Complex<f64>>,
}
impl Matrix {
pub fn from_row_major(dim: usize, data: Vec<Complex<f64>>) -> Result<Self, StateError> {
if data.len() != dim * dim {
return Err(StateError::MatrixSizeMismatch {
expected: dim * dim,
got: data.len(),
});
}
Ok(Matrix { dim, data })
}
#[must_use]
pub fn dim(&self) -> usize {
self.dim
}
#[must_use]
pub fn get(&self, row: usize, col: usize) -> Complex<f64> {
self.data[row * self.dim + col]
}
#[must_use]
pub fn data(&self) -> &[Complex<f64>] {
&self.data
}
}
fn embed_single_qubit_kraus(
n: usize,
qubit: usize,
gate: &[Complex<f64>; 4],
coeff: f64,
) -> Matrix {
let scaled = [
gate[0].scale(coeff),
gate[1].scale(coeff),
gate[2].scale(coeff),
gate[3].scale(coeff),
];
let real = tensor::expand_single(n, qubit, &scaled);
let d = 1usize << n;
let mut data = vec![Complex::new(0.0, 0.0); d * d];
for (i, row) in data.chunks_mut(d).enumerate() {
for (j, entry) in row.iter_mut().enumerate() {
*entry = tensor::unembed_entry(&real, i, j);
}
}
Matrix { dim: d, data }
}
#[derive(Clone, Debug, PartialEq)]
pub struct DensityMatrix {
n: usize,
real: DMatrix<f64>,
}
impl DensityMatrix {
#[must_use]
pub fn n_qubits(&self) -> usize {
self.n
}
#[must_use]
pub fn dim(&self) -> usize {
1usize << self.n
}
#[must_use]
pub fn from_pure_state(state: &State) -> Self {
let n = state.n_qubits();
let d = 1usize << n;
let amps = state.amplitudes();
let mut entries = vec![Complex::new(0.0, 0.0); d * d];
for i in 0..d {
for j in 0..d {
entries[i * d + j] = amps[i] * amps[j].conj();
}
}
let real = tensor::embed_complex_square(&entries, d);
DensityMatrix { n, real }
}
pub fn from_mixture(states: &[State], probs: &[f64]) -> Result<Self, StateError> {
if states.is_empty() {
return Err(StateError::EmptyMixture);
}
if states.len() != probs.len() {
return Err(StateError::MixtureSizeMismatch {
states: states.len(),
probs: probs.len(),
});
}
if probs.iter().any(|&p| p < 0.0) {
return Err(StateError::NegativeProbability);
}
let sum: f64 = probs.iter().sum();
if (sum - 1.0).abs() > 1e-6 {
return Err(StateError::ProbabilitiesNotNormalized(sum));
}
let n = states[0].n_qubits();
for s in states {
if s.n_qubits() != n {
return Err(StateError::QubitCountMismatch {
expected: n,
got: s.n_qubits(),
});
}
}
let size = (1usize << n) * 2;
let mut real = DMatrix::from_fn(size, size, |_, _| 0.0);
for (s, &p) in states.iter().zip(probs) {
let component = Self::from_pure_state(s).real * p;
real = real + component;
}
Ok(DensityMatrix { n, real })
}
#[must_use]
pub fn entry(&self, i: usize, j: usize) -> Complex<f64> {
tensor::unembed_entry(&self.real, i, j)
}
#[must_use]
pub fn trace(&self) -> Complex<f64> {
let d = self.dim();
(0..d).map(|i| self.entry(i, i)).sum()
}
#[must_use]
pub fn probabilities(&self) -> Vec<f64> {
let d = self.dim();
(0..d).map(|i| self.entry(i, i).re).collect()
}
#[must_use]
pub fn expectation_z(&self) -> f64 {
let d = self.dim();
(0..d)
.map(|i| {
let sign = if i.count_ones() & 1 == 0 { 1.0 } else { -1.0 };
sign * self.entry(i, i).re
})
.sum()
}
fn conjugate_by(&mut self, u: &DMatrix<f64>) {
let ut = u.transpose();
self.real = u.clone() * self.real.clone() * ut;
}
pub fn apply_gate(&mut self, gate: &[Complex<f64>; 4], qubit: usize) -> Result<(), StateError> {
let n = self.n;
if qubit >= n {
return Err(StateError::InvalidQubit { qubit, n });
}
let mut c = Circuit::new(n);
c.gate(qubit, *gate);
self.conjugate_by(&c.unitary());
Ok(())
}
pub fn apply_cnot(&mut self, control: usize, target: usize) -> Result<(), StateError> {
let n = self.n;
if control >= n {
return Err(StateError::InvalidQubit { qubit: control, n });
}
if target >= n {
return Err(StateError::InvalidQubit { qubit: target, n });
}
if control == target {
return Err(StateError::SameQubits(control));
}
let mut c = Circuit::new(n);
c.cnot(control, target);
self.conjugate_by(&c.unitary());
Ok(())
}
pub fn h(&mut self, qubit: usize) -> Result<(), StateError> {
self.apply_gate(&crate::H, qubit)
}
pub fn x(&mut self, qubit: usize) -> Result<(), StateError> {
self.apply_gate(&crate::X, qubit)
}
pub fn y(&mut self, qubit: usize) -> Result<(), StateError> {
self.apply_gate(&crate::Y, qubit)
}
pub fn z(&mut self, qubit: usize) -> Result<(), StateError> {
self.apply_gate(&crate::Z, qubit)
}
pub fn apply_kraus(&mut self, kraus_ops: &[Matrix]) -> Result<(), StateError> {
let d = self.dim();
for k in kraus_ops {
if k.dim() != d {
return Err(StateError::MatrixSizeMismatch {
expected: d,
got: k.dim(),
});
}
}
let size = self.real.nrows();
let mut acc = DMatrix::from_fn(size, size, |_, _| 0.0);
for k in kraus_ops {
let k_real = tensor::embed_complex_square(k.data(), d);
let kt = k_real.transpose();
let term = k_real * self.real.clone() * kt;
acc = acc + term;
}
self.real = acc;
Ok(())
}
pub fn bit_flip_kraus(n: usize, qubit: usize, p: f64) -> Result<Vec<Matrix>, StateError> {
if qubit >= n {
return Err(StateError::InvalidQubit { qubit, n });
}
if !(0.0..=1.0).contains(&p) {
return Err(StateError::InvalidProbability(p));
}
let k0 = embed_single_qubit_kraus(n, qubit, &IDENTITY, (1.0 - p).sqrt());
let k1 = embed_single_qubit_kraus(n, qubit, &crate::X, p.sqrt());
Ok(vec![k0, k1])
}
pub fn depolarizing_kraus(n: usize, qubit: usize, p: f64) -> Result<Vec<Matrix>, StateError> {
if qubit >= n {
return Err(StateError::InvalidQubit { qubit, n });
}
if !(0.0..=1.0).contains(&p) {
return Err(StateError::InvalidProbability(p));
}
let c0 = (1.0 - 0.75 * p).sqrt();
let c = (p / 4.0).sqrt();
let k0 = embed_single_qubit_kraus(n, qubit, &IDENTITY, c0);
let k1 = embed_single_qubit_kraus(n, qubit, &crate::X, c);
let k2 = embed_single_qubit_kraus(n, qubit, &crate::Y, c);
let k3 = embed_single_qubit_kraus(n, qubit, &crate::Z, c);
Ok(vec![k0, k1, k2, k3])
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_abs_diff_eq;
const TOL: f64 = 1e-9;
#[test]
fn from_pure_state_matches_outer_product() {
let mut s = State::new(2).unwrap();
s.h(0).unwrap();
s.cnot(0, 1).unwrap();
let amps = s.amplitudes().to_vec();
let rho = DensityMatrix::from_pure_state(&s);
for i in 0..4 {
for j in 0..4 {
let expected = amps[i] * amps[j].conj();
let got = rho.entry(i, j);
assert_abs_diff_eq!(got.re, expected.re, epsilon = TOL);
assert_abs_diff_eq!(got.im, expected.im, epsilon = TOL);
}
}
assert_abs_diff_eq!(rho.trace().re, 1.0, epsilon = TOL);
assert_abs_diff_eq!(rho.trace().im, 0.0, epsilon = TOL);
}
#[test]
fn trace_stays_one_after_gate_application() {
let s = State::new(2).unwrap();
let mut rho = DensityMatrix::from_pure_state(&s);
rho.h(0).unwrap();
rho.apply_cnot(0, 1).unwrap();
assert_abs_diff_eq!(rho.trace().re, 1.0, epsilon = TOL);
assert_abs_diff_eq!(rho.trace().im, 0.0, epsilon = TOL);
}
#[test]
fn trace_stays_one_after_kraus_application() {
let mut s = State::new(2).unwrap();
s.h(0).unwrap();
s.cnot(0, 1).unwrap();
let mut rho = DensityMatrix::from_pure_state(&s);
let bit_flip = DensityMatrix::bit_flip_kraus(2, 0, 0.3).unwrap();
rho.apply_kraus(&bit_flip).unwrap();
assert_abs_diff_eq!(rho.trace().re, 1.0, epsilon = TOL);
let depolarizing = DensityMatrix::depolarizing_kraus(2, 1, 0.4).unwrap();
rho.apply_kraus(&depolarizing).unwrap();
assert_abs_diff_eq!(rho.trace().re, 1.0, epsilon = TOL);
}
#[test]
fn depolarizing_p0_is_identity() {
let mut s = State::new(1).unwrap();
s.h(0).unwrap();
let rho_before = DensityMatrix::from_pure_state(&s);
let mut rho = rho_before.clone();
let kraus = DensityMatrix::depolarizing_kraus(1, 0, 0.0).unwrap();
rho.apply_kraus(&kraus).unwrap();
for i in 0..2 {
for j in 0..2 {
let a = rho.entry(i, j);
let b = rho_before.entry(i, j);
assert_abs_diff_eq!(a.re, b.re, epsilon = TOL);
assert_abs_diff_eq!(a.im, b.im, epsilon = TOL);
}
}
}
#[test]
fn depolarizing_p1_is_maximally_mixed() {
let mut s = State::new(1).unwrap();
s.h(0).unwrap();
let mut rho = DensityMatrix::from_pure_state(&s);
let kraus = DensityMatrix::depolarizing_kraus(1, 0, 1.0).unwrap();
rho.apply_kraus(&kraus).unwrap();
assert_abs_diff_eq!(rho.entry(0, 0).re, 0.5, epsilon = TOL);
assert_abs_diff_eq!(rho.entry(1, 1).re, 0.5, epsilon = TOL);
assert_abs_diff_eq!(rho.entry(0, 1).norm(), 0.0, epsilon = TOL);
assert_abs_diff_eq!(rho.entry(1, 0).norm(), 0.0, epsilon = TOL);
assert_abs_diff_eq!(rho.trace().re, 1.0, epsilon = TOL);
}
#[test]
fn bit_flip_p1_matches_x_gate() {
let mut s = State::new(2).unwrap();
s.h(0).unwrap();
s.cnot(0, 1).unwrap();
let mut rho = DensityMatrix::from_pure_state(&s);
let kraus = DensityMatrix::bit_flip_kraus(2, 0, 1.0).unwrap();
rho.apply_kraus(&kraus).unwrap();
let mut s_flipped = s.clone();
s_flipped.x(0).unwrap();
let rho_expected = DensityMatrix::from_pure_state(&s_flipped);
for i in 0..4 {
for j in 0..4 {
let a = rho.entry(i, j);
let b = rho_expected.entry(i, j);
assert_abs_diff_eq!(a.re, b.re, epsilon = TOL);
assert_abs_diff_eq!(a.im, b.im, epsilon = TOL);
}
}
}
#[test]
fn gate_application_matches_pure_state_path() {
let mut s = State::new(3).unwrap();
s.h(0).unwrap();
let mut rho = DensityMatrix::from_pure_state(&State::new(3).unwrap());
rho.h(0).unwrap();
s.cnot(0, 1).unwrap();
rho.apply_cnot(0, 1).unwrap();
s.cnot(1, 2).unwrap();
rho.apply_cnot(1, 2).unwrap();
let p_state = s.probabilities();
let p_rho = rho.probabilities();
for (a, b) in p_state.iter().zip(p_rho.iter()) {
assert_abs_diff_eq!(a, b, epsilon = TOL);
}
assert_abs_diff_eq!(s.expectation_z(), rho.expectation_z(), epsilon = TOL);
}
#[test]
fn from_mixture_of_one_state_matches_pure() {
let mut s = State::new(1).unwrap();
s.h(0).unwrap();
let rho_pure = DensityMatrix::from_pure_state(&s);
let rho_mix = DensityMatrix::from_mixture(&[s], &[1.0]).unwrap();
for i in 0..2 {
for j in 0..2 {
let a = rho_pure.entry(i, j);
let b = rho_mix.entry(i, j);
assert_abs_diff_eq!(a.re, b.re, epsilon = TOL);
assert_abs_diff_eq!(a.im, b.im, epsilon = TOL);
}
}
}
#[test]
fn from_mixture_error_paths() {
let s0 = State::new(1).unwrap();
let mut s1 = State::new(1).unwrap();
s1.x(0).unwrap();
assert_eq!(
DensityMatrix::from_mixture(&[], &[]),
Err(StateError::EmptyMixture)
);
assert_eq!(
DensityMatrix::from_mixture(std::slice::from_ref(&s0), &[0.5, 0.5]),
Err(StateError::MixtureSizeMismatch {
states: 1,
probs: 2
})
);
assert_eq!(
DensityMatrix::from_mixture(&[s0.clone(), s1.clone()], &[0.5, 0.6]),
Err(StateError::ProbabilitiesNotNormalized(1.1))
);
assert_eq!(
DensityMatrix::from_mixture(&[s0, s1], &[-0.5, 1.5]),
Err(StateError::NegativeProbability)
);
}
#[test]
fn maximally_mixed_two_state_average() {
let s0 = State::new(1).unwrap(); let mut s1 = State::new(1).unwrap();
s1.x(0).unwrap(); let rho = DensityMatrix::from_mixture(&[s0, s1], &[0.5, 0.5]).unwrap();
assert_abs_diff_eq!(rho.entry(0, 0).re, 0.5, epsilon = TOL);
assert_abs_diff_eq!(rho.entry(1, 1).re, 0.5, epsilon = TOL);
assert_abs_diff_eq!(rho.entry(0, 1).norm(), 0.0, epsilon = TOL);
assert_abs_diff_eq!(rho.trace().re, 1.0, epsilon = TOL);
}
#[test]
fn invalid_qubit_and_probability_errors() {
let s = State::new(2).unwrap();
let mut rho = DensityMatrix::from_pure_state(&s);
assert_eq!(rho.h(5), Err(StateError::InvalidQubit { qubit: 5, n: 2 }));
assert_eq!(rho.apply_cnot(1, 1), Err(StateError::SameQubits(1)));
assert_eq!(
DensityMatrix::bit_flip_kraus(2, 0, 1.5),
Err(StateError::InvalidProbability(1.5))
);
assert_eq!(
DensityMatrix::depolarizing_kraus(2, 5, 0.1),
Err(StateError::InvalidQubit { qubit: 5, n: 2 })
);
}
#[test]
fn matrix_from_row_major_rejects_wrong_length() {
let data = vec![Complex::new(1.0, 0.0); 3];
assert!(Matrix::from_row_major(2, data).is_err());
}
}