|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Generate the library-wide call graph from all bloq examples.""" |
| 16 | +import warnings |
| 17 | +from typing import Iterable |
| 18 | + |
| 19 | +import networkx as nx |
| 20 | + |
| 21 | +from qualtran import Bloq, BloqExample, DecomposeNotImplementedError, DecomposeTypeError |
| 22 | +from qualtran.bloqs.basic_gates import Rx, Ry, Rz, TGate, Toffoli, XPowGate, YPowGate, ZPowGate |
| 23 | +from qualtran.resource_counting import SympySymbolAllocator |
| 24 | +from qualtran.resource_counting._call_graph import _build_call_graph |
| 25 | +from qualtran.resource_counting._generalization import _make_composite_generalizer |
| 26 | +from qualtran.resource_counting.generalizers import ( |
| 27 | + generalize_cvs, |
| 28 | + generalize_rotation_angle, |
| 29 | + ignore_alloc_free, |
| 30 | + ignore_cliffords, |
| 31 | + ignore_split_join, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +def get_all_call_graph(bes: Iterable[BloqExample]): |
| 36 | + """Create a call graph that is the union of all of the bloqs in the list of bloq examples. |
| 37 | +
|
| 38 | + This applies some standard generalizers, and will stop at a larger-than-default set |
| 39 | + of leaf bloqs in accordance with https://github.com/quantumlib/Qualtran/issues/873 |
| 40 | + """ |
| 41 | + generalize = _make_composite_generalizer( |
| 42 | + ignore_split_join, |
| 43 | + generalize_cvs, |
| 44 | + generalize_rotation_angle, |
| 45 | + ignore_alloc_free, |
| 46 | + ignore_cliffords, |
| 47 | + ) |
| 48 | + |
| 49 | + def keep(b: Bloq) -> bool: |
| 50 | + if b == Toffoli(): |
| 51 | + return True |
| 52 | + |
| 53 | + if b == TGate(): |
| 54 | + return True |
| 55 | + |
| 56 | + if isinstance(b, (Rx, Ry, Rz, XPowGate, YPowGate, ZPowGate)): |
| 57 | + return True |
| 58 | + |
| 59 | + try: |
| 60 | + _ = b.build_call_graph(SympySymbolAllocator()) |
| 61 | + except DecomposeTypeError: |
| 62 | + return True |
| 63 | + except DecomposeNotImplementedError: |
| 64 | + warnings.warn(f"{b} lacks a call graph.") |
| 65 | + return True |
| 66 | + |
| 67 | + return False |
| 68 | + |
| 69 | + g = nx.DiGraph() |
| 70 | + ssa = SympySymbolAllocator() |
| 71 | + |
| 72 | + for be in bes: |
| 73 | + bloq = be.make() |
| 74 | + _build_call_graph( |
| 75 | + bloq=bloq, generalizer=generalize, ssa=ssa, keep=keep, max_depth=None, g=g, depth=0 |
| 76 | + ) |
| 77 | + |
| 78 | + return g |
0 commit comments