Skip to content

Commit

Permalink
JHHA2016: implement RotateRight
Browse files Browse the repository at this point in the history
  • Loading branch information
fedimser committed Dec 16, 2024
1 parent 3dc0b72 commit 9599486
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
1 change: 1 addition & 0 deletions qsharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"src/QuantumArithmetic/CDKM2004.qs",
"src/QuantumArithmetic/CT2002.qs",
"src/QuantumArithmetic/DM2004.qs",
"src/QuantumArithmetic/JHHA2016.qs",
"src/QuantumArithmetic/PG2012.qs",
"src/QuantumArithmetic/PG2012Test.qs",
"src/QuantumArithmetic/TMVH2019.qs",
Expand Down
15 changes: 6 additions & 9 deletions src/Main.qs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import TestUtils.BinaryOp;
import TestUtils.BinaryOpInPlace;
import TestUtils.BinaryOpInPlaceExtraOut;
import QuantumArithmetic.PG2012Test;
import TestUtils;
import QuantumArithmetic.JHHA2016;

// For debugging, run with Ctrl+F5,
operation Main() : Unit {
let n = 3;
let a = 5;
let b = 4;
//Message($"ans1={PG2012Test.TestFMAC_MOD2(n,a,a,7)}");
Message($"{a}^{b}={PG2012Test.TestEXP_MOD(n,a,b,7)}");
let n = 8;
let a = 196;
let ans = TestUtils.UnaryOpInPlace(n, a, JHHA2016.RotateRight);
Message($"ans={ans}");
}
35 changes: 35 additions & 0 deletions src/QuantumArithmetic/JHHA2016.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Implementation of the multiplier presented in paper:
/// Ancilla-Input and Garbage-Output Optimized Design of a Reversible Quantum Integer Multiplier
/// Jayashree HV, Himanshu Thapliyal, Hamid R. Arabnia, V K Agrawal, 2016.
/// https://arxiv.org/abs/1608.01228

import Std.Diagnostics.Fact;
import QuantumArithmetic.HigherRadixUtils.HigherRadix.HigherRadix;


// Computes P+=Am*B.
// Zcin must be prepared in zero state and is returned in zero state.
operation AddNop(P:Qubit[], B:Qubit[], Zcin:Qubit, Am:Qubit) : Unit is Adj + Ctl {

}

// Rotates right bits of P.
operation RotateRight(P: Qubit[]): Unit is Adj+Ctl {
let k: Int = Length(P);
let k1: Int = k/2;
for i in 0..k1-1 {
SWAP(P[i], P[k-1-i]);
}
for i in 0..k1-2+(k%2) {
SWAP(P[i], P[k-2-i]);
}
}

// Computes P+=A*B.
operation Multiply(A: Qubit[], B: Qubit[], P: Qubit[]) : Unit is Adj + Ctl {
let n : Int = Length(A);
Fact(Length(B) == n, "Register sizes must match.");
Fact(Length(P) == 2* n, "Register sizes must match.");


}
8 changes: 8 additions & 0 deletions src/TestUtils.qs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ operation BinaryOpExtraOut(n : Int, x_val : Int, y_val : Int, op : (Qubit[], Qub
let z = MeasureInteger([z_extra]);
let ans = MeasureInteger(results);
return ans;
}

// Computes op(x).
operation UnaryOpInPlace(n : Int, x_val : Int, op : (Qubit[]) => Unit) : Int {
use x = Qubit[n];
ApplyPauliFromInt(PauliX, true, x_val, x);
op(x);
return MeasureInteger(x);
}
18 changes: 18 additions & 0 deletions test/JHHA_2016_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from qsharp import init, eval
import random


@pytest.fixture(scope="session", autouse=True)
def setup():
init(project_root='.')


@pytest.mark.parametrize("n", [5, 8, 32, 62, 63])
def test_RotateRight(n: int):
op = "QuantumArithmetic.JHHA2016.RotateRight"
for _ in range(5):
x = random.randint(0, 2**n-1)
ans = eval(f"TestUtils.UnaryOpInPlace({n},{x},{op})")
expected = (x>>1) + ((x%2) << (n-1))
assert ans == expected

0 comments on commit 9599486

Please sign in to comment.