Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

symbolic_hamsim_by_gqsp does not qubit count #1365

Closed
mpharrigan opened this issue Aug 29, 2024 · 0 comments · Fixed by #1366
Closed

symbolic_hamsim_by_gqsp does not qubit count #1365

mpharrigan opened this issue Aug 29, 2024 · 0 comments · Fixed by #1366

Comments

@mpharrigan
Copy link
Collaborator

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/qutran/qutran/qualtran/resource_counting/_qubit_counts.py:110, in QubitCount.compute(self, bloq, get_callee_cost)
    109     logger.info("Computing %s for %s from its decomposition", self, bloq)
--> 110     return _cbloq_max_width(cbloq._binst_graph, get_callee_cost)
    111 except (DecomposeNotImplementedError, DecomposeTypeError):

File ~/qutran/qutran/qualtran/resource_counting/_qubit_counts.py:60, in _cbloq_max_width(binst_graph, _bloq_max_width)
     56 if not isinstance(binst, DanglingT):
     57     # During the application of the binst, we have "observer" connections that have
     58     # width as well as the width from the binst itself. We consider the case where
     59     # the bloq may have a max_width greater than the max of its left/right registers.
---> 60     during_size = _bloq_max_width(binst.bloq) + sum(s.shape for s in in_play)
     61     max_width = smax(max_width, during_size)

File ~/qutran/qutran/qualtran/resource_counting/_costing.py:142, in _get_cost_value.<locals>._get_cost_val_internal(callee)
    141 def _get_cost_val_internal(callee: 'Bloq'):
--> 142     return _get_cost_value(callee, cost_key, costs_cache=costs_cache, generalizer=generalizer)

File ~/qutran/qutran/qualtran/resource_counting/_costing.py:146, in _get_cost_value(bloq, cost_key, costs_cache, generalizer)
    145 tstart = time.perf_counter()
--> 146 computed_cost = cost_key.compute(bloq, _get_cost_val_internal)
    147 tdur = time.perf_counter() - tstart

File ~/qutran/qutran/qualtran/resource_counting/_qubit_counts.py:123, in QubitCount.compute(self, bloq, get_callee_cost)
    122 min_bloq_size = bloq.signature.n_qubits()
--> 123 callees = get_bloq_callee_counts(bloq)
    124 tot: int = min_bloq_size

File ~/qutran/qutran/qualtran/resource_counting/_call_graph.py:122, in get_bloq_callee_counts(bloq, generalizer, ssa, ignore_decomp_failure)
    121 try:
--> 122     return _generalize_callees(bloq.build_call_graph(ssa), cast(GeneralizerT, generalizer))
    123 except (DecomposeNotImplementedError, DecomposeTypeError) as e:

File ~/qutran/qutran/qualtran/bloqs/qsp/generalized_qsp.py:383, in GeneralizedQSP.build_call_graph(self, ssa)
    381 counts[self.U.adjoint().controlled()] += smin(degree, self.negative_power)
--> 383 return set((bloq, count) for bloq, count in counts.items() if not is_zero(count))

File ~/qutran/qutran/qualtran/bloqs/qsp/generalized_qsp.py:383, in <genexpr>(.0)
    381 counts[self.U.adjoint().controlled()] += smin(degree, self.negative_power)
--> 383 return set((bloq, count) for bloq, count in counts.items() if not is_zero(count))

File ~/qutran/qutran/qualtran/symbolics/math_funcs.py:326, in is_zero(x)
    325 if is_symbolic(x):
--> 326     return x.equals(0)
    327 return x == 0

File ~/qutran/py311-cf/lib/python3.11/site-packages/sympy/core/expr.py:757, in Expr.equals(self, other, failing_expression)
    755         return any(fac_zero)
--> 757 constant = diff.is_constant(simplify=False, failing_number=True)
    759 if constant is False:

File ~/qutran/py311-cf/lib/python3.11/site-packages/sympy/core/expr.py:661, in Expr.is_constant(self, *wrt, **flags)
    660 try:
--> 661     a = expr.subs(list(zip(free, [0]*len(free))),
    662         simultaneous=True)
    663     if a is S.NaN:
    664         # evaluation may succeed when substitution fails

File ~/qutran/py311-cf/lib/python3.11/site-packages/sympy/core/basic.py:1036, in Basic.subs(self, *args, **kwargs)
   1035     reps[m] = S.One  # get rid of m
-> 1036     return rv.xreplace(reps)
   1037 else:

File ~/qutran/py311-cf/lib/python3.11/site-packages/sympy/core/basic.py:1230, in Basic.xreplace(self, rule)
   1169 """
   1170 Replace occurrences of objects within the expression.
   1171 
   (...)
   1228 
   1229 """
