Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
90e5163
Add Q# library for Arithmetic
fedimser Jul 23, 2026
ee4b7cf
add library tests to integration tests
fedimser Jul 23, 2026
04aff6b
remove tests from pyright config
fedimser Jul 23, 2026
9edcd90
Prevent divergent types from flowing out of loop and conjugate exprs …
swernli Jul 23, 2026
3fef601
Add outFiles to launch.json (#3506)
amcasey Jul 23, 2026
f97b3e1
update some references
fedimser Jul 23, 2026
d5be0d0
add trailing newlines
fedimser Jul 23, 2026
117b095
update references
fedimser Jul 23, 2026
a34c25b
add trailing newline
fedimser Jul 23, 2026
78950d2
Q# format
fedimser Jul 23, 2026
d537df3
update some comments
fedimser Jul 23, 2026
bec5264
update comments
fedimser Jul 23, 2026
9ee664f
Allow blocks in interpolated string expressions (#3503)
HABER7789 Jul 23, 2026
268e445
[Stim] Optimize noise intrinsic emission and support more noise instr…
joao-boechat Jul 24, 2026
b5ad165
Bump linkify-it from 5.0.1 to 5.0.2 (#3513)
dependabot[bot] Jul 24, 2026
7d8a8a6
[STIM] SELECT block improvements + support for REPEAT (#3499)
joao-boechat Jul 24, 2026
2b113cf
Bump dompurify from 3.4.11 to 3.4.12 (#3514)
dependabot[bot] Jul 24, 2026
88eddf6
Bump postcss from 8.5.12 to 8.5.23 in /source/jupyterlab (#3515)
dependabot[bot] Jul 24, 2026
0529b5a
Bump fast-uri from 3.1.2 to 3.1.4 in /source/jupyterlab (#3516)
dependabot[bot] Jul 24, 2026
51f714b
Fix a bug in Compiler::new (#3509)
fedimser Jul 24, 2026
1bd096a
Support qdk_config in qsharp.init (and rename qsharp_config to qdk_co…
fedimser Jul 24, 2026
b6217a7
Revert "Bump postcss from 8.5.12 to 8.5.23 in /source/jupyterlab (#35…
swernli Jul 24, 2026
e9fe064
Update grammar file (#3508)
ScottCarda-MS Jul 24, 2026
1b4e402
npm fixes (#3519)
billti Jul 24, 2026
9bf903b
Add Q# library for Arithmetic
fedimser Jul 23, 2026
7b5ffce
add library tests to integration tests
fedimser Jul 23, 2026
71f33aa
remove tests from pyright config
fedimser Jul 23, 2026
b4a7694
update some references
fedimser Jul 23, 2026
31cc879
add trailing newlines
fedimser Jul 23, 2026
40a4788
update references
fedimser Jul 23, 2026
2a0e628
add trailing newline
fedimser Jul 23, 2026
32ac1a4
Q# format
fedimser Jul 23, 2026
c5252ec
update some comments
fedimser Jul 23, 2026
706a681
update comments
fedimser Jul 23, 2026
58c837b
add reference to registry
fedimser Jul 23, 2026
cfadaf4
flatten files
fedimser Jul 23, 2026
ed6f1b5
rename optimization config
fedimser Jul 27, 2026
e204291
Merge branch 'fedimser/arith-lib' of github.com:microsoft/qdk into fe…
fedimser Jul 27, 2026
786d576
remove old files
fedimser Jul 27, 2026
510a85d
prettier fix
fedimser Jul 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
min-release-age=7
12 changes: 8 additions & 4 deletions .vscode/launch.shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"--profile=dev",
"--extensionDevelopmentPath=${workspaceFolder}/source/vscode",
"${workspaceFolder}/samples/"
]
],
"outFiles": ["${workspaceFolder}/source/vscode/out/**/*.js"]
},
{
// Launches the extension in "web extension" mode (like vscode.dev).
Expand All @@ -24,7 +25,8 @@
"--extensionDevelopmentPath=${workspaceFolder}/source/vscode",
"--extensionDevelopmentKind=web",
"${workspaceFolder}/samples/"
]
],
"outFiles": ["${workspaceFolder}/source/vscode/out/**/*.js"]
},
{
// Use when developing in WSL.
Expand All @@ -40,7 +42,8 @@
"--remote=wsl+${env:WSL_DISTRO_NAME}",
"--extensionDevelopmentPath=${workspaceFolder}/source/vscode",
"${workspaceFolder}/samples/"
]
],
"outFiles": ["${workspaceFolder}/source/vscode/out/**/*.js"]
},
{
// Use when developing in Codespaces.
Expand All @@ -57,7 +60,8 @@
"--remote=codespaces+${env:CODESPACE_NAME}",
"--extensionDevelopmentPath=${workspaceFolder}/source/vscode",
"${workspaceFolder}/samples/"
]
],
"outFiles": ["${workspaceFolder}/source/vscode/out/**/*.js"]
}
]
}
6 changes: 5 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,11 @@ def run_python_tests(cwd, interpreter, pip_env):
def run_python_integration_tests(cwd, interpreter):
# don't check to see if pip succeeds. We'll see if the import works later.
# If it doesn't, we'll skip the tests.
command_args = [interpreter, "-m", "pytest"]
paths = [
cwd,
os.path.join(root_dir, "library", "arithmetic", "test"),
]
command_args = [interpreter, "-m", "pytest", *paths]
subprocess.run(command_args, check=True, text=True, cwd=cwd)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ namespace Kata.Verification {
return false;
}

