use super::{FermionHamiltonianSystem, FermionLindbladNoiseSystem};
use crate::mappings::JordanWignerFermionToSpin;
use crate::spins::SpinLindbladOpenSystem;
use crate::{OpenSystem, OperateOnDensityMatrix, OperateOnModes, StruqtureError};
use qoqo_calculator::CalculatorFloat;
use serde::{Deserialize, Serialize};
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 FermionLindbladOpenSystem {
system: FermionHamiltonianSystem,
noise: FermionLindbladNoiseSystem,
}
impl crate::MinSupportedVersion for FermionLindbladOpenSystem {}
impl OpenSystem<'_> for FermionLindbladOpenSystem {
type System = FermionHamiltonianSystem;
type Noise = FermionLindbladNoiseSystem;
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_modes != noise.number_modes {
match (system.number_modes, noise.number_modes) {
(Some(n), None) => {
if n >= noise.number_modes() {
let mut noise = noise;
noise.number_modes = Some(n);
(system, noise)
} else {
return Err(StruqtureError::MissmatchedNumberModes);
}
}
(None, Some(n)) => {
if n >= system.number_modes() {
let mut system = system;
system.number_modes = Some(n);
(system, noise)
} else {
return Err(StruqtureError::MissmatchedNumberModes);
}
}
(Some(_), Some(_)) => {
return Err(StruqtureError::MissmatchedNumberModes);
}
_ => panic!("Unexpected missmatch of number modes"),
}
} 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 modes in system and noise unexpectedly does not match.",
)
}
}
impl OperateOnModes<'_> for FermionLindbladOpenSystem {
fn number_modes(&self) -> usize {
self.system.number_modes().max(self.noise.number_modes())
}
fn current_number_modes(&self) -> usize {
self.system
.current_number_modes()
.max(self.noise.current_number_modes())
}
}
impl FermionLindbladOpenSystem {
pub fn new(number_modes: Option<usize>) -> Self {
FermionLindbladOpenSystem {
system: FermionHamiltonianSystem::new(number_modes),
noise: FermionLindbladNoiseSystem::new(number_modes),
}
}
}
impl ops::Neg for FermionLindbladOpenSystem {
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<FermionLindbladOpenSystem> for FermionLindbladOpenSystem {
type Output = Result<Self, StruqtureError>;
fn add(self, other: FermionLindbladOpenSystem) -> 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<FermionLindbladOpenSystem> for FermionLindbladOpenSystem {
type Output = Result<Self, StruqtureError>;
fn sub(self, other: FermionLindbladOpenSystem) -> 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 FermionLindbladOpenSystem {
type Output = Self;
fn mul(self, rhs: CalculatorFloat) -> Self::Output {
Self {
system: self.system * rhs.clone(),
noise: self.noise * rhs,
}
}
}
impl fmt::Display for FermionLindbladOpenSystem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = format!("FermionLindbladOpenSystem({}){{\n", self.number_modes());
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 JordanWignerFermionToSpin for FermionLindbladOpenSystem {
type Output = SpinLindbladOpenSystem;
fn jordan_wigner(&self) -> Self::Output {
let jw_system = self.system().jordan_wigner();
let jw_noise = self.noise().jordan_wigner();
SpinLindbladOpenSystem::group(jw_system, jw_noise)
.expect("Internal bug in jordan_wigner() for FermionHamiltonianSystem or FermionLindbladNoiseSystem. The number of modes in the fermionic system should equal the number of spins in the spin system.")
}
}