From eb775b22e7559fcac055791ffca7ace3f3c43f50 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 3 Dec 2024 19:43:07 +0100 Subject: [PATCH] Adressing review comments. --- library/rotations/src/HammingWeightPhasing.qs | 13 ++++++++----- library/rotations/src/Tests.qs | 8 +++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/library/rotations/src/HammingWeightPhasing.qs b/library/rotations/src/HammingWeightPhasing.qs index 8d3884670f..ee4ac4c9d3 100644 --- a/library/rotations/src/HammingWeightPhasing.qs +++ b/library/rotations/src/HammingWeightPhasing.qs @@ -4,7 +4,7 @@ import Std.Arrays.Enumerated, Std.Arrays.Most, Std.Arrays.Partitioned, Std.Arrays.Tail; import Std.Convert.IntAsDouble; import Std.Diagnostics.Fact; -import Std.Math.Floor, Std.Math.Lg, Std.Math.MaxI, Std.Math.MinI; +import Std.Math.BitSizeI, Std.Math.Floor, Std.Math.Lg, Std.Math.MaxI, Std.Math.MinI; /// # Summary /// Applies a Z-rotation (`Rz`) with given angle to each qubit in qs. @@ -12,7 +12,9 @@ import Std.Math.Floor, Std.Math.Lg, Std.Math.MaxI, Std.Math.MinI; /// # Description /// This implementation is based on Hamming-weight phasing to reduce the number /// of rotation gates. The technique was first presented in [1], and further -/// improved in [2] based on results in [3, 4]. +/// improved in [2] based on results in [3, 4]. Note, that the reduction of +/// rotation gates comes at a cost of additional qubits and additional quantum +/// operations to compute the Hamming-weight. /// /// # Reference /// - [1](https://arxiv.org/abs/1709.06648) "Halving the cost of quantum @@ -50,9 +52,10 @@ internal operation WithHammingWeight(qs : Qubit[], action : Qubit[] => Unit) : U } elif n == 3 { WithSum(qs[0], qs[1..1], qs[2..2], action); } else { - let power = Floor(Lg(IntAsDouble(n - 1))); - let (leftLen, rightLen) = (n - 2^power, 2^power - 1); - // handle corner case if n is power of 2 + let splitSize = 2^(BitSizeI(n - 1) - 1); + let (leftLen, rightLen) = (n - splitSize, splitSize - 1); + // handle corner case if n is power of 2; in that case the first + // partition is longer than the second one, and we want to avoid that. let split = Partitioned([MinI(leftLen, rightLen), MaxI(leftLen, rightLen)], qs); Fact(Length(split) == 3 and Length(split[2]) == 1, $"Unexpected split for n = {n}"); diff --git a/library/rotations/src/Tests.qs b/library/rotations/src/Tests.qs index 4df0774cb6..0d9267e52c 100644 --- a/library/rotations/src/Tests.qs +++ b/library/rotations/src/Tests.qs @@ -2,7 +2,7 @@ // Licensed under the MIT License. import Std.Diagnostics.CheckOperationsAreEqual, Std.Diagnostics.Fact; -import Std.Math.HammingWeightI; +import Std.Math.HammingWeightI, Std.Math.PI; import HammingWeightPhasing.HammingWeightPhasing, HammingWeightPhasing.WithHammingWeight; @@ -42,7 +42,9 @@ operation TestHammingWeight() : Unit { } operation TestPhasing() : Unit { - for numQubits in 1..6 { - Fact(CheckOperationsAreEqual(numQubits, qs => HammingWeightPhasing(2.0, qs), qs => ApplyToEachA(Rz(2.0, _), qs)), "Operations are not equal"); + for theta in [1.0, 2.0, 0.0, -0.5, 5.0 * PI()] { + for numQubits in 1..6 { + Fact(CheckOperationsAreEqual(numQubits, qs => HammingWeightPhasing(theta, qs), qs => ApplyToEachA(Rz(theta, _), qs)), "Operations are not equal"); + } } }