Multiplication#
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
PlusEqualProduct
#
Performs result += a * b.
Parameters#
a_bitsize
: bitsize of inputa
.b_bitsize
: bitsize of inputb
.result_bitsize
: bitsize of the output register.is_adjoint
: If true, performsresult -= a * b
instead. Defaults to False.
Registers#
a
: QUInt ofa_bitsize
bits.b
: QUInt ofb_bitsize
bits.result
: QUInt ofresult_bitsize
bits.
from qualtran.bloqs.arithmetic import PlusEqualProduct
Example Instances#
a_bit, b_bit, res_bit = 2, 2, 4
plus_equal_product = PlusEqualProduct(a_bit, b_bit, res_bit)
Graphical Signature#
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
plus_equal_product_g, plus_equal_product_sigma = plus_equal_product.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(plus_equal_product_g)
show_counts_sigma(plus_equal_product_sigma)
Counts totals:
T
: 32
Product
#
Compute the product of an n
and m
bit binary number.
Implements \(U|a\rangle|b\rangle|0\rangle \rightarrow |a\rangle|b\rangle|a\times b\rangle\) using \(2nm-n\) Toffolis.
Parameters#
a_bitsize
: Number of bits used to represent the first integer.b_bitsize
: Number of bits used to represent the second integer.
Registers#
a
: a_bitsize-sized input register.b
: b_bitsize-sized input register.result
: A 2*max(a_bitsize, b_bitsize)
bit-sized output register to store the result a*b.
References#
Fault-Tolerant Quantum Simulations of Chemistry in First Quantization. pg 81 gives a Toffoli complexity for multiplying two numbers.
from qualtran.bloqs.arithmetic import Product
Example Instances#
product = Product(a_bitsize=4, b_bitsize=6)
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([product],
['`product`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
product_g, product_sigma = product.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(product_g)
show_counts_sigma(product_sigma)
Counts totals:
Toffoli
: 42
Square
#
Square an n-bit binary number.
Implements \(U|a\rangle|0\rangle \rightarrow |a\rangle|a^2\rangle\) using \(n^2 - n\) Toffolis.
Parameters#
bitsize
: Number of bits used to represent the integer to be squared. The result is stored in a register of size 2*bitsize.
Registers#
a
: A bitsize-sized input register (register a above).result
: A 2-bitsize-sized input/output register.
References#
Fault-Tolerant Quantum Simulations of Chemistry in First Quantization. pg 76 for Toffoli complexity.
from qualtran.bloqs.arithmetic import Square
Example Instances#
square = Square(bitsize=8)
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([square],
['`square`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
square_g, square_sigma = square.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(square_g)
show_counts_sigma(square_sigma)
Counts totals:
Toffoli
: 56
SumOfSquares
#
Compute the sum of squares of k n-bit binary numbers.
Implements \(U|a\rangle|b\rangle\dots k\rangle|0\rangle \rightarrow |a\rangle|b\rangle\dots|k\rangle|a^2+b^2+\dots k^2\rangle\) using \(4 k n^2 T\) gates.
The number of bits required by the output register is 2*bitsize + ceil(log2(k)).
Parameters#
bitsize
: Number of bits used to represent each of the k integers.k
: The number of integers we want to square.
Registers#
input
: k n-bit registers.result
: 2 * bitsize + ceil(log2(k)) sized output register.
References#
Fault-Tolerant Quantum Simulations of Chemistry in First Quantization. pg 80 gives a Toffoli complexity for squaring.
from qualtran.bloqs.arithmetic import SumOfSquares
Example Instances#
sum_of_squares = SumOfSquares(bitsize=8, k=4)
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([sum_of_squares],
['`sum_of_squares`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
sum_of_squares_g, sum_of_squares_sigma = sum_of_squares.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(sum_of_squares_g)
show_counts_sigma(sum_of_squares_sigma)
Counts totals:
Toffoli
: 248
ScaleIntByReal
#
Scale an integer by fixed-point representation of a real number.
i.e.
The real number is assumed to be in the range [0, 1).
Parameters#
r_bitsize
: Number of bits used to represent the real number.i_bitsize
: Number of bits used to represent the integer.
Registers#
real_in
: r_bitsize-sized input fixed-point register.int_in
: i_bitsize-sized input register.result
: a r_bitsize sized output fixed-point register.
References#
from qualtran.bloqs.arithmetic import ScaleIntByReal
Example Instances#
scale_int_by_real = ScaleIntByReal(r_bitsize=12, i_bitsize=4)
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([scale_int_by_real],
['`scale_int_by_real`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
scale_int_by_real_g, scale_int_by_real_sigma = scale_int_by_real.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(scale_int_by_real_g)
show_counts_sigma(scale_int_by_real_sigma)
Counts totals:
Toffoli
: 68
MultiplyTwoReals
#
Multiply two fixed-point representations of real numbers
i.e.
The real numbers are assumed to be in the range [0, 1).
Parameters#
bitsize
: Number of bits used to represent the real number.
Registers#
a
: bitsize-sized input register.b
: bitsize-sized input register.result
: bitsize output register
References#
Compilation of Fault-Tolerant Quantum Heuristics for Combinatorial Optimization. Appendix D. Section 5. (p. 71).
from qualtran.bloqs.arithmetic import MultiplyTwoReals
Example Instances#
multiply_two_reals = MultiplyTwoReals(bitsize=10)
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([multiply_two_reals],
['`multiply_two_reals`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
multiply_two_reals_g, multiply_two_reals_sigma = multiply_two_reals.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(multiply_two_reals_g)
show_counts_sigma(multiply_two_reals_sigma)
Counts totals:
Toffoli
: 89
SquareRealNumber
#
Square a fixed-point representation of a real number
i.e.
The real numbers are assumed to be in the range [0, 1).
Parameters#
bitsize
: Number of bits used to represent the real number.
Registers#
a
: bitsize-sized input register.b
: bitsize-sized input register.result
: bitsize output register
References#
Compilation of Fault-Tolerant Quantum Heuristics for Combinatorial Optimization. Appendix D. Section 6. (p. 74).
from qualtran.bloqs.arithmetic import SquareRealNumber
Example Instances#
square_real_number = SquareRealNumber(bitsize=10)
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([square_real_number],
['`square_real_number`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
square_real_number_g, square_real_number_sigma = square_real_number.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(square_real_number_g)
show_counts_sigma(square_real_number_sigma)
Counts totals:
Toffoli
: 46
from qualtran.drawing import show_bloqs
show_bloqs([plus_equal_product],
['`plus_equal_product`'])
InvertRealNumber
#
Invert a fixed-point representation of a real number.
Implements the unitary:
Parameters#
bitsize
: Number of bits used to represent the number.num_frac
: Number of fraction bits in the number.
Registers#
a
:bitsize
-sized input register.result
:bitsize
-sized output register.References
:[Quantum Algorithms and Circuits for Scientific Computing](https
: //arxiv.org/pdf/1511.08253). Section 2.1.
from qualtran.bloqs.arithmetic import InvertRealNumber
Example Instances#
invert_real_number = InvertRealNumber(bitsize=10, num_frac=7)
Graphical Signature#
from qualtran.drawing import show_bloqs
show_bloqs([invert_real_number],
['`invert_real_number`'])
Call Graph#
from qualtran.resource_counting.generalizers import ignore_split_join
invert_real_number_g, invert_real_number_sigma = invert_real_number.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(invert_real_number_g)
show_counts_sigma(invert_real_number_sigma)
Counts totals:
CNOT
: 4C^3XGate
: 1MultiplyTwoReals
: 4ScaleIntByReal
: 4SquareRealNumber
: 4Subtract
: 4Toffoli
: 2XGate
: 1