Skip to content

Conversation

@mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Feb 7, 2025

Context:
This PR follows up on #6838 to add support to execute circuits transformed with defer_measurements on default.qubit. Note that this PR does not integrate the transforms and device preprocessing, so the transform needs to be manually applied to the program before sending to the device for execution. That will happen in future work.

Description of the Change:

  • Update DefaultQubitInterpreter to support ctrl_transform and adjoint_transform primitives using CollectOpsandMeas. This causes control flow to get unrolled, but I'm not really sure how else to get them to work.
  • Update DefaultQubitInterpreter.interpret_operation to renormalize the state after a Projector is applied.
  • Added a minor update to qml.devices.qubit.measure.get_measurement_function so that for expvals that don't have an obs, we default to using state_diagonalizing_gates. This was needed as terminal MCM measurements get replaced by measurements on eigvals + wires.

Benefits:

Possible Drawbacks:

Related GitHub Issues:

[sc-82054]

@mudit2812 mudit2812 marked this pull request as ready for review February 19, 2025 21:56
Base automatically changed from capture-defer-measurements to master February 20, 2025 16:02
@mudit2812 mudit2812 requested a review from astralcai February 20, 2025 21:43
@mudit2812
Copy link
Contributor Author

After discussion with @lillian542, it seems like the autograph test failures are unrelated to this PR. Removing the NotImplementedError for the ctrl and adjoint transform handlers in DefaultQubitInterpreter just exposed the issue. I will open an issue and xfail the tests, with a reference to the issue in the xfail reason.

@mudit2812 mudit2812 enabled auto-merge (squash) February 21, 2025 19:35
@codecov
Copy link

codecov bot commented Feb 21, 2025

Codecov Report

Attention: Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 99.59%. Comparing base (a27193a) to head (01891d0).
Report is 340 commits behind head on master.

Files with missing lines Patch % Lines
pennylane/devices/qubit/dq_interpreter.py 93.10% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6937      +/-   ##
==========================================
- Coverage   99.60%   99.59%   -0.01%     
==========================================
  Files         481      481              
  Lines       45947    45974      +27     
==========================================
+ Hits        45764    45789      +25     
- Misses        183      185       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@mudit2812 mudit2812 merged commit 91f51b5 into master Feb 21, 2025
45 checks passed
@mudit2812 mudit2812 deleted the dq-capture-deferred branch February 21, 2025 19:37
mudit2812 added a commit that referenced this pull request Mar 4, 2025
…#6961)

**Context:**
This PR is a follow up on #6838 and #6937 to add finite shot execution
support to `default.qubit`.

**Description of the Change:**
* Update `DefaultQubitInterpreter` to handle the ability to update shots
based on the specified `postselect_mode`. The currently supported
`postselect_mode`s are `"hw-like"` and `"fill-shots"`.
* Update `DeferMeasurementsInterpreter` to not use `aux_wires`. This is
needed so that we don't need to know ahead of time which wires to use to
map MCMs. Instead, now we specify `num_wires`, which should be an
integer for the total number of available wires. When executing, this
should be known since the device requires wires to be specified.
* Because of traceable wires, there might be some sharp bits. Take a
look at the following example:
  ```python
  qml.capture.enable()
  import jax

  @qml.capture.expand_plxpr_transforms
  @partial(qml.defer_measurements, num_wires=1)
  def f(n):
      qml.measure(n)
  ```
  ```pycon
  >>> jax.make_jaxpr(f)(0)
{ lambda ; a:i64[]. let _:AbstractOperator() = CNOT[n_wires=2] a 0 in ()
}
  ```
The jaxpr is correct, but if I try to execute with `n=0`, we will
expectedly fail because we tried to create a `CNOT([0, 0])`. This is
expected, but would be nice if it could be caught at capture time rather
than runtime. I have updated the `defer_measurements` documentation
accordingly. Let me know if there are any other suggestions about how to
deal with this.
* Update `DefaultQubitInterpreter` to accept `ExecutionConfig`.

**Benefits:**

**Possible Drawbacks:**
Using `hw-like` postselection currently will not be jit friendly when
returning `qml.sample`, as the number of samples can be less than the
original number of samples non-deterministically.

**Future Work:**
* Integrate execution with the `mcm_method` and `postselect_mode`
arguments of `QNode`.

**Related GitHub Issues:**

[sc-82064]

---------

Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
Co-authored-by: albi3ro <chrissie.c.l@gmail.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
astralcai added a commit to PennyLaneAI/pennylane-lightning that referenced this pull request Mar 7, 2025
…defer_measurements`, add support for `ctrl` and `adjoint` (#1069)

**Context:**
This PR follows up on PennyLaneAI/pennylane#6838
to add support to execute circuits transformed with `defer_measurements`
on `lightning.qubit`.

This PR mirrors the same implementation as for `default.qubit`:
PennyLaneAI/pennylane#6937

**Description of the Change:**
Update `LightningInterpreter` to support `ctrl_transform` and
`adjoint_transform` primitives using `CollectOpsandMeas`.

**Related Shortcut Story:**
[sc-82056]

---------

Co-authored-by: ringo-but-quantum <github-ringo-but-quantum@xanadu.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants