Skip to content

Conversation

@albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Dec 4, 2025

Context:

Currently, our signature and docstring for transforms comes from the tape transform. If we don't have a tape transform, and just have a pass_name definition, we don't have a place to pull the signature and docstring from.

Also, our argument validation is lazy and only performed when actually applying the transform to a tape. This delays potentially useful error messages.

Description of the Change:

Adds an optional setup_inputs argument to transforms. This setup_inputs function is called before any type of dispatch, including onto a qnode, quantum function, or anything else. This allows us to standardize and validate any inputs. We can also sort arguments into being positional or keyword, which will be important for capturing and lowering the transform.

Benefits:

Improved argument validation, preprocessing, docstrings, and signature.

Possible Drawbacks:

transform is getting very cluttered with components.

Related GitHub Issues:

Example:

For current transforms, we can apply them to a qnode with incorrect arguments with no problem:

@partial(qml.transforms.merge_rotations, rtol=1e-5)
@qml.qnode(qml.device('default.qubit'))
def c():
    qml.RX(0.5, 0)
    qml.RX(0.5, 0)
    return qml.expval(qml.Z(0))

Errors will only be raised when the qnode is actually executed.

We can create new version of it with a setup_inputs:

def setup_inputs(atol=1e-8, include_gates=None):
    """A pass that does some stuff"""
    return (), {"atol":atol, "include_gates": include_gates}

merge_rotations = qml.transform(setup_inputs=setup_inputs, pass_name="merge-rotations")

And now we get an error when it is applied to a QNode:

@partial(merge_rotations, rtol=1e-5)
@qml.qnode(qml.device('default.qubit'))
def c():
    qml.RX(0.5, 0)
    qml.RX(0.5, 0)
    return qml.expval(qml.Z(0))
TypeError: setup_inputs() got an unexpected keyword argument 'rtol'

@github-actions
Copy link
Contributor

github-actions bot commented Dec 4, 2025

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.

@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.43%. Comparing base (77531f6) to head (99e202f).

Files with missing lines Patch % Lines
pennylane/transforms/core/transform_dispatcher.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8732      +/-   ##
==========================================
- Coverage   99.43%   99.43%   -0.01%     
==========================================
  Files         588      588              
  Lines       62706    62712       +6     
==========================================
+ Hits        62352    62357       +5     
- Misses        354      355       +1     

☔ 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.

>>> my_transform(circuit)
Traceback (most recent call last):
...
TypeError: setup_inputs() missing 1 required positional argument: 'a'
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible for the error message to tell us exactly which transform is getting incorrect arguments? This would make debugging easier when we have multiple transforms applied on the QNode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So this is the name of the function provided to setup_inputs. So potentially can rename something like my_transform_setup, which would add more information.

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