From 776fc895aeefba2184256404e6b879ae5e70fa98 Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Mon, 8 Jul 2024 12:13:04 -0700 Subject: [PATCH 1/2] Extra flush for rotations by PI This fixes an issue where Rx/Ry rotations by PI would become X/Y that get enqueued rather than flushed immediately. --- sparsesim/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/sparsesim/src/lib.rs b/sparsesim/src/lib.rs index 823532f5..c4f3d830 100644 --- a/sparsesim/src/lib.rs +++ b/sparsesim/src/lib.rs @@ -1057,6 +1057,7 @@ impl QuantumSim { } else { self.mcx(ctls, target); } + self.flush_ops(); // Rx/Ry are different from X/Y by a global phase of -i, so apply that here when indicated by m01, // for mathematical correctness. let (_, ctls) = self.resolve_and_check_qubits(target, ctls); @@ -1507,6 +1508,26 @@ mod tests { assert!(sim.joint_probability(&[q]).is_nearly_zero()); } + /// Verifies that an Rx rotation by PI, which becomes an X gate, is correctly flushed. + #[test] + fn test_rx_pi_flushed() { + let mut sim = QuantumSim::new(None); + let q = sim.allocate(); + sim.rx(PI, q); + assert!(almost_equal(sim.joint_probability(&[q]), sim.joint_probability(&[q]))); + assert!(!sim.joint_probability(&[q]).is_nearly_zero()); + } + + /// Verifies that an Ry rotation by PI, which becomes an Y gate, is correctly flushed. + #[test] + fn test_ry_pi_flushed() { + let mut sim = QuantumSim::new(None); + let q = sim.allocate(); + sim.ry(PI, q); + assert!(almost_equal(sim.joint_probability(&[q]), sim.joint_probability(&[q]))); + assert!(!sim.joint_probability(&[q]).is_nearly_zero()); + } + /// Utility for testing operation equivalence. fn assert_operation_equal_referenced(mut op: F1, mut reference: F2, count: usize) where From 86369d77d40292a07df9e9a871ca8e399263ff23 Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Mon, 8 Jul 2024 12:16:03 -0700 Subject: [PATCH 2/2] Fix formatting --- sparsesim/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sparsesim/src/lib.rs b/sparsesim/src/lib.rs index c4f3d830..34fe8a62 100644 --- a/sparsesim/src/lib.rs +++ b/sparsesim/src/lib.rs @@ -1514,7 +1514,10 @@ mod tests { let mut sim = QuantumSim::new(None); let q = sim.allocate(); sim.rx(PI, q); - assert!(almost_equal(sim.joint_probability(&[q]), sim.joint_probability(&[q]))); + assert!(almost_equal( + sim.joint_probability(&[q]), + sim.joint_probability(&[q]) + )); assert!(!sim.joint_probability(&[q]).is_nearly_zero()); } @@ -1524,7 +1527,10 @@ mod tests { let mut sim = QuantumSim::new(None); let q = sim.allocate(); sim.ry(PI, q); - assert!(almost_equal(sim.joint_probability(&[q]), sim.joint_probability(&[q]))); + assert!(almost_equal( + sim.joint_probability(&[q]), + sim.joint_probability(&[q]) + )); assert!(!sim.joint_probability(&[q]).is_nearly_zero()); }