Message($"Correct.");
ResetAll([qAlice1, qAlice2, qBob1, qBob2]);
return true;
}

Message("Correct.");
return true;
}

@EntryPoint()
Expand Down
38 changes: 38 additions & 0 deletions library/arithmetic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Arithmetic

This library contains advanced quantum arithmetic algorithms.

Unless otherwise noted, all quantum inputs are interpreted as unsigned little-endian
integers and represented by `Qubit[]`.

The library contains the following algorithms:
* `Add.qs` - quantum-quantum in-place addition and subtraction (modulo `2^n`).
* `AddConst.qs` - quantum-classical addition (modulo `2^n`).
* `AddLookup.qs` - computes `x += table[i]` where `x`, `i` are quantum registers and
`table` is a classical table. Supports modular and non-modular addition.
* `Compare.qs` - compares two unsigned quantum integers, writing the result to an output
qubit. Supports inequality and equality.
* Modular arithmetic:
* `Modular/ModAdd.qs` - quantum-quantum in-place addition modulo classical constant.
* `Modular/ModDiv.qs` - division of two quantum numbers modulo classical constant.
This is based on the extended Euclidean algorithm, so some restrictions apply (in
particular, the divisor must be mutually prime with the modulus). It can also be used
for modular multiplication and modular inversion.
* `Modular/ModMul.qs` - modular multiplication and square.
* `Modular/ModNegate.qs` - modular negation.
* `Modular/WindowModExp.qs` - modular exponentiation (computes `t:=(t*b^x)%m` where
`t`, `x` are quantum and `b`, `m` are classical).

### Space-optimized and time-optimized variants

Some algorithms (addition, constant addition, comparison) are implemented with two
different circuit variants: space-optimized (minimizing the number of qubits used) and
time-optimized (minimizing the number of certain gates). The variants are functionally
equivalent but have different resource usage.

To select a variant, use the Q# configuration `"optimize"` with the value `"space"` or
`"time"`. For example, when creating a QDK Context using the Python API:
`qdk.Context(..., qdk_config={"optimize": "space"})`.

The variant used by default is unspecified (but it is usually the space-optimized one).

19 changes: 19 additions & 0 deletions library/arithmetic/qsharp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"license": "MIT",
"author": "Microsoft Quantum",
"files": [
"src/Add.qs",
"src/AddConst.qs",
"src/AddLookup.qs",
"src/ClassicalMath.qs",
"src/Compare.qs",
"src/ModAdd.qs",
"src/ModDiv.qs",
"src/ModExp.qs",
"src/ModMul.qs",
"src/ModNegate.qs",
"src/MultiControl.qs",
"src/ResourceEstimation.qs",
"src/Utils.qs"
]
}
40 changes: 40 additions & 0 deletions library/arithmetic/src/Add.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import Std.Arithmetic.RippleCarryCGIncByLE;
import Std.Arithmetic.RippleCarryTTKIncByLE;

/// This file re-exports addition algorithms from Std.Arithmetic.

/// Computes y += x (mod 2^n).
operation Add(x : Qubit[], y : Qubit[]) : Unit is Ctl + Adj {
body (...) {
let optimize = Std.Core.ConfigValue("optimize", "");
if (optimize == "space") {
RippleCarryTTKIncByLE(x, y);
} elif (optimize == "time") {
RippleCarryCGIncByLE(x, y);
} else {
RippleCarryTTKIncByLE(x, y);
}
}
controlled (controls, ...) {
let optimize = Std.Core.ConfigValue("optimize", "");
if (Length(controls) == 0) {
Add(x, y);
} elif (optimize == "space") {
Controlled RippleCarryTTKIncByLE(controls, (x, y));
} elif (optimize == "time") {
Controlled RippleCarryCGIncByLE(controls, (x, y));
} else {
Controlled RippleCarryTTKIncByLE(controls, (x, y));
}
}
}

