use super::SpinLindbladNoiseSystem;
use crate::fermions::FermionLindbladOpenSystem;
use crate::mappings::JordanWignerSpinToFermion;
use crate::spins::{OperateOnSpins, SpinHamiltonianSystem, ToSparseMatrixSuperOperator};
use crate::{CooSparseMatrix, OpenSystem, OperateOnDensityMatrix, StruqtureError};
use num_complex::Complex64;
use qoqo_calculator::CalculatorFloat;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{self, Write};
use std::ops;
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "json_schema", schemars(deny_unknown_fields))]
pub struct SpinLindbladOpenSystem {
system: SpinHamiltonianSystem,
noise: SpinLindbladNoiseSystem,
}
impl crate::MinSupportedVersion for SpinLindbladOpenSystem {}
impl OpenSystem<'_> for SpinLindbladOpenSystem {
type System = SpinHamiltonianSystem;
type Noise = SpinLindbladNoiseSystem;
fn noise(&self) -> &Self::Noise {
&self.noise
}
fn system(&self) -> &Self::System {
&self.system
}
fn noise_mut(&mut self) -> &mut Self::Noise {
&mut self.noise
}
fn system_mut(&mut self) -> &mut Self::System {
&mut self.system
}
fn ungroup(self) -> (Self::System, Self::Noise) {
(self.system, self.noise)
}
fn group(system: Self::System, noise: Self::Noise) -> Result<Self, StruqtureError> {
let (system, noise) = if system.number_spins != noise.number_spins {
match (system.number_spins, noise.number_spins) {
(Some(n), None) => {
if n >= noise.number_spins() {
let mut noise = noise;
noise.number_spins = Some(n);
(system, noise)
} else {
return Err(StruqtureError::MissmatchedNumberSpins);
}
}
(None, Some(n)) => {
if n >= system.number_spins() {
let mut system = system;
system.number_spins = Some(n);
(system, noise)
} else {
return Err(StruqtureError::MissmatchedNumberSpins);
}
}
(Some(_), Some(_)) => {
return Err(StruqtureError::MissmatchedNumberSpins);
}
_ => panic!("Unexpected missmatch of number spins"),
}
} else {
(system, noise)
};
Ok(Self { system, noise })
}
fn empty_clone(&self) -> Self {
Self::group(self.system.empty_clone(None), self.noise.empty_clone(None)).expect(
"Internal error: Number of spins in system and noise unexpectedly does not match.",
)
}
}
impl OperateOnSpins<'_> for SpinLindbladOpenSystem {
fn number_spins(&self) -> usize {
self.system.number_spins().max(self.noise.number_spins())
}
fn current_number_spins(&self) -> usize {
self.system
.current_number_spins()
.max(self.noise.current_number_spins())
}
}
impl<'a> ToSparseMatrixSuperOperator<'a> for SpinLindbladOpenSystem {
fn sparse_matrix_superoperator_entries_on_row(
&'a self,
row: usize,
number_spins: usize,
) -> Result<HashMap<usize, Complex64>, StruqtureError> {
let mut system_row = self
.system
.sparse_matrix_superoperator_entries_on_row(row, number_spins)
.unwrap();
let noise_row = self
.noise
.sparse_matrix_superoperator_entries_on_row(row, number_spins)
.unwrap();
for (key, val) in noise_row.into_iter() {
match system_row.get_mut(&key) {
Some(x) => *x += val,
None => {
system_row.insert(key, val);
}
}
}
Ok(system_row)
}
fn unitary_sparse_matrix_coo(&'a self) -> Result<CooSparseMatrix, StruqtureError> {
self.system.unitary_sparse_matrix_coo()
}
fn sparse_lindblad_entries(
&'a self,
) -> Result<Vec<(CooSparseMatrix, CooSparseMatrix, Complex64)>, StruqtureError> {
let mut coo_matrices =
Vec::<(CooSparseMatrix, CooSparseMatrix, Complex64)>::with_capacity(self.noise.len());
for ((left, right), val) in self.noise.iter() {
coo_matrices.push((
left.to_coo(self.number_spins()).unwrap(),
right.to_coo(self.number_spins()).unwrap(),
Complex64 {
re: *val.re.float()?,
im: *val.im.float()?,
},
))
}
Ok(coo_matrices)
}
}
impl SpinLindbladOpenSystem {
pub fn new(number_spins: Option<usize>) -> Self {
SpinLindbladOpenSystem {
system: SpinHamiltonianSystem::new(number_spins),
noise: SpinLindbladNoiseSystem::new(number_spins),
}
}
}
impl ops::Neg for SpinLindbladOpenSystem {
type Output = Self;
fn neg(self) -> Self {
let (self_sys, self_noise) = self.ungroup();
Self {
system: self_sys.neg(),
noise: self_noise.neg(),
}
}
}
impl ops::Add<SpinLindbladOpenSystem> for SpinLindbladOpenSystem {
type Output = Result<Self, StruqtureError>;
fn add(self, other: SpinLindbladOpenSystem) -> Self::Output {
let (self_sys, self_noise) = self.ungroup();
let (other_sys, other_noise) = other.ungroup();
Self::group((self_sys + other_sys)?, (self_noise + other_noise)?)
}
}
impl ops::Sub<SpinLindbladOpenSystem> for SpinLindbladOpenSystem {
type Output = Result<Self, StruqtureError>;
fn sub(self, other: SpinLindbladOpenSystem) -> Self::Output {
let (self_sys, self_noise) = self.ungroup();
let (other_sys, other_noise) = other.ungroup();
Self::group((self_sys - other_sys)?, (self_noise - other_noise)?)
}
}
impl ops::Mul<CalculatorFloat> for SpinLindbladOpenSystem {
type Output = Self;
fn mul(self, rhs: CalculatorFloat) -> Self::Output {
Self {
system: self.system * rhs.clone(),
noise: self.noise * rhs,
}
}
}
impl fmt::Display for SpinLindbladOpenSystem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = format!("SpinLindbladOpenSystem({}){{\n", self.number_spins());
output.push_str("System: {\n");
for (key, val) in self.system.iter() {
writeln!(output, "{key}: {val},")?;
}
output.push_str("}\n");
output.push_str("Noise: {\n");
for ((row, column), val) in self.noise.iter() {
writeln!(output, "({row}, {column}): {val},")?;
}
output.push_str("}\n");
output.push('}');
write!(f, "{output}")
}
}
impl JordanWignerSpinToFermion for SpinLindbladOpenSystem {
type Output = FermionLindbladOpenSystem;
fn jordan_wigner(&self) -> Self::Output {
let jw_system = self.system().jordan_wigner();
let jw_noise = self.noise().jordan_wigner();
FermionLindbladOpenSystem::group(jw_system, jw_noise)
.expect("Internal bug in jordan_wigner() for SpinHamiltonianSystem or SpinLindbladNoiseSystem. The number of modes in the fermionic system should equal the number of spins in the spin system.")
}
}