Skip to content

Commit

Permalink
Implement decomposition for BitonicSort (#1089)
Browse files Browse the repository at this point in the history
* decomposition for `GreaterThan`

* sorting: implement comparator

* implement bitonic sort

* add classical simulation tests, fix bugs

* more random tests and bug fixes

* fix mypy

* fix serialization bug

* add all perm 4! test

* more assertions

* use `bitsize` instead of `L`

* link issue in TODOs

* add to resolverdict

* add tests for `ParallelComparators`, more docstring

* more docstring

* more docstrings and tests for merge

* add BloqDocSpec for `BitonicMerge` and `ParallelComparators`

* add ancilla cost to docstring

* allow decomposing when `bitsize` is symbolic

* update docstrings, fix latex

* cleanup

* docstring: explain compare ancilla
  • Loading branch information
anurudhp authored Jun 24, 2024
1 parent 7d14079 commit cc4e3bc
Show file tree
Hide file tree
Showing 7 changed files with 756 additions and 107 deletions.
2 changes: 2 additions & 0 deletions dev_tools/autogenerate-bloqs-notebooks-v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@
module=qualtran.bloqs.arithmetic.sorting,
bloq_specs=[
qualtran.bloqs.arithmetic.sorting._COMPARATOR_DOC,
qualtran.bloqs.arithmetic.sorting._PARALLEL_COMPARATORS_DOC,
qualtran.bloqs.arithmetic.sorting._BITONIC_MERGE_DOC,
qualtran.bloqs.arithmetic.sorting._BITONIC_SORT_DOC,
],
directory=f'{SOURCE_DIR}/bloqs/arithmetic/',
Expand Down
13 changes: 11 additions & 2 deletions qualtran/bloqs/arithmetic/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
Register,
Side,
Signature,
Soquet,
SoquetT,
)
from qualtran._infra.quantum_graph import Soquet
from qualtran.bloqs.basic_gates import CNOT, TGate, XGate
from qualtran.bloqs.mcmt.and_bloq import And, MultiAnd
from qualtran.bloqs.mcmt.multi_control_multi_target_pauli import MultiControlX
Expand Down Expand Up @@ -671,8 +671,17 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -
return TextBox("⨁(a > b)")
raise ValueError(f'Unknown register name {reg.name}')

def build_composite_bloq(
self, bb: 'BloqBuilder', a: 'Soquet', b: 'Soquet', target: 'Soquet'
) -> Dict[str, 'SoquetT']:
a, b, target = bb.add(
LessThanEqual(self.a_bitsize, self.b_bitsize), x=a, y=b, target=target
)
target = bb.add(XGate(), q=target)
return {'a': a, 'b': b, 'target': target}

def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
return {(LessThanEqual(self.a_bitsize, self.b_bitsize), 1)}
return {(LessThanEqual(self.a_bitsize, self.b_bitsize), 1), (XGate(), 1)}


@bloq_example
Expand Down
4 changes: 3 additions & 1 deletion qualtran/bloqs/arithmetic/comparison_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def test_greater_than_manual():
qlt_testing.assert_wire_symbols_match_expected(
GreaterThanConstant(bitsize, 17), ['In(x)', '⨁(x > 17)']
)
assert cbloq.t_complexity() == t_complexity(LessThanEqual(bitsize, bitsize))
assert cbloq.t_complexity() == (
t_complexity(LessThanEqual(bitsize, bitsize)) + TComplexity(clifford=1)
)


@pytest.mark.parametrize('bitsize', [1, 2, 5])
Expand Down
Loading

0 comments on commit cc4e3bc

Please sign in to comment.