1515from collections import Counter
1616from functools import cached_property
1717
18- import attrs
1918import numpy as np
2019import sympy
2120from attrs import frozen
2221
2322from qualtran import (
24- AddControlledT ,
2523 Bloq ,
2624 bloq_example ,
2725 BloqBuilder ,
2826 BloqDocSpec ,
29- CtrlSpec ,
3027 DecomposeTypeError ,
3128 QAny ,
3229 QBit ,
3532 Soquet ,
3633 SoquetT ,
3734)
38- from qualtran .bloqs .basic_gates import CSwap , Ry , Swap
35+ from qualtran .bloqs .basic_gates import Ry , Swap
3936from qualtran .bloqs .block_encoding import BlockEncoding
4037from qualtran .bloqs .block_encoding .sparse_matrix import RowColumnOracle
41- from qualtran .bloqs .bookkeeping import Partition
38+ from qualtran .bloqs .bookkeeping import Always , Partition
4239from qualtran .bloqs .bookkeeping .auto_partition import AutoPartition , Unused
4340from qualtran .bloqs .reflections .prepare_identity import PrepareIdentity
4441from qualtran .bloqs .state_preparation .black_box_prepare import BlackBoxPrepare
@@ -133,18 +130,14 @@ class SparseMatrixHermitian(BlockEncoding):
133130 col_oracle : RowColumnOracle
134131 entry_oracle : SqrtEntryOracle
135132 eps : SymbolicFloat
136- is_controlled : bool = False
137133
138134 def __attrs_post_init__ (self ):
139135 if self .col_oracle .system_bitsize != self .entry_oracle .system_bitsize :
140136 raise ValueError ("column and entry oracles must have same bitsize" )
141137
142138 @cached_property
143139 def signature (self ) -> Signature :
144- n_ctrls = 1 if self .is_controlled else 0
145-
146140 return Signature .build_from_dtypes (
147- ctrl = QAny (n_ctrls ),
148141 system = QAny (self .system_bitsize ),
149142 ancilla = QAny (self .ancilla_bitsize ),
150143 resource = QAny (self .resource_bitsize ), # if resource_bitsize is 0, not present
@@ -190,29 +183,23 @@ def diffusion(self):
190183 def build_call_graph (self , ssa : SympySymbolAllocator ) -> BloqCountDictT :
191184 counts = Counter [Bloq ]()
192185
193- counts [self .diffusion ] += 1
194- counts [self .col_oracle ] += 1
195- counts [self .entry_oracle ] += 1
196- if self .is_controlled :
197- counts [CSwap (self .system_bitsize )] += 1
198- counts [CSwap (1 )] += 1
199- else :
200- counts [Swap (self .system_bitsize )] += 1
201- counts [Swap (1 )] += 1
202- counts [self .entry_oracle .adjoint ()] += 1
203- counts [self .col_oracle .adjoint ()] += 1
204- counts [self .diffusion .adjoint ()] += 1
186+ counts [Always (self .diffusion )] += 1
187+ counts [Always (self .col_oracle )] += 1
188+ counts [Always (self .entry_oracle )] += 1
189+ counts [Swap (self .system_bitsize )] += 1
190+ counts [Swap (1 )] += 1
191+ counts [Always (self .entry_oracle .adjoint ())] += 1
192+ counts [Always (self .col_oracle .adjoint ())] += 1
193+ counts [Always (self .diffusion .adjoint ())] += 1
205194
206195 return counts
207196
208197 def build_composite_bloq (
209- self , bb : BloqBuilder , system : SoquetT , ancilla : SoquetT , ** soqs
198+ self , bb : BloqBuilder , system : SoquetT , ancilla : SoquetT
210199 ) -> dict [str , SoquetT ]:
211200 if is_symbolic (self .system_bitsize ) or is_symbolic (self .col_oracle .num_nonzero ):
212201 raise DecomposeTypeError (f"Cannot decompose symbolic { self = } " )
213202
214- ctrl = soqs .pop ('ctrl' , None )
215-
216203 assert not isinstance (ancilla , np .ndarray )
217204 partition_ancilla = Partition (
218205 n = self .ancilla_bitsize ,
@@ -225,42 +212,24 @@ def build_composite_bloq(
225212
226213 a , l , b = bb .add (partition_ancilla , x = ancilla )
227214
228- l = bb .add (self .diffusion , target = l )
229- l , system = bb .add (self .col_oracle , l = l , i = system )
230- b , l , system = bb .add (self .entry_oracle , q = b , i = l , j = system )
215+ l = bb .add (Always ( self .diffusion ) , target = l )
216+ l , system = bb .add (Always ( self .col_oracle ) , l = l , i = system )
217+ b , l , system = bb .add (Always ( self .entry_oracle ) , q = b , i = l , j = system )
231218
232- if self .is_controlled :
233- ctrl , l , system = bb .add (CSwap (self .system_bitsize ), ctrl = ctrl , x = l , y = system )
234- ctrl , a , b = bb .add (CSwap (1 ), ctrl = ctrl , x = a , y = b )
235- else :
236- l , system = bb .add (Swap (self .system_bitsize ), x = l , y = system )
237- a , b = bb .add (Swap (1 ), x = a , y = b )
219+ l , system = bb .add (Swap (self .system_bitsize ), x = l , y = system )
220+ a , b = bb .add (Swap (1 ), x = a , y = b )
238221
239- b , l , system = bb .add (self .entry_oracle .adjoint (), q = b , i = l , j = system )
240- l , system = bb .add (self .col_oracle .adjoint (), l = l , i = system )
241- l = bb .add (self .diffusion .adjoint (), target = l )
222+ b , l , system = bb .add (Always ( self .entry_oracle .adjoint () ), q = b , i = l , j = system )
223+ l , system = bb .add (Always ( self .col_oracle .adjoint () ), l = l , i = system )
224+ l = bb .add (Always ( self .diffusion .adjoint () ), target = l )
242225
243226 ancilla = bb .add (partition_ancilla .adjoint (), a = a , l = l , b = b )
244227
245- out_soqs = {"system" : system , "ancilla" : ancilla }
246- if self .is_controlled :
247- out_soqs |= {"ctrl" : ctrl }
248- return out_soqs
228+ return {"system" : system , "ancilla" : ancilla }
249229
250230 def adjoint (self ) -> 'SparseMatrixHermitian' :
251231 return self
252232
253- def get_ctrl_system (self , ctrl_spec : 'CtrlSpec' ) -> tuple ['Bloq' , 'AddControlledT' ]:
254- from qualtran .bloqs .mcmt .specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs
255-
256- return get_ctrl_system_1bit_cv_from_bloqs (
257- self ,
258- ctrl_spec ,
259- current_ctrl_bit = 1 if self .is_controlled else None ,
260- bloq_with_ctrl = self if self .is_controlled else attrs .evolve (self , is_controlled = True ),
261- ctrl_reg_name = 'ctrl' ,
262- )
263-
264233
265234@frozen
266235class UniformSqrtEntryOracle (SqrtEntryOracle ):
0 commit comments