Skip to content

Commit d0c5732

Browse files
authored
Changed controlled attribute to is_controlled (#916)
- Change controlled attribute to is_controlled or controlled_by. - This attribute shadows the controlled() function inherited by GateWithRegisters which is confusing and type-unsafe.
1 parent 08e24fd commit d0c5732

8 files changed

Lines changed: 34 additions & 28 deletions

File tree

qualtran/bloqs/arithmetic/multiplication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343

4444
@frozen
45-
class PlusEqualProduct(GateWithRegisters, cirq.ArithmeticGate): # ignore: type[misc]
45+
class PlusEqualProduct(GateWithRegisters, cirq.ArithmeticGate): # type: ignore[misc]
4646
"""Performs result += a * b"""
4747

4848
a_bitsize: int

qualtran/bloqs/chemistry/quad_fermion/givens_bloq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def build_composite_bloq(
131131
phase_bitsize=self.phasegrad_bitsize,
132132
right_shift=0,
133133
sign=1,
134-
controlled=1,
134+
controlled_by=1,
135135
)
136136

137137
# clifford block
@@ -247,7 +247,7 @@ def build_composite_bloq(
247247
phase_bitsize=self.phasegrad_bitsize,
248248
right_shift=0,
249249
sign=1,
250-
controlled=1,
250+
controlled_by=1,
251251
)
252252
target_j, cplx_rom_data, phase_gradient = bb.add(
253253
add_into_phasegrad_gate, x=cplx_rom_data, phase_grad=phase_gradient, ctrl=target_j

qualtran/bloqs/chemistry/quad_fermion/givens_bloq_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def circuit_construction(eta):
7272
@pytest.mark.parametrize("x_bitsize", [4, 5, 6, 7])
7373
def test_count_t_cliffords(x_bitsize: int):
7474
add_into_phasegrad_gate = RzAddIntoPhaseGradient(
75-
x_bitsize=x_bitsize, phase_bitsize=x_bitsize, right_shift=0, sign=1, controlled=1
75+
x_bitsize=x_bitsize, phase_bitsize=x_bitsize, right_shift=0, sign=1, controlled_by=1
7676
)
7777
bloq_counts = add_into_phasegrad_gate.bloq_counts()
7878
# produces Toffoli costs given in chemistry papers
@@ -91,7 +91,7 @@ def test_count_t_cliffords(x_bitsize: int):
9191
@pytest.mark.parametrize("x_bitsize", [4, 5, 6, 7])
9292
def test_complex_givens_costs(x_bitsize: int):
9393
add_into_phasegrad_gate = RzAddIntoPhaseGradient(
94-
x_bitsize=x_bitsize, phase_bitsize=x_bitsize, right_shift=0, sign=1, controlled=1
94+
x_bitsize=x_bitsize, phase_bitsize=x_bitsize, right_shift=0, sign=1, controlled_by=1
9595
)
9696
real_givens_gate = RealGivensRotationByPhaseGradient(phasegrad_bitsize=x_bitsize)
9797
gate = ComplexGivensRotationByPhaseGradient(phasegrad_bitsize=x_bitsize)

qualtran/bloqs/qft/approximate_qft.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def decompose_from_registers(
128128
a, b = q[addition_start_index:i], phase_grad[: addition_bitsize + 1]
129129

130130
yield AddIntoPhaseGrad(
131-
addition_bitsize, addition_bitsize + 1, right_shift=1, controlled=1
131+
addition_bitsize, addition_bitsize + 1, right_shift=1, controlled_by=1
132132
).on_registers(ctrl=q[i], x=a[::-1], phase_grad=b)
133133
yield cirq.H(q[i])
134134

@@ -141,13 +141,13 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
141141
if is_symbolic(self.bitsize, self.phase_bitsize):
142142
phase_dict[
143143
AddIntoPhaseGrad(
144-
self.phase_bitsize, self.phase_bitsize, right_shift=1, controlled=1
144+
self.phase_bitsize, self.phase_bitsize, right_shift=1, controlled_by=1
145145
)
146146
] = self.bitsize
147147
else:
148148
for i in range(1, int(self.bitsize)):
149149
b = min(i, self.phase_bitsize - 1)
150-
phase_dict[AddIntoPhaseGrad(b, b + 1, right_shift=1, controlled=1)] += 1
150+
phase_dict[AddIntoPhaseGrad(b, b + 1, right_shift=1, controlled_by=1)] += 1
151151
ret = {(Hadamard(), self.bitsize), *phase_dict.items()}
152152
if self.with_reverse:
153153
ret |= {(TwoBitSwap(), self.bitsize // 2)}

qualtran/bloqs/qft/approximate_qft_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def f(n, b):
111111
t_complexity = 0
112112
for i in range(1, n):
113113
t_complexity += (
114-
AddIntoPhaseGrad(min(i, b - 1), min(i, b - 1) + 1, controlled=True).t_complexity().t
114+
AddIntoPhaseGrad(min(i, b - 1), min(i, b - 1) + 1, controlled_by=True)
115+
.t_complexity()
116+
.t
115117
)
116118
return t_complexity
117119

qualtran/bloqs/qft/qft_text_book.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def decompose_from_registers(
4848
) -> cirq.OP_TREE:
4949
yield cirq.H(q[0])
5050
for i in range(1, len(q)):
51-
yield PhaseGradientUnitary(i, exponent=0.5, controlled=True).on_registers(
51+
yield PhaseGradientUnitary(i, exponent=0.5, is_controlled=True).on_registers(
5252
ctrl=q[i], phase_grad=q[:i][::-1]
5353
)
5454
yield cirq.H(q[i])

qualtran/bloqs/rotations/phase_gradient.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,37 @@ class PhaseGradientUnitary(GateWithRegisters):
6060

6161
bitsize: int
6262
exponent: float = 1
63-
controlled: bool = False
63+
is_controlled: bool = False
6464
eps: float = 1e-10
6565

6666
@cached_property
6767
def signature(self) -> 'Signature':
6868
return (
6969
Signature.build_from_dtypes(ctrl=QBit(), phase_grad=QFxp(self.bitsize, self.bitsize))
70-
if self.controlled
70+
if self.is_controlled
7171
else Signature.build_from_dtypes(phase_grad=QFxp(self.bitsize, self.bitsize))
7272
)
7373

7474
def decompose_from_registers(
7575
self, *, context: cirq.DecompositionContext, **quregs: NDArray[cirq.Qid] # type: ignore[type-var]
7676
) -> cirq.OP_TREE:
7777
ctrl = quregs.get('ctrl', ())
78-
gate = CZPowGate if self.controlled else ZPowGate
78+
gate = CZPowGate if self.is_controlled else ZPowGate
7979
for i, q in enumerate(quregs['phase_grad']):
8080
yield gate(exponent=self.exponent / 2**i, eps=self.eps / self.bitsize).on(*ctrl, q)
8181

8282
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
83-
wire_symbols = ['@'] * self.controlled + [
83+
wire_symbols = ['@'] * self.is_controlled + [
8484
f'Z^{self.exponent}/{2**(i+1)}' for i in range(self.bitsize)
8585
]
8686
return cirq.CircuitDiagramInfo(wire_symbols=wire_symbols)
8787

8888
def __pow__(self, power):
8989
if power == 1:
9090
return self
91-
return PhaseGradientUnitary(self.bitsize, self.exponent * power, self.controlled, self.eps)
91+
return PhaseGradientUnitary(
92+
self.bitsize, self.exponent * power, self.is_controlled, self.eps
93+
)
9294

9395

9496
@attrs.frozen
@@ -135,7 +137,7 @@ def decompose_from_registers(
135137

136138

137139
@attrs.frozen
138-
class AddIntoPhaseGrad(GateWithRegisters, cirq.ArithmeticGate):
140+
class AddIntoPhaseGrad(GateWithRegisters, cirq.ArithmeticGate): # type: ignore[misc]
139141
r"""Quantum-quantum addition into a phase gradient register using $b_{phase} - 2$ Toffolis
140142
141143
$$
@@ -149,9 +151,9 @@ class AddIntoPhaseGrad(GateWithRegisters, cirq.ArithmeticGate):
149151
shifted before adding to the phase gradient register.
150152
sign: Whether the input register x should be added or subtracted from the phase gradient
151153
register.
152-
controlled: Whether to control this bloq with a ctrl register. When controlled=None, this bloq
153-
is not controlled. When controlled=0, this bloq is active when the ctrl register is 0. When
154-
controlled=1, this bloq is active when the ctrl register is 1.
154+
controlled_by: Whether to control this bloq with a ctrl register. When controlled_by=None, this bloq
155+
is not controlled. When controlled_by=0, this bloq is active when the ctrl register is 0. When
156+
controlled_by=1, this bloq is active when the ctrl register is 1.
155157
156158
Registers:
157159
- ctrl: Control THRU register
@@ -167,7 +169,7 @@ class AddIntoPhaseGrad(GateWithRegisters, cirq.ArithmeticGate):
167169
phase_bitsize: 'SymbolicInt'
168170
right_shift: int = 0
169171
sign: int = +1
170-
controlled: Optional[int] = None
172+
controlled_by: Optional[int] = None
171173

172174
def pretty_name(self) -> str:
173175
sign = '+' if self.sign > 0 else '-'
@@ -181,7 +183,7 @@ def signature(self) -> 'Signature':
181183
x=QFxp(self.x_bitsize, self.x_bitsize, signed=False),
182184
phase_grad=QFxp(self.phase_bitsize, self.phase_bitsize, signed=False),
183185
)
184-
if self.controlled is not None
186+
if self.controlled_by is not None
185187
else Signature.build_from_dtypes(
186188
x=QFxp(self.x_bitsize, self.x_bitsize, signed=False),
187189
phase_grad=QFxp(self.phase_bitsize, self.phase_bitsize, signed=False),
@@ -195,7 +197,9 @@ def phase_dtype(self) -> QFxp:
195197
def registers(self) -> Sequence[Union[int, Sequence[int]]]:
196198
if isinstance(self.phase_bitsize, sympy.Expr):
197199
raise ValueError(f'Symbolic phase {self.phase_bitsize} not supported')
198-
if self.controlled is not None:
200+
if isinstance(self.x_bitsize, sympy.Expr):
201+
raise ValueError(f'Symbolic bitsize {self.x_bitsize} not supported')
202+
if self.controlled_by is not None:
199203
return [2], [2] * self.x_bitsize, [2] * self.phase_bitsize
200204
return [2] * self.x_bitsize, [2] * self.phase_bitsize
201205

@@ -210,7 +214,7 @@ def scaled_val(self, x: int) -> int:
210214
return int(x_fxp.astype(float) * 2**self.phase_bitsize)
211215

212216
def apply(self, *args) -> Union[int, Iterable[int]]:
213-
if self.controlled is not None:
217+
if self.controlled_by is not None:
214218
ctrl, x, phase_grad = args
215219
out = self.on_classical_vals(ctrl=ctrl, x=x, phase_grad=phase_grad)
216220
return out['ctrl'], out['x'], out['phase_grad']
@@ -221,9 +225,9 @@ def apply(self, *args) -> Union[int, Iterable[int]]:
221225

222226
def on_classical_vals(self, **kwargs) -> Dict[str, 'ClassicalValT']:
223227
x, phase_grad = kwargs['x'], kwargs['phase_grad']
224-
if self.controlled is not None:
228+
if self.controlled_by is not None:
225229
ctrl = kwargs['ctrl']
226-
if ctrl == self.controlled:
230+
if ctrl == self.controlled_by:
227231
phase_grad_out = (phase_grad + self.sign * self.scaled_val(x)) % (
228232
2**self.phase_bitsize
229233
)
@@ -236,7 +240,7 @@ def on_classical_vals(self, **kwargs) -> Dict[str, 'ClassicalValT']:
236240

237241
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
238242
num_toffoli = self.phase_bitsize - 2
239-
if self.controlled is not None:
243+
if self.controlled_by is not None:
240244
return {(Toffoli(), 2 * num_toffoli)}
241245

242246
return {(Toffoli(), num_toffoli)}
@@ -251,7 +255,7 @@ def adjoint(self) -> 'Bloq':
251255
self.phase_bitsize,
252256
self.right_shift,
253257
sign=-1 * self.sign,
254-
controlled=self.controlled,
258+
controlled_by=self.controlled_by,
255259
)
256260

257261
def __pow__(self, power):

qualtran/bloqs/rotations/phase_gradient_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_add_into_phase_grad_controlled(controlled: int):
105105
from qualtran.bloqs.rotations.phase_gradient import _fxp
106106

107107
x_bit, phase_bit = 4, 7
108-
bloq = AddIntoPhaseGrad(x_bit, phase_bit, controlled=controlled)
108+
bloq = AddIntoPhaseGrad(x_bit, phase_bit, controlled_by=controlled)
109109
basis_map: Dict[int, int] = {}
110110
num_bits = 1 + x_bit + phase_bit
111111
expected_unitary = np.zeros((2**num_bits, 2**num_bits))

0 commit comments

Comments
 (0)