use super::{OperateOnSpins, SingleDecoherenceOperator, ToSparseMatrixSuperOperator};
use crate::fermions::FermionLindbladNoiseOperator;
use crate::mappings::JordanWignerSpinToFermion;
use crate::spins::{DecoherenceOperator, DecoherenceProduct};
use crate::{OperateOnDensityMatrix, SpinIndex, StruqtureError, SymmetricIndex};
use itertools::Itertools;
use num_complex::Complex64;
use qoqo_calculator::{CalculatorComplex, CalculatorFloat};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Write};
use std::iter::{FromIterator, IntoIterator};
use std::ops;
use indexmap::map::{Entry, Iter};
use indexmap::IndexMap;
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(try_from = "PauliLindbladNoiseOperatorSerialize")]
#[serde(into = "PauliLindbladNoiseOperatorSerialize")]
pub struct PauliLindbladNoiseOperator {
internal_map: IndexMap<(DecoherenceProduct, DecoherenceProduct), CalculatorComplex>,
}
impl crate::SerializationSupport for PauliLindbladNoiseOperator {
fn struqture_type() -> crate::StruqtureType {
crate::StruqtureType::PauliLindbladNoiseOperator
}
}
#[cfg(feature = "json_schema")]
impl schemars::JsonSchema for PauliLindbladNoiseOperator {
fn schema_name() -> std::borrow::Cow<'static, str> {
"PlusMinusOperator".into()
}
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
<PauliLindbladNoiseOperatorSerialize>::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 PauliLindbladNoiseOperatorSerialize {
items: Vec<(
DecoherenceProduct,
DecoherenceProduct,
CalculatorFloat,
CalculatorFloat,
)>,
serialisation_meta: crate::StruqtureSerialisationMeta,
}
impl TryFrom<PauliLindbladNoiseOperatorSerialize> for PauliLindbladNoiseOperator {
type Error = StruqtureError;
fn try_from(value: PauliLindbladNoiseOperatorSerialize) -> Result<Self, Self::Error> {
let target_serialisation_meta =
<Self as crate::SerializationSupport>::target_serialisation_meta();
crate::check_can_be_deserialised(&target_serialisation_meta, &value.serialisation_meta)?;
let new_noise_op: PauliLindbladNoiseOperator = value
.items
.into_iter()
.map(|(left, right, real, imag)| {
((left, right), CalculatorComplex { re: real, im: imag })
})
.collect();
Ok(new_noise_op)
}
}
impl From<PauliLindbladNoiseOperator> for PauliLindbladNoiseOperatorSerialize {
fn from(value: PauliLindbladNoiseOperator) -> Self {
let serialisation_meta = crate::SerializationSupport::struqture_serialisation_meta(&value);
let new_noise_op: Vec<(
DecoherenceProduct,
DecoherenceProduct,
CalculatorFloat,
CalculatorFloat,
)> = value
.into_iter()
.map(|((left, right), val)| (left, right, val.re, val.im))
.collect();
Self {
items: new_noise_op,
serialisation_meta,
}
}
}
impl<'a> OperateOnDensityMatrix<'a> for PauliLindbladNoiseOperator {
type Index = (DecoherenceProduct, DecoherenceProduct);
type Value = 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) -> impl ExactSizeIterator<Item = (&'a Self::Index, &'a Self::Value)> {
self.internal_map.iter()
}
fn keys(&'a self) -> impl ExactSizeIterator<Item = &'a Self::Index> {
self.internal_map.keys()
}
fn values(&'a self) -> impl ExactSizeIterator<Item = &'a Self::Value> {
self.internal_map.values()
}
fn remove(&mut self, key: &Self::Index) -> Option<Self::Value> {
self.internal_map.shift_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.is_empty() || key.1.is_empty() {
return Err(StruqtureError::InvalidLindbladTerms);
}
if value != CalculatorComplex::ZERO {
Ok(self.internal_map.insert(key, value))
} else {
match self.internal_map.entry(key) {
Entry::Occupied(val) => Ok(Some(val.shift_remove())),
Entry::Vacant(_) => Ok(None),
}
}
}
}
impl OperateOnSpins<'_> for PauliLindbladNoiseOperator {
fn current_number_spins(&self) -> usize {
let mut max_mode: usize = 0;
if !self.internal_map.is_empty() {
for key in self.internal_map.keys() {
let maxk = (key.0.current_number_spins()).max(key.1.current_number_spins());
if maxk > max_mode {
max_mode = maxk
}
}
}
max_mode
}
}
impl ToSparseMatrixSuperOperator<'_> for PauliLindbladNoiseOperator {
fn sparse_matrix_superoperator_entries_on_row(
&self,
row: usize,
number_spins: usize,
) -> Result<HashMap<usize, Complex64>, StruqtureError> {
let mut entries: HashMap<usize, Complex64> = HashMap::new();
let dimension = 2_usize.pow(number_spins as u32);
for ((left, right), value) in self.iter() {
add_lindblad_terms(
left,
right,
row,
dimension,
number_spins,
&mut entries,
value,
)?;
add_anti_commutator(
left,
right,
row,
dimension,
number_spins,
&mut entries,
value,
)?;
}
Ok(entries)
}
}
impl Default for PauliLindbladNoiseOperator {
fn default() -> Self {
Self::new()
}
}
impl PauliLindbladNoiseOperator {
pub fn new() -> Self {
PauliLindbladNoiseOperator {
internal_map: IndexMap::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
PauliLindbladNoiseOperator {
internal_map: IndexMap::with_capacity(capacity),
}
}
pub fn add_noise_from_full_operators(
&mut self,
left: &DecoherenceOperator,
right: &DecoherenceOperator,
value: CalculatorComplex,
) -> Result<(), StruqtureError> {
if left.is_empty() || right.is_empty() {
return Err(StruqtureError::InvalidLindbladTerms);
}
for ((decoherence_product_left, value_left), (decoherence_product_right, value_right)) in
left.iter().cartesian_product(right)
{
if !decoherence_product_left.is_empty() && !decoherence_product_right.is_empty() {
let value_complex = value_right.conj() * value_left;
self.add_operator_product(
(
decoherence_product_left.clone(),
decoherence_product_right.clone(),
),
value_complex * value.clone(),
)?;
}
}
Ok(())
}
pub fn remap_qubits(&self, mapping: &HashMap<usize, usize>) -> Self {
let mut new_noise = PauliLindbladNoiseOperator::new();
for ((left, right), rate) in self.iter() {
let new_left = left.remap_qubits(mapping);
let new_right = right.remap_qubits(mapping);
new_noise
.add_operator_product((new_left, new_right), rate.clone())
.expect("Internal bug in add_operator_product");
}
new_noise
}
pub fn separate_into_n_terms(
&self,
number_spins_left: usize,
number_spins_right: 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.iter().len() == number_spins_left && prod_r.iter().len() == number_spins_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))
}
#[cfg(feature = "struqture_1_export")]
pub fn to_struqture_1(
&self,
) -> Result<struqture_1::spins::SpinLindbladNoiseSystem, StruqtureError> {
let mut new_system = struqture_1::spins::SpinLindbladNoiseSystem::new(None);
for (key, val) in self.iter() {
let one_key_left = key.0.to_struqture_1()?;
let one_key_right = key.1.to_struqture_1()?;
let _ = struqture_1::OperateOnDensityMatrix::set(
&mut new_system,
(one_key_left, one_key_right),
val.clone(),
);
}
Ok(new_system)
}
#[cfg(feature = "struqture_1_import")]
pub fn from_struqture_1(
value: &struqture_1::spins::SpinLindbladNoiseSystem,
) -> Result<Self, StruqtureError> {
let mut new_operator = Self::new();
for (key, val) in struqture_1::OperateOnDensityMatrix::iter(value) {
let self_key_left = DecoherenceProduct::from_struqture_1(&key.0)?;
let self_key_right = DecoherenceProduct::from_struqture_1(&key.1)?;
let _ = new_operator.set((self_key_left, self_key_right), val.clone());
}
Ok(new_operator)
}
}
impl ops::Neg for PauliLindbladNoiseOperator {
type Output = PauliLindbladNoiseOperator;
fn neg(self) -> Self {
let mut internal = IndexMap::with_capacity(self.len());
for (key, val) in self {
internal.insert(key.clone(), val.neg());
}
PauliLindbladNoiseOperator {
internal_map: internal,
}
}
}
impl<T, V> ops::Add<T> for PauliLindbladNoiseOperator
where
T: IntoIterator<Item = ((DecoherenceProduct, DecoherenceProduct), 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 PauliLindbladNoiseOperator
where
T: IntoIterator<Item = ((DecoherenceProduct, DecoherenceProduct), 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 PauliLindbladNoiseOperator
where
T: Into<CalculatorComplex>,
{
type Output = Self;
fn mul(self, other: T) -> Self {
let other_cc = Into::<CalculatorComplex>::into(other);
let mut internal = IndexMap::with_capacity(self.len());
for (key, val) in self {
internal.insert(key, val * other_cc.clone());
}
PauliLindbladNoiseOperator {
internal_map: internal,
}
}
}
impl IntoIterator for PauliLindbladNoiseOperator {
type Item = ((DecoherenceProduct, DecoherenceProduct), CalculatorComplex);
type IntoIter =
indexmap::map::IntoIter<(DecoherenceProduct, DecoherenceProduct), CalculatorComplex>;
fn into_iter(self) -> Self::IntoIter {
self.internal_map.into_iter()
}
}
impl<'a> IntoIterator for &'a PauliLindbladNoiseOperator {
type Item = (
&'a (DecoherenceProduct, DecoherenceProduct),
&'a CalculatorComplex,
);
type IntoIter = Iter<'a, (DecoherenceProduct, DecoherenceProduct), CalculatorComplex>;
fn into_iter(self) -> Self::IntoIter {
self.internal_map.iter()
}
}
impl FromIterator<((DecoherenceProduct, DecoherenceProduct), CalculatorComplex)>
for PauliLindbladNoiseOperator
{
fn from_iter<
I: IntoIterator<Item = ((DecoherenceProduct, DecoherenceProduct), CalculatorComplex)>,
>(
iter: I,
) -> Self {
let mut slno = PauliLindbladNoiseOperator::new();
for (pair, cc) in iter {
slno.add_operator_product(pair, cc)
.expect("Internal bug in add_operator_product");
}
slno
}
}
impl Extend<((DecoherenceProduct, DecoherenceProduct), CalculatorComplex)>
for PauliLindbladNoiseOperator
{
fn extend<
I: IntoIterator<Item = ((DecoherenceProduct, DecoherenceProduct), 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 PauliLindbladNoiseOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = "PauliLindbladNoiseOperator{\n".to_string();
for (key, val) in self.iter() {
writeln!(output, "({}, {}): {},", key.0, key.1, val)?;
}
output.push('}');
write!(f, "{output}")
}
}
fn add_anti_commutator(
left: &DecoherenceProduct,
right: &DecoherenceProduct,
row: usize,
dimension: usize,
number_spins: usize,
entries: &mut HashMap<usize, Complex64>,
value: &CalculatorComplex,
) -> Result<(), StruqtureError> {
let constant_prefactor = -0.5;
let (right_conj, conjugate_prefactor) = right.hermitian_conjugate();
let (product, product_prefactor) = DecoherenceProduct::multiply(right_conj, left.clone());
for (row_adjusted, shift, (operator, transpose_prefactor)) in [
(
row.div_euclid(dimension),
number_spins,
(product.clone(), 1.0),
),
(row % dimension, 0, product.hermitian_conjugate()),
] {
let mut column = row;
let mut prefac = Complex64::new(1.0, 0.0);
for (spin_op_index, dec_op) in operator.iter() {
match dec_op {
SingleDecoherenceOperator::X => {
match row_adjusted.div_euclid(2usize.pow(*spin_op_index as u32)) % 2 {
0 => column += 2usize.pow((*spin_op_index + shift) as u32),
1 => column -= 2usize.pow((*spin_op_index + shift) as u32),
_ => panic!("Internal error in constructing matrix"),
}
}
SingleDecoherenceOperator::IY => {
match row_adjusted.div_euclid(2usize.pow(*spin_op_index as u32)) % 2 {
0 => {
column += 2usize.pow((*spin_op_index + shift) as u32);
prefac *= 1.0;
}
1 => {
column -= 2usize.pow((*spin_op_index + shift) as u32);
prefac *= -1.0;
}
_ => panic!("Internal error in constructing matrix"),
};
}
SingleDecoherenceOperator::Z => {
match row_adjusted.div_euclid(2usize.pow(*spin_op_index as u32)) % 2 {
0 => {
prefac *= 1.0;
}
1 => {
prefac *= -1.0;
}
_ => panic!("Internal error in constructing matrix"),
};
}
SingleDecoherenceOperator::Identity => (),
}
}
prefac *=
transpose_prefactor * conjugate_prefactor * product_prefactor * constant_prefactor;
let mut_value = entries.get_mut(&column);
let value = Complex64 {
re: *value.re.float()?,
im: *value.im.float()?,
};
match mut_value {
Some(x) => *x += value * prefac,
None => {
entries.insert(column, value * prefac);
}
}
}
Ok(())
}
fn add_lindblad_terms(
left: &DecoherenceProduct,
right: &DecoherenceProduct,
row: usize,
dimension: usize,
number_spins: usize,
entries: &mut HashMap<usize, Complex64>,
value: &CalculatorComplex,
) -> Result<(), StruqtureError> {
let mut column = row;
let mut prefac = 1.0;
for (index_operator_iter, shift, div_euclid) in
[(left.iter(), number_spins, true), (right.iter(), 0, false)]
{
for (index, operator) in index_operator_iter {
let row_adjusted = if div_euclid {
row.div_euclid(dimension)
} else {
row % dimension
};
match operator {
SingleDecoherenceOperator::X => {
match row_adjusted.div_euclid(2usize.pow(*index as u32)) % 2 {
0 => column += 2usize.pow((*index + shift) as u32),
1 => column -= 2usize.pow((*index + shift) as u32),
_ => panic!("Internal error in constructing matrix"),
}
}
SingleDecoherenceOperator::IY => {
match row_adjusted.div_euclid(2usize.pow(*index as u32)) % 2 {
0 => {
column += 2usize.pow((*index + shift) as u32);
prefac *= 1.0;
}
1 => {
column -= 2usize.pow((*index + shift) as u32);
prefac *= -1.0;
}
_ => panic!("Internal error in constructing matrix"),
};
}
SingleDecoherenceOperator::Z => {
match row_adjusted.div_euclid(2usize.pow(*index as u32)) % 2 {
0 => {
prefac *= 1.0;
}
1 => {
prefac *= -1.0;
}
_ => panic!("Internal error in constructing matrix"),
};
}
SingleDecoherenceOperator::Identity => (),
}
}
}
let mut_value = entries.get_mut(&column);
let value = Complex64 {
re: *value.re.float()?,
im: *value.im.float()?,
};
match mut_value {
Some(x) => *x += value * prefac,
None => {
entries.insert(column, value * prefac);
}
}
Ok(())
}
impl JordanWignerSpinToFermion for PauliLindbladNoiseOperator {
type Output = FermionLindbladNoiseOperator;
fn jordan_wigner(&self) -> Self::Output {
let mut out = FermionLindbladNoiseOperator::new();
for key in self.keys() {
let fermion_operator_left = key.0.jordan_wigner();
let fermion_operator_right = key.1.jordan_wigner();
out.add_noise_from_full_operators(
&fermion_operator_left,
&fermion_operator_right,
self.get(key).into(),
)
.expect("Internal bug in add_noise_from_full_operators");
}
out
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::STRUQTURE_VERSION;
use serde_test::{assert_tokens, Configure, Token};
#[test]
fn so_from_sos() {
let pp: DecoherenceProduct = DecoherenceProduct::new().z(0);
let sos = PauliLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp.clone(), 0.5.into(), 0.0.into())],
serialisation_meta: crate::StruqtureSerialisationMeta {
type_name: "PauliLindbladNoiseOperator".to_string(),
min_version: (2, 0, 0),
version: STRUQTURE_VERSION.to_string(),
},
};
let mut so = PauliLindbladNoiseOperator::new();
so.set((pp.clone(), pp), CalculatorComplex::from(0.5))
.unwrap();
assert_eq!(
PauliLindbladNoiseOperator::try_from(sos.clone()).unwrap(),
so
);
assert_eq!(PauliLindbladNoiseOperatorSerialize::from(so), sos);
}
#[test]
fn clone_partial_eq() {
let pp: DecoherenceProduct = DecoherenceProduct::new().z(0);
let sos = PauliLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
serialisation_meta: crate::StruqtureSerialisationMeta {
type_name: "PauliLindbladNoiseOperator".to_string(),
min_version: (2, 0, 0),
version: "2.0.0".to_string(),
},
};
assert_eq!(sos.clone(), sos);
let pp_1: DecoherenceProduct = DecoherenceProduct::new().z(0);
let sos_1 = PauliLindbladNoiseOperatorSerialize {
items: vec![(pp_1.clone(), pp_1, 0.5.into(), 0.0.into())],
serialisation_meta: crate::StruqtureSerialisationMeta {
type_name: "PauliLindbladNoiseOperator".to_string(),
min_version: (2, 0, 0),
version: "2.0.0".to_string(),
},
};
let pp_2: DecoherenceProduct = DecoherenceProduct::new().z(2);
let sos_2 = PauliLindbladNoiseOperatorSerialize {
items: vec![(pp_2.clone(), pp_2, 0.5.into(), 0.0.into())],
serialisation_meta: crate::StruqtureSerialisationMeta {
type_name: "PauliLindbladNoiseOperator".to_string(),
min_version: (2, 0, 0),
version: "2.0.0".to_string(),
},
};
assert!(sos_1 == sos);
assert!(sos == sos_1);
assert!(sos_2 != sos);
assert!(sos != sos_2);
}
#[test]
fn debug() {
let pp: DecoherenceProduct = DecoherenceProduct::new().z(0);
let sos = PauliLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
serialisation_meta: crate::StruqtureSerialisationMeta {
type_name: "PauliLindbladNoiseOperator".to_string(),
min_version: (2, 0, 0),
version: "2.0.0".to_string(),
},
};
assert_eq!(
format!("{sos:?}"),
"PauliLindbladNoiseOperatorSerialize { items: [(DecoherenceProduct { items: [(0, Z)] }, DecoherenceProduct { items: [(0, Z)] }, Float(0.5), Float(0.0))], serialisation_meta: StruqtureSerialisationMeta { type_name: \"PauliLindbladNoiseOperator\", min_version: (2, 0, 0), version: \"2.0.0\" } }"
);
}
#[test]
fn serde_readable() {
let pp = DecoherenceProduct::new().x(0);
let sos = PauliLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
serialisation_meta: crate::StruqtureSerialisationMeta {
type_name: "PauliLindbladNoiseOperator".to_string(),
min_version: (2, 0, 0),
version: "2.0.0".to_string(),
},
};
assert_tokens(
&sos.readable(),
&[
Token::Struct {
name: "PauliLindbladNoiseOperatorSerialize",
len: 2,
},
Token::Str("items"),
Token::Seq { len: Some(1) },
Token::Tuple { len: 4 },
Token::Str("0X"),
Token::Str("0X"),
Token::F64(0.5),
Token::F64(0.0),
Token::TupleEnd,
Token::SeqEnd,
Token::Str("serialisation_meta"),
Token::Struct {
name: "StruqtureSerialisationMeta",
len: 3,
},
Token::Str("type_name"),
Token::Str("PauliLindbladNoiseOperator"),
Token::Str("min_version"),
Token::Tuple { len: 3 },
Token::U64(2),
Token::U64(0),
Token::U64(0),
Token::TupleEnd,
Token::Str("version"),
Token::Str("2.0.0"),
Token::StructEnd,
Token::StructEnd,
],
);
}
#[test]
fn serde_compact() {
let pp = DecoherenceProduct::new().x(0);
let sos = PauliLindbladNoiseOperatorSerialize {
items: vec![(pp.clone(), pp, 0.5.into(), 0.0.into())],
serialisation_meta: crate::StruqtureSerialisationMeta {
type_name: "PauliLindbladNoiseOperator".to_string(),
min_version: (2, 0, 0),
version: "2.0.0".to_string(),
},
};
assert_tokens(
&sos.compact(),
&[
Token::Struct {
name: "PauliLindbladNoiseOperatorSerialize",
len: 2,
},
Token::Str("items"),
Token::Seq { len: Some(1) },
Token::Tuple { len: 4 },
Token::Seq { len: Some(1) },
Token::Tuple { len: 2 },
Token::U64(0),
Token::UnitVariant {
name: "SingleDecoherenceOperator",
variant: "X",
},
Token::TupleEnd,
Token::SeqEnd,
Token::Seq { len: Some(1) },
Token::Tuple { len: 2 },
Token::U64(0),
Token::UnitVariant {
name: "SingleDecoherenceOperator",
variant: "X",
},
Token::TupleEnd,
Token::SeqEnd,
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("serialisation_meta"),
Token::Struct {
name: "StruqtureSerialisationMeta",
len: 3,
},
Token::Str("type_name"),
Token::Str("PauliLindbladNoiseOperator"),
Token::Str("min_version"),
Token::Tuple { len: 3 },
Token::U64(2),
Token::U64(0),
Token::U64(0),
Token::TupleEnd,
Token::Str("version"),
Token::Str("2.0.0"),
Token::StructEnd,
Token::StructEnd,
],
);
}
}