Skip to content

Commit 24f24a0

Browse files
authored
Merge branch 'main' into 2026-01/deps
2 parents 1f6356e + 69d71af commit 24f24a0

6 files changed

Lines changed: 66 additions & 11 deletions

File tree

.github/workflows/nightly.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ permissions: read-all
3030
jobs:
3131
pytest:
3232
runs-on: ubuntu-22.04
33-
timeout-minutes: 60
33+
timeout-minutes: 120
3434
steps:
3535
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
3636
- uses: ts-graphviz/setup-graphviz@c001ccfb5aff62e28bda6a6c39b59a7e061be5b9 # v1
@@ -43,4 +43,4 @@ jobs:
4343
pip install -r dev_tools/requirements/envs/pytest.env.txt
4444
pip install --no-deps -e .
4545
- run: |
46-
check/pytest --durations=10
46+
check/pytest --durations=10 -v -n=0

qualtran/bloqs/basic_gates/identity.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
Signature,
3333
SoquetT,
3434
)
35+
from qualtran.bloqs.bookkeeping import Partition
3536
from qualtran.drawing import Text, TextBox, WireSymbol
3637
from qualtran.symbolics import is_symbolic, SymbolicInt
3738

@@ -116,11 +117,17 @@ def ctrl_adder(
116117
bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: Dict[str, 'SoquetT']
117118
) -> Tuple[Iterable['SoquetT'], Iterable['SoquetT']]:
118119
parts = [
119-
(Register(f'ctrl_{i}', dtype=dtype, shape=shape), 'q')
120+
Register(f"ctrl_{i}", dtype=dtype, shape=shape)
120121
for i, (dtype, shape) in enumerate(ctrl_spec.activation_function_dtypes())
121-
] + [(reg, 'q') for reg in self.signature]
122-
all_soqs = in_soqs | {f'ctrl_{i}': ctrl_soq for i, ctrl_soq in enumerate(ctrl_soqs)}
123-
out_soqs = bb.add_and_partition(ctrl_I, partitions=parts, left_only=False, **all_soqs)
122+
] + [reg for reg in self.signature]
123+
124+
all_soqs = in_soqs | {f"ctrl_{i}": ctrl_soq for i, ctrl_soq in enumerate(ctrl_soqs)}
125+
126+
pratition = Partition(ctrl_I.signature.n_qubits(), regs=parts)
127+
q = bb.add(pratition.adjoint(), **all_soqs)
128+
q = bb.add(ctrl_I, q=q)
129+
out_soqs = bb.add(pratition, x=q)
130+
124131
return out_soqs[:-1], out_soqs[-1:]
125132

126133
return ctrl_I, ctrl_adder

qualtran/bloqs/basic_gates/identity_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pytest
1717
import sympy
1818

19-
from qualtran import BloqBuilder
19+
from qualtran import BloqBuilder, CtrlSpec, QBit, Register
2020
from qualtran.bloqs.basic_gates import OneState
2121
from qualtran.bloqs.basic_gates.identity import _identity, _identity_n, _identity_symb, Identity
2222
from qualtran.simulation.classical_sim import (
@@ -106,6 +106,19 @@ def test_identity_controlled():
106106
assert Identity(n).controlled() == Identity(n + 1)
107107

108108

109+
def test_identity_ctrl_adder():
110+
111+
ctrl_I, ctrl_adder = Identity(1).get_ctrl_system(CtrlSpec())
112+
113+
bb = BloqBuilder()
114+
ctrl0 = bb.add_register(Register("ctrl_0", QBit()))
115+
q = bb.add_register(Register("q", QBit()))
116+
assert ctrl0 is not None and q is not None
117+
[ctrl_out], (out_reg,) = ctrl_adder(bb, ctrl_soqs=[ctrl0], in_soqs={"q": q})
118+
composite = bb.finalize(ctrl_0=ctrl_out, q=out_reg)
119+
composite.flatten()
120+
121+
109122
@pytest.mark.notebook
110123
def test_notebook():
111124
execute_notebook('identity')

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)