-
Notifications
You must be signed in to change notification settings - Fork 179
Simple and consistent arithmetic API #423
Description
Simple and consistent arithmetic API
Abstract and conceptual overview
The proposal introduces
- simple names for common arithmetic operations
- additional operations where currently only one is available (e.g., additional comparison operations)
- optimized implementations for
T-count (all operations make use ofApplyAndwhen possible) - optimizations based on measurement-based uncomputation
Proposal
All operations are in the Microsoft.Quantum.Arithmetic namespace.
New functions and operations
-
Addition
-
operation Add(x : LittleEndian, y : LittleEndian) : Unit is Adj+Ctl -
operation AddWithCarryIn(x : LittleEndian, y : LittleEndian, carryIn : Qubit) : Unit is Adj+CtlBoth operations cover without carry-out (
Length(x!) == Length(y!)) as well as with carry-out (Length(x!) + 1 == Length(y!)) -
operation AddConstant(c : BigInt, y : LittleEndian) : Unit is Adj+Ctl
-
-
Subtraction
-
operation Subtract(x : LittleEndian, y : LittleEndian) : Unit is Ctl+AdjThe operation covers without carry-out (
Length(x!) == Length(y!)) as well as with carry-out (Length(x!) + 1 == Length(y!))
-
-
Comparison
-
operation GreaterThanOrEqual(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl -
operation GreaterThan(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+CtlThis API already exists, but would be replaced by new implementation.
-
operation LessThanOrEqual(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl -
operation LessThan(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl -
operation GreaterThanOrEqualConstant(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl -
operation GreaterThanConstant(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl -
operation LessThanOrEqualConstant(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl -
operation LessThanConstant(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl -
There are special variants for when it's guaranteed that the
targetqubit
is released in stateZero, making use of measurement-based uncomputation.
(all these variants can be emulated with the general variants above when
applied inToffoliSimulator):operation GreaterThanOrEqualWithZeroOutput(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+Ctloperation GreaterThanWithZeroOutput(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+Ctloperation LessThanOrEqualWithZeroOutput(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+Ctloperation LessThanWithZeroOutput(x : LittleEndian, y : LittleEndian, target : Qubit) : Unit is Adj+Ctloperation GreaterThanOrEqualConstantWithZeroOutput(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctloperation GreaterThanConstantWithZeroOutput(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctloperation LessThanOrEqualConstantWithZeroOutput(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctloperation LessThanConstantWithZeroOutput(c : BigInt, y : LittleEndian, target : Qubit) : Unit is Adj+Ctl
-
-
Modular arithmetic
operation ModularAdd(modulus : BigInt, x : LittleEndian, y : LittleEndian) : Unit is Adjoperation ModularAddConstant(modulus : BigInt, c : BigInt, y : LittleEndian) : Unit is Adj
Deprecations
CompareUsingRippleCarryin favor ofGreaterThanGreaterThanin favor ofGreaterThan(same name, new implementation)CompareGTIin favor ofGreaterThan- delete
Carry(should have beeninternal) - delete
Sum(should have beeninternal) RippleCarryAdderDin favor ofAddAddIin favor ofAdd- rename
RippleCarryAdderCDKMtoLowWidthAdd - rename
RippleCarryAdderTTKtoNoWidthAdd(distinguish by register length) - rename
RippleCarryAdderNoCarryTTKtoNoWidthAdd(distinguish by register length)
Considerations regarding Q# runtime
- If we were tracking whether qubits are in state
Zero(e.g., after each call toAssertAllZeroorAssertQubituntil the qubit is used again),
one could automatically use the*WithZeroOutputvariants and only use the general variants in the user source code.
Relationship to Q# language feature proposals
- Using a
Maybetype (see Discriminated Unions qsharp-language#51) to unifyAddandAddWithCarryIn(carry-in can then be declared optional)
Open design questions and considerations
- Consider adjusting names with respect to proposal Enhancements to UDTs for representing numeric data in quantum registers #337
- Order of arguments in comparison with constant operations is unnatural (constant first), but necessary due to style guide (qubit register last)