Skip to content

Commit

Permalink
Adressing review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
msoeken committed Dec 3, 2024
1 parent 007ec94 commit eb775b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 8 additions & 5 deletions library/rotations/src/HammingWeightPhasing.qs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
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.
///
/// # 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
Expand Down Expand Up @@ -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}");

Expand Down
8 changes: 5 additions & 3 deletions library/rotations/src/Tests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit eb775b2

Please sign in to comment.