use super::{FermionHamiltonian, FermionLindbladNoiseOperator};
use crate::mappings::JordanWignerFermionToSpin;
use crate::spins::PauliLindbladOpenSystem;
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: FermionHamiltonian,
noise: FermionLindbladNoiseOperator,
}
impl crate::SerializationSupport for FermionLindbladOpenSystem {
fn struqture_type() -> crate::StruqtureType {
crate::StruqtureType::FermionLindbladOpenSystem
}
}
impl OpenSystem<'_> for FermionLindbladOpenSystem {
type System = FermionHamiltonian;
type Noise = FermionLindbladNoiseOperator;
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> {
Ok(Self { system, noise })
}
fn empty_clone(&self) -> Self {
Self::group(self.system.empty_clone(None), self.noise.empty_clone(None))
.expect("Internal error.")
}
}
impl OperateOnModes<'_> for FermionLindbladOpenSystem {
fn current_number_modes(&self) -> usize {
self.system
.current_number_modes()
.max(self.noise.current_number_modes())
}
}
impl FermionLindbladOpenSystem {
pub fn new() -> Self {
FermionLindbladOpenSystem {
system: FermionHamiltonian::new(),
noise: FermionLindbladNoiseOperator::new(),
}
}
#[cfg(feature = "struqture_1_export")]
pub fn to_struqture_1(
&self,
) -> Result<struqture_1::fermions::FermionLindbladOpenSystem, StruqtureError> {
let new_system = self.system().to_struqture_1()?;
let new_noise = self.noise().to_struqture_1()?;
struqture_1::OpenSystem::group(new_system, new_noise).map_err(
|err| StruqtureError::GenericError { msg:
format!("Could not convert struqture 2.x FermionLindbladOpenSystem to 1.x FermionLindbladOpenSystem, group function failed: {err:?}.")
}
)
}
#[cfg(feature = "struqture_1_import")]
pub fn from_struqture_1(
value: &struqture_1::fermions::FermionLindbladOpenSystem,
) -> Result<Self, StruqtureError> {
let (system_one, noise_one) = struqture_1::OpenSystem::ungroup(value.clone());
let new_system = FermionHamiltonian::from_struqture_1(&system_one)?;
let new_noise = FermionLindbladNoiseOperator::from_struqture_1(&noise_one)?;
Self::group(new_system, new_noise)
}
}
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 = "FermionLindbladOpenSystem{\n".to_string();
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 = PauliLindbladOpenSystem;
fn jordan_wigner(&self) -> Self::Output {
let jw_system = self.system().jordan_wigner();
let jw_noise = self.noise().jordan_wigner();
PauliLindbladOpenSystem::group(jw_system, jw_noise)
.expect("Internal bug in jordan_wigner() for FermionHamiltonian or FermionLindbladNoiseOperator.")
}
}