Subtraction#
from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register
from qualtran import QBit, QInt, QUInt, QAny
from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma
from typing import *
import numpy as np
import sympy
import cirq
Subtract
#
An n-bit subtraction gate.
Implements \(U|a\rangle|b\rangle \rightarrow |a\rangle|a-b\rangle\) using \(4n - 4\) T gates.
This construction uses the relation a - b = ~(~a + b)
to turn the operation into addition. This relation is used in
Compilation of Fault-Tolerant Quantum Heuristics for Combinatorial Optimization
to turn addition into subtraction conditioned on a qubit.
Parameters#
a_dtype
: Quantum datatype used to represent the integer a.b_dtype
: Quantum datatype used to represent the integer b. Must be large enough to hold the result in the output register of a - b, or else it simply drops the most significant bits. If not specified, b_dtype is set to a_dtype.
Registers#
a
: A a_dtype.bitsize-sized input register (register a above).b
: A b_dtype.bitsize-sized input/output register (register b above).
References#
from qualtran.bloqs.arithmetic import Subtract
Example Instances#
n = sympy.Symbol('n')
sub_symb = Subtract(QInt(bitsize=n))
sub_small = Subtract(QInt(bitsize=4))
sub_large = Subtract(QInt(bitsize=64))
sub_diff_size_regs = Subtract(QInt(bitsize=4), QInt(bitsize=16))
n = sympy.Symbol('n')
sub_symp_decomposition = Subtract(QInt(bitsize=n)).decompose_bloq()
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([sub_symb, sub_small, sub_large, sub_diff_size_regs, sub_symp_decomposition],
['`sub_symb`', '`sub_small`', '`sub_large`', '`sub_diff_size_regs`', '`sub_symp_decomposition`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
sub_symb_g, sub_symb_sigma = sub_symb.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(sub_symb_g)
show_counts_sigma(sub_symb_sigma)
Counts totals:
Add
: 1XGate⨂n
: 3
SubtractFrom
#
A version of Subtract
that subtracts the first register from the second in place.
Implements \(U|a
angle|b
angle
ightarrow |a
angle|b - a
angle\), essentially equivalent to
the statement b -= a
.
Parameters#
dtype
: Quantum datatype used to represent the integers a, b, and b - a.
Registers#
a
: A dtype.bitsize-sized input register (register a above).b
: A dtype.bitsize-sized input/output register (register b above).
from qualtran.bloqs.arithmetic import SubtractFrom
Example Instances#
n = sympy.Symbol('n')
sub_from_symb = SubtractFrom(QInt(bitsize=n))
sub_from_small = SubtractFrom(QInt(bitsize=4))
sub_from_large = SubtractFrom(QInt(bitsize=64))
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([sub_from_symb, sub_from_small, sub_from_large],
['`sub_from_symb`', '`sub_from_small`', '`sub_from_large`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
sub_from_symb_g, sub_from_symb_sigma = sub_from_symb.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(sub_from_symb_g)
show_counts_sigma(sub_from_symb_sigma)
Counts totals:
Add
: 1BitwiseNot
: 2