Skip to content

Commit 16b4031

Browse files
committed
Warn when wrapping a bloq
1 parent 58ca326 commit 16b4031

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

qualtran/cirq_interop/_cirq_to_bloq.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import abc
1717
import itertools
1818
import numbers
19+
import warnings
1920
from functools import cached_property
2021
from typing import Any, Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING, TypeVar, Union
2122

@@ -32,6 +33,7 @@
3233
CtrlSpec,
3334
DecomposeNotImplementedError,
3435
DecomposeTypeError,
36+
GateWithRegisters,
3537
QAny,
3638
QBit,
3739
QDType,
@@ -151,6 +153,14 @@ class CirqGateAsBloq(CirqGateAsBloqBase):
151153

152154
gate: cirq.Gate
153155

156+
def __attrs_post_init__(self):
157+
if isinstance(self.gate, GateWithRegisters):
158+
warnings.warn(
159+
f"Tried to use `CirqGateAsBloq` to adapt a `qualtran.GateWithRegisters`, "
160+
f"which already satisfies the Bloq API. Consider using {self.gate} "
161+
f"directly (without the adapter)."
162+
)
163+
154164
@property
155165
def cirq_gate(self) -> cirq.Gate:
156166
return self.gate

qualtran/cirq_interop/_cirq_to_bloq_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def test_cirq_gate_as_bloq_tensor_contract_for_and_gate():
8888
ctrl = [bb.add(OneState()) for _ in range(2)]
8989
target = bb.add(ZeroState())
9090
q = [*ctrl, target]
91-
c0, c1, target = bb.add(CirqGateAsBloq(and_gate), q=q)
91+
with pytest.warns(UserWarning):
92+
# It's odd to use CirqGateAsBloq to wrap a GateWithRegisters, which is already a bloq.
93+
c0, c1, target = bb.add(CirqGateAsBloq(and_gate), q=q)
9294
cbloq = bb.finalize(ctrl=np.array([c0, c1]), target=target)
9395
state_vector = cbloq.tensor_contract()
9496
assert np.isclose(state_vector[7], 1)

0 commit comments

Comments
 (0)