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

Fix bug where unexpected queuing occurs in qml.ctrl among other functions #6284

Closed
wants to merge 16 commits into from

Conversation

KetpuntoG
Copy link
Contributor

@KetpuntoG KetpuntoG commented Sep 19, 2024

The issue was that unwanted operators were being added when applying the qml.ctrl, qml.prod, qml.cond and qml.adjoint functions if they were located as arguments or kwargs.

To solve it I do a recursive search, for operators inside args and kwargs and I remove them.

Drawback:

This will delete it always. I can't think of a case where we want to add an operator to the queue by passing it as an argument.

Fixes #6267
[sc-73690]

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link

codecov bot commented Sep 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.70%. Comparing base (de54c54) to head (342a92c).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6284   +/-   ##
=======================================
  Coverage   99.70%   99.70%           
=======================================
  Files         444      444           
  Lines       42133    42151   +18     
=======================================
+ Hits        42008    42026   +18     
  Misses        125      125           

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

@KetpuntoG KetpuntoG changed the title [WIP] Fix bug where unexpected queuing occurs in qml.ctrl among other functions Fix bug where unexpected queuing occurs in qml.ctrl among other functions Sep 20, 2024
Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qml.cond also contains similar logic. Can you also update that to use this recursive method?

@@ -199,11 +199,29 @@ def create_controlled_op(op, control, control_values=None, work_wires=None):
return _ctrl_transform(op, control, control_values, work_wires)


def remove_from_queue_args_and_kwargs(item):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this helper method to the tape module and just call it something like recursively_remove_operators_from_queue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, is okey in tape/operation_recorder.py?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be more suitable in pennylane/queueing.py actually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use pytrees and simply do:

leaves, _ = qml.pytrees.flatten((args, kwargs), lambda obj: isinstance(obj, Operator))
_ = [qml.QueuingManager.remove(l) for l in leaves if isinstance(l, Operator)]

Given this is just two lines, I'd be worried that adding a public function adds more complexity and coupling that it solves. Duplicate code is not a bad thing when it helps keep modules simple and loosely coupled.

I'd prefer to just copy those two lines into adjoint, controlled, and TransformDispatcher._qfunc_transform.

@KetpuntoG
Copy link
Contributor Author

qml.cond also contains similar logic. Can you also update that to use this recursive method?

I tried this but it seems qml.cond only accept operators, not functions as qml.ctrl or qml.adjoint

mudit2812 and others added 6 commits October 28, 2024 11:22
**Context:**

The PennyLane CI suffers from sporadic failures due to stochasticity of
test cases. In order to save CI runtime, we have seeded many tests.
However, this approach introduces the risk of a test case passing due to
a special seed, potentially hiding bugs that could have been discovered.
Therefore, it is beneficial to periodically update the seeds to review
such tests.

**Description of the Change:**

- The test suite uses `pytest-rng` to handle seed generation. A `seed`
fixture is now available to use for every test case. If seeding is
required, please use the seed provided by `pytest-rng`

```python
def test_some_function(..., seed):
    dev = qml.device("default.qubit", seed=seed)
```

- A `local_salt` test marker has been added that allows you to modify
the seed locally for a particular test case if the generated seed
happens to make your test fail.

```python
@pytest.mark.local_salt(42)
def test_some_function(..., seed):
    dev = qml.device("default.qubit", seed=seed)
```

- All tests that uses local seeds has been updated to use the new
seeding approach.

- An `rng_salt` has been added to `pytest.ini`. This controls the seed
generation for the entire test suite. We want to periodically change
this `rng_salt` which updates all the seeds across the test suite.

- The fixture that sets the global seed for every test case has been
removed, and another fixture is added that restores the global seed
after each test in case the test modifies the global seed.

**Benefits:**

We will be able to catch bugs hidden in tests that are passing only
because of a magic seed that was set locally.

**Possible Drawbacks:**

This is a chore to maintain this seed an ensure that all tests pass with
the new salt when it is updated.

**Related Shortcut Story:**

[sc-74294]

---------

Co-authored-by: Mudit Pandey <[email protected]>
@KetpuntoG KetpuntoG changed the base branch from master to v0.39.0-rc0 October 29, 2024 18:40
@KetpuntoG KetpuntoG closed this Oct 29, 2024
KetpuntoG added a commit that referenced this pull request Oct 31, 2024
…ions (#6474)

This PR is a copy of this [other
one](#6284) pointing to
v0.39.0-rc0

[[sc-73690](https://app.shortcut.com/xanaduai/story/73690)]

---------

Co-authored-by: Astral Cai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Templates with custom queueing fail with qml.ctrl
5 participants