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 handling of trainable_params in qs.copy #6363

Merged
merged 6 commits into from
Oct 8, 2024
Merged

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented Oct 8, 2024

Context:

  1. The existing implementation of QuantumScript.copy relies on users to explicitly pass trainable_params=None to recalculate trainable params; however, updating operations and/or measurements often makes the trainable_params list outdated.
  2. There is a bug that raises an error when explicitly passing trainable_params=None to qs.copy

Description of the Change:

  1. If a user passes trainable_params explicitly along with operations and/or measurements, we continue using the user-defined trainable_params. However, if updating operations/measurements and trainable_params is not passed, we default to recalculating for the new tape, rather than to copying over the initial tape's trainable_params attribute.
  2. We stop trying to cast the input trainable_params to a list. I think this was intended to make it possible to pass trainable_params=1, but in hindsight, this isn't valid input on in QuantumScript.__init__ and it shouldn't be valid input here, and calling list(None) was the source of the error.

[sc-75393]

@lillian542 lillian542 marked this pull request as ready for review October 8, 2024 17:28
@lillian542 lillian542 requested a review from andrijapau October 8, 2024 17:29
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.

LGTM

Copy link
Contributor

@andrijapau andrijapau left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for the quick fix! 😄

pennylane/tape/qscript.py Outdated Show resolved Hide resolved
lillian542 and others added 2 commits October 8, 2024 14:17
Co-authored-by: Andrija Paurevic <46359773+andrijapau@users.noreply.github.com>
@lillian542 lillian542 enabled auto-merge (squash) October 8, 2024 18:18
@lillian542 lillian542 disabled auto-merge October 8, 2024 18:44
@lillian542 lillian542 enabled auto-merge (squash) October 8, 2024 18:44
Copy link

codecov bot commented Oct 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.39%. Comparing base (13e3666) to head (7798e02).
Report is 333 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6363   +/-   ##
=======================================
  Coverage   99.39%   99.39%           
=======================================
  Files         446      446           
  Lines       42326    42328    +2     
=======================================
+ Hits        42071    42073    +2     
  Misses        255      255           

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

@lillian542 lillian542 merged commit 6c6b80f into master Oct 8, 2024
37 checks passed
@lillian542 lillian542 deleted the fix_tape_copy_bug branch October 8, 2024 19:48
austingmhuang pushed a commit that referenced this pull request Oct 23, 2024
**Context:**

1. The existing implementation of `QuantumScript.copy` relies on users
to explicitly pass `trainable_params=None` to recalculate trainable
params; however, updating `operations` and/or `measurements` often makes
the `trainable_params` list outdated.
2. There is a bug that raises an error when explicitly passing
`trainable_params=None` to qs.copy

**Description of the Change:**
1. If a user passes `trainable_params` explicitly along with
`operations` and/or `measurements`, we continue using the user-defined
`trainable_params`. However, if updating `operations`/`measurements` and
`trainable_params` is not passed, we default to recalculating for the
new tape, rather than to copying over the initial tape's
`trainable_params` attribute.
2. We stop trying to cast the input `trainable_params` to a list. I
think this was intended to make it possible to pass
`trainable_params=1`, but in hindsight, this isn't valid input on in
`QuantumScript.__init__` and it shouldn't be valid input here, and
calling `list(None)` was the source of the error.

[sc-75393]

---------

Co-authored-by: Andrija Paurevic <46359773+andrijapau@users.noreply.github.com>
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.

3 participants