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

estimate_resources fails for DF QPE circuit #35

Open
max-radin opened this issue Dec 6, 2024 · 1 comment
Open

estimate_resources fails for DF QPE circuit #35

max-radin opened this issue Dec 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@max-radin
Copy link

Here is a minimum working example that produces the exception I was describing at the last GSEE call. This is with pyliqtr 1.3.3.

from openfermion.chem import MolecularData
from openfermionpyscf import run_pyscf
from pyLIQTR.BlockEncodings.DoubleFactorized import DoubleFactorized
from pyLIQTR.ProblemInstances.getInstance import getInstance
from pyLIQTR.qubitization.phase_estimation import QubitizedPhaseEstimation
from pyLIQTR.utils.resource_analysis import estimate_resources

mol_data = MolecularData(
    [("H", (0.0, 0.0, 0.63164)), ("H", (0.0, 0.0, 1.76836))], "sto-3g", 1.0, 0, "H2"
)

mol = run_pyscf(
    mol_data, run_scf=1, run_mp2=0, run_cisd=0, run_ccsd=0, run_fci=0, verbose=0
)

mol_ham = mol.get_molecular_hamiltonian()
mol_instance = getInstance("ChemicalHamiltonian", mol_ham=mol_ham, mol_name="H2")

n_qubits = mol_instance.n_qubits()
print(f"Number of qubits: {n_qubits}")

encoding = DoubleFactorized(
    mol_instance,
)
circuit = QubitizedPhaseEstimation(encoding)
logical_resources = estimate_resources(circuit.circuit)

Traceback:

Traceback (most recent call last):
  File "/Users/maxradin/Development/scratch/pyliqtr_133.py", line 26, in <module>
    logical_resources = estimate_resources(circuit.circuit)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 126, in estimate_resources
    t_cliff_rot_resources = pylqt_t_complexity(circuit_element)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 86, in pylqt_t_complexity
    thisComplexity = _pylqt_t_complexity_for_gate_or_op(op)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/cachetools/__init__.py", line 675, in wrapper
    v = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 67, in _pylqt_t_complexity_for_gate_or_op
    return _t_complexity_from_strategies(gate_or_op, strategies)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/t_complexity_protocol.py", line 204, in _t_complexity_from_strategies
    ret = strategy(stc)
          ^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/t_complexity_protocol.py", line 84, in _from_explicit_annotation
    result = estimator()
             ^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/qubitization/phase_estimation.py", line 181, in _t_complexity_
    walk_cost        =  t_complexity( QubitizedWalkOperator( self._block_encoding ))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 81, in pylqt_t_complexity
    ret = _pylqt_t_complexity_for_gate_or_op(stc)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/cachetools/__init__.py", line 675, in wrapper
    v = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 67, in _pylqt_t_complexity_for_gate_or_op
    return _t_complexity_from_strategies(gate_or_op, strategies)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/t_complexity_protocol.py", line 204, in _t_complexity_from_strategies
    ret = strategy(stc)
          ^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/t_complexity_protocol.py", line 84, in _from_explicit_annotation
    result = estimator()
             ^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/qubitization/qubitized_gates.py", line 393, in _t_complexity_
    encoding_cost  =  t_complexity(self._block_encoding)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 81, in pylqt_t_complexity
    ret = _pylqt_t_complexity_for_gate_or_op(stc)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/cachetools/__init__.py", line 675, in wrapper
    v = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 67, in _pylqt_t_complexity_for_gate_or_op
    return _t_complexity_from_strategies(gate_or_op, strategies)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/t_complexity_protocol.py", line 204, in _t_complexity_from_strategies
    ret = strategy(stc)
          ^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 42, in _plyqt_from_bloq_build_call_graph
    r = pylqt_t_complexity(bloq)
        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 81, in pylqt_t_complexity
    ret = _pylqt_t_complexity_for_gate_or_op(stc)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/cachetools/__init__.py", line 675, in wrapper
    v = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 67, in _pylqt_t_complexity_for_gate_or_op
    return _t_complexity_from_strategies(gate_or_op, strategies)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/t_complexity_protocol.py", line 204, in _t_complexity_from_strategies
    ret = strategy(stc)
          ^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 42, in _plyqt_from_bloq_build_call_graph
    r = pylqt_t_complexity(bloq)
        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 81, in pylqt_t_complexity
    ret = _pylqt_t_complexity_for_gate_or_op(stc)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/cachetools/__init__.py", line 675, in wrapper
    v = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 67, in _pylqt_t_complexity_for_gate_or_op
    return _t_complexity_from_strategies(gate_or_op, strategies)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/t_complexity_protocol.py", line 204, in _t_complexity_from_strategies
    ret = strategy(stc)
          ^^^^^^^^^^^^^
  File "/Users/maxradin/Development/pyLIQTR/src/pyLIQTR/utils/resource_analysis.py", line 37, in _plyqt_from_bloq_build_call_graph
    callee_counts = get_bloq_callee_counts(bloq=stc, generalizer=cirq_to_bloqs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/resource_counting/_call_graph.py", line 118, in get_bloq_callee_counts
    return _generalize_callees(bloq.build_call_graph(ssa), generalizer)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/_infra/bloq.py", line 297, in build_call_graph
    return self.decompose_bloq().build_call_graph(ssa)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/_infra/gate_with_registers.py", line 293, in decompose_bloq
    return decompose_from_cirq_style_method(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/_cirq_to_bloq.py", line 602, in decompose_from_cirq_style_method
    return cirq_optree_to_cbloq(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/cirq_interop/_cirq_to_bloq.py", line 537, in cirq_optree_to_cbloq
    qvars_out = bb.add_d(bloq, **qvars_in)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/_infra/composite_bloq.py", line 961, in add_d
    return dict(self._add_binst(binst, in_soqs=in_soqs))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/_infra/composite_bloq.py", line 1005, in _add_binst
    self._binsts.add(binst)
  File "<attrs generated hash qualtran._infra.quantum_graph.BloqInstance>", line 2, in __hash__
  File "<attrs generated hash qualtran._infra.controlled.Controlled>", line 2, in __hash__
  File "/Users/maxradin/anaconda3/lib/python3.11/site-packages/qualtran/_infra/controlled.py", line 204, in __hash__
    return hash((self.qdtypes, self.shapes, self._cvs_tuple))
                               ^^^^^^^^^^^
  File "/Users/maxradin/anaconda3/lib/python3.11/functools.py", line 994, in __get__
    raise TypeError(msg) from None
TypeError: No '__dict__' attribute on 'CtrlSpec' instance to cache 'shapes' property.
@max-radin max-radin added the bug Something isn't working label Dec 6, 2024
@kjm538
Copy link
Collaborator

kjm538 commented Dec 16, 2024

I'll dig into this. I recently ran into a similar error so I have an idea for what the issue might be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants