Skip to content

Commit 6cc6927

Browse files
authored
Fix #1782 and #1783: LinearCombinaison adjoint is more general and checks are correct (#1784)
This implements solutions to #1783 and #1782, ~~still need to implement some tests~~.
1 parent d2f8baa commit 6cc6927

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

qualtran/bloqs/block_encoding/linear_combination.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,15 @@ def __attrs_post_init__(self):
108108
"If given, prepare and select oracles must have same selection registers."
109109
)
110110
if self._select is not None and self._select.target_registers != (
111-
self.signature.get_left("system"),
111+
Register(
112+
"system",
113+
QAny(self.system_bitsize + self.be_ancilla_bitsize + self.be_resource_bitsize),
114+
),
112115
):
113116
raise ValueError(
114-
"If given, select oracle must have block encoding `system` register as target."
117+
"Invalid select oracle target registers. "
118+
"The `system` register must include the block encoding's system, ancilla, "
119+
"and resource registers."
115120
)
116121
if not all(
117122
isinstance(u.signal_state.prepare, PrepareIdentity) for u in self._block_encodings
@@ -367,8 +372,13 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlled
367372
ctrl_reg_name='ctrl',
368373
)
369374

370-
def adjoint(self) -> 'LinearCombination':
371-
return self
375+
def adjoint(self) -> 'Bloq':
376+
from qualtran.bloqs.mcmt.specialized_ctrl import (
377+
AdjointWithSpecializedCtrl,
378+
SpecializeOnCtrlBit,
379+
)
380+
381+
return AdjointWithSpecializedCtrl(self, SpecializeOnCtrlBit.ONE)
372382

373383

374384
@bloq_example

qualtran/bloqs/block_encoding/linear_combination_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@ def test_linear_combination5(lambd):
164164
run_gate_test([TGate(), Hadamard(), XGate(), ZGate(), Ry(angle=np.pi / 4.0)], lambd)
165165

166166

167+
@pytest.mark.parametrize('lambd', exact3)
168+
def test_linear_combinaison_hermician_when_bloqs_hermician(lambd):
169+
gates = [Hadamard().controlled(), CNOT(), Swap(1)]
170+
bloq = LinearCombination(tuple(Unitary(g) for g in gates), lambd, lambd_bits=1)
171+
np.testing.assert_allclose(bloq.tensor_contract(), bloq.adjoint().tensor_contract(), atol=1e-16)
172+
173+
174+
@pytest.mark.parametrize('lambd', exact3)
175+
def test_linear_combinaison_not_hermician_when_bloqs_not_hermician(lambd):
176+
gates = [TGate().controlled(), CNOT(), Swap(1)]
177+
bloq = LinearCombination(tuple(Unitary(g) for g in gates), lambd, lambd_bits=1)
178+
with pytest.raises(AssertionError):
179+
np.testing.assert_allclose(
180+
bloq.tensor_contract(), bloq.adjoint().tensor_contract(), atol=1e-16
181+
)
182+
183+
167184
# coefficients are not multiples of small negative powers of 2 after normalization
168185
approx2 = [
169186
[1 / 3, 2 / 3],

qualtran/bloqs/multiplexers/apply_lth_bloq.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlled
127127
get_ctrl_bloq_and_ctrl_reg_name=lambda cv: (evolve(self, control_val=cv), 'control'),
128128
)
129129

130+
def adjoint(self) -> 'Bloq':
131+
from qualtran.bloqs.mcmt.specialized_ctrl import (
132+
AdjointWithSpecializedCtrl,
133+
SpecializeOnCtrlBit,
134+
)
135+
136+
return AdjointWithSpecializedCtrl(self, SpecializeOnCtrlBit.BOTH)
137+
130138

131139
@bloq_example
132140
def _apply_lth_bloq() -> ApplyLthBloq:

0 commit comments

Comments
 (0)