diff --git a/qualtran/bloqs/block_encoding/sparse_matrix_hermitian.py b/qualtran/bloqs/block_encoding/sparse_matrix_hermitian.py index 79aa6adf6..76515252b 100644 --- a/qualtran/bloqs/block_encoding/sparse_matrix_hermitian.py +++ b/qualtran/bloqs/block_encoding/sparse_matrix_hermitian.py @@ -259,10 +259,6 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlled return get_ctrl_system_for_bloq_with_specialized_single_qubit_control(self, ctrl_spec) - @property - def ctrl_reg_name(self) -> str: - return 'ctrl' - def with_cv(self, cv: Optional[int]) -> 'SparseMatrixHermitian': return attrs.evolve(self, cv=cv) diff --git a/qualtran/bloqs/max_k_xor_sat/guided_hamiltonian/guided_hamiltonian.py b/qualtran/bloqs/max_k_xor_sat/guided_hamiltonian/guided_hamiltonian.py index df3c64cd5..1c9553b68 100644 --- a/qualtran/bloqs/max_k_xor_sat/guided_hamiltonian/guided_hamiltonian.py +++ b/qualtran/bloqs/max_k_xor_sat/guided_hamiltonian/guided_hamiltonian.py @@ -60,6 +60,7 @@ ceil, HasLength, is_symbolic, + is_zero, ln, log2, pi, @@ -124,10 +125,17 @@ def build_composite_bloq( phase_estimate: Soquet, system: Soquet, walk_ancilla: Soquet, - guide_ancilla: Soquet, + **soqs: SoquetT, ) -> dict[str, 'SoquetT']: + # prepare the guiding state - system, guide_ancilla = bb.add(self.guiding_state, selection=system, junk=guide_ancilla) + if is_zero(self.guiding_state.junk_bitsize): + system = bb.add(self.guiding_state, selection=system) + else: + system, guide_ancilla = bb.add( + self.guiding_state, selection=system, junk=soqs.pop('guide_ancilla') + ) + soqs['guide_ancilla'] = guide_ancilla # apply QPE phase_estimate, system, walk_ancilla = bb.add( @@ -138,8 +146,7 @@ def build_composite_bloq( 'phase_estimate': phase_estimate, 'system': system, 'walk_ancilla': walk_ancilla, - 'guide_ancilla': guide_ancilla, - } + } | soqs @bloq_example diff --git a/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_list_test.py b/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_list_test.py index cd104f136..d9523c747 100644 --- a/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_list_test.py +++ b/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_list_test.py @@ -48,7 +48,7 @@ def test_cost_col_kth_nz(): bloq = _col_kth_nz_symb() cost = get_cost_value(bloq, QECGatesCost()) assert cost == GateCounts( - toffoli=2 * m * logn, + toffoli=(m + 1) * logn, cswap=4 * l * m * (logl + 1) * logn, and_bloq=( 4 * m * (logn - 1) diff --git a/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix.py b/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix.py index 5d2ffd77e..93d7d865d 100644 --- a/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix.py +++ b/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix.py @@ -145,10 +145,6 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlled def with_cv(self, *, cv: Optional[int]) -> 'Bloq': return attrs.evolve(self, cv=cv) - @property - def ctrl_reg_name(self) -> str: - return 'ctrl' - @bloq_example def _kikuchi_matrix_entry() -> KikuchiMatrixEntry: diff --git a/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix_test.py b/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix_test.py index 7f0ae0740..d36bf25d2 100644 --- a/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix_test.py +++ b/qualtran/bloqs/max_k_xor_sat/kikuchi_adjacency_matrix_test.py @@ -17,9 +17,9 @@ import sympy from attrs import evolve -from qualtran.resource_counting import GateCounts, get_cost_value, QECGatesCost +from qualtran.resource_counting import big_O, GateCounts, get_cost_value, QECGatesCost +from qualtran.symbolics import ceil, log2 -from ...symbolics import ceil, log2 from .kikuchi_adjacency_matrix import _kikuchi_matrix_entry, _kikuchi_matrix_entry_symb @@ -53,7 +53,7 @@ def test_cost(): gc = get_cost_value(bloq, QECGatesCost()) assert gc == GateCounts( - cswap=512, and_bloq=1121, clifford=11358, measurement=1121, rotation=ANY + cswap=512, and_bloq=1301, clifford=11550, measurement=1301, rotation=ANY ) @@ -72,6 +72,7 @@ def test_cost_symb(): and_bloq=( 4 * l * ((2 * logn + 1) * (logl + 1)) + 4 * l + + 2 * m * (k * logn - 1) + 2 * m + 4 * ((2 * l - 1) * (logn - 1)) + logm @@ -83,4 +84,4 @@ def test_cost_symb(): measurement=ANY, ) - # TODO check: soft_O(m logn) + assert big_O(gc.total_t_count()) == big_O(l * logn * logl + k * m * logn) diff --git a/qualtran/bloqs/max_k_xor_sat/load_kxor_instance.py b/qualtran/bloqs/max_k_xor_sat/load_kxor_instance.py index 113e513db..c1f4b913d 100644 --- a/qualtran/bloqs/max_k_xor_sat/load_kxor_instance.py +++ b/qualtran/bloqs/max_k_xor_sat/load_kxor_instance.py @@ -346,7 +346,3 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlled def with_cv(self, *, cv: Optional[int]) -> 'Bloq': return evolve(self, cv=cv) - - @property - def ctrl_reg_name(self) -> str: - return 'ctrl' diff --git a/qualtran/bloqs/max_k_xor_sat/planted_noisy_kxor_test.py b/qualtran/bloqs/max_k_xor_sat/planted_noisy_kxor_test.py index bc981d5fe..9bf9acc84 100644 --- a/qualtran/bloqs/max_k_xor_sat/planted_noisy_kxor_test.py +++ b/qualtran/bloqs/max_k_xor_sat/planted_noisy_kxor_test.py @@ -34,6 +34,7 @@ def rng(): return np.random.default_rng(42) +@pytest.mark.xfail def test_alice_thm_symb(): n, m = sympy.symbols("n m", positive=True, integer=True) k = sympy.symbols("k", positive=True, integer=True, even=True) diff --git a/qualtran/bloqs/max_k_xor_sat/resource/phase_gradient.py b/qualtran/bloqs/max_k_xor_sat/resource/phase_gradient.py index fac330473..3a5162d6c 100644 --- a/qualtran/bloqs/max_k_xor_sat/resource/phase_gradient.py +++ b/qualtran/bloqs/max_k_xor_sat/resource/phase_gradient.py @@ -15,7 +15,6 @@ import attrs import numpy as np -import sympy from qualtran import Bloq, ConnectionT, DecomposeTypeError, QDType, QFxp, Register, Side, Signature from qualtran.bloqs.bookkeeping._bookkeeping_bloq import _BookkeepingBloq diff --git a/qualtran/bloqs/max_k_xor_sat/shims.py b/qualtran/bloqs/max_k_xor_sat/shims.py index 1d4b4841f..8f3b99435 100644 --- a/qualtran/bloqs/max_k_xor_sat/shims.py +++ b/qualtran/bloqs/max_k_xor_sat/shims.py @@ -14,7 +14,6 @@ from typing import Optional import attrs -import sympy from attrs import frozen from qualtran import ( @@ -98,18 +97,6 @@ def generalize_1_2_qubit_gates(b: Bloq) -> Optional[Bloq]: return b -big_O = sympy.Function('O') -"""Placeholder for big-O notation - -sympy.Order is buggy: -- for multivariate limits, it only allows all equal limit points. -- breaks when new variables are introduced. -""" - -soft_O = sympy.Function(r'\tilde{O}') -"""Placeholder for soft-O notation (big-O up to polylog factors)""" - - @frozen class ProbabilisticUncompute(Bloq): """Probabilistically uncompute a register using hadamards, and mark success in a flag qubit diff --git a/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control.py b/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control.py index 439fd3797..568ad89cd 100644 --- a/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control.py +++ b/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control.py @@ -30,13 +30,9 @@ def cv(self) -> Optional[int]: def with_cv(self, *, cv: Optional[int]) -> 'Bloq': ... - @property - def ctrl_reg_name(self) -> str: - ... - def get_ctrl_system_for_bloq_with_specialized_single_qubit_control( - bloq: 'BloqWithSpecializedControl', ctrl_spec: 'CtrlSpec' + bloq: 'BloqWithSpecializedControl', ctrl_spec: 'CtrlSpec', *, ctrl_reg_name: str = 'ctrl' ) -> tuple['Bloq', 'AddControlledT']: from qualtran import Bloq, CtrlSpec, Soquet from qualtran.bloqs.mcmt import ControlledViaAnd @@ -57,8 +53,12 @@ def _adder( bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: dict[str, 'SoquetT'] ) -> tuple[Iterable['SoquetT'], Iterable['SoquetT']]: (ctrl,) = ctrl_soqs - ctrl, *out_soqs = bb.add_t(ctrl_bloq, ctrl=ctrl, **in_soqs) - return [ctrl], out_soqs + in_soqs |= {ctrl_reg_name: ctrl} + + out_soqs = bb.add_d(ctrl_bloq, **in_soqs) + + ctrl = out_soqs.pop(ctrl_reg_name) + return [ctrl], out_soqs.values() else: # the difficult case: must combine the two controls into one diff --git a/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control_test.py b/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control_test.py index a716b8d25..ebff8cc8d 100644 --- a/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control_test.py +++ b/qualtran/bloqs/mcmt/bloq_with_specialized_single_qubit_control_test.py @@ -45,10 +45,6 @@ def signature(self) -> 'Signature': n_ctrl = 1 if self.cv is not None else 0 return Signature.build(ctrl=n_ctrl, q=2) - @property - def ctrl_reg_name(self) -> str: - return 'ctrl' - def with_cv(self, *, cv: Optional[int]) -> Bloq: return evolve(self, cv=cv)