Skip to content

Commit

Permalink
Merge branch 'main' into 2024/SR/planted-noisy-kxor-algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
anurudhp committed Sep 1, 2024
2 parents 490afa9 + 64acb52 commit 8909a9c
Show file tree
Hide file tree
Showing 21 changed files with 367 additions and 194 deletions.
40 changes: 2 additions & 38 deletions qualtran/_infra/adjoint_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from functools import cached_property
from typing import cast, Dict, TYPE_CHECKING
from typing import cast

import pytest
import sympy

import qualtran.testing as qlt_testing
from qualtran import Adjoint, Bloq, CompositeBloq, Side, Signature
from qualtran import Adjoint, CompositeBloq, Side
from qualtran._infra.adjoint import _adjoint_cbloq
from qualtran.bloqs.basic_gates import CNOT, CSwap, ZeroState
from qualtran.bloqs.for_testing.atom import TestAtom
from qualtran.bloqs.for_testing.with_call_graph import TestBloqWithCallGraph
from qualtran.bloqs.for_testing.with_decomposition import TestParallelCombo, TestSerialCombo
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import LarrowTextBox, RarrowTextBox, Text

if TYPE_CHECKING:
from qualtran import BloqBuilder, SoquetT


def test_serial_combo_adjoint():
# The normal decomposition is three `TestAtom` tagged atom{0,1,2}.
Expand Down Expand Up @@ -168,37 +163,6 @@ def test_wire_symbol():
assert isinstance(adj_ws, RarrowTextBox)


class TAcceptsAdjoint(TestAtom):
def _t_complexity_(self, adjoint: bool = False) -> TComplexity:
return TComplexity(t=2 if adjoint else 1)


class TDoesNotAcceptAdjoint(TestAtom):
def _t_complexity_(self) -> TComplexity:
return TComplexity(t=3)


class DecomposesIntoTAcceptsAdjoint(Bloq):
@cached_property
def signature(self) -> Signature:
return Signature.build(q=1)

def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> Dict[str, 'SoquetT']:
soqs = bb.add_d(TAcceptsAdjoint(), **soqs)
return soqs


def test_t_complexity():
assert TAcceptsAdjoint().t_complexity().t == 1
assert Adjoint(TAcceptsAdjoint()).t_complexity().t == 2

assert DecomposesIntoTAcceptsAdjoint().t_complexity().t == 1
assert Adjoint(DecomposesIntoTAcceptsAdjoint()).t_complexity().t == 2

assert TDoesNotAcceptAdjoint().t_complexity().t == 3
assert Adjoint(TDoesNotAcceptAdjoint()).t_complexity().t == 3


@pytest.mark.notebook
def test_notebook():
qlt_testing.execute_notebook('../Adjoint')
2 changes: 0 additions & 2 deletions qualtran/bloqs/arithmetic/comparison_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ def test_clineardepthgreaterthan_classical_action_unsigned(ctrl, dtype, bitsize)
cb = b.decompose_bloq()
for c, target in itertools.product(range(2), repeat=2):
for (x, y) in itertools.product(range(2**bitsize), repeat=2):
print(f'{c=} {target=} {x=} {y=}')
assert b.call_classically(ctrl=c, a=x, b=y, target=target) == cb.call_classically(
ctrl=c, a=x, b=y, target=target
)
Expand All @@ -351,7 +350,6 @@ def test_clineardepthgreaterthan_classical_action_signed(ctrl, bitsize):
cb = b.decompose_bloq()
for c, target in itertools.product(range(2), repeat=2):
for (x, y) in itertools.product(range(-(2 ** (bitsize - 1)), 2 ** (bitsize - 1)), repeat=2):
print(f'{c=} {target=} {x=} {y=}')
assert b.call_classically(ctrl=c, a=x, b=y, target=target) == cb.call_classically(
ctrl=c, a=x, b=y, target=target
)
Expand Down
7 changes: 0 additions & 7 deletions qualtran/bloqs/arithmetic/controlled_addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from qualtran.bloqs.arithmetic.addition import Add
from qualtran.bloqs.bookkeeping import Cast
from qualtran.bloqs.mcmt.and_bloq import And
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.resource_counting.generalizers import ignore_split_join
from qualtran.simulation.classical_sim import add_ints

