Skip to content

Commit

Permalink
Merge branch 'main' into LessThanGateWriteup
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri authored Sep 25, 2023
2 parents d325ac7 + 9bb70e7 commit d798ee6
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 347 deletions.
3 changes: 1 addition & 2 deletions qualtran/_infra/composite_bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,7 @@ def add(self, bloq: Bloq, **in_soqs: SoquetInT) -> Union[None, SoquetT, Tuple[So
unpacking. In this final case, the ordering is according to `bloq.signature`
and irrespective of the order of `**in_soqs`.
"""
binst = BloqInstance(bloq, i=self._new_binst_i())
outs = tuple(soq for _, soq in self._add_binst(binst, in_soqs=in_soqs))
outs = self.add_t(bloq, **in_soqs)
if len(outs) == 0:
return None
if len(outs) == 1:
Expand Down
80 changes: 1 addition & 79 deletions qualtran/bloqs/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from functools import cached_property
from typing import Dict, Tuple, Union

import cirq
from attrs import frozen
from cirq_ft import LessThanEqualGate as CirqLessThanEqual
from cirq_ft import LessThanGate as CirqLessThanGate
from cirq_ft import TComplexity

from qualtran import Bloq, CompositeBloq, Register, Signature
from qualtran.cirq_interop import CirqQuregT, decompose_from_cirq_op
from qualtran import Bloq, Register, Signature


@frozen
Expand Down Expand Up @@ -219,74 +212,3 @@ def t_complexity(self):
# See: https://github.com/quantumlib/cirq-qubitization/issues/219
# See: https://github.com/quantumlib/cirq-qubitization/issues/217
return TComplexity(t=8 * self.bitsize)


@frozen
class LessThanEqual(Bloq):
r"""Implements $U|x,y,z\rangle = |x, y, z \oplus {x \le y}\rangle$.
Args:
x_bitsize: bitsize of x register.
y_bitsize: bitsize of y register.
Registers:
- x, y: Registers to compare against eachother.
- z: Register to hold result of comparison.
"""

x_bitsize: int
y_bitsize: int

@cached_property
def signature(self) -> Signature:
return Signature(
[
Register("x", bitsize=self.x_bitsize),
Register("y", bitsize=self.y_bitsize),
Register("z", bitsize=1),
]
)

def decompose_bloq(self) -> 'CompositeBloq':
return decompose_from_cirq_op(self)

def as_cirq_op(
self, qubit_manager: 'cirq.QubitManager', **cirq_quregs: 'CirqQuregT'
) -> Tuple[Union['cirq.Operation', None], Dict[str, 'CirqQuregT']]:
less_than = CirqLessThanEqual(x_bitsize=self.x_bitsize, y_bitsize=self.y_bitsize)
x = cirq_quregs['x']
y = cirq_quregs['y']
z = cirq_quregs['z']
return (less_than.on(*x, *y, *z), cirq_quregs)


@frozen
class LessThanConstant(Bloq):
r"""Implements $U_a|x\rangle = U_a|x\rangle|z\rangle = |x\rangle |z ^ (x < a)\rangle"
Args:
bitsize: bitsize of x register.
val: integer to compare x against (a above.)
Registers:
- x: Registers to compare against val.
- z: Register to hold result of comparison.
"""

bitsize: int
val: int

@cached_property
def signature(self) -> Signature:
return Signature.build(x=self.bitsize, z=1)

def decompose_bloq(self) -> 'CompositeBloq':
return decompose_from_cirq_op(self)

def as_cirq_op(
self, qubit_manager: 'cirq.QubitManager', **cirq_quregs: 'CirqQuregT'
) -> Tuple[Union['cirq.Operation', None], Dict[str, 'CirqQuregT']]:
less_than = CirqLessThanGate(bitsize=self.bitsize, less_than_val=self.val)
x = cirq_quregs['x']
z = cirq_quregs['z']
return (less_than.on(*x, *z), cirq_quregs)
29 changes: 1 addition & 28 deletions qualtran/bloqs/arithmetic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from cirq_ft.algos import LessThanEqualGate as CirqLessThanEquals
from cirq_ft.algos import LessThanGate as CirqLessThanConstant
from cirq_ft.infra import t_complexity

import qualtran.testing as qlt_testing
from qualtran import BloqBuilder, Register
from qualtran.bloqs.arithmetic import (
Add,
GreaterThan,
LessThanConstant,
LessThanEqual,
Product,
Square,
SumOfSquares,
)
from qualtran.bloqs.arithmetic import Add, GreaterThan, Product, Square, SumOfSquares
from qualtran.testing import execute_notebook


Expand Down Expand Up @@ -109,19 +96,5 @@ def test_greater_than():
cbloq = bb.finalize(a=q0, b=q1, result=anc)


def test_less_than_equal():
lte = LessThanEqual(5, 5)
qlt_testing.assert_valid_bloq_decomposition(lte)
cirq_lte = CirqLessThanEquals(5, 5)
assert lte.decompose_bloq().t_complexity() == t_complexity(cirq_lte)


def test_less_than_constant():
ltc = LessThanConstant(5, 7)
qlt_testing.assert_valid_bloq_decomposition(ltc)
cirq_ltc = CirqLessThanConstant(5, 7)
assert ltc.decompose_bloq().t_complexity() == t_complexity(cirq_ltc)


def test_notebook():
execute_notebook('arithmetic')
87 changes: 0 additions & 87 deletions qualtran/bloqs/qrom.py

This file was deleted.

54 changes: 0 additions & 54 deletions qualtran/bloqs/qrom_test.py

This file was deleted.

9 changes: 4 additions & 5 deletions qualtran/bloqs/swap_network_cirq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ def test_swap_with_zero_gate(selection_bitsize, target_bitsize, n_target_registe
# Allocate selection and target qubits.
all_qubits = cirq.LineQubit.range(cirq.num_qubits(gate))
selection = all_qubits[:selection_bitsize]
targets = {
f'targets_{i}': all_qubits[st : st + target_bitsize]
for i, st in enumerate(range(selection_bitsize, len(all_qubits), target_bitsize))
}
targets = np.asarray(all_qubits[selection_bitsize:]).reshape(
(n_target_registers, target_bitsize)
)
# Create a circuit.
circuit = cirq.Circuit(gate.on_registers(selection=selection, **targets))
circuit = cirq.Circuit(gate.on_registers(selection=selection, targets=targets))

# Load data[i] in i'th target register; where each register is of size target_bitsize
data = [random.randint(0, 2**target_bitsize - 1) for _ in range(n_target_registers)]
Expand Down
Loading

0 comments on commit d798ee6

Please sign in to comment.