-
Notifications
You must be signed in to change notification settings - Fork 0
71 allow for circuit to pauli exponential #86
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
74dfa88
Shift definition of PauliExponential to `data_structures`
5765674
Implement algorithm to find and then remove repeated terms in PauliPo…
keefehuang d0ef9f2
Introduce the new Angle enum type that allows for radians and pi4 rot…
keefehuang 0b0c072
Implements for PauliString and PauliPolynomial.
keefehuang bb4a97e
Add methods for composing Clifford gadgets into CliffordTableau
keefehuang aeef9a5
Make imports for Sub, Add prettier
58b17ba
Rename from_pi4rotation functions
c22a622
Optimize other_pauli_string in implementation
871d268
Update interface for
6eee674
Set assert! to assert_eq
55811d1
Correct assert in to ensure source of panic
f055ec5
Improve Angle struct
ec9f42b
Update angle implementation to mixed arbitrary and pi4 angles
b56a65c
Add new wrapper for Qiskit
5b47d83
Fixup
f264893
Implement PropagateClifford for PauliExponential
5d88c75
Implement full loop for qiskit to IR to qiskit
ce11a00
Add __init__.py into qiskit folder
8727918
Add test for loop
53b68ac
Rename op to op1 for ease of reading
17aa3fe
Format wrapper.rs
ff61d9e
Format files
9c54dec
Add trailing white-space
8d13e70
Removed unneeded copy from test_qiskit_loop
ad35505
Remove print statements
bd769ca
Set return of compose_gadget to Result
104c682
Include pi4 rotations in PauliPolynomial tests
2da3a53
Format pauli_exponential.rs
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.
I guess you cannot say
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.
same for subassign below
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.
Yup, I don't think you can do this.