Struct pauli::PauliOperator
source · [−]pub struct PauliOperator { /* private fields */ }
Expand description
A Pauli operator optimized for sparse operations.
A Pauli operator is a string of Paulis
such as IXIX
or XIYIZ
.
However,
we usually only care about the non-identity positions
and we refer to the previous as operators as X_1 X_3
and X_0 Y_2 Z_4
.
Implementations
sourceimpl PauliOperator
impl PauliOperator
sourcepub fn new(length: usize, positions: Vec<usize>, paulis: Vec<Pauli>) -> Self
pub fn new(length: usize, positions: Vec<usize>, paulis: Vec<Pauli>) -> Self
Builds a new Pauli Operator.
To build an operator, we specify the length, the position of non-identity elements and their values.
Exemple
This creates the XIYIZ
operator.
let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
Panic
Panics if a position is greater or equal to the length or if the number of positions and Paulis are different.
sourcepub fn try_new(
length: usize,
positions: Vec<usize>,
paulis: Vec<Pauli>
) -> Result<Self, PauliError>
pub fn try_new(
length: usize,
positions: Vec<usize>,
paulis: Vec<Pauli>
) -> Result<Self, PauliError>
Builds a new Pauli Operator or returns an error if either a position is greater or equal to the length or if the numbers of positions and Paulis are different.
Exemple
This creates the XIYIZ
operator.
let operator = PauliOperator::try_new(5, vec![0, 2, 4], vec![X, Y, Z]);
assert!(operator.is_ok());
sourcepub fn commutes_with(&self, other: &Self) -> bool
pub fn commutes_with(&self, other: &Self) -> bool
Checks if two operators commute.
If an operator is smaller than the other, it is padded with identities.
Example
let op1 = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
let op2 = PauliOperator::new(5, vec![2, 3, 4], vec![X, X, X]);
let op3 = PauliOperator::new(5, vec![0, 1], vec![Z, Z]);
assert!(op1.commutes_with(&op2));
assert!(!op1.commutes_with(&op3));
sourcepub fn anticommutes_with(&self, other: &Self) -> bool
pub fn anticommutes_with(&self, other: &Self) -> bool
Checks if two operators anticommute.
If an operator is smaller than the other, it is padded with identities.
Example
let op1 = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
let op2 = PauliOperator::new(5, vec![2, 3, 4], vec![X, X, X]);
let op3 = PauliOperator::new(5, vec![0, 1], vec![Z, Z]);
assert!(!op1.anticommutes_with(&op2));
assert!(op1.anticommutes_with(&op3));
sourcepub fn iter(&self) -> VectorIterator<'_, Pauli, usize>
pub fn iter(&self) -> VectorIterator<'_, Pauli, usize>
Returns an iterator over pairs of positions and Paulis.
Example
let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
let mut iter = operator.iter();
assert_eq!(iter.next(), Some((0, &X)));
assert_eq!(iter.next(), Some((2, &Y)));
assert_eq!(iter.next(), Some((4, &Z)));
assert_eq!(iter.next(), None);
sourcepub fn get(&self, position: usize) -> Option<Pauli>
pub fn get(&self, position: usize) -> Option<Pauli>
Returns the Pauli at the given position or None if the position is out of bound.
Example
let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
assert_eq!(operator.get(0), Some(X));
assert_eq!(operator.get(1), Some(I));
assert_eq!(operator.get(2), Some(Y));
assert_eq!(operator.get(10), None);
sourcepub fn non_trivial_positions(&self) -> &[usize]
pub fn non_trivial_positions(&self) -> &[usize]
Returns a slice of the positions where the element is not identity.
Example
let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
assert_eq!(operator.non_trivial_positions(), &[0, 2, 4]);
sourcepub fn partition_x_and_z(&self) -> (Self, Self)
pub fn partition_x_and_z(&self) -> (Self, Self)
Returns two operators such that there product is the original operator and the first contains only Xs and the second only Zs.
Example
let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
let (x_operator, z_operator) = operator.partition_x_and_z();
assert_eq!(x_operator, PauliOperator::new(5, vec![0, 2], vec![X, X]));
assert_eq!(z_operator, PauliOperator::new(5, vec![2, 4], vec![Z, Z]));
sourcepub fn x_part(&self) -> Self
pub fn x_part(&self) -> Self
Returns the X part of the operator.
Example
let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
let x_operator = operator.x_part();
assert_eq!(x_operator, PauliOperator::new(5, vec![0, 2], vec![X, X]));
sourcepub fn z_part(&self) -> Self
pub fn z_part(&self) -> Self
Returns the Z part of the operator.
Example
let operator = PauliOperator::new(5, vec![0, 2, 4], vec![X, Y, Z]);
let z_operator = operator.z_part();
assert_eq!(z_operator, PauliOperator::new(5, vec![2, 4], vec![Z, Z]));
sourcepub fn multiply_with(&self, other: &Self) -> Result<Self, PauliError>
pub fn multiply_with(&self, other: &Self) -> Result<Self, PauliError>
Returns the element-wise product of two operators or an Error if they have different lengths.
For a panicking version, use the *
operator.
Example
let op1 = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
let op2 = PauliOperator::new(5, vec![2, 3, 4], vec![Y, X, Z]);
let product = PauliOperator::new(5, vec![1, 3, 4], vec![X, Y, Z]);
assert_eq!(op1.multiply_with(&op2), Ok(product))
sourcepub fn into_raw_positions(self) -> Vec<usize>
pub fn into_raw_positions(self) -> Vec<usize>
Converts a PauliOperator to a Vec of its non trivial positions consumming the operator.
Example
let operator = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
let positions = operator.into_raw_positions();
assert_eq!(positions, vec![1, 2, 3]);
sourcepub fn into_raw_paulis(self) -> Vec<Pauli>
pub fn into_raw_paulis(self) -> Vec<Pauli>
Converts a PauliOperator to a Vec of the Paulis consumming the operator.
Example
let operator = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
let paulis = operator.into_raw_paulis();
assert_eq!(paulis, vec![X, Y, Z]);
sourcepub fn into_raw(self) -> (Vec<usize>, Vec<Pauli>)
pub fn into_raw(self) -> (Vec<usize>, Vec<Pauli>)
Converts a PauliOperator to a Vec of the positions and a Vec of Paulis consumming the operator.
Example
let operator = PauliOperator::new(5, vec![1, 2, 3], vec![X, Y, Z]);
let (positions, paulis) = operator.into_raw();
assert_eq!(positions, vec![1, 2, 3]);
assert_eq!(paulis, vec![X, Y, Z]);
Trait Implementations
sourceimpl Clone for PauliOperator
impl Clone for PauliOperator
sourcefn clone(&self) -> PauliOperator
fn clone(&self) -> PauliOperator
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for PauliOperator
impl Debug for PauliOperator
sourceimpl<'de> Deserialize<'de> for PauliOperator
impl<'de> Deserialize<'de> for PauliOperator
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Display for PauliOperator
impl Display for PauliOperator
sourceimpl Hash for PauliOperator
impl Hash for PauliOperator
sourceimpl<'a> Mul<&'a PauliOperator> for &'a PauliOperator
impl<'a> Mul<&'a PauliOperator> for &'a PauliOperator
sourceimpl PartialEq<PauliOperator> for PauliOperator
impl PartialEq<PauliOperator> for PauliOperator
sourcefn eq(&self, other: &PauliOperator) -> bool
fn eq(&self, other: &PauliOperator) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &PauliOperator) -> bool
fn ne(&self, other: &PauliOperator) -> bool
This method tests for !=
.
sourceimpl Serialize for PauliOperator
impl Serialize for PauliOperator
impl Eq for PauliOperator
impl StructuralEq for PauliOperator
impl StructuralPartialEq for PauliOperator
Auto Trait Implementations
impl RefUnwindSafe for PauliOperator
impl Send for PauliOperator
impl Sync for PauliOperator
impl Unpin for PauliOperator
impl UnwindSafe for PauliOperator
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Pointable for T
impl<T> Pointable for T
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more