/// Computes y -= x (mod 2^n).
operation Subtract(x : Qubit[], y : Qubit[]) : Unit is Ctl + Adj {
Adjoint Add(x, y);
}

export Add, Subtract;
121 changes: 121 additions & 0 deletions library/arithmetic/src/AddConst.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import Std.Arithmetic.RippleCarryCGAddLE;
import Std.Arithmetic.RippleCarryCGIncByLE;
import Std.Convert.BigIntAsBoolArray;
import Std.Math.TrailingZeroCountL;

import ClassicalMath.SafeMod;

/// # Summary
/// Computes `input := (input + constant) % 2^Length(input)`.
///
/// # Reference
/// - [1](https://arxiv.org/pdf/2007.07391) "Compilation of Fault-Tolerant Quantum
/// Heuristics for Combinatorial Optimization", Sanders et al. (Fig. 18).
///
/// # Resources
/// Uses n-1 auxiliary qubits and 2n-2 Toffoli gates.
///
/// # Input
/// ## constant
/// Classical constant to add.
/// ## inp
/// Target register updated in place.
operation AddConstantSanders(constant : BigInt, inp : Qubit[]) : Unit is Adj + Ctl {
body (...) {
Controlled AddConstantSanders([], (constant, inp));
}
controlled (ctrl, ...) {
let n = Length(inp);
let constant_bits = BigIntAsBoolArray(constant, n);

if n == 1 {
// Base case: single qubit addition.
if (constant_bits[0]) { Controlled X(ctrl, (inp[0])); }
} else {
use ancillas = Qubit[n - 1];

if (constant_bits[0]) { CNOT(inp[0], ancillas[0]); }

for i in 1..n - 2 {
let j = i - 1;
Controlled CNOT(ctrl, (ancillas[j], inp[i]));
within {
if (constant_bits[i]) { X(ancillas[j]); }
} apply {
AND(ancillas[j], inp[i], ancillas[i]);
}
CNOT(ancillas[j], ancillas[i]);
}

Controlled CNOT(ctrl, (ancillas[n - 2], inp[n - 1]));

for i in n - 2..-1..1 {
let j = i - 1;
CNOT(ancillas[j], ancillas[i]);
within {
if (constant_bits[i]) { X(ancillas[j]); }
} apply {
Adjoint AND(ancillas[j], inp[i], ancillas[i]);
}
}

if (constant_bits[0]) { CNOT(inp[0], ancillas[0]); }

for i in 0..n - 1 {
if (constant_bits[i]) { Controlled X(ctrl, (inp[i])); }
}
}
}
}

/// # Summary
/// Constant adder using the Gidney ripple-carry adder.
///
/// # Reference
/// - [1](https://arxiv.org/abs/1709.06648) "Halving the cost of quantum addition",
/// Craig Gidney.
operation AddConstantUsingCGAdd(constant : BigInt, input : Qubit[]) : Unit is Adj + Ctl {
body (...) {
Controlled AddConstantUsingCGAdd([], (constant, input));
}
controlled (ctrl, ...) {
use anc = Qubit[Length(input)];
within {
Controlled ApplyXorInPlaceL(ctrl, (constant, anc));
} apply {
RippleCarryCGIncByLE(anc, input);
}
}
}

/// # Summary
/// Computes `input := (input + constant) % 2^Length(input)`.
///
/// # Input
/// ## constant
/// Classical constant to add.
/// ## input
/// Target register updated in place.
operation AddConstant(constant : BigInt, input : Qubit[]) : Unit is Adj + Ctl {
body (...) {
Controlled AddConstant([], (constant, input));
}
controlled (ctrl, ...) {
let n = Length(input);
let constant = SafeMod(constant, 1L <<< n);
if (constant != 0L) {
let tz = TrailingZeroCountL(constant);
let optimize = Std.Core.ConfigValue("optimize", "");
if (optimize == "time" and Length(ctrl) > 0) {
Controlled AddConstantUsingCGAdd(ctrl, (constant >>> tz, input[tz...]));
} else {
Controlled AddConstantSanders(ctrl, (constant >>> tz, input[tz...]));
}
}
}
}

export AddConstantSanders, AddConstant;
Loading
Loading