Skip to content

Commit

Permalink
[QECGatesCost] Adjustments part 3 (#1333)
Browse files Browse the repository at this point in the history
* [QECGatesCost] Adjustments part 3

* lint

* restrict num controls
  • Loading branch information
mpharrigan authored Aug 23, 2024
1 parent 40eb7cb commit 9c47157
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions qualtran/resource_counting/_qubit_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def compute(
return _cbloq_max_width(cbloq._binst_graph, get_callee_cost)
except (DecomposeNotImplementedError, DecomposeTypeError):
pass
except Exception as e:
raise RuntimeError(
f"An unexpected error occurred when trying to compute {self} for {bloq}: {e}"
) from e

# Fallback:
# Use the simple maximum of callees and of this bloq's signature. If there
Expand Down
11 changes: 8 additions & 3 deletions qualtran/resource_counting/classify_bloqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def bloq_is_rotation(b: Bloq) -> bool:
This function has a shim for counting Controlled[Rotation] gates as a rotation, which
will be remediated when the Qualtran standard library gains a bespoke bloq for each CRot.
"""
from qualtran.bloqs.basic_gates import GlobalPhase, SGate, TGate
from qualtran.bloqs.basic_gates import SGate, TGate
from qualtran.bloqs.basic_gates.rotation import (
CZPowGate,
Rx,
Expand All @@ -242,12 +242,17 @@ def bloq_is_rotation(b: Bloq) -> bool:
)

if isinstance(b, Controlled):
if b.ctrl_spec.num_qubits > 1:
return False

# TODO https://github.com/quantumlib/Qualtran/issues/878
# explicit representation of all two-qubit rotations.
if isinstance(b.subbloq, (SGate, TGate, GlobalPhase)):
if isinstance(b.subbloq, (SGate, TGate)):
return True

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

if isinstance(b, CZPowGate):
return True
Expand Down
11 changes: 10 additions & 1 deletion qualtran/resource_counting/classify_bloqs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import numpy as np
import pytest

from qualtran import Bloq, QInt, Signature
from qualtran import Bloq, CtrlSpec, QInt, Signature
from qualtran.bloqs.arithmetic import Add
from qualtran.bloqs.arithmetic.comparison import LessThanConstant
from qualtran.bloqs.basic_gates import CSwap, Rx, Rz, TGate, YPowGate
Expand All @@ -33,6 +33,7 @@
from qualtran.resource_counting import BloqCountT
from qualtran.resource_counting.classify_bloqs import (
_get_basic_bloq_classification,
bloq_is_rotation,
bloq_is_t_like,
classify_bloq,
classify_t_count_by_bloq_type,
Expand Down Expand Up @@ -112,6 +113,14 @@ def test_classify_bloq_counts_with_custom_bloq_classification():
assert test_bloq.call_graph()[1].get(TGate()) == sum(classified_bloqs.values())


def test_bloq_is_rotation():
assert bloq_is_rotation(Rx(np.pi * 0.123))
assert not bloq_is_rotation(Rx(np.pi / 2))
assert bloq_is_rotation(Rx(np.pi * 0.123).controlled())
assert bloq_is_rotation(Rx(np.pi / 2).controlled())
assert not bloq_is_rotation(Rx(np.pi).controlled(ctrl_spec=CtrlSpec(cvs=(1, 1))))


def test_bloq_is_t_like():
assert bloq_is_t_like(TGate())
assert bloq_is_t_like(TGate().adjoint())
Expand Down

0 comments on commit 9c47157

Please sign in to comment.