use super::{BosonProduct, OperateOnBosons};
use crate::{
ModeIndex, OperateOnDensityMatrix, OperateOnModes, StruqtureError,
StruqtureVersionSerializable, MINIMUM_STRUQTURE_VERSION,
};
use qoqo_calculator::{CalculatorComplex, CalculatorFloat};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Write};
use std::iter::{FromIterator, IntoIterator};
use std::ops;
#[cfg(feature = "indexed_map_iterators")]
use indexmap::map::{Entry, Iter, Keys, Values};
#[cfg(feature = "indexed_map_iterators")]
use indexmap::IndexMap;
#[cfg(not(feature = "indexed_map_iterators"))]
use std::collections::hash_map::{Entry, Iter, Keys, Values};
#[cfg(not(feature = "indexed_map_iterators"))]
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(from = "BosonLindbladNoiseOperatorSerialize")]
#[serde(into = "BosonLindbladNoiseOperatorSerialize")]
pub struct BosonLindbladNoiseOperator {
#[cfg(feature = "indexed_map_iterators")]
internal_map: IndexMap<(BosonProduct, BosonProduct), CalculatorComplex>,
#[cfg(not(feature = "indexed_map_iterators"))]
internal_map: HashMap<(BosonProduct, BosonProduct), CalculatorComplex>,
}
impl crate::MinSupportedVersion for BosonLindbladNoiseOperator {}
#[cfg(feature = "json_schema")]
impl schemars::JsonSchema for BosonLindbladNoiseOperator {
fn schema_name() -> std::borrow::Cow<'static, str> {
"BosonLindbladNoiseOperator".into()
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
<BosonLindbladNoiseOperatorSerialize>::json_schema(generator)
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "json_schema", schemars(deny_unknown_fields))]
struct BosonLindbladNoiseOperatorSerialize {
items: Vec<(BosonProduct, BosonProduct, CalculatorFloat, CalculatorFloat)>,
_struqture_version: StruqtureVersionSerializable,
}
impl From<BosonLindbladNoiseOperatorSerialize> for BosonLindbladNoiseOperator {
fn from(value: BosonLindbladNoiseOperatorSerialize) -> Self {
let new_noise_op: BosonLindbladNoiseOperator = value
.items
.into_iter()
.map(|(left, right, real, imag)| {
((left, right), CalculatorComplex { re: real, im: imag })
})
.collect();
new_noise_op
}
}
impl From<BosonLindbladNoiseOperator> for BosonLindbladNoiseOperatorSerialize {
fn from(value: BosonLindbladNoiseOperator) -> Self {
let new_noise_op: Vec<(BosonProduct, BosonProduct, CalculatorFloat, CalculatorFloat)> =
value
.into_iter()
.map(|((left, right), val)| (left, right, val.re, val.im))
.collect();
let current_version = StruqtureVersionSerializable {
major_version: MINIMUM_STRUQTURE_VERSION.0,
minor_version: MINIMUM_STRUQTURE_VERSION.1,
};
Self {
items: new_noise_op,
_struqture_version: current_version,
}
}
}
impl<'a> OperateOnDensityMatrix<'a> for BosonLindbladNoiseOperator {
type Index = (BosonProduct, BosonProduct);
type Value = CalculatorComplex;
type IteratorType = Iter<'a, (BosonProduct, BosonProduct), CalculatorComplex>;
type KeyIteratorType = Keys<'a, (BosonProduct, BosonProduct), CalculatorComplex>;
type ValueIteratorType = Values<'a, (BosonProduct, BosonProduct), CalculatorComplex>;
fn get(&self, key: &Self::Index) -> &Self::Value {
match self.internal_map.get(key) {
Some(value) => value,
None => &CalculatorComplex::ZERO,
}
}
fn iter(&'a self) -> Self::IteratorType {
self.internal_map.iter()
}
fn keys(&'a self) -> Self::KeyIteratorType {
self.internal_map.keys()
}
fn values(&'a self) -> Self::ValueIteratorType {
self.internal_map.values()
}
#[cfg(feature = "indexed_map_iterators")]
fn remove(&mut self, key: &Self::Index) -> Option<Self::Value> {
self.internal_map.shift_remove(key)
}
#[cfg(not(feature = "indexed_map_iterators"))]
fn remove(&mut self, key: &Self::Index) -> Option<Self::Value> {
self.internal_map.remove(key)
}
fn empty_clone(&self, capacity: Option<usize>) -> Self {
match capacity {
Some(cap) => Self::with_capacity(cap),
None => Self::new(),
}
}
fn set(
&mut self,
key: Self::Index,
value: Self::Value,
) -> Result<Option<Self::Value>, StruqtureError> {
if key.0 == BosonProduct::new([], [])? || key.1 == BosonProduct::new([], [])? {
return Err(StruqtureError::InvalidLindbladTerms);
}
if value != CalculatorComplex::ZERO {
Ok(self.internal_map.insert(key, value))
} else {
match self.internal_map.entry(key) {
#[cfg(feature = "indexed_map_iterators")]
Entry::Occupied(val) => Ok(Some(val.shift_remove())),
#[cfg(not(feature = "indexed_map_iterators"))]
Entry::Occupied(val) => Ok(Some(val.remove())),
Entry::Vacant(_) => Ok(None),
}
}
}
}
impl<'a> OperateOnModes<'a> for BosonLindbladNoiseOperator {
fn current_number_modes(&'a self) -> usize {
let mut max_mode: usize = 0;
if !self.is_empty() {
for key in self.keys() {
let maxk = key
.0
.current_number_modes()
.max(key.1.current_number_modes());
if maxk > max_mode {
max_mode = maxk;
}
}
}
max_mode
}
fn number_modes(&'a self) -> usize {
self.current_number_modes()
}
}
impl OperateOnBosons<'_> for BosonLindbladNoiseOperator {}
impl Default for BosonLindbladNoiseOperator {
fn default() -> Self {
Self::new()
}
}
impl BosonLindbladNoiseOperator {
pub fn new() -> Self {
BosonLindbladNoiseOperator {
#[cfg(not(feature = "indexed_map_iterators"))]
internal_map: HashMap::new(),
#[cfg(feature = "indexed_map_iterators")]
internal_map: IndexMap::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
BosonLindbladNoiseOperator {
#[cfg(not(feature = "indexed_map_iterators"))]
internal_map: HashMap::with_capacity(capacity),
#[cfg(feature = "indexed_map_iterators")]
internal_map: IndexMap::with_capacity(capacity),
}
}
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 BosonLindbladNoiseOperator {
type Output = BosonLindbladNoiseOperator;
fn neg(self) -> Self {
#[cfg(not(feature = "indexed_map_iterators"))]
let mut internal = HashMap::with_capacity(self.len());
#[cfg(feature = "indexed_map_iterators")]
let mut internal = IndexMap::with_capacity(self.len());
for (key, val) in self {
internal.insert(key.clone(), val.neg());
}
BosonLindbladNoiseOperator {
internal_map: internal,
}
}
}
impl<T, V> ops::Add<T> for BosonLindbladNoiseOperator
where
T: IntoIterator<Item = ((BosonProduct, BosonProduct), V)>,
V: Into<CalculatorComplex>,
{
type Output = Self;
fn add(mut self, other: T) -> Self {
for (key, value) in other.into_iter() {
self.add_operator_product(key.clone(), Into::<CalculatorComplex>::into(value))
.expect("Internal bug in add_operator_product");
}
self
}
}
impl<T, V> ops::Sub<T> for BosonLindbladNoiseOperator
where
T: IntoIterator<Item = ((BosonProduct, BosonProduct), V)>,
V: Into<CalculatorComplex>,
{
type Output = Self;
fn sub(mut self, other: T) -> Self {
for (key, value) in other.into_iter() {
self.add_operator_product(key.clone(), Into::<CalculatorComplex>::into(value) * -1.0)
.expect("Internal bug in add_operator_product");
}
self
}
}
impl<T> ops::Mul<T> for BosonLindbladNoiseOperator
where
T: Into<CalculatorComplex>,
{
type Output = Self;
fn mul(self, other: T) -> Self {
let other_cc = Into::<CalculatorComplex>::into(other);
#[cfg(not(feature = "indexed_map_iterators"))]
let mut internal = HashMap::with_capacity(self.len());
#[cfg(feature = "indexed_map_iterators")]
let mut internal = IndexMap::with_capacity(self.len());
for (key, val) in self {
internal.insert(key, val * other_cc.clone());
}
BosonLindbladNoiseOperator {
internal_map: internal,
}
}
}
impl IntoIterator for BosonLindbladNoiseOperator {
type Item = ((BosonProduct, BosonProduct), CalculatorComplex);
#[cfg(not(feature = "indexed_map_iterators"))]
type IntoIter =
std::collections::hash_map::IntoIter<(BosonProduct, BosonProduct), CalculatorComplex>;
#[cfg(feature = "indexed_map_iterators")]
type IntoIter = indexmap::map::IntoIter<(BosonProduct, BosonProduct), CalculatorComplex>;
fn into_iter(self) -> Self::IntoIter {
self.internal_map.into_iter()
}
}
impl<'a> IntoIterator for &'a BosonLindbladNoiseOperator {
type Item = (&'a (BosonProduct, BosonProduct), &'a CalculatorComplex);
type IntoIter = Iter<'a, (BosonProduct, BosonProduct), CalculatorComplex>;
fn into_iter(self) -> Self::IntoIter {
self.internal_map.iter()
}
}
impl FromIterator<((BosonProduct, BosonProduct), CalculatorComplex)>
for BosonLindbladNoiseOperator
{
fn from_iter<I: IntoIterator<Item = ((BosonProduct, BosonProduct), CalculatorComplex)>>(
iter: I,
) -> Self {
let mut slno = BosonLindbladNoiseOperator::new();
for (pair, cc) in iter {
slno.add_operator_product(pair, cc)
.expect("Internal bug in add_operator_product");
}
slno
}
}
impl Extend<((BosonProduct, BosonProduct), CalculatorComplex)> for BosonLindbladNoiseOperator {
fn extend<I: IntoIterator<Item = ((BosonProduct, BosonProduct), CalculatorComplex)>>(
&mut self,
iter: I,
) {
for (pair, cc) in iter {
self.add_operator_product(pair, cc)
.expect("Internal bug in add_operator_product");
}
}
}
impl fmt::Display for BosonLindbladNoiseOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = "BosonLindbladNoiseOperator{\n".to_string();
for (key, val) in self.iter() {
writeln!(output, "({}, {}): {},", key.0, key.1, val)?;
}
output.push('}');
write!(f, "{output}")
}
}
#[cfg(test)]
mod test {
use super::*;
use serde_test::{assert_tokens, Configure, Token};
#[test]
fn so_from_sos() {
let pp: BosonProduct = BosonProduct::new([0], [0]).unwrap();
let sos = BosonLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp.clone(), 0.5.into(), 0.0.into())],
_struqture_version: StruqtureVersionSerializable {
major_version: 1,
minor_version: 0,
},
};
let mut so = BosonLindbladNoiseOperator::new();
so.set((pp.clone(), pp), CalculatorComplex::from(0.5))
.unwrap();
assert_eq!(BosonLindbladNoiseOperator::from(sos.clone()), so);
assert_eq!(BosonLindbladNoiseOperatorSerialize::from(so), sos);
}
#[test]
fn clone_partial_eq() {
let pp: BosonProduct = BosonProduct::new([0], [0]).unwrap();
let sos = BosonLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
_struqture_version: StruqtureVersionSerializable {
major_version: 1,
minor_version: 0,
},
};
assert_eq!(sos.clone(), sos);
let pp_1: BosonProduct = BosonProduct::new([0], [0]).unwrap();
let sos_1 = BosonLindbladNoiseOperatorSerialize {
items: vec![(pp_1.clone(), pp_1, 0.5.into(), 0.0.into())],
_struqture_version: StruqtureVersionSerializable {
major_version: 1,
minor_version: 0,
},
};
let pp_2: BosonProduct = BosonProduct::new([0], [1]).unwrap();
let sos_2 = BosonLindbladNoiseOperatorSerialize {
items: vec![(pp_2.clone(), pp_2, 0.5.into(), 0.0.into())],
_struqture_version: StruqtureVersionSerializable {
major_version: 1,
minor_version: 0,
},
};
assert!(sos_1 == sos);
assert!(sos == sos_1);
assert!(sos_2 != sos);
assert!(sos != sos_2);
}
#[test]
fn debug() {
let pp: BosonProduct = BosonProduct::new([0], [0]).unwrap();
let sos = BosonLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
_struqture_version: StruqtureVersionSerializable {
major_version: 1,
minor_version: 0,
},
};
assert_eq!(
format!("{sos:?}"),
"BosonLindbladNoiseOperatorSerialize { items: [(BosonProduct { creators: [0], annihilators: [0] }, BosonProduct { creators: [0], annihilators: [0] }, Float(0.5), Float(0.0))], _struqture_version: StruqtureVersionSerializable { major_version: 1, minor_version: 0 } }"
);
}
#[test]
fn serde_readable() {
let pp: BosonProduct = BosonProduct::new([0], [0]).unwrap();
let sos = BosonLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
_struqture_version: StruqtureVersionSerializable {
major_version: 1,
minor_version: 0,
},
};
assert_tokens(
&sos.readable(),
&[
Token::Struct {
name: "BosonLindbladNoiseOperatorSerialize",
len: 2,
},
Token::Str("items"),
Token::Seq { len: Some(1) },
Token::Tuple { len: 4 },
Token::Str("c0a0"),
Token::Str("c0a0"),
Token::F64(0.5),
Token::F64(0.0),
Token::TupleEnd,
Token::SeqEnd,
Token::Str("_struqture_version"),
Token::Struct {
name: "StruqtureVersionSerializable",
len: 2,
},
Token::Str("major_version"),
Token::U32(1),
Token::Str("minor_version"),
Token::U32(0),
Token::StructEnd,
Token::StructEnd,
],
);
}
#[test]
fn serde_compact() {
let pp: BosonProduct = BosonProduct::new([0], [0]).unwrap();
let sos = BosonLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
_struqture_version: StruqtureVersionSerializable {
major_version: 1,
minor_version: 0,
},
};
assert_tokens(
&sos.compact(),
&[
Token::Struct {
name: "BosonLindbladNoiseOperatorSerialize",
len: 2,
},
Token::Str("items"),
Token::Seq { len: Some(1) },
Token::Tuple { len: 4 },
Token::Tuple { len: 2 },
Token::Seq { len: Some(1) },
Token::U64(0),
Token::SeqEnd,
Token::Seq { len: Some(1) },
Token::U64(0),
Token::SeqEnd,
Token::TupleEnd,
Token::Tuple { len: 2 },
Token::Seq { len: Some(1) },
Token::U64(0),
Token::SeqEnd,
Token::Seq { len: Some(1) },
Token::U64(0),
Token::SeqEnd,
Token::TupleEnd,
Token::NewtypeVariant {
name: "CalculatorFloat",
variant: "Float",
},
Token::F64(0.5),
Token::NewtypeVariant {
name: "CalculatorFloat",
variant: "Float",
},
Token::F64(0.0),
Token::TupleEnd,
Token::SeqEnd,
Token::Str("_struqture_version"),
Token::Struct {
name: "StruqtureVersionSerializable",
len: 2,
},
Token::Str("major_version"),
Token::U32(1),
Token::Str("minor_version"),
Token::U32(0),
Token::StructEnd,
Token::StructEnd,
],
);
}
}