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] Can't initialize ParticleConservingU1/2 with default init_state #5522

Closed
1 task done
dwierichs opened this issue Apr 16, 2024 · 0 comments
Closed
1 task done
Labels
bug 🐛 Something isn't working

Comments

@dwierichs
Copy link
Contributor

dwierichs commented Apr 16, 2024

Expected behavior

ParticleConservingU1 and ParticleConservingU2 can be initialized with their default kwargs.

Actual behavior

They error out with init_state=None (the default setting)

Additional information

While it may be intended that users should provide an init_state, it is not good practice to have keyword arguments with invalid defaults. None should be interpreted as a valid init_state, like tuple(0 for _ in wires) or so.
If init_state should be provided by users always, init_state should be a positional arg, not a kwarg.

Source code

qml.ParticleConservingU1(np.ones((3, 1, 2)), [2, 3])
# qml.ParticleConservingU2(np.ones((3, 3)), [2, 3]) # U2 has the same problem

Tracebacks

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 2
      1 # qml.GateFabric(np.ones((3, 1, 2)), [0, 1, 1, 0], wires=[2, 3, 0, 1])
----> 2 qml.ParticleConservingU1(np.ones((3, 1, 2)), [2, 3])

File ~/repos/pennylane/pennylane/capture/meta_type.py:151, in PLXPRMeta.__call__(cls, *args, **kwargs)
    145 def __call__(cls, *args, **kwargs):
    146     # this method is called everytime we want to create an instance of the class.
    147     # default behavior uses __new__ then __init__
    148     # when tracing is enabled, we want to
    150     if not plxpr_enabled():
--> 151         return type.__call__(cls, *args, **kwargs)
    152     # use bind to construct the class if we want class construction to add it to the jaxpr
    153     return cls._primitive_bind_call(*args, **kwargs)

File ~/repos/pennylane/pennylane/templates/layers/particle_conserving_u1.py:269, in ParticleConservingU1.__init__(self, weights, wires, init_state, id)
    264 if shape[2] != 2:
    265     raise ValueError(
    266         f"Weights tensor must have third dimension of length 2; got {shape[2]}"
    267     )
--> 269 self._hyperparameters = {"init_state": tuple(init_state)}
    271 super().__init__(weights, wires=wires, id=id)

TypeError: 'NoneType' object is not iterable

System information

pl dev

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@dwierichs dwierichs added the bug 🐛 Something isn't working label Apr 16, 2024
@dwierichs dwierichs changed the title [BUG] Can't initialize ParticleConservingU1 with default init_state [BUG] Can't initialize ParticleConservingU1/2 with default init_state Apr 16, 2024
PietropaoloFrisoni added a commit that referenced this issue Apr 19, 2024
…_state` (#5535)

**Context:** `qml.ParticleConservingU1` and `qml.ParticleConservingU2`
cannot be initialized with their default value for the `initial_state`
argument (the default value is `None`).

**Description of the Change:** If the `initial_state` argument is not
specified, it is set to a tuple of zeros, which seems a reasonable
choice for an initial state in physics. This modification occurs only if
the initial state is not specified and the information is reported in
the class's docstring.

**Benefits:** The error is no longer raised when the initial state is
not specified, and the class works with the default argument.

**Possible Drawbacks:** As mentioned above, this modification only
occurs if the initial state is not specified and should not cause any
issues.

**Related GitHub Issues:** #5522 

[sc-61431]
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

No branches or pull requests

2 participants