Skip to content

Commit 7e318f2

Browse files
authored
Remove supports_decompose_bloq (#1382)
1 parent 76fdc9d commit 7e318f2

7 files changed

Lines changed: 12 additions & 36 deletions

File tree

qualtran/_infra/adjoint.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ def _circuit_diagram_info_(
151151
sub_info.exponent *= -1
152152
return sub_info
153153

154-
def supports_decompose_bloq(self) -> bool:
155-
"""Delegate to `subbloq.supports_decompose_bloq()`"""
156-
return self.subbloq.supports_decompose_bloq()
157-
158154
def adjoint(self) -> 'Bloq':
159155
"""The 'double adjoint' brings you back to the original bloq."""
160156
return self.subbloq

qualtran/_infra/adjoint_test.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import qualtran.testing as qlt_testing
2020
from qualtran import Adjoint, CompositeBloq, Side
2121
from qualtran._infra.adjoint import _adjoint_cbloq
22-
from qualtran.bloqs.basic_gates import CNOT, CSwap, ZeroState
22+
from qualtran.bloqs.basic_gates import CNOT, ZeroState
2323
from qualtran.bloqs.for_testing.atom import TestAtom
2424
from qualtran.bloqs.for_testing.with_call_graph import TestBloqWithCallGraph
2525
from qualtran.bloqs.for_testing.with_decomposition import TestParallelCombo, TestSerialCombo
@@ -101,14 +101,6 @@ def test_adjoint_signature():
101101
assert adj_reg.side == Side.LEFT
102102

103103

104-
def test_adjoint_supports_decompose():
105-
assert not CNOT().supports_decompose_bloq()
106-
assert not Adjoint(CNOT()).supports_decompose_bloq()
107-
108-
assert CSwap(bitsize=5).supports_decompose_bloq()
109-
assert Adjoint(CSwap(bitsize=5)).supports_decompose_bloq()
110-
111-
112104
def test_adjoint_adjoint():
113105
zero = ZeroState()
114106
adj = Adjoint(zero) # specifically use the Adjoint wrapper for testing

qualtran/_infra/bloq.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,6 @@ def decompose_bloq(self) -> 'CompositeBloq':
146146
"""
147147
return _decompose_from_build_composite_bloq(self)
148148

149-
def supports_decompose_bloq(self) -> bool:
150-
"""Whether this bloq supports `.decompose_bloq()`.
151-
152-
By default, we check that the method `build_composite_bloq` is overriden. For
153-
extraordinary circumstances, you may need to override this method directly to
154-
return an accurate value.
155-
"""
156-
return not self.build_composite_bloq.__qualname__.startswith('Bloq.')
157-
158149
def as_composite_bloq(self) -> 'CompositeBloq':
159150
"""Wrap this Bloq into a size-1 CompositeBloq.
160151

qualtran/_infra/bloq_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def test_bloq():
3636

3737
def test_as_composite_bloq():
3838
tb = TestAtom()
39-
assert not tb.supports_decompose_bloq()
39+
with pytest.raises(DecomposeTypeError):
40+
tb.decompose_bloq()
4041
cb = tb.as_composite_bloq()
4142
assert isinstance(cb, CompositeBloq)
4243
bloqs = list(cb.bloq_instances)

qualtran/_infra/composite_bloq_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ def test_test_parallel_combo_decomp():
420420

421421
@pytest.mark.parametrize('cls', [TestSerialCombo, TestParallelCombo])
422422
def test_copy(cls):
423-
assert cls().supports_decompose_bloq()
424423
cbloq = cls().decompose_bloq()
425424
cbloq2 = cbloq.copy()
426425
assert cbloq is not cbloq2
@@ -509,9 +508,6 @@ def test_flatten():
509508
cbloq3 = cbloq.flatten(lambda binst: True)
510509
assert len(cbloq3.bloq_instances) == 5 * 2
511510

512-
cbloq4 = cbloq.flatten(lambda binst: binst.bloq.supports_decompose_bloq())
513-
assert len(cbloq4.bloq_instances) == 5 * 2
514-
515511
cbloq5 = cbloq.flatten()
516512
assert len(cbloq5.bloq_instances) == 5 * 2
517513

qualtran/bloqs/basic_gates/su2_rotation_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from qualtran import Bloq
1919
from qualtran.bloqs.basic_gates import GlobalPhase, Hadamard, Rx, Rz, TGate, XGate, YGate, ZGate
20-
from qualtran.cirq_interop import BloqAsCirqGate
2120

2221
from .su2_rotation import _hadamard, _su2_rotation_gate, _t_gate, SU2RotationGate
2322

@@ -30,7 +29,7 @@ def test_decompose_SU2_to_single_qubit_pauli_gates():
3029
gate = SU2RotationGate(theta, phi, lambd, global_shift)
3130

3231
np.testing.assert_allclose(
33-
cirq.unitary(BloqAsCirqGate(gate.decompose_bloq())), gate.rotation_matrix
32+
cirq.unitary(gate.decompose_bloq().to_cirq_circuit()), gate.rotation_matrix
3433
)
3534

3635

qualtran/cirq_interop/_bloq_to_cirq.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,15 @@ def _decompose_(self, qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
147147
return self._decompose_with_context_(qubits)
148148

149149
def _unitary_(self):
150-
if (
151-
all(reg.side == Side.THRU for reg in self.signature)
152-
and not self.bloq.supports_decompose_bloq()
153-
):
154-
tensor = self.bloq.tensor_contract()
155-
if tensor.ndim != 2:
150+
if all(reg.side == Side.THRU for reg in self.signature):
151+
try:
152+
_ = self.bloq.decompose_bloq() # check for decomposability
156153
return NotImplemented
157-
return tensor
154+
except (DecomposeNotImplementedError, DecomposeTypeError):
155+
tensor = self.bloq.tensor_contract()
156+
if tensor.ndim != 2:
157+
return NotImplemented
158+
return tensor
158159
return NotImplemented
159160

160161
def on_registers(

0 commit comments

Comments
 (0)