-
Notifications
You must be signed in to change notification settings - Fork 0
76 allow merging of pp terms and extend ct interface to allow discretized angles #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
keefehuang
wants to merge
13
commits into
main
Choose a base branch
from
76-allow-merging-of-pp-terms-and-extend-ct-interface-to-allow-discretized-angles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f08d8e2
Implement algorithm to find and then remove repeated terms in PauliPo…
keefehuang c44b791
Introduce the new Angle enum type that allows for radians and pi4 rot…
keefehuang b268b88
Implements for PauliString and PauliPolynomial.
keefehuang 12273a1
Add methods for composing Clifford gadgets into CliffordTableau
keefehuang 62c8ac9
Make imports for Sub, Add prettier
c58e57c
Rename from_pi4rotation functions
c34cbad
Optimize other_pauli_string in implementation
82092db
Update interface for
7242af2
Set assert! to assert_eq
8275447
Correct assert in to ensure source of panic
fc7ec2f
Improve Angle struct
b29ec67
Update angle implementation to mixed arbitrary and pi4 angles
b61376d
Stashed implementation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,292 @@ | ||
| use std::{ | ||
| f64::consts::PI, | ||
| ops::{Add, AddAssign, Sub, SubAssign}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq)] | ||
| pub enum Angle { | ||
| Arbitrary(f64), | ||
| Pi4Rotations(u8), | ||
| } | ||
|
|
||
| impl Angle { | ||
| pub fn from_angle(rad: f64) -> Self { | ||
| Angle::Arbitrary(rad) | ||
| } | ||
|
|
||
| pub fn from_angles(angles: &[f64]) -> Vec<Self> { | ||
| angles | ||
| .into_iter() | ||
| .map(|rad| Angle::from_angle(*rad)) | ||
| .collect() | ||
| } | ||
|
|
||
| pub fn from_pi4_rotation(n: u8) -> Self { | ||
| Angle::Pi4Rotations(n % 8) | ||
| } | ||
|
|
||
| pub fn from_pi4_rotations(ns: &[u8]) -> Vec<Self> { | ||
| ns.into_iter() | ||
| .map(|n| Angle::from_pi4_rotation(*n)) | ||
| .collect() | ||
| } | ||
|
|
||
| pub fn to_radians(&self) -> f64 { | ||
| match self { | ||
| Angle::Arbitrary(rad) => *rad, | ||
| Angle::Pi4Rotations(n) => (*n as f64) * (std::f64::consts::FRAC_PI_4), | ||
| } | ||
| } | ||
|
|
||
| pub fn flip(&mut self) { | ||
| match self { | ||
| Angle::Arbitrary(rad) => *rad = -*rad, | ||
| Angle::Pi4Rotations(n) => *n = (8 - *n) % 8, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl AddAssign for Angle { | ||
| fn add_assign(&mut self, other: Self) { | ||
| match (self, other) { | ||
| (Angle::Arbitrary(rad1), Angle::Arbitrary(rad2)) => { | ||
| *rad1 += rad2; | ||
| } | ||
| (Angle::Pi4Rotations(n1), Angle::Pi4Rotations(n2)) => { | ||
| *n1 = (*n1 + n2) % 8; | ||
| } | ||
| (Angle::Arbitrary(rad1), Angle::Pi4Rotations(n2)) => { | ||
| *rad1 += n2 as f64 * PI / 4.0; | ||
| } | ||
| _ => panic!("Cannot add Arbitrary Angle to Pi4 rotation"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SubAssign for Angle { | ||
| fn sub_assign(&mut self, other: Self) { | ||
| match (self, other) { | ||
| (Angle::Arbitrary(rad1), Angle::Arbitrary(rad2)) => { | ||
| *rad1 -= rad2; | ||
| } | ||
| (Angle::Pi4Rotations(n1), Angle::Pi4Rotations(n2)) => { | ||
| *n1 = (*n1 + (8 - n2)) % 8; | ||
| } | ||
| (Angle::Arbitrary(rad1), Angle::Pi4Rotations(n2)) => { | ||
| *rad1 -= n2 as f64 * PI / 4.0; | ||
| } | ||
| _ => panic!("Cannot subtract different types of Angles"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Add for Angle { | ||
| type Output = Angle; | ||
|
|
||
| fn add(self, other: Angle) -> Angle { | ||
| match (self, other) { | ||
| (Angle::Arbitrary(rad1), Angle::Arbitrary(rad2)) => Angle::Arbitrary(rad1 + rad2), | ||
| (Angle::Pi4Rotations(n1), Angle::Pi4Rotations(n2)) => { | ||
| Angle::Pi4Rotations((n1 + n2) % 8) | ||
| } | ||
| (Angle::Arbitrary(rad1), Angle::Pi4Rotations(n2)) => { | ||
| Angle::Arbitrary(rad1 + n2 as f64 * PI / 4.0) | ||
| } | ||
| (Angle::Pi4Rotations(n1), Angle::Arbitrary(rad2)) => { | ||
| Angle::Arbitrary(rad2 + n1 as f64 * PI / 4.0) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Sub for Angle { | ||
| type Output = Angle; | ||
|
|
||
| fn sub(self, other: Angle) -> Angle { | ||
| match (self, other) { | ||
| (Angle::Arbitrary(rad1), Angle::Arbitrary(rad2)) => Angle::Arbitrary(rad1 - rad2), | ||
| (Angle::Pi4Rotations(n1), Angle::Pi4Rotations(n2)) => { | ||
| Angle::Pi4Rotations((n1 + 8 - n2) % 8) | ||
| } | ||
| (Angle::Arbitrary(rad1), Angle::Pi4Rotations(n2)) => { | ||
| Angle::Arbitrary(rad1 - n2 as f64 * PI / 4.0) | ||
| } | ||
| (Angle::Pi4Rotations(n1), Angle::Arbitrary(rad2)) => { | ||
| Angle::Arbitrary(n1 as f64 * PI / 4.0 - rad2) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn check_angle_approx(angle1: Angle, angle2: Angle) -> bool { | ||
| match (angle1, angle2) { | ||
| (Angle::Arbitrary(a1), Angle::Arbitrary(a2)) => (a1 - a2).abs() < 1e-9, | ||
| (Angle::Pi4Rotations(a1), Angle::Pi4Rotations(a2)) => a1 == a2, | ||
| _ => panic!("Not defined for Arbitrary Angles and Pi4 rotations"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_simple_add() { | ||
| let n1 = 1; | ||
| let n2 = 2; | ||
|
|
||
| let a1 = Angle::from_pi4_rotation(n1); | ||
| let a2 = Angle::from_pi4_rotation(n2); | ||
|
|
||
| assert_eq!(a1 + a2, Angle::from_pi4_rotation(3)); | ||
|
|
||
| let mut a3 = Angle::from_pi4_rotation(n1); | ||
| a3 += a2; | ||
|
|
||
| assert_eq!(a3, Angle::from_pi4_rotation(3)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_overflow_add() { | ||
| let n1 = 5; | ||
| let n2 = 6; | ||
|
|
||
| let a1 = Angle::from_pi4_rotation(n1); | ||
| let a2 = Angle::from_pi4_rotation(n2); | ||
|
|
||
| assert_eq!(a1 + a2, Angle::from_pi4_rotation(3)); | ||
|
|
||
| let mut a3 = Angle::from_pi4_rotation(n1); | ||
| a3 += a2; | ||
|
|
||
| assert_eq!(a3, Angle::from_pi4_rotation(3)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_simple_sub() { | ||
| let n1 = 4; | ||
| let n2 = 2; | ||
|
|
||
| let a1 = Angle::from_pi4_rotation(n1); | ||
| let a2 = Angle::from_pi4_rotation(n2); | ||
|
|
||
| assert_eq!(a1 - a2, Angle::from_pi4_rotation(2)); | ||
|
|
||
| let mut a3 = Angle::from_pi4_rotation(n1); | ||
| a3 -= a2; | ||
|
|
||
| assert_eq!(a3, Angle::from_pi4_rotation(2)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_overflow_sub() { | ||
| let n1 = 2; | ||
| let n2 = 6; | ||
|
|
||
| let a1 = Angle::from_pi4_rotation(n1); | ||
| let a2 = Angle::from_pi4_rotation(n2); | ||
|
|
||
| assert_eq!(a1 - a2, Angle::from_pi4_rotation(4)); | ||
|
|
||
| let mut a3 = Angle::from_pi4_rotation(n1); | ||
| a3 -= a2; | ||
|
|
||
| assert_eq!(a3, Angle::from_pi4_rotation(4)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_float_simple_add() { | ||
| let n1 = 0.32; | ||
| let n2 = 0.64; | ||
|
|
||
| let a1 = Angle::from_angle(n1); | ||
| let a2 = Angle::from_angle(n2); | ||
|
|
||
| let ref_a = Angle::from_angle(0.96); | ||
|
|
||
| assert!(check_angle_approx(a1 + a2, ref_a)); | ||
|
|
||
| let mut a3 = Angle::from_angle(n1); | ||
| a3 += a2; | ||
|
|
||
| assert!(check_angle_approx(a3, ref_a)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_float_simple_sub() { | ||
| let n1 = 0.32; | ||
| let n2 = 0.64; | ||
|
|
||
| let a1 = Angle::from_angle(n1); | ||
| let a2 = Angle::from_angle(n2); | ||
|
|
||
| let ref_a = Angle::from_angle(-0.32); | ||
|
|
||
| assert!(check_angle_approx(a1 - a2, ref_a)); | ||
|
|
||
| let mut a3 = Angle::from_angle(n1); | ||
| a3 -= a2; | ||
|
|
||
| assert!(check_angle_approx(a3, ref_a)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_mixed_simple_add() { | ||
| let n1 = 0.32; | ||
| let n2 = 2; | ||
|
|
||
| let a1 = Angle::from_angle(n1); | ||
| let a2 = Angle::from_pi4_rotation(n2); | ||
|
|
||
| let ref_a = Angle::from_angle(1.8907963268); | ||
| assert!(check_angle_approx(a1 + a2, ref_a)); | ||
| assert!(check_angle_approx(a2 + a1, ref_a)); | ||
|
|
||
| let mut a3 = Angle::from_angle(n1); | ||
| a3 += a2; | ||
|
|
||
| assert!(check_angle_approx(a3, ref_a)); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn test_angle_bad_mixed_simple_add() { | ||
| let n1 = 0.32; | ||
| let n2 = 2; | ||
|
|
||
| let mut a2 = Angle::from_pi4_rotation(n2); | ||
| let a3 = Angle::from_angle(n1); | ||
| a2 += a3 | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_angle_mixed_simple_sub() { | ||
| let n1 = 0.32; | ||
| let n2 = 2; | ||
|
|
||
| let a1 = Angle::from_angle(n1); | ||
| let a2 = Angle::from_pi4_rotation(n2); | ||
|
|
||
| let ref_a1 = Angle::from_angle(-1.2507963268); | ||
| let ref_a2 = Angle::from_angle(1.2507963268); | ||
|
|
||
| assert!(check_angle_approx(a1 - a2, ref_a1)); | ||
| assert!(check_angle_approx(a2 - a1, ref_a2)); | ||
|
|
||
| let mut a3 = Angle::from_angle(n1); | ||
| a3 -= a2; | ||
|
|
||
| assert!(check_angle_approx(a3, ref_a1)); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn test_angle_bad_mixed_simple_sub() { | ||
| let n1 = 0.32; | ||
| let n2 = 2; | ||
|
|
||
| let mut a2 = Angle::from_pi4_rotation(n2); | ||
| let a3 = Angle::from_angle(n1); | ||
| a2 -= a3 | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also implement
std::ops::Neginstead