Skip to content

Commit cc791d4

Browse files
authored
Library for rotation operations (#2040)
This is a Q# library for rotation operations. So far it includes an implementation of the Hamming weight phasing operation. I intend to include other rotation operations at a later point.
1 parent 83c3682 commit cc791d4

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

library/rotations/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Rotations
2+
3+
The `rotations` library defines operations for applying rotations to qubits and
4+
registers.

library/rotations/qsharp.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"author": "Microsoft",
3+
"license": "MIT",
4+
"files": [
5+
"src/HammingWeightPhasing.qs",
6+
"src/Main.qs",
7+
"src/Tests.qs"
8+
]
9+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import Std.Arrays.Enumerated, Std.Arrays.Most, Std.Arrays.Partitioned, Std.Arrays.Tail;
5+
import Std.Convert.IntAsDouble;
6+
import Std.Diagnostics.Fact;
7+
import Std.Math.BitSizeI, Std.Math.Floor, Std.Math.Lg, Std.Math.MaxI, Std.Math.MinI;
8+
9+
/// # Summary
10+
/// Applies a Z-rotation (`Rz`) with given angle to each qubit in qs.
11+
///
12+
/// # Description
13+
/// This implementation is based on Hamming-weight phasing to reduce the number
14+
/// of rotation gates. The technique was first presented in [1], and further
15+
/// improved in [2] based on results in [3, 4]. Note, that the reduction of
16+
/// rotation gates comes at a cost of additional qubits and additional quantum
17+
/// operations to compute the Hamming-weight.
18+
///
19+
/// # Reference
20+
/// - [1](https://arxiv.org/abs/1709.06648) "Halving the cost of quantum
21+
/// addition", Craig Gidney.
22+
/// - [2](https://arxiv.org/abs/2012.09238) "Early fault-tolerant simulations of
23+
/// the Hubbard model", Earl T. Campbell.
24+
/// - [3](https://cpsc.yale.edu/sites/default/files/files/tr1260.pdf) "The exact
25+
/// multiplicative complexity of the Hamming weight function", Joan Boyar and
26+
/// René Peralta.
27+
/// - [4](https://arxiv.org/abs/1908.01609) "The role of multiplicative
28+
/// complexity in compiling low T-count oracle circuits", Giulia Meuli,
29+
/// Mathias Soeken, Earl Campbell, Martin Roetteler, Giovanni De Micheli.
30+
operation HammingWeightPhasing(angle : Double, qs : Qubit[]) : Unit {
31+
WithHammingWeight(qs, (sum) => {
32+
for (i, sumQubit) in Enumerated(sum) {
33+
Rz(IntAsDouble(2^i) * angle, sumQubit);
34+
}
35+
});
36+
}
37+
38+
internal operation WithHammingWeight(qs : Qubit[], action : Qubit[] => Unit) : Unit {
39+
let n = Length(qs);
40+
41+
if n <= 1 {
42+
action(qs);
43+
} elif n == 2 {
44+
use sum = Qubit();
45+
46+
within {
47+
AND(qs[0], qs[1], sum);
48+
CNOT(qs[0], qs[1]);
49+
} apply {
50+
action([qs[1], sum]);
51+
}
52+
} elif n == 3 {
53+
WithSum(qs[0], qs[1..1], qs[2..2], action);
54+
} else {
55+
let splitSize = 2^(BitSizeI(n - 1) - 1);
56+
let (leftLen, rightLen) = (n - splitSize, splitSize - 1);
57+
// handle corner case if n is power of 2; in that case the first
58+
// partition is longer than the second one, and we want to avoid that.
59+
let split = Partitioned([MinI(leftLen, rightLen), MaxI(leftLen, rightLen)], qs);
60+
Fact(Length(split) == 3 and Length(split[2]) == 1, $"Unexpected split for n = {n}");
61+
62+
WithHammingWeight(split[0], (leftHW) => {
63+
WithHammingWeight(split[1], (rightHW) => {
64+
WithSum(split[2][0], leftHW, rightHW, action);
65+
});
66+
});
67+
}
68+
}
69+
70+
internal operation Carry(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubit) : Unit is Adj {
71+
CNOT(carryIn, x);
72+
CNOT(carryIn, y);
73+
AND(x, y, carryOut);
74+
CNOT(carryIn, x);
75+
CNOT(x, y);
76+
CNOT(carryIn, carryOut);
77+
}
78+
79+
internal operation WithSum(carry : Qubit, xs : Qubit[], ys : Qubit[], action : Qubit[] => Unit) : Unit {
80+
let n = Length(ys);
81+
Fact(Length(xs) <= n, "Length of xs must be less or equal to length of ys");
82+
Fact(n > 0, "Length must be at least 1");
83+
84+
use carryOut = Qubit[n];
85+
let carryIn = [carry] + Most(carryOut);
86+
87+
within {
88+
for i in 0..n-1 {
89+
if i < Length(xs) {
90+
Carry(carryIn[i], xs[i], ys[i], carryOut[i]);
91+
} else {
92+
// there is no corresponding bit in xs; this is a version of
93+
// Carry in which x == 0.
94+
CNOT(carryIn[i], ys[i]);
95+
AND(carryIn[i], ys[i], carryOut[i]);
96+
CNOT(carryIn[i], carryOut[i]);
97+
}
98+
}
99+
} apply {
100+
action(ys + [Tail(carryOut)]);
101+
}
102+
}

library/rotations/src/Main.qs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
export HammingWeightPhasing.HammingWeightPhasing;

library/rotations/src/Tests.qs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import Std.Diagnostics.CheckOperationsAreEqual, Std.Diagnostics.Fact;
5+
import Std.Math.HammingWeightI, Std.Math.PI;
6+
7+
import HammingWeightPhasing.HammingWeightPhasing, HammingWeightPhasing.WithHammingWeight;
8+
9+
operation Main() : Unit {
10+
TestHammingWeight();
11+
TestPhasing();
12+
}
13+
14+
operation TestHammingWeight() : Unit {
15+
// exhaustive
16+
use qs = Qubit[4];
17+
18+
for x in 0..2^Length(qs) - 1 {
19+
ApplyXorInPlace(x, qs);
20+
WithHammingWeight(qs, sum => {
21+
Fact(MeasureInteger(sum) == HammingWeightI(x), $"wrong Hamming weight computed for x = {x}");
22+
});
23+
ResetAll(qs);
24+
}
25+
26+
// some explicit cases
27+
for (width, number) in [
28+
(1, 1),
29+
(2, 0),
30+
(2, 3),
31+
(8, 10),
32+
(7, 99)
33+
] {
34+
use qs = Qubit[width];
35+
36+
ApplyXorInPlace(number, qs);
37+
WithHammingWeight(qs, sum => {
38+
Fact(MeasureInteger(sum) == HammingWeightI(number), $"wrong Hamming weight computed for number = {number}");
39+
});
40+
ResetAll(qs);
41+
}
42+
}
43+
44+
operation TestPhasing() : Unit {
45+
for theta in [1.0, 2.0, 0.0, -0.5, 5.0 * PI()] {
46+
for numQubits in 1..6 {
47+
Fact(CheckOperationsAreEqual(numQubits, qs => HammingWeightPhasing(theta, qs), qs => ApplyToEachA(Rz(theta, _), qs)), "Operations are not equal");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)