From bb34807ff1a620ff083f51d4e0b3ac9403e2d8b7 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 19 Nov 2024 13:52:09 +0100 Subject: [PATCH 1/6] Hamming weight phasing operation. --- library/rotations/README.md | 4 + library/rotations/qsharp.json | 9 +++ library/rotations/src/HammingWeightPhasing.qs | 76 +++++++++++++++++++ library/rotations/src/Main.qs | 4 + library/rotations/src/Tests.qs | 4 + 5 files changed, 97 insertions(+) create mode 100644 library/rotations/README.md create mode 100644 library/rotations/qsharp.json create mode 100644 library/rotations/src/HammingWeightPhasing.qs create mode 100644 library/rotations/src/Main.qs create mode 100644 library/rotations/src/Tests.qs diff --git a/library/rotations/README.md b/library/rotations/README.md new file mode 100644 index 0000000000..bacdec2029 --- /dev/null +++ b/library/rotations/README.md @@ -0,0 +1,4 @@ +# Rotations + +The `rotations` library defines operations for applying rotations to qubits and +registers. diff --git a/library/rotations/qsharp.json b/library/rotations/qsharp.json new file mode 100644 index 0000000000..363d2739ee --- /dev/null +++ b/library/rotations/qsharp.json @@ -0,0 +1,9 @@ +{ + "author": "Microsoft", + "license": "MIT", + "files": [ + "src/HammingWeightPhasing.qs", + "src/Main.qs", + "src/Tests.qs" + ] +} diff --git a/library/rotations/src/HammingWeightPhasing.qs b/library/rotations/src/HammingWeightPhasing.qs new file mode 100644 index 0000000000..e888d96f46 --- /dev/null +++ b/library/rotations/src/HammingWeightPhasing.qs @@ -0,0 +1,76 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +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; + +operation HammingWeightPhasing(angle: Double, qs : Qubit[]) : Unit { + WithHammingWeight(qs, (sum) => { + for (i, sumQubit) in Enumerated(sum) { + Rz(IntAsDouble(2^i) * angle, sumQubit); + } + }); +} + +internal operation WithHammingWeight(qs : Qubit[], action : Qubit[] => Unit) : Unit { + let n = Length(qs); + + if n <= 1 { + action(qs); + } elif n == 2 { + use sum = Qubit(); + + within { + AND(qs[0], qs[1], sum); + CNOT(qs[0], qs[1]); + } apply { + action([qs[1], sum]); + } + } elif n == 3 { + WithSum(qs[0], qs[1..1], qs[2..2], action); + } else { + let power = Floor(Lg(IntAsDouble(n - 1))); + let split = Partitioned([n - 2^power, 2^power - 1], qs); + Fact(Length(split) == 3 and Length(split[2]) == 1, $"Unexpected split for n = {n}"); + + WithHammingWeight(split[0], (leftHW) => { + WithHammingWeight(split[1], (rightHW) => { + WithSum(split[2][0], leftHW, rightHW, action); + }); + }); + } +} + +internal operation Carry(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubit) : Unit is Adj { + CNOT(carryIn, x); + CNOT(carryIn, y); + AND(x, y, carryOut); + CNOT(carryIn, x); + CNOT(x, y); + CNOT(carryIn, carryOut); +} + +internal operation WithSum(carry : Qubit, xs : Qubit[], ys : Qubit[], action : Qubit[] => Unit) : Unit { + let n = Length(ys); + Fact(Length(xs) <= n, "Length of xs must be less or equal to length of ys"); + Fact(n > 0, "Length must be at least 1"); + + use carryOut = Qubit[n]; + let carryIn = [carry] + Most(carryOut); + + within { + for i in 0..n-1 { + if i < Length(xs) { + Carry(carryIn[i], xs[i], ys[i], carryOut[i]); + } else { + CNOT(carryIn[i], ys[i]); + AND(carryIn[i], ys[i], carryOut[i]); + CNOT(carryIn[i], carryOut[i]); + } + } + } apply { + action(ys + [Tail(carryOut)]); + } +} diff --git a/library/rotations/src/Main.qs b/library/rotations/src/Main.qs new file mode 100644 index 0000000000..d0c6a31b6a --- /dev/null +++ b/library/rotations/src/Main.qs @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +export HammingWeightPhasing.HammingWeightPhasing; diff --git a/library/rotations/src/Tests.qs b/library/rotations/src/Tests.qs new file mode 100644 index 0000000000..710f2e0d60 --- /dev/null +++ b/library/rotations/src/Tests.qs @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +operation Main() : Unit {} From dd6df388d07dbfb437a7526a529dc303edaaf372 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 19 Nov 2024 14:07:49 +0100 Subject: [PATCH 2/6] Add test case and handle corner case. --- library/rotations/src/HammingWeightPhasing.qs | 6 +++-- library/rotations/src/Tests.qs | 22 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/library/rotations/src/HammingWeightPhasing.qs b/library/rotations/src/HammingWeightPhasing.qs index e888d96f46..1c905e4650 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; +import Std.Math.Floor, Std.Math.Lg, Std.Math.MaxI, Std.Math.MinI; operation HammingWeightPhasing(angle: Double, qs : Qubit[]) : Unit { WithHammingWeight(qs, (sum) => { @@ -32,7 +32,9 @@ internal operation WithHammingWeight(qs : Qubit[], action : Qubit[] => Unit) : U WithSum(qs[0], qs[1..1], qs[2..2], action); } else { let power = Floor(Lg(IntAsDouble(n - 1))); - let split = Partitioned([n - 2^power, 2^power - 1], qs); + let (leftLen, rightLen) = (n - 2^power, 2^power - 1); + // handle corner case if n is power of 2 + let split = Partitioned([MinI(leftLen, rightLen), MaxI(leftLen, rightLen)], qs); Fact(Length(split) == 3 and Length(split[2]) == 1, $"Unexpected split for n = {n}"); WithHammingWeight(split[0], (leftHW) => { diff --git a/library/rotations/src/Tests.qs b/library/rotations/src/Tests.qs index 710f2e0d60..520958ecd8 100644 --- a/library/rotations/src/Tests.qs +++ b/library/rotations/src/Tests.qs @@ -1,4 +1,24 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -operation Main() : Unit {} +import Std.Diagnostics.Fact; +import Std.Math.HammingWeightI; + +import HammingWeightPhasing.WithHammingWeight; + +operation Main() : Unit { + TestHammingWeight(); +} + +operation TestHammingWeight() : Unit { + // exhaustive + use qs = Qubit[4]; + + for x in 0..2^Length(qs) - 1 { + ApplyXorInPlace(x, qs); + WithHammingWeight(qs, sum => { + Fact(MeasureInteger(sum) == HammingWeightI(x), $"wrong Hamming weight computed for x = {x}"); + }); + ResetAll(qs); + } +} From fd3f378ae2e3e832bea5db5efb2ec028265a9210 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 19 Nov 2024 18:31:28 +0100 Subject: [PATCH 3/6] Add tests and docs. --- library/rotations/src/HammingWeightPhasing.qs | 21 +++++++++++++++ library/rotations/src/Tests.qs | 26 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/library/rotations/src/HammingWeightPhasing.qs b/library/rotations/src/HammingWeightPhasing.qs index 1c905e4650..4587f66c97 100644 --- a/library/rotations/src/HammingWeightPhasing.qs +++ b/library/rotations/src/HammingWeightPhasing.qs @@ -6,6 +6,25 @@ import Std.Convert.IntAsDouble; import Std.Diagnostics.Fact; import 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]. +/// +/// # Reference +/// - [1](https://arxiv.org/abs/1709.06648) "Halving the cost of quantum +/// addition", Craig Gidney. +/// - [2](https://arxiv.org/abs/2012.09238) "Early fault-tolerant simulations of +/// the Hubbard model", Earl T. Campbell. +/// - [3](https://cpsc.yale.edu/sites/default/files/files/tr1260.pdf) "The exact +/// multiplicative complexity of the Hamming weight function", Joan Boyar and +/// René Peralta. +/// - [4](https://arxiv.org/abs/1908.01609) "The role of multiplicative +/// complexity in compiling low T-count oracle circuits", Giulia Meuli, +/// Mathias Soeken, Earl Campbell, Martin Roetteler, Giovanni De Micheli. operation HammingWeightPhasing(angle: Double, qs : Qubit[]) : Unit { WithHammingWeight(qs, (sum) => { for (i, sumQubit) in Enumerated(sum) { @@ -67,6 +86,8 @@ internal operation WithSum(carry : Qubit, xs : Qubit[], ys : Qubit[], action : Q if i < Length(xs) { Carry(carryIn[i], xs[i], ys[i], carryOut[i]); } else { + // there is no corresponding bit in xs; this is a version of + // Carry in which x == 0. CNOT(carryIn[i], ys[i]); AND(carryIn[i], ys[i], carryOut[i]); CNOT(carryIn[i], carryOut[i]); diff --git a/library/rotations/src/Tests.qs b/library/rotations/src/Tests.qs index 520958ecd8..1da449af32 100644 --- a/library/rotations/src/Tests.qs +++ b/library/rotations/src/Tests.qs @@ -1,3 +1,5 @@ +import HammingWeightPhasing.HammingWeightPhasing; +import Std.Diagnostics.CheckOperationsAreEqual; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. @@ -8,6 +10,7 @@ import HammingWeightPhasing.WithHammingWeight; operation Main() : Unit { TestHammingWeight(); + TestPhasing(); } operation TestHammingWeight() : Unit { @@ -21,4 +24,27 @@ operation TestHammingWeight() : Unit { }); ResetAll(qs); } + + // some explicit cases + for (width, number) in [ + (1, 1), + (2, 0), + (2, 3), + (8, 10), + (7, 99) + ] { + use qs = Qubit[width]; + + ApplyXorInPlace(number, qs); + WithHammingWeight(qs, sum => { + Fact(MeasureInteger(sum) == HammingWeightI(number), $"wrong Hamming weight computed for number = {number}"); + }); + ResetAll(qs); + } +} + +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"); + } } From 6ed8b29647359a2724f3c9091c8393a5d6534036 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 19 Nov 2024 18:39:16 +0100 Subject: [PATCH 4/6] Format files. --- library/rotations/src/HammingWeightPhasing.qs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/rotations/src/HammingWeightPhasing.qs b/library/rotations/src/HammingWeightPhasing.qs index 4587f66c97..8d3884670f 100644 --- a/library/rotations/src/HammingWeightPhasing.qs +++ b/library/rotations/src/HammingWeightPhasing.qs @@ -25,7 +25,7 @@ import Std.Math.Floor, Std.Math.Lg, Std.Math.MaxI, Std.Math.MinI; /// - [4](https://arxiv.org/abs/1908.01609) "The role of multiplicative /// complexity in compiling low T-count oracle circuits", Giulia Meuli, /// Mathias Soeken, Earl Campbell, Martin Roetteler, Giovanni De Micheli. -operation HammingWeightPhasing(angle: Double, qs : Qubit[]) : Unit { +operation HammingWeightPhasing(angle : Double, qs : Qubit[]) : Unit { WithHammingWeight(qs, (sum) => { for (i, sumQubit) in Enumerated(sum) { Rz(IntAsDouble(2^i) * angle, sumQubit); From ca5723e580d398c93584b80945ca3f690e97166e Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 19 Nov 2024 19:08:06 +0100 Subject: [PATCH 5/6] Fix import order. --- library/rotations/src/Tests.qs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/rotations/src/Tests.qs b/library/rotations/src/Tests.qs index 1da449af32..4df0774cb6 100644 --- a/library/rotations/src/Tests.qs +++ b/library/rotations/src/Tests.qs @@ -1,12 +1,10 @@ -import HammingWeightPhasing.HammingWeightPhasing; -import Std.Diagnostics.CheckOperationsAreEqual; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import Std.Diagnostics.Fact; +import Std.Diagnostics.CheckOperationsAreEqual, Std.Diagnostics.Fact; import Std.Math.HammingWeightI; -import HammingWeightPhasing.WithHammingWeight; +import HammingWeightPhasing.HammingWeightPhasing, HammingWeightPhasing.WithHammingWeight; operation Main() : Unit { TestHammingWeight(); From eb775b22e7559fcac055791ffca7ace3f3c43f50 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 3 Dec 2024 19:43:07 +0100 Subject: [PATCH 6/6] 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"); + } } }