Skip to main content

PauliString

Struct PauliString 

Source
pub struct PauliString { /* private fields */ }
Expand description

A dense unsigned Pauli operator over len qubits, stored as XZ bit vectors.

Phaseless, with each site read as the Hermitian single-qubit Pauli it names — a site carrying both bits is Y = iXZ, not the bare product XZ. A product of Hermitian factors on distinct sites is Hermitian, so every PauliString is a valid measurement observable or rotation axis by construction; there is no phase to make it otherwise. Operations between different widths treat missing trailing sites as identities; XOR keeps the wider width. Parse strings of I, _, X, Y, and Z with str::parse; Display renders identity as _.

Implementations§

Source§

impl PauliString

Source

pub fn new(num_qubits: usize) -> Self

Creates the identity Pauli string over num_qubits qubits.

Source

pub fn single(num_qubits: usize, index: usize, pauli: Pauli) -> Self

Creates the string with pauli at index and identity everywhere else.

§Panics

Panics if index >= num_qubits.

§Examples
use bloq_utils::{Pauli, PauliString};

let observable = PauliString::single(4, 2, Pauli::Y);
assert_eq!(observable.to_string(), "__Y_");
Source

pub fn from_terms( num_qubits: usize, terms: impl IntoIterator<Item = (usize, Pauli)>, ) -> Self

Collects (index, pauli) terms into a string over num_qubits qubits.

Terms are assignments, not factors: a repeated index keeps the last Pauli given for it rather than multiplying the two.

§Panics

Panics if any index is >= num_qubits.

§Examples
use bloq_utils::{Pauli, PauliString};

let stabilizer = PauliString::from_terms(3, [(0, Pauli::Z), (2, Pauli::Z)]);
assert_eq!(stabilizer.to_string(), "Z_Z");
Source

pub fn commutes_with(&self, other: &Self) -> bool

Returns true if self and other commute as operators.

Two Pauli strings anticommute exactly when they anticommute on an odd number of sites, which the symplectic form ⟨x, other.z⟩ + ⟨z, other.x⟩ counts mod 2.

The two strings need not name the same number of qubits. Where one runs out the other is being compared against implicit identities, which commute with everything — so the fold below stops at the shorter operand rather than requiring equal widths. Going through the words directly is also what avoids BitVec::dot, whose length assertion holds in release builds and would turn a width mismatch into a panic.

§Examples
use bloq_utils::PauliString;

let xx = PauliString::try_from("XX")?;
let zz = PauliString::try_from("ZZ")?;
let zi = PauliString::try_from("Z_")?;
assert!(xx.commutes_with(&zz));   // two anticommuting sites
assert!(!xx.commutes_with(&zi));  // one

// Widths may differ; the shorter string ends in implicit identities.
let x_wide = PauliString::try_from("X___")?;
assert!(!x_wide.commutes_with(&PauliString::try_from("Z")?));
assert!(x_wide.commutes_with(&PauliString::try_from("_Z")?));
Source

pub fn product_phase(&self, other: &Self) -> u8

Returns the power of i contributed by multiplying these strings. Missing sites are identities.

Source

pub fn x_words(&self) -> &[u64]

The X components as u64 words, least-significant qubit first.

Word-level access for bit algebra over whole strings — the simulator’s frame decomposition walks these directly rather than one site at a time. Bits at or beyond len are always clear.

Source

pub fn z_words(&self) -> &[u64]

The Z components as u64 words. See x_words.

Source

pub fn len(&self) -> usize

Returns the number of qubits.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no qubits.

Source

pub fn weight(&self) -> usize

Returns the number of non-identity Paulis.

Source

pub fn is_identity(&self) -> bool

Returns whether every site is identity, stopping at the first nonzero word.

Source

pub fn iter_support(&self) -> impl Iterator<Item = (usize, Pauli)> + '_

Iterates the non-identity sites in ascending index order.

§Panics

Panics if the packed X/Z representation contains an impossible support bit.

Source

pub fn get(&self, index: usize) -> Pauli

Returns the Pauli at index.

§Panics

Panics if index is out of bounds.

Source

pub fn set(&mut self, index: usize, pauli: Pauli)

Sets the Pauli at index.

§Panics

Panics if index is out of bounds.

Trait Implementations§

Source§

impl BitXor for &PauliString

Source§

type Output = PauliString

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXorAssign<&PauliString> for PauliString

Source§

fn bitxor_assign(&mut self, other: &Self)

Performs the ^= operation. Read more
Source§

impl Clone for PauliString

Source§

fn clone(&self) -> PauliString

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PauliString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PauliString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for PauliString

Source§

impl FromStr for PauliString

Source§

type Err = PauliError

The associated error which can be returned from parsing.
Source§

fn from_str(value: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for PauliString

Source§

fn eq(&self, other: &PauliString) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for PauliString

Source§

impl TryFrom<&str> for PauliString

Source§

type Error = PauliError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T, BorrowedSelf> BitwiseMutViaBorrow<BorrowedSelf> for T
where T: BorrowMut<BorrowedSelf>, BorrowedSelf: BitwiseMut + ?Sized,

§

fn assign_index(&mut self, index: usize, to: bool)

§

fn negate_index(&mut self, index: usize)

§

fn clear_bits(&mut self)

§

fn assign_random( &mut self, bit_count: usize, random_number_generator: &mut impl RngExt, )
where BorrowedSelf: BitLength,

§

impl<Bits, Other, BorrowedSelf, BorrowedOther> BitwisePairMutViaBorrow<Other, BorrowedSelf, BorrowedOther> for Bits
where Bits: BorrowMut<BorrowedSelf> + ?Sized, Other: Borrow<BorrowedOther> + ?Sized, BorrowedSelf: Bitwise + BitwisePairMut<BorrowedOther> + ?Sized, BorrowedOther: Bitwise + ?Sized,

§

fn assign(&mut self, other: &Other)

§

fn bitxor_assign(&mut self, other: &Other)

§

fn bitand_assign(&mut self, other: &Other)

§

fn bitor_assign(&mut self, other: &Other)

§

impl<T, Other, BorrowedSelf, BorrowedOther> BitwisePairViaBorrow<Other, BorrowedSelf, BorrowedOther> for T
where T: Borrow<BorrowedSelf> + ?Sized, Other: Borrow<BorrowedOther> + ?Sized, BorrowedSelf: Bitwise + BitwisePair<BorrowedOther> + ?Sized, BorrowedOther: Bitwise + ?Sized,

§

fn dot(&self, other: &Other) -> bool

§

fn and_weight(&self, other: &Other) -> usize

§

fn or_weight(&self, other: &Other) -> usize

§

fn xor_weight(&self, other: &Other) -> usize

§

impl<T, BorrowedSelf> BitwiseViaBorrow<BorrowedSelf> for T
where T: Borrow<BorrowedSelf>, BorrowedSelf: Bitwise + ?Sized,

§

fn index(&self, index: usize) -> bool

§

fn weight(&self) -> usize

§

fn parity(&self) -> bool

§

fn is_zero(&self) -> bool

§

fn is_unit(&self, index: usize) -> bool

§

fn support<'a>(&'a self) -> impl SortedIterator
where BorrowedSelf: 'a,

§

fn max_support(&self) -> Option<usize>

§

fn min_support(&self) -> Option<usize>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.