-> 1230 value, _ = self._xreplace(rule)
   1231 return value

File ~/qutran/py311-cf/lib/python3.11/site-packages/sympy/core/basic.py:1252, in Basic._xreplace(self, rule)
   1251     if changed:
-> 1252         return self.func(*args), True
   1253 return self, False

File ~/qutran/py311-cf/lib/python3.11/site-packages/sympy/core/cache.py:72, in __cacheit.<locals>.func_wrapper.<locals>.wrapper(*args, **kwargs)
     71 try:
---> 72     retval = cfunc(*args, **kwargs)
     73 except TypeError as e:

File ~/qutran/py311-cf/lib/python3.11/site-packages/sympy/series/order.py:154, in Order.__new__(cls, expr, *args, **kwargs)
    153 if not all(v.is_symbol for v in variables):
--> 154     raise TypeError('Variables are not symbols, got %s' % variables)
    156 if len(list(uniq(variables))) != len(variables):

TypeError: Variables are not symbols, got [0, 1]

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
Cell In[14], line 4
      2 be = get_bloq_example('symbolic_hamsim_by_gqsp')
      3 bloq = be.make()
----> 4 show_call_graph(bloq, max_depth=1)

File ~/qutran/qutran/qualtran/drawing/_show_funcs.py:120, in show_call_graph(item, max_depth, agg_gate_counts)
    102 """Display a graph representation of the call graph.
    103 
    104 Args:
   (...)
    116 
    117 """
    118 if isinstance(item, Bloq):
    119     IPython.display.display(
--> 120         GraphvizCallGraph.from_bloq(
    121             item, max_depth=max_depth, agg_gate_counts=agg_gate_counts
    122         ).get_svg()
    123     )
    124 else:
    125     IPython.display.display(GraphvizCounts(item).get_svg())

File ~/qutran/qutran/qualtran/drawing/bloq_counts_graph.py:330, in GraphvizCallGraph.from_bloq(cls, bloq, max_depth, agg_gate_counts)
    327 from qualtran.resource_counting import QECGatesCost, QubitCount, query_costs
    329 call_graph, _ = bloq.call_graph(max_depth=max_depth)
--> 330 cost_data: Dict['Bloq', Dict[CostKey, Any]] = query_costs(
    331     bloq, [QubitCount(), QECGatesCost()]
    332 )
    333 formatted_cost_data = cls.format_cost_data(cost_data, agg_gate_counts=agg_gate_counts)
    334 return cls(g=call_graph, bloq_data=formatted_cost_data)

File ~/qutran/qutran/qualtran/resource_counting/_costing.py:249, in query_costs(bloq, cost_keys, generalizer)
    247 costs: Dict['Bloq', Dict[CostKey, CostValT]] = defaultdict(dict)
    248 for cost_key in cost_keys:
--> 249     cost_for_bloqs = get_cost_cache(bloq, cost_key, generalizer=generalizer)
    250     for bloq, val in cost_for_bloqs.items():
    251         costs[bloq][cost_key] = val

File ~/qutran/qutran/qualtran/resource_counting/_costing.py:220, in get_cost_cache(bloq, cost_key, costs_cache, generalizer)
    217 if isinstance(generalizer, collections.abc.Sequence):
    218     generalizer = _make_composite_generalizer(*generalizer)
--> 220 _get_cost_value(bloq, cost_key, costs_cache=costs_cache, generalizer=generalizer)
    221 return costs_cache

File ~/qutran/qutran/qualtran/resource_counting/_costing.py:146, in _get_cost_value(bloq, cost_key, costs_cache, generalizer)
    144 # part b. call the compute method and cache the result.
    145 tstart = time.perf_counter()
--> 146 computed_cost = cost_key.compute(bloq, _get_cost_val_internal)
    147 tdur = time.perf_counter() - tstart
    148 logger.info("Computed %s for %s in %g s", cost_key, bloq, tdur)

File ~/qutran/qutran/qualtran/resource_counting/_qubit_counts.py:114, in QubitCount.compute(self, bloq, get_callee_cost)
    112     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
    118 # Fallback:
    119 # Use the simple maximum of callees and of this bloq's signature. If there
    120 # are no callees, this will be the number of qubits implied by the signature.
    121 # In any case, this strategy is likely an under-estimate of the qubit count.
    122 min_bloq_size = bloq.signature.n_qubits()

RuntimeError: An unexpected error occurred when trying to compute qubit count for HamiltonianSimulationByGQSP: Variables are not symbols, got [0, 1]

It seems that the is_zero function is choking on some counts in GeneralizedQSP.build_call_graph

@anurudhp changed this recently can you investigate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant