use super::{FermionLindbladNoiseOperator, OperateOnFermions};
use crate::mappings::JordanWignerFermionToSpin;
use crate::spins::SpinLindbladNoiseSystem;
use crate::{ModeIndex, OperateOnDensityMatrix, OperateOnModes, StruqtureError};
use qoqo_calculator::CalculatorComplex;
use serde::{Deserialize, Serialize};
use std::iter::{FromIterator, IntoIterator};
use std::{
fmt::{self, Write},
ops,
};
#[cfg(feature = "indexed_map_iterators")]
use indexmap::map::{Iter, Keys, Values};
#[cfg(not(feature = "indexed_map_iterators"))]
use std::collections::hash_map::{Iter, Keys, Values};
use super::FermionProduct;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "json_schema", schemars(deny_unknown_fields))]
pub struct FermionLindbladNoiseSystem {
pub(crate) number_modes: Option<usize>,
pub(crate) operator: FermionLindbladNoiseOperator,
}
impl crate::MinSupportedVersion for FermionLindbladNoiseSystem {}
impl<'a> OperateOnDensityMatrix<'a> for FermionLindbladNoiseSystem {
type Value = CalculatorComplex;
type Index = (FermionProduct, FermionProduct);
type IteratorType = Iter<'a, Self::Index, Self::Value>;
type KeyIteratorType = Keys<'a, Self::Index, Self::Value>;
type ValueIteratorType = Values<'a, Self::Index, Self::Value>;
fn get(&self, key: &Self::Index) -> &Self::Value {
self.operator.get(key)
}
fn iter(&'a self) -> Self::IteratorType {
self.operator.iter()
}
fn keys(&'a self) -> Self::KeyIteratorType {
self.operator.keys()
}
fn values(&'a self) -> Self::ValueIteratorType {
self.operator.values()
}
fn remove(&mut self, key: &Self::Index) -> Option<Self::Value> {
self.operator.remove(key)
}
fn empty_clone(&self, capacity: Option<usize>) -> Self {
match capacity {
Some(cap) => Self {
number_modes: self.number_modes,
operator: FermionLindbladNoiseOperator::with_capacity(cap),
},
None => Self {
number_modes: self.number_modes,
operator: FermionLindbladNoiseOperator::new(),
},
}
}
fn set(
&mut self,
key: Self::Index,
value: Self::Value,
) -> Result<Option<Self::Value>, StruqtureError> {
match self.number_modes {
Some(x) => {
if key.0.current_number_modes() <= x && key.1.current_number_modes() <= x {
self.operator.set(key, value)
} else {
Err(StruqtureError::NumberModesExceeded)
}
}
None => self.operator.set(key, value),
}
}
fn add_operator_product(
&mut self,
key: Self::Index,
value: Self::Value,
) -> Result<(), StruqtureError> {
match self.number_modes {
Some(x) => {
if key.0.current_number_modes() <= x && key.1.current_number_modes() <= x {
self.operator.add_operator_product(key, value)
} else {
Err(StruqtureError::NumberModesExceeded)
}
}
None => self.operator.add_operator_product(key, value),
}
}
}
impl<'a> OperateOnModes<'a> for FermionLindbladNoiseSystem {
fn current_number_modes(&'a self) -> usize {
match self.number_modes {
Some(modes) => modes,
None => self.operator.current_number_modes(),
}
}
fn number_modes(&'a self) -> usize {
match self.number_modes {
Some(modes) => modes,
None => self.operator.current_number_modes(),
}
}
}
impl OperateOnFermions<'_> for FermionLindbladNoiseSystem {}
impl FermionLindbladNoiseSystem {
pub fn new(number_modes: Option<usize>) -> Self {
FermionLindbladNoiseSystem {
number_modes,
operator: FermionLindbladNoiseOperator::new(),
}
}
pub fn with_capacity(number_modes: Option<usize>, capacity: usize) -> Self {
FermionLindbladNoiseSystem {
number_modes,
operator: FermionLindbladNoiseOperator::with_capacity(capacity),
}
}
pub fn operator(&self) -> &FermionLindbladNoiseOperator {
&self.operator
}
pub fn from_operator(
operator: FermionLindbladNoiseOperator,
number_modes: Option<usize>,
) -> Result<Self, StruqtureError> {
match number_modes {
Some(x) => {
if operator.current_number_modes() <= x {
Ok(FermionLindbladNoiseSystem {
number_modes: Some(x),
operator,
})
} else {
Err(StruqtureError::NumberModesExceeded)
}
}
None => Ok(FermionLindbladNoiseSystem {
number_modes: None,
operator,
}),
}
}
pub fn separate_into_n_terms(
&self,
number_creators_annihilators_left: (usize, usize),
number_creators_annihilators_right: (usize, usize),
) -> Result<(Self, Self), StruqtureError> {
let mut separated = Self::default();
let mut remainder = Self::default();
for ((prod_l, prod_r), val) in self.iter() {
if (prod_l.creators().len(), prod_l.annihilators().len())
== number_creators_annihilators_left
&& (prod_r.creators().len(), prod_r.annihilators().len())
== number_creators_annihilators_right
{
separated.add_operator_product((prod_l.clone(), prod_r.clone()), val.clone())?;
} else {
remainder.add_operator_product((prod_l.clone(), prod_r.clone()), val.clone())?;
}
}
Ok((separated, remainder))
}
}
impl ops::Neg for FermionLindbladNoiseSystem {
type Output = Self;
fn neg(mut self) -> Self {
self.operator = self.operator.neg();
self
}
}
impl<T, V> ops::Add<T> for FermionLindbladNoiseSystem
where
T: IntoIterator<Item = ((FermionProduct, FermionProduct), V)>,
V: Into<CalculatorComplex>,
{
type Output = Result<Self, StruqtureError>;
fn add(mut self, other: T) -> Self::Output {
for (key, value) in other.into_iter() {
self.add_operator_product(key.clone(), Into::<CalculatorComplex>::into(value))?;
}
Ok(self)
}
}
impl<T, V> ops::Sub<T> for FermionLindbladNoiseSystem
where
T: IntoIterator<Item = ((FermionProduct, FermionProduct), V)>,
V: Into<CalculatorComplex>,
{
type Output = Result<Self, StruqtureError>;
fn sub(mut self, other: T) -> Self::Output {
for (key, value) in other.into_iter() {
self.add_operator_product(key.clone(), Into::<CalculatorComplex>::into(value) * -1.0)?;
}
Ok(self)
}
}
impl<T> ops::Mul<T> for FermionLindbladNoiseSystem
where
T: Into<CalculatorComplex>,
{
type Output = Self;
fn mul(mut self, other: T) -> Self {
self.operator = self.operator * other;
self
}
}
impl IntoIterator for FermionLindbladNoiseSystem {
type Item = ((FermionProduct, FermionProduct), CalculatorComplex);
#[cfg(not(feature = "indexed_map_iterators"))]
type IntoIter =
std::collections::hash_map::IntoIter<(FermionProduct, FermionProduct), CalculatorComplex>;
#[cfg(feature = "indexed_map_iterators")]
type IntoIter = indexmap::map::IntoIter<(FermionProduct, FermionProduct), CalculatorComplex>;
fn into_iter(self) -> Self::IntoIter {
self.operator.into_iter()
}
}
impl<'a> IntoIterator for &'a FermionLindbladNoiseSystem {
type Item = (&'a (FermionProduct, FermionProduct), &'a CalculatorComplex);
type IntoIter = Iter<'a, (FermionProduct, FermionProduct), CalculatorComplex>;
fn into_iter(self) -> Self::IntoIter {
self.operator.iter()
}
}
impl FromIterator<((FermionProduct, FermionProduct), CalculatorComplex)>
for FermionLindbladNoiseSystem
{
fn from_iter<I: IntoIterator<Item = ((FermionProduct, FermionProduct), CalculatorComplex)>>(
iter: I,
) -> Self {
let mut so = FermionLindbladNoiseSystem::new(None);
for (pp, cc) in iter {
so.add_operator_product(pp.clone(), cc.clone())
.expect("Internal error in add_operator_product");
}
so
}
}
impl Extend<((FermionProduct, FermionProduct), CalculatorComplex)> for FermionLindbladNoiseSystem {
fn extend<I: IntoIterator<Item = ((FermionProduct, FermionProduct), CalculatorComplex)>>(
&mut self,
iter: I,
) {
for (pp, cc) in iter {
self.add_operator_product(pp, cc)
.expect("Internal error in add_operator_product");
}
}
}
impl fmt::Display for FermionLindbladNoiseSystem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = format!("FermionLindbladNoiseSystem({}){{\n", self.number_modes());
for (key, val) in self.iter() {
writeln!(output, "({}, {}): {},", key.0, key.1, val)?;
}
output.push('}');
write!(f, "{output}")
}
}
impl JordanWignerFermionToSpin for FermionLindbladNoiseSystem {
type Output = SpinLindbladNoiseSystem;
fn jordan_wigner(&self) -> Self::Output {
SpinLindbladNoiseSystem::from_operator(
self.operator().jordan_wigner(),
self.number_modes,
)
.expect("Internal bug in jordan_wigner for FermionLindbladNoiseOperator. The number of spins in the resulting SpinLindbladNoiseOperator should equal the number of modes of the FermionLindbladNoiseOperator.")
}
}