Skip to content

Commit

Permalink
adding pauli structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranra committed Jan 4, 2025
1 parent 7536d27 commit bcf332e
Show file tree
Hide file tree
Showing 19 changed files with 1,175 additions and 562 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pyo3 = "0.23"
rand = "0.8"
rand_chacha = "0.3"
rand_xoshiro = "0.6"
num-complex = "0.4"

pecos-core = { version = "0.1.1", path = "crates/pecos-core" }
pecos-qsim = { version = "0.1.1", path = "crates/pecos-qsim" }
Expand Down
4 changes: 2 additions & 2 deletions crates/benchmarks/benches/modules/element_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn bench_conversion<E: IndexableElement + From<u16>, M: Measurement>(
b.iter(|| {
let mut sum = 0_usize;
for i in 0..1000_u16 {
sum += E::from(i).to_usize();
sum += E::from(i).to_index();
}
black_box(sum)
});
Expand All @@ -50,7 +50,7 @@ fn bench_memory_access<E: IndexableElement + From<u16>, M: Measurement>(
b.iter(|| {
let mut sum = E::from(0_u16);
for &item in &vec {
sum = E::from((sum.to_usize() + item.to_usize()) as u16);
sum = E::from((sum.to_index() + item.to_index()) as u16);
}
black_box(sum)
});
Expand Down
1 change: 1 addition & 0 deletions crates/pecos-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ description = "Provides core definitions and functions for PECOS simulations."
rand = { workspace = true }
rand_chacha = { workspace = true }
rand_xoshiro = { workspace = true }
num-complex = { workspace = true }

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ impl<T: Debug + Clone + Ord + Copy + Hash> Element for T {}

#[allow(clippy::module_name_repetitions)]
pub trait IndexableElement: Element {
fn to_usize(&self) -> usize;
fn from_usize(value: usize) -> Self;
fn to_index(&self) -> usize;
fn from_index(value: usize) -> Self;
}

macro_rules! impl_indexable_element_safe {
($t:ty) => {
impl IndexableElement for $t {
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
#[inline(always)]
fn to_usize(&self) -> usize {
fn to_index(&self) -> usize {
*self as usize
}

#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
#[inline(always)]
fn from_usize(value: usize) -> Self {
fn from_index(value: usize) -> Self {
value as $t
}
}
Expand All @@ -55,26 +55,26 @@ impl_indexable_element_safe!(u64);
#[cfg(target_pointer_width = "32")]
impl IndexableElement for u64 {
#[inline(always)]
fn to_usize(&self) -> usize {
fn to_index(&self) -> usize {
usize::try_from(*self).expect("u64 value too large for 32-bit usize")
}

#[allow(clippy::as_conversions)]
#[inline(always)]
fn from_usize(value: usize) -> Self {
fn from_index(value: usize) -> Self {
value as u64
}
}

// u128 is always problematic for current architectures, so we'll implement it to always panic
impl IndexableElement for u128 {
#[inline(always)]
fn to_usize(&self) -> usize {
fn to_index(&self) -> usize {
panic!("u128 cannot be safely converted to usize without potential data loss")
}

#[inline(always)]
fn from_usize(_value: usize) -> Self {
fn from_index(_value: usize) -> Self {
panic!("usize cannot be safely converted to u128 on all platforms")
}
}
98 changes: 98 additions & 0 deletions crates/pecos-core/src/gate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
pub enum Gate {
// Paulis
I,
X,
Y,
Z,

// Sqrt of Paulis
SX,
SXdg,
SY,
SYdg,
SZ,
SZdg,

// Hadamards
H,
H2,
H3,
H4,
H5,
H6,

// Face rotations
F,
Fdg,
F2,
F2dg,
F3,
F3dg,
F4,
F4dg,

// Controlled-Paulis
CX,
CY,
CZ,

// Sqrt of two Paulis
SXX,
SXXdg,
SYY,
SYYdg,
SZZ,
SZZdg,

// Other TQ gates
SWAP,
G,

// Measurements
MX,
MnX,
MY,
MnY,
MZ,
MnZ,

// Preps
PX,
PnX,
PY,
PnY,
PZ,
PnZ,

// Measure + Prep
MPX,
MPnX,
MPY,
MPnY,
MPZ,
MPnZ,

// # Non-Cliffords
// SQ rotations
RX(f64),
RY(f64),
RZ(f64),

U(f64, f64, f64),
R1XY(f64, f64),

// T gates
T,
Tdg,

// TQ rotations
RXX(f64),
RYY(f64),
RZZ(f64),

// Composite TQ rotation
RXXRYYRZZ(f64, f64, f64),

// Custom user gate
CustomGate,
}
22 changes: 17 additions & 5 deletions crates/pecos-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

mod pauli;
mod sets;
mod sims_rngs;
pub mod element;
pub mod gate;
pub mod pauli;
pub mod phase;
pub mod qubit_id;
pub mod sets;
pub mod sign;
pub mod sims_rngs;

pub use sets::element::{Element, IndexableElement};
pub use element::{Element, IndexableElement};
pub use phase::Phase;
pub use qubit_id::QubitId;
pub use sets::set::Set;
pub use sets::vec_set::VecSet;
pub use sign::Sign;

pub use crate::sims_rngs::chacha_rng::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};
pub use crate::sims_rngs::choices::Choices;
Expand All @@ -27,4 +35,8 @@ pub use crate::sims_rngs::xoshiro_rng::{
Xoshiro128PlusPlus, Xoshiro128StarStar, Xoshiro256PlusPlus, Xoshiro256StarStar,
Xoshiro512PlusPlus, Xoshiro512StarStar,
};
pub use pauli::{BitSetPauli, PauliCollection, PauliOperator, Phase, SetPauli, StdPauli};
pub use gate::Gate;
pub use pauli::pauli_bitmap::PauliBitmap;
pub use pauli::pauli_sparse::PauliSparse;
pub use pauli::pauli_string::PauliString;
pub use pauli::{Pauli, PauliOperator};
Loading

0 comments on commit bcf332e

Please sign in to comment.