Reference: Gidney Arithmetic Qubricks
Contents
- AddBase Class
- GidneyAdd Class
- NaiveAdd Class
- ConditionallyCleanAdd Class
- CuccaroAdd Class
- CuccaroDepthOptimisedAdd Class
- Square Class
- GidneySquare Class
- NaiveSquare Class
- MultiplyAdd Class
- GidneyMultiplyAdd Class
- NaiveMultiplyAdd Class
- CuccaroMultiplyAdd Class
- OptimizedGidneyMultiplyAdd Class
- OptimizedNaiveMultiplyAdd Class
- RSqrt Class
- GidneyLadder Class
AddBase Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.AddBase
AddBase(
add_engine=gidney_add_engine,
expand_dest_qreg: bool = False,
release_ancillae: bool = True,
**kwargs,
)
Bases: Qubrick
Basic boilerplate for adder.
compute
compute(
lhs: QType,
rhs: QType,
ctrl: Qubits | None = None,
*,
carry_in: bool = None,
subtract_condition: bool = False,
alloc_result: bool = False,
release_ancillae: bool = None,
condition: bool = None,
)
This computes (lhs += rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
rhs
|
Qubits or int
|
Right-hand operand |
required |
ctrl
|
Qubits
|
Quantum condition |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
alloc_result
|
bool
|
If True, compute into a new register |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
None
|
carry_in
|
Qubits or int or None
|
Additional value to add, if any |
None
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
GidneyAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.GidneyAdd
Bases: AddBase
Wrapper for Gidney Add.
compute
compute(
lhs: QType,
rhs: QType,
ctrl: Qubits | None = None,
*,
carry_in: bool = None,
subtract_condition: bool = False,
alloc_result: bool = False,
release_ancillae: bool = None,
condition: bool = None,
)
This computes (lhs += rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
rhs
|
Qubits or int
|
Right-hand operand |
required |
ctrl
|
Qubits
|
Quantum condition |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
alloc_result
|
bool
|
If True, compute into a new register |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
None
|
carry_in
|
Qubits or int or None
|
Additional value to add, if any |
None
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
NaiveAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.NaiveAdd
Bases: AddBase
Wrapper for Naive Add.
compute
compute(
lhs: QType,
rhs: QType,
ctrl: Qubits | None = None,
*,
carry_in: bool = None,
subtract_condition: bool = False,
alloc_result: bool = False,
release_ancillae: bool = None,
condition: bool = None,
)
This computes (lhs += rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
rhs
|
Qubits or int
|
Right-hand operand |
required |
ctrl
|
Qubits
|
Quantum condition |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
alloc_result
|
bool
|
If True, compute into a new register |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
None
|
carry_in
|
Qubits or int or None
|
Additional value to add, if any |
None
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
CuccaroAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.CuccaroAdd
Bases: AddBase
Wrapper for Cuccaro Add.
compute
compute(
lhs: QType,
rhs: QType,
ctrl: Qubits | None = None,
*,
carry_in: bool = None,
subtract_condition: bool = False,
alloc_result: bool = False,
release_ancillae: bool = None,
condition: bool = None,
)
This computes (lhs += rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
rhs
|
Qubits or int
|
Right-hand operand |
required |
ctrl
|
Qubits
|
Quantum condition |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
alloc_result
|
bool
|
If True, compute into a new register |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
None
|
carry_in
|
Qubits or int or None
|
Additional value to add, if any |
None
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
ConditionallyCleanAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.ConditionallyCleanAdd
Bases: AddBase
Wrapper for Conditionally Clean Add.
compute
compute(
lhs: QType,
rhs: QType,
ctrl: Qubits | None = None,
*,
carry_in: bool = None,
subtract_condition: bool = False,
alloc_result: bool = False,
release_ancillae: bool = None,
condition: bool = None,
)
This computes (lhs += rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
rhs
|
Qubits or int
|
Right-hand operand |
required |
ctrl
|
Qubits
|
Quantum condition |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
alloc_result
|
bool
|
If True, compute into a new register |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
None
|
carry_in
|
Qubits or int or None
|
Additional value to add, if any |
None
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
CuccaroDepthOptimisedAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.CuccaroDepthOptimisedAdd
Bases: AddBase
Wrapper for Cuccaro Depth Optimised Add.
compute
compute(
lhs: QType,
rhs: QType,
ctrl: Qubits | None = None,
*,
carry_in: bool = None,
subtract_condition: bool = False,
alloc_result: bool = False,
release_ancillae: bool = None,
condition: bool = None,
)
This computes (lhs += rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
rhs
|
Qubits or int
|
Right-hand operand |
required |
ctrl
|
Qubits
|
Quantum condition |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
alloc_result
|
bool
|
If True, compute into a new register |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
None
|
carry_in
|
Qubits or int or None
|
Additional value to add, if any |
None
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
Square Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.Square
Square(
add_engine=gidney_add_engine,
square_engine=square_engine,
allow_negative_input=None,
**kwargs,
)
Bases: Qubrick
Basic Gidney square.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
allow_negative_input
|
bool or None
|
Set to allow negative input values. If None, decide based on input params. |
None
|
compute
compute(
lhs,
ctrl=None,
dst=None,
alloc_result=None,
add_to_dest=None,
release_ancillae: bool = True,
condition=None,
) -> None
This computes (lhs * lhs) into a new register.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
ctrl
|
Qubits
|
Quantum ctrl |
None
|
dst
|
Qubits or None
|
If provided, ADD result into this reg |
None
|
alloc_result
|
None
|
Deprecated and will be removed in WB 5.0.0. To allocate a new result, pass None as dst. |
None
|
add_to_dest
|
None
|
If False or dst=None, just compute square assuming dst is zero. Otherwise, add to dst |
None
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
GidneySquare Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.GidneySquare
Bases: Square
Wrapper for Gidney Square.
compute
compute(
lhs,
ctrl=None,
dst=None,
alloc_result=None,
add_to_dest=None,
release_ancillae: bool = True,
condition=None,
) -> None
This computes (lhs * lhs) into a new register.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
ctrl
|
Qubits
|
Quantum ctrl |
None
|
dst
|
Qubits or None
|
If provided, ADD result into this reg |
None
|
alloc_result
|
None
|
Deprecated and will be removed in WB 5.0.0. To allocate a new result, pass None as dst. |
None
|
add_to_dest
|
None
|
If False or dst=None, just compute square assuming dst is zero. Otherwise, add to dst |
None
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
NaiveSquare Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.NaiveSquare
Bases: Square
Wrapper for Naive Square.
compute
compute(
lhs,
ctrl=None,
dst=None,
alloc_result=None,
add_to_dest=None,
release_ancillae: bool = True,
condition=None,
) -> None
This computes (lhs * lhs) into a new register.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
ctrl
|
Qubits
|
Quantum ctrl |
None
|
dst
|
Qubits or None
|
If provided, ADD result into this reg |
None
|
alloc_result
|
None
|
Deprecated and will be removed in WB 5.0.0. To allocate a new result, pass None as dst. |
None
|
add_to_dest
|
None
|
If False or dst=None, just compute square assuming dst is zero. Otherwise, add to dst |
None
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
MultiplyAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.MultiplyAdd
MultiplyAdd(
add_engine=gidney_add_engine,
mul_engine=naive_mul_engine,
allow_negative_input=None,
**kwargs,
)
Bases: Qubrick
Basic Gidney Multiply-Add.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
allow_negative_input
|
bool or None
|
Set to allow negative input values. If None, decide based on input params. |
None
|
compute
compute(
dst,
lhs,
rhs,
ctrl=None,
release_ancillae: bool = True,
condition=None,
subtract_condition: bool = False,
)
This computes dst + (lhs * rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst
|
Qubits or None
|
Destination to add into. If None, allocate a new register |
required |
lhs
|
Qubits
|
Left-hand multiply operand |
required |
rhs
|
Qubits
|
Right-hand multiply operand |
required |
ctrl
|
Qubits
|
Quantum control |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
GidneyMultiplyAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.GidneyMultiplyAdd
Bases: MultiplyAdd
Wrapper for Gidney Multiply-Add.
compute
compute(
dst,
lhs,
rhs,
ctrl=None,
release_ancillae: bool = True,
condition=None,
subtract_condition: bool = False,
)
This computes dst + (lhs * rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst
|
Qubits or None
|
Destination to add into. If None, allocate a new register |
required |
lhs
|
Qubits
|
Left-hand multiply operand |
required |
rhs
|
Qubits
|
Right-hand multiply operand |
required |
ctrl
|
Qubits
|
Quantum control |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
NaiveMultiplyAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.NaiveMultiplyAdd
Bases: MultiplyAdd
Wrapper for Naive Multiply-Add.
compute
compute(
dst,
lhs,
rhs,
ctrl=None,
release_ancillae: bool = True,
condition=None,
subtract_condition: bool = False,
)
This computes dst + (lhs * rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst
|
Qubits or None
|
Destination to add into. If None, allocate a new register |
required |
lhs
|
Qubits
|
Left-hand multiply operand |
required |
rhs
|
Qubits
|
Right-hand multiply operand |
required |
ctrl
|
Qubits
|
Quantum control |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
CuccaroMultiplyAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.CuccaroMultiplyAdd
Bases: MultiplyAdd
Wrapper for Cuccaro Multiply-Add.
compute
compute(
dst,
lhs,
rhs,
ctrl=None,
release_ancillae: bool = True,
condition=None,
subtract_condition: bool = False,
)
This computes dst + (lhs * rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst
|
Qubits or None
|
Destination to add into. If None, allocate a new register |
required |
lhs
|
Qubits
|
Left-hand multiply operand |
required |
rhs
|
Qubits
|
Right-hand multiply operand |
required |
ctrl
|
Qubits
|
Quantum control |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
OptimizedGidneyMultiplyAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.OptimizedGidneyMultiplyAdd
Bases: MultiplyAdd
Wrapper for Gidney Multiply-Add.
compute
compute(
dst,
lhs,
rhs,
ctrl=None,
release_ancillae: bool = True,
condition=None,
subtract_condition: bool = False,
)
This computes dst + (lhs * rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst
|
Qubits or None
|
Destination to add into. If None, allocate a new register |
required |
lhs
|
Qubits
|
Left-hand multiply operand |
required |
rhs
|
Qubits
|
Right-hand multiply operand |
required |
ctrl
|
Qubits
|
Quantum control |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
OptimizedNaiveMultiplyAdd Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.OptimizedNaiveMultiplyAdd
Bases: MultiplyAdd
Wrapper for Naive Multiply-Add.
compute
compute(
dst,
lhs,
rhs,
ctrl=None,
release_ancillae: bool = True,
condition=None,
subtract_condition: bool = False,
)
This computes dst + (lhs * rhs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst
|
Qubits or None
|
Destination to add into. If None, allocate a new register |
required |
lhs
|
Qubits
|
Left-hand multiply operand |
required |
rhs
|
Qubits
|
Right-hand multiply operand |
required |
ctrl
|
Qubits
|
Quantum control |
None
|
subtract_condition
|
Qubits or bool
|
If True (classically or quantumly), subtract instead of adding |
False
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Notes
release_ancillae allows us to trade off Toffoli cost vs. qubit cost:
- If True, then the ONLY effect remaining after compute() is the desired sum.
- If False, it uses fewer gates but rhs and the auxiliary qubits will be scrambled until uncompute.
RSqrt Class
psiqworkbench.qubricks.qbk_gidney_arithmetic.RSqrt
Bases: Qubrick
Fast reciprocal square root.
compute
This computes reg = rsqrt(reg) in-place, where the input is a positive integer and the output is a fixed-point fraction with all bits below the decimal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lhs
|
Qubits
|
Left-hand operand and destination |
required |
ctrl
|
Qubits
|
Quantum control |
None
|
release_ancillae
|
bool
|
If True, uncompute and release auxiliary qubits. If False, hold auxiliary qubits till uncompute. |
True
|
Note
For more details, see Built-in Qubricks tutorial