Expand Down Expand Up @@ -156,12 +155,6 @@ def build_composite_bloq(
ctrl = bb.join(np.array([ctrl_q]))
return {'ctrl': ctrl, 'a': a, 'b': b}

def _t_complexity_(self):
n = self.b_dtype.bitsize
num_and = self.a_dtype.bitsize + self.b_dtype.bitsize - 1
num_clifford = 33 * (n - 2) + 43
return TComplexity(t=4 * num_and, clifford=num_clifford)

def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
return {
(And(self.cv, 1), self.a_dtype.bitsize),
Expand Down
95 changes: 64 additions & 31 deletions qualtran/bloqs/arithmetic/permutation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "a48878df",
"id": "a697fa0f",
"metadata": {
"cq.autogen": "title_cell"
},
Expand All @@ -13,7 +13,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d083d346",
"id": "f3a56020",
"metadata": {
"cq.autogen": "top_imports"
},
Expand All @@ -30,7 +30,7 @@
},
{
"cell_type": "markdown",
"id": "56539662",
"id": "39be898d",
"metadata": {
"cq.autogen": "Permutation.bloq_doc.md"
},
Expand Down Expand Up @@ -62,7 +62,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "df73124f",
"id": "88b7712d",
"metadata": {
"cq.autogen": "Permutation.bloq_doc.py"
},
Expand All @@ -73,7 +73,7 @@
},
{
"cell_type": "markdown",
"id": "31147b0d",
"id": "5c4722c6",
"metadata": {
"cq.autogen": "Permutation.example_instances.md"
},
Expand All @@ -84,7 +84,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ec6460b6",
"id": "8e38bf3a",
"metadata": {
"cq.autogen": "Permutation.permutation"
},
Expand All @@ -96,7 +96,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0c8e04f0",
"id": "52218544",
"metadata": {
"cq.autogen": "Permutation.permutation_symb"
},
Expand All @@ -113,7 +113,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "8578d036",
"id": "cb1a6988",
"metadata": {
"cq.autogen": "Permutation.permutation_symb_with_cycles"
},
Expand All @@ -132,7 +132,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "84b5606a",
"id": "f58b58f7",
"metadata": {
"cq.autogen": "Permutation.sparse_permutation"
},
Expand All @@ -143,9 +143,26 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e644f2ac",
"metadata": {
"cq.autogen": "Permutation.sparse_permutation_with_symbolic_N"
},
"outputs": [],
"source": [
"import sympy\n",
"\n",
"N = sympy.symbols(\"N\", positive=True, integer=True)\n",
"sparse_permutation_with_symbolic_N = Permutation.from_partial_permutation_map(\n",
" N, {0: 1, 1: 3, 2: 4, 3: 7}\n",
")"
]
},
{
"cell_type": "markdown",
"id": "369df9cb",
"id": "ace174d5",
"metadata": {
"cq.autogen": "Permutation.graphical_signature.md"
},
Expand All @@ -156,20 +173,20 @@
{
"cell_type": "code",
"execution_count": null,
"id": "25b2dc2b",
"id": "1e55dcd4",
"metadata": {
"cq.autogen": "Permutation.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([permutation, permutation_symb, permutation_symb_with_cycles, sparse_permutation],\n",
" ['`permutation`', '`permutation_symb`', '`permutation_symb_with_cycles`', '`sparse_permutation`'])"
"show_bloqs([permutation, permutation_symb, permutation_symb_with_cycles, sparse_permutation, sparse_permutation_with_symbolic_N],\n",
" ['`permutation`', '`permutation_symb`', '`permutation_symb_with_cycles`', '`sparse_permutation`', '`sparse_permutation_with_symbolic_N`'])"
]
},
{
"cell_type": "markdown",
"id": "ad4321ad",
"id": "32e2b5dd",
"metadata": {
"cq.autogen": "Permutation.call_graph.md"
},
Expand All @@ -180,7 +197,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3bfbf3f7",
"id": "fab3fd69",
"metadata": {
"cq.autogen": "Permutation.call_graph.py"
},
Expand All @@ -194,7 +211,7 @@
},
{
"cell_type": "markdown",
"id": "b3e895db",
"id": "408e209c",
"metadata": {
"cq.autogen": "PermutationCycle.bloq_doc.md"
},
Expand Down Expand Up @@ -230,7 +247,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d569cd2c",
"id": "bc68fccd",
"metadata": {
"cq.autogen": "PermutationCycle.bloq_doc.py"
},
Expand All @@ -241,7 +258,7 @@
},
{
"cell_type": "markdown",
"id": "a93c6e89",
"id": "de4c7922",
"metadata": {
"cq.autogen": "PermutationCycle.example_instances.md"
},
Expand All @@ -252,19 +269,23 @@
{
"cell_type": "code",
"execution_count": null,
"id": "264ba946",
"id": "071de1e1",
"metadata": {
"cq.autogen": "PermutationCycle.permutation_cycle"
"cq.autogen": "PermutationCycle.permutation_cycle_symb_N"
},
"outputs": [],
"source": [
"permutation_cycle = PermutationCycle(4, (0, 1, 2))"
"import sympy\n",
"\n",
"N = sympy.symbols(\"n\", positive=True, integer=True)\n",
"cycle = (3, 1, 2)\n",
"permutation_cycle_symb_N = PermutationCycle(N, cycle)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78572528",
"id": "fd61e92a",
"metadata": {
"cq.autogen": "PermutationCycle.permutation_cycle_symb"
},
Expand All @@ -279,9 +300,21 @@
"permutation_cycle_symb = PermutationCycle(N, cycle)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b818f3b1",
"metadata": {
"cq.autogen": "PermutationCycle.permutation_cycle"
},
"outputs": [],
"source": [
"permutation_cycle = PermutationCycle(4, (0, 1, 2))"
]
},
{
"cell_type": "markdown",
"id": "87615783",
"id": "3d3c3e1b",
"metadata": {
"cq.autogen": "PermutationCycle.graphical_signature.md"
},
Expand All @@ -292,20 +325,20 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f6961010",
"id": "de967993",
"metadata": {
"cq.autogen": "PermutationCycle.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([permutation_cycle, permutation_cycle_symb],\n",
" ['`permutation_cycle`', '`permutation_cycle_symb`'])"
"show_bloqs([permutation_cycle_symb_N, permutation_cycle_symb, permutation_cycle],\n",
" ['`permutation_cycle_symb_N`', '`permutation_cycle_symb`', '`permutation_cycle`'])"
]
},
{
"cell_type": "markdown",
"id": "16e1c105",
"id": "58deda5c",
"metadata": {
"cq.autogen": "PermutationCycle.call_graph.md"
},
Expand All @@ -316,16 +349,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "27628f10",
"id": "ec16903f",
"metadata": {
"cq.autogen": "PermutationCycle.call_graph.py"
},
"outputs": [],
"source": [
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
"permutation_cycle_g, permutation_cycle_sigma = permutation_cycle.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(permutation_cycle_g)\n",
"show_counts_sigma(permutation_cycle_sigma)"
"permutation_cycle_symb_N_g, permutation_cycle_symb_N_sigma = permutation_cycle_symb_N.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(permutation_cycle_symb_N_g)\n",
"show_counts_sigma(permutation_cycle_symb_N_sigma)"
]
}
],
Expand Down
Loading

0 comments on commit 8909a9c

Please sign in to comment.