Skip to content

Commit 4517c28

Browse files
authored
Add ctrl system for LCUBlockEncoding (#1593)
* add failing test for controlled LCUBlockEncoding * add ctrl system for `LCUBlockEncoding` * support cv=0 * rename import
1 parent b416b29 commit 4517c28

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

qualtran/bloqs/block_encoding/lcu_block_encoding.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import attrs
1919

2020
from qualtran import (
21+
AddControlledT,
2122
Bloq,
2223
bloq_example,
2324
BloqBuilder,
@@ -283,6 +284,16 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -
283284
else:
284285
return TextBox('B[H]')
285286

287+
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlledT']:
288+
from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv
289+
290+
return get_ctrl_system_1bit_cv(
291+
self,
292+
ctrl_spec,
293+
current_ctrl_bit=self.control_val,
294+
get_ctrl_bloq_and_ctrl_reg_name=lambda cv: (attrs.evolve(self, control_val=cv), 'ctrl'),
295+
)
296+
286297
def adjoint(self) -> 'Bloq':
287298
from qualtran.bloqs.mcmt.specialized_ctrl import (
288299
AdjointWithSpecializedCtrl,

qualtran/bloqs/block_encoding/lcu_block_encoding_test.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import attrs
1516
import pytest
1617

18+
import qualtran.testing as qlt_testing
19+
from qualtran import CtrlSpec
1720
from qualtran.bloqs.block_encoding.lcu_block_encoding import (
1821
_black_box_lcu_block,
1922
_black_box_select_block,
2023
_lcu_block,
2124
_select_block,
2225
)
23-
from qualtran.testing import execute_notebook
26+
from qualtran.resource_counting import GateCounts, get_cost_value, QECGatesCost
2427

2528

2629
def test_lcu_block_encoding(bloq_autotester):
@@ -39,6 +42,16 @@ def test_black_box_select_block_encoding(bloq_autotester):
3942
bloq_autotester(_black_box_select_block)
4043

4144

45+
def test_ctrl_lcu_be_cost():
46+
bloq = _lcu_block()
47+
assert bloq.controlled() == attrs.evolve(bloq, control_val=1)
48+
assert bloq.controlled(CtrlSpec(cvs=0)) == attrs.evolve(bloq, control_val=0)
49+
50+
assert get_cost_value(bloq.controlled(), QECGatesCost()) == GateCounts(
51+
cswap=28, and_bloq=77, clifford=438, rotation=16, measurement=77
52+
)
53+
54+
4255
@pytest.mark.notebook
4356
def test_notebook():
44-
execute_notebook('lcu_block_encoding')
57+
qlt_testing.execute_notebook('lcu_block_encoding')

0 commit comments

Comments
 (0)