Skip to content

Commit 9c47157

Browse files
authored
[QECGatesCost] Adjustments part 3 (#1333)
* [QECGatesCost] Adjustments part 3 * lint * restrict num controls
1 parent 40eb7cb commit 9c47157

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

qualtran/resource_counting/_qubit_counts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def compute(
110110
return _cbloq_max_width(cbloq._binst_graph, get_callee_cost)
111111
except (DecomposeNotImplementedError, DecomposeTypeError):
112112
pass
113+
except Exception as e:
114+
raise RuntimeError(
115+
f"An unexpected error occurred when trying to compute {self} for {bloq}: {e}"
116+
) from e
113117

114118
# Fallback:
115119
# Use the simple maximum of callees and of this bloq's signature. If there

qualtran/resource_counting/classify_bloqs.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def bloq_is_rotation(b: Bloq) -> bool:
230230
This function has a shim for counting Controlled[Rotation] gates as a rotation, which
231231
will be remediated when the Qualtran standard library gains a bespoke bloq for each CRot.
232232
"""
233-
from qualtran.bloqs.basic_gates import GlobalPhase, SGate, TGate
233+
from qualtran.bloqs.basic_gates import SGate, TGate
234234
from qualtran.bloqs.basic_gates.rotation import (
235235
CZPowGate,
236236
Rx,
@@ -242,12 +242,17 @@ def bloq_is_rotation(b: Bloq) -> bool:
242242
)
243243

244244
if isinstance(b, Controlled):
245+
if b.ctrl_spec.num_qubits > 1:
246+
return False
247+
245248
# TODO https://github.com/quantumlib/Qualtran/issues/878
246249
# explicit representation of all two-qubit rotations.
247-
if isinstance(b.subbloq, (SGate, TGate, GlobalPhase)):
250+
if isinstance(b.subbloq, (SGate, TGate)):
248251
return True
249252

250-
return bloq_is_rotation(b.subbloq)
253+
# For historical reasons, this hacky solution for controlled rotations does *not*
254+
# do clifford, T angle simplification.
255+
return isinstance(b.subbloq, (Rx, Ry, Rz, XPowGate, YPowGate, ZPowGate))
251256

252257
if isinstance(b, CZPowGate):
253258
return True

qualtran/resource_counting/classify_bloqs_test.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919
import pytest
2020

21-
from qualtran import Bloq, QInt, Signature
21+
from qualtran import Bloq, CtrlSpec, QInt, Signature
2222
from qualtran.bloqs.arithmetic import Add
2323
from qualtran.bloqs.arithmetic.comparison import LessThanConstant
2424
from qualtran.bloqs.basic_gates import CSwap, Rx, Rz, TGate, YPowGate
@@ -33,6 +33,7 @@
3333
from qualtran.resource_counting import BloqCountT
3434
from qualtran.resource_counting.classify_bloqs import (
3535
_get_basic_bloq_classification,
36+
bloq_is_rotation,
3637
bloq_is_t_like,
3738
classify_bloq,
3839
classify_t_count_by_bloq_type,
@@ -112,6 +113,14 @@ def test_classify_bloq_counts_with_custom_bloq_classification():
112113
assert test_bloq.call_graph()[1].get(TGate()) == sum(classified_bloqs.values())
113114

114115

116+
def test_bloq_is_rotation():
117+
assert bloq_is_rotation(Rx(np.pi * 0.123))
118+
assert not bloq_is_rotation(Rx(np.pi / 2))
119+
assert bloq_is_rotation(Rx(np.pi * 0.123).controlled())
120+
assert bloq_is_rotation(Rx(np.pi / 2).controlled())
121+
assert not bloq_is_rotation(Rx(np.pi).controlled(ctrl_spec=CtrlSpec(cvs=(1, 1))))
122+
123+
115124
def test_bloq_is_t_like():
116125
assert bloq_is_t_like(TGate())
117126
assert bloq_is_t_like(TGate().adjoint())

0 commit comments

Comments
 (0)