Resource estimation with mid-circuit measurements

Hi, I am trying to estimate resources for a circuit with mid-circuit measurements. Is there a way to make this work?

import pennylane as qml

@qml.qnode(qml.device("default.qubit"))
def circuit():
    qml.H(0)
    qml.H(1)
    qml.ctrl(qml.X, control=0)(1)
    qml.measure(1, postselect=0)
    qml.H(0)
    return qml.probs(0)

circuit()
qml.estimator.estimate(circuit)()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[15], line 12
      9     return qml.probs(0)
     11 circuit()
---> 12 qml.estimator.estimate(circuit)()

File ~/efficient-multiscale-learning/.venv/lib/python3.11/site-packages/pennylane/estimator/estimate.py:317, in _resources_from_qfunc.<locals>.wrapper(*args, **kwargs)
    315 wire_manager.algo_wires = num_algo_qubits
    316 # Obtain resources in the gate_set
--> 317 compressed_res_ops_list = _ops_to_compressed_reps(q.queue)
    318 gate_counts = defaultdict(int)
    319 for cmp_rep_op in compressed_res_ops_list:

File ~/miniconda311/lib/python3.11/contextlib.py:81, in ContextDecorator.__call__.<locals>.inner(*args, **kwds)
     78 @wraps(func)
     79 def inner(*args, **kwds):
     80     with self._recreate_cm():
---> 81         return func(*args, **kwds)

File ~/efficient-multiscale-learning/.venv/lib/python3.11/site-packages/pennylane/estimator/estimate.py:527, in _ops_to_compressed_reps(ops)
    525         cmp_rep_ops.append(op.resource_rep_from_op())
    526     elif isinstance(op, Operator):
--> 527         cmp_rep_ops.append(_map_to_resource_op(op).resource_rep_from_op())
    529 return cmp_rep_ops

File ~/miniconda311/lib/python3.11/functools.py:909, in singledispatch.<locals>.wrapper(*args, **kw)
    905 if not args:
    906     raise TypeError(f'{funcname} requires at least '
    907                     '1 positional argument')
--> 909 return dispatch(args[0].__class__)(*args, **kw)

File ~/efficient-multiscale-learning/.venv/lib/python3.11/site-packages/pennylane/estimator/resource_mapping.py:66, in _map_to_resource_op(op)
     49 r"""Maps an instance of :class:`~.Operation` to its associated :class:`~.estimator.ResourceOperator`.
     50 
     51 Args:
   (...)     62         a decomposition.
     63 """
     65 if not isinstance(op, Operation):
---> 66     raise TypeError(f"Operator of type {type(op)} is not a valid operation.")
     68 if op.has_decomposition:
     69     decomp = op.decomposition()

TypeError: Operator of type <class 'pennylane.ops.mid_measure.mid_measure.MidMeasure'> is not a valid operation.
Name: pennylane
Version: 0.44.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: 
Author: 
License: 
Location: /home/felix.herbort/efficient-multiscale-learning/.venv/lib/python3.11/site-packages
Platform info:           Linux-6.1.0-39-amd64-x86_64-with-glibc2.36
Python version:          3.11.13
Numpy version:           2.3.4
Scipy version:           1.16.3
JAX version:             0.9.0
Installed devices:
- lightning.qubit (pennylane_lightning-0.44.0)
- default.clifford (pennylane-0.44.0)
- default.gaussian (pennylane-0.44.0)
- default.mixed (pennylane-0.44.0)
- default.qubit (pennylane-0.44.0)
- default.qutrit (pennylane-0.44.0)
- default.qutrit.mixed (pennylane-0.44.0)
- default.tensor (pennylane-0.44.0)
- null.qubit (pennylane-0.44.0)
- reference.qubit (pennylane-0.44.0)

Hi @RisingPhoelix , it’s good to see you back!

Unfortunately resource estimation doesn’t currently support mid-circuit measurements. For now the best alternative would be to comment this line out. Is this something you could potentially do or would this break your workflow?

Thank you for your response, @CatalinaAlbornoz .

Sure, I can also comment the line out. Since I am working on a larger project and analyzing many runs, it’s a bit inconvenient though. I will have to set up some extra argument to condition the measurement. But it’s good to know that this is indeed the best workaround :+1: