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

[BUG] default.tensor errors out with mid circuit measurements #6361

Closed
1 task done
albi3ro opened this issue Oct 8, 2024 · 0 comments · Fixed by #6408
Closed
1 task done

[BUG] default.tensor errors out with mid circuit measurements #6361

albi3ro opened this issue Oct 8, 2024 · 0 comments · Fixed by #6408
Labels
bug 🐛 Something isn't working

Comments

@albi3ro
Copy link
Contributor

albi3ro commented Oct 8, 2024

Expected behavior

I would expect the defer_measurements transform to be applied prior to decompose.

Actual behavior

Error with traceback below.

Additional information

The solution would simply be adding defer_measurements to the transform program before decompose.

Source code

@qml.qnode(qml.device('default.tensor', wires=2))
def circuit():
    m0 = qml.measure(0)
    qml.cond(m0, qml.RX)(0.1, 0)
    return qml.expval(qml.Z(0))

circuit()

Tracebacks

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[16], line 7
      4     qml.cond(m0, qml.RX)(0.1, 0)
      5     return qml.expval(qml.Z(0))
----> 7 circuit()

File ~/Prog/pennylane/pennylane/workflow/qnode.py:995, in QNode.__call__(self, *args, **kwargs)
    993 if qml.capture.enabled():
    994     return qml.capture.qnode_call(self, *args, **kwargs)
--> 995 return self._impl_call(*args, **kwargs)

File ~/Prog/pennylane/pennylane/workflow/qnode.py:985, in QNode._impl_call(self, *args, **kwargs)
    982     self._interface = interface
    984 try:
--> 985     res = self._execution_component(args, kwargs)
    986 finally:
    987     if old_interface == "auto":

File ~/Prog/pennylane/pennylane/workflow/qnode.py:943, in QNode._execution_component(self, args, kwargs)
    940 interface = None if self.interface == "numpy" else self.interface
    942 # pylint: disable=unexpected-keyword-arg
--> 943 res = qml.execute(
    944     (self._tape,),
    945     device=self.device,
    946     gradient_fn=gradient_fn,
    947     interface=interface,
    948     transform_program=full_transform_program,
    949     inner_transform=inner_transform_program,
    950     config=config,
    951     gradient_kwargs=gradient_kwargs,
    952     **execute_kwargs,
    953 )
    954 res = res[0]
    956 # convert result to the interface in case the qfunc has no parameters

File ~/Prog/pennylane/pennylane/workflow/execution.py:528, in execute(tapes, device, gradient_fn, interface, transform_program, inner_transform, config, grad_on_execution, gradient_kwargs, cache, cachesize, max_diff, device_vjp, mcm_config)
    526 # Exiting early if we do not need to deal with an interface boundary
    527 if no_interface_boundary_required:
--> 528     results = inner_execute(tapes)
    529     return post_processing(results)
    531 if (
    532     device_vjp
    533     and getattr(device, "short_name", "") in ("lightning.gpu", "lightning.kokkos")
    534     and interface in jpc_interfaces
    535 ):  # pragma: no cover

File ~/Prog/pennylane/pennylane/workflow/execution.py:204, in _make_inner_execute.<locals>.inner_execute(tapes, **_)
    201 if cache is not None:
    202     transform_program.add_transform(_cache_transform, cache=cache)
--> 204 transformed_tapes, transform_post_processing = transform_program(tapes)
    206 if transformed_tapes:
    207     results = device.execute(transformed_tapes, execution_config=execution_config)

File ~/Prog/pennylane/pennylane/transforms/core/transform_program.py:515, in TransformProgram.__call__(self, tapes)
    513 if self._argnums is not None and self._argnums[i] is not None:
    514     tape.trainable_params = self._argnums[i][j]
--> 515 new_tapes, fn = transform(tape, *targs, **tkwargs)
    516 execution_tapes.extend(new_tapes)
    518 fns.append(fn)

File ~/Prog/pennylane/pennylane/devices/preprocess.py:369, in decompose(tape, stopping_condition, stopping_condition_shots, skip_initial_state_prep, decomposer, max_expansion, name, error)
    366     return (tape,), null_postprocessing
    367 try:
--> 369     new_ops = [
    370         final_op
    371         for op in tape.operations[len(prep_op) :]
    372         for final_op in _operator_decomposition_gen(
    373             op,
    374             stopping_condition,
    375             decomposer=decomposer,
    376             max_expansion=max_expansion,
    377             name=name,
    378             error=error,
    379         )
    380     ]
    381 except RecursionError as e:
    382     raise error(
    383         "Reached recursion limit trying to decompose operations. "
    384         "Operator decomposition may have entered an infinite loop."
    385     ) from e

File ~/Prog/pennylane/pennylane/devices/preprocess.py:64, in _operator_decomposition_gen(op, acceptance_function, decomposer, max_expansion, current_depth, name, error)
     62 else:
     63     try:
---> 64         decomp = decomposer(op)
     65         current_depth += 1
     66     except qml.operation.DecompositionUndefinedError as e:

File ~/Prog/pennylane/pennylane/devices/preprocess.py:355, in decompose.<locals>.decomposer(op)
    354 def decomposer(op):
--> 355     return op.decomposition()

AttributeError: 'MidMeasureMP' object has no attribute 'decomposition'

System information

master.

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@albi3ro albi3ro added the bug 🐛 Something isn't working label Oct 8, 2024
austingmhuang pushed a commit that referenced this issue Oct 23, 2024
**Context:**

`default.tensor` currently errors out with an unhelpful error message
with any mid-circuit measurements. This went uncaught, and we don't know
what other devices might have faulty handling of MCMs.

**Description of the Change:**

1) Adds the `defer_measurement` transform to `default.tensor`
preprocessing
2) Raises an error if a different mcm method is requested
4) Allows `qml.device('default.tensor', shots=None)` to be provided so
it can be run with the device test suite.


**Benefits:**

Improved handling of mid circuit measurements on `default.tensor`.

**Possible Drawbacks:**



**Related GitHub Issues:**

[sc-75339] Fixes #6361
mudit2812 pushed a commit that referenced this issue Nov 11, 2024
**Context:**

`default.tensor` currently errors out with an unhelpful error message
with any mid-circuit measurements. This went uncaught, and we don't know
what other devices might have faulty handling of MCMs.

**Description of the Change:**

1) Adds the `defer_measurement` transform to `default.tensor`
preprocessing
2) Raises an error if a different mcm method is requested
4) Allows `qml.device('default.tensor', shots=None)` to be provided so
it can be run with the device test suite.


**Benefits:**

Improved handling of mid circuit measurements on `default.tensor`.

**Possible Drawbacks:**



**Related GitHub Issues:**

[sc-75339] Fixes #6361
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

Successfully merging a pull request may close this issue.

